xref: /netbsd-src/sys/dev/raidframe/rf_fifo.c (revision d20841bb642898112fe68f0ad3f7b26dddf56f07)
1 /*	$NetBSD: rf_fifo.c,v 1.10 2003/12/30 21:59:03 oster Exp $	*/
2 /*
3  * Copyright (c) 1995 Carnegie-Mellon University.
4  * All rights reserved.
5  *
6  * Author: Mark Holland
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 /***************************************************
30  *
31  * rf_fifo.c --  prioritized fifo queue code.
32  * There are only two priority levels: hi and lo.
33  *
34  * Aug 4, 1994, adapted from raidSim version (MCH)
35  *
36  ***************************************************/
37 
38 #include <sys/cdefs.h>
39 __KERNEL_RCSID(0, "$NetBSD: rf_fifo.c,v 1.10 2003/12/30 21:59:03 oster Exp $");
40 
41 #include <dev/raidframe/raidframevar.h>
42 
43 #include "rf_alloclist.h"
44 #include "rf_stripelocks.h"
45 #include "rf_layout.h"
46 #include "rf_diskqueue.h"
47 #include "rf_fifo.h"
48 #include "rf_debugMem.h"
49 #include "rf_general.h"
50 #include "rf_options.h"
51 #include "rf_raid.h"
52 
53 /* just malloc a header, zero it (via calloc), and return it */
54 /*ARGSUSED*/
55 void   *
56 rf_FifoCreate(RF_SectorCount_t sectPerDisk, RF_AllocListElem_t *clList,
57 	      RF_ShutdownList_t **listp)
58 {
59 	RF_FifoHeader_t *q;
60 
61 	RF_MallocAndAdd(q, sizeof(RF_FifoHeader_t),
62 				(RF_FifoHeader_t *), clList);
63 	q->hq_count = q->lq_count = 0;
64 	return ((void *) q);
65 }
66 
67 void
68 rf_FifoEnqueue(void *q_in, RF_DiskQueueData_t *elem, int priority)
69 {
70 	RF_FifoHeader_t *q = (RF_FifoHeader_t *) q_in;
71 
72 	RF_ASSERT(priority == RF_IO_NORMAL_PRIORITY || priority == RF_IO_LOW_PRIORITY);
73 
74 	elem->next = NULL;
75 	if (priority == RF_IO_NORMAL_PRIORITY) {
76 		if (!q->hq_tail) {
77 			RF_ASSERT(q->hq_count == 0 && q->hq_head == NULL);
78 			q->hq_head = q->hq_tail = elem;
79 		} else {
80 			RF_ASSERT(q->hq_count != 0 && q->hq_head != NULL);
81 			q->hq_tail->next = elem;
82 			q->hq_tail = elem;
83 		}
84 		q->hq_count++;
85 	} else {
86 		RF_ASSERT(elem->next == NULL);
87 #if RF_DEBUG_QUEUE
88 		if (rf_fifoDebug) {
89 			printf("raid%d: fifo: ENQ lopri\n",
90 			       elem->raidPtr->raidid);
91 		}
92 #endif
93 		if (!q->lq_tail) {
94 			RF_ASSERT(q->lq_count == 0 && q->lq_head == NULL);
95 			q->lq_head = q->lq_tail = elem;
96 		} else {
97 			RF_ASSERT(q->lq_count != 0 && q->lq_head != NULL);
98 			q->lq_tail->next = elem;
99 			q->lq_tail = elem;
100 		}
101 		q->lq_count++;
102 	}
103 	if ((q->hq_count + q->lq_count) != elem->queue->queueLength) {
104 		printf("Queue lengths differ!: %d %d %d\n",
105 		    q->hq_count, q->lq_count, (int) elem->queue->queueLength);
106 		printf("%d %d %d %d\n",
107 		    (int) elem->queue->numOutstanding,
108 		    (int) elem->queue->maxOutstanding,
109 		    (int) elem->queue->row,
110 		    (int) elem->queue->col);
111 	}
112 	RF_ASSERT((q->hq_count + q->lq_count) == elem->queue->queueLength);
113 }
114 
115 RF_DiskQueueData_t *
116 rf_FifoDequeue(void *q_in)
117 {
118 	RF_FifoHeader_t *q = (RF_FifoHeader_t *) q_in;
119 	RF_DiskQueueData_t *nd;
120 
121 	RF_ASSERT(q);
122 	if (q->hq_head) {
123 		RF_ASSERT(q->hq_count != 0 && q->hq_tail != NULL);
124 		nd = q->hq_head;
125 		q->hq_head = q->hq_head->next;
126 		if (!q->hq_head)
127 			q->hq_tail = NULL;
128 		nd->next = NULL;
129 		q->hq_count--;
130 	} else
131 		if (q->lq_head) {
132 			RF_ASSERT(q->lq_count != 0 && q->lq_tail != NULL);
133 			nd = q->lq_head;
134 			q->lq_head = q->lq_head->next;
135 			if (!q->lq_head)
136 				q->lq_tail = NULL;
137 			nd->next = NULL;
138 			q->lq_count--;
139 #if RF_DEBUG_QUEUE
140 			if (rf_fifoDebug) {
141 				printf("raid%d: fifo: DEQ lopri %lx\n",
142 				       nd->raidPtr->raidid, (long) nd);
143 			}
144 #endif
145 		} else {
146 			RF_ASSERT(q->hq_count == 0 && q->lq_count == 0 && q->hq_tail == NULL && q->lq_tail == NULL);
147 			nd = NULL;
148 		}
149 	return (nd);
150 }
151 
152 /* Return ptr to item at head of queue.  Used to examine request
153  * info without actually dequeueing the request.
154  */
155 RF_DiskQueueData_t *
156 rf_FifoPeek(void *q_in)
157 {
158 	RF_DiskQueueData_t *headElement = NULL;
159 	RF_FifoHeader_t *q = (RF_FifoHeader_t *) q_in;
160 
161 	RF_ASSERT(q);
162 	if (q->hq_head)
163 		headElement = q->hq_head;
164 	else
165 		if (q->lq_head)
166 			headElement = q->lq_head;
167 	return (headElement);
168 }
169 /* We sometimes need to promote a low priority access to a regular priority access.
170  * Currently, this is only used when the user wants to write a stripe which is currently
171  * under reconstruction.
172  * This routine will promote all accesses tagged with the indicated parityStripeID from
173  * the low priority queue to the end of the normal priority queue.
174  * We assume the queue is locked upon entry.
175  */
176 int
177 rf_FifoPromote(void *q_in, RF_StripeNum_t parityStripeID,
178 	       RF_ReconUnitNum_t which_ru)
179 {
180 	RF_FifoHeader_t *q = (RF_FifoHeader_t *) q_in;
181 	RF_DiskQueueData_t *lp = q->lq_head, *pt = NULL;	/* lp = lo-pri queue
182 								 * pointer, pt = trailer */
183 	int     retval = 0;
184 
185 	while (lp) {
186 
187 		/* search for the indicated parity stripe in the low-pri queue */
188 		if (lp->parityStripeID == parityStripeID && lp->which_ru == which_ru) {
189 			/* printf("FifoPromote:  promoting access for psid
190 			 * %ld\n",parityStripeID); */
191 			if (pt)
192 				pt->next = lp->next;	/* delete an entry other
193 							 * than the first */
194 			else
195 				q->lq_head = lp->next;	/* delete the head entry */
196 
197 			if (!q->lq_head)
198 				q->lq_tail = NULL;	/* we deleted the only
199 							 * entry */
200 			else
201 				if (lp == q->lq_tail)
202 					q->lq_tail = pt;	/* we deleted the tail
203 								 * entry */
204 
205 			lp->next = NULL;
206 			q->lq_count--;
207 
208 			if (q->hq_tail) {
209 				q->hq_tail->next = lp;
210 				q->hq_tail = lp;
211 			}
212 			 /* append to hi-priority queue */
213 			else {
214 				q->hq_head = q->hq_tail = lp;
215 			}
216 			q->hq_count++;
217 
218 			/* UpdateShortestSeekFinishTimeForced(lp->requestPtr,
219 			 * lp->diskState); *//* deal with this later, if ever */
220 
221 			lp = (pt) ? pt->next : q->lq_head;	/* reset low-pri pointer
222 								 * and continue */
223 			retval++;
224 
225 		} else {
226 			pt = lp;
227 			lp = lp->next;
228 		}
229 	}
230 
231 	/* sanity check.  delete this if you ever put more than one entry in
232 	 * the low-pri queue */
233 	RF_ASSERT(retval == 0 || retval == 1);
234 	return (retval);
235 }
236