Christoph Hellwig | a497ee3 | 2019-04-30 14:42:40 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Mike Christie | aa387cc | 2011-07-31 22:05:09 +0200 | [diff] [blame] | 2 | /* |
| 3 | * BSG helper library |
| 4 | * |
| 5 | * Copyright (C) 2008 James Smart, Emulex Corporation |
| 6 | * Copyright (C) 2011 Red Hat, Inc. All rights reserved. |
| 7 | * Copyright (C) 2011 Mike Christie |
Mike Christie | aa387cc | 2011-07-31 22:05:09 +0200 | [diff] [blame] | 8 | */ |
| 9 | #include <linux/slab.h> |
Jens Axboe | cd2f076 | 2018-10-24 07:11:39 -0600 | [diff] [blame] | 10 | #include <linux/blk-mq.h> |
Mike Christie | aa387cc | 2011-07-31 22:05:09 +0200 | [diff] [blame] | 11 | #include <linux/delay.h> |
| 12 | #include <linux/scatterlist.h> |
| 13 | #include <linux/bsg-lib.h> |
Paul Gortmaker | 6adb123 | 2011-09-28 18:26:05 -0400 | [diff] [blame] | 14 | #include <linux/export.h> |
Mike Christie | aa387cc | 2011-07-31 22:05:09 +0200 | [diff] [blame] | 15 | #include <scsi/scsi_cmnd.h> |
Christoph Hellwig | 17cb960 | 2018-03-13 17:28:41 +0100 | [diff] [blame] | 16 | #include <scsi/sg.h> |
| 17 | |
| 18 | #define uptr64(val) ((void __user *)(uintptr_t)(val)) |
| 19 | |
Jens Axboe | 1028e4b | 2018-10-29 09:47:17 -0600 | [diff] [blame] | 20 | struct bsg_set { |
| 21 | struct blk_mq_tag_set tag_set; |
| 22 | bsg_job_fn *job_fn; |
| 23 | bsg_timeout_fn *timeout_fn; |
| 24 | }; |
| 25 | |
Christoph Hellwig | 17cb960 | 2018-03-13 17:28:41 +0100 | [diff] [blame] | 26 | static int bsg_transport_check_proto(struct sg_io_v4 *hdr) |
| 27 | { |
| 28 | if (hdr->protocol != BSG_PROTOCOL_SCSI || |
| 29 | hdr->subprotocol != BSG_SUB_PROTOCOL_SCSI_TRANSPORT) |
| 30 | return -EINVAL; |
| 31 | if (!capable(CAP_SYS_RAWIO)) |
| 32 | return -EPERM; |
| 33 | return 0; |
| 34 | } |
| 35 | |
| 36 | static int bsg_transport_fill_hdr(struct request *rq, struct sg_io_v4 *hdr, |
| 37 | fmode_t mode) |
| 38 | { |
| 39 | struct bsg_job *job = blk_mq_rq_to_pdu(rq); |
Christoph Hellwig | 972248e | 2019-01-29 09:32:03 +0100 | [diff] [blame] | 40 | int ret; |
Christoph Hellwig | 17cb960 | 2018-03-13 17:28:41 +0100 | [diff] [blame] | 41 | |
| 42 | job->request_len = hdr->request_len; |
| 43 | job->request = memdup_user(uptr64(hdr->request), hdr->request_len); |
Christoph Hellwig | 972248e | 2019-01-29 09:32:03 +0100 | [diff] [blame] | 44 | if (IS_ERR(job->request)) |
| 45 | return PTR_ERR(job->request); |
zhong jiang | 4725549 | 2018-08-01 00:13:14 +0800 | [diff] [blame] | 46 | |
Christoph Hellwig | 972248e | 2019-01-29 09:32:03 +0100 | [diff] [blame] | 47 | if (hdr->dout_xfer_len && hdr->din_xfer_len) { |
| 48 | job->bidi_rq = blk_get_request(rq->q, REQ_OP_SCSI_IN, 0); |
| 49 | if (IS_ERR(job->bidi_rq)) { |
| 50 | ret = PTR_ERR(job->bidi_rq); |
| 51 | goto out; |
| 52 | } |
| 53 | |
| 54 | ret = blk_rq_map_user(rq->q, job->bidi_rq, NULL, |
| 55 | uptr64(hdr->din_xferp), hdr->din_xfer_len, |
| 56 | GFP_KERNEL); |
| 57 | if (ret) |
| 58 | goto out_free_bidi_rq; |
| 59 | |
| 60 | job->bidi_bio = job->bidi_rq->bio; |
| 61 | } else { |
| 62 | job->bidi_rq = NULL; |
| 63 | job->bidi_bio = NULL; |
| 64 | } |
| 65 | |
| 66 | return 0; |
| 67 | |
| 68 | out_free_bidi_rq: |
| 69 | if (job->bidi_rq) |
| 70 | blk_put_request(job->bidi_rq); |
| 71 | out: |
| 72 | kfree(job->request); |
| 73 | return ret; |
Christoph Hellwig | 17cb960 | 2018-03-13 17:28:41 +0100 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | static int bsg_transport_complete_rq(struct request *rq, struct sg_io_v4 *hdr) |
| 77 | { |
| 78 | struct bsg_job *job = blk_mq_rq_to_pdu(rq); |
| 79 | int ret = 0; |
| 80 | |
| 81 | /* |
| 82 | * The assignments below don't make much sense, but are kept for |
| 83 | * bug by bug backwards compatibility: |
| 84 | */ |
| 85 | hdr->device_status = job->result & 0xff; |
| 86 | hdr->transport_status = host_byte(job->result); |
| 87 | hdr->driver_status = driver_byte(job->result); |
| 88 | hdr->info = 0; |
| 89 | if (hdr->device_status || hdr->transport_status || hdr->driver_status) |
| 90 | hdr->info |= SG_INFO_CHECK; |
| 91 | hdr->response_len = 0; |
| 92 | |
| 93 | if (job->result < 0) { |
| 94 | /* we're only returning the result field in the reply */ |
| 95 | job->reply_len = sizeof(u32); |
| 96 | ret = job->result; |
| 97 | } |
| 98 | |
| 99 | if (job->reply_len && hdr->response) { |
| 100 | int len = min(hdr->max_response_len, job->reply_len); |
| 101 | |
| 102 | if (copy_to_user(uptr64(hdr->response), job->reply, len)) |
| 103 | ret = -EFAULT; |
| 104 | else |
| 105 | hdr->response_len = len; |
| 106 | } |
| 107 | |
| 108 | /* we assume all request payload was transferred, residual == 0 */ |
| 109 | hdr->dout_resid = 0; |
| 110 | |
Christoph Hellwig | 972248e | 2019-01-29 09:32:03 +0100 | [diff] [blame] | 111 | if (job->bidi_rq) { |
Christoph Hellwig | 17cb960 | 2018-03-13 17:28:41 +0100 | [diff] [blame] | 112 | unsigned int rsp_len = job->reply_payload.payload_len; |
| 113 | |
| 114 | if (WARN_ON(job->reply_payload_rcv_len > rsp_len)) |
| 115 | hdr->din_resid = 0; |
| 116 | else |
| 117 | hdr->din_resid = rsp_len - job->reply_payload_rcv_len; |
| 118 | } else { |
| 119 | hdr->din_resid = 0; |
| 120 | } |
| 121 | |
| 122 | return ret; |
| 123 | } |
| 124 | |
| 125 | static void bsg_transport_free_rq(struct request *rq) |
| 126 | { |
| 127 | struct bsg_job *job = blk_mq_rq_to_pdu(rq); |
| 128 | |
Christoph Hellwig | 972248e | 2019-01-29 09:32:03 +0100 | [diff] [blame] | 129 | if (job->bidi_rq) { |
| 130 | blk_rq_unmap_user(job->bidi_bio); |
| 131 | blk_put_request(job->bidi_rq); |
| 132 | } |
| 133 | |
Christoph Hellwig | 17cb960 | 2018-03-13 17:28:41 +0100 | [diff] [blame] | 134 | kfree(job->request); |
| 135 | } |
| 136 | |
| 137 | static const struct bsg_ops bsg_transport_ops = { |
| 138 | .check_proto = bsg_transport_check_proto, |
| 139 | .fill_hdr = bsg_transport_fill_hdr, |
| 140 | .complete_rq = bsg_transport_complete_rq, |
| 141 | .free_rq = bsg_transport_free_rq, |
| 142 | }; |
Mike Christie | aa387cc | 2011-07-31 22:05:09 +0200 | [diff] [blame] | 143 | |
| 144 | /** |
Benjamin Block | 50b4d48 | 2017-08-24 01:57:56 +0200 | [diff] [blame] | 145 | * bsg_teardown_job - routine to teardown a bsg job |
Bart Van Assche | aa98192 | 2018-01-09 10:11:00 -0800 | [diff] [blame] | 146 | * @kref: kref inside bsg_job that is to be torn down |
Mike Christie | aa387cc | 2011-07-31 22:05:09 +0200 | [diff] [blame] | 147 | */ |
Benjamin Block | 50b4d48 | 2017-08-24 01:57:56 +0200 | [diff] [blame] | 148 | static void bsg_teardown_job(struct kref *kref) |
Mike Christie | aa387cc | 2011-07-31 22:05:09 +0200 | [diff] [blame] | 149 | { |
Johannes Thumshirn | bf0f2d3 | 2016-11-17 10:31:18 +0100 | [diff] [blame] | 150 | struct bsg_job *job = container_of(kref, struct bsg_job, kref); |
Christoph Hellwig | ef6fa64 | 2018-03-13 17:28:40 +0100 | [diff] [blame] | 151 | struct request *rq = blk_mq_rq_from_pdu(job); |
Johannes Thumshirn | c00da4c | 2016-11-17 10:31:20 +0100 | [diff] [blame] | 152 | |
Mike Christie | aa387cc | 2011-07-31 22:05:09 +0200 | [diff] [blame] | 153 | put_device(job->dev); /* release reference for the request */ |
| 154 | |
| 155 | kfree(job->request_payload.sg_list); |
| 156 | kfree(job->reply_payload.sg_list); |
Benjamin Block | 50b4d48 | 2017-08-24 01:57:56 +0200 | [diff] [blame] | 157 | |
Jens Axboe | cd2f076 | 2018-10-24 07:11:39 -0600 | [diff] [blame] | 158 | blk_mq_end_request(rq, BLK_STS_OK); |
Mike Christie | aa387cc | 2011-07-31 22:05:09 +0200 | [diff] [blame] | 159 | } |
| 160 | |
Johannes Thumshirn | fb6f7c8 | 2016-11-17 10:31:23 +0100 | [diff] [blame] | 161 | void bsg_job_put(struct bsg_job *job) |
| 162 | { |
Benjamin Block | 50b4d48 | 2017-08-24 01:57:56 +0200 | [diff] [blame] | 163 | kref_put(&job->kref, bsg_teardown_job); |
Johannes Thumshirn | fb6f7c8 | 2016-11-17 10:31:23 +0100 | [diff] [blame] | 164 | } |
| 165 | EXPORT_SYMBOL_GPL(bsg_job_put); |
| 166 | |
| 167 | int bsg_job_get(struct bsg_job *job) |
| 168 | { |
| 169 | return kref_get_unless_zero(&job->kref); |
| 170 | } |
| 171 | EXPORT_SYMBOL_GPL(bsg_job_get); |
Mike Christie | aa387cc | 2011-07-31 22:05:09 +0200 | [diff] [blame] | 172 | |
| 173 | /** |
| 174 | * bsg_job_done - completion routine for bsg requests |
| 175 | * @job: bsg_job that is complete |
| 176 | * @result: job reply result |
| 177 | * @reply_payload_rcv_len: length of payload recvd |
| 178 | * |
| 179 | * The LLD should call this when the bsg job has completed. |
| 180 | */ |
| 181 | void bsg_job_done(struct bsg_job *job, int result, |
| 182 | unsigned int reply_payload_rcv_len) |
| 183 | { |
Christoph Hellwig | 17cb960 | 2018-03-13 17:28:41 +0100 | [diff] [blame] | 184 | job->result = result; |
| 185 | job->reply_payload_rcv_len = reply_payload_rcv_len; |
Jens Axboe | cd2f076 | 2018-10-24 07:11:39 -0600 | [diff] [blame] | 186 | blk_mq_complete_request(blk_mq_rq_from_pdu(job)); |
Mike Christie | aa387cc | 2011-07-31 22:05:09 +0200 | [diff] [blame] | 187 | } |
| 188 | EXPORT_SYMBOL_GPL(bsg_job_done); |
| 189 | |
| 190 | /** |
Jens Axboe | cd2f076 | 2018-10-24 07:11:39 -0600 | [diff] [blame] | 191 | * bsg_complete - softirq done routine for destroying the bsg requests |
Mike Christie | aa387cc | 2011-07-31 22:05:09 +0200 | [diff] [blame] | 192 | * @rq: BSG request that holds the job to be destroyed |
| 193 | */ |
Jens Axboe | cd2f076 | 2018-10-24 07:11:39 -0600 | [diff] [blame] | 194 | static void bsg_complete(struct request *rq) |
Mike Christie | aa387cc | 2011-07-31 22:05:09 +0200 | [diff] [blame] | 195 | { |
Benjamin Block | 50b4d48 | 2017-08-24 01:57:56 +0200 | [diff] [blame] | 196 | struct bsg_job *job = blk_mq_rq_to_pdu(rq); |
Mike Christie | aa387cc | 2011-07-31 22:05:09 +0200 | [diff] [blame] | 197 | |
Johannes Thumshirn | fb6f7c8 | 2016-11-17 10:31:23 +0100 | [diff] [blame] | 198 | bsg_job_put(job); |
Mike Christie | aa387cc | 2011-07-31 22:05:09 +0200 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | static int bsg_map_buffer(struct bsg_buffer *buf, struct request *req) |
| 202 | { |
| 203 | size_t sz = (sizeof(struct scatterlist) * req->nr_phys_segments); |
| 204 | |
| 205 | BUG_ON(!req->nr_phys_segments); |
| 206 | |
| 207 | buf->sg_list = kzalloc(sz, GFP_KERNEL); |
| 208 | if (!buf->sg_list) |
| 209 | return -ENOMEM; |
| 210 | sg_init_table(buf->sg_list, req->nr_phys_segments); |
| 211 | buf->sg_cnt = blk_rq_map_sg(req->q, req, buf->sg_list); |
| 212 | buf->payload_len = blk_rq_bytes(req); |
| 213 | return 0; |
| 214 | } |
| 215 | |
| 216 | /** |
Benjamin Block | 50b4d48 | 2017-08-24 01:57:56 +0200 | [diff] [blame] | 217 | * bsg_prepare_job - create the bsg_job structure for the bsg request |
Mike Christie | aa387cc | 2011-07-31 22:05:09 +0200 | [diff] [blame] | 218 | * @dev: device that is being sent the bsg request |
| 219 | * @req: BSG request that needs a job structure |
| 220 | */ |
Christoph Hellwig | 17cb960 | 2018-03-13 17:28:41 +0100 | [diff] [blame] | 221 | static bool bsg_prepare_job(struct device *dev, struct request *req) |
Mike Christie | aa387cc | 2011-07-31 22:05:09 +0200 | [diff] [blame] | 222 | { |
Benjamin Block | 50b4d48 | 2017-08-24 01:57:56 +0200 | [diff] [blame] | 223 | struct bsg_job *job = blk_mq_rq_to_pdu(req); |
Mike Christie | aa387cc | 2011-07-31 22:05:09 +0200 | [diff] [blame] | 224 | int ret; |
| 225 | |
Christoph Hellwig | 31156ec | 2018-03-13 17:28:39 +0100 | [diff] [blame] | 226 | job->timeout = req->timeout; |
Benjamin Block | 50b4d48 | 2017-08-24 01:57:56 +0200 | [diff] [blame] | 227 | |
Mike Christie | aa387cc | 2011-07-31 22:05:09 +0200 | [diff] [blame] | 228 | if (req->bio) { |
| 229 | ret = bsg_map_buffer(&job->request_payload, req); |
| 230 | if (ret) |
| 231 | goto failjob_rls_job; |
| 232 | } |
Christoph Hellwig | 972248e | 2019-01-29 09:32:03 +0100 | [diff] [blame] | 233 | if (job->bidi_rq) { |
| 234 | ret = bsg_map_buffer(&job->reply_payload, job->bidi_rq); |
Mike Christie | aa387cc | 2011-07-31 22:05:09 +0200 | [diff] [blame] | 235 | if (ret) |
| 236 | goto failjob_rls_rqst_payload; |
| 237 | } |
| 238 | job->dev = dev; |
| 239 | /* take a reference for the request */ |
| 240 | get_device(job->dev); |
Johannes Thumshirn | bf0f2d3 | 2016-11-17 10:31:18 +0100 | [diff] [blame] | 241 | kref_init(&job->kref); |
Christoph Hellwig | 17cb960 | 2018-03-13 17:28:41 +0100 | [diff] [blame] | 242 | return true; |
Mike Christie | aa387cc | 2011-07-31 22:05:09 +0200 | [diff] [blame] | 243 | |
| 244 | failjob_rls_rqst_payload: |
| 245 | kfree(job->request_payload.sg_list); |
| 246 | failjob_rls_job: |
Christoph Hellwig | 17cb960 | 2018-03-13 17:28:41 +0100 | [diff] [blame] | 247 | job->result = -ENOMEM; |
| 248 | return false; |
Mike Christie | aa387cc | 2011-07-31 22:05:09 +0200 | [diff] [blame] | 249 | } |
| 250 | |
Mike Christie | aa387cc | 2011-07-31 22:05:09 +0200 | [diff] [blame] | 251 | /** |
Jens Axboe | cd2f076 | 2018-10-24 07:11:39 -0600 | [diff] [blame] | 252 | * bsg_queue_rq - generic handler for bsg requests |
| 253 | * @hctx: hardware queue |
| 254 | * @bd: queue data |
Mike Christie | aa387cc | 2011-07-31 22:05:09 +0200 | [diff] [blame] | 255 | * |
| 256 | * 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] | 257 | * that will be set to ->result. |
Mike Christie | aa387cc | 2011-07-31 22:05:09 +0200 | [diff] [blame] | 258 | * |
| 259 | * Drivers/subsys should pass this to the queue init function. |
| 260 | */ |
Jens Axboe | cd2f076 | 2018-10-24 07:11:39 -0600 | [diff] [blame] | 261 | static blk_status_t bsg_queue_rq(struct blk_mq_hw_ctx *hctx, |
| 262 | const struct blk_mq_queue_data *bd) |
Mike Christie | aa387cc | 2011-07-31 22:05:09 +0200 | [diff] [blame] | 263 | { |
Jens Axboe | cd2f076 | 2018-10-24 07:11:39 -0600 | [diff] [blame] | 264 | struct request_queue *q = hctx->queue; |
Mike Christie | aa387cc | 2011-07-31 22:05:09 +0200 | [diff] [blame] | 265 | struct device *dev = q->queuedata; |
Jens Axboe | cd2f076 | 2018-10-24 07:11:39 -0600 | [diff] [blame] | 266 | struct request *req = bd->rq; |
Jens Axboe | 1028e4b | 2018-10-29 09:47:17 -0600 | [diff] [blame] | 267 | struct bsg_set *bset = |
| 268 | container_of(q->tag_set, struct bsg_set, tag_set); |
Bart Van Assche | c44a4ed | 2019-12-17 16:23:29 -0800 | [diff] [blame] | 269 | blk_status_t sts = BLK_STS_IOERR; |
Mike Christie | aa387cc | 2011-07-31 22:05:09 +0200 | [diff] [blame] | 270 | int ret; |
| 271 | |
Jens Axboe | cd2f076 | 2018-10-24 07:11:39 -0600 | [diff] [blame] | 272 | blk_mq_start_request(req); |
| 273 | |
Mike Christie | aa387cc | 2011-07-31 22:05:09 +0200 | [diff] [blame] | 274 | if (!get_device(dev)) |
Jens Axboe | cd2f076 | 2018-10-24 07:11:39 -0600 | [diff] [blame] | 275 | return BLK_STS_IOERR; |
Mike Christie | aa387cc | 2011-07-31 22:05:09 +0200 | [diff] [blame] | 276 | |
Jens Axboe | cd2f076 | 2018-10-24 07:11:39 -0600 | [diff] [blame] | 277 | if (!bsg_prepare_job(dev, req)) |
Martin Wilck | d46fe2c | 2019-09-23 14:02:02 +0000 | [diff] [blame] | 278 | goto out; |
Mike Christie | aa387cc | 2011-07-31 22:05:09 +0200 | [diff] [blame] | 279 | |
Jens Axboe | 1028e4b | 2018-10-29 09:47:17 -0600 | [diff] [blame] | 280 | ret = bset->job_fn(blk_mq_rq_to_pdu(req)); |
Martin Wilck | d46fe2c | 2019-09-23 14:02:02 +0000 | [diff] [blame] | 281 | if (!ret) |
| 282 | sts = BLK_STS_OK; |
Mike Christie | aa387cc | 2011-07-31 22:05:09 +0200 | [diff] [blame] | 283 | |
Martin Wilck | d46fe2c | 2019-09-23 14:02:02 +0000 | [diff] [blame] | 284 | out: |
Mike Christie | aa387cc | 2011-07-31 22:05:09 +0200 | [diff] [blame] | 285 | put_device(dev); |
Martin Wilck | d46fe2c | 2019-09-23 14:02:02 +0000 | [diff] [blame] | 286 | return sts; |
Mike Christie | aa387cc | 2011-07-31 22:05:09 +0200 | [diff] [blame] | 287 | } |
Mike Christie | aa387cc | 2011-07-31 22:05:09 +0200 | [diff] [blame] | 288 | |
Christoph Hellwig | 17cb960 | 2018-03-13 17:28:41 +0100 | [diff] [blame] | 289 | /* called right after the request is allocated for the request_queue */ |
Jens Axboe | cd2f076 | 2018-10-24 07:11:39 -0600 | [diff] [blame] | 290 | static int bsg_init_rq(struct blk_mq_tag_set *set, struct request *req, |
| 291 | unsigned int hctx_idx, unsigned int numa_node) |
Benjamin Block | 50b4d48 | 2017-08-24 01:57:56 +0200 | [diff] [blame] | 292 | { |
| 293 | struct bsg_job *job = blk_mq_rq_to_pdu(req); |
Benjamin Block | 50b4d48 | 2017-08-24 01:57:56 +0200 | [diff] [blame] | 294 | |
Jens Axboe | cd2f076 | 2018-10-24 07:11:39 -0600 | [diff] [blame] | 295 | job->reply = kzalloc(SCSI_SENSE_BUFFERSIZE, GFP_KERNEL); |
Christoph Hellwig | 17cb960 | 2018-03-13 17:28:41 +0100 | [diff] [blame] | 296 | if (!job->reply) |
Benjamin Block | 50b4d48 | 2017-08-24 01:57:56 +0200 | [diff] [blame] | 297 | return -ENOMEM; |
Benjamin Block | eab40cf | 2017-10-03 12:48:37 +0200 | [diff] [blame] | 298 | return 0; |
| 299 | } |
| 300 | |
Christoph Hellwig | 17cb960 | 2018-03-13 17:28:41 +0100 | [diff] [blame] | 301 | /* called right before the request is given to the request_queue user */ |
Benjamin Block | eab40cf | 2017-10-03 12:48:37 +0200 | [diff] [blame] | 302 | static void bsg_initialize_rq(struct request *req) |
| 303 | { |
| 304 | struct bsg_job *job = blk_mq_rq_to_pdu(req); |
Christoph Hellwig | 17cb960 | 2018-03-13 17:28:41 +0100 | [diff] [blame] | 305 | void *reply = job->reply; |
Benjamin Block | eab40cf | 2017-10-03 12:48:37 +0200 | [diff] [blame] | 306 | |
| 307 | memset(job, 0, sizeof(*job)); |
Christoph Hellwig | 17cb960 | 2018-03-13 17:28:41 +0100 | [diff] [blame] | 308 | job->reply = reply; |
| 309 | job->reply_len = SCSI_SENSE_BUFFERSIZE; |
Benjamin Block | 50b4d48 | 2017-08-24 01:57:56 +0200 | [diff] [blame] | 310 | job->dd_data = job + 1; |
Benjamin Block | 50b4d48 | 2017-08-24 01:57:56 +0200 | [diff] [blame] | 311 | } |
| 312 | |
Jens Axboe | cd2f076 | 2018-10-24 07:11:39 -0600 | [diff] [blame] | 313 | static void bsg_exit_rq(struct blk_mq_tag_set *set, struct request *req, |
| 314 | unsigned int hctx_idx) |
Benjamin Block | 50b4d48 | 2017-08-24 01:57:56 +0200 | [diff] [blame] | 315 | { |
| 316 | struct bsg_job *job = blk_mq_rq_to_pdu(req); |
Benjamin Block | 50b4d48 | 2017-08-24 01:57:56 +0200 | [diff] [blame] | 317 | |
Christoph Hellwig | 17cb960 | 2018-03-13 17:28:41 +0100 | [diff] [blame] | 318 | kfree(job->reply); |
Benjamin Block | 50b4d48 | 2017-08-24 01:57:56 +0200 | [diff] [blame] | 319 | } |
| 320 | |
Jens Axboe | 5e28b8d | 2018-10-26 11:27:02 -0600 | [diff] [blame] | 321 | void bsg_remove_queue(struct request_queue *q) |
| 322 | { |
| 323 | if (q) { |
Jens Axboe | 1028e4b | 2018-10-29 09:47:17 -0600 | [diff] [blame] | 324 | struct bsg_set *bset = |
| 325 | container_of(q->tag_set, struct bsg_set, tag_set); |
Jens Axboe | cd2f076 | 2018-10-24 07:11:39 -0600 | [diff] [blame] | 326 | |
Jens Axboe | 5e28b8d | 2018-10-26 11:27:02 -0600 | [diff] [blame] | 327 | bsg_unregister_queue(q); |
| 328 | blk_cleanup_queue(q); |
Jens Axboe | 1028e4b | 2018-10-29 09:47:17 -0600 | [diff] [blame] | 329 | blk_mq_free_tag_set(&bset->tag_set); |
| 330 | kfree(bset); |
Jens Axboe | 5e28b8d | 2018-10-26 11:27:02 -0600 | [diff] [blame] | 331 | } |
| 332 | } |
| 333 | EXPORT_SYMBOL_GPL(bsg_remove_queue); |
| 334 | |
Jens Axboe | cd2f076 | 2018-10-24 07:11:39 -0600 | [diff] [blame] | 335 | static enum blk_eh_timer_return bsg_timeout(struct request *rq, bool reserved) |
| 336 | { |
Jens Axboe | 1028e4b | 2018-10-29 09:47:17 -0600 | [diff] [blame] | 337 | struct bsg_set *bset = |
| 338 | container_of(rq->q->tag_set, struct bsg_set, tag_set); |
Jens Axboe | cd2f076 | 2018-10-24 07:11:39 -0600 | [diff] [blame] | 339 | |
Jens Axboe | 1028e4b | 2018-10-29 09:47:17 -0600 | [diff] [blame] | 340 | if (!bset->timeout_fn) |
| 341 | return BLK_EH_DONE; |
| 342 | return bset->timeout_fn(rq); |
Jens Axboe | cd2f076 | 2018-10-24 07:11:39 -0600 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | static const struct blk_mq_ops bsg_mq_ops = { |
| 346 | .queue_rq = bsg_queue_rq, |
| 347 | .init_request = bsg_init_rq, |
| 348 | .exit_request = bsg_exit_rq, |
| 349 | .initialize_rq_fn = bsg_initialize_rq, |
| 350 | .complete = bsg_complete, |
| 351 | .timeout = bsg_timeout, |
| 352 | }; |
| 353 | |
Mike Christie | aa387cc | 2011-07-31 22:05:09 +0200 | [diff] [blame] | 354 | /** |
| 355 | * bsg_setup_queue - Create and add the bsg hooks so we can receive requests |
| 356 | * @dev: device to attach bsg device to |
Mike Christie | aa387cc | 2011-07-31 22:05:09 +0200 | [diff] [blame] | 357 | * @name: device to give bsg device |
| 358 | * @job_fn: bsg job handler |
Bart Van Assche | a0b77e3 | 2019-05-30 17:00:51 -0700 | [diff] [blame] | 359 | * @timeout: timeout handler function pointer |
Mike Christie | aa387cc | 2011-07-31 22:05:09 +0200 | [diff] [blame] | 360 | * @dd_job_size: size of LLD data needed for each job |
Mike Christie | aa387cc | 2011-07-31 22:05:09 +0200 | [diff] [blame] | 361 | */ |
Christoph Hellwig | c1225f0 | 2017-08-25 17:37:38 +0200 | [diff] [blame] | 362 | struct request_queue *bsg_setup_queue(struct device *dev, const char *name, |
Jens Axboe | 1028e4b | 2018-10-29 09:47:17 -0600 | [diff] [blame] | 363 | bsg_job_fn *job_fn, bsg_timeout_fn *timeout, int dd_job_size) |
Mike Christie | aa387cc | 2011-07-31 22:05:09 +0200 | [diff] [blame] | 364 | { |
Jens Axboe | 1028e4b | 2018-10-29 09:47:17 -0600 | [diff] [blame] | 365 | struct bsg_set *bset; |
Jens Axboe | cd2f076 | 2018-10-24 07:11:39 -0600 | [diff] [blame] | 366 | struct blk_mq_tag_set *set; |
Christoph Hellwig | 8ae94eb | 2017-01-03 15:25:02 +0300 | [diff] [blame] | 367 | struct request_queue *q; |
Jens Axboe | cd2f076 | 2018-10-24 07:11:39 -0600 | [diff] [blame] | 368 | int ret = -ENOMEM; |
Mike Christie | aa387cc | 2011-07-31 22:05:09 +0200 | [diff] [blame] | 369 | |
Jens Axboe | 1028e4b | 2018-10-29 09:47:17 -0600 | [diff] [blame] | 370 | bset = kzalloc(sizeof(*bset), GFP_KERNEL); |
| 371 | if (!bset) |
Christoph Hellwig | 8ae94eb | 2017-01-03 15:25:02 +0300 | [diff] [blame] | 372 | return ERR_PTR(-ENOMEM); |
Christoph Hellwig | 82ed4db | 2017-01-27 09:46:29 +0100 | [diff] [blame] | 373 | |
Jens Axboe | 1028e4b | 2018-10-29 09:47:17 -0600 | [diff] [blame] | 374 | bset->job_fn = job_fn; |
| 375 | bset->timeout_fn = timeout; |
| 376 | |
| 377 | set = &bset->tag_set; |
Jens Axboe | cd2f076 | 2018-10-24 07:11:39 -0600 | [diff] [blame] | 378 | set->ops = &bsg_mq_ops, |
| 379 | set->nr_hw_queues = 1; |
| 380 | set->queue_depth = 128; |
| 381 | set->numa_node = NUMA_NO_NODE; |
| 382 | set->cmd_size = sizeof(struct bsg_job) + dd_job_size; |
| 383 | set->flags = BLK_MQ_F_NO_SCHED | BLK_MQ_F_BLOCKING; |
| 384 | if (blk_mq_alloc_tag_set(set)) |
| 385 | goto out_tag_set; |
| 386 | |
| 387 | q = blk_mq_init_queue(set); |
| 388 | if (IS_ERR(q)) { |
| 389 | ret = PTR_ERR(q); |
| 390 | goto out_queue; |
| 391 | } |
Christoph Hellwig | 8ae94eb | 2017-01-03 15:25:02 +0300 | [diff] [blame] | 392 | |
Mike Christie | aa387cc | 2011-07-31 22:05:09 +0200 | [diff] [blame] | 393 | q->queuedata = dev; |
Mike Christie | aa387cc | 2011-07-31 22:05:09 +0200 | [diff] [blame] | 394 | blk_queue_rq_timeout(q, BLK_DEFAULT_SG_TIMEOUT); |
| 395 | |
Christoph Hellwig | 5de815a | 2018-05-29 08:40:23 +0200 | [diff] [blame] | 396 | ret = bsg_register_queue(q, dev, name, &bsg_transport_ops); |
Mike Christie | aa387cc | 2011-07-31 22:05:09 +0200 | [diff] [blame] | 397 | if (ret) { |
| 398 | printk(KERN_ERR "%s: bsg interface failed to " |
| 399 | "initialize - register queue\n", dev->kobj.name); |
Christoph Hellwig | 82ed4db | 2017-01-27 09:46:29 +0100 | [diff] [blame] | 400 | goto out_cleanup_queue; |
Mike Christie | aa387cc | 2011-07-31 22:05:09 +0200 | [diff] [blame] | 401 | } |
| 402 | |
Christoph Hellwig | 8ae94eb | 2017-01-03 15:25:02 +0300 | [diff] [blame] | 403 | return q; |
Christoph Hellwig | 82ed4db | 2017-01-27 09:46:29 +0100 | [diff] [blame] | 404 | out_cleanup_queue: |
| 405 | blk_cleanup_queue(q); |
Jens Axboe | cd2f076 | 2018-10-24 07:11:39 -0600 | [diff] [blame] | 406 | out_queue: |
| 407 | blk_mq_free_tag_set(set); |
| 408 | out_tag_set: |
Jens Axboe | 1028e4b | 2018-10-29 09:47:17 -0600 | [diff] [blame] | 409 | kfree(bset); |
Christoph Hellwig | 82ed4db | 2017-01-27 09:46:29 +0100 | [diff] [blame] | 410 | return ERR_PTR(ret); |
Mike Christie | aa387cc | 2011-07-31 22:05:09 +0200 | [diff] [blame] | 411 | } |
| 412 | EXPORT_SYMBOL_GPL(bsg_setup_queue); |