xref: /netbsd/sys/dev/raidframe/rf_fifo.c (revision bf9ec67e)
1 /*	$NetBSD: rf_fifo.c,v 1.7 2001/11/13 07:11:14 lukem 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.7 2001/11/13 07:11:14 lukem 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(sectPerDisk, clList, listp)
57 	RF_SectorCount_t sectPerDisk;
58 	RF_AllocListElem_t *clList;
59 	RF_ShutdownList_t **listp;
60 {
61 	RF_FifoHeader_t *q;
62 
63 	RF_CallocAndAdd(q, 1, sizeof(RF_FifoHeader_t), (RF_FifoHeader_t *), clList);
64 	q->hq_count = q->lq_count = 0;
65 	return ((void *) q);
66 }
67 
68 void
69 rf_FifoEnqueue(q_in, elem, priority)
70 	void   *q_in;
71 	RF_DiskQueueData_t *elem;
72 	int     priority;
73 {
74 	RF_FifoHeader_t *q = (RF_FifoHeader_t *) q_in;
75 
76 	RF_ASSERT(priority == RF_IO_NORMAL_PRIORITY || priority == RF_IO_LOW_PRIORITY);
77 
78 	elem->next = NULL;
79 	if (priority == RF_IO_NORMAL_PRIORITY) {
80 		if (!q->hq_tail) {
81 			RF_ASSERT(q->hq_count == 0 && q->hq_head == NULL);
82 			q->hq_head = q->hq_tail = elem;
83 		} else {
84 			RF_ASSERT(q->hq_count != 0 && q->hq_head != NULL);
85 			q->hq_tail->next = elem;
86 			q->hq_tail = elem;
87 		}
88 		q->hq_count++;
89 	} else {
90 		RF_ASSERT(elem->next == NULL);
91 		if (rf_fifoDebug) {
92 			printf("raid%d: fifo: ENQ lopri\n",
93 			       elem->raidPtr->raidid);
94 		}
95 		if (!q->lq_tail) {
96 			RF_ASSERT(q->lq_count == 0 && q->lq_head == NULL);
97 			q->lq_head = q->lq_tail = elem;
98 		} else {
99 			RF_ASSERT(q->lq_count != 0 && q->lq_head != NULL);
100 			q->lq_tail->next = elem;
101 			q->lq_tail = elem;
102 		}
103 		q->lq_count++;
104 	}
105 	if ((q->hq_count + q->lq_count) != elem->queue->queueLength) {
106 		printf("Queue lengths differ!: %d %d %d\n",
107 		    q->hq_count, q->lq_count, (int) elem->queue->queueLength);
108 		printf("%d %d %d %d\n",
109 		    (int) elem->queue->numOutstanding,
110 		    (int) elem->queue->maxOutstanding,
111 		    (int) elem->queue->row,
112 		    (int) elem->queue->col);
113 	}
114 	RF_ASSERT((q->hq_count + q->lq_count) == elem->queue->queueLength);
115 }
116 
117 RF_DiskQueueData_t *
118 rf_FifoDequeue(q_in)
119 	void   *q_in;
120 {
121 	RF_FifoHeader_t *q = (RF_FifoHeader_t *) q_in;
122 	RF_DiskQueueData_t *nd;
123 
124 	RF_ASSERT(q);
125 	if (q->hq_head) {
126 		RF_ASSERT(q->hq_count != 0 && q->hq_tail != NULL);
127 		nd = q->hq_head;
128 		q->hq_head = q->hq_head->next;
129 		if (!q->hq_head)
130 			q->hq_tail = NULL;
131 		nd->next = NULL;
132 		q->hq_count--;
133 	} else
134 		if (q->lq_head) {
135 			RF_ASSERT(q->lq_count != 0 && q->lq_tail != NULL);
136 			nd = q->lq_head;
137 			q->lq_head = q->lq_head->next;
138 			if (!q->lq_head)
139 				q->lq_tail = NULL;
140 			nd->next = NULL;
141 			q->lq_count--;
142 			if (rf_fifoDebug) {
143 				printf("raid%d: fifo: DEQ lopri %lx\n",
144 				       nd->raidPtr->raidid, (long) nd);
145 			}
146 		} else {
147 			RF_ASSERT(q->hq_count == 0 && q->lq_count == 0 && q->hq_tail == NULL && q->lq_tail == NULL);
148 			nd = NULL;
149 		}
150 	return (nd);
151 }
152 
153 /* Return ptr to item at head of queue.  Used to examine request
154  * info without actually dequeueing the request.
155  */
156 RF_DiskQueueData_t *
157 rf_FifoPeek(void *q_in)
158 {
159 	RF_DiskQueueData_t *headElement = NULL;
160 	RF_FifoHeader_t *q = (RF_FifoHeader_t *) q_in;
161 
162 	RF_ASSERT(q);
163 	if (q->hq_head)
164 		headElement = q->hq_head;
165 	else
166 		if (q->lq_head)
167 			headElement = q->lq_head;
168 	return (headElement);
169 }
170 /* We sometimes need to promote a low priority access to a regular priority access.
171  * Currently, this is only used when the user wants to write a stripe which is currently
172  * under reconstruction.
173  * This routine will promote all accesses tagged with the indicated parityStripeID from
174  * the low priority queue to the end of the normal priority queue.
175  * We assume the queue is locked upon entry.
176  */
177 int
178 rf_FifoPromote(q_in, parityStripeID, which_ru)
179 	void   *q_in;
180 	RF_StripeNum_t parityStripeID;
181 	RF_ReconUnitNum_t which_ru;
182 {
183 	RF_FifoHeader_t *q = (RF_FifoHeader_t *) q_in;
184 	RF_DiskQueueData_t *lp = q->lq_head, *pt = NULL;	/* lp = lo-pri queue
185 								 * pointer, pt = trailer */
186 	int     retval = 0;
187 
188 	while (lp) {
189 
190 		/* search for the indicated parity stripe in the low-pri queue */
191 		if (lp->parityStripeID == parityStripeID && lp->which_ru == which_ru) {
192 			/* printf("FifoPromote:  promoting access for psid
193 			 * %ld\n",parityStripeID); */
194 			if (pt)
195 				pt->next = lp->next;	/* delete an entry other
196 							 * than the first */
197 			else
198 				q->lq_head = lp->next;	/* delete the head entry */
199 
200 			if (!q->lq_head)
201 				q->lq_tail = NULL;	/* we deleted the only
202 							 * entry */
203 			else
204 				if (lp == q->lq_tail)
205 					q->lq_tail = pt;	/* we deleted the tail
206 								 * entry */
207 
208 			lp->next = NULL;
209 			q->lq_count--;
210 
211 			if (q->hq_tail) {
212 				q->hq_tail->next = lp;
213 				q->hq_tail = lp;
214 			}
215 			 /* append to hi-priority queue */
216 			else {
217 				q->hq_head = q->hq_tail = lp;
218 			}
219 			q->hq_count++;
220 
221 			/* UpdateShortestSeekFinishTimeForced(lp->requestPtr,
222 			 * lp->diskState); *//* deal with this later, if ever */
223 
224 			lp = (pt) ? pt->next : q->lq_head;	/* reset low-pri pointer
225 								 * and continue */
226 			retval++;
227 
228 		} else {
229 			pt = lp;
230 			lp = lp->next;
231 		}
232 	}
233 
234 	/* sanity check.  delete this if you ever put more than one entry in
235 	 * the low-pri queue */
236 	RF_ASSERT(retval == 0 || retval == 1);
237 	return (retval);
238 }
239