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