ktest.pl: Use run_command to execute sending mail
[linux/fpc-iii.git] / block / bsg-lib.c
blob1474153f73e3e657055001502f39d51bf00e989f
1 /*
2 * BSG helper library
4 * Copyright (C) 2008 James Smart, Emulex Corporation
5 * Copyright (C) 2011 Red Hat, Inc. All rights reserved.
6 * Copyright (C) 2011 Mike Christie
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.
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.
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
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/export.h>
29 #include <scsi/scsi_cmnd.h>
31 /**
32 * bsg_teardown_job - routine to teardown a bsg job
33 * @kref: kref inside bsg_job that is to be torn down
35 static void bsg_teardown_job(struct kref *kref)
37 struct bsg_job *job = container_of(kref, struct bsg_job, kref);
38 struct request *rq = job->req;
40 put_device(job->dev); /* release reference for the request */
42 kfree(job->request_payload.sg_list);
43 kfree(job->reply_payload.sg_list);
45 blk_end_request_all(rq, BLK_STS_OK);
48 void bsg_job_put(struct bsg_job *job)
50 kref_put(&job->kref, bsg_teardown_job);
52 EXPORT_SYMBOL_GPL(bsg_job_put);
54 int bsg_job_get(struct bsg_job *job)
56 return kref_get_unless_zero(&job->kref);
58 EXPORT_SYMBOL_GPL(bsg_job_get);
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
66 * The LLD should call this when the bsg job has completed.
68 void bsg_job_done(struct bsg_job *job, int result,
69 unsigned int reply_payload_rcv_len)
71 struct request *req = job->req;
72 struct request *rsp = req->next_rq;
73 struct scsi_request *rq = scsi_req(req);
74 int err;
76 err = scsi_req(job->req)->result = result;
77 if (err < 0)
78 /* we're only returning the result field in the reply */
79 rq->sense_len = sizeof(u32);
80 else
81 rq->sense_len = job->reply_len;
82 /* we assume all request payload was transferred, residual == 0 */
83 rq->resid_len = 0;
85 if (rsp) {
86 WARN_ON(reply_payload_rcv_len > scsi_req(rsp)->resid_len);
88 /* set reply (bidi) residual */
89 scsi_req(rsp)->resid_len -=
90 min(reply_payload_rcv_len, scsi_req(rsp)->resid_len);
92 blk_complete_request(req);
94 EXPORT_SYMBOL_GPL(bsg_job_done);
96 /**
97 * bsg_softirq_done - softirq done routine for destroying the bsg requests
98 * @rq: BSG request that holds the job to be destroyed
100 static void bsg_softirq_done(struct request *rq)
102 struct bsg_job *job = blk_mq_rq_to_pdu(rq);
104 bsg_job_put(job);
107 static int bsg_map_buffer(struct bsg_buffer *buf, struct request *req)
109 size_t sz = (sizeof(struct scatterlist) * req->nr_phys_segments);
111 BUG_ON(!req->nr_phys_segments);
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);
117 scsi_req(req)->resid_len = blk_rq_bytes(req);
118 buf->sg_cnt = blk_rq_map_sg(req->q, req, buf->sg_list);
119 buf->payload_len = blk_rq_bytes(req);
120 return 0;
124 * bsg_prepare_job - create the bsg_job structure for the bsg request
125 * @dev: device that is being sent the bsg request
126 * @req: BSG request that needs a job structure
128 static int bsg_prepare_job(struct device *dev, struct request *req)
130 struct request *rsp = req->next_rq;
131 struct scsi_request *rq = scsi_req(req);
132 struct bsg_job *job = blk_mq_rq_to_pdu(req);
133 int ret;
135 job->request = rq->cmd;
136 job->request_len = rq->cmd_len;
138 if (req->bio) {
139 ret = bsg_map_buffer(&job->request_payload, req);
140 if (ret)
141 goto failjob_rls_job;
143 if (rsp && rsp->bio) {
144 ret = bsg_map_buffer(&job->reply_payload, rsp);
145 if (ret)
146 goto failjob_rls_rqst_payload;
148 job->dev = dev;
149 /* take a reference for the request */
150 get_device(job->dev);
151 kref_init(&job->kref);
152 return 0;
154 failjob_rls_rqst_payload:
155 kfree(job->request_payload.sg_list);
156 failjob_rls_job:
157 return -ENOMEM;
161 * bsg_request_fn - generic handler for bsg requests
162 * @q: request queue to manage
164 * On error the create_bsg_job function should return a -Exyz error value
165 * that will be set to ->result.
167 * Drivers/subsys should pass this to the queue init function.
169 static void bsg_request_fn(struct request_queue *q)
170 __releases(q->queue_lock)
171 __acquires(q->queue_lock)
173 struct device *dev = q->queuedata;
174 struct request *req;
175 int ret;
177 if (!get_device(dev))
178 return;
180 while (1) {
181 req = blk_fetch_request(q);
182 if (!req)
183 break;
184 spin_unlock_irq(q->queue_lock);
186 ret = bsg_prepare_job(dev, req);
187 if (ret) {
188 scsi_req(req)->result = ret;
189 blk_end_request_all(req, BLK_STS_OK);
190 spin_lock_irq(q->queue_lock);
191 continue;
194 ret = q->bsg_job_fn(blk_mq_rq_to_pdu(req));
195 spin_lock_irq(q->queue_lock);
196 if (ret)
197 break;
200 spin_unlock_irq(q->queue_lock);
201 put_device(dev);
202 spin_lock_irq(q->queue_lock);
205 static int bsg_init_rq(struct request_queue *q, struct request *req, gfp_t gfp)
207 struct bsg_job *job = blk_mq_rq_to_pdu(req);
208 struct scsi_request *sreq = &job->sreq;
210 /* called right after the request is allocated for the request_queue */
212 sreq->sense = kzalloc(SCSI_SENSE_BUFFERSIZE, gfp);
213 if (!sreq->sense)
214 return -ENOMEM;
216 return 0;
219 static void bsg_initialize_rq(struct request *req)
221 struct bsg_job *job = blk_mq_rq_to_pdu(req);
222 struct scsi_request *sreq = &job->sreq;
223 void *sense = sreq->sense;
225 /* called right before the request is given to the request_queue user */
227 memset(job, 0, sizeof(*job));
229 scsi_req_init(sreq);
231 sreq->sense = sense;
232 sreq->sense_len = SCSI_SENSE_BUFFERSIZE;
234 job->req = req;
235 job->reply = sense;
236 job->reply_len = sreq->sense_len;
237 job->dd_data = job + 1;
240 static void bsg_exit_rq(struct request_queue *q, struct request *req)
242 struct bsg_job *job = blk_mq_rq_to_pdu(req);
243 struct scsi_request *sreq = &job->sreq;
245 kfree(sreq->sense);
249 * bsg_setup_queue - Create and add the bsg hooks so we can receive requests
250 * @dev: device to attach bsg device to
251 * @name: device to give bsg device
252 * @job_fn: bsg job handler
253 * @dd_job_size: size of LLD data needed for each job
254 * @release: @dev release function
256 struct request_queue *bsg_setup_queue(struct device *dev, const char *name,
257 bsg_job_fn *job_fn, int dd_job_size,
258 void (*release)(struct device *))
260 struct request_queue *q;
261 int ret;
263 q = blk_alloc_queue(GFP_KERNEL);
264 if (!q)
265 return ERR_PTR(-ENOMEM);
266 q->cmd_size = sizeof(struct bsg_job) + dd_job_size;
267 q->init_rq_fn = bsg_init_rq;
268 q->exit_rq_fn = bsg_exit_rq;
269 q->initialize_rq_fn = bsg_initialize_rq;
270 q->request_fn = bsg_request_fn;
272 ret = blk_init_allocated_queue(q);
273 if (ret)
274 goto out_cleanup_queue;
276 q->queuedata = dev;
277 q->bsg_job_fn = job_fn;
278 queue_flag_set_unlocked(QUEUE_FLAG_BIDI, q);
279 queue_flag_set_unlocked(QUEUE_FLAG_SCSI_PASSTHROUGH, q);
280 blk_queue_softirq_done(q, bsg_softirq_done);
281 blk_queue_rq_timeout(q, BLK_DEFAULT_SG_TIMEOUT);
283 ret = bsg_register_queue(q, dev, name, release);
284 if (ret) {
285 printk(KERN_ERR "%s: bsg interface failed to "
286 "initialize - register queue\n", dev->kobj.name);
287 goto out_cleanup_queue;
290 return q;
291 out_cleanup_queue:
292 blk_cleanup_queue(q);
293 return ERR_PTR(ret);
295 EXPORT_SYMBOL_GPL(bsg_setup_queue);