Home | History | Annotate | Line # | Download | only in raidframe
rf_revent.c revision 1.4
      1 /*	$NetBSD: rf_revent.c,v 1.4 1999/03/14 21:53:31 oster Exp $	*/
      2 /*
      3  * Copyright (c) 1995 Carnegie-Mellon University.
      4  * All rights reserved.
      5  *
      6  * Author:
      7  *
      8  * Permission to use, copy, modify and distribute this software and
      9  * its documentation is hereby granted, provided that both the copyright
     10  * notice and this permission notice appear in all copies of the
     11  * software, derivative works or modified versions, and any portions
     12  * thereof, and that both notices appear in supporting documentation.
     13  *
     14  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
     15  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
     16  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
     17  *
     18  * Carnegie Mellon requests users of this software to return to
     19  *
     20  *  Software Distribution Coordinator  or  Software.Distribution (at) CS.CMU.EDU
     21  *  School of Computer Science
     22  *  Carnegie Mellon University
     23  *  Pittsburgh PA 15213-3890
     24  *
     25  * any improvements or extensions that they make and grant Carnegie the
     26  * rights to redistribute these changes.
     27  */
     28 /*
     29  * revent.c -- reconstruction event handling code
     30  */
     31 
     32 #include <sys/errno.h>
     33 
     34 #include "rf_raid.h"
     35 #include "rf_revent.h"
     36 #include "rf_etimer.h"
     37 #include "rf_general.h"
     38 #include "rf_freelist.h"
     39 #include "rf_desc.h"
     40 #include "rf_shutdown.h"
     41 
     42 static RF_FreeList_t *rf_revent_freelist;
     43 #define RF_MAX_FREE_REVENT 128
     44 #define RF_REVENT_INC        8
     45 #define RF_REVENT_INITIAL    8
     46 
     47 
     48 
     49 #include <sys/proc.h>
     50 
     51 extern int hz;
     52 
     53 #define DO_WAIT(_rc)   tsleep(&(_rc)->eventQueue, PRIBIO, "raidframe eventq", 0)
     54 
     55 #define DO_SIGNAL(_rc)     wakeup(&(_rc)->eventQueue)
     56 
     57 
     58 static void rf_ShutdownReconEvent(void *);
     59 
     60 static RF_ReconEvent_t *
     61 GetReconEventDesc(RF_RowCol_t row, RF_RowCol_t col,
     62     void *arg, RF_Revent_t type);
     63 RF_ReconEvent_t *
     64 rf_GetNextReconEvent(RF_RaidReconDesc_t *,
     65     RF_RowCol_t, void (*continueFunc) (void *),
     66     void *);
     67 
     68 	static void rf_ShutdownReconEvent(ignored)
     69 	void   *ignored;
     70 {
     71 	RF_FREELIST_DESTROY(rf_revent_freelist, next, (RF_ReconEvent_t *));
     72 }
     73 
     74 int
     75 rf_ConfigureReconEvent(listp)
     76 	RF_ShutdownList_t **listp;
     77 {
     78 	int     rc;
     79 
     80 	RF_FREELIST_CREATE(rf_revent_freelist, RF_MAX_FREE_REVENT,
     81 	    RF_REVENT_INC, sizeof(RF_ReconEvent_t));
     82 	if (rf_revent_freelist == NULL)
     83 		return (ENOMEM);
     84 	rc = rf_ShutdownCreate(listp, rf_ShutdownReconEvent, NULL);
     85 	if (rc) {
     86 		RF_ERRORMSG3("Unable to add to shutdown list file %s line %d rc=%d\n", __FILE__,
     87 		    __LINE__, rc);
     88 		rf_ShutdownReconEvent(NULL);
     89 		return (rc);
     90 	}
     91 	RF_FREELIST_PRIME(rf_revent_freelist, RF_REVENT_INITIAL, next,
     92 	    (RF_ReconEvent_t *));
     93 	return (0);
     94 }
     95 /* returns the next reconstruction event, blocking the calling thread until
     96  * one becomes available
     97  */
     98 
     99 /* will now return null if it is blocked or will return an event if it is not */
    100 
    101 RF_ReconEvent_t *
    102 rf_GetNextReconEvent(reconDesc, row, continueFunc, continueArg)
    103 	RF_RaidReconDesc_t *reconDesc;
    104 	RF_RowCol_t row;
    105 	void    (*continueFunc) (void *);
    106 	void   *continueArg;
    107 {
    108 	RF_Raid_t *raidPtr = reconDesc->raidPtr;
    109 	RF_ReconCtrl_t *rctrl = raidPtr->reconControl[row];
    110 	RF_ReconEvent_t *event;
    111 
    112 	RF_ASSERT(row >= 0 && row <= raidPtr->numRow);
    113 	RF_LOCK_MUTEX(rctrl->eq_mutex);
    114 	RF_ASSERT((rctrl->eventQueue == NULL) == (rctrl->eq_count == 0));	/* q null and count==0
    115 										 * must be equivalent
    116 										 * conditions */
    117 
    118 
    119 	rctrl->continueFunc = continueFunc;
    120 	rctrl->continueArg = continueArg;
    121 
    122 
    123 /* mpsleep timeout value: secs = timo_val/hz.  'ticks' here is defined as cycle-counter ticks, not softclock ticks */
    124 #define MAX_RECON_EXEC_TICKS 15000000	/* 150 Mhz => this many ticks in 100
    125 					 * ms */
    126 #define RECON_DELAY_MS 25
    127 #define RECON_TIMO     ((RECON_DELAY_MS * hz) / 1000)
    128 
    129 	/* we are not pre-emptible in the kernel, but we don't want to run
    130 	 * forever.  If we run w/o blocking for more than MAX_RECON_EXEC_TICKS
    131 	 * ticks of the cycle counter, delay for RECON_DELAY before
    132 	 * continuing. this may murder us with context switches, so we may
    133 	 * need to increase both the MAX...TICKS and the RECON_DELAY_MS. */
    134 	if (reconDesc->reconExecTimerRunning) {
    135 		int     status;
    136 
    137 		RF_ETIMER_STOP(reconDesc->recon_exec_timer);
    138 		RF_ETIMER_EVAL(reconDesc->recon_exec_timer);
    139 		reconDesc->reconExecTicks += RF_ETIMER_VAL_TICKS(reconDesc->recon_exec_timer);
    140 		if (reconDesc->reconExecTicks > reconDesc->maxReconExecTicks)
    141 			reconDesc->maxReconExecTicks = reconDesc->reconExecTicks;
    142 		if (reconDesc->reconExecTicks >= MAX_RECON_EXEC_TICKS) {
    143 			/* we've been running too long.  delay for
    144 			 * RECON_DELAY_MS */
    145 #if RF_RECON_STATS > 0
    146 			reconDesc->numReconExecDelays++;
    147 #endif				/* RF_RECON_STATS > 0 */
    148 			status = tsleep(&reconDesc->reconExecTicks, PRIBIO, "recon delay", RECON_TIMO);
    149 			RF_ASSERT(status == EWOULDBLOCK);
    150 			reconDesc->reconExecTicks = 0;
    151 		}
    152 	}
    153 	while (!rctrl->eventQueue) {
    154 #if RF_RECON_STATS > 0
    155 		reconDesc->numReconEventWaits++;
    156 #endif				/* RF_RECON_STATS > 0 */
    157 		DO_WAIT(rctrl);
    158 		reconDesc->reconExecTicks = 0;	/* we've just waited */
    159 	}
    160 
    161 	reconDesc->reconExecTimerRunning = 1;
    162 	RF_ETIMER_START(reconDesc->recon_exec_timer);
    163 
    164 	event = rctrl->eventQueue;
    165 	rctrl->eventQueue = event->next;
    166 	event->next = NULL;
    167 	rctrl->eq_count--;
    168 	RF_ASSERT((rctrl->eventQueue == NULL) == (rctrl->eq_count == 0));	/* q null and count==0
    169 										 * must be equivalent
    170 										 * conditions */
    171 	RF_UNLOCK_MUTEX(rctrl->eq_mutex);
    172 	return (event);
    173 }
    174 /* enqueues a reconstruction event on the indicated queue */
    175 void
    176 rf_CauseReconEvent(raidPtr, row, col, arg, type)
    177 	RF_Raid_t *raidPtr;
    178 	RF_RowCol_t row;
    179 	RF_RowCol_t col;
    180 	void   *arg;
    181 	RF_Revent_t type;
    182 {
    183 	RF_ReconCtrl_t *rctrl = raidPtr->reconControl[row];
    184 	RF_ReconEvent_t *event = GetReconEventDesc(row, col, arg, type);
    185 
    186 	if (type == RF_REVENT_BUFCLEAR) {
    187 		RF_ASSERT(col != rctrl->fcol);
    188 	}
    189 	RF_ASSERT(row >= 0 && row <= raidPtr->numRow && col >= 0 && col <= raidPtr->numCol);
    190 	RF_LOCK_MUTEX(rctrl->eq_mutex);
    191 	RF_ASSERT((rctrl->eventQueue == NULL) == (rctrl->eq_count == 0));	/* q null and count==0
    192 										 * must be equivalent
    193 										 * conditions */
    194 	event->next = rctrl->eventQueue;
    195 	rctrl->eventQueue = event;
    196 	rctrl->eq_count++;
    197 	RF_UNLOCK_MUTEX(rctrl->eq_mutex);
    198 
    199 	DO_SIGNAL(rctrl);
    200 }
    201 /* allocates and initializes a recon event descriptor */
    202 static RF_ReconEvent_t *
    203 GetReconEventDesc(row, col, arg, type)
    204 	RF_RowCol_t row;
    205 	RF_RowCol_t col;
    206 	void   *arg;
    207 	RF_Revent_t type;
    208 {
    209 	RF_ReconEvent_t *t;
    210 
    211 	RF_FREELIST_GET(rf_revent_freelist, t, next, (RF_ReconEvent_t *));
    212 	if (t == NULL)
    213 		return (NULL);
    214 	t->col = col;
    215 	t->arg = arg;
    216 	t->type = type;
    217 	return (t);
    218 }
    219 
    220 void
    221 rf_FreeReconEventDesc(event)
    222 	RF_ReconEvent_t *event;
    223 {
    224 	RF_FREELIST_FREE(rf_revent_freelist, event, next);
    225 }
    226