xref: /illumos-gate/usr/src/uts/common/io/usb/usba/hcdi.c (revision 4610e4a0)
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
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate /*
297c478bd9Sstevel@tonic-gate  * USBA: Solaris USB Architecture support
307c478bd9Sstevel@tonic-gate  *
317c478bd9Sstevel@tonic-gate  * hcdi.c contains the code for client driver callbacks.  A host controller
327c478bd9Sstevel@tonic-gate  * driver registers/unregisters with usba through usba_hcdi_register/unregister.
337c478bd9Sstevel@tonic-gate  *
347c478bd9Sstevel@tonic-gate  * When the transfer has finished, the host controller driver will call into
357c478bd9Sstevel@tonic-gate  * usba with the result.  The call is usba_hcdi_cb().
367c478bd9Sstevel@tonic-gate  *
377c478bd9Sstevel@tonic-gate  * The callback queue is maintained in FIFO order.  usba_hcdi_cb
387c478bd9Sstevel@tonic-gate  * adds to the queue, and hcdi_cb_thread takes the callbacks off the queue
397c478bd9Sstevel@tonic-gate  * and executes them.
407c478bd9Sstevel@tonic-gate  */
417c478bd9Sstevel@tonic-gate #define	USBA_FRAMEWORK
427c478bd9Sstevel@tonic-gate #include <sys/usb/usba/usba_impl.h>
437c478bd9Sstevel@tonic-gate #include <sys/usb/usba/hcdi_impl.h>
447c478bd9Sstevel@tonic-gate #include <sys/kstat.h>
457c478bd9Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate /* function prototypes, XXXX use hcdi_ prefix?	*/
487c478bd9Sstevel@tonic-gate static void usba_hcdi_create_stats(usba_hcdi_t *, int);
497c478bd9Sstevel@tonic-gate static void usba_hcdi_update_error_stats(usba_hcdi_t *, usb_cr_t);
507c478bd9Sstevel@tonic-gate static void usba_hcdi_destroy_stats(usba_hcdi_t *);
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate /* internal functions */
537c478bd9Sstevel@tonic-gate static uint_t hcdi_soft_intr(caddr_t arg1, caddr_t arg2);
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate static void hcdi_cb_thread(void *);
567c478bd9Sstevel@tonic-gate static void hcdi_shared_cb_thread(void *);
577c478bd9Sstevel@tonic-gate static void hcdi_do_cb(usba_pipe_handle_data_t *, usba_req_wrapper_t *,
587c478bd9Sstevel@tonic-gate 							usba_hcdi_t *);
597c478bd9Sstevel@tonic-gate static void hcdi_autoclearing(usba_req_wrapper_t *);
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate /* private function from USBAI */
627c478bd9Sstevel@tonic-gate void	usba_pipe_clear(usb_pipe_handle_t);
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate /* for debug messages */
65*4610e4a0Sfrits uint_t	hcdi_errmask	= (uint_t)DPRINT_MASK_ALL;
66*4610e4a0Sfrits uint_t	hcdi_errlevel	= USB_LOG_L4;
67*4610e4a0Sfrits uint_t	hcdi_instance_debug = (uint_t)-1;
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate void
707c478bd9Sstevel@tonic-gate usba_hcdi_initialization()
717c478bd9Sstevel@tonic-gate {
727c478bd9Sstevel@tonic-gate }
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate void
767c478bd9Sstevel@tonic-gate usba_hcdi_destroy()
777c478bd9Sstevel@tonic-gate {
787c478bd9Sstevel@tonic-gate }
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate /*
827c478bd9Sstevel@tonic-gate  * store hcdi structure in the dip
837c478bd9Sstevel@tonic-gate  */
847c478bd9Sstevel@tonic-gate void
857c478bd9Sstevel@tonic-gate usba_hcdi_set_hcdi(dev_info_t *dip, usba_hcdi_t *hcdi)
867c478bd9Sstevel@tonic-gate {
877c478bd9Sstevel@tonic-gate 	ddi_set_driver_private(dip, hcdi);
887c478bd9Sstevel@tonic-gate }
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate /*
927c478bd9Sstevel@tonic-gate  * retrieve hcdi structure from the dip
937c478bd9Sstevel@tonic-gate  */
947c478bd9Sstevel@tonic-gate usba_hcdi_t *
957c478bd9Sstevel@tonic-gate usba_hcdi_get_hcdi(dev_info_t *dip)
967c478bd9Sstevel@tonic-gate {
977c478bd9Sstevel@tonic-gate 	return (ddi_get_driver_private(dip));
987c478bd9Sstevel@tonic-gate }
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate /*
1017c478bd9Sstevel@tonic-gate  * Called by an	HCD to attach an instance of the driver
1027c478bd9Sstevel@tonic-gate  *	make this instance known to USBA
1037c478bd9Sstevel@tonic-gate  *	the HCD	should initialize usba_hcdi structure prior
1047c478bd9Sstevel@tonic-gate  *	to calling this	interface
1057c478bd9Sstevel@tonic-gate  */
1067c478bd9Sstevel@tonic-gate int
1077c478bd9Sstevel@tonic-gate usba_hcdi_register(usba_hcdi_register_args_t *args, uint_t flags)
1087c478bd9Sstevel@tonic-gate {
1097c478bd9Sstevel@tonic-gate 	char		*datap;
1107c478bd9Sstevel@tonic-gate 	uint_t		soft_prip;
1117c478bd9Sstevel@tonic-gate 	usba_hcdi_t	*hcdi = kmem_zalloc(sizeof (usba_hcdi_t), KM_SLEEP);
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate 	if (args->usba_hcdi_register_version != HCDI_REGISTER_VERS_0) {
1147c478bd9Sstevel@tonic-gate 		kmem_free(hcdi, sizeof (usba_hcdi_t));
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate 		return (USB_FAILURE);
1177c478bd9Sstevel@tonic-gate 	}
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate 	hcdi->hcdi_dip = args->usba_hcdi_register_dip;
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate 	/*
1227c478bd9Sstevel@tonic-gate 	 * Create a log_handle
1237c478bd9Sstevel@tonic-gate 	 */
1247c478bd9Sstevel@tonic-gate 	hcdi->hcdi_log_handle = usb_alloc_log_hdl(hcdi->hcdi_dip, NULL,
1257c478bd9Sstevel@tonic-gate 			&hcdi_errlevel, &hcdi_errmask, &hcdi_instance_debug,
1267c478bd9Sstevel@tonic-gate 			0);
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_HCDI, hcdi->hcdi_log_handle,
1297c478bd9Sstevel@tonic-gate 	    "usba_hcdi_register: %s", ddi_node_name(hcdi->hcdi_dip));
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate 	/*
1327c478bd9Sstevel@tonic-gate 	 * Initialize the mutex.  Use the iblock cookie passed in
1337c478bd9Sstevel@tonic-gate 	 * by the host controller driver.
1347c478bd9Sstevel@tonic-gate 	 */
1357c478bd9Sstevel@tonic-gate 	mutex_init(&hcdi->hcdi_mutex, NULL, MUTEX_DRIVER,
1367c478bd9Sstevel@tonic-gate 			args->usba_hcdi_register_iblock_cookie);
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate 	/* add soft interrupt */
1397c478bd9Sstevel@tonic-gate 	if (ddi_intr_add_softint(hcdi->hcdi_dip, &hcdi->hcdi_softint_hdl,
1407c478bd9Sstevel@tonic-gate 	    DDI_INTR_SOFTPRI_MAX, hcdi_soft_intr, (caddr_t)hcdi) !=
1417c478bd9Sstevel@tonic-gate 	    DDI_SUCCESS) {
1427c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(DPRINT_MASK_HCDI, hcdi->hcdi_log_handle,
1437c478bd9Sstevel@tonic-gate 		    "usba_hcd_register: add soft interrupt failed");
1447c478bd9Sstevel@tonic-gate 		mutex_destroy(&hcdi->hcdi_mutex);
1457c478bd9Sstevel@tonic-gate 		usb_free_log_hdl(hcdi->hcdi_log_handle);
1467c478bd9Sstevel@tonic-gate 		kmem_free(hcdi, sizeof (usba_hcdi_t));
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate 		return (USB_FAILURE);
1497c478bd9Sstevel@tonic-gate 	}
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate 	if (ddi_intr_get_softint_pri(hcdi->hcdi_softint_hdl, &soft_prip) !=
1527c478bd9Sstevel@tonic-gate 	    DDI_SUCCESS) {
1537c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(DPRINT_MASK_HCDI, hcdi->hcdi_log_handle,
1547c478bd9Sstevel@tonic-gate 		    "usba_hcd_register: get soft interrupt priority failed");
1557c478bd9Sstevel@tonic-gate 		(void) ddi_intr_remove_softint(hcdi->hcdi_softint_hdl);
1567c478bd9Sstevel@tonic-gate 		mutex_destroy(&hcdi->hcdi_mutex);
1577c478bd9Sstevel@tonic-gate 		usb_free_log_hdl(hcdi->hcdi_log_handle);
1587c478bd9Sstevel@tonic-gate 		kmem_free(hcdi, sizeof (usba_hcdi_t));
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate 		return (USB_FAILURE);
1617c478bd9Sstevel@tonic-gate 	}
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate 	/*
1647c478bd9Sstevel@tonic-gate 	 * Priority and iblock_cookie are one and the same
1657c478bd9Sstevel@tonic-gate 	 * (However, retaining hcdi_soft_iblock_cookie for now
1667c478bd9Sstevel@tonic-gate 	 * assigning it w/ priority. In future all iblock_cookie
1677c478bd9Sstevel@tonic-gate 	 * could just go)
1687c478bd9Sstevel@tonic-gate 	 */
1697c478bd9Sstevel@tonic-gate 	hcdi->hcdi_soft_iblock_cookie =
170abdbd06dSagiri 	    (ddi_iblock_cookie_t)(uintptr_t)soft_prip;
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate 	usba_init_list(&hcdi->hcdi_cb_queue, NULL, NULL);
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate 	hcdi->hcdi_dma_attr	= args->usba_hcdi_register_dma_attr;
1757c478bd9Sstevel@tonic-gate 	hcdi->hcdi_flags	= flags;
1767c478bd9Sstevel@tonic-gate 	hcdi->hcdi_ops		= args->usba_hcdi_register_ops;
1777c478bd9Sstevel@tonic-gate 	hcdi->hcdi_iblock_cookie = args->usba_hcdi_register_iblock_cookie;
1787c478bd9Sstevel@tonic-gate 	usba_hcdi_create_stats(hcdi, ddi_get_instance(hcdi->hcdi_dip));
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate 	hcdi->hcdi_min_xfer	= hcdi->hcdi_dma_attr->dma_attr_minxfer;
1817c478bd9Sstevel@tonic-gate 	hcdi->hcdi_min_burst_size =
1827c478bd9Sstevel@tonic-gate 		(1<<(ddi_ffs(hcdi->hcdi_dma_attr->dma_attr_burstsizes)-1));
1837c478bd9Sstevel@tonic-gate 	hcdi->hcdi_max_burst_size =
1847c478bd9Sstevel@tonic-gate 		(1<<(ddi_fls(hcdi->hcdi_dma_attr->dma_attr_burstsizes)-1));
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate 	usba_hcdi_set_hcdi(hcdi->hcdi_dip, hcdi);
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY,
1897c478bd9Sstevel@tonic-gate 	    hcdi->hcdi_dip,
1907c478bd9Sstevel@tonic-gate 	    DDI_PROP_DONTPASS, "ugen-default-binding", &datap) ==
1917c478bd9Sstevel@tonic-gate 	    DDI_PROP_SUCCESS) {
1927c478bd9Sstevel@tonic-gate 		if (strcmp(datap, "device") == 0) {
1937c478bd9Sstevel@tonic-gate 			hcdi->hcdi_ugen_default_binding =
1947c478bd9Sstevel@tonic-gate 			    USBA_UGEN_DEVICE_BINDING;
1957c478bd9Sstevel@tonic-gate 		} else if (strcmp(datap, "interface") == 0) {
1967c478bd9Sstevel@tonic-gate 			hcdi->hcdi_ugen_default_binding =
1977c478bd9Sstevel@tonic-gate 			    USBA_UGEN_INTERFACE_BINDING;
1987c478bd9Sstevel@tonic-gate 		} else {
1997c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L1(DPRINT_MASK_HCDI,
2007c478bd9Sstevel@tonic-gate 			    hcdi->hcdi_log_handle,
2017c478bd9Sstevel@tonic-gate 			    "illegal value (%s) for "
2027c478bd9Sstevel@tonic-gate 			    "ugen_default_binding property",
2037c478bd9Sstevel@tonic-gate 			    datap);
2047c478bd9Sstevel@tonic-gate 		}
2057c478bd9Sstevel@tonic-gate 		ddi_prop_free(datap);
2067c478bd9Sstevel@tonic-gate 	}
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate 	return (USB_SUCCESS);
2097c478bd9Sstevel@tonic-gate }
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate /*
2137c478bd9Sstevel@tonic-gate  * Called by an	HCD to detach an instance of the driver
2147c478bd9Sstevel@tonic-gate  */
2157c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2167c478bd9Sstevel@tonic-gate void
2177c478bd9Sstevel@tonic-gate usba_hcdi_unregister(dev_info_t *dip)
2187c478bd9Sstevel@tonic-gate {
2197c478bd9Sstevel@tonic-gate 	usba_hcdi_t *hcdi = usba_hcdi_get_hcdi(dip);
2207c478bd9Sstevel@tonic-gate 
2217c478bd9Sstevel@tonic-gate 	if (hcdi) {
2227c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L4(DPRINT_MASK_HCDI, hcdi->hcdi_log_handle,
2237c478bd9Sstevel@tonic-gate 		    "usba_hcdi_unregister: %s", ddi_node_name(dip));
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate 		usba_hcdi_set_hcdi(dip, NULL);
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate 		mutex_destroy(&hcdi->hcdi_mutex);
2287c478bd9Sstevel@tonic-gate 		usba_hcdi_destroy_stats(hcdi);
2297c478bd9Sstevel@tonic-gate 		usb_free_log_hdl(hcdi->hcdi_log_handle);
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate 		/* Destroy the soft interrupt */
2327c478bd9Sstevel@tonic-gate 		(void) ddi_intr_remove_softint(hcdi->hcdi_softint_hdl);
2337c478bd9Sstevel@tonic-gate 		kmem_free(hcdi, sizeof (usba_hcdi_t));
2347c478bd9Sstevel@tonic-gate 	}
2357c478bd9Sstevel@tonic-gate }
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate /*
2397c478bd9Sstevel@tonic-gate  * alloc usba_hcdi_ops structure
2407c478bd9Sstevel@tonic-gate  *	called from the HCD attach routine
2417c478bd9Sstevel@tonic-gate  */
2427c478bd9Sstevel@tonic-gate usba_hcdi_ops_t *
2437c478bd9Sstevel@tonic-gate usba_alloc_hcdi_ops()
2447c478bd9Sstevel@tonic-gate {
2457c478bd9Sstevel@tonic-gate 	usba_hcdi_ops_t	*usba_hcdi_ops;
2467c478bd9Sstevel@tonic-gate 
2477c478bd9Sstevel@tonic-gate 	usba_hcdi_ops = kmem_zalloc(sizeof (usba_hcdi_ops_t), KM_SLEEP);
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@tonic-gate 	return (usba_hcdi_ops);
2507c478bd9Sstevel@tonic-gate }
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate 
2537c478bd9Sstevel@tonic-gate /*
2547c478bd9Sstevel@tonic-gate  * dealloc usba_hcdi_ops structure
2557c478bd9Sstevel@tonic-gate  */
2567c478bd9Sstevel@tonic-gate void
2577c478bd9Sstevel@tonic-gate usba_free_hcdi_ops(usba_hcdi_ops_t *hcdi_ops)
2587c478bd9Sstevel@tonic-gate {
2597c478bd9Sstevel@tonic-gate 	if (hcdi_ops) {
2607c478bd9Sstevel@tonic-gate 		kmem_free(hcdi_ops, sizeof (usba_hcdi_ops_t));
2617c478bd9Sstevel@tonic-gate 	}
2627c478bd9Sstevel@tonic-gate }
2637c478bd9Sstevel@tonic-gate 
2647c478bd9Sstevel@tonic-gate 
2657c478bd9Sstevel@tonic-gate /*
2667c478bd9Sstevel@tonic-gate  * Allocate the hotplug kstats structure
2677c478bd9Sstevel@tonic-gate  */
2687c478bd9Sstevel@tonic-gate void
2697c478bd9Sstevel@tonic-gate usba_hcdi_create_stats(usba_hcdi_t *hcdi, int instance)
2707c478bd9Sstevel@tonic-gate {
2717c478bd9Sstevel@tonic-gate 	char			kstatname[KSTAT_STRLEN];
2727c478bd9Sstevel@tonic-gate 	const char		*dname = ddi_driver_name(hcdi->hcdi_dip);
2737c478bd9Sstevel@tonic-gate 	hcdi_hotplug_stats_t	*hsp;
2747c478bd9Sstevel@tonic-gate 	hcdi_error_stats_t	*esp;
2757c478bd9Sstevel@tonic-gate 
2767c478bd9Sstevel@tonic-gate 	if (HCDI_HOTPLUG_STATS(hcdi) == NULL) {
2777c478bd9Sstevel@tonic-gate 		(void) snprintf(kstatname, KSTAT_STRLEN, "%s%d,hotplug",
2787c478bd9Sstevel@tonic-gate 		    dname, instance);
2797c478bd9Sstevel@tonic-gate 		HCDI_HOTPLUG_STATS(hcdi) = kstat_create("usba", instance,
2807c478bd9Sstevel@tonic-gate 		    kstatname, "usb_hotplug", KSTAT_TYPE_NAMED,
2817c478bd9Sstevel@tonic-gate 		    sizeof (hcdi_hotplug_stats_t) / sizeof (kstat_named_t),
2827c478bd9Sstevel@tonic-gate 		    KSTAT_FLAG_PERSISTENT);
2837c478bd9Sstevel@tonic-gate 
2847c478bd9Sstevel@tonic-gate 		if (HCDI_HOTPLUG_STATS(hcdi) == NULL) {
2857c478bd9Sstevel@tonic-gate 
2867c478bd9Sstevel@tonic-gate 			return;
2877c478bd9Sstevel@tonic-gate 		}
2887c478bd9Sstevel@tonic-gate 
2897c478bd9Sstevel@tonic-gate 		hsp = HCDI_HOTPLUG_STATS_DATA(hcdi);
2907c478bd9Sstevel@tonic-gate 		kstat_named_init(&hsp->hcdi_hotplug_total_success,
2917c478bd9Sstevel@tonic-gate 		    "Total Hotplug Successes", KSTAT_DATA_UINT64);
2927c478bd9Sstevel@tonic-gate 		kstat_named_init(&hsp->hcdi_hotplug_success,
2937c478bd9Sstevel@tonic-gate 		    "Hotplug Successes", KSTAT_DATA_UINT64);
2947c478bd9Sstevel@tonic-gate 		kstat_named_init(&hsp->hcdi_hotplug_total_failure,
2957c478bd9Sstevel@tonic-gate 		    "Hotplug Total Failures", KSTAT_DATA_UINT64);
2967c478bd9Sstevel@tonic-gate 		kstat_named_init(&hsp->hcdi_hotplug_failure,
2977c478bd9Sstevel@tonic-gate 		    "Hotplug Failures", KSTAT_DATA_UINT64);
2987c478bd9Sstevel@tonic-gate 		kstat_named_init(&hsp->hcdi_device_count,
2997c478bd9Sstevel@tonic-gate 		    "Device Count", KSTAT_DATA_UINT64);
3007c478bd9Sstevel@tonic-gate 
3017c478bd9Sstevel@tonic-gate 		HCDI_HOTPLUG_STATS(hcdi)->ks_private = hcdi;
3027c478bd9Sstevel@tonic-gate 		HCDI_HOTPLUG_STATS(hcdi)->ks_update = nulldev;
3037c478bd9Sstevel@tonic-gate 		kstat_install(HCDI_HOTPLUG_STATS(hcdi));
3047c478bd9Sstevel@tonic-gate 	}
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate 	if (HCDI_ERROR_STATS(hcdi) == NULL) {
3077c478bd9Sstevel@tonic-gate 		(void) snprintf(kstatname, KSTAT_STRLEN, "%s%d,error",
3087c478bd9Sstevel@tonic-gate 		    dname, instance);
3097c478bd9Sstevel@tonic-gate 		HCDI_ERROR_STATS(hcdi) = kstat_create("usba", instance,
3107c478bd9Sstevel@tonic-gate 		    kstatname, "usb_errors", KSTAT_TYPE_NAMED,
3117c478bd9Sstevel@tonic-gate 		    sizeof (hcdi_error_stats_t) / sizeof (kstat_named_t),
3127c478bd9Sstevel@tonic-gate 		    KSTAT_FLAG_PERSISTENT);
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate 		if (HCDI_ERROR_STATS(hcdi) == NULL) {
3157c478bd9Sstevel@tonic-gate 
3167c478bd9Sstevel@tonic-gate 			return;
3177c478bd9Sstevel@tonic-gate 		}
3187c478bd9Sstevel@tonic-gate 
3197c478bd9Sstevel@tonic-gate 		esp = HCDI_ERROR_STATS_DATA(hcdi);
3207c478bd9Sstevel@tonic-gate 		kstat_named_init(&esp->cc_crc, "CRC Errors", KSTAT_DATA_UINT64);
3217c478bd9Sstevel@tonic-gate 		kstat_named_init(&esp->cc_bitstuffing,
3227c478bd9Sstevel@tonic-gate 		    "Bit Stuffing Violations", KSTAT_DATA_UINT64);
3237c478bd9Sstevel@tonic-gate 		kstat_named_init(&esp->cc_data_toggle_mm,
3247c478bd9Sstevel@tonic-gate 		    "Data Toggle PID Errors", KSTAT_DATA_UINT64);
3257c478bd9Sstevel@tonic-gate 		kstat_named_init(&esp->cc_stall,
3267c478bd9Sstevel@tonic-gate 		    "Endpoint Stalls", KSTAT_DATA_UINT64);
3277c478bd9Sstevel@tonic-gate 		kstat_named_init(&esp->cc_dev_not_resp,
3287c478bd9Sstevel@tonic-gate 		    "Device Not Responding", KSTAT_DATA_UINT64);
3297c478bd9Sstevel@tonic-gate 		kstat_named_init(&esp->cc_pid_checkfailure,
3307c478bd9Sstevel@tonic-gate 		    "PID Check Bit Errors", KSTAT_DATA_UINT64);
3317c478bd9Sstevel@tonic-gate 		kstat_named_init(&esp->cc_unexp_pid,
3327c478bd9Sstevel@tonic-gate 		    "Invalid PID Errors", KSTAT_DATA_UINT64);
3337c478bd9Sstevel@tonic-gate 		kstat_named_init(&esp->cc_data_overrun,
3347c478bd9Sstevel@tonic-gate 		    "Data Overruns", KSTAT_DATA_UINT64);
3357c478bd9Sstevel@tonic-gate 		kstat_named_init(&esp->cc_data_underrun,
3367c478bd9Sstevel@tonic-gate 		    "Data Underruns", KSTAT_DATA_UINT64);
3377c478bd9Sstevel@tonic-gate 		kstat_named_init(&esp->cc_buffer_overrun,
3387c478bd9Sstevel@tonic-gate 		    "Buffer Overruns", KSTAT_DATA_UINT64);
3397c478bd9Sstevel@tonic-gate 		kstat_named_init(&esp->cc_buffer_underrun,
3407c478bd9Sstevel@tonic-gate 		    "Buffer Underruns", KSTAT_DATA_UINT64);
3417c478bd9Sstevel@tonic-gate 		kstat_named_init(&esp->cc_timeout,
3427c478bd9Sstevel@tonic-gate 		    "Command Timed Out", KSTAT_DATA_UINT64);
3437c478bd9Sstevel@tonic-gate 		kstat_named_init(&esp->cc_not_accessed,
3447c478bd9Sstevel@tonic-gate 		    "Not Accessed By Hardware", KSTAT_DATA_UINT64);
3457c478bd9Sstevel@tonic-gate 		kstat_named_init(&esp->cc_unspecified_err,
3467c478bd9Sstevel@tonic-gate 		    "Unspecified Error", KSTAT_DATA_UINT64);
3477c478bd9Sstevel@tonic-gate #ifdef	NOTYETNEEDED
3487c478bd9Sstevel@tonic-gate 		kstat_named_init(&esp->hcdi_usb_failure,
3497c478bd9Sstevel@tonic-gate 		    "USB Failure", KSTAT_DATA_UINT64);
3507c478bd9Sstevel@tonic-gate 		kstat_named_init(&esp->hcdi_usb_no_resources,
3517c478bd9Sstevel@tonic-gate 		    "No Resources", KSTAT_DATA_UINT64);
3527c478bd9Sstevel@tonic-gate 		kstat_named_init(&esp->hcdi_usb_no_bandwidth,
3537c478bd9Sstevel@tonic-gate 		    "No Bandwidth", KSTAT_DATA_UINT64);
3547c478bd9Sstevel@tonic-gate 		kstat_named_init(&esp->hcdi_usb_pipe_reserved,
3557c478bd9Sstevel@tonic-gate 		    "Pipe Reserved", KSTAT_DATA_UINT64);
3567c478bd9Sstevel@tonic-gate 		kstat_named_init(&esp->hcdi_usb_pipe_unshareable,
3577c478bd9Sstevel@tonic-gate 		    "Pipe Unshareable", KSTAT_DATA_UINT64);
3587c478bd9Sstevel@tonic-gate 		kstat_named_init(&esp->hcdi_usb_not_supported,
3597c478bd9Sstevel@tonic-gate 		    "Function Not Supported", KSTAT_DATA_UINT64);
3607c478bd9Sstevel@tonic-gate 		kstat_named_init(&esp->hcdi_usb_pipe_error,
3617c478bd9Sstevel@tonic-gate 		    "Pipe Error", KSTAT_DATA_UINT64);
3627c478bd9Sstevel@tonic-gate 		kstat_named_init(&esp->hcdi_usb_pipe_busy,
3637c478bd9Sstevel@tonic-gate 		    "Pipe Busy", KSTAT_DATA_UINT64);
3647c478bd9Sstevel@tonic-gate #endif
3657c478bd9Sstevel@tonic-gate 
3667c478bd9Sstevel@tonic-gate 		HCDI_ERROR_STATS(hcdi)->ks_private = hcdi;
3677c478bd9Sstevel@tonic-gate 		HCDI_ERROR_STATS(hcdi)->ks_update = nulldev;
3687c478bd9Sstevel@tonic-gate 		kstat_install(HCDI_ERROR_STATS(hcdi));
3697c478bd9Sstevel@tonic-gate 	}
3707c478bd9Sstevel@tonic-gate }
3717c478bd9Sstevel@tonic-gate 
3727c478bd9Sstevel@tonic-gate 
3737c478bd9Sstevel@tonic-gate /*
3747c478bd9Sstevel@tonic-gate  * Do actual error stats
3757c478bd9Sstevel@tonic-gate  */
3767c478bd9Sstevel@tonic-gate void
3777c478bd9Sstevel@tonic-gate usba_hcdi_update_error_stats(usba_hcdi_t *hcdi, usb_cr_t completion_reason)
3787c478bd9Sstevel@tonic-gate {
3797c478bd9Sstevel@tonic-gate 	if (HCDI_ERROR_STATS(hcdi) == NULL) {
3807c478bd9Sstevel@tonic-gate 
3817c478bd9Sstevel@tonic-gate 		return;
3827c478bd9Sstevel@tonic-gate 	}
3837c478bd9Sstevel@tonic-gate 
3847c478bd9Sstevel@tonic-gate 	switch (completion_reason) {
3857c478bd9Sstevel@tonic-gate 	case USB_CR_OK:
3867c478bd9Sstevel@tonic-gate 		break;
3877c478bd9Sstevel@tonic-gate 	case USB_CR_CRC:
3887c478bd9Sstevel@tonic-gate 		HCDI_ERROR_STATS_DATA(hcdi)->cc_crc.value.ui64++;
3897c478bd9Sstevel@tonic-gate 		break;
3907c478bd9Sstevel@tonic-gate 	case USB_CR_BITSTUFFING:
3917c478bd9Sstevel@tonic-gate 		HCDI_ERROR_STATS_DATA(hcdi)->cc_bitstuffing.value.ui64++;
3927c478bd9Sstevel@tonic-gate 		break;
3937c478bd9Sstevel@tonic-gate 	case USB_CR_DATA_TOGGLE_MM:
3947c478bd9Sstevel@tonic-gate 		HCDI_ERROR_STATS_DATA(hcdi)->cc_data_toggle_mm.value.ui64++;
3957c478bd9Sstevel@tonic-gate 		break;
3967c478bd9Sstevel@tonic-gate 	case USB_CR_STALL:
3977c478bd9Sstevel@tonic-gate 		HCDI_ERROR_STATS_DATA(hcdi)->cc_stall.value.ui64++;
3987c478bd9Sstevel@tonic-gate 		break;
3997c478bd9Sstevel@tonic-gate 	case USB_CR_DEV_NOT_RESP:
4007c478bd9Sstevel@tonic-gate 		HCDI_ERROR_STATS_DATA(hcdi)->cc_dev_not_resp.value.ui64++;
4017c478bd9Sstevel@tonic-gate 		break;
4027c478bd9Sstevel@tonic-gate 	case USB_CR_PID_CHECKFAILURE:
4037c478bd9Sstevel@tonic-gate 		HCDI_ERROR_STATS_DATA(hcdi)->cc_pid_checkfailure.value.ui64++;
4047c478bd9Sstevel@tonic-gate 		break;
4057c478bd9Sstevel@tonic-gate 	case USB_CR_UNEXP_PID:
4067c478bd9Sstevel@tonic-gate 		HCDI_ERROR_STATS_DATA(hcdi)->cc_unexp_pid.value.ui64++;
4077c478bd9Sstevel@tonic-gate 		break;
4087c478bd9Sstevel@tonic-gate 	case USB_CR_DATA_OVERRUN:
4097c478bd9Sstevel@tonic-gate 		HCDI_ERROR_STATS_DATA(hcdi)->cc_data_overrun.value.ui64++;
4107c478bd9Sstevel@tonic-gate 		break;
4117c478bd9Sstevel@tonic-gate 	case USB_CR_DATA_UNDERRUN:
4127c478bd9Sstevel@tonic-gate 		HCDI_ERROR_STATS_DATA(hcdi)->cc_data_underrun.value.ui64++;
4137c478bd9Sstevel@tonic-gate 		break;
4147c478bd9Sstevel@tonic-gate 	case USB_CR_BUFFER_OVERRUN:
4157c478bd9Sstevel@tonic-gate 		HCDI_ERROR_STATS_DATA(hcdi)->cc_buffer_overrun.value.ui64++;
4167c478bd9Sstevel@tonic-gate 		break;
4177c478bd9Sstevel@tonic-gate 	case USB_CR_BUFFER_UNDERRUN:
4187c478bd9Sstevel@tonic-gate 		HCDI_ERROR_STATS_DATA(hcdi)->cc_buffer_underrun.value.ui64++;
4197c478bd9Sstevel@tonic-gate 		break;
4207c478bd9Sstevel@tonic-gate 	case USB_CR_TIMEOUT:
4217c478bd9Sstevel@tonic-gate 		HCDI_ERROR_STATS_DATA(hcdi)->cc_timeout.value.ui64++;
4227c478bd9Sstevel@tonic-gate 		break;
4237c478bd9Sstevel@tonic-gate 	case USB_CR_NOT_ACCESSED:
4247c478bd9Sstevel@tonic-gate 		HCDI_ERROR_STATS_DATA(hcdi)->cc_not_accessed.value.ui64++;
4257c478bd9Sstevel@tonic-gate 		break;
4267c478bd9Sstevel@tonic-gate 	case USB_CR_NO_RESOURCES:
4277c478bd9Sstevel@tonic-gate 		HCDI_ERROR_STATS_DATA(hcdi)->cc_no_resources.value.ui64++;
4287c478bd9Sstevel@tonic-gate 		break;
4297c478bd9Sstevel@tonic-gate 	case USB_CR_UNSPECIFIED_ERR:
4307c478bd9Sstevel@tonic-gate 		HCDI_ERROR_STATS_DATA(hcdi)->cc_unspecified_err.value.ui64++;
4317c478bd9Sstevel@tonic-gate 		break;
4327c478bd9Sstevel@tonic-gate 	case USB_CR_STOPPED_POLLING:
4337c478bd9Sstevel@tonic-gate 		HCDI_ERROR_STATS_DATA(hcdi)->cc_stopped_polling.value.ui64++;
4347c478bd9Sstevel@tonic-gate 		break;
4357c478bd9Sstevel@tonic-gate 	case USB_CR_PIPE_CLOSING:
4367c478bd9Sstevel@tonic-gate 		HCDI_ERROR_STATS_DATA(hcdi)->cc_pipe_closing.value.ui64++;
4377c478bd9Sstevel@tonic-gate 		break;
4387c478bd9Sstevel@tonic-gate 	case USB_CR_PIPE_RESET:
4397c478bd9Sstevel@tonic-gate 		HCDI_ERROR_STATS_DATA(hcdi)->cc_pipe_reset.value.ui64++;
4407c478bd9Sstevel@tonic-gate 		break;
4417c478bd9Sstevel@tonic-gate 	case USB_CR_NOT_SUPPORTED:
4427c478bd9Sstevel@tonic-gate 		HCDI_ERROR_STATS_DATA(hcdi)->cc_not_supported.value.ui64++;
4437c478bd9Sstevel@tonic-gate 		break;
4447c478bd9Sstevel@tonic-gate 	case USB_CR_FLUSHED:
4457c478bd9Sstevel@tonic-gate 		HCDI_ERROR_STATS_DATA(hcdi)->cc_flushed.value.ui64++;
4467c478bd9Sstevel@tonic-gate 		break;
4477c478bd9Sstevel@tonic-gate 	default:
4487c478bd9Sstevel@tonic-gate 		break;
4497c478bd9Sstevel@tonic-gate 	}
4507c478bd9Sstevel@tonic-gate }
4517c478bd9Sstevel@tonic-gate 
4527c478bd9Sstevel@tonic-gate 
4537c478bd9Sstevel@tonic-gate /*
4547c478bd9Sstevel@tonic-gate  * Destroy the hotplug kstats structure
4557c478bd9Sstevel@tonic-gate  */
4567c478bd9Sstevel@tonic-gate static void
4577c478bd9Sstevel@tonic-gate usba_hcdi_destroy_stats(usba_hcdi_t *hcdi)
4587c478bd9Sstevel@tonic-gate {
4597c478bd9Sstevel@tonic-gate 	if (HCDI_HOTPLUG_STATS(hcdi)) {
4607c478bd9Sstevel@tonic-gate 		kstat_delete(HCDI_HOTPLUG_STATS(hcdi));
4617c478bd9Sstevel@tonic-gate 		HCDI_HOTPLUG_STATS(hcdi) = NULL;
4627c478bd9Sstevel@tonic-gate 	}
4637c478bd9Sstevel@tonic-gate 
4647c478bd9Sstevel@tonic-gate 	if (HCDI_ERROR_STATS(hcdi)) {
4657c478bd9Sstevel@tonic-gate 		kstat_delete(HCDI_ERROR_STATS(hcdi));
4667c478bd9Sstevel@tonic-gate 		HCDI_ERROR_STATS(hcdi) = NULL;
4677c478bd9Sstevel@tonic-gate 	}
4687c478bd9Sstevel@tonic-gate }
4697c478bd9Sstevel@tonic-gate 
4707c478bd9Sstevel@tonic-gate 
4717c478bd9Sstevel@tonic-gate /*
4727c478bd9Sstevel@tonic-gate  * HCD callback handling
4737c478bd9Sstevel@tonic-gate  */
4747c478bd9Sstevel@tonic-gate void
4757c478bd9Sstevel@tonic-gate usba_hcdi_cb(usba_pipe_handle_data_t *ph_data,
4767c478bd9Sstevel@tonic-gate 	usb_opaque_t	req,
4777c478bd9Sstevel@tonic-gate 	usb_cr_t	completion_reason)
4787c478bd9Sstevel@tonic-gate {
4797c478bd9Sstevel@tonic-gate 
4807c478bd9Sstevel@tonic-gate 	usba_device_t		*usba_device = ph_data->p_usba_device;
4817c478bd9Sstevel@tonic-gate 	usba_hcdi_t		*hcdi =	usba_hcdi_get_hcdi(
4827c478bd9Sstevel@tonic-gate 					usba_device->usb_root_hub_dip);
4837c478bd9Sstevel@tonic-gate 	usba_req_wrapper_t	*req_wrp = USBA_REQ2WRP(req);
4847c478bd9Sstevel@tonic-gate 	usb_ep_descr_t		*eptd = &ph_data->p_ep;
4857c478bd9Sstevel@tonic-gate 
4867c478bd9Sstevel@tonic-gate 	mutex_enter(&ph_data->p_mutex);
4877c478bd9Sstevel@tonic-gate 
4887c478bd9Sstevel@tonic-gate #ifdef DEBUG
4897c478bd9Sstevel@tonic-gate 	mutex_enter(&ph_data->p_ph_impl->usba_ph_mutex);
4907c478bd9Sstevel@tonic-gate 
4917c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_HCDI, hcdi->hcdi_log_handle,
4927c478bd9Sstevel@tonic-gate 	    "usba_hcdi_cb: "
4937c478bd9Sstevel@tonic-gate 	    "ph_data=0x%p req=0x%p state=%d ref=%d cnt=%d cr=%d",
4947c478bd9Sstevel@tonic-gate 	    ph_data, req, ph_data->p_ph_impl->usba_ph_state,
4957c478bd9Sstevel@tonic-gate 	    ph_data->p_ph_impl->usba_ph_ref_count, ph_data->p_req_count,
4967c478bd9Sstevel@tonic-gate 	    completion_reason);
4977c478bd9Sstevel@tonic-gate 
4987c478bd9Sstevel@tonic-gate 	mutex_exit(&ph_data->p_ph_impl->usba_ph_mutex);
4997c478bd9Sstevel@tonic-gate #endif
5007c478bd9Sstevel@tonic-gate 
5017c478bd9Sstevel@tonic-gate 	/* Set the completion reason */
5027c478bd9Sstevel@tonic-gate 	switch (eptd->bmAttributes & USB_EP_ATTR_MASK) {
5037c478bd9Sstevel@tonic-gate 	case USB_EP_ATTR_CONTROL:
5047c478bd9Sstevel@tonic-gate 		((usb_ctrl_req_t *)req)->
5057c478bd9Sstevel@tonic-gate 		    ctrl_completion_reason = completion_reason;
5067c478bd9Sstevel@tonic-gate 		break;
5077c478bd9Sstevel@tonic-gate 	case USB_EP_ATTR_BULK:
5087c478bd9Sstevel@tonic-gate 		((usb_bulk_req_t *)req)->
5097c478bd9Sstevel@tonic-gate 		    bulk_completion_reason = completion_reason;
5107c478bd9Sstevel@tonic-gate 		break;
5117c478bd9Sstevel@tonic-gate 	case USB_EP_ATTR_INTR:
5127c478bd9Sstevel@tonic-gate 		((usb_intr_req_t *)req)->
5137c478bd9Sstevel@tonic-gate 		    intr_completion_reason = completion_reason;
5147c478bd9Sstevel@tonic-gate 		break;
5157c478bd9Sstevel@tonic-gate 	case USB_EP_ATTR_ISOCH:
5167c478bd9Sstevel@tonic-gate 		((usb_isoc_req_t *)req)->
5177c478bd9Sstevel@tonic-gate 		    isoc_completion_reason = completion_reason;
5187c478bd9Sstevel@tonic-gate 		break;
5197c478bd9Sstevel@tonic-gate 	}
5207c478bd9Sstevel@tonic-gate 
5217c478bd9Sstevel@tonic-gate 	/*
5227c478bd9Sstevel@tonic-gate 	 * exception callbacks will still go thru a taskq thread
5237c478bd9Sstevel@tonic-gate 	 * but should occur after the soft interrupt callback
5247c478bd9Sstevel@tonic-gate 	 * By design of periodic pipes, polling will stop on any
5257c478bd9Sstevel@tonic-gate 	 * exception
5267c478bd9Sstevel@tonic-gate 	 */
5277c478bd9Sstevel@tonic-gate 	if ((ph_data->p_spec_flag & USBA_PH_FLAG_USE_SOFT_INTR) &&
5287c478bd9Sstevel@tonic-gate 	    (completion_reason == USB_CR_OK)) {
5297c478bd9Sstevel@tonic-gate 		ph_data->p_soft_intr++;
5307c478bd9Sstevel@tonic-gate 		mutex_exit(&ph_data->p_mutex);
5317c478bd9Sstevel@tonic-gate 
5327c478bd9Sstevel@tonic-gate 		usba_add_to_list(&hcdi->hcdi_cb_queue, &req_wrp->wr_queue);
5337c478bd9Sstevel@tonic-gate 
5347c478bd9Sstevel@tonic-gate 		if (ddi_intr_trigger_softint(hcdi->hcdi_softint_hdl, NULL) !=
5357c478bd9Sstevel@tonic-gate 		    DDI_SUCCESS)
5367c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L2(DPRINT_MASK_HCDI, hcdi->hcdi_log_handle,
5377c478bd9Sstevel@tonic-gate 			    "usba_hcdi_cb: ddi_intr_trigger_softint  failed");
5387c478bd9Sstevel@tonic-gate 
5397c478bd9Sstevel@tonic-gate 		return;
5407c478bd9Sstevel@tonic-gate 	}
5417c478bd9Sstevel@tonic-gate 
5427c478bd9Sstevel@tonic-gate 	/*
5437c478bd9Sstevel@tonic-gate 	 * USBA_PH_FLAG_TQ_SHARE is for bulk and intr requests,
5447c478bd9Sstevel@tonic-gate 	 * USBA_PH_FLAG_USE_SOFT_INTR is only for isoch,
5457c478bd9Sstevel@tonic-gate 	 * so there are no conflicts.
5467c478bd9Sstevel@tonic-gate 	 */
5477c478bd9Sstevel@tonic-gate 	if (ph_data->p_spec_flag & USBA_PH_FLAG_TQ_SHARE) {
5487c478bd9Sstevel@tonic-gate 		int iface;
5497c478bd9Sstevel@tonic-gate 
5507c478bd9Sstevel@tonic-gate 		mutex_exit(&ph_data->p_mutex);
5517c478bd9Sstevel@tonic-gate 		iface = usb_get_if_number(ph_data->p_dip);
5527c478bd9Sstevel@tonic-gate 		if (iface < 0) {
5537c478bd9Sstevel@tonic-gate 			/* we own the device, use the first taskq */
5547c478bd9Sstevel@tonic-gate 			iface = 0;
5557c478bd9Sstevel@tonic-gate 		}
5567c478bd9Sstevel@tonic-gate 		if (taskq_dispatch(usba_device->usb_shared_taskq[iface],
5577c478bd9Sstevel@tonic-gate 		    hcdi_shared_cb_thread, req_wrp, TQ_NOSLEEP) ==
5587c478bd9Sstevel@tonic-gate 		    NULL) {
5597c478bd9Sstevel@tonic-gate 			usba_req_exc_cb(req_wrp,
5607c478bd9Sstevel@tonic-gate 			    USB_CR_NO_RESOURCES, USB_CB_ASYNC_REQ_FAILED);
5617c478bd9Sstevel@tonic-gate 		}
5627c478bd9Sstevel@tonic-gate 
5637c478bd9Sstevel@tonic-gate 		return;
5647c478bd9Sstevel@tonic-gate 	}
5657c478bd9Sstevel@tonic-gate 
5667c478bd9Sstevel@tonic-gate 	/* Add the callback to the pipehandles callback list */
5677c478bd9Sstevel@tonic-gate 	usba_add_to_list(&ph_data->p_cb_queue, &req_wrp->wr_queue);
5687c478bd9Sstevel@tonic-gate 
5697c478bd9Sstevel@tonic-gate 	/* only dispatch if there is no thread running */
5707c478bd9Sstevel@tonic-gate 	if (ph_data->p_thread_id == 0) {
5717c478bd9Sstevel@tonic-gate 		if (usba_async_ph_req(ph_data, hcdi_cb_thread,
5727c478bd9Sstevel@tonic-gate 		    ph_data, USB_FLAGS_NOSLEEP) != USB_SUCCESS) {
5737c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L2(DPRINT_MASK_HCDI, hcdi->hcdi_log_handle,
5747c478bd9Sstevel@tonic-gate 			    "usba_hcdi_cb: taskq_dispatch failed");
5757c478bd9Sstevel@tonic-gate 			if (usba_rm_from_list(&ph_data->p_cb_queue,
5767c478bd9Sstevel@tonic-gate 			    &req_wrp->wr_queue) == USB_SUCCESS) {
5777c478bd9Sstevel@tonic-gate 				mutex_exit(&ph_data->p_mutex);
5787c478bd9Sstevel@tonic-gate 				usba_req_exc_cb(req_wrp,
5797c478bd9Sstevel@tonic-gate 				    USB_CR_NO_RESOURCES,
5807c478bd9Sstevel@tonic-gate 				    USB_CB_ASYNC_REQ_FAILED);
5817c478bd9Sstevel@tonic-gate 
5827c478bd9Sstevel@tonic-gate 				return;
5837c478bd9Sstevel@tonic-gate 			}
5847c478bd9Sstevel@tonic-gate 		} else {
5857c478bd9Sstevel@tonic-gate 			ph_data->p_thread_id = (kthread_t *)1;
5867c478bd9Sstevel@tonic-gate 		}
5877c478bd9Sstevel@tonic-gate 	}
5887c478bd9Sstevel@tonic-gate 	mutex_exit(&ph_data->p_mutex);
5897c478bd9Sstevel@tonic-gate }
5907c478bd9Sstevel@tonic-gate 
5917c478bd9Sstevel@tonic-gate 
5927c478bd9Sstevel@tonic-gate /*
5937c478bd9Sstevel@tonic-gate  * thread to perform the callbacks
5947c478bd9Sstevel@tonic-gate  */
5957c478bd9Sstevel@tonic-gate static void
5967c478bd9Sstevel@tonic-gate hcdi_cb_thread(void *arg)
5977c478bd9Sstevel@tonic-gate {
5987c478bd9Sstevel@tonic-gate 	usba_pipe_handle_data_t	*ph_data =
5997c478bd9Sstevel@tonic-gate 				(usba_pipe_handle_data_t *)arg;
6007c478bd9Sstevel@tonic-gate 	usba_ph_impl_t		*ph_impl = ph_data->p_ph_impl;
6017c478bd9Sstevel@tonic-gate 	usba_hcdi_t		*hcdi = usba_hcdi_get_hcdi(ph_data->
6027c478bd9Sstevel@tonic-gate 				p_usba_device->usb_root_hub_dip);
6037c478bd9Sstevel@tonic-gate 	usba_req_wrapper_t	*req_wrp;
6047c478bd9Sstevel@tonic-gate 
6057c478bd9Sstevel@tonic-gate 	mutex_enter(&ph_data->p_mutex);
6067c478bd9Sstevel@tonic-gate 	ASSERT(ph_data->p_thread_id == (kthread_t *)1);
6077c478bd9Sstevel@tonic-gate 	ph_data->p_thread_id = curthread;
6087c478bd9Sstevel@tonic-gate 
6097c478bd9Sstevel@tonic-gate 	/*
6107c478bd9Sstevel@tonic-gate 	 * hold the ph_data. we can't use usba_hold_ph_data() since
6117c478bd9Sstevel@tonic-gate 	 * it will return NULL if we are closing the pipe which would
6127c478bd9Sstevel@tonic-gate 	 * then leave all requests stuck in the cb_queue
6137c478bd9Sstevel@tonic-gate 	 */
6147c478bd9Sstevel@tonic-gate 	mutex_enter(&ph_impl->usba_ph_mutex);
6157c478bd9Sstevel@tonic-gate 	ph_impl->usba_ph_ref_count++;
6167c478bd9Sstevel@tonic-gate 
6177c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_HCDI, hcdi->hcdi_log_handle,
6187c478bd9Sstevel@tonic-gate 	    "hcdi_cb_thread: ph_data=0x%p ref=%d", ph_data,
6197c478bd9Sstevel@tonic-gate 	    ph_impl->usba_ph_ref_count);
6207c478bd9Sstevel@tonic-gate 
6217c478bd9Sstevel@tonic-gate 	mutex_exit(&ph_impl->usba_ph_mutex);
6227c478bd9Sstevel@tonic-gate 
6237c478bd9Sstevel@tonic-gate 	/*
6247c478bd9Sstevel@tonic-gate 	 * wait till soft interrupt callbacks are taken care of
6257c478bd9Sstevel@tonic-gate 	 */
6267c478bd9Sstevel@tonic-gate 	while (ph_data->p_soft_intr) {
6277c478bd9Sstevel@tonic-gate 		mutex_exit(&ph_data->p_mutex);
6287c478bd9Sstevel@tonic-gate 		delay(1);
6297c478bd9Sstevel@tonic-gate 		mutex_enter(&ph_data->p_mutex);
6307c478bd9Sstevel@tonic-gate 	}
6317c478bd9Sstevel@tonic-gate 
6327c478bd9Sstevel@tonic-gate 	while ((req_wrp = (usba_req_wrapper_t *)
6337c478bd9Sstevel@tonic-gate 	    usba_rm_first_pvt_from_list(&ph_data->p_cb_queue)) != NULL) {
6347c478bd9Sstevel@tonic-gate 		hcdi_do_cb(ph_data, req_wrp, hcdi);
6357c478bd9Sstevel@tonic-gate 	}
6367c478bd9Sstevel@tonic-gate 
6377c478bd9Sstevel@tonic-gate 	ph_data->p_thread_id = 0;
6387c478bd9Sstevel@tonic-gate 	mutex_exit(&ph_data->p_mutex);
6397c478bd9Sstevel@tonic-gate 
6407c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_HCDI, hcdi->hcdi_log_handle,
6417c478bd9Sstevel@tonic-gate 	    "hcdi_cb_thread done: ph_data=0x%p", ph_data);
6427c478bd9Sstevel@tonic-gate 
6437c478bd9Sstevel@tonic-gate 	usba_release_ph_data(ph_impl);
6447c478bd9Sstevel@tonic-gate }
6457c478bd9Sstevel@tonic-gate 
6467c478bd9Sstevel@tonic-gate 
6477c478bd9Sstevel@tonic-gate static void
6487c478bd9Sstevel@tonic-gate hcdi_do_cb(usba_pipe_handle_data_t *ph_data, usba_req_wrapper_t *req_wrp,
6497c478bd9Sstevel@tonic-gate     usba_hcdi_t *hcdi)
6507c478bd9Sstevel@tonic-gate {
6517c478bd9Sstevel@tonic-gate 	usb_cr_t		completion_reason;
6527c478bd9Sstevel@tonic-gate 	usb_req_attrs_t		attrs = req_wrp->wr_attrs;
6537c478bd9Sstevel@tonic-gate 
6547c478bd9Sstevel@tonic-gate 	switch (req_wrp->wr_ph_data->p_ep.bmAttributes &
6557c478bd9Sstevel@tonic-gate 	    USB_EP_ATTR_MASK) {
6567c478bd9Sstevel@tonic-gate 	case USB_EP_ATTR_CONTROL:
6577c478bd9Sstevel@tonic-gate 		completion_reason =
6587c478bd9Sstevel@tonic-gate 		    USBA_WRP2CTRL_REQ(req_wrp)->ctrl_completion_reason;
6597c478bd9Sstevel@tonic-gate 		break;
6607c478bd9Sstevel@tonic-gate 	case USB_EP_ATTR_INTR:
6617c478bd9Sstevel@tonic-gate 		completion_reason =
6627c478bd9Sstevel@tonic-gate 		    USBA_WRP2INTR_REQ(req_wrp)->intr_completion_reason;
6637c478bd9Sstevel@tonic-gate 		break;
6647c478bd9Sstevel@tonic-gate 	case USB_EP_ATTR_BULK:
6657c478bd9Sstevel@tonic-gate 		completion_reason =
6667c478bd9Sstevel@tonic-gate 		    USBA_WRP2BULK_REQ(req_wrp)->bulk_completion_reason;
6677c478bd9Sstevel@tonic-gate 		break;
6687c478bd9Sstevel@tonic-gate 	case USB_EP_ATTR_ISOCH:
6697c478bd9Sstevel@tonic-gate 		completion_reason =
6707c478bd9Sstevel@tonic-gate 		    USBA_WRP2ISOC_REQ(req_wrp)->isoc_completion_reason;
6717c478bd9Sstevel@tonic-gate 		break;
6727c478bd9Sstevel@tonic-gate 	}
6737c478bd9Sstevel@tonic-gate 	req_wrp->wr_cr = completion_reason;
6747c478bd9Sstevel@tonic-gate 
6757c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_HCDI, hcdi->hcdi_log_handle,
6767c478bd9Sstevel@tonic-gate 	    "hcdi_do_cb: wrp=0x%p cr=0x%x", req_wrp, completion_reason);
6777c478bd9Sstevel@tonic-gate 
6787c478bd9Sstevel@tonic-gate 	/*
6797c478bd9Sstevel@tonic-gate 	 * Normal callbacks:
6807c478bd9Sstevel@tonic-gate 	 */
6817c478bd9Sstevel@tonic-gate 	if (completion_reason == USB_CR_OK) {
6827c478bd9Sstevel@tonic-gate 		mutex_exit(&ph_data->p_mutex);
6837c478bd9Sstevel@tonic-gate 		usba_req_normal_cb(req_wrp);
6847c478bd9Sstevel@tonic-gate 		mutex_enter(&ph_data->p_mutex);
6857c478bd9Sstevel@tonic-gate 	} else {
6867c478bd9Sstevel@tonic-gate 		usb_pipe_state_t pipe_state;
6877c478bd9Sstevel@tonic-gate 
6887c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L4(DPRINT_MASK_HCDI, hcdi->hcdi_log_handle,
6897c478bd9Sstevel@tonic-gate 		    "exception callback handling: attrs=0x%x", attrs);
6907c478bd9Sstevel@tonic-gate 
6917c478bd9Sstevel@tonic-gate 		/*
6927c478bd9Sstevel@tonic-gate 		 * In exception callback handling, if we were
6937c478bd9Sstevel@tonic-gate 		 * not able to clear stall, we need to modify
6947c478bd9Sstevel@tonic-gate 		 * pipe state. Also if auto-clearing is not set
6957c478bd9Sstevel@tonic-gate 		 * pipe state needs to be modified.
6967c478bd9Sstevel@tonic-gate 		 */
6977c478bd9Sstevel@tonic-gate 		pipe_state = usba_get_ph_state(ph_data);
6987c478bd9Sstevel@tonic-gate 
6997c478bd9Sstevel@tonic-gate 		if (!USBA_PIPE_CLOSING(pipe_state)) {
7007c478bd9Sstevel@tonic-gate 			switch (completion_reason) {
7017c478bd9Sstevel@tonic-gate 			case USB_CR_STOPPED_POLLING:
7027c478bd9Sstevel@tonic-gate 				if (pipe_state ==
7037c478bd9Sstevel@tonic-gate 				    USB_PIPE_STATE_ACTIVE) {
7047c478bd9Sstevel@tonic-gate 					usba_pipe_new_state(ph_data,
7057c478bd9Sstevel@tonic-gate 					    USB_PIPE_STATE_IDLE);
7067c478bd9Sstevel@tonic-gate 				}
7077c478bd9Sstevel@tonic-gate 				break;
7087c478bd9Sstevel@tonic-gate 			case USB_CR_NOT_SUPPORTED:
7097c478bd9Sstevel@tonic-gate 				usba_pipe_new_state(ph_data,
7107c478bd9Sstevel@tonic-gate 				    USB_PIPE_STATE_IDLE);
7117c478bd9Sstevel@tonic-gate 				break;
7127c478bd9Sstevel@tonic-gate 			case USB_CR_PIPE_RESET:
7137c478bd9Sstevel@tonic-gate 			case USB_CR_FLUSHED:
7147c478bd9Sstevel@tonic-gate 				break;
7157c478bd9Sstevel@tonic-gate 			default:
7167c478bd9Sstevel@tonic-gate 				usba_pipe_new_state(ph_data,
7177c478bd9Sstevel@tonic-gate 				    USB_PIPE_STATE_ERROR);
7187c478bd9Sstevel@tonic-gate 				break;
7197c478bd9Sstevel@tonic-gate 			}
7207c478bd9Sstevel@tonic-gate 		}
7217c478bd9Sstevel@tonic-gate 
7227c478bd9Sstevel@tonic-gate 		pipe_state = usba_get_ph_state(ph_data);
7237c478bd9Sstevel@tonic-gate 
7247c478bd9Sstevel@tonic-gate 		mutex_exit(&ph_data->p_mutex);
7257c478bd9Sstevel@tonic-gate 		if (attrs & USB_ATTRS_PIPE_RESET) {
7267c478bd9Sstevel@tonic-gate 			if ((completion_reason != USB_CR_PIPE_RESET) &&
7277c478bd9Sstevel@tonic-gate 			    (pipe_state == USB_PIPE_STATE_ERROR)) {
7287c478bd9Sstevel@tonic-gate 
7297c478bd9Sstevel@tonic-gate 				hcdi_autoclearing(req_wrp);
7307c478bd9Sstevel@tonic-gate 			}
7317c478bd9Sstevel@tonic-gate 		}
7327c478bd9Sstevel@tonic-gate 
7337c478bd9Sstevel@tonic-gate 		usba_req_exc_cb(req_wrp, 0, 0);
7347c478bd9Sstevel@tonic-gate 		mutex_enter(&ph_data->p_mutex);
7357c478bd9Sstevel@tonic-gate 	}
7367c478bd9Sstevel@tonic-gate 
7377c478bd9Sstevel@tonic-gate 	/* Update the hcdi error kstats */
7387c478bd9Sstevel@tonic-gate 	if (completion_reason) {
7397c478bd9Sstevel@tonic-gate 		mutex_enter(&hcdi->hcdi_mutex);
7407c478bd9Sstevel@tonic-gate 		usba_hcdi_update_error_stats(hcdi, completion_reason);
7417c478bd9Sstevel@tonic-gate 		mutex_exit(&hcdi->hcdi_mutex);
7427c478bd9Sstevel@tonic-gate 	}
7437c478bd9Sstevel@tonic-gate 
7447c478bd9Sstevel@tonic-gate 	/*
7457c478bd9Sstevel@tonic-gate 	 * Once the callback is finished, release the pipe handle
7467c478bd9Sstevel@tonic-gate 	 * we start the next request first to avoid that the
7477c478bd9Sstevel@tonic-gate 	 * pipe gets closed while starting the next request
7487c478bd9Sstevel@tonic-gate 	 */
7497c478bd9Sstevel@tonic-gate 	mutex_exit(&ph_data->p_mutex);
7507c478bd9Sstevel@tonic-gate 	usba_start_next_req(ph_data);
7517c478bd9Sstevel@tonic-gate 
7527c478bd9Sstevel@tonic-gate 	mutex_enter(&ph_data->p_mutex);
7537c478bd9Sstevel@tonic-gate }
7547c478bd9Sstevel@tonic-gate 
7557c478bd9Sstevel@tonic-gate 
7567c478bd9Sstevel@tonic-gate /*
7577c478bd9Sstevel@tonic-gate  * thread to perform callbacks on the shared queue
7587c478bd9Sstevel@tonic-gate  */
7597c478bd9Sstevel@tonic-gate static void
7607c478bd9Sstevel@tonic-gate hcdi_shared_cb_thread(void *arg)
7617c478bd9Sstevel@tonic-gate {
7627c478bd9Sstevel@tonic-gate 	usba_req_wrapper_t *req_wrp = (usba_req_wrapper_t *)arg;
7637c478bd9Sstevel@tonic-gate 	usba_pipe_handle_data_t	*ph_data = req_wrp->wr_ph_data;
7647c478bd9Sstevel@tonic-gate 	usba_ph_impl_t		*ph_impl = ph_data->p_ph_impl;
7657c478bd9Sstevel@tonic-gate 	usba_hcdi_t		*hcdi = usba_hcdi_get_hcdi(ph_data->
7667c478bd9Sstevel@tonic-gate 				p_usba_device->usb_root_hub_dip);
7677c478bd9Sstevel@tonic-gate 	/*
7687c478bd9Sstevel@tonic-gate 	 * hold the ph_data. we can't use usba_hold_ph_data() since
7697c478bd9Sstevel@tonic-gate 	 * it will return NULL if we are closing the pipe which would
7707c478bd9Sstevel@tonic-gate 	 * then leave all requests stuck in the cb_queue
7717c478bd9Sstevel@tonic-gate 	 */
7727c478bd9Sstevel@tonic-gate 	mutex_enter(&ph_impl->usba_ph_mutex);
7737c478bd9Sstevel@tonic-gate 	ph_impl->usba_ph_ref_count++;
7747c478bd9Sstevel@tonic-gate 
7757c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_HCDI, hcdi->hcdi_log_handle,
7767c478bd9Sstevel@tonic-gate 	    "hcdi_shared_cb_thread: ph_data=0x%p ref=%d req=0x%p",
7777c478bd9Sstevel@tonic-gate 	    ph_data, ph_impl->usba_ph_ref_count, req_wrp);
7787c478bd9Sstevel@tonic-gate 	mutex_exit(&ph_impl->usba_ph_mutex);
7797c478bd9Sstevel@tonic-gate 
7807c478bd9Sstevel@tonic-gate 	/* do the callback */
7817c478bd9Sstevel@tonic-gate 	mutex_enter(&ph_data->p_mutex);
7827c478bd9Sstevel@tonic-gate 	hcdi_do_cb(ph_data, req_wrp, hcdi);
7837c478bd9Sstevel@tonic-gate 	mutex_exit(&ph_data->p_mutex);
7847c478bd9Sstevel@tonic-gate 
7857c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_HCDI, hcdi->hcdi_log_handle,
7867c478bd9Sstevel@tonic-gate 	    "hcdi_cb_thread done: ph_data=0x%p", ph_data);
7877c478bd9Sstevel@tonic-gate 
7887c478bd9Sstevel@tonic-gate 	usba_release_ph_data(ph_impl);
7897c478bd9Sstevel@tonic-gate }
7907c478bd9Sstevel@tonic-gate 
7917c478bd9Sstevel@tonic-gate 
7927c478bd9Sstevel@tonic-gate /*
7937c478bd9Sstevel@tonic-gate  * soft interrupt handler
7947c478bd9Sstevel@tonic-gate  */
7957c478bd9Sstevel@tonic-gate /*ARGSUSED*/
7967c478bd9Sstevel@tonic-gate static uint_t
7977c478bd9Sstevel@tonic-gate hcdi_soft_intr(caddr_t arg1, caddr_t arg2)
7987c478bd9Sstevel@tonic-gate {
7997c478bd9Sstevel@tonic-gate 	usba_hcdi_t		*hcdi = (usba_hcdi_t *)arg1;
8007c478bd9Sstevel@tonic-gate 	usba_req_wrapper_t	*req_wrp;
8017c478bd9Sstevel@tonic-gate 	int			count = 0;
8027c478bd9Sstevel@tonic-gate 
8037c478bd9Sstevel@tonic-gate 	while ((req_wrp = (usba_req_wrapper_t *)
8047c478bd9Sstevel@tonic-gate 	    usba_rm_first_pvt_from_list(&hcdi->hcdi_cb_queue)) != NULL) {
8057c478bd9Sstevel@tonic-gate 		usba_pipe_handle_data_t *ph_data = req_wrp->wr_ph_data;
8067c478bd9Sstevel@tonic-gate 		usba_ph_impl_t		*ph_impl = ph_data->p_ph_impl;
8077c478bd9Sstevel@tonic-gate 
8087c478bd9Sstevel@tonic-gate 		/* hold the pipe */
8097c478bd9Sstevel@tonic-gate 		mutex_enter(&ph_impl->usba_ph_mutex);
8107c478bd9Sstevel@tonic-gate 		ph_impl->usba_ph_ref_count++;
8117c478bd9Sstevel@tonic-gate 		mutex_exit(&ph_impl->usba_ph_mutex);
8127c478bd9Sstevel@tonic-gate 
8137c478bd9Sstevel@tonic-gate 		/* do the callback */
8147c478bd9Sstevel@tonic-gate 		usba_req_normal_cb(req_wrp);
8157c478bd9Sstevel@tonic-gate 
8167c478bd9Sstevel@tonic-gate 		/* decrement the soft interrupt count */
8177c478bd9Sstevel@tonic-gate 		mutex_enter(&ph_data->p_mutex);
8187c478bd9Sstevel@tonic-gate 		ph_data->p_soft_intr--;
8197c478bd9Sstevel@tonic-gate 		mutex_exit(&ph_data->p_mutex);
8207c478bd9Sstevel@tonic-gate 
8217c478bd9Sstevel@tonic-gate 		/* release the pipe */
8227c478bd9Sstevel@tonic-gate 		mutex_enter(&ph_impl->usba_ph_mutex);
8237c478bd9Sstevel@tonic-gate 		ph_impl->usba_ph_ref_count--;
8247c478bd9Sstevel@tonic-gate 		mutex_exit(&ph_impl->usba_ph_mutex);
8257c478bd9Sstevel@tonic-gate 
8267c478bd9Sstevel@tonic-gate 		count++;
8277c478bd9Sstevel@tonic-gate 	}
8287c478bd9Sstevel@tonic-gate 
8297c478bd9Sstevel@tonic-gate 	return (count == 0 ? DDI_INTR_UNCLAIMED : DDI_INTR_CLAIMED);
8307c478bd9Sstevel@tonic-gate }
8317c478bd9Sstevel@tonic-gate 
8327c478bd9Sstevel@tonic-gate 
8337c478bd9Sstevel@tonic-gate /*
8347c478bd9Sstevel@tonic-gate  * hcdi_autoclearing:
8357c478bd9Sstevel@tonic-gate  *	This function is called under the taskq context. It
8367c478bd9Sstevel@tonic-gate  *	resets the pipe, and clears the stall, if necessary
8377c478bd9Sstevel@tonic-gate  */
8387c478bd9Sstevel@tonic-gate static void
8397c478bd9Sstevel@tonic-gate hcdi_autoclearing(usba_req_wrapper_t *req_wrp)
8407c478bd9Sstevel@tonic-gate {
8417c478bd9Sstevel@tonic-gate 	usb_cr_t		cr = req_wrp->wr_cr;
8427c478bd9Sstevel@tonic-gate 	usb_pipe_handle_t	pipe_handle, def_pipe_handle;
8437c478bd9Sstevel@tonic-gate 	usb_cr_t		completion_reason;
8447c478bd9Sstevel@tonic-gate 	usb_cb_flags_t		cb_flags;
8457c478bd9Sstevel@tonic-gate 	int			rval;
8467c478bd9Sstevel@tonic-gate 	usba_device_t		*usba_device =
8477c478bd9Sstevel@tonic-gate 				    req_wrp->wr_ph_data->p_usba_device;
8487c478bd9Sstevel@tonic-gate 	usba_hcdi_t		*hcdi = usba_hcdi_get_hcdi(
8497c478bd9Sstevel@tonic-gate 				    usba_device->usb_root_hub_dip);
8507c478bd9Sstevel@tonic-gate 	usb_req_attrs_t		attrs = req_wrp->wr_attrs;
8517c478bd9Sstevel@tonic-gate 
8527c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_HCDI, hcdi->hcdi_log_handle,
8537c478bd9Sstevel@tonic-gate 	    "hcdi_autoclearing: wrp=0x%p", req_wrp);
8547c478bd9Sstevel@tonic-gate 
8557c478bd9Sstevel@tonic-gate 	pipe_handle = usba_get_pipe_handle(req_wrp->wr_ph_data);
8567c478bd9Sstevel@tonic-gate 	def_pipe_handle = usba_get_dflt_pipe_handle(req_wrp->wr_ph_data->p_dip);
8577c478bd9Sstevel@tonic-gate 
8587c478bd9Sstevel@tonic-gate 	/*
8597c478bd9Sstevel@tonic-gate 	 * first reset the pipe synchronously
8607c478bd9Sstevel@tonic-gate 	 */
8617c478bd9Sstevel@tonic-gate 	if ((attrs & USB_ATTRS_PIPE_RESET) == USB_ATTRS_PIPE_RESET) {
8627c478bd9Sstevel@tonic-gate 		usba_pipe_clear(pipe_handle);
8637c478bd9Sstevel@tonic-gate 		usba_req_set_cb_flags(req_wrp, USB_CB_RESET_PIPE);
8647c478bd9Sstevel@tonic-gate 	}
8657c478bd9Sstevel@tonic-gate 
8667c478bd9Sstevel@tonic-gate 	ASSERT(def_pipe_handle);
8677c478bd9Sstevel@tonic-gate 
8687c478bd9Sstevel@tonic-gate 	/* Do not clear if this request was a usb_get_status request */
8697c478bd9Sstevel@tonic-gate 	if ((pipe_handle == def_pipe_handle) &&
8707c478bd9Sstevel@tonic-gate 	    (USBA_WRP2CTRL_REQ(req_wrp)->ctrl_bRequest ==
8717c478bd9Sstevel@tonic-gate 	    USB_REQ_GET_STATUS)) {
8727c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(DPRINT_MASK_USBAI, hcdi->hcdi_log_handle,
8737c478bd9Sstevel@tonic-gate 		    "hcdi_autoclearing: usb_get_status failed, no clearing");
8747c478bd9Sstevel@tonic-gate 
8757c478bd9Sstevel@tonic-gate 	/* if default pipe and stall no auto clearing */
8767c478bd9Sstevel@tonic-gate 	} else if ((pipe_handle == def_pipe_handle) && (cr == USB_CR_STALL)) {
8777c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(DPRINT_MASK_USBAI, hcdi->hcdi_log_handle,
8787c478bd9Sstevel@tonic-gate 		    "hcdi_autoclearing: default pipe stalled, no clearing");
8797c478bd9Sstevel@tonic-gate 
8807c478bd9Sstevel@tonic-gate 		usba_req_set_cb_flags(req_wrp, USB_CB_PROTOCOL_STALL);
8817c478bd9Sstevel@tonic-gate 
8827c478bd9Sstevel@tonic-gate 	/* else do auto clearing */
8837c478bd9Sstevel@tonic-gate 	} else if (((attrs & USB_ATTRS_AUTOCLEARING) ==
8847c478bd9Sstevel@tonic-gate 	    USB_ATTRS_AUTOCLEARING) && (cr == USB_CR_STALL)) {
8857c478bd9Sstevel@tonic-gate 		ushort_t status = 0;
8867c478bd9Sstevel@tonic-gate 
8877c478bd9Sstevel@tonic-gate 		rval = usb_get_status(req_wrp->wr_dip, def_pipe_handle,
8887c478bd9Sstevel@tonic-gate 		    USB_DEV_REQ_DEV_TO_HOST | USB_DEV_REQ_RCPT_EP,
8897c478bd9Sstevel@tonic-gate 		    req_wrp->wr_ph_data->p_ep.bEndpointAddress,
8907c478bd9Sstevel@tonic-gate 		    &status, USB_FLAGS_SLEEP);
8917c478bd9Sstevel@tonic-gate 		if (rval != USB_SUCCESS) {
8927c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L2(DPRINT_MASK_USBAI, hcdi->hcdi_log_handle,
8937c478bd9Sstevel@tonic-gate 			    "get status (STALL) failed: rval=%d", rval);
8947c478bd9Sstevel@tonic-gate 
8957c478bd9Sstevel@tonic-gate 			usba_pipe_clear(def_pipe_handle);
8967c478bd9Sstevel@tonic-gate 		}
8977c478bd9Sstevel@tonic-gate 
8987c478bd9Sstevel@tonic-gate 		if ((rval != USB_SUCCESS) ||
8997c478bd9Sstevel@tonic-gate 		    (status & USB_EP_HALT_STATUS)) {
9007c478bd9Sstevel@tonic-gate 			usba_req_set_cb_flags(req_wrp, USB_CB_FUNCTIONAL_STALL);
9017c478bd9Sstevel@tonic-gate 
9027c478bd9Sstevel@tonic-gate 			if ((rval = usb_pipe_sync_ctrl_xfer(
9037c478bd9Sstevel@tonic-gate 			    req_wrp->wr_dip, def_pipe_handle,
9047c478bd9Sstevel@tonic-gate 			    USB_DEV_REQ_HOST_TO_DEV |
9057c478bd9Sstevel@tonic-gate 			    USB_DEV_REQ_RCPT_EP,
9067c478bd9Sstevel@tonic-gate 			    USB_REQ_CLEAR_FEATURE,
9077c478bd9Sstevel@tonic-gate 			    0,
9087c478bd9Sstevel@tonic-gate 			    req_wrp->wr_ph_data->p_ep.bEndpointAddress,
9097c478bd9Sstevel@tonic-gate 			    0,
9107c478bd9Sstevel@tonic-gate 			    NULL, 0,
9117c478bd9Sstevel@tonic-gate 			    &completion_reason,
9127c478bd9Sstevel@tonic-gate 			    &cb_flags, USB_FLAGS_SLEEP)) != USB_SUCCESS) {
9137c478bd9Sstevel@tonic-gate 				USB_DPRINTF_L2(DPRINT_MASK_USBAI,
9147c478bd9Sstevel@tonic-gate 				    hcdi->hcdi_log_handle,
9157c478bd9Sstevel@tonic-gate 				    "auto clearing (STALL) failed: "
9167c478bd9Sstevel@tonic-gate 				    "rval=%d, cr=0x%x cb=0x%x",
9177c478bd9Sstevel@tonic-gate 				    rval, completion_reason, cb_flags);
9187c478bd9Sstevel@tonic-gate 
9197c478bd9Sstevel@tonic-gate 				usba_pipe_clear(def_pipe_handle);
9207c478bd9Sstevel@tonic-gate 			} else {
9217c478bd9Sstevel@tonic-gate 				usba_req_set_cb_flags(req_wrp,
9227c478bd9Sstevel@tonic-gate 						USB_CB_STALL_CLEARED);
9237c478bd9Sstevel@tonic-gate 			}
9247c478bd9Sstevel@tonic-gate 		} else {
9257c478bd9Sstevel@tonic-gate 			usba_req_set_cb_flags(req_wrp, USB_CB_PROTOCOL_STALL);
9267c478bd9Sstevel@tonic-gate 		}
9277c478bd9Sstevel@tonic-gate 	}
9287c478bd9Sstevel@tonic-gate }
9297c478bd9Sstevel@tonic-gate 
9307c478bd9Sstevel@tonic-gate 
9317c478bd9Sstevel@tonic-gate /*
9327c478bd9Sstevel@tonic-gate  * usba_hcdi_get_req_private:
9337c478bd9Sstevel@tonic-gate  *	This function is used to get the HCD private field
9347c478bd9Sstevel@tonic-gate  *	maintained by USBA. HCD calls this function.
9357c478bd9Sstevel@tonic-gate  *
9367c478bd9Sstevel@tonic-gate  * Arguments:
9377c478bd9Sstevel@tonic-gate  *	req		- pointer to usb_*_req_t
9387c478bd9Sstevel@tonic-gate  *
9397c478bd9Sstevel@tonic-gate  * Return Values:
9407c478bd9Sstevel@tonic-gate  *	wr_hcd_private field from wrapper
9417c478bd9Sstevel@tonic-gate  */
9427c478bd9Sstevel@tonic-gate usb_opaque_t
9437c478bd9Sstevel@tonic-gate usba_hcdi_get_req_private(usb_opaque_t req)
9447c478bd9Sstevel@tonic-gate {
9457c478bd9Sstevel@tonic-gate 	usba_req_wrapper_t *wrp = USBA_REQ2WRP(req);
9467c478bd9Sstevel@tonic-gate 
9477c478bd9Sstevel@tonic-gate 	return (wrp->wr_hcd_private);
9487c478bd9Sstevel@tonic-gate }
9497c478bd9Sstevel@tonic-gate 
9507c478bd9Sstevel@tonic-gate 
9517c478bd9Sstevel@tonic-gate /*
9527c478bd9Sstevel@tonic-gate  * usba_hcdi_set_req_private:
9537c478bd9Sstevel@tonic-gate  *	This function is used to set the HCD private field
9547c478bd9Sstevel@tonic-gate  *	maintained by USBA. HCD calls this function.
9557c478bd9Sstevel@tonic-gate  *
9567c478bd9Sstevel@tonic-gate  * Arguments:
9577c478bd9Sstevel@tonic-gate  *	req		- pointer to usb_*_req_t
9587c478bd9Sstevel@tonic-gate  *	hcd_private	- wr_hcd_private field from wrapper
9597c478bd9Sstevel@tonic-gate  */
9607c478bd9Sstevel@tonic-gate void
9617c478bd9Sstevel@tonic-gate usba_hcdi_set_req_private(usb_opaque_t req,
9627c478bd9Sstevel@tonic-gate 			usb_opaque_t	hcd_private)
9637c478bd9Sstevel@tonic-gate {
9647c478bd9Sstevel@tonic-gate 	usba_req_wrapper_t *wrp = USBA_REQ2WRP(req);
9657c478bd9Sstevel@tonic-gate 
9667c478bd9Sstevel@tonic-gate 	wrp->wr_hcd_private = hcd_private;
9677c478bd9Sstevel@tonic-gate }
9687c478bd9Sstevel@tonic-gate 
9697c478bd9Sstevel@tonic-gate 
9707c478bd9Sstevel@tonic-gate /* get data toggle information for this endpoint */
9717c478bd9Sstevel@tonic-gate uchar_t
9727c478bd9Sstevel@tonic-gate usba_hcdi_get_data_toggle(usba_device_t *usba_device, uint8_t ep_addr)
9737c478bd9Sstevel@tonic-gate {
9747c478bd9Sstevel@tonic-gate 	uchar_t		toggle;
9757c478bd9Sstevel@tonic-gate 	usba_ph_impl_t	*ph_impl;
9767c478bd9Sstevel@tonic-gate 	int		ep_index;
9777c478bd9Sstevel@tonic-gate 
9787c478bd9Sstevel@tonic-gate 	ep_index = usb_get_ep_index(ep_addr);
9797c478bd9Sstevel@tonic-gate 	mutex_enter(&usba_device->usb_mutex);
9807c478bd9Sstevel@tonic-gate 	ph_impl = &usba_device->usb_ph_list[ep_index];
9817c478bd9Sstevel@tonic-gate 	mutex_enter(&ph_impl->usba_ph_mutex);
9827c478bd9Sstevel@tonic-gate 	toggle = (uchar_t)(ph_impl->usba_ph_flags & USBA_PH_DATA_TOGGLE);
9837c478bd9Sstevel@tonic-gate 	mutex_exit(&ph_impl->usba_ph_mutex);
9847c478bd9Sstevel@tonic-gate 	mutex_exit(&usba_device->usb_mutex);
9857c478bd9Sstevel@tonic-gate 
9867c478bd9Sstevel@tonic-gate 	return (toggle);
9877c478bd9Sstevel@tonic-gate }
9887c478bd9Sstevel@tonic-gate 
9897c478bd9Sstevel@tonic-gate 
9907c478bd9Sstevel@tonic-gate /* set data toggle information for this endpoint */
9917c478bd9Sstevel@tonic-gate void
9927c478bd9Sstevel@tonic-gate usba_hcdi_set_data_toggle(usba_device_t *usba_device, uint8_t ep_addr,
9937c478bd9Sstevel@tonic-gate     uchar_t toggle)
9947c478bd9Sstevel@tonic-gate {
9957c478bd9Sstevel@tonic-gate 	usba_ph_impl_t	*ph_impl;
9967c478bd9Sstevel@tonic-gate 	int		ep_index;
9977c478bd9Sstevel@tonic-gate 
9987c478bd9Sstevel@tonic-gate 	ep_index = usb_get_ep_index(ep_addr);
9997c478bd9Sstevel@tonic-gate 	mutex_enter(&usba_device->usb_mutex);
10007c478bd9Sstevel@tonic-gate 	ph_impl = &usba_device->usb_ph_list[ep_index];
10017c478bd9Sstevel@tonic-gate 	mutex_enter(&ph_impl->usba_ph_mutex);
10027c478bd9Sstevel@tonic-gate 	ph_impl->usba_ph_flags &= ~USBA_PH_DATA_TOGGLE;
10037c478bd9Sstevel@tonic-gate 	ph_impl->usba_ph_flags |= (USBA_PH_DATA_TOGGLE & toggle);
10047c478bd9Sstevel@tonic-gate 	mutex_exit(&ph_impl->usba_ph_mutex);
10057c478bd9Sstevel@tonic-gate 	mutex_exit(&usba_device->usb_mutex);
10067c478bd9Sstevel@tonic-gate }
10077c478bd9Sstevel@tonic-gate 
10087c478bd9Sstevel@tonic-gate 
10097c478bd9Sstevel@tonic-gate /* get pipe_handle_impl ptr for this ep */
10107c478bd9Sstevel@tonic-gate usba_pipe_handle_data_t *
10117c478bd9Sstevel@tonic-gate usba_hcdi_get_ph_data(usba_device_t *usba_device, uint8_t ep_addr)
10127c478bd9Sstevel@tonic-gate {
10137c478bd9Sstevel@tonic-gate 	return (usba_device->usb_ph_list[usb_get_ep_index(ep_addr)].
10147c478bd9Sstevel@tonic-gate 							usba_ph_data);
10157c478bd9Sstevel@tonic-gate }
1016