blob: cb11ccef54e5104d2825d4e6bfdfcefa8260672c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
30typedef struct queue_entry {
31 struct list_head list;
Henneee0ca6b2006-10-01 13:18:37 +020032 struct scsi_cmnd *SCpnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#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 */
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 * 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 */
90void queue_free (Queue_t *queue)
91{
92 if (!list_empty(&queue->head))
93 printk(KERN_WARNING "freeing non-empty queue %p\n", queue);
Jesper Juhlc9475cb2005-11-07 01:01:26 -080094 kfree(queue->alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095}
96
97
98/*
Henneee0ca6b2006-10-01 13:18:37 +020099 * Function: int __queue_add(Queue_t *queue, struct scsi_cmnd *SCpnt, int head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 * 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 */
Henneee0ca6b2006-10-01 13:18:37 +0200106int __queue_add(Queue_t *queue, struct scsi_cmnd *SCpnt, int head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107{
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);
Eric Sesterhenn125e1872006-06-23 02:06:06 -0700121 BUG_ON(BAD_MAGIC(q, QUEUE_MAGIC_FREE));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
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
Henneee0ca6b2006-10-01 13:18:37 +0200137static struct scsi_cmnd *__queue_remove(Queue_t *queue, struct list_head *ent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138{
139 QE_t *q;
140
141 /*
142 * Move the entry from the "used" list onto the "free" list
143 */
144 list_del(ent);
145 q = list_entry(ent, QE_t, list);
Eric Sesterhenn125e1872006-06-23 02:06:06 -0700146 BUG_ON(BAD_MAGIC(q, QUEUE_MAGIC_USED));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
148 SET_MAGIC(q, QUEUE_MAGIC_FREE);
149 list_add(ent, &queue->free);
150
151 return q->SCpnt;
152}
153
154/*
Henneee0ca6b2006-10-01 13:18:37 +0200155 * Function: struct scsi_cmnd *queue_remove_exclude (queue, exclude)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 * Purpose : remove a SCSI command from a queue
157 * Params : queue - queue to remove command from
158 * exclude - bit array of target&lun which is busy
Henneee0ca6b2006-10-01 13:18:37 +0200159 * Returns : struct scsi_cmnd if successful (and a reference), or NULL if no command available
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 */
Henneee0ca6b2006-10-01 13:18:37 +0200161struct scsi_cmnd *queue_remove_exclude(Queue_t *queue, unsigned long *exclude)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162{
163 unsigned long flags;
164 struct list_head *l;
Henneee0ca6b2006-10-01 13:18:37 +0200165 struct scsi_cmnd *SCpnt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
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 + q->SCpnt->device->lun, exclude)) {
171 SCpnt = __queue_remove(queue, l);
172 break;
173 }
174 }
175 spin_unlock_irqrestore(&queue->queue_lock, flags);
176
177 return SCpnt;
178}
179
180/*
Henneee0ca6b2006-10-01 13:18:37 +0200181 * Function: struct scsi_cmnd *queue_remove (queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 * Purpose : removes first SCSI command from a queue
183 * Params : queue - queue to remove command from
Henneee0ca6b2006-10-01 13:18:37 +0200184 * Returns : struct scsi_cmnd if successful (and a reference), or NULL if no command available
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 */
Henneee0ca6b2006-10-01 13:18:37 +0200186struct scsi_cmnd *queue_remove(Queue_t *queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187{
188 unsigned long flags;
Henneee0ca6b2006-10-01 13:18:37 +0200189 struct scsi_cmnd *SCpnt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
191 spin_lock_irqsave(&queue->queue_lock, flags);
192 if (!list_empty(&queue->head))
193 SCpnt = __queue_remove(queue, queue->head.next);
194 spin_unlock_irqrestore(&queue->queue_lock, flags);
195
196 return SCpnt;
197}
198
199/*
Henneee0ca6b2006-10-01 13:18:37 +0200200 * Function: struct scsi_cmnd *queue_remove_tgtluntag (queue, target, lun, tag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 * Purpose : remove a SCSI command from the queue for a specified target/lun/tag
202 * Params : queue - queue to remove command from
203 * target - target that we want
204 * lun - lun on device
205 * tag - tag on device
Henneee0ca6b2006-10-01 13:18:37 +0200206 * Returns : struct scsi_cmnd if successful, or NULL if no command satisfies requirements
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 */
Henneee0ca6b2006-10-01 13:18:37 +0200208struct scsi_cmnd *queue_remove_tgtluntag(Queue_t *queue, int target, int lun,
209 int tag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210{
211 unsigned long flags;
212 struct list_head *l;
Henneee0ca6b2006-10-01 13:18:37 +0200213 struct scsi_cmnd *SCpnt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
215 spin_lock_irqsave(&queue->queue_lock, flags);
216 list_for_each(l, &queue->head) {
217 QE_t *q = list_entry(l, QE_t, list);
218 if (q->SCpnt->device->id == target && q->SCpnt->device->lun == lun &&
219 q->SCpnt->tag == tag) {
220 SCpnt = __queue_remove(queue, l);
221 break;
222 }
223 }
224 spin_unlock_irqrestore(&queue->queue_lock, flags);
225
226 return SCpnt;
227}
228
229/*
230 * Function: queue_remove_all_target(queue, target)
231 * Purpose : remove all SCSI commands from the queue for a specified target
232 * Params : queue - queue to remove command from
233 * target - target device id
234 * Returns : nothing
235 */
236void queue_remove_all_target(Queue_t *queue, int target)
237{
238 unsigned long flags;
239 struct list_head *l;
240
241 spin_lock_irqsave(&queue->queue_lock, flags);
242 list_for_each(l, &queue->head) {
243 QE_t *q = list_entry(l, QE_t, list);
244 if (q->SCpnt->device->id == target)
245 __queue_remove(queue, l);
246 }
247 spin_unlock_irqrestore(&queue->queue_lock, flags);
248}
249
250/*
251 * Function: int queue_probetgtlun (queue, target, lun)
252 * Purpose : check to see if we have a command in the queue for the specified
253 * target/lun.
254 * Params : queue - queue to look in
255 * target - target we want to probe
256 * lun - lun on target
257 * Returns : 0 if not found, != 0 if found
258 */
259int queue_probetgtlun (Queue_t *queue, int target, int lun)
260{
261 unsigned long flags;
262 struct list_head *l;
263 int found = 0;
264
265 spin_lock_irqsave(&queue->queue_lock, flags);
266 list_for_each(l, &queue->head) {
267 QE_t *q = list_entry(l, QE_t, list);
268 if (q->SCpnt->device->id == target && q->SCpnt->device->lun == lun) {
269 found = 1;
270 break;
271 }
272 }
273 spin_unlock_irqrestore(&queue->queue_lock, flags);
274
275 return found;
276}
277
278/*
Henneee0ca6b2006-10-01 13:18:37 +0200279 * Function: int queue_remove_cmd(Queue_t *queue, struct scsi_cmnd *SCpnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 * Purpose : remove a specific command from the queues
281 * Params : queue - queue to look in
282 * SCpnt - command to find
283 * Returns : 0 if not found
284 */
Henneee0ca6b2006-10-01 13:18:37 +0200285int queue_remove_cmd(Queue_t *queue, struct scsi_cmnd *SCpnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286{
287 unsigned long flags;
288 struct list_head *l;
289 int found = 0;
290
291 spin_lock_irqsave(&queue->queue_lock, flags);
292 list_for_each(l, &queue->head) {
293 QE_t *q = list_entry(l, QE_t, list);
294 if (q->SCpnt == SCpnt) {
295 __queue_remove(queue, l);
296 found = 1;
297 break;
298 }
299 }
300 spin_unlock_irqrestore(&queue->queue_lock, flags);
301
302 return found;
303}
304
305EXPORT_SYMBOL(queue_initialise);
306EXPORT_SYMBOL(queue_free);
307EXPORT_SYMBOL(__queue_add);
308EXPORT_SYMBOL(queue_remove);
309EXPORT_SYMBOL(queue_remove_exclude);
310EXPORT_SYMBOL(queue_remove_tgtluntag);
311EXPORT_SYMBOL(queue_remove_cmd);
312EXPORT_SYMBOL(queue_remove_all_target);
313EXPORT_SYMBOL(queue_probetgtlun);
314
315MODULE_AUTHOR("Russell King");
316MODULE_DESCRIPTION("SCSI command queueing");
317MODULE_LICENSE("GPL");