queue.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. /*
  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. #include "../scsi.h"
  26. #define DEBUG
  27. typedef struct queue_entry {
  28. struct list_head list;
  29. struct scsi_cmnd *SCpnt;
  30. #ifdef DEBUG
  31. unsigned long magic;
  32. #endif
  33. } QE_t;
  34. #ifdef DEBUG
  35. #define QUEUE_MAGIC_FREE 0xf7e1c9a3
  36. #define QUEUE_MAGIC_USED 0xf7e1cc33
  37. #define SET_MAGIC(q,m) ((q)->magic = (m))
  38. #define BAD_MAGIC(q,m) ((q)->magic != (m))
  39. #else
  40. #define SET_MAGIC(q,m) do { } while (0)
  41. #define BAD_MAGIC(q,m) (0)
  42. #endif
  43. #include "queue.h"
  44. #define NR_QE 32
  45. /*
  46. * Function: void queue_initialise (Queue_t *queue)
  47. * Purpose : initialise a queue
  48. * Params : queue - queue to initialise
  49. */
  50. int queue_initialise (Queue_t *queue)
  51. {
  52. unsigned int nqueues = NR_QE;
  53. QE_t *q;
  54. spin_lock_init(&queue->queue_lock);
  55. INIT_LIST_HEAD(&queue->head);
  56. INIT_LIST_HEAD(&queue->free);
  57. /*
  58. * If life was easier, then SCpnt would have a
  59. * host-available list head, and we wouldn't
  60. * need to keep free lists or allocate this
  61. * memory.
  62. */
  63. queue->alloc = q = kmalloc(sizeof(QE_t) * nqueues, GFP_KERNEL);
  64. if (q) {
  65. for (; nqueues; q++, nqueues--) {
  66. SET_MAGIC(q, QUEUE_MAGIC_FREE);
  67. q->SCpnt = NULL;
  68. list_add(&q->list, &queue->free);
  69. }
  70. }
  71. return queue->alloc != NULL;
  72. }
  73. /*
  74. * Function: void queue_free (Queue_t *queue)
  75. * Purpose : free a queue
  76. * Params : queue - queue to free
  77. */
  78. void queue_free (Queue_t *queue)
  79. {
  80. if (!list_empty(&queue->head))
  81. printk(KERN_WARNING "freeing non-empty queue %p\n", queue);
  82. kfree(queue->alloc);
  83. }
  84. /*
  85. * Function: int __queue_add(Queue_t *queue, struct scsi_cmnd *SCpnt, int head)
  86. * Purpose : Add a new command onto a queue, adding REQUEST_SENSE to head.
  87. * Params : queue - destination queue
  88. * SCpnt - command to add
  89. * head - add command to head of queue
  90. * Returns : 0 on error, !0 on success
  91. */
  92. int __queue_add(Queue_t *queue, struct scsi_cmnd *SCpnt, int head)
  93. {
  94. unsigned long flags;
  95. struct list_head *l;
  96. QE_t *q;
  97. int ret = 0;
  98. spin_lock_irqsave(&queue->queue_lock, flags);
  99. if (list_empty(&queue->free))
  100. goto empty;
  101. l = queue->free.next;
  102. list_del(l);
  103. q = list_entry(l, QE_t, list);
  104. BUG_ON(BAD_MAGIC(q, QUEUE_MAGIC_FREE));
  105. SET_MAGIC(q, QUEUE_MAGIC_USED);
  106. q->SCpnt = SCpnt;
  107. if (head)
  108. list_add(l, &queue->head);
  109. else
  110. list_add_tail(l, &queue->head);
  111. ret = 1;
  112. empty:
  113. spin_unlock_irqrestore(&queue->queue_lock, flags);
  114. return ret;
  115. }
  116. static struct scsi_cmnd *__queue_remove(Queue_t *queue, struct list_head *ent)
  117. {
  118. QE_t *q;
  119. /*
  120. * Move the entry from the "used" list onto the "free" list
  121. */
  122. list_del(ent);
  123. q = list_entry(ent, QE_t, list);
  124. BUG_ON(BAD_MAGIC(q, QUEUE_MAGIC_USED));
  125. SET_MAGIC(q, QUEUE_MAGIC_FREE);
  126. list_add(ent, &queue->free);
  127. return q->SCpnt;
  128. }
  129. /*
  130. * Function: struct scsi_cmnd *queue_remove_exclude (queue, exclude)
  131. * Purpose : remove a SCSI command from a queue
  132. * Params : queue - queue to remove command from
  133. * exclude - bit array of target&lun which is busy
  134. * Returns : struct scsi_cmnd if successful (and a reference), or NULL if no command available
  135. */
  136. struct scsi_cmnd *queue_remove_exclude(Queue_t *queue, unsigned long *exclude)
  137. {
  138. unsigned long flags;
  139. struct list_head *l;
  140. struct scsi_cmnd *SCpnt = NULL;
  141. spin_lock_irqsave(&queue->queue_lock, flags);
  142. list_for_each(l, &queue->head) {
  143. QE_t *q = list_entry(l, QE_t, list);
  144. if (!test_bit(q->SCpnt->device->id * 8 +
  145. (u8)(q->SCpnt->device->lun & 0x7), exclude)) {
  146. SCpnt = __queue_remove(queue, l);
  147. break;
  148. }
  149. }
  150. spin_unlock_irqrestore(&queue->queue_lock, flags);
  151. return SCpnt;
  152. }
  153. /*
  154. * Function: struct scsi_cmnd *queue_remove (queue)
  155. * Purpose : removes first SCSI command from a queue
  156. * Params : queue - queue to remove command from
  157. * Returns : struct scsi_cmnd if successful (and a reference), or NULL if no command available
  158. */
  159. struct scsi_cmnd *queue_remove(Queue_t *queue)
  160. {
  161. unsigned long flags;
  162. struct scsi_cmnd *SCpnt = NULL;
  163. spin_lock_irqsave(&queue->queue_lock, flags);
  164. if (!list_empty(&queue->head))
  165. SCpnt = __queue_remove(queue, queue->head.next);
  166. spin_unlock_irqrestore(&queue->queue_lock, flags);
  167. return SCpnt;
  168. }
  169. /*
  170. * Function: struct scsi_cmnd *queue_remove_tgtluntag (queue, target, lun, tag)
  171. * Purpose : remove a SCSI command from the queue for a specified target/lun/tag
  172. * Params : queue - queue to remove command from
  173. * target - target that we want
  174. * lun - lun on device
  175. * tag - tag on device
  176. * Returns : struct scsi_cmnd if successful, or NULL if no command satisfies requirements
  177. */
  178. struct scsi_cmnd *queue_remove_tgtluntag(Queue_t *queue, int target, int lun,
  179. int tag)
  180. {
  181. unsigned long flags;
  182. struct list_head *l;
  183. struct scsi_cmnd *SCpnt = NULL;
  184. spin_lock_irqsave(&queue->queue_lock, flags);
  185. list_for_each(l, &queue->head) {
  186. QE_t *q = list_entry(l, QE_t, list);
  187. if (q->SCpnt->device->id == target && q->SCpnt->device->lun == lun &&
  188. q->SCpnt->tag == tag) {
  189. SCpnt = __queue_remove(queue, l);
  190. break;
  191. }
  192. }
  193. spin_unlock_irqrestore(&queue->queue_lock, flags);
  194. return SCpnt;
  195. }
  196. /*
  197. * Function: queue_remove_all_target(queue, target)
  198. * Purpose : remove all SCSI commands from the queue for a specified target
  199. * Params : queue - queue to remove command from
  200. * target - target device id
  201. * Returns : nothing
  202. */
  203. void queue_remove_all_target(Queue_t *queue, int target)
  204. {
  205. unsigned long flags;
  206. struct list_head *l;
  207. spin_lock_irqsave(&queue->queue_lock, flags);
  208. list_for_each(l, &queue->head) {
  209. QE_t *q = list_entry(l, QE_t, list);
  210. if (q->SCpnt->device->id == target)
  211. __queue_remove(queue, l);
  212. }
  213. spin_unlock_irqrestore(&queue->queue_lock, flags);
  214. }
  215. /*
  216. * Function: int queue_probetgtlun (queue, target, lun)
  217. * Purpose : check to see if we have a command in the queue for the specified
  218. * target/lun.
  219. * Params : queue - queue to look in
  220. * target - target we want to probe
  221. * lun - lun on target
  222. * Returns : 0 if not found, != 0 if found
  223. */
  224. int queue_probetgtlun (Queue_t *queue, int target, int lun)
  225. {
  226. unsigned long flags;
  227. struct list_head *l;
  228. int found = 0;
  229. spin_lock_irqsave(&queue->queue_lock, flags);
  230. list_for_each(l, &queue->head) {
  231. QE_t *q = list_entry(l, QE_t, list);
  232. if (q->SCpnt->device->id == target && q->SCpnt->device->lun == lun) {
  233. found = 1;
  234. break;
  235. }
  236. }
  237. spin_unlock_irqrestore(&queue->queue_lock, flags);
  238. return found;
  239. }
  240. /*
  241. * Function: int queue_remove_cmd(Queue_t *queue, struct scsi_cmnd *SCpnt)
  242. * Purpose : remove a specific command from the queues
  243. * Params : queue - queue to look in
  244. * SCpnt - command to find
  245. * Returns : 0 if not found
  246. */
  247. int queue_remove_cmd(Queue_t *queue, struct scsi_cmnd *SCpnt)
  248. {
  249. unsigned long flags;
  250. struct list_head *l;
  251. int found = 0;
  252. spin_lock_irqsave(&queue->queue_lock, flags);
  253. list_for_each(l, &queue->head) {
  254. QE_t *q = list_entry(l, QE_t, list);
  255. if (q->SCpnt == SCpnt) {
  256. __queue_remove(queue, l);
  257. found = 1;
  258. break;
  259. }
  260. }
  261. spin_unlock_irqrestore(&queue->queue_lock, flags);
  262. return found;
  263. }
  264. EXPORT_SYMBOL(queue_initialise);
  265. EXPORT_SYMBOL(queue_free);
  266. EXPORT_SYMBOL(__queue_add);
  267. EXPORT_SYMBOL(queue_remove);
  268. EXPORT_SYMBOL(queue_remove_exclude);
  269. EXPORT_SYMBOL(queue_remove_tgtluntag);
  270. EXPORT_SYMBOL(queue_remove_cmd);
  271. EXPORT_SYMBOL(queue_remove_all_target);
  272. EXPORT_SYMBOL(queue_probetgtlun);
  273. MODULE_AUTHOR("Russell King");
  274. MODULE_DESCRIPTION("SCSI command queueing");
  275. MODULE_LICENSE("GPL");