4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
28 * smq.c: to provide a message queue system for scadm functions (used in the
29 * firmware download context where BP messages, received from the service
30 * processor, are stored in the message queue)
32 * these routines come from the libxposix library
35 #include <sys/types.h>
42 #define SMQ_VALID_SMQ 0x0000003b
43 #define SMQ_VALID_SMQ_MASK 0x000000FF
47 smq_init(smq_t
*smq
, smq_msg_t
*msgbuffer
, int depth
)
49 /* allocate local semaphore initialized to 0 */
50 if (xsem_init(&smq
->smq_msgAvail
, 0, 0) != 0)
53 smq
->smq_control
= SMQ_VALID_SMQ
;
54 smq
->smq_msgBuffer
= msgbuffer
;
55 smq
->smq_head
= msgbuffer
;
56 smq
->smq_tail
= msgbuffer
;
58 smq
->smq_depth
= depth
;
65 smq_destroy(smq_t
*smq
)
67 if ((smq
->smq_control
& SMQ_VALID_SMQ_MASK
) != SMQ_VALID_SMQ
)
71 (void) xsem_destroy(&smq
->smq_msgAvail
);
78 smq_receive(smq_t
*smq
, smq_msg_t
*msg
)
80 if ((smq
->smq_control
& SMQ_VALID_SMQ_MASK
) != SMQ_VALID_SMQ
)
83 /* Wait for message */
84 (void) xsem_wait(&smq
->smq_msgAvail
);
86 if (smq
->smq_count
== 0)
89 /* Copy messaged into queue */
90 *msg
= *smq
->smq_head
;
93 if ((unsigned long)smq
->smq_head
> ((unsigned long)smq
->smq_msgBuffer
+
94 (unsigned long)(smq
->smq_depth
* sizeof (smq_msg_t
)))) {
95 smq
->smq_head
= smq
->smq_msgBuffer
;
104 smq_send(smq_t
*smq
, smq_msg_t
*msg
)
106 if ((smq
->smq_control
& SMQ_VALID_SMQ_MASK
) != SMQ_VALID_SMQ
)
107 return (SMQ_INVALID
);
109 if (smq
->smq_count
== smq
->smq_depth
)
112 /* Copy messaged into queue */
113 *smq
->smq_tail
= *msg
;
116 if ((unsigned long)smq
->smq_tail
> ((unsigned long)smq
->smq_msgBuffer
+
117 (unsigned long)(smq
->smq_depth
* sizeof (smq_msg_t
)))) {
118 smq
->smq_tail
= smq
->smq_msgBuffer
;
122 (void) xsem_post(&smq
->smq_msgAvail
);
129 smq_pendingmsgs(smq_t
*smq
, int *num
)
131 if ((smq
->smq_control
& SMQ_VALID_SMQ_MASK
) != SMQ_VALID_SMQ
)
132 return (SMQ_INVALID
);
134 *num
= smq
->smq_count
;
141 smq_depth(smq_t
*smq
, int *depth
)
143 if ((smq
->smq_control
& SMQ_VALID_SMQ_MASK
) != SMQ_VALID_SMQ
)
144 return (SMQ_INVALID
);
146 *depth
= smq
->smq_depth
;
153 smq_xreceive(smq_t
*smq
, timestruc_t
*timeout
, smq_msg_t
*msg
)
158 if ((smq
->smq_control
& SMQ_VALID_SMQ_MASK
) != SMQ_VALID_SMQ
)
159 return (SMQ_INVALID
);
161 /* Wait for message */
162 if ((Status
= xsem_xwait(&smq
->smq_msgAvail
, 1, timeout
)) == XSEM_ETIME
)
168 if (smq
->smq_count
== 0)
171 /* Copy messaged into queue */
172 *msg
= *smq
->smq_head
;
175 if ((unsigned long)smq
->smq_head
> ((unsigned long)smq
->smq_msgBuffer
+
176 (unsigned long)(smq
->smq_depth
* sizeof (smq_msg_t
)))) {
177 smq
->smq_head
= smq
->smq_msgBuffer
;