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