1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * linux/drivers/acorn/scsi/msgqueue.c
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Copyright (C) 1997-1998 Russell King
6*4882a593Smuzhiyun *
7*4882a593Smuzhiyun * message queue handling
8*4882a593Smuzhiyun */
9*4882a593Smuzhiyun #include <linux/module.h>
10*4882a593Smuzhiyun #include <linux/kernel.h>
11*4882a593Smuzhiyun #include <linux/stddef.h>
12*4882a593Smuzhiyun #include <linux/init.h>
13*4882a593Smuzhiyun
14*4882a593Smuzhiyun #include "msgqueue.h"
15*4882a593Smuzhiyun
16*4882a593Smuzhiyun /*
17*4882a593Smuzhiyun * Function: struct msgqueue_entry *mqe_alloc(MsgQueue_t *msgq)
18*4882a593Smuzhiyun * Purpose : Allocate a message queue entry
19*4882a593Smuzhiyun * Params : msgq - message queue to claim entry for
20*4882a593Smuzhiyun * Returns : message queue entry or NULL.
21*4882a593Smuzhiyun */
mqe_alloc(MsgQueue_t * msgq)22*4882a593Smuzhiyun static struct msgqueue_entry *mqe_alloc(MsgQueue_t *msgq)
23*4882a593Smuzhiyun {
24*4882a593Smuzhiyun struct msgqueue_entry *mq;
25*4882a593Smuzhiyun
26*4882a593Smuzhiyun if ((mq = msgq->free) != NULL)
27*4882a593Smuzhiyun msgq->free = mq->next;
28*4882a593Smuzhiyun
29*4882a593Smuzhiyun return mq;
30*4882a593Smuzhiyun }
31*4882a593Smuzhiyun
32*4882a593Smuzhiyun /*
33*4882a593Smuzhiyun * Function: void mqe_free(MsgQueue_t *msgq, struct msgqueue_entry *mq)
34*4882a593Smuzhiyun * Purpose : free a message queue entry
35*4882a593Smuzhiyun * Params : msgq - message queue to free entry from
36*4882a593Smuzhiyun * mq - message queue entry to free
37*4882a593Smuzhiyun */
mqe_free(MsgQueue_t * msgq,struct msgqueue_entry * mq)38*4882a593Smuzhiyun static void mqe_free(MsgQueue_t *msgq, struct msgqueue_entry *mq)
39*4882a593Smuzhiyun {
40*4882a593Smuzhiyun if (mq) {
41*4882a593Smuzhiyun mq->next = msgq->free;
42*4882a593Smuzhiyun msgq->free = mq;
43*4882a593Smuzhiyun }
44*4882a593Smuzhiyun }
45*4882a593Smuzhiyun
46*4882a593Smuzhiyun /*
47*4882a593Smuzhiyun * Function: void msgqueue_initialise(MsgQueue_t *msgq)
48*4882a593Smuzhiyun * Purpose : initialise a message queue
49*4882a593Smuzhiyun * Params : msgq - queue to initialise
50*4882a593Smuzhiyun */
msgqueue_initialise(MsgQueue_t * msgq)51*4882a593Smuzhiyun void msgqueue_initialise(MsgQueue_t *msgq)
52*4882a593Smuzhiyun {
53*4882a593Smuzhiyun int i;
54*4882a593Smuzhiyun
55*4882a593Smuzhiyun msgq->qe = NULL;
56*4882a593Smuzhiyun msgq->free = &msgq->entries[0];
57*4882a593Smuzhiyun
58*4882a593Smuzhiyun for (i = 0; i < NR_MESSAGES; i++)
59*4882a593Smuzhiyun msgq->entries[i].next = &msgq->entries[i + 1];
60*4882a593Smuzhiyun
61*4882a593Smuzhiyun msgq->entries[NR_MESSAGES - 1].next = NULL;
62*4882a593Smuzhiyun }
63*4882a593Smuzhiyun
64*4882a593Smuzhiyun
65*4882a593Smuzhiyun /*
66*4882a593Smuzhiyun * Function: void msgqueue_free(MsgQueue_t *msgq)
67*4882a593Smuzhiyun * Purpose : free a queue
68*4882a593Smuzhiyun * Params : msgq - queue to free
69*4882a593Smuzhiyun */
msgqueue_free(MsgQueue_t * msgq)70*4882a593Smuzhiyun void msgqueue_free(MsgQueue_t *msgq)
71*4882a593Smuzhiyun {
72*4882a593Smuzhiyun }
73*4882a593Smuzhiyun
74*4882a593Smuzhiyun /*
75*4882a593Smuzhiyun * Function: int msgqueue_msglength(MsgQueue_t *msgq)
76*4882a593Smuzhiyun * Purpose : calculate the total length of all messages on the message queue
77*4882a593Smuzhiyun * Params : msgq - queue to examine
78*4882a593Smuzhiyun * Returns : number of bytes of messages in queue
79*4882a593Smuzhiyun */
msgqueue_msglength(MsgQueue_t * msgq)80*4882a593Smuzhiyun int msgqueue_msglength(MsgQueue_t *msgq)
81*4882a593Smuzhiyun {
82*4882a593Smuzhiyun struct msgqueue_entry *mq = msgq->qe;
83*4882a593Smuzhiyun int length = 0;
84*4882a593Smuzhiyun
85*4882a593Smuzhiyun for (mq = msgq->qe; mq; mq = mq->next)
86*4882a593Smuzhiyun length += mq->msg.length;
87*4882a593Smuzhiyun
88*4882a593Smuzhiyun return length;
89*4882a593Smuzhiyun }
90*4882a593Smuzhiyun
91*4882a593Smuzhiyun /*
92*4882a593Smuzhiyun * Function: struct message *msgqueue_getmsg(MsgQueue_t *msgq, int msgno)
93*4882a593Smuzhiyun * Purpose : return a message
94*4882a593Smuzhiyun * Params : msgq - queue to obtain message from
95*4882a593Smuzhiyun * : msgno - message number
96*4882a593Smuzhiyun * Returns : pointer to message string, or NULL
97*4882a593Smuzhiyun */
msgqueue_getmsg(MsgQueue_t * msgq,int msgno)98*4882a593Smuzhiyun struct message *msgqueue_getmsg(MsgQueue_t *msgq, int msgno)
99*4882a593Smuzhiyun {
100*4882a593Smuzhiyun struct msgqueue_entry *mq;
101*4882a593Smuzhiyun
102*4882a593Smuzhiyun for (mq = msgq->qe; mq && msgno; mq = mq->next, msgno--);
103*4882a593Smuzhiyun
104*4882a593Smuzhiyun return mq ? &mq->msg : NULL;
105*4882a593Smuzhiyun }
106*4882a593Smuzhiyun
107*4882a593Smuzhiyun /*
108*4882a593Smuzhiyun * Function: int msgqueue_addmsg(MsgQueue_t *msgq, int length, ...)
109*4882a593Smuzhiyun * Purpose : add a message onto a message queue
110*4882a593Smuzhiyun * Params : msgq - queue to add message on
111*4882a593Smuzhiyun * length - length of message
112*4882a593Smuzhiyun * ... - message bytes
113*4882a593Smuzhiyun * Returns : != 0 if successful
114*4882a593Smuzhiyun */
msgqueue_addmsg(MsgQueue_t * msgq,int length,...)115*4882a593Smuzhiyun int msgqueue_addmsg(MsgQueue_t *msgq, int length, ...)
116*4882a593Smuzhiyun {
117*4882a593Smuzhiyun struct msgqueue_entry *mq = mqe_alloc(msgq);
118*4882a593Smuzhiyun va_list ap;
119*4882a593Smuzhiyun
120*4882a593Smuzhiyun if (mq) {
121*4882a593Smuzhiyun struct msgqueue_entry **mqp;
122*4882a593Smuzhiyun int i;
123*4882a593Smuzhiyun
124*4882a593Smuzhiyun va_start(ap, length);
125*4882a593Smuzhiyun for (i = 0; i < length; i++)
126*4882a593Smuzhiyun mq->msg.msg[i] = va_arg(ap, unsigned int);
127*4882a593Smuzhiyun va_end(ap);
128*4882a593Smuzhiyun
129*4882a593Smuzhiyun mq->msg.length = length;
130*4882a593Smuzhiyun mq->msg.fifo = 0;
131*4882a593Smuzhiyun mq->next = NULL;
132*4882a593Smuzhiyun
133*4882a593Smuzhiyun mqp = &msgq->qe;
134*4882a593Smuzhiyun while (*mqp)
135*4882a593Smuzhiyun mqp = &(*mqp)->next;
136*4882a593Smuzhiyun
137*4882a593Smuzhiyun *mqp = mq;
138*4882a593Smuzhiyun }
139*4882a593Smuzhiyun
140*4882a593Smuzhiyun return mq != NULL;
141*4882a593Smuzhiyun }
142*4882a593Smuzhiyun
143*4882a593Smuzhiyun /*
144*4882a593Smuzhiyun * Function: void msgqueue_flush(MsgQueue_t *msgq)
145*4882a593Smuzhiyun * Purpose : flush all messages from message queue
146*4882a593Smuzhiyun * Params : msgq - queue to flush
147*4882a593Smuzhiyun */
msgqueue_flush(MsgQueue_t * msgq)148*4882a593Smuzhiyun void msgqueue_flush(MsgQueue_t *msgq)
149*4882a593Smuzhiyun {
150*4882a593Smuzhiyun struct msgqueue_entry *mq, *mqnext;
151*4882a593Smuzhiyun
152*4882a593Smuzhiyun for (mq = msgq->qe; mq; mq = mqnext) {
153*4882a593Smuzhiyun mqnext = mq->next;
154*4882a593Smuzhiyun mqe_free(msgq, mq);
155*4882a593Smuzhiyun }
156*4882a593Smuzhiyun msgq->qe = NULL;
157*4882a593Smuzhiyun }
158*4882a593Smuzhiyun
159*4882a593Smuzhiyun EXPORT_SYMBOL(msgqueue_initialise);
160*4882a593Smuzhiyun EXPORT_SYMBOL(msgqueue_free);
161*4882a593Smuzhiyun EXPORT_SYMBOL(msgqueue_msglength);
162*4882a593Smuzhiyun EXPORT_SYMBOL(msgqueue_getmsg);
163*4882a593Smuzhiyun EXPORT_SYMBOL(msgqueue_addmsg);
164*4882a593Smuzhiyun EXPORT_SYMBOL(msgqueue_flush);
165*4882a593Smuzhiyun
166*4882a593Smuzhiyun MODULE_AUTHOR("Russell King");
167*4882a593Smuzhiyun MODULE_DESCRIPTION("SCSI message queue handling");
168*4882a593Smuzhiyun MODULE_LICENSE("GPL");
169