queue.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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. if (queue->alloc)
  83. kfree(queue->alloc);
  84. }
  85. /*
  86. * Function: int queue_add_cmd(Queue_t *queue, Scsi_Cmnd *SCpnt, int head)
  87. * Purpose : Add a new command onto a queue, adding REQUEST_SENSE to head.
  88. * Params : queue - destination queue
  89. * SCpnt - command to add
  90. * head - add command to head of queue
  91. * Returns : 0 on error, !0 on success
  92. */
  93. int __queue_add(Queue_t *queue, Scsi_Cmnd *SCpnt, int head)
  94. {
  95. unsigned long flags;
  96. struct list_head *l;
  97. QE_t *q;
  98. int ret = 0;
  99. spin_lock_irqsave(&queue->queue_lock, flags);
  100. if (list_empty(&queue->free))
  101. goto empty;
  102. l = queue->free.next;
  103. list_del(l);
  104. q = list_entry(l, QE_t, list);
  105. if (BAD_MAGIC(q, QUEUE_MAGIC_FREE))
  106. BUG();
  107. SET_MAGIC(q, QUEUE_MAGIC_USED);
  108. q->SCpnt = SCpnt;
  109. if (head)
  110. list_add(l, &queue->head);
  111. else
  112. list_add_tail(l, &queue->head);
  113. ret = 1;
  114. empty:
  115. spin_unlock_irqrestore(&queue->queue_lock, flags);
  116. return ret;
  117. }
  118. static Scsi_Cmnd *__queue_remove(Queue_t *queue, struct list_head *ent)
  119. {
  120. QE_t *q;
  121. /*
  122. * Move the entry from the "used" list onto the "free" list
  123. */
  124. list_del(ent);
  125. q = list_entry(ent, QE_t, list);
  126. if (BAD_MAGIC(q, QUEUE_MAGIC_USED))
  127. BUG();
  128. SET_MAGIC(q, QUEUE_MAGIC_FREE);
  129. list_add(ent, &queue->free);
  130. return q->SCpnt;
  131. }
  132. /*
  133. * Function: Scsi_Cmnd *queue_remove_exclude (queue, exclude)
  134. * Purpose : remove a SCSI command from a queue
  135. * Params : queue - queue to remove command from
  136. * exclude - bit array of target&lun which is busy
  137. * Returns : Scsi_Cmnd if successful (and a reference), or NULL if no command available
  138. */
  139. Scsi_Cmnd *queue_remove_exclude(Queue_t *queue, unsigned long *exclude)
  140. {
  141. unsigned long flags;
  142. struct list_head *l;
  143. Scsi_Cmnd *SCpnt = NULL;
  144. spin_lock_irqsave(&queue->queue_lock, flags);
  145. list_for_each(l, &queue->head) {
  146. QE_t *q = list_entry(l, QE_t, list);
  147. if (!test_bit(q->SCpnt->device->id * 8 + q->SCpnt->device->lun, exclude)) {
  148. SCpnt = __queue_remove(queue, l);
  149. break;
  150. }
  151. }
  152. spin_unlock_irqrestore(&queue->queue_lock, flags);
  153. return SCpnt;
  154. }
  155. /*
  156. * Function: Scsi_Cmnd *queue_remove (queue)
  157. * Purpose : removes first SCSI command from a queue
  158. * Params : queue - queue to remove command from
  159. * Returns : Scsi_Cmnd if successful (and a reference), or NULL if no command available
  160. */
  161. Scsi_Cmnd *queue_remove(Queue_t *queue)
  162. {
  163. unsigned long flags;
  164. Scsi_Cmnd *SCpnt = NULL;
  165. spin_lock_irqsave(&queue->queue_lock, flags);
  166. if (!list_empty(&queue->head))
  167. SCpnt = __queue_remove(queue, queue->head.next);
  168. spin_unlock_irqrestore(&queue->queue_lock, flags);
  169. return SCpnt;
  170. }
  171. /*
  172. * Function: Scsi_Cmnd *queue_remove_tgtluntag (queue, target, lun, tag)
  173. * Purpose : remove a SCSI command from the queue for a specified target/lun/tag
  174. * Params : queue - queue to remove command from
  175. * target - target that we want
  176. * lun - lun on device
  177. * tag - tag on device
  178. * Returns : Scsi_Cmnd if successful, or NULL if no command satisfies requirements
  179. */
  180. Scsi_Cmnd *queue_remove_tgtluntag (Queue_t *queue, int target, int lun, int tag)
  181. {
  182. unsigned long flags;
  183. struct list_head *l;
  184. Scsi_Cmnd *SCpnt = NULL;
  185. spin_lock_irqsave(&queue->queue_lock, flags);
  186. list_for_each(l, &queue->head) {
  187. QE_t *q = list_entry(l, QE_t, list);
  188. if (q->SCpnt->device->id == target && q->SCpnt->device->lun == lun &&
  189. q->SCpnt->tag == tag) {
  190. SCpnt = __queue_remove(queue, l);
  191. break;
  192. }
  193. }
  194. spin_unlock_irqrestore(&queue->queue_lock, flags);
  195. return SCpnt;
  196. }
  197. /*
  198. * Function: queue_remove_all_target(queue, target)
  199. * Purpose : remove all SCSI commands from the queue for a specified target
  200. * Params : queue - queue to remove command from
  201. * target - target device id
  202. * Returns : nothing
  203. */
  204. void queue_remove_all_target(Queue_t *queue, int target)
  205. {
  206. unsigned long flags;
  207. struct list_head *l;
  208. spin_lock_irqsave(&queue->queue_lock, flags);
  209. list_for_each(l, &queue->head) {
  210. QE_t *q = list_entry(l, QE_t, list);
  211. if (q->SCpnt->device->id == target)
  212. __queue_remove(queue, l);
  213. }
  214. spin_unlock_irqrestore(&queue->queue_lock, flags);
  215. }
  216. /*
  217. * Function: int queue_probetgtlun (queue, target, lun)
  218. * Purpose : check to see if we have a command in the queue for the specified
  219. * target/lun.
  220. * Params : queue - queue to look in
  221. * target - target we want to probe
  222. * lun - lun on target
  223. * Returns : 0 if not found, != 0 if found
  224. */
  225. int queue_probetgtlun (Queue_t *queue, int target, int lun)
  226. {
  227. unsigned long flags;
  228. struct list_head *l;
  229. int found = 0;
  230. spin_lock_irqsave(&queue->queue_lock, flags);
  231. list_for_each(l, &queue->head) {
  232. QE_t *q = list_entry(l, QE_t, list);
  233. if (q->SCpnt->device->id == target && q->SCpnt->device->lun == lun) {
  234. found = 1;
  235. break;
  236. }
  237. }
  238. spin_unlock_irqrestore(&queue->queue_lock, flags);
  239. return found;
  240. }
  241. /*
  242. * Function: int queue_remove_cmd(Queue_t *queue, Scsi_Cmnd *SCpnt)
  243. * Purpose : remove a specific command from the queues
  244. * Params : queue - queue to look in
  245. * SCpnt - command to find
  246. * Returns : 0 if not found
  247. */
  248. int queue_remove_cmd(Queue_t *queue, Scsi_Cmnd *SCpnt)
  249. {
  250. unsigned long flags;
  251. struct list_head *l;
  252. int found = 0;
  253. spin_lock_irqsave(&queue->queue_lock, flags);
  254. list_for_each(l, &queue->head) {
  255. QE_t *q = list_entry(l, QE_t, list);
  256. if (q->SCpnt == SCpnt) {
  257. __queue_remove(queue, l);
  258. found = 1;
  259. break;
  260. }
  261. }
  262. spin_unlock_irqrestore(&queue->queue_lock, flags);
  263. return found;
  264. }
  265. EXPORT_SYMBOL(queue_initialise);
  266. EXPORT_SYMBOL(queue_free);
  267. EXPORT_SYMBOL(__queue_add);
  268. EXPORT_SYMBOL(queue_remove);
  269. EXPORT_SYMBOL(queue_remove_exclude);
  270. EXPORT_SYMBOL(queue_remove_tgtluntag);
  271. EXPORT_SYMBOL(queue_remove_cmd);
  272. EXPORT_SYMBOL(queue_remove_all_target);
  273. EXPORT_SYMBOL(queue_probetgtlun);
  274. MODULE_AUTHOR("Russell King");
  275. MODULE_DESCRIPTION("SCSI command queueing");
  276. MODULE_LICENSE("GPL");