Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * linux/drivers/acorn/scsi/queue.c: queue handling primitives | |
3 | * | |
4 | * Copyright (C) 1997-2000 Russell King | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License version 2 as | |
8 | * published by the Free Software Foundation. | |
9 | * | |
10 | * Changelog: | |
11 | * 15-Sep-1997 RMK Created. | |
12 | * 11-Oct-1997 RMK Corrected problem with queue_remove_exclude | |
13 | * not updating internal linked list properly | |
14 | * (was causing commands to go missing). | |
15 | * 30-Aug-2000 RMK Use Linux list handling and spinlocks | |
16 | */ | |
17 | #include <linux/module.h> | |
18 | #include <linux/blkdev.h> | |
19 | #include <linux/kernel.h> | |
20 | #include <linux/string.h> | |
21 | #include <linux/slab.h> | |
22 | #include <linux/spinlock.h> | |
23 | #include <linux/list.h> | |
24 | #include <linux/init.h> | |
25 | ||
26 | #include "../scsi.h" | |
27 | ||
28 | #define DEBUG | |
29 | ||
30 | typedef struct queue_entry { | |
31 | struct list_head list; | |
32 | Scsi_Cmnd *SCpnt; | |
33 | #ifdef DEBUG | |
34 | unsigned long magic; | |
35 | #endif | |
36 | } QE_t; | |
37 | ||
38 | #ifdef DEBUG | |
39 | #define QUEUE_MAGIC_FREE 0xf7e1c9a3 | |
40 | #define QUEUE_MAGIC_USED 0xf7e1cc33 | |
41 | ||
42 | #define SET_MAGIC(q,m) ((q)->magic = (m)) | |
43 | #define BAD_MAGIC(q,m) ((q)->magic != (m)) | |
44 | #else | |
45 | #define SET_MAGIC(q,m) do { } while (0) | |
46 | #define BAD_MAGIC(q,m) (0) | |
47 | #endif | |
48 | ||
49 | #include "queue.h" | |
50 | ||
51 | #define NR_QE 32 | |
52 | ||
53 | /* | |
54 | * Function: void queue_initialise (Queue_t *queue) | |
55 | * Purpose : initialise a queue | |
56 | * Params : queue - queue to initialise | |
57 | */ | |
58 | int queue_initialise (Queue_t *queue) | |
59 | { | |
60 | unsigned int nqueues = NR_QE; | |
61 | QE_t *q; | |
62 | ||
63 | spin_lock_init(&queue->queue_lock); | |
64 | INIT_LIST_HEAD(&queue->head); | |
65 | INIT_LIST_HEAD(&queue->free); | |
66 | ||
67 | /* | |
68 | * If life was easier, then SCpnt would have a | |
69 | * host-available list head, and we wouldn't | |
70 | * need to keep free lists or allocate this | |
71 | * memory. | |
72 | */ | |
73 | queue->alloc = q = kmalloc(sizeof(QE_t) * nqueues, GFP_KERNEL); | |
74 | if (q) { | |
75 | for (; nqueues; q++, nqueues--) { | |
76 | SET_MAGIC(q, QUEUE_MAGIC_FREE); | |
77 | q->SCpnt = NULL; | |
78 | list_add(&q->list, &queue->free); | |
79 | } | |
80 | } | |
81 | ||
82 | return queue->alloc != NULL; | |
83 | } | |
84 | ||
85 | /* | |
86 | * Function: void queue_free (Queue_t *queue) | |
87 | * Purpose : free a queue | |
88 | * Params : queue - queue to free | |
89 | */ | |
90 | void queue_free (Queue_t *queue) | |
91 | { | |
92 | if (!list_empty(&queue->head)) | |
93 | printk(KERN_WARNING "freeing non-empty queue %p\n", queue); | |
c9475cb0 | 94 | kfree(queue->alloc); |
1da177e4 LT |
95 | } |
96 | ||
97 | ||
98 | /* | |
99 | * Function: int queue_add_cmd(Queue_t *queue, Scsi_Cmnd *SCpnt, int head) | |
100 | * Purpose : Add a new command onto a queue, adding REQUEST_SENSE to head. | |
101 | * Params : queue - destination queue | |
102 | * SCpnt - command to add | |
103 | * head - add command to head of queue | |
104 | * Returns : 0 on error, !0 on success | |
105 | */ | |
106 | int __queue_add(Queue_t *queue, Scsi_Cmnd *SCpnt, int head) | |
107 | { | |
108 | unsigned long flags; | |
109 | struct list_head *l; | |
110 | QE_t *q; | |
111 | int ret = 0; | |
112 | ||
113 | spin_lock_irqsave(&queue->queue_lock, flags); | |
114 | if (list_empty(&queue->free)) | |
115 | goto empty; | |
116 | ||
117 | l = queue->free.next; | |
118 | list_del(l); | |
119 | ||
120 | q = list_entry(l, QE_t, list); | |
121 | if (BAD_MAGIC(q, QUEUE_MAGIC_FREE)) | |
122 | BUG(); | |
123 | ||
124 | SET_MAGIC(q, QUEUE_MAGIC_USED); | |
125 | q->SCpnt = SCpnt; | |
126 | ||
127 | if (head) | |
128 | list_add(l, &queue->head); | |
129 | else | |
130 | list_add_tail(l, &queue->head); | |
131 | ||
132 | ret = 1; | |
133 | empty: | |
134 | spin_unlock_irqrestore(&queue->queue_lock, flags); | |
135 | return ret; | |
136 | } | |
137 | ||
138 | static Scsi_Cmnd *__queue_remove(Queue_t *queue, struct list_head *ent) | |
139 | { | |
140 | QE_t *q; | |
141 | ||
142 | /* | |
143 | * Move the entry from the "used" list onto the "free" list | |
144 | */ | |
145 | list_del(ent); | |
146 | q = list_entry(ent, QE_t, list); | |
147 | if (BAD_MAGIC(q, QUEUE_MAGIC_USED)) | |
148 | BUG(); | |
149 | ||
150 | SET_MAGIC(q, QUEUE_MAGIC_FREE); | |
151 | list_add(ent, &queue->free); | |
152 | ||
153 | return q->SCpnt; | |
154 | } | |
155 | ||
156 | /* | |
157 | * Function: Scsi_Cmnd *queue_remove_exclude (queue, exclude) | |
158 | * Purpose : remove a SCSI command from a queue | |
159 | * Params : queue - queue to remove command from | |
160 | * exclude - bit array of target&lun which is busy | |
161 | * Returns : Scsi_Cmnd if successful (and a reference), or NULL if no command available | |
162 | */ | |
163 | Scsi_Cmnd *queue_remove_exclude(Queue_t *queue, unsigned long *exclude) | |
164 | { | |
165 | unsigned long flags; | |
166 | struct list_head *l; | |
167 | Scsi_Cmnd *SCpnt = NULL; | |
168 | ||
169 | spin_lock_irqsave(&queue->queue_lock, flags); | |
170 | list_for_each(l, &queue->head) { | |
171 | QE_t *q = list_entry(l, QE_t, list); | |
172 | if (!test_bit(q->SCpnt->device->id * 8 + q->SCpnt->device->lun, exclude)) { | |
173 | SCpnt = __queue_remove(queue, l); | |
174 | break; | |
175 | } | |
176 | } | |
177 | spin_unlock_irqrestore(&queue->queue_lock, flags); | |
178 | ||
179 | return SCpnt; | |
180 | } | |
181 | ||
182 | /* | |
183 | * Function: Scsi_Cmnd *queue_remove (queue) | |
184 | * Purpose : removes first SCSI command from a queue | |
185 | * Params : queue - queue to remove command from | |
186 | * Returns : Scsi_Cmnd if successful (and a reference), or NULL if no command available | |
187 | */ | |
188 | Scsi_Cmnd *queue_remove(Queue_t *queue) | |
189 | { | |
190 | unsigned long flags; | |
191 | Scsi_Cmnd *SCpnt = NULL; | |
192 | ||
193 | spin_lock_irqsave(&queue->queue_lock, flags); | |
194 | if (!list_empty(&queue->head)) | |
195 | SCpnt = __queue_remove(queue, queue->head.next); | |
196 | spin_unlock_irqrestore(&queue->queue_lock, flags); | |
197 | ||
198 | return SCpnt; | |
199 | } | |
200 | ||
201 | /* | |
202 | * Function: Scsi_Cmnd *queue_remove_tgtluntag (queue, target, lun, tag) | |
203 | * Purpose : remove a SCSI command from the queue for a specified target/lun/tag | |
204 | * Params : queue - queue to remove command from | |
205 | * target - target that we want | |
206 | * lun - lun on device | |
207 | * tag - tag on device | |
208 | * Returns : Scsi_Cmnd if successful, or NULL if no command satisfies requirements | |
209 | */ | |
210 | Scsi_Cmnd *queue_remove_tgtluntag (Queue_t *queue, int target, int lun, int tag) | |
211 | { | |
212 | unsigned long flags; | |
213 | struct list_head *l; | |
214 | Scsi_Cmnd *SCpnt = NULL; | |
215 | ||
216 | spin_lock_irqsave(&queue->queue_lock, flags); | |
217 | list_for_each(l, &queue->head) { | |
218 | QE_t *q = list_entry(l, QE_t, list); | |
219 | if (q->SCpnt->device->id == target && q->SCpnt->device->lun == lun && | |
220 | q->SCpnt->tag == tag) { | |
221 | SCpnt = __queue_remove(queue, l); | |
222 | break; | |
223 | } | |
224 | } | |
225 | spin_unlock_irqrestore(&queue->queue_lock, flags); | |
226 | ||
227 | return SCpnt; | |
228 | } | |
229 | ||
230 | /* | |
231 | * Function: queue_remove_all_target(queue, target) | |
232 | * Purpose : remove all SCSI commands from the queue for a specified target | |
233 | * Params : queue - queue to remove command from | |
234 | * target - target device id | |
235 | * Returns : nothing | |
236 | */ | |
237 | void queue_remove_all_target(Queue_t *queue, int target) | |
238 | { | |
239 | unsigned long flags; | |
240 | struct list_head *l; | |
241 | ||
242 | spin_lock_irqsave(&queue->queue_lock, flags); | |
243 | list_for_each(l, &queue->head) { | |
244 | QE_t *q = list_entry(l, QE_t, list); | |
245 | if (q->SCpnt->device->id == target) | |
246 | __queue_remove(queue, l); | |
247 | } | |
248 | spin_unlock_irqrestore(&queue->queue_lock, flags); | |
249 | } | |
250 | ||
251 | /* | |
252 | * Function: int queue_probetgtlun (queue, target, lun) | |
253 | * Purpose : check to see if we have a command in the queue for the specified | |
254 | * target/lun. | |
255 | * Params : queue - queue to look in | |
256 | * target - target we want to probe | |
257 | * lun - lun on target | |
258 | * Returns : 0 if not found, != 0 if found | |
259 | */ | |
260 | int queue_probetgtlun (Queue_t *queue, int target, int lun) | |
261 | { | |
262 | unsigned long flags; | |
263 | struct list_head *l; | |
264 | int found = 0; | |
265 | ||
266 | spin_lock_irqsave(&queue->queue_lock, flags); | |
267 | list_for_each(l, &queue->head) { | |
268 | QE_t *q = list_entry(l, QE_t, list); | |
269 | if (q->SCpnt->device->id == target && q->SCpnt->device->lun == lun) { | |
270 | found = 1; | |
271 | break; | |
272 | } | |
273 | } | |
274 | spin_unlock_irqrestore(&queue->queue_lock, flags); | |
275 | ||
276 | return found; | |
277 | } | |
278 | ||
279 | /* | |
280 | * Function: int queue_remove_cmd(Queue_t *queue, Scsi_Cmnd *SCpnt) | |
281 | * Purpose : remove a specific command from the queues | |
282 | * Params : queue - queue to look in | |
283 | * SCpnt - command to find | |
284 | * Returns : 0 if not found | |
285 | */ | |
286 | int queue_remove_cmd(Queue_t *queue, Scsi_Cmnd *SCpnt) | |
287 | { | |
288 | unsigned long flags; | |
289 | struct list_head *l; | |
290 | int found = 0; | |
291 | ||
292 | spin_lock_irqsave(&queue->queue_lock, flags); | |
293 | list_for_each(l, &queue->head) { | |
294 | QE_t *q = list_entry(l, QE_t, list); | |
295 | if (q->SCpnt == SCpnt) { | |
296 | __queue_remove(queue, l); | |
297 | found = 1; | |
298 | break; | |
299 | } | |
300 | } | |
301 | spin_unlock_irqrestore(&queue->queue_lock, flags); | |
302 | ||
303 | return found; | |
304 | } | |
305 | ||
306 | EXPORT_SYMBOL(queue_initialise); | |
307 | EXPORT_SYMBOL(queue_free); | |
308 | EXPORT_SYMBOL(__queue_add); | |
309 | EXPORT_SYMBOL(queue_remove); | |
310 | EXPORT_SYMBOL(queue_remove_exclude); | |
311 | EXPORT_SYMBOL(queue_remove_tgtluntag); | |
312 | EXPORT_SYMBOL(queue_remove_cmd); | |
313 | EXPORT_SYMBOL(queue_remove_all_target); | |
314 | EXPORT_SYMBOL(queue_probetgtlun); | |
315 | ||
316 | MODULE_AUTHOR("Russell King"); | |
317 | MODULE_DESCRIPTION("SCSI command queueing"); | |
318 | MODULE_LICENSE("GPL"); |