| .. | .. |
|---|
| 1 | +// SPDX-License-Identifier: GPL-2.0 |
|---|
| 1 | 2 | /* |
|---|
| 2 | 3 | * bsg.c - block layer implementation of the sg v4 interface |
|---|
| 3 | | - * |
|---|
| 4 | | - * Copyright (C) 2004 Jens Axboe <axboe@suse.de> SUSE Labs |
|---|
| 5 | | - * Copyright (C) 2004 Peter M. Jones <pjones@redhat.com> |
|---|
| 6 | | - * |
|---|
| 7 | | - * This file is subject to the terms and conditions of the GNU General Public |
|---|
| 8 | | - * License version 2. See the file "COPYING" in the main directory of this |
|---|
| 9 | | - * archive for more details. |
|---|
| 10 | | - * |
|---|
| 11 | 4 | */ |
|---|
| 12 | 5 | #include <linux/module.h> |
|---|
| 13 | 6 | #include <linux/init.h> |
|---|
| .. | .. |
|---|
| 19 | 12 | #include <linux/idr.h> |
|---|
| 20 | 13 | #include <linux/bsg.h> |
|---|
| 21 | 14 | #include <linux/slab.h> |
|---|
| 15 | +#include <linux/pm_runtime.h> |
|---|
| 22 | 16 | |
|---|
| 23 | 17 | #include <scsi/scsi.h> |
|---|
| 24 | 18 | #include <scsi/scsi_ioctl.h> |
|---|
| .. | .. |
|---|
| 74 | 68 | { |
|---|
| 75 | 69 | struct scsi_request *sreq = scsi_req(rq); |
|---|
| 76 | 70 | |
|---|
| 71 | + if (hdr->dout_xfer_len && hdr->din_xfer_len) { |
|---|
| 72 | + pr_warn_once("BIDI support in bsg has been removed.\n"); |
|---|
| 73 | + return -EOPNOTSUPP; |
|---|
| 74 | + } |
|---|
| 75 | + |
|---|
| 77 | 76 | sreq->cmd_len = hdr->request_len; |
|---|
| 78 | 77 | if (sreq->cmd_len > BLK_MAX_CDB) { |
|---|
| 79 | 78 | sreq->cmd = kzalloc(sreq->cmd_len, GFP_KERNEL); |
|---|
| .. | .. |
|---|
| 114 | 113 | hdr->response_len = len; |
|---|
| 115 | 114 | } |
|---|
| 116 | 115 | |
|---|
| 117 | | - if (rq->next_rq) { |
|---|
| 118 | | - hdr->dout_resid = sreq->resid_len; |
|---|
| 119 | | - hdr->din_resid = scsi_req(rq->next_rq)->resid_len; |
|---|
| 120 | | - } else if (rq_data_dir(rq) == READ) { |
|---|
| 116 | + if (rq_data_dir(rq) == READ) |
|---|
| 121 | 117 | hdr->din_resid = sreq->resid_len; |
|---|
| 122 | | - } else { |
|---|
| 118 | + else |
|---|
| 123 | 119 | hdr->dout_resid = sreq->resid_len; |
|---|
| 124 | | - } |
|---|
| 125 | 120 | |
|---|
| 126 | 121 | return ret; |
|---|
| 127 | 122 | } |
|---|
| .. | .. |
|---|
| 138 | 133 | .free_rq = bsg_scsi_free_rq, |
|---|
| 139 | 134 | }; |
|---|
| 140 | 135 | |
|---|
| 141 | | -static struct request * |
|---|
| 142 | | -bsg_map_hdr(struct request_queue *q, struct sg_io_v4 *hdr, fmode_t mode) |
|---|
| 136 | +static int bsg_sg_io(struct request_queue *q, fmode_t mode, void __user *uarg) |
|---|
| 143 | 137 | { |
|---|
| 144 | | - struct request *rq, *next_rq = NULL; |
|---|
| 138 | + struct request *rq; |
|---|
| 139 | + struct bio *bio; |
|---|
| 140 | + struct sg_io_v4 hdr; |
|---|
| 145 | 141 | int ret; |
|---|
| 146 | 142 | |
|---|
| 143 | + if (copy_from_user(&hdr, uarg, sizeof(hdr))) |
|---|
| 144 | + return -EFAULT; |
|---|
| 145 | + |
|---|
| 147 | 146 | if (!q->bsg_dev.class_dev) |
|---|
| 148 | | - return ERR_PTR(-ENXIO); |
|---|
| 147 | + return -ENXIO; |
|---|
| 149 | 148 | |
|---|
| 150 | | - if (hdr->guard != 'Q') |
|---|
| 151 | | - return ERR_PTR(-EINVAL); |
|---|
| 152 | | - |
|---|
| 153 | | - ret = q->bsg_dev.ops->check_proto(hdr); |
|---|
| 149 | + if (hdr.guard != 'Q') |
|---|
| 150 | + return -EINVAL; |
|---|
| 151 | + ret = q->bsg_dev.ops->check_proto(&hdr); |
|---|
| 154 | 152 | if (ret) |
|---|
| 155 | | - return ERR_PTR(ret); |
|---|
| 153 | + return ret; |
|---|
| 156 | 154 | |
|---|
| 157 | | - rq = blk_get_request(q, hdr->dout_xfer_len ? |
|---|
| 155 | + rq = blk_get_request(q, hdr.dout_xfer_len ? |
|---|
| 158 | 156 | REQ_OP_SCSI_OUT : REQ_OP_SCSI_IN, 0); |
|---|
| 159 | 157 | if (IS_ERR(rq)) |
|---|
| 160 | | - return rq; |
|---|
| 158 | + return PTR_ERR(rq); |
|---|
| 161 | 159 | |
|---|
| 162 | | - ret = q->bsg_dev.ops->fill_hdr(rq, hdr, mode); |
|---|
| 163 | | - if (ret) |
|---|
| 164 | | - goto out; |
|---|
| 160 | + ret = q->bsg_dev.ops->fill_hdr(rq, &hdr, mode); |
|---|
| 161 | + if (ret) { |
|---|
| 162 | + blk_put_request(rq); |
|---|
| 163 | + return ret; |
|---|
| 164 | + } |
|---|
| 165 | 165 | |
|---|
| 166 | | - rq->timeout = msecs_to_jiffies(hdr->timeout); |
|---|
| 166 | + rq->timeout = msecs_to_jiffies(hdr.timeout); |
|---|
| 167 | 167 | if (!rq->timeout) |
|---|
| 168 | 168 | rq->timeout = q->sg_timeout; |
|---|
| 169 | 169 | if (!rq->timeout) |
|---|
| .. | .. |
|---|
| 171 | 171 | if (rq->timeout < BLK_MIN_SG_TIMEOUT) |
|---|
| 172 | 172 | rq->timeout = BLK_MIN_SG_TIMEOUT; |
|---|
| 173 | 173 | |
|---|
| 174 | | - if (hdr->dout_xfer_len && hdr->din_xfer_len) { |
|---|
| 175 | | - if (!test_bit(QUEUE_FLAG_BIDI, &q->queue_flags)) { |
|---|
| 176 | | - ret = -EOPNOTSUPP; |
|---|
| 177 | | - goto out; |
|---|
| 178 | | - } |
|---|
| 179 | | - |
|---|
| 180 | | - next_rq = blk_get_request(q, REQ_OP_SCSI_IN, 0); |
|---|
| 181 | | - if (IS_ERR(next_rq)) { |
|---|
| 182 | | - ret = PTR_ERR(next_rq); |
|---|
| 183 | | - goto out; |
|---|
| 184 | | - } |
|---|
| 185 | | - |
|---|
| 186 | | - rq->next_rq = next_rq; |
|---|
| 187 | | - ret = blk_rq_map_user(q, next_rq, NULL, uptr64(hdr->din_xferp), |
|---|
| 188 | | - hdr->din_xfer_len, GFP_KERNEL); |
|---|
| 189 | | - if (ret) |
|---|
| 190 | | - goto out_free_nextrq; |
|---|
| 191 | | - } |
|---|
| 192 | | - |
|---|
| 193 | | - if (hdr->dout_xfer_len) { |
|---|
| 194 | | - ret = blk_rq_map_user(q, rq, NULL, uptr64(hdr->dout_xferp), |
|---|
| 195 | | - hdr->dout_xfer_len, GFP_KERNEL); |
|---|
| 196 | | - } else if (hdr->din_xfer_len) { |
|---|
| 197 | | - ret = blk_rq_map_user(q, rq, NULL, uptr64(hdr->din_xferp), |
|---|
| 198 | | - hdr->din_xfer_len, GFP_KERNEL); |
|---|
| 174 | + if (hdr.dout_xfer_len) { |
|---|
| 175 | + ret = blk_rq_map_user(q, rq, NULL, uptr64(hdr.dout_xferp), |
|---|
| 176 | + hdr.dout_xfer_len, GFP_KERNEL); |
|---|
| 177 | + } else if (hdr.din_xfer_len) { |
|---|
| 178 | + ret = blk_rq_map_user(q, rq, NULL, uptr64(hdr.din_xferp), |
|---|
| 179 | + hdr.din_xfer_len, GFP_KERNEL); |
|---|
| 199 | 180 | } |
|---|
| 200 | 181 | |
|---|
| 201 | 182 | if (ret) |
|---|
| 202 | | - goto out_unmap_nextrq; |
|---|
| 203 | | - return rq; |
|---|
| 183 | + goto out_free_rq; |
|---|
| 204 | 184 | |
|---|
| 205 | | -out_unmap_nextrq: |
|---|
| 206 | | - if (rq->next_rq) |
|---|
| 207 | | - blk_rq_unmap_user(rq->next_rq->bio); |
|---|
| 208 | | -out_free_nextrq: |
|---|
| 209 | | - if (rq->next_rq) |
|---|
| 210 | | - blk_put_request(rq->next_rq); |
|---|
| 211 | | -out: |
|---|
| 212 | | - q->bsg_dev.ops->free_rq(rq); |
|---|
| 213 | | - blk_put_request(rq); |
|---|
| 214 | | - return ERR_PTR(ret); |
|---|
| 215 | | -} |
|---|
| 185 | + bio = rq->bio; |
|---|
| 216 | 186 | |
|---|
| 217 | | -static int blk_complete_sgv4_hdr_rq(struct request *rq, struct sg_io_v4 *hdr, |
|---|
| 218 | | - struct bio *bio, struct bio *bidi_bio) |
|---|
| 219 | | -{ |
|---|
| 220 | | - int ret; |
|---|
| 221 | | - |
|---|
| 222 | | - ret = rq->q->bsg_dev.ops->complete_rq(rq, hdr); |
|---|
| 223 | | - |
|---|
| 224 | | - if (rq->next_rq) { |
|---|
| 225 | | - blk_rq_unmap_user(bidi_bio); |
|---|
| 226 | | - blk_put_request(rq->next_rq); |
|---|
| 227 | | - } |
|---|
| 228 | | - |
|---|
| 187 | + blk_execute_rq(q, NULL, rq, !(hdr.flags & BSG_FLAG_Q_AT_TAIL)); |
|---|
| 188 | + ret = rq->q->bsg_dev.ops->complete_rq(rq, &hdr); |
|---|
| 229 | 189 | blk_rq_unmap_user(bio); |
|---|
| 190 | + |
|---|
| 191 | +out_free_rq: |
|---|
| 230 | 192 | rq->q->bsg_dev.ops->free_rq(rq); |
|---|
| 231 | 193 | blk_put_request(rq); |
|---|
| 194 | + if (!ret && copy_to_user(uarg, &hdr, sizeof(hdr))) |
|---|
| 195 | + return -EFAULT; |
|---|
| 232 | 196 | return ret; |
|---|
| 233 | 197 | } |
|---|
| 234 | 198 | |
|---|
| .. | .. |
|---|
| 345 | 309 | static int bsg_open(struct inode *inode, struct file *file) |
|---|
| 346 | 310 | { |
|---|
| 347 | 311 | struct bsg_device *bd; |
|---|
| 312 | + struct bsg_class_device *bcd; |
|---|
| 348 | 313 | |
|---|
| 349 | 314 | bd = bsg_get_device(inode, file); |
|---|
| 350 | 315 | |
|---|
| 351 | 316 | if (IS_ERR(bd)) |
|---|
| 352 | 317 | return PTR_ERR(bd); |
|---|
| 353 | 318 | |
|---|
| 319 | + bcd = &bd->queue->bsg_dev; |
|---|
| 320 | + pm_runtime_get_sync(bcd->class_dev->parent); |
|---|
| 354 | 321 | file->private_data = bd; |
|---|
| 355 | 322 | return 0; |
|---|
| 356 | 323 | } |
|---|
| .. | .. |
|---|
| 358 | 325 | static int bsg_release(struct inode *inode, struct file *file) |
|---|
| 359 | 326 | { |
|---|
| 360 | 327 | struct bsg_device *bd = file->private_data; |
|---|
| 328 | + struct bsg_class_device *bcd; |
|---|
| 361 | 329 | |
|---|
| 362 | 330 | file->private_data = NULL; |
|---|
| 331 | + |
|---|
| 332 | + bcd = &bd->queue->bsg_dev; |
|---|
| 333 | + pm_runtime_put_sync(bcd->class_dev->parent); |
|---|
| 363 | 334 | return bsg_put_device(bd); |
|---|
| 335 | +} |
|---|
| 336 | + |
|---|
| 337 | +static int bsg_get_command_q(struct bsg_device *bd, int __user *uarg) |
|---|
| 338 | +{ |
|---|
| 339 | + return put_user(bd->max_queue, uarg); |
|---|
| 340 | +} |
|---|
| 341 | + |
|---|
| 342 | +static int bsg_set_command_q(struct bsg_device *bd, int __user *uarg) |
|---|
| 343 | +{ |
|---|
| 344 | + int queue; |
|---|
| 345 | + |
|---|
| 346 | + if (get_user(queue, uarg)) |
|---|
| 347 | + return -EFAULT; |
|---|
| 348 | + if (queue < 1) |
|---|
| 349 | + return -EINVAL; |
|---|
| 350 | + |
|---|
| 351 | + spin_lock_irq(&bd->lock); |
|---|
| 352 | + bd->max_queue = queue; |
|---|
| 353 | + spin_unlock_irq(&bd->lock); |
|---|
| 354 | + return 0; |
|---|
| 364 | 355 | } |
|---|
| 365 | 356 | |
|---|
| 366 | 357 | static long bsg_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
|---|
| 367 | 358 | { |
|---|
| 368 | 359 | struct bsg_device *bd = file->private_data; |
|---|
| 369 | | - int __user *uarg = (int __user *) arg; |
|---|
| 370 | | - int ret; |
|---|
| 360 | + void __user *uarg = (void __user *) arg; |
|---|
| 371 | 361 | |
|---|
| 372 | 362 | switch (cmd) { |
|---|
| 373 | | - /* |
|---|
| 374 | | - * our own ioctls |
|---|
| 375 | | - */ |
|---|
| 363 | + /* |
|---|
| 364 | + * Our own ioctls |
|---|
| 365 | + */ |
|---|
| 376 | 366 | case SG_GET_COMMAND_Q: |
|---|
| 377 | | - return put_user(bd->max_queue, uarg); |
|---|
| 378 | | - case SG_SET_COMMAND_Q: { |
|---|
| 379 | | - int queue; |
|---|
| 380 | | - |
|---|
| 381 | | - if (get_user(queue, uarg)) |
|---|
| 382 | | - return -EFAULT; |
|---|
| 383 | | - if (queue < 1) |
|---|
| 384 | | - return -EINVAL; |
|---|
| 385 | | - |
|---|
| 386 | | - spin_lock_irq(&bd->lock); |
|---|
| 387 | | - bd->max_queue = queue; |
|---|
| 388 | | - spin_unlock_irq(&bd->lock); |
|---|
| 389 | | - return 0; |
|---|
| 390 | | - } |
|---|
| 367 | + return bsg_get_command_q(bd, uarg); |
|---|
| 368 | + case SG_SET_COMMAND_Q: |
|---|
| 369 | + return bsg_set_command_q(bd, uarg); |
|---|
| 391 | 370 | |
|---|
| 392 | 371 | /* |
|---|
| 393 | 372 | * SCSI/sg ioctls |
|---|
| .. | .. |
|---|
| 400 | 379 | case SG_GET_RESERVED_SIZE: |
|---|
| 401 | 380 | case SG_SET_RESERVED_SIZE: |
|---|
| 402 | 381 | case SG_EMULATED_HOST: |
|---|
| 403 | | - case SCSI_IOCTL_SEND_COMMAND: { |
|---|
| 404 | | - void __user *uarg = (void __user *) arg; |
|---|
| 405 | 382 | return scsi_cmd_ioctl(bd->queue, NULL, file->f_mode, cmd, uarg); |
|---|
| 406 | | - } |
|---|
| 407 | | - case SG_IO: { |
|---|
| 408 | | - struct request *rq; |
|---|
| 409 | | - struct bio *bio, *bidi_bio = NULL; |
|---|
| 410 | | - struct sg_io_v4 hdr; |
|---|
| 411 | | - int at_head; |
|---|
| 412 | | - |
|---|
| 413 | | - if (copy_from_user(&hdr, uarg, sizeof(hdr))) |
|---|
| 414 | | - return -EFAULT; |
|---|
| 415 | | - |
|---|
| 416 | | - rq = bsg_map_hdr(bd->queue, &hdr, file->f_mode); |
|---|
| 417 | | - if (IS_ERR(rq)) |
|---|
| 418 | | - return PTR_ERR(rq); |
|---|
| 419 | | - |
|---|
| 420 | | - bio = rq->bio; |
|---|
| 421 | | - if (rq->next_rq) |
|---|
| 422 | | - bidi_bio = rq->next_rq->bio; |
|---|
| 423 | | - |
|---|
| 424 | | - at_head = (0 == (hdr.flags & BSG_FLAG_Q_AT_TAIL)); |
|---|
| 425 | | - blk_execute_rq(bd->queue, NULL, rq, at_head); |
|---|
| 426 | | - ret = blk_complete_sgv4_hdr_rq(rq, &hdr, bio, bidi_bio); |
|---|
| 427 | | - |
|---|
| 428 | | - if (copy_to_user(uarg, &hdr, sizeof(hdr))) |
|---|
| 429 | | - return -EFAULT; |
|---|
| 430 | | - |
|---|
| 431 | | - return ret; |
|---|
| 432 | | - } |
|---|
| 383 | + case SG_IO: |
|---|
| 384 | + return bsg_sg_io(bd->queue, file->f_mode, uarg); |
|---|
| 385 | + case SCSI_IOCTL_SEND_COMMAND: |
|---|
| 386 | + pr_warn_ratelimited("%s: calling unsupported SCSI_IOCTL_SEND_COMMAND\n", |
|---|
| 387 | + current->comm); |
|---|
| 388 | + return -EINVAL; |
|---|
| 433 | 389 | default: |
|---|
| 434 | 390 | return -ENOTTY; |
|---|
| 435 | 391 | } |
|---|
| .. | .. |
|---|
| 439 | 395 | .open = bsg_open, |
|---|
| 440 | 396 | .release = bsg_release, |
|---|
| 441 | 397 | .unlocked_ioctl = bsg_ioctl, |
|---|
| 398 | + .compat_ioctl = compat_ptr_ioctl, |
|---|
| 442 | 399 | .owner = THIS_MODULE, |
|---|
| 443 | 400 | .llseek = default_llseek, |
|---|
| 444 | 401 | }; |
|---|
| .. | .. |
|---|
| 471 | 428 | /* |
|---|
| 472 | 429 | * we need a proper transport to send commands, not a stacked device |
|---|
| 473 | 430 | */ |
|---|
| 474 | | - if (!queue_is_rq_based(q)) |
|---|
| 431 | + if (!queue_is_mq(q)) |
|---|
| 475 | 432 | return 0; |
|---|
| 476 | 433 | |
|---|
| 477 | 434 | bcd = &q->bsg_dev; |
|---|