xref: /illumos-gate/usr/src/uts/common/io/1394/h1394.c (revision 2570281c)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate /*
28*7c478bd9Sstevel@tonic-gate  * h1394.c
29*7c478bd9Sstevel@tonic-gate  *    1394 Services Layer HAL Interface
30*7c478bd9Sstevel@tonic-gate  *    Contains all of the routines that define the HAL to Services Layer
31*7c478bd9Sstevel@tonic-gate  *    interface
32*7c478bd9Sstevel@tonic-gate  */
33*7c478bd9Sstevel@tonic-gate 
34*7c478bd9Sstevel@tonic-gate #include <sys/conf.h>
35*7c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
36*7c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
37*7c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
38*7c478bd9Sstevel@tonic-gate #include <sys/sunndi.h>
39*7c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
40*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
41*7c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
42*7c478bd9Sstevel@tonic-gate #include <sys/thread.h>
43*7c478bd9Sstevel@tonic-gate #include <sys/proc.h>
44*7c478bd9Sstevel@tonic-gate #include <sys/disp.h>
45*7c478bd9Sstevel@tonic-gate #include <sys/time.h>
46*7c478bd9Sstevel@tonic-gate #include <sys/devctl.h>
47*7c478bd9Sstevel@tonic-gate #include <sys/1394/t1394.h>
48*7c478bd9Sstevel@tonic-gate #include <sys/1394/s1394.h>
49*7c478bd9Sstevel@tonic-gate #include <sys/1394/h1394.h>
50*7c478bd9Sstevel@tonic-gate #include <sys/1394/ieee1394.h>
51*7c478bd9Sstevel@tonic-gate 
52*7c478bd9Sstevel@tonic-gate 
53*7c478bd9Sstevel@tonic-gate extern struct bus_ops nx1394_busops;
54*7c478bd9Sstevel@tonic-gate extern int nx1394_define_events(s1394_hal_t *hal);
55*7c478bd9Sstevel@tonic-gate extern void nx1394_undefine_events(s1394_hal_t *hal);
56*7c478bd9Sstevel@tonic-gate extern int s1394_ignore_invalid_gap_cnt;
57*7c478bd9Sstevel@tonic-gate 
58*7c478bd9Sstevel@tonic-gate /*
59*7c478bd9Sstevel@tonic-gate  * Function:    h1394_init()
60*7c478bd9Sstevel@tonic-gate  * Input(s):    modlp			The structure containing all of the
61*7c478bd9Sstevel@tonic-gate  *					    HAL's relevant information
62*7c478bd9Sstevel@tonic-gate  *
63*7c478bd9Sstevel@tonic-gate  * Output(s):
64*7c478bd9Sstevel@tonic-gate  *
65*7c478bd9Sstevel@tonic-gate  * Description:	h1394_init() is called by the HAL's _init function and is
66*7c478bd9Sstevel@tonic-gate  *		used to set up the nexus bus ops.
67*7c478bd9Sstevel@tonic-gate  */
68*7c478bd9Sstevel@tonic-gate int
h1394_init(struct modlinkage * modlp)69*7c478bd9Sstevel@tonic-gate h1394_init(struct modlinkage *modlp)
70*7c478bd9Sstevel@tonic-gate {
71*7c478bd9Sstevel@tonic-gate 	struct dev_ops	*devops;
72*7c478bd9Sstevel@tonic-gate 
73*7c478bd9Sstevel@tonic-gate 	devops = ((struct modldrv *)(modlp->ml_linkage[0]))->drv_dev_ops;
74*7c478bd9Sstevel@tonic-gate 	devops->devo_bus_ops = &nx1394_busops;
75*7c478bd9Sstevel@tonic-gate 
76*7c478bd9Sstevel@tonic-gate 	return (0);
77*7c478bd9Sstevel@tonic-gate }
78*7c478bd9Sstevel@tonic-gate 
79*7c478bd9Sstevel@tonic-gate /*
80*7c478bd9Sstevel@tonic-gate  * Function:    h1394_fini()
81*7c478bd9Sstevel@tonic-gate  * Input(s):    modlp			The structure containing all of the
82*7c478bd9Sstevel@tonic-gate  *					    HAL's relevant information
83*7c478bd9Sstevel@tonic-gate  *
84*7c478bd9Sstevel@tonic-gate  * Output(s):
85*7c478bd9Sstevel@tonic-gate  *
86*7c478bd9Sstevel@tonic-gate  * Description:	h1394_fini() is called by the HAL's _fini function and is
87*7c478bd9Sstevel@tonic-gate  *		used to NULL out the nexus bus ops.
88*7c478bd9Sstevel@tonic-gate  */
89*7c478bd9Sstevel@tonic-gate void
h1394_fini(struct modlinkage * modlp)90*7c478bd9Sstevel@tonic-gate h1394_fini(struct modlinkage *modlp)
91*7c478bd9Sstevel@tonic-gate {
92*7c478bd9Sstevel@tonic-gate 	struct dev_ops	*devops;
93*7c478bd9Sstevel@tonic-gate 
94*7c478bd9Sstevel@tonic-gate 	devops = ((struct modldrv *)(modlp->ml_linkage[0]))->drv_dev_ops;
95*7c478bd9Sstevel@tonic-gate 	devops->devo_bus_ops = NULL;
96*7c478bd9Sstevel@tonic-gate }
97*7c478bd9Sstevel@tonic-gate 
98*7c478bd9Sstevel@tonic-gate /*
99*7c478bd9Sstevel@tonic-gate  * Function:    h1394_attach()
100*7c478bd9Sstevel@tonic-gate  * Input(s):    halinfo			The structure containing all of the
101*7c478bd9Sstevel@tonic-gate  *					    HAL's relevant information
102*7c478bd9Sstevel@tonic-gate  *		cmd			The ddi_attach_cmd_t that tells us
103*7c478bd9Sstevel@tonic-gate  *					    if this is a RESUME or a regular
104*7c478bd9Sstevel@tonic-gate  *					    attach() call
105*7c478bd9Sstevel@tonic-gate  *
106*7c478bd9Sstevel@tonic-gate  * Output(s):	sl_private		The HAL "handle" to be used for
107*7c478bd9Sstevel@tonic-gate  *					    all subsequent calls into the
108*7c478bd9Sstevel@tonic-gate  *					    1394 Software Framework
109*7c478bd9Sstevel@tonic-gate  *
110*7c478bd9Sstevel@tonic-gate  * Description:	h1394_attach() registers the HAL with the 1394 Software
111*7c478bd9Sstevel@tonic-gate  *		Framework.  It returns a HAL "handle" to be used for
112*7c478bd9Sstevel@tonic-gate  *		all subsequent calls into the 1394 Software Framework.
113*7c478bd9Sstevel@tonic-gate  */
114*7c478bd9Sstevel@tonic-gate int
h1394_attach(h1394_halinfo_t * halinfo,ddi_attach_cmd_t cmd,void ** sl_private)115*7c478bd9Sstevel@tonic-gate h1394_attach(h1394_halinfo_t *halinfo, ddi_attach_cmd_t cmd, void **sl_private)
116*7c478bd9Sstevel@tonic-gate {
117*7c478bd9Sstevel@tonic-gate 	s1394_hal_t	*hal;
118*7c478bd9Sstevel@tonic-gate 	int		ret;
119*7c478bd9Sstevel@tonic-gate 	char		buf[32];
120*7c478bd9Sstevel@tonic-gate 	uint_t		cmd_size;
121*7c478bd9Sstevel@tonic-gate 
122*7c478bd9Sstevel@tonic-gate 	ASSERT(sl_private != NULL);
123*7c478bd9Sstevel@tonic-gate 
124*7c478bd9Sstevel@tonic-gate 	/* If this is a DDI_RESUME, return success */
125*7c478bd9Sstevel@tonic-gate 	if (cmd == DDI_RESUME) {
126*7c478bd9Sstevel@tonic-gate 		hal = (s1394_hal_t *)(*sl_private);
127*7c478bd9Sstevel@tonic-gate 		/* If we have a 1394A PHY, then reset the "contender bit" */
128*7c478bd9Sstevel@tonic-gate 		if (hal->halinfo.phy == H1394_PHY_1394A)
129*7c478bd9Sstevel@tonic-gate 			(void) HAL_CALL(hal).set_contender_bit(
130*7c478bd9Sstevel@tonic-gate 			    hal->halinfo.hal_private);
131*7c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
132*7c478bd9Sstevel@tonic-gate 	} else if (cmd != DDI_ATTACH) {
133*7c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
134*7c478bd9Sstevel@tonic-gate 	}
135*7c478bd9Sstevel@tonic-gate 
136*7c478bd9Sstevel@tonic-gate 	/* Allocate space for s1394_hal_t */
137*7c478bd9Sstevel@tonic-gate 	hal = kmem_zalloc(sizeof (s1394_hal_t), KM_SLEEP);
138*7c478bd9Sstevel@tonic-gate 
139*7c478bd9Sstevel@tonic-gate 	/* Setup HAL state */
140*7c478bd9Sstevel@tonic-gate 	hal->hal_state = S1394_HAL_INIT;
141*7c478bd9Sstevel@tonic-gate 
142*7c478bd9Sstevel@tonic-gate 	/* Copy in the halinfo struct */
143*7c478bd9Sstevel@tonic-gate 	hal->halinfo = *halinfo;
144*7c478bd9Sstevel@tonic-gate 
145*7c478bd9Sstevel@tonic-gate 	/* Create the topology tree mutex */
146*7c478bd9Sstevel@tonic-gate 	mutex_init(&hal->topology_tree_mutex, NULL, MUTEX_DRIVER,
147*7c478bd9Sstevel@tonic-gate 	    hal->halinfo.hw_interrupt);
148*7c478bd9Sstevel@tonic-gate 
149*7c478bd9Sstevel@tonic-gate 	/* Create the Cycle Mater timer mutex */
150*7c478bd9Sstevel@tonic-gate 	mutex_init(&hal->cm_timer_mutex, NULL, MUTEX_DRIVER,
151*7c478bd9Sstevel@tonic-gate 	    hal->halinfo.hw_interrupt);
152*7c478bd9Sstevel@tonic-gate 
153*7c478bd9Sstevel@tonic-gate 	/* Initialize the Isoch CEC list */
154*7c478bd9Sstevel@tonic-gate 	hal->isoch_cec_list_head = NULL;
155*7c478bd9Sstevel@tonic-gate 	hal->isoch_cec_list_tail = NULL;
156*7c478bd9Sstevel@tonic-gate 	mutex_init(&hal->isoch_cec_list_mutex, NULL, MUTEX_DRIVER,
157*7c478bd9Sstevel@tonic-gate 	    hal->halinfo.hw_interrupt);
158*7c478bd9Sstevel@tonic-gate 
159*7c478bd9Sstevel@tonic-gate 	/* Initialize the Bus Manager node ID mutex and cv */
160*7c478bd9Sstevel@tonic-gate 	mutex_init(&hal->bus_mgr_node_mutex, NULL, MUTEX_DRIVER,
161*7c478bd9Sstevel@tonic-gate 	    hal->halinfo.hw_interrupt);
162*7c478bd9Sstevel@tonic-gate 	cv_init(&hal->bus_mgr_node_cv, NULL, CV_DRIVER,
163*7c478bd9Sstevel@tonic-gate 	    hal->halinfo.hw_interrupt);
164*7c478bd9Sstevel@tonic-gate 
165*7c478bd9Sstevel@tonic-gate 	/* Initialize the Bus Manager node ID - "-1" means undetermined */
166*7c478bd9Sstevel@tonic-gate 	hal->bus_mgr_node	= -1;
167*7c478bd9Sstevel@tonic-gate 	hal->incumbent_bus_mgr	= B_FALSE;
168*7c478bd9Sstevel@tonic-gate 
169*7c478bd9Sstevel@tonic-gate 	/* Initialize the Target list */
170*7c478bd9Sstevel@tonic-gate 	hal->target_head = NULL;
171*7c478bd9Sstevel@tonic-gate 	hal->target_tail = NULL;
172*7c478bd9Sstevel@tonic-gate 	rw_init(&hal->target_list_rwlock, NULL, RW_DRIVER,
173*7c478bd9Sstevel@tonic-gate 	    hal->halinfo.hw_interrupt);
174*7c478bd9Sstevel@tonic-gate 
175*7c478bd9Sstevel@tonic-gate 	/* Setup Request Q's */
176*7c478bd9Sstevel@tonic-gate 	hal->outstanding_q_head	= NULL;
177*7c478bd9Sstevel@tonic-gate 	hal->outstanding_q_tail	= NULL;
178*7c478bd9Sstevel@tonic-gate 	mutex_init(&hal->outstanding_q_mutex, NULL, MUTEX_DRIVER,
179*7c478bd9Sstevel@tonic-gate 	    hal->halinfo.hw_interrupt);
180*7c478bd9Sstevel@tonic-gate 	hal->pending_q_head	= NULL;
181*7c478bd9Sstevel@tonic-gate 	hal->pending_q_tail	= NULL;
182*7c478bd9Sstevel@tonic-gate 	mutex_init(&hal->pending_q_mutex, NULL, MUTEX_DRIVER,
183*7c478bd9Sstevel@tonic-gate 	    hal->halinfo.hw_interrupt);
184*7c478bd9Sstevel@tonic-gate 
185*7c478bd9Sstevel@tonic-gate 	/* Create the kmem_cache for command allocations */
186*7c478bd9Sstevel@tonic-gate 	(void) sprintf(buf, "hal%d_cache", ddi_get_instance(hal->halinfo.dip));
187*7c478bd9Sstevel@tonic-gate 	cmd_size = sizeof (cmd1394_cmd_t) + sizeof (s1394_cmd_priv_t) +
188*7c478bd9Sstevel@tonic-gate 	    hal->halinfo.hal_overhead;
189*7c478bd9Sstevel@tonic-gate 
190*7c478bd9Sstevel@tonic-gate 	hal->hal_kmem_cachep = kmem_cache_create(buf, cmd_size, 8, NULL, NULL,
191*7c478bd9Sstevel@tonic-gate 	    NULL, NULL, NULL, 0);
192*7c478bd9Sstevel@tonic-gate 
193*7c478bd9Sstevel@tonic-gate 	/* Setup the event stuff */
194*7c478bd9Sstevel@tonic-gate 	ret = nx1394_define_events(hal);
195*7c478bd9Sstevel@tonic-gate 	if (ret != DDI_SUCCESS) {
196*7c478bd9Sstevel@tonic-gate 		/* Clean up before leaving */
197*7c478bd9Sstevel@tonic-gate 		s1394_cleanup_for_detach(hal, H1394_CLEANUP_LEVEL0);
198*7c478bd9Sstevel@tonic-gate 
199*7c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
200*7c478bd9Sstevel@tonic-gate 	}
201*7c478bd9Sstevel@tonic-gate 
202*7c478bd9Sstevel@tonic-gate 	/* Initialize the mutexes and cv's used by the bus reset thread */
203*7c478bd9Sstevel@tonic-gate 	mutex_init(&hal->br_thread_mutex, NULL, MUTEX_DRIVER,
204*7c478bd9Sstevel@tonic-gate 	    hal->halinfo.hw_interrupt);
205*7c478bd9Sstevel@tonic-gate 	cv_init(&hal->br_thread_cv, NULL, CV_DRIVER, hal->halinfo.hw_interrupt);
206*7c478bd9Sstevel@tonic-gate 	mutex_init(&hal->br_cmplq_mutex, NULL, MUTEX_DRIVER,
207*7c478bd9Sstevel@tonic-gate 	    hal->halinfo.hw_interrupt);
208*7c478bd9Sstevel@tonic-gate 	cv_init(&hal->br_cmplq_cv, NULL, CV_DRIVER, hal->halinfo.hw_interrupt);
209*7c478bd9Sstevel@tonic-gate 
210*7c478bd9Sstevel@tonic-gate 	/*
211*7c478bd9Sstevel@tonic-gate 	 * Create a bus reset thread to handle the device discovery.
212*7c478bd9Sstevel@tonic-gate 	 *    It should take the default stack sizes, it should run
213*7c478bd9Sstevel@tonic-gate 	 *    the s1394_br_thread() routine at the start, passing the
214*7c478bd9Sstevel@tonic-gate 	 *    HAL pointer as its argument.  The thread should be put
215*7c478bd9Sstevel@tonic-gate 	 *    on processor p0, its state should be set to runnable,
216*7c478bd9Sstevel@tonic-gate 	 *    but not yet on a processor, and its scheduling priority
217*7c478bd9Sstevel@tonic-gate 	 *    should be the minimum level of any system class.
218*7c478bd9Sstevel@tonic-gate 	 */
219*7c478bd9Sstevel@tonic-gate 	hal->br_thread = thread_create((caddr_t)NULL, 0, s1394_br_thread,
220*7c478bd9Sstevel@tonic-gate 	    hal, 0, &p0, TS_RUN, minclsyspri);
221*7c478bd9Sstevel@tonic-gate 
222*7c478bd9Sstevel@tonic-gate 	/* Until we see a bus reset this HAL has no nodes */
223*7c478bd9Sstevel@tonic-gate 	hal->number_of_nodes = 0;
224*7c478bd9Sstevel@tonic-gate 	hal->num_bus_reset_till_fail = NUM_BR_FAIL;
225*7c478bd9Sstevel@tonic-gate 
226*7c478bd9Sstevel@tonic-gate 	/* Initialize the SelfID Info */
227*7c478bd9Sstevel@tonic-gate 	hal->current_buffer = 0;
228*7c478bd9Sstevel@tonic-gate 	hal->selfid_buf0 = kmem_zalloc(S1394_SELFID_BUF_SIZE, KM_SLEEP);
229*7c478bd9Sstevel@tonic-gate 	hal->selfid_buf1 = kmem_zalloc(S1394_SELFID_BUF_SIZE, KM_SLEEP);
230*7c478bd9Sstevel@tonic-gate 
231*7c478bd9Sstevel@tonic-gate 	/* Initialize kstat structures */
232*7c478bd9Sstevel@tonic-gate 	ret = s1394_kstat_init(hal);
233*7c478bd9Sstevel@tonic-gate 	if (ret != DDI_SUCCESS) {
234*7c478bd9Sstevel@tonic-gate 		/* Clean up before leaving */
235*7c478bd9Sstevel@tonic-gate 		s1394_cleanup_for_detach(hal, H1394_CLEANUP_LEVEL3);
236*7c478bd9Sstevel@tonic-gate 
237*7c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
238*7c478bd9Sstevel@tonic-gate 	}
239*7c478bd9Sstevel@tonic-gate 	hal->hal_kstats->guid = hal->halinfo.guid;
240*7c478bd9Sstevel@tonic-gate 
241*7c478bd9Sstevel@tonic-gate 	/* Setup the node tree pointers */
242*7c478bd9Sstevel@tonic-gate 	hal->old_tree	   = &hal->last_valid_tree[0];
243*7c478bd9Sstevel@tonic-gate 	hal->topology_tree = &hal->current_tree[0];
244*7c478bd9Sstevel@tonic-gate 
245*7c478bd9Sstevel@tonic-gate 	/* Initialize the local Config ROM entry */
246*7c478bd9Sstevel@tonic-gate 	ret = s1394_init_local_config_rom(hal);
247*7c478bd9Sstevel@tonic-gate 	if (ret != DDI_SUCCESS) {
248*7c478bd9Sstevel@tonic-gate 		/* Clean up before leaving */
249*7c478bd9Sstevel@tonic-gate 		s1394_cleanup_for_detach(hal, H1394_CLEANUP_LEVEL4);
250*7c478bd9Sstevel@tonic-gate 
251*7c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
252*7c478bd9Sstevel@tonic-gate 	}
253*7c478bd9Sstevel@tonic-gate 
254*7c478bd9Sstevel@tonic-gate 	/* Initialize 1394 Address Space */
255*7c478bd9Sstevel@tonic-gate 	ret = s1394_init_addr_space(hal);
256*7c478bd9Sstevel@tonic-gate 	if (ret != DDI_SUCCESS) {
257*7c478bd9Sstevel@tonic-gate 		/* Clean up before leaving */
258*7c478bd9Sstevel@tonic-gate 		s1394_cleanup_for_detach(hal, H1394_CLEANUP_LEVEL5);
259*7c478bd9Sstevel@tonic-gate 
260*7c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
261*7c478bd9Sstevel@tonic-gate 	}
262*7c478bd9Sstevel@tonic-gate 
263*7c478bd9Sstevel@tonic-gate 	/* Initialize FCP subsystem */
264*7c478bd9Sstevel@tonic-gate 	ret = s1394_fcp_hal_init(hal);
265*7c478bd9Sstevel@tonic-gate 	if (ret != DDI_SUCCESS) {
266*7c478bd9Sstevel@tonic-gate 		/* Clean up before leaving */
267*7c478bd9Sstevel@tonic-gate 		s1394_cleanup_for_detach(hal, H1394_CLEANUP_LEVEL6);
268*7c478bd9Sstevel@tonic-gate 
269*7c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
270*7c478bd9Sstevel@tonic-gate 	}
271*7c478bd9Sstevel@tonic-gate 
272*7c478bd9Sstevel@tonic-gate 	/* Initialize the IRM node ID - "-1" means invalid, undetermined */
273*7c478bd9Sstevel@tonic-gate 	hal->IRM_node = -1;
274*7c478bd9Sstevel@tonic-gate 
275*7c478bd9Sstevel@tonic-gate 	/* If we have a 1394A PHY, then set the "contender bit" */
276*7c478bd9Sstevel@tonic-gate 	if (hal->halinfo.phy == H1394_PHY_1394A)
277*7c478bd9Sstevel@tonic-gate 		(void) HAL_CALL(hal).set_contender_bit(
278*7c478bd9Sstevel@tonic-gate 		    hal->halinfo.hal_private);
279*7c478bd9Sstevel@tonic-gate 
280*7c478bd9Sstevel@tonic-gate 	/* Add into linked list */
281*7c478bd9Sstevel@tonic-gate 	mutex_enter(&s1394_statep->hal_list_mutex);
282*7c478bd9Sstevel@tonic-gate 	if ((s1394_statep->hal_head == NULL) &&
283*7c478bd9Sstevel@tonic-gate 	    (s1394_statep->hal_tail == NULL)) {
284*7c478bd9Sstevel@tonic-gate 		s1394_statep->hal_head = hal;
285*7c478bd9Sstevel@tonic-gate 		s1394_statep->hal_tail = hal;
286*7c478bd9Sstevel@tonic-gate 	} else {
287*7c478bd9Sstevel@tonic-gate 		s1394_statep->hal_tail->hal_next = hal;
288*7c478bd9Sstevel@tonic-gate 		hal->hal_prev = s1394_statep->hal_tail;
289*7c478bd9Sstevel@tonic-gate 		s1394_statep->hal_tail = hal;
290*7c478bd9Sstevel@tonic-gate 	}
291*7c478bd9Sstevel@tonic-gate 	mutex_exit(&s1394_statep->hal_list_mutex);
292*7c478bd9Sstevel@tonic-gate 
293*7c478bd9Sstevel@tonic-gate 	/* Fill in services layer private info */
294*7c478bd9Sstevel@tonic-gate 	*sl_private = (void *)hal;
295*7c478bd9Sstevel@tonic-gate 
296*7c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
297*7c478bd9Sstevel@tonic-gate }
298*7c478bd9Sstevel@tonic-gate 
299*7c478bd9Sstevel@tonic-gate /*
300*7c478bd9Sstevel@tonic-gate  * Function:    h1394_detach()
301*7c478bd9Sstevel@tonic-gate  * Input(s):    sl_private		The HAL "handle" returned by
302*7c478bd9Sstevel@tonic-gate  *					    h1394_attach()
303*7c478bd9Sstevel@tonic-gate  *		cmd			The ddi_detach_cmd_t that tells us
304*7c478bd9Sstevel@tonic-gate  *					    if this is a SUSPEND or a regular
305*7c478bd9Sstevel@tonic-gate  *					    detach() call
306*7c478bd9Sstevel@tonic-gate  *
307*7c478bd9Sstevel@tonic-gate  * Output(s):	DDI_SUCCESS		HAL successfully detached
308*7c478bd9Sstevel@tonic-gate  *		DDI_FAILURE		HAL failed to detach
309*7c478bd9Sstevel@tonic-gate  *
310*7c478bd9Sstevel@tonic-gate  * Description:	h1394_detach() unregisters the HAL from the 1394 Software
311*7c478bd9Sstevel@tonic-gate  *		Framework.  It can be called during a SUSPEND operation or
312*7c478bd9Sstevel@tonic-gate  *		for a real detach() event.
313*7c478bd9Sstevel@tonic-gate  */
314*7c478bd9Sstevel@tonic-gate int
h1394_detach(void ** sl_private,ddi_detach_cmd_t cmd)315*7c478bd9Sstevel@tonic-gate h1394_detach(void **sl_private, ddi_detach_cmd_t cmd)
316*7c478bd9Sstevel@tonic-gate {
317*7c478bd9Sstevel@tonic-gate 	s1394_hal_t	*hal;
318*7c478bd9Sstevel@tonic-gate 
319*7c478bd9Sstevel@tonic-gate 	hal = (s1394_hal_t *)(*sl_private);
320*7c478bd9Sstevel@tonic-gate 
321*7c478bd9Sstevel@tonic-gate 	switch (cmd) {
322*7c478bd9Sstevel@tonic-gate 	case DDI_DETACH:
323*7c478bd9Sstevel@tonic-gate 		/* Clean up before leaving */
324*7c478bd9Sstevel@tonic-gate 		s1394_cleanup_for_detach(hal, H1394_CLEANUP_LEVEL7);
325*7c478bd9Sstevel@tonic-gate 		/* NULL out the HAL "handle" */
326*7c478bd9Sstevel@tonic-gate 		*sl_private = NULL;
327*7c478bd9Sstevel@tonic-gate 		break;
328*7c478bd9Sstevel@tonic-gate 
329*7c478bd9Sstevel@tonic-gate 	case DDI_SUSPEND:
330*7c478bd9Sstevel@tonic-gate 		/* Turn off any timers that might be set */
331*7c478bd9Sstevel@tonic-gate 		s1394_destroy_timers(hal);
332*7c478bd9Sstevel@tonic-gate 		/* Set the hal_was_suspended bit */
333*7c478bd9Sstevel@tonic-gate 		hal->hal_was_suspended = B_TRUE;
334*7c478bd9Sstevel@tonic-gate 		break;
335*7c478bd9Sstevel@tonic-gate 
336*7c478bd9Sstevel@tonic-gate 	default:
337*7c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
338*7c478bd9Sstevel@tonic-gate 	}
339*7c478bd9Sstevel@tonic-gate 
340*7c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
341*7c478bd9Sstevel@tonic-gate }
342*7c478bd9Sstevel@tonic-gate 
343*7c478bd9Sstevel@tonic-gate /*
344*7c478bd9Sstevel@tonic-gate  * Function:    h1394_alloc_cmd()
345*7c478bd9Sstevel@tonic-gate  * Input(s):    sl_private		The HAL "handle" returned by
346*7c478bd9Sstevel@tonic-gate  *					    h1394_attach()
347*7c478bd9Sstevel@tonic-gate  *		flags			The flags parameter is described below
348*7c478bd9Sstevel@tonic-gate  *
349*7c478bd9Sstevel@tonic-gate  * Output(s):	cmdp			Pointer to the newly allocated command
350*7c478bd9Sstevel@tonic-gate  *		hal_priv_ptr		Offset into the command, points to
351*7c478bd9Sstevel@tonic-gate  *					    the HAL's private area
352*7c478bd9Sstevel@tonic-gate  *
353*7c478bd9Sstevel@tonic-gate  * Description:	h1394_alloc_cmd() allocates a command for use with the
354*7c478bd9Sstevel@tonic-gate  *		h1394_read_request(), h1394_write_request(), or
355*7c478bd9Sstevel@tonic-gate  *		h1394_lock_request() interfaces of the 1394 Software Framework.
356*7c478bd9Sstevel@tonic-gate  *		By default, h1394_alloc_cmd() may sleep while allocating
357*7c478bd9Sstevel@tonic-gate  *		memory for the command structure.  If this is undesirable,
358*7c478bd9Sstevel@tonic-gate  *		the HAL may set the H1394_ALLOC_CMD_NOSLEEP bit in the flags
359*7c478bd9Sstevel@tonic-gate  *		parameter.
360*7c478bd9Sstevel@tonic-gate  */
361*7c478bd9Sstevel@tonic-gate int
h1394_alloc_cmd(void * sl_private,uint_t flags,cmd1394_cmd_t ** cmdp,h1394_cmd_priv_t ** hal_priv_ptr)362*7c478bd9Sstevel@tonic-gate h1394_alloc_cmd(void *sl_private, uint_t flags, cmd1394_cmd_t **cmdp,
363*7c478bd9Sstevel@tonic-gate     h1394_cmd_priv_t **hal_priv_ptr)
364*7c478bd9Sstevel@tonic-gate {
365*7c478bd9Sstevel@tonic-gate 	s1394_hal_t	 *hal;
366*7c478bd9Sstevel@tonic-gate 	s1394_cmd_priv_t *s_priv;
367*7c478bd9Sstevel@tonic-gate 
368*7c478bd9Sstevel@tonic-gate 	hal = (s1394_hal_t *)sl_private;
369*7c478bd9Sstevel@tonic-gate 
370*7c478bd9Sstevel@tonic-gate 	if (s1394_alloc_cmd(hal, flags, cmdp) != DDI_SUCCESS) {
371*7c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
372*7c478bd9Sstevel@tonic-gate 	}
373*7c478bd9Sstevel@tonic-gate 
374*7c478bd9Sstevel@tonic-gate 	/* Get the Services Layer private area */
375*7c478bd9Sstevel@tonic-gate 	s_priv = S1394_GET_CMD_PRIV(*cmdp);
376*7c478bd9Sstevel@tonic-gate 
377*7c478bd9Sstevel@tonic-gate 	*hal_priv_ptr = &s_priv->hal_cmd_private;
378*7c478bd9Sstevel@tonic-gate 
379*7c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
380*7c478bd9Sstevel@tonic-gate }
381*7c478bd9Sstevel@tonic-gate 
382*7c478bd9Sstevel@tonic-gate /*
383*7c478bd9Sstevel@tonic-gate  * Function:    h1394_free_cmd()
384*7c478bd9Sstevel@tonic-gate  * Input(s):    sl_private		The HAL "handle" returned by
385*7c478bd9Sstevel@tonic-gate  *					    h1394_attach()
386*7c478bd9Sstevel@tonic-gate  *		cmdp			Pointer to the command to be freed
387*7c478bd9Sstevel@tonic-gate  *
388*7c478bd9Sstevel@tonic-gate  * Output(s):	DDI_SUCCESS		HAL successfully freed command
389*7c478bd9Sstevel@tonic-gate  *		DDI_FAILURE		HAL failed to free command
390*7c478bd9Sstevel@tonic-gate  *
391*7c478bd9Sstevel@tonic-gate  * Description:	h1394_free_cmd() attempts to free a command that has previously
392*7c478bd9Sstevel@tonic-gate  *		been allocated by the HAL.  It is possible for h1394_free_cmd()
393*7c478bd9Sstevel@tonic-gate  *		to fail because the command is currently in-use by the 1394
394*7c478bd9Sstevel@tonic-gate  *		Software Framework.
395*7c478bd9Sstevel@tonic-gate  */
396*7c478bd9Sstevel@tonic-gate int
h1394_free_cmd(void * sl_private,cmd1394_cmd_t ** cmdp)397*7c478bd9Sstevel@tonic-gate h1394_free_cmd(void *sl_private, cmd1394_cmd_t **cmdp)
398*7c478bd9Sstevel@tonic-gate {
399*7c478bd9Sstevel@tonic-gate 	s1394_hal_t	 *hal;
400*7c478bd9Sstevel@tonic-gate 	s1394_cmd_priv_t *s_priv;
401*7c478bd9Sstevel@tonic-gate 
402*7c478bd9Sstevel@tonic-gate 	hal = (s1394_hal_t *)sl_private;
403*7c478bd9Sstevel@tonic-gate 
404*7c478bd9Sstevel@tonic-gate 	/* Get the Services Layer private area */
405*7c478bd9Sstevel@tonic-gate 	s_priv = S1394_GET_CMD_PRIV(*cmdp);
406*7c478bd9Sstevel@tonic-gate 
407*7c478bd9Sstevel@tonic-gate 	/* Check that command isn't in use */
408*7c478bd9Sstevel@tonic-gate 	if (s_priv->cmd_in_use == B_TRUE) {
409*7c478bd9Sstevel@tonic-gate 		ASSERT(s_priv->cmd_in_use == B_FALSE);
410*7c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
411*7c478bd9Sstevel@tonic-gate 	}
412*7c478bd9Sstevel@tonic-gate 
413*7c478bd9Sstevel@tonic-gate 	kmem_cache_free(hal->hal_kmem_cachep, *cmdp);
414*7c478bd9Sstevel@tonic-gate 
415*7c478bd9Sstevel@tonic-gate 	/* Command pointer is set to NULL before returning */
416*7c478bd9Sstevel@tonic-gate 	*cmdp = NULL;
417*7c478bd9Sstevel@tonic-gate 
418*7c478bd9Sstevel@tonic-gate 	/* kstats - number of cmds freed */
419*7c478bd9Sstevel@tonic-gate 	hal->hal_kstats->cmd_free++;
420*7c478bd9Sstevel@tonic-gate 
421*7c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
422*7c478bd9Sstevel@tonic-gate }
423*7c478bd9Sstevel@tonic-gate 
424*7c478bd9Sstevel@tonic-gate /*
425*7c478bd9Sstevel@tonic-gate  * Function:    h1394_cmd_is_complete()
426*7c478bd9Sstevel@tonic-gate  * Input(s):    sl_private		The HAL "handle" returned by
427*7c478bd9Sstevel@tonic-gate  *					    h1394_attach()
428*7c478bd9Sstevel@tonic-gate  *		command_id		Pointer to the command that has
429*7c478bd9Sstevel@tonic-gate  *					    just completed
430*7c478bd9Sstevel@tonic-gate  *		cmd_type		AT_RESP => AT response or ATREQ =
431*7c478bd9Sstevel@tonic-gate  *					    AT request
432*7c478bd9Sstevel@tonic-gate  *		status			Command's completion status
433*7c478bd9Sstevel@tonic-gate  *
434*7c478bd9Sstevel@tonic-gate  * Output(s):	None
435*7c478bd9Sstevel@tonic-gate  *
436*7c478bd9Sstevel@tonic-gate  * Description:	h1394_cmd_is_complete() is called by the HAL whenever an
437*7c478bd9Sstevel@tonic-gate  *		outstanding command has completed (successfully or otherwise).
438*7c478bd9Sstevel@tonic-gate  *		After determining whether it was an AT request or and AT
439*7c478bd9Sstevel@tonic-gate  *		response that we are handling, the command is dispatched to
440*7c478bd9Sstevel@tonic-gate  *		the appropriate handler in the 1394 Software Framework.
441*7c478bd9Sstevel@tonic-gate  */
442*7c478bd9Sstevel@tonic-gate void
h1394_cmd_is_complete(void * sl_private,cmd1394_cmd_t * command_id,uint32_t cmd_type,int status)443*7c478bd9Sstevel@tonic-gate h1394_cmd_is_complete(void *sl_private, cmd1394_cmd_t *command_id,
444*7c478bd9Sstevel@tonic-gate     uint32_t cmd_type, int status)
445*7c478bd9Sstevel@tonic-gate {
446*7c478bd9Sstevel@tonic-gate 	s1394_hal_t	*hal;
447*7c478bd9Sstevel@tonic-gate 	dev_info_t	*dip;
448*7c478bd9Sstevel@tonic-gate 
449*7c478bd9Sstevel@tonic-gate 	hal = (s1394_hal_t *)sl_private;
450*7c478bd9Sstevel@tonic-gate 
451*7c478bd9Sstevel@tonic-gate 	/* Is it AT_RESP or AT_REQ? */
452*7c478bd9Sstevel@tonic-gate 	switch (cmd_type) {
453*7c478bd9Sstevel@tonic-gate 	case H1394_AT_REQ:
454*7c478bd9Sstevel@tonic-gate 		s1394_atreq_cmd_complete(hal, command_id, status);
455*7c478bd9Sstevel@tonic-gate 		break;
456*7c478bd9Sstevel@tonic-gate 
457*7c478bd9Sstevel@tonic-gate 	case H1394_AT_RESP:
458*7c478bd9Sstevel@tonic-gate 		s1394_atresp_cmd_complete(hal, command_id, status);
459*7c478bd9Sstevel@tonic-gate 		break;
460*7c478bd9Sstevel@tonic-gate 
461*7c478bd9Sstevel@tonic-gate 	default:
462*7c478bd9Sstevel@tonic-gate 		dip = hal->halinfo.dip;
463*7c478bd9Sstevel@tonic-gate 
464*7c478bd9Sstevel@tonic-gate 		/* An unexpected error in the HAL */
465*7c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, HALT_ERROR_MESSAGE,
466*7c478bd9Sstevel@tonic-gate 		    ddi_node_name(dip), ddi_get_instance(dip));
467*7c478bd9Sstevel@tonic-gate 
468*7c478bd9Sstevel@tonic-gate 		/* Disable the HAL */
469*7c478bd9Sstevel@tonic-gate 		s1394_hal_shutdown(hal, B_TRUE);
470*7c478bd9Sstevel@tonic-gate 
471*7c478bd9Sstevel@tonic-gate 		break;
472*7c478bd9Sstevel@tonic-gate 	}
473*7c478bd9Sstevel@tonic-gate }
474*7c478bd9Sstevel@tonic-gate 
475*7c478bd9Sstevel@tonic-gate /*
476*7c478bd9Sstevel@tonic-gate  * Function:    h1394_bus_reset()
477*7c478bd9Sstevel@tonic-gate  * Input(s):    sl_private		The HAL "handle" returned by
478*7c478bd9Sstevel@tonic-gate  *					    h1394_attach()
479*7c478bd9Sstevel@tonic-gate  *
480*7c478bd9Sstevel@tonic-gate  * Output(s):	selfid_buf_addr		The pointer to a buffer into which
481*7c478bd9Sstevel@tonic-gate  *					    any Self ID packets should be put
482*7c478bd9Sstevel@tonic-gate  *
483*7c478bd9Sstevel@tonic-gate  * Description:	h1394_bus_reset() is called whenever a 1394 bus reset event
484*7c478bd9Sstevel@tonic-gate  *		is detected by the HAL.  This routine simply prepares for
485*7c478bd9Sstevel@tonic-gate  *		the subsequent Self ID packets.
486*7c478bd9Sstevel@tonic-gate  */
487*7c478bd9Sstevel@tonic-gate void
h1394_bus_reset(void * sl_private,void ** selfid_buf_addr)488*7c478bd9Sstevel@tonic-gate h1394_bus_reset(void *sl_private, void **selfid_buf_addr)
489*7c478bd9Sstevel@tonic-gate {
490*7c478bd9Sstevel@tonic-gate 	s1394_hal_t	*hal;
491*7c478bd9Sstevel@tonic-gate 
492*7c478bd9Sstevel@tonic-gate 	hal = (s1394_hal_t *)sl_private;
493*7c478bd9Sstevel@tonic-gate 
494*7c478bd9Sstevel@tonic-gate 	mutex_enter(&hal->topology_tree_mutex);
495*7c478bd9Sstevel@tonic-gate 
496*7c478bd9Sstevel@tonic-gate 	/* Update the HAL's state */
497*7c478bd9Sstevel@tonic-gate 	if (hal->hal_state != S1394_HAL_SHUTDOWN) {
498*7c478bd9Sstevel@tonic-gate 		hal->hal_state = S1394_HAL_RESET;
499*7c478bd9Sstevel@tonic-gate 	} else {
500*7c478bd9Sstevel@tonic-gate 		mutex_exit(&hal->topology_tree_mutex);
501*7c478bd9Sstevel@tonic-gate 		return;
502*7c478bd9Sstevel@tonic-gate 	}
503*7c478bd9Sstevel@tonic-gate 
504*7c478bd9Sstevel@tonic-gate 	if (hal->initiated_bus_reset == B_TRUE) {
505*7c478bd9Sstevel@tonic-gate 		hal->initiated_bus_reset = B_FALSE;
506*7c478bd9Sstevel@tonic-gate 		if (hal->num_bus_reset_till_fail > 0) {
507*7c478bd9Sstevel@tonic-gate 			hal->num_bus_reset_till_fail--;
508*7c478bd9Sstevel@tonic-gate 		}
509*7c478bd9Sstevel@tonic-gate 	} else {
510*7c478bd9Sstevel@tonic-gate 		hal->num_bus_reset_till_fail = NUM_BR_FAIL;
511*7c478bd9Sstevel@tonic-gate 	}
512*7c478bd9Sstevel@tonic-gate 
513*7c478bd9Sstevel@tonic-gate 	/* Reset the IRM node ID */
514*7c478bd9Sstevel@tonic-gate 	hal->IRM_node = -1;
515*7c478bd9Sstevel@tonic-gate 
516*7c478bd9Sstevel@tonic-gate 	/* Slowest node defaults to IEEE1394_S400 */
517*7c478bd9Sstevel@tonic-gate 	hal->slowest_node_speed = IEEE1394_S400;
518*7c478bd9Sstevel@tonic-gate 
519*7c478bd9Sstevel@tonic-gate 	/* Pick a SelfID buffer to give */
520*7c478bd9Sstevel@tonic-gate 	if (hal->current_buffer == 0) {
521*7c478bd9Sstevel@tonic-gate 		*selfid_buf_addr = (void *)hal->selfid_buf1;
522*7c478bd9Sstevel@tonic-gate 		hal->current_buffer = 1;
523*7c478bd9Sstevel@tonic-gate 	} else {
524*7c478bd9Sstevel@tonic-gate 		*selfid_buf_addr = (void *)hal->selfid_buf0;
525*7c478bd9Sstevel@tonic-gate 		hal->current_buffer = 0;
526*7c478bd9Sstevel@tonic-gate 	}
527*7c478bd9Sstevel@tonic-gate 
528*7c478bd9Sstevel@tonic-gate 	/* Disable the CSR topology_map (temporarily) */
529*7c478bd9Sstevel@tonic-gate 	s1394_CSR_topology_map_disable(hal);
530*7c478bd9Sstevel@tonic-gate 
531*7c478bd9Sstevel@tonic-gate 	mutex_exit(&hal->topology_tree_mutex);
532*7c478bd9Sstevel@tonic-gate 
533*7c478bd9Sstevel@tonic-gate 	/* Reset the Bus Manager node ID */
534*7c478bd9Sstevel@tonic-gate 	mutex_enter(&hal->bus_mgr_node_mutex);
535*7c478bd9Sstevel@tonic-gate 	hal->bus_mgr_node = -1;
536*7c478bd9Sstevel@tonic-gate 	mutex_exit(&hal->bus_mgr_node_mutex);
537*7c478bd9Sstevel@tonic-gate }
538*7c478bd9Sstevel@tonic-gate 
539*7c478bd9Sstevel@tonic-gate /*
540*7c478bd9Sstevel@tonic-gate  * Function:    h1394_self_ids()
541*7c478bd9Sstevel@tonic-gate  * Input(s):    sl_private		The HAL "handle" returned by
542*7c478bd9Sstevel@tonic-gate  *					    h1394_attach()
543*7c478bd9Sstevel@tonic-gate  *		selfid_buf_addr		Pointer to the Self ID buffer
544*7c478bd9Sstevel@tonic-gate  *		selfid_size		The size of the filled part of the
545*7c478bd9Sstevel@tonic-gate  *					    Self ID buffer
546*7c478bd9Sstevel@tonic-gate  *		node_id			The local (host) node ID for the
547*7c478bd9Sstevel@tonic-gate  *					    current generation
548*7c478bd9Sstevel@tonic-gate  *		generation_count	The current generation number
549*7c478bd9Sstevel@tonic-gate  *
550*7c478bd9Sstevel@tonic-gate  * Output(s):	None
551*7c478bd9Sstevel@tonic-gate  *
552*7c478bd9Sstevel@tonic-gate  * Description:	h1394_self_ids() does alot of the work at bus reset.  It
553*7c478bd9Sstevel@tonic-gate  *		takes the Self ID packets and parses them, builds a topology
554*7c478bd9Sstevel@tonic-gate  *		tree representation of them, calculates gap count, IRM, speed
555*7c478bd9Sstevel@tonic-gate  *		map, does any node matching that's possible, and then wakes
556*7c478bd9Sstevel@tonic-gate  *		up the br_thread.
557*7c478bd9Sstevel@tonic-gate  */
558*7c478bd9Sstevel@tonic-gate void
h1394_self_ids(void * sl_private,void * selfid_buf_addr,uint32_t selfid_size,uint32_t node_id,uint32_t generation_count)559*7c478bd9Sstevel@tonic-gate h1394_self_ids(void *sl_private, void *selfid_buf_addr, uint32_t selfid_size,
560*7c478bd9Sstevel@tonic-gate     uint32_t node_id, uint32_t generation_count)
561*7c478bd9Sstevel@tonic-gate {
562*7c478bd9Sstevel@tonic-gate 	s1394_hal_t	*hal;
563*7c478bd9Sstevel@tonic-gate 	int		diameter;
564*7c478bd9Sstevel@tonic-gate 	uint_t		gen_diff, gen_rollover;
565*7c478bd9Sstevel@tonic-gate 	boolean_t	tree_copied = B_FALSE;
566*7c478bd9Sstevel@tonic-gate 	ushort_t	saved_number_of_nodes;
567*7c478bd9Sstevel@tonic-gate 
568*7c478bd9Sstevel@tonic-gate 	/*
569*7c478bd9Sstevel@tonic-gate 	 * NOTE: current topology tree is referred to as topology_tree
570*7c478bd9Sstevel@tonic-gate 	 * and the old topology tree is referred to as old_tree.
571*7c478bd9Sstevel@tonic-gate 	 * tree_valid indicates selfID buffer checked out OK and we were
572*7c478bd9Sstevel@tonic-gate 	 * able to build the topology tree.
573*7c478bd9Sstevel@tonic-gate 	 * tree_processed indicates we read the config ROMs as needed.
574*7c478bd9Sstevel@tonic-gate 	 */
575*7c478bd9Sstevel@tonic-gate 	hal = (s1394_hal_t *)sl_private;
576*7c478bd9Sstevel@tonic-gate 
577*7c478bd9Sstevel@tonic-gate 	/* Lock the topology tree */
578*7c478bd9Sstevel@tonic-gate 	mutex_enter(&hal->topology_tree_mutex);
579*7c478bd9Sstevel@tonic-gate 	if (hal->hal_state == S1394_HAL_SHUTDOWN) {
580*7c478bd9Sstevel@tonic-gate 		mutex_exit(&hal->topology_tree_mutex);
581*7c478bd9Sstevel@tonic-gate 		return;
582*7c478bd9Sstevel@tonic-gate 	}
583*7c478bd9Sstevel@tonic-gate 
584*7c478bd9Sstevel@tonic-gate 	/* kstats - number of selfid completes */
585*7c478bd9Sstevel@tonic-gate 	hal->hal_kstats->selfid_complete++;
586*7c478bd9Sstevel@tonic-gate 
587*7c478bd9Sstevel@tonic-gate 	if (generation_count > hal->generation_count) {
588*7c478bd9Sstevel@tonic-gate 		gen_diff = generation_count - hal->generation_count;
589*7c478bd9Sstevel@tonic-gate 		hal->hal_kstats->bus_reset += gen_diff;
590*7c478bd9Sstevel@tonic-gate 	} else {
591*7c478bd9Sstevel@tonic-gate 		gen_diff = hal->generation_count - generation_count;
592*7c478bd9Sstevel@tonic-gate 		/* Use max_generation to determine how many bus resets */
593*7c478bd9Sstevel@tonic-gate 		hal->hal_kstats->bus_reset +=
594*7c478bd9Sstevel@tonic-gate 		    (hal->halinfo.max_generation - gen_diff);
595*7c478bd9Sstevel@tonic-gate 	}
596*7c478bd9Sstevel@tonic-gate 
597*7c478bd9Sstevel@tonic-gate 	/*
598*7c478bd9Sstevel@tonic-gate 	 * If the current tree has a valid topology tree (selfids
599*7c478bd9Sstevel@tonic-gate 	 * checked out OK etc) and config roms read as needed,
600*7c478bd9Sstevel@tonic-gate 	 * then make it the old tree before building a new one.
601*7c478bd9Sstevel@tonic-gate 	 */
602*7c478bd9Sstevel@tonic-gate 	if ((hal->topology_tree_valid == B_TRUE) &&
603*7c478bd9Sstevel@tonic-gate 	    (hal->topology_tree_processed == B_TRUE)) {
604*7c478bd9Sstevel@tonic-gate 		/* Trees are switched after the copy completes */
605*7c478bd9Sstevel@tonic-gate 		s1394_copy_old_tree(hal);
606*7c478bd9Sstevel@tonic-gate 		tree_copied = B_TRUE;
607*7c478bd9Sstevel@tonic-gate 	}
608*7c478bd9Sstevel@tonic-gate 
609*7c478bd9Sstevel@tonic-gate 	/* Set the new generation and node id */
610*7c478bd9Sstevel@tonic-gate 	hal->node_id = node_id;
611*7c478bd9Sstevel@tonic-gate 	hal->generation_count = generation_count;
612*7c478bd9Sstevel@tonic-gate 
613*7c478bd9Sstevel@tonic-gate 	/* Invalidate the current topology tree */
614*7c478bd9Sstevel@tonic-gate 	hal->topology_tree_valid = B_FALSE;
615*7c478bd9Sstevel@tonic-gate 	hal->topology_tree_processed = B_FALSE;
616*7c478bd9Sstevel@tonic-gate 	hal->cfgroms_being_read = 0;
617*7c478bd9Sstevel@tonic-gate 
618*7c478bd9Sstevel@tonic-gate 	/*
619*7c478bd9Sstevel@tonic-gate 	 * Save the number of nodes prior to parsing the self id buffer.
620*7c478bd9Sstevel@tonic-gate 	 * We need this saved value while initializing the topology tree
621*7c478bd9Sstevel@tonic-gate 	 * (for non-copy case).
622*7c478bd9Sstevel@tonic-gate 	 */
623*7c478bd9Sstevel@tonic-gate 	saved_number_of_nodes = hal->number_of_nodes;
624*7c478bd9Sstevel@tonic-gate 
625*7c478bd9Sstevel@tonic-gate 	/* Parse the SelfID buffer */
626*7c478bd9Sstevel@tonic-gate 	if (s1394_parse_selfid_buffer(hal, selfid_buf_addr, selfid_size) !=
627*7c478bd9Sstevel@tonic-gate 	    DDI_SUCCESS) {
628*7c478bd9Sstevel@tonic-gate 		/* Unlock the topology tree */
629*7c478bd9Sstevel@tonic-gate 		mutex_exit(&hal->topology_tree_mutex);
630*7c478bd9Sstevel@tonic-gate 
631*7c478bd9Sstevel@tonic-gate 		/* kstats - SelfID buffer error */
632*7c478bd9Sstevel@tonic-gate 		hal->hal_kstats->selfid_buffer_error++;
633*7c478bd9Sstevel@tonic-gate 		return;		/* Error parsing SelfIDs */
634*7c478bd9Sstevel@tonic-gate 	}
635*7c478bd9Sstevel@tonic-gate 
636*7c478bd9Sstevel@tonic-gate 	/* Sort the SelfID packets by node number (if it's a 1995 PHY) */
637*7c478bd9Sstevel@tonic-gate 	if (hal->halinfo.phy == H1394_PHY_1995) {
638*7c478bd9Sstevel@tonic-gate 		s1394_sort_selfids(hal);
639*7c478bd9Sstevel@tonic-gate 	}
640*7c478bd9Sstevel@tonic-gate 
641*7c478bd9Sstevel@tonic-gate 	/*
642*7c478bd9Sstevel@tonic-gate 	 * Update the cycle master timer - if the timer is set and
643*7c478bd9Sstevel@tonic-gate 	 * we were the root but we are not anymore, then disable it.
644*7c478bd9Sstevel@tonic-gate 	 */
645*7c478bd9Sstevel@tonic-gate 	mutex_enter(&hal->cm_timer_mutex);
646*7c478bd9Sstevel@tonic-gate 	if ((hal->cm_timer_set == B_TRUE) &&
647*7c478bd9Sstevel@tonic-gate 	    ((hal->old_number_of_nodes - 1) ==
648*7c478bd9Sstevel@tonic-gate 		IEEE1394_NODE_NUM(hal->old_node_id)) &&
649*7c478bd9Sstevel@tonic-gate 	    ((hal->number_of_nodes - 1) !=
650*7c478bd9Sstevel@tonic-gate 		IEEE1394_NODE_NUM(hal->node_id))) {
651*7c478bd9Sstevel@tonic-gate 		mutex_exit(&hal->cm_timer_mutex);
652*7c478bd9Sstevel@tonic-gate 		(void) untimeout(hal->cm_timer);
653*7c478bd9Sstevel@tonic-gate 	} else {
654*7c478bd9Sstevel@tonic-gate 		mutex_exit(&hal->cm_timer_mutex);
655*7c478bd9Sstevel@tonic-gate 	}
656*7c478bd9Sstevel@tonic-gate 
657*7c478bd9Sstevel@tonic-gate 	s1394_init_topology_tree(hal, tree_copied, saved_number_of_nodes);
658*7c478bd9Sstevel@tonic-gate 
659*7c478bd9Sstevel@tonic-gate 	/* Determine the 1394 bus gap count */
660*7c478bd9Sstevel@tonic-gate 	hal->gap_count = s1394_get_current_gap_count(hal);
661*7c478bd9Sstevel@tonic-gate 	/* If gap counts are inconsistent, reset */
662*7c478bd9Sstevel@tonic-gate 	if (hal->gap_count == -1) {
663*7c478bd9Sstevel@tonic-gate 		/* Unlock the topology tree */
664*7c478bd9Sstevel@tonic-gate 		mutex_exit(&hal->topology_tree_mutex);
665*7c478bd9Sstevel@tonic-gate 
666*7c478bd9Sstevel@tonic-gate 		/* kstats - SelfID buffer error (invalid gap counts) */
667*7c478bd9Sstevel@tonic-gate 		hal->hal_kstats->selfid_buffer_error++;
668*7c478bd9Sstevel@tonic-gate 
669*7c478bd9Sstevel@tonic-gate 		if (s1394_ignore_invalid_gap_cnt == 1) {
670*7c478bd9Sstevel@tonic-gate 			/* Lock the topology tree again */
671*7c478bd9Sstevel@tonic-gate 			mutex_enter(&hal->topology_tree_mutex);
672*7c478bd9Sstevel@tonic-gate 			hal->gap_count = 0x3F;
673*7c478bd9Sstevel@tonic-gate 		} else {
674*7c478bd9Sstevel@tonic-gate 			return;	/* Invalid gap counts in SelfID buffer */
675*7c478bd9Sstevel@tonic-gate 		}
676*7c478bd9Sstevel@tonic-gate 	}
677*7c478bd9Sstevel@tonic-gate 
678*7c478bd9Sstevel@tonic-gate 	/* Determine the Isoch Resource Manager */
679*7c478bd9Sstevel@tonic-gate 	hal->IRM_node = s1394_get_isoch_rsrc_mgr(hal);
680*7c478bd9Sstevel@tonic-gate 
681*7c478bd9Sstevel@tonic-gate 	/* Build the topology tree */
682*7c478bd9Sstevel@tonic-gate 	if (s1394_topology_tree_build(hal) != DDI_SUCCESS) {
683*7c478bd9Sstevel@tonic-gate 		/* Unlock the topology tree */
684*7c478bd9Sstevel@tonic-gate 		mutex_exit(&hal->topology_tree_mutex);
685*7c478bd9Sstevel@tonic-gate 
686*7c478bd9Sstevel@tonic-gate 		/* kstats - SelfID buffer error (Invalid topology tree) */
687*7c478bd9Sstevel@tonic-gate 		hal->hal_kstats->selfid_buffer_error++;
688*7c478bd9Sstevel@tonic-gate 		return;		/* Error building topology tree from SelfIDs */
689*7c478bd9Sstevel@tonic-gate 	}
690*7c478bd9Sstevel@tonic-gate 
691*7c478bd9Sstevel@tonic-gate 	/* Update the CSR topology_map */
692*7c478bd9Sstevel@tonic-gate 	s1394_CSR_topology_map_update(hal);
693*7c478bd9Sstevel@tonic-gate 
694*7c478bd9Sstevel@tonic-gate 	/* Calculate the diameter */
695*7c478bd9Sstevel@tonic-gate 	diameter = s1394_topology_tree_calculate_diameter(hal);
696*7c478bd9Sstevel@tonic-gate 
697*7c478bd9Sstevel@tonic-gate 	/* Determine the optimum gap count */
698*7c478bd9Sstevel@tonic-gate 	hal->optimum_gap_count = s1394_gap_count_optimize(diameter);
699*7c478bd9Sstevel@tonic-gate 
700*7c478bd9Sstevel@tonic-gate 	/* Fill in the speed map */
701*7c478bd9Sstevel@tonic-gate 	s1394_speed_map_fill(hal);
702*7c478bd9Sstevel@tonic-gate 
703*7c478bd9Sstevel@tonic-gate 	/* Initialize the two trees (for tree walking) */
704*7c478bd9Sstevel@tonic-gate 	s1394_topology_tree_mark_all_unvisited(hal);
705*7c478bd9Sstevel@tonic-gate 	s1394_old_tree_mark_all_unvisited(hal);
706*7c478bd9Sstevel@tonic-gate 	s1394_old_tree_mark_all_unmatched(hal);
707*7c478bd9Sstevel@tonic-gate 
708*7c478bd9Sstevel@tonic-gate 	/* Are both trees (old and new) valid? */
709*7c478bd9Sstevel@tonic-gate 	if ((hal->old_tree_valid == B_TRUE) &&
710*7c478bd9Sstevel@tonic-gate 	    (hal->topology_tree_valid == B_TRUE)) {
711*7c478bd9Sstevel@tonic-gate 		/* If HAL was in a suspended state, then do no matching */
712*7c478bd9Sstevel@tonic-gate 		if (hal->hal_was_suspended == B_TRUE) {
713*7c478bd9Sstevel@tonic-gate 		    hal->hal_was_suspended = B_FALSE;
714*7c478bd9Sstevel@tonic-gate 		} else {
715*7c478bd9Sstevel@tonic-gate 			gen_rollover = hal->halinfo.max_generation + 1;
716*7c478bd9Sstevel@tonic-gate 			/* If only one bus reset occurred, match the trees */
717*7c478bd9Sstevel@tonic-gate 			if (((hal->old_generation_count + 1) % gen_rollover) ==
718*7c478bd9Sstevel@tonic-gate 			    generation_count) {
719*7c478bd9Sstevel@tonic-gate 				s1394_match_tree_nodes(hal);
720*7c478bd9Sstevel@tonic-gate 			}
721*7c478bd9Sstevel@tonic-gate 		}
722*7c478bd9Sstevel@tonic-gate 	}
723*7c478bd9Sstevel@tonic-gate 
724*7c478bd9Sstevel@tonic-gate 	/* Unlock the topology tree */
725*7c478bd9Sstevel@tonic-gate 	mutex_exit(&hal->topology_tree_mutex);
726*7c478bd9Sstevel@tonic-gate 
727*7c478bd9Sstevel@tonic-gate 	/* Wake up the bus reset processing thread */
728*7c478bd9Sstevel@tonic-gate 	s1394_tickle_bus_reset_thread(hal);
729*7c478bd9Sstevel@tonic-gate }
730*7c478bd9Sstevel@tonic-gate 
731*7c478bd9Sstevel@tonic-gate /*
732*7c478bd9Sstevel@tonic-gate  * Function:    h1394_read_request()
733*7c478bd9Sstevel@tonic-gate  * Input(s):    sl_private		The HAL "handle" returned by
734*7c478bd9Sstevel@tonic-gate  *					    h1394_attach()
735*7c478bd9Sstevel@tonic-gate  *		req			The incoming AR request
736*7c478bd9Sstevel@tonic-gate  *
737*7c478bd9Sstevel@tonic-gate  * Output(s):	None
738*7c478bd9Sstevel@tonic-gate  *
739*7c478bd9Sstevel@tonic-gate  * Description:	h1394_read_request() receives incoming AR requests.  These
740*7c478bd9Sstevel@tonic-gate  *		asynchronous read requests are dispatched to the appropriate
741*7c478bd9Sstevel@tonic-gate  *		target (if one has registered) or are handled by the 1394
742*7c478bd9Sstevel@tonic-gate  *		Software Framework, which will send out an appropriate
743*7c478bd9Sstevel@tonic-gate  *		response.
744*7c478bd9Sstevel@tonic-gate  */
745*7c478bd9Sstevel@tonic-gate void
h1394_read_request(void * sl_private,cmd1394_cmd_t * req)746*7c478bd9Sstevel@tonic-gate h1394_read_request(void *sl_private, cmd1394_cmd_t *req)
747*7c478bd9Sstevel@tonic-gate {
748*7c478bd9Sstevel@tonic-gate 	s1394_hal_t		*hal;
749*7c478bd9Sstevel@tonic-gate 	s1394_cmd_priv_t	*s_priv;
750*7c478bd9Sstevel@tonic-gate 	s1394_addr_space_blk_t  *addr_blk;
751*7c478bd9Sstevel@tonic-gate 	dev_info_t		*dip;
752*7c478bd9Sstevel@tonic-gate 	uint64_t		end_of_request;
753*7c478bd9Sstevel@tonic-gate 	uint32_t		offset;
754*7c478bd9Sstevel@tonic-gate 	size_t			cmd_length;
755*7c478bd9Sstevel@tonic-gate 	uchar_t			*bufp_addr;
756*7c478bd9Sstevel@tonic-gate 	uchar_t			*begin_ptr;
757*7c478bd9Sstevel@tonic-gate 	uchar_t			*end_ptr;
758*7c478bd9Sstevel@tonic-gate 	uchar_t			*tmp_ptr;
759*7c478bd9Sstevel@tonic-gate 	void (*recv_read_req)(cmd1394_cmd_t *);
760*7c478bd9Sstevel@tonic-gate 
761*7c478bd9Sstevel@tonic-gate 	hal = (s1394_hal_t *)sl_private;
762*7c478bd9Sstevel@tonic-gate 
763*7c478bd9Sstevel@tonic-gate 	/* Get the Services Layer private area */
764*7c478bd9Sstevel@tonic-gate 	s_priv = S1394_GET_CMD_PRIV(req);
765*7c478bd9Sstevel@tonic-gate 
766*7c478bd9Sstevel@tonic-gate 	s_priv->cmd_priv_xfer_type = S1394_CMD_READ;
767*7c478bd9Sstevel@tonic-gate 
768*7c478bd9Sstevel@tonic-gate 	switch (req->cmd_type) {
769*7c478bd9Sstevel@tonic-gate 	case CMD1394_ASYNCH_RD_QUAD:
770*7c478bd9Sstevel@tonic-gate 		cmd_length = IEEE1394_QUADLET;
771*7c478bd9Sstevel@tonic-gate 		hal->hal_kstats->arreq_quad_rd++;
772*7c478bd9Sstevel@tonic-gate 		break;
773*7c478bd9Sstevel@tonic-gate 
774*7c478bd9Sstevel@tonic-gate 	case CMD1394_ASYNCH_RD_BLOCK:
775*7c478bd9Sstevel@tonic-gate 		cmd_length = req->cmd_u.b.blk_length;
776*7c478bd9Sstevel@tonic-gate 		hal->hal_kstats->arreq_blk_rd++;
777*7c478bd9Sstevel@tonic-gate 		break;
778*7c478bd9Sstevel@tonic-gate 
779*7c478bd9Sstevel@tonic-gate 	default:
780*7c478bd9Sstevel@tonic-gate 		dip = hal->halinfo.dip;
781*7c478bd9Sstevel@tonic-gate 
782*7c478bd9Sstevel@tonic-gate 		/* An unexpected error in the HAL */
783*7c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, HALT_ERROR_MESSAGE,
784*7c478bd9Sstevel@tonic-gate 		    ddi_node_name(dip), ddi_get_instance(dip));
785*7c478bd9Sstevel@tonic-gate 
786*7c478bd9Sstevel@tonic-gate 		/* Disable the HAL */
787*7c478bd9Sstevel@tonic-gate 		s1394_hal_shutdown(hal, B_TRUE);
788*7c478bd9Sstevel@tonic-gate 
789*7c478bd9Sstevel@tonic-gate 		return;
790*7c478bd9Sstevel@tonic-gate 	}
791*7c478bd9Sstevel@tonic-gate 
792*7c478bd9Sstevel@tonic-gate 	/* Lock the "used" tree */
793*7c478bd9Sstevel@tonic-gate 	mutex_enter(&hal->addr_space_used_mutex);
794*7c478bd9Sstevel@tonic-gate 
795*7c478bd9Sstevel@tonic-gate 	/* Has the 1394 address been allocated? */
796*7c478bd9Sstevel@tonic-gate 	addr_blk = s1394_used_tree_search(hal, req->cmd_addr);
797*7c478bd9Sstevel@tonic-gate 
798*7c478bd9Sstevel@tonic-gate 	/* If it wasn't found, it isn't owned... */
799*7c478bd9Sstevel@tonic-gate 	if (addr_blk == NULL) {
800*7c478bd9Sstevel@tonic-gate 		/* Unlock the "used" tree */
801*7c478bd9Sstevel@tonic-gate 		mutex_exit(&hal->addr_space_used_mutex);
802*7c478bd9Sstevel@tonic-gate 		req->cmd_result = IEEE1394_RESP_ADDRESS_ERROR;
803*7c478bd9Sstevel@tonic-gate 		(void) s1394_send_response(hal, req);
804*7c478bd9Sstevel@tonic-gate 		return;
805*7c478bd9Sstevel@tonic-gate 	}
806*7c478bd9Sstevel@tonic-gate 
807*7c478bd9Sstevel@tonic-gate 	/* Does the WHOLE request fit in the allocated block? */
808*7c478bd9Sstevel@tonic-gate 	end_of_request = (req->cmd_addr + cmd_length) - 1;
809*7c478bd9Sstevel@tonic-gate 	if (end_of_request > addr_blk->addr_hi) {
810*7c478bd9Sstevel@tonic-gate 		/* Unlock the "used" tree */
811*7c478bd9Sstevel@tonic-gate 		mutex_exit(&hal->addr_space_used_mutex);
812*7c478bd9Sstevel@tonic-gate 		req->cmd_result = IEEE1394_RESP_ADDRESS_ERROR;
813*7c478bd9Sstevel@tonic-gate 		(void) s1394_send_response(hal, req);
814*7c478bd9Sstevel@tonic-gate 		return;
815*7c478bd9Sstevel@tonic-gate 	}
816*7c478bd9Sstevel@tonic-gate 
817*7c478bd9Sstevel@tonic-gate 	/* Is a read request valid for this address space? */
818*7c478bd9Sstevel@tonic-gate 	if (!(addr_blk->addr_enable & T1394_ADDR_RDENBL)) {
819*7c478bd9Sstevel@tonic-gate 		/* Unlock the "used" tree */
820*7c478bd9Sstevel@tonic-gate 		mutex_exit(&hal->addr_space_used_mutex);
821*7c478bd9Sstevel@tonic-gate 		req->cmd_result = IEEE1394_RESP_TYPE_ERROR;
822*7c478bd9Sstevel@tonic-gate 		(void) s1394_send_response(hal, req);
823*7c478bd9Sstevel@tonic-gate 		return;
824*7c478bd9Sstevel@tonic-gate 	}
825*7c478bd9Sstevel@tonic-gate 
826*7c478bd9Sstevel@tonic-gate 	/* Make sure quadlet requests are quadlet-aligned */
827*7c478bd9Sstevel@tonic-gate 	offset = req->cmd_addr - addr_blk->addr_lo;
828*7c478bd9Sstevel@tonic-gate 	if ((req->cmd_type == CMD1394_ASYNCH_RD_QUAD) &&
829*7c478bd9Sstevel@tonic-gate 	    ((offset & 0x3) != 0)) {
830*7c478bd9Sstevel@tonic-gate 		/* Unlock the "used" tree */
831*7c478bd9Sstevel@tonic-gate 		mutex_exit(&hal->addr_space_used_mutex);
832*7c478bd9Sstevel@tonic-gate 		req->cmd_result = IEEE1394_RESP_TYPE_ERROR;
833*7c478bd9Sstevel@tonic-gate 		(void) s1394_send_response(hal, req);
834*7c478bd9Sstevel@tonic-gate 		return;
835*7c478bd9Sstevel@tonic-gate 	}
836*7c478bd9Sstevel@tonic-gate 
837*7c478bd9Sstevel@tonic-gate 	/* Fill in the backing store if necessary */
838*7c478bd9Sstevel@tonic-gate 	if (addr_blk->kmem_bufp != NULL) {
839*7c478bd9Sstevel@tonic-gate 		offset = req->cmd_addr - addr_blk->addr_lo;
840*7c478bd9Sstevel@tonic-gate 		bufp_addr = (uchar_t *)addr_blk->kmem_bufp + offset;
841*7c478bd9Sstevel@tonic-gate 
842*7c478bd9Sstevel@tonic-gate 		switch (req->cmd_type) {
843*7c478bd9Sstevel@tonic-gate 		case CMD1394_ASYNCH_RD_QUAD:
844*7c478bd9Sstevel@tonic-gate 			bcopy((void *)bufp_addr,
845*7c478bd9Sstevel@tonic-gate 			    (void *)&(req->cmd_u.q.quadlet_data), cmd_length);
846*7c478bd9Sstevel@tonic-gate 			break;
847*7c478bd9Sstevel@tonic-gate 
848*7c478bd9Sstevel@tonic-gate 		case CMD1394_ASYNCH_RD_BLOCK:
849*7c478bd9Sstevel@tonic-gate 			begin_ptr = req->cmd_u.b.data_block->b_wptr;
850*7c478bd9Sstevel@tonic-gate 			end_ptr	  = begin_ptr + cmd_length;
851*7c478bd9Sstevel@tonic-gate 			tmp_ptr	  = req->cmd_u.b.data_block->b_datap->db_lim;
852*7c478bd9Sstevel@tonic-gate 			if (end_ptr <= tmp_ptr) {
853*7c478bd9Sstevel@tonic-gate 				bcopy((void *)bufp_addr, (void *)begin_ptr,
854*7c478bd9Sstevel@tonic-gate 				    cmd_length);
855*7c478bd9Sstevel@tonic-gate 				/* Update b_wptr to refelect the new data */
856*7c478bd9Sstevel@tonic-gate 				req->cmd_u.b.data_block->b_wptr = end_ptr;
857*7c478bd9Sstevel@tonic-gate 			} else {
858*7c478bd9Sstevel@tonic-gate 				dip = hal->halinfo.dip;
859*7c478bd9Sstevel@tonic-gate 
860*7c478bd9Sstevel@tonic-gate 				/* An unexpected error in the HAL */
861*7c478bd9Sstevel@tonic-gate 				cmn_err(CE_WARN, HALT_ERROR_MESSAGE,
862*7c478bd9Sstevel@tonic-gate 				    ddi_node_name(dip), ddi_get_instance(dip));
863*7c478bd9Sstevel@tonic-gate 
864*7c478bd9Sstevel@tonic-gate 				/* Unlock the "used" tree */
865*7c478bd9Sstevel@tonic-gate 				mutex_exit(&hal->addr_space_used_mutex);
866*7c478bd9Sstevel@tonic-gate 
867*7c478bd9Sstevel@tonic-gate 				/* Disable the HAL */
868*7c478bd9Sstevel@tonic-gate 				s1394_hal_shutdown(hal, B_TRUE);
869*7c478bd9Sstevel@tonic-gate 
870*7c478bd9Sstevel@tonic-gate 				return;
871*7c478bd9Sstevel@tonic-gate 			}
872*7c478bd9Sstevel@tonic-gate 			break;
873*7c478bd9Sstevel@tonic-gate 
874*7c478bd9Sstevel@tonic-gate 		default:
875*7c478bd9Sstevel@tonic-gate 			dip = hal->halinfo.dip;
876*7c478bd9Sstevel@tonic-gate 
877*7c478bd9Sstevel@tonic-gate 			/* An unexpected error in the HAL */
878*7c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN, HALT_ERROR_MESSAGE,
879*7c478bd9Sstevel@tonic-gate 			    ddi_node_name(dip), ddi_get_instance(dip));
880*7c478bd9Sstevel@tonic-gate 
881*7c478bd9Sstevel@tonic-gate 			/* Unlock the "used" tree */
882*7c478bd9Sstevel@tonic-gate 			mutex_exit(&hal->addr_space_used_mutex);
883*7c478bd9Sstevel@tonic-gate 
884*7c478bd9Sstevel@tonic-gate 			/* Disable the HAL */
885*7c478bd9Sstevel@tonic-gate 			s1394_hal_shutdown(hal, B_TRUE);
886*7c478bd9Sstevel@tonic-gate 
887*7c478bd9Sstevel@tonic-gate 			return;
888*7c478bd9Sstevel@tonic-gate 		}
889*7c478bd9Sstevel@tonic-gate 	}
890*7c478bd9Sstevel@tonic-gate 
891*7c478bd9Sstevel@tonic-gate 	/* Fill in the rest of the info in the request */
892*7c478bd9Sstevel@tonic-gate 	s_priv->arreq_valid_addr = B_TRUE;
893*7c478bd9Sstevel@tonic-gate 	req->cmd_callback_arg	 = addr_blk->addr_arg;
894*7c478bd9Sstevel@tonic-gate 	recv_read_req		 = addr_blk->addr_events.recv_read_request;
895*7c478bd9Sstevel@tonic-gate 
896*7c478bd9Sstevel@tonic-gate 	/* Unlock the "used" tree */
897*7c478bd9Sstevel@tonic-gate 	mutex_exit(&hal->addr_space_used_mutex);
898*7c478bd9Sstevel@tonic-gate 
899*7c478bd9Sstevel@tonic-gate 	/*
900*7c478bd9Sstevel@tonic-gate 	 * Add no code that modifies the command after the target
901*7c478bd9Sstevel@tonic-gate 	 * callback is called or after the response is sent to the
902*7c478bd9Sstevel@tonic-gate 	 * HAL.
903*7c478bd9Sstevel@tonic-gate 	 */
904*7c478bd9Sstevel@tonic-gate 	if (recv_read_req != NULL) {
905*7c478bd9Sstevel@tonic-gate 		recv_read_req(req);
906*7c478bd9Sstevel@tonic-gate 	} else {
907*7c478bd9Sstevel@tonic-gate 		req->cmd_result = IEEE1394_RESP_COMPLETE;
908*7c478bd9Sstevel@tonic-gate 		(void) s1394_send_response(hal, req);
909*7c478bd9Sstevel@tonic-gate 		return;
910*7c478bd9Sstevel@tonic-gate 	}
911*7c478bd9Sstevel@tonic-gate }
912*7c478bd9Sstevel@tonic-gate 
913*7c478bd9Sstevel@tonic-gate /*
914*7c478bd9Sstevel@tonic-gate  * Function:    h1394_write_request()
915*7c478bd9Sstevel@tonic-gate  * Input(s):    sl_private		The HAL "handle" returned by
916*7c478bd9Sstevel@tonic-gate  *					    h1394_attach()
917*7c478bd9Sstevel@tonic-gate  *		req			The incoming AR request
918*7c478bd9Sstevel@tonic-gate  *
919*7c478bd9Sstevel@tonic-gate  * Output(s):	None
920*7c478bd9Sstevel@tonic-gate  *
921*7c478bd9Sstevel@tonic-gate  * Description:	h1394_write_request() receives incoming AR requests.  These
922*7c478bd9Sstevel@tonic-gate  *		asynchronous write requests are dispatched to the appropriate
923*7c478bd9Sstevel@tonic-gate  *		target (if one has registered) or are handled by the 1394
924*7c478bd9Sstevel@tonic-gate  *		Software Framework, which will send out an appropriate
925*7c478bd9Sstevel@tonic-gate  *		response.
926*7c478bd9Sstevel@tonic-gate  */
927*7c478bd9Sstevel@tonic-gate void
h1394_write_request(void * sl_private,cmd1394_cmd_t * req)928*7c478bd9Sstevel@tonic-gate h1394_write_request(void *sl_private, cmd1394_cmd_t *req)
929*7c478bd9Sstevel@tonic-gate {
930*7c478bd9Sstevel@tonic-gate 	s1394_hal_t		*hal;
931*7c478bd9Sstevel@tonic-gate 	s1394_cmd_priv_t	*s_priv;
932*7c478bd9Sstevel@tonic-gate 	h1394_cmd_priv_t	*h_priv;
933*7c478bd9Sstevel@tonic-gate 	s1394_addr_space_blk_t	*addr_blk;
934*7c478bd9Sstevel@tonic-gate 	dev_info_t		*dip;
935*7c478bd9Sstevel@tonic-gate 	uint32_t		offset;
936*7c478bd9Sstevel@tonic-gate 	size_t			cmd_length;
937*7c478bd9Sstevel@tonic-gate 	uchar_t			*bufp_addr;
938*7c478bd9Sstevel@tonic-gate 	uchar_t			*begin_ptr;
939*7c478bd9Sstevel@tonic-gate 	uchar_t			*end_ptr;
940*7c478bd9Sstevel@tonic-gate 	uchar_t			*tmp_ptr;
941*7c478bd9Sstevel@tonic-gate 	uint64_t		end_of_request;
942*7c478bd9Sstevel@tonic-gate 	boolean_t		posted_write = B_FALSE;
943*7c478bd9Sstevel@tonic-gate 	boolean_t		write_error = B_FALSE;
944*7c478bd9Sstevel@tonic-gate 	void (*recv_write_req)(cmd1394_cmd_t *);
945*7c478bd9Sstevel@tonic-gate 
946*7c478bd9Sstevel@tonic-gate 	hal = (s1394_hal_t *)sl_private;
947*7c478bd9Sstevel@tonic-gate 
948*7c478bd9Sstevel@tonic-gate 	/* Get the Services Layer private area */
949*7c478bd9Sstevel@tonic-gate 	s_priv = S1394_GET_CMD_PRIV(req);
950*7c478bd9Sstevel@tonic-gate 
951*7c478bd9Sstevel@tonic-gate 	s_priv->cmd_priv_xfer_type = S1394_CMD_WRITE;
952*7c478bd9Sstevel@tonic-gate 
953*7c478bd9Sstevel@tonic-gate 	switch (req->cmd_type) {
954*7c478bd9Sstevel@tonic-gate 	case CMD1394_ASYNCH_WR_QUAD:
955*7c478bd9Sstevel@tonic-gate 		cmd_length = IEEE1394_QUADLET;
956*7c478bd9Sstevel@tonic-gate 		hal->hal_kstats->arreq_quad_wr++;
957*7c478bd9Sstevel@tonic-gate 		break;
958*7c478bd9Sstevel@tonic-gate 
959*7c478bd9Sstevel@tonic-gate 	case CMD1394_ASYNCH_WR_BLOCK:
960*7c478bd9Sstevel@tonic-gate 		cmd_length = req->cmd_u.b.blk_length;
961*7c478bd9Sstevel@tonic-gate 		hal->hal_kstats->arreq_blk_wr++;
962*7c478bd9Sstevel@tonic-gate 		hal->hal_kstats->arreq_blk_wr_size += cmd_length;
963*7c478bd9Sstevel@tonic-gate 		break;
964*7c478bd9Sstevel@tonic-gate 
965*7c478bd9Sstevel@tonic-gate 	default:
966*7c478bd9Sstevel@tonic-gate 		dip = hal->halinfo.dip;
967*7c478bd9Sstevel@tonic-gate 
968*7c478bd9Sstevel@tonic-gate 		/* An unexpected error in the HAL */
969*7c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, HALT_ERROR_MESSAGE,
970*7c478bd9Sstevel@tonic-gate 		    ddi_node_name(dip), ddi_get_instance(dip));
971*7c478bd9Sstevel@tonic-gate 
972*7c478bd9Sstevel@tonic-gate 		/* Disable the HAL */
973*7c478bd9Sstevel@tonic-gate 		s1394_hal_shutdown(hal, B_TRUE);
974*7c478bd9Sstevel@tonic-gate 
975*7c478bd9Sstevel@tonic-gate 		return;
976*7c478bd9Sstevel@tonic-gate 	}
977*7c478bd9Sstevel@tonic-gate 
978*7c478bd9Sstevel@tonic-gate 	/* Lock the "used" tree */
979*7c478bd9Sstevel@tonic-gate 	mutex_enter(&hal->addr_space_used_mutex);
980*7c478bd9Sstevel@tonic-gate 
981*7c478bd9Sstevel@tonic-gate 	/* Has the 1394 address been allocated? */
982*7c478bd9Sstevel@tonic-gate 	addr_blk = s1394_used_tree_search(hal, req->cmd_addr);
983*7c478bd9Sstevel@tonic-gate 
984*7c478bd9Sstevel@tonic-gate 	/* Is this a posted write request? */
985*7c478bd9Sstevel@tonic-gate 	posted_write = s1394_is_posted_write(hal, req->cmd_addr);
986*7c478bd9Sstevel@tonic-gate 
987*7c478bd9Sstevel@tonic-gate 	/* If it wasn't found, it isn't owned... */
988*7c478bd9Sstevel@tonic-gate 	if (addr_blk == NULL) {
989*7c478bd9Sstevel@tonic-gate 		req->cmd_result = IEEE1394_RESP_ADDRESS_ERROR;
990*7c478bd9Sstevel@tonic-gate 		write_error	= B_TRUE;
991*7c478bd9Sstevel@tonic-gate 		goto write_error_check;
992*7c478bd9Sstevel@tonic-gate 	}
993*7c478bd9Sstevel@tonic-gate 
994*7c478bd9Sstevel@tonic-gate 	/* Does the WHOLE request fit in the allocated block? */
995*7c478bd9Sstevel@tonic-gate 	end_of_request = (req->cmd_addr + cmd_length) - 1;
996*7c478bd9Sstevel@tonic-gate 	if (end_of_request > addr_blk->addr_hi) {
997*7c478bd9Sstevel@tonic-gate 		req->cmd_result = IEEE1394_RESP_ADDRESS_ERROR;
998*7c478bd9Sstevel@tonic-gate 		write_error	= B_TRUE;
999*7c478bd9Sstevel@tonic-gate 		goto write_error_check;
1000*7c478bd9Sstevel@tonic-gate 	}
1001*7c478bd9Sstevel@tonic-gate 
1002*7c478bd9Sstevel@tonic-gate 	/* Is a write request valid for this address space? */
1003*7c478bd9Sstevel@tonic-gate 	if (!(addr_blk->addr_enable & T1394_ADDR_WRENBL)) {
1004*7c478bd9Sstevel@tonic-gate 		req->cmd_result = IEEE1394_RESP_TYPE_ERROR;
1005*7c478bd9Sstevel@tonic-gate 		write_error	= B_TRUE;
1006*7c478bd9Sstevel@tonic-gate 		goto write_error_check;
1007*7c478bd9Sstevel@tonic-gate 	}
1008*7c478bd9Sstevel@tonic-gate 
1009*7c478bd9Sstevel@tonic-gate 	/* Make sure quadlet request is quadlet aligned */
1010*7c478bd9Sstevel@tonic-gate 	offset = req->cmd_addr - addr_blk->addr_lo;
1011*7c478bd9Sstevel@tonic-gate 	if ((req->cmd_type == CMD1394_ASYNCH_WR_QUAD) &&
1012*7c478bd9Sstevel@tonic-gate 	    ((offset & 0x3) != 0)) {
1013*7c478bd9Sstevel@tonic-gate 		req->cmd_result = IEEE1394_RESP_TYPE_ERROR;
1014*7c478bd9Sstevel@tonic-gate 		write_error	= B_TRUE;
1015*7c478bd9Sstevel@tonic-gate 		goto write_error_check;
1016*7c478bd9Sstevel@tonic-gate 	}
1017*7c478bd9Sstevel@tonic-gate 
1018*7c478bd9Sstevel@tonic-gate write_error_check:
1019*7c478bd9Sstevel@tonic-gate 	/* Check if posted-write when sending error responses */
1020*7c478bd9Sstevel@tonic-gate 	if (write_error == B_TRUE) {
1021*7c478bd9Sstevel@tonic-gate 		/* Unlock the "used" tree */
1022*7c478bd9Sstevel@tonic-gate 		mutex_exit(&hal->addr_space_used_mutex);
1023*7c478bd9Sstevel@tonic-gate 
1024*7c478bd9Sstevel@tonic-gate 		if (posted_write == B_TRUE) {
1025*7c478bd9Sstevel@tonic-gate 			/* Get a pointer to the HAL private struct */
1026*7c478bd9Sstevel@tonic-gate 			h_priv = (h1394_cmd_priv_t *)&s_priv->hal_cmd_private;
1027*7c478bd9Sstevel@tonic-gate 			hal->hal_kstats->arreq_posted_write_error++;
1028*7c478bd9Sstevel@tonic-gate 			/* Free the command - Pass it back to the HAL */
1029*7c478bd9Sstevel@tonic-gate 			HAL_CALL(hal).response_complete(
1030*7c478bd9Sstevel@tonic-gate 			    hal->halinfo.hal_private, req, h_priv);
1031*7c478bd9Sstevel@tonic-gate 			return;
1032*7c478bd9Sstevel@tonic-gate 		} else {
1033*7c478bd9Sstevel@tonic-gate 			(void) s1394_send_response(hal, req);
1034*7c478bd9Sstevel@tonic-gate 			return;
1035*7c478bd9Sstevel@tonic-gate 		}
1036*7c478bd9Sstevel@tonic-gate 	}
1037*7c478bd9Sstevel@tonic-gate 
1038*7c478bd9Sstevel@tonic-gate 	/* Fill in the backing store if necessary */
1039*7c478bd9Sstevel@tonic-gate 	if (addr_blk->kmem_bufp != NULL) {
1040*7c478bd9Sstevel@tonic-gate 		offset = req->cmd_addr - addr_blk->addr_lo;
1041*7c478bd9Sstevel@tonic-gate 		bufp_addr = (uchar_t *)addr_blk->kmem_bufp + offset;
1042*7c478bd9Sstevel@tonic-gate 		switch (req->cmd_type) {
1043*7c478bd9Sstevel@tonic-gate 		case CMD1394_ASYNCH_WR_QUAD:
1044*7c478bd9Sstevel@tonic-gate 			bcopy((void *)&(req->cmd_u.q.quadlet_data),
1045*7c478bd9Sstevel@tonic-gate 			    (void *)bufp_addr, cmd_length);
1046*7c478bd9Sstevel@tonic-gate 			break;
1047*7c478bd9Sstevel@tonic-gate 
1048*7c478bd9Sstevel@tonic-gate 		case CMD1394_ASYNCH_WR_BLOCK:
1049*7c478bd9Sstevel@tonic-gate 			begin_ptr = req->cmd_u.b.data_block->b_rptr;
1050*7c478bd9Sstevel@tonic-gate 			end_ptr = begin_ptr + cmd_length;
1051*7c478bd9Sstevel@tonic-gate 			tmp_ptr = req->cmd_u.b.data_block->b_wptr;
1052*7c478bd9Sstevel@tonic-gate 			if (end_ptr <= tmp_ptr) {
1053*7c478bd9Sstevel@tonic-gate 				bcopy((void *)begin_ptr, (void *)bufp_addr,
1054*7c478bd9Sstevel@tonic-gate 				    cmd_length);
1055*7c478bd9Sstevel@tonic-gate 			} else {
1056*7c478bd9Sstevel@tonic-gate 				dip = hal->halinfo.dip;
1057*7c478bd9Sstevel@tonic-gate 
1058*7c478bd9Sstevel@tonic-gate 				/* An unexpected error in the HAL */
1059*7c478bd9Sstevel@tonic-gate 				cmn_err(CE_WARN, HALT_ERROR_MESSAGE,
1060*7c478bd9Sstevel@tonic-gate 				    ddi_node_name(dip), ddi_get_instance(dip));
1061*7c478bd9Sstevel@tonic-gate 
1062*7c478bd9Sstevel@tonic-gate 				/* Unlock the "used" tree */
1063*7c478bd9Sstevel@tonic-gate 				mutex_exit(&hal->addr_space_used_mutex);
1064*7c478bd9Sstevel@tonic-gate 
1065*7c478bd9Sstevel@tonic-gate 				/* Disable the HAL */
1066*7c478bd9Sstevel@tonic-gate 				s1394_hal_shutdown(hal, B_TRUE);
1067*7c478bd9Sstevel@tonic-gate 
1068*7c478bd9Sstevel@tonic-gate 				return;
1069*7c478bd9Sstevel@tonic-gate 			}
1070*7c478bd9Sstevel@tonic-gate 			break;
1071*7c478bd9Sstevel@tonic-gate 
1072*7c478bd9Sstevel@tonic-gate 		default:
1073*7c478bd9Sstevel@tonic-gate 			dip = hal->halinfo.dip;
1074*7c478bd9Sstevel@tonic-gate 
1075*7c478bd9Sstevel@tonic-gate 			/* An unexpected error in the HAL */
1076*7c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN, HALT_ERROR_MESSAGE,
1077*7c478bd9Sstevel@tonic-gate 			    ddi_node_name(dip), ddi_get_instance(dip));
1078*7c478bd9Sstevel@tonic-gate 
1079*7c478bd9Sstevel@tonic-gate 			/* Unlock the "used" tree */
1080*7c478bd9Sstevel@tonic-gate 			mutex_exit(&hal->addr_space_used_mutex);
1081*7c478bd9Sstevel@tonic-gate 
1082*7c478bd9Sstevel@tonic-gate 			/* Disable the HAL */
1083*7c478bd9Sstevel@tonic-gate 			s1394_hal_shutdown(hal, B_TRUE);
1084*7c478bd9Sstevel@tonic-gate 
1085*7c478bd9Sstevel@tonic-gate 			return;
1086*7c478bd9Sstevel@tonic-gate 		}
1087*7c478bd9Sstevel@tonic-gate 	}
1088*7c478bd9Sstevel@tonic-gate 
1089*7c478bd9Sstevel@tonic-gate 	/* Fill in the rest of the info in the request */
1090*7c478bd9Sstevel@tonic-gate 	if (addr_blk->addr_type == T1394_ADDR_POSTED_WRITE)
1091*7c478bd9Sstevel@tonic-gate 		s_priv->posted_write = B_TRUE;
1092*7c478bd9Sstevel@tonic-gate 
1093*7c478bd9Sstevel@tonic-gate 	s_priv->arreq_valid_addr = B_TRUE;
1094*7c478bd9Sstevel@tonic-gate 	req->cmd_callback_arg	 = addr_blk->addr_arg;
1095*7c478bd9Sstevel@tonic-gate 	recv_write_req		 = addr_blk->addr_events.recv_write_request;
1096*7c478bd9Sstevel@tonic-gate 
1097*7c478bd9Sstevel@tonic-gate 	/* Unlock the "used" tree */
1098*7c478bd9Sstevel@tonic-gate 	mutex_exit(&hal->addr_space_used_mutex);
1099*7c478bd9Sstevel@tonic-gate 
1100*7c478bd9Sstevel@tonic-gate 	/*
1101*7c478bd9Sstevel@tonic-gate 	 * Add no code that modifies the command after the target
1102*7c478bd9Sstevel@tonic-gate 	 * callback is called or after the response is sent to the
1103*7c478bd9Sstevel@tonic-gate 	 * HAL.
1104*7c478bd9Sstevel@tonic-gate 	 */
1105*7c478bd9Sstevel@tonic-gate 	if (recv_write_req != NULL) {
1106*7c478bd9Sstevel@tonic-gate 		recv_write_req(req);
1107*7c478bd9Sstevel@tonic-gate 	} else {
1108*7c478bd9Sstevel@tonic-gate 		req->cmd_result = IEEE1394_RESP_COMPLETE;
1109*7c478bd9Sstevel@tonic-gate 		(void) s1394_send_response(hal, req);
1110*7c478bd9Sstevel@tonic-gate 		return;
1111*7c478bd9Sstevel@tonic-gate 	}
1112*7c478bd9Sstevel@tonic-gate }
1113*7c478bd9Sstevel@tonic-gate 
1114*7c478bd9Sstevel@tonic-gate /*
1115*7c478bd9Sstevel@tonic-gate  * Function:    h1394_lock_request()
1116*7c478bd9Sstevel@tonic-gate  * Input(s):    sl_private		The HAL "handle" returned by
1117*7c478bd9Sstevel@tonic-gate  *					    h1394_attach()
1118*7c478bd9Sstevel@tonic-gate  *		req			The incoming AR request
1119*7c478bd9Sstevel@tonic-gate  *
1120*7c478bd9Sstevel@tonic-gate  * Output(s):	None
1121*7c478bd9Sstevel@tonic-gate  *
1122*7c478bd9Sstevel@tonic-gate  * Description:	h1394_lock_request() receives incoming AR requests.  These
1123*7c478bd9Sstevel@tonic-gate  *		asynchronous lock requests are dispatched to the appropriate
1124*7c478bd9Sstevel@tonic-gate  *		target (if one has registered) or are handled by the 1394
1125*7c478bd9Sstevel@tonic-gate  *		Software Framework, which will send out an appropriate
1126*7c478bd9Sstevel@tonic-gate  *		response.
1127*7c478bd9Sstevel@tonic-gate  */
1128*7c478bd9Sstevel@tonic-gate void
h1394_lock_request(void * sl_private,cmd1394_cmd_t * req)1129*7c478bd9Sstevel@tonic-gate h1394_lock_request(void *sl_private, cmd1394_cmd_t *req)
1130*7c478bd9Sstevel@tonic-gate {
1131*7c478bd9Sstevel@tonic-gate 	s1394_hal_t		*hal;
1132*7c478bd9Sstevel@tonic-gate 	s1394_cmd_priv_t	*s_priv;
1133*7c478bd9Sstevel@tonic-gate 	s1394_addr_space_blk_t	*addr_blk;
1134*7c478bd9Sstevel@tonic-gate 	dev_info_t		*dip;
1135*7c478bd9Sstevel@tonic-gate 	uint64_t		end_of_request;
1136*7c478bd9Sstevel@tonic-gate 	uint32_t		offset;
1137*7c478bd9Sstevel@tonic-gate 	uchar_t			*bufp_addr;
1138*7c478bd9Sstevel@tonic-gate 	cmd1394_lock_type_t	lock_type;
1139*7c478bd9Sstevel@tonic-gate 	void (*recv_lock_req)(cmd1394_cmd_t *);
1140*7c478bd9Sstevel@tonic-gate 
1141*7c478bd9Sstevel@tonic-gate 	hal = (s1394_hal_t *)sl_private;
1142*7c478bd9Sstevel@tonic-gate 
1143*7c478bd9Sstevel@tonic-gate 	/* Get the Services Layer private area */
1144*7c478bd9Sstevel@tonic-gate 	s_priv = S1394_GET_CMD_PRIV(req);
1145*7c478bd9Sstevel@tonic-gate 
1146*7c478bd9Sstevel@tonic-gate 	s_priv->cmd_priv_xfer_type = S1394_CMD_LOCK;
1147*7c478bd9Sstevel@tonic-gate 
1148*7c478bd9Sstevel@tonic-gate 	/* Lock the "used" tree */
1149*7c478bd9Sstevel@tonic-gate 	mutex_enter(&hal->addr_space_used_mutex);
1150*7c478bd9Sstevel@tonic-gate 
1151*7c478bd9Sstevel@tonic-gate 	/* Has the 1394 address been allocated? */
1152*7c478bd9Sstevel@tonic-gate 	addr_blk = s1394_used_tree_search(hal, req->cmd_addr);
1153*7c478bd9Sstevel@tonic-gate 
1154*7c478bd9Sstevel@tonic-gate 	/* If it wasn't found, it isn't owned... */
1155*7c478bd9Sstevel@tonic-gate 	if (addr_blk == NULL) {
1156*7c478bd9Sstevel@tonic-gate 		/* Unlock the "used" tree */
1157*7c478bd9Sstevel@tonic-gate 		mutex_exit(&hal->addr_space_used_mutex);
1158*7c478bd9Sstevel@tonic-gate 		req->cmd_result = IEEE1394_RESP_ADDRESS_ERROR;
1159*7c478bd9Sstevel@tonic-gate 		(void) s1394_send_response(hal, req);
1160*7c478bd9Sstevel@tonic-gate 		return;
1161*7c478bd9Sstevel@tonic-gate 	}
1162*7c478bd9Sstevel@tonic-gate 
1163*7c478bd9Sstevel@tonic-gate 	/* Does the WHOLE request fit in the allocated block? */
1164*7c478bd9Sstevel@tonic-gate 	switch (req->cmd_type) {
1165*7c478bd9Sstevel@tonic-gate 	case CMD1394_ASYNCH_LOCK_32:
1166*7c478bd9Sstevel@tonic-gate 		end_of_request = (req->cmd_addr + IEEE1394_QUADLET) - 1;
1167*7c478bd9Sstevel@tonic-gate 		/* kstats - 32-bit lock request */
1168*7c478bd9Sstevel@tonic-gate 		hal->hal_kstats->arreq_lock32++;
1169*7c478bd9Sstevel@tonic-gate 		break;
1170*7c478bd9Sstevel@tonic-gate 
1171*7c478bd9Sstevel@tonic-gate 	case CMD1394_ASYNCH_LOCK_64:
1172*7c478bd9Sstevel@tonic-gate 		end_of_request = (req->cmd_addr + IEEE1394_OCTLET) - 1;
1173*7c478bd9Sstevel@tonic-gate 		/* kstats - 64-bit lock request */
1174*7c478bd9Sstevel@tonic-gate 		hal->hal_kstats->arreq_lock64++;
1175*7c478bd9Sstevel@tonic-gate 		break;
1176*7c478bd9Sstevel@tonic-gate 
1177*7c478bd9Sstevel@tonic-gate 	default:
1178*7c478bd9Sstevel@tonic-gate 		/* Unlock the "used" tree */
1179*7c478bd9Sstevel@tonic-gate 		mutex_exit(&hal->addr_space_used_mutex);
1180*7c478bd9Sstevel@tonic-gate 
1181*7c478bd9Sstevel@tonic-gate 		dip = hal->halinfo.dip;
1182*7c478bd9Sstevel@tonic-gate 
1183*7c478bd9Sstevel@tonic-gate 		/* An unexpected error in the HAL */
1184*7c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, HALT_ERROR_MESSAGE,
1185*7c478bd9Sstevel@tonic-gate 		    ddi_node_name(dip), ddi_get_instance(dip));
1186*7c478bd9Sstevel@tonic-gate 
1187*7c478bd9Sstevel@tonic-gate 		/* Disable the HAL */
1188*7c478bd9Sstevel@tonic-gate 		s1394_hal_shutdown(hal, B_TRUE);
1189*7c478bd9Sstevel@tonic-gate 
1190*7c478bd9Sstevel@tonic-gate 		return;
1191*7c478bd9Sstevel@tonic-gate 	}
1192*7c478bd9Sstevel@tonic-gate 
1193*7c478bd9Sstevel@tonic-gate 	if (end_of_request > addr_blk->addr_hi) {
1194*7c478bd9Sstevel@tonic-gate 		/* Unlock the "used" tree */
1195*7c478bd9Sstevel@tonic-gate 		mutex_exit(&hal->addr_space_used_mutex);
1196*7c478bd9Sstevel@tonic-gate 		req->cmd_result = IEEE1394_RESP_ADDRESS_ERROR;
1197*7c478bd9Sstevel@tonic-gate 		(void) s1394_send_response(hal, req);
1198*7c478bd9Sstevel@tonic-gate 		return;
1199*7c478bd9Sstevel@tonic-gate 	}
1200*7c478bd9Sstevel@tonic-gate 
1201*7c478bd9Sstevel@tonic-gate 	/* Is a lock request valid for this address space? */
1202*7c478bd9Sstevel@tonic-gate 	if (!(addr_blk->addr_enable & T1394_ADDR_LKENBL)) {
1203*7c478bd9Sstevel@tonic-gate 		/* Unlock the "used" tree */
1204*7c478bd9Sstevel@tonic-gate 		mutex_exit(&hal->addr_space_used_mutex);
1205*7c478bd9Sstevel@tonic-gate 		req->cmd_result = IEEE1394_RESP_TYPE_ERROR;
1206*7c478bd9Sstevel@tonic-gate 		(void) s1394_send_response(hal, req);
1207*7c478bd9Sstevel@tonic-gate 		return;
1208*7c478bd9Sstevel@tonic-gate 	}
1209*7c478bd9Sstevel@tonic-gate 
1210*7c478bd9Sstevel@tonic-gate 	/* Fill in the backing store if necessary */
1211*7c478bd9Sstevel@tonic-gate 	if (addr_blk->kmem_bufp != NULL) {
1212*7c478bd9Sstevel@tonic-gate 		offset = req->cmd_addr - addr_blk->addr_lo;
1213*7c478bd9Sstevel@tonic-gate 		bufp_addr = (uchar_t *)addr_blk->kmem_bufp + offset;
1214*7c478bd9Sstevel@tonic-gate 
1215*7c478bd9Sstevel@tonic-gate 		if (req->cmd_type == CMD1394_ASYNCH_LOCK_32) {
1216*7c478bd9Sstevel@tonic-gate 			uint32_t	old_value;
1217*7c478bd9Sstevel@tonic-gate 			uint32_t	arg_value;
1218*7c478bd9Sstevel@tonic-gate 			uint32_t	data_value;
1219*7c478bd9Sstevel@tonic-gate 			uint32_t	new_value;
1220*7c478bd9Sstevel@tonic-gate 
1221*7c478bd9Sstevel@tonic-gate 			arg_value	= req->cmd_u.l32.arg_value;
1222*7c478bd9Sstevel@tonic-gate 			data_value	= req->cmd_u.l32.data_value;
1223*7c478bd9Sstevel@tonic-gate 			lock_type	= req->cmd_u.l32.lock_type;
1224*7c478bd9Sstevel@tonic-gate 			bcopy((void *)bufp_addr, (void *)&old_value,
1225*7c478bd9Sstevel@tonic-gate 			    IEEE1394_QUADLET);
1226*7c478bd9Sstevel@tonic-gate 
1227*7c478bd9Sstevel@tonic-gate 			switch (lock_type) {
1228*7c478bd9Sstevel@tonic-gate 			case CMD1394_LOCK_MASK_SWAP:
1229*7c478bd9Sstevel@tonic-gate 				/* Mask-Swap (see P1394A - Table 1.7) */
1230*7c478bd9Sstevel@tonic-gate 				new_value = (data_value & arg_value) |
1231*7c478bd9Sstevel@tonic-gate 				    (old_value & ~arg_value);
1232*7c478bd9Sstevel@tonic-gate 				/* Copy new_value into backing store */
1233*7c478bd9Sstevel@tonic-gate 				bcopy((void *)&new_value, (void *)bufp_addr,
1234*7c478bd9Sstevel@tonic-gate 				    IEEE1394_QUADLET);
1235*7c478bd9Sstevel@tonic-gate 				req->cmd_u.l32.old_value = old_value;
1236*7c478bd9Sstevel@tonic-gate 				break;
1237*7c478bd9Sstevel@tonic-gate 
1238*7c478bd9Sstevel@tonic-gate 			case CMD1394_LOCK_COMPARE_SWAP:
1239*7c478bd9Sstevel@tonic-gate 				/* Compare-Swap */
1240*7c478bd9Sstevel@tonic-gate 				if (old_value == arg_value) {
1241*7c478bd9Sstevel@tonic-gate 					new_value = data_value;
1242*7c478bd9Sstevel@tonic-gate 					/* Copy new_value into backing store */
1243*7c478bd9Sstevel@tonic-gate 					bcopy((void *)&new_value,
1244*7c478bd9Sstevel@tonic-gate 					    (void *)bufp_addr,
1245*7c478bd9Sstevel@tonic-gate 					    IEEE1394_QUADLET);
1246*7c478bd9Sstevel@tonic-gate 				}
1247*7c478bd9Sstevel@tonic-gate 				req->cmd_u.l32.old_value = old_value;
1248*7c478bd9Sstevel@tonic-gate 				break;
1249*7c478bd9Sstevel@tonic-gate 
1250*7c478bd9Sstevel@tonic-gate 			case CMD1394_LOCK_FETCH_ADD:
1251*7c478bd9Sstevel@tonic-gate 				/* Fetch-Add (see P1394A - Table 1.7) */
1252*7c478bd9Sstevel@tonic-gate 				old_value = T1394_DATA32(old_value);
1253*7c478bd9Sstevel@tonic-gate 				new_value = old_value + data_value;
1254*7c478bd9Sstevel@tonic-gate 				new_value = T1394_DATA32(new_value);
1255*7c478bd9Sstevel@tonic-gate 				/* Copy new_value into backing store */
1256*7c478bd9Sstevel@tonic-gate 				bcopy((void *)&new_value, (void *)bufp_addr,
1257*7c478bd9Sstevel@tonic-gate 				    IEEE1394_QUADLET);
1258*7c478bd9Sstevel@tonic-gate 				req->cmd_u.l32.old_value = old_value;
1259*7c478bd9Sstevel@tonic-gate 				break;
1260*7c478bd9Sstevel@tonic-gate 
1261*7c478bd9Sstevel@tonic-gate 			case CMD1394_LOCK_LITTLE_ADD:
1262*7c478bd9Sstevel@tonic-gate 				/* Little-Add (see P1394A - Table 1.7) */
1263*7c478bd9Sstevel@tonic-gate 				old_value = T1394_DATA32(old_value);
1264*7c478bd9Sstevel@tonic-gate 				new_value = old_value + data_value;
1265*7c478bd9Sstevel@tonic-gate 				new_value = T1394_DATA32(new_value);
1266*7c478bd9Sstevel@tonic-gate 				/* Copy new_value into backing store */
1267*7c478bd9Sstevel@tonic-gate 				bcopy((void *)&new_value, (void *)bufp_addr,
1268*7c478bd9Sstevel@tonic-gate 				    IEEE1394_QUADLET);
1269*7c478bd9Sstevel@tonic-gate 				req->cmd_u.l32.old_value = old_value;
1270*7c478bd9Sstevel@tonic-gate 				break;
1271*7c478bd9Sstevel@tonic-gate 
1272*7c478bd9Sstevel@tonic-gate 			case CMD1394_LOCK_BOUNDED_ADD:
1273*7c478bd9Sstevel@tonic-gate 				/* Bounded-Add (see P1394A - Table 1.7) */
1274*7c478bd9Sstevel@tonic-gate 				old_value = T1394_DATA32(old_value);
1275*7c478bd9Sstevel@tonic-gate 				if (old_value != arg_value) {
1276*7c478bd9Sstevel@tonic-gate 					new_value = old_value + data_value;
1277*7c478bd9Sstevel@tonic-gate 					new_value = T1394_DATA32(new_value);
1278*7c478bd9Sstevel@tonic-gate 					/* Copy new_value into backing store */
1279*7c478bd9Sstevel@tonic-gate 					bcopy((void *)&new_value,
1280*7c478bd9Sstevel@tonic-gate 					    (void *)bufp_addr,
1281*7c478bd9Sstevel@tonic-gate 					    IEEE1394_QUADLET);
1282*7c478bd9Sstevel@tonic-gate 				}
1283*7c478bd9Sstevel@tonic-gate 				req->cmd_u.l32.old_value = old_value;
1284*7c478bd9Sstevel@tonic-gate 				break;
1285*7c478bd9Sstevel@tonic-gate 
1286*7c478bd9Sstevel@tonic-gate 			case CMD1394_LOCK_WRAP_ADD:
1287*7c478bd9Sstevel@tonic-gate 				/* Wrap-Add (see P1394A - Table 1.7) */
1288*7c478bd9Sstevel@tonic-gate 				old_value = T1394_DATA32(old_value);
1289*7c478bd9Sstevel@tonic-gate 				if (old_value != arg_value) {
1290*7c478bd9Sstevel@tonic-gate 					new_value = old_value + data_value;
1291*7c478bd9Sstevel@tonic-gate 				} else {
1292*7c478bd9Sstevel@tonic-gate 					new_value = data_value;
1293*7c478bd9Sstevel@tonic-gate 				}
1294*7c478bd9Sstevel@tonic-gate 				new_value = T1394_DATA32(new_value);
1295*7c478bd9Sstevel@tonic-gate 				/* Copy new_value into backing store */
1296*7c478bd9Sstevel@tonic-gate 				bcopy((void *)&new_value, (void *)bufp_addr,
1297*7c478bd9Sstevel@tonic-gate 				    IEEE1394_QUADLET);
1298*7c478bd9Sstevel@tonic-gate 				req->cmd_u.l32.old_value = old_value;
1299*7c478bd9Sstevel@tonic-gate 				break;
1300*7c478bd9Sstevel@tonic-gate 
1301*7c478bd9Sstevel@tonic-gate 			default:
1302*7c478bd9Sstevel@tonic-gate 				/* Unlock the "used" tree */
1303*7c478bd9Sstevel@tonic-gate 				mutex_exit(&hal->addr_space_used_mutex);
1304*7c478bd9Sstevel@tonic-gate 				req->cmd_result = IEEE1394_RESP_TYPE_ERROR;
1305*7c478bd9Sstevel@tonic-gate 				(void) s1394_send_response(hal, req);
1306*7c478bd9Sstevel@tonic-gate 				return;
1307*7c478bd9Sstevel@tonic-gate 			}
1308*7c478bd9Sstevel@tonic-gate 		} else {
1309*7c478bd9Sstevel@tonic-gate 			/* Handling for the 8-byte (64-bit) lock requests */
1310*7c478bd9Sstevel@tonic-gate 			uint64_t	old_value;
1311*7c478bd9Sstevel@tonic-gate 			uint64_t	arg_value;
1312*7c478bd9Sstevel@tonic-gate 			uint64_t	data_value;
1313*7c478bd9Sstevel@tonic-gate 			uint64_t	new_value;
1314*7c478bd9Sstevel@tonic-gate 
1315*7c478bd9Sstevel@tonic-gate 			arg_value	= req->cmd_u.l64.arg_value;
1316*7c478bd9Sstevel@tonic-gate 			data_value	= req->cmd_u.l64.data_value;
1317*7c478bd9Sstevel@tonic-gate 			lock_type	= req->cmd_u.l64.lock_type;
1318*7c478bd9Sstevel@tonic-gate 			bcopy((void *)bufp_addr, (void *)&old_value,
1319*7c478bd9Sstevel@tonic-gate 			    IEEE1394_OCTLET);
1320*7c478bd9Sstevel@tonic-gate 
1321*7c478bd9Sstevel@tonic-gate 			switch (lock_type) {
1322*7c478bd9Sstevel@tonic-gate 			case CMD1394_LOCK_MASK_SWAP:
1323*7c478bd9Sstevel@tonic-gate 				/* Mask-Swap (see P1394A - Table 1.7) */
1324*7c478bd9Sstevel@tonic-gate 				new_value = (data_value & arg_value) |
1325*7c478bd9Sstevel@tonic-gate 				    (old_value & ~arg_value);
1326*7c478bd9Sstevel@tonic-gate 				/* Copy new_value into backing store */
1327*7c478bd9Sstevel@tonic-gate 				bcopy((void *)&new_value, (void *)bufp_addr,
1328*7c478bd9Sstevel@tonic-gate 				    IEEE1394_OCTLET);
1329*7c478bd9Sstevel@tonic-gate 				req->cmd_u.l64.old_value = old_value;
1330*7c478bd9Sstevel@tonic-gate 				break;
1331*7c478bd9Sstevel@tonic-gate 
1332*7c478bd9Sstevel@tonic-gate 			case CMD1394_LOCK_COMPARE_SWAP:
1333*7c478bd9Sstevel@tonic-gate 				/* Compare-Swap */
1334*7c478bd9Sstevel@tonic-gate 				if (old_value == arg_value) {
1335*7c478bd9Sstevel@tonic-gate 					new_value = data_value;
1336*7c478bd9Sstevel@tonic-gate 					/* Copy new_value into backing store */
1337*7c478bd9Sstevel@tonic-gate 					bcopy((void *)&new_value,
1338*7c478bd9Sstevel@tonic-gate 					    (void *)bufp_addr,
1339*7c478bd9Sstevel@tonic-gate 					    IEEE1394_OCTLET);
1340*7c478bd9Sstevel@tonic-gate 				}
1341*7c478bd9Sstevel@tonic-gate 				req->cmd_u.l64.old_value = old_value;
1342*7c478bd9Sstevel@tonic-gate 				break;
1343*7c478bd9Sstevel@tonic-gate 
1344*7c478bd9Sstevel@tonic-gate 			case CMD1394_LOCK_FETCH_ADD:
1345*7c478bd9Sstevel@tonic-gate 				/* Fetch-Add (see P1394A - Table 1.7) */
1346*7c478bd9Sstevel@tonic-gate 				old_value = T1394_DATA64(old_value);
1347*7c478bd9Sstevel@tonic-gate 				new_value = old_value + data_value;
1348*7c478bd9Sstevel@tonic-gate 				new_value = T1394_DATA64(new_value);
1349*7c478bd9Sstevel@tonic-gate 				/* Copy new_value into backing store */
1350*7c478bd9Sstevel@tonic-gate 				bcopy((void *)&new_value, (void *)bufp_addr,
1351*7c478bd9Sstevel@tonic-gate 				    IEEE1394_OCTLET);
1352*7c478bd9Sstevel@tonic-gate 				req->cmd_u.l64.old_value = old_value;
1353*7c478bd9Sstevel@tonic-gate 				break;
1354*7c478bd9Sstevel@tonic-gate 
1355*7c478bd9Sstevel@tonic-gate 			case CMD1394_LOCK_LITTLE_ADD:
1356*7c478bd9Sstevel@tonic-gate 				/* Little-Add (see P1394A - Table 1.7) */
1357*7c478bd9Sstevel@tonic-gate 				old_value = T1394_DATA64(old_value);
1358*7c478bd9Sstevel@tonic-gate 				new_value = old_value + data_value;
1359*7c478bd9Sstevel@tonic-gate 				new_value = T1394_DATA64(new_value);
1360*7c478bd9Sstevel@tonic-gate 				/* Copy new_value into backing store */
1361*7c478bd9Sstevel@tonic-gate 				bcopy((void *)&new_value, (void *)bufp_addr,
1362*7c478bd9Sstevel@tonic-gate 				    IEEE1394_OCTLET);
1363*7c478bd9Sstevel@tonic-gate 				req->cmd_u.l64.old_value = old_value;
1364*7c478bd9Sstevel@tonic-gate 				break;
1365*7c478bd9Sstevel@tonic-gate 
1366*7c478bd9Sstevel@tonic-gate 			case CMD1394_LOCK_BOUNDED_ADD:
1367*7c478bd9Sstevel@tonic-gate 				/* Bounded-Add (see P1394A - Table 1.7) */
1368*7c478bd9Sstevel@tonic-gate 				old_value = T1394_DATA64(old_value);
1369*7c478bd9Sstevel@tonic-gate 				if (old_value != arg_value) {
1370*7c478bd9Sstevel@tonic-gate 					new_value = old_value + data_value;
1371*7c478bd9Sstevel@tonic-gate 					new_value = T1394_DATA64(new_value);
1372*7c478bd9Sstevel@tonic-gate 					/* Copy new_value into backing store */
1373*7c478bd9Sstevel@tonic-gate 					bcopy((void *)&new_value,
1374*7c478bd9Sstevel@tonic-gate 					    (void *)bufp_addr,
1375*7c478bd9Sstevel@tonic-gate 					    IEEE1394_OCTLET);
1376*7c478bd9Sstevel@tonic-gate 				}
1377*7c478bd9Sstevel@tonic-gate 				req->cmd_u.l64.old_value = old_value;
1378*7c478bd9Sstevel@tonic-gate 				break;
1379*7c478bd9Sstevel@tonic-gate 
1380*7c478bd9Sstevel@tonic-gate 			case CMD1394_LOCK_WRAP_ADD:
1381*7c478bd9Sstevel@tonic-gate 				/* Wrap-Add (see P1394A - Table 1.7) */
1382*7c478bd9Sstevel@tonic-gate 				old_value = T1394_DATA64(old_value);
1383*7c478bd9Sstevel@tonic-gate 				if (old_value != arg_value) {
1384*7c478bd9Sstevel@tonic-gate 					new_value = old_value + data_value;
1385*7c478bd9Sstevel@tonic-gate 				} else {
1386*7c478bd9Sstevel@tonic-gate 					new_value = data_value;
1387*7c478bd9Sstevel@tonic-gate 				}
1388*7c478bd9Sstevel@tonic-gate 				new_value = T1394_DATA64(new_value);
1389*7c478bd9Sstevel@tonic-gate 				/* Copy new_value into backing store */
1390*7c478bd9Sstevel@tonic-gate 				bcopy((void *)&new_value, (void *)bufp_addr,
1391*7c478bd9Sstevel@tonic-gate 				    IEEE1394_OCTLET);
1392*7c478bd9Sstevel@tonic-gate 				req->cmd_u.l64.old_value = old_value;
1393*7c478bd9Sstevel@tonic-gate 				break;
1394*7c478bd9Sstevel@tonic-gate 
1395*7c478bd9Sstevel@tonic-gate 			default:
1396*7c478bd9Sstevel@tonic-gate 				/* Unlock the "used" tree */
1397*7c478bd9Sstevel@tonic-gate 				mutex_exit(&hal->addr_space_used_mutex);
1398*7c478bd9Sstevel@tonic-gate 				req->cmd_result = IEEE1394_RESP_TYPE_ERROR;
1399*7c478bd9Sstevel@tonic-gate 				(void) s1394_send_response(hal, req);
1400*7c478bd9Sstevel@tonic-gate 				return;
1401*7c478bd9Sstevel@tonic-gate 			}
1402*7c478bd9Sstevel@tonic-gate 		}
1403*7c478bd9Sstevel@tonic-gate 	}
1404*7c478bd9Sstevel@tonic-gate 
1405*7c478bd9Sstevel@tonic-gate 	/* Fill in the rest of the info in the request */
1406*7c478bd9Sstevel@tonic-gate 	s_priv->arreq_valid_addr = B_TRUE;
1407*7c478bd9Sstevel@tonic-gate 	req->cmd_callback_arg	 = addr_blk->addr_arg;
1408*7c478bd9Sstevel@tonic-gate 	recv_lock_req		 = addr_blk->addr_events.recv_lock_request;
1409*7c478bd9Sstevel@tonic-gate 
1410*7c478bd9Sstevel@tonic-gate 	/* Unlock the "used" tree */
1411*7c478bd9Sstevel@tonic-gate 	mutex_exit(&hal->addr_space_used_mutex);
1412*7c478bd9Sstevel@tonic-gate 
1413*7c478bd9Sstevel@tonic-gate 	/*
1414*7c478bd9Sstevel@tonic-gate 	 * Add no code that modifies the command after the target
1415*7c478bd9Sstevel@tonic-gate 	 * callback is called or after the response is sent to the
1416*7c478bd9Sstevel@tonic-gate 	 * HAL.
1417*7c478bd9Sstevel@tonic-gate 	 */
1418*7c478bd9Sstevel@tonic-gate 	if (recv_lock_req != NULL) {
1419*7c478bd9Sstevel@tonic-gate 		recv_lock_req(req);
1420*7c478bd9Sstevel@tonic-gate 	} else {
1421*7c478bd9Sstevel@tonic-gate 		req->cmd_result = IEEE1394_RESP_COMPLETE;
1422*7c478bd9Sstevel@tonic-gate 		(void) s1394_send_response(hal, req);
1423*7c478bd9Sstevel@tonic-gate 		return;
1424*7c478bd9Sstevel@tonic-gate 	}
1425*7c478bd9Sstevel@tonic-gate }
1426*7c478bd9Sstevel@tonic-gate 
1427*7c478bd9Sstevel@tonic-gate /*
1428*7c478bd9Sstevel@tonic-gate  * Function:    h1394_ioctl()
1429*7c478bd9Sstevel@tonic-gate  * Input(s):    sl_private		The HAL "handle" returned by
1430*7c478bd9Sstevel@tonic-gate  *					    h1394_attach()
1431*7c478bd9Sstevel@tonic-gate  *		cmd			ioctl cmd
1432*7c478bd9Sstevel@tonic-gate  *		arg			argument for the ioctl cmd
1433*7c478bd9Sstevel@tonic-gate  *		mode			mode bits (see ioctl(9e))
1434*7c478bd9Sstevel@tonic-gate  *		cred_p			cred structure pointer
1435*7c478bd9Sstevel@tonic-gate  *		rval_p			pointer to return value (see ioctl(9e))
1436*7c478bd9Sstevel@tonic-gate  *
1437*7c478bd9Sstevel@tonic-gate  * Output(s):	EINVAL if not a DEVCTL ioctl, else return value from s1394_ioctl
1438*7c478bd9Sstevel@tonic-gate  *
1439*7c478bd9Sstevel@tonic-gate  * Description:	h1394_ioctl() implements non-HAL specific ioctls. Currently,
1440*7c478bd9Sstevel@tonic-gate  *		DEVCTL ioctls are the only generic ioctls supported.
1441*7c478bd9Sstevel@tonic-gate  */
1442*7c478bd9Sstevel@tonic-gate int
h1394_ioctl(void * sl_private,int cmd,intptr_t arg,int mode,cred_t * cred_p,int * rval_p)1443*7c478bd9Sstevel@tonic-gate h1394_ioctl(void *sl_private, int cmd, intptr_t arg, int mode, cred_t *cred_p,
1444*7c478bd9Sstevel@tonic-gate     int *rval_p)
1445*7c478bd9Sstevel@tonic-gate {
1446*7c478bd9Sstevel@tonic-gate 	int	status;
1447*7c478bd9Sstevel@tonic-gate 
1448*7c478bd9Sstevel@tonic-gate 	if ((cmd & DEVCTL_IOC) != DEVCTL_IOC)
1449*7c478bd9Sstevel@tonic-gate 		return (EINVAL);
1450*7c478bd9Sstevel@tonic-gate 
1451*7c478bd9Sstevel@tonic-gate 	status = s1394_ioctl((s1394_hal_t *)sl_private, cmd, arg, mode,
1452*7c478bd9Sstevel@tonic-gate 	    cred_p, rval_p);
1453*7c478bd9Sstevel@tonic-gate 
1454*7c478bd9Sstevel@tonic-gate 	return (status);
1455*7c478bd9Sstevel@tonic-gate }
1456*7c478bd9Sstevel@tonic-gate 
1457*7c478bd9Sstevel@tonic-gate /*
1458*7c478bd9Sstevel@tonic-gate  * Function:    h1394_phy_packet()
1459*7c478bd9Sstevel@tonic-gate  * Input(s):    sl_private		The HAL "handle" returned by
1460*7c478bd9Sstevel@tonic-gate  *					    h1394_attach()
1461*7c478bd9Sstevel@tonic-gate  *		packet_data		Pointer to a buffer of packet data
1462*7c478bd9Sstevel@tonic-gate  *		quadlet_count		Length of the buffer
1463*7c478bd9Sstevel@tonic-gate  *		timestamp		Timestamp indicating time of arrival
1464*7c478bd9Sstevel@tonic-gate  *
1465*7c478bd9Sstevel@tonic-gate  * Output(s):	None
1466*7c478bd9Sstevel@tonic-gate  *
1467*7c478bd9Sstevel@tonic-gate  * Description:	h1394_phy_packet() is not implemented currently, but would
1468*7c478bd9Sstevel@tonic-gate  *		be used to process the responses to PHY ping packets in P1394A
1469*7c478bd9Sstevel@tonic-gate  *		When one is sent out, a timestamp is given indicating its time
1470*7c478bd9Sstevel@tonic-gate  *		of departure. Comparing that old timestamp with this new
1471*7c478bd9Sstevel@tonic-gate  *		timestamp, we can determine the time of flight and can use
1472*7c478bd9Sstevel@tonic-gate  *		those times to optimize the gap count.
1473*7c478bd9Sstevel@tonic-gate  */
1474*7c478bd9Sstevel@tonic-gate /* ARGSUSED */
1475*7c478bd9Sstevel@tonic-gate void
h1394_phy_packet(void * sl_private,uint32_t * packet_data,uint_t quadlet_count,uint_t timestamp)1476*7c478bd9Sstevel@tonic-gate h1394_phy_packet(void *sl_private, uint32_t *packet_data, uint_t quadlet_count,
1477*7c478bd9Sstevel@tonic-gate 	uint_t timestamp)
1478*7c478bd9Sstevel@tonic-gate {
1479*7c478bd9Sstevel@tonic-gate 	/* This interface is not yet implemented */
1480*7c478bd9Sstevel@tonic-gate }
1481*7c478bd9Sstevel@tonic-gate 
1482*7c478bd9Sstevel@tonic-gate /*
1483*7c478bd9Sstevel@tonic-gate  * Function:    h1394_error_detected()
1484*7c478bd9Sstevel@tonic-gate  * Input(s):    sl_private		The HAL "handle" returned by
1485*7c478bd9Sstevel@tonic-gate  *					    h1394_attach()
1486*7c478bd9Sstevel@tonic-gate  *		type			The type of error the HAL detected
1487*7c478bd9Sstevel@tonic-gate  *		arg			Pointer to any extra information
1488*7c478bd9Sstevel@tonic-gate  *
1489*7c478bd9Sstevel@tonic-gate  * Output(s):	None
1490*7c478bd9Sstevel@tonic-gate  *
1491*7c478bd9Sstevel@tonic-gate  * Description:	h1394_error_detected() is used by the HAL to report errors
1492*7c478bd9Sstevel@tonic-gate  *		to the 1394 Software Framework.
1493*7c478bd9Sstevel@tonic-gate  */
1494*7c478bd9Sstevel@tonic-gate void
h1394_error_detected(void * sl_private,h1394_error_t type,void * arg)1495*7c478bd9Sstevel@tonic-gate h1394_error_detected(void *sl_private, h1394_error_t type, void *arg)
1496*7c478bd9Sstevel@tonic-gate {
1497*7c478bd9Sstevel@tonic-gate 	s1394_hal_t	*hal;
1498*7c478bd9Sstevel@tonic-gate 	uint_t		hal_node_num;
1499*7c478bd9Sstevel@tonic-gate 	uint_t		IRM_node_num;
1500*7c478bd9Sstevel@tonic-gate 
1501*7c478bd9Sstevel@tonic-gate 	hal = (s1394_hal_t *)sl_private;
1502*7c478bd9Sstevel@tonic-gate 
1503*7c478bd9Sstevel@tonic-gate 	switch (type) {
1504*7c478bd9Sstevel@tonic-gate 	case H1394_LOCK_RESP_ERR:
1505*7c478bd9Sstevel@tonic-gate 		/* If we are the IRM, then initiate a bus reset */
1506*7c478bd9Sstevel@tonic-gate 		mutex_enter(&hal->topology_tree_mutex);
1507*7c478bd9Sstevel@tonic-gate 		hal_node_num = IEEE1394_NODE_NUM(hal->node_id);
1508*7c478bd9Sstevel@tonic-gate 		IRM_node_num = hal->IRM_node;
1509*7c478bd9Sstevel@tonic-gate 		mutex_exit(&hal->topology_tree_mutex);
1510*7c478bd9Sstevel@tonic-gate 		if (IRM_node_num == hal_node_num)
1511*7c478bd9Sstevel@tonic-gate 			s1394_initiate_hal_reset(hal, NON_CRITICAL);
1512*7c478bd9Sstevel@tonic-gate 		break;
1513*7c478bd9Sstevel@tonic-gate 
1514*7c478bd9Sstevel@tonic-gate 	case H1394_POSTED_WR_ERR:
1515*7c478bd9Sstevel@tonic-gate 		break;
1516*7c478bd9Sstevel@tonic-gate 
1517*7c478bd9Sstevel@tonic-gate 	case H1394_SELF_INITIATED_SHUTDOWN:
1518*7c478bd9Sstevel@tonic-gate 		s1394_hal_shutdown(hal, B_FALSE);
1519*7c478bd9Sstevel@tonic-gate 		break;
1520*7c478bd9Sstevel@tonic-gate 
1521*7c478bd9Sstevel@tonic-gate 	case H1394_CYCLE_TOO_LONG:
1522*7c478bd9Sstevel@tonic-gate 		/* Set a timer to become cycle master after 1 second */
1523*7c478bd9Sstevel@tonic-gate 		mutex_enter(&hal->cm_timer_mutex);
1524*7c478bd9Sstevel@tonic-gate 		hal->cm_timer_set = B_TRUE;
1525*7c478bd9Sstevel@tonic-gate 		mutex_exit(&hal->cm_timer_mutex);
1526*7c478bd9Sstevel@tonic-gate 		hal->cm_timer = timeout(s1394_cycle_too_long_callback, hal,
1527*7c478bd9Sstevel@tonic-gate 		    drv_usectohz(CYCLE_MASTER_TIMER * 1000));
1528*7c478bd9Sstevel@tonic-gate 
1529*7c478bd9Sstevel@tonic-gate 		break;
1530*7c478bd9Sstevel@tonic-gate 
1531*7c478bd9Sstevel@tonic-gate 	default:
1532*7c478bd9Sstevel@tonic-gate 		break;
1533*7c478bd9Sstevel@tonic-gate 	}
1534*7c478bd9Sstevel@tonic-gate }
1535