xref: /netbsd-src/sys/dev/raidframe/rf_driver.c (revision 220b5c059a84c51ea44107ea8951a57ffaecdc8c)
1 /*	$NetBSD: rf_driver.c,v 1.45 2001/11/15 09:48:13 lukem 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  * 3. All advertising materials mentioning features or use of this software
18  *    must display the following acknowledgement:
19  *        This product includes software developed by the NetBSD
20  *        Foundation, Inc. and its contributors.
21  * 4. Neither the name of The NetBSD Foundation nor the names of its
22  *    contributors may be used to endorse or promote products derived
23  *    from this software without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
26  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
29  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  * POSSIBILITY OF SUCH DAMAGE.
36  */
37 
38 /*
39  * Copyright (c) 1995 Carnegie-Mellon University.
40  * All rights reserved.
41  *
42  * Author: Mark Holland, Khalil Amiri, Claudson Bornstein, William V. Courtright II,
43  *         Robby Findler, Daniel Stodolsky, Rachad Youssef, Jim Zelenka
44  *
45  * Permission to use, copy, modify and distribute this software and
46  * its documentation is hereby granted, provided that both the copyright
47  * notice and this permission notice appear in all copies of the
48  * software, derivative works or modified versions, and any portions
49  * thereof, and that both notices appear in supporting documentation.
50  *
51  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
52  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
53  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
54  *
55  * Carnegie Mellon requests users of this software to return to
56  *
57  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
58  *  School of Computer Science
59  *  Carnegie Mellon University
60  *  Pittsburgh PA 15213-3890
61  *
62  * any improvements or extensions that they make and grant Carnegie the
63  * rights to redistribute these changes.
64  */
65 
66 /******************************************************************************
67  *
68  * rf_driver.c -- main setup, teardown, and access routines for the RAID driver
69  *
70  * all routines are prefixed with rf_ (raidframe), to avoid conficts.
71  *
72  ******************************************************************************/
73 
74 
75 #include <sys/cdefs.h>
76 __KERNEL_RCSID(0, "$NetBSD: rf_driver.c,v 1.45 2001/11/15 09:48:13 lukem Exp $");
77 
78 #include <sys/param.h>
79 #include <sys/systm.h>
80 #include <sys/ioctl.h>
81 #include <sys/fcntl.h>
82 #include <sys/vnode.h>
83 
84 
85 #include "rf_archs.h"
86 #include "rf_threadstuff.h"
87 
88 #include <sys/errno.h>
89 
90 #include "rf_raid.h"
91 #include "rf_dag.h"
92 #include "rf_aselect.h"
93 #include "rf_diskqueue.h"
94 #include "rf_parityscan.h"
95 #include "rf_alloclist.h"
96 #include "rf_dagutils.h"
97 #include "rf_utils.h"
98 #include "rf_etimer.h"
99 #include "rf_acctrace.h"
100 #include "rf_general.h"
101 #include "rf_desc.h"
102 #include "rf_states.h"
103 #include "rf_freelist.h"
104 #include "rf_decluster.h"
105 #include "rf_map.h"
106 #include "rf_revent.h"
107 #include "rf_callback.h"
108 #include "rf_engine.h"
109 #include "rf_memchunk.h"
110 #include "rf_mcpair.h"
111 #include "rf_nwayxor.h"
112 #include "rf_debugprint.h"
113 #include "rf_copyback.h"
114 #include "rf_driver.h"
115 #include "rf_options.h"
116 #include "rf_shutdown.h"
117 #include "rf_kintf.h"
118 
119 #include <sys/buf.h>
120 
121 /* rad == RF_RaidAccessDesc_t */
122 static RF_FreeList_t *rf_rad_freelist;
123 #define RF_MAX_FREE_RAD 128
124 #define RF_RAD_INC       16
125 #define RF_RAD_INITIAL   32
126 
127 /* debug variables */
128 char    rf_panicbuf[2048];	/* a buffer to hold an error msg when we panic */
129 
130 /* main configuration routines */
131 static int raidframe_booted = 0;
132 
133 static void rf_ConfigureDebug(RF_Config_t * cfgPtr);
134 static void set_debug_option(char *name, long val);
135 static void rf_UnconfigureArray(void);
136 static int init_rad(RF_RaidAccessDesc_t *);
137 static void clean_rad(RF_RaidAccessDesc_t *);
138 static void rf_ShutdownRDFreeList(void *);
139 static int rf_ConfigureRDFreeList(RF_ShutdownList_t **);
140 
141 RF_DECLARE_MUTEX(rf_printf_mutex)	/* debug only:  avoids interleaved
142 					 * printfs by different stripes */
143 
144 #define SIGNAL_QUIESCENT_COND(_raid_)  wakeup(&((_raid_)->accesses_suspended))
145 #define WAIT_FOR_QUIESCENCE(_raid_) \
146 	ltsleep(&((_raid_)->accesses_suspended), PRIBIO, \
147 		"raidframe quiesce", 0, &((_raid_)->access_suspend_mutex))
148 
149 #define IO_BUF_ERR(bp, err) { \
150 	bp->b_flags |= B_ERROR; \
151 	bp->b_resid = bp->b_bcount; \
152 	bp->b_error = err; \
153 	biodone(bp); \
154 }
155 
156 static int configureCount = 0;	/* number of active configurations */
157 static int isconfigged = 0;	/* is basic raidframe (non per-array)
158 				 * stuff configged */
159 RF_DECLARE_STATIC_MUTEX(configureMutex)	/* used to lock the configuration
160 					 * stuff */
161 static RF_ShutdownList_t *globalShutdown;	/* non array-specific
162 						 * stuff */
163 
164 static int rf_ConfigureRDFreeList(RF_ShutdownList_t ** listp);
165 
166 /* called at system boot time */
167 int
168 rf_BootRaidframe()
169 {
170 	int     rc;
171 
172 	if (raidframe_booted)
173 		return (EBUSY);
174 	raidframe_booted = 1;
175 
176 	rc = rf_mutex_init(&configureMutex);
177 	if (rc) {
178 		RF_ERRORMSG3("Unable to init mutex file %s line %d rc=%d\n", __FILE__,
179 		    __LINE__, rc);
180 		RF_PANIC();
181 	}
182 	configureCount = 0;
183 	isconfigged = 0;
184 	globalShutdown = NULL;
185 	return (0);
186 }
187 /*
188  * This function is really just for debugging user-level stuff: it
189  * frees up all memory, other RAIDframe resources which might otherwise
190  * be kept around. This is used with systems like "sentinel" to detect
191  * memory leaks.
192  */
193 int
194 rf_UnbootRaidframe()
195 {
196 	int     rc;
197 
198 	RF_LOCK_MUTEX(configureMutex);
199 	if (configureCount) {
200 		RF_UNLOCK_MUTEX(configureMutex);
201 		return (EBUSY);
202 	}
203 	raidframe_booted = 0;
204 	RF_UNLOCK_MUTEX(configureMutex);
205 	rc = rf_mutex_destroy(&configureMutex);
206 	if (rc) {
207 		RF_ERRORMSG3("Unable to destroy mutex file %s line %d rc=%d\n", __FILE__,
208 		    __LINE__, rc);
209 		RF_PANIC();
210 	}
211 	return (0);
212 }
213 /*
214  * Called whenever an array is shutdown
215  */
216 static void
217 rf_UnconfigureArray()
218 {
219 	int     rc;
220 
221 	RF_LOCK_MUTEX(configureMutex);
222 	if (--configureCount == 0) {	/* if no active configurations, shut
223 					 * everything down */
224 		isconfigged = 0;
225 
226 		rc = rf_ShutdownList(&globalShutdown);
227 		if (rc) {
228 			RF_ERRORMSG1("RAIDFRAME: unable to do global shutdown, rc=%d\n", rc);
229 		}
230 
231 		/*
232 	         * We must wait until now, because the AllocList module
233 	         * uses the DebugMem module.
234 	         */
235 		if (rf_memDebug)
236 			rf_print_unfreed();
237 	}
238 	RF_UNLOCK_MUTEX(configureMutex);
239 }
240 
241 /*
242  * Called to shut down an array.
243  */
244 int
245 rf_Shutdown(raidPtr)
246 	RF_Raid_t *raidPtr;
247 {
248 
249 	if (!raidPtr->valid) {
250 		RF_ERRORMSG("Attempt to shut down unconfigured RAIDframe driver.  Aborting shutdown\n");
251 		return (EINVAL);
252 	}
253 	/*
254          * wait for outstanding IOs to land
255          * As described in rf_raid.h, we use the rad_freelist lock
256          * to protect the per-array info about outstanding descs
257          * since we need to do freelist locking anyway, and this
258          * cuts down on the amount of serialization we've got going
259          * on.
260          */
261 	RF_FREELIST_DO_LOCK(rf_rad_freelist);
262 	if (raidPtr->waitShutdown) {
263 		RF_FREELIST_DO_UNLOCK(rf_rad_freelist);
264 		return (EBUSY);
265 	}
266 	raidPtr->waitShutdown = 1;
267 	while (raidPtr->nAccOutstanding) {
268 		RF_WAIT_COND(raidPtr->outstandingCond, RF_FREELIST_MUTEX_OF(rf_rad_freelist));
269 	}
270 	RF_FREELIST_DO_UNLOCK(rf_rad_freelist);
271 
272 	/* Wait for any parity re-writes to stop... */
273 	while (raidPtr->parity_rewrite_in_progress) {
274 		printf("Waiting for parity re-write to exit...\n");
275 		tsleep(&raidPtr->parity_rewrite_in_progress, PRIBIO,
276 		       "rfprwshutdown", 0);
277 	}
278 
279 	raidPtr->valid = 0;
280 
281 	rf_update_component_labels(raidPtr, RF_FINAL_COMPONENT_UPDATE);
282 
283 	rf_UnconfigureVnodes(raidPtr);
284 
285 	rf_ShutdownList(&raidPtr->shutdownList);
286 
287 	rf_UnconfigureArray();
288 
289 	return (0);
290 }
291 
292 
293 #define DO_INIT_CONFIGURE(f) { \
294 	rc = f (&globalShutdown); \
295 	if (rc) { \
296 		RF_ERRORMSG2("RAIDFRAME: failed %s with %d\n", RF_STRING(f), rc); \
297 		rf_ShutdownList(&globalShutdown); \
298 		configureCount--; \
299 		RF_UNLOCK_MUTEX(configureMutex); \
300 		return(rc); \
301 	} \
302 }
303 
304 #define DO_RAID_FAIL() { \
305 	rf_UnconfigureVnodes(raidPtr); \
306 	rf_ShutdownList(&raidPtr->shutdownList); \
307 	rf_UnconfigureArray(); \
308 }
309 
310 #define DO_RAID_INIT_CONFIGURE(f) { \
311 	rc = f (&raidPtr->shutdownList, raidPtr, cfgPtr); \
312 	if (rc) { \
313 		RF_ERRORMSG2("RAIDFRAME: failed %s with %d\n", RF_STRING(f), rc); \
314 		DO_RAID_FAIL(); \
315 		return(rc); \
316 	} \
317 }
318 
319 #define DO_RAID_MUTEX(_m_) { \
320 	rc = rf_create_managed_mutex(&raidPtr->shutdownList, (_m_)); \
321 	if (rc) { \
322 		RF_ERRORMSG3("Unable to init mutex file %s line %d rc=%d\n", \
323 			__FILE__, __LINE__, rc); \
324 		DO_RAID_FAIL(); \
325 		return(rc); \
326 	} \
327 }
328 
329 #define DO_RAID_COND(_c_) { \
330 	rc = rf_create_managed_cond(&raidPtr->shutdownList, (_c_)); \
331 	if (rc) { \
332 		RF_ERRORMSG3("Unable to init cond file %s line %d rc=%d\n", \
333 			__FILE__, __LINE__, rc); \
334 		DO_RAID_FAIL(); \
335 		return(rc); \
336 	} \
337 }
338 
339 int
340 rf_Configure(raidPtr, cfgPtr, ac)
341 	RF_Raid_t *raidPtr;
342 	RF_Config_t *cfgPtr;
343 	RF_AutoConfig_t *ac;
344 {
345 	RF_RowCol_t row, col;
346 	int     i, rc;
347 
348 	/* XXX This check can probably be removed now, since
349 	   RAIDFRAME_CONFIGURRE now checks to make sure that the
350 	   RAID set is not already valid
351 	*/
352 	if (raidPtr->valid) {
353 		RF_ERRORMSG("RAIDframe configuration not shut down.  Aborting configure.\n");
354 		return (EINVAL);
355 	}
356 	RF_LOCK_MUTEX(configureMutex);
357 	configureCount++;
358 	if (isconfigged == 0) {
359 		rc = rf_create_managed_mutex(&globalShutdown, &rf_printf_mutex);
360 		if (rc) {
361 			RF_ERRORMSG3("Unable to init mutex file %s line %d rc=%d\n", __FILE__,
362 			    __LINE__, rc);
363 			rf_ShutdownList(&globalShutdown);
364 			return (rc);
365 		}
366 		/* initialize globals */
367 		printf("RAIDFRAME: protectedSectors is %ld\n",
368 		       rf_protectedSectors);
369 
370 		rf_clear_debug_print_buffer();
371 
372 		DO_INIT_CONFIGURE(rf_ConfigureAllocList);
373 
374 		/*
375 	         * Yes, this does make debugging general to the whole
376 	         * system instead of being array specific. Bummer, drag.
377 		 */
378 		rf_ConfigureDebug(cfgPtr);
379 		DO_INIT_CONFIGURE(rf_ConfigureDebugMem);
380 		DO_INIT_CONFIGURE(rf_ConfigureAccessTrace);
381 		DO_INIT_CONFIGURE(rf_ConfigureMapModule);
382 		DO_INIT_CONFIGURE(rf_ConfigureReconEvent);
383 		DO_INIT_CONFIGURE(rf_ConfigureCallback);
384 		DO_INIT_CONFIGURE(rf_ConfigureMemChunk);
385 		DO_INIT_CONFIGURE(rf_ConfigureRDFreeList);
386 		DO_INIT_CONFIGURE(rf_ConfigureNWayXor);
387 		DO_INIT_CONFIGURE(rf_ConfigureStripeLockFreeList);
388 		DO_INIT_CONFIGURE(rf_ConfigureMCPair);
389 		DO_INIT_CONFIGURE(rf_ConfigureDAGs);
390 		DO_INIT_CONFIGURE(rf_ConfigureDAGFuncs);
391 		DO_INIT_CONFIGURE(rf_ConfigureDebugPrint);
392 		DO_INIT_CONFIGURE(rf_ConfigureReconstruction);
393 		DO_INIT_CONFIGURE(rf_ConfigureCopyback);
394 		DO_INIT_CONFIGURE(rf_ConfigureDiskQueueSystem);
395 		isconfigged = 1;
396 	}
397 	RF_UNLOCK_MUTEX(configureMutex);
398 
399 	DO_RAID_MUTEX(&raidPtr->mutex);
400 	/* set up the cleanup list.  Do this after ConfigureDebug so that
401 	 * value of memDebug will be set */
402 
403 	rf_MakeAllocList(raidPtr->cleanupList);
404 	if (raidPtr->cleanupList == NULL) {
405 		DO_RAID_FAIL();
406 		return (ENOMEM);
407 	}
408 	rc = rf_ShutdownCreate(&raidPtr->shutdownList,
409 	    (void (*) (void *)) rf_FreeAllocList,
410 	    raidPtr->cleanupList);
411 	if (rc) {
412 		RF_ERRORMSG3("Unable to add to shutdown list file %s line %d rc=%d\n",
413 		    __FILE__, __LINE__, rc);
414 		DO_RAID_FAIL();
415 		return (rc);
416 	}
417 	raidPtr->numRow = cfgPtr->numRow;
418 	raidPtr->numCol = cfgPtr->numCol;
419 	raidPtr->numSpare = cfgPtr->numSpare;
420 
421 	/* XXX we don't even pretend to support more than one row in the
422 	 * kernel... */
423 	if (raidPtr->numRow != 1) {
424 		RF_ERRORMSG("Only one row supported in kernel.\n");
425 		DO_RAID_FAIL();
426 		return (EINVAL);
427 	}
428 	RF_CallocAndAdd(raidPtr->status, raidPtr->numRow, sizeof(RF_RowStatus_t),
429 	    (RF_RowStatus_t *), raidPtr->cleanupList);
430 	if (raidPtr->status == NULL) {
431 		DO_RAID_FAIL();
432 		return (ENOMEM);
433 	}
434 	RF_CallocAndAdd(raidPtr->reconControl, raidPtr->numRow,
435 	    sizeof(RF_ReconCtrl_t *), (RF_ReconCtrl_t **), raidPtr->cleanupList);
436 	if (raidPtr->reconControl == NULL) {
437 		DO_RAID_FAIL();
438 		return (ENOMEM);
439 	}
440 	for (i = 0; i < raidPtr->numRow; i++) {
441 		raidPtr->status[i] = rf_rs_optimal;
442 		raidPtr->reconControl[i] = NULL;
443 	}
444 
445 	DO_RAID_INIT_CONFIGURE(rf_ConfigureEngine);
446 	DO_RAID_INIT_CONFIGURE(rf_ConfigureStripeLocks);
447 
448 	DO_RAID_COND(&raidPtr->outstandingCond);
449 
450 	raidPtr->nAccOutstanding = 0;
451 	raidPtr->waitShutdown = 0;
452 
453 	DO_RAID_MUTEX(&raidPtr->access_suspend_mutex);
454 	DO_RAID_COND(&raidPtr->quiescent_cond);
455 
456 	DO_RAID_COND(&raidPtr->waitForReconCond);
457 
458 	DO_RAID_MUTEX(&raidPtr->recon_done_proc_mutex);
459 
460 	if (ac!=NULL) {
461 		/* We have an AutoConfig structure..  Don't do the
462 		   normal disk configuration... call the auto config
463 		   stuff */
464 		rf_AutoConfigureDisks(raidPtr, cfgPtr, ac);
465 	} else {
466 		DO_RAID_INIT_CONFIGURE(rf_ConfigureDisks);
467 		DO_RAID_INIT_CONFIGURE(rf_ConfigureSpareDisks);
468 	}
469 	/* do this after ConfigureDisks & ConfigureSpareDisks to be sure dev
470 	 * no. is set */
471 	DO_RAID_INIT_CONFIGURE(rf_ConfigureDiskQueues);
472 
473 	DO_RAID_INIT_CONFIGURE(rf_ConfigureLayout);
474 
475 	DO_RAID_INIT_CONFIGURE(rf_ConfigurePSStatus);
476 
477 	for (row = 0; row < raidPtr->numRow; row++) {
478 		for (col = 0; col < raidPtr->numCol; col++) {
479 			/*
480 		         * XXX better distribution
481 		         */
482 			raidPtr->hist_diskreq[row][col] = 0;
483 		}
484 	}
485 
486 	raidPtr->numNewFailures = 0;
487 	raidPtr->copyback_in_progress = 0;
488 	raidPtr->parity_rewrite_in_progress = 0;
489 	raidPtr->recon_in_progress = 0;
490 	raidPtr->maxOutstanding = cfgPtr->maxOutstandingDiskReqs;
491 
492 	/* autoconfigure and root_partition will actually get filled in
493 	   after the config is done */
494 	raidPtr->autoconfigure = 0;
495 	raidPtr->root_partition = 0;
496 	raidPtr->last_unit = raidPtr->raidid;
497 	raidPtr->config_order = 0;
498 
499 	if (rf_keepAccTotals) {
500 		raidPtr->keep_acc_totals = 1;
501 	}
502 	rf_StartUserStats(raidPtr);
503 
504 	raidPtr->valid = 1;
505 	return (0);
506 }
507 
508 static int
509 init_rad(desc)
510 	RF_RaidAccessDesc_t *desc;
511 {
512 	int     rc;
513 
514 	rc = rf_mutex_init(&desc->mutex);
515 	if (rc) {
516 		RF_ERRORMSG3("Unable to init mutex file %s line %d rc=%d\n", __FILE__,
517 		    __LINE__, rc);
518 		return (rc);
519 	}
520 	rc = rf_cond_init(&desc->cond);
521 	if (rc) {
522 		RF_ERRORMSG3("Unable to init cond file %s line %d rc=%d\n", __FILE__,
523 		    __LINE__, rc);
524 		rf_mutex_destroy(&desc->mutex);
525 		return (rc);
526 	}
527 	return (0);
528 }
529 
530 static void
531 clean_rad(desc)
532 	RF_RaidAccessDesc_t *desc;
533 {
534 	rf_mutex_destroy(&desc->mutex);
535 	rf_cond_destroy(&desc->cond);
536 }
537 
538 static void
539 rf_ShutdownRDFreeList(ignored)
540 	void   *ignored;
541 {
542 	RF_FREELIST_DESTROY_CLEAN(rf_rad_freelist, next, (RF_RaidAccessDesc_t *), clean_rad);
543 }
544 
545 static int
546 rf_ConfigureRDFreeList(listp)
547 	RF_ShutdownList_t **listp;
548 {
549 	int     rc;
550 
551 	RF_FREELIST_CREATE(rf_rad_freelist, RF_MAX_FREE_RAD,
552 	    RF_RAD_INC, sizeof(RF_RaidAccessDesc_t));
553 	if (rf_rad_freelist == NULL) {
554 		return (ENOMEM);
555 	}
556 	rc = rf_ShutdownCreate(listp, rf_ShutdownRDFreeList, NULL);
557 	if (rc) {
558 		RF_ERRORMSG3("Unable to add to shutdown list file %s line %d rc=%d\n", __FILE__,
559 		    __LINE__, rc);
560 		rf_ShutdownRDFreeList(NULL);
561 		return (rc);
562 	}
563 	RF_FREELIST_PRIME_INIT(rf_rad_freelist, RF_RAD_INITIAL, next,
564 	    (RF_RaidAccessDesc_t *), init_rad);
565 	return (0);
566 }
567 
568 RF_RaidAccessDesc_t *
569 rf_AllocRaidAccDesc(
570     RF_Raid_t * raidPtr,
571     RF_IoType_t type,
572     RF_RaidAddr_t raidAddress,
573     RF_SectorCount_t numBlocks,
574     caddr_t bufPtr,
575     void *bp,
576     RF_RaidAccessFlags_t flags,
577     RF_AccessState_t * states)
578 {
579 	RF_RaidAccessDesc_t *desc;
580 
581 	RF_FREELIST_GET_INIT_NOUNLOCK(rf_rad_freelist, desc, next, (RF_RaidAccessDesc_t *), init_rad);
582 	if (raidPtr->waitShutdown) {
583 		/*
584 	         * Actually, we're shutting the array down. Free the desc
585 	         * and return NULL.
586 	         */
587 		RF_FREELIST_DO_UNLOCK(rf_rad_freelist);
588 		RF_FREELIST_FREE_CLEAN(rf_rad_freelist, desc, next, clean_rad);
589 		return (NULL);
590 	}
591 	raidPtr->nAccOutstanding++;
592 	RF_FREELIST_DO_UNLOCK(rf_rad_freelist);
593 
594 	desc->raidPtr = (void *) raidPtr;
595 	desc->type = type;
596 	desc->raidAddress = raidAddress;
597 	desc->numBlocks = numBlocks;
598 	desc->bufPtr = bufPtr;
599 	desc->bp = bp;
600 	desc->paramDAG = NULL;
601 	desc->paramASM = NULL;
602 	desc->flags = flags;
603 	desc->states = states;
604 	desc->state = 0;
605 
606 	desc->status = 0;
607 	memset((char *) &desc->tracerec, 0, sizeof(RF_AccTraceEntry_t));
608 	desc->callbackFunc = NULL;
609 	desc->callbackArg = NULL;
610 	desc->next = NULL;
611 	desc->head = desc;
612 	desc->numPending = 0;
613 	desc->cleanupList = NULL;
614 	rf_MakeAllocList(desc->cleanupList);
615 	return (desc);
616 }
617 
618 void
619 rf_FreeRaidAccDesc(RF_RaidAccessDesc_t * desc)
620 {
621 	RF_Raid_t *raidPtr = desc->raidPtr;
622 
623 	RF_ASSERT(desc);
624 
625 	rf_FreeAllocList(desc->cleanupList);
626 	RF_FREELIST_FREE_CLEAN_NOUNLOCK(rf_rad_freelist, desc, next, clean_rad);
627 	raidPtr->nAccOutstanding--;
628 	if (raidPtr->waitShutdown) {
629 		RF_SIGNAL_COND(raidPtr->outstandingCond);
630 	}
631 	RF_FREELIST_DO_UNLOCK(rf_rad_freelist);
632 }
633 /*********************************************************************
634  * Main routine for performing an access.
635  * Accesses are retried until a DAG can not be selected.  This occurs
636  * when either the DAG library is incomplete or there are too many
637  * failures in a parity group.
638  ********************************************************************/
639 int
640 rf_DoAccess(
641     RF_Raid_t * raidPtr,
642     RF_IoType_t type,
643     int async_flag,
644     RF_RaidAddr_t raidAddress,
645     RF_SectorCount_t numBlocks,
646     caddr_t bufPtr,
647     void *bp_in,
648     RF_RaidAccessFlags_t flags)
649 /*
650 type should be read or write
651 async_flag should be RF_TRUE or RF_FALSE
652 bp_in is a buf pointer.  void * to facilitate ignoring it outside the kernel
653 */
654 {
655 	RF_RaidAccessDesc_t *desc;
656 	caddr_t lbufPtr = bufPtr;
657 	struct buf *bp = (struct buf *) bp_in;
658 
659 	raidAddress += rf_raidSectorOffset;
660 
661 	if (!raidPtr->valid) {
662 		RF_ERRORMSG("RAIDframe driver not successfully configured.  Rejecting access.\n");
663 		IO_BUF_ERR(bp, EINVAL);
664 		return (EINVAL);
665 	}
666 
667 	if (rf_accessDebug) {
668 
669 		printf("logBytes is: %d %d %d\n", raidPtr->raidid,
670 		    raidPtr->logBytesPerSector,
671 		    (int) rf_RaidAddressToByte(raidPtr, numBlocks));
672 		printf("raid%d: %s raidAddr %d (stripeid %d-%d) numBlocks %d (%d bytes) buf 0x%lx\n", raidPtr->raidid,
673 		    (type == RF_IO_TYPE_READ) ? "READ" : "WRITE", (int) raidAddress,
674 		    (int) rf_RaidAddressToStripeID(&raidPtr->Layout, raidAddress),
675 		    (int) rf_RaidAddressToStripeID(&raidPtr->Layout, raidAddress + numBlocks - 1),
676 		    (int) numBlocks,
677 		    (int) rf_RaidAddressToByte(raidPtr, numBlocks),
678 		    (long) bufPtr);
679 	}
680 	if (raidAddress + numBlocks > raidPtr->totalSectors) {
681 
682 		printf("DoAccess: raid addr %lu too large to access %lu sectors.  Max legal addr is %lu\n",
683 		    (u_long) raidAddress, (u_long) numBlocks, (u_long) raidPtr->totalSectors);
684 
685 		IO_BUF_ERR(bp, ENOSPC);
686 		return (ENOSPC);
687 	}
688 	desc = rf_AllocRaidAccDesc(raidPtr, type, raidAddress,
689 	    numBlocks, lbufPtr, bp, flags, raidPtr->Layout.map->states);
690 
691 	if (desc == NULL) {
692 		return (ENOMEM);
693 	}
694 	RF_ETIMER_START(desc->tracerec.tot_timer);
695 
696 	desc->async_flag = async_flag;
697 
698 	rf_ContinueRaidAccess(desc);
699 
700 	return (0);
701 }
702 /* force the array into reconfigured mode without doing reconstruction */
703 int
704 rf_SetReconfiguredMode(raidPtr, row, col)
705 	RF_Raid_t *raidPtr;
706 	int     row;
707 	int     col;
708 {
709 	if (!(raidPtr->Layout.map->flags & RF_DISTRIBUTE_SPARE)) {
710 		printf("Can't set reconfigured mode in dedicated-spare array\n");
711 		RF_PANIC();
712 	}
713 	RF_LOCK_MUTEX(raidPtr->mutex);
714 	raidPtr->numFailures++;
715 	raidPtr->Disks[row][col].status = rf_ds_dist_spared;
716 	raidPtr->status[row] = rf_rs_reconfigured;
717 	rf_update_component_labels(raidPtr, RF_NORMAL_COMPONENT_UPDATE);
718 	/* install spare table only if declustering + distributed sparing
719 	 * architecture. */
720 	if (raidPtr->Layout.map->flags & RF_BD_DECLUSTERED)
721 		rf_InstallSpareTable(raidPtr, row, col);
722 	RF_UNLOCK_MUTEX(raidPtr->mutex);
723 	return (0);
724 }
725 
726 int
727 rf_FailDisk(
728     RF_Raid_t * raidPtr,
729     int frow,
730     int fcol,
731     int initRecon)
732 {
733 	printf("raid%d: Failing disk r%d c%d\n", raidPtr->raidid, frow, fcol);
734 	RF_LOCK_MUTEX(raidPtr->mutex);
735 	raidPtr->numFailures++;
736 	raidPtr->Disks[frow][fcol].status = rf_ds_failed;
737 	raidPtr->status[frow] = rf_rs_degraded;
738 	rf_update_component_labels(raidPtr, RF_NORMAL_COMPONENT_UPDATE);
739 	RF_UNLOCK_MUTEX(raidPtr->mutex);
740 	if (initRecon)
741 		rf_ReconstructFailedDisk(raidPtr, frow, fcol);
742 	return (0);
743 }
744 /* releases a thread that is waiting for the array to become quiesced.
745  * access_suspend_mutex should be locked upon calling this
746  */
747 void
748 rf_SignalQuiescenceLock(raidPtr, reconDesc)
749 	RF_Raid_t *raidPtr;
750 	RF_RaidReconDesc_t *reconDesc;
751 {
752 	if (rf_quiesceDebug) {
753 		printf("raid%d: Signalling quiescence lock\n",
754 		       raidPtr->raidid);
755 	}
756 	raidPtr->access_suspend_release = 1;
757 
758 	if (raidPtr->waiting_for_quiescence) {
759 		SIGNAL_QUIESCENT_COND(raidPtr);
760 	}
761 }
762 /* suspends all new requests to the array.  No effect on accesses that are in flight.  */
763 int
764 rf_SuspendNewRequestsAndWait(raidPtr)
765 	RF_Raid_t *raidPtr;
766 {
767 	if (rf_quiesceDebug)
768 		printf("Suspending new reqs\n");
769 
770 	RF_LOCK_MUTEX(raidPtr->access_suspend_mutex);
771 	raidPtr->accesses_suspended++;
772 	raidPtr->waiting_for_quiescence = (raidPtr->accs_in_flight == 0) ? 0 : 1;
773 
774 	if (raidPtr->waiting_for_quiescence) {
775 		raidPtr->access_suspend_release = 0;
776 		while (!raidPtr->access_suspend_release) {
777 			printf("Suspending: Waiting for Quiescence\n");
778 			WAIT_FOR_QUIESCENCE(raidPtr);
779 			raidPtr->waiting_for_quiescence = 0;
780 		}
781 	}
782 	printf("Quiescence reached..\n");
783 
784 	RF_UNLOCK_MUTEX(raidPtr->access_suspend_mutex);
785 	return (raidPtr->waiting_for_quiescence);
786 }
787 /* wake up everyone waiting for quiescence to be released */
788 void
789 rf_ResumeNewRequests(raidPtr)
790 	RF_Raid_t *raidPtr;
791 {
792 	RF_CallbackDesc_t *t, *cb;
793 
794 	if (rf_quiesceDebug)
795 		printf("Resuming new reqs\n");
796 
797 	RF_LOCK_MUTEX(raidPtr->access_suspend_mutex);
798 	raidPtr->accesses_suspended--;
799 	if (raidPtr->accesses_suspended == 0)
800 		cb = raidPtr->quiesce_wait_list;
801 	else
802 		cb = NULL;
803 	raidPtr->quiesce_wait_list = NULL;
804 	RF_UNLOCK_MUTEX(raidPtr->access_suspend_mutex);
805 
806 	while (cb) {
807 		t = cb;
808 		cb = cb->next;
809 		(t->callbackFunc) (t->callbackArg);
810 		rf_FreeCallbackDesc(t);
811 	}
812 }
813 /*****************************************************************************************
814  *
815  * debug routines
816  *
817  ****************************************************************************************/
818 
819 static void
820 set_debug_option(name, val)
821 	char   *name;
822 	long    val;
823 {
824 	RF_DebugName_t *p;
825 
826 	for (p = rf_debugNames; p->name; p++) {
827 		if (!strcmp(p->name, name)) {
828 			*(p->ptr) = val;
829 			printf("[Set debug variable %s to %ld]\n", name, val);
830 			return;
831 		}
832 	}
833 	RF_ERRORMSG1("Unknown debug string \"%s\"\n", name);
834 }
835 
836 
837 /* would like to use sscanf here, but apparently not available in kernel */
838 /*ARGSUSED*/
839 static void
840 rf_ConfigureDebug(cfgPtr)
841 	RF_Config_t *cfgPtr;
842 {
843 	char   *val_p, *name_p, *white_p;
844 	long    val;
845 	int     i;
846 
847 	rf_ResetDebugOptions();
848 	for (i = 0; cfgPtr->debugVars[i][0] && i < RF_MAXDBGV; i++) {
849 		name_p = rf_find_non_white(&cfgPtr->debugVars[i][0]);
850 		white_p = rf_find_white(name_p);	/* skip to start of 2nd
851 							 * word */
852 		val_p = rf_find_non_white(white_p);
853 		if (*val_p == '0' && *(val_p + 1) == 'x')
854 			val = rf_htoi(val_p + 2);
855 		else
856 			val = rf_atoi(val_p);
857 		*white_p = '\0';
858 		set_debug_option(name_p, val);
859 	}
860 }
861 /* performance monitoring stuff */
862 
863 #define TIMEVAL_TO_US(t) (((long) t.tv_sec) * 1000000L + (long) t.tv_usec)
864 
865 #if !defined(_KERNEL) && !defined(SIMULATE)
866 
867 /*
868  * Throughput stats currently only used in user-level RAIDframe
869  */
870 
871 static int
872 rf_InitThroughputStats(
873     RF_ShutdownList_t ** listp,
874     RF_Raid_t * raidPtr,
875     RF_Config_t * cfgPtr)
876 {
877 	int     rc;
878 
879 	/* these used by user-level raidframe only */
880 	rc = rf_create_managed_mutex(listp, &raidPtr->throughputstats.mutex);
881 	if (rc) {
882 		RF_ERRORMSG3("Unable to init mutex file %s line %d rc=%d\n", __FILE__,
883 		    __LINE__, rc);
884 		return (rc);
885 	}
886 	raidPtr->throughputstats.sum_io_us = 0;
887 	raidPtr->throughputstats.num_ios = 0;
888 	raidPtr->throughputstats.num_out_ios = 0;
889 	return (0);
890 }
891 
892 void
893 rf_StartThroughputStats(RF_Raid_t * raidPtr)
894 {
895 	RF_LOCK_MUTEX(raidPtr->throughputstats.mutex);
896 	raidPtr->throughputstats.num_ios++;
897 	raidPtr->throughputstats.num_out_ios++;
898 	if (raidPtr->throughputstats.num_out_ios == 1)
899 		RF_GETTIME(raidPtr->throughputstats.start);
900 	RF_UNLOCK_MUTEX(raidPtr->throughputstats.mutex);
901 }
902 
903 static void
904 rf_StopThroughputStats(RF_Raid_t * raidPtr)
905 {
906 	struct timeval diff;
907 
908 	RF_LOCK_MUTEX(raidPtr->throughputstats.mutex);
909 	raidPtr->throughputstats.num_out_ios--;
910 	if (raidPtr->throughputstats.num_out_ios == 0) {
911 		RF_GETTIME(raidPtr->throughputstats.stop);
912 		RF_TIMEVAL_DIFF(&raidPtr->throughputstats.start, &raidPtr->throughputstats.stop, &diff);
913 		raidPtr->throughputstats.sum_io_us += TIMEVAL_TO_US(diff);
914 	}
915 	RF_UNLOCK_MUTEX(raidPtr->throughputstats.mutex);
916 }
917 
918 static void
919 rf_PrintThroughputStats(RF_Raid_t * raidPtr)
920 {
921 	RF_ASSERT(raidPtr->throughputstats.num_out_ios == 0);
922 	if (raidPtr->throughputstats.sum_io_us != 0) {
923 		printf("[Througphut: %8.2f IOs/second]\n", raidPtr->throughputstats.num_ios
924 		    / (raidPtr->throughputstats.sum_io_us / 1000000.0));
925 	}
926 }
927 #endif				/* !KERNEL && !SIMULATE */
928 
929 void
930 rf_StartUserStats(RF_Raid_t * raidPtr)
931 {
932 	RF_GETTIME(raidPtr->userstats.start);
933 	raidPtr->userstats.sum_io_us = 0;
934 	raidPtr->userstats.num_ios = 0;
935 	raidPtr->userstats.num_sect_moved = 0;
936 }
937 
938 void
939 rf_StopUserStats(RF_Raid_t * raidPtr)
940 {
941 	RF_GETTIME(raidPtr->userstats.stop);
942 }
943 
944 void
945 rf_UpdateUserStats(raidPtr, rt, numsect)
946 	RF_Raid_t *raidPtr;
947 	int     rt;		/* resp time in us */
948 	int     numsect;	/* number of sectors for this access */
949 {
950 	raidPtr->userstats.sum_io_us += rt;
951 	raidPtr->userstats.num_ios++;
952 	raidPtr->userstats.num_sect_moved += numsect;
953 }
954 
955 void
956 rf_PrintUserStats(RF_Raid_t * raidPtr)
957 {
958 	long    elapsed_us, mbs, mbs_frac;
959 	struct timeval diff;
960 
961 	RF_TIMEVAL_DIFF(&raidPtr->userstats.start, &raidPtr->userstats.stop, &diff);
962 	elapsed_us = TIMEVAL_TO_US(diff);
963 
964 	/* 2000 sectors per megabyte, 10000000 microseconds per second */
965 	if (elapsed_us)
966 		mbs = (raidPtr->userstats.num_sect_moved / 2000) / (elapsed_us / 1000000);
967 	else
968 		mbs = 0;
969 
970 	/* this computes only the first digit of the fractional mb/s moved */
971 	if (elapsed_us) {
972 		mbs_frac = ((raidPtr->userstats.num_sect_moved / 200) / (elapsed_us / 1000000))
973 		    - (mbs * 10);
974 	} else {
975 		mbs_frac = 0;
976 	}
977 
978 	printf("Number of I/Os:             %ld\n", raidPtr->userstats.num_ios);
979 	printf("Elapsed time (us):          %ld\n", elapsed_us);
980 	printf("User I/Os per second:       %ld\n", RF_DB0_CHECK(raidPtr->userstats.num_ios, (elapsed_us / 1000000)));
981 	printf("Average user response time: %ld us\n", RF_DB0_CHECK(raidPtr->userstats.sum_io_us, raidPtr->userstats.num_ios));
982 	printf("Total sectors moved:        %ld\n", raidPtr->userstats.num_sect_moved);
983 	printf("Average access size (sect): %ld\n", RF_DB0_CHECK(raidPtr->userstats.num_sect_moved, raidPtr->userstats.num_ios));
984 	printf("Achieved data rate:         %ld.%ld MB/sec\n", mbs, mbs_frac);
985 }
986 
987 
988 void
989 rf_print_panic_message(line,file)
990 	int line;
991 	char *file;
992 {
993 	sprintf(rf_panicbuf,"raidframe error at line %d file %s",
994 		line, file);
995 }
996 
997 void
998 rf_print_assert_panic_message(line,file,condition)
999 	int line;
1000 	char *file;
1001 	char *condition;
1002 {
1003 	sprintf(rf_panicbuf,
1004 		"raidframe error at line %d file %s (failed asserting %s)\n",
1005 		line, file, condition);
1006 }
1007