blob: b27977e8aaed9b12bf6497fbdf01879a47b26cbc [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#ifndef _SCSI_SCSI_TCQ_H
2#define _SCSI_SCSI_TCQ_H
3
4#include <linux/blkdev.h>
5#include <scsi/scsi_cmnd.h>
6#include <scsi/scsi_device.h>
James Bottomley86e33a22006-08-30 09:45:51 -04007#include <scsi/scsi_host.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#define SCSI_NO_TAG (-1) /* identify no tag in use */
10
11
David Howells93614012006-09-30 20:45:40 +020012#ifdef CONFIG_BLOCK
Christoph Hellwigd2852032014-01-17 12:06:53 +010013static inline struct scsi_cmnd *scsi_mq_find_tag(struct Scsi_Host *shost,
Bart Van Assche1ee8e882014-10-30 14:46:00 +010014 int unique_tag)
Christoph Hellwigd2852032014-01-17 12:06:53 +010015{
Bart Van Assche1ee8e882014-10-30 14:46:00 +010016 u16 hwq = blk_mq_unique_tag_to_hwq(unique_tag);
17 struct request *req = NULL;
Christoph Hellwigd2852032014-01-17 12:06:53 +010018
Bart Van Assche1ee8e882014-10-30 14:46:00 +010019 if (hwq < shost->tag_set.nr_hw_queues)
20 req = blk_mq_tag_to_rq(shost->tag_set.tags[hwq],
21 blk_mq_unique_tag_to_tag(unique_tag));
Christoph Hellwigd2852032014-01-17 12:06:53 +010022 return req ? (struct scsi_cmnd *)req->special : NULL;
23}
24
Linus Torvalds1da177e2005-04-16 15:20:36 -070025/**
26 * scsi_find_tag - find a tagged command by device
27 * @SDpnt: pointer to the ScSI device
Bart Van Assche1ee8e882014-10-30 14:46:00 +010028 * @tag: tag generated by blk_mq_unique_tag()
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 *
30 * Notes:
31 * Only works with tags allocated by the generic blk layer.
32 **/
33static inline struct scsi_cmnd *scsi_find_tag(struct scsi_device *sdev, int tag)
34{
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 struct request *req;
36
37 if (tag != SCSI_NO_TAG) {
Christoph Hellwigd2852032014-01-17 12:06:53 +010038 if (shost_use_blk_mq(sdev->host))
Bart Van Assche1ee8e882014-10-30 14:46:00 +010039 return scsi_mq_find_tag(sdev->host, tag);
Christoph Hellwigd2852032014-01-17 12:06:53 +010040
Bart Van Assche1ee8e882014-10-30 14:46:00 +010041 req = blk_queue_find_tag(sdev->request_queue, tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 return req ? (struct scsi_cmnd *)req->special : NULL;
43 }
44
45 /* single command, look in space */
46 return sdev->current_cmnd;
47}
48
Christoph Hellwigd2852032014-01-17 12:06:53 +010049
James Bottomley86e33a22006-08-30 09:45:51 -040050/**
51 * scsi_init_shared_tag_map - create a shared tag map
52 * @shost: the host to share the tag map among all devices
53 * @depth: the total depth of the map
54 */
James Bottomleydeb81d82006-09-01 09:28:48 -040055static inline int scsi_init_shared_tag_map(struct Scsi_Host *shost, int depth)
James Bottomley86e33a22006-08-30 09:45:51 -040056{
Jens Axboe3070f692008-10-27 19:20:59 +010057 /*
Christoph Hellwigd2852032014-01-17 12:06:53 +010058 * We always have a shared tag map around when using blk-mq.
59 */
60 if (shost_use_blk_mq(shost))
61 return 0;
62
63 /*
Jens Axboe3070f692008-10-27 19:20:59 +010064 * If the shared tag map isn't already initialized, do it now.
65 * This saves callers from having to check ->bqt when setting up
66 * devices on the shared host (for libata)
67 */
68 if (!shost->bqt) {
Shaohua Liee1b6f72015-01-15 17:32:25 -080069 shost->bqt = blk_init_tags(depth,
70 shost->hostt->tag_alloc_policy);
Jens Axboe3070f692008-10-27 19:20:59 +010071 if (!shost->bqt)
72 return -ENOMEM;
73 }
74
75 return 0;
James Bottomley86e33a22006-08-30 09:45:51 -040076}
77
David C Somayajuluf583f492006-10-04 08:27:25 +020078/**
79 * scsi_host_find_tag - find the tagged command by host
80 * @shost: pointer to scsi_host
Bart Van Assche1ee8e882014-10-30 14:46:00 +010081 * @tag: tag generated by blk_mq_unique_tag()
David C Somayajuluf583f492006-10-04 08:27:25 +020082 *
83 * Notes:
84 * Only works with tags allocated by the generic blk layer.
85 **/
86static inline struct scsi_cmnd *scsi_host_find_tag(struct Scsi_Host *shost,
87 int tag)
88{
89 struct request *req;
90
91 if (tag != SCSI_NO_TAG) {
Christoph Hellwigd2852032014-01-17 12:06:53 +010092 if (shost_use_blk_mq(shost))
Bart Van Assche1ee8e882014-10-30 14:46:00 +010093 return scsi_mq_find_tag(shost, tag);
David C Somayajuluf583f492006-10-04 08:27:25 +020094 req = blk_map_queue_find_tag(shost->bqt, tag);
95 return req ? (struct scsi_cmnd *)req->special : NULL;
96 }
97 return NULL;
98}
99
David Howells93614012006-09-30 20:45:40 +0200100#endif /* CONFIG_BLOCK */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101#endif /* _SCSI_SCSI_TCQ_H */