1 // SPDX-License-Identifier: GPL-2.0-or-later
5 * Copyright (C) 2008 James Smart, Emulex Corporation
6 * Copyright (C) 2011 Red Hat, Inc. All rights reserved.
7 * Copyright (C) 2011 Mike Christie
9 #include <linux/slab.h>
10 #include <linux/blk-mq.h>
11 #include <linux/delay.h>
12 #include <linux/scatterlist.h>
13 #include <linux/bsg-lib.h>
14 #include <linux/export.h>
15 #include <scsi/scsi_cmnd.h>
18 #define uptr64(val) ((void __user *)(uintptr_t)(val))
21 struct blk_mq_tag_set tag_set;
23 bsg_timeout_fn *timeout_fn;
26 static int bsg_transport_check_proto(struct sg_io_v4 *hdr)
28 if (hdr->protocol != BSG_PROTOCOL_SCSI ||
29 hdr->subprotocol != BSG_SUB_PROTOCOL_SCSI_TRANSPORT)
31 if (!capable(CAP_SYS_RAWIO))
36 static int bsg_transport_fill_hdr(struct request *rq, struct sg_io_v4 *hdr,
39 struct bsg_job *job = blk_mq_rq_to_pdu(rq);
42 job->request_len = hdr->request_len;
43 job->request = memdup_user(uptr64(hdr->request), hdr->request_len);
44 if (IS_ERR(job->request))
45 return PTR_ERR(job->request);
47 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);
54 ret = blk_rq_map_user(rq->q, job->bidi_rq, NULL,
55 uptr64(hdr->din_xferp), hdr->din_xfer_len,
58 goto out_free_bidi_rq;
60 job->bidi_bio = job->bidi_rq->bio;
70 blk_put_request(job->bidi_rq);
76 static int bsg_transport_complete_rq(struct request *rq, struct sg_io_v4 *hdr)
78 struct bsg_job *job = blk_mq_rq_to_pdu(rq);
82 * The assignments below don't make much sense, but are kept for
83 * bug by bug backwards compatibility:
85 hdr->device_status = job->result & 0xff;
86 hdr->transport_status = host_byte(job->result);
87 hdr->driver_status = driver_byte(job->result);
89 if (hdr->device_status || hdr->transport_status || hdr->driver_status)
90 hdr->info |= SG_INFO_CHECK;
91 hdr->response_len = 0;
93 if (job->result < 0) {
94 /* we're only returning the result field in the reply */
95 job->reply_len = sizeof(u32);
99 if (job->reply_len && hdr->response) {
100 int len = min(hdr->max_response_len, job->reply_len);
102 if (copy_to_user(uptr64(hdr->response), job->reply, len))
105 hdr->response_len = len;
108 /* we assume all request payload was transferred, residual == 0 */
112 unsigned int rsp_len = job->reply_payload.payload_len;
114 if (WARN_ON(job->reply_payload_rcv_len > rsp_len))
117 hdr->din_resid = rsp_len - job->reply_payload_rcv_len;
125 static void bsg_transport_free_rq(struct request *rq)
127 struct bsg_job *job = blk_mq_rq_to_pdu(rq);
130 blk_rq_unmap_user(job->bidi_bio);
131 blk_put_request(job->bidi_rq);
137 static 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,
145 * bsg_teardown_job - routine to teardown a bsg job
146 * @kref: kref inside bsg_job that is to be torn down
148 static void bsg_teardown_job(struct kref *kref)
150 struct bsg_job *job = container_of(kref, struct bsg_job, kref);
151 struct request *rq = blk_mq_rq_from_pdu(job);
153 put_device(job->dev); /* release reference for the request */
155 kfree(job->request_payload.sg_list);
156 kfree(job->reply_payload.sg_list);
158 blk_mq_end_request(rq, BLK_STS_OK);
161 void bsg_job_put(struct bsg_job *job)
163 kref_put(&job->kref, bsg_teardown_job);
165 EXPORT_SYMBOL_GPL(bsg_job_put);
167 int bsg_job_get(struct bsg_job *job)
169 return kref_get_unless_zero(&job->kref);
171 EXPORT_SYMBOL_GPL(bsg_job_get);
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
179 * The LLD should call this when the bsg job has completed.
181 void bsg_job_done(struct bsg_job *job, int result,
182 unsigned int reply_payload_rcv_len)
184 job->result = result;
185 job->reply_payload_rcv_len = reply_payload_rcv_len;
186 blk_mq_complete_request(blk_mq_rq_from_pdu(job));
188 EXPORT_SYMBOL_GPL(bsg_job_done);
191 * bsg_complete - softirq done routine for destroying the bsg requests
192 * @rq: BSG request that holds the job to be destroyed
194 static void bsg_complete(struct request *rq)
196 struct bsg_job *job = blk_mq_rq_to_pdu(rq);
201 static int bsg_map_buffer(struct bsg_buffer *buf, struct request *req)
203 size_t sz = (sizeof(struct scatterlist) * req->nr_phys_segments);
205 BUG_ON(!req->nr_phys_segments);
207 buf->sg_list = kzalloc(sz, GFP_KERNEL);
210 sg_init_table(buf->sg_list, req->nr_phys_segments);
211 buf->sg_cnt = blk_rq_map_sg(req->q, req, buf->sg_list);
212 buf->payload_len = blk_rq_bytes(req);
217 * bsg_prepare_job - create the bsg_job structure for the bsg request
218 * @dev: device that is being sent the bsg request
219 * @req: BSG request that needs a job structure
221 static bool bsg_prepare_job(struct device *dev, struct request *req)
223 struct bsg_job *job = blk_mq_rq_to_pdu(req);
226 job->timeout = req->timeout;
229 ret = bsg_map_buffer(&job->request_payload, req);
231 goto failjob_rls_job;
234 ret = bsg_map_buffer(&job->reply_payload, job->bidi_rq);
236 goto failjob_rls_rqst_payload;
239 /* take a reference for the request */
240 get_device(job->dev);
241 kref_init(&job->kref);
244 failjob_rls_rqst_payload:
245 kfree(job->request_payload.sg_list);
247 job->result = -ENOMEM;
252 * bsg_queue_rq - generic handler for bsg requests
253 * @hctx: hardware queue
256 * On error the create_bsg_job function should return a -Exyz error value
257 * that will be set to ->result.
259 * Drivers/subsys should pass this to the queue init function.
261 static blk_status_t bsg_queue_rq(struct blk_mq_hw_ctx *hctx,
262 const struct blk_mq_queue_data *bd)
264 struct request_queue *q = hctx->queue;
265 struct device *dev = q->queuedata;
266 struct request *req = bd->rq;
267 struct bsg_set *bset =
268 container_of(q->tag_set, struct bsg_set, tag_set);
271 blk_mq_start_request(req);
273 if (!get_device(dev))
274 return BLK_STS_IOERR;
276 if (!bsg_prepare_job(dev, req))
277 return BLK_STS_IOERR;
279 ret = bset->job_fn(blk_mq_rq_to_pdu(req));
281 return BLK_STS_IOERR;
287 /* called right after the request is allocated for the request_queue */
288 static int bsg_init_rq(struct blk_mq_tag_set *set, struct request *req,
289 unsigned int hctx_idx, unsigned int numa_node)
291 struct bsg_job *job = blk_mq_rq_to_pdu(req);
293 job->reply = kzalloc(SCSI_SENSE_BUFFERSIZE, GFP_KERNEL);
299 /* called right before the request is given to the request_queue user */
300 static void bsg_initialize_rq(struct request *req)
302 struct bsg_job *job = blk_mq_rq_to_pdu(req);
303 void *reply = job->reply;
305 memset(job, 0, sizeof(*job));
307 job->reply_len = SCSI_SENSE_BUFFERSIZE;
308 job->dd_data = job + 1;
311 static void bsg_exit_rq(struct blk_mq_tag_set *set, struct request *req,
312 unsigned int hctx_idx)
314 struct bsg_job *job = blk_mq_rq_to_pdu(req);
319 void bsg_remove_queue(struct request_queue *q)
322 struct bsg_set *bset =
323 container_of(q->tag_set, struct bsg_set, tag_set);
325 bsg_unregister_queue(q);
326 blk_cleanup_queue(q);
327 blk_mq_free_tag_set(&bset->tag_set);
331 EXPORT_SYMBOL_GPL(bsg_remove_queue);
333 static enum blk_eh_timer_return bsg_timeout(struct request *rq, bool reserved)
335 struct bsg_set *bset =
336 container_of(rq->q->tag_set, struct bsg_set, tag_set);
338 if (!bset->timeout_fn)
340 return bset->timeout_fn(rq);
343 static const struct blk_mq_ops bsg_mq_ops = {
344 .queue_rq = bsg_queue_rq,
345 .init_request = bsg_init_rq,
346 .exit_request = bsg_exit_rq,
347 .initialize_rq_fn = bsg_initialize_rq,
348 .complete = bsg_complete,
349 .timeout = bsg_timeout,
353 * bsg_setup_queue - Create and add the bsg hooks so we can receive requests
354 * @dev: device to attach bsg device to
355 * @name: device to give bsg device
356 * @job_fn: bsg job handler
357 * @dd_job_size: size of LLD data needed for each job
359 struct request_queue *bsg_setup_queue(struct device *dev, const char *name,
360 bsg_job_fn *job_fn, bsg_timeout_fn *timeout, int dd_job_size)
362 struct bsg_set *bset;
363 struct blk_mq_tag_set *set;
364 struct request_queue *q;
367 bset = kzalloc(sizeof(*bset), GFP_KERNEL);
369 return ERR_PTR(-ENOMEM);
371 bset->job_fn = job_fn;
372 bset->timeout_fn = timeout;
374 set = &bset->tag_set;
375 set->ops = &bsg_mq_ops,
376 set->nr_hw_queues = 1;
377 set->queue_depth = 128;
378 set->numa_node = NUMA_NO_NODE;
379 set->cmd_size = sizeof(struct bsg_job) + dd_job_size;
380 set->flags = BLK_MQ_F_NO_SCHED | BLK_MQ_F_BLOCKING;
381 if (blk_mq_alloc_tag_set(set))
384 q = blk_mq_init_queue(set);
391 blk_queue_rq_timeout(q, BLK_DEFAULT_SG_TIMEOUT);
393 ret = bsg_register_queue(q, dev, name, &bsg_transport_ops);
395 printk(KERN_ERR "%s: bsg interface failed to "
396 "initialize - register queue\n", dev->kobj.name);
397 goto out_cleanup_queue;
402 blk_cleanup_queue(q);
404 blk_mq_free_tag_set(set);
409 EXPORT_SYMBOL_GPL(bsg_setup_queue);