1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17#include <linux/module.h>
18#include <linux/blk.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/scsi.h"
27
28#define DEBUG
29
30typedef 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
55
56
57
58int 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
69
70
71
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
87
88
89
90void queue_free (Queue_t *queue)
91{
92 if (!list_empty(&queue->head))
93 printk(KERN_WARNING "freeing non-empty queue %p\n", queue);
94 if (queue->alloc)
95 kfree(queue->alloc);
96}
97
98
99
100
101
102
103
104
105
106
107int __queue_add(Queue_t *queue, Scsi_Cmnd *SCpnt, int head)
108{
109 unsigned long flags;
110 struct list_head *l;
111 QE_t *q;
112 int ret = 0;
113
114 spin_lock_irqsave(&queue->queue_lock, flags);
115 if (list_empty(&queue->free))
116 goto empty;
117
118 l = queue->free.next;
119 list_del(l);
120
121 q = list_entry(l, QE_t, list);
122 if (BAD_MAGIC(q, QUEUE_MAGIC_FREE))
123 BUG();
124
125 SET_MAGIC(q, QUEUE_MAGIC_USED);
126 q->SCpnt = SCpnt;
127
128 if (head)
129 list_add(l, &queue->head);
130 else
131 list_add_tail(l, &queue->head);
132
133 ret = 1;
134empty:
135 spin_unlock_irqrestore(&queue->queue_lock, flags);
136 return ret;
137}
138
139static Scsi_Cmnd *__queue_remove(Queue_t *queue, struct list_head *ent)
140{
141 QE_t *q;
142
143
144
145
146 list_del(ent);
147 q = list_entry(ent, QE_t, list);
148 if (BAD_MAGIC(q, QUEUE_MAGIC_USED))
149 BUG();
150
151 SET_MAGIC(q, QUEUE_MAGIC_FREE);
152 list_add(ent, &queue->free);
153
154 return q->SCpnt;
155}
156
157
158
159
160
161
162
163
164Scsi_Cmnd *queue_remove_exclude(Queue_t *queue, void *exclude)
165{
166 unsigned long flags;
167 struct list_head *l;
168 Scsi_Cmnd *SCpnt = NULL;
169
170 spin_lock_irqsave(&queue->queue_lock, flags);
171 list_for_each(l, &queue->head) {
172 QE_t *q = list_entry(l, QE_t, list);
173 if (!test_bit(q->SCpnt->target * 8 + q->SCpnt->lun, exclude)) {
174 SCpnt = __queue_remove(queue, l);
175 break;
176 }
177 }
178 spin_unlock_irqrestore(&queue->queue_lock, flags);
179
180 return SCpnt;
181}
182
183
184
185
186
187
188
189Scsi_Cmnd *queue_remove(Queue_t *queue)
190{
191 unsigned long flags;
192 Scsi_Cmnd *SCpnt = NULL;
193
194 spin_lock_irqsave(&queue->queue_lock, flags);
195 if (!list_empty(&queue->head))
196 SCpnt = __queue_remove(queue, queue->head.next);
197 spin_unlock_irqrestore(&queue->queue_lock, flags);
198
199 return SCpnt;
200}
201
202
203
204
205
206
207
208
209
210
211Scsi_Cmnd *queue_remove_tgtluntag (Queue_t *queue, int target, int lun, int tag)
212{
213 unsigned long flags;
214 struct list_head *l;
215 Scsi_Cmnd *SCpnt = NULL;
216
217 spin_lock_irqsave(&queue->queue_lock, flags);
218 list_for_each(l, &queue->head) {
219 QE_t *q = list_entry(l, QE_t, list);
220 if (q->SCpnt->target == target && q->SCpnt->lun == lun &&
221 q->SCpnt->tag == tag) {
222 SCpnt = __queue_remove(queue, l);
223 break;
224 }
225 }
226 spin_unlock_irqrestore(&queue->queue_lock, flags);
227
228 return SCpnt;
229}
230
231
232
233
234
235
236
237
238void queue_remove_all_target(Queue_t *queue, int target)
239{
240 unsigned long flags;
241 struct list_head *l;
242
243 spin_lock_irqsave(&queue->queue_lock, flags);
244 list_for_each(l, &queue->head) {
245 QE_t *q = list_entry(l, QE_t, list);
246 if (q->SCpnt->target == target)
247 __queue_remove(queue, l);
248 }
249 spin_unlock_irqrestore(&queue->queue_lock, flags);
250}
251
252
253
254
255
256
257
258
259
260
261int queue_probetgtlun (Queue_t *queue, int target, int lun)
262{
263 unsigned long flags;
264 struct list_head *l;
265 int found = 0;
266
267 spin_lock_irqsave(&queue->queue_lock, flags);
268 list_for_each(l, &queue->head) {
269 QE_t *q = list_entry(l, QE_t, list);
270 if (q->SCpnt->target == target && q->SCpnt->lun == lun) {
271 found = 1;
272 break;
273 }
274 }
275 spin_unlock_irqrestore(&queue->queue_lock, flags);
276
277 return found;
278}
279
280
281
282
283
284
285
286
287int queue_remove_cmd(Queue_t *queue, Scsi_Cmnd *SCpnt)
288{
289 unsigned long flags;
290 struct list_head *l;
291 int found = 0;
292
293 spin_lock_irqsave(&queue->queue_lock, flags);
294 list_for_each(l, &queue->head) {
295 QE_t *q = list_entry(l, QE_t, list);
296 if (q->SCpnt == SCpnt) {
297 __queue_remove(queue, l);
298 found = 1;
299 break;
300 }
301 }
302 spin_unlock_irqrestore(&queue->queue_lock, flags);
303
304 return found;
305}
306
307EXPORT_SYMBOL(queue_initialise);
308EXPORT_SYMBOL(queue_free);
309EXPORT_SYMBOL(__queue_add);
310EXPORT_SYMBOL(queue_remove);
311EXPORT_SYMBOL(queue_remove_exclude);
312EXPORT_SYMBOL(queue_remove_tgtluntag);
313EXPORT_SYMBOL(queue_remove_cmd);
314EXPORT_SYMBOL(queue_remove_all_target);
315EXPORT_SYMBOL(queue_probetgtlun);
316
317MODULE_AUTHOR("Russell King");
318MODULE_DESCRIPTION("SCSI command queueing");
319MODULE_LICENSE("GPL");
320