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