Christoph Hellwig | 7801104 | 2021-07-24 09:20:23 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | #include <linux/bsg.h> |
| 3 | #include <scsi/scsi.h> |
| 4 | #include <scsi/scsi_ioctl.h> |
| 5 | #include <scsi/scsi_cmnd.h> |
| 6 | #include <scsi/scsi_device.h> |
| 7 | #include <scsi/sg.h> |
| 8 | #include "scsi_priv.h" |
| 9 | |
| 10 | #define uptr64(val) ((void __user *)(uintptr_t)(val)) |
| 11 | |
Christoph Hellwig | 75ca564 | 2021-07-29 08:48:45 +0200 | [diff] [blame] | 12 | static int scsi_bsg_sg_io_fn(struct request_queue *q, struct sg_io_v4 *hdr, |
| 13 | fmode_t mode, unsigned int timeout) |
Christoph Hellwig | 7801104 | 2021-07-24 09:20:23 +0200 | [diff] [blame] | 14 | { |
Christoph Hellwig | 75ca564 | 2021-07-29 08:48:45 +0200 | [diff] [blame] | 15 | struct scsi_request *sreq; |
| 16 | struct request *rq; |
| 17 | struct bio *bio; |
| 18 | int ret; |
| 19 | |
Christoph Hellwig | 7801104 | 2021-07-24 09:20:23 +0200 | [diff] [blame] | 20 | if (hdr->protocol != BSG_PROTOCOL_SCSI || |
| 21 | hdr->subprotocol != BSG_SUB_PROTOCOL_SCSI_CMD) |
| 22 | return -EINVAL; |
Christoph Hellwig | 7801104 | 2021-07-24 09:20:23 +0200 | [diff] [blame] | 23 | if (hdr->dout_xfer_len && hdr->din_xfer_len) { |
| 24 | pr_warn_once("BIDI support in bsg has been removed.\n"); |
| 25 | return -EOPNOTSUPP; |
| 26 | } |
| 27 | |
Christoph Hellwig | 68ec3b8 | 2021-10-21 08:06:05 +0200 | [diff] [blame] | 28 | rq = scsi_alloc_request(q, hdr->dout_xfer_len ? |
| 29 | REQ_OP_DRV_OUT : REQ_OP_DRV_IN, 0); |
Christoph Hellwig | 75ca564 | 2021-07-29 08:48:45 +0200 | [diff] [blame] | 30 | if (IS_ERR(rq)) |
| 31 | return PTR_ERR(rq); |
| 32 | rq->timeout = timeout; |
| 33 | |
| 34 | ret = -ENOMEM; |
| 35 | sreq = scsi_req(rq); |
Christoph Hellwig | 7801104 | 2021-07-24 09:20:23 +0200 | [diff] [blame] | 36 | sreq->cmd_len = hdr->request_len; |
| 37 | if (sreq->cmd_len > BLK_MAX_CDB) { |
| 38 | sreq->cmd = kzalloc(sreq->cmd_len, GFP_KERNEL); |
| 39 | if (!sreq->cmd) |
Christoph Hellwig | 75ca564 | 2021-07-29 08:48:45 +0200 | [diff] [blame] | 40 | goto out_put_request; |
Christoph Hellwig | 7801104 | 2021-07-24 09:20:23 +0200 | [diff] [blame] | 41 | } |
| 42 | |
Christoph Hellwig | 75ca564 | 2021-07-29 08:48:45 +0200 | [diff] [blame] | 43 | ret = -EFAULT; |
Christoph Hellwig | 7801104 | 2021-07-24 09:20:23 +0200 | [diff] [blame] | 44 | if (copy_from_user(sreq->cmd, uptr64(hdr->request), sreq->cmd_len)) |
Christoph Hellwig | 75ca564 | 2021-07-29 08:48:45 +0200 | [diff] [blame] | 45 | goto out_free_cmd; |
| 46 | ret = -EPERM; |
Christoph Hellwig | 7353dc0 | 2021-07-24 09:20:26 +0200 | [diff] [blame] | 47 | if (!scsi_cmd_allowed(sreq->cmd, mode)) |
Christoph Hellwig | 75ca564 | 2021-07-29 08:48:45 +0200 | [diff] [blame] | 48 | goto out_free_cmd; |
Christoph Hellwig | 7801104 | 2021-07-24 09:20:23 +0200 | [diff] [blame] | 49 | |
Christoph Hellwig | 5c0f613 | 2021-07-31 09:40:26 +0200 | [diff] [blame] | 50 | ret = 0; |
Christoph Hellwig | 75ca564 | 2021-07-29 08:48:45 +0200 | [diff] [blame] | 51 | if (hdr->dout_xfer_len) { |
| 52 | ret = blk_rq_map_user(rq->q, rq, NULL, uptr64(hdr->dout_xferp), |
| 53 | hdr->dout_xfer_len, GFP_KERNEL); |
| 54 | } else if (hdr->din_xfer_len) { |
| 55 | ret = blk_rq_map_user(rq->q, rq, NULL, uptr64(hdr->din_xferp), |
| 56 | hdr->din_xfer_len, GFP_KERNEL); |
| 57 | } |
| 58 | |
| 59 | if (ret) |
| 60 | goto out_free_cmd; |
| 61 | |
| 62 | bio = rq->bio; |
| 63 | blk_execute_rq(NULL, rq, !(hdr->flags & BSG_FLAG_Q_AT_TAIL)); |
Christoph Hellwig | 7801104 | 2021-07-24 09:20:23 +0200 | [diff] [blame] | 64 | |
| 65 | /* |
| 66 | * fill in all the output members |
| 67 | */ |
| 68 | hdr->device_status = sreq->result & 0xff; |
| 69 | hdr->transport_status = host_byte(sreq->result); |
| 70 | hdr->driver_status = 0; |
| 71 | if (scsi_status_is_check_condition(sreq->result)) |
| 72 | hdr->driver_status = DRIVER_SENSE; |
| 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 (sreq->sense_len && hdr->response) { |
| 79 | int len = min_t(unsigned int, hdr->max_response_len, |
| 80 | sreq->sense_len); |
| 81 | |
| 82 | if (copy_to_user(uptr64(hdr->response), sreq->sense, len)) |
| 83 | ret = -EFAULT; |
| 84 | else |
| 85 | hdr->response_len = len; |
| 86 | } |
| 87 | |
| 88 | if (rq_data_dir(rq) == READ) |
| 89 | hdr->din_resid = sreq->resid_len; |
| 90 | else |
| 91 | hdr->dout_resid = sreq->resid_len; |
| 92 | |
Christoph Hellwig | 75ca564 | 2021-07-29 08:48:45 +0200 | [diff] [blame] | 93 | blk_rq_unmap_user(bio); |
| 94 | |
| 95 | out_free_cmd: |
| 96 | scsi_req_free_cmd(scsi_req(rq)); |
| 97 | out_put_request: |
Christoph Hellwig | 0bf6d96 | 2021-10-25 09:05:07 +0200 | [diff] [blame] | 98 | blk_mq_free_request(rq); |
Christoph Hellwig | 7801104 | 2021-07-24 09:20:23 +0200 | [diff] [blame] | 99 | return ret; |
| 100 | } |
| 101 | |
Christoph Hellwig | ead09dd | 2021-07-29 08:48:42 +0200 | [diff] [blame] | 102 | struct bsg_device *scsi_bsg_register_queue(struct scsi_device *sdev) |
Christoph Hellwig | 7801104 | 2021-07-24 09:20:23 +0200 | [diff] [blame] | 103 | { |
Christoph Hellwig | ead09dd | 2021-07-29 08:48:42 +0200 | [diff] [blame] | 104 | return bsg_register_queue(sdev->request_queue, &sdev->sdev_gendev, |
Christoph Hellwig | 75ca564 | 2021-07-29 08:48:45 +0200 | [diff] [blame] | 105 | dev_name(&sdev->sdev_gendev), scsi_bsg_sg_io_fn); |
Christoph Hellwig | 7801104 | 2021-07-24 09:20:23 +0200 | [diff] [blame] | 106 | } |