1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
33
34
35static void bsg_destroy_job(struct bsg_job *job)
36{
37 put_device(job->dev);
38
39 kfree(job->request_payload.sg_list);
40 kfree(job->reply_payload.sg_list);
41 kfree(job);
42}
43
44
45
46
47
48
49
50
51
52void bsg_job_done(struct bsg_job *job, int result,
53 unsigned int reply_payload_rcv_len)
54{
55 struct request *req = job->req;
56 struct request *rsp = req->next_rq;
57 int err;
58
59 err = job->req->errors = result;
60 if (err < 0)
61
62 job->req->sense_len = sizeof(u32);
63 else
64 job->req->sense_len = job->reply_len;
65
66 req->resid_len = 0;
67
68 if (rsp) {
69 WARN_ON(reply_payload_rcv_len > rsp->resid_len);
70
71
72 rsp->resid_len -= min(reply_payload_rcv_len, rsp->resid_len);
73 }
74 blk_complete_request(req);
75}
76EXPORT_SYMBOL_GPL(bsg_job_done);
77
78
79
80
81
82static void bsg_softirq_done(struct request *rq)
83{
84 struct bsg_job *job = rq->special;
85
86 blk_end_request_all(rq, rq->errors);
87 bsg_destroy_job(job);
88}
89
90static int bsg_map_buffer(struct bsg_buffer *buf, struct request *req)
91{
92 size_t sz = (sizeof(struct scatterlist) * req->nr_phys_segments);
93
94 BUG_ON(!req->nr_phys_segments);
95
96 buf->sg_list = kzalloc(sz, GFP_KERNEL);
97 if (!buf->sg_list)
98 return -ENOMEM;
99 sg_init_table(buf->sg_list, req->nr_phys_segments);
100 buf->sg_cnt = blk_rq_map_sg(req->q, req, buf->sg_list);
101 buf->payload_len = blk_rq_bytes(req);
102 return 0;
103}
104
105
106
107
108
109
110static int bsg_create_job(struct device *dev, struct request *req)
111{
112 struct request *rsp = req->next_rq;
113 struct request_queue *q = req->q;
114 struct bsg_job *job;
115 int ret;
116
117 BUG_ON(req->special);
118
119 job = kzalloc(sizeof(struct bsg_job) + q->bsg_job_size, GFP_KERNEL);
120 if (!job)
121 return -ENOMEM;
122
123 req->special = job;
124 job->req = req;
125 if (q->bsg_job_size)
126 job->dd_data = (void *)&job[1];
127 job->request = req->cmd;
128 job->request_len = req->cmd_len;
129 job->reply = req->sense;
130 job->reply_len = SCSI_SENSE_BUFFERSIZE;
131
132 if (req->bio) {
133 ret = bsg_map_buffer(&job->request_payload, req);
134 if (ret)
135 goto failjob_rls_job;
136 }
137 if (rsp && rsp->bio) {
138 ret = bsg_map_buffer(&job->reply_payload, rsp);
139 if (ret)
140 goto failjob_rls_rqst_payload;
141 }
142 job->dev = dev;
143
144 get_device(job->dev);
145 return 0;
146
147failjob_rls_rqst_payload:
148 kfree(job->request_payload.sg_list);
149failjob_rls_job:
150 kfree(job);
151 return -ENOMEM;
152}
153
154
155
156
157
158
159
160
161
162
163void bsg_request_fn(struct request_queue *q)
164{
165 struct device *dev = q->queuedata;
166 struct request *req;
167 struct bsg_job *job;
168 int ret;
169
170 if (!get_device(dev))
171 return;
172
173 while (1) {
174 req = blk_fetch_request(q);
175 if (!req)
176 break;
177 spin_unlock_irq(q->queue_lock);
178
179 ret = bsg_create_job(dev, req);
180 if (ret) {
181 req->errors = ret;
182 blk_end_request_all(req, ret);
183 spin_lock_irq(q->queue_lock);
184 continue;
185 }
186
187 job = req->special;
188 ret = q->bsg_job_fn(job);
189 spin_lock_irq(q->queue_lock);
190 if (ret)
191 break;
192 }
193
194 spin_unlock_irq(q->queue_lock);
195 put_device(dev);
196 spin_lock_irq(q->queue_lock);
197}
198EXPORT_SYMBOL_GPL(bsg_request_fn);
199
200
201
202
203
204
205
206
207
208
209
210
211int bsg_setup_queue(struct device *dev, struct request_queue *q,
212 char *name, bsg_job_fn *job_fn, int dd_job_size)
213{
214 int ret;
215
216 q->queuedata = dev;
217 q->bsg_job_size = dd_job_size;
218 q->bsg_job_fn = job_fn;
219 queue_flag_set_unlocked(QUEUE_FLAG_BIDI, q);
220 queue_flag_set_unlocked(QUEUE_FLAG_SCSI_PASSTHROUGH, q);
221 blk_queue_softirq_done(q, bsg_softirq_done);
222 blk_queue_rq_timeout(q, BLK_DEFAULT_SG_TIMEOUT);
223
224 ret = bsg_register_queue(q, dev, name, NULL);
225 if (ret) {
226 printk(KERN_ERR "%s: bsg interface failed to "
227 "initialize - register queue\n", dev->kobj.name);
228 return ret;
229 }
230
231 return 0;
232}
233EXPORT_SYMBOL_GPL(bsg_setup_queue);
234