Commit | Line | Data |
---|---|---|
a497ee34 | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
aa387cc8 MC |
2 | /* |
3 | * BSG helper library | |
4 | * | |
5 | * Copyright (C) 2008 James Smart, Emulex Corporation | |
6 | * Copyright (C) 2011 Red Hat, Inc. All rights reserved. | |
7 | * Copyright (C) 2011 Mike Christie | |
aa387cc8 | 8 | */ |
ead09dd3 | 9 | #include <linux/bsg.h> |
aa387cc8 | 10 | #include <linux/slab.h> |
cd2f076f | 11 | #include <linux/blk-mq.h> |
aa387cc8 MC |
12 | #include <linux/delay.h> |
13 | #include <linux/scatterlist.h> | |
14 | #include <linux/bsg-lib.h> | |
6adb1236 | 15 | #include <linux/export.h> |
aa387cc8 | 16 | #include <scsi/scsi_cmnd.h> |
17cb960f CH |
17 | #include <scsi/sg.h> |
18 | ||
19 | #define uptr64(val) ((void __user *)(uintptr_t)(val)) | |
20 | ||
1028e4b3 JA |
21 | struct bsg_set { |
22 | struct blk_mq_tag_set tag_set; | |
ead09dd3 | 23 | struct bsg_device *bd; |
1028e4b3 JA |
24 | bsg_job_fn *job_fn; |
25 | bsg_timeout_fn *timeout_fn; | |
26 | }; | |
27 | ||
75ca5640 | 28 | static int bsg_transport_sg_io_fn(struct request_queue *q, struct sg_io_v4 *hdr, |
1991299e | 29 | bool open_for_write, unsigned int timeout) |
17cb960f | 30 | { |
75ca5640 CH |
31 | struct bsg_job *job; |
32 | struct request *rq; | |
33 | struct bio *bio; | |
237ea160 | 34 | void *reply; |
75ca5640 CH |
35 | int ret; |
36 | ||
17cb960f CH |
37 | if (hdr->protocol != BSG_PROTOCOL_SCSI || |
38 | hdr->subprotocol != BSG_SUB_PROTOCOL_SCSI_TRANSPORT) | |
39 | return -EINVAL; | |
40 | if (!capable(CAP_SYS_RAWIO)) | |
41 | return -EPERM; | |
17cb960f | 42 | |
237ea160 | 43 | rq = blk_mq_alloc_request(q, hdr->dout_xfer_len ? |
75ca5640 CH |
44 | REQ_OP_DRV_OUT : REQ_OP_DRV_IN, 0); |
45 | if (IS_ERR(rq)) | |
46 | return PTR_ERR(rq); | |
47 | rq->timeout = timeout; | |
17cb960f | 48 | |
75ca5640 | 49 | job = blk_mq_rq_to_pdu(rq); |
237ea160 CH |
50 | reply = job->reply; |
51 | memset(job, 0, sizeof(*job)); | |
52 | job->reply = reply; | |
53 | job->reply_len = SCSI_SENSE_BUFFERSIZE; | |
54 | job->dd_data = job + 1; | |
55 | ||
17cb960f CH |
56 | job->request_len = hdr->request_len; |
57 | job->request = memdup_user(uptr64(hdr->request), hdr->request_len); | |
75ca5640 CH |
58 | if (IS_ERR(job->request)) { |
59 | ret = PTR_ERR(job->request); | |
237ea160 | 60 | goto out_free_rq; |
75ca5640 | 61 | } |
972248e9 CH |
62 | |
63 | if (hdr->dout_xfer_len && hdr->din_xfer_len) { | |
237ea160 | 64 | job->bidi_rq = blk_mq_alloc_request(rq->q, REQ_OP_DRV_IN, 0); |
972248e9 CH |
65 | if (IS_ERR(job->bidi_rq)) { |
66 | ret = PTR_ERR(job->bidi_rq); | |
75ca5640 | 67 | goto out_free_job_request; |
972248e9 CH |
68 | } |
69 | ||
70 | ret = blk_rq_map_user(rq->q, job->bidi_rq, NULL, | |
71 | uptr64(hdr->din_xferp), hdr->din_xfer_len, | |
72 | GFP_KERNEL); | |
73 | if (ret) | |
74 | goto out_free_bidi_rq; | |
75 | ||
76 | job->bidi_bio = job->bidi_rq->bio; | |
77 | } else { | |
78 | job->bidi_rq = NULL; | |
79 | job->bidi_bio = NULL; | |
80 | } | |
47255491 | 81 | |
659a3784 | 82 | ret = 0; |
75ca5640 CH |
83 | if (hdr->dout_xfer_len) { |
84 | ret = blk_rq_map_user(rq->q, rq, NULL, uptr64(hdr->dout_xferp), | |
85 | hdr->dout_xfer_len, GFP_KERNEL); | |
86 | } else if (hdr->din_xfer_len) { | |
87 | ret = blk_rq_map_user(rq->q, rq, NULL, uptr64(hdr->din_xferp), | |
88 | hdr->din_xfer_len, GFP_KERNEL); | |
89 | } | |
972248e9 | 90 | |
75ca5640 CH |
91 | if (ret) |
92 | goto out_unmap_bidi_rq; | |
17cb960f | 93 | |
75ca5640 | 94 | bio = rq->bio; |
b84ba30b | 95 | blk_execute_rq(rq, !(hdr->flags & BSG_FLAG_Q_AT_TAIL)); |
17cb960f CH |
96 | |
97 | /* | |
98 | * The assignments below don't make much sense, but are kept for | |
99 | * bug by bug backwards compatibility: | |
100 | */ | |
101 | hdr->device_status = job->result & 0xff; | |
102 | hdr->transport_status = host_byte(job->result); | |
54c29086 | 103 | hdr->driver_status = 0; |
17cb960f CH |
104 | hdr->info = 0; |
105 | if (hdr->device_status || hdr->transport_status || hdr->driver_status) | |
106 | hdr->info |= SG_INFO_CHECK; | |
107 | hdr->response_len = 0; | |
108 | ||
109 | if (job->result < 0) { | |
110 | /* we're only returning the result field in the reply */ | |
111 | job->reply_len = sizeof(u32); | |
112 | ret = job->result; | |
113 | } | |
114 | ||
115 | if (job->reply_len && hdr->response) { | |
116 | int len = min(hdr->max_response_len, job->reply_len); | |
117 | ||
118 | if (copy_to_user(uptr64(hdr->response), job->reply, len)) | |
119 | ret = -EFAULT; | |
120 | else | |
121 | hdr->response_len = len; | |
122 | } | |
123 | ||
124 | /* we assume all request payload was transferred, residual == 0 */ | |
125 | hdr->dout_resid = 0; | |
126 | ||
972248e9 | 127 | if (job->bidi_rq) { |
17cb960f CH |
128 | unsigned int rsp_len = job->reply_payload.payload_len; |
129 | ||
130 | if (WARN_ON(job->reply_payload_rcv_len > rsp_len)) | |
131 | hdr->din_resid = 0; | |
132 | else | |
133 | hdr->din_resid = rsp_len - job->reply_payload_rcv_len; | |
134 | } else { | |
135 | hdr->din_resid = 0; | |
136 | } | |
137 | ||
75ca5640 CH |
138 | blk_rq_unmap_user(bio); |
139 | out_unmap_bidi_rq: | |
140 | if (job->bidi_rq) | |
972248e9 | 141 | blk_rq_unmap_user(job->bidi_bio); |
75ca5640 CH |
142 | out_free_bidi_rq: |
143 | if (job->bidi_rq) | |
237ea160 | 144 | blk_mq_free_request(job->bidi_rq); |
75ca5640 | 145 | out_free_job_request: |
17cb960f | 146 | kfree(job->request); |
237ea160 CH |
147 | out_free_rq: |
148 | blk_mq_free_request(rq); | |
75ca5640 | 149 | return ret; |
17cb960f CH |
150 | } |
151 | ||
aa387cc8 | 152 | /** |
50b4d485 | 153 | * bsg_teardown_job - routine to teardown a bsg job |
aa98192d | 154 | * @kref: kref inside bsg_job that is to be torn down |
aa387cc8 | 155 | */ |
50b4d485 | 156 | static void bsg_teardown_job(struct kref *kref) |
aa387cc8 | 157 | { |
bf0f2d38 | 158 | struct bsg_job *job = container_of(kref, struct bsg_job, kref); |
ef6fa64f | 159 | struct request *rq = blk_mq_rq_from_pdu(job); |
c00da4c9 | 160 | |
aa387cc8 MC |
161 | put_device(job->dev); /* release reference for the request */ |
162 | ||
163 | kfree(job->request_payload.sg_list); | |
164 | kfree(job->reply_payload.sg_list); | |
50b4d485 | 165 | |
cd2f076f | 166 | blk_mq_end_request(rq, BLK_STS_OK); |
aa387cc8 MC |
167 | } |
168 | ||
fb6f7c8d JT |
169 | void bsg_job_put(struct bsg_job *job) |
170 | { | |
50b4d485 | 171 | kref_put(&job->kref, bsg_teardown_job); |
fb6f7c8d JT |
172 | } |
173 | EXPORT_SYMBOL_GPL(bsg_job_put); | |
174 | ||
175 | int bsg_job_get(struct bsg_job *job) | |
176 | { | |
177 | return kref_get_unless_zero(&job->kref); | |
178 | } | |
179 | EXPORT_SYMBOL_GPL(bsg_job_get); | |
aa387cc8 MC |
180 | |
181 | /** | |
182 | * bsg_job_done - completion routine for bsg requests | |
183 | * @job: bsg_job that is complete | |
184 | * @result: job reply result | |
185 | * @reply_payload_rcv_len: length of payload recvd | |
186 | * | |
187 | * The LLD should call this when the bsg job has completed. | |
188 | */ | |
189 | void bsg_job_done(struct bsg_job *job, int result, | |
190 | unsigned int reply_payload_rcv_len) | |
191 | { | |
15f73f5b CH |
192 | struct request *rq = blk_mq_rq_from_pdu(job); |
193 | ||
17cb960f CH |
194 | job->result = result; |
195 | job->reply_payload_rcv_len = reply_payload_rcv_len; | |
15f73f5b CH |
196 | if (likely(!blk_should_fake_timeout(rq->q))) |
197 | blk_mq_complete_request(rq); | |
aa387cc8 MC |
198 | } |
199 | EXPORT_SYMBOL_GPL(bsg_job_done); | |
200 | ||
201 | /** | |
cd2f076f | 202 | * bsg_complete - softirq done routine for destroying the bsg requests |
aa387cc8 MC |
203 | * @rq: BSG request that holds the job to be destroyed |
204 | */ | |
cd2f076f | 205 | static void bsg_complete(struct request *rq) |
aa387cc8 | 206 | { |
50b4d485 | 207 | struct bsg_job *job = blk_mq_rq_to_pdu(rq); |
aa387cc8 | 208 | |
fb6f7c8d | 209 | bsg_job_put(job); |
aa387cc8 MC |
210 | } |
211 | ||
212 | static int bsg_map_buffer(struct bsg_buffer *buf, struct request *req) | |
213 | { | |
214 | size_t sz = (sizeof(struct scatterlist) * req->nr_phys_segments); | |
215 | ||
216 | BUG_ON(!req->nr_phys_segments); | |
217 | ||
f952eefe | 218 | buf->sg_list = kmalloc(sz, GFP_KERNEL); |
aa387cc8 MC |
219 | if (!buf->sg_list) |
220 | return -ENOMEM; | |
221 | sg_init_table(buf->sg_list, req->nr_phys_segments); | |
222 | buf->sg_cnt = blk_rq_map_sg(req->q, req, buf->sg_list); | |
223 | buf->payload_len = blk_rq_bytes(req); | |
224 | return 0; | |
225 | } | |
226 | ||
227 | /** | |
50b4d485 | 228 | * bsg_prepare_job - create the bsg_job structure for the bsg request |
aa387cc8 MC |
229 | * @dev: device that is being sent the bsg request |
230 | * @req: BSG request that needs a job structure | |
231 | */ | |
17cb960f | 232 | static bool bsg_prepare_job(struct device *dev, struct request *req) |
aa387cc8 | 233 | { |
50b4d485 | 234 | struct bsg_job *job = blk_mq_rq_to_pdu(req); |
aa387cc8 MC |
235 | int ret; |
236 | ||
31156ec3 | 237 | job->timeout = req->timeout; |
50b4d485 | 238 | |
aa387cc8 MC |
239 | if (req->bio) { |
240 | ret = bsg_map_buffer(&job->request_payload, req); | |
241 | if (ret) | |
242 | goto failjob_rls_job; | |
243 | } | |
972248e9 CH |
244 | if (job->bidi_rq) { |
245 | ret = bsg_map_buffer(&job->reply_payload, job->bidi_rq); | |
aa387cc8 MC |
246 | if (ret) |
247 | goto failjob_rls_rqst_payload; | |
248 | } | |
249 | job->dev = dev; | |
250 | /* take a reference for the request */ | |
251 | get_device(job->dev); | |
bf0f2d38 | 252 | kref_init(&job->kref); |
17cb960f | 253 | return true; |
aa387cc8 MC |
254 | |
255 | failjob_rls_rqst_payload: | |
256 | kfree(job->request_payload.sg_list); | |
257 | failjob_rls_job: | |
17cb960f CH |
258 | job->result = -ENOMEM; |
259 | return false; | |
aa387cc8 MC |
260 | } |
261 | ||
aa387cc8 | 262 | /** |
cd2f076f JA |
263 | * bsg_queue_rq - generic handler for bsg requests |
264 | * @hctx: hardware queue | |
265 | * @bd: queue data | |
aa387cc8 MC |
266 | * |
267 | * On error the create_bsg_job function should return a -Exyz error value | |
17d5363b | 268 | * that will be set to ->result. |
aa387cc8 MC |
269 | * |
270 | * Drivers/subsys should pass this to the queue init function. | |
271 | */ | |
cd2f076f JA |
272 | static blk_status_t bsg_queue_rq(struct blk_mq_hw_ctx *hctx, |
273 | const struct blk_mq_queue_data *bd) | |
aa387cc8 | 274 | { |
cd2f076f | 275 | struct request_queue *q = hctx->queue; |
aa387cc8 | 276 | struct device *dev = q->queuedata; |
cd2f076f | 277 | struct request *req = bd->rq; |
1028e4b3 JA |
278 | struct bsg_set *bset = |
279 | container_of(q->tag_set, struct bsg_set, tag_set); | |
c44a4edb | 280 | blk_status_t sts = BLK_STS_IOERR; |
aa387cc8 MC |
281 | int ret; |
282 | ||
cd2f076f JA |
283 | blk_mq_start_request(req); |
284 | ||
aa387cc8 | 285 | if (!get_device(dev)) |
cd2f076f JA |
286 | return BLK_STS_IOERR; |
287 | ||
288 | if (!bsg_prepare_job(dev, req)) | |
d46fe2cb | 289 | goto out; |
cd2f076f | 290 | |
1028e4b3 | 291 | ret = bset->job_fn(blk_mq_rq_to_pdu(req)); |
d46fe2cb MW |
292 | if (!ret) |
293 | sts = BLK_STS_OK; | |
aa387cc8 | 294 | |
d46fe2cb | 295 | out: |
aa387cc8 | 296 | put_device(dev); |
d46fe2cb | 297 | return sts; |
aa387cc8 | 298 | } |
aa387cc8 | 299 | |
17cb960f | 300 | /* called right after the request is allocated for the request_queue */ |
cd2f076f JA |
301 | static int bsg_init_rq(struct blk_mq_tag_set *set, struct request *req, |
302 | unsigned int hctx_idx, unsigned int numa_node) | |
50b4d485 BB |
303 | { |
304 | struct bsg_job *job = blk_mq_rq_to_pdu(req); | |
eab40cf3 | 305 | |
cd2f076f | 306 | job->reply = kzalloc(SCSI_SENSE_BUFFERSIZE, GFP_KERNEL); |
17cb960f | 307 | if (!job->reply) |
eab40cf3 | 308 | return -ENOMEM; |
eab40cf3 BB |
309 | return 0; |
310 | } | |
311 | ||
cd2f076f JA |
312 | static void bsg_exit_rq(struct blk_mq_tag_set *set, struct request *req, |
313 | unsigned int hctx_idx) | |
50b4d485 BB |
314 | { |
315 | struct bsg_job *job = blk_mq_rq_to_pdu(req); | |
50b4d485 | 316 | |
17cb960f | 317 | kfree(job->reply); |
50b4d485 BB |
318 | } |
319 | ||
5e28b8d8 JA |
320 | void bsg_remove_queue(struct request_queue *q) |
321 | { | |
322 | if (q) { | |
1028e4b3 JA |
323 | struct bsg_set *bset = |
324 | container_of(q->tag_set, struct bsg_set, tag_set); | |
cd2f076f | 325 | |
ead09dd3 | 326 | bsg_unregister_queue(bset->bd); |
6f8191fd | 327 | blk_mq_destroy_queue(q); |
2b3f056f | 328 | blk_put_queue(q); |
1028e4b3 JA |
329 | blk_mq_free_tag_set(&bset->tag_set); |
330 | kfree(bset); | |
5e28b8d8 JA |
331 | } |
332 | } | |
333 | EXPORT_SYMBOL_GPL(bsg_remove_queue); | |
334 | ||
9bdb4833 | 335 | static enum blk_eh_timer_return bsg_timeout(struct request *rq) |
cd2f076f | 336 | { |
1028e4b3 JA |
337 | struct bsg_set *bset = |
338 | container_of(rq->q->tag_set, struct bsg_set, tag_set); | |
cd2f076f | 339 | |
1028e4b3 JA |
340 | if (!bset->timeout_fn) |
341 | return BLK_EH_DONE; | |
342 | return bset->timeout_fn(rq); | |
cd2f076f JA |
343 | } |
344 | ||
345 | static const struct blk_mq_ops bsg_mq_ops = { | |
346 | .queue_rq = bsg_queue_rq, | |
347 | .init_request = bsg_init_rq, | |
348 | .exit_request = bsg_exit_rq, | |
cd2f076f JA |
349 | .complete = bsg_complete, |
350 | .timeout = bsg_timeout, | |
351 | }; | |
352 | ||
aa387cc8 MC |
353 | /** |
354 | * bsg_setup_queue - Create and add the bsg hooks so we can receive requests | |
355 | * @dev: device to attach bsg device to | |
aa387cc8 MC |
356 | * @name: device to give bsg device |
357 | * @job_fn: bsg job handler | |
a0b77e36 | 358 | * @timeout: timeout handler function pointer |
aa387cc8 | 359 | * @dd_job_size: size of LLD data needed for each job |
aa387cc8 | 360 | */ |
c1225f01 | 361 | struct request_queue *bsg_setup_queue(struct device *dev, const char *name, |
1028e4b3 | 362 | bsg_job_fn *job_fn, bsg_timeout_fn *timeout, int dd_job_size) |
aa387cc8 | 363 | { |
1028e4b3 | 364 | struct bsg_set *bset; |
cd2f076f | 365 | struct blk_mq_tag_set *set; |
8ae94eb6 | 366 | struct request_queue *q; |
cd2f076f | 367 | int ret = -ENOMEM; |
aa387cc8 | 368 | |
1028e4b3 JA |
369 | bset = kzalloc(sizeof(*bset), GFP_KERNEL); |
370 | if (!bset) | |
8ae94eb6 | 371 | return ERR_PTR(-ENOMEM); |
82ed4db4 | 372 | |
1028e4b3 JA |
373 | bset->job_fn = job_fn; |
374 | bset->timeout_fn = timeout; | |
375 | ||
376 | set = &bset->tag_set; | |
03ef5941 | 377 | set->ops = &bsg_mq_ops; |
cd2f076f JA |
378 | set->nr_hw_queues = 1; |
379 | set->queue_depth = 128; | |
380 | set->numa_node = NUMA_NO_NODE; | |
381 | set->cmd_size = sizeof(struct bsg_job) + dd_job_size; | |
382 | set->flags = BLK_MQ_F_NO_SCHED | BLK_MQ_F_BLOCKING; | |
383 | if (blk_mq_alloc_tag_set(set)) | |
384 | goto out_tag_set; | |
385 | ||
386 | q = blk_mq_init_queue(set); | |
387 | if (IS_ERR(q)) { | |
388 | ret = PTR_ERR(q); | |
389 | goto out_queue; | |
390 | } | |
8ae94eb6 | 391 | |
aa387cc8 | 392 | q->queuedata = dev; |
aa387cc8 MC |
393 | blk_queue_rq_timeout(q, BLK_DEFAULT_SG_TIMEOUT); |
394 | ||
75ca5640 | 395 | bset->bd = bsg_register_queue(q, dev, name, bsg_transport_sg_io_fn); |
ead09dd3 CH |
396 | if (IS_ERR(bset->bd)) { |
397 | ret = PTR_ERR(bset->bd); | |
82ed4db4 | 398 | goto out_cleanup_queue; |
aa387cc8 MC |
399 | } |
400 | ||
8ae94eb6 | 401 | return q; |
82ed4db4 | 402 | out_cleanup_queue: |
6f8191fd | 403 | blk_mq_destroy_queue(q); |
2b3f056f | 404 | blk_put_queue(q); |
cd2f076f JA |
405 | out_queue: |
406 | blk_mq_free_tag_set(set); | |
407 | out_tag_set: | |
1028e4b3 | 408 | kfree(bset); |
82ed4db4 | 409 | return ERR_PTR(ret); |
aa387cc8 MC |
410 | } |
411 | EXPORT_SYMBOL_GPL(bsg_setup_queue); |