blob: c82408c7cc3c91181f91d32c0147e18781506691 [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>
24#include <linux/blkdev.h>
25#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>
30
31/**
Benjamin Block50b4d482017-08-24 01:57:56 +020032 * bsg_teardown_job - routine to teardown a bsg job
Mike Christieaa387cc2011-07-31 22:05:09 +020033 * @job: bsg_job that is to be torn down
34 */
Benjamin Block50b4d482017-08-24 01:57:56 +020035static void bsg_teardown_job(struct kref *kref)
Mike Christieaa387cc2011-07-31 22:05:09 +020036{
Johannes Thumshirnbf0f2d32016-11-17 10:31:18 +010037 struct bsg_job *job = container_of(kref, struct bsg_job, kref);
Johannes Thumshirnc00da4c2016-11-17 10:31:20 +010038 struct request *rq = job->req;
39
Mike Christieaa387cc2011-07-31 22:05:09 +020040 put_device(job->dev); /* release reference for the request */
41
42 kfree(job->request_payload.sg_list);
43 kfree(job->reply_payload.sg_list);
Benjamin Block50b4d482017-08-24 01:57:56 +020044
45 blk_end_request_all(rq, BLK_STS_OK);
Mike Christieaa387cc2011-07-31 22:05:09 +020046}
47
Johannes Thumshirnfb6f7c82016-11-17 10:31:23 +010048void bsg_job_put(struct bsg_job *job)
49{
Benjamin Block50b4d482017-08-24 01:57:56 +020050 kref_put(&job->kref, bsg_teardown_job);
Johannes Thumshirnfb6f7c82016-11-17 10:31:23 +010051}
52EXPORT_SYMBOL_GPL(bsg_job_put);
53
54int bsg_job_get(struct bsg_job *job)
55{
56 return kref_get_unless_zero(&job->kref);
57}
58EXPORT_SYMBOL_GPL(bsg_job_get);
Mike Christieaa387cc2011-07-31 22:05:09 +020059
60/**
61 * bsg_job_done - completion routine for bsg requests
62 * @job: bsg_job that is complete
63 * @result: job reply result
64 * @reply_payload_rcv_len: length of payload recvd
65 *
66 * The LLD should call this when the bsg job has completed.
67 */
68void bsg_job_done(struct bsg_job *job, int result,
69 unsigned int reply_payload_rcv_len)
70{
71 struct request *req = job->req;
72 struct request *rsp = req->next_rq;
Christoph Hellwig82ed4db2017-01-27 09:46:29 +010073 struct scsi_request *rq = scsi_req(req);
Mike Christieaa387cc2011-07-31 22:05:09 +020074 int err;
75
Christoph Hellwig17d53632017-04-20 16:03:01 +020076 err = scsi_req(job->req)->result = result;
Mike Christieaa387cc2011-07-31 22:05:09 +020077 if (err < 0)
78 /* we're only returning the result field in the reply */
Christoph Hellwig82ed4db2017-01-27 09:46:29 +010079 rq->sense_len = sizeof(u32);
Mike Christieaa387cc2011-07-31 22:05:09 +020080 else
Christoph Hellwig82ed4db2017-01-27 09:46:29 +010081 rq->sense_len = job->reply_len;
Mike Christieaa387cc2011-07-31 22:05:09 +020082 /* we assume all request payload was transferred, residual == 0 */
Christoph Hellwig82ed4db2017-01-27 09:46:29 +010083 rq->resid_len = 0;
Mike Christieaa387cc2011-07-31 22:05:09 +020084
85 if (rsp) {
Christoph Hellwig82ed4db2017-01-27 09:46:29 +010086 WARN_ON(reply_payload_rcv_len > scsi_req(rsp)->resid_len);
Mike Christieaa387cc2011-07-31 22:05:09 +020087
88 /* set reply (bidi) residual */
Christoph Hellwig82ed4db2017-01-27 09:46:29 +010089 scsi_req(rsp)->resid_len -=
90 min(reply_payload_rcv_len, scsi_req(rsp)->resid_len);
Mike Christieaa387cc2011-07-31 22:05:09 +020091 }
92 blk_complete_request(req);
93}
94EXPORT_SYMBOL_GPL(bsg_job_done);
95
96/**
97 * bsg_softirq_done - softirq done routine for destroying the bsg requests
98 * @rq: BSG request that holds the job to be destroyed
99 */
100static void bsg_softirq_done(struct request *rq)
101{
Benjamin Block50b4d482017-08-24 01:57:56 +0200102 struct bsg_job *job = blk_mq_rq_to_pdu(rq);
Mike Christieaa387cc2011-07-31 22:05:09 +0200103
Johannes Thumshirnfb6f7c82016-11-17 10:31:23 +0100104 bsg_job_put(job);
Mike Christieaa387cc2011-07-31 22:05:09 +0200105}
106
107static int bsg_map_buffer(struct bsg_buffer *buf, struct request *req)
108{
109 size_t sz = (sizeof(struct scatterlist) * req->nr_phys_segments);
110
111 BUG_ON(!req->nr_phys_segments);
112
113 buf->sg_list = kzalloc(sz, GFP_KERNEL);
114 if (!buf->sg_list)
115 return -ENOMEM;
116 sg_init_table(buf->sg_list, req->nr_phys_segments);
Christoph Hellwig82ed4db2017-01-27 09:46:29 +0100117 scsi_req(req)->resid_len = blk_rq_bytes(req);
Mike Christieaa387cc2011-07-31 22:05:09 +0200118 buf->sg_cnt = blk_rq_map_sg(req->q, req, buf->sg_list);
119 buf->payload_len = blk_rq_bytes(req);
120 return 0;
121}
122
123/**
Benjamin Block50b4d482017-08-24 01:57:56 +0200124 * bsg_prepare_job - create the bsg_job structure for the bsg request
Mike Christieaa387cc2011-07-31 22:05:09 +0200125 * @dev: device that is being sent the bsg request
126 * @req: BSG request that needs a job structure
127 */
Benjamin Block50b4d482017-08-24 01:57:56 +0200128static int bsg_prepare_job(struct device *dev, struct request *req)
Mike Christieaa387cc2011-07-31 22:05:09 +0200129{
130 struct request *rsp = req->next_rq;
Christoph Hellwig82ed4db2017-01-27 09:46:29 +0100131 struct scsi_request *rq = scsi_req(req);
Benjamin Block50b4d482017-08-24 01:57:56 +0200132 struct bsg_job *job = blk_mq_rq_to_pdu(req);
Mike Christieaa387cc2011-07-31 22:05:09 +0200133 int ret;
134
Christoph Hellwig82ed4db2017-01-27 09:46:29 +0100135 job->request = rq->cmd;
136 job->request_len = rq->cmd_len;
Benjamin Block50b4d482017-08-24 01:57:56 +0200137
Mike Christieaa387cc2011-07-31 22:05:09 +0200138 if (req->bio) {
139 ret = bsg_map_buffer(&job->request_payload, req);
140 if (ret)
141 goto failjob_rls_job;
142 }
143 if (rsp && rsp->bio) {
144 ret = bsg_map_buffer(&job->reply_payload, rsp);
145 if (ret)
146 goto failjob_rls_rqst_payload;
147 }
148 job->dev = dev;
149 /* take a reference for the request */
150 get_device(job->dev);
Johannes Thumshirnbf0f2d32016-11-17 10:31:18 +0100151 kref_init(&job->kref);
Mike Christieaa387cc2011-07-31 22:05:09 +0200152 return 0;
153
154failjob_rls_rqst_payload:
155 kfree(job->request_payload.sg_list);
156failjob_rls_job:
157 kfree(job);
158 return -ENOMEM;
159}
160
Mike Christieaa387cc2011-07-31 22:05:09 +0200161/**
162 * bsg_request_fn - generic handler for bsg requests
163 * @q: request queue to manage
164 *
165 * On error the create_bsg_job function should return a -Exyz error value
Christoph Hellwig17d53632017-04-20 16:03:01 +0200166 * that will be set to ->result.
Mike Christieaa387cc2011-07-31 22:05:09 +0200167 *
168 * Drivers/subsys should pass this to the queue init function.
169 */
Christoph Hellwig8ae94eb2017-01-03 15:25:02 +0300170static void bsg_request_fn(struct request_queue *q)
Bart Van Asschedbb3ab02016-09-25 19:54:38 -0700171 __releases(q->queue_lock)
172 __acquires(q->queue_lock)
Mike Christieaa387cc2011-07-31 22:05:09 +0200173{
174 struct device *dev = q->queuedata;
175 struct request *req;
Mike Christieaa387cc2011-07-31 22:05:09 +0200176 int ret;
177
178 if (!get_device(dev))
179 return;
180
181 while (1) {
182 req = blk_fetch_request(q);
183 if (!req)
184 break;
185 spin_unlock_irq(q->queue_lock);
186
Benjamin Block50b4d482017-08-24 01:57:56 +0200187 ret = bsg_prepare_job(dev, req);
Mike Christieaa387cc2011-07-31 22:05:09 +0200188 if (ret) {
Christoph Hellwig17d53632017-04-20 16:03:01 +0200189 scsi_req(req)->result = ret;
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200190 blk_end_request_all(req, BLK_STS_OK);
Mike Christieaa387cc2011-07-31 22:05:09 +0200191 spin_lock_irq(q->queue_lock);
192 continue;
193 }
194
Benjamin Block50b4d482017-08-24 01:57:56 +0200195 ret = q->bsg_job_fn(blk_mq_rq_to_pdu(req));
Mike Christieaa387cc2011-07-31 22:05:09 +0200196 spin_lock_irq(q->queue_lock);
197 if (ret)
198 break;
199 }
200
201 spin_unlock_irq(q->queue_lock);
202 put_device(dev);
203 spin_lock_irq(q->queue_lock);
204}
Mike Christieaa387cc2011-07-31 22:05:09 +0200205
Benjamin Block50b4d482017-08-24 01:57:56 +0200206static int bsg_init_rq(struct request_queue *q, struct request *req, gfp_t gfp)
207{
208 struct bsg_job *job = blk_mq_rq_to_pdu(req);
209 struct scsi_request *sreq = &job->sreq;
210
211 memset(job, 0, sizeof(*job));
212
213 scsi_req_init(sreq);
214 sreq->sense_len = SCSI_SENSE_BUFFERSIZE;
215 sreq->sense = kzalloc(sreq->sense_len, gfp);
216 if (!sreq->sense)
217 return -ENOMEM;
218
219 job->req = req;
220 job->reply = sreq->sense;
221 job->reply_len = sreq->sense_len;
222 job->dd_data = job + 1;
223
224 return 0;
225}
226
227static void bsg_exit_rq(struct request_queue *q, struct request *req)
228{
229 struct bsg_job *job = blk_mq_rq_to_pdu(req);
230 struct scsi_request *sreq = &job->sreq;
231
232 kfree(sreq->sense);
233}
234
Mike Christieaa387cc2011-07-31 22:05:09 +0200235/**
236 * bsg_setup_queue - Create and add the bsg hooks so we can receive requests
237 * @dev: device to attach bsg device to
Mike Christieaa387cc2011-07-31 22:05:09 +0200238 * @name: device to give bsg device
239 * @job_fn: bsg job handler
240 * @dd_job_size: size of LLD data needed for each job
Mike Christieaa387cc2011-07-31 22:05:09 +0200241 */
Christoph Hellwigc1225f02017-08-25 17:37:38 +0200242struct request_queue *bsg_setup_queue(struct device *dev, const char *name,
243 bsg_job_fn *job_fn, int dd_job_size,
244 void (*release)(struct device *))
Mike Christieaa387cc2011-07-31 22:05:09 +0200245{
Christoph Hellwig8ae94eb2017-01-03 15:25:02 +0300246 struct request_queue *q;
Mike Christieaa387cc2011-07-31 22:05:09 +0200247 int ret;
248
Christoph Hellwig82ed4db2017-01-27 09:46:29 +0100249 q = blk_alloc_queue(GFP_KERNEL);
Christoph Hellwig8ae94eb2017-01-03 15:25:02 +0300250 if (!q)
251 return ERR_PTR(-ENOMEM);
Benjamin Block50b4d482017-08-24 01:57:56 +0200252 q->cmd_size = sizeof(struct bsg_job) + dd_job_size;
253 q->init_rq_fn = bsg_init_rq;
254 q->exit_rq_fn = bsg_exit_rq;
Christoph Hellwig82ed4db2017-01-27 09:46:29 +0100255 q->request_fn = bsg_request_fn;
256
257 ret = blk_init_allocated_queue(q);
258 if (ret)
259 goto out_cleanup_queue;
Christoph Hellwig8ae94eb2017-01-03 15:25:02 +0300260
Mike Christieaa387cc2011-07-31 22:05:09 +0200261 q->queuedata = dev;
Mike Christieaa387cc2011-07-31 22:05:09 +0200262 q->bsg_job_fn = job_fn;
263 queue_flag_set_unlocked(QUEUE_FLAG_BIDI, q);
Bart Van Assche9efc1602017-05-31 14:43:46 -0700264 queue_flag_set_unlocked(QUEUE_FLAG_SCSI_PASSTHROUGH, q);
Mike Christieaa387cc2011-07-31 22:05:09 +0200265 blk_queue_softirq_done(q, bsg_softirq_done);
266 blk_queue_rq_timeout(q, BLK_DEFAULT_SG_TIMEOUT);
267
Christoph Hellwigc1225f02017-08-25 17:37:38 +0200268 ret = bsg_register_queue(q, dev, name, release);
Mike Christieaa387cc2011-07-31 22:05:09 +0200269 if (ret) {
270 printk(KERN_ERR "%s: bsg interface failed to "
271 "initialize - register queue\n", dev->kobj.name);
Christoph Hellwig82ed4db2017-01-27 09:46:29 +0100272 goto out_cleanup_queue;
Mike Christieaa387cc2011-07-31 22:05:09 +0200273 }
274
Christoph Hellwig8ae94eb2017-01-03 15:25:02 +0300275 return q;
Christoph Hellwig82ed4db2017-01-27 09:46:29 +0100276out_cleanup_queue:
277 blk_cleanup_queue(q);
278 return ERR_PTR(ret);
Mike Christieaa387cc2011-07-31 22:05:09 +0200279}
280EXPORT_SYMBOL_GPL(bsg_setup_queue);