scsi: remove ordered_tags scsi_device field
[cascardo/linux.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 static inline int scsi_get_tag_type(struct scsi_device *sdev)
26 {
27         if (!sdev->tagged_supported)
28                 return 0;
29         if (sdev->simple_tags)
30                 return MSG_SIMPLE_TAG;
31         return 0;
32 }
33
34 static inline void scsi_set_tag_type(struct scsi_device *sdev, int tag)
35 {
36         switch (tag) {
37         case MSG_ORDERED_TAG:
38         case MSG_SIMPLE_TAG:
39                 sdev->simple_tags = 1;
40                 break;
41         case 0:
42                 /* fall through */
43         default:
44                 sdev->simple_tags = 0;
45                 break;
46         }
47 }
48 /**
49  * scsi_activate_tcq - turn on tag command queueing
50  * @SDpnt:      device to turn on TCQ for
51  * @depth:      queue depth
52  *
53  * Notes:
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.
57  **/
58 static inline void scsi_activate_tcq(struct scsi_device *sdev, int depth)
59 {
60         if (!sdev->tagged_supported)
61                 return;
62
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,
67                                     sdev->host->bqt);
68
69         scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth);
70 }
71
72 /**
73  * scsi_deactivate_tcq - turn off tag command queueing
74  * @SDpnt:      device to turn off TCQ for
75  **/
76 static inline void scsi_deactivate_tcq(struct scsi_device *sdev, int depth)
77 {
78         if (blk_queue_tagged(sdev->request_queue))
79                 blk_queue_free_tags(sdev->request_queue);
80         scsi_adjust_queue_depth(sdev, 0, depth);
81 }
82
83 /**
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
87  *
88  * Notes:
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.
92  **/
93 static inline int scsi_populate_tag_msg(struct scsi_cmnd *cmd, char *msg)
94 {
95         if (cmd->flags & SCMD_TAGGED) {
96                 *msg++ = MSG_SIMPLE_TAG;
97                 *msg++ = cmd->request->tag;
98                 return 2;
99         }
100
101         return 0;
102 }
103
104 static inline struct scsi_cmnd *scsi_mq_find_tag(struct Scsi_Host *shost,
105                                                  int unique_tag)
106 {
107         u16 hwq = blk_mq_unique_tag_to_hwq(unique_tag);
108         struct request *req = NULL;
109
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;
114 }
115
116 /**
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()
120  *
121  * Notes:
122  *      Only works with tags allocated by the generic blk layer.
123  **/
124 static inline struct scsi_cmnd *scsi_find_tag(struct scsi_device *sdev, int tag)
125 {
126         struct request *req;
127
128         if (tag != SCSI_NO_TAG) {
129                 if (shost_use_blk_mq(sdev->host))
130                         return scsi_mq_find_tag(sdev->host, tag);
131
132                 req = blk_queue_find_tag(sdev->request_queue, tag);
133                 return req ? (struct scsi_cmnd *)req->special : NULL;
134         }
135
136         /* single command, look in space */
137         return sdev->current_cmnd;
138 }
139
140
141 /**
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
145  */
146 static inline int scsi_init_shared_tag_map(struct Scsi_Host *shost, int depth)
147 {
148         /*
149          * We always have a shared tag map around when using blk-mq.
150          */
151         if (shost_use_blk_mq(shost))
152                 return 0;
153
154         /*
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)
158          */
159         if (!shost->bqt) {
160                 shost->bqt = blk_init_tags(depth);
161                 if (!shost->bqt)
162                         return -ENOMEM;
163         }
164
165         return 0;
166 }
167
168 /**
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()
172  *
173  * Notes:
174  *      Only works with tags allocated by the generic blk layer.
175  **/
176 static inline struct scsi_cmnd *scsi_host_find_tag(struct Scsi_Host *shost,
177                                                 int tag)
178 {
179         struct request *req;
180
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;
186         }
187         return NULL;
188 }
189
190 #endif /* CONFIG_BLOCK */
191 #endif /* _SCSI_SCSI_TCQ_H */