Loading...
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>
28#include <linux/module.h>
29#include <scsi/scsi_cmnd.h>
30
31/**
32 * bsg_destroy_job - routine to teardown/delete a bsg job
33 * @job: bsg_job that is to be torn down
34 */
35static void bsg_destroy_job(struct bsg_job *job)
36{
37 put_device(job->dev); /* release reference for the request */
38
39 kfree(job->request_payload.sg_list);
40 kfree(job->reply_payload.sg_list);
41 kfree(job);
42}
43
44/**
45 * bsg_job_done - completion routine for bsg requests
46 * @job: bsg_job that is complete
47 * @result: job reply result
48 * @reply_payload_rcv_len: length of payload recvd
49 *
50 * The LLD should call this when the bsg job has completed.
51 */
52void bsg_job_done(struct bsg_job *job, int result,
53 unsigned int reply_payload_rcv_len)
54{
55 struct request *req = job->req;
56 struct request *rsp = req->next_rq;
57 int err;
58
59 err = job->req->errors = result;
60 if (err < 0)
61 /* we're only returning the result field in the reply */
62 job->req->sense_len = sizeof(u32);
63 else
64 job->req->sense_len = job->reply_len;
65 /* we assume all request payload was transferred, residual == 0 */
66 req->resid_len = 0;
67
68 if (rsp) {
69 WARN_ON(reply_payload_rcv_len > rsp->resid_len);
70
71 /* set reply (bidi) residual */
72 rsp->resid_len -= min(reply_payload_rcv_len, rsp->resid_len);
73 }
74 blk_complete_request(req);
75}
76EXPORT_SYMBOL_GPL(bsg_job_done);
77
78/**
79 * bsg_softirq_done - softirq done routine for destroying the bsg requests
80 * @rq: BSG request that holds the job to be destroyed
81 */
82static void bsg_softirq_done(struct request *rq)
83{
84 struct bsg_job *job = rq->special;
85
86 blk_end_request_all(rq, rq->errors);
87 bsg_destroy_job(job);
88}
89
90static int bsg_map_buffer(struct bsg_buffer *buf, struct request *req)
91{
92 size_t sz = (sizeof(struct scatterlist) * req->nr_phys_segments);
93
94 BUG_ON(!req->nr_phys_segments);
95
96 buf->sg_list = kzalloc(sz, GFP_KERNEL);
97 if (!buf->sg_list)
98 return -ENOMEM;
99 sg_init_table(buf->sg_list, req->nr_phys_segments);
100 buf->sg_cnt = blk_rq_map_sg(req->q, req, buf->sg_list);
101 buf->payload_len = blk_rq_bytes(req);
102 return 0;
103}
104
105/**
106 * bsg_create_job - create the bsg_job structure for the bsg request
107 * @dev: device that is being sent the bsg request
108 * @req: BSG request that needs a job structure
109 */
110static int bsg_create_job(struct device *dev, struct request *req)
111{
112 struct request *rsp = req->next_rq;
113 struct request_queue *q = req->q;
114 struct bsg_job *job;
115 int ret;
116
117 BUG_ON(req->special);
118
119 job = kzalloc(sizeof(struct bsg_job) + q->bsg_job_size, GFP_KERNEL);
120 if (!job)
121 return -ENOMEM;
122
123 req->special = job;
124 job->req = req;
125 if (q->bsg_job_size)
126 job->dd_data = (void *)&job[1];
127 job->request = req->cmd;
128 job->request_len = req->cmd_len;
129 job->reply = req->sense;
130 job->reply_len = SCSI_SENSE_BUFFERSIZE; /* Size of sense buffer
131 * allocated */
132 if (req->bio) {
133 ret = bsg_map_buffer(&job->request_payload, req);
134 if (ret)
135 goto failjob_rls_job;
136 }
137 if (rsp && rsp->bio) {
138 ret = bsg_map_buffer(&job->reply_payload, rsp);
139 if (ret)
140 goto failjob_rls_rqst_payload;
141 }
142 job->dev = dev;
143 /* take a reference for the request */
144 get_device(job->dev);
145 return 0;
146
147failjob_rls_rqst_payload:
148 kfree(job->request_payload.sg_list);
149failjob_rls_job:
150 kfree(job);
151 return -ENOMEM;
152}
153
154/*
155 * bsg_goose_queue - restart queue in case it was stopped
156 * @q: request q to be restarted
157 */
158void bsg_goose_queue(struct request_queue *q)
159{
160 if (!q)
161 return;
162
163 blk_run_queue_async(q);
164}
165EXPORT_SYMBOL_GPL(bsg_goose_queue);
166
167/**
168 * bsg_request_fn - generic handler for bsg requests
169 * @q: request queue to manage
170 *
171 * On error the create_bsg_job function should return a -Exyz error value
172 * that will be set to the req->errors.
173 *
174 * Drivers/subsys should pass this to the queue init function.
175 */
176void bsg_request_fn(struct request_queue *q)
177{
178 struct device *dev = q->queuedata;
179 struct request *req;
180 struct bsg_job *job;
181 int ret;
182
183 if (!get_device(dev))
184 return;
185
186 while (1) {
187 req = blk_fetch_request(q);
188 if (!req)
189 break;
190 spin_unlock_irq(q->queue_lock);
191
192 ret = bsg_create_job(dev, req);
193 if (ret) {
194 req->errors = ret;
195 blk_end_request_all(req, ret);
196 spin_lock_irq(q->queue_lock);
197 continue;
198 }
199
200 job = req->special;
201 ret = q->bsg_job_fn(job);
202 spin_lock_irq(q->queue_lock);
203 if (ret)
204 break;
205 }
206
207 spin_unlock_irq(q->queue_lock);
208 put_device(dev);
209 spin_lock_irq(q->queue_lock);
210}
211EXPORT_SYMBOL_GPL(bsg_request_fn);
212
213/**
214 * bsg_setup_queue - Create and add the bsg hooks so we can receive requests
215 * @dev: device to attach bsg device to
216 * @q: request queue setup by caller
217 * @name: device to give bsg device
218 * @job_fn: bsg job handler
219 * @dd_job_size: size of LLD data needed for each job
220 *
221 * The caller should have setup the reuqest queue with bsg_request_fn
222 * as the request_fn.
223 */
224int bsg_setup_queue(struct device *dev, struct request_queue *q,
225 char *name, bsg_job_fn *job_fn, int dd_job_size)
226{
227 int ret;
228
229 q->queuedata = dev;
230 q->bsg_job_size = dd_job_size;
231 q->bsg_job_fn = job_fn;
232 queue_flag_set_unlocked(QUEUE_FLAG_BIDI, q);
233 blk_queue_softirq_done(q, bsg_softirq_done);
234 blk_queue_rq_timeout(q, BLK_DEFAULT_SG_TIMEOUT);
235
236 ret = bsg_register_queue(q, dev, name, NULL);
237 if (ret) {
238 printk(KERN_ERR "%s: bsg interface failed to "
239 "initialize - register queue\n", dev->kobj.name);
240 return ret;
241 }
242
243 return 0;
244}
245EXPORT_SYMBOL_GPL(bsg_setup_queue);
246
247/**
248 * bsg_remove_queue - Deletes the bsg dev from the q
249 * @q: the request_queue that is to be torn down.
250 *
251 * Notes:
252 * Before unregistering the queue empty any requests that are blocked
253 */
254void bsg_remove_queue(struct request_queue *q)
255{
256 struct request *req; /* block request */
257 int counts; /* totals for request_list count and starved */
258
259 if (!q)
260 return;
261
262 /* Stop taking in new requests */
263 spin_lock_irq(q->queue_lock);
264 blk_stop_queue(q);
265
266 /* drain all requests in the queue */
267 while (1) {
268 /* need the lock to fetch a request
269 * this may fetch the same reqeust as the previous pass
270 */
271 req = blk_fetch_request(q);
272 /* save requests in use and starved */
273 counts = q->rq.count[0] + q->rq.count[1] +
274 q->rq.starved[0] + q->rq.starved[1];
275 spin_unlock_irq(q->queue_lock);
276 /* any requests still outstanding? */
277 if (counts == 0)
278 break;
279
280 /* This may be the same req as the previous iteration,
281 * always send the blk_end_request_all after a prefetch.
282 * It is not okay to not end the request because the
283 * prefetch started the request.
284 */
285 if (req) {
286 /* return -ENXIO to indicate that this queue is
287 * going away
288 */
289 req->errors = -ENXIO;
290 blk_end_request_all(req, -ENXIO);
291 }
292
293 msleep(200); /* allow bsg to possibly finish */
294 spin_lock_irq(q->queue_lock);
295 }
296 bsg_unregister_queue(q);
297}
298EXPORT_SYMBOL_GPL(bsg_remove_queue);
1// SPDX-License-Identifier: GPL-2.0-or-later
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
8 */
9#include <linux/bsg.h>
10#include <linux/slab.h>
11#include <linux/blk-mq.h>
12#include <linux/delay.h>
13#include <linux/scatterlist.h>
14#include <linux/bsg-lib.h>
15#include <linux/export.h>
16#include <scsi/scsi_cmnd.h>
17#include <scsi/sg.h>
18
19#define uptr64(val) ((void __user *)(uintptr_t)(val))
20
21struct bsg_set {
22 struct blk_mq_tag_set tag_set;
23 struct bsg_device *bd;
24 bsg_job_fn *job_fn;
25 bsg_timeout_fn *timeout_fn;
26};
27
28static int bsg_transport_sg_io_fn(struct request_queue *q, struct sg_io_v4 *hdr,
29 bool open_for_write, unsigned int timeout)
30{
31 struct bsg_job *job;
32 struct request *rq;
33 struct bio *bio;
34 void *reply;
35 int ret;
36
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;
42
43 rq = blk_mq_alloc_request(q, hdr->dout_xfer_len ?
44 REQ_OP_DRV_OUT : REQ_OP_DRV_IN, 0);
45 if (IS_ERR(rq))
46 return PTR_ERR(rq);
47 rq->timeout = timeout;
48
49 job = blk_mq_rq_to_pdu(rq);
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
56 job->request_len = hdr->request_len;
57 job->request = memdup_user(uptr64(hdr->request), hdr->request_len);
58 if (IS_ERR(job->request)) {
59 ret = PTR_ERR(job->request);
60 goto out_free_rq;
61 }
62
63 if (hdr->dout_xfer_len && hdr->din_xfer_len) {
64 job->bidi_rq = blk_mq_alloc_request(rq->q, REQ_OP_DRV_IN, 0);
65 if (IS_ERR(job->bidi_rq)) {
66 ret = PTR_ERR(job->bidi_rq);
67 goto out_free_job_request;
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 }
81
82 ret = 0;
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 }
90
91 if (ret)
92 goto out_unmap_bidi_rq;
93
94 bio = rq->bio;
95 blk_execute_rq(rq, !(hdr->flags & BSG_FLAG_Q_AT_TAIL));
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);
103 hdr->driver_status = 0;
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
127 if (job->bidi_rq) {
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
138 blk_rq_unmap_user(bio);
139out_unmap_bidi_rq:
140 if (job->bidi_rq)
141 blk_rq_unmap_user(job->bidi_bio);
142out_free_bidi_rq:
143 if (job->bidi_rq)
144 blk_mq_free_request(job->bidi_rq);
145out_free_job_request:
146 kfree(job->request);
147out_free_rq:
148 blk_mq_free_request(rq);
149 return ret;
150}
151
152/**
153 * bsg_teardown_job - routine to teardown a bsg job
154 * @kref: kref inside bsg_job that is to be torn down
155 */
156static void bsg_teardown_job(struct kref *kref)
157{
158 struct bsg_job *job = container_of(kref, struct bsg_job, kref);
159 struct request *rq = blk_mq_rq_from_pdu(job);
160
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);
165
166 blk_mq_end_request(rq, BLK_STS_OK);
167}
168
169void bsg_job_put(struct bsg_job *job)
170{
171 kref_put(&job->kref, bsg_teardown_job);
172}
173EXPORT_SYMBOL_GPL(bsg_job_put);
174
175int bsg_job_get(struct bsg_job *job)
176{
177 return kref_get_unless_zero(&job->kref);
178}
179EXPORT_SYMBOL_GPL(bsg_job_get);
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 */
189void bsg_job_done(struct bsg_job *job, int result,
190 unsigned int reply_payload_rcv_len)
191{
192 struct request *rq = blk_mq_rq_from_pdu(job);
193
194 job->result = result;
195 job->reply_payload_rcv_len = reply_payload_rcv_len;
196 if (likely(!blk_should_fake_timeout(rq->q)))
197 blk_mq_complete_request(rq);
198}
199EXPORT_SYMBOL_GPL(bsg_job_done);
200
201/**
202 * bsg_complete - softirq done routine for destroying the bsg requests
203 * @rq: BSG request that holds the job to be destroyed
204 */
205static void bsg_complete(struct request *rq)
206{
207 struct bsg_job *job = blk_mq_rq_to_pdu(rq);
208
209 bsg_job_put(job);
210}
211
212static 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
218 buf->sg_list = kmalloc(sz, GFP_KERNEL);
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/**
228 * bsg_prepare_job - create the bsg_job structure for the bsg request
229 * @dev: device that is being sent the bsg request
230 * @req: BSG request that needs a job structure
231 */
232static bool bsg_prepare_job(struct device *dev, struct request *req)
233{
234 struct bsg_job *job = blk_mq_rq_to_pdu(req);
235 int ret;
236
237 job->timeout = req->timeout;
238
239 if (req->bio) {
240 ret = bsg_map_buffer(&job->request_payload, req);
241 if (ret)
242 goto failjob_rls_job;
243 }
244 if (job->bidi_rq) {
245 ret = bsg_map_buffer(&job->reply_payload, job->bidi_rq);
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);
252 kref_init(&job->kref);
253 return true;
254
255failjob_rls_rqst_payload:
256 kfree(job->request_payload.sg_list);
257failjob_rls_job:
258 job->result = -ENOMEM;
259 return false;
260}
261
262/**
263 * bsg_queue_rq - generic handler for bsg requests
264 * @hctx: hardware queue
265 * @bd: queue data
266 *
267 * On error the create_bsg_job function should return a -Exyz error value
268 * that will be set to ->result.
269 *
270 * Drivers/subsys should pass this to the queue init function.
271 */
272static blk_status_t bsg_queue_rq(struct blk_mq_hw_ctx *hctx,
273 const struct blk_mq_queue_data *bd)
274{
275 struct request_queue *q = hctx->queue;
276 struct device *dev = q->queuedata;
277 struct request *req = bd->rq;
278 struct bsg_set *bset =
279 container_of(q->tag_set, struct bsg_set, tag_set);
280 blk_status_t sts = BLK_STS_IOERR;
281 int ret;
282
283 blk_mq_start_request(req);
284
285 if (!get_device(dev))
286 return BLK_STS_IOERR;
287
288 if (!bsg_prepare_job(dev, req))
289 goto out;
290
291 ret = bset->job_fn(blk_mq_rq_to_pdu(req));
292 if (!ret)
293 sts = BLK_STS_OK;
294
295out:
296 put_device(dev);
297 return sts;
298}
299
300/* called right after the request is allocated for the request_queue */
301static int bsg_init_rq(struct blk_mq_tag_set *set, struct request *req,
302 unsigned int hctx_idx, unsigned int numa_node)
303{
304 struct bsg_job *job = blk_mq_rq_to_pdu(req);
305
306 job->reply = kzalloc(SCSI_SENSE_BUFFERSIZE, GFP_KERNEL);
307 if (!job->reply)
308 return -ENOMEM;
309 return 0;
310}
311
312static void bsg_exit_rq(struct blk_mq_tag_set *set, struct request *req,
313 unsigned int hctx_idx)
314{
315 struct bsg_job *job = blk_mq_rq_to_pdu(req);
316
317 kfree(job->reply);
318}
319
320void bsg_remove_queue(struct request_queue *q)
321{
322 if (q) {
323 struct bsg_set *bset =
324 container_of(q->tag_set, struct bsg_set, tag_set);
325
326 bsg_unregister_queue(bset->bd);
327 blk_mq_destroy_queue(q);
328 blk_put_queue(q);
329 blk_mq_free_tag_set(&bset->tag_set);
330 kfree(bset);
331 }
332}
333EXPORT_SYMBOL_GPL(bsg_remove_queue);
334
335static enum blk_eh_timer_return bsg_timeout(struct request *rq)
336{
337 struct bsg_set *bset =
338 container_of(rq->q->tag_set, struct bsg_set, tag_set);
339
340 if (!bset->timeout_fn)
341 return BLK_EH_DONE;
342 return bset->timeout_fn(rq);
343}
344
345static 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,
349 .complete = bsg_complete,
350 .timeout = bsg_timeout,
351};
352
353/**
354 * bsg_setup_queue - Create and add the bsg hooks so we can receive requests
355 * @dev: device to attach bsg device to
356 * @name: device to give bsg device
357 * @lim: queue limits for the bsg queue
358 * @job_fn: bsg job handler
359 * @timeout: timeout handler function pointer
360 * @dd_job_size: size of LLD data needed for each job
361 */
362struct request_queue *bsg_setup_queue(struct device *dev, const char *name,
363 struct queue_limits *lim, bsg_job_fn *job_fn,
364 bsg_timeout_fn *timeout, int dd_job_size)
365{
366 struct bsg_set *bset;
367 struct blk_mq_tag_set *set;
368 struct request_queue *q;
369 int ret = -ENOMEM;
370
371 bset = kzalloc(sizeof(*bset), GFP_KERNEL);
372 if (!bset)
373 return ERR_PTR(-ENOMEM);
374
375 bset->job_fn = job_fn;
376 bset->timeout_fn = timeout;
377
378 set = &bset->tag_set;
379 set->ops = &bsg_mq_ops;
380 set->nr_hw_queues = 1;
381 set->queue_depth = 128;
382 set->numa_node = NUMA_NO_NODE;
383 set->cmd_size = sizeof(struct bsg_job) + dd_job_size;
384 set->flags = BLK_MQ_F_NO_SCHED | BLK_MQ_F_BLOCKING;
385 if (blk_mq_alloc_tag_set(set))
386 goto out_tag_set;
387
388 q = blk_mq_alloc_queue(set, lim, dev);
389 if (IS_ERR(q)) {
390 ret = PTR_ERR(q);
391 goto out_queue;
392 }
393
394 blk_queue_rq_timeout(q, BLK_DEFAULT_SG_TIMEOUT);
395
396 bset->bd = bsg_register_queue(q, dev, name, bsg_transport_sg_io_fn);
397 if (IS_ERR(bset->bd)) {
398 ret = PTR_ERR(bset->bd);
399 goto out_cleanup_queue;
400 }
401
402 return q;
403out_cleanup_queue:
404 blk_mq_destroy_queue(q);
405 blk_put_queue(q);
406out_queue:
407 blk_mq_free_tag_set(set);
408out_tag_set:
409 kfree(bset);
410 return ERR_PTR(ret);
411}
412EXPORT_SYMBOL_GPL(bsg_setup_queue);