blob: 005e2b75d775317ff2546e3574ea35894b1b3319 [file] [log] [blame]
Mike Christieaa387cc2011-07-31 22:05:09 +02001/*
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>
Jens Axboecd2f0762018-10-24 07:11:39 -060024#include <linux/blk-mq.h>
Mike Christieaa387cc2011-07-31 22:05:09 +020025#include <linux/delay.h>
26#include <linux/scatterlist.h>
27#include <linux/bsg-lib.h>
Paul Gortmaker6adb1232011-09-28 18:26:05 -040028#include <linux/export.h>
Mike Christieaa387cc2011-07-31 22:05:09 +020029#include <scsi/scsi_cmnd.h>
Christoph Hellwig17cb9602018-03-13 17:28:41 +010030#include <scsi/sg.h>
31
32#define uptr64(val) ((void __user *)(uintptr_t)(val))
33
Jens Axboe1028e4b2018-10-29 09:47:17 -060034struct bsg_set {
35 struct blk_mq_tag_set tag_set;
36 bsg_job_fn *job_fn;
37 bsg_timeout_fn *timeout_fn;
38};
39
Christoph Hellwig17cb9602018-03-13 17:28:41 +010040static int bsg_transport_check_proto(struct sg_io_v4 *hdr)
41{
42 if (hdr->protocol != BSG_PROTOCOL_SCSI ||
43 hdr->subprotocol != BSG_SUB_PROTOCOL_SCSI_TRANSPORT)
44 return -EINVAL;
45 if (!capable(CAP_SYS_RAWIO))
46 return -EPERM;
47 return 0;
48}
49
50static int bsg_transport_fill_hdr(struct request *rq, struct sg_io_v4 *hdr,
51 fmode_t mode)
52{
53 struct bsg_job *job = blk_mq_rq_to_pdu(rq);
Christoph Hellwig972248e2019-01-29 09:32:03 +010054 int ret;
Christoph Hellwig17cb9602018-03-13 17:28:41 +010055
56 job->request_len = hdr->request_len;
57 job->request = memdup_user(uptr64(hdr->request), hdr->request_len);
Christoph Hellwig972248e2019-01-29 09:32:03 +010058 if (IS_ERR(job->request))
59 return PTR_ERR(job->request);
zhong jiang47255492018-08-01 00:13:14 +080060
Christoph Hellwig972248e2019-01-29 09:32:03 +010061 if (hdr->dout_xfer_len && hdr->din_xfer_len) {
62 job->bidi_rq = blk_get_request(rq->q, REQ_OP_SCSI_IN, 0);
63 if (IS_ERR(job->bidi_rq)) {
64 ret = PTR_ERR(job->bidi_rq);
65 goto out;
66 }
67
68 ret = blk_rq_map_user(rq->q, job->bidi_rq, NULL,
69 uptr64(hdr->din_xferp), hdr->din_xfer_len,
70 GFP_KERNEL);
71 if (ret)
72 goto out_free_bidi_rq;
73
74 job->bidi_bio = job->bidi_rq->bio;
75 } else {
76 job->bidi_rq = NULL;
77 job->bidi_bio = NULL;
78 }
79
80 return 0;
81
82out_free_bidi_rq:
83 if (job->bidi_rq)
84 blk_put_request(job->bidi_rq);
85out:
86 kfree(job->request);
87 return ret;
Christoph Hellwig17cb9602018-03-13 17:28:41 +010088}
89
90static int bsg_transport_complete_rq(struct request *rq, struct sg_io_v4 *hdr)
91{
92 struct bsg_job *job = blk_mq_rq_to_pdu(rq);
93 int ret = 0;
94
95 /*
96 * The assignments below don't make much sense, but are kept for
97 * bug by bug backwards compatibility:
98 */
99 hdr->device_status = job->result & 0xff;
100 hdr->transport_status = host_byte(job->result);
101 hdr->driver_status = driver_byte(job->result);
102 hdr->info = 0;
103 if (hdr->device_status || hdr->transport_status || hdr->driver_status)
104 hdr->info |= SG_INFO_CHECK;
105 hdr->response_len = 0;
106
107 if (job->result < 0) {
108 /* we're only returning the result field in the reply */
109 job->reply_len = sizeof(u32);
110 ret = job->result;
111 }
112
113 if (job->reply_len && hdr->response) {
114 int len = min(hdr->max_response_len, job->reply_len);
115
116 if (copy_to_user(uptr64(hdr->response), job->reply, len))
117 ret = -EFAULT;
118 else
119 hdr->response_len = len;
120 }
121
122 /* we assume all request payload was transferred, residual == 0 */
123 hdr->dout_resid = 0;
124
Christoph Hellwig972248e2019-01-29 09:32:03 +0100125 if (job->bidi_rq) {
Christoph Hellwig17cb9602018-03-13 17:28:41 +0100126 unsigned int rsp_len = job->reply_payload.payload_len;
127
128 if (WARN_ON(job->reply_payload_rcv_len > rsp_len))
129 hdr->din_resid = 0;
130 else
131 hdr->din_resid = rsp_len - job->reply_payload_rcv_len;
132 } else {
133 hdr->din_resid = 0;
134 }
135
136 return ret;
137}
138
139static void bsg_transport_free_rq(struct request *rq)
140{
141 struct bsg_job *job = blk_mq_rq_to_pdu(rq);
142
Christoph Hellwig972248e2019-01-29 09:32:03 +0100143 if (job->bidi_rq) {
144 blk_rq_unmap_user(job->bidi_bio);
145 blk_put_request(job->bidi_rq);
146 }
147
Christoph Hellwig17cb9602018-03-13 17:28:41 +0100148 kfree(job->request);
149}
150
151static const struct bsg_ops bsg_transport_ops = {
152 .check_proto = bsg_transport_check_proto,
153 .fill_hdr = bsg_transport_fill_hdr,
154 .complete_rq = bsg_transport_complete_rq,
155 .free_rq = bsg_transport_free_rq,
156};
Mike Christieaa387cc2011-07-31 22:05:09 +0200157
158/**
Benjamin Block50b4d482017-08-24 01:57:56 +0200159 * bsg_teardown_job - routine to teardown a bsg job
Bart Van Asscheaa981922018-01-09 10:11:00 -0800160 * @kref: kref inside bsg_job that is to be torn down
Mike Christieaa387cc2011-07-31 22:05:09 +0200161 */
Benjamin Block50b4d482017-08-24 01:57:56 +0200162static void bsg_teardown_job(struct kref *kref)
Mike Christieaa387cc2011-07-31 22:05:09 +0200163{
Johannes Thumshirnbf0f2d32016-11-17 10:31:18 +0100164 struct bsg_job *job = container_of(kref, struct bsg_job, kref);
Christoph Hellwigef6fa642018-03-13 17:28:40 +0100165 struct request *rq = blk_mq_rq_from_pdu(job);
Johannes Thumshirnc00da4c2016-11-17 10:31:20 +0100166
Mike Christieaa387cc2011-07-31 22:05:09 +0200167 put_device(job->dev); /* release reference for the request */
168
169 kfree(job->request_payload.sg_list);
170 kfree(job->reply_payload.sg_list);
Benjamin Block50b4d482017-08-24 01:57:56 +0200171
Jens Axboecd2f0762018-10-24 07:11:39 -0600172 blk_mq_end_request(rq, BLK_STS_OK);
Mike Christieaa387cc2011-07-31 22:05:09 +0200173}
174
Johannes Thumshirnfb6f7c82016-11-17 10:31:23 +0100175void bsg_job_put(struct bsg_job *job)
176{
Benjamin Block50b4d482017-08-24 01:57:56 +0200177 kref_put(&job->kref, bsg_teardown_job);
Johannes Thumshirnfb6f7c82016-11-17 10:31:23 +0100178}
179EXPORT_SYMBOL_GPL(bsg_job_put);
180
181int bsg_job_get(struct bsg_job *job)
182{
183 return kref_get_unless_zero(&job->kref);
184}
185EXPORT_SYMBOL_GPL(bsg_job_get);
Mike Christieaa387cc2011-07-31 22:05:09 +0200186
187/**
188 * bsg_job_done - completion routine for bsg requests
189 * @job: bsg_job that is complete
190 * @result: job reply result
191 * @reply_payload_rcv_len: length of payload recvd
192 *
193 * The LLD should call this when the bsg job has completed.
194 */
195void bsg_job_done(struct bsg_job *job, int result,
196 unsigned int reply_payload_rcv_len)
197{
Christoph Hellwig17cb9602018-03-13 17:28:41 +0100198 job->result = result;
199 job->reply_payload_rcv_len = reply_payload_rcv_len;
Jens Axboecd2f0762018-10-24 07:11:39 -0600200 blk_mq_complete_request(blk_mq_rq_from_pdu(job));
Mike Christieaa387cc2011-07-31 22:05:09 +0200201}
202EXPORT_SYMBOL_GPL(bsg_job_done);
203
204/**
Jens Axboecd2f0762018-10-24 07:11:39 -0600205 * bsg_complete - softirq done routine for destroying the bsg requests
Mike Christieaa387cc2011-07-31 22:05:09 +0200206 * @rq: BSG request that holds the job to be destroyed
207 */
Jens Axboecd2f0762018-10-24 07:11:39 -0600208static void bsg_complete(struct request *rq)
Mike Christieaa387cc2011-07-31 22:05:09 +0200209{
Benjamin Block50b4d482017-08-24 01:57:56 +0200210 struct bsg_job *job = blk_mq_rq_to_pdu(rq);
Mike Christieaa387cc2011-07-31 22:05:09 +0200211
Johannes Thumshirnfb6f7c82016-11-17 10:31:23 +0100212 bsg_job_put(job);
Mike Christieaa387cc2011-07-31 22:05:09 +0200213}
214
215static int bsg_map_buffer(struct bsg_buffer *buf, struct request *req)
216{
217 size_t sz = (sizeof(struct scatterlist) * req->nr_phys_segments);
218
219 BUG_ON(!req->nr_phys_segments);
220
221 buf->sg_list = kzalloc(sz, GFP_KERNEL);
222 if (!buf->sg_list)
223 return -ENOMEM;
224 sg_init_table(buf->sg_list, req->nr_phys_segments);
225 buf->sg_cnt = blk_rq_map_sg(req->q, req, buf->sg_list);
226 buf->payload_len = blk_rq_bytes(req);
227 return 0;
228}
229
230/**
Benjamin Block50b4d482017-08-24 01:57:56 +0200231 * bsg_prepare_job - create the bsg_job structure for the bsg request
Mike Christieaa387cc2011-07-31 22:05:09 +0200232 * @dev: device that is being sent the bsg request
233 * @req: BSG request that needs a job structure
234 */
Christoph Hellwig17cb9602018-03-13 17:28:41 +0100235static bool bsg_prepare_job(struct device *dev, struct request *req)
Mike Christieaa387cc2011-07-31 22:05:09 +0200236{
Benjamin Block50b4d482017-08-24 01:57:56 +0200237 struct bsg_job *job = blk_mq_rq_to_pdu(req);
Mike Christieaa387cc2011-07-31 22:05:09 +0200238 int ret;
239
Christoph Hellwig31156ec2018-03-13 17:28:39 +0100240 job->timeout = req->timeout;
Benjamin Block50b4d482017-08-24 01:57:56 +0200241
Mike Christieaa387cc2011-07-31 22:05:09 +0200242 if (req->bio) {
243 ret = bsg_map_buffer(&job->request_payload, req);
244 if (ret)
245 goto failjob_rls_job;
246 }
Christoph Hellwig972248e2019-01-29 09:32:03 +0100247 if (job->bidi_rq) {
248 ret = bsg_map_buffer(&job->reply_payload, job->bidi_rq);
Mike Christieaa387cc2011-07-31 22:05:09 +0200249 if (ret)
250 goto failjob_rls_rqst_payload;
251 }
252 job->dev = dev;
253 /* take a reference for the request */
254 get_device(job->dev);
Johannes Thumshirnbf0f2d32016-11-17 10:31:18 +0100255 kref_init(&job->kref);
Christoph Hellwig17cb9602018-03-13 17:28:41 +0100256 return true;
Mike Christieaa387cc2011-07-31 22:05:09 +0200257
258failjob_rls_rqst_payload:
259 kfree(job->request_payload.sg_list);
260failjob_rls_job:
Christoph Hellwig17cb9602018-03-13 17:28:41 +0100261 job->result = -ENOMEM;
262 return false;
Mike Christieaa387cc2011-07-31 22:05:09 +0200263}
264
Mike Christieaa387cc2011-07-31 22:05:09 +0200265/**
Jens Axboecd2f0762018-10-24 07:11:39 -0600266 * bsg_queue_rq - generic handler for bsg requests
267 * @hctx: hardware queue
268 * @bd: queue data
Mike Christieaa387cc2011-07-31 22:05:09 +0200269 *
270 * On error the create_bsg_job function should return a -Exyz error value
Christoph Hellwig17d53632017-04-20 16:03:01 +0200271 * that will be set to ->result.
Mike Christieaa387cc2011-07-31 22:05:09 +0200272 *
273 * Drivers/subsys should pass this to the queue init function.
274 */
Jens Axboecd2f0762018-10-24 07:11:39 -0600275static blk_status_t bsg_queue_rq(struct blk_mq_hw_ctx *hctx,
276 const struct blk_mq_queue_data *bd)
Mike Christieaa387cc2011-07-31 22:05:09 +0200277{
Jens Axboecd2f0762018-10-24 07:11:39 -0600278 struct request_queue *q = hctx->queue;
Mike Christieaa387cc2011-07-31 22:05:09 +0200279 struct device *dev = q->queuedata;
Jens Axboecd2f0762018-10-24 07:11:39 -0600280 struct request *req = bd->rq;
Jens Axboe1028e4b2018-10-29 09:47:17 -0600281 struct bsg_set *bset =
282 container_of(q->tag_set, struct bsg_set, tag_set);
Mike Christieaa387cc2011-07-31 22:05:09 +0200283 int ret;
284
Jens Axboecd2f0762018-10-24 07:11:39 -0600285 blk_mq_start_request(req);
286
Mike Christieaa387cc2011-07-31 22:05:09 +0200287 if (!get_device(dev))
Jens Axboecd2f0762018-10-24 07:11:39 -0600288 return BLK_STS_IOERR;
Mike Christieaa387cc2011-07-31 22:05:09 +0200289
Jens Axboecd2f0762018-10-24 07:11:39 -0600290 if (!bsg_prepare_job(dev, req))
291 return BLK_STS_IOERR;
Mike Christieaa387cc2011-07-31 22:05:09 +0200292
Jens Axboe1028e4b2018-10-29 09:47:17 -0600293 ret = bset->job_fn(blk_mq_rq_to_pdu(req));
Jens Axboecd2f0762018-10-24 07:11:39 -0600294 if (ret)
295 return BLK_STS_IOERR;
Mike Christieaa387cc2011-07-31 22:05:09 +0200296
Mike Christieaa387cc2011-07-31 22:05:09 +0200297 put_device(dev);
Jens Axboecd2f0762018-10-24 07:11:39 -0600298 return BLK_STS_OK;
Mike Christieaa387cc2011-07-31 22:05:09 +0200299}
Mike Christieaa387cc2011-07-31 22:05:09 +0200300
Christoph Hellwig17cb9602018-03-13 17:28:41 +0100301/* called right after the request is allocated for the request_queue */
Jens Axboecd2f0762018-10-24 07:11:39 -0600302static int bsg_init_rq(struct blk_mq_tag_set *set, struct request *req,
303 unsigned int hctx_idx, unsigned int numa_node)
Benjamin Block50b4d482017-08-24 01:57:56 +0200304{
305 struct bsg_job *job = blk_mq_rq_to_pdu(req);
Benjamin Block50b4d482017-08-24 01:57:56 +0200306
Jens Axboecd2f0762018-10-24 07:11:39 -0600307 job->reply = kzalloc(SCSI_SENSE_BUFFERSIZE, GFP_KERNEL);
Christoph Hellwig17cb9602018-03-13 17:28:41 +0100308 if (!job->reply)
Benjamin Block50b4d482017-08-24 01:57:56 +0200309 return -ENOMEM;
Benjamin Blockeab40cf2017-10-03 12:48:37 +0200310 return 0;
311}
312
Christoph Hellwig17cb9602018-03-13 17:28:41 +0100313/* called right before the request is given to the request_queue user */
Benjamin Blockeab40cf2017-10-03 12:48:37 +0200314static void bsg_initialize_rq(struct request *req)
315{
316 struct bsg_job *job = blk_mq_rq_to_pdu(req);
Christoph Hellwig17cb9602018-03-13 17:28:41 +0100317 void *reply = job->reply;
Benjamin Blockeab40cf2017-10-03 12:48:37 +0200318
319 memset(job, 0, sizeof(*job));
Christoph Hellwig17cb9602018-03-13 17:28:41 +0100320 job->reply = reply;
321 job->reply_len = SCSI_SENSE_BUFFERSIZE;
Benjamin Block50b4d482017-08-24 01:57:56 +0200322 job->dd_data = job + 1;
Benjamin Block50b4d482017-08-24 01:57:56 +0200323}
324
Jens Axboecd2f0762018-10-24 07:11:39 -0600325static void bsg_exit_rq(struct blk_mq_tag_set *set, struct request *req,
326 unsigned int hctx_idx)
Benjamin Block50b4d482017-08-24 01:57:56 +0200327{
328 struct bsg_job *job = blk_mq_rq_to_pdu(req);
Benjamin Block50b4d482017-08-24 01:57:56 +0200329
Christoph Hellwig17cb9602018-03-13 17:28:41 +0100330 kfree(job->reply);
Benjamin Block50b4d482017-08-24 01:57:56 +0200331}
332
Jens Axboe5e28b8d2018-10-26 11:27:02 -0600333void bsg_remove_queue(struct request_queue *q)
334{
335 if (q) {
Jens Axboe1028e4b2018-10-29 09:47:17 -0600336 struct bsg_set *bset =
337 container_of(q->tag_set, struct bsg_set, tag_set);
Jens Axboecd2f0762018-10-24 07:11:39 -0600338
Jens Axboe5e28b8d2018-10-26 11:27:02 -0600339 bsg_unregister_queue(q);
340 blk_cleanup_queue(q);
Jens Axboe1028e4b2018-10-29 09:47:17 -0600341 blk_mq_free_tag_set(&bset->tag_set);
342 kfree(bset);
Jens Axboe5e28b8d2018-10-26 11:27:02 -0600343 }
344}
345EXPORT_SYMBOL_GPL(bsg_remove_queue);
346
Jens Axboecd2f0762018-10-24 07:11:39 -0600347static enum blk_eh_timer_return bsg_timeout(struct request *rq, bool reserved)
348{
Jens Axboe1028e4b2018-10-29 09:47:17 -0600349 struct bsg_set *bset =
350 container_of(rq->q->tag_set, struct bsg_set, tag_set);
Jens Axboecd2f0762018-10-24 07:11:39 -0600351
Jens Axboe1028e4b2018-10-29 09:47:17 -0600352 if (!bset->timeout_fn)
353 return BLK_EH_DONE;
354 return bset->timeout_fn(rq);
Jens Axboecd2f0762018-10-24 07:11:39 -0600355}
356
357static const struct blk_mq_ops bsg_mq_ops = {
358 .queue_rq = bsg_queue_rq,
359 .init_request = bsg_init_rq,
360 .exit_request = bsg_exit_rq,
361 .initialize_rq_fn = bsg_initialize_rq,
362 .complete = bsg_complete,
363 .timeout = bsg_timeout,
364};
365
Mike Christieaa387cc2011-07-31 22:05:09 +0200366/**
367 * bsg_setup_queue - Create and add the bsg hooks so we can receive requests
368 * @dev: device to attach bsg device to
Mike Christieaa387cc2011-07-31 22:05:09 +0200369 * @name: device to give bsg device
370 * @job_fn: bsg job handler
371 * @dd_job_size: size of LLD data needed for each job
Mike Christieaa387cc2011-07-31 22:05:09 +0200372 */
Christoph Hellwigc1225f02017-08-25 17:37:38 +0200373struct request_queue *bsg_setup_queue(struct device *dev, const char *name,
Jens Axboe1028e4b2018-10-29 09:47:17 -0600374 bsg_job_fn *job_fn, bsg_timeout_fn *timeout, int dd_job_size)
Mike Christieaa387cc2011-07-31 22:05:09 +0200375{
Jens Axboe1028e4b2018-10-29 09:47:17 -0600376 struct bsg_set *bset;
Jens Axboecd2f0762018-10-24 07:11:39 -0600377 struct blk_mq_tag_set *set;
Christoph Hellwig8ae94eb2017-01-03 15:25:02 +0300378 struct request_queue *q;
Jens Axboecd2f0762018-10-24 07:11:39 -0600379 int ret = -ENOMEM;
Mike Christieaa387cc2011-07-31 22:05:09 +0200380
Jens Axboe1028e4b2018-10-29 09:47:17 -0600381 bset = kzalloc(sizeof(*bset), GFP_KERNEL);
382 if (!bset)
Christoph Hellwig8ae94eb2017-01-03 15:25:02 +0300383 return ERR_PTR(-ENOMEM);
Christoph Hellwig82ed4db2017-01-27 09:46:29 +0100384
Jens Axboe1028e4b2018-10-29 09:47:17 -0600385 bset->job_fn = job_fn;
386 bset->timeout_fn = timeout;
387
388 set = &bset->tag_set;
Jens Axboecd2f0762018-10-24 07:11:39 -0600389 set->ops = &bsg_mq_ops,
390 set->nr_hw_queues = 1;
391 set->queue_depth = 128;
392 set->numa_node = NUMA_NO_NODE;
393 set->cmd_size = sizeof(struct bsg_job) + dd_job_size;
394 set->flags = BLK_MQ_F_NO_SCHED | BLK_MQ_F_BLOCKING;
395 if (blk_mq_alloc_tag_set(set))
396 goto out_tag_set;
397
398 q = blk_mq_init_queue(set);
399 if (IS_ERR(q)) {
400 ret = PTR_ERR(q);
401 goto out_queue;
402 }
Christoph Hellwig8ae94eb2017-01-03 15:25:02 +0300403
Mike Christieaa387cc2011-07-31 22:05:09 +0200404 q->queuedata = dev;
Mike Christieaa387cc2011-07-31 22:05:09 +0200405 blk_queue_rq_timeout(q, BLK_DEFAULT_SG_TIMEOUT);
406
Christoph Hellwig5de815a2018-05-29 08:40:23 +0200407 ret = bsg_register_queue(q, dev, name, &bsg_transport_ops);
Mike Christieaa387cc2011-07-31 22:05:09 +0200408 if (ret) {
409 printk(KERN_ERR "%s: bsg interface failed to "
410 "initialize - register queue\n", dev->kobj.name);
Christoph Hellwig82ed4db2017-01-27 09:46:29 +0100411 goto out_cleanup_queue;
Mike Christieaa387cc2011-07-31 22:05:09 +0200412 }
413
Christoph Hellwig8ae94eb2017-01-03 15:25:02 +0300414 return q;
Christoph Hellwig82ed4db2017-01-27 09:46:29 +0100415out_cleanup_queue:
416 blk_cleanup_queue(q);
Jens Axboecd2f0762018-10-24 07:11:39 -0600417out_queue:
418 blk_mq_free_tag_set(set);
419out_tag_set:
Jens Axboe1028e4b2018-10-29 09:47:17 -0600420 kfree(bset);
Christoph Hellwig82ed4db2017-01-27 09:46:29 +0100421 return ERR_PTR(ret);
Mike Christieaa387cc2011-07-31 22:05:09 +0200422}
423EXPORT_SYMBOL_GPL(bsg_setup_queue);