queue.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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. 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_cmd(Queue_t *queue, 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, 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 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: 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 : Scsi_Cmnd if successful (and a reference), or NULL if no command available
  135. */
  136. Scsi_Cmnd *queue_remove_exclude(Queue_t *queue, unsigned long *exclude)
  137. {
  138. unsigned long flags;
  139. struct list_head *l;
  140. 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 + q->SCpnt->device->lun, exclude)) {
  145. SCpnt = __queue_remove(queue, l);
  146. break;
  147. }
  148. }
  149. spin_unlock_irqrestore(&queue->queue_lock, flags);
  150. return SCpnt;
  151. }
  152. /*
  153. * Function: Scsi_Cmnd *queue_remove (queue)
  154. * Purpose : removes first SCSI command from a queue
  155. * Params : queue - queue to remove command from
  156. * Returns : Scsi_Cmnd if successful (and a reference), or NULL if no command available
  157. */
  158. Scsi_Cmnd *queue_remove(Queue_t *queue)
  159. {
  160. unsigned long flags;
  161. Scsi_Cmnd *SCpnt = NULL;
  162. spin_lock_irqsave(&queue->queue_lock, flags);
  163. if (!list_empty(&queue->head))
  164. SCpnt = __queue_remove(queue, queue->head.next);
  165. spin_unlock_irqrestore(&queue->queue_lock, flags);
  166. return SCpnt;
  167. }
  168. /*
  169. * Function: Scsi_Cmnd *queue_remove_tgtluntag (queue, target, lun, tag)
  170. * Purpose : remove a SCSI command from the queue for a specified target/lun/tag
  171. * Params : queue - queue to remove command from
  172. * target - target that we want
  173. * lun - lun on device
  174. * tag - tag on device
  175. * Returns : Scsi_Cmnd if successful, or NULL if no command satisfies requirements
  176. */
  177. Scsi_Cmnd *queue_remove_tgtluntag (Queue_t *queue, int target, int lun, int tag)
  178. {
  179. unsigned long flags;
  180. struct list_head *l;
  181. Scsi_Cmnd *SCpnt = NULL;
  182. spin_lock_irqsave(&queue->queue_lock, flags);
  183. list_for_each(l, &queue->head) {
  184. QE_t *q = list_entry(l, QE_t, list);
  185. if (q->SCpnt->device->id == target && q->SCpnt->device->lun == lun &&
  186. q->SCpnt->tag == tag) {
  187. SCpnt = __queue_remove(queue, l);
  188. break;
  189. }
  190. }
  191. spin_unlock_irqrestore(&queue->queue_lock, flags);
  192. return SCpnt;
  193. }
  194. /*
  195. * Function: queue_remove_all_target(queue, target)
  196. * Purpose : remove all SCSI commands from the queue for a specified target
  197. * Params : queue - queue to remove command from
  198. * target - target device id
  199. * Returns : nothing
  200. */
  201. void queue_remove_all_target(Queue_t *queue, int target)
  202. {
  203. unsigned long flags;
  204. struct list_head *l;
  205. spin_lock_irqsave(&queue->queue_lock, flags);
  206. list_for_each(l, &queue->head) {
  207. QE_t *q = list_entry(l, QE_t, list);
  208. if (q->SCpnt->device->id == target)
  209. __queue_remove(queue, l);
  210. }
  211. spin_unlock_irqrestore(&queue->queue_lock, flags);
  212. }
  213. /*
  214. * Function: int queue_probetgtlun (queue, target, lun)
  215. * Purpose : check to see if we have a command in the queue for the specified
  216. * target/lun.
  217. * Params : queue - queue to look in
  218. * target - target we want to probe
  219. * lun - lun on target
  220. * Returns : 0 if not found, != 0 if found
  221. */
  222. int queue_probetgtlun (Queue_t *queue, int target, int lun)
  223. {
  224. unsigned long flags;
  225. struct list_head *l;
  226. int found = 0;
  227. spin_lock_irqsave(&queue->queue_lock, flags);
  228. list_for_each(l, &queue->head) {
  229. QE_t *q = list_entry(l, QE_t, list);
  230. if (q->SCpnt->device->id == target && q->SCpnt->device->lun == lun) {
  231. found = 1;
  232. break;
  233. }
  234. }
  235. spin_unlock_irqrestore(&queue->queue_lock, flags);
  236. return found;
  237. }
  238. /*
  239. * Function: int queue_remove_cmd(Queue_t *queue, Scsi_Cmnd *SCpnt)
  240. * Purpose : remove a specific command from the queues
  241. * Params : queue - queue to look in
  242. * SCpnt - command to find
  243. * Returns : 0 if not found
  244. */
  245. int queue_remove_cmd(Queue_t *queue, Scsi_Cmnd *SCpnt)
  246. {
  247. unsigned long flags;
  248. struct list_head *l;
  249. int found = 0;
  250. spin_lock_irqsave(&queue->queue_lock, flags);
  251. list_for_each(l, &queue->head) {
  252. QE_t *q = list_entry(l, QE_t, list);
  253. if (q->SCpnt == SCpnt) {
  254. __queue_remove(queue, l);
  255. found = 1;
  256. break;
  257. }
  258. }
  259. spin_unlock_irqrestore(&queue->queue_lock, flags);
  260. return found;
  261. }
  262. EXPORT_SYMBOL(queue_initialise);
  263. EXPORT_SYMBOL(queue_free);
  264. EXPORT_SYMBOL(__queue_add);
  265. EXPORT_SYMBOL(queue_remove);
  266. EXPORT_SYMBOL(queue_remove_exclude);
  267. EXPORT_SYMBOL(queue_remove_tgtluntag);
  268. EXPORT_SYMBOL(queue_remove_cmd);
  269. EXPORT_SYMBOL(queue_remove_all_target);
  270. EXPORT_SYMBOL(queue_probetgtlun);
  271. MODULE_AUTHOR("Russell King");
  272. MODULE_DESCRIPTION("SCSI command queueing");
  273. MODULE_LICENSE("GPL");