xref: /illumos-gate/usr/src/lib/udapl/udapl_tavor/common/dapl_cno_wait.c (revision 9e39c5ba00a55fa05777cc94b148296af305e135)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright (c) 2002-2003, Network Appliance, Inc. All rights reserved.
24  */
25 
26 /*
27  * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
28  * Use is subject to license terms.
29  */
30 
31 /*
32  *
33  * MODULE: dapl_cno_wait.c
34  *
35  * PURPOSE: Wait for a consumer notification event
36  * Description: Interfaces in this file are completely described in
37  *		the DAPL 1.1 API, Chapter 6, section 3.2.3
38  *
39  * $Id: dapl_cno_wait.c,v 1.6 2003/06/16 17:53:32 sjs2 Exp $
40  */
41 
42 #include "dapl.h"
43 #include "dapl_adapter_util.h"
44 #include "dapl_ring_buffer_util.h"
45 
46 /*
47  * dapl_cno_wait
48  *
49  * DAPL Requirements Version xxx, 6.3.2.3
50  *
51  * Wait for a consumer notification event
52  *
53  * Input:
54  *	cno_handle
55  *	timeout
56  *	evd_handle
57  *
58  * Output:
59  *	evd_handle
60  *
61  * Returns:
62  *	DAT_SUCCESS
63  *	DAT_INVALID_HANDLE
64  *	DAT_QUEUE_EMPTY
65  *	DAT_INVALID_PARAMETER
66  */
67 DAT_RETURN
dapl_cno_wait(IN DAT_CNO_HANDLE cno_handle,IN DAT_TIMEOUT timeout,OUT DAT_EVD_HANDLE * evd_handle)68 dapl_cno_wait(
69 	IN	DAT_CNO_HANDLE		cno_handle,	/* cno_handle */
70 	IN	DAT_TIMEOUT		timeout,	/* agent */
71 	OUT	DAT_EVD_HANDLE		*evd_handle)	/* evd_handle */
72 {
73 	DAPL_CNO 	*cno_ptr;
74 	DAPL_EVD	*evd_ptr, *head_evd_ptr;
75 	DAT_RETURN 	dat_status;
76 	int		nevents;
77 
78 	if (DAPL_BAD_HANDLE(cno_handle, DAPL_MAGIC_CNO)) {
79 		dat_status = DAT_INVALID_HANDLE | DAT_INVALID_HANDLE_CNO;
80 		goto bail;
81 	}
82 
83 	dat_status = DAT_SUCCESS;
84 	cno_ptr = (DAPL_CNO *) cno_handle;
85 
86 	if (cno_ptr->cno_state == DAPL_CNO_STATE_DEAD) {
87 		dat_status = DAT_ERROR(DAT_INVALID_STATE,
88 		    DAT_INVALID_STATE_CNO_DEAD);
89 		goto bail;
90 	}
91 
92 	dapl_os_lock(&cno_ptr->header.lock);
93 	if (dapl_llist_is_empty(&cno_ptr->evd_list_head)) {
94 		dapl_os_unlock(&cno_ptr->header.lock);
95 		return (DAT_ERROR(DAT_QUEUE_EMPTY, 0));
96 	}
97 
98 	/* scan evd list */
99 	evd_ptr = (DAPL_EVD *)
100 	    dapl_llist_next_entry(&cno_ptr->evd_list_head, NULL);
101 
102 	for (;;) {
103 		/*
104 		 * Check the evd ring buffer for events, if nothing is found
105 		 * peek into the CQ to see if there are events
106 		 */
107 		if (dapls_rbuf_count(&evd_ptr->pending_event_queue) > 0) {
108 			break;
109 		}
110 
111 		nevents = 0;
112 		dapls_ib_cq_peek(evd_ptr, &nevents);
113 		if (nevents > 0) {
114 			break;
115 		}
116 
117 		evd_ptr = (DAPL_EVD *)
118 		    dapl_llist_next_entry(&cno_ptr->evd_list_head,
119 		    &evd_ptr->cno_list_entry);
120 		if (evd_ptr == NULL) {
121 			break;
122 		}
123 	}
124 
125 	/* shift list by one to simulate round-robin */
126 	head_evd_ptr = (DAPL_EVD *)
127 	    dapl_llist_remove_head(&cno_ptr->evd_list_head);
128 	dapl_os_assert(head_evd_ptr != NULL);
129 	dapl_llist_add_tail(&cno_ptr->evd_list_head,
130 	    &head_evd_ptr->cno_list_entry, head_evd_ptr);
131 
132 	if (evd_ptr != NULL) {
133 		*evd_handle = evd_ptr;
134 		dapl_os_unlock(&cno_ptr->header.lock);
135 		return (DAT_SUCCESS);
136 	}
137 
138 	cno_ptr->cno_waiters++;
139 	dapl_os_unlock(&cno_ptr->header.lock);
140 
141 	dat_status = dapls_ib_cno_wait(cno_ptr, timeout, &evd_ptr);
142 
143 	dapl_os_lock(&cno_ptr->header.lock);
144 	cno_ptr->cno_waiters--;
145 
146 	/* verify that evd_ptr is still in the list */
147 	head_evd_ptr = dapl_llist_next_entry(&cno_ptr->evd_list_head, NULL);
148 	for (;;) {
149 		if (head_evd_ptr == NULL || head_evd_ptr == evd_ptr) {
150 			break;
151 		}
152 		head_evd_ptr = (DAPL_EVD *)
153 		    dapl_llist_next_entry(&cno_ptr->evd_list_head,
154 		    &head_evd_ptr->cno_list_entry);
155 	}
156 
157 	if (cno_ptr->cno_state == DAPL_CNO_STATE_DEAD) {
158 		dat_status = DAT_ERROR(DAT_INVALID_STATE,
159 		    DAT_INVALID_STATE_CNO_DEAD);
160 	} else if (dat_status == DAT_SUCCESS) {
161 		/*
162 		 * After the first triggering, this will be a valid handle.
163 		 * If we're racing with wakeups of other CNO waiters,
164 		 * that's ok.
165 		 */
166 		if (head_evd_ptr == evd_ptr && evd_ptr != NULL) {
167 			*evd_handle = evd_ptr;
168 		} else {
169 			dat_status = DAT_ERROR(DAT_QUEUE_EMPTY, 0);
170 		}
171 	}
172 	/*
173 	 * The only other reason we could have made it out of
174 	 * the loop is a timeout.
175 	 */
176 	dapl_os_unlock(&cno_ptr->header.lock);
177 bail:
178 	return (dat_status);
179 }
180