xref: /netbsd-src/sys/dev/raidframe/rf_driver.c (revision 46f5119e40af2e51998f686b2fdcc76b5488f7f3)
1 /*	$NetBSD: rf_driver.c,v 1.128 2011/05/11 18:13:12 mrg Exp $	*/
2 /*-
3  * Copyright (c) 1999 The NetBSD Foundation, Inc.
4  * All rights reserved.
5  *
6  * This code is derived from software contributed to The NetBSD Foundation
7  * by Greg Oster
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 /*
32  * Copyright (c) 1995 Carnegie-Mellon University.
33  * All rights reserved.
34  *
35  * Author: Mark Holland, Khalil Amiri, Claudson Bornstein, William V. Courtright II,
36  *         Robby Findler, Daniel Stodolsky, Rachad Youssef, Jim Zelenka
37  *
38  * Permission to use, copy, modify and distribute this software and
39  * its documentation is hereby granted, provided that both the copyright
40  * notice and this permission notice appear in all copies of the
41  * software, derivative works or modified versions, and any portions
42  * thereof, and that both notices appear in supporting documentation.
43  *
44  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
45  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
46  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
47  *
48  * Carnegie Mellon requests users of this software to return to
49  *
50  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
51  *  School of Computer Science
52  *  Carnegie Mellon University
53  *  Pittsburgh PA 15213-3890
54  *
55  * any improvements or extensions that they make and grant Carnegie the
56  * rights to redistribute these changes.
57  */
58 
59 /******************************************************************************
60  *
61  * rf_driver.c -- main setup, teardown, and access routines for the RAID driver
62  *
63  * all routines are prefixed with rf_ (raidframe), to avoid conficts.
64  *
65  ******************************************************************************/
66 
67 
68 #include <sys/cdefs.h>
69 __KERNEL_RCSID(0, "$NetBSD: rf_driver.c,v 1.128 2011/05/11 18:13:12 mrg Exp $");
70 
71 #ifdef _KERNEL_OPT
72 #include "opt_raid_diagnostic.h"
73 #endif
74 
75 #include <sys/param.h>
76 #include <sys/systm.h>
77 #include <sys/ioctl.h>
78 #include <sys/fcntl.h>
79 #include <sys/vnode.h>
80 
81 
82 #include "rf_archs.h"
83 #include "rf_threadstuff.h"
84 
85 #include <sys/errno.h>
86 
87 #include "rf_raid.h"
88 #include "rf_dag.h"
89 #include "rf_aselect.h"
90 #include "rf_diskqueue.h"
91 #include "rf_parityscan.h"
92 #include "rf_alloclist.h"
93 #include "rf_dagutils.h"
94 #include "rf_utils.h"
95 #include "rf_etimer.h"
96 #include "rf_acctrace.h"
97 #include "rf_general.h"
98 #include "rf_desc.h"
99 #include "rf_states.h"
100 #include "rf_decluster.h"
101 #include "rf_map.h"
102 #include "rf_revent.h"
103 #include "rf_callback.h"
104 #include "rf_engine.h"
105 #include "rf_mcpair.h"
106 #include "rf_nwayxor.h"
107 #include "rf_copyback.h"
108 #include "rf_driver.h"
109 #include "rf_options.h"
110 #include "rf_shutdown.h"
111 #include "rf_kintf.h"
112 #include "rf_paritymap.h"
113 
114 #include <sys/buf.h>
115 
116 #ifndef RF_ACCESS_DEBUG
117 #define RF_ACCESS_DEBUG 0
118 #endif
119 
120 /* rad == RF_RaidAccessDesc_t */
121 #define RF_MAX_FREE_RAD 128
122 #define RF_MIN_FREE_RAD  32
123 
124 /* debug variables */
125 char    rf_panicbuf[2048];	/* a buffer to hold an error msg when we panic */
126 
127 /* main configuration routines */
128 static int raidframe_booted = 0;
129 
130 static void rf_ConfigureDebug(RF_Config_t * cfgPtr);
131 static void set_debug_option(char *name, long val);
132 static void rf_UnconfigureArray(void);
133 static void rf_ShutdownRDFreeList(void *);
134 static int rf_ConfigureRDFreeList(RF_ShutdownList_t **);
135 
136 rf_declare_mutex2(rf_printf_mutex);	/* debug only:  avoids interleaved
137 					 * printfs by different stripes */
138 
139 #define SIGNAL_QUIESCENT_COND(_raid_) \
140 	rf_broadcast_cond2((_raid_)->access_suspend_cv)
141 #define WAIT_FOR_QUIESCENCE(_raid_) \
142 	rf_wait_cond2((_raid_)->access_suspend_cv, \
143 		      (_raid_)->access_suspend_mutex)
144 
145 static int configureCount = 0;	/* number of active configurations */
146 static int isconfigged = 0;	/* is basic raidframe (non per-array)
147 				 * stuff configured */
148 static rf_declare_mutex2(configureMutex); /* used to lock the configuration
149 					   * stuff */
150 static RF_ShutdownList_t *globalShutdown;	/* non array-specific
151 						 * stuff */
152 
153 static int rf_ConfigureRDFreeList(RF_ShutdownList_t ** listp);
154 static int rf_AllocEmergBuffers(RF_Raid_t *);
155 static void rf_FreeEmergBuffers(RF_Raid_t *);
156 
157 /* called at system boot time */
158 int
159 rf_BootRaidframe(void)
160 {
161 
162 	if (raidframe_booted)
163 		return (EBUSY);
164 	raidframe_booted = 1;
165 	rf_init_mutex2(configureMutex, IPL_NONE);
166  	configureCount = 0;
167 	isconfigged = 0;
168 	globalShutdown = NULL;
169 	return (0);
170 }
171 
172 /*
173  * Called whenever an array is shutdown
174  */
175 static void
176 rf_UnconfigureArray(void)
177 {
178 
179 	rf_lock_mutex2(configureMutex);
180 	if (--configureCount == 0) {	/* if no active configurations, shut
181 					 * everything down */
182 		isconfigged = 0;
183 		rf_ShutdownList(&globalShutdown);
184 
185 		/*
186 	         * We must wait until now, because the AllocList module
187 	         * uses the DebugMem module.
188 	         */
189 #if RF_DEBUG_MEM
190 		if (rf_memDebug)
191 			rf_print_unfreed();
192 #endif
193 	}
194 	rf_unlock_mutex2(configureMutex);
195 }
196 
197 /*
198  * Called to shut down an array.
199  */
200 int
201 rf_Shutdown(RF_Raid_t *raidPtr)
202 {
203 
204 	if (!raidPtr->valid) {
205 		RF_ERRORMSG("Attempt to shut down unconfigured RAIDframe driver.  Aborting shutdown\n");
206 		return (EINVAL);
207 	}
208 	/*
209          * wait for outstanding IOs to land
210          * As described in rf_raid.h, we use the rad_freelist lock
211          * to protect the per-array info about outstanding descs
212          * since we need to do freelist locking anyway, and this
213          * cuts down on the amount of serialization we've got going
214          * on.
215          */
216 	rf_lock_mutex2(raidPtr->rad_lock);
217 	if (raidPtr->waitShutdown) {
218 		rf_unlock_mutex2(raidPtr->rad_lock);
219 		return (EBUSY);
220 	}
221 	raidPtr->waitShutdown = 1;
222 	while (raidPtr->nAccOutstanding) {
223 		rf_wait_cond2(raidPtr->outstandingCond, raidPtr->rad_lock);
224 	}
225 	rf_unlock_mutex2(raidPtr->rad_lock);
226 
227 	/* Wait for any parity re-writes to stop... */
228 	while (raidPtr->parity_rewrite_in_progress) {
229 		printf("raid%d: Waiting for parity re-write to exit...\n",
230 		       raidPtr->raidid);
231 		tsleep(&raidPtr->parity_rewrite_in_progress, PRIBIO,
232 		       "rfprwshutdown", 0);
233 	}
234 
235 	/* Wait for any reconstruction to stop... */
236 	rf_lock_mutex2(raidPtr->mutex);
237 	while (raidPtr->reconInProgress) {
238 		printf("raid%d: Waiting for reconstruction to stop...\n",
239 		       raidPtr->raidid);
240 		rf_wait_cond2(raidPtr->waitForReconCond, raidPtr->mutex);
241 	}
242 	rf_unlock_mutex2(raidPtr->mutex);
243 
244 	raidPtr->valid = 0;
245 
246 	if (raidPtr->parity_map != NULL)
247 		rf_paritymap_detach(raidPtr);
248 
249 	rf_update_component_labels(raidPtr, RF_FINAL_COMPONENT_UPDATE);
250 
251 	rf_UnconfigureVnodes(raidPtr);
252 
253 	rf_FreeEmergBuffers(raidPtr);
254 
255 	rf_ShutdownList(&raidPtr->shutdownList);
256 
257 	rf_destroy_cond2(raidPtr->waitForReconCond);
258 	rf_destroy_cond2(raidPtr->adding_hot_spare_cv);
259 
260 	rf_destroy_mutex2(raidPtr->access_suspend_mutex);
261 	rf_destroy_cond2(raidPtr->access_suspend_cv);
262 
263 	rf_destroy_cond2(raidPtr->outstandingCond);
264 	rf_destroy_mutex2(raidPtr->rad_lock);
265 
266 	rf_destroy_mutex2(raidPtr->mutex);
267 
268 	rf_UnconfigureArray();
269 
270 	return (0);
271 }
272 
273 
274 #define DO_INIT_CONFIGURE(f) { \
275 	rc = f (&globalShutdown); \
276 	if (rc) { \
277 		RF_ERRORMSG2("RAIDFRAME: failed %s with %d\n", RF_STRING(f), rc); \
278 		rf_ShutdownList(&globalShutdown); \
279 		configureCount--; \
280 		rf_unlock_mutex2(configureMutex); \
281 		return(rc); \
282 	} \
283 }
284 
285 #define DO_RAID_FAIL() { \
286 	rf_UnconfigureVnodes(raidPtr); \
287 	rf_FreeEmergBuffers(raidPtr); \
288 	rf_ShutdownList(&raidPtr->shutdownList); \
289 	rf_UnconfigureArray(); \
290 }
291 
292 #define DO_RAID_INIT_CONFIGURE(f) { \
293 	rc = f (&raidPtr->shutdownList, raidPtr, cfgPtr); \
294 	if (rc) { \
295 		RF_ERRORMSG2("RAIDFRAME: failed %s with %d\n", RF_STRING(f), rc); \
296 		DO_RAID_FAIL(); \
297 		return(rc); \
298 	} \
299 }
300 
301 int
302 rf_Configure(RF_Raid_t *raidPtr, RF_Config_t *cfgPtr, RF_AutoConfig_t *ac)
303 {
304 	RF_RowCol_t col;
305 	int rc;
306 
307 	rf_lock_mutex2(configureMutex);
308 	configureCount++;
309 	if (isconfigged == 0) {
310 		rf_init_mutex2(rf_printf_mutex, IPL_VM);
311 
312 		/* initialize globals */
313 
314 		DO_INIT_CONFIGURE(rf_ConfigureAllocList);
315 
316 		/*
317 	         * Yes, this does make debugging general to the whole
318 	         * system instead of being array specific. Bummer, drag.
319 		 */
320 		rf_ConfigureDebug(cfgPtr);
321 		DO_INIT_CONFIGURE(rf_ConfigureDebugMem);
322 #if RF_ACC_TRACE > 0
323 		DO_INIT_CONFIGURE(rf_ConfigureAccessTrace);
324 #endif
325 		DO_INIT_CONFIGURE(rf_ConfigureMapModule);
326 		DO_INIT_CONFIGURE(rf_ConfigureReconEvent);
327 		DO_INIT_CONFIGURE(rf_ConfigureCallback);
328 		DO_INIT_CONFIGURE(rf_ConfigureRDFreeList);
329 		DO_INIT_CONFIGURE(rf_ConfigureNWayXor);
330 		DO_INIT_CONFIGURE(rf_ConfigureStripeLockFreeList);
331 		DO_INIT_CONFIGURE(rf_ConfigureMCPair);
332 		DO_INIT_CONFIGURE(rf_ConfigureDAGs);
333 		DO_INIT_CONFIGURE(rf_ConfigureDAGFuncs);
334 		DO_INIT_CONFIGURE(rf_ConfigureReconstruction);
335 		DO_INIT_CONFIGURE(rf_ConfigureCopyback);
336 		DO_INIT_CONFIGURE(rf_ConfigureDiskQueueSystem);
337 		DO_INIT_CONFIGURE(rf_ConfigurePSStatus);
338 		isconfigged = 1;
339 	}
340 	rf_unlock_mutex2(configureMutex);
341 
342 	rf_init_mutex2(raidPtr->mutex, IPL_VM);
343 	/* set up the cleanup list.  Do this after ConfigureDebug so that
344 	 * value of memDebug will be set */
345 
346 	rf_MakeAllocList(raidPtr->cleanupList);
347 	if (raidPtr->cleanupList == NULL) {
348 		DO_RAID_FAIL();
349 		return (ENOMEM);
350 	}
351 	rf_ShutdownCreate(&raidPtr->shutdownList,
352 			  (void (*) (void *)) rf_FreeAllocList,
353 			  raidPtr->cleanupList);
354 
355 	raidPtr->numCol = cfgPtr->numCol;
356 	raidPtr->numSpare = cfgPtr->numSpare;
357 
358 	raidPtr->status = rf_rs_optimal;
359 	raidPtr->reconControl = NULL;
360 
361 	DO_RAID_INIT_CONFIGURE(rf_ConfigureEngine);
362 	DO_RAID_INIT_CONFIGURE(rf_ConfigureStripeLocks);
363 
364 	rf_init_cond2(raidPtr->outstandingCond, "rfocond");
365 	rf_init_mutex2(raidPtr->rad_lock, IPL_VM);
366 
367 	raidPtr->nAccOutstanding = 0;
368 	raidPtr->waitShutdown = 0;
369 
370 	rf_init_mutex2(raidPtr->access_suspend_mutex, IPL_VM);
371 	rf_init_cond2(raidPtr->access_suspend_cv, "rfquiesce");
372 
373 	rf_init_cond2(raidPtr->waitForReconCond, "rfrcnw");
374 
375 	if (ac!=NULL) {
376 		/* We have an AutoConfig structure..  Don't do the
377 		   normal disk configuration... call the auto config
378 		   stuff */
379 		rf_AutoConfigureDisks(raidPtr, cfgPtr, ac);
380 	} else {
381 		DO_RAID_INIT_CONFIGURE(rf_ConfigureDisks);
382 		DO_RAID_INIT_CONFIGURE(rf_ConfigureSpareDisks);
383 	}
384 	/* do this after ConfigureDisks & ConfigureSpareDisks to be sure dev
385 	 * no. is set */
386 	DO_RAID_INIT_CONFIGURE(rf_ConfigureDiskQueues);
387 
388 	DO_RAID_INIT_CONFIGURE(rf_ConfigureLayout);
389 
390 	/* Initialize per-RAID PSS bits */
391 	rf_InitPSStatus(raidPtr);
392 
393 #if RF_INCLUDE_CHAINDECLUSTER > 0
394 	for (col = 0; col < raidPtr->numCol; col++) {
395 		/*
396 		 * XXX better distribution
397 		 */
398 		raidPtr->hist_diskreq[col] = 0;
399 	}
400 #endif
401 	raidPtr->numNewFailures = 0;
402 	raidPtr->copyback_in_progress = 0;
403 	raidPtr->parity_rewrite_in_progress = 0;
404 	raidPtr->adding_hot_spare = 0;
405 	raidPtr->recon_in_progress = 0;
406 
407 	rf_init_cond2(raidPtr->adding_hot_spare_cv, "raidhs");
408 
409 	raidPtr->maxOutstanding = cfgPtr->maxOutstandingDiskReqs;
410 
411 	/* autoconfigure and root_partition will actually get filled in
412 	   after the config is done */
413 	raidPtr->autoconfigure = 0;
414 	raidPtr->root_partition = 0;
415 	raidPtr->last_unit = raidPtr->raidid;
416 	raidPtr->config_order = 0;
417 
418 	if (rf_keepAccTotals) {
419 		raidPtr->keep_acc_totals = 1;
420 	}
421 
422 	/* Allocate a bunch of buffers to be used in low-memory conditions */
423 	raidPtr->iobuf = NULL;
424 
425 	rc = rf_AllocEmergBuffers(raidPtr);
426 	if (rc) {
427 		printf("raid%d: Unable to allocate emergency buffers.\n",
428 		       raidPtr->raidid);
429 		DO_RAID_FAIL();
430 		return(rc);
431 	}
432 
433 	/* Set up parity map stuff, if applicable. */
434 #ifndef RF_NO_PARITY_MAP
435 	rf_paritymap_attach(raidPtr, cfgPtr->force);
436 #endif
437 
438 	raidPtr->valid = 1;
439 
440 	printf("raid%d: %s\n", raidPtr->raidid,
441 	       raidPtr->Layout.map->configName);
442 	printf("raid%d: Components:", raidPtr->raidid);
443 
444 	for (col = 0; col < raidPtr->numCol; col++) {
445 		printf(" %s", raidPtr->Disks[col].devname);
446 		if (RF_DEAD_DISK(raidPtr->Disks[col].status)) {
447 			printf("[**FAILED**]");
448 		}
449 	}
450 	printf("\n");
451 	printf("raid%d: Total Sectors: %" PRIu64 " (%" PRIu64 " MB)\n",
452 	       raidPtr->raidid,
453 	       raidPtr->totalSectors,
454 	       (raidPtr->totalSectors / 1024 *
455 				(1 << raidPtr->logBytesPerSector) / 1024));
456 
457 	return (0);
458 }
459 
460 
461 /*
462 
463   Routines to allocate and free the "emergency buffers" for a given
464   RAID set.  These emergency buffers will be used when the kernel runs
465   out of kernel memory.
466 
467  */
468 
469 static int
470 rf_AllocEmergBuffers(RF_Raid_t *raidPtr)
471 {
472 	void *tmpbuf;
473 	RF_VoidPointerListElem_t *vple;
474 	int i;
475 
476 	/* XXX next line needs tuning... */
477 	raidPtr->numEmergencyBuffers = 10 * raidPtr->numCol;
478 #if DEBUG
479 	printf("raid%d: allocating %d buffers of %d bytes.\n",
480 	       raidPtr->raidid,
481 	       raidPtr->numEmergencyBuffers,
482 	       (int)(raidPtr->Layout.sectorsPerStripeUnit <<
483 	       raidPtr->logBytesPerSector));
484 #endif
485 	for (i = 0; i < raidPtr->numEmergencyBuffers; i++) {
486 		tmpbuf = malloc( raidPtr->Layout.sectorsPerStripeUnit <<
487 				 raidPtr->logBytesPerSector,
488 				 M_RAIDFRAME, M_WAITOK);
489 		if (tmpbuf) {
490 			vple = rf_AllocVPListElem();
491 			vple->p= tmpbuf;
492 			vple->next = raidPtr->iobuf;
493 			raidPtr->iobuf = vple;
494 			raidPtr->iobuf_count++;
495 		} else {
496 			printf("raid%d: failed to allocate emergency buffer!\n",
497 			       raidPtr->raidid);
498 			return 1;
499 		}
500 	}
501 
502 	/* XXX next line needs tuning too... */
503 	raidPtr->numEmergencyStripeBuffers = 10;
504         for (i = 0; i < raidPtr->numEmergencyStripeBuffers; i++) {
505                 tmpbuf = malloc( raidPtr->numCol * (raidPtr->Layout.sectorsPerStripeUnit <<
506                                  raidPtr->logBytesPerSector),
507                                  M_RAIDFRAME, M_WAITOK);
508                 if (tmpbuf) {
509                         vple = rf_AllocVPListElem();
510                         vple->p= tmpbuf;
511                         vple->next = raidPtr->stripebuf;
512                         raidPtr->stripebuf = vple;
513                         raidPtr->stripebuf_count++;
514                 } else {
515                         printf("raid%d: failed to allocate emergency stripe buffer!\n",
516                                raidPtr->raidid);
517 			return 1;
518                 }
519         }
520 
521 	return (0);
522 }
523 
524 static void
525 rf_FreeEmergBuffers(RF_Raid_t *raidPtr)
526 {
527 	RF_VoidPointerListElem_t *tmp;
528 
529 	/* Free the emergency IO buffers */
530 	while (raidPtr->iobuf != NULL) {
531 		tmp = raidPtr->iobuf;
532 		raidPtr->iobuf = raidPtr->iobuf->next;
533 		free(tmp->p, M_RAIDFRAME);
534 		rf_FreeVPListElem(tmp);
535 	}
536 
537 	/* Free the emergency stripe buffers */
538 	while (raidPtr->stripebuf != NULL) {
539 		tmp = raidPtr->stripebuf;
540 		raidPtr->stripebuf = raidPtr->stripebuf->next;
541 		free(tmp->p, M_RAIDFRAME);
542 		rf_FreeVPListElem(tmp);
543 	}
544 }
545 
546 
547 static void
548 rf_ShutdownRDFreeList(void *ignored)
549 {
550 	pool_destroy(&rf_pools.rad);
551 }
552 
553 static int
554 rf_ConfigureRDFreeList(RF_ShutdownList_t **listp)
555 {
556 
557 	rf_pool_init(&rf_pools.rad, sizeof(RF_RaidAccessDesc_t),
558 		     "rf_rad_pl", RF_MIN_FREE_RAD, RF_MAX_FREE_RAD);
559 	rf_ShutdownCreate(listp, rf_ShutdownRDFreeList, NULL);
560 	return (0);
561 }
562 
563 RF_RaidAccessDesc_t *
564 rf_AllocRaidAccDesc(RF_Raid_t *raidPtr, RF_IoType_t type,
565 		    RF_RaidAddr_t raidAddress, RF_SectorCount_t numBlocks,
566 		    void *bufPtr, void *bp, RF_RaidAccessFlags_t flags,
567 		    const RF_AccessState_t *states)
568 {
569 	RF_RaidAccessDesc_t *desc;
570 
571 	desc = pool_get(&rf_pools.rad, PR_WAITOK);
572 
573 	rf_lock_mutex2(raidPtr->rad_lock);
574 	if (raidPtr->waitShutdown) {
575 		/*
576 	         * Actually, we're shutting the array down. Free the desc
577 	         * and return NULL.
578 	         */
579 
580 		rf_unlock_mutex2(raidPtr->rad_lock);
581 		pool_put(&rf_pools.rad, desc);
582 		return (NULL);
583 	}
584 	raidPtr->nAccOutstanding++;
585 
586 	rf_unlock_mutex2(raidPtr->rad_lock);
587 
588 	desc->raidPtr = (void *) raidPtr;
589 	desc->type = type;
590 	desc->raidAddress = raidAddress;
591 	desc->numBlocks = numBlocks;
592 	desc->bufPtr = bufPtr;
593 	desc->bp = bp;
594 	desc->flags = flags;
595 	desc->states = states;
596 	desc->state = 0;
597 	desc->dagList = NULL;
598 
599 	desc->status = 0;
600 	desc->numRetries = 0;
601 #if RF_ACC_TRACE > 0
602 	memset((char *) &desc->tracerec, 0, sizeof(RF_AccTraceEntry_t));
603 #endif
604 	desc->callbackFunc = NULL;
605 	desc->callbackArg = NULL;
606 	desc->next = NULL;
607 	desc->iobufs = NULL;
608 	desc->stripebufs = NULL;
609 
610 	return (desc);
611 }
612 
613 void
614 rf_FreeRaidAccDesc(RF_RaidAccessDesc_t *desc)
615 {
616 	RF_Raid_t *raidPtr = desc->raidPtr;
617 	RF_DagList_t *dagList, *temp;
618 	RF_VoidPointerListElem_t *tmp;
619 
620 	RF_ASSERT(desc);
621 
622 	/* Cleanup the dagList(s) */
623 	dagList = desc->dagList;
624 	while(dagList != NULL) {
625 		temp = dagList;
626 		dagList = dagList->next;
627 		rf_FreeDAGList(temp);
628 	}
629 
630 	while (desc->iobufs) {
631 		tmp = desc->iobufs;
632 		desc->iobufs = desc->iobufs->next;
633 		rf_FreeIOBuffer(raidPtr, tmp);
634 	}
635 
636 	while (desc->stripebufs) {
637 		tmp = desc->stripebufs;
638 		desc->stripebufs = desc->stripebufs->next;
639 		rf_FreeStripeBuffer(raidPtr, tmp);
640 	}
641 
642 	pool_put(&rf_pools.rad, desc);
643 	rf_lock_mutex2(raidPtr->rad_lock);
644 	raidPtr->nAccOutstanding--;
645 	if (raidPtr->waitShutdown) {
646 		rf_signal_cond2(raidPtr->outstandingCond);
647 	}
648 	rf_unlock_mutex2(raidPtr->rad_lock);
649 }
650 /*********************************************************************
651  * Main routine for performing an access.
652  * Accesses are retried until a DAG can not be selected.  This occurs
653  * when either the DAG library is incomplete or there are too many
654  * failures in a parity group.
655  *
656  * type should be read or write async_flag should be RF_TRUE or
657  * RF_FALSE bp_in is a buf pointer.  void *to facilitate ignoring it
658  * outside the kernel
659  ********************************************************************/
660 int
661 rf_DoAccess(RF_Raid_t * raidPtr, RF_IoType_t type, int async_flag,
662 	    RF_RaidAddr_t raidAddress, RF_SectorCount_t numBlocks,
663 	    void *bufPtr, struct buf *bp, RF_RaidAccessFlags_t flags)
664 {
665 	RF_RaidAccessDesc_t *desc;
666 	void *lbufPtr = bufPtr;
667 
668 	raidAddress += rf_raidSectorOffset;
669 
670 #if RF_ACCESS_DEBUG
671 	if (rf_accessDebug) {
672 
673 		printf("logBytes is: %d %d %d\n", raidPtr->raidid,
674 		    raidPtr->logBytesPerSector,
675 		    (int) rf_RaidAddressToByte(raidPtr, numBlocks));
676 		printf("raid%d: %s raidAddr %d (stripeid %d-%d) numBlocks %d (%d bytes) buf 0x%lx\n", raidPtr->raidid,
677 		    (type == RF_IO_TYPE_READ) ? "READ" : "WRITE", (int) raidAddress,
678 		    (int) rf_RaidAddressToStripeID(&raidPtr->Layout, raidAddress),
679 		    (int) rf_RaidAddressToStripeID(&raidPtr->Layout, raidAddress + numBlocks - 1),
680 		    (int) numBlocks,
681 		    (int) rf_RaidAddressToByte(raidPtr, numBlocks),
682 		    (long) bufPtr);
683 	}
684 #endif
685 
686 	desc = rf_AllocRaidAccDesc(raidPtr, type, raidAddress,
687 	    numBlocks, lbufPtr, bp, flags, raidPtr->Layout.map->states);
688 
689 	if (desc == NULL) {
690 		return (ENOMEM);
691 	}
692 #if RF_ACC_TRACE > 0
693 	RF_ETIMER_START(desc->tracerec.tot_timer);
694 #endif
695 	desc->async_flag = async_flag;
696 
697 	if (raidPtr->parity_map != NULL &&
698 	    type == RF_IO_TYPE_WRITE)
699 		rf_paritymap_begin(raidPtr->parity_map, raidAddress,
700 		    numBlocks);
701 
702 	rf_ContinueRaidAccess(desc);
703 
704 	return (0);
705 }
706 #if 0
707 /* force the array into reconfigured mode without doing reconstruction */
708 int
709 rf_SetReconfiguredMode(RF_Raid_t *raidPtr, int col)
710 {
711 	if (!(raidPtr->Layout.map->flags & RF_DISTRIBUTE_SPARE)) {
712 		printf("Can't set reconfigured mode in dedicated-spare array\n");
713 		RF_PANIC();
714 	}
715 	rf_lock_mutex2(raidPtr->mutex);
716 	raidPtr->numFailures++;
717 	raidPtr->Disks[col].status = rf_ds_dist_spared;
718 	raidPtr->status = rf_rs_reconfigured;
719 	rf_update_component_labels(raidPtr, RF_NORMAL_COMPONENT_UPDATE);
720 	/* install spare table only if declustering + distributed sparing
721 	 * architecture. */
722 	if (raidPtr->Layout.map->flags & RF_BD_DECLUSTERED)
723 		rf_InstallSpareTable(raidPtr, col);
724 	rf_unlock_mutex2(raidPtr->mutex);
725 	return (0);
726 }
727 #endif
728 
729 int
730 rf_FailDisk(RF_Raid_t *raidPtr, int fcol, int initRecon)
731 {
732 
733 	/* need to suspend IO's here -- if there are DAGs in flight
734 	   and we pull the rug out from under ci_vp, Bad Things
735 	   can happen.  */
736 
737 	rf_SuspendNewRequestsAndWait(raidPtr);
738 
739 	rf_lock_mutex2(raidPtr->mutex);
740 	if (raidPtr->Disks[fcol].status != rf_ds_failed) {
741 		/* must be failing something that is valid, or else it's
742 		   already marked as failed (in which case we don't
743 		   want to mark it failed again!) */
744 		raidPtr->numFailures++;
745 		raidPtr->Disks[fcol].status = rf_ds_failed;
746 		raidPtr->status = rf_rs_degraded;
747 	}
748 	rf_unlock_mutex2(raidPtr->mutex);
749 
750 	rf_update_component_labels(raidPtr, RF_NORMAL_COMPONENT_UPDATE);
751 
752 	/* Close the component, so that it's not "locked" if someone
753 	   else want's to use it! */
754 
755 	rf_close_component(raidPtr, raidPtr->raid_cinfo[fcol].ci_vp,
756 			   raidPtr->Disks[fcol].auto_configured);
757 
758 	rf_lock_mutex2(raidPtr->mutex);
759 	raidPtr->raid_cinfo[fcol].ci_vp = NULL;
760 
761 	/* Need to mark the component as not being auto_configured
762 	   (in case it was previously). */
763 
764 	raidPtr->Disks[fcol].auto_configured = 0;
765 	rf_unlock_mutex2(raidPtr->mutex);
766 	/* now we can allow IO to continue -- we'll be suspending it
767 	   again in rf_ReconstructFailedDisk() if we have to.. */
768 
769 	rf_ResumeNewRequests(raidPtr);
770 
771 	if (initRecon)
772 		rf_ReconstructFailedDisk(raidPtr, fcol);
773 	return (0);
774 }
775 /* releases a thread that is waiting for the array to become quiesced.
776  * access_suspend_mutex should be locked upon calling this
777  */
778 void
779 rf_SignalQuiescenceLock(RF_Raid_t *raidPtr)
780 {
781 #if RF_DEBUG_QUIESCE
782 	if (rf_quiesceDebug) {
783 		printf("raid%d: Signalling quiescence lock\n",
784 		       raidPtr->raidid);
785 	}
786 #endif
787 	raidPtr->access_suspend_release = 1;
788 
789 	if (raidPtr->waiting_for_quiescence) {
790 		SIGNAL_QUIESCENT_COND(raidPtr);
791 	}
792 }
793 /* suspends all new requests to the array.  No effect on accesses that are in flight.  */
794 int
795 rf_SuspendNewRequestsAndWait(RF_Raid_t *raidPtr)
796 {
797 #if RF_DEBUG_QUIESCE
798 	if (rf_quiesceDebug)
799 		printf("raid%d: Suspending new reqs\n", raidPtr->raidid);
800 #endif
801 	rf_lock_mutex2(raidPtr->access_suspend_mutex);
802 	raidPtr->accesses_suspended++;
803 	raidPtr->waiting_for_quiescence = (raidPtr->accs_in_flight == 0) ? 0 : 1;
804 
805 	if (raidPtr->waiting_for_quiescence) {
806 		raidPtr->access_suspend_release = 0;
807 		while (!raidPtr->access_suspend_release) {
808 #if RF_DEBUG_QUIESCE
809 			printf("raid%d: Suspending: Waiting for Quiescence\n",
810 			       raidPtr->raidid);
811 #endif
812 			WAIT_FOR_QUIESCENCE(raidPtr);
813 			raidPtr->waiting_for_quiescence = 0;
814 		}
815 	}
816 #if RF_DEBUG_QUIESCE
817 	printf("raid%d: Quiescence reached..\n", raidPtr->raidid);
818 #endif
819 
820 	rf_unlock_mutex2(raidPtr->access_suspend_mutex);
821 	return (raidPtr->waiting_for_quiescence);
822 }
823 /* wake up everyone waiting for quiescence to be released */
824 void
825 rf_ResumeNewRequests(RF_Raid_t *raidPtr)
826 {
827 	RF_CallbackDesc_t *t, *cb;
828 
829 #if RF_DEBUG_QUIESCE
830 	if (rf_quiesceDebug)
831 		printf("raid%d: Resuming new requests\n", raidPtr->raidid);
832 #endif
833 
834 	rf_lock_mutex2(raidPtr->access_suspend_mutex);
835 	raidPtr->accesses_suspended--;
836 	if (raidPtr->accesses_suspended == 0)
837 		cb = raidPtr->quiesce_wait_list;
838 	else
839 		cb = NULL;
840 	raidPtr->quiesce_wait_list = NULL;
841 	rf_unlock_mutex2(raidPtr->access_suspend_mutex);
842 
843 	while (cb) {
844 		t = cb;
845 		cb = cb->next;
846 		(t->callbackFunc) (t->callbackArg);
847 		rf_FreeCallbackDesc(t);
848 	}
849 }
850 /*****************************************************************************************
851  *
852  * debug routines
853  *
854  ****************************************************************************************/
855 
856 static void
857 set_debug_option(char *name, long val)
858 {
859 	RF_DebugName_t *p;
860 
861 	for (p = rf_debugNames; p->name; p++) {
862 		if (!strcmp(p->name, name)) {
863 			*(p->ptr) = val;
864 			printf("[Set debug variable %s to %ld]\n", name, val);
865 			return;
866 		}
867 	}
868 	RF_ERRORMSG1("Unknown debug string \"%s\"\n", name);
869 }
870 
871 
872 /* would like to use sscanf here, but apparently not available in kernel */
873 /*ARGSUSED*/
874 static void
875 rf_ConfigureDebug(RF_Config_t *cfgPtr)
876 {
877 	char   *val_p, *name_p, *white_p;
878 	long    val;
879 	int     i;
880 
881 	rf_ResetDebugOptions();
882 	for (i = 0; cfgPtr->debugVars[i][0] && i < RF_MAXDBGV; i++) {
883 		name_p = rf_find_non_white(&cfgPtr->debugVars[i][0]);
884 		white_p = rf_find_white(name_p);	/* skip to start of 2nd
885 							 * word */
886 		val_p = rf_find_non_white(white_p);
887 		if (*val_p == '0' && *(val_p + 1) == 'x')
888 			val = rf_htoi(val_p + 2);
889 		else
890 			val = rf_atoi(val_p);
891 		*white_p = '\0';
892 		set_debug_option(name_p, val);
893 	}
894 }
895 
896 void
897 rf_print_panic_message(int line, const char *file)
898 {
899 	snprintf(rf_panicbuf, sizeof(rf_panicbuf),
900 	    "raidframe error at line %d file %s", line, file);
901 }
902 
903 #ifdef RAID_DIAGNOSTIC
904 void
905 rf_print_assert_panic_message(int line,	const char *file, const char *condition)
906 {
907 	snprintf(rf_panicbuf, sizeof(rf_panicbuf),
908 		"raidframe error at line %d file %s (failed asserting %s)\n",
909 		line, file, condition);
910 }
911 #endif
912 
913 void
914 rf_print_unable_to_init_mutex(const char *file, int line, int rc)
915 {
916 	RF_ERRORMSG3("Unable to init mutex file %s line %d rc=%d\n",
917 		     file, line, rc);
918 }
919 
920 void
921 rf_print_unable_to_add_shutdown(const char *file, int line, int rc)
922 {
923 	RF_ERRORMSG3("Unable to add to shutdown list file %s line %d rc=%d\n",
924 		     file, line, rc);
925 }
926