1 // SPDX-License-Identifier: GPL-2.0-only
3 * linux/drivers/acorn/scsi/queue.c: queue handling primitives
5 * Copyright (C) 1997-2000 Russell King
8 * 15-Sep-1997 RMK Created.
9 * 11-Oct-1997 RMK Corrected problem with queue_remove_exclude
10 * not updating internal linked list properly
11 * (was causing commands to go missing).
12 * 30-Aug-2000 RMK Use Linux list handling and spinlocks
14 #include <linux/module.h>
15 #include <linux/blkdev.h>
16 #include <linux/kernel.h>
17 #include <linux/string.h>
18 #include <linux/slab.h>
19 #include <linux/spinlock.h>
20 #include <linux/list.h>
21 #include <linux/init.h>
27 typedef struct queue_entry
{
28 struct list_head list
;
29 struct scsi_cmnd
*SCpnt
;
36 #define QUEUE_MAGIC_FREE 0xf7e1c9a3
37 #define QUEUE_MAGIC_USED 0xf7e1cc33
39 #define SET_MAGIC(q,m) ((q)->magic = (m))
40 #define BAD_MAGIC(q,m) ((q)->magic != (m))
42 #define SET_MAGIC(q,m) do { } while (0)
43 #define BAD_MAGIC(q,m) (0)
51 * Function: void queue_initialise (Queue_t *queue)
52 * Purpose : initialise a queue
53 * Params : queue - queue to initialise
55 int queue_initialise (Queue_t
*queue
)
57 unsigned int nqueues
= NR_QE
;
60 spin_lock_init(&queue
->queue_lock
);
61 INIT_LIST_HEAD(&queue
->head
);
62 INIT_LIST_HEAD(&queue
->free
);
65 * If life was easier, then SCpnt would have a
66 * host-available list head, and we wouldn't
67 * need to keep free lists or allocate this
70 queue
->alloc
= q
= kmalloc_array(nqueues
, sizeof(QE_t
), GFP_KERNEL
);
72 for (; nqueues
; q
++, nqueues
--) {
73 SET_MAGIC(q
, QUEUE_MAGIC_FREE
);
75 list_add(&q
->list
, &queue
->free
);
79 return queue
->alloc
!= NULL
;
83 * Function: void queue_free (Queue_t *queue)
84 * Purpose : free a queue
85 * Params : queue - queue to free
87 void queue_free (Queue_t
*queue
)
89 if (!list_empty(&queue
->head
))
90 printk(KERN_WARNING
"freeing non-empty queue %p\n", queue
);
96 * Function: int __queue_add(Queue_t *queue, struct scsi_cmnd *SCpnt, int head)
97 * Purpose : Add a new command onto a queue, adding REQUEST_SENSE to head.
98 * Params : queue - destination queue
99 * SCpnt - command to add
100 * head - add command to head of queue
101 * Returns : 0 on error, !0 on success
103 int __queue_add(Queue_t
*queue
, struct scsi_cmnd
*SCpnt
, int head
)
110 spin_lock_irqsave(&queue
->queue_lock
, flags
);
111 if (list_empty(&queue
->free
))
114 l
= queue
->free
.next
;
117 q
= list_entry(l
, QE_t
, list
);
118 BUG_ON(BAD_MAGIC(q
, QUEUE_MAGIC_FREE
));
120 SET_MAGIC(q
, QUEUE_MAGIC_USED
);
124 list_add(l
, &queue
->head
);
126 list_add_tail(l
, &queue
->head
);
130 spin_unlock_irqrestore(&queue
->queue_lock
, flags
);
134 static struct scsi_cmnd
*__queue_remove(Queue_t
*queue
, struct list_head
*ent
)
139 * Move the entry from the "used" list onto the "free" list
142 q
= list_entry(ent
, QE_t
, list
);
143 BUG_ON(BAD_MAGIC(q
, QUEUE_MAGIC_USED
));
145 SET_MAGIC(q
, QUEUE_MAGIC_FREE
);
146 list_add(ent
, &queue
->free
);
152 * Function: struct scsi_cmnd *queue_remove_exclude (queue, exclude)
153 * Purpose : remove a SCSI command from a queue
154 * Params : queue - queue to remove command from
155 * exclude - bit array of target&lun which is busy
156 * Returns : struct scsi_cmnd if successful (and a reference), or NULL if no command available
158 struct scsi_cmnd
*queue_remove_exclude(Queue_t
*queue
, unsigned long *exclude
)
162 struct scsi_cmnd
*SCpnt
= NULL
;
164 spin_lock_irqsave(&queue
->queue_lock
, flags
);
165 list_for_each(l
, &queue
->head
) {
166 QE_t
*q
= list_entry(l
, QE_t
, list
);
167 if (!test_bit(q
->SCpnt
->device
->id
* 8 +
168 (u8
)(q
->SCpnt
->device
->lun
& 0x7), exclude
)) {
169 SCpnt
= __queue_remove(queue
, l
);
173 spin_unlock_irqrestore(&queue
->queue_lock
, flags
);
179 * Function: struct scsi_cmnd *queue_remove (queue)
180 * Purpose : removes first SCSI command from a queue
181 * Params : queue - queue to remove command from
182 * Returns : struct scsi_cmnd if successful (and a reference), or NULL if no command available
184 struct scsi_cmnd
*queue_remove(Queue_t
*queue
)
187 struct scsi_cmnd
*SCpnt
= NULL
;
189 spin_lock_irqsave(&queue
->queue_lock
, flags
);
190 if (!list_empty(&queue
->head
))
191 SCpnt
= __queue_remove(queue
, queue
->head
.next
);
192 spin_unlock_irqrestore(&queue
->queue_lock
, flags
);
198 * Function: struct scsi_cmnd *queue_remove_tgtluntag (queue, target, lun, tag)
199 * Purpose : remove a SCSI command from the queue for a specified target/lun/tag
200 * Params : queue - queue to remove command from
201 * target - target that we want
202 * lun - lun on device
203 * tag - tag on device
204 * Returns : struct scsi_cmnd if successful, or NULL if no command satisfies requirements
206 struct scsi_cmnd
*queue_remove_tgtluntag(Queue_t
*queue
, int target
, int lun
,
211 struct scsi_cmnd
*SCpnt
= NULL
;
213 spin_lock_irqsave(&queue
->queue_lock
, flags
);
214 list_for_each(l
, &queue
->head
) {
215 QE_t
*q
= list_entry(l
, QE_t
, list
);
216 if (q
->SCpnt
->device
->id
== target
&& q
->SCpnt
->device
->lun
== lun
&&
217 q
->SCpnt
->tag
== tag
) {
218 SCpnt
= __queue_remove(queue
, l
);
222 spin_unlock_irqrestore(&queue
->queue_lock
, flags
);
228 * Function: queue_remove_all_target(queue, target)
229 * Purpose : remove all SCSI commands from the queue for a specified target
230 * Params : queue - queue to remove command from
231 * target - target device id
234 void queue_remove_all_target(Queue_t
*queue
, int target
)
239 spin_lock_irqsave(&queue
->queue_lock
, flags
);
240 list_for_each(l
, &queue
->head
) {
241 QE_t
*q
= list_entry(l
, QE_t
, list
);
242 if (q
->SCpnt
->device
->id
== target
)
243 __queue_remove(queue
, l
);
245 spin_unlock_irqrestore(&queue
->queue_lock
, flags
);
249 * Function: int queue_probetgtlun (queue, target, lun)
250 * Purpose : check to see if we have a command in the queue for the specified
252 * Params : queue - queue to look in
253 * target - target we want to probe
254 * lun - lun on target
255 * Returns : 0 if not found, != 0 if found
257 int queue_probetgtlun (Queue_t
*queue
, int target
, int lun
)
263 spin_lock_irqsave(&queue
->queue_lock
, flags
);
264 list_for_each(l
, &queue
->head
) {
265 QE_t
*q
= list_entry(l
, QE_t
, list
);
266 if (q
->SCpnt
->device
->id
== target
&& q
->SCpnt
->device
->lun
== lun
) {
271 spin_unlock_irqrestore(&queue
->queue_lock
, flags
);
277 * Function: int queue_remove_cmd(Queue_t *queue, struct scsi_cmnd *SCpnt)
278 * Purpose : remove a specific command from the queues
279 * Params : queue - queue to look in
280 * SCpnt - command to find
281 * Returns : 0 if not found
283 int queue_remove_cmd(Queue_t
*queue
, struct scsi_cmnd
*SCpnt
)
289 spin_lock_irqsave(&queue
->queue_lock
, flags
);
290 list_for_each(l
, &queue
->head
) {
291 QE_t
*q
= list_entry(l
, QE_t
, list
);
292 if (q
->SCpnt
== SCpnt
) {
293 __queue_remove(queue
, l
);
298 spin_unlock_irqrestore(&queue
->queue_lock
, flags
);
303 EXPORT_SYMBOL(queue_initialise
);
304 EXPORT_SYMBOL(queue_free
);
305 EXPORT_SYMBOL(__queue_add
);
306 EXPORT_SYMBOL(queue_remove
);
307 EXPORT_SYMBOL(queue_remove_exclude
);
308 EXPORT_SYMBOL(queue_remove_tgtluntag
);
309 EXPORT_SYMBOL(queue_remove_cmd
);
310 EXPORT_SYMBOL(queue_remove_all_target
);
311 EXPORT_SYMBOL(queue_probetgtlun
);
313 MODULE_AUTHOR("Russell King");
314 MODULE_DESCRIPTION("SCSI command queueing");
315 MODULE_LICENSE("GPL");