xref: /netbsd/sys/dev/raidframe/rf_revent.c (revision 6550d01e)
1 /*	$NetBSD: rf_revent.c,v 1.25 2008/05/19 19:49:55 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@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/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: rf_revent.c,v 1.25 2008/05/19 19:49:55 oster Exp $");
34 
35 #include <sys/errno.h>
36 
37 #include "rf_raid.h"
38 #include "rf_revent.h"
39 #include "rf_etimer.h"
40 #include "rf_general.h"
41 #include "rf_desc.h"
42 #include "rf_shutdown.h"
43 
44 #define RF_MAX_FREE_REVENT 128
45 #define RF_MIN_FREE_REVENT  32
46 #define RF_EVENTQ_WAIT 5000
47 
48 #include <sys/proc.h>
49 #include <sys/kernel.h>
50 
51 static void rf_ShutdownReconEvent(void *);
52 
53 static RF_ReconEvent_t *
54 GetReconEventDesc(RF_RowCol_t col, void *arg, RF_Revent_t type);
55 
56 static void rf_ShutdownReconEvent(void *ignored)
57 {
58 	pool_destroy(&rf_pools.revent);
59 }
60 
61 int
62 rf_ConfigureReconEvent(RF_ShutdownList_t **listp)
63 {
64 
65 	rf_pool_init(&rf_pools.revent, sizeof(RF_ReconEvent_t),
66 		     "rf_revent_pl", RF_MIN_FREE_REVENT, RF_MAX_FREE_REVENT);
67 	rf_ShutdownCreate(listp, rf_ShutdownReconEvent, NULL);
68 
69 	return (0);
70 }
71 
72 /* returns the next reconstruction event, blocking the calling thread
73  * until one becomes available.  will now return null if it is blocked
74  * or will return an event if it is not */
75 
76 RF_ReconEvent_t *
77 rf_GetNextReconEvent(RF_RaidReconDesc_t *reconDesc)
78 {
79 	RF_Raid_t *raidPtr = reconDesc->raidPtr;
80 	RF_ReconCtrl_t *rctrl = raidPtr->reconControl;
81 	RF_ReconEvent_t *event;
82 	int stall_count;
83 
84 	RF_LOCK_MUTEX(rctrl->eq_mutex);
85 	/* q null and count==0 must be equivalent conditions */
86 	RF_ASSERT((rctrl->eventQueue == NULL) == (rctrl->eq_count == 0));
87 
88 	/* mpsleep timeout value: secs = timo_val/hz.  'ticks' here is
89 	   defined as cycle-counter ticks, not softclock ticks */
90 
91 #define MAX_RECON_EXEC_USECS (100 * 1000)  /* 100 ms */
92 #define RECON_DELAY_MS 25
93 #define RECON_TIMO     ((RECON_DELAY_MS * hz) / 1000)
94 
95 	/* we are not pre-emptible in the kernel, but we don't want to run
96 	 * forever.  If we run w/o blocking for more than MAX_RECON_EXEC_TICKS
97 	 * ticks of the cycle counter, delay for RECON_DELAY before
98 	 * continuing. this may murder us with context switches, so we may
99 	 * need to increase both the MAX...TICKS and the RECON_DELAY_MS. */
100 	if (reconDesc->reconExecTimerRunning) {
101 		int     status;
102 
103 		RF_ETIMER_STOP(reconDesc->recon_exec_timer);
104 		RF_ETIMER_EVAL(reconDesc->recon_exec_timer);
105 		reconDesc->reconExecTicks +=
106 			RF_ETIMER_VAL_US(reconDesc->recon_exec_timer);
107 		if (reconDesc->reconExecTicks > reconDesc->maxReconExecTicks)
108 			reconDesc->maxReconExecTicks =
109 				reconDesc->reconExecTicks;
110 		if (reconDesc->reconExecTicks >= MAX_RECON_EXEC_USECS) {
111 			/* we've been running too long.  delay for
112 			 * RECON_DELAY_MS */
113 #if RF_RECON_STATS > 0
114 			reconDesc->numReconExecDelays++;
115 #endif				/* RF_RECON_STATS > 0 */
116 
117 			status = ltsleep(&reconDesc->reconExecTicks, PRIBIO,
118 					 "recon delay", RECON_TIMO,
119 					 &rctrl->eq_mutex);
120 			RF_ASSERT(status == EWOULDBLOCK);
121 			reconDesc->reconExecTicks = 0;
122 		}
123 	}
124 
125 	stall_count = 0;
126 	while (!rctrl->eventQueue) {
127 #if RF_RECON_STATS > 0
128 		reconDesc->numReconEventWaits++;
129 #endif				/* RF_RECON_STATS > 0 */
130 
131 		ltsleep(&(rctrl)->eventQueue, PRIBIO,  "raidframe eventq",
132 			RF_EVENTQ_WAIT, &((rctrl)->eq_mutex));
133 
134 		stall_count++;
135 
136 		if ((stall_count > 10) &&
137 		    rctrl->headSepCBList) {
138 			/* There is work to do on the callback list, and
139 			   we've waited long enough... */
140 			rf_WakeupHeadSepCBWaiters(raidPtr);
141 			stall_count = 0;
142 		}
143 		reconDesc->reconExecTicks = 0;	/* we've just waited */
144 	}
145 
146 	reconDesc->reconExecTimerRunning = 1;
147 	if (RF_ETIMER_VAL_US(reconDesc->recon_exec_timer)!=0) {
148 		/* it moved!!  reset the timer. */
149 		RF_ETIMER_START(reconDesc->recon_exec_timer);
150 	}
151 	event = rctrl->eventQueue;
152 	rctrl->eventQueue = event->next;
153 	event->next = NULL;
154 	rctrl->eq_count--;
155 
156 	/* q null and count==0 must be equivalent conditions */
157 	RF_ASSERT((rctrl->eventQueue == NULL) == (rctrl->eq_count == 0));
158 	RF_UNLOCK_MUTEX(rctrl->eq_mutex);
159 	return (event);
160 }
161 /* enqueues a reconstruction event on the indicated queue */
162 void
163 rf_CauseReconEvent(RF_Raid_t *raidPtr, RF_RowCol_t col, void *arg,
164 		   RF_Revent_t type)
165 {
166 	RF_ReconCtrl_t *rctrl = raidPtr->reconControl;
167 	RF_ReconEvent_t *event = GetReconEventDesc(col, arg, type);
168 
169 	if (type == RF_REVENT_BUFCLEAR) {
170 		RF_ASSERT(col != rctrl->fcol);
171 	}
172 	RF_ASSERT(col >= 0 && col <= raidPtr->numCol);
173 	RF_LOCK_MUTEX(rctrl->eq_mutex);
174 	/* q null and count==0 must be equivalent conditions */
175 	RF_ASSERT((rctrl->eventQueue == NULL) == (rctrl->eq_count == 0));
176 	event->next = rctrl->eventQueue;
177 	rctrl->eventQueue = event;
178 	rctrl->eq_count++;
179 	RF_UNLOCK_MUTEX(rctrl->eq_mutex);
180 
181 	wakeup(&(rctrl)->eventQueue);
182 }
183 /* allocates and initializes a recon event descriptor */
184 static RF_ReconEvent_t *
185 GetReconEventDesc(RF_RowCol_t col, void *arg, RF_Revent_t type)
186 {
187 	RF_ReconEvent_t *t;
188 
189 	t = pool_get(&rf_pools.revent, PR_WAITOK);
190 	t->col = col;
191 	t->arg = arg;
192 	t->type = type;
193 	t->next = NULL;
194 	return (t);
195 }
196 
197 /*
198   rf_DrainReconEventQueue() -- used in the event of a reconstruction
199   problem, this function simply drains all pending events from the
200   reconstruct event queue.
201  */
202 
203 void
204 rf_DrainReconEventQueue(RF_RaidReconDesc_t *reconDesc)
205 {
206 	RF_ReconCtrl_t *rctrl = reconDesc->raidPtr->reconControl;
207 	RF_ReconEvent_t *event;
208 
209 	RF_LOCK_MUTEX(rctrl->eq_mutex);
210 	while (rctrl->eventQueue!=NULL) {
211 
212 		event = rctrl->eventQueue;
213 		rctrl->eventQueue = event->next;
214 		event->next = NULL;
215 		rctrl->eq_count--;
216 		/* dump it */
217 		rf_FreeReconEventDesc(event);
218 	}
219 	RF_UNLOCK_MUTEX(rctrl->eq_mutex);
220 }
221 
222 void
223 rf_FreeReconEventDesc(RF_ReconEvent_t *event)
224 {
225 	pool_put(&rf_pools.revent, event);
226 }
227