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