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
25 static inline int scsi_get_tag_type(struct scsi_device *sdev)
27 if (!sdev->tagged_supported)
29 if (sdev->simple_tags)
30 return MSG_SIMPLE_TAG;
34 static inline void scsi_set_tag_type(struct scsi_device *sdev, int tag)
39 sdev->simple_tags = 1;
44 sdev->simple_tags = 0;
49 * scsi_activate_tcq - turn on tag command queueing
50 * @SDpnt: device to turn on TCQ for
54 * Eventually, I hope depth would be the maximum depth
55 * the device could cope with and the real queue depth
56 * would be adjustable from 0 to depth.
58 static inline void scsi_activate_tcq(struct scsi_device *sdev, int depth)
60 if (!sdev->tagged_supported)
63 if (shost_use_blk_mq(sdev->host))
64 queue_flag_set_unlocked(QUEUE_FLAG_QUEUED, sdev->request_queue);
65 else if (!blk_queue_tagged(sdev->request_queue))
66 blk_queue_init_tags(sdev->request_queue, depth,
69 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth);
73 * scsi_deactivate_tcq - turn off tag command queueing
74 * @SDpnt: device to turn off TCQ for
76 static inline void scsi_deactivate_tcq(struct scsi_device *sdev, int depth)
78 if (blk_queue_tagged(sdev->request_queue))
79 blk_queue_free_tags(sdev->request_queue);
80 scsi_adjust_queue_depth(sdev, 0, depth);
84 * scsi_populate_tag_msg - place a tag message in a buffer
85 * @SCpnt: pointer to the Scsi_Cmnd for the tag
86 * @msg: pointer to the area to place the tag
89 * designed to create the correct type of tag message for the
90 * particular request. Returns the size of the tag message.
91 * May return 0 if TCQ is disabled for this device.
93 static inline int scsi_populate_tag_msg(struct scsi_cmnd *cmd, char *msg)
95 if (cmd->flags & SCMD_TAGGED) {
96 *msg++ = MSG_SIMPLE_TAG;
97 *msg++ = cmd->request->tag;
104 static inline struct scsi_cmnd *scsi_mq_find_tag(struct Scsi_Host *shost,
107 u16 hwq = blk_mq_unique_tag_to_hwq(unique_tag);
108 struct request *req = NULL;
110 if (hwq < shost->tag_set.nr_hw_queues)
111 req = blk_mq_tag_to_rq(shost->tag_set.tags[hwq],
112 blk_mq_unique_tag_to_tag(unique_tag));
113 return req ? (struct scsi_cmnd *)req->special : NULL;
117 * scsi_find_tag - find a tagged command by device
118 * @SDpnt: pointer to the ScSI device
119 * @tag: tag generated by blk_mq_unique_tag()
122 * Only works with tags allocated by the generic blk layer.
124 static inline struct scsi_cmnd *scsi_find_tag(struct scsi_device *sdev, int tag)
128 if (tag != SCSI_NO_TAG) {
129 if (shost_use_blk_mq(sdev->host))
130 return scsi_mq_find_tag(sdev->host, tag);
132 req = blk_queue_find_tag(sdev->request_queue, tag);
133 return req ? (struct scsi_cmnd *)req->special : NULL;
136 /* single command, look in space */
137 return sdev->current_cmnd;
142 * scsi_init_shared_tag_map - create a shared tag map
143 * @shost: the host to share the tag map among all devices
144 * @depth: the total depth of the map
146 static inline int scsi_init_shared_tag_map(struct Scsi_Host *shost, int depth)
149 * We always have a shared tag map around when using blk-mq.
151 if (shost_use_blk_mq(shost))
155 * If the shared tag map isn't already initialized, do it now.
156 * This saves callers from having to check ->bqt when setting up
157 * devices on the shared host (for libata)
160 shost->bqt = blk_init_tags(depth);
169 * scsi_host_find_tag - find the tagged command by host
170 * @shost: pointer to scsi_host
171 * @tag: tag generated by blk_mq_unique_tag()
174 * Only works with tags allocated by the generic blk layer.
176 static inline struct scsi_cmnd *scsi_host_find_tag(struct Scsi_Host *shost,
181 if (tag != SCSI_NO_TAG) {
182 if (shost_use_blk_mq(shost))
183 return scsi_mq_find_tag(shost, tag);
184 req = blk_map_queue_find_tag(shost->bqt, tag);
185 return req ? (struct scsi_cmnd *)req->special : NULL;
190 #endif /* CONFIG_BLOCK */
191 #endif /* _SCSI_SCSI_TCQ_H */