queue.c 7.92 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
/*
 *  linux/drivers/acorn/scsi/queue.c: queue handling primitives
 *
 *  Copyright (C) 1997-2000 Russell King
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 *  Changelog:
 *   15-Sep-1997 RMK	Created.
 *   11-Oct-1997 RMK	Corrected problem with queue_remove_exclude
 *			not updating internal linked list properly
 *			(was causing commands to go missing).
 *   30-Aug-2000 RMK	Use Linux list handling and spinlocks
 */
#include <linux/module.h>
#include <linux/blkdev.h>
#include <linux/kernel.h>
#include <linux/string.h>
#include <linux/slab.h>
#include <linux/spinlock.h>
#include <linux/list.h>
#include <linux/init.h>

#include "../scsi.h"

#define DEBUG

typedef struct queue_entry {
	struct list_head   list;
32
	struct scsi_cmnd   *SCpnt;
Linus Torvalds's avatar
Linus Torvalds committed
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
#ifdef DEBUG
	unsigned long	   magic;
#endif
} QE_t;

#ifdef DEBUG
#define QUEUE_MAGIC_FREE	0xf7e1c9a3
#define QUEUE_MAGIC_USED	0xf7e1cc33

#define SET_MAGIC(q,m)	((q)->magic = (m))
#define BAD_MAGIC(q,m)	((q)->magic != (m))
#else
#define SET_MAGIC(q,m)	do { } while (0)
#define BAD_MAGIC(q,m)	(0)
#endif

#include "queue.h"

#define NR_QE	32

/*
 * Function: void queue_initialise (Queue_t *queue)
 * Purpose : initialise a queue
 * Params  : queue - queue to initialise
 */
int queue_initialise (Queue_t *queue)
{
	unsigned int nqueues = NR_QE;
	QE_t *q;

	spin_lock_init(&queue->queue_lock);
	INIT_LIST_HEAD(&queue->head);
	INIT_LIST_HEAD(&queue->free);

	/*
	 * If life was easier, then SCpnt would have a
	 * host-available list head, and we wouldn't
	 * need to keep free lists or allocate this
	 * memory.
	 */
	queue->alloc = q = kmalloc(sizeof(QE_t) * nqueues, GFP_KERNEL);
	if (q) {
		for (; nqueues; q++, nqueues--) {
			SET_MAGIC(q, QUEUE_MAGIC_FREE);
			q->SCpnt = NULL;
			list_add(&q->list, &queue->free);
		}
	}

	return queue->alloc != NULL;
}

/*
 * Function: void queue_free (Queue_t *queue)
 * Purpose : free a queue
 * Params  : queue - queue to free
 */
void queue_free (Queue_t *queue)
{
	if (!list_empty(&queue->head))
		printk(KERN_WARNING "freeing non-empty queue %p\n", queue);
94
	kfree(queue->alloc);
Linus Torvalds's avatar
Linus Torvalds committed
95 96 97 98
}
     

/*
99
 * Function: int __queue_add(Queue_t *queue, struct scsi_cmnd *SCpnt, int head)
Linus Torvalds's avatar
Linus Torvalds committed
100 101 102 103 104 105
 * Purpose : Add a new command onto a queue, adding REQUEST_SENSE to head.
 * Params  : queue - destination queue
 *	     SCpnt - command to add
 *	     head  - add command to head of queue
 * Returns : 0 on error, !0 on success
 */
106
int __queue_add(Queue_t *queue, struct scsi_cmnd *SCpnt, int head)
Linus Torvalds's avatar
Linus Torvalds committed
107 108 109 110 111 112 113 114 115 116 117 118 119 120
{
	unsigned long flags;
	struct list_head *l;
	QE_t *q;
	int ret = 0;

	spin_lock_irqsave(&queue->queue_lock, flags);
	if (list_empty(&queue->free))
		goto empty;

	l = queue->free.next;
	list_del(l);

	q = list_entry(l, QE_t, list);
121
	BUG_ON(BAD_MAGIC(q, QUEUE_MAGIC_FREE));
Linus Torvalds's avatar
Linus Torvalds committed
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136

	SET_MAGIC(q, QUEUE_MAGIC_USED);
	q->SCpnt = SCpnt;

	if (head)
		list_add(l, &queue->head);
	else
		list_add_tail(l, &queue->head);

	ret = 1;
empty:
	spin_unlock_irqrestore(&queue->queue_lock, flags);
	return ret;
}

