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