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