18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * linux/drivers/acorn/scsi/msgqueue.c 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Copyright (C) 1997-1998 Russell King 68c2ecf20Sopenharmony_ci * 78c2ecf20Sopenharmony_ci * message queue handling 88c2ecf20Sopenharmony_ci */ 98c2ecf20Sopenharmony_ci#include <linux/module.h> 108c2ecf20Sopenharmony_ci#include <linux/kernel.h> 118c2ecf20Sopenharmony_ci#include <linux/stddef.h> 128c2ecf20Sopenharmony_ci#include <linux/init.h> 138c2ecf20Sopenharmony_ci 148c2ecf20Sopenharmony_ci#include "msgqueue.h" 158c2ecf20Sopenharmony_ci 168c2ecf20Sopenharmony_ci/* 178c2ecf20Sopenharmony_ci * Function: struct msgqueue_entry *mqe_alloc(MsgQueue_t *msgq) 188c2ecf20Sopenharmony_ci * Purpose : Allocate a message queue entry 198c2ecf20Sopenharmony_ci * Params : msgq - message queue to claim entry for 208c2ecf20Sopenharmony_ci * Returns : message queue entry or NULL. 218c2ecf20Sopenharmony_ci */ 228c2ecf20Sopenharmony_cistatic struct msgqueue_entry *mqe_alloc(MsgQueue_t *msgq) 238c2ecf20Sopenharmony_ci{ 248c2ecf20Sopenharmony_ci struct msgqueue_entry *mq; 258c2ecf20Sopenharmony_ci 268c2ecf20Sopenharmony_ci if ((mq = msgq->free) != NULL) 278c2ecf20Sopenharmony_ci msgq->free = mq->next; 288c2ecf20Sopenharmony_ci 298c2ecf20Sopenharmony_ci return mq; 308c2ecf20Sopenharmony_ci} 318c2ecf20Sopenharmony_ci 328c2ecf20Sopenharmony_ci/* 338c2ecf20Sopenharmony_ci * Function: void mqe_free(MsgQueue_t *msgq, struct msgqueue_entry *mq) 348c2ecf20Sopenharmony_ci * Purpose : free a message queue entry 358c2ecf20Sopenharmony_ci * Params : msgq - message queue to free entry from 368c2ecf20Sopenharmony_ci * mq - message queue entry to free 378c2ecf20Sopenharmony_ci */ 388c2ecf20Sopenharmony_cistatic void mqe_free(MsgQueue_t *msgq, struct msgqueue_entry *mq) 398c2ecf20Sopenharmony_ci{ 408c2ecf20Sopenharmony_ci if (mq) { 418c2ecf20Sopenharmony_ci mq->next = msgq->free; 428c2ecf20Sopenharmony_ci msgq->free = mq; 438c2ecf20Sopenharmony_ci } 448c2ecf20Sopenharmony_ci} 458c2ecf20Sopenharmony_ci 468c2ecf20Sopenharmony_ci/* 478c2ecf20Sopenharmony_ci * Function: void msgqueue_initialise(MsgQueue_t *msgq) 488c2ecf20Sopenharmony_ci * Purpose : initialise a message queue 498c2ecf20Sopenharmony_ci * Params : msgq - queue to initialise 508c2ecf20Sopenharmony_ci */ 518c2ecf20Sopenharmony_civoid msgqueue_initialise(MsgQueue_t *msgq) 528c2ecf20Sopenharmony_ci{ 538c2ecf20Sopenharmony_ci int i; 548c2ecf20Sopenharmony_ci 558c2ecf20Sopenharmony_ci msgq->qe = NULL; 568c2ecf20Sopenharmony_ci msgq->free = &msgq->entries[0]; 578c2ecf20Sopenharmony_ci 588c2ecf20Sopenharmony_ci for (i = 0; i < NR_MESSAGES; i++) 598c2ecf20Sopenharmony_ci msgq->entries[i].next = &msgq->entries[i + 1]; 608c2ecf20Sopenharmony_ci 618c2ecf20Sopenharmony_ci msgq->entries[NR_MESSAGES - 1].next = NULL; 628c2ecf20Sopenharmony_ci} 638c2ecf20Sopenharmony_ci 648c2ecf20Sopenharmony_ci 658c2ecf20Sopenharmony_ci/* 668c2ecf20Sopenharmony_ci * Function: void msgqueue_free(MsgQueue_t *msgq) 678c2ecf20Sopenharmony_ci * Purpose : free a queue 688c2ecf20Sopenharmony_ci * Params : msgq - queue to free 698c2ecf20Sopenharmony_ci */ 708c2ecf20Sopenharmony_civoid msgqueue_free(MsgQueue_t *msgq) 718c2ecf20Sopenharmony_ci{ 728c2ecf20Sopenharmony_ci} 738c2ecf20Sopenharmony_ci 748c2ecf20Sopenharmony_ci/* 758c2ecf20Sopenharmony_ci * Function: int msgqueue_msglength(MsgQueue_t *msgq) 768c2ecf20Sopenharmony_ci * Purpose : calculate the total length of all messages on the message queue 778c2ecf20Sopenharmony_ci * Params : msgq - queue to examine 788c2ecf20Sopenharmony_ci * Returns : number of bytes of messages in queue 798c2ecf20Sopenharmony_ci */ 808c2ecf20Sopenharmony_ciint msgqueue_msglength(MsgQueue_t *msgq) 818c2ecf20Sopenharmony_ci{ 828c2ecf20Sopenharmony_ci struct msgqueue_entry *mq = msgq->qe; 838c2ecf20Sopenharmony_ci int length = 0; 848c2ecf20Sopenharmony_ci 858c2ecf20Sopenharmony_ci for (mq = msgq->qe; mq; mq = mq->next) 868c2ecf20Sopenharmony_ci length += mq->msg.length; 878c2ecf20Sopenharmony_ci 888c2ecf20Sopenharmony_ci return length; 898c2ecf20Sopenharmony_ci} 908c2ecf20Sopenharmony_ci 918c2ecf20Sopenharmony_ci/* 928c2ecf20Sopenharmony_ci * Function: struct message *msgqueue_getmsg(MsgQueue_t *msgq, int msgno) 938c2ecf20Sopenharmony_ci * Purpose : return a message 948c2ecf20Sopenharmony_ci * Params : msgq - queue to obtain message from 958c2ecf20Sopenharmony_ci * : msgno - message number 968c2ecf20Sopenharmony_ci * Returns : pointer to message string, or NULL 978c2ecf20Sopenharmony_ci */ 988c2ecf20Sopenharmony_cistruct message *msgqueue_getmsg(MsgQueue_t *msgq, int msgno) 998c2ecf20Sopenharmony_ci{ 1008c2ecf20Sopenharmony_ci struct msgqueue_entry *mq; 1018c2ecf20Sopenharmony_ci 1028c2ecf20Sopenharmony_ci for (mq = msgq->qe; mq && msgno; mq = mq->next, msgno--); 1038c2ecf20Sopenharmony_ci 1048c2ecf20Sopenharmony_ci return mq ? &mq->msg : NULL; 1058c2ecf20Sopenharmony_ci} 1068c2ecf20Sopenharmony_ci 1078c2ecf20Sopenharmony_ci/* 1088c2ecf20Sopenharmony_ci * Function: int msgqueue_addmsg(MsgQueue_t *msgq, int length, ...) 1098c2ecf20Sopenharmony_ci * Purpose : add a message onto a message queue 1108c2ecf20Sopenharmony_ci * Params : msgq - queue to add message on 1118c2ecf20Sopenharmony_ci * length - length of message 1128c2ecf20Sopenharmony_ci * ... - message bytes 1138c2ecf20Sopenharmony_ci * Returns : != 0 if successful 1148c2ecf20Sopenharmony_ci */ 1158c2ecf20Sopenharmony_ciint msgqueue_addmsg(MsgQueue_t *msgq, int length, ...) 1168c2ecf20Sopenharmony_ci{ 1178c2ecf20Sopenharmony_ci struct msgqueue_entry *mq = mqe_alloc(msgq); 1188c2ecf20Sopenharmony_ci va_list ap; 1198c2ecf20Sopenharmony_ci 1208c2ecf20Sopenharmony_ci if (mq) { 1218c2ecf20Sopenharmony_ci struct msgqueue_entry **mqp; 1228c2ecf20Sopenharmony_ci int i; 1238c2ecf20Sopenharmony_ci 1248c2ecf20Sopenharmony_ci va_start(ap, length); 1258c2ecf20Sopenharmony_ci for (i = 0; i < length; i++) 1268c2ecf20Sopenharmony_ci mq->msg.msg[i] = va_arg(ap, unsigned int); 1278c2ecf20Sopenharmony_ci va_end(ap); 1288c2ecf20Sopenharmony_ci 1298c2ecf20Sopenharmony_ci mq->msg.length = length; 1308c2ecf20Sopenharmony_ci mq->msg.fifo = 0; 1318c2ecf20Sopenharmony_ci mq->next = NULL; 1328c2ecf20Sopenharmony_ci 1338c2ecf20Sopenharmony_ci mqp = &msgq->qe; 1348c2ecf20Sopenharmony_ci while (*mqp) 1358c2ecf20Sopenharmony_ci mqp = &(*mqp)->next; 1368c2ecf20Sopenharmony_ci 1378c2ecf20Sopenharmony_ci *mqp = mq; 1388c2ecf20Sopenharmony_ci } 1398c2ecf20Sopenharmony_ci 1408c2ecf20Sopenharmony_ci return mq != NULL; 1418c2ecf20Sopenharmony_ci} 1428c2ecf20Sopenharmony_ci 1438c2ecf20Sopenharmony_ci/* 1448c2ecf20Sopenharmony_ci * Function: void msgqueue_flush(MsgQueue_t *msgq) 1458c2ecf20Sopenharmony_ci * Purpose : flush all messages from message queue 1468c2ecf20Sopenharmony_ci * Params : msgq - queue to flush 1478c2ecf20Sopenharmony_ci */ 1488c2ecf20Sopenharmony_civoid msgqueue_flush(MsgQueue_t *msgq) 1498c2ecf20Sopenharmony_ci{ 1508c2ecf20Sopenharmony_ci struct msgqueue_entry *mq, *mqnext; 1518c2ecf20Sopenharmony_ci 1528c2ecf20Sopenharmony_ci for (mq = msgq->qe; mq; mq = mqnext) { 1538c2ecf20Sopenharmony_ci mqnext = mq->next; 1548c2ecf20Sopenharmony_ci mqe_free(msgq, mq); 1558c2ecf20Sopenharmony_ci } 1568c2ecf20Sopenharmony_ci msgq->qe = NULL; 1578c2ecf20Sopenharmony_ci} 1588c2ecf20Sopenharmony_ci 1598c2ecf20Sopenharmony_ciEXPORT_SYMBOL(msgqueue_initialise); 1608c2ecf20Sopenharmony_ciEXPORT_SYMBOL(msgqueue_free); 1618c2ecf20Sopenharmony_ciEXPORT_SYMBOL(msgqueue_msglength); 1628c2ecf20Sopenharmony_ciEXPORT_SYMBOL(msgqueue_getmsg); 1638c2ecf20Sopenharmony_ciEXPORT_SYMBOL(msgqueue_addmsg); 1648c2ecf20Sopenharmony_ciEXPORT_SYMBOL(msgqueue_flush); 1658c2ecf20Sopenharmony_ci 1668c2ecf20Sopenharmony_ciMODULE_AUTHOR("Russell King"); 1678c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("SCSI message queue handling"); 1688c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL"); 169