blob: fb7b347f80105b142c3577f0c0bde4fc72583c6a [file] [log] [blame]
Christoph Hellwiga497ee32019-04-30 14:42:40 -04001// SPDX-License-Identifier: GPL-2.0-or-later
Mike Christieaa387cc2011-07-31 22:05:09 +02002/*
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
Mike Christieaa387cc2011-07-31 22:05:09 +02008 */
9#include <linux/slab.h>
Jens Axboecd2f0762018-10-24 07:11:39 -060010#include <linux/blk-mq.h>
Mike Christieaa387cc2011-07-31 22:05:09 +020011#include <linux/delay.h>
12#include <linux/scatterlist.h>
13#include <linux/bsg-lib.h>
Paul Gortmaker6adb1232011-09-28 18:26:05 -040014#include <linux/export.h>
Mike Christieaa387cc2011-07-31 22:05:09 +020015#include <scsi/scsi_cmnd.h>
Christoph Hellwig17cb9602018-03-13 17:28:41 +010016#include <scsi/sg.h>
17
18#define uptr64(val) ((void __user *)(uintptr_t)(val))
19
Jens Axboe1028e4b2018-10-29 09:47:17 -060020struct bsg_set {
21 struct blk_mq_tag_set tag_set;
22 bsg_job_fn *job_fn;
23 bsg_timeout_fn *timeout_fn;
24};
25
Christoph Hellwig17cb9602018-03-13 17:28:41 +010026static int bsg_transport_check_proto(struct sg_io_v4 *hdr)
27{
28 if (hdr->protocol != BSG_PROTOCOL_SCSI ||
29 hdr->subprotocol != BSG_SUB_PROTOCOL_SCSI_TRANSPORT)
30 return -EINVAL;
31 if (!capable(CAP_SYS_RAWIO))
32 return -EPERM;
33 return 0;
34}
35
36static int bsg_transport_fill_hdr(struct request *rq, struct sg_io_v4 *hdr,
37 fmode_t mode)
38{
39 struct bsg_job *job = blk_mq_rq_to_pdu(rq);
Christoph Hellwig972248e2019-01-29 09:32:03 +010040 int ret;
Christoph Hellwig17cb9602018-03-13 17:28:41 +010041
42 job->request_len = hdr->request_len;
43 job->request = memdup_user(uptr64(hdr->request), hdr->request_len);
Christoph Hellwig972248e2019-01-29 09:32:03 +010044 if (IS_ERR(job->request))
45 return PTR_ERR(job->request);
zhong jiang47255492018-08-01 00:13:14 +080046
Christoph Hellwig972248e2019-01-29 09:32:03 +010047 if (hdr->dout_xfer_len && hdr->din_xfer_len) {
48 job->bidi_rq = blk_get_request(rq->q, REQ_OP_SCSI_IN, 0);
49 if (IS_ERR(job->bidi_rq)) {
50 ret = PTR_ERR(job->bidi_rq);
51 goto out;
52 }
53
54 ret = blk_rq_map_user(rq->q, job->bidi_rq, NULL,
55 uptr64(hdr->din_xferp), hdr->din_xfer_len,
56 GFP_KERNEL);
57 if (ret)
58 goto out_free_bidi_rq;
59
60 job->bidi_bio = job->bidi_rq->bio;
61 } else {
62 job->bidi_rq = NULL;
63 job->bidi_bio = NULL;
64 }
65
66 return 0;
67
68out_free_bidi_rq:
69 if (job->bidi_rq)
70 blk_put_request(job->bidi_rq);
71out:
72 kfree(job->request);
73 return ret;
Christoph Hellwig17cb9602018-03-13 17:28:41 +010074}
75
76static int bsg_transport_complete_rq(struct request *rq, struct sg_io_v4 *hdr)
77{
78 struct bsg_job *job = blk_mq_rq_to_pdu(rq);
79 int ret = 0;
80
81 /*
82 * The assignments below don't make much sense, but are kept for
83 * bug by bug backwards compatibility:
84 */
85 hdr->device_status = job->result & 0xff;
86 hdr->transport_status = host_byte(job->result);
87 hdr->driver_status = driver_byte(job->result);
88 hdr->info = 0;
89 if (hdr->device_status || hdr->transport_status || hdr->driver_status)
90 hdr->info |= SG_INFO_CHECK;
91 hdr->response_len = 0;
92
93 if (job->result < 0) {
94 /* we're only returning the result field in the reply */
95 job->reply_len = sizeof(u32);
96 ret = job->result;
97 }
98
99 if (job->reply_len && hdr->response) {
100 int len = min(hdr->max_response_len, job->reply_len);
101
102 if (copy_to_user(uptr64(hdr->response), job->reply, len))
103 ret = -EFAULT;
104 else
105 hdr->response_len = len;
106 }
107
108 /* we assume all request payload was transferred, residual == 0 */
109 hdr->dout_resid = 0;
110
Christoph Hellwig972248e2019-01-29 09:32:03 +0100111 if (job->bidi_rq) {
Christoph Hellwig17cb9602018-03-13 17:28:41 +0100112 unsigned int rsp_len = job->reply_payload.payload_len;
113
114 if (WARN_ON(job->reply_payload_rcv_len > rsp_len))
115 hdr->din_resid = 0;
116 else
117 hdr->din_resid = rsp_len - job->reply_payload_rcv_len;
118 } else {
119 hdr->din_resid = 0;
120 }
121
122 return ret;
123}
124
125static void bsg_transport_free_rq(struct request *rq)
126{
127 struct bsg_job *job = blk_mq_rq_to_pdu(rq);
128
Christoph Hellwig972248e2019-01-29 09:32:03 +0100129 if (job->bidi_rq) {
130 blk_rq_unmap_user(job->bidi_bio);
131 blk_put_request(job->bidi_rq);
132 }
133
Christoph Hellwig17cb9602018-03-13 17:28:41 +0100134 kfree(job->request);
135}
136
137static const struct bsg_ops bsg_transport_ops = {
138 .check_proto = bsg_transport_check_proto,
139 .fill_hdr = bsg_transport_fill_hdr,
140 .complete_rq = bsg_transport_complete_rq,
141 .free_rq = bsg_transport_free_rq,
142};
Mike Christieaa387cc2011-07-31 22:05:09 +0200143
144/**
Benjamin Block50b4d482017-08-24 01:57:56 +0200145 * bsg_teardown_job - routine to teardown a bsg job
Bart Van Asscheaa981922018-01-09 10:11:00 -0800146 * @kref: kref inside bsg_job that is to be torn down
Mike Christieaa387cc2011-07-31 22:05:09 +0200147 */
Benjamin Block50b4d482017-08-24 01:57:56 +0200148static void bsg_teardown_job(struct kref *kref)
Mike Christieaa387cc2011-07-31 22:05:09 +0200149{
Johannes Thumshirnbf0f2d32016-11-17 10:31:18 +0100150 struct bsg_job *job = container_of(kref, struct bsg_job, kref);
Christoph Hellwigef6fa642018-03-13 17:28:40 +0100151 struct request *rq = blk_mq_rq_from_pdu(job);
Johannes Thumshirnc00da4c2016-11-17 10:31:20 +0100152
Mike Christieaa387cc2011-07-31 22:05:09 +0200153 put_device(job->dev); /* release reference for the request */
154
155 kfree(job->request_payload.sg_list);
156 kfree(job->reply_payload.sg_list);
Benjamin Block50b4d482017-08-24 01:57:56 +0200157
Jens Axboecd2f0762018-10-24 07:11:39 -0600158 blk_mq_end_request(rq, BLK_STS_OK);
Mike Christieaa387cc2011-07-31 22:05:09 +0200159}
160
Johannes Thumshirnfb6f7c82016-11-17 10:31:23 +0100161void bsg_job_put(struct bsg_job *job)
162{
Benjamin Block50b4d482017-08-24 01:57:56 +0200163 kref_put(&job->kref, bsg_teardown_job);
Johannes Thumshirnfb6f7c82016-11-17 10:31:23 +0100164}
165EXPORT_SYMBOL_GPL(bsg_job_put);
166
167int bsg_job_get(struct bsg_job *job)
168{
169 return kref_get_unless_zero(&job->kref);
170}
171EXPORT_SYMBOL_GPL(bsg_job_get);
Mike Christieaa387cc2011-07-31 22:05:09 +0200172
173/**
174 * bsg_job_done - completion routine for bsg requests
175 * @job: bsg_job that is complete
176 * @result: job reply result
177 * @reply_payload_rcv_len: length of payload recvd
178 *
179 * The LLD should call this when the bsg job has completed.
180 */
181void bsg_job_done(struct bsg_job *job, int result,
182 unsigned int reply_payload_rcv_len)
183{
Christoph Hellwig15f73f52020-06-11 08:44:47 +0200184 struct request *rq = blk_mq_rq_from_pdu(job);
185
Christoph Hellwig17cb9602018-03-13 17:28:41 +0100186 job->result = result;
187 job->reply_payload_rcv_len = reply_payload_rcv_len;
Christoph Hellwig15f73f52020-06-11 08:44:47 +0200188 if (likely(!blk_should_fake_timeout(rq->q)))
189 blk_mq_complete_request(rq);
Mike Christieaa387cc2011-07-31 22:05:09 +0200190}
191EXPORT_SYMBOL_GPL(bsg_job_done);
192
193/**
Jens Axboecd2f0762018-10-24 07:11:39 -0600194 * bsg_complete - softirq done routine for destroying the bsg requests
Mike Christieaa387cc2011-07-31 22:05:09 +0200195 * @rq: BSG request that holds the job to be destroyed
196 */
Jens Axboecd2f0762018-10-24 07:11:39 -0600197static void bsg_complete(struct request *rq)
Mike Christieaa387cc2011-07-31 22:05:09 +0200198{
Benjamin Block50b4d482017-08-24 01:57:56 +0200199 struct bsg_job *job = blk_mq_rq_to_pdu(rq);
Mike Christieaa387cc2011-07-31 22:05:09 +0200200
Johannes Thumshirnfb6f7c82016-11-17 10:31:23 +0100201 bsg_job_put(job);
Mike Christieaa387cc2011-07-31 22:05:09 +0200202}
203
204static int bsg_map_buffer(struct bsg_buffer *buf, struct request *req)
205{
206 size_t sz = (sizeof(struct scatterlist) * req->nr_phys_segments);
207
208 BUG_ON(!req->nr_phys_segments);
209
210 buf->sg_list = kzalloc(sz, GFP_KERNEL);
211 if (!buf->sg_list)
212 return -ENOMEM;
213 sg_init_table(buf->sg_list, req->nr_phys_segments);
214 buf->sg_cnt = blk_rq_map_sg(req->q, req, buf->sg_list);
215 buf->payload_len = blk_rq_bytes(req);
216 return 0;
217}
218
219/**
Benjamin Block50b4d482017-08-24 01:57:56 +0200220 * bsg_prepare_job - create the bsg_job structure for the bsg request
Mike Christieaa387cc2011-07-31 22:05:09 +0200221 * @dev: device that is being sent the bsg request
222 * @req: BSG request that needs a job structure
223 */
Christoph Hellwig17cb9602018-03-13 17:28:41 +0100224static bool bsg_prepare_job(struct device *dev, struct request *req)
Mike Christieaa387cc2011-07-31 22:05:09 +0200225{
Benjamin Block50b4d482017-08-24 01:57:56 +0200226 struct bsg_job *job = blk_mq_rq_to_pdu(req);
Mike Christieaa387cc2011-07-31 22:05:09 +0200227 int ret;
228
Christoph Hellwig31156ec2018-03-13 17:28:39 +0100229 job->timeout = req->timeout;
Benjamin Block50b4d482017-08-24 01:57:56 +0200230
Mike Christieaa387cc2011-07-31 22:05:09 +0200231 if (req->bio) {
232 ret = bsg_map_buffer(&job->request_payload, req);
233 if (ret)
234 goto failjob_rls_job;
235 }
Christoph Hellwig972248e2019-01-29 09:32:03 +0100236 if (job->bidi_rq) {
237 ret = bsg_map_buffer(&job->reply_payload, job->bidi_rq);
Mike Christieaa387cc2011-07-31 22:05:09 +0200238 if (ret)
239 goto failjob_rls_rqst_payload;
240 }
241 job->dev = dev;
242 /* take a reference for the request */
243 get_device(job->dev);
Johannes Thumshirnbf0f2d32016-11-17 10:31:18 +0100244 kref_init(&job->kref);
Christoph Hellwig17cb9602018-03-13 17:28:41 +0100245 return true;
Mike Christieaa387cc2011-07-31 22:05:09 +0200246
247failjob_rls_rqst_payload:
248 kfree(job->request_payload.sg_list);
249failjob_rls_job:
Christoph Hellwig17cb9602018-03-13 17:28:41 +0100250 job->result = -ENOMEM;
251 return false;
Mike Christieaa387cc2011-07-31 22:05:09 +0200252}
253
Mike Christieaa387cc2011-07-31 22:05:09 +0200254/**
Jens Axboecd2f0762018-10-24 07:11:39 -0600255 * bsg_queue_rq - generic handler for bsg requests
256 * @hctx: hardware queue
257 * @bd: queue data
Mike Christieaa387cc2011-07-31 22:05:09 +0200258 *
259 * On error the create_bsg_job function should return a -Exyz error value
Christoph Hellwig17d53632017-04-20 16:03:01 +0200260 * that will be set to ->result.
Mike Christieaa387cc2011-07-31 22:05:09 +0200261 *
262 * Drivers/subsys should pass this to the queue init function.
263 */
Jens Axboecd2f0762018-10-24 07:11:39 -0600264static blk_status_t bsg_queue_rq(struct blk_mq_hw_ctx *hctx,
265 const struct blk_mq_queue_data *bd)
Mike Christieaa387cc2011-07-31 22:05:09 +0200266{
Jens Axboecd2f0762018-10-24 07:11:39 -0600267 struct request_queue *q = hctx->queue;
Mike Christieaa387cc2011-07-31 22:05:09 +0200268 struct device *dev = q->queuedata;
Jens Axboecd2f0762018-10-24 07:11:39 -0600269 struct request *req = bd->rq;
Jens Axboe1028e4b2018-10-29 09:47:17 -0600270 struct bsg_set *bset =
271 container_of(q->tag_set, struct bsg_set, tag_set);
Bart Van Asschec44a4ed2019-12-17 16:23:29 -0800272 blk_status_t sts = BLK_STS_IOERR;
Mike Christieaa387cc2011-07-31 22:05:09 +0200273 int ret;
274
Jens Axboecd2f0762018-10-24 07:11:39 -0600275 blk_mq_start_request(req);
276
Mike Christieaa387cc2011-07-31 22:05:09 +0200277 if (!get_device(dev))
Jens Axboecd2f0762018-10-24 07:11:39 -0600278 return BLK_STS_IOERR;
Mike Christieaa387cc2011-07-31 22:05:09 +0200279
Jens Axboecd2f0762018-10-24 07:11:39 -0600280 if (!bsg_prepare_job(dev, req))
Martin Wilckd46fe2c2019-09-23 14:02:02 +0000281 goto out;
Mike Christieaa387cc2011-07-31 22:05:09 +0200282
Jens Axboe1028e4b2018-10-29 09:47:17 -0600283 ret = bset->job_fn(blk_mq_rq_to_pdu(req));
Martin Wilckd46fe2c2019-09-23 14:02:02 +0000284 if (!ret)
285 sts = BLK_STS_OK;
Mike Christieaa387cc2011-07-31 22:05:09 +0200286
Martin Wilckd46fe2c2019-09-23 14:02:02 +0000287out:
Mike Christieaa387cc2011-07-31 22:05:09 +0200288 put_device(dev);
Martin Wilckd46fe2c2019-09-23 14:02:02 +0000289 return sts;
Mike Christieaa387cc2011-07-31 22:05:09 +0200290}
Mike Christieaa387cc2011-07-31 22:05:09 +0200291
Christoph Hellwig17cb9602018-03-13 17:28:41 +0100292/* called right after the request is allocated for the request_queue */
Jens Axboecd2f0762018-10-24 07:11:39 -0600293static int bsg_init_rq(struct blk_mq_tag_set *set, struct request *req,
294 unsigned int hctx_idx, unsigned int numa_node)
Benjamin Block50b4d482017-08-24 01:57:56 +0200295{
296 struct bsg_job *job = blk_mq_rq_to_pdu(req);
Benjamin Block50b4d482017-08-24 01:57:56 +0200297
Jens Axboecd2f0762018-10-24 07:11:39 -0600298 job->reply = kzalloc(SCSI_SENSE_BUFFERSIZE, GFP_KERNEL);
Christoph Hellwig17cb9602018-03-13 17:28:41 +0100299 if (!job->reply)
Benjamin Block50b4d482017-08-24 01:57:56 +0200300 return -ENOMEM;
Benjamin Blockeab40cf2017-10-03 12:48:37 +0200301 return 0;
302}
303
Christoph Hellwig17cb9602018-03-13 17:28:41 +0100304/* called right before the request is given to the request_queue user */
Benjamin Blockeab40cf2017-10-03 12:48:37 +0200305static void bsg_initialize_rq(struct request *req)
306{
307 struct bsg_job *job = blk_mq_rq_to_pdu(req);
Christoph Hellwig17cb9602018-03-13 17:28:41 +0100308 void *reply = job->reply;
Benjamin Blockeab40cf2017-10-03 12:48:37 +0200309
310 memset(job, 0, sizeof(*job));
Christoph Hellwig17cb9602018-03-13 17:28:41 +0100311 job->reply = reply;
312 job->reply_len = SCSI_SENSE_BUFFERSIZE;
Benjamin Block50b4d482017-08-24 01:57:56 +0200313 job->dd_data = job + 1;
Benjamin Block50b4d482017-08-24 01:57:56 +0200314}
315
Jens Axboecd2f0762018-10-24 07:11:39 -0600316static void bsg_exit_rq(struct blk_mq_tag_set *set, struct request *req,
317 unsigned int hctx_idx)
Benjamin Block50b4d482017-08-24 01:57:56 +0200318{
319 struct bsg_job *job = blk_mq_rq_to_pdu(req);
Benjamin Block50b4d482017-08-24 01:57:56 +0200320
Christoph Hellwig17cb9602018-03-13 17:28:41 +0100321 kfree(job->reply);
Benjamin Block50b4d482017-08-24 01:57:56 +0200322}
323
Jens Axboe5e28b8d2018-10-26 11:27:02 -0600324void bsg_remove_queue(struct request_queue *q)
325{
326 if (q) {
Jens Axboe1028e4b2018-10-29 09:47:17 -0600327 struct bsg_set *bset =
328 container_of(q->tag_set, struct bsg_set, tag_set);
Jens Axboecd2f0762018-10-24 07:11:39 -0600329
Jens Axboe5e28b8d2018-10-26 11:27:02 -0600330 bsg_unregister_queue(q);
331 blk_cleanup_queue(q);
Jens Axboe1028e4b2018-10-29 09:47:17 -0600332 blk_mq_free_tag_set(&bset->tag_set);
333 kfree(bset);
Jens Axboe5e28b8d2018-10-26 11:27:02 -0600334 }
335}
336EXPORT_SYMBOL_GPL(bsg_remove_queue);
337
Jens Axboecd2f0762018-10-24 07:11:39 -0600338static enum blk_eh_timer_return bsg_timeout(struct request *rq, bool reserved)
339{
Jens Axboe1028e4b2018-10-29 09:47:17 -0600340 struct bsg_set *bset =
341 container_of(rq->q->tag_set, struct bsg_set, tag_set);
Jens Axboecd2f0762018-10-24 07:11:39 -0600342
Jens Axboe1028e4b2018-10-29 09:47:17 -0600343 if (!bset->timeout_fn)
344 return BLK_EH_DONE;
345 return bset->timeout_fn(rq);
Jens Axboecd2f0762018-10-24 07:11:39 -0600346}
347
348static const struct blk_mq_ops bsg_mq_ops = {
349 .queue_rq = bsg_queue_rq,
350 .init_request = bsg_init_rq,
351 .exit_request = bsg_exit_rq,
352 .initialize_rq_fn = bsg_initialize_rq,
353 .complete = bsg_complete,
354 .timeout = bsg_timeout,
355};
356
Mike Christieaa387cc2011-07-31 22:05:09 +0200357/**
358 * bsg_setup_queue - Create and add the bsg hooks so we can receive requests
359 * @dev: device to attach bsg device to
Mike Christieaa387cc2011-07-31 22:05:09 +0200360 * @name: device to give bsg device
361 * @job_fn: bsg job handler
Bart Van Asschea0b77e32019-05-30 17:00:51 -0700362 * @timeout: timeout handler function pointer
Mike Christieaa387cc2011-07-31 22:05:09 +0200363 * @dd_job_size: size of LLD data needed for each job
Mike Christieaa387cc2011-07-31 22:05:09 +0200364 */
Christoph Hellwigc1225f02017-08-25 17:37:38 +0200365struct request_queue *bsg_setup_queue(struct device *dev, const char *name,
Jens Axboe1028e4b2018-10-29 09:47:17 -0600366 bsg_job_fn *job_fn, bsg_timeout_fn *timeout, int dd_job_size)
Mike Christieaa387cc2011-07-31 22:05:09 +0200367{
Jens Axboe1028e4b2018-10-29 09:47:17 -0600368 struct bsg_set *bset;
Jens Axboecd2f0762018-10-24 07:11:39 -0600369 struct blk_mq_tag_set *set;
Christoph Hellwig8ae94eb2017-01-03 15:25:02 +0300370 struct request_queue *q;
Jens Axboecd2f0762018-10-24 07:11:39 -0600371 int ret = -ENOMEM;
Mike Christieaa387cc2011-07-31 22:05:09 +0200372
Jens Axboe1028e4b2018-10-29 09:47:17 -0600373 bset = kzalloc(sizeof(*bset), GFP_KERNEL);
374 if (!bset)
Christoph Hellwig8ae94eb2017-01-03 15:25:02 +0300375 return ERR_PTR(-ENOMEM);
Christoph Hellwig82ed4db2017-01-27 09:46:29 +0100376
Jens Axboe1028e4b2018-10-29 09:47:17 -0600377 bset->job_fn = job_fn;
378 bset->timeout_fn = timeout;
379
380 set = &bset->tag_set;
Jens Axboecd2f0762018-10-24 07:11:39 -0600381 set->ops = &bsg_mq_ops,
382 set->nr_hw_queues = 1;
383 set->queue_depth = 128;
384 set->numa_node = NUMA_NO_NODE;
385 set->cmd_size = sizeof(struct bsg_job) + dd_job_size;
386 set->flags = BLK_MQ_F_NO_SCHED | BLK_MQ_F_BLOCKING;
387 if (blk_mq_alloc_tag_set(set))
388 goto out_tag_set;
389
390 q = blk_mq_init_queue(set);
391 if (IS_ERR(q)) {
392 ret = PTR_ERR(q);
393 goto out_queue;
394 }
Christoph Hellwig8ae94eb2017-01-03 15:25:02 +0300395
Mike Christieaa387cc2011-07-31 22:05:09 +0200396 q->queuedata = dev;
Mike Christieaa387cc2011-07-31 22:05:09 +0200397 blk_queue_rq_timeout(q, BLK_DEFAULT_SG_TIMEOUT);
398
Christoph Hellwig5de815a2018-05-29 08:40:23 +0200399 ret = bsg_register_queue(q, dev, name, &bsg_transport_ops);
Mike Christieaa387cc2011-07-31 22:05:09 +0200400 if (ret) {
401 printk(KERN_ERR "%s: bsg interface failed to "
402 "initialize - register queue\n", dev->kobj.name);
Christoph Hellwig82ed4db2017-01-27 09:46:29 +0100403 goto out_cleanup_queue;
Mike Christieaa387cc2011-07-31 22:05:09 +0200404 }
405
Christoph Hellwig8ae94eb2017-01-03 15:25:02 +0300406 return q;
Christoph Hellwig82ed4db2017-01-27 09:46:29 +0100407out_cleanup_queue:
408 blk_cleanup_queue(q);
Jens Axboecd2f0762018-10-24 07:11:39 -0600409out_queue:
410 blk_mq_free_tag_set(set);
411out_tag_set:
Jens Axboe1028e4b2018-10-29 09:47:17 -0600412 kfree(bset);
Christoph Hellwig82ed4db2017-01-27 09:46:29 +0100413 return ERR_PTR(ret);
Mike Christieaa387cc2011-07-31 22:05:09 +0200414}
415EXPORT_SYMBOL_GPL(bsg_setup_queue);