]>
Commit | Line | Data |
---|---|---|
aa387cc8 MC |
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> | |
6adb1236 | 28 | #include <linux/export.h> |
aa387cc8 MC |
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 | */ | |
fb6f7c8d | 35 | static void bsg_destroy_job(struct kref *kref) |
aa387cc8 | 36 | { |
bf0f2d38 | 37 | struct bsg_job *job = container_of(kref, struct bsg_job, kref); |
c00da4c9 JT |
38 | struct request *rq = job->req; |
39 | ||
40 | blk_end_request_all(rq, rq->errors); | |
bf0f2d38 | 41 | |
aa387cc8 MC |
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 | ||
fb6f7c8d JT |
49 | void bsg_job_put(struct bsg_job *job) |
50 | { | |
51 | kref_put(&job->kref, bsg_destroy_job); | |
52 | } | |
53 | EXPORT_SYMBOL_GPL(bsg_job_put); | |
54 | ||
55 | int bsg_job_get(struct bsg_job *job) | |
56 | { | |
57 | return kref_get_unless_zero(&job->kref); | |
58 | } | |
59 | EXPORT_SYMBOL_GPL(bsg_job_get); | |
aa387cc8 MC |
60 | |
61 | /** | |
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 | |
66 | * | |
67 | * The LLD should call this when the bsg job has completed. | |
68 | */ | |
69 | void bsg_job_done(struct bsg_job *job, int result, | |
70 | unsigned int reply_payload_rcv_len) | |
71 | { | |
72 | struct request *req = job->req; | |
73 | struct request *rsp = req->next_rq; | |
74 | int err; | |
75 | ||
76 | err = job->req->errors = result; | |
77 | if (err < 0) | |
78 | /* we're only returning the result field in the reply */ | |
79 | job->req->sense_len = sizeof(u32); | |
80 | else | |
81 | job->req->sense_len = job->reply_len; | |
82 | /* we assume all request payload was transferred, residual == 0 */ | |
83 | req->resid_len = 0; | |
84 | ||
85 | if (rsp) { | |
86 | WARN_ON(reply_payload_rcv_len > rsp->resid_len); | |
87 | ||
88 | /* set reply (bidi) residual */ | |
89 | rsp->resid_len -= min(reply_payload_rcv_len, rsp->resid_len); | |
90 | } | |
91 | blk_complete_request(req); | |
92 | } | |
93 | EXPORT_SYMBOL_GPL(bsg_job_done); | |
94 | ||
95 | /** | |
96 | * bsg_softirq_done - softirq done routine for destroying the bsg requests | |
97 | * @rq: BSG request that holds the job to be destroyed | |
98 | */ | |
99 | static void bsg_softirq_done(struct request *rq) | |
100 | { | |
101 | struct bsg_job *job = rq->special; | |
102 | ||
fb6f7c8d | 103 | bsg_job_put(job); |
aa387cc8 MC |
104 | } |
105 | ||
106 | static int bsg_map_buffer(struct bsg_buffer *buf, struct request *req) | |
107 | { | |
108 | size_t sz = (sizeof(struct scatterlist) * req->nr_phys_segments); | |
109 | ||
110 | BUG_ON(!req->nr_phys_segments); | |
111 | ||
112 | buf->sg_list = kzalloc(sz, GFP_KERNEL); | |
113 | if (!buf->sg_list) | |
114 | return -ENOMEM; | |
115 | sg_init_table(buf->sg_list, req->nr_phys_segments); | |
116 | buf->sg_cnt = blk_rq_map_sg(req->q, req, buf->sg_list); | |
117 | buf->payload_len = blk_rq_bytes(req); | |
118 | return 0; | |
119 | } | |
120 | ||
121 | /** | |
122 | * bsg_create_job - create the bsg_job structure for the bsg request | |
123 | * @dev: device that is being sent the bsg request | |
124 | * @req: BSG request that needs a job structure | |
125 | */ | |
126 | static int bsg_create_job(struct device *dev, struct request *req) | |
127 | { | |
128 | struct request *rsp = req->next_rq; | |
129 | struct request_queue *q = req->q; | |
130 | struct bsg_job *job; | |
131 | int ret; | |
132 | ||
133 | BUG_ON(req->special); | |
134 | ||
135 | job = kzalloc(sizeof(struct bsg_job) + q->bsg_job_size, GFP_KERNEL); | |
136 | if (!job) | |
137 | return -ENOMEM; | |
138 | ||
139 | req->special = job; | |
140 | job->req = req; | |
141 | if (q->bsg_job_size) | |
142 | job->dd_data = (void *)&job[1]; | |
143 | job->request = req->cmd; | |
144 | job->request_len = req->cmd_len; | |
145 | job->reply = req->sense; | |
146 | job->reply_len = SCSI_SENSE_BUFFERSIZE; /* Size of sense buffer | |
147 | * allocated */ | |
148 | if (req->bio) { | |
149 | ret = bsg_map_buffer(&job->request_payload, req); | |
150 | if (ret) | |
151 | goto failjob_rls_job; | |
152 | } | |
153 | if (rsp && rsp->bio) { | |
154 | ret = bsg_map_buffer(&job->reply_payload, rsp); | |
155 | if (ret) | |
156 | goto failjob_rls_rqst_payload; | |
157 | } | |
158 | job->dev = dev; | |
159 | /* take a reference for the request */ | |
160 | get_device(job->dev); | |
bf0f2d38 | 161 | kref_init(&job->kref); |
aa387cc8 MC |
162 | return 0; |
163 | ||
164 | failjob_rls_rqst_payload: | |
165 | kfree(job->request_payload.sg_list); | |
166 | failjob_rls_job: | |
167 | kfree(job); | |
168 | return -ENOMEM; | |
169 | } | |
170 | ||
aa387cc8 MC |
171 | /** |
172 | * bsg_request_fn - generic handler for bsg requests | |
173 | * @q: request queue to manage | |
174 | * | |
175 | * On error the create_bsg_job function should return a -Exyz error value | |
176 | * that will be set to the req->errors. | |
177 | * | |
178 | * Drivers/subsys should pass this to the queue init function. | |
179 | */ | |
8ae94eb6 | 180 | static void bsg_request_fn(struct request_queue *q) |
dbb3ab03 BVA |
181 | __releases(q->queue_lock) |
182 | __acquires(q->queue_lock) | |
aa387cc8 MC |
183 | { |
184 | struct device *dev = q->queuedata; | |
185 | struct request *req; | |
186 | struct bsg_job *job; | |
187 | int ret; | |
188 | ||
189 | if (!get_device(dev)) | |
190 | return; | |
191 | ||
192 | while (1) { | |
193 | req = blk_fetch_request(q); | |
194 | if (!req) | |
195 | break; | |
196 | spin_unlock_irq(q->queue_lock); | |
197 | ||
198 | ret = bsg_create_job(dev, req); | |
199 | if (ret) { | |
200 | req->errors = ret; | |
201 | blk_end_request_all(req, ret); | |
202 | spin_lock_irq(q->queue_lock); | |
203 | continue; | |
204 | } | |
205 | ||
206 | job = req->special; | |
207 | ret = q->bsg_job_fn(job); | |
208 | spin_lock_irq(q->queue_lock); | |
209 | if (ret) | |
210 | break; | |
211 | } | |
212 | ||
213 | spin_unlock_irq(q->queue_lock); | |
214 | put_device(dev); | |
215 | spin_lock_irq(q->queue_lock); | |
216 | } | |
aa387cc8 MC |
217 | |
218 | /** | |
219 | * bsg_setup_queue - Create and add the bsg hooks so we can receive requests | |
220 | * @dev: device to attach bsg device to | |
aa387cc8 MC |
221 | * @name: device to give bsg device |
222 | * @job_fn: bsg job handler | |
223 | * @dd_job_size: size of LLD data needed for each job | |
aa387cc8 | 224 | */ |
8ae94eb6 CH |
225 | struct request_queue *bsg_setup_queue(struct device *dev, char *name, |
226 | bsg_job_fn *job_fn, int dd_job_size) | |
aa387cc8 | 227 | { |
8ae94eb6 | 228 | struct request_queue *q; |
aa387cc8 MC |
229 | int ret; |
230 | ||
8ae94eb6 CH |
231 | q = blk_init_queue(bsg_request_fn, NULL); |
232 | if (!q) | |
233 | return ERR_PTR(-ENOMEM); | |
234 | ||
aa387cc8 MC |
235 | q->queuedata = dev; |
236 | q->bsg_job_size = dd_job_size; | |
237 | q->bsg_job_fn = job_fn; | |
238 | queue_flag_set_unlocked(QUEUE_FLAG_BIDI, q); | |
239 | blk_queue_softirq_done(q, bsg_softirq_done); | |
240 | blk_queue_rq_timeout(q, BLK_DEFAULT_SG_TIMEOUT); | |
241 | ||
242 | ret = bsg_register_queue(q, dev, name, NULL); | |
243 | if (ret) { | |
244 | printk(KERN_ERR "%s: bsg interface failed to " | |
245 | "initialize - register queue\n", dev->kobj.name); | |
8ae94eb6 CH |
246 | blk_cleanup_queue(q); |
247 | return ERR_PTR(ret); | |
aa387cc8 MC |
248 | } |
249 | ||
8ae94eb6 | 250 | return q; |
aa387cc8 MC |
251 | } |
252 | EXPORT_SYMBOL_GPL(bsg_setup_queue); |