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