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