1 #ifndef _SCSI_SCSI_TCQ_H
2 #define _SCSI_SCSI_TCQ_H
4 #include <linux/blkdev.h>
5 #include <scsi/scsi_cmnd.h>
6 #include <scsi/scsi_device.h>
7 #include <scsi/scsi_host.h>
9 #define MSG_SIMPLE_TAG 0x20
10 #define MSG_HEAD_TAG 0x21
11 #define MSG_ORDERED_TAG 0x22
12 #define MSG_ACA_TAG 0x24 /* unsupported */
14 #define SCSI_NO_TAG (-1) /* identify no tag in use */
19 int scsi_change_queue_type(struct scsi_device *sdev, int tag_type);
22 * scsi_get_tag_type - get the type of tag the device supports
23 * @sdev: the scsi device
26 * If the drive only supports simple tags, returns MSG_SIMPLE_TAG
27 * if it supports all tag types, returns MSG_ORDERED_TAG.
29 static inline int scsi_get_tag_type(struct scsi_device *sdev)
31 if (!sdev->tagged_supported)
33 if (sdev->ordered_tags)
34 return MSG_ORDERED_TAG;
35 if (sdev->simple_tags)
36 return MSG_SIMPLE_TAG;
40 static inline void scsi_set_tag_type(struct scsi_device *sdev, int tag)
44 sdev->ordered_tags = 1;
47 sdev->simple_tags = 1;
52 sdev->ordered_tags = 0;
53 sdev->simple_tags = 0;
58 * scsi_activate_tcq - turn on tag command queueing
59 * @SDpnt: device to turn on TCQ for
63 * Eventually, I hope depth would be the maximum depth
64 * the device could cope with and the real queue depth
65 * would be adjustable from 0 to depth.
67 static inline void scsi_activate_tcq(struct scsi_device *sdev, int depth)
69 if (!sdev->tagged_supported)
72 if (shost_use_blk_mq(sdev->host))
73 queue_flag_set_unlocked(QUEUE_FLAG_QUEUED, sdev->request_queue);
74 else if (!blk_queue_tagged(sdev->request_queue))
75 blk_queue_init_tags(sdev->request_queue, depth,
78 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth);
82 * scsi_deactivate_tcq - turn off tag command queueing
83 * @SDpnt: device to turn off TCQ for
85 static inline void scsi_deactivate_tcq(struct scsi_device *sdev, int depth)
87 if (blk_queue_tagged(sdev->request_queue))
88 blk_queue_free_tags(sdev->request_queue);
89 scsi_adjust_queue_depth(sdev, 0, depth);
93 * scsi_populate_tag_msg - place a tag message in a buffer
94 * @SCpnt: pointer to the Scsi_Cmnd for the tag
95 * @msg: pointer to the area to place the tag
98 * designed to create the correct type of tag message for the
99 * particular request. Returns the size of the tag message.
100 * May return 0 if TCQ is disabled for this device.
102 static inline int scsi_populate_tag_msg(struct scsi_cmnd *cmd, char *msg)
104 if (cmd->flags & SCMD_TAGGED) {
105 *msg++ = MSG_SIMPLE_TAG;
106 *msg++ = cmd->request->tag;
113 static inline struct scsi_cmnd *scsi_mq_find_tag(struct Scsi_Host *shost,
116 u16 hwq = blk_mq_unique_tag_to_hwq(unique_tag);
117 struct request *req = NULL;
119 if (hwq < shost->tag_set.nr_hw_queues)
120 req = blk_mq_tag_to_rq(shost->tag_set.tags[hwq],
121 blk_mq_unique_tag_to_tag(unique_tag));
122 return req ? (struct scsi_cmnd *)req->special : NULL;
126 * scsi_find_tag - find a tagged command by device
127 * @SDpnt: pointer to the ScSI device
128 * @tag: tag generated by blk_mq_unique_tag()
131 * Only works with tags allocated by the generic blk layer.
133 static inline struct scsi_cmnd *scsi_find_tag(struct scsi_device *sdev, int tag)
137 if (tag != SCSI_NO_TAG) {
138 if (shost_use_blk_mq(sdev->host))
139 return scsi_mq_find_tag(sdev->host, tag);
141 req = blk_queue_find_tag(sdev->request_queue, tag);
142 return req ? (struct scsi_cmnd *)req->special : NULL;
145 /* single command, look in space */
146 return sdev->current_cmnd;
151 * scsi_init_shared_tag_map - create a shared tag map
152 * @shost: the host to share the tag map among all devices
153 * @depth: the total depth of the map
155 static inline int scsi_init_shared_tag_map(struct Scsi_Host *shost, int depth)
158 * We always have a shared tag map around when using blk-mq.
160 if (shost_use_blk_mq(shost))
164 * If the shared tag map isn't already initialized, do it now.
165 * This saves callers from having to check ->bqt when setting up
166 * devices on the shared host (for libata)
169 shost->bqt = blk_init_tags(depth);
178 * scsi_host_find_tag - find the tagged command by host
179 * @shost: pointer to scsi_host
180 * @tag: tag generated by blk_mq_unique_tag()
183 * Only works with tags allocated by the generic blk layer.
185 static inline struct scsi_cmnd *scsi_host_find_tag(struct Scsi_Host *shost,
190 if (tag != SCSI_NO_TAG) {
191 if (shost_use_blk_mq(shost))
192 return scsi_mq_find_tag(shost, tag);
193 req = blk_map_queue_find_tag(shost->bqt, tag);
194 return req ? (struct scsi_cmnd *)req->special : NULL;
199 #endif /* CONFIG_BLOCK */
200 #endif /* _SCSI_SCSI_TCQ_H */