Home | History | Annotate | Line # | Download | only in raidframe
rf_pq.c revision 1.10
      1  1.10    oster /*	$NetBSD: rf_pq.c,v 1.10 2001/10/04 15:58:55 oster Exp $	*/
      2   1.1    oster /*
      3   1.1    oster  * Copyright (c) 1995 Carnegie-Mellon University.
      4   1.1    oster  * All rights reserved.
      5   1.1    oster  *
      6   1.1    oster  * Author: Daniel Stodolsky
      7   1.1    oster  *
      8   1.1    oster  * Permission to use, copy, modify and distribute this software and
      9   1.1    oster  * its documentation is hereby granted, provided that both the copyright
     10   1.1    oster  * notice and this permission notice appear in all copies of the
     11   1.1    oster  * software, derivative works or modified versions, and any portions
     12   1.1    oster  * thereof, and that both notices appear in supporting documentation.
     13   1.1    oster  *
     14   1.1    oster  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
     15   1.1    oster  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
     16   1.1    oster  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
     17   1.1    oster  *
     18   1.1    oster  * Carnegie Mellon requests users of this software to return to
     19   1.1    oster  *
     20   1.1    oster  *  Software Distribution Coordinator  or  Software.Distribution (at) CS.CMU.EDU
     21   1.1    oster  *  School of Computer Science
     22   1.1    oster  *  Carnegie Mellon University
     23   1.1    oster  *  Pittsburgh PA 15213-3890
     24   1.1    oster  *
     25   1.1    oster  * any improvements or extensions that they make and grant Carnegie the
     26   1.1    oster  * rights to redistribute these changes.
     27   1.1    oster  */
     28   1.1    oster 
     29   1.1    oster /*
     30   1.1    oster  * Code for RAID level 6 (P + Q) disk array architecture.
     31   1.1    oster  */
     32   1.1    oster 
     33   1.1    oster #include "rf_archs.h"
     34   1.8    oster 
     35   1.8    oster #if (RF_INCLUDE_DECL_PQ > 0) || (RF_INCLUDE_RAID6 > 0) || (RF_INCLUDE_EVENODD > 0)
     36   1.8    oster 
     37  1.10    oster #include <dev/raidframe/raidframevar.h>
     38  1.10    oster 
     39   1.1    oster #include "rf_raid.h"
     40   1.1    oster #include "rf_dag.h"
     41   1.1    oster #include "rf_dagffrd.h"
     42   1.1    oster #include "rf_dagffwr.h"
     43   1.1    oster #include "rf_dagdegrd.h"
     44   1.1    oster #include "rf_dagdegwr.h"
     45   1.1    oster #include "rf_dagutils.h"
     46   1.1    oster #include "rf_dagfuncs.h"
     47   1.1    oster #include "rf_etimer.h"
     48   1.1    oster #include "rf_pqdeg.h"
     49   1.1    oster #include "rf_general.h"
     50   1.1    oster #include "rf_map.h"
     51   1.1    oster #include "rf_pq.h"
     52   1.1    oster 
     53   1.3    oster RF_RedFuncs_t rf_pFuncs = {rf_RegularONPFunc, "Regular Old-New P", rf_SimpleONPFunc, "Simple Old-New P"};
     54   1.3    oster RF_RedFuncs_t rf_pRecoveryFuncs = {rf_RecoveryPFunc, "Recovery P Func", rf_RecoveryPFunc, "Recovery P Func"};
     55   1.1    oster 
     56   1.3    oster int
     57   1.3    oster rf_RegularONPFunc(node)
     58   1.3    oster 	RF_DagNode_t *node;
     59   1.1    oster {
     60   1.3    oster 	return (rf_RegularXorFunc(node));
     61   1.1    oster }
     62   1.1    oster /*
     63   1.3    oster    same as simpleONQ func, but the coefficient is always 1
     64   1.1    oster */
     65   1.1    oster 
     66   1.3    oster int
     67   1.3    oster rf_SimpleONPFunc(node)
     68   1.3    oster 	RF_DagNode_t *node;
     69   1.1    oster {
     70   1.3    oster 	return (rf_SimpleXorFunc(node));
     71   1.1    oster }
     72   1.1    oster 
     73   1.3    oster int
     74   1.3    oster rf_RecoveryPFunc(node)
     75   1.3    oster 	RF_DagNode_t *node;
     76   1.1    oster {
     77   1.3    oster 	return (rf_RecoveryXorFunc(node));
     78   1.1    oster }
     79   1.1    oster 
     80   1.3    oster int
     81   1.3    oster rf_RegularPFunc(node)
     82   1.3    oster 	RF_DagNode_t *node;
     83   1.1    oster {
     84   1.3    oster 	return (rf_RegularXorFunc(node));
     85   1.1    oster }
     86   1.8    oster #endif /* (RF_INCLUDE_DECL_PQ > 0) || (RF_INCLUDE_RAID6 > 0) || (RF_INCLUDE_EVENODD > 0) */
     87   1.1    oster #if (RF_INCLUDE_DECL_PQ > 0) || (RF_INCLUDE_RAID6 > 0)
     88   1.1    oster 
     89   1.3    oster static void
     90   1.3    oster QDelta(char *dest, char *obuf, char *nbuf, unsigned length,
     91   1.3    oster     unsigned char coeff);
     92   1.3    oster static void
     93   1.3    oster rf_InvertQ(unsigned long *qbuf, unsigned long *abuf,
     94   1.3    oster     unsigned length, unsigned coeff);
     95   1.3    oster 
     96   1.3    oster RF_RedFuncs_t rf_qFuncs = {rf_RegularONQFunc, "Regular Old-New Q", rf_SimpleONQFunc, "Simple Old-New Q"};
     97   1.3    oster RF_RedFuncs_t rf_qRecoveryFuncs = {rf_RecoveryQFunc, "Recovery Q Func", rf_RecoveryQFunc, "Recovery Q Func"};
     98   1.3    oster RF_RedFuncs_t rf_pqRecoveryFuncs = {rf_RecoveryPQFunc, "Recovery PQ Func", rf_RecoveryPQFunc, "Recovery PQ Func"};
     99   1.3    oster 
    100   1.3    oster void
    101   1.3    oster rf_PQDagSelect(
    102   1.3    oster     RF_Raid_t * raidPtr,
    103   1.3    oster     RF_IoType_t type,
    104   1.3    oster     RF_AccessStripeMap_t * asmap,
    105   1.3    oster     RF_VoidFuncPtr * createFunc)
    106   1.3    oster {
    107   1.3    oster 	RF_RaidLayout_t *layoutPtr = &(raidPtr->Layout);
    108   1.3    oster 	unsigned ndfail = asmap->numDataFailed;
    109   1.3    oster 	unsigned npfail = asmap->numParityFailed;
    110   1.3    oster 	unsigned ntfail = npfail + ndfail;
    111   1.3    oster 
    112   1.3    oster 	RF_ASSERT(RF_IO_IS_R_OR_W(type));
    113   1.3    oster 	if (ntfail > 2) {
    114   1.3    oster 		RF_ERRORMSG("more than two disks failed in a single group!  Aborting I/O operation.\n");
    115   1.3    oster 		 /* *infoFunc = */ *createFunc = NULL;
    116   1.3    oster 		return;
    117   1.3    oster 	}
    118   1.3    oster 	/* ok, we can do this I/O */
    119   1.3    oster 	if (type == RF_IO_TYPE_READ) {
    120   1.3    oster 		switch (ndfail) {
    121   1.3    oster 		case 0:
    122   1.3    oster 			/* fault free read */
    123   1.5    oster 			*createFunc = (RF_VoidFuncPtr) rf_CreateFaultFreeReadDAG;	/* same as raid 5 */
    124   1.3    oster 			break;
    125   1.3    oster 		case 1:
    126   1.3    oster 			/* lost a single data unit */
    127   1.3    oster 			/* two cases: (1) parity is not lost. do a normal raid
    128   1.3    oster 			 * 5 reconstruct read. (2) parity is lost. do a
    129   1.3    oster 			 * reconstruct read using "q". */
    130   1.3    oster 			if (ntfail == 2) {	/* also lost redundancy */
    131   1.3    oster 				if (asmap->failedPDAs[1]->type == RF_PDA_TYPE_PARITY)
    132   1.5    oster 					*createFunc = (RF_VoidFuncPtr) rf_PQ_110_CreateReadDAG;
    133   1.3    oster 				else
    134   1.5    oster 					*createFunc = (RF_VoidFuncPtr) rf_PQ_101_CreateReadDAG;
    135   1.3    oster 			} else {
    136   1.3    oster 				/* P and Q are ok. But is there a failure in
    137   1.3    oster 				 * some unaccessed data unit? */
    138   1.3    oster 				if (rf_NumFailedDataUnitsInStripe(raidPtr, asmap) == 2)
    139   1.5    oster 					*createFunc = (RF_VoidFuncPtr) rf_PQ_200_CreateReadDAG;
    140   1.3    oster 				else
    141   1.5    oster 					*createFunc = (RF_VoidFuncPtr) rf_PQ_100_CreateReadDAG;
    142   1.3    oster 			}
    143   1.3    oster 			break;
    144   1.3    oster 		case 2:
    145   1.3    oster 			/* lost two data units */
    146   1.3    oster 			/* *infoFunc = PQOneTwo; */
    147   1.5    oster 			*createFunc = (RF_VoidFuncPtr) rf_PQ_200_CreateReadDAG;
    148   1.3    oster 			break;
    149   1.3    oster 		}
    150   1.3    oster 		return;
    151   1.3    oster 	}
    152   1.3    oster 	/* a write */
    153   1.3    oster 	switch (ntfail) {
    154   1.3    oster 	case 0:		/* fault free */
    155   1.3    oster 		if (rf_suppressLocksAndLargeWrites ||
    156   1.3    oster 		    (((asmap->numStripeUnitsAccessed <= (layoutPtr->numDataCol / 2)) && (layoutPtr->numDataCol != 1)) ||
    157   1.3    oster 			(asmap->parityInfo->next != NULL) || (asmap->qInfo->next != NULL) || rf_CheckStripeForFailures(raidPtr, asmap))) {
    158   1.3    oster 
    159   1.5    oster 			*createFunc = (RF_VoidFuncPtr) rf_PQCreateSmallWriteDAG;
    160   1.3    oster 		} else {
    161   1.5    oster 			*createFunc = (RF_VoidFuncPtr) rf_PQCreateLargeWriteDAG;
    162   1.3    oster 		}
    163   1.3    oster 		break;
    164   1.3    oster 
    165   1.3    oster 	case 1:		/* single disk fault */
    166   1.3    oster 		if (npfail == 1) {
    167   1.3    oster 			RF_ASSERT((asmap->failedPDAs[0]->type == RF_PDA_TYPE_PARITY) || (asmap->failedPDAs[0]->type == RF_PDA_TYPE_Q));
    168   1.3    oster 			if (asmap->failedPDAs[0]->type == RF_PDA_TYPE_Q) {	/* q died, treat like
    169   1.3    oster 										 * normal mode raid5
    170   1.3    oster 										 * write. */
    171   1.3    oster 				if (((asmap->numStripeUnitsAccessed <= (layoutPtr->numDataCol / 2)) || (asmap->numStripeUnitsAccessed == 1))
    172   1.3    oster 				    || rf_NumFailedDataUnitsInStripe(raidPtr, asmap))
    173   1.5    oster 					*createFunc = (RF_VoidFuncPtr) rf_PQ_001_CreateSmallWriteDAG;
    174   1.3    oster 				else
    175   1.5    oster 					*createFunc = (RF_VoidFuncPtr) rf_PQ_001_CreateLargeWriteDAG;
    176   1.3    oster 			} else {/* parity died, small write only updating Q */
    177   1.3    oster 				if (((asmap->numStripeUnitsAccessed <= (layoutPtr->numDataCol / 2)) || (asmap->numStripeUnitsAccessed == 1))
    178   1.3    oster 				    || rf_NumFailedDataUnitsInStripe(raidPtr, asmap))
    179   1.5    oster 					*createFunc = (RF_VoidFuncPtr) rf_PQ_010_CreateSmallWriteDAG;
    180   1.3    oster 				else
    181   1.5    oster 					*createFunc = (RF_VoidFuncPtr) rf_PQ_010_CreateLargeWriteDAG;
    182   1.3    oster 			}
    183   1.3    oster 		} else {	/* data missing. Do a P reconstruct write if
    184   1.3    oster 				 * only a single data unit is lost in the
    185   1.3    oster 				 * stripe, otherwise a PQ reconstruct write. */
    186   1.3    oster 			if (rf_NumFailedDataUnitsInStripe(raidPtr, asmap) == 2)
    187   1.5    oster 				*createFunc = (RF_VoidFuncPtr) rf_PQ_200_CreateWriteDAG;
    188   1.3    oster 			else
    189   1.5    oster 				*createFunc = (RF_VoidFuncPtr) rf_PQ_100_CreateWriteDAG;
    190   1.3    oster 		}
    191   1.3    oster 		break;
    192   1.3    oster 
    193   1.3    oster 	case 2:		/* two disk faults */
    194   1.3    oster 		switch (npfail) {
    195   1.3    oster 		case 2:	/* both p and q dead */
    196   1.5    oster 			*createFunc = (RF_VoidFuncPtr) rf_PQ_011_CreateWriteDAG;
    197   1.3    oster 			break;
    198   1.3    oster 		case 1:	/* either p or q and dead data */
    199   1.3    oster 			RF_ASSERT(asmap->failedPDAs[0]->type == RF_PDA_TYPE_DATA);
    200   1.3    oster 			RF_ASSERT((asmap->failedPDAs[1]->type == RF_PDA_TYPE_PARITY) || (asmap->failedPDAs[1]->type == RF_PDA_TYPE_Q));
    201   1.3    oster 			if (asmap->failedPDAs[1]->type == RF_PDA_TYPE_Q)
    202   1.5    oster 				*createFunc = (RF_VoidFuncPtr) rf_PQ_101_CreateWriteDAG;
    203   1.3    oster 			else
    204   1.5    oster 				*createFunc = (RF_VoidFuncPtr) rf_PQ_110_CreateWriteDAG;
    205   1.3    oster 			break;
    206   1.3    oster 		case 0:	/* double data loss */
    207   1.5    oster 			*createFunc = (RF_VoidFuncPtr) rf_PQ_200_CreateWriteDAG;
    208   1.3    oster 			break;
    209   1.3    oster 		}
    210   1.3    oster 		break;
    211   1.3    oster 
    212   1.3    oster 	default:		/* more than 2 disk faults */
    213   1.3    oster 		*createFunc = NULL;
    214   1.3    oster 		RF_PANIC();
    215   1.3    oster 	}
    216   1.3    oster 	return;
    217   1.3    oster }
    218   1.3    oster /*
    219   1.3    oster    Used as a stop gap info function
    220   1.3    oster */
    221   1.5    oster #if 0
    222   1.3    oster static void
    223   1.3    oster PQOne(raidPtr, nSucc, nAnte, asmap)
    224   1.3    oster 	RF_Raid_t *raidPtr;
    225   1.3    oster 	int    *nSucc;
    226   1.3    oster 	int    *nAnte;
    227   1.3    oster 	RF_AccessStripeMap_t *asmap;
    228   1.1    oster {
    229   1.3    oster 	*nSucc = *nAnte = 1;
    230   1.1    oster }
    231   1.1    oster 
    232   1.3    oster static void
    233   1.3    oster PQOneTwo(raidPtr, nSucc, nAnte, asmap)
    234   1.3    oster 	RF_Raid_t *raidPtr;
    235   1.3    oster 	int    *nSucc;
    236   1.3    oster 	int    *nAnte;
    237   1.3    oster 	RF_AccessStripeMap_t *asmap;
    238   1.3    oster {
    239   1.3    oster 	*nSucc = 1;
    240   1.3    oster 	*nAnte = 2;
    241   1.3    oster }
    242   1.5    oster #endif
    243   1.5    oster 
    244   1.1    oster RF_CREATE_DAG_FUNC_DECL(rf_PQCreateLargeWriteDAG)
    245   1.1    oster {
    246   1.3    oster 	rf_CommonCreateLargeWriteDAG(raidPtr, asmap, dag_h, bp, flags, allocList, 2,
    247   1.3    oster 	    rf_RegularPQFunc, RF_FALSE);
    248   1.1    oster }
    249   1.1    oster 
    250   1.3    oster int
    251   1.3    oster rf_RegularONQFunc(node)
    252   1.3    oster 	RF_DagNode_t *node;
    253   1.3    oster {
    254   1.3    oster 	int     np = node->numParams;
    255   1.3    oster 	int     d;
    256   1.3    oster 	RF_Raid_t *raidPtr = (RF_Raid_t *) node->params[np - 1].p;
    257   1.3    oster 	int     i;
    258   1.3    oster 	RF_AccTraceEntry_t *tracerec = node->dagHdr->tracerec;
    259   1.3    oster 	RF_Etimer_t timer;
    260   1.3    oster 	char   *qbuf, *qpbuf;
    261   1.3    oster 	char   *obuf, *nbuf;
    262   1.3    oster 	RF_PhysDiskAddr_t *old, *new;
    263   1.3    oster 	unsigned long coeff;
    264   1.3    oster 	unsigned secPerSU = raidPtr->Layout.sectorsPerStripeUnit;
    265   1.3    oster 
    266   1.3    oster 	RF_ETIMER_START(timer);
    267   1.3    oster 
    268   1.3    oster 	d = (np - 3) / 4;
    269   1.3    oster 	RF_ASSERT(4 * d + 3 == np);
    270   1.3    oster 	qbuf = (char *) node->params[2 * d + 1].p;	/* q buffer */
    271   1.3    oster 	for (i = 0; i < d; i++) {
    272   1.3    oster 		old = (RF_PhysDiskAddr_t *) node->params[2 * i].p;
    273   1.3    oster 		obuf = (char *) node->params[2 * i + 1].p;
    274   1.3    oster 		new = (RF_PhysDiskAddr_t *) node->params[2 * (d + 1 + i)].p;
    275   1.3    oster 		nbuf = (char *) node->params[2 * (d + 1 + i) + 1].p;
    276   1.3    oster 		RF_ASSERT(new->numSector == old->numSector);
    277   1.3    oster 		RF_ASSERT(new->raidAddress == old->raidAddress);
    278   1.3    oster 		/* the stripe unit within the stripe tells us the coefficient
    279   1.3    oster 		 * to use for the multiply. */
    280   1.3    oster 		coeff = rf_RaidAddressToStripeUnitID(&(raidPtr->Layout), new->raidAddress);
    281   1.3    oster 		/* compute the data unit offset within the column, then add
    282   1.3    oster 		 * one */
    283   1.3    oster 		coeff = (coeff % raidPtr->Layout.numDataCol);
    284   1.3    oster 		qpbuf = qbuf + rf_RaidAddressToByte(raidPtr, old->startSector % secPerSU);
    285   1.3    oster 		QDelta(qpbuf, obuf, nbuf, rf_RaidAddressToByte(raidPtr, old->numSector), coeff);
    286   1.3    oster 	}
    287   1.3    oster 
    288   1.3    oster 	RF_ETIMER_STOP(timer);
    289   1.3    oster 	RF_ETIMER_EVAL(timer);
    290   1.3    oster 	tracerec->q_us += RF_ETIMER_VAL_US(timer);
    291   1.3    oster 	rf_GenericWakeupFunc(node, 0);	/* call wake func explicitly since no
    292   1.3    oster 					 * I/O in this node */
    293   1.3    oster 	return (0);
    294   1.1    oster }
    295   1.1    oster /*
    296   1.1    oster    See the SimpleXORFunc for the difference between a simple and regular func.
    297   1.3    oster    These Q functions should be used for
    298   1.3    oster 
    299   1.3    oster          new q = Q(data,old data,old q)
    300   1.1    oster 
    301   1.3    oster    style updates and not for
    302   1.1    oster 
    303   1.1    oster          q = ( new data, new data, .... )
    304   1.1    oster 
    305   1.1    oster    computations.
    306   1.1    oster 
    307   1.1    oster    The simple q takes 2(2d+1)+1 params, where d is the number
    308   1.1    oster    of stripes written. The order of params is
    309   1.1    oster    old data pda_0, old data buffer_0, old data pda_1, old data buffer_1, ... old data pda_d, old data buffer_d
    310   1.1    oster    [2d] old q pda_0, old q buffer
    311   1.1    oster    [2d_2] new data pda_0, new data buffer_0, ...                                    new data pda_d, new data buffer_d
    312   1.1    oster    raidPtr
    313   1.1    oster */
    314   1.1    oster 
    315   1.3    oster int
    316   1.3    oster rf_SimpleONQFunc(node)
    317   1.3    oster 	RF_DagNode_t *node;
    318   1.3    oster {
    319   1.3    oster 	int     np = node->numParams;
    320   1.3    oster 	int     d;
    321   1.3    oster 	RF_Raid_t *raidPtr = (RF_Raid_t *) node->params[np - 1].p;
    322   1.3    oster 	int     i;
    323   1.3    oster 	RF_AccTraceEntry_t *tracerec = node->dagHdr->tracerec;
    324   1.3    oster 	RF_Etimer_t timer;
    325   1.3    oster 	char   *qbuf;
    326   1.3    oster 	char   *obuf, *nbuf;
    327   1.3    oster 	RF_PhysDiskAddr_t *old, *new;
    328   1.3    oster 	unsigned long coeff;
    329   1.3    oster 
    330   1.3    oster 	RF_ETIMER_START(timer);
    331   1.3    oster 
    332   1.3    oster 	d = (np - 3) / 4;
    333   1.3    oster 	RF_ASSERT(4 * d + 3 == np);
    334   1.3    oster 	qbuf = (char *) node->params[2 * d + 1].p;	/* q buffer */
    335   1.3    oster 	for (i = 0; i < d; i++) {
    336   1.3    oster 		old = (RF_PhysDiskAddr_t *) node->params[2 * i].p;
    337   1.3    oster 		obuf = (char *) node->params[2 * i + 1].p;
    338   1.3    oster 		new = (RF_PhysDiskAddr_t *) node->params[2 * (d + 1 + i)].p;
    339   1.3    oster 		nbuf = (char *) node->params[2 * (d + 1 + i) + 1].p;
    340   1.3    oster 		RF_ASSERT(new->numSector == old->numSector);
    341   1.3    oster 		RF_ASSERT(new->raidAddress == old->raidAddress);
    342   1.3    oster 		/* the stripe unit within the stripe tells us the coefficient
    343   1.3    oster 		 * to use for the multiply. */
    344   1.3    oster 		coeff = rf_RaidAddressToStripeUnitID(&(raidPtr->Layout), new->raidAddress);
    345   1.3    oster 		/* compute the data unit offset within the column, then add
    346   1.3    oster 		 * one */
    347   1.3    oster 		coeff = (coeff % raidPtr->Layout.numDataCol);
    348   1.3    oster 		QDelta(qbuf, obuf, nbuf, rf_RaidAddressToByte(raidPtr, old->numSector), coeff);
    349   1.3    oster 	}
    350   1.3    oster 
    351   1.3    oster 	RF_ETIMER_STOP(timer);
    352   1.3    oster 	RF_ETIMER_EVAL(timer);
    353   1.3    oster 	tracerec->q_us += RF_ETIMER_VAL_US(timer);
    354   1.3    oster 	rf_GenericWakeupFunc(node, 0);	/* call wake func explicitly since no
    355   1.3    oster 					 * I/O in this node */
    356   1.3    oster 	return (0);
    357   1.1    oster }
    358   1.1    oster RF_CREATE_DAG_FUNC_DECL(rf_PQCreateSmallWriteDAG)
    359   1.1    oster {
    360   1.3    oster 	rf_CommonCreateSmallWriteDAG(raidPtr, asmap, dag_h, bp, flags, allocList, &rf_pFuncs, &rf_qFuncs);
    361   1.1    oster }
    362   1.1    oster 
    363   1.5    oster static void RegularQSubr(RF_DagNode_t *node, char   *qbuf);
    364   1.5    oster 
    365   1.3    oster static void
    366   1.3    oster RegularQSubr(node, qbuf)
    367   1.3    oster 	RF_DagNode_t *node;
    368   1.3    oster 	char   *qbuf;
    369   1.3    oster {
    370   1.3    oster 	int     np = node->numParams;
    371   1.3    oster 	int     d;
    372   1.3    oster 	RF_Raid_t *raidPtr = (RF_Raid_t *) node->params[np - 1].p;
    373   1.3    oster 	unsigned secPerSU = raidPtr->Layout.sectorsPerStripeUnit;
    374   1.3    oster 	int     i;
    375   1.3    oster 	RF_AccTraceEntry_t *tracerec = node->dagHdr->tracerec;
    376   1.3    oster 	RF_Etimer_t timer;
    377   1.3    oster 	char   *obuf, *qpbuf;
    378   1.3    oster 	RF_PhysDiskAddr_t *old;
    379   1.3    oster 	unsigned long coeff;
    380   1.3    oster 
    381   1.3    oster 	RF_ETIMER_START(timer);
    382   1.3    oster 
    383   1.3    oster 	d = (np - 1) / 2;
    384   1.3    oster 	RF_ASSERT(2 * d + 1 == np);
    385   1.3    oster 	for (i = 0; i < d; i++) {
    386   1.3    oster 		old = (RF_PhysDiskAddr_t *) node->params[2 * i].p;
    387   1.3    oster 		obuf = (char *) node->params[2 * i + 1].p;
    388   1.3    oster 		coeff = rf_RaidAddressToStripeUnitID(&(raidPtr->Layout), old->raidAddress);
    389   1.3    oster 		/* compute the data unit offset within the column, then add
    390   1.3    oster 		 * one */
    391   1.3    oster 		coeff = (coeff % raidPtr->Layout.numDataCol);
    392   1.3    oster 		/* the input buffers may not all be aligned with the start of
    393   1.3    oster 		 * the stripe. so shift by their sector offset within the
    394   1.3    oster 		 * stripe unit */
    395   1.3    oster 		qpbuf = qbuf + rf_RaidAddressToByte(raidPtr, old->startSector % secPerSU);
    396   1.3    oster 		rf_IncQ((unsigned long *) qpbuf, (unsigned long *) obuf, rf_RaidAddressToByte(raidPtr, old->numSector), coeff);
    397   1.3    oster 	}
    398   1.3    oster 
    399   1.3    oster 	RF_ETIMER_STOP(timer);
    400   1.3    oster 	RF_ETIMER_EVAL(timer);
    401   1.3    oster 	tracerec->q_us += RF_ETIMER_VAL_US(timer);
    402   1.1    oster }
    403   1.1    oster /*
    404   1.1    oster    used in degraded writes.
    405   1.1    oster */
    406   1.1    oster 
    407   1.5    oster static void DegrQSubr(RF_DagNode_t *node);
    408   1.5    oster 
    409   1.3    oster static void
    410   1.3    oster DegrQSubr(node)
    411   1.3    oster 	RF_DagNode_t *node;
    412   1.3    oster {
    413   1.3    oster 	int     np = node->numParams;
    414   1.3    oster 	int     d;
    415   1.3    oster 	RF_Raid_t *raidPtr = (RF_Raid_t *) node->params[np - 1].p;
    416   1.3    oster 	unsigned secPerSU = raidPtr->Layout.sectorsPerStripeUnit;
    417   1.3    oster 	int     i;
    418   1.3    oster 	RF_AccTraceEntry_t *tracerec = node->dagHdr->tracerec;
    419   1.3    oster 	RF_Etimer_t timer;
    420   1.3    oster 	char   *qbuf = node->results[1];
    421   1.3    oster 	char   *obuf, *qpbuf;
    422   1.3    oster 	RF_PhysDiskAddr_t *old;
    423   1.3    oster 	unsigned long coeff;
    424   1.3    oster 	unsigned fail_start;
    425   1.3    oster 	int     j;
    426   1.3    oster 
    427   1.3    oster 	old = (RF_PhysDiskAddr_t *) node->params[np - 2].p;
    428   1.3    oster 	fail_start = old->startSector % secPerSU;
    429   1.3    oster 
    430   1.3    oster 	RF_ETIMER_START(timer);
    431   1.3    oster 
    432   1.3    oster 	d = (np - 2) / 2;
    433   1.3    oster 	RF_ASSERT(2 * d + 2 == np);
    434   1.3    oster 	for (i = 0; i < d; i++) {
    435   1.3    oster 		old = (RF_PhysDiskAddr_t *) node->params[2 * i].p;
    436   1.3    oster 		obuf = (char *) node->params[2 * i + 1].p;
    437   1.3    oster 		coeff = rf_RaidAddressToStripeUnitID(&(raidPtr->Layout), old->raidAddress);
    438   1.3    oster 		/* compute the data unit offset within the column, then add
    439   1.3    oster 		 * one */
    440   1.3    oster 		coeff = (coeff % raidPtr->Layout.numDataCol);
    441   1.3    oster 		/* the input buffers may not all be aligned with the start of
    442   1.3    oster 		 * the stripe. so shift by their sector offset within the
    443   1.3    oster 		 * stripe unit */
    444   1.3    oster 		j = old->startSector % secPerSU;
    445   1.3    oster 		RF_ASSERT(j >= fail_start);
    446   1.3    oster 		qpbuf = qbuf + rf_RaidAddressToByte(raidPtr, j - fail_start);
    447   1.3    oster 		rf_IncQ((unsigned long *) qpbuf, (unsigned long *) obuf, rf_RaidAddressToByte(raidPtr, old->numSector), coeff);
    448   1.3    oster 	}
    449   1.3    oster 
    450   1.3    oster 	RF_ETIMER_STOP(timer);
    451   1.3    oster 	RF_ETIMER_EVAL(timer);
    452   1.3    oster 	tracerec->q_us += RF_ETIMER_VAL_US(timer);
    453   1.1    oster }
    454   1.1    oster /*
    455   1.1    oster    Called by large write code to compute the new parity and the new q.
    456   1.3    oster 
    457   1.1    oster    structure of the params:
    458   1.1    oster 
    459   1.1    oster    pda_0, buffer_0, pda_1 , buffer_1, ... , pda_d, buffer_d ( d = numDataCol
    460   1.3    oster    raidPtr
    461   1.1    oster 
    462   1.1    oster    for a total of 2d+1 arguments.
    463   1.1    oster    The result buffers results[0], results[1] are the buffers for the p and q,
    464   1.1    oster    respectively.
    465   1.1    oster 
    466   1.1    oster    We compute Q first, then compute P. The P calculation may try to reuse
    467   1.1    oster    one of the input buffers for its output, so if we computed P first, we would
    468   1.1    oster    corrupt the input for the q calculation.
    469   1.1    oster */
    470   1.1    oster 
    471   1.3    oster int
    472   1.3    oster rf_RegularPQFunc(node)
    473   1.3    oster 	RF_DagNode_t *node;
    474   1.3    oster {
    475   1.3    oster 	RegularQSubr(node, node->results[1]);
    476   1.3    oster 	return (rf_RegularXorFunc(node));	/* does the wakeup */
    477   1.3    oster }
    478   1.3    oster 
    479   1.3    oster int
    480   1.3    oster rf_RegularQFunc(node)
    481   1.3    oster 	RF_DagNode_t *node;
    482   1.3    oster {
    483   1.3    oster 	/* Almost ... adjust Qsubr args */
    484   1.3    oster 	RegularQSubr(node, node->results[0]);
    485   1.3    oster 	rf_GenericWakeupFunc(node, 0);	/* call wake func explicitly since no
    486   1.3    oster 					 * I/O in this node */
    487   1.3    oster 	return (0);
    488   1.1    oster }
    489   1.1    oster /*
    490   1.1    oster    Called by singly degraded write code to compute the new parity and the new q.
    491   1.3    oster 
    492   1.1    oster    structure of the params:
    493   1.1    oster 
    494   1.3    oster    pda_0, buffer_0, pda_1 , buffer_1, ... , pda_d, buffer_d
    495   1.3    oster    failedPDA raidPtr
    496   1.1    oster 
    497   1.1    oster    for a total of 2d+2 arguments.
    498   1.1    oster    The result buffers results[0], results[1] are the buffers for the parity and q,
    499   1.1    oster    respectively.
    500   1.1    oster 
    501   1.1    oster    We compute Q first, then compute parity. The parity calculation may try to reuse
    502   1.1    oster    one of the input buffers for its output, so if we computed parity first, we would
    503   1.1    oster    corrupt the input for the q calculation.
    504   1.1    oster 
    505   1.1    oster    We treat this identically to the regularPQ case, ignoring the failedPDA extra argument.
    506   1.1    oster */
    507   1.1    oster 
    508   1.3    oster void
    509   1.3    oster rf_Degraded_100_PQFunc(node)
    510   1.3    oster 	RF_DagNode_t *node;
    511   1.3    oster {
    512   1.3    oster 	int     np = node->numParams;
    513   1.3    oster 
    514   1.3    oster 	RF_ASSERT(np >= 2);
    515   1.3    oster 	DegrQSubr(node);
    516   1.3    oster 	rf_RecoveryXorFunc(node);
    517   1.1    oster }
    518   1.1    oster 
    519   1.1    oster 
    520   1.1    oster /*
    521   1.1    oster    The two below are used when reading a stripe with a single lost data unit.
    522   1.1    oster    The parameters are
    523   1.1    oster 
    524   1.1    oster    pda_0, buffer_0, .... pda_n, buffer_n, P pda, P buffer, failedPDA, raidPtr
    525   1.1    oster 
    526   1.1    oster    and results[0] contains the data buffer. Which is originally zero-filled.
    527   1.3    oster 
    528   1.1    oster */
    529   1.1    oster 
    530   1.1    oster /* this Q func is used by the degraded-mode dag functions to recover lost data.
    531   1.1    oster  * the second-to-last parameter is the PDA for the failed portion of the access.
    532   1.1    oster  * the code here looks at this PDA and assumes that the xor target buffer is
    533   1.1    oster  * equal in size to the number of sectors in the failed PDA.  It then uses
    534   1.1    oster  * the other PDAs in the parameter list to determine where within the target
    535   1.1    oster  * buffer the corresponding data should be xored.
    536   1.1    oster  *
    537   1.3    oster  * Recall the basic equation is
    538   1.3    oster  *
    539   1.1    oster  *     Q = ( data_1 + 2 * data_2 ... + k * data_k  ) mod 256
    540   1.1    oster  *
    541   1.1    oster  * so to recover data_j we need
    542   1.1    oster  *
    543   1.1    oster  *    J data_j = (Q - data_1 - 2 data_2 ....- k* data_k) mod 256
    544   1.1    oster  *
    545   1.1    oster  * So the coefficient for each buffer is (255 - data_col), and j should be initialized by
    546   1.1    oster  * copying Q into it. Then we need to do a table lookup to convert to solve
    547   1.1    oster  *   data_j /= J
    548   1.3    oster  *
    549   1.3    oster  *
    550   1.1    oster  */
    551   1.3    oster int
    552   1.3    oster rf_RecoveryQFunc(node)
    553   1.3    oster 	RF_DagNode_t *node;
    554   1.3    oster {
    555   1.3    oster 	RF_Raid_t *raidPtr = (RF_Raid_t *) node->params[node->numParams - 1].p;
    556   1.3    oster 	RF_RaidLayout_t *layoutPtr = (RF_RaidLayout_t *) & raidPtr->Layout;
    557   1.3    oster 	RF_PhysDiskAddr_t *failedPDA = (RF_PhysDiskAddr_t *) node->params[node->numParams - 2].p;
    558   1.3    oster 	int     i;
    559   1.3    oster 	RF_PhysDiskAddr_t *pda;
    560   1.3    oster 	RF_RaidAddr_t suoffset, failedSUOffset = rf_StripeUnitOffset(layoutPtr, failedPDA->startSector);
    561   1.3    oster 	char   *srcbuf, *destbuf;
    562   1.3    oster 	RF_AccTraceEntry_t *tracerec = node->dagHdr->tracerec;
    563   1.3    oster 	RF_Etimer_t timer;
    564   1.3    oster 	unsigned long coeff;
    565   1.3    oster 
    566   1.3    oster 	RF_ETIMER_START(timer);
    567   1.3    oster 	/* start by copying Q into the buffer */
    568   1.3    oster 	bcopy(node->params[node->numParams - 3].p, node->results[0],
    569   1.3    oster 	    rf_RaidAddressToByte(raidPtr, failedPDA->numSector));
    570   1.3    oster 	for (i = 0; i < node->numParams - 4; i += 2) {
    571   1.3    oster 		RF_ASSERT(node->params[i + 1].p != node->results[0]);
    572   1.3    oster 		pda = (RF_PhysDiskAddr_t *) node->params[i].p;
    573   1.3    oster 		srcbuf = (char *) node->params[i + 1].p;
    574   1.3    oster 		suoffset = rf_StripeUnitOffset(layoutPtr, pda->startSector);
    575   1.3    oster 		destbuf = ((char *) node->results[0]) + rf_RaidAddressToByte(raidPtr, suoffset - failedSUOffset);
    576   1.3    oster 		coeff = rf_RaidAddressToStripeUnitID(&(raidPtr->Layout), pda->raidAddress);
    577   1.3    oster 		/* compute the data unit offset within the column */
    578   1.3    oster 		coeff = (coeff % raidPtr->Layout.numDataCol);
    579   1.3    oster 		rf_IncQ((unsigned long *) destbuf, (unsigned long *) srcbuf, rf_RaidAddressToByte(raidPtr, pda->numSector), coeff);
    580   1.3    oster 	}
    581   1.3    oster 	/* Do the nasty inversion now */
    582   1.3    oster 	coeff = (rf_RaidAddressToStripeUnitID(&(raidPtr->Layout), failedPDA->startSector) % raidPtr->Layout.numDataCol);
    583   1.3    oster 	rf_InvertQ(node->results[0], node->results[0], rf_RaidAddressToByte(raidPtr, pda->numSector), coeff);
    584   1.3    oster 	RF_ETIMER_STOP(timer);
    585   1.3    oster 	RF_ETIMER_EVAL(timer);
    586   1.3    oster 	tracerec->q_us += RF_ETIMER_VAL_US(timer);
    587   1.3    oster 	rf_GenericWakeupFunc(node, 0);
    588   1.3    oster 	return (0);
    589   1.3    oster }
    590   1.3    oster 
    591   1.3    oster int
    592   1.3    oster rf_RecoveryPQFunc(node)
    593   1.3    oster 	RF_DagNode_t *node;
    594   1.1    oster {
    595   1.6    oster 	RF_Raid_t *raidPtr = (RF_Raid_t *) node->params[node->numParams - 1].p;
    596   1.6    oster 	printf("raid%d: Recovery from PQ not implemented.\n",raidPtr->raidid);
    597   1.3    oster 	return (1);
    598   1.1    oster }
    599   1.1    oster /*
    600   1.3    oster    Degraded write Q subroutine.
    601   1.1    oster    Used when P is dead.
    602   1.3    oster    Large-write style Q computation.
    603   1.1    oster    Parameters
    604   1.1    oster 
    605   1.1    oster    (pda,buf),(pda,buf),.....,(failedPDA,bufPtr),failedPDA,raidPtr.
    606   1.1    oster 
    607   1.1    oster    We ignore failedPDA.
    608   1.1    oster 
    609   1.1    oster    This is a "simple style" recovery func.
    610   1.1    oster */
    611   1.1    oster 
    612   1.3    oster void
    613   1.3    oster rf_PQ_DegradedWriteQFunc(node)
    614   1.3    oster 	RF_DagNode_t *node;
    615   1.3    oster {
    616   1.3    oster 	int     np = node->numParams;
    617   1.3    oster 	int     d;
    618   1.3    oster 	RF_Raid_t *raidPtr = (RF_Raid_t *) node->params[np - 1].p;
    619   1.3    oster 	unsigned secPerSU = raidPtr->Layout.sectorsPerStripeUnit;
    620   1.3    oster 	int     i;
    621   1.3    oster 	RF_AccTraceEntry_t *tracerec = node->dagHdr->tracerec;
    622   1.3    oster 	RF_Etimer_t timer;
    623   1.3    oster 	char   *qbuf = node->results[0];
    624   1.3    oster 	char   *obuf, *qpbuf;
    625   1.3    oster 	RF_PhysDiskAddr_t *old;
    626   1.3    oster 	unsigned long coeff;
    627   1.3    oster 	int     fail_start, j;
    628   1.3    oster 
    629   1.3    oster 	old = (RF_PhysDiskAddr_t *) node->params[np - 2].p;
    630   1.3    oster 	fail_start = old->startSector % secPerSU;
    631   1.3    oster 
    632   1.3    oster 	RF_ETIMER_START(timer);
    633   1.3    oster 
    634   1.3    oster 	d = (np - 2) / 2;
    635   1.3    oster 	RF_ASSERT(2 * d + 2 == np);
    636   1.3    oster 
    637   1.3    oster 	for (i = 0; i < d; i++) {
    638   1.3    oster 		old = (RF_PhysDiskAddr_t *) node->params[2 * i].p;
    639   1.3    oster 		obuf = (char *) node->params[2 * i + 1].p;
    640   1.3    oster 		coeff = rf_RaidAddressToStripeUnitID(&(raidPtr->Layout), old->raidAddress);
    641   1.3    oster 		/* compute the data unit offset within the column, then add
    642   1.3    oster 		 * one */
    643   1.3    oster 		coeff = (coeff % raidPtr->Layout.numDataCol);
    644   1.3    oster 		j = old->startSector % secPerSU;
    645   1.3    oster 		RF_ASSERT(j >= fail_start);
    646   1.3    oster 		qpbuf = qbuf + rf_RaidAddressToByte(raidPtr, j - fail_start);
    647   1.3    oster 		rf_IncQ((unsigned long *) qpbuf, (unsigned long *) obuf, rf_RaidAddressToByte(raidPtr, old->numSector), coeff);
    648   1.3    oster 	}
    649   1.3    oster 
    650   1.3    oster 	RF_ETIMER_STOP(timer);
    651   1.3    oster 	RF_ETIMER_EVAL(timer);
    652   1.3    oster 	tracerec->q_us += RF_ETIMER_VAL_US(timer);
    653   1.3    oster 	rf_GenericWakeupFunc(node, 0);
    654   1.1    oster }
    655   1.1    oster 
    656   1.1    oster 
    657   1.1    oster 
    658   1.1    oster 
    659   1.1    oster /* Q computations */
    660   1.1    oster 
    661   1.1    oster /*
    662   1.1    oster    coeff - colummn;
    663   1.1    oster 
    664   1.1    oster    compute  dest ^= qfor[28-coeff][rn[coeff+1] a]
    665   1.1    oster 
    666   1.1    oster    on 5-bit basis;
    667   1.1    oster    length in bytes;
    668   1.1    oster */
    669   1.1    oster 
    670   1.3    oster void
    671   1.3    oster rf_IncQ(dest, buf, length, coeff)
    672   1.3    oster 	unsigned long *dest;
    673   1.3    oster 	unsigned long *buf;
    674   1.3    oster 	unsigned length;
    675   1.3    oster 	unsigned coeff;
    676   1.3    oster {
    677   1.3    oster 	unsigned long a, d, new;
    678   1.3    oster 	unsigned long a1, a2;
    679   1.3    oster 	unsigned int *q = &(rf_qfor[28 - coeff][0]);
    680   1.3    oster 	unsigned r = rf_rn[coeff + 1];
    681   1.1    oster 
    682   1.1    oster #define EXTRACT(a,i) ((a >> (5L*i)) & 0x1f)
    683   1.1    oster #define INSERT(a,i) (a << (5L*i))
    684   1.1    oster 
    685   1.3    oster 	length /= 8;
    686   1.3    oster 	/* 13 5 bit quants in a 64 bit word */
    687   1.3    oster 	while (length) {
    688   1.3    oster 		a = *buf++;
    689   1.3    oster 		d = *dest;
    690   1.3    oster 		a1 = EXTRACT(a, 0) ^ r;
    691   1.3    oster 		a2 = EXTRACT(a, 1) ^ r;
    692   1.3    oster 		new = INSERT(a2, 1) | a1;
    693   1.3    oster 		a1 = EXTRACT(a, 2) ^ r;
    694   1.3    oster 		a2 = EXTRACT(a, 3) ^ r;
    695   1.3    oster 		a1 = q[a1];
    696   1.3    oster 		a2 = q[a2];
    697   1.3    oster 		new = new | INSERT(a1, 2) | INSERT(a2, 3);
    698   1.3    oster 		a1 = EXTRACT(a, 4) ^ r;
    699   1.3    oster 		a2 = EXTRACT(a, 5) ^ r;
    700   1.3    oster 		a1 = q[a1];
    701   1.3    oster 		a2 = q[a2];
    702   1.3    oster 		new = new | INSERT(a1, 4) | INSERT(a2, 5);
    703   1.3    oster 		a1 = EXTRACT(a, 5) ^ r;
    704   1.3    oster 		a2 = EXTRACT(a, 6) ^ r;
    705   1.3    oster 		a1 = q[a1];
    706   1.3    oster 		a2 = q[a2];
    707   1.3    oster 		new = new | INSERT(a1, 5) | INSERT(a2, 6);
    708   1.1    oster #if RF_LONGSHIFT > 2
    709   1.3    oster 		a1 = EXTRACT(a, 7) ^ r;
    710   1.3    oster 		a2 = EXTRACT(a, 8) ^ r;
    711   1.3    oster 		a1 = q[a1];
    712   1.3    oster 		a2 = q[a2];
    713   1.3    oster 		new = new | INSERT(a1, 7) | INSERT(a2, 8);
    714   1.3    oster 		a1 = EXTRACT(a, 9) ^ r;
    715   1.3    oster 		a2 = EXTRACT(a, 10) ^ r;
    716   1.3    oster 		a1 = q[a1];
    717   1.3    oster 		a2 = q[a2];
    718   1.3    oster 		new = new | INSERT(a1, 9) | INSERT(a2, 10);
    719   1.3    oster 		a1 = EXTRACT(a, 11) ^ r;
    720   1.3    oster 		a2 = EXTRACT(a, 12) ^ r;
    721   1.3    oster 		a1 = q[a1];
    722   1.3    oster 		a2 = q[a2];
    723   1.3    oster 		new = new | INSERT(a1, 11) | INSERT(a2, 12);
    724   1.3    oster #endif				/* RF_LONGSHIFT > 2 */
    725   1.3    oster 		d ^= new;
    726   1.3    oster 		*dest++ = d;
    727   1.3    oster 		length--;
    728   1.3    oster 	}
    729   1.1    oster }
    730   1.3    oster /*
    731   1.3    oster    compute
    732   1.1    oster 
    733   1.1    oster    dest ^= rf_qfor[28-coeff][rf_rn[coeff+1] (old^new) ]
    734   1.1    oster 
    735   1.1    oster    on a five bit basis.
    736   1.1    oster    optimization: compute old ^ new on 64 bit basis.
    737   1.1    oster 
    738   1.1    oster    length in bytes.
    739   1.1    oster */
    740   1.1    oster 
    741   1.3    oster static void
    742   1.3    oster QDelta(
    743   1.3    oster     char *dest,
    744   1.3    oster     char *obuf,
    745   1.3    oster     char *nbuf,
    746   1.3    oster     unsigned length,
    747   1.3    oster     unsigned char coeff)
    748   1.3    oster {
    749   1.3    oster 	unsigned long a, d, new;
    750   1.3    oster 	unsigned long a1, a2;
    751   1.3    oster 	unsigned int *q = &(rf_qfor[28 - coeff][0]);
    752   1.5    oster 	unsigned int r = rf_rn[coeff + 1];
    753   1.5    oster 
    754   1.5    oster 	r = a1 = a2 = new = d = a = 0; /* XXX for now... */
    755   1.5    oster 	q = NULL; /* XXX for now */
    756   1.1    oster 
    757   1.2    oster #ifdef _KERNEL
    758   1.3    oster 	/* PQ in kernel currently not supported because the encoding/decoding
    759   1.3    oster 	 * table is not present */
    760   1.9  thorpej 	memset(dest, 0, length);
    761   1.3    oster #else				/* KERNEL */
    762   1.3    oster 	/* this code probably doesn't work and should be rewritten  -wvcii */
    763   1.3    oster 	/* 13 5 bit quants in a 64 bit word */
    764   1.3    oster 	length /= 8;
    765   1.3    oster 	while (length) {
    766   1.3    oster 		a = *obuf++;	/* XXX need to reorg to avoid cache conflicts */
    767   1.3    oster 		a ^= *nbuf++;
    768   1.3    oster 		d = *dest;
    769   1.3    oster 		a1 = EXTRACT(a, 0) ^ r;
    770   1.3    oster 		a2 = EXTRACT(a, 1) ^ r;
    771   1.3    oster 		a1 = q[a1];
    772   1.3    oster 		a2 = q[a2];
    773   1.3    oster 		new = INSERT(a2, 1) | a1;
    774   1.3    oster 		a1 = EXTRACT(a, 2) ^ r;
    775   1.3    oster 		a2 = EXTRACT(a, 3) ^ r;
    776   1.3    oster 		a1 = q[a1];
    777   1.3    oster 		a2 = q[a2];
    778   1.3    oster 		new = new | INSERT(a1, 2) | INSERT(a2, 3);
    779   1.3    oster 		a1 = EXTRACT(a, 4) ^ r;
    780   1.3    oster 		a2 = EXTRACT(a, 5) ^ r;
    781   1.3    oster 		a1 = q[a1];
    782   1.3    oster 		a2 = q[a2];
    783   1.3    oster 		new = new | INSERT(a1, 4) | INSERT(a2, 5);
    784   1.3    oster 		a1 = EXTRACT(a, 5) ^ r;
    785   1.3    oster 		a2 = EXTRACT(a, 6) ^ r;
    786   1.3    oster 		a1 = q[a1];
    787   1.3    oster 		a2 = q[a2];
    788   1.3    oster 		new = new | INSERT(a1, 5) | INSERT(a2, 6);
    789   1.1    oster #if RF_LONGSHIFT > 2
    790   1.3    oster 		a1 = EXTRACT(a, 7) ^ r;
    791   1.3    oster 		a2 = EXTRACT(a, 8) ^ r;
    792   1.3    oster 		a1 = q[a1];
    793   1.3    oster 		a2 = q[a2];
    794   1.3    oster 		new = new | INSERT(a1, 7) | INSERT(a2, 8);
    795   1.3    oster 		a1 = EXTRACT(a, 9) ^ r;
    796   1.3    oster 		a2 = EXTRACT(a, 10) ^ r;
    797   1.3    oster 		a1 = q[a1];
    798   1.3    oster 		a2 = q[a2];
    799   1.3    oster 		new = new | INSERT(a1, 9) | INSERT(a2, 10);
    800   1.3    oster 		a1 = EXTRACT(a, 11) ^ r;
    801   1.3    oster 		a2 = EXTRACT(a, 12) ^ r;
    802   1.3    oster 		a1 = q[a1];
    803   1.3    oster 		a2 = q[a2];
    804   1.3    oster 		new = new | INSERT(a1, 11) | INSERT(a2, 12);
    805   1.3    oster #endif				/* RF_LONGSHIFT > 2 */
    806   1.3    oster 		d ^= new;
    807   1.3    oster 		*dest++ = d;
    808   1.3    oster 		length--;
    809   1.3    oster 	}
    810   1.3    oster #endif				/* _KERNEL */
    811   1.1    oster }
    812   1.1    oster /*
    813   1.1    oster    recover columns a and b from the given p and q into
    814   1.1    oster    bufs abuf and bbuf. All bufs are word aligned.
    815   1.1    oster    Length is in bytes.
    816   1.1    oster */
    817   1.3    oster 
    818   1.1    oster 
    819   1.1    oster /*
    820   1.1    oster  * XXX
    821   1.1    oster  *
    822   1.1    oster  * Everything about this seems wrong.
    823   1.1    oster  */
    824   1.3    oster void
    825   1.3    oster rf_PQ_recover(pbuf, qbuf, abuf, bbuf, length, coeff_a, coeff_b)
    826   1.3    oster 	unsigned long *pbuf;
    827   1.3    oster 	unsigned long *qbuf;
    828   1.3    oster 	unsigned long *abuf;
    829   1.3    oster 	unsigned long *bbuf;
    830   1.3    oster 	unsigned length;
    831   1.3    oster 	unsigned coeff_a;
    832   1.3    oster 	unsigned coeff_b;
    833   1.3    oster {
    834   1.3    oster 	unsigned long p, q, a, a0, a1;
    835   1.3    oster 	int     col = (29 * coeff_a) + coeff_b;
    836   1.3    oster 	unsigned char *q0 = &(rf_qinv[col][0]);
    837   1.3    oster 
    838   1.3    oster 	length /= 8;
    839   1.3    oster 	while (length) {
    840   1.3    oster 		p = *pbuf++;
    841   1.3    oster 		q = *qbuf++;
    842   1.3    oster 		a0 = EXTRACT(p, 0);
    843   1.3    oster 		a1 = EXTRACT(q, 0);
    844   1.3    oster 		a = q0[a0 << 5 | a1];
    845   1.1    oster #define MF(i) \
    846   1.1    oster       a0 = EXTRACT(p,i); \
    847   1.1    oster       a1 = EXTRACT(q,i); \
    848   1.1    oster       a  = a | INSERT(q0[a0<<5 | a1],i)
    849   1.1    oster 
    850   1.3    oster 		MF(1);
    851   1.3    oster 		MF(2);
    852   1.3    oster 		MF(3);
    853   1.3    oster 		MF(4);
    854   1.3    oster 		MF(5);
    855   1.3    oster 		MF(6);
    856   1.1    oster #if 0
    857   1.3    oster 		MF(7);
    858   1.3    oster 		MF(8);
    859   1.3    oster 		MF(9);
    860   1.3    oster 		MF(10);
    861   1.3    oster 		MF(11);
    862   1.3    oster 		MF(12);
    863   1.3    oster #endif				/* 0 */
    864   1.3    oster 		*abuf++ = a;
    865   1.3    oster 		*bbuf++ = a ^ p;
    866   1.3    oster 		length--;
    867   1.3    oster 	}
    868   1.1    oster }
    869   1.3    oster /*
    870   1.1    oster    Lost parity and a data column. Recover that data column.
    871   1.1    oster    Assume col coeff is lost. Let q the contents of Q after
    872   1.1    oster    all surviving data columns have been q-xored out of it.
    873   1.1    oster    Then we have the equation
    874   1.1    oster 
    875   1.1    oster    q[28-coeff][a_i ^ r_i+1] = q
    876   1.1    oster 
    877   1.3    oster    but q is cyclic with period 31.
    878   1.1    oster    So q[3+coeff][q[28-coeff][a_i ^ r_{i+1}]] =
    879   1.1    oster       q[31][a_i ^ r_{i+1}] = a_i ^ r_{i+1} .
    880   1.1    oster 
    881   1.1    oster    so a_i = r_{coeff+1} ^ q[3+coeff][q]
    882   1.1    oster 
    883   1.1    oster    The routine is passed q buffer and the buffer
    884   1.1    oster    the data is to be recoverd into. They can be the same.
    885   1.1    oster */
    886   1.1    oster 
    887   1.1    oster 
    888   1.3    oster 
    889   1.3    oster static void
    890   1.3    oster rf_InvertQ(
    891   1.3    oster     unsigned long *qbuf,
    892   1.3    oster     unsigned long *abuf,
    893   1.3    oster     unsigned length,
    894   1.3    oster     unsigned coeff)
    895   1.3    oster {
    896   1.3    oster 	unsigned long a, new;
    897   1.3    oster 	unsigned long a1, a2;
    898   1.3    oster 	unsigned int *q = &(rf_qfor[3 + coeff][0]);
    899   1.3    oster 	unsigned r = rf_rn[coeff + 1];
    900   1.3    oster 
    901   1.3    oster 	/* 13 5 bit quants in a 64 bit word */
    902   1.3    oster 	length /= 8;
    903   1.3    oster 	while (length) {
    904   1.3    oster 		a = *qbuf++;
    905   1.3    oster 		a1 = EXTRACT(a, 0);
    906   1.3    oster 		a2 = EXTRACT(a, 1);
    907   1.3    oster 		a1 = r ^ q[a1];
    908   1.3    oster 		a2 = r ^ q[a2];
    909   1.3    oster 		new = INSERT(a2, 1) | a1;
    910   1.1    oster #define M(i,j) \
    911   1.1    oster       a1 = EXTRACT(a,i); \
    912   1.1    oster       a2 = EXTRACT(a,j); \
    913   1.1    oster       a1 = r ^ q[a1]; \
    914   1.1    oster       a2 = r ^ q[a2]; \
    915   1.1    oster       new = new | INSERT(a1,i) | INSERT(a2,j)
    916   1.1    oster 
    917   1.3    oster 		M(2, 3);
    918   1.3    oster 		M(4, 5);
    919   1.3    oster 		M(5, 6);
    920   1.1    oster #if RF_LONGSHIFT > 2
    921   1.3    oster 		M(7, 8);
    922   1.3    oster 		M(9, 10);
    923   1.3    oster 		M(11, 12);
    924   1.3    oster #endif				/* RF_LONGSHIFT > 2 */
    925   1.3    oster 		*abuf++ = new;
    926   1.3    oster 		length--;
    927   1.3    oster 	}
    928   1.1    oster }
    929   1.3    oster #endif				/* (RF_INCLUDE_DECL_PQ > 0) ||
    930   1.3    oster 				 * (RF_INCLUDE_RAID6 > 0) */
    931