scsi: fc: use bsg_softirq_done
[linux-2.6-microblaze.git] / block / bsg-lib.c
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/export.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  */
35 void bsg_destroy_job(struct kref *kref)
36 {
37         struct bsg_job *job = container_of(kref, struct bsg_job, kref);
38         struct request *rq = job->req;
39
40         blk_end_request_all(rq, rq->errors);
41
42         put_device(job->dev);   /* release reference for the request */
43
44         kfree(job->request_payload.sg_list);
45         kfree(job->reply_payload.sg_list);
46         kfree(job);
47 }
48 EXPORT_SYMBOL_GPL(bsg_destroy_job);
49
50 /**
51  * bsg_job_done - completion routine for bsg requests
52  * @job: bsg_job that is complete
53  * @result: job reply result
54  * @reply_payload_rcv_len: length of payload recvd
55  *
56  * The LLD should call this when the bsg job has completed.
57  */
58 void bsg_job_done(struct bsg_job *job, int result,
59                   unsigned int reply_payload_rcv_len)
60 {
61         struct request *req = job->req;
62         struct request *rsp = req->next_rq;
63         int err;
64
65         err = job->req->errors = result;
66         if (err < 0)
67                 /* we're only returning the result field in the reply */
68                 job->req->sense_len = sizeof(u32);
69         else
70                 job->req->sense_len = job->reply_len;
71         /* we assume all request payload was transferred, residual == 0 */
72         req->resid_len = 0;
73
74         if (rsp) {
75                 WARN_ON(reply_payload_rcv_len > rsp->resid_len);
76
77                 /* set reply (bidi) residual */
78                 rsp->resid_len -= min(reply_payload_rcv_len, rsp->resid_len);
79         }
80         blk_complete_request(req);
81 }
82 EXPORT_SYMBOL_GPL(bsg_job_done);
83
84 /**
85  * bsg_softirq_done - softirq done routine for destroying the bsg requests
86  * @rq: BSG request that holds the job to be destroyed
87  */
88 void bsg_softirq_done(struct request *rq)
89 {
90         struct bsg_job *job = rq->special;
91
92         kref_put(&job->kref, bsg_destroy_job);
93 }
94 EXPORT_SYMBOL_GPL(bsg_softirq_done);
95
96 static int bsg_map_buffer(struct bsg_buffer *buf, struct request *req)
97 {
98         size_t sz = (sizeof(struct scatterlist) * req->nr_phys_segments);
99
100         BUG_ON(!req->nr_phys_segments);
101
102         buf->sg_list = kzalloc(sz, GFP_KERNEL);
103         if (!buf->sg_list)
104                 return -ENOMEM;
105         sg_init_table(buf->sg_list, req->nr_phys_segments);
106         buf->sg_cnt = blk_rq_map_sg(req->q, req, buf->sg_list);
107         buf->payload_len = blk_rq_bytes(req);
108         return 0;
109 }
110
111 /**
112  * bsg_create_job - create the bsg_job structure for the bsg request
113  * @dev: device that is being sent the bsg request
114  * @req: BSG request that needs a job structure
115  */
116 static int bsg_create_job(struct device *dev, struct request *req)
117 {
118         struct request *rsp = req->next_rq;
119         struct request_queue *q = req->q;
120         struct bsg_job *job;
121         int ret;
122
123         BUG_ON(req->special);
124
125         job = kzalloc(sizeof(struct bsg_job) + q->bsg_job_size, GFP_KERNEL);
126         if (!job)
127                 return -ENOMEM;
128
129         req->special = job;
130         job->req = req;
131         if (q->bsg_job_size)
132                 job->dd_data = (void *)&job[1];
133         job->request = req->cmd;
134         job->request_len = req->cmd_len;
135         job->reply = req->sense;
136         job->reply_len = SCSI_SENSE_BUFFERSIZE; /* Size of sense buffer
137                                                  * allocated */
138         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);
151         kref_init(&job->kref);
152         return 0;
153
154 failjob_rls_rqst_payload:
155         kfree(job->request_payload.sg_list);
156 failjob_rls_job:
157         kfree(job);
158         return -ENOMEM;
159 }
160
161 /**
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
166  * that will be set to the req->errors.
167  *
168  * Drivers/subsys should pass this to the queue init function.
169  */
170 void bsg_request_fn(struct request_queue *q)
171 {
172         struct device *dev = q->queuedata;
173         struct request *req;
174         struct bsg_job *job;
175         int ret;
176
177         if (!get_device(dev))
178                 return;
179
180         while (1) {
181                 req = blk_fetch_request(q);
182                 if (!req)
183                         break;
184                 spin_unlock_irq(q->queue_lock);
185
186                 ret = bsg_create_job(dev, req);
187                 if (ret) {
188                         req->errors = ret;
189                         blk_end_request_all(req, ret);
190                         spin_lock_irq(q->queue_lock);
191                         continue;
192                 }
193
194                 job = req->special;
195                 ret = q->bsg_job_fn(job);
196                 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 }
205 EXPORT_SYMBOL_GPL(bsg_request_fn);
206
207 /**
208  * bsg_setup_queue - Create and add the bsg hooks so we can receive requests
209  * @dev: device to attach bsg device to
210  * @q: request queue setup by caller
211  * @name: device to give bsg device
212  * @job_fn: bsg job handler
213  * @dd_job_size: size of LLD data needed for each job
214  *
215  * The caller should have setup the reuqest queue with bsg_request_fn
216  * as the request_fn.
217  */
218 int bsg_setup_queue(struct device *dev, struct request_queue *q,
219                     char *name, bsg_job_fn *job_fn, int dd_job_size)
220 {
221         int ret;
222
223         q->queuedata = dev;
224         q->bsg_job_size = dd_job_size;
225         q->bsg_job_fn = job_fn;
226         queue_flag_set_unlocked(QUEUE_FLAG_BIDI, q);
227         blk_queue_softirq_done(q, bsg_softirq_done);
228         blk_queue_rq_timeout(q, BLK_DEFAULT_SG_TIMEOUT);
229
230         ret = bsg_register_queue(q, dev, name, NULL);
231         if (ret) {
232                 printk(KERN_ERR "%s: bsg interface failed to "
233                        "initialize - register queue\n", dev->kobj.name);
234                 return ret;
235         }
236
237         return 0;
238 }
239 EXPORT_SYMBOL_GPL(bsg_setup_queue);