#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: rf_aselect.c,v 1.31 2022/03/20 19:26:27 andvar Exp $");
#include <dev/raidframe/raidframevar.h>
#include "rf_archs.h"
#include "rf_raid.h"
#include "rf_dag.h"
#include "rf_dagutils.h"
#include "rf_dagfuncs.h"
#include "rf_general.h"
#include "rf_desc.h"
#include "rf_map.h"
static void InitHdrNode(RF_DagHeader_t **, RF_Raid_t *, RF_RaidAccessDesc_t *);
int rf_SelectAlgorithm(RF_RaidAccessDesc_t *, RF_RaidAccessFlags_t);
static void
InitHdrNode(RF_DagHeader_t **hdr, RF_Raid_t *raidPtr, RF_RaidAccessDesc_t *desc)
{
*hdr = rf_AllocDAGHeader(raidPtr);
rf_MakeAllocList((*hdr)->allocList);
(*hdr)->status = rf_enable;
(*hdr)->numSuccedents = 0;
(*hdr)->nodes = NULL;
(*hdr)->raidPtr = raidPtr;
(*hdr)->next = NULL;
(*hdr)->desc = desc;
}
int
rf_SelectAlgorithm(RF_RaidAccessDesc_t *desc, RF_RaidAccessFlags_t flags)
{
RF_AccessStripeMapHeader_t *asm_h = desc->asmap;
RF_IoType_t type = desc->type;
RF_Raid_t *raidPtr = desc->raidPtr;
void *bp = desc->bp;
RF_AccessStripeMap_t *asmap = asm_h->stripeMap;
RF_AccessStripeMap_t *asm_p;
RF_DagHeader_t *dag_h = NULL, *tempdag_h, *lastdag_h;
RF_DagList_t *dagList, *dagListend;
int i, j, k;
RF_FuncList_t *stripeFuncsList, *stripeFuncs, *stripeFuncsEnd, *temp;
RF_AccessStripeMap_t *asm_up, *asm_bp;
RF_AccessStripeMapHeader_t *endASMList;
RF_ASMHeaderListElem_t *asmhle, *tmpasmhle;
RF_VoidFunctionPointerListElem_t *vfple, *tmpvfple;
RF_FailedStripe_t *failed_stripes_list, *failed_stripes_list_end;
RF_FailedStripe_t *tmpfailed_stripe, *failed_stripe = NULL;
RF_ASMHeaderListElem_t *failed_stripes_asmh_u_end = NULL;
RF_ASMHeaderListElem_t *failed_stripes_asmh_b_end = NULL;
RF_VoidFunctionPointerListElem_t *failed_stripes_vfple_end = NULL;
RF_VoidFunctionPointerListElem_t *failed_stripes_bvfple_end = NULL;
RF_VoidFuncPtr uFunc;
RF_VoidFuncPtr bFunc;
int numStripesBailed = 0, cantCreateDAGs = RF_FALSE;
int numStripeUnitsBailed = 0;
int stripeNum, stripeUnitNum, numBlockDags = 0;
RF_StripeNum_t numStripeUnits;
RF_SectorNum_t numBlocks;
RF_RaidAddr_t address;
int length;
RF_PhysDiskAddr_t *physPtr;
void *buffer;
lastdag_h = NULL;
stripeFuncsList = NULL;
stripeFuncsEnd = NULL;
failed_stripes_list = NULL;
failed_stripes_list_end = NULL;
desc->numStripes = 0;
for (i = 0, asm_p = asmap; asm_p; asm_p = asm_p->next, i++) {
desc->numStripes++;
stripeFuncs = rf_AllocFuncList(raidPtr);
if (stripeFuncsEnd == NULL) {
stripeFuncsList = stripeFuncs;
} else {
stripeFuncsEnd->next = stripeFuncs;
}
stripeFuncsEnd = stripeFuncs;
(raidPtr->Layout.map->SelectionFunc) (raidPtr, type, asm_p, &(stripeFuncs->fp));
if (stripeFuncs->fp == NULL) {
failed_stripe = rf_AllocFailedStripeStruct(raidPtr);
if (failed_stripes_list == NULL) {
failed_stripes_list = failed_stripe;
failed_stripes_list_end = failed_stripe;
} else {
failed_stripes_list_end->next = failed_stripe;
failed_stripes_list_end = failed_stripe;
}
numStripeUnits = asm_p->numStripeUnitsAccessed;
failed_stripes_asmh_u_end = NULL;
failed_stripes_vfple_end = NULL;
for (j = 0, physPtr = asm_p->physInfo; physPtr; physPtr = physPtr->next, j++) {
address = physPtr->raidAddress;
length = physPtr->numSector;
buffer = physPtr->bufPtr;
asmhle = rf_AllocASMHeaderListElem(raidPtr);
if (failed_stripe->asmh_u == NULL) {
failed_stripe->asmh_u = asmhle;
failed_stripes_asmh_u_end = asmhle;
} else {
failed_stripes_asmh_u_end->next = asmhle;
failed_stripes_asmh_u_end = asmhle;
}
asmhle->asmh = rf_MapAccess(raidPtr, address, length, buffer, RF_DONT_REMAP);
asm_up = asmhle->asmh->stripeMap;
vfple = rf_AllocVFPListElem(raidPtr);
if (failed_stripe->vfple == NULL) {
failed_stripe->vfple = vfple;
failed_stripes_vfple_end = vfple;
} else {
failed_stripes_vfple_end->next = vfple;
failed_stripes_vfple_end = vfple;
}
(raidPtr->Layout.map->SelectionFunc) (raidPtr, type, asm_up, &(vfple->fn));
if (vfple->fn == NULL) {
numBlocks = physPtr->numSector;
numBlockDags += numBlocks;
for (k = 0; k < numBlocks; k++) {
address = physPtr->raidAddress + k;
length = 1;
buffer = (char *)physPtr->bufPtr + (k * (1 << raidPtr->logBytesPerSector));
asmhle = rf_AllocASMHeaderListElem(raidPtr);
if (failed_stripe->asmh_b == NULL) {
failed_stripe->asmh_b = asmhle;
failed_stripes_asmh_b_end = asmhle;
} else {
failed_stripes_asmh_b_end->next = asmhle;
failed_stripes_asmh_b_end = asmhle;
}
asmhle->asmh = rf_MapAccess(raidPtr, address, length, buffer, RF_DONT_REMAP);
asm_bp = asmhle->asmh->stripeMap;
vfple = rf_AllocVFPListElem(raidPtr);
if (failed_stripe->bvfple == NULL) {
failed_stripe->bvfple = vfple;
failed_stripes_bvfple_end = vfple;
} else {
failed_stripes_bvfple_end->next = vfple;
failed_stripes_bvfple_end = vfple;
}
(raidPtr->Layout.map->SelectionFunc) (raidPtr, type, asm_bp, &(vfple->fn));
if (vfple->fn == NULL)
cantCreateDAGs = RF_TRUE;
}
numStripeUnitsBailed++;
}
}
RF_ASSERT(j == numStripeUnits);
numStripesBailed++;
}
}
if (cantCreateDAGs) {
if (numStripesBailed > 0) {
stripeNum = 0;
stripeFuncs = stripeFuncsList;
failed_stripe = failed_stripes_list;
for (i = 0, asm_p = asmap; asm_p; asm_p = asm_p->next, i++) {
if (stripeFuncs->fp == NULL) {
asmhle = failed_stripe->asmh_u;
while (asmhle) {
tmpasmhle= asmhle;
asmhle = tmpasmhle->next;
rf_FreeAccessStripeMap(raidPtr, tmpasmhle->asmh);
rf_FreeASMHeaderListElem(raidPtr, tmpasmhle);
}
asmhle = failed_stripe->asmh_b;
while (asmhle) {
tmpasmhle= asmhle;
asmhle = tmpasmhle->next;
rf_FreeAccessStripeMap(raidPtr, tmpasmhle->asmh);
rf_FreeASMHeaderListElem(raidPtr, tmpasmhle);
}
vfple = failed_stripe->vfple;
while (vfple) {
tmpvfple = vfple;
vfple = tmpvfple->next;
rf_FreeVFPListElem(raidPtr, tmpvfple);
}
vfple = failed_stripe->bvfple;
while (vfple) {
tmpvfple = vfple;
vfple = tmpvfple->next;
rf_FreeVFPListElem(raidPtr, tmpvfple);
}
stripeNum++;
tmpfailed_stripe = failed_stripe;
failed_stripe = failed_stripe->next;
rf_FreeFailedStripeStruct(raidPtr, tmpfailed_stripe);
}
stripeFuncs = stripeFuncs->next;
}
RF_ASSERT(stripeNum == numStripesBailed);
}
while (stripeFuncsList != NULL) {
temp = stripeFuncsList;
stripeFuncsList = stripeFuncsList->next;
rf_FreeFuncList(raidPtr, temp);
}
desc->numStripes = 0;
return (1);
} else {
stripeNum = 0;
stripeUnitNum = 0;
dagListend = NULL;
stripeFuncs = stripeFuncsList;
failed_stripe = failed_stripes_list;
for (i = 0, asm_p = asmap; asm_p; asm_p = asm_p->next, i++) {
dag_h = NULL;
dagList = rf_AllocDAGList(raidPtr);
if (dagListend == NULL) {
desc->dagList = dagList;
} else {
dagListend->next = dagList;
}
dagListend = dagList;
dagList->desc = desc;
if (stripeFuncs->fp == NULL) {
asmhle = failed_stripe->asmh_u;
vfple = failed_stripe->vfple;
tmpasmhle = failed_stripe->asmh_b;
tmpvfple = failed_stripe->bvfple;
for (j = 0, physPtr = asm_p->physInfo; physPtr; physPtr = physPtr->next, j++) {
uFunc = vfple->fn;
if (uFunc == NULL) {
for (k = 0; k < physPtr->numSector; k++) {
InitHdrNode(&tempdag_h, raidPtr, desc);
dagList->numDags++;
if (dag_h == NULL) {
dag_h = tempdag_h;
} else {
lastdag_h->next = tempdag_h;
}
lastdag_h = tempdag_h;
bFunc = tmpvfple->fn;
RF_ASSERT(bFunc);
asm_bp = tmpasmhle->asmh->stripeMap;
(*bFunc) (raidPtr, asm_bp, tempdag_h, bp, flags, tempdag_h->allocList);
tmpasmhle = tmpasmhle->next;
tmpvfple = tmpvfple->next;
}
stripeUnitNum++;
} else {
InitHdrNode(&tempdag_h, raidPtr, desc);
dagList->numDags++;
if (dag_h == NULL) {
dag_h = tempdag_h;
} else {
lastdag_h->next = tempdag_h;
}
lastdag_h = tempdag_h;
asm_up = asmhle->asmh->stripeMap;
(*uFunc) (raidPtr, asm_up, tempdag_h, bp, flags, tempdag_h->allocList);
}
asmhle = asmhle->next;
vfple = vfple->next;
}
RF_ASSERT(j == asm_p->numStripeUnitsAccessed);
stripeNum++;
failed_stripe = failed_stripe->next;
} else {
InitHdrNode(&tempdag_h, raidPtr, desc);
dagList->numDags++;
dag_h = tempdag_h;
lastdag_h = tempdag_h;
(stripeFuncs->fp) (raidPtr, asm_p, tempdag_h, bp, flags, tempdag_h->allocList);
}
dagList->dags = dag_h;
stripeFuncs = stripeFuncs->next;
}
RF_ASSERT(i == desc->numStripes);
if ((numStripesBailed > 0) || (numStripeUnitsBailed > 0)) {
stripeNum = 0;
stripeUnitNum = 0;
stripeFuncs = stripeFuncsList;
failed_stripe = failed_stripes_list;
dagList = desc->dagList;
for (i = 0, asm_p = asmap; asm_p; asm_p = asm_p->next, i++) {
dag_h = dagList->dags;
if (dag_h->asmList) {
endASMList = dag_h->asmList;
while (endASMList->next)
endASMList = endASMList->next;
} else
endASMList = NULL;
if (stripeFuncs->fp == NULL) {
numStripeUnits = asm_p->numStripeUnitsAccessed;
asmhle = failed_stripe->asmh_u;
vfple = failed_stripe->vfple;
tmpasmhle = failed_stripe->asmh_b;
for (j = 0, physPtr = asm_p->physInfo; physPtr; physPtr = physPtr->next, j++) {
if (vfple->fn == NULL) {
numBlocks = physPtr->numSector;
for (k = 0; k < numBlocks; k++) {
if (dag_h->asmList == NULL) {
dag_h->asmList = tmpasmhle->asmh;
endASMList = dag_h->asmList;
} else {
endASMList->next = tmpasmhle->asmh;
endASMList = endASMList->next;
}
tmpasmhle = tmpasmhle->next;
}
stripeUnitNum++;
}
if (dag_h->asmList == NULL) {
dag_h->asmList = asmhle->asmh;
endASMList = dag_h->asmList;
} else {
endASMList->next = asmhle->asmh;
endASMList = endASMList->next;
}
asmhle = asmhle->next;
vfple = vfple->next;
}
stripeNum++;
failed_stripe = failed_stripe->next;
}
dagList = dagList->next;
stripeFuncs = stripeFuncs->next;
}
RF_ASSERT(stripeNum == numStripesBailed);
RF_ASSERT(stripeUnitNum == numStripeUnitsBailed);
failed_stripe = failed_stripes_list;
while (failed_stripe) {
asmhle = failed_stripe->asmh_u;
while (asmhle) {
tmpasmhle= asmhle;
asmhle = tmpasmhle->next;
rf_FreeASMHeaderListElem(raidPtr, tmpasmhle);
}
asmhle = failed_stripe->asmh_b;
while (asmhle) {
tmpasmhle= asmhle;
asmhle = tmpasmhle->next;
rf_FreeASMHeaderListElem(raidPtr, tmpasmhle);
}
vfple = failed_stripe->vfple;
while (vfple) {
tmpvfple = vfple;
vfple = tmpvfple->next;
rf_FreeVFPListElem(raidPtr, tmpvfple);
}
vfple = failed_stripe->bvfple;
while (vfple) {
tmpvfple = vfple;
vfple = tmpvfple->next;
rf_FreeVFPListElem(raidPtr, tmpvfple);
}
tmpfailed_stripe = failed_stripe;
failed_stripe = tmpfailed_stripe->next;
rf_FreeFailedStripeStruct(raidPtr, tmpfailed_stripe);
}
}
while (stripeFuncsList != NULL) {
temp = stripeFuncsList;
stripeFuncsList = stripeFuncsList->next;
rf_FreeFuncList(raidPtr, temp);
}
return (0);
}
}