137
static struct scsi_cmnd *__queue_remove(Queue_t *queue, struct list_head *ent)
Linus Torvalds's avatar
Linus Torvalds committed
138 139 140 141 142 143 144 145
{
	QE_t *q;

	/*
	 * Move the entry from the "used" list onto the "free" list
	 */
	list_del(ent);
	q = list_entry(ent, QE_t, list);
146
	BUG_ON(BAD_MAGIC(q, QUEUE_MAGIC_USED));
Linus Torvalds's avatar
Linus Torvalds committed
147 148 149 150 151 152 153 154

	SET_MAGIC(q, QUEUE_MAGIC_FREE);
	list_add(ent, &queue->free);

	return q->SCpnt;
}

/*
155
 * Function: struct scsi_cmnd *queue_remove_exclude (queue, exclude)
Linus Torvalds's avatar
Linus Torvalds committed
156 157 158
 * Purpose : remove a SCSI command from a queue
 * Params  : queue   - queue to remove command from
 *	     exclude - bit array of target&lun which is busy
159
 * Returns : struct scsi_cmnd if successful (and a reference), or NULL if no command available
Linus Torvalds's avatar
Linus Torvalds committed
160
 */
161
struct scsi_cmnd *queue_remove_exclude(Queue_t *queue, unsigned long *exclude)
Linus Torvalds's avatar
Linus Torvalds committed
162 163 164
{
	unsigned long flags;
	struct list_head *l;
165
	struct scsi_cmnd *SCpnt = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180

	spin_lock_irqsave(&queue->queue_lock, flags);
	list_for_each(l, &queue->head) {
		QE_t *q = list_entry(l, QE_t, list);
		if (!test_bit(q->SCpnt->device->id * 8 + q->SCpnt->device->lun, exclude)) {
			SCpnt = __queue_remove(queue, l);
			break;
		}
	}
	spin_unlock_irqrestore(&queue->queue_lock, flags);

	return SCpnt;
}

/*
181
 * Function: struct scsi_cmnd *queue_remove (queue)
Linus Torvalds's avatar
Linus Torvalds committed
182 183
 * Purpose : removes first SCSI command from a queue
 * Params  : queue   - queue to remove command from
184
 * Returns : struct scsi_cmnd if successful (and a reference), or NULL if no command available
Linus Torvalds's avatar
Linus Torvalds committed
185
 */
186
struct scsi_cmnd *queue_remove(Queue_t *queue)
Linus Torvalds's avatar
Linus Torvalds committed
187 188
{
	unsigned long flags;
189
	struct scsi_cmnd *SCpnt = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
190 191 192 193 194 195 196 197 198 199

	spin_lock_irqsave(&queue->queue_lock, flags);
	if (!list_empty(&queue->head))
		SCpnt = __queue_remove(queue, queue->head.next);
	spin_unlock_irqrestore(&queue->queue_lock, flags);

	return SCpnt;
}

/*
200
 * Function: struct scsi_cmnd *queue_remove_tgtluntag (queue, target, lun, tag)
Linus Torvalds's avatar
Linus Torvalds committed
201 202 203 204 205
 * Purpose : remove a SCSI command from the queue for a specified target/lun/tag
 * Params  : queue  - queue to remove command from
 *	     target - target that we want
 *	     lun    - lun on device
 *	     tag    - tag on device
206
 * Returns : struct scsi_cmnd if successful, or NULL if no command satisfies requirements
Linus Torvalds's avatar
Linus Torvalds committed
207
 */
208 209
struct scsi_cmnd *queue_remove_tgtluntag(Queue_t *queue, int target, int lun,
					 int tag)
Linus Torvalds's avatar
Linus Torvalds committed
210 211 212
{
	unsigned long flags;
	struct list_head *l;
213
	struct scsi_cmnd *SCpnt = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278

	spin_lock_irqsave(&queue->queue_lock, flags);
	list_for_each(l, &queue->head) {
		QE_t *q = list_entry(l, QE_t, list);
		if (q->SCpnt->device->id == target && q->SCpnt->device->lun == lun &&
		    q->SCpnt->tag == tag) {
			SCpnt = __queue_remove(queue, l);
			break;
		}
	}
	spin_unlock_irqrestore(&queue->queue_lock, flags);

	return SCpnt;
}

/*
 * Function: queue_remove_all_target(queue, target)
 * Purpose : remove all SCSI commands from the queue for a specified target
 * Params  : queue  - queue to remove command from
 *           target - target device id
 * Returns : nothing
 */
void queue_remove_all_target(Queue_t *queue, int target)
{
	unsigned long flags;
	struct list_head *l;

	spin_lock_irqsave(&queue->queue_lock, flags);
	list_for_each(l, &queue->head) {
		QE_t *q = list_entry(l, QE_t, list);
		if (q->SCpnt->device->id == target)
			__queue_remove(queue, l);
	}
	spin_unlock_irqrestore(&queue->queue_lock, flags);
}

/*
 * Function: int queue_probetgtlun (queue, target, lun)
 * Purpose : check to see if we have a command in the queue for the specified
 *	     target/lun.
 * Params  : queue  - queue to look in
 *	     target - target we want to probe
 *	     lun    - lun on target
 * Returns : 0 if not found, != 0 if found
 */
int queue_probetgtlun (Queue_t *queue, int target, int lun)
{
	unsigned long flags;
	struct list_head *l;
	int found = 0;

	spin_lock_irqsave(&queue->queue_lock, flags);
	list_for_each(l, &queue->head) {
		QE_t *q = list_entry(l, QE_t, list);
		if (q->SCpnt->device->id == target && q->SCpnt->device->lun == lun) {
			found = 1;
			break;
		}
	}
	spin_unlock_irqrestore(&queue->queue_lock, flags);

	return found;
}

/*
279
 * Function: int queue_remove_cmd(Queue_t *queue, struct scsi_cmnd *SCpnt)
Linus Torvalds's avatar
Linus Torvalds committed
280 281 282 283 284
 * Purpose : remove a specific command from the queues
 * Params  : queue - queue to look in
 *	     SCpnt - command to find
 * Returns : 0 if not found
 */
285
int queue_remove_cmd(Queue_t *queue, struct scsi_cmnd *SCpnt)
Linus Torvalds's avatar
Linus Torvalds committed
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
{
	unsigned long flags;
	struct list_head *l;
	int found = 0;

	spin_lock_irqsave(&queue->queue_lock, flags);
	list_for_each(l, &queue->head) {
		QE_t *q = list_entry(l, QE_t, list);
		if (q->SCpnt == SCpnt) {
			__queue_remove(queue, l);
			found = 1;
			break;
		}
	}
	spin_unlock_irqrestore(&queue->queue_lock, flags);

	return found;
}

EXPORT_SYMBOL(queue_initialise);
EXPORT_SYMBOL(queue_free);
EXPORT_SYMBOL(__queue_add);
EXPORT_SYMBOL(queue_remove);
EXPORT_SYMBOL(queue_remove_exclude);
EXPORT_SYMBOL(queue_remove_tgtluntag);
EXPORT_SYMBOL(queue_remove_cmd);
EXPORT_SYMBOL(queue_remove_all_target);
EXPORT_SYMBOL(queue_probetgtlun);

MODULE_AUTHOR("Russell King");
MODULE_DESCRIPTION("SCSI command queueing");
MODULE_LICENSE("GPL");