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_tlist.c
29*7c478bd9Sstevel@tonic-gate  *   This implements a timed double linked list.
30*7c478bd9Sstevel@tonic-gate  *   This list supports:
31*7c478bd9Sstevel@tonic-gate  *	- addition of node to the end of the list
32*7c478bd9Sstevel@tonic-gate  *	- atomic deletion of node anywhere in list
33*7c478bd9Sstevel@tonic-gate  *	- get and remove node from head of list
34*7c478bd9Sstevel@tonic-gate  *	- enable/disable of timeout feature
35*7c478bd9Sstevel@tonic-gate  *	- timeout feature, if enabled, will remove each node on the list which
36*7c478bd9Sstevel@tonic-gate  *	  has been on the list for > timeout.  The callback provided will be
37*7c478bd9Sstevel@tonic-gate  *	  called for each node removed. The worst case time is around
38*7c478bd9Sstevel@tonic-gate  *	  timer_resolution after the timeout has occurred (i.e. if you set the
39*7c478bd9Sstevel@tonic-gate  *	  timer resolution to 50uS and the timeout to 100uS, you could get the
40*7c478bd9Sstevel@tonic-gate  *	  callback anywhere from 100uS to 150uS from when you added the node to
41*7c478bd9Sstevel@tonic-gate  *	  the list.  This is a general statement and ignores things like
42*7c478bd9Sstevel@tonic-gate  *	  interrupt latency, context switching, etc.  So if you see a time
43*7c478bd9Sstevel@tonic-gate  *	  around 155uS, don't complain :-)
44*7c478bd9Sstevel@tonic-gate  *	- The timer is only used when something is on the list
45*7c478bd9Sstevel@tonic-gate  */
46*7c478bd9Sstevel@tonic-gate 
47*7c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
48*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
49*7c478bd9Sstevel@tonic-gate #include <sys/conf.h>
50*7c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
51*7c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
52*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
53*7c478bd9Sstevel@tonic-gate 
54*7c478bd9Sstevel@tonic-gate #include <sys/1394/adapters/hci1394.h>
55*7c478bd9Sstevel@tonic-gate 
56*7c478bd9Sstevel@tonic-gate 
57*7c478bd9Sstevel@tonic-gate static clock_t t1394_tlist_nsectohz(hrtime_t  nS);
58*7c478bd9Sstevel@tonic-gate static void hci1394_tlist_remove(hci1394_tlist_t *list,
59*7c478bd9Sstevel@tonic-gate     hci1394_tlist_node_t *node);
60*7c478bd9Sstevel@tonic-gate static void hci1394_tlist_callback(void *tlist_handle);
61*7c478bd9Sstevel@tonic-gate 
62*7c478bd9Sstevel@tonic-gate 
63*7c478bd9Sstevel@tonic-gate /*
64*7c478bd9Sstevel@tonic-gate  * hci1394_tlist_init()
65*7c478bd9Sstevel@tonic-gate  *    Initialize the tlist.  The list will be protected by a mutex at the
66*7c478bd9Sstevel@tonic-gate  *    iblock_cookie passed in.  init() returns a handle to be used for the rest
67*7c478bd9Sstevel@tonic-gate  *    of the functions. If you do not wish to use the timeout feature, set
68*7c478bd9Sstevel@tonic-gate  *    (hci1394_timer_t *) to null.
69*7c478bd9Sstevel@tonic-gate  */
70*7c478bd9Sstevel@tonic-gate void
hci1394_tlist_init(hci1394_drvinfo_t * drvinfo,hci1394_tlist_timer_t * timer,hci1394_tlist_handle_t * tlist_handle)71*7c478bd9Sstevel@tonic-gate hci1394_tlist_init(hci1394_drvinfo_t *drvinfo, hci1394_tlist_timer_t *timer,
72*7c478bd9Sstevel@tonic-gate     hci1394_tlist_handle_t *tlist_handle)
73*7c478bd9Sstevel@tonic-gate {
74*7c478bd9Sstevel@tonic-gate 	hci1394_tlist_t *list;
75*7c478bd9Sstevel@tonic-gate 
76*7c478bd9Sstevel@tonic-gate 
77*7c478bd9Sstevel@tonic-gate 	ASSERT(tlist_handle != NULL);
78*7c478bd9Sstevel@tonic-gate 
79*7c478bd9Sstevel@tonic-gate 	/* try to alloc the space to keep track of the list */
80*7c478bd9Sstevel@tonic-gate 	list = kmem_alloc(sizeof (hci1394_tlist_t), KM_SLEEP);
81*7c478bd9Sstevel@tonic-gate 
82*7c478bd9Sstevel@tonic-gate 	/* setup the return parameter */
83*7c478bd9Sstevel@tonic-gate 	*tlist_handle = list;
84*7c478bd9Sstevel@tonic-gate 
85*7c478bd9Sstevel@tonic-gate 	/* initialize the list structure */
86*7c478bd9Sstevel@tonic-gate 	list->tl_drvinfo = drvinfo;
87*7c478bd9Sstevel@tonic-gate 	list->tl_state = HCI1394_TLIST_TIMEOUT_OFF;
88*7c478bd9Sstevel@tonic-gate 	list->tl_head = NULL;
89*7c478bd9Sstevel@tonic-gate 	list->tl_tail = NULL;
90*7c478bd9Sstevel@tonic-gate 	if (timer == NULL) {
91*7c478bd9Sstevel@tonic-gate 		list->tl_timer_enabled = B_FALSE;
92*7c478bd9Sstevel@tonic-gate 	} else {
93*7c478bd9Sstevel@tonic-gate 		ASSERT(timer->tlt_callback != NULL);
94*7c478bd9Sstevel@tonic-gate 		list->tl_timer_enabled = B_TRUE;
95*7c478bd9Sstevel@tonic-gate 		list->tl_timer_info = *timer;
96*7c478bd9Sstevel@tonic-gate 	}
97*7c478bd9Sstevel@tonic-gate 	mutex_init(&list->tl_mutex, NULL, MUTEX_DRIVER,
98*7c478bd9Sstevel@tonic-gate 	    drvinfo->di_iblock_cookie);
99*7c478bd9Sstevel@tonic-gate }
100*7c478bd9Sstevel@tonic-gate 
101*7c478bd9Sstevel@tonic-gate 
102*7c478bd9Sstevel@tonic-gate /*
103*7c478bd9Sstevel@tonic-gate  * hci1394_tlist_fini()
104*7c478bd9Sstevel@tonic-gate  *    Frees up the space allocated in init().  Notice that a pointer to the
105*7c478bd9Sstevel@tonic-gate  *    handle is used for the parameter.  fini() will set your handle to NULL
106*7c478bd9Sstevel@tonic-gate  *    before returning. Make sure that any pending timeouts are canceled.
107*7c478bd9Sstevel@tonic-gate  */
108*7c478bd9Sstevel@tonic-gate void
hci1394_tlist_fini(hci1394_tlist_handle_t * tlist_handle)109*7c478bd9Sstevel@tonic-gate hci1394_tlist_fini(hci1394_tlist_handle_t *tlist_handle)
110*7c478bd9Sstevel@tonic-gate {
111*7c478bd9Sstevel@tonic-gate 	hci1394_tlist_t *list;
112*7c478bd9Sstevel@tonic-gate 
113*7c478bd9Sstevel@tonic-gate 
114*7c478bd9Sstevel@tonic-gate 	ASSERT(tlist_handle != NULL);
115*7c478bd9Sstevel@tonic-gate 
116*7c478bd9Sstevel@tonic-gate 	list = (hci1394_tlist_t *)*tlist_handle;
117*7c478bd9Sstevel@tonic-gate 	hci1394_tlist_timeout_cancel(list);
118*7c478bd9Sstevel@tonic-gate 	mutex_destroy(&list->tl_mutex);
119*7c478bd9Sstevel@tonic-gate 	kmem_free(list, sizeof (hci1394_tlist_t));
120*7c478bd9Sstevel@tonic-gate 
121*7c478bd9Sstevel@tonic-gate 	/* set handle to null.  This helps catch bugs. */
122*7c478bd9Sstevel@tonic-gate 	*tlist_handle = NULL;
123*7c478bd9Sstevel@tonic-gate }
124*7c478bd9Sstevel@tonic-gate 
125*7c478bd9Sstevel@tonic-gate 
126*7c478bd9Sstevel@tonic-gate /*
127*7c478bd9Sstevel@tonic-gate  * hci1394_tlist_add()
128*7c478bd9Sstevel@tonic-gate  *    Add the node to the tail of the linked list. The list is protected by a
129*7c478bd9Sstevel@tonic-gate  *    mutex at the iblock_cookie passed in during init.
130*7c478bd9Sstevel@tonic-gate  */
131*7c478bd9Sstevel@tonic-gate void
hci1394_tlist_add(hci1394_tlist_handle_t tlist_handle,hci1394_tlist_node_t * node)132*7c478bd9Sstevel@tonic-gate hci1394_tlist_add(hci1394_tlist_handle_t tlist_handle,
133*7c478bd9Sstevel@tonic-gate     hci1394_tlist_node_t *node)
134*7c478bd9Sstevel@tonic-gate {
135*7c478bd9Sstevel@tonic-gate 	ASSERT(tlist_handle != NULL);
136*7c478bd9Sstevel@tonic-gate 	ASSERT(node != NULL);
137*7c478bd9Sstevel@tonic-gate 
138*7c478bd9Sstevel@tonic-gate 	mutex_enter(&tlist_handle->tl_mutex);
139*7c478bd9Sstevel@tonic-gate 
140*7c478bd9Sstevel@tonic-gate 	/* add's always go at the end of the list */
141*7c478bd9Sstevel@tonic-gate 	node->tln_next = NULL;
142*7c478bd9Sstevel@tonic-gate 
143*7c478bd9Sstevel@tonic-gate 	/* Set state that this node is currently on the tlist */
144*7c478bd9Sstevel@tonic-gate 	node->tln_on_list = B_TRUE;
145*7c478bd9Sstevel@tonic-gate 
146*7c478bd9Sstevel@tonic-gate 	/* enter in the expire time (in uS) */
147*7c478bd9Sstevel@tonic-gate 	if (tlist_handle->tl_timer_enabled == B_TRUE) {
148*7c478bd9Sstevel@tonic-gate 		node->tln_expire_time = gethrtime() +
149*7c478bd9Sstevel@tonic-gate 		    tlist_handle->tl_timer_info.tlt_timeout;
150*7c478bd9Sstevel@tonic-gate 	}
151*7c478bd9Sstevel@tonic-gate 
152*7c478bd9Sstevel@tonic-gate 	/* if there is nothing in the list */
153*7c478bd9Sstevel@tonic-gate 	if (tlist_handle->tl_tail == NULL) {
154*7c478bd9Sstevel@tonic-gate 		tlist_handle->tl_head = node;
155*7c478bd9Sstevel@tonic-gate 		tlist_handle->tl_tail = node;
156*7c478bd9Sstevel@tonic-gate 		node->tln_prev = NULL;
157*7c478bd9Sstevel@tonic-gate 
158*7c478bd9Sstevel@tonic-gate 		if ((tlist_handle->tl_timer_enabled == B_TRUE) &&
159*7c478bd9Sstevel@tonic-gate 		    (tlist_handle->tl_state == HCI1394_TLIST_TIMEOUT_OFF)) {
160*7c478bd9Sstevel@tonic-gate 			/* turn the timer on */
161*7c478bd9Sstevel@tonic-gate 			tlist_handle->tl_timeout_id = timeout(
162*7c478bd9Sstevel@tonic-gate 			    hci1394_tlist_callback, tlist_handle,
163*7c478bd9Sstevel@tonic-gate 			    t1394_tlist_nsectohz(
164*7c478bd9Sstevel@tonic-gate 			    tlist_handle->tl_timer_info.tlt_timer_resolution));
165*7c478bd9Sstevel@tonic-gate 			tlist_handle->tl_state = HCI1394_TLIST_TIMEOUT_ON;
166*7c478bd9Sstevel@tonic-gate 		}
167*7c478bd9Sstevel@tonic-gate 	} else {
168*7c478bd9Sstevel@tonic-gate 		/* put the node on the end of the list */
169*7c478bd9Sstevel@tonic-gate 		tlist_handle->tl_tail->tln_next = node;
170*7c478bd9Sstevel@tonic-gate 		node->tln_prev = tlist_handle->tl_tail;
171*7c478bd9Sstevel@tonic-gate 		tlist_handle->tl_tail = node;
172*7c478bd9Sstevel@tonic-gate 		/*
173*7c478bd9Sstevel@tonic-gate 		 * if timeouts are enabled,  we don't have to call
174*7c478bd9Sstevel@tonic-gate 		 * timeout() because the timer is already on.
175*7c478bd9Sstevel@tonic-gate 		 */
176*7c478bd9Sstevel@tonic-gate 	}
177*7c478bd9Sstevel@tonic-gate 
178*7c478bd9Sstevel@tonic-gate 	mutex_exit(&tlist_handle->tl_mutex);
179*7c478bd9Sstevel@tonic-gate }
180*7c478bd9Sstevel@tonic-gate 
181*7c478bd9Sstevel@tonic-gate 
182*7c478bd9Sstevel@tonic-gate /*
183*7c478bd9Sstevel@tonic-gate  * hci1394_tlist_delete()
184*7c478bd9Sstevel@tonic-gate  *    Remove the node from the list.  The node can be anywhere in the list. Make
185*7c478bd9Sstevel@tonic-gate  *    sure that the node is only removed once since different threads maybe
186*7c478bd9Sstevel@tonic-gate  *    trying to delete the same node at the same time.
187*7c478bd9Sstevel@tonic-gate  */
188*7c478bd9Sstevel@tonic-gate int
hci1394_tlist_delete(hci1394_tlist_handle_t tlist_handle,hci1394_tlist_node_t * node)189*7c478bd9Sstevel@tonic-gate hci1394_tlist_delete(hci1394_tlist_handle_t tlist_handle,
190*7c478bd9Sstevel@tonic-gate     hci1394_tlist_node_t *node)
191*7c478bd9Sstevel@tonic-gate {
192*7c478bd9Sstevel@tonic-gate 	ASSERT(tlist_handle != NULL);
193*7c478bd9Sstevel@tonic-gate 	ASSERT(node != NULL);
194*7c478bd9Sstevel@tonic-gate 
195*7c478bd9Sstevel@tonic-gate 	mutex_enter(&tlist_handle->tl_mutex);
196*7c478bd9Sstevel@tonic-gate 
197*7c478bd9Sstevel@tonic-gate 	/*
198*7c478bd9Sstevel@tonic-gate 	 * check for race condition.  Someone else may have already removed this
199*7c478bd9Sstevel@tonic-gate 	 * node from the list. hci1394_tlist_delete() supports two threads
200*7c478bd9Sstevel@tonic-gate 	 * trying to delete the node at the same time. The "losing" thread will
201*7c478bd9Sstevel@tonic-gate 	 * have DDI_FAILURE returned.
202*7c478bd9Sstevel@tonic-gate 	 */
203*7c478bd9Sstevel@tonic-gate 	if (node->tln_on_list == B_FALSE) {
204*7c478bd9Sstevel@tonic-gate 		mutex_exit(&tlist_handle->tl_mutex);
205*7c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
206*7c478bd9Sstevel@tonic-gate 	}
207*7c478bd9Sstevel@tonic-gate 
208*7c478bd9Sstevel@tonic-gate 	hci1394_tlist_remove(tlist_handle, node);
209*7c478bd9Sstevel@tonic-gate 	mutex_exit(&tlist_handle->tl_mutex);
210*7c478bd9Sstevel@tonic-gate 
211*7c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
212*7c478bd9Sstevel@tonic-gate }
213*7c478bd9Sstevel@tonic-gate 
214*7c478bd9Sstevel@tonic-gate 
215*7c478bd9Sstevel@tonic-gate /*
216*7c478bd9Sstevel@tonic-gate  * hci1394_tlist_get()
217*7c478bd9Sstevel@tonic-gate  *    get the node at the head of the linked list. This function also removes
218*7c478bd9Sstevel@tonic-gate  *    the node from the list.
219*7c478bd9Sstevel@tonic-gate  */
220*7c478bd9Sstevel@tonic-gate void
hci1394_tlist_get(hci1394_tlist_handle_t tlist_handle,hci1394_tlist_node_t ** node)221*7c478bd9Sstevel@tonic-gate hci1394_tlist_get(hci1394_tlist_handle_t tlist_handle,
222*7c478bd9Sstevel@tonic-gate     hci1394_tlist_node_t **node)
223*7c478bd9Sstevel@tonic-gate {
224*7c478bd9Sstevel@tonic-gate 	ASSERT(tlist_handle != NULL);
225*7c478bd9Sstevel@tonic-gate 	ASSERT(node != NULL);
226*7c478bd9Sstevel@tonic-gate 
227*7c478bd9Sstevel@tonic-gate 	mutex_enter(&tlist_handle->tl_mutex);
228*7c478bd9Sstevel@tonic-gate 
229*7c478bd9Sstevel@tonic-gate 	/* set the return parameter */
230*7c478bd9Sstevel@tonic-gate 	*node = tlist_handle->tl_head;
231*7c478bd9Sstevel@tonic-gate 
232*7c478bd9Sstevel@tonic-gate 	/* remove the node from the tlist */
233*7c478bd9Sstevel@tonic-gate 	if (*node != NULL) {
234*7c478bd9Sstevel@tonic-gate 		hci1394_tlist_remove(tlist_handle, *node);
235*7c478bd9Sstevel@tonic-gate 	}
236*7c478bd9Sstevel@tonic-gate 
237*7c478bd9Sstevel@tonic-gate 	mutex_exit(&tlist_handle->tl_mutex);
238*7c478bd9Sstevel@tonic-gate }
239*7c478bd9Sstevel@tonic-gate 
240*7c478bd9Sstevel@tonic-gate 
241*7c478bd9Sstevel@tonic-gate /*
242*7c478bd9Sstevel@tonic-gate  * hci1394_tlist_peek()
243*7c478bd9Sstevel@tonic-gate  *    get the node at the head of the linked list. This function does not
244*7c478bd9Sstevel@tonic-gate  *    remove the node from the list.
245*7c478bd9Sstevel@tonic-gate  */
246*7c478bd9Sstevel@tonic-gate void
hci1394_tlist_peek(hci1394_tlist_handle_t tlist_handle,hci1394_tlist_node_t ** node)247*7c478bd9Sstevel@tonic-gate hci1394_tlist_peek(hci1394_tlist_handle_t tlist_handle,
248*7c478bd9Sstevel@tonic-gate     hci1394_tlist_node_t **node)
249*7c478bd9Sstevel@tonic-gate {
250*7c478bd9Sstevel@tonic-gate 	ASSERT(tlist_handle != NULL);
251*7c478bd9Sstevel@tonic-gate 	ASSERT(node != NULL);
252*7c478bd9Sstevel@tonic-gate 
253*7c478bd9Sstevel@tonic-gate 	mutex_enter(&tlist_handle->tl_mutex);
254*7c478bd9Sstevel@tonic-gate 	*node = tlist_handle->tl_head;
255*7c478bd9Sstevel@tonic-gate 	mutex_exit(&tlist_handle->tl_mutex);
256*7c478bd9Sstevel@tonic-gate }
257*7c478bd9Sstevel@tonic-gate 
258*7c478bd9Sstevel@tonic-gate 
259*7c478bd9Sstevel@tonic-gate /*
260*7c478bd9Sstevel@tonic-gate  * hci1394_tlist_timeout_update()
261*7c478bd9Sstevel@tonic-gate  *    update the timeout to a different value. timeout is in uS.  The update
262*7c478bd9Sstevel@tonic-gate  *    does not happen immediately.  The new timeout will not take effect until
263*7c478bd9Sstevel@tonic-gate  *    the all of nodes currently present in the list are gone. It only makes
264*7c478bd9Sstevel@tonic-gate  *    sense to call this function when you have the timeout feature enabled.
265*7c478bd9Sstevel@tonic-gate  */
266*7c478bd9Sstevel@tonic-gate void
hci1394_tlist_timeout_update(hci1394_tlist_handle_t tlist_handle,hrtime_t timeout)267*7c478bd9Sstevel@tonic-gate hci1394_tlist_timeout_update(hci1394_tlist_handle_t tlist_handle,
268*7c478bd9Sstevel@tonic-gate     hrtime_t timeout)
269*7c478bd9Sstevel@tonic-gate {
270*7c478bd9Sstevel@tonic-gate 	ASSERT(tlist_handle != NULL);
271*7c478bd9Sstevel@tonic-gate 
272*7c478bd9Sstevel@tonic-gate 	/* set timeout to the new timeout */
273*7c478bd9Sstevel@tonic-gate 	tlist_handle->tl_timer_info.tlt_timeout = timeout;
274*7c478bd9Sstevel@tonic-gate }
275*7c478bd9Sstevel@tonic-gate 
276*7c478bd9Sstevel@tonic-gate 
277*7c478bd9Sstevel@tonic-gate /*
278*7c478bd9Sstevel@tonic-gate  * hci1394_tlist_timeout_cancel()
279*7c478bd9Sstevel@tonic-gate  *    cancel any scheduled timeouts.  This should be called after the list is
280*7c478bd9Sstevel@tonic-gate  *    empty and there is no chance for any other nodes to be placed on the list.
281*7c478bd9Sstevel@tonic-gate  *    This function is meant to be called during a suspend or detach.
282*7c478bd9Sstevel@tonic-gate  */
283*7c478bd9Sstevel@tonic-gate void
hci1394_tlist_timeout_cancel(hci1394_tlist_handle_t tlist_handle)284*7c478bd9Sstevel@tonic-gate hci1394_tlist_timeout_cancel(hci1394_tlist_handle_t tlist_handle)
285*7c478bd9Sstevel@tonic-gate {
286*7c478bd9Sstevel@tonic-gate 	ASSERT(tlist_handle != NULL);
287*7c478bd9Sstevel@tonic-gate 
288*7c478bd9Sstevel@tonic-gate 	/*
289*7c478bd9Sstevel@tonic-gate 	 * Cancel the timeout. Do NOT use the tlist mutex here. It could cause a
290*7c478bd9Sstevel@tonic-gate 	 * deadlock.
291*7c478bd9Sstevel@tonic-gate 	 */
292*7c478bd9Sstevel@tonic-gate 	if (tlist_handle->tl_state == HCI1394_TLIST_TIMEOUT_ON) {
293*7c478bd9Sstevel@tonic-gate 		(void) untimeout(tlist_handle->tl_timeout_id);
294*7c478bd9Sstevel@tonic-gate 		tlist_handle->tl_state = HCI1394_TLIST_TIMEOUT_OFF;
295*7c478bd9Sstevel@tonic-gate 	}
296*7c478bd9Sstevel@tonic-gate }
297*7c478bd9Sstevel@tonic-gate 
298*7c478bd9Sstevel@tonic-gate 
299*7c478bd9Sstevel@tonic-gate /*
300*7c478bd9Sstevel@tonic-gate  * hci1394_tlist_callback()
301*7c478bd9Sstevel@tonic-gate  *    The callback we use for the timeout() function. See if there are any nodes
302*7c478bd9Sstevel@tonic-gate  *    on the list which have timed out. If so, call the registered callback for
303*7c478bd9Sstevel@tonic-gate  *    each timed out node. We always start looking at the top of the list since
304*7c478bd9Sstevel@tonic-gate  *    the list is time sorted (oldest at the top).
305*7c478bd9Sstevel@tonic-gate  */
306*7c478bd9Sstevel@tonic-gate static void
hci1394_tlist_callback(void * tlist_handle)307*7c478bd9Sstevel@tonic-gate hci1394_tlist_callback(void *tlist_handle)
308*7c478bd9Sstevel@tonic-gate {
309*7c478bd9Sstevel@tonic-gate 	hci1394_tlist_t *list;
310*7c478bd9Sstevel@tonic-gate 	hci1394_tlist_node_t *node;
311*7c478bd9Sstevel@tonic-gate 	hrtime_t current_time;
312*7c478bd9Sstevel@tonic-gate 
313*7c478bd9Sstevel@tonic-gate 
314*7c478bd9Sstevel@tonic-gate 	ASSERT(tlist_handle != NULL);
315*7c478bd9Sstevel@tonic-gate 
316*7c478bd9Sstevel@tonic-gate 	list = (hci1394_tlist_t *)tlist_handle;
317*7c478bd9Sstevel@tonic-gate 
318*7c478bd9Sstevel@tonic-gate 	mutex_enter(&list->tl_mutex);
319*7c478bd9Sstevel@tonic-gate 
320*7c478bd9Sstevel@tonic-gate 	/*
321*7c478bd9Sstevel@tonic-gate 	 * if there is something on the list, check to see if the oldest has
322*7c478bd9Sstevel@tonic-gate 	 * expired.  If there is nothing on the list, there is no reason to
323*7c478bd9Sstevel@tonic-gate 	 * renew the timeout.
324*7c478bd9Sstevel@tonic-gate 	 */
325*7c478bd9Sstevel@tonic-gate 	node = list->tl_head;
326*7c478bd9Sstevel@tonic-gate 	current_time = gethrtime();
327*7c478bd9Sstevel@tonic-gate 	while (node != NULL) {
328*7c478bd9Sstevel@tonic-gate 		/*
329*7c478bd9Sstevel@tonic-gate 		 * if current time is greater than the time the command expires,
330*7c478bd9Sstevel@tonic-gate 		 * AND, the expire time has not rolled over, then the command
331*7c478bd9Sstevel@tonic-gate 		 * has timed out.
332*7c478bd9Sstevel@tonic-gate 		 */
333*7c478bd9Sstevel@tonic-gate 		if (((uint64_t)current_time >=
334*7c478bd9Sstevel@tonic-gate 		    (uint64_t)node->tln_expire_time) &&
335*7c478bd9Sstevel@tonic-gate 		    (((uint64_t)node->tln_expire_time -
336*7c478bd9Sstevel@tonic-gate 		    (uint64_t)list->tl_timer_info.tlt_timeout) <
337*7c478bd9Sstevel@tonic-gate 		    (uint64_t)node->tln_expire_time)) {
338*7c478bd9Sstevel@tonic-gate 			/* remove the node from the tlist */
339*7c478bd9Sstevel@tonic-gate 			hci1394_tlist_remove(list, node);
340*7c478bd9Sstevel@tonic-gate 
341*7c478bd9Sstevel@tonic-gate 			/*
342*7c478bd9Sstevel@tonic-gate 			 * Call the timeout callback. We unlock the the mutex
343*7c478bd9Sstevel@tonic-gate 			 * around the callback so that other transactions will
344*7c478bd9Sstevel@tonic-gate 			 * not be blocked while the callback is running. This
345*7c478bd9Sstevel@tonic-gate 			 * is OK to do here because we have already removed this
346*7c478bd9Sstevel@tonic-gate 			 * entry from our list. This code should not reference
347*7c478bd9Sstevel@tonic-gate 			 * "node" again after the callback! After the callback
348*7c478bd9Sstevel@tonic-gate 			 * returns, we need to resync node to the head of the
349*7c478bd9Sstevel@tonic-gate 			 * list since we released/acquired the list mutex around
350*7c478bd9Sstevel@tonic-gate 			 * the callback.
351*7c478bd9Sstevel@tonic-gate 			 */
352*7c478bd9Sstevel@tonic-gate 			mutex_exit(&list->tl_mutex);
353*7c478bd9Sstevel@tonic-gate 			list->tl_timer_info.tlt_callback(node,
354*7c478bd9Sstevel@tonic-gate 			    list->tl_timer_info.tlt_callback_arg);
355*7c478bd9Sstevel@tonic-gate 			mutex_enter(&list->tl_mutex);
356*7c478bd9Sstevel@tonic-gate 			node = list->tl_head;
357*7c478bd9Sstevel@tonic-gate 
358*7c478bd9Sstevel@tonic-gate 		/*
359*7c478bd9Sstevel@tonic-gate 		 * else, if current time is greater than the time the command
360*7c478bd9Sstevel@tonic-gate 		 * expires, AND, current_time is not about to rollover. (this
361*7c478bd9Sstevel@tonic-gate 		 * works since it is in the else and we periodically sample
362*7c478bd9Sstevel@tonic-gate 		 * well below the rollover time)
363*7c478bd9Sstevel@tonic-gate 		 */
364*7c478bd9Sstevel@tonic-gate 		} else if ((uint64_t)(current_time >=
365*7c478bd9Sstevel@tonic-gate 		    (uint64_t)node->tln_expire_time) &&
366*7c478bd9Sstevel@tonic-gate 		    (((uint64_t)current_time +
367*7c478bd9Sstevel@tonic-gate 		    (uint64_t)list->tl_timer_info.tlt_timeout) >
368*7c478bd9Sstevel@tonic-gate 		    (uint64_t)current_time)) {
369*7c478bd9Sstevel@tonic-gate 			/* remove the node from the tlist */
370*7c478bd9Sstevel@tonic-gate 			hci1394_tlist_remove(list, node);
371*7c478bd9Sstevel@tonic-gate 
372*7c478bd9Sstevel@tonic-gate 			/*
373*7c478bd9Sstevel@tonic-gate 			 * Call the timeout callback. We unlock the the mutex
374*7c478bd9Sstevel@tonic-gate 			 * around the callback so that other transactions will
375*7c478bd9Sstevel@tonic-gate 			 * not be blocked while the callback is running. This
376*7c478bd9Sstevel@tonic-gate 			 * is OK to do here because we have already removed this
377*7c478bd9Sstevel@tonic-gate 			 * entry from our list. This code should not reference
378*7c478bd9Sstevel@tonic-gate 			 * "node" again after the callback! After the callback
379*7c478bd9Sstevel@tonic-gate 			 * returns, we need to resync node to the head of the
380*7c478bd9Sstevel@tonic-gate 			 * list since we released/acquired the list mutex around
381*7c478bd9Sstevel@tonic-gate 			 * the callback.
382*7c478bd9Sstevel@tonic-gate 			 */
383*7c478bd9Sstevel@tonic-gate 			mutex_exit(&list->tl_mutex);
384*7c478bd9Sstevel@tonic-gate 			list->tl_timer_info.tlt_callback(node,
385*7c478bd9Sstevel@tonic-gate 			    list->tl_timer_info.tlt_callback_arg);
386*7c478bd9Sstevel@tonic-gate 			mutex_enter(&list->tl_mutex);
387*7c478bd9Sstevel@tonic-gate 			node = list->tl_head;
388*7c478bd9Sstevel@tonic-gate 
389*7c478bd9Sstevel@tonic-gate 		} else {
390*7c478bd9Sstevel@tonic-gate 			/*
391*7c478bd9Sstevel@tonic-gate 			 * this command has not timed out.
392*7c478bd9Sstevel@tonic-gate 			 * Since this list is time sorted, we are
393*7c478bd9Sstevel@tonic-gate 			 * done looking for nodes that have expired
394*7c478bd9Sstevel@tonic-gate 			 */
395*7c478bd9Sstevel@tonic-gate 			break;
396*7c478bd9Sstevel@tonic-gate 		}
397*7c478bd9Sstevel@tonic-gate 	}
398*7c478bd9Sstevel@tonic-gate 
399*7c478bd9Sstevel@tonic-gate 	/*
400*7c478bd9Sstevel@tonic-gate 	 * if there are nodes still on the pending list, kick
401*7c478bd9Sstevel@tonic-gate 	 * off the timer again.
402*7c478bd9Sstevel@tonic-gate 	 */
403*7c478bd9Sstevel@tonic-gate 	if (node != NULL) {
404*7c478bd9Sstevel@tonic-gate 		list->tl_timeout_id = timeout(hci1394_tlist_callback, list,
405*7c478bd9Sstevel@tonic-gate 		    t1394_tlist_nsectohz(
406*7c478bd9Sstevel@tonic-gate 		    list->tl_timer_info.tlt_timer_resolution));
407*7c478bd9Sstevel@tonic-gate 		list->tl_state = HCI1394_TLIST_TIMEOUT_ON;
408*7c478bd9Sstevel@tonic-gate 	} else {
409*7c478bd9Sstevel@tonic-gate 		list->tl_state = HCI1394_TLIST_TIMEOUT_OFF;
410*7c478bd9Sstevel@tonic-gate 	}
411*7c478bd9Sstevel@tonic-gate 
412*7c478bd9Sstevel@tonic-gate 	mutex_exit(&list->tl_mutex);
413*7c478bd9Sstevel@tonic-gate }
414*7c478bd9Sstevel@tonic-gate 
415*7c478bd9Sstevel@tonic-gate 
416*7c478bd9Sstevel@tonic-gate /*
417*7c478bd9Sstevel@tonic-gate  * hci1394_tlist_remove()
418*7c478bd9Sstevel@tonic-gate  *    This is an internal function which removes the given node from the list.
419*7c478bd9Sstevel@tonic-gate  *    The list MUST be locked before calling this function.
420*7c478bd9Sstevel@tonic-gate  */
421*7c478bd9Sstevel@tonic-gate static void
hci1394_tlist_remove(hci1394_tlist_t * list,hci1394_tlist_node_t * node)422*7c478bd9Sstevel@tonic-gate hci1394_tlist_remove(hci1394_tlist_t *list, hci1394_tlist_node_t *node)
423*7c478bd9Sstevel@tonic-gate {
424*7c478bd9Sstevel@tonic-gate 	ASSERT(list != NULL);
425*7c478bd9Sstevel@tonic-gate 	ASSERT(node != NULL);
426*7c478bd9Sstevel@tonic-gate 	ASSERT(node->tln_on_list == B_TRUE);
427*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&list->tl_mutex));
428*7c478bd9Sstevel@tonic-gate 
429*7c478bd9Sstevel@tonic-gate 	/* if this is the only node on the list */
430*7c478bd9Sstevel@tonic-gate 	if ((list->tl_head == node) &&
431*7c478bd9Sstevel@tonic-gate 	    (list->tl_tail == node)) {
432*7c478bd9Sstevel@tonic-gate 		list->tl_head = NULL;
433*7c478bd9Sstevel@tonic-gate 		list->tl_tail = NULL;
434*7c478bd9Sstevel@tonic-gate 
435*7c478bd9Sstevel@tonic-gate 	/* if the node is at the head of the list */
436*7c478bd9Sstevel@tonic-gate 	} else if (list->tl_head == node) {
437*7c478bd9Sstevel@tonic-gate 		list->tl_head = node->tln_next;
438*7c478bd9Sstevel@tonic-gate 		node->tln_next->tln_prev = NULL;
439*7c478bd9Sstevel@tonic-gate 
440*7c478bd9Sstevel@tonic-gate 	/* if the node is at the tail of the list */
441*7c478bd9Sstevel@tonic-gate 	} else if (list->tl_tail == node) {
442*7c478bd9Sstevel@tonic-gate 		list->tl_tail = node->tln_prev;
443*7c478bd9Sstevel@tonic-gate 		node->tln_prev->tln_next = NULL;
444*7c478bd9Sstevel@tonic-gate 
445*7c478bd9Sstevel@tonic-gate 	/* if the node is in the middle of the list */
446*7c478bd9Sstevel@tonic-gate 	} else {
447*7c478bd9Sstevel@tonic-gate 		node->tln_prev->tln_next = node->tln_next;
448*7c478bd9Sstevel@tonic-gate 		node->tln_next->tln_prev = node->tln_prev;
449*7c478bd9Sstevel@tonic-gate 	}
450*7c478bd9Sstevel@tonic-gate 
451*7c478bd9Sstevel@tonic-gate 	/* Set state that this node has been removed from the list */
452*7c478bd9Sstevel@tonic-gate 	node->tln_on_list = B_FALSE;
453*7c478bd9Sstevel@tonic-gate 
454*7c478bd9Sstevel@tonic-gate 	/* cleanup the node's link pointers */
455*7c478bd9Sstevel@tonic-gate 	node->tln_prev = NULL;
456*7c478bd9Sstevel@tonic-gate 	node->tln_next = NULL;
457*7c478bd9Sstevel@tonic-gate }
458*7c478bd9Sstevel@tonic-gate 
459*7c478bd9Sstevel@tonic-gate 
460*7c478bd9Sstevel@tonic-gate /*
461*7c478bd9Sstevel@tonic-gate  * t1394_tlist_nsectohz()
462*7c478bd9Sstevel@tonic-gate  *     Convert nS to hz.  This allows us to call timeout() but keep our time
463*7c478bd9Sstevel@tonic-gate  *     reference in nS.
464*7c478bd9Sstevel@tonic-gate  */
465*7c478bd9Sstevel@tonic-gate #define	HCI1394_TLIST_nS_TO_uS(nS)  ((clock_t)(nS / 1000))
t1394_tlist_nsectohz(hrtime_t nS)466*7c478bd9Sstevel@tonic-gate static clock_t t1394_tlist_nsectohz(hrtime_t  nS)
467*7c478bd9Sstevel@tonic-gate {
468*7c478bd9Sstevel@tonic-gate 	return (drv_usectohz(HCI1394_TLIST_nS_TO_uS(nS)));
469*7c478bd9Sstevel@tonic-gate }
470