2 * linux/drivers/acorn/scsi/queue.c: queue handling primitives
4 * Copyright (C) 1997-2000 Russell King
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.
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
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>
30 typedef struct queue_entry
{
31 struct list_head list
;
39 #define QUEUE_MAGIC_FREE 0xf7e1c9a3
40 #define QUEUE_MAGIC_USED 0xf7e1cc33
42 #define SET_MAGIC(q,m) ((q)->magic = (m))
43 #define BAD_MAGIC(q,m) ((q)->magic != (m))
45 #define SET_MAGIC(q,m) do { } while (0)
46 #define BAD_MAGIC(q,m) (0)
54 * Function: void queue_initialise (Queue_t *queue)
55 * Purpose : initialise a queue
56 * Params : queue - queue to initialise
58 int queue_initialise (Queue_t
*queue
)
60 unsigned int nqueues
= NR_QE
;
63 spin_lock_init(&queue
->queue_lock
);
64 INIT_LIST_HEAD(&queue
->head
);
65 INIT_LIST_HEAD(&queue
->free
);
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
73 queue
->alloc
= q
= kmalloc(sizeof(QE_t
) * nqueues
, GFP_KERNEL
);
75 for (; nqueues
; q
++, nqueues
--) {
76 SET_MAGIC(q
, QUEUE_MAGIC_FREE
);
78 list_add(&q
->list
, &queue
->free
);
82 return queue
->alloc
!= NULL
;
86 * Function: void queue_free (Queue_t *queue)
87 * Purpose : free a queue
88 * Params : queue - queue to free
90 void queue_free (Queue_t
*queue
)
92 if (!list_empty(&queue
->head
))
93 printk(KERN_WARNING
"freeing non-empty queue %p\n", queue
);
100 * Function: int queue_add_cmd(Queue_t *queue, Scsi_Cmnd *SCpnt, int head)
101 * Purpose : Add a new command onto a queue, adding REQUEST_SENSE to head.
102 * Params : queue - destination queue
103 * SCpnt - command to add
104 * head - add command to head of queue
105 * Returns : 0 on error, !0 on success
107 int __queue_add(Queue_t
*queue
, Scsi_Cmnd
*SCpnt
, int head
)
114 spin_lock_irqsave(&queue
->queue_lock
, flags
);
115 if (list_empty(&queue
->free
))
118 l
= queue
->free
.next
;
121 q
= list_entry(l
, QE_t
, list
);
122 if (BAD_MAGIC(q
, QUEUE_MAGIC_FREE
))
125 SET_MAGIC(q
, QUEUE_MAGIC_USED
);
129 list_add(l
, &queue
->head
);
131 list_add_tail(l
, &queue
->head
);
135 spin_unlock_irqrestore(&queue
->queue_lock
, flags
);
139 static Scsi_Cmnd
*__queue_remove(Queue_t
*queue
, struct list_head
*ent
)
144 * Move the entry from the "used" list onto the "free" list
147 q
= list_entry(ent
, QE_t
, list
);
148 if (BAD_MAGIC(q
, QUEUE_MAGIC_USED
))
151 SET_MAGIC(q
, QUEUE_MAGIC_FREE
);
152 list_add(ent
, &queue
->free
);
158 * Function: Scsi_Cmnd *queue_remove_exclude (queue, exclude)
159 * Purpose : remove a SCSI command from a queue
160 * Params : queue - queue to remove command from
161 * exclude - bit array of target&lun which is busy
162 * Returns : Scsi_Cmnd if successful (and a reference), or NULL if no command available
164 Scsi_Cmnd
*queue_remove_exclude(Queue_t
*queue
, unsigned long *exclude
)
168 Scsi_Cmnd
*SCpnt
= NULL
;
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
->device
->id
* 8 + q
->SCpnt
->device
->lun
, exclude
)) {
174 SCpnt
= __queue_remove(queue
, l
);
178 spin_unlock_irqrestore(&queue
->queue_lock
, flags
);
184 * Function: Scsi_Cmnd *queue_remove (queue)
185 * Purpose : removes first SCSI command from a queue
186 * Params : queue - queue to remove command from
187 * Returns : Scsi_Cmnd if successful (and a reference), or NULL if no command available
189 Scsi_Cmnd
*queue_remove(Queue_t
*queue
)
192 Scsi_Cmnd
*SCpnt
= NULL
;
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
);
203 * Function: Scsi_Cmnd *queue_remove_tgtluntag (queue, target, lun, tag)
204 * Purpose : remove a SCSI command from the queue for a specified target/lun/tag
205 * Params : queue - queue to remove command from
206 * target - target that we want
207 * lun - lun on device
208 * tag - tag on device
209 * Returns : Scsi_Cmnd if successful, or NULL if no command satisfies requirements
211 Scsi_Cmnd
*queue_remove_tgtluntag (Queue_t
*queue
, int target
, int lun
, int tag
)
215 Scsi_Cmnd
*SCpnt
= NULL
;
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
->device
->id
== target
&& q
->SCpnt
->device
->lun
== lun
&&
221 q
->SCpnt
->tag
== tag
) {
222 SCpnt
= __queue_remove(queue
, l
);
226 spin_unlock_irqrestore(&queue
->queue_lock
, flags
);
232 * Function: queue_remove_all_target(queue, target)
233 * Purpose : remove all SCSI commands from the queue for a specified target
234 * Params : queue - queue to remove command from
235 * target - target device id
238 void queue_remove_all_target(Queue_t
*queue
, int target
)
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
->device
->id
== target
)
247 __queue_remove(queue
, l
);
249 spin_unlock_irqrestore(&queue
->queue_lock
, flags
);
253 * Function: int queue_probetgtlun (queue, target, lun)
254 * Purpose : check to see if we have a command in the queue for the specified
256 * Params : queue - queue to look in
257 * target - target we want to probe
258 * lun - lun on target
259 * Returns : 0 if not found, != 0 if found
261 int queue_probetgtlun (Queue_t
*queue
, int target
, int lun
)
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
->device
->id
== target
&& q
->SCpnt
->device
->lun
== lun
) {
275 spin_unlock_irqrestore(&queue
->queue_lock
, flags
);
281 * Function: int queue_remove_cmd(Queue_t *queue, Scsi_Cmnd *SCpnt)
282 * Purpose : remove a specific command from the queues
283 * Params : queue - queue to look in
284 * SCpnt - command to find
285 * Returns : 0 if not found
287 int queue_remove_cmd(Queue_t
*queue
, Scsi_Cmnd
*SCpnt
)
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
);
302 spin_unlock_irqrestore(&queue
->queue_lock
, flags
);
307 EXPORT_SYMBOL(queue_initialise
);
308 EXPORT_SYMBOL(queue_free
);
309 EXPORT_SYMBOL(__queue_add
);
310 EXPORT_SYMBOL(queue_remove
);
311 EXPORT_SYMBOL(queue_remove_exclude
);
312 EXPORT_SYMBOL(queue_remove_tgtluntag
);
313 EXPORT_SYMBOL(queue_remove_cmd
);
314 EXPORT_SYMBOL(queue_remove_all_target
);
315 EXPORT_SYMBOL(queue_probetgtlun
);
317 MODULE_AUTHOR("Russell King");
318 MODULE_DESCRIPTION("SCSI command queueing");
319 MODULE_LICENSE("GPL");