xref: /onnv-gate/usr/src/uts/common/io/1394/adapters/hci1394_tlabel.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright (c) 1999-2000 by Sun Microsystems, Inc.
24*0Sstevel@tonic-gate  * All rights reserved.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate /*
30*0Sstevel@tonic-gate  * hci1394_tlabel.h
31*0Sstevel@tonic-gate  *   These routines track the tlabel usage for a 1394 adapter.
32*0Sstevel@tonic-gate  */
33*0Sstevel@tonic-gate 
34*0Sstevel@tonic-gate #include <sys/kmem.h>
35*0Sstevel@tonic-gate #include <sys/types.h>
36*0Sstevel@tonic-gate #include <sys/conf.h>
37*0Sstevel@tonic-gate #include <sys/ddi.h>
38*0Sstevel@tonic-gate #include <sys/sunddi.h>
39*0Sstevel@tonic-gate 
40*0Sstevel@tonic-gate #include <sys/1394/ieee1394.h>
41*0Sstevel@tonic-gate #include <sys/1394/adapters/hci1394.h>
42*0Sstevel@tonic-gate 
43*0Sstevel@tonic-gate 
44*0Sstevel@tonic-gate /*
45*0Sstevel@tonic-gate  * hci1394_tlabel_init()
46*0Sstevel@tonic-gate  *    Initialize the tlabel structures.  These structures will be protected
47*0Sstevel@tonic-gate  *    by a mutex at the iblock_cookie passed in.  Bad tlabels will be usable
48*0Sstevel@tonic-gate  *    when > reclaim_time_nS has gone by.  init() returns a handle to be used
49*0Sstevel@tonic-gate  *    for the rest of the tlabel functions.
50*0Sstevel@tonic-gate  */
51*0Sstevel@tonic-gate void
hci1394_tlabel_init(hci1394_drvinfo_t * drvinfo,hrtime_t reclaim_time_nS,hci1394_tlabel_handle_t * tlabel_handle)52*0Sstevel@tonic-gate hci1394_tlabel_init(hci1394_drvinfo_t *drvinfo, hrtime_t reclaim_time_nS,
53*0Sstevel@tonic-gate     hci1394_tlabel_handle_t *tlabel_handle)
54*0Sstevel@tonic-gate {
55*0Sstevel@tonic-gate 	hci1394_tlabel_t *tstruct;
56*0Sstevel@tonic-gate 
57*0Sstevel@tonic-gate 
58*0Sstevel@tonic-gate 	ASSERT(tlabel_handle != NULL);
59*0Sstevel@tonic-gate 	TNF_PROBE_0_DEBUG(hci1394_tlabel_init_enter, HCI1394_TNF_HAL_STACK, "");
60*0Sstevel@tonic-gate 
61*0Sstevel@tonic-gate 	/* alloc space for tlabel data */
62*0Sstevel@tonic-gate 	tstruct = kmem_alloc(sizeof (hci1394_tlabel_t), KM_SLEEP);
63*0Sstevel@tonic-gate 
64*0Sstevel@tonic-gate 	/* setup handle which is returned from this function */
65*0Sstevel@tonic-gate 	*tlabel_handle = tstruct;
66*0Sstevel@tonic-gate 
67*0Sstevel@tonic-gate 	/*
68*0Sstevel@tonic-gate 	 * Initialize tlabel structure. We start with max node set to the
69*0Sstevel@tonic-gate 	 * maxiumum node we could have so that we make sure the arrays are
70*0Sstevel@tonic-gate 	 * initialized correctly in hci1394_tlabel_reset().
71*0Sstevel@tonic-gate 	 */
72*0Sstevel@tonic-gate 	tstruct->tb_drvinfo = drvinfo;
73*0Sstevel@tonic-gate 	tstruct->tb_reclaim_time = reclaim_time_nS;
74*0Sstevel@tonic-gate 	tstruct->tb_max_node = TLABEL_RANGE - 1;
75*0Sstevel@tonic-gate 	tstruct->tb_bcast_sent = B_FALSE;
76*0Sstevel@tonic-gate 
77*0Sstevel@tonic-gate 	mutex_init(&tstruct->tb_mutex, NULL, MUTEX_DRIVER,
78*0Sstevel@tonic-gate 	    drvinfo->di_iblock_cookie);
79*0Sstevel@tonic-gate 
80*0Sstevel@tonic-gate 	/*
81*0Sstevel@tonic-gate 	 * The mutex must be initialized before tlabel_reset()
82*0Sstevel@tonic-gate 	 * is called.  This is because tlabel_reset is also
83*0Sstevel@tonic-gate 	 * used in normal tlabel processing (i.e. not just during
84*0Sstevel@tonic-gate 	 * initialization)
85*0Sstevel@tonic-gate 	 */
86*0Sstevel@tonic-gate 	hci1394_tlabel_reset(tstruct);
87*0Sstevel@tonic-gate 
88*0Sstevel@tonic-gate 	TNF_PROBE_0_DEBUG(hci1394_tlabel_init_exit, HCI1394_TNF_HAL_STACK, "");
89*0Sstevel@tonic-gate }
90*0Sstevel@tonic-gate 
91*0Sstevel@tonic-gate 
92*0Sstevel@tonic-gate /*
93*0Sstevel@tonic-gate  * hci1394_tlabel_fini()
94*0Sstevel@tonic-gate  *    Frees up the space allocated in init().  Notice that a pointer to the
95*0Sstevel@tonic-gate  *    handle is used for the parameter.  fini() will set your handle to NULL
96*0Sstevel@tonic-gate  *    before returning.
97*0Sstevel@tonic-gate  */
98*0Sstevel@tonic-gate void
hci1394_tlabel_fini(hci1394_tlabel_handle_t * tlabel_handle)99*0Sstevel@tonic-gate hci1394_tlabel_fini(hci1394_tlabel_handle_t *tlabel_handle)
100*0Sstevel@tonic-gate {
101*0Sstevel@tonic-gate 	hci1394_tlabel_t *tstruct;
102*0Sstevel@tonic-gate 
103*0Sstevel@tonic-gate 
104*0Sstevel@tonic-gate 	ASSERT(tlabel_handle != NULL);
105*0Sstevel@tonic-gate 	TNF_PROBE_0_DEBUG(hci1394_tlabel_fini_enter, HCI1394_TNF_HAL_STACK, "");
106*0Sstevel@tonic-gate 
107*0Sstevel@tonic-gate 	tstruct = (hci1394_tlabel_t *)*tlabel_handle;
108*0Sstevel@tonic-gate 
109*0Sstevel@tonic-gate 	mutex_destroy(&tstruct->tb_mutex);
110*0Sstevel@tonic-gate 	kmem_free(tstruct, sizeof (hci1394_tlabel_t));
111*0Sstevel@tonic-gate 
112*0Sstevel@tonic-gate 	/* set handle to null.  This helps catch bugs. */
113*0Sstevel@tonic-gate 	*tlabel_handle = NULL;
114*0Sstevel@tonic-gate 
115*0Sstevel@tonic-gate 	TNF_PROBE_0_DEBUG(hci1394_tlabel_fini_exit, HCI1394_TNF_HAL_STACK, "");
116*0Sstevel@tonic-gate }
117*0Sstevel@tonic-gate 
118*0Sstevel@tonic-gate 
119*0Sstevel@tonic-gate /*
120*0Sstevel@tonic-gate  * hci1394_tlabel_alloc()
121*0Sstevel@tonic-gate  *    alloc a tlabel based on the node id. If alloc fails, we are out of
122*0Sstevel@tonic-gate  *    tlabels for that node. See comments before set_reclaim_time() on when
123*0Sstevel@tonic-gate  *    bad tlabel's are free to be used again.
124*0Sstevel@tonic-gate  */
125*0Sstevel@tonic-gate int
hci1394_tlabel_alloc(hci1394_tlabel_handle_t tlabel_handle,uint_t destination,hci1394_tlabel_info_t * tlabel_info)126*0Sstevel@tonic-gate hci1394_tlabel_alloc(hci1394_tlabel_handle_t tlabel_handle, uint_t destination,
127*0Sstevel@tonic-gate     hci1394_tlabel_info_t *tlabel_info)
128*0Sstevel@tonic-gate {
129*0Sstevel@tonic-gate 	uint_t node_number;
130*0Sstevel@tonic-gate 	uint_t index;
131*0Sstevel@tonic-gate 	uint64_t bad;
132*0Sstevel@tonic-gate 	uint64_t free;
133*0Sstevel@tonic-gate 	hrtime_t time;
134*0Sstevel@tonic-gate 	uint8_t last;
135*0Sstevel@tonic-gate 
136*0Sstevel@tonic-gate 
137*0Sstevel@tonic-gate 	ASSERT(tlabel_handle != NULL);
138*0Sstevel@tonic-gate 	ASSERT(tlabel_info != NULL);
139*0Sstevel@tonic-gate 	TNF_PROBE_0_DEBUG(hci1394_tlabel_alloc_enter,
140*0Sstevel@tonic-gate 	    HCI1394_TNF_HAL_STACK, "");
141*0Sstevel@tonic-gate 
142*0Sstevel@tonic-gate 	/* copy destination into tlabel_info */
143*0Sstevel@tonic-gate 	tlabel_info->tbi_destination = destination;
144*0Sstevel@tonic-gate 
145*0Sstevel@tonic-gate 	/* figure out what node we are going to */
146*0Sstevel@tonic-gate 	node_number = IEEE1394_NODE_NUM(destination);
147*0Sstevel@tonic-gate 
148*0Sstevel@tonic-gate 	mutex_enter(&tlabel_handle->tb_mutex);
149*0Sstevel@tonic-gate 
150*0Sstevel@tonic-gate 	/*
151*0Sstevel@tonic-gate 	 * Keep track of if we have sent out a broadcast request and what the
152*0Sstevel@tonic-gate 	 * maximum # node we have sent to for reset processing optimization
153*0Sstevel@tonic-gate 	 */
154*0Sstevel@tonic-gate 	if (node_number == IEEE1394_BROADCAST_NODEID) {
155*0Sstevel@tonic-gate 		tlabel_handle->tb_bcast_sent = B_TRUE;
156*0Sstevel@tonic-gate 	} else if (node_number > tlabel_handle->tb_max_node) {
157*0Sstevel@tonic-gate 		tlabel_handle->tb_max_node = node_number;
158*0Sstevel@tonic-gate 	}
159*0Sstevel@tonic-gate 
160*0Sstevel@tonic-gate 	/* setup copies so we don't take up so much space :-) */
161*0Sstevel@tonic-gate 	bad = tlabel_handle->tb_bad[node_number];
162*0Sstevel@tonic-gate 	free = tlabel_handle->tb_free[node_number];
163*0Sstevel@tonic-gate 	time = tlabel_handle->tb_bad_timestamp[node_number];
164*0Sstevel@tonic-gate 	last = tlabel_handle->tb_last[node_number];
165*0Sstevel@tonic-gate 
166*0Sstevel@tonic-gate 	/*
167*0Sstevel@tonic-gate 	 * If there are any bad tlabels, see if the last bad tlabel recorded for
168*0Sstevel@tonic-gate 	 * this nodeid is now good to use. If so, add all bad tlabels for that
169*0Sstevel@tonic-gate 	 * node id back into the free list
170*0Sstevel@tonic-gate 	 *
171*0Sstevel@tonic-gate 	 * NOTE: This assumes that bad tlabels are infrequent.
172*0Sstevel@tonic-gate 	 */
173*0Sstevel@tonic-gate 	if (bad != 0) {
174*0Sstevel@tonic-gate 		if (gethrtime() > time) {
175*0Sstevel@tonic-gate 
176*0Sstevel@tonic-gate 			/* add the bad tlabels back into the free list */
177*0Sstevel@tonic-gate 			free |= bad;
178*0Sstevel@tonic-gate 
179*0Sstevel@tonic-gate 			/* clear the bad list */
180*0Sstevel@tonic-gate 			bad = 0;
181*0Sstevel@tonic-gate 
182*0Sstevel@tonic-gate 			TNF_PROBE_1(hci1394_tlabel_free_bad,
183*0Sstevel@tonic-gate 			    HCI1394_TNF_HAL_ERROR, "", tnf_uint, nodeid,
184*0Sstevel@tonic-gate 			    node_number);
185*0Sstevel@tonic-gate 		}
186*0Sstevel@tonic-gate 	}
187*0Sstevel@tonic-gate 
188*0Sstevel@tonic-gate 	/*
189*0Sstevel@tonic-gate 	 * Find a free tlabel.  This will break out of the loop once it finds a
190*0Sstevel@tonic-gate 	 * tlabel.  There are a total of TLABEL_RANGE tlabels.  The alloc
191*0Sstevel@tonic-gate 	 * rotates the check so that we don't always use the same tlabel. It
192*0Sstevel@tonic-gate 	 * stores the last tlabel used in last.
193*0Sstevel@tonic-gate 	 */
194*0Sstevel@tonic-gate 	for (index = 0; index < TLABEL_RANGE; index++) {
195*0Sstevel@tonic-gate 
196*0Sstevel@tonic-gate 		/* if the next tlabel to check is free */
197*0Sstevel@tonic-gate 		if ((free & ((uint64_t)1 << last)) != 0) {
198*0Sstevel@tonic-gate 			/* we are using this tlabel */
199*0Sstevel@tonic-gate 			tlabel_info->tbi_tlabel = last;
200*0Sstevel@tonic-gate 
201*0Sstevel@tonic-gate 			TNF_PROBE_2_DEBUG(hci1394_tlabel_alloc,
202*0Sstevel@tonic-gate 			    HCI1394_TNF_HAL_TLABEL, "", tnf_uint, nodeid,
203*0Sstevel@tonic-gate 			    node_number, tnf_uint, alloced_tlabel,
204*0Sstevel@tonic-gate 			    tlabel_info->tbi_tlabel);
205*0Sstevel@tonic-gate 
206*0Sstevel@tonic-gate 			/* take it out of the free list */
207*0Sstevel@tonic-gate 			free = free & ~((uint64_t)1 << last);
208*0Sstevel@tonic-gate 
209*0Sstevel@tonic-gate 			/*
210*0Sstevel@tonic-gate 			 * increment the last count so we start checking on the
211*0Sstevel@tonic-gate 			 * next tlabel next alloc().  Note the rollover at
212*0Sstevel@tonic-gate 			 * TLABEL_RANGE since we only have TLABEL_RANGE tlabels.
213*0Sstevel@tonic-gate 			 */
214*0Sstevel@tonic-gate 			(last)++;
215*0Sstevel@tonic-gate 			if (last >= TLABEL_RANGE) {
216*0Sstevel@tonic-gate 				last = 0;
217*0Sstevel@tonic-gate 			}
218*0Sstevel@tonic-gate 
219*0Sstevel@tonic-gate 			/* Copy the copies back */
220*0Sstevel@tonic-gate 			tlabel_handle->tb_bad[node_number] = bad;
221*0Sstevel@tonic-gate 			tlabel_handle->tb_free[node_number] = free;
222*0Sstevel@tonic-gate 			tlabel_handle->tb_bad_timestamp[node_number] = time;
223*0Sstevel@tonic-gate 			tlabel_handle->tb_last[node_number] = last;
224*0Sstevel@tonic-gate 
225*0Sstevel@tonic-gate 			/* unlock the tlabel structure */
226*0Sstevel@tonic-gate 			mutex_exit(&tlabel_handle->tb_mutex);
227*0Sstevel@tonic-gate 
228*0Sstevel@tonic-gate 			TNF_PROBE_0_DEBUG(hci1394_tlabel_alloc_exit,
229*0Sstevel@tonic-gate 			    HCI1394_TNF_HAL_STACK, "");
230*0Sstevel@tonic-gate 			return (DDI_SUCCESS);
231*0Sstevel@tonic-gate 		}
232*0Sstevel@tonic-gate 
233*0Sstevel@tonic-gate 		/*
234*0Sstevel@tonic-gate 		 * This tlabel is not free, lets go to the next one. Note the
235*0Sstevel@tonic-gate 		 * rollover at TLABEL_RANGE since we only have TLABEL_RANGE
236*0Sstevel@tonic-gate 		 * tlabels.
237*0Sstevel@tonic-gate 		 */
238*0Sstevel@tonic-gate 		(last)++;
239*0Sstevel@tonic-gate 		if (last >= TLABEL_RANGE) {
240*0Sstevel@tonic-gate 			last = 0;
241*0Sstevel@tonic-gate 		}
242*0Sstevel@tonic-gate 	}
243*0Sstevel@tonic-gate 
244*0Sstevel@tonic-gate 	/* Copy the copies back */
245*0Sstevel@tonic-gate 	tlabel_handle->tb_bad[node_number] = bad;
246*0Sstevel@tonic-gate 	tlabel_handle->tb_free[node_number] = free;
247*0Sstevel@tonic-gate 	tlabel_handle->tb_bad_timestamp[node_number] = time;
248*0Sstevel@tonic-gate 	tlabel_handle->tb_last[node_number] = last;
249*0Sstevel@tonic-gate 
250*0Sstevel@tonic-gate 	mutex_exit(&tlabel_handle->tb_mutex);
251*0Sstevel@tonic-gate 
252*0Sstevel@tonic-gate 	TNF_PROBE_1(hci1394_tlabel_alloc_empty, HCI1394_TNF_HAL_ERROR, "",
253*0Sstevel@tonic-gate 	    tnf_string, errmsg, "No more tlabels left to alloc");
254*0Sstevel@tonic-gate 	TNF_PROBE_0_DEBUG(hci1394_tlabel_alloc_exit, HCI1394_TNF_HAL_STACK, "");
255*0Sstevel@tonic-gate 
256*0Sstevel@tonic-gate 	return (DDI_FAILURE);
257*0Sstevel@tonic-gate }
258*0Sstevel@tonic-gate 
259*0Sstevel@tonic-gate 
260*0Sstevel@tonic-gate /*
261*0Sstevel@tonic-gate  * hci1394_tlabel_free()
262*0Sstevel@tonic-gate  *    free the previously alloc()'d tlabel.  Once a tlabel has been free'd, it
263*0Sstevel@tonic-gate  *    can be used again when alloc() is called.
264*0Sstevel@tonic-gate  */
265*0Sstevel@tonic-gate void
hci1394_tlabel_free(hci1394_tlabel_handle_t tlabel_handle,hci1394_tlabel_info_t * tlabel_info)266*0Sstevel@tonic-gate hci1394_tlabel_free(hci1394_tlabel_handle_t tlabel_handle,
267*0Sstevel@tonic-gate     hci1394_tlabel_info_t *tlabel_info)
268*0Sstevel@tonic-gate {
269*0Sstevel@tonic-gate 	uint_t node_number;
270*0Sstevel@tonic-gate 	uint_t tlabel;
271*0Sstevel@tonic-gate 
272*0Sstevel@tonic-gate 
273*0Sstevel@tonic-gate 	ASSERT(tlabel_handle != NULL);
274*0Sstevel@tonic-gate 	ASSERT(tlabel_info != NULL);
275*0Sstevel@tonic-gate 	ASSERT(tlabel_info->tbi_tlabel <= TLABEL_MASK);
276*0Sstevel@tonic-gate 	TNF_PROBE_0_DEBUG(hci1394_tlabel_free_enter, HCI1394_TNF_HAL_STACK, "");
277*0Sstevel@tonic-gate 
278*0Sstevel@tonic-gate 	/* figure out what node and tlabel we are using */
279*0Sstevel@tonic-gate 	node_number = IEEE1394_NODE_NUM(tlabel_info->tbi_destination);
280*0Sstevel@tonic-gate 	tlabel = tlabel_info->tbi_tlabel;
281*0Sstevel@tonic-gate 
282*0Sstevel@tonic-gate 	TNF_PROBE_2_DEBUG(hci1394_tlabel_free,
283*0Sstevel@tonic-gate 	    HCI1394_TNF_HAL_TLABEL, "", tnf_uint, nodeid, node_number,
284*0Sstevel@tonic-gate 	    tnf_uint, freed_tlabel, tlabel_info->tbi_tlabel);
285*0Sstevel@tonic-gate 
286*0Sstevel@tonic-gate 	mutex_enter(&tlabel_handle->tb_mutex);
287*0Sstevel@tonic-gate 
288*0Sstevel@tonic-gate 	/*
289*0Sstevel@tonic-gate 	 * Put the tlabel back in the free list and NULL out the (void *) in the
290*0Sstevel@tonic-gate 	 * lookup structure.  You wouldn't expect to have to null out the lookup
291*0Sstevel@tonic-gate 	 * structure, but we know first hand that bad HW will send invalid
292*0Sstevel@tonic-gate 	 * tlabels which could really mess things up if you didn't :-)
293*0Sstevel@tonic-gate 	 */
294*0Sstevel@tonic-gate 	tlabel_handle->tb_lookup[node_number][tlabel] = NULL;
295*0Sstevel@tonic-gate 	tlabel_handle->tb_free[node_number] |= ((uint64_t)1 << tlabel);
296*0Sstevel@tonic-gate 
297*0Sstevel@tonic-gate 	mutex_exit(&tlabel_handle->tb_mutex);
298*0Sstevel@tonic-gate 
299*0Sstevel@tonic-gate 	TNF_PROBE_0_DEBUG(hci1394_tlabel_free_exit, HCI1394_TNF_HAL_STACK, "");
300*0Sstevel@tonic-gate }
301*0Sstevel@tonic-gate 
302*0Sstevel@tonic-gate 
303*0Sstevel@tonic-gate /*
304*0Sstevel@tonic-gate  * hci1394_tlabel_register()
305*0Sstevel@tonic-gate  *    Register an opaque command with an alloc()'d tlabel. Each nodeID has it's
306*0Sstevel@tonic-gate  *    own tlabel list.
307*0Sstevel@tonic-gate  */
308*0Sstevel@tonic-gate void
hci1394_tlabel_register(hci1394_tlabel_handle_t tlabel_handle,hci1394_tlabel_info_t * tlabel_info,void * cmd)309*0Sstevel@tonic-gate hci1394_tlabel_register(hci1394_tlabel_handle_t tlabel_handle,
310*0Sstevel@tonic-gate     hci1394_tlabel_info_t *tlabel_info, void *cmd)
311*0Sstevel@tonic-gate {
312*0Sstevel@tonic-gate 	uint_t node_number;
313*0Sstevel@tonic-gate 	uint_t tlabel;
314*0Sstevel@tonic-gate 
315*0Sstevel@tonic-gate 
316*0Sstevel@tonic-gate 	ASSERT(tlabel_handle != NULL);
317*0Sstevel@tonic-gate 	ASSERT(tlabel_info != NULL);
318*0Sstevel@tonic-gate 	ASSERT(tlabel_info->tbi_tlabel <= TLABEL_MASK);
319*0Sstevel@tonic-gate 	TNF_PROBE_0_DEBUG(hci1394_tlabel_register_enter,
320*0Sstevel@tonic-gate 	    HCI1394_TNF_HAL_STACK, "");
321*0Sstevel@tonic-gate 
322*0Sstevel@tonic-gate 	/* figure out what node and tlabel we are using */
323*0Sstevel@tonic-gate 	node_number = IEEE1394_NODE_NUM(tlabel_info->tbi_destination);
324*0Sstevel@tonic-gate 	tlabel = tlabel_info->tbi_tlabel;
325*0Sstevel@tonic-gate 
326*0Sstevel@tonic-gate 	mutex_enter(&tlabel_handle->tb_mutex);
327*0Sstevel@tonic-gate 
328*0Sstevel@tonic-gate 	/* enter the (void *) into the lookup table */
329*0Sstevel@tonic-gate 	tlabel_handle->tb_lookup[node_number][tlabel] = cmd;
330*0Sstevel@tonic-gate 
331*0Sstevel@tonic-gate 	mutex_exit(&tlabel_handle->tb_mutex);
332*0Sstevel@tonic-gate 
333*0Sstevel@tonic-gate 	TNF_PROBE_0_DEBUG(hci1394_tlabel_register_exit,
334*0Sstevel@tonic-gate 	    HCI1394_TNF_HAL_STACK, "");
335*0Sstevel@tonic-gate }
336*0Sstevel@tonic-gate 
337*0Sstevel@tonic-gate 
338*0Sstevel@tonic-gate /*
339*0Sstevel@tonic-gate  * hci1394_tlabel_lookup()
340*0Sstevel@tonic-gate  *    returns (in cmd) the opaque command which was registered with the
341*0Sstevel@tonic-gate  *    specified tlabel from alloc(). If a tlabel was not registered, cmd ='s
342*0Sstevel@tonic-gate  *    NULL.
343*0Sstevel@tonic-gate  */
344*0Sstevel@tonic-gate void
hci1394_tlabel_lookup(hci1394_tlabel_handle_t tlabel_handle,hci1394_tlabel_info_t * tlabel_info,void ** cmd)345*0Sstevel@tonic-gate hci1394_tlabel_lookup(hci1394_tlabel_handle_t tlabel_handle,
346*0Sstevel@tonic-gate     hci1394_tlabel_info_t *tlabel_info, void **cmd)
347*0Sstevel@tonic-gate {
348*0Sstevel@tonic-gate 	uint_t node_number;
349*0Sstevel@tonic-gate 	uint_t tlabel;
350*0Sstevel@tonic-gate 
351*0Sstevel@tonic-gate 
352*0Sstevel@tonic-gate 	ASSERT(tlabel_handle != NULL);
353*0Sstevel@tonic-gate 	ASSERT(tlabel_info != NULL);
354*0Sstevel@tonic-gate 	ASSERT(cmd != NULL);
355*0Sstevel@tonic-gate 	ASSERT(tlabel_info->tbi_tlabel <= TLABEL_MASK);
356*0Sstevel@tonic-gate 	TNF_PROBE_0_DEBUG(hci1394_tlabel_lookup_enter,
357*0Sstevel@tonic-gate 	    HCI1394_TNF_HAL_STACK, "");
358*0Sstevel@tonic-gate 
359*0Sstevel@tonic-gate 	/* figure out what node and tlabel we are using */
360*0Sstevel@tonic-gate 	node_number = IEEE1394_NODE_NUM(tlabel_info->tbi_destination);
361*0Sstevel@tonic-gate 	tlabel = tlabel_info->tbi_tlabel;
362*0Sstevel@tonic-gate 
363*0Sstevel@tonic-gate 	mutex_enter(&tlabel_handle->tb_mutex);
364*0Sstevel@tonic-gate 
365*0Sstevel@tonic-gate 	/*
366*0Sstevel@tonic-gate 	 * fetch the (void *) from the lookup table.  The case where the pointer
367*0Sstevel@tonic-gate 	 * equals NULL will be handled by the layer above.
368*0Sstevel@tonic-gate 	 */
369*0Sstevel@tonic-gate 	*cmd = tlabel_handle->tb_lookup[node_number][tlabel];
370*0Sstevel@tonic-gate 
371*0Sstevel@tonic-gate 	mutex_exit(&tlabel_handle->tb_mutex);
372*0Sstevel@tonic-gate 
373*0Sstevel@tonic-gate 	TNF_PROBE_2_DEBUG(hci1394_tlabel_lookup,
374*0Sstevel@tonic-gate 	    HCI1394_TNF_HAL_TLABEL, "", tnf_uint, nodeid, node_number,
375*0Sstevel@tonic-gate 	    tnf_uint, lookup_tlabel, tlabel_info->tbi_tlabel);
376*0Sstevel@tonic-gate 
377*0Sstevel@tonic-gate 	TNF_PROBE_0_DEBUG(hci1394_tlabel_lookup_exit,
378*0Sstevel@tonic-gate 	    HCI1394_TNF_HAL_STACK, "");
379*0Sstevel@tonic-gate }
380*0Sstevel@tonic-gate 
381*0Sstevel@tonic-gate 
382*0Sstevel@tonic-gate /*
383*0Sstevel@tonic-gate  * hci1394_tlabel_bad()
384*0Sstevel@tonic-gate  *    Register the specified tlabel as bad.  tlabel_lookup() will no longer
385*0Sstevel@tonic-gate  *    return a registered opaque command and this tlabel will not be returned
386*0Sstevel@tonic-gate  *    from alloc() until > reclaim_time has passed. See set_reclaim_time() for
387*0Sstevel@tonic-gate  *    more info.
388*0Sstevel@tonic-gate  */
389*0Sstevel@tonic-gate void
hci1394_tlabel_bad(hci1394_tlabel_handle_t tlabel_handle,hci1394_tlabel_info_t * tlabel_info)390*0Sstevel@tonic-gate hci1394_tlabel_bad(hci1394_tlabel_handle_t tlabel_handle,
391*0Sstevel@tonic-gate     hci1394_tlabel_info_t *tlabel_info)
392*0Sstevel@tonic-gate {
393*0Sstevel@tonic-gate 	uint_t node_number;
394*0Sstevel@tonic-gate 	uint_t tlabel;
395*0Sstevel@tonic-gate 
396*0Sstevel@tonic-gate 
397*0Sstevel@tonic-gate 	ASSERT(tlabel_handle != NULL);
398*0Sstevel@tonic-gate 	ASSERT(tlabel_info != NULL);
399*0Sstevel@tonic-gate 	TNF_PROBE_0_DEBUG(hci1394_tlabel_bad_enter, HCI1394_TNF_HAL_STACK, "");
400*0Sstevel@tonic-gate 
401*0Sstevel@tonic-gate 	/* figure out what node and tlabel we are using */
402*0Sstevel@tonic-gate 	node_number = IEEE1394_NODE_NUM(tlabel_info->tbi_destination);
403*0Sstevel@tonic-gate 	tlabel = tlabel_info->tbi_tlabel & TLABEL_MASK;
404*0Sstevel@tonic-gate 
405*0Sstevel@tonic-gate 	mutex_enter(&tlabel_handle->tb_mutex);
406*0Sstevel@tonic-gate 
407*0Sstevel@tonic-gate 	TNF_PROBE_2(hci1394_tlabel_timeout, HCI1394_TNF_HAL_ERROR, "", tnf_uint,
408*0Sstevel@tonic-gate 	    nodeid, node_number, tnf_uint, bad_tlabel, tlabel_info->tbi_tlabel);
409*0Sstevel@tonic-gate 
410*0Sstevel@tonic-gate 	/*
411*0Sstevel@tonic-gate 	 * Put the tlabel in the bad list and NULL out the (void *) in the
412*0Sstevel@tonic-gate 	 * lookup structure.  We may see this tlabel shortly if the device is
413*0Sstevel@tonic-gate 	 * late in responding. We want to make sure to drop the message if we
414*0Sstevel@tonic-gate 	 * do. Set the bad timestamp to the current time plus the reclaim time.
415*0Sstevel@tonic-gate 	 * This is the "new" time when all of the bad tlabels for this node will
416*0Sstevel@tonic-gate 	 * be free'd.
417*0Sstevel@tonic-gate 	 */
418*0Sstevel@tonic-gate 	tlabel_handle->tb_bad_timestamp[node_number] = gethrtime() +
419*0Sstevel@tonic-gate 	    tlabel_handle->tb_reclaim_time;
420*0Sstevel@tonic-gate 	tlabel_handle->tb_bad[node_number] |= ((uint64_t)1 << tlabel);
421*0Sstevel@tonic-gate 	tlabel_handle->tb_lookup[node_number][tlabel] = NULL;
422*0Sstevel@tonic-gate 
423*0Sstevel@tonic-gate 	mutex_exit(&tlabel_handle->tb_mutex);
424*0Sstevel@tonic-gate 
425*0Sstevel@tonic-gate 	TNF_PROBE_0_DEBUG(hci1394_tlabel_bad_exit, HCI1394_TNF_HAL_STACK, "");
426*0Sstevel@tonic-gate }
427*0Sstevel@tonic-gate 
428*0Sstevel@tonic-gate 
429*0Sstevel@tonic-gate /*
430*0Sstevel@tonic-gate  * hci1394_tlabel_reset()
431*0Sstevel@tonic-gate  *    resets the tlabel tracking structures to an initial state where no
432*0Sstevel@tonic-gate  *    tlabels are outstanding and all tlabels are registered as good.  This
433*0Sstevel@tonic-gate  *    routine should be called every bus reset.
434*0Sstevel@tonic-gate  */
435*0Sstevel@tonic-gate void
hci1394_tlabel_reset(hci1394_tlabel_handle_t tlabel_handle)436*0Sstevel@tonic-gate hci1394_tlabel_reset(hci1394_tlabel_handle_t tlabel_handle)
437*0Sstevel@tonic-gate {
438*0Sstevel@tonic-gate 	int index;
439*0Sstevel@tonic-gate 	int index2;
440*0Sstevel@tonic-gate 
441*0Sstevel@tonic-gate 
442*0Sstevel@tonic-gate 	ASSERT(tlabel_handle != NULL);
443*0Sstevel@tonic-gate 	TNF_PROBE_0_DEBUG(hci1394_tlabel_reset_enter,
444*0Sstevel@tonic-gate 	    HCI1394_TNF_HAL_STACK, "");
445*0Sstevel@tonic-gate 
446*0Sstevel@tonic-gate 	mutex_enter(&tlabel_handle->tb_mutex);
447*0Sstevel@tonic-gate 
448*0Sstevel@tonic-gate 	TNF_PROBE_0_DEBUG(hci1394_tlabel_reset, HCI1394_TNF_HAL_TLABEL, "");
449*0Sstevel@tonic-gate 
450*0Sstevel@tonic-gate 	/* Bus reset optimization. handle broadcast writes separately */
451*0Sstevel@tonic-gate 	if (tlabel_handle->tb_bcast_sent == B_TRUE) {
452*0Sstevel@tonic-gate 		tlabel_handle->tb_free[IEEE1394_BROADCAST_NODEID] =
453*0Sstevel@tonic-gate 		    (uint64_t)0xFFFFFFFFFFFFFFFF;
454*0Sstevel@tonic-gate 		tlabel_handle->tb_bad[IEEE1394_BROADCAST_NODEID] =
455*0Sstevel@tonic-gate 		    (uint64_t)0;
456*0Sstevel@tonic-gate 		tlabel_handle->tb_bad_timestamp[IEEE1394_BROADCAST_NODEID] =
457*0Sstevel@tonic-gate 		    (hrtime_t)0;
458*0Sstevel@tonic-gate 		tlabel_handle->tb_last[IEEE1394_BROADCAST_NODEID] = 0;
459*0Sstevel@tonic-gate 		for (index2 = 0; index2 < TLABEL_RANGE; index2++) {
460*0Sstevel@tonic-gate 			tlabel_handle->tb_lookup[IEEE1394_BROADCAST_NODEID
461*0Sstevel@tonic-gate 			    ][index2] = NULL;
462*0Sstevel@tonic-gate 		}
463*0Sstevel@tonic-gate 	}
464*0Sstevel@tonic-gate 
465*0Sstevel@tonic-gate 	/*
466*0Sstevel@tonic-gate 	 * Mark all tlabels as free.  No bad tlabels.  Start the first tlabel
467*0Sstevel@tonic-gate 	 * alloc at 0.  Cleanout the lookup table.  An optimization to only do
468*0Sstevel@tonic-gate 	 * this up to the max node we have seen on the bus has been added.
469*0Sstevel@tonic-gate 	 */
470*0Sstevel@tonic-gate 	for (index = 0; index <= tlabel_handle->tb_max_node; index++) {
471*0Sstevel@tonic-gate 		tlabel_handle->tb_free[index] = (uint64_t)0xFFFFFFFFFFFFFFFF;
472*0Sstevel@tonic-gate 		tlabel_handle->tb_bad[index] = (uint64_t)0;
473*0Sstevel@tonic-gate 		tlabel_handle->tb_bad_timestamp[index] = (hrtime_t)0;
474*0Sstevel@tonic-gate 		tlabel_handle->tb_last[index] = 0;
475*0Sstevel@tonic-gate 		for (index2 = 0; index2 < TLABEL_RANGE; index2++) {
476*0Sstevel@tonic-gate 			tlabel_handle->tb_lookup[index][index2] = NULL;
477*0Sstevel@tonic-gate 		}
478*0Sstevel@tonic-gate 	}
479*0Sstevel@tonic-gate 
480*0Sstevel@tonic-gate 	tlabel_handle->tb_max_node = 0;
481*0Sstevel@tonic-gate 	tlabel_handle->tb_bcast_sent = B_FALSE;
482*0Sstevel@tonic-gate 
483*0Sstevel@tonic-gate 	mutex_exit(&tlabel_handle->tb_mutex);
484*0Sstevel@tonic-gate 
485*0Sstevel@tonic-gate 	TNF_PROBE_0_DEBUG(hci1394_tlabel_reset_exit, HCI1394_TNF_HAL_STACK, "");
486*0Sstevel@tonic-gate }
487*0Sstevel@tonic-gate 
488*0Sstevel@tonic-gate 
489*0Sstevel@tonic-gate /*
490*0Sstevel@tonic-gate  * hci1394_tlabel_set_reclaim_time()
491*0Sstevel@tonic-gate  *    This function should be called if a change to the reclaim_time is
492*0Sstevel@tonic-gate  *    required after the initial call to init().  It is not necessary to call
493*0Sstevel@tonic-gate  *    this function if the reclaim time never changes.
494*0Sstevel@tonic-gate  *
495*0Sstevel@tonic-gate  *    Currently, bad tlabels are reclaimed in tlabel_alloc().
496*0Sstevel@tonic-gate  *    It looks like the following for a given node:
497*0Sstevel@tonic-gate  *
498*0Sstevel@tonic-gate  *    if bad tlabels exist
499*0Sstevel@tonic-gate  *	    if ((current time + reclaim time) >= last bad tlabel time)
500*0Sstevel@tonic-gate  *		    free all bad tlabels.
501*0Sstevel@tonic-gate  */
502*0Sstevel@tonic-gate void
hci1394_tlabel_set_reclaim_time(hci1394_tlabel_handle_t tlabel_handle,hrtime_t reclaim_time_nS)503*0Sstevel@tonic-gate hci1394_tlabel_set_reclaim_time(hci1394_tlabel_handle_t tlabel_handle,
504*0Sstevel@tonic-gate     hrtime_t reclaim_time_nS)
505*0Sstevel@tonic-gate {
506*0Sstevel@tonic-gate 	ASSERT(tlabel_handle != NULL);
507*0Sstevel@tonic-gate 	TNF_PROBE_0_DEBUG(hci1394_tlabel_set_reclaim_time_enter,
508*0Sstevel@tonic-gate 	    HCI1394_TNF_HAL_STACK, "");
509*0Sstevel@tonic-gate 
510*0Sstevel@tonic-gate 	/*
511*0Sstevel@tonic-gate 	 * We do not need to lock the tlabel structure in this because we are
512*0Sstevel@tonic-gate 	 * doing a single write to reclaim_time. If this changes in the future,
513*0Sstevel@tonic-gate 	 * we may need to add calls to lock() and unlock().
514*0Sstevel@tonic-gate 	 */
515*0Sstevel@tonic-gate 	tlabel_handle->tb_reclaim_time = reclaim_time_nS;
516*0Sstevel@tonic-gate 
517*0Sstevel@tonic-gate 	TNF_PROBE_0_DEBUG(hci1394_tlabel_set_reclaim_time_exit,
518*0Sstevel@tonic-gate 	    HCI1394_TNF_HAL_STACK, "");
519*0Sstevel@tonic-gate }
520