scsi: add new scsi-command flag for tagged commands
[linux-block.git] / include / scsi / scsi_tcq.h
1 #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>
7 #include <scsi/scsi_host.h>
8
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 */
13
14 #define SCSI_NO_TAG     (-1)    /* identify no tag in use */
15
16
17 #ifdef CONFIG_BLOCK
18
19 int scsi_change_queue_type(struct scsi_device *sdev, int tag_type);
20
21 /**
22  * scsi_get_tag_type - get the type of tag the device supports
23  * @sdev:       the scsi device
24  *
25  * Notes:
26  *      If the drive only supports simple tags, returns MSG_SIMPLE_TAG
27  *      if it supports all tag types, returns MSG_ORDERED_TAG.
28  */
29 static inline int scsi_get_tag_type(struct scsi_device *sdev)
30 {
31         if (!sdev->tagged_supported)
32                 return 0;
33         if (sdev->ordered_tags)
34                 return MSG_ORDERED_TAG;
35         if (sdev->simple_tags)
36                 return MSG_SIMPLE_TAG;
37         return 0;
38 }
39
40 static inline void scsi_set_tag_type(struct scsi_device *sdev, int tag)
41 {
42         switch (tag) {
43         case MSG_ORDERED_TAG:
44                 sdev->ordered_tags = 1;
45                 /* fall through */
46         case MSG_SIMPLE_TAG:
47                 sdev->simple_tags = 1;
48                 break;
49         case 0:
50                 /* fall through */
51         default:
52                 sdev->ordered_tags = 0;
53                 sdev->simple_tags = 0;
54                 break;
55         }
56 }
57 /**
58  * scsi_activate_tcq - turn on tag command queueing
59  * @SDpnt:      device to turn on TCQ for
60  * @depth:      queue depth
61  *
62  * Notes:
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.
66  **/
67 static inline void scsi_activate_tcq(struct scsi_device *sdev, int depth)
68 {
69         if (!sdev->tagged_supported)
70                 return;
71
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,
76                                     sdev->host->bqt);
77
78         scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth);
79 }
80
81 /**
82  * scsi_deactivate_tcq - turn off tag command queueing
83  * @SDpnt:      device to turn off TCQ for
84  **/
85 static inline void scsi_deactivate_tcq(struct scsi_device *sdev, int depth)
86 {
87         if (blk_queue_tagged(sdev->request_queue))
88                 blk_queue_free_tags(sdev->request_queue);
89         scsi_adjust_queue_depth(sdev, 0, depth);
90 }
91
92 /**
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
96  *
97  * Notes:
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.
101  **/
102 static inline int scsi_populate_tag_msg(struct scsi_cmnd *cmd, char *msg)
103 {
104         if (cmd->flags & SCMD_TAGGED) {
105                 *msg++ = MSG_SIMPLE_TAG;
106                 *msg++ = cmd->request->tag;
107                 return 2;
108         }
109
110         return 0;
111 }
112
113 static inline struct scsi_cmnd *scsi_mq_find_tag(struct Scsi_Host *shost,
114                                                  int unique_tag)
115 {
116         u16 hwq = blk_mq_unique_tag_to_hwq(unique_tag);
117         struct request *req = NULL;
118
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;
123 }
124
125 /**
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()
129  *
130  * Notes:
131  *      Only works with tags allocated by the generic blk layer.
132  **/
133 static inline struct scsi_cmnd *scsi_find_tag(struct scsi_device *sdev, int tag)
134 {
135         struct request *req;
136
137         if (tag != SCSI_NO_TAG) {
138                 if (shost_use_blk_mq(sdev->host))
139                         return scsi_mq_find_tag(sdev->host, tag);
140
141                 req = blk_queue_find_tag(sdev->request_queue, tag);
142                 return req ? (struct scsi_cmnd *)req->special : NULL;
143         }
144
145         /* single command, look in space */
146         return sdev->current_cmnd;
147 }
148
149
150 /**
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
154  */
155 static inline int scsi_init_shared_tag_map(struct Scsi_Host *shost, int depth)
156 {
157         /*
158          * We always have a shared tag map around when using blk-mq.
159          */
160         if (shost_use_blk_mq(shost))
161                 return 0;
162
163         /*
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)
167          */
168         if (!shost->bqt) {
169                 shost->bqt = blk_init_tags(depth);
170                 if (!shost->bqt)
171                         return -ENOMEM;
172         }
173
174         return 0;
175 }
176
177 /**
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()
181  *
182  * Notes:
183  *      Only works with tags allocated by the generic blk layer.
184  **/
185 static inline struct scsi_cmnd *scsi_host_find_tag(struct Scsi_Host *shost,
186                                                 int tag)
187 {
188         struct request *req;
189
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;
195         }
196         return NULL;
197 }
198
199 #endif /* CONFIG_BLOCK */
200 #endif /* _SCSI_SCSI_TCQ_H */