Home | History | Annotate | Line # | Download | only in raidframe
rf_aselect.c revision 1.12
      1 /*	$NetBSD: rf_aselect.c,v 1.12 2004/02/27 02:55:18 oster Exp $	*/
      2 /*
      3  * Copyright (c) 1995 Carnegie-Mellon University.
      4  * All rights reserved.
      5  *
      6  * Author: Mark Holland, William V. Courtright II
      7  *
      8  * Permission to use, copy, modify and distribute this software and
      9  * its documentation is hereby granted, provided that both the copyright
     10  * notice and this permission notice appear in all copies of the
     11  * software, derivative works or modified versions, and any portions
     12  * thereof, and that both notices appear in supporting documentation.
     13  *
     14  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
     15  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
     16  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
     17  *
     18  * Carnegie Mellon requests users of this software to return to
     19  *
     20  *  Software Distribution Coordinator  or  Software.Distribution (at) CS.CMU.EDU
     21  *  School of Computer Science
     22  *  Carnegie Mellon University
     23  *  Pittsburgh PA 15213-3890
     24  *
     25  * any improvements or extensions that they make and grant Carnegie the
     26  * rights to redistribute these changes.
     27  */
     28 
     29 /*****************************************************************************
     30  *
     31  * aselect.c -- algorithm selection code
     32  *
     33  *****************************************************************************/
     34 
     35 #include <sys/cdefs.h>
     36 __KERNEL_RCSID(0, "$NetBSD: rf_aselect.c,v 1.12 2004/02/27 02:55:18 oster Exp $");
     37 
     38 #include <dev/raidframe/raidframevar.h>
     39 
     40 #include "rf_archs.h"
     41 #include "rf_raid.h"
     42 #include "rf_dag.h"
     43 #include "rf_dagutils.h"
     44 #include "rf_dagfuncs.h"
     45 #include "rf_general.h"
     46 #include "rf_desc.h"
     47 #include "rf_map.h"
     48 
     49 #if defined(__NetBSD__) && defined(_KERNEL)
     50 /* the function below is not used... so don't define it! */
     51 #else
     52 static void TransferDagMemory(RF_DagHeader_t *, RF_DagHeader_t *);
     53 #endif
     54 
     55 static int InitHdrNode(RF_DagHeader_t **, RF_Raid_t *);
     56 int     rf_SelectAlgorithm(RF_RaidAccessDesc_t *, RF_RaidAccessFlags_t);
     57 
     58 
     59 /******************************************************************************
     60  *
     61  * Create and Initialiaze a dag header and termination node
     62  *
     63  *****************************************************************************/
     64 static int
     65 InitHdrNode(RF_DagHeader_t **hdr, RF_Raid_t *raidPtr)
     66 {
     67 	/* create and initialize dag hdr */
     68 	*hdr = rf_AllocDAGHeader();
     69 	rf_MakeAllocList((*hdr)->allocList);
     70 	if ((*hdr)->allocList == NULL) {
     71 		rf_FreeDAGHeader(*hdr);
     72 		return (ENOMEM);
     73 	}
     74 	(*hdr)->status = rf_enable;
     75 	(*hdr)->numSuccedents = 0;
     76 	(*hdr)->raidPtr = raidPtr;
     77 	(*hdr)->next = NULL;
     78 	return (0);
     79 }
     80 
     81 /******************************************************************************
     82  *
     83  * Create a DAG to do a read or write operation.
     84  *
     85  * create a list of dagLists, one list per parity stripe.
     86  * return the lists in the desc->dagList (which is a list of lists).
     87  *
     88  * Normally, each list contains one dag for the entire stripe.  In some
     89  * tricky cases, we break this into multiple dags, either one per stripe
     90  * unit or one per block (sector).  When this occurs, these dags are returned
     91  * as a linked list (dagList) which is executed sequentially (to preserve
     92  * atomic parity updates in the stripe).
     93  *
     94  * dags which operate on independent parity goups (stripes) are returned in
     95  * independent dagLists (distinct elements in desc->dagArray) and may be
     96  * executed concurrently.
     97  *
     98  * Finally, if the SelectionFunc fails to create a dag for a block, we punt
     99  * and return 1.
    100  *
    101  * The above process is performed in two phases:
    102  *   1) create an array(s) of creation functions (eg stripeFuncs)
    103  *   2) create dags and concatenate/merge to form the final dag.
    104  *
    105  * Because dag's are basic blocks (single entry, single exit, unconditional
    106  * control flow, we can add the following optimizations (future work):
    107  *   first-pass optimizer to allow max concurrency (need all data dependencies)
    108  *   second-pass optimizer to eliminate common subexpressions (need true
    109  *                         data dependencies)
    110  *   third-pass optimizer to eliminate dead code (need true data dependencies)
    111  *****************************************************************************/
    112 
    113 #define MAXNSTRIPES 50
    114 
    115 int
    116 rf_SelectAlgorithm(RF_RaidAccessDesc_t *desc, RF_RaidAccessFlags_t flags)
    117 {
    118 	RF_AccessStripeMapHeader_t *asm_h = desc->asmap;
    119 	RF_IoType_t type = desc->type;
    120 	RF_Raid_t *raidPtr = desc->raidPtr;
    121 	void   *bp = desc->bp;
    122 
    123 	RF_AccessStripeMap_t *asmap = asm_h->stripeMap;
    124 	RF_AccessStripeMap_t *asm_p;
    125 	RF_DagHeader_t *dag_h = NULL, *tempdag_h, *lastdag_h;
    126 	RF_DagList_t *dagList, *dagListend;
    127 	int     i, j, k;
    128 	RF_VoidFuncPtr *stripeFuncs, normalStripeFuncs[MAXNSTRIPES];
    129 	RF_AccessStripeMap_t *asm_up, *asm_bp;
    130 	RF_AccessStripeMapHeader_t ***asmh_u, *endASMList;
    131 	RF_AccessStripeMapHeader_t ***asmh_b;
    132 	RF_VoidFuncPtr **stripeUnitFuncs, uFunc;
    133 	RF_VoidFuncPtr **blockFuncs, bFunc;
    134 	int     numStripesBailed = 0, cantCreateDAGs = RF_FALSE;
    135 	int     numStripeUnitsBailed = 0;
    136 	int     stripeNum, numUnitDags = 0, stripeUnitNum, numBlockDags = 0;
    137 	RF_StripeNum_t numStripeUnits;
    138 	RF_SectorNum_t numBlocks;
    139 	RF_RaidAddr_t address;
    140 	int     length;
    141 	RF_PhysDiskAddr_t *physPtr;
    142 	caddr_t buffer;
    143 
    144 	lastdag_h = NULL;
    145 	asmh_u = asmh_b = NULL;
    146 	stripeUnitFuncs = NULL;
    147 	blockFuncs = NULL;
    148 
    149 	/* get an array of dag-function creation pointers, try to avoid
    150 	 * calling malloc */
    151 	if (asm_h->numStripes <= MAXNSTRIPES)
    152 		stripeFuncs = normalStripeFuncs;
    153 	else
    154 		RF_Malloc(stripeFuncs, asm_h->numStripes * sizeof(RF_VoidFuncPtr), (RF_VoidFuncPtr *));
    155 
    156 	/* walk through the asm list once collecting information */
    157 	/* attempt to find a single creation function for each stripe */
    158 	desc->numStripes = 0;
    159 	for (i = 0, asm_p = asmap; asm_p; asm_p = asm_p->next, i++) {
    160 		desc->numStripes++;
    161 		(raidPtr->Layout.map->SelectionFunc) (raidPtr, type, asm_p, &stripeFuncs[i]);
    162 		/* check to see if we found a creation func for this stripe */
    163 		if (stripeFuncs[i] == (RF_VoidFuncPtr) NULL) {
    164 			/* could not find creation function for entire stripe
    165 			 * so, let's see if we can find one for each stripe
    166 			 * unit in the stripe */
    167 
    168 			if (numStripesBailed == 0) {
    169 				/* one stripe map header for each stripe we
    170 				 * bail on */
    171 				RF_Malloc(asmh_u, sizeof(RF_AccessStripeMapHeader_t **) * asm_h->numStripes, (RF_AccessStripeMapHeader_t ***));
    172 				/* create an array of ptrs to arrays of
    173 				 * stripeFuncs */
    174 				RF_Malloc(stripeUnitFuncs, asm_h->numStripes * sizeof(RF_VoidFuncPtr), (RF_VoidFuncPtr **));
    175 			}
    176 			/* create an array of creation funcs (called
    177 			 * stripeFuncs) for this stripe */
    178 			numStripeUnits = asm_p->numStripeUnitsAccessed;
    179 			RF_Malloc(stripeUnitFuncs[numStripesBailed], numStripeUnits * sizeof(RF_VoidFuncPtr), (RF_VoidFuncPtr *));
    180 			RF_Malloc(asmh_u[numStripesBailed], numStripeUnits * sizeof(RF_AccessStripeMapHeader_t *), (RF_AccessStripeMapHeader_t **));
    181 
    182 			/* lookup array of stripeUnitFuncs for this stripe */
    183 			for (j = 0, physPtr = asm_p->physInfo; physPtr; physPtr = physPtr->next, j++) {
    184 				/* remap for series of single stripe-unit
    185 				 * accesses */
    186 				address = physPtr->raidAddress;
    187 				length = physPtr->numSector;
    188 				buffer = physPtr->bufPtr;
    189 
    190 				asmh_u[numStripesBailed][j] = rf_MapAccess(raidPtr, address, length, buffer, RF_DONT_REMAP);
    191 				asm_up = asmh_u[numStripesBailed][j]->stripeMap;
    192 
    193 				/* get the creation func for this stripe unit */
    194 				(raidPtr->Layout.map->SelectionFunc) (raidPtr, type, asm_up, &(stripeUnitFuncs[numStripesBailed][j]));
    195 
    196 				/* check to see if we found a creation func
    197 				 * for this stripe unit */
    198 				if (stripeUnitFuncs[numStripesBailed][j] == (RF_VoidFuncPtr) NULL) {
    199 					/* could not find creation function
    200 					 * for stripe unit so, let's see if we
    201 					 * can find one for each block in the
    202 					 * stripe unit */
    203 					if (numStripeUnitsBailed == 0) {
    204 						/* one stripe map header for
    205 						 * each stripe unit we bail on */
    206 						RF_Malloc(asmh_b, sizeof(RF_AccessStripeMapHeader_t **) * asm_h->numStripes * raidPtr->Layout.numDataCol, (RF_AccessStripeMapHeader_t ***));
    207 						/* create an array of ptrs to
    208 						 * arrays of blockFuncs */
    209 						RF_Malloc(blockFuncs, asm_h->numStripes * raidPtr->Layout.numDataCol * sizeof(RF_VoidFuncPtr), (RF_VoidFuncPtr **));
    210 					}
    211 					/* create an array of creation funcs
    212 					 * (called blockFuncs) for this stripe
    213 					 * unit */
    214 					numBlocks = physPtr->numSector;
    215 					numBlockDags += numBlocks;
    216 					RF_Malloc(blockFuncs[numStripeUnitsBailed], numBlocks * sizeof(RF_VoidFuncPtr), (RF_VoidFuncPtr *));
    217 					RF_Malloc(asmh_b[numStripeUnitsBailed], numBlocks * sizeof(RF_AccessStripeMapHeader_t *), (RF_AccessStripeMapHeader_t **));
    218 
    219 					/* lookup array of blockFuncs for this
    220 					 * stripe unit */
    221 					for (k = 0; k < numBlocks; k++) {
    222 						/* remap for series of single
    223 						 * stripe-unit accesses */
    224 						address = physPtr->raidAddress + k;
    225 						length = 1;
    226 						buffer = physPtr->bufPtr + (k * (1 << raidPtr->logBytesPerSector));
    227 
    228 						asmh_b[numStripeUnitsBailed][k] = rf_MapAccess(raidPtr, address, length, buffer, RF_DONT_REMAP);
    229 						asm_bp = asmh_b[numStripeUnitsBailed][k]->stripeMap;
    230 
    231 						/* get the creation func for
    232 						 * this stripe unit */
    233 						(raidPtr->Layout.map->SelectionFunc) (raidPtr, type, asm_bp, &(blockFuncs[numStripeUnitsBailed][k]));
    234 
    235 						/* check to see if we found a
    236 						 * creation func for this
    237 						 * stripe unit */
    238 						if (blockFuncs[numStripeUnitsBailed][k] == NULL)
    239 							cantCreateDAGs = RF_TRUE;
    240 					}
    241 					numStripeUnitsBailed++;
    242 				} else {
    243 					numUnitDags++;
    244 				}
    245 			}
    246 			RF_ASSERT(j == numStripeUnits);
    247 			numStripesBailed++;
    248 		}
    249 	}
    250 
    251 	if (cantCreateDAGs) {
    252 		/* free memory and punt */
    253 		if (asm_h->numStripes > MAXNSTRIPES)
    254 			RF_Free(stripeFuncs, asm_h->numStripes * sizeof(RF_VoidFuncPtr));
    255 		if (numStripesBailed > 0) {
    256 			stripeNum = 0;
    257 			for (i = 0, asm_p = asmap; asm_p; asm_p = asm_p->next, i++)
    258 				if (stripeFuncs[i] == NULL) {
    259 					numStripeUnits = asm_p->numStripeUnitsAccessed;
    260 					for (j = 0; j < numStripeUnits; j++)
    261 						rf_FreeAccessStripeMap(asmh_u[stripeNum][j]);
    262 					RF_Free(asmh_u[stripeNum], numStripeUnits * sizeof(RF_AccessStripeMapHeader_t *));
    263 					RF_Free(stripeUnitFuncs[stripeNum], numStripeUnits * sizeof(RF_VoidFuncPtr));
    264 					stripeNum++;
    265 				}
    266 			RF_ASSERT(stripeNum == numStripesBailed);
    267 			RF_Free(stripeUnitFuncs, asm_h->numStripes * sizeof(RF_VoidFuncPtr));
    268 			RF_Free(asmh_u, asm_h->numStripes * sizeof(RF_AccessStripeMapHeader_t **));
    269 		}
    270 		desc->numStripes = 0;
    271 		return (1);
    272 	} else {
    273 		/* begin dag creation */
    274 		stripeNum = 0;
    275 		stripeUnitNum = 0;
    276 
    277 		/* create a list of dagLists and fill them in */
    278 
    279 		dagListend = NULL;
    280 
    281 		for (i = 0, asm_p = asmap; asm_p; asm_p = asm_p->next, i++) {
    282 			/* grab dag header for this stripe */
    283 			dag_h = NULL;
    284 
    285 			dagList = rf_AllocDAGList();
    286 
    287 			/* always tack the new dagList onto the end of the list... */
    288 			if (dagListend == NULL) {
    289 				desc->dagList = dagList;
    290 			} else {
    291 				dagListend->next = dagList;
    292 			}
    293 			dagListend = dagList;
    294 
    295 			dagList->desc = desc;
    296 
    297 			if (stripeFuncs[i] == (RF_VoidFuncPtr) NULL) {
    298 				/* use bailout functions for this stripe */
    299 				for (j = 0, physPtr = asm_p->physInfo; physPtr; physPtr = physPtr->next, j++) {
    300 					uFunc = stripeUnitFuncs[stripeNum][j];
    301 					if (uFunc == (RF_VoidFuncPtr) NULL) {
    302 						/* use bailout functions for
    303 						 * this stripe unit */
    304 						for (k = 0; k < physPtr->numSector; k++) {
    305 							/* create a dag for
    306 							 * this block */
    307 							InitHdrNode(&tempdag_h, raidPtr);
    308 							dagList->numDags++;
    309 							if (dag_h == NULL) {
    310 								dag_h = tempdag_h;
    311 							} else {
    312 								lastdag_h->next = tempdag_h;
    313 							}
    314 							lastdag_h = tempdag_h;
    315 
    316 							bFunc = blockFuncs[stripeUnitNum][k];
    317 							RF_ASSERT(bFunc);
    318 							asm_bp = asmh_b[stripeUnitNum][k]->stripeMap;
    319 							(*bFunc) (raidPtr, asm_bp, tempdag_h, bp, flags, tempdag_h->allocList);
    320 						}
    321 						stripeUnitNum++;
    322 					} else {
    323 						/* create a dag for this unit */
    324 						InitHdrNode(&tempdag_h, raidPtr);
    325 						dagList->numDags++;
    326 						if (dag_h == NULL) {
    327 							dag_h = tempdag_h;
    328 						} else {
    329 							lastdag_h->next = tempdag_h;
    330 						}
    331 						lastdag_h = tempdag_h;
    332 
    333 						asm_up = asmh_u[stripeNum][j]->stripeMap;
    334 						(*uFunc) (raidPtr, asm_up, tempdag_h, bp, flags, tempdag_h->allocList);
    335 					}
    336 				}
    337 				RF_ASSERT(j == asm_p->numStripeUnitsAccessed);
    338 				/* merge linked bailout dag to existing dag
    339 				 * collection */
    340 				stripeNum++;
    341 			} else {
    342 				/* Create a dag for this parity stripe */
    343 				InitHdrNode(&tempdag_h, raidPtr);
    344 				dagList->numDags++;
    345 				if (dag_h == NULL) {
    346 					dag_h = tempdag_h;
    347 				} else {
    348 					lastdag_h->next = tempdag_h;
    349 				}
    350 				lastdag_h = tempdag_h;
    351 
    352 				(stripeFuncs[i]) (raidPtr, asm_p, tempdag_h, bp, flags, tempdag_h->allocList);
    353 			}
    354 			dagList->dags = dag_h;
    355 		}
    356 		RF_ASSERT(i == desc->numStripes);
    357 
    358 		/* free memory */
    359 		if (asm_h->numStripes > MAXNSTRIPES)
    360 			RF_Free(stripeFuncs, asm_h->numStripes * sizeof(RF_VoidFuncPtr));
    361 		if ((numStripesBailed > 0) || (numStripeUnitsBailed > 0)) {
    362 			stripeNum = 0;
    363 			stripeUnitNum = 0;
    364 			if (dag_h->asmList) {
    365 				endASMList = dag_h->asmList;
    366 				while (endASMList->next)
    367 					endASMList = endASMList->next;
    368 			} else
    369 				endASMList = NULL;
    370 			/* walk through io, stripe by stripe */
    371 			for (i = 0, asm_p = asmap; asm_p; asm_p = asm_p->next, i++)
    372 				if (stripeFuncs[i] == NULL) {
    373 					numStripeUnits = asm_p->numStripeUnitsAccessed;
    374 					/* walk through stripe, stripe unit by
    375 					 * stripe unit */
    376 					for (j = 0, physPtr = asm_p->physInfo; physPtr; physPtr = physPtr->next, j++) {
    377 						if (stripeUnitFuncs[stripeNum][j] == NULL) {
    378 							numBlocks = physPtr->numSector;
    379 							/* walk through stripe
    380 							 * unit, block by
    381 							 * block */
    382 							for (k = 0; k < numBlocks; k++)
    383 								if (dag_h->asmList == NULL) {
    384 									dag_h->asmList = asmh_b[stripeUnitNum][k];
    385 									endASMList = dag_h->asmList;
    386 								} else {
    387 									endASMList->next = asmh_b[stripeUnitNum][k];
    388 									endASMList = endASMList->next;
    389 								}
    390 							RF_Free(asmh_b[stripeUnitNum], numBlocks * sizeof(RF_AccessStripeMapHeader_t *));
    391 							RF_Free(blockFuncs[stripeUnitNum], numBlocks * sizeof(RF_VoidFuncPtr));
    392 							stripeUnitNum++;
    393 						}
    394 						if (dag_h->asmList == NULL) {
    395 							dag_h->asmList = asmh_u[stripeNum][j];
    396 							endASMList = dag_h->asmList;
    397 						} else {
    398 							endASMList->next = asmh_u[stripeNum][j];
    399 							endASMList = endASMList->next;
    400 						}
    401 					}
    402 					RF_Free(asmh_u[stripeNum], numStripeUnits * sizeof(RF_AccessStripeMapHeader_t *));
    403 					RF_Free(stripeUnitFuncs[stripeNum], numStripeUnits * sizeof(RF_VoidFuncPtr));
    404 					stripeNum++;
    405 				}
    406 			RF_ASSERT(stripeNum == numStripesBailed);
    407 			RF_Free(stripeUnitFuncs, asm_h->numStripes * sizeof(RF_VoidFuncPtr));
    408 			RF_Free(asmh_u, asm_h->numStripes * sizeof(RF_AccessStripeMapHeader_t **));
    409 			if (numStripeUnitsBailed > 0) {
    410 				RF_ASSERT(stripeUnitNum == numStripeUnitsBailed);
    411 				RF_Free(blockFuncs, raidPtr->Layout.numDataCol * asm_h->numStripes * sizeof(RF_VoidFuncPtr));
    412 				RF_Free(asmh_b, raidPtr->Layout.numDataCol * asm_h->numStripes * sizeof(RF_AccessStripeMapHeader_t **));
    413 			}
    414 		}
    415 		return (0);
    416 	}
    417 }
    418