1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
30typedef struct queue_entry {
31 struct list_head list;
32 struct 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_array(nqueues, sizeof(QE_t), 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 kfree(queue->alloc);
95}
96
97
98
99
100
101
102
103
104
105
106int __queue_add(Queue_t *queue, struct 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 BUG_ON(BAD_MAGIC(q, QUEUE_MAGIC_FREE));
122
123 SET_MAGIC(q, QUEUE_MAGIC_USED);
124 q->SCpnt = SCpnt;
125
126 if (head)
127 list_add(l, &queue->head);
128 else
129 list_add_tail(l, &queue->head);
130
131 ret = 1;
132empty:
133 spin_unlock_irqrestore(&queue->queue_lock, flags);
134 return ret;
135}
136
137static struct scsi_cmnd *__queue_remove(Queue_t *queue, struct list_head *ent)
138{
139 QE_t *q;
140
141
142
143
144 list_del(ent);
145 q = list_entry(ent, QE_t, list);
146 BUG_ON(BAD_MAGIC(q, QUEUE_MAGIC_USED));
147
148 SET_MAGIC(q, QUEUE_MAGIC_FREE);
149 list_add(ent, &queue->free);
150
151 return q->SCpnt;
152}
153
154
155
156
157
158
159
160
161struct scsi_cmnd *queue_remove_exclude(Queue_t *queue, unsigned long *exclude)
162{
163 unsigned long flags;
164 struct list_head *l;
165 struct scsi_cmnd *SCpnt = NULL;
166
167 spin_lock_irqsave(&queue->queue_lock, flags);
168 list_for_each(l, &queue->head) {
169 QE_t *q = list_entry(l, QE_t, list);
170 if (!test_bit(q->SCpnt->device->id * 8 +
171 (u8)(q->SCpnt->device->lun & 0x7), exclude)) {
172 SCpnt = __queue_remove(queue, l);
173 break;
174 }
175 }
176 spin_unlock_irqrestore(&queue->queue_lock, flags);
177
178 return SCpnt;
179}
180
181
182
183
184
185
186
187struct scsi_cmnd *queue_remove(Queue_t *queue)
188{
189 unsigned long flags;
190 struct scsi_cmnd *SCpnt = NULL;
191
192 spin_lock_irqsave(&queue->queue_lock, flags);
193 if (!list_empty(&queue->head))
194 SCpnt = __queue_remove(queue, queue->head.next);
195 spin_unlock_irqrestore(&queue->queue_lock, flags);
196
197 return SCpnt;
198}
199
200
201
202
203
204
205
206
207
208
209struct scsi_cmnd *queue_remove_tgtluntag(Queue_t *queue, int target, int lun,
210 int tag)
211{
212 unsigned long flags;
213 struct list_head *l;
214 struct 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
232
233
234
235
236
237void 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
253
254
255
256
257
258
259
260int 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
281
282
283
284
285
286int queue_remove_cmd(Queue_t *queue, struct 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
306EXPORT_SYMBOL(queue_initialise);
307EXPORT_SYMBOL(queue_free);
308EXPORT_SYMBOL(__queue_add);
309EXPORT_SYMBOL(queue_remove);
310EXPORT_SYMBOL(queue_remove_exclude);
311EXPORT_SYMBOL(queue_remove_tgtluntag);
312EXPORT_SYMBOL(queue_remove_cmd);
313EXPORT_SYMBOL(queue_remove_all_target);
314EXPORT_SYMBOL(queue_probetgtlun);
315
316MODULE_AUTHOR("Russell King");
317MODULE_DESCRIPTION("SCSI command queueing");
318MODULE_LICENSE("GPL");
319