#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: rf_fifo.c,v 1.17 2021/07/23 20:18:24 oster Exp $");
#include <dev/raidframe/raidframevar.h>
#include "rf_alloclist.h"
#include "rf_stripelocks.h"
#include "rf_layout.h"
#include "rf_diskqueue.h"
#include "rf_fifo.h"
#include "rf_debugMem.h"
#include "rf_general.h"
#include "rf_options.h"
#include "rf_raid.h"
void *
rf_FifoCreate(RF_SectorCount_t sectPerDisk, RF_AllocListElem_t *clList,
RF_ShutdownList_t **listp)
{
RF_FifoHeader_t *q;
q = RF_MallocAndAdd(sizeof(*q), clList);
q->hq_count = q->lq_count = 0;
return ((void *) q);
}
void
rf_FifoEnqueue(void *q_in, RF_DiskQueueData_t *elem, int priority)
{
RF_FifoHeader_t *q = (RF_FifoHeader_t *) q_in;
RF_ASSERT(priority == RF_IO_NORMAL_PRIORITY || priority == RF_IO_LOW_PRIORITY);
elem->next = NULL;
if (priority == RF_IO_NORMAL_PRIORITY) {
if (!q->hq_tail) {
RF_ASSERT(q->hq_count == 0 && q->hq_head == NULL);
q->hq_head = q->hq_tail = elem;
} else {
RF_ASSERT(q->hq_count != 0 && q->hq_head != NULL);
q->hq_tail->next = elem;
q->hq_tail = elem;
}
q->hq_count++;
} else {
RF_ASSERT(elem->next == NULL);
#if RF_DEBUG_QUEUE
if (rf_fifoDebug) {
printf("raid%d: fifo: ENQ lopri\n",
elem->raidPtr->raidid);
}
#endif
if (!q->lq_tail) {
RF_ASSERT(q->lq_count == 0 && q->lq_head == NULL);
q->lq_head = q->lq_tail = elem;
} else {
RF_ASSERT(q->lq_count != 0 && q->lq_head != NULL);
q->lq_tail->next = elem;
q->lq_tail = elem;
}
q->lq_count++;
}
if ((q->hq_count + q->lq_count) != elem->queue->queueLength) {
printf("Queue lengths differ!: %d %d %d\n",
q->hq_count, q->lq_count, (int) elem->queue->queueLength);
printf("%d %d %d\n",
(int) elem->queue->numOutstanding,
(int) elem->queue->maxOutstanding,
(int) elem->queue->col);
}
RF_ASSERT((q->hq_count + q->lq_count) == elem->queue->queueLength);
}
RF_DiskQueueData_t *
rf_FifoDequeue(void *q_in)
{
RF_FifoHeader_t *q = (RF_FifoHeader_t *) q_in;
RF_DiskQueueData_t *nd;
RF_ASSERT(q);
if (q->hq_head) {
RF_ASSERT(q->hq_count != 0 && q->hq_tail != NULL);
nd = q->hq_head;
q->hq_head = q->hq_head->next;
if (!q->hq_head)
q->hq_tail = NULL;
nd->next = NULL;
q->hq_count--;
} else
if (q->lq_head) {
RF_ASSERT(q->lq_count != 0 && q->lq_tail != NULL);
nd = q->lq_head;
q->lq_head = q->lq_head->next;
if (!q->lq_head)
q->lq_tail = NULL;
nd->next = NULL;
q->lq_count--;
#if RF_DEBUG_QUEUE
if (rf_fifoDebug) {
printf("raid%d: fifo: DEQ lopri %lx\n",
nd->raidPtr->raidid, (long) nd);
}
#endif
} else {
RF_ASSERT(q->hq_count == 0 && q->lq_count == 0 && q->hq_tail == NULL && q->lq_tail == NULL);
nd = NULL;
}
return (nd);
}
int
rf_FifoPromote(void *q_in, RF_StripeNum_t parityStripeID,
RF_ReconUnitNum_t which_ru)
{
RF_FifoHeader_t *q = (RF_FifoHeader_t *) q_in;
RF_DiskQueueData_t *lp = q->lq_head, *pt = NULL;
int retval = 0;
while (lp) {
if (lp->parityStripeID == parityStripeID && lp->which_ru == which_ru) {
if (pt)
pt->next = lp->next;
else
q->lq_head = lp->next;
if (!q->lq_head)
q->lq_tail = NULL;
else
if (lp == q->lq_tail)
q->lq_tail = pt;
lp->next = NULL;
q->lq_count--;
if (q->hq_tail) {
q->hq_tail->next = lp;
q->hq_tail = lp;
}
else {
q->hq_head = q->hq_tail = lp;
}
q->hq_count++;
lp = (pt) ? pt->next : q->lq_head;
retval++;
} else {
pt = lp;
lp = lp->next;
}
}
RF_ASSERT(retval == 0 || retval == 1);
return (retval);
}