xref: /netbsd-src/sys/dev/raidframe/rf_dagfuncs.c (revision e5548b402ae4c44fb816de42c7bba9581ce23ef5)
1 /*	$NetBSD: rf_dagfuncs.c,v 1.25 2005/12/11 12:23:37 christos 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@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  * dagfuncs.c -- DAG node execution routines
31  *
32  * Rules:
33  * 1. Every DAG execution function must eventually cause node->status to
34  *    get set to "good" or "bad", and "FinishNode" to be called. In the
35  *    case of nodes that complete immediately (xor, NullNodeFunc, etc),
36  *    the node execution function can do these two things directly. In
37  *    the case of nodes that have to wait for some event (a disk read to
38  *    complete, a lock to be released, etc) to occur before they can
39  *    complete, this is typically achieved by having whatever module
40  *    is doing the operation call GenericWakeupFunc upon completion.
41  * 2. DAG execution functions should check the status in the DAG header
42  *    and NOP out their operations if the status is not "enable". However,
43  *    execution functions that release resources must be sure to release
44  *    them even when they NOP out the function that would use them.
45  *    Functions that acquire resources should go ahead and acquire them
46  *    even when they NOP, so that a downstream release node will not have
47  *    to check to find out whether or not the acquire was suppressed.
48  */
49 
50 #include <sys/cdefs.h>
51 __KERNEL_RCSID(0, "$NetBSD: rf_dagfuncs.c,v 1.25 2005/12/11 12:23:37 christos Exp $");
52 
53 #include <sys/param.h>
54 #include <sys/ioctl.h>
55 
56 #include "rf_archs.h"
57 #include "rf_raid.h"
58 #include "rf_dag.h"
59 #include "rf_layout.h"
60 #include "rf_etimer.h"
61 #include "rf_acctrace.h"
62 #include "rf_diskqueue.h"
63 #include "rf_dagfuncs.h"
64 #include "rf_general.h"
65 #include "rf_engine.h"
66 #include "rf_dagutils.h"
67 
68 #include "rf_kintf.h"
69 
70 #if RF_INCLUDE_PARITYLOGGING > 0
71 #include "rf_paritylog.h"
72 #endif				/* RF_INCLUDE_PARITYLOGGING > 0 */
73 
74 int     (*rf_DiskReadFunc) (RF_DagNode_t *);
75 int     (*rf_DiskWriteFunc) (RF_DagNode_t *);
76 int     (*rf_DiskReadUndoFunc) (RF_DagNode_t *);
77 int     (*rf_DiskWriteUndoFunc) (RF_DagNode_t *);
78 int     (*rf_DiskUnlockFunc) (RF_DagNode_t *);
79 int     (*rf_DiskUnlockUndoFunc) (RF_DagNode_t *);
80 int     (*rf_RegularXorUndoFunc) (RF_DagNode_t *);
81 int     (*rf_SimpleXorUndoFunc) (RF_DagNode_t *);
82 int     (*rf_RecoveryXorUndoFunc) (RF_DagNode_t *);
83 
84 /*****************************************************************************
85  * main (only) configuration routine for this module
86  ****************************************************************************/
87 int
88 rf_ConfigureDAGFuncs(RF_ShutdownList_t **listp)
89 {
90 	RF_ASSERT(((sizeof(long) == 8) && RF_LONGSHIFT == 3) ||
91 		  ((sizeof(long) == 4) && RF_LONGSHIFT == 2));
92 	rf_DiskReadFunc = rf_DiskReadFuncForThreads;
93 	rf_DiskReadUndoFunc = rf_DiskUndoFunc;
94 	rf_DiskWriteFunc = rf_DiskWriteFuncForThreads;
95 	rf_DiskWriteUndoFunc = rf_DiskUndoFunc;
96 	rf_DiskUnlockFunc = rf_DiskUnlockFuncForThreads;
97 	rf_DiskUnlockUndoFunc = rf_NullNodeUndoFunc;
98 	rf_RegularXorUndoFunc = rf_NullNodeUndoFunc;
99 	rf_SimpleXorUndoFunc = rf_NullNodeUndoFunc;
100 	rf_RecoveryXorUndoFunc = rf_NullNodeUndoFunc;
101 	return (0);
102 }
103 
104 
105 
106 /*****************************************************************************
107  * the execution function associated with a terminate node
108  ****************************************************************************/
109 int
110 rf_TerminateFunc(RF_DagNode_t *node)
111 {
112 	RF_ASSERT(node->dagHdr->numCommits == node->dagHdr->numCommitNodes);
113 	node->status = rf_good;
114 	return (rf_FinishNode(node, RF_THREAD_CONTEXT));
115 }
116 
117 int
118 rf_TerminateUndoFunc(RF_DagNode_t *node)
119 {
120 	return (0);
121 }
122 
123 
124 /*****************************************************************************
125  * execution functions associated with a mirror node
126  *
127  * parameters:
128  *
129  * 0 - physical disk addres of data
130  * 1 - buffer for holding read data
131  * 2 - parity stripe ID
132  * 3 - flags
133  * 4 - physical disk address of mirror (parity)
134  *
135  ****************************************************************************/
136 
137 int
138 rf_DiskReadMirrorIdleFunc(RF_DagNode_t *node)
139 {
140 	/* select the mirror copy with the shortest queue and fill in node
141 	 * parameters with physical disk address */
142 
143 	rf_SelectMirrorDiskIdle(node);
144 	return (rf_DiskReadFunc(node));
145 }
146 
147 #if (RF_INCLUDE_CHAINDECLUSTER > 0) || (RF_INCLUDE_INTERDECLUSTER > 0) || (RF_DEBUG_VALIDATE_DAG > 0)
148 int
149 rf_DiskReadMirrorPartitionFunc(RF_DagNode_t *node)
150 {
151 	/* select the mirror copy with the shortest queue and fill in node
152 	 * parameters with physical disk address */
153 
154 	rf_SelectMirrorDiskPartition(node);
155 	return (rf_DiskReadFunc(node));
156 }
157 #endif
158 
159 int
160 rf_DiskReadMirrorUndoFunc(RF_DagNode_t *node)
161 {
162 	return (0);
163 }
164 
165 
166 
167 #if RF_INCLUDE_PARITYLOGGING > 0
168 /*****************************************************************************
169  * the execution function associated with a parity log update node
170  ****************************************************************************/
171 int
172 rf_ParityLogUpdateFunc(RF_DagNode_t *node)
173 {
174 	RF_PhysDiskAddr_t *pda = (RF_PhysDiskAddr_t *) node->params[0].p;
175 	caddr_t bf = (caddr_t) node->params[1].p;
176 	RF_ParityLogData_t *logData;
177 #if RF_ACC_TRACE > 0
178 	RF_AccTraceEntry_t *tracerec = node->dagHdr->tracerec;
179 	RF_Etimer_t timer;
180 #endif
181 
182 	if (node->dagHdr->status == rf_enable) {
183 #if RF_ACC_TRACE > 0
184 		RF_ETIMER_START(timer);
185 #endif
186 		logData = rf_CreateParityLogData(RF_UPDATE, pda, bf,
187 		    (RF_Raid_t *) (node->dagHdr->raidPtr),
188 		    node->wakeFunc, (void *) node,
189 		    node->dagHdr->tracerec, timer);
190 		if (logData)
191 			rf_ParityLogAppend(logData, RF_FALSE, NULL, RF_FALSE);
192 		else {
193 #if RF_ACC_TRACE > 0
194 			RF_ETIMER_STOP(timer);
195 			RF_ETIMER_EVAL(timer);
196 			tracerec->plog_us += RF_ETIMER_VAL_US(timer);
197 #endif
198 			(node->wakeFunc) (node, ENOMEM);
199 		}
200 	}
201 	return (0);
202 }
203 
204 
205 /*****************************************************************************
206  * the execution function associated with a parity log overwrite node
207  ****************************************************************************/
208 int
209 rf_ParityLogOverwriteFunc(RF_DagNode_t *node)
210 {
211 	RF_PhysDiskAddr_t *pda = (RF_PhysDiskAddr_t *) node->params[0].p;
212 	caddr_t bf = (caddr_t) node->params[1].p;
213 	RF_ParityLogData_t *logData;
214 #if RF_ACC_TRACE > 0
215 	RF_AccTraceEntry_t *tracerec = node->dagHdr->tracerec;
216 	RF_Etimer_t timer;
217 #endif
218 
219 	if (node->dagHdr->status == rf_enable) {
220 #if RF_ACC_TRACE > 0
221 		RF_ETIMER_START(timer);
222 #endif
223 		logData = rf_CreateParityLogData(RF_OVERWRITE, pda, bf,
224 (RF_Raid_t *) (node->dagHdr->raidPtr),
225 		    node->wakeFunc, (void *) node, node->dagHdr->tracerec, timer);
226 		if (logData)
227 			rf_ParityLogAppend(logData, RF_FALSE, NULL, RF_FALSE);
228 		else {
229 #if RF_ACC_TRACE > 0
230 			RF_ETIMER_STOP(timer);
231 			RF_ETIMER_EVAL(timer);
232 			tracerec->plog_us += RF_ETIMER_VAL_US(timer);
233 #endif
234 			(node->wakeFunc) (node, ENOMEM);
235 		}
236 	}
237 	return (0);
238 }
239 
240 int
241 rf_ParityLogUpdateUndoFunc(RF_DagNode_t *node)
242 {
243 	return (0);
244 }
245 
246 int
247 rf_ParityLogOverwriteUndoFunc(RF_DagNode_t *node)
248 {
249 	return (0);
250 }
251 #endif				/* RF_INCLUDE_PARITYLOGGING > 0 */
252 
253 /*****************************************************************************
254  * the execution function associated with a NOP node
255  ****************************************************************************/
256 int
257 rf_NullNodeFunc(RF_DagNode_t *node)
258 {
259 	node->status = rf_good;
260 	return (rf_FinishNode(node, RF_THREAD_CONTEXT));
261 }
262 
263 int
264 rf_NullNodeUndoFunc(RF_DagNode_t *node)
265 {
266 	node->status = rf_undone;
267 	return (rf_FinishNode(node, RF_THREAD_CONTEXT));
268 }
269 
270 
271 /*****************************************************************************
272  * the execution function associated with a disk-read node
273  ****************************************************************************/
274 int
275 rf_DiskReadFuncForThreads(RF_DagNode_t *node)
276 {
277 	RF_DiskQueueData_t *req;
278 	RF_PhysDiskAddr_t *pda = (RF_PhysDiskAddr_t *) node->params[0].p;
279 	caddr_t bf = (caddr_t) node->params[1].p;
280 	RF_StripeNum_t parityStripeID = (RF_StripeNum_t) node->params[2].v;
281 	unsigned priority = RF_EXTRACT_PRIORITY(node->params[3].v);
282 	unsigned which_ru = RF_EXTRACT_RU(node->params[3].v);
283 	RF_IoType_t iotype = (node->dagHdr->status == rf_enable) ? RF_IO_TYPE_READ : RF_IO_TYPE_NOP;
284 	RF_DiskQueue_t *dqs = ((RF_Raid_t *) (node->dagHdr->raidPtr))->Queues;
285 	void   *b_proc = NULL;
286 
287 	if (node->dagHdr->bp)
288 		b_proc = (void *) ((struct buf *) node->dagHdr->bp)->b_proc;
289 
290 	req = rf_CreateDiskQueueData(iotype, pda->startSector, pda->numSector,
291 	    bf, parityStripeID, which_ru,
292 	    (int (*) (void *, int)) node->wakeFunc,
293 	    node,
294 #if RF_ACC_TRACE > 0
295 	     node->dagHdr->tracerec,
296 #else
297              NULL,
298 #endif
299 	    (void *) (node->dagHdr->raidPtr), 0, b_proc, PR_NOWAIT);
300 	if (!req) {
301 		(node->wakeFunc) (node, ENOMEM);
302 	} else {
303 		node->dagFuncData = (void *) req;
304 		rf_DiskIOEnqueue(&(dqs[pda->col]), req, priority);
305 	}
306 	return (0);
307 }
308 
309 
310 /*****************************************************************************
311  * the execution function associated with a disk-write node
312  ****************************************************************************/
313 int
314 rf_DiskWriteFuncForThreads(RF_DagNode_t *node)
315 {
316 	RF_DiskQueueData_t *req;
317 	RF_PhysDiskAddr_t *pda = (RF_PhysDiskAddr_t *) node->params[0].p;
318 	caddr_t bf = (caddr_t) node->params[1].p;
319 	RF_StripeNum_t parityStripeID = (RF_StripeNum_t) node->params[2].v;
320 	unsigned priority = RF_EXTRACT_PRIORITY(node->params[3].v);
321 	unsigned which_ru = RF_EXTRACT_RU(node->params[3].v);
322 	RF_IoType_t iotype = (node->dagHdr->status == rf_enable) ? RF_IO_TYPE_WRITE : RF_IO_TYPE_NOP;
323 	RF_DiskQueue_t *dqs = ((RF_Raid_t *) (node->dagHdr->raidPtr))->Queues;
324 	void   *b_proc = NULL;
325 
326 	if (node->dagHdr->bp)
327 		b_proc = (void *) ((struct buf *) node->dagHdr->bp)->b_proc;
328 
329 	/* normal processing (rollaway or forward recovery) begins here */
330 	req = rf_CreateDiskQueueData(iotype, pda->startSector, pda->numSector,
331 	    bf, parityStripeID, which_ru,
332 	    (int (*) (void *, int)) node->wakeFunc,
333 	    (void *) node,
334 #if RF_ACC_TRACE > 0
335 	    node->dagHdr->tracerec,
336 #else
337 	    NULL,
338 #endif
339 	    (void *) (node->dagHdr->raidPtr),
340 	    0, b_proc, PR_NOWAIT);
341 
342 	if (!req) {
343 		(node->wakeFunc) (node, ENOMEM);
344 	} else {
345 		node->dagFuncData = (void *) req;
346 		rf_DiskIOEnqueue(&(dqs[pda->col]), req, priority);
347 	}
348 
349 	return (0);
350 }
351 /*****************************************************************************
352  * the undo function for disk nodes
353  * Note:  this is not a proper undo of a write node, only locks are released.
354  *        old data is not restored to disk!
355  ****************************************************************************/
356 int
357 rf_DiskUndoFunc(RF_DagNode_t *node)
358 {
359 	RF_DiskQueueData_t *req;
360 	RF_PhysDiskAddr_t *pda = (RF_PhysDiskAddr_t *) node->params[0].p;
361 	RF_DiskQueue_t *dqs = ((RF_Raid_t *) (node->dagHdr->raidPtr))->Queues;
362 
363 	req = rf_CreateDiskQueueData(RF_IO_TYPE_NOP,
364 	    0L, 0, NULL, 0L, 0,
365 	    (int (*) (void *, int)) node->wakeFunc,
366 	    (void *) node,
367 #if RF_ACC_TRACE > 0
368 	     node->dagHdr->tracerec,
369 #else
370 	     NULL,
371 #endif
372 	    (void *) (node->dagHdr->raidPtr),
373 	    RF_UNLOCK_DISK_QUEUE, NULL, PR_NOWAIT);
374 	if (!req)
375 		(node->wakeFunc) (node, ENOMEM);
376 	else {
377 		node->dagFuncData = (void *) req;
378 		rf_DiskIOEnqueue(&(dqs[pda->col]), req, RF_IO_NORMAL_PRIORITY);
379 	}
380 
381 	return (0);
382 }
383 /*****************************************************************************
384  * the execution function associated with an "unlock disk queue" node
385  ****************************************************************************/
386 int
387 rf_DiskUnlockFuncForThreads(RF_DagNode_t *node)
388 {
389 	RF_DiskQueueData_t *req;
390 	RF_PhysDiskAddr_t *pda = (RF_PhysDiskAddr_t *) node->params[0].p;
391 	RF_DiskQueue_t *dqs = ((RF_Raid_t *) (node->dagHdr->raidPtr))->Queues;
392 
393 	req = rf_CreateDiskQueueData(RF_IO_TYPE_NOP,
394 	    0L, 0, NULL, 0L, 0,
395 	    (int (*) (void *, int)) node->wakeFunc,
396 	    (void *) node,
397 #if RF_ACC_TRACE > 0
398 	    node->dagHdr->tracerec,
399 #else
400 	    NULL,
401 #endif
402 	    (void *) (node->dagHdr->raidPtr),
403 	    RF_UNLOCK_DISK_QUEUE, NULL, PR_NOWAIT);
404 	if (!req)
405 		(node->wakeFunc) (node, ENOMEM);
406 	else {
407 		node->dagFuncData = (void *) req;
408 		rf_DiskIOEnqueue(&(dqs[pda->col]), req, RF_IO_NORMAL_PRIORITY);
409 	}
410 
411 	return (0);
412 }
413 /*****************************************************************************
414  * Callback routine for DiskRead and DiskWrite nodes.  When the disk
415  * op completes, the routine is called to set the node status and
416  * inform the execution engine that the node has fired.
417  ****************************************************************************/
418 int
419 rf_GenericWakeupFunc(RF_DagNode_t *node, int status)
420 {
421 
422 	switch (node->status) {
423 	case rf_fired:
424 		if (status)
425 			node->status = rf_bad;
426 		else
427 			node->status = rf_good;
428 		break;
429 	case rf_recover:
430 		/* probably should never reach this case */
431 		if (status)
432 			node->status = rf_panic;
433 		else
434 			node->status = rf_undone;
435 		break;
436 	default:
437 		printf("rf_GenericWakeupFunc:");
438 		printf("node->status is %d,", node->status);
439 		printf("status is %d \n", status);
440 		RF_PANIC();
441 		break;
442 	}
443 	if (node->dagFuncData)
444 		rf_FreeDiskQueueData((RF_DiskQueueData_t *) node->dagFuncData);
445 	return (rf_FinishNode(node, RF_INTR_CONTEXT));
446 }
447 
448 
449 /*****************************************************************************
450  * there are three distinct types of xor nodes:
451 
452  * A "regular xor" is used in the fault-free case where the access
453  * spans a complete stripe unit.  It assumes that the result buffer is
454  * one full stripe unit in size, and uses the stripe-unit-offset
455  * values that it computes from the PDAs to determine where within the
456  * stripe unit to XOR each argument buffer.
457  *
458  * A "simple xor" is used in the fault-free case where the access
459  * touches only a portion of one (or two, in some cases) stripe
460  * unit(s).  It assumes that all the argument buffers are of the same
461  * size and have the same stripe unit offset.
462  *
463  * A "recovery xor" is used in the degraded-mode case.  It's similar
464  * to the regular xor function except that it takes the failed PDA as
465  * an additional parameter, and uses it to determine what portions of
466  * the argument buffers need to be xor'd into the result buffer, and
467  * where in the result buffer they should go.
468  ****************************************************************************/
469 
470 /* xor the params together and store the result in the result field.
471  * assume the result field points to a buffer that is the size of one
472  * SU, and use the pda params to determine where within the buffer to
473  * XOR the input buffers.  */
474 int
475 rf_RegularXorFunc(RF_DagNode_t *node)
476 {
477 	RF_Raid_t *raidPtr = (RF_Raid_t *) node->params[node->numParams - 1].p;
478 #if RF_ACC_TRACE > 0
479 	RF_AccTraceEntry_t *tracerec = node->dagHdr->tracerec;
480 	RF_Etimer_t timer;
481 #endif
482 	int     i, retcode;
483 
484 	retcode = 0;
485 	if (node->dagHdr->status == rf_enable) {
486 		/* don't do the XOR if the input is the same as the output */
487 #if RF_ACC_TRACE > 0
488 		RF_ETIMER_START(timer);
489 #endif
490 		for (i = 0; i < node->numParams - 1; i += 2)
491 			if (node->params[i + 1].p != node->results[0]) {
492 				retcode = rf_XorIntoBuffer(raidPtr, (RF_PhysDiskAddr_t *) node->params[i].p,
493 							   (char *) node->params[i + 1].p, (char *) node->results[0]);
494 			}
495 #if RF_ACC_TRACE > 0
496 		RF_ETIMER_STOP(timer);
497 		RF_ETIMER_EVAL(timer);
498 		tracerec->xor_us += RF_ETIMER_VAL_US(timer);
499 #endif
500 	}
501 	return (rf_GenericWakeupFunc(node, retcode));	/* call wake func
502 							 * explicitly since no
503 							 * I/O in this node */
504 }
505 /* xor the inputs into the result buffer, ignoring placement issues */
506 int
507 rf_SimpleXorFunc(RF_DagNode_t *node)
508 {
509 	RF_Raid_t *raidPtr = (RF_Raid_t *) node->params[node->numParams - 1].p;
510 	int     i, retcode = 0;
511 #if RF_ACC_TRACE > 0
512 	RF_AccTraceEntry_t *tracerec = node->dagHdr->tracerec;
513 	RF_Etimer_t timer;
514 #endif
515 
516 	if (node->dagHdr->status == rf_enable) {
517 #if RF_ACC_TRACE > 0
518 		RF_ETIMER_START(timer);
519 #endif
520 		/* don't do the XOR if the input is the same as the output */
521 		for (i = 0; i < node->numParams - 1; i += 2)
522 			if (node->params[i + 1].p != node->results[0]) {
523 				retcode = rf_bxor((char *) node->params[i + 1].p, (char *) node->results[0],
524 				    rf_RaidAddressToByte(raidPtr, ((RF_PhysDiskAddr_t *) node->params[i].p)->numSector));
525 			}
526 #if RF_ACC_TRACE > 0
527 		RF_ETIMER_STOP(timer);
528 		RF_ETIMER_EVAL(timer);
529 		tracerec->xor_us += RF_ETIMER_VAL_US(timer);
530 #endif
531 	}
532 	return (rf_GenericWakeupFunc(node, retcode));	/* call wake func
533 							 * explicitly since no
534 							 * I/O in this node */
535 }
536 /* this xor is used by the degraded-mode dag functions to recover lost
537  * data.  the second-to-last parameter is the PDA for the failed
538  * portion of the access.  the code here looks at this PDA and assumes
539  * that the xor target buffer is equal in size to the number of
540  * sectors in the failed PDA.  It then uses the other PDAs in the
541  * parameter list to determine where within the target buffer the
542  * corresponding data should be xored.  */
543 int
544 rf_RecoveryXorFunc(RF_DagNode_t *node)
545 {
546 	RF_Raid_t *raidPtr = (RF_Raid_t *) node->params[node->numParams - 1].p;
547 	RF_RaidLayout_t *layoutPtr = (RF_RaidLayout_t *) & raidPtr->Layout;
548 	RF_PhysDiskAddr_t *failedPDA = (RF_PhysDiskAddr_t *) node->params[node->numParams - 2].p;
549 	int     i, retcode = 0;
550 	RF_PhysDiskAddr_t *pda;
551 	int     suoffset, failedSUOffset = rf_StripeUnitOffset(layoutPtr, failedPDA->startSector);
552 	char   *srcbuf, *destbuf;
553 #if RF_ACC_TRACE > 0
554 	RF_AccTraceEntry_t *tracerec = node->dagHdr->tracerec;
555 	RF_Etimer_t timer;
556 #endif
557 
558 	if (node->dagHdr->status == rf_enable) {
559 #if RF_ACC_TRACE > 0
560 		RF_ETIMER_START(timer);
561 #endif
562 		for (i = 0; i < node->numParams - 2; i += 2)
563 			if (node->params[i + 1].p != node->results[0]) {
564 				pda = (RF_PhysDiskAddr_t *) node->params[i].p;
565 				srcbuf = (char *) node->params[i + 1].p;
566 				suoffset = rf_StripeUnitOffset(layoutPtr, pda->startSector);
567 				destbuf = ((char *) node->results[0]) + rf_RaidAddressToByte(raidPtr, suoffset - failedSUOffset);
568 				retcode = rf_bxor(srcbuf, destbuf, rf_RaidAddressToByte(raidPtr, pda->numSector));
569 			}
570 #if RF_ACC_TRACE > 0
571 		RF_ETIMER_STOP(timer);
572 		RF_ETIMER_EVAL(timer);
573 		tracerec->xor_us += RF_ETIMER_VAL_US(timer);
574 #endif
575 	}
576 	return (rf_GenericWakeupFunc(node, retcode));
577 }
578 /*****************************************************************************
579  * The next three functions are utilities used by the above
580  * xor-execution functions.
581  ****************************************************************************/
582 
583 
584 /*
585  * this is just a glorified buffer xor.  targbuf points to a buffer
586  * that is one full stripe unit in size.  srcbuf points to a buffer
587  * that may be less than 1 SU, but never more.  When the access
588  * described by pda is one SU in size (which by implication means it's
589  * SU-aligned), all that happens is (targbuf) <- (srcbuf ^ targbuf).
590  * When the access is less than one SU in size the XOR occurs on only
591  * the portion of targbuf identified in the pda.  */
592 
593 int
594 rf_XorIntoBuffer(RF_Raid_t *raidPtr, RF_PhysDiskAddr_t *pda,
595 		 char *srcbuf, char *targbuf)
596 {
597 	char   *targptr;
598 	int     sectPerSU = raidPtr->Layout.sectorsPerStripeUnit;
599 	int     SUOffset = pda->startSector % sectPerSU;
600 	int     length, retcode = 0;
601 
602 	RF_ASSERT(pda->numSector <= sectPerSU);
603 
604 	targptr = targbuf + rf_RaidAddressToByte(raidPtr, SUOffset);
605 	length = rf_RaidAddressToByte(raidPtr, pda->numSector);
606 	retcode = rf_bxor(srcbuf, targptr, length);
607 	return (retcode);
608 }
609 /* it really should be the case that the buffer pointers (returned by
610  * malloc) are aligned to the natural word size of the machine, so
611  * this is the only case we optimize for.  The length should always be
612  * a multiple of the sector size, so there should be no problem with
613  * leftover bytes at the end.  */
614 int
615 rf_bxor(char *src, char *dest, int len)
616 {
617 	unsigned mask = sizeof(long) - 1, retcode = 0;
618 
619 	if (!(((unsigned long) src) & mask) &&
620 	    !(((unsigned long) dest) & mask) && !(len & mask)) {
621 		retcode = rf_longword_bxor((unsigned long *) src,
622 					   (unsigned long *) dest,
623 					   len >> RF_LONGSHIFT);
624 	} else {
625 		RF_ASSERT(0);
626 	}
627 	return (retcode);
628 }
629 
630 /* When XORing in kernel mode, we need to map each user page to kernel
631  * space before we can access it.  We don't want to assume anything
632  * about which input buffers are in kernel/user space, nor about their
633  * alignment, so in each loop we compute the maximum number of bytes
634  * that we can xor without crossing any page boundaries, and do only
635  * this many bytes before the next remap.
636  *
637  * len - is in longwords
638  */
639 int
640 rf_longword_bxor(unsigned long *src, unsigned long *dest, int len)
641 {
642 	unsigned long *end = src + len;
643 	unsigned long d0, d1, d2, d3, s0, s1, s2, s3;	/* temps */
644 	unsigned long *pg_src, *pg_dest;   /* per-page source/dest pointers */
645 	int     longs_this_time;/* # longwords to xor in the current iteration */
646 
647 	pg_src = src;
648 	pg_dest = dest;
649 	if (!pg_src || !pg_dest)
650 		return (EFAULT);
651 
652 	while (len >= 4) {
653 		longs_this_time = RF_MIN(len, RF_MIN(RF_BLIP(pg_src), RF_BLIP(pg_dest)) >> RF_LONGSHIFT);	/* note len in longwords */
654 		src += longs_this_time;
655 		dest += longs_this_time;
656 		len -= longs_this_time;
657 		while (longs_this_time >= 4) {
658 			d0 = pg_dest[0];
659 			d1 = pg_dest[1];
660 			d2 = pg_dest[2];
661 			d3 = pg_dest[3];
662 			s0 = pg_src[0];
663 			s1 = pg_src[1];
664 			s2 = pg_src[2];
665 			s3 = pg_src[3];
666 			pg_dest[0] = d0 ^ s0;
667 			pg_dest[1] = d1 ^ s1;
668 			pg_dest[2] = d2 ^ s2;
669 			pg_dest[3] = d3 ^ s3;
670 			pg_src += 4;
671 			pg_dest += 4;
672 			longs_this_time -= 4;
673 		}
674 		while (longs_this_time > 0) {	/* cannot cross any page
675 						 * boundaries here */
676 			*pg_dest++ ^= *pg_src++;
677 			longs_this_time--;
678 		}
679 
680 		/* either we're done, or we've reached a page boundary on one
681 		 * (or possibly both) of the pointers */
682 		if (len) {
683 			if (RF_PAGE_ALIGNED(src))
684 				pg_src = src;
685 			if (RF_PAGE_ALIGNED(dest))
686 				pg_dest = dest;
687 			if (!pg_src || !pg_dest)
688 				return (EFAULT);
689 		}
690 	}
691 	while (src < end) {
692 		*pg_dest++ ^= *pg_src++;
693 		src++;
694 		dest++;
695 		len--;
696 		if (RF_PAGE_ALIGNED(src))
697 			pg_src = src;
698 		if (RF_PAGE_ALIGNED(dest))
699 			pg_dest = dest;
700 	}
701 	RF_ASSERT(len == 0);
702 	return (0);
703 }
704 
705 #if 0
706 /*
707    dst = a ^ b ^ c;
708    a may equal dst
709    see comment above longword_bxor
710    len is length in longwords
711 */
712 int
713 rf_longword_bxor3(unsigned long *dst, unsigned long *a, unsigned long *b,
714 		  unsigned long *c, int len, void *bp)
715 {
716 	unsigned long a0, a1, a2, a3, b0, b1, b2, b3;
717 	unsigned long *pg_a, *pg_b, *pg_c, *pg_dst;	/* per-page source/dest
718 								 * pointers */
719 	int     longs_this_time;/* # longs to xor in the current iteration */
720 	char    dst_is_a = 0;
721 
722 	pg_a = a;
723 	pg_b = b;
724 	pg_c = c;
725 	if (a == dst) {
726 		pg_dst = pg_a;
727 		dst_is_a = 1;
728 	} else {
729 		pg_dst = dst;
730 	}
731 
732 	/* align dest to cache line.  Can't cross a pg boundary on dst here. */
733 	while ((((unsigned long) pg_dst) & 0x1f)) {
734 		*pg_dst++ = *pg_a++ ^ *pg_b++ ^ *pg_c++;
735 		dst++;
736 		a++;
737 		b++;
738 		c++;
739 		if (RF_PAGE_ALIGNED(a)) {
740 			pg_a = a;
741 			if (!pg_a)
742 				return (EFAULT);
743 		}
744 		if (RF_PAGE_ALIGNED(b)) {
745 			pg_b = a;
746 			if (!pg_b)
747 				return (EFAULT);
748 		}
749 		if (RF_PAGE_ALIGNED(c)) {
750 			pg_c = a;
751 			if (!pg_c)
752 				return (EFAULT);
753 		}
754 		len--;
755 	}
756 
757 	while (len > 4) {
758 		longs_this_time = RF_MIN(len, RF_MIN(RF_BLIP(a), RF_MIN(RF_BLIP(b), RF_MIN(RF_BLIP(c), RF_BLIP(dst)))) >> RF_LONGSHIFT);
759 		a += longs_this_time;
760 		b += longs_this_time;
761 		c += longs_this_time;
762 		dst += longs_this_time;
763 		len -= longs_this_time;
764 		while (longs_this_time >= 4) {
765 			a0 = pg_a[0];
766 			longs_this_time -= 4;
767 
768 			a1 = pg_a[1];
769 			a2 = pg_a[2];
770 
771 			a3 = pg_a[3];
772 			pg_a += 4;
773 
774 			b0 = pg_b[0];
775 			b1 = pg_b[1];
776 
777 			b2 = pg_b[2];
778 			b3 = pg_b[3];
779 			/* start dual issue */
780 			a0 ^= b0;
781 			b0 = pg_c[0];
782 
783 			pg_b += 4;
784 			a1 ^= b1;
785 
786 			a2 ^= b2;
787 			a3 ^= b3;
788 
789 			b1 = pg_c[1];
790 			a0 ^= b0;
791 
792 			b2 = pg_c[2];
793 			a1 ^= b1;
794 
795 			b3 = pg_c[3];
796 			a2 ^= b2;
797 
798 			pg_dst[0] = a0;
799 			a3 ^= b3;
800 			pg_dst[1] = a1;
801 			pg_c += 4;
802 			pg_dst[2] = a2;
803 			pg_dst[3] = a3;
804 			pg_dst += 4;
805 		}
806 		while (longs_this_time > 0) {	/* cannot cross any page
807 						 * boundaries here */
808 			*pg_dst++ = *pg_a++ ^ *pg_b++ ^ *pg_c++;
809 			longs_this_time--;
810 		}
811 
812 		if (len) {
813 			if (RF_PAGE_ALIGNED(a)) {
814 				pg_a = a;
815 				if (!pg_a)
816 					return (EFAULT);
817 				if (dst_is_a)
818 					pg_dst = pg_a;
819 			}
820 			if (RF_PAGE_ALIGNED(b)) {
821 				pg_b = b;
822 				if (!pg_b)
823 					return (EFAULT);
824 			}
825 			if (RF_PAGE_ALIGNED(c)) {
826 				pg_c = c;
827 				if (!pg_c)
828 					return (EFAULT);
829 			}
830 			if (!dst_is_a)
831 				if (RF_PAGE_ALIGNED(dst)) {
832 					pg_dst = dst;
833 					if (!pg_dst)
834 						return (EFAULT);
835 				}
836 		}
837 	}
838 	while (len) {
839 		*pg_dst++ = *pg_a++ ^ *pg_b++ ^ *pg_c++;
840 		dst++;
841 		a++;
842 		b++;
843 		c++;
844 		if (RF_PAGE_ALIGNED(a)) {
845 			pg_a = a;
846 			if (!pg_a)
847 				return (EFAULT);
848 			if (dst_is_a)
849 				pg_dst = pg_a;
850 		}
851 		if (RF_PAGE_ALIGNED(b)) {
852 			pg_b = b;
853 			if (!pg_b)
854 				return (EFAULT);
855 		}
856 		if (RF_PAGE_ALIGNED(c)) {
857 			pg_c = c;
858 			if (!pg_c)
859 				return (EFAULT);
860 		}
861 		if (!dst_is_a)
862 			if (RF_PAGE_ALIGNED(dst)) {
863 				pg_dst = dst;
864 				if (!pg_dst)
865 					return (EFAULT);
866 			}
867 		len--;
868 	}
869 	return (0);
870 }
871 
872 int
873 rf_bxor3(unsigned char *dst, unsigned char *a, unsigned char *b,
874 	 unsigned char *c, unsigned long len, void *bp)
875 {
876 	RF_ASSERT(((RF_UL(dst) | RF_UL(a) | RF_UL(b) | RF_UL(c) | len) & 0x7) == 0);
877 
878 	return (rf_longword_bxor3((unsigned long *) dst, (unsigned long *) a,
879 		(unsigned long *) b, (unsigned long *) c, len >> RF_LONGSHIFT, bp));
880 }
881 #endif
882