xref: /illumos-gate/usr/src/uts/common/io/1394/nx1394.c (revision 2570281c)
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  */
26cd21e7c5SGarrett D'Amore /*
27cd21e7c5SGarrett D'Amore  * Copyright 2012 Garrett D'Amore <garrett@damore.org>.  All rights reserved.
28cd21e7c5SGarrett D'Amore  */
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate /*
317c478bd9Sstevel@tonic-gate  * nx1394.c
327c478bd9Sstevel@tonic-gate  *    1394 Services Layer Nexus Support Routines
337c478bd9Sstevel@tonic-gate  *    Routines in this file implement nexus bus_ops.
347c478bd9Sstevel@tonic-gate  */
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate #include <sys/conf.h>
377c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
387c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
397c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
407c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
417c478bd9Sstevel@tonic-gate #include <sys/types.h>
427c478bd9Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
437c478bd9Sstevel@tonic-gate #include <sys/1394/t1394.h>
447c478bd9Sstevel@tonic-gate #include <sys/1394/s1394.h>
457c478bd9Sstevel@tonic-gate #include <sys/1394/h1394.h>
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate static int nx1394_dma_allochdl(dev_info_t *dip, dev_info_t *rdip,
487c478bd9Sstevel@tonic-gate     ddi_dma_attr_t *attr, int (*waitfnp)(caddr_t), caddr_t arg,
497c478bd9Sstevel@tonic-gate     ddi_dma_handle_t *handlep);
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate static int nx1394_bus_ctl(dev_info_t *dip, dev_info_t *rdip, ddi_ctl_enum_t op,
527c478bd9Sstevel@tonic-gate     void *arg, void *result);
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate static int nx1394_get_event_cookie(dev_info_t *dip, dev_info_t *rdip,
557c478bd9Sstevel@tonic-gate     char *name, ddi_eventcookie_t *event_cookiep);
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate static int nx1394_add_eventcall(dev_info_t *dip, dev_info_t *rdip,
587c478bd9Sstevel@tonic-gate     ddi_eventcookie_t eventhdl, void (*callback)(), void *arg,
597c478bd9Sstevel@tonic-gate     ddi_callback_id_t *cb_id);
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate static int nx1394_remove_eventcall(dev_info_t *dip, ddi_callback_id_t cb_id);
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate static int nx1394_post_event(dev_info_t *dip, dev_info_t *rdip,
647c478bd9Sstevel@tonic-gate     ddi_eventcookie_t eventhdl, void *impl_data);
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate struct bus_ops nx1394_busops = {
677c478bd9Sstevel@tonic-gate 	BUSO_REV,
687c478bd9Sstevel@tonic-gate 	nullbusmap,			/* bus_map */
697c478bd9Sstevel@tonic-gate 	NULL,				/* bus_get_intrspec */
707c478bd9Sstevel@tonic-gate 	NULL,				/* bus_add_intrspec */
717c478bd9Sstevel@tonic-gate 	NULL,				/* bus_remove_intrspec */
727c478bd9Sstevel@tonic-gate 	i_ddi_map_fault,		/* XXXX bus_map_fault */
73cd21e7c5SGarrett D'Amore 	NULL,				/* bus_dma_map */
747c478bd9Sstevel@tonic-gate 	nx1394_dma_allochdl,
757c478bd9Sstevel@tonic-gate 	ddi_dma_freehdl,
767c478bd9Sstevel@tonic-gate 	ddi_dma_bindhdl,
777c478bd9Sstevel@tonic-gate 	ddi_dma_unbindhdl,
787c478bd9Sstevel@tonic-gate 	ddi_dma_flush,
797c478bd9Sstevel@tonic-gate 	ddi_dma_win,
807c478bd9Sstevel@tonic-gate 	ddi_dma_mctl,			/* bus_dma_ctl */
817c478bd9Sstevel@tonic-gate 	nx1394_bus_ctl,			/* bus_ctl */
827c478bd9Sstevel@tonic-gate 	ddi_bus_prop_op,		/* bus_prop_op */
837c478bd9Sstevel@tonic-gate 	nx1394_get_event_cookie,	/* (*bus_get_eventcookie() */
847c478bd9Sstevel@tonic-gate 	nx1394_add_eventcall,		/* (*bus_add_eventcall)(); */
857c478bd9Sstevel@tonic-gate 	nx1394_remove_eventcall,	/* (*bus_remove_eventcall)(); */
867c478bd9Sstevel@tonic-gate 	nx1394_post_event,		/* (*bus_post_event)(); */
877c478bd9Sstevel@tonic-gate 	0,				/* (*interrupt control)();	*/
887c478bd9Sstevel@tonic-gate 	0,				/* (*bus_config)();	*/
897c478bd9Sstevel@tonic-gate 	0,				/* (*bus_unconfig)();	*/
907c478bd9Sstevel@tonic-gate 	0,				/* (*bus_fm_init)();	*/
917c478bd9Sstevel@tonic-gate 	0,				/* (*bus_fm_fini)();	*/
927c478bd9Sstevel@tonic-gate 	0,				/* (*bus_fm_access_enter)();	*/
937c478bd9Sstevel@tonic-gate 	0,				/* (*bus_fm_access_exit)();	*/
947c478bd9Sstevel@tonic-gate 	0,				/* (*bus_power)();	*/
957c478bd9Sstevel@tonic-gate 	i_ddi_intr_ops			/* (*bus_intr_op)();	*/
967c478bd9Sstevel@tonic-gate };
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate /*
997c478bd9Sstevel@tonic-gate  * removal/insertion/reset events
1007c478bd9Sstevel@tonic-gate  */
1017c478bd9Sstevel@tonic-gate #define	NX1394_EVENT_TAG_HOT_REMOVAL		0
1027c478bd9Sstevel@tonic-gate #define	NX1394_EVENT_TAG_HOT_INSERTION		1
1037c478bd9Sstevel@tonic-gate #define	NX1394_EVENT_TAG_BUS_RESET		2
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate static ndi_event_definition_t nx1394_event_defs[] = {
1067c478bd9Sstevel@tonic-gate 	{NX1394_EVENT_TAG_HOT_REMOVAL, DDI_DEVI_REMOVE_EVENT, EPL_KERNEL,
1077c478bd9Sstevel@tonic-gate 	    NDI_EVENT_POST_TO_TGT},
1087c478bd9Sstevel@tonic-gate 	{NX1394_EVENT_TAG_HOT_INSERTION, DDI_DEVI_INSERT_EVENT, EPL_KERNEL,
1097c478bd9Sstevel@tonic-gate 	    NDI_EVENT_POST_TO_TGT},
1107c478bd9Sstevel@tonic-gate 	{NX1394_EVENT_TAG_BUS_RESET, DDI_DEVI_BUS_RESET_EVENT, EPL_KERNEL,
1117c478bd9Sstevel@tonic-gate 	    NDI_EVENT_POST_TO_ALL},
1127c478bd9Sstevel@tonic-gate };
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate #define	NX1394_N_EVENTS \
1157c478bd9Sstevel@tonic-gate 	(sizeof (nx1394_event_defs) / sizeof (ndi_event_definition_t))
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate static ndi_event_set_t nx1394_events = {
1187c478bd9Sstevel@tonic-gate 	NDI_EVENTS_REV1, NX1394_N_EVENTS, nx1394_event_defs
1197c478bd9Sstevel@tonic-gate };
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate /*
1227c478bd9Sstevel@tonic-gate  * nx1394_bus_ctl()
1237c478bd9Sstevel@tonic-gate  *    This routine implements nexus bus ctl operations. Of importance are
1247c478bd9Sstevel@tonic-gate  *    DDI_CTLOPS_REPORTDEV, DDI_CTLOPS_INITCHILD, DDI_CTLOPS_UNINITCHILD
1257c478bd9Sstevel@tonic-gate  *    and DDI_CTLOPS_POWER. For DDI_CTLOPS_INITCHILD, it tries to lookup
1267c478bd9Sstevel@tonic-gate  *    reg property on the child node and builds and sets the name
1277c478bd9Sstevel@tonic-gate  *    (name is of the form GGGGGGGGGGGGGGGG[,AAAAAAAAAAAA], where
1287c478bd9Sstevel@tonic-gate  *    GGGGGGGGGGGGGGGG is the GUID and AAAAAAAAAAAA is the optional unit
1297c478bd9Sstevel@tonic-gate  *    address).
1307c478bd9Sstevel@tonic-gate  */
1317c478bd9Sstevel@tonic-gate static int
nx1394_bus_ctl(dev_info_t * dip,dev_info_t * rdip,ddi_ctl_enum_t op,void * arg,void * result)1327c478bd9Sstevel@tonic-gate nx1394_bus_ctl(dev_info_t *dip, dev_info_t *rdip, ddi_ctl_enum_t op, void *arg,
1337c478bd9Sstevel@tonic-gate     void *result)
1347c478bd9Sstevel@tonic-gate {
1357c478bd9Sstevel@tonic-gate 	int status;
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate 	switch (op) {
1387c478bd9Sstevel@tonic-gate 	case DDI_CTLOPS_REPORTDEV: {
1397c478bd9Sstevel@tonic-gate 		dev_info_t *pdip = ddi_get_parent(rdip);
1407c478bd9Sstevel@tonic-gate 		cmn_err(CE_CONT, "?%s%d at %s%d",
1417c478bd9Sstevel@tonic-gate 		    ddi_node_name(rdip), ddi_get_instance(rdip),
1427c478bd9Sstevel@tonic-gate 		    ddi_node_name(pdip), ddi_get_instance(pdip));
1437c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
1447c478bd9Sstevel@tonic-gate 	}
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate 	case DDI_CTLOPS_INITCHILD: {
1477c478bd9Sstevel@tonic-gate 		dev_info_t *ocdip, *cdip = (dev_info_t *)arg;
1487c478bd9Sstevel@tonic-gate 		dev_info_t *pdip = ddi_get_parent(cdip);
1497c478bd9Sstevel@tonic-gate 		int reglen, i;
1507c478bd9Sstevel@tonic-gate 		uint32_t *regptr;
1517c478bd9Sstevel@tonic-gate 		char addr[MAXNAMELEN];
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate 		i = ddi_prop_lookup_int_array(DDI_DEV_T_ANY, cdip,
1547c478bd9Sstevel@tonic-gate 		    DDI_PROP_DONTPASS, "reg", (int **)&regptr,
1557c478bd9Sstevel@tonic-gate 		    (uint_t *)&reglen);
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate 		if (i != DDI_PROP_SUCCESS) {
1587c478bd9Sstevel@tonic-gate 			cmn_err(CE_NOTE, "!%s(%d): \"reg\" property not found",
1597c478bd9Sstevel@tonic-gate 			    ddi_node_name(cdip), ddi_get_instance(cdip));
1607c478bd9Sstevel@tonic-gate 			return (DDI_NOT_WELL_FORMED);
1617c478bd9Sstevel@tonic-gate 		}
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate 		ASSERT(reglen != 0);
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate 		/*
1667c478bd9Sstevel@tonic-gate 		 * addr is of the format GGGGGGGGGGGGGGGG[,AAAAAAAAAAAA]
1677c478bd9Sstevel@tonic-gate 		 */
1687c478bd9Sstevel@tonic-gate 		if (regptr[2] || regptr[3]) {
1697c478bd9Sstevel@tonic-gate 			(void) sprintf(addr, "%08x%08x,%04x%08x", regptr[0],
1707c478bd9Sstevel@tonic-gate 			    regptr[1], regptr[2], regptr[3]);
1717c478bd9Sstevel@tonic-gate 		} else {
1727c478bd9Sstevel@tonic-gate 			(void) sprintf(addr, "%08x%08x", regptr[0], regptr[1]);
1737c478bd9Sstevel@tonic-gate 		}
1747c478bd9Sstevel@tonic-gate 		ddi_prop_free(regptr);
1757c478bd9Sstevel@tonic-gate 		ddi_set_name_addr(cdip, addr);
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate 		/*
1787c478bd9Sstevel@tonic-gate 		 * Check for a node with the same name & addr as the current
1797c478bd9Sstevel@tonic-gate 		 * node. If such a node exists, return failure.
1807c478bd9Sstevel@tonic-gate 		 */
1817c478bd9Sstevel@tonic-gate 		if ((ocdip = ndi_devi_find(pdip, ddi_node_name(cdip), addr)) !=
1827c478bd9Sstevel@tonic-gate 		    NULL && ocdip != cdip) {
1837c478bd9Sstevel@tonic-gate 			cmn_err(CE_NOTE,
1847c478bd9Sstevel@tonic-gate 			    "!%s(%d): Duplicate dev_info node found %s@%s",
1857c478bd9Sstevel@tonic-gate 			    ddi_node_name(cdip), ddi_get_instance(cdip),
1867c478bd9Sstevel@tonic-gate 			    ddi_node_name(ocdip), addr);
1877c478bd9Sstevel@tonic-gate 			ddi_set_name_addr(cdip, NULL);
1887c478bd9Sstevel@tonic-gate 			return (DDI_NOT_WELL_FORMED);
1897c478bd9Sstevel@tonic-gate 		}
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate 		/*
1927c478bd9Sstevel@tonic-gate 		 * If HAL (parent dip) has "active-dma-flush" property, then
1937c478bd9Sstevel@tonic-gate 		 * add property to child as well.  Workaround for active
1947c478bd9Sstevel@tonic-gate 		 * context flushing bug in Schizo rev 2.1 and 2.2.
1957c478bd9Sstevel@tonic-gate 		 */
1967c478bd9Sstevel@tonic-gate 		if (ddi_prop_exists(DDI_DEV_T_ANY, pdip, DDI_PROP_DONTPASS,
1977c478bd9Sstevel@tonic-gate 		    "active-dma-flush") != 0) {
1987c478bd9Sstevel@tonic-gate 			status = ndi_prop_update_int(DDI_DEV_T_NONE, cdip,
1997c478bd9Sstevel@tonic-gate 			    "active-dma-flush", 1);
2007c478bd9Sstevel@tonic-gate 			if (status != NDI_SUCCESS) {
2017c478bd9Sstevel@tonic-gate 				cmn_err(CE_NOTE, "!%s(%d): Unable to add "
2027c478bd9Sstevel@tonic-gate 				    "\"active-dma-flush\" property",
2037c478bd9Sstevel@tonic-gate 				    ddi_node_name(cdip),
2047c478bd9Sstevel@tonic-gate 				    ddi_get_instance(cdip));
2057c478bd9Sstevel@tonic-gate 				ddi_set_name_addr(cdip, NULL);
2067c478bd9Sstevel@tonic-gate 				return (DDI_NOT_WELL_FORMED);
2077c478bd9Sstevel@tonic-gate 			}
2087c478bd9Sstevel@tonic-gate 		}
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
2117c478bd9Sstevel@tonic-gate 	}
2127c478bd9Sstevel@tonic-gate 
2137c478bd9Sstevel@tonic-gate 	case DDI_CTLOPS_UNINITCHILD: {
2147c478bd9Sstevel@tonic-gate 		ddi_prop_remove_all((dev_info_t *)arg);
2157c478bd9Sstevel@tonic-gate 		ddi_set_name_addr((dev_info_t *)arg, NULL);
2167c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
2177c478bd9Sstevel@tonic-gate 	}
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate 	case DDI_CTLOPS_IOMIN: {
2207c478bd9Sstevel@tonic-gate 		status = ddi_ctlops(dip, rdip, op, arg, result);
2217c478bd9Sstevel@tonic-gate 		return (status);
2227c478bd9Sstevel@tonic-gate 	}
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate 	case DDI_CTLOPS_POWER: {
2257c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
2267c478bd9Sstevel@tonic-gate 	}
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate 	/*
2297c478bd9Sstevel@tonic-gate 	 * These ops correspond to functions that "shouldn't" be called
2307c478bd9Sstevel@tonic-gate 	 * by a 1394 client driver.
2317c478bd9Sstevel@tonic-gate 	 */
2327c478bd9Sstevel@tonic-gate 	case DDI_CTLOPS_DMAPMAPC:
2337c478bd9Sstevel@tonic-gate 	case DDI_CTLOPS_REPORTINT:
2347c478bd9Sstevel@tonic-gate 	case DDI_CTLOPS_REGSIZE:
2357c478bd9Sstevel@tonic-gate 	case DDI_CTLOPS_NREGS:
2367c478bd9Sstevel@tonic-gate 	case DDI_CTLOPS_SIDDEV:
2377c478bd9Sstevel@tonic-gate 	case DDI_CTLOPS_SLAVEONLY:
2387c478bd9Sstevel@tonic-gate 	case DDI_CTLOPS_AFFINITY:
2397c478bd9Sstevel@tonic-gate 	case DDI_CTLOPS_POKE:
2407c478bd9Sstevel@tonic-gate 	case DDI_CTLOPS_PEEK: {
2417c478bd9Sstevel@tonic-gate 		cmn_err(CE_CONT, "!%s(%d): invalid op (%d) from %s(%d)",
2427c478bd9Sstevel@tonic-gate 		    ddi_node_name(dip), ddi_get_instance(dip),
2437c478bd9Sstevel@tonic-gate 		    op, ddi_node_name(rdip), ddi_get_instance(rdip));
2447c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
2457c478bd9Sstevel@tonic-gate 	}
2467c478bd9Sstevel@tonic-gate 
2477c478bd9Sstevel@tonic-gate 	/*
2487c478bd9Sstevel@tonic-gate 	 * Everything else (e.g. PTOB/BTOP/BTOPR requests) we pass up
2497c478bd9Sstevel@tonic-gate 	 */
2507c478bd9Sstevel@tonic-gate 	default: {
2517c478bd9Sstevel@tonic-gate 		status = ddi_ctlops(dip, rdip, op, arg, result);
2527c478bd9Sstevel@tonic-gate 		return (status);
2537c478bd9Sstevel@tonic-gate 	}
2547c478bd9Sstevel@tonic-gate 	}
2557c478bd9Sstevel@tonic-gate }
2567c478bd9Sstevel@tonic-gate 
2577c478bd9Sstevel@tonic-gate /*
2587c478bd9Sstevel@tonic-gate  * nx1394_dma_allochdl()
2597c478bd9Sstevel@tonic-gate  *    Merges the ddi_dma_attr_t passed in by the target (using
2607c478bd9Sstevel@tonic-gate  *    ddi_dma_alloc_handle() call) with that of the hal and passes the alloc
2617c478bd9Sstevel@tonic-gate  *    handle request up the device by calling ddi_dma_allochdl().
2627c478bd9Sstevel@tonic-gate  */
2637c478bd9Sstevel@tonic-gate static int
nx1394_dma_allochdl(dev_info_t * dip,dev_info_t * rdip,ddi_dma_attr_t * attr,int (* waitfnp)(caddr_t),caddr_t arg,ddi_dma_handle_t * handlep)2647c478bd9Sstevel@tonic-gate nx1394_dma_allochdl(dev_info_t *dip, dev_info_t *rdip, ddi_dma_attr_t *attr,
2657c478bd9Sstevel@tonic-gate     int (*waitfnp)(caddr_t), caddr_t arg, ddi_dma_handle_t *handlep)
2667c478bd9Sstevel@tonic-gate {
2677c478bd9Sstevel@tonic-gate 	s1394_hal_t *hal;
2687c478bd9Sstevel@tonic-gate 	ddi_dma_attr_t *hal_attr;
2697c478bd9Sstevel@tonic-gate 	int status;
2707c478bd9Sstevel@tonic-gate 
2717c478bd9Sstevel@tonic-gate 	_NOTE(SCHEME_PROTECTS_DATA("unique (per thread)", ddi_dma_attr_t))
2727c478bd9Sstevel@tonic-gate 
2737c478bd9Sstevel@tonic-gate 	/*
2747c478bd9Sstevel@tonic-gate 	 * If hal calls ddi_dma_alloc_handle, dip == rdip == hal dip.
2757c478bd9Sstevel@tonic-gate 	 * Unfortunately, we cannot verify this (by way of looking up for hal
2767c478bd9Sstevel@tonic-gate 	 * dip) here because h1394_attach() may happen much later.
2777c478bd9Sstevel@tonic-gate 	 */
2787c478bd9Sstevel@tonic-gate 	if (dip != rdip) {
2797c478bd9Sstevel@tonic-gate 		hal = s1394_dip_to_hal(ddi_get_parent(rdip));
2807c478bd9Sstevel@tonic-gate 		ASSERT(hal);
2817c478bd9Sstevel@tonic-gate 		hal_attr = &hal->halinfo.dma_attr;
2827c478bd9Sstevel@tonic-gate 		ASSERT(hal_attr);
2837c478bd9Sstevel@tonic-gate 		ddi_dma_attr_merge(attr, hal_attr);
2847c478bd9Sstevel@tonic-gate 	}
2857c478bd9Sstevel@tonic-gate 	status = ddi_dma_allochdl(dip, rdip, attr, waitfnp, arg, handlep);
2867c478bd9Sstevel@tonic-gate 	return (status);
2877c478bd9Sstevel@tonic-gate }
2887c478bd9Sstevel@tonic-gate 
2897c478bd9Sstevel@tonic-gate /*
2907c478bd9Sstevel@tonic-gate  * nx1394_get_event_cookie()
2917c478bd9Sstevel@tonic-gate  *    Called when a child node calls ddi_get_eventcookie().
2927c478bd9Sstevel@tonic-gate  *    Returns event cookie corresponding to event "name".
2937c478bd9Sstevel@tonic-gate  */
2947c478bd9Sstevel@tonic-gate static int
nx1394_get_event_cookie(dev_info_t * dip,dev_info_t * rdip,char * name,ddi_eventcookie_t * event_cookiep)2957c478bd9Sstevel@tonic-gate nx1394_get_event_cookie(dev_info_t *dip, dev_info_t *rdip, char *name,
2967c478bd9Sstevel@tonic-gate     ddi_eventcookie_t *event_cookiep)
2977c478bd9Sstevel@tonic-gate {
2987c478bd9Sstevel@tonic-gate 	int ret;
2997c478bd9Sstevel@tonic-gate 	s1394_hal_t *hal;
3007c478bd9Sstevel@tonic-gate 
3017c478bd9Sstevel@tonic-gate 	hal = s1394_dip_to_hal(dip);
3027c478bd9Sstevel@tonic-gate 	ASSERT(hal);
3037c478bd9Sstevel@tonic-gate 
3047c478bd9Sstevel@tonic-gate 	ret = ndi_event_retrieve_cookie(hal->hal_ndi_event_hdl,
3057c478bd9Sstevel@tonic-gate 	    rdip, name, event_cookiep, 0);
3067c478bd9Sstevel@tonic-gate 
3077c478bd9Sstevel@tonic-gate 	return (ret);
3087c478bd9Sstevel@tonic-gate 
3097c478bd9Sstevel@tonic-gate }
3107c478bd9Sstevel@tonic-gate 
3117c478bd9Sstevel@tonic-gate /*
3127c478bd9Sstevel@tonic-gate  * nx1394_add_eventcall()
3137c478bd9Sstevel@tonic-gate  *    This gets called when a child node calls ddi_add_eventcall(). Registers
3147c478bd9Sstevel@tonic-gate  *    the specified callback for the requested event cookie with the ndi
3157c478bd9Sstevel@tonic-gate  *    event framework.
3167c478bd9Sstevel@tonic-gate  *    dip is the hal dip. This routine calls ndi_event_add_callback(),
3177c478bd9Sstevel@tonic-gate  *    allowing requests for events we don't generate to pass up the tree.
3187c478bd9Sstevel@tonic-gate  */
3197c478bd9Sstevel@tonic-gate static int
nx1394_add_eventcall(dev_info_t * dip,dev_info_t * rdip,ddi_eventcookie_t cookie,void (* callback)(),void * arg,ddi_callback_id_t * cb_id)3207c478bd9Sstevel@tonic-gate nx1394_add_eventcall(dev_info_t *dip, dev_info_t *rdip,
3217c478bd9Sstevel@tonic-gate     ddi_eventcookie_t cookie, void (*callback)(), void *arg,
3227c478bd9Sstevel@tonic-gate     ddi_callback_id_t *cb_id)
3237c478bd9Sstevel@tonic-gate {
3247c478bd9Sstevel@tonic-gate 	int ret;
3257c478bd9Sstevel@tonic-gate 	s1394_hal_t *hal;
3267c478bd9Sstevel@tonic-gate #if defined(DEBUG)
3277c478bd9Sstevel@tonic-gate 	char *event_name = NULL;
3287c478bd9Sstevel@tonic-gate #endif
3297c478bd9Sstevel@tonic-gate 
3307c478bd9Sstevel@tonic-gate 	hal = s1394_dip_to_hal(dip);
3317c478bd9Sstevel@tonic-gate 	ASSERT(hal);
3327c478bd9Sstevel@tonic-gate 
3337c478bd9Sstevel@tonic-gate 	ret = ndi_event_add_callback(hal->hal_ndi_event_hdl, rdip, cookie,
3347c478bd9Sstevel@tonic-gate 	    callback, arg, NDI_NOSLEEP, cb_id);
3357c478bd9Sstevel@tonic-gate #if defined(DEBUG)
3367c478bd9Sstevel@tonic-gate 	event_name = ndi_event_cookie_to_name(hal->hal_ndi_event_hdl, cookie);
3377c478bd9Sstevel@tonic-gate 	if (event_name == NULL)
338cd21e7c5SGarrett D'Amore 		event_name = "";
3397c478bd9Sstevel@tonic-gate #endif
3407c478bd9Sstevel@tonic-gate 
3417c478bd9Sstevel@tonic-gate 	return (ret);
3427c478bd9Sstevel@tonic-gate }
3437c478bd9Sstevel@tonic-gate 
3447c478bd9Sstevel@tonic-gate /*
3457c478bd9Sstevel@tonic-gate  * nx1394_remove_eventcall()
3467c478bd9Sstevel@tonic-gate  *    Called as a result of a child node calling ddi_remove_eventcall().
3477c478bd9Sstevel@tonic-gate  *    Unregisters the callback corresponding to the callback id passed in.
3487c478bd9Sstevel@tonic-gate  */
3497c478bd9Sstevel@tonic-gate static int
nx1394_remove_eventcall(dev_info_t * dip,ddi_callback_id_t cb_id)3507c478bd9Sstevel@tonic-gate nx1394_remove_eventcall(dev_info_t *dip, ddi_callback_id_t cb_id)
3517c478bd9Sstevel@tonic-gate {
3527c478bd9Sstevel@tonic-gate 	int ret;
3537c478bd9Sstevel@tonic-gate 	s1394_hal_t *hal;
3547c478bd9Sstevel@tonic-gate 	ddi_eventcookie_t cookie;
3557c478bd9Sstevel@tonic-gate #if defined(DEBUG)
3567c478bd9Sstevel@tonic-gate 	char *event_name = NULL;
3577c478bd9Sstevel@tonic-gate #endif
3587c478bd9Sstevel@tonic-gate 
3597c478bd9Sstevel@tonic-gate 	ASSERT(cb_id);
3607c478bd9Sstevel@tonic-gate 	cookie = ((ndi_event_callbacks_t *)cb_id)->ndi_evtcb_cookie;
3617c478bd9Sstevel@tonic-gate 
3627c478bd9Sstevel@tonic-gate 	hal = s1394_dip_to_hal(dip);
3637c478bd9Sstevel@tonic-gate 	ASSERT(hal);
3647c478bd9Sstevel@tonic-gate 
3657c478bd9Sstevel@tonic-gate 	ret = ndi_event_remove_callback(hal->hal_ndi_event_hdl, cb_id);
3667c478bd9Sstevel@tonic-gate 
3677c478bd9Sstevel@tonic-gate #if defined(DEBUG)
3687c478bd9Sstevel@tonic-gate 	event_name = ndi_event_cookie_to_name(hal->hal_ndi_event_hdl, cookie);
3697c478bd9Sstevel@tonic-gate 	if (event_name == NULL)
370cd21e7c5SGarrett D'Amore 		event_name = "";
3717c478bd9Sstevel@tonic-gate 
3727c478bd9Sstevel@tonic-gate #endif
3737c478bd9Sstevel@tonic-gate 
3747c478bd9Sstevel@tonic-gate 	return (ret);
3757c478bd9Sstevel@tonic-gate }
3767c478bd9Sstevel@tonic-gate 
3777c478bd9Sstevel@tonic-gate /*
3787c478bd9Sstevel@tonic-gate  * nx1394_post_event()
3797c478bd9Sstevel@tonic-gate  *    Called when a child node calls ddi_post_event. If the event is one of
3807c478bd9Sstevel@tonic-gate  *    the events supported by us (bus reset/insert/remove, for now), builds
3817c478bd9Sstevel@tonic-gate  *    a t1394_localinfo_t structure and calls ndi_event_run_callbacks(). This
3827c478bd9Sstevel@tonic-gate  *    will result in all registered callbacks being invoked with
3837c478bd9Sstevel@tonic-gate  *    t1394_localinfo_t as the impl_data. (see ddi_add_eventcall for callback
3847c478bd9Sstevel@tonic-gate  *    arguments.) If the event is not defined by us, the request is
3857c478bd9Sstevel@tonic-gate  *    propagated up the device tree by calling ndi_post_event().
3867c478bd9Sstevel@tonic-gate  */
3877c478bd9Sstevel@tonic-gate static int
nx1394_post_event(dev_info_t * dip,dev_info_t * rdip,ddi_eventcookie_t cookie,void * impl_data)3887c478bd9Sstevel@tonic-gate nx1394_post_event(dev_info_t *dip, dev_info_t *rdip, ddi_eventcookie_t cookie,
3897c478bd9Sstevel@tonic-gate     void *impl_data)
3907c478bd9Sstevel@tonic-gate {
3917c478bd9Sstevel@tonic-gate 	int ret;
3927c478bd9Sstevel@tonic-gate 	char *name;
3937c478bd9Sstevel@tonic-gate 	s1394_hal_t *hal;
3947c478bd9Sstevel@tonic-gate 	t1394_localinfo_t localinfo;
3957c478bd9Sstevel@tonic-gate 
3967c478bd9Sstevel@tonic-gate 	hal = s1394_dip_to_hal(dip);
3977c478bd9Sstevel@tonic-gate 	ASSERT(hal);
3987c478bd9Sstevel@tonic-gate 
3997c478bd9Sstevel@tonic-gate 	name = ndi_event_cookie_to_name(hal->hal_ndi_event_hdl, cookie);
4007c478bd9Sstevel@tonic-gate 	/* name is NULL if we don't generate the event */
4017c478bd9Sstevel@tonic-gate 	if (name != NULL) {
4027c478bd9Sstevel@tonic-gate 
4037c478bd9Sstevel@tonic-gate 		mutex_enter(&hal->topology_tree_mutex);
4047c478bd9Sstevel@tonic-gate 		localinfo.bus_generation = hal->generation_count;
4057c478bd9Sstevel@tonic-gate 		localinfo.local_nodeID = hal->node_id;
4067c478bd9Sstevel@tonic-gate 		mutex_exit(&hal->topology_tree_mutex);
4077c478bd9Sstevel@tonic-gate 		impl_data = &localinfo;
4087c478bd9Sstevel@tonic-gate 
4097c478bd9Sstevel@tonic-gate 		ret = ndi_event_run_callbacks(hal->hal_ndi_event_hdl,
4107c478bd9Sstevel@tonic-gate 		    rdip, cookie, impl_data);
4117c478bd9Sstevel@tonic-gate 
4127c478bd9Sstevel@tonic-gate 		return (ret);
4137c478bd9Sstevel@tonic-gate 
4147c478bd9Sstevel@tonic-gate 	} else {
4157c478bd9Sstevel@tonic-gate 		ret = ndi_post_event(ddi_get_parent(dip), rdip, cookie,
4167c478bd9Sstevel@tonic-gate 		    impl_data);
4177c478bd9Sstevel@tonic-gate 		return (ret);
4187c478bd9Sstevel@tonic-gate 	}
4197c478bd9Sstevel@tonic-gate }
4207c478bd9Sstevel@tonic-gate 
4217c478bd9Sstevel@tonic-gate /*
4227c478bd9Sstevel@tonic-gate  * nx1394_define_events()
4237c478bd9Sstevel@tonic-gate  *    Allocates event handle for the hal dip and binds event set to it.
4247c478bd9Sstevel@tonic-gate  */
4257c478bd9Sstevel@tonic-gate int
nx1394_define_events(s1394_hal_t * hal)4267c478bd9Sstevel@tonic-gate nx1394_define_events(s1394_hal_t *hal)
4277c478bd9Sstevel@tonic-gate {
4287c478bd9Sstevel@tonic-gate 	int ret;
4297c478bd9Sstevel@tonic-gate 
4307c478bd9Sstevel@tonic-gate 	/* get event handle */
4317c478bd9Sstevel@tonic-gate 	ret = ndi_event_alloc_hdl(hal->halinfo.dip, hal->halinfo.hw_interrupt,
4327c478bd9Sstevel@tonic-gate 	    &hal->hal_ndi_event_hdl, NDI_SLEEP);
433*2570281cSToomas Soome 	if (ret == NDI_SUCCESS) {
4347c478bd9Sstevel@tonic-gate 		/* and bind to it */
4357c478bd9Sstevel@tonic-gate 		ret = ndi_event_bind_set(hal->hal_ndi_event_hdl, &nx1394_events,
4367c478bd9Sstevel@tonic-gate 		    NDI_SLEEP);
4377c478bd9Sstevel@tonic-gate 		if (ret != NDI_SUCCESS) {
4387c478bd9Sstevel@tonic-gate 			(void) ndi_event_free_hdl(hal->hal_ndi_event_hdl);
4397c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
4407c478bd9Sstevel@tonic-gate 		}
4417c478bd9Sstevel@tonic-gate 	}
4427c478bd9Sstevel@tonic-gate 
4437c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
4447c478bd9Sstevel@tonic-gate }
4457c478bd9Sstevel@tonic-gate 
4467c478bd9Sstevel@tonic-gate /*
4477c478bd9Sstevel@tonic-gate  * nx1394_undefine_events()
4487c478bd9Sstevel@tonic-gate  *    Unbinds event set bound to the hal and frees the event handle.
4497c478bd9Sstevel@tonic-gate  */
4507c478bd9Sstevel@tonic-gate void
nx1394_undefine_events(s1394_hal_t * hal)4517c478bd9Sstevel@tonic-gate nx1394_undefine_events(s1394_hal_t *hal)
4527c478bd9Sstevel@tonic-gate {
4537c478bd9Sstevel@tonic-gate 	int ret;
4547c478bd9Sstevel@tonic-gate 
4557c478bd9Sstevel@tonic-gate 	ret = ndi_event_unbind_set(hal->hal_ndi_event_hdl, &nx1394_events,
4567c478bd9Sstevel@tonic-gate 	    NDI_SLEEP);
457*2570281cSToomas Soome 	if (ret == NDI_SUCCESS)
4587c478bd9Sstevel@tonic-gate 		ret = ndi_event_free_hdl(hal->hal_ndi_event_hdl);
4597c478bd9Sstevel@tonic-gate }
460