scsi: provide a generic change_queue_type method
[firefly-linux-kernel-4.4.55.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         struct request *req = cmd->request;
105
106         if (blk_rq_tagged(req)) {
107                 *msg++ = MSG_SIMPLE_TAG;
108                 *msg++ = req->tag;
109                 return 2;
110         }
111
112         return 0;
113 }
114
115 static inline struct scsi_cmnd *scsi_mq_find_tag(struct Scsi_Host *shost,
116                                                  int unique_tag)
117 {
118         u16 hwq = blk_mq_unique_tag_to_hwq(unique_tag);
119         struct request *req = NULL;
120
121         if (hwq < shost->tag_set.nr_hw_queues)
122                 req = blk_mq_tag_to_rq(shost->tag_set.tags[hwq],
123                                        blk_mq_unique_tag_to_tag(unique_tag));
124         return req ? (struct scsi_cmnd *)req->special : NULL;
125 }
126
127 /**
128  * scsi_find_tag - find a tagged command by device
129  * @SDpnt:      pointer to the ScSI device
130  * @tag:        tag generated by blk_mq_unique_tag()
131  *
132  * Notes:
133  *      Only works with tags allocated by the generic blk layer.
134  **/
135 static inline struct scsi_cmnd *scsi_find_tag(struct scsi_device *sdev, int tag)
136 {
137         struct request *req;
138
139         if (tag != SCSI_NO_TAG) {
140                 if (shost_use_blk_mq(sdev->host))
141                         return scsi_mq_find_tag(sdev->host, tag);
142
143                 req = blk_queue_find_tag(sdev->request_queue, tag);
144                 return req ? (struct scsi_cmnd *)req->special : NULL;
145         }
146
147         /* single command, look in space */
148         return sdev->current_cmnd;
149 }
150
151
152 /**
153  * scsi_init_shared_tag_map - create a shared tag map
154  * @shost:      the host to share the tag map among all devices
155  * @depth:      the total depth of the map
156  */
157 static inline int scsi_init_shared_tag_map(struct Scsi_Host *shost, int depth)
158 {
159         /*
160          * We always have a shared tag map around when using blk-mq.
161          */
162         if (shost_use_blk_mq(shost))
163                 return 0;
164
165         /*
166          * If the shared tag map isn't already initialized, do it now.
167          * This saves callers from having to check ->bqt when setting up
168          * devices on the shared host (for libata)
169          */
170         if (!shost->bqt) {
171                 shost->bqt = blk_init_tags(depth);
172                 if (!shost->bqt)
173                         return -ENOMEM;
174         }
175
176         return 0;
177 }
178
179 /**
180  * scsi_host_find_tag - find the tagged command by host
181  * @shost:      pointer to scsi_host
182  * @tag:        tag generated by blk_mq_unique_tag()
183  *
184  * Notes:
185  *      Only works with tags allocated by the generic blk layer.
186  **/
187 static inline struct scsi_cmnd *scsi_host_find_tag(struct Scsi_Host *shost,
188                                                 int tag)
189 {
190         struct request *req;
191
192         if (tag != SCSI_NO_TAG) {
193                 if (shost_use_blk_mq(shost))
194                         return scsi_mq_find_tag(shost, tag);
195                 req = blk_map_queue_find_tag(shost->bqt, tag);
196                 return req ? (struct scsi_cmnd *)req->special : NULL;
197         }
198         return NULL;
199 }
200
201 #endif /* CONFIG_BLOCK */
202 #endif /* _SCSI_SCSI_TCQ_H */