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>
32 * bsg_destroy_job - routine to teardown/delete a bsg job
33 * @job: bsg_job that is to be torn down
35 static void bsg_destroy_job(struct kref *kref)
37 struct bsg_job *job = container_of(kref, struct bsg_job, kref);
38 struct request *rq = job->req;
40 blk_end_request_all(rq, rq->errors);
42 put_device(job->dev); /* release reference for the request */
44 kfree(job->request_payload.sg_list);
45 kfree(job->reply_payload.sg_list);
49 void bsg_job_put(struct bsg_job *job)
51 kref_put(&job->kref, bsg_destroy_job);
53 EXPORT_SYMBOL_GPL(bsg_job_put);
55 int bsg_job_get(struct bsg_job *job)
57 return kref_get_unless_zero(&job->kref);
59 EXPORT_SYMBOL_GPL(bsg_job_get);
62 * bsg_job_done - completion routine for bsg requests
63 * @job: bsg_job that is complete
64 * @result: job reply result
65 * @reply_payload_rcv_len: length of payload recvd
67 * The LLD should call this when the bsg job has completed.
69 void bsg_job_done(struct bsg_job *job, int result,
70 unsigned int reply_payload_rcv_len)
72 struct request *req = job->req;
73 struct request *rsp = req->next_rq;
76 err = job->req->errors = result;
78 /* we're only returning the result field in the reply */
79 job->req->sense_len = sizeof(u32);
81 job->req->sense_len = job->reply_len;
82 /* we assume all request payload was transferred, residual == 0 */
86 WARN_ON(reply_payload_rcv_len > rsp->resid_len);
88 /* set reply (bidi) residual */
89 rsp->resid_len -= min(reply_payload_rcv_len, rsp->resid_len);
91 blk_complete_request(req);
93 EXPORT_SYMBOL_GPL(bsg_job_done);
96 * bsg_softirq_done - softirq done routine for destroying the bsg requests
97 * @rq: BSG request that holds the job to be destroyed
99 void bsg_softirq_done(struct request *rq)
101 struct bsg_job *job = rq->special;
105 EXPORT_SYMBOL_GPL(bsg_softirq_done);
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);
116 sg_init_table(buf->sg_list, req->nr_phys_segments);
117 buf->sg_cnt = blk_rq_map_sg(req->q, req, buf->sg_list);
118 buf->payload_len = blk_rq_bytes(req);
123 * bsg_create_job - create the bsg_job structure for the bsg request
124 * @dev: device that is being sent the bsg request
125 * @req: BSG request that needs a job structure
127 static int bsg_create_job(struct device *dev, struct request *req)
129 struct request *rsp = req->next_rq;
130 struct request_queue *q = req->q;
134 BUG_ON(req->special);
136 job = kzalloc(sizeof(struct bsg_job) + q->bsg_job_size, GFP_KERNEL);
143 job->dd_data = (void *)&job[1];
144 job->request = req->cmd;
145 job->request_len = req->cmd_len;
146 job->reply = req->sense;
147 job->reply_len = SCSI_SENSE_BUFFERSIZE; /* Size of sense buffer
150 ret = bsg_map_buffer(&job->request_payload, req);
152 goto failjob_rls_job;
154 if (rsp && rsp->bio) {
155 ret = bsg_map_buffer(&job->reply_payload, rsp);
157 goto failjob_rls_rqst_payload;
160 /* take a reference for the request */
161 get_device(job->dev);
162 kref_init(&job->kref);
165 failjob_rls_rqst_payload:
166 kfree(job->request_payload.sg_list);
173 * bsg_request_fn - generic handler for bsg requests
174 * @q: request queue to manage
176 * On error the create_bsg_job function should return a -Exyz error value
177 * that will be set to the req->errors.
179 * Drivers/subsys should pass this to the queue init function.
181 void bsg_request_fn(struct request_queue *q)
183 struct device *dev = q->queuedata;
188 if (!get_device(dev))
192 req = blk_fetch_request(q);
195 spin_unlock_irq(q->queue_lock);
197 ret = bsg_create_job(dev, req);
200 blk_end_request_all(req, ret);
201 spin_lock_irq(q->queue_lock);
206 ret = q->bsg_job_fn(job);
207 spin_lock_irq(q->queue_lock);
212 spin_unlock_irq(q->queue_lock);
214 spin_lock_irq(q->queue_lock);
216 EXPORT_SYMBOL_GPL(bsg_request_fn);
219 * bsg_setup_queue - Create and add the bsg hooks so we can receive requests
220 * @dev: device to attach bsg device to
221 * @q: request queue setup by caller
222 * @name: device to give bsg device
223 * @job_fn: bsg job handler
224 * @dd_job_size: size of LLD data needed for each job
226 * The caller should have setup the reuqest queue with bsg_request_fn
229 int bsg_setup_queue(struct device *dev, struct request_queue *q,
230 char *name, bsg_job_fn *job_fn, int dd_job_size)
235 q->bsg_job_size = dd_job_size;
236 q->bsg_job_fn = job_fn;
237 queue_flag_set_unlocked(QUEUE_FLAG_BIDI, q);
238 blk_queue_softirq_done(q, bsg_softirq_done);
239 blk_queue_rq_timeout(q, BLK_DEFAULT_SG_TIMEOUT);
241 ret = bsg_register_queue(q, dev, name, NULL);
243 printk(KERN_ERR "%s: bsg interface failed to "
244 "initialize - register queue\n", dev->kobj.name);
250 EXPORT_SYMBOL_GPL(bsg_setup_queue);