queue.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * linux/drivers/acorn/scsi/queue.c: queue handling primitives
  4. *
  5. * Copyright (C) 1997-2000 Russell King
  6. *
  7. * Changelog:
  8. * 15-Sep-1997 RMK Created.
  9. * 11-Oct-1997 RMK Corrected problem with queue_remove_exclude
  10. * not updating internal linked list properly
  11. * (was causing commands to go missing).
  12. * 30-Aug-2000 RMK Use Linux list handling and spinlocks
  13. */
  14. #include <linux/module.h>
  15. #include <linux/blkdev.h>
  16. #include <linux/kernel.h>
  17. #include <linux/string.h>
  18. #include <linux/slab.h>
  19. #include <linux/spinlock.h>
  20. #include <linux/list.h>
  21. #include <linux/init.h>
  22. #include <scsi/scsi.h>
  23. #include <scsi/scsi_cmnd.h>
  24. #include <scsi/scsi_device.h>
  25. #include <scsi/scsi_eh.h>
  26. #include <scsi/scsi_tcq.h>
  27. #define DEBUG
  28. typedef struct queue_entry {
  29. struct list_head list;
  30. struct scsi_cmnd *SCpnt;
  31. #ifdef DEBUG
  32. unsigned long magic;
  33. #endif
  34. } QE_t;
  35. #ifdef DEBUG
  36. #define QUEUE_MAGIC_FREE 0xf7e1c9a3
  37. #define QUEUE_MAGIC_USED 0xf7e1cc33
  38. #define SET_MAGIC(q,m) ((q)->magic = (m))
  39. #define BAD_MAGIC(q,m) ((q)->magic != (m))
  40. #else
  41. #define SET_MAGIC(q,m) do { } while (0)
  42. #define BAD_MAGIC(q,m) (0)
  43. #endif
  44. #include "queue.h"
  45. #define NR_QE 32
  46. /*
  47. * Function: void queue_initialise (Queue_t *queue)
  48. * Purpose : initialise a queue
  49. * Params : queue - queue to initialise
  50. */
  51. int queue_initialise (Queue_t *queue)
  52. {
  53. unsigned int nqueues = NR_QE;
  54. QE_t *q;
  55. spin_lock_init(&queue->queue_lock);
  56. INIT_LIST_HEAD(&queue->head);
  57. INIT_LIST_HEAD(&queue->free);
  58. /*
  59. * If life was easier, then SCpnt would have a
  60. * host-available list head, and we wouldn't
  61. * need to keep free lists or allocate this
  62. * memory.
  63. */
  64. queue->alloc = q = kmalloc_array(nqueues, sizeof(QE_t), GFP_KERNEL);
  65. if (q) {
  66. for (; nqueues; q++, nqueues--) {
  67. SET_MAGIC(q, QUEUE_MAGIC_FREE);
  68. q->SCpnt = NULL;
  69. list_add(&q->list, &queue->free);
  70. }
  71. }
  72. return queue->alloc != NULL;
  73. }
  74. /*
  75. * Function: void queue_free (Queue_t *queue)
  76. * Purpose : free a queue
  77. * Params : queue - queue to free
  78. */
  79. void queue_free (Queue_t *queue)
  80. {
  81. if (!list_empty(&queue->head))
  82. printk(KERN_WARNING "freeing non-empty queue %p\n", queue);
  83. kfree(queue->alloc);
  84. }
  85. /*
  86. * Function: int __queue_add(Queue_t *queue, struct 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, struct 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. BUG_ON(BAD_MAGIC(q, QUEUE_MAGIC_FREE));
  106. SET_MAGIC(q, QUEUE_MAGIC_USED);
  107. q->SCpnt = SCpnt;
  108. if (head)
  109. list_add(l, &queue->head);
  110. else
  111. list_add_tail(l, &queue->head);
  112. ret = 1;
  113. empty:
  114. spin_unlock_irqrestore(&queue->queue_lock, flags);
  115. return ret;
  116. }
  117. static struct scsi_cmnd *__queue_remove(Queue_t *queue, struct list_head *ent)
  118. {
  119. QE_t *q;
  120. /*
  121. * Move the entry from the "used" list onto the "free" list
  122. */
  123. list_del(ent);
  124. q = list_entry(ent, QE_t, list);
  125. BUG_ON(BAD_MAGIC(q, QUEUE_MAGIC_USED));
  126. SET_MAGIC(q, QUEUE_MAGIC_FREE);
  127. list_add(ent, &queue->free);
  128. return q->SCpnt;
  129. }
  130. /*
  131. * Function: struct scsi_cmnd *queue_remove_exclude (queue, exclude)
  132. * Purpose : remove a SCSI command from a queue
  133. * Params : queue - queue to remove command from
  134. * exclude - bit array of target&lun which is busy
  135. * Returns : struct scsi_cmnd if successful (and a reference), or NULL if no command available
  136. */
  137. struct scsi_cmnd *queue_remove_exclude(Queue_t *queue, unsigned long *exclude)
  138. {
  139. unsigned long flags;
  140. struct list_head *l;
  141. struct scsi_cmnd *SCpnt = NULL;
  142. spin_lock_irqsave(&queue->queue_lock, flags);
  143. list_for_each(l, &queue->head) {
  144. QE_t *q = list_entry(l, QE_t, list);
  145. if (!test_bit(q->SCpnt->device->id * 8 +
  146. (u8)(q->SCpnt->device->lun & 0x7), exclude)) {
  147. SCpnt = __queue_remove(queue, l);
  148. break;
  149. }
  150. }
  151. spin_unlock_irqrestore(&queue->queue_lock, flags);
  152. return SCpnt;
  153. }
  154. /*
  155. * Function: struct scsi_cmnd *queue_remove (queue)
  156. * Purpose : removes first SCSI command from a queue
  157. * Params : queue - queue to remove command from
  158. * Returns : struct scsi_cmnd if successful (and a reference), or NULL if no command available
  159. */
  160. struct scsi_cmnd *queue_remove(Queue_t *queue)
  161. {
  162. unsigned long flags;
  163. struct scsi_cmnd *SCpnt = NULL;
  164. spin_lock_irqsave(&queue->queue_lock, flags);
  165. if (!list_empty(&queue->head))
  166. SCpnt = __queue_remove(queue, queue->head.next);
  167. spin_unlock_irqrestore(&queue->queue_lock, flags);
  168. return SCpnt;
  169. }
  170. /*
  171. * Function: struct scsi_cmnd *queue_remove_tgtluntag (queue, target, lun, tag)
  172. * Purpose : remove a SCSI command from the queue for a specified target/lun/tag
  173. * Params : queue - queue to remove command from
  174. * target - target that we want
  175. * lun - lun on device
  176. * tag - tag on device
  177. * Returns : struct scsi_cmnd if successful, or NULL if no command satisfies requirements
  178. */
  179. struct scsi_cmnd *queue_remove_tgtluntag(Queue_t *queue, int target, int lun,
  180. int tag)
  181. {
  182. unsigned long flags;
  183. struct list_head *l;
  184. struct 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. scsi_cmd_to_rq(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, struct 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, struct 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");