]>
Commit | Line | Data |
---|---|---|
aa387cc8 MC |
1 | /* |
2 | * BSG helper library | |
3 | * | |
4 | * Copyright (C) 2008 James Smart, Emulex Corporation | |
5 | * Copyright (C) 2011 Red Hat, Inc. All rights reserved. | |
6 | * Copyright (C) 2011 Mike Christie | |
7 | * | |
8 | * This program is free software; you can redistribute it and/or modify | |
9 | * it under the terms of the GNU General Public License as published by | |
10 | * the Free Software Foundation; either version 2 of the License, or | |
11 | * (at your option) any later version. | |
12 | * | |
13 | * This program is distributed in the hope that it will be useful, | |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | * GNU General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU General Public License | |
19 | * along with this program; if not, write to the Free Software | |
20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
21 | * | |
22 | */ | |
23 | #include <linux/slab.h> | |
24 | #include <linux/blkdev.h> | |
25 | #include <linux/delay.h> | |
26 | #include <linux/scatterlist.h> | |
27 | #include <linux/bsg-lib.h> | |
6adb1236 | 28 | #include <linux/export.h> |
aa387cc8 MC |
29 | #include <scsi/scsi_cmnd.h> |
30 | ||
31 | /** | |
50b4d485 | 32 | * bsg_teardown_job - routine to teardown a bsg job |
aa98192d | 33 | * @kref: kref inside bsg_job that is to be torn down |
aa387cc8 | 34 | */ |
50b4d485 | 35 | static void bsg_teardown_job(struct kref *kref) |
aa387cc8 | 36 | { |
bf0f2d38 | 37 | struct bsg_job *job = container_of(kref, struct bsg_job, kref); |
c00da4c9 JT |
38 | struct request *rq = job->req; |
39 | ||
aa387cc8 MC |
40 | put_device(job->dev); /* release reference for the request */ |
41 | ||
42 | kfree(job->request_payload.sg_list); | |
43 | kfree(job->reply_payload.sg_list); | |
50b4d485 BB |
44 | |
45 | blk_end_request_all(rq, BLK_STS_OK); | |
aa387cc8 MC |
46 | } |
47 | ||
fb6f7c8d JT |
48 | void bsg_job_put(struct bsg_job *job) |
49 | { | |
50b4d485 | 50 | kref_put(&job->kref, bsg_teardown_job); |
fb6f7c8d JT |
51 | } |
52 | EXPORT_SYMBOL_GPL(bsg_job_put); | |
53 | ||
54 | int bsg_job_get(struct bsg_job *job) | |
55 | { | |
56 | return kref_get_unless_zero(&job->kref); | |
57 | } | |
58 | EXPORT_SYMBOL_GPL(bsg_job_get); | |
aa387cc8 MC |
59 | |
60 | /** | |
61 | * bsg_job_done - completion routine for bsg requests | |
62 | * @job: bsg_job that is complete | |
63 | * @result: job reply result | |
64 | * @reply_payload_rcv_len: length of payload recvd | |
65 | * | |
66 | * The LLD should call this when the bsg job has completed. | |
67 | */ | |
68 | void bsg_job_done(struct bsg_job *job, int result, | |
69 | unsigned int reply_payload_rcv_len) | |
70 | { | |
71 | struct request *req = job->req; | |
72 | struct request *rsp = req->next_rq; | |
82ed4db4 | 73 | struct scsi_request *rq = scsi_req(req); |
aa387cc8 MC |
74 | int err; |
75 | ||
17d5363b | 76 | err = scsi_req(job->req)->result = result; |
aa387cc8 MC |
77 | if (err < 0) |
78 | /* we're only returning the result field in the reply */ | |
82ed4db4 | 79 | rq->sense_len = sizeof(u32); |
aa387cc8 | 80 | else |
82ed4db4 | 81 | rq->sense_len = job->reply_len; |
aa387cc8 | 82 | /* we assume all request payload was transferred, residual == 0 */ |
82ed4db4 | 83 | rq->resid_len = 0; |
aa387cc8 MC |
84 | |
85 | if (rsp) { | |
82ed4db4 | 86 | WARN_ON(reply_payload_rcv_len > scsi_req(rsp)->resid_len); |
aa387cc8 MC |
87 | |
88 | /* set reply (bidi) residual */ | |
82ed4db4 CH |
89 | scsi_req(rsp)->resid_len -= |
90 | min(reply_payload_rcv_len, scsi_req(rsp)->resid_len); | |
aa387cc8 MC |
91 | } |
92 | blk_complete_request(req); | |
93 | } | |
94 | EXPORT_SYMBOL_GPL(bsg_job_done); | |
95 | ||
96 | /** | |
97 | * bsg_softirq_done - softirq done routine for destroying the bsg requests | |
98 | * @rq: BSG request that holds the job to be destroyed | |
99 | */ | |
100 | static void bsg_softirq_done(struct request *rq) | |
101 | { | |
50b4d485 | 102 | struct bsg_job *job = blk_mq_rq_to_pdu(rq); |
aa387cc8 | 103 | |
fb6f7c8d | 104 | bsg_job_put(job); |
aa387cc8 MC |
105 | } |
106 | ||
107 | static int bsg_map_buffer(struct bsg_buffer *buf, struct request *req) | |
108 | { | |
109 | size_t sz = (sizeof(struct scatterlist) * req->nr_phys_segments); | |
110 | ||
111 | BUG_ON(!req->nr_phys_segments); | |
112 | ||
113 | buf->sg_list = kzalloc(sz, GFP_KERNEL); | |
114 | if (!buf->sg_list) | |
115 | return -ENOMEM; | |
116 | sg_init_table(buf->sg_list, req->nr_phys_segments); | |
82ed4db4 | 117 | scsi_req(req)->resid_len = blk_rq_bytes(req); |
aa387cc8 MC |
118 | buf->sg_cnt = blk_rq_map_sg(req->q, req, buf->sg_list); |
119 | buf->payload_len = blk_rq_bytes(req); | |
120 | return 0; | |
121 | } | |
122 | ||
123 | /** | |
50b4d485 | 124 | * bsg_prepare_job - create the bsg_job structure for the bsg request |
aa387cc8 MC |
125 | * @dev: device that is being sent the bsg request |
126 | * @req: BSG request that needs a job structure | |
127 | */ | |
50b4d485 | 128 | static int bsg_prepare_job(struct device *dev, struct request *req) |
aa387cc8 MC |
129 | { |
130 | struct request *rsp = req->next_rq; | |
82ed4db4 | 131 | struct scsi_request *rq = scsi_req(req); |
50b4d485 | 132 | struct bsg_job *job = blk_mq_rq_to_pdu(req); |
aa387cc8 MC |
133 | int ret; |
134 | ||
31156ec3 | 135 | job->timeout = req->timeout; |
82ed4db4 CH |
136 | job->request = rq->cmd; |
137 | job->request_len = rq->cmd_len; | |
50b4d485 | 138 | |
aa387cc8 MC |
139 | if (req->bio) { |
140 | ret = bsg_map_buffer(&job->request_payload, req); | |
141 | if (ret) | |
142 | goto failjob_rls_job; | |
143 | } | |
144 | if (rsp && rsp->bio) { | |
145 | ret = bsg_map_buffer(&job->reply_payload, rsp); | |
146 | if (ret) | |
147 | goto failjob_rls_rqst_payload; | |
148 | } | |
149 | job->dev = dev; | |
150 | /* take a reference for the request */ | |
151 | get_device(job->dev); | |
bf0f2d38 | 152 | kref_init(&job->kref); |
aa387cc8 MC |
153 | return 0; |
154 | ||
155 | failjob_rls_rqst_payload: | |
156 | kfree(job->request_payload.sg_list); | |
157 | failjob_rls_job: | |
aa387cc8 MC |
158 | return -ENOMEM; |
159 | } | |
160 | ||
aa387cc8 MC |
161 | /** |
162 | * bsg_request_fn - generic handler for bsg requests | |
163 | * @q: request queue to manage | |
164 | * | |
165 | * On error the create_bsg_job function should return a -Exyz error value | |
17d5363b | 166 | * that will be set to ->result. |
aa387cc8 MC |
167 | * |
168 | * Drivers/subsys should pass this to the queue init function. | |
169 | */ | |
8ae94eb6 | 170 | static void bsg_request_fn(struct request_queue *q) |
dbb3ab03 BVA |
171 | __releases(q->queue_lock) |
172 | __acquires(q->queue_lock) | |
aa387cc8 MC |
173 | { |
174 | struct device *dev = q->queuedata; | |
175 | struct request *req; | |
aa387cc8 MC |
176 | int ret; |
177 | ||
178 | if (!get_device(dev)) | |
179 | return; | |
180 | ||
181 | while (1) { | |
182 | req = blk_fetch_request(q); | |
183 | if (!req) | |
184 | break; | |
185 | spin_unlock_irq(q->queue_lock); | |
186 | ||
50b4d485 | 187 | ret = bsg_prepare_job(dev, req); |
aa387cc8 | 188 | if (ret) { |
17d5363b | 189 | scsi_req(req)->result = ret; |
2a842aca | 190 | blk_end_request_all(req, BLK_STS_OK); |
aa387cc8 MC |
191 | spin_lock_irq(q->queue_lock); |
192 | continue; | |
193 | } | |
194 | ||
50b4d485 | 195 | ret = q->bsg_job_fn(blk_mq_rq_to_pdu(req)); |
aa387cc8 MC |
196 | spin_lock_irq(q->queue_lock); |
197 | if (ret) | |
198 | break; | |
199 | } | |
200 | ||
201 | spin_unlock_irq(q->queue_lock); | |
202 | put_device(dev); | |
203 | spin_lock_irq(q->queue_lock); | |
204 | } | |
aa387cc8 | 205 | |
50b4d485 BB |
206 | static int bsg_init_rq(struct request_queue *q, struct request *req, gfp_t gfp) |
207 | { | |
208 | struct bsg_job *job = blk_mq_rq_to_pdu(req); | |
209 | struct scsi_request *sreq = &job->sreq; | |
210 | ||
eab40cf3 BB |
211 | /* called right after the request is allocated for the request_queue */ |
212 | ||
213 | sreq->sense = kzalloc(SCSI_SENSE_BUFFERSIZE, gfp); | |
214 | if (!sreq->sense) | |
215 | return -ENOMEM; | |
216 | ||
217 | return 0; | |
218 | } | |
219 | ||
220 | static void bsg_initialize_rq(struct request *req) | |
221 | { | |
222 | struct bsg_job *job = blk_mq_rq_to_pdu(req); | |
223 | struct scsi_request *sreq = &job->sreq; | |
224 | void *sense = sreq->sense; | |
225 | ||
226 | /* called right before the request is given to the request_queue user */ | |
227 | ||
50b4d485 BB |
228 | memset(job, 0, sizeof(*job)); |
229 | ||
230 | scsi_req_init(sreq); | |
eab40cf3 BB |
231 | |
232 | sreq->sense = sense; | |
50b4d485 | 233 | sreq->sense_len = SCSI_SENSE_BUFFERSIZE; |
50b4d485 BB |
234 | |
235 | job->req = req; | |
eab40cf3 | 236 | job->reply = sense; |
50b4d485 BB |
237 | job->reply_len = sreq->sense_len; |
238 | job->dd_data = job + 1; | |
50b4d485 BB |
239 | } |
240 | ||
241 | static void bsg_exit_rq(struct request_queue *q, struct request *req) | |
242 | { | |
243 | struct bsg_job *job = blk_mq_rq_to_pdu(req); | |
244 | struct scsi_request *sreq = &job->sreq; | |
245 | ||
246 | kfree(sreq->sense); | |
247 | } | |
248 | ||
aa387cc8 MC |
249 | /** |
250 | * bsg_setup_queue - Create and add the bsg hooks so we can receive requests | |
251 | * @dev: device to attach bsg device to | |
aa387cc8 MC |
252 | * @name: device to give bsg device |
253 | * @job_fn: bsg job handler | |
254 | * @dd_job_size: size of LLD data needed for each job | |
aa98192d | 255 | * @release: @dev release function |
aa387cc8 | 256 | */ |
c1225f01 CH |
257 | struct request_queue *bsg_setup_queue(struct device *dev, const char *name, |
258 | bsg_job_fn *job_fn, int dd_job_size, | |
259 | void (*release)(struct device *)) | |
aa387cc8 | 260 | { |
8ae94eb6 | 261 | struct request_queue *q; |
aa387cc8 MC |
262 | int ret; |
263 | ||
82ed4db4 | 264 | q = blk_alloc_queue(GFP_KERNEL); |
8ae94eb6 CH |
265 | if (!q) |
266 | return ERR_PTR(-ENOMEM); | |
50b4d485 BB |
267 | q->cmd_size = sizeof(struct bsg_job) + dd_job_size; |
268 | q->init_rq_fn = bsg_init_rq; | |
269 | q->exit_rq_fn = bsg_exit_rq; | |
eab40cf3 | 270 | q->initialize_rq_fn = bsg_initialize_rq; |
82ed4db4 CH |
271 | q->request_fn = bsg_request_fn; |
272 | ||
273 | ret = blk_init_allocated_queue(q); | |
274 | if (ret) | |
275 | goto out_cleanup_queue; | |
8ae94eb6 | 276 | |
aa387cc8 | 277 | q->queuedata = dev; |
aa387cc8 | 278 | q->bsg_job_fn = job_fn; |
8b904b5b BVA |
279 | blk_queue_flag_set(QUEUE_FLAG_BIDI, q); |
280 | blk_queue_flag_set(QUEUE_FLAG_SCSI_PASSTHROUGH, q); | |
aa387cc8 MC |
281 | blk_queue_softirq_done(q, bsg_softirq_done); |
282 | blk_queue_rq_timeout(q, BLK_DEFAULT_SG_TIMEOUT); | |
283 | ||
c1225f01 | 284 | ret = bsg_register_queue(q, dev, name, release); |
aa387cc8 MC |
285 | if (ret) { |
286 | printk(KERN_ERR "%s: bsg interface failed to " | |
287 | "initialize - register queue\n", dev->kobj.name); | |
82ed4db4 | 288 | goto out_cleanup_queue; |
aa387cc8 MC |
289 | } |
290 | ||
8ae94eb6 | 291 | return q; |
82ed4db4 CH |
292 | out_cleanup_queue: |
293 | blk_cleanup_queue(q); | |
294 | return ERR_PTR(ret); | |
aa387cc8 MC |
295 | } |
296 | EXPORT_SYMBOL_GPL(bsg_setup_queue); |