Mike Christie | aa387cc | 2011-07-31 22:05:09 +0200 | [diff] [blame] | 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> |
Paul Gortmaker | 6adb123 | 2011-09-28 18:26:05 -0400 | [diff] [blame] | 28 | #include <linux/export.h> |
Mike Christie | aa387cc | 2011-07-31 22:05:09 +0200 | [diff] [blame] | 29 | #include <scsi/scsi_cmnd.h> |
| 30 | |
| 31 | /** |
Benjamin Block | 50b4d48 | 2017-08-24 01:57:56 +0200 | [diff] [blame] | 32 | * bsg_teardown_job - routine to teardown a bsg job |
Bart Van Assche | aa98192 | 2018-01-09 10:11:00 -0800 | [diff] [blame] | 33 | * @kref: kref inside bsg_job that is to be torn down |
Mike Christie | aa387cc | 2011-07-31 22:05:09 +0200 | [diff] [blame] | 34 | */ |
Benjamin Block | 50b4d48 | 2017-08-24 01:57:56 +0200 | [diff] [blame] | 35 | static void bsg_teardown_job(struct kref *kref) |
Mike Christie | aa387cc | 2011-07-31 22:05:09 +0200 | [diff] [blame] | 36 | { |
Johannes Thumshirn | bf0f2d3 | 2016-11-17 10:31:18 +0100 | [diff] [blame] | 37 | struct bsg_job *job = container_of(kref, struct bsg_job, kref); |
Christoph Hellwig | ef6fa64 | 2018-03-13 17:28:40 +0100 | [diff] [blame^] | 38 | struct request *rq = blk_mq_rq_from_pdu(job); |
Johannes Thumshirn | c00da4c | 2016-11-17 10:31:20 +0100 | [diff] [blame] | 39 | |
Mike Christie | aa387cc | 2011-07-31 22:05:09 +0200 | [diff] [blame] | 40 | put_device(job->dev); /* release reference for the request */ |
| 41 | |
| 42 | kfree(job->request_payload.sg_list); |
| 43 | kfree(job->reply_payload.sg_list); |
Benjamin Block | 50b4d48 | 2017-08-24 01:57:56 +0200 | [diff] [blame] | 44 | |
| 45 | blk_end_request_all(rq, BLK_STS_OK); |
Mike Christie | aa387cc | 2011-07-31 22:05:09 +0200 | [diff] [blame] | 46 | } |
| 47 | |
Johannes Thumshirn | fb6f7c8 | 2016-11-17 10:31:23 +0100 | [diff] [blame] | 48 | void bsg_job_put(struct bsg_job *job) |
| 49 | { |
Benjamin Block | 50b4d48 | 2017-08-24 01:57:56 +0200 | [diff] [blame] | 50 | kref_put(&job->kref, bsg_teardown_job); |
Johannes Thumshirn | fb6f7c8 | 2016-11-17 10:31:23 +0100 | [diff] [blame] | 51 | } |
| 52 | EXPORT_SYMBOL_GPL(bsg_job_put); |
| 53 | |
| 54 | int bsg_job_get(struct bsg_job *job) |
| 55 | { |
| 56 | return kref_get_unless_zero(&job->kref); |
| 57 | } |
| 58 | EXPORT_SYMBOL_GPL(bsg_job_get); |
Mike Christie | aa387cc | 2011-07-31 22:05:09 +0200 | [diff] [blame] | 59 | |
| 60 | /** |
| 61 | * bsg_job_done - completion routine for bsg requests |
| 62 | * @job: bsg_job that is complete |
| 63 | * @result: job reply result |
| 64 | * @reply_payload_rcv_len: length of payload recvd |
| 65 | * |
| 66 | * The LLD should call this when the bsg job has completed. |
| 67 | */ |
| 68 | void bsg_job_done(struct bsg_job *job, int result, |
| 69 | unsigned int reply_payload_rcv_len) |
| 70 | { |
Christoph Hellwig | ef6fa64 | 2018-03-13 17:28:40 +0100 | [diff] [blame^] | 71 | struct request *req = blk_mq_rq_from_pdu(job); |
Mike Christie | aa387cc | 2011-07-31 22:05:09 +0200 | [diff] [blame] | 72 | struct request *rsp = req->next_rq; |
| 73 | int err; |
| 74 | |
Christoph Hellwig | ef6fa64 | 2018-03-13 17:28:40 +0100 | [diff] [blame^] | 75 | err = job->sreq.result = result; |
Mike Christie | aa387cc | 2011-07-31 22:05:09 +0200 | [diff] [blame] | 76 | if (err < 0) |
| 77 | /* we're only returning the result field in the reply */ |
Christoph Hellwig | ef6fa64 | 2018-03-13 17:28:40 +0100 | [diff] [blame^] | 78 | job->sreq.sense_len = sizeof(u32); |
Mike Christie | aa387cc | 2011-07-31 22:05:09 +0200 | [diff] [blame] | 79 | else |
Christoph Hellwig | ef6fa64 | 2018-03-13 17:28:40 +0100 | [diff] [blame^] | 80 | job->sreq.sense_len = job->reply_len; |
Mike Christie | aa387cc | 2011-07-31 22:05:09 +0200 | [diff] [blame] | 81 | /* we assume all request payload was transferred, residual == 0 */ |
Christoph Hellwig | ef6fa64 | 2018-03-13 17:28:40 +0100 | [diff] [blame^] | 82 | job->sreq.resid_len = 0; |
Mike Christie | aa387cc | 2011-07-31 22:05:09 +0200 | [diff] [blame] | 83 | |
| 84 | if (rsp) { |
Christoph Hellwig | 82ed4db | 2017-01-27 09:46:29 +0100 | [diff] [blame] | 85 | WARN_ON(reply_payload_rcv_len > scsi_req(rsp)->resid_len); |
Mike Christie | aa387cc | 2011-07-31 22:05:09 +0200 | [diff] [blame] | 86 | |
| 87 | /* set reply (bidi) residual */ |
Christoph Hellwig | 82ed4db | 2017-01-27 09:46:29 +0100 | [diff] [blame] | 88 | scsi_req(rsp)->resid_len -= |
| 89 | min(reply_payload_rcv_len, scsi_req(rsp)->resid_len); |
Mike Christie | aa387cc | 2011-07-31 22:05:09 +0200 | [diff] [blame] | 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 | { |
Benjamin Block | 50b4d48 | 2017-08-24 01:57:56 +0200 | [diff] [blame] | 101 | struct bsg_job *job = blk_mq_rq_to_pdu(rq); |
Mike Christie | aa387cc | 2011-07-31 22:05:09 +0200 | [diff] [blame] | 102 | |
Johannes Thumshirn | fb6f7c8 | 2016-11-17 10:31:23 +0100 | [diff] [blame] | 103 | bsg_job_put(job); |
Mike Christie | aa387cc | 2011-07-31 22:05:09 +0200 | [diff] [blame] | 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); |
Christoph Hellwig | 82ed4db | 2017-01-27 09:46:29 +0100 | [diff] [blame] | 116 | scsi_req(req)->resid_len = blk_rq_bytes(req); |
Mike Christie | aa387cc | 2011-07-31 22:05:09 +0200 | [diff] [blame] | 117 | buf->sg_cnt = blk_rq_map_sg(req->q, req, buf->sg_list); |
| 118 | buf->payload_len = blk_rq_bytes(req); |
| 119 | return 0; |
| 120 | } |
| 121 | |
| 122 | /** |
Benjamin Block | 50b4d48 | 2017-08-24 01:57:56 +0200 | [diff] [blame] | 123 | * bsg_prepare_job - create the bsg_job structure for the bsg request |
Mike Christie | aa387cc | 2011-07-31 22:05:09 +0200 | [diff] [blame] | 124 | * @dev: device that is being sent the bsg request |
| 125 | * @req: BSG request that needs a job structure |
| 126 | */ |
Benjamin Block | 50b4d48 | 2017-08-24 01:57:56 +0200 | [diff] [blame] | 127 | static int bsg_prepare_job(struct device *dev, struct request *req) |
Mike Christie | aa387cc | 2011-07-31 22:05:09 +0200 | [diff] [blame] | 128 | { |
| 129 | struct request *rsp = req->next_rq; |
Christoph Hellwig | 82ed4db | 2017-01-27 09:46:29 +0100 | [diff] [blame] | 130 | struct scsi_request *rq = scsi_req(req); |
Benjamin Block | 50b4d48 | 2017-08-24 01:57:56 +0200 | [diff] [blame] | 131 | struct bsg_job *job = blk_mq_rq_to_pdu(req); |
Mike Christie | aa387cc | 2011-07-31 22:05:09 +0200 | [diff] [blame] | 132 | int ret; |
| 133 | |
Christoph Hellwig | 31156ec | 2018-03-13 17:28:39 +0100 | [diff] [blame] | 134 | job->timeout = req->timeout; |
Christoph Hellwig | 82ed4db | 2017-01-27 09:46:29 +0100 | [diff] [blame] | 135 | job->request = rq->cmd; |
| 136 | job->request_len = rq->cmd_len; |
Benjamin Block | 50b4d48 | 2017-08-24 01:57:56 +0200 | [diff] [blame] | 137 | |
Mike Christie | aa387cc | 2011-07-31 22:05:09 +0200 | [diff] [blame] | 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); |
Johannes Thumshirn | bf0f2d3 | 2016-11-17 10:31:18 +0100 | [diff] [blame] | 151 | kref_init(&job->kref); |
Mike Christie | aa387cc | 2011-07-31 22:05:09 +0200 | [diff] [blame] | 152 | return 0; |
| 153 | |
| 154 | failjob_rls_rqst_payload: |
| 155 | kfree(job->request_payload.sg_list); |
| 156 | failjob_rls_job: |
Mike Christie | aa387cc | 2011-07-31 22:05:09 +0200 | [diff] [blame] | 157 | return -ENOMEM; |
| 158 | } |
| 159 | |
Mike Christie | aa387cc | 2011-07-31 22:05:09 +0200 | [diff] [blame] | 160 | /** |
| 161 | * bsg_request_fn - generic handler for bsg requests |
| 162 | * @q: request queue to manage |
| 163 | * |
| 164 | * On error the create_bsg_job function should return a -Exyz error value |
Christoph Hellwig | 17d5363 | 2017-04-20 16:03:01 +0200 | [diff] [blame] | 165 | * that will be set to ->result. |
Mike Christie | aa387cc | 2011-07-31 22:05:09 +0200 | [diff] [blame] | 166 | * |
| 167 | * Drivers/subsys should pass this to the queue init function. |
| 168 | */ |
Christoph Hellwig | 8ae94eb | 2017-01-03 15:25:02 +0300 | [diff] [blame] | 169 | static void bsg_request_fn(struct request_queue *q) |
Bart Van Assche | dbb3ab0 | 2016-09-25 19:54:38 -0700 | [diff] [blame] | 170 | __releases(q->queue_lock) |
| 171 | __acquires(q->queue_lock) |
Mike Christie | aa387cc | 2011-07-31 22:05:09 +0200 | [diff] [blame] | 172 | { |
| 173 | struct device *dev = q->queuedata; |
| 174 | struct request *req; |
Mike Christie | aa387cc | 2011-07-31 22:05:09 +0200 | [diff] [blame] | 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 | |
Benjamin Block | 50b4d48 | 2017-08-24 01:57:56 +0200 | [diff] [blame] | 186 | ret = bsg_prepare_job(dev, req); |
Mike Christie | aa387cc | 2011-07-31 22:05:09 +0200 | [diff] [blame] | 187 | if (ret) { |
Christoph Hellwig | 17d5363 | 2017-04-20 16:03:01 +0200 | [diff] [blame] | 188 | scsi_req(req)->result = ret; |
Christoph Hellwig | 2a842ac | 2017-06-03 09:38:04 +0200 | [diff] [blame] | 189 | blk_end_request_all(req, BLK_STS_OK); |
Mike Christie | aa387cc | 2011-07-31 22:05:09 +0200 | [diff] [blame] | 190 | spin_lock_irq(q->queue_lock); |
| 191 | continue; |
| 192 | } |
| 193 | |
Benjamin Block | 50b4d48 | 2017-08-24 01:57:56 +0200 | [diff] [blame] | 194 | ret = q->bsg_job_fn(blk_mq_rq_to_pdu(req)); |
Mike Christie | aa387cc | 2011-07-31 22:05:09 +0200 | [diff] [blame] | 195 | spin_lock_irq(q->queue_lock); |
| 196 | if (ret) |
| 197 | break; |
| 198 | } |
| 199 | |
| 200 | spin_unlock_irq(q->queue_lock); |
| 201 | put_device(dev); |
| 202 | spin_lock_irq(q->queue_lock); |
| 203 | } |
Mike Christie | aa387cc | 2011-07-31 22:05:09 +0200 | [diff] [blame] | 204 | |
Benjamin Block | 50b4d48 | 2017-08-24 01:57:56 +0200 | [diff] [blame] | 205 | static int bsg_init_rq(struct request_queue *q, struct request *req, gfp_t gfp) |
| 206 | { |
| 207 | struct bsg_job *job = blk_mq_rq_to_pdu(req); |
| 208 | struct scsi_request *sreq = &job->sreq; |
| 209 | |
Benjamin Block | eab40cf | 2017-10-03 12:48:37 +0200 | [diff] [blame] | 210 | /* called right after the request is allocated for the request_queue */ |
Benjamin Block | 50b4d48 | 2017-08-24 01:57:56 +0200 | [diff] [blame] | 211 | |
Benjamin Block | eab40cf | 2017-10-03 12:48:37 +0200 | [diff] [blame] | 212 | sreq->sense = kzalloc(SCSI_SENSE_BUFFERSIZE, gfp); |
Benjamin Block | 50b4d48 | 2017-08-24 01:57:56 +0200 | [diff] [blame] | 213 | if (!sreq->sense) |
| 214 | return -ENOMEM; |
| 215 | |
Benjamin Block | eab40cf | 2017-10-03 12:48:37 +0200 | [diff] [blame] | 216 | return 0; |
| 217 | } |
| 218 | |
| 219 | static void bsg_initialize_rq(struct request *req) |
| 220 | { |
| 221 | struct bsg_job *job = blk_mq_rq_to_pdu(req); |
| 222 | struct scsi_request *sreq = &job->sreq; |
| 223 | void *sense = sreq->sense; |
| 224 | |
| 225 | /* called right before the request is given to the request_queue user */ |
| 226 | |
| 227 | memset(job, 0, sizeof(*job)); |
| 228 | |
| 229 | scsi_req_init(sreq); |
| 230 | |
| 231 | sreq->sense = sense; |
| 232 | sreq->sense_len = SCSI_SENSE_BUFFERSIZE; |
| 233 | |
Benjamin Block | eab40cf | 2017-10-03 12:48:37 +0200 | [diff] [blame] | 234 | job->reply = sense; |
Benjamin Block | 50b4d48 | 2017-08-24 01:57:56 +0200 | [diff] [blame] | 235 | job->reply_len = sreq->sense_len; |
| 236 | job->dd_data = job + 1; |
Benjamin Block | 50b4d48 | 2017-08-24 01:57:56 +0200 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | static void bsg_exit_rq(struct request_queue *q, struct request *req) |
| 240 | { |
| 241 | struct bsg_job *job = blk_mq_rq_to_pdu(req); |
| 242 | struct scsi_request *sreq = &job->sreq; |
| 243 | |
| 244 | kfree(sreq->sense); |
| 245 | } |
| 246 | |
Mike Christie | aa387cc | 2011-07-31 22:05:09 +0200 | [diff] [blame] | 247 | /** |
| 248 | * bsg_setup_queue - Create and add the bsg hooks so we can receive requests |
| 249 | * @dev: device to attach bsg device to |
Mike Christie | aa387cc | 2011-07-31 22:05:09 +0200 | [diff] [blame] | 250 | * @name: device to give bsg device |
| 251 | * @job_fn: bsg job handler |
| 252 | * @dd_job_size: size of LLD data needed for each job |
Bart Van Assche | aa98192 | 2018-01-09 10:11:00 -0800 | [diff] [blame] | 253 | * @release: @dev release function |
Mike Christie | aa387cc | 2011-07-31 22:05:09 +0200 | [diff] [blame] | 254 | */ |
Christoph Hellwig | c1225f0 | 2017-08-25 17:37:38 +0200 | [diff] [blame] | 255 | struct request_queue *bsg_setup_queue(struct device *dev, const char *name, |
| 256 | bsg_job_fn *job_fn, int dd_job_size, |
| 257 | void (*release)(struct device *)) |
Mike Christie | aa387cc | 2011-07-31 22:05:09 +0200 | [diff] [blame] | 258 | { |
Christoph Hellwig | 8ae94eb | 2017-01-03 15:25:02 +0300 | [diff] [blame] | 259 | struct request_queue *q; |
Mike Christie | aa387cc | 2011-07-31 22:05:09 +0200 | [diff] [blame] | 260 | int ret; |
| 261 | |
Christoph Hellwig | 82ed4db | 2017-01-27 09:46:29 +0100 | [diff] [blame] | 262 | q = blk_alloc_queue(GFP_KERNEL); |
Christoph Hellwig | 8ae94eb | 2017-01-03 15:25:02 +0300 | [diff] [blame] | 263 | if (!q) |
| 264 | return ERR_PTR(-ENOMEM); |
Benjamin Block | 50b4d48 | 2017-08-24 01:57:56 +0200 | [diff] [blame] | 265 | q->cmd_size = sizeof(struct bsg_job) + dd_job_size; |
| 266 | q->init_rq_fn = bsg_init_rq; |
| 267 | q->exit_rq_fn = bsg_exit_rq; |
Benjamin Block | eab40cf | 2017-10-03 12:48:37 +0200 | [diff] [blame] | 268 | q->initialize_rq_fn = bsg_initialize_rq; |
Christoph Hellwig | 82ed4db | 2017-01-27 09:46:29 +0100 | [diff] [blame] | 269 | q->request_fn = bsg_request_fn; |
| 270 | |
| 271 | ret = blk_init_allocated_queue(q); |
| 272 | if (ret) |
| 273 | goto out_cleanup_queue; |
Christoph Hellwig | 8ae94eb | 2017-01-03 15:25:02 +0300 | [diff] [blame] | 274 | |
Mike Christie | aa387cc | 2011-07-31 22:05:09 +0200 | [diff] [blame] | 275 | q->queuedata = dev; |
Mike Christie | aa387cc | 2011-07-31 22:05:09 +0200 | [diff] [blame] | 276 | q->bsg_job_fn = job_fn; |
Bart Van Assche | 8b904b5 | 2018-03-07 17:10:10 -0800 | [diff] [blame] | 277 | blk_queue_flag_set(QUEUE_FLAG_BIDI, q); |
| 278 | blk_queue_flag_set(QUEUE_FLAG_SCSI_PASSTHROUGH, q); |
Mike Christie | aa387cc | 2011-07-31 22:05:09 +0200 | [diff] [blame] | 279 | blk_queue_softirq_done(q, bsg_softirq_done); |
| 280 | blk_queue_rq_timeout(q, BLK_DEFAULT_SG_TIMEOUT); |
| 281 | |
Christoph Hellwig | c1225f0 | 2017-08-25 17:37:38 +0200 | [diff] [blame] | 282 | ret = bsg_register_queue(q, dev, name, release); |
Mike Christie | aa387cc | 2011-07-31 22:05:09 +0200 | [diff] [blame] | 283 | if (ret) { |
| 284 | printk(KERN_ERR "%s: bsg interface failed to " |
| 285 | "initialize - register queue\n", dev->kobj.name); |
Christoph Hellwig | 82ed4db | 2017-01-27 09:46:29 +0100 | [diff] [blame] | 286 | goto out_cleanup_queue; |
Mike Christie | aa387cc | 2011-07-31 22:05:09 +0200 | [diff] [blame] | 287 | } |
| 288 | |
Christoph Hellwig | 8ae94eb | 2017-01-03 15:25:02 +0300 | [diff] [blame] | 289 | return q; |
Christoph Hellwig | 82ed4db | 2017-01-27 09:46:29 +0100 | [diff] [blame] | 290 | out_cleanup_queue: |
| 291 | blk_cleanup_queue(q); |
| 292 | return ERR_PTR(ret); |
Mike Christie | aa387cc | 2011-07-31 22:05:09 +0200 | [diff] [blame] | 293 | } |
| 294 | EXPORT_SYMBOL_GPL(bsg_setup_queue); |