blob: d13a67b8242912da486d6dbf8a24a6b1a194ee1c [file] [log] [blame]
Christoph Hellwig78011042021-07-24 09:20:23 +02001// 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 Hellwig75ca5642021-07-29 08:48:45 +020012static int scsi_bsg_sg_io_fn(struct request_queue *q, struct sg_io_v4 *hdr,
13 fmode_t mode, unsigned int timeout)
Christoph Hellwig78011042021-07-24 09:20:23 +020014{
Christoph Hellwig75ca5642021-07-29 08:48:45 +020015 struct scsi_request *sreq;
16 struct request *rq;
17 struct bio *bio;
18 int ret;
19
Christoph Hellwig78011042021-07-24 09:20:23 +020020 if (hdr->protocol != BSG_PROTOCOL_SCSI ||
21 hdr->subprotocol != BSG_SUB_PROTOCOL_SCSI_CMD)
22 return -EINVAL;
Christoph Hellwig78011042021-07-24 09:20:23 +020023 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 Hellwig75ca5642021-07-29 08:48:45 +020028 rq = blk_get_request(q, hdr->dout_xfer_len ?
29 REQ_OP_DRV_OUT : REQ_OP_DRV_IN, 0);
30 if (IS_ERR(rq))
31 return PTR_ERR(rq);
32 rq->timeout = timeout;
33
34 ret = -ENOMEM;
35 sreq = scsi_req(rq);
Christoph Hellwig78011042021-07-24 09:20:23 +020036 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 Hellwig75ca5642021-07-29 08:48:45 +020040 goto out_put_request;
Christoph Hellwig78011042021-07-24 09:20:23 +020041 }
42
Christoph Hellwig75ca5642021-07-29 08:48:45 +020043 ret = -EFAULT;
Christoph Hellwig78011042021-07-24 09:20:23 +020044 if (copy_from_user(sreq->cmd, uptr64(hdr->request), sreq->cmd_len))
Christoph Hellwig75ca5642021-07-29 08:48:45 +020045 goto out_free_cmd;
46 ret = -EPERM;
Christoph Hellwig7353dc02021-07-24 09:20:26 +020047 if (!scsi_cmd_allowed(sreq->cmd, mode))
Christoph Hellwig75ca5642021-07-29 08:48:45 +020048 goto out_free_cmd;
Christoph Hellwig78011042021-07-24 09:20:23 +020049
Christoph Hellwig75ca5642021-07-29 08:48:45 +020050 if (hdr->dout_xfer_len) {
51 ret = blk_rq_map_user(rq->q, rq, NULL, uptr64(hdr->dout_xferp),
52 hdr->dout_xfer_len, GFP_KERNEL);
53 } else if (hdr->din_xfer_len) {
54 ret = blk_rq_map_user(rq->q, rq, NULL, uptr64(hdr->din_xferp),
55 hdr->din_xfer_len, GFP_KERNEL);
56 }
57
58 if (ret)
59 goto out_free_cmd;
60
61 bio = rq->bio;
62 blk_execute_rq(NULL, rq, !(hdr->flags & BSG_FLAG_Q_AT_TAIL));
Christoph Hellwig78011042021-07-24 09:20:23 +020063
64 /*
65 * fill in all the output members
66 */
67 hdr->device_status = sreq->result & 0xff;
68 hdr->transport_status = host_byte(sreq->result);
69 hdr->driver_status = 0;
70 if (scsi_status_is_check_condition(sreq->result))
71 hdr->driver_status = DRIVER_SENSE;
72 hdr->info = 0;
73 if (hdr->device_status || hdr->transport_status || hdr->driver_status)
74 hdr->info |= SG_INFO_CHECK;
75 hdr->response_len = 0;
76
77 if (sreq->sense_len && hdr->response) {
78 int len = min_t(unsigned int, hdr->max_response_len,
79 sreq->sense_len);
80
81 if (copy_to_user(uptr64(hdr->response), sreq->sense, len))
82 ret = -EFAULT;
83 else
84 hdr->response_len = len;
85 }
86
87 if (rq_data_dir(rq) == READ)
88 hdr->din_resid = sreq->resid_len;
89 else
90 hdr->dout_resid = sreq->resid_len;
91
Christoph Hellwig75ca5642021-07-29 08:48:45 +020092 blk_rq_unmap_user(bio);
93
94out_free_cmd:
95 scsi_req_free_cmd(scsi_req(rq));
96out_put_request:
97 blk_put_request(rq);
Christoph Hellwig78011042021-07-24 09:20:23 +020098 return ret;
99}
100
Christoph Hellwigead09dd2021-07-29 08:48:42 +0200101struct bsg_device *scsi_bsg_register_queue(struct scsi_device *sdev)
Christoph Hellwig78011042021-07-24 09:20:23 +0200102{
Christoph Hellwigead09dd2021-07-29 08:48:42 +0200103 return bsg_register_queue(sdev->request_queue, &sdev->sdev_gendev,
Christoph Hellwig75ca5642021-07-29 08:48:45 +0200104 dev_name(&sdev->sdev_gendev), scsi_bsg_sg_io_fn);
Christoph Hellwig78011042021-07-24 09:20:23 +0200105}