blob: 96ee35256a168f3b70a4e1eeab0eb8a5cf7b0f49 [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 Hellwigce70fd92022-02-24 18:55:47 +010015 struct scsi_cmnd *scmd;
Christoph Hellwig75ca5642021-07-29 08:48:45 +020016 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 Hellwig68ec3b82021-10-21 08:06:05 +020028 rq = scsi_alloc_request(q, hdr->dout_xfer_len ?
29 REQ_OP_DRV_OUT : REQ_OP_DRV_IN, 0);
Christoph Hellwig75ca5642021-07-29 08:48:45 +020030 if (IS_ERR(rq))
31 return PTR_ERR(rq);
32 rq->timeout = timeout;
33
Christoph Hellwigce70fd92022-02-24 18:55:47 +010034 scmd = blk_mq_rq_to_pdu(rq);
35 scmd->cmd_len = hdr->request_len;
36 if (scmd->cmd_len > sizeof(scmd->cmnd)) {
37 ret = -EINVAL;
38 goto out_put_request;
Christoph Hellwig78011042021-07-24 09:20:23 +020039 }
40
Christoph Hellwig75ca5642021-07-29 08:48:45 +020041 ret = -EFAULT;
Christoph Hellwigce70fd92022-02-24 18:55:47 +010042 if (copy_from_user(scmd->cmnd, uptr64(hdr->request), scmd->cmd_len))
43 goto out_put_request;
Christoph Hellwig75ca5642021-07-29 08:48:45 +020044 ret = -EPERM;
Christoph Hellwigce70fd92022-02-24 18:55:47 +010045 if (!scsi_cmd_allowed(scmd->cmnd, mode))
46 goto out_put_request;
Christoph Hellwig78011042021-07-24 09:20:23 +020047
Christoph Hellwig5c0f6132021-07-31 09:40:26 +020048 ret = 0;
Christoph Hellwig75ca5642021-07-29 08:48:45 +020049 if (hdr->dout_xfer_len) {
50 ret = blk_rq_map_user(rq->q, rq, NULL, uptr64(hdr->dout_xferp),
51 hdr->dout_xfer_len, GFP_KERNEL);
52 } else if (hdr->din_xfer_len) {
53 ret = blk_rq_map_user(rq->q, rq, NULL, uptr64(hdr->din_xferp),
54 hdr->din_xfer_len, GFP_KERNEL);
55 }
56
57 if (ret)
Christoph Hellwigce70fd92022-02-24 18:55:47 +010058 goto out_put_request;
Christoph Hellwig75ca5642021-07-29 08:48:45 +020059
60 bio = rq->bio;
Christoph Hellwigb84ba302021-11-26 13:18:01 +010061 blk_execute_rq(rq, !(hdr->flags & BSG_FLAG_Q_AT_TAIL));
Christoph Hellwig78011042021-07-24 09:20:23 +020062
63 /*
64 * fill in all the output members
65 */
Christoph Hellwigdbb4c842022-02-24 18:55:50 +010066 hdr->device_status = scmd->result & 0xff;
67 hdr->transport_status = host_byte(scmd->result);
Christoph Hellwig78011042021-07-24 09:20:23 +020068 hdr->driver_status = 0;
Christoph Hellwigdbb4c842022-02-24 18:55:50 +010069 if (scsi_status_is_check_condition(scmd->result))
Christoph Hellwig78011042021-07-24 09:20:23 +020070 hdr->driver_status = DRIVER_SENSE;
71 hdr->info = 0;
72 if (hdr->device_status || hdr->transport_status || hdr->driver_status)
73 hdr->info |= SG_INFO_CHECK;
74 hdr->response_len = 0;
75
Christoph Hellwig5b794f92022-02-24 18:55:48 +010076 if (scmd->sense_len && hdr->response) {
Christoph Hellwig78011042021-07-24 09:20:23 +020077 int len = min_t(unsigned int, hdr->max_response_len,
Christoph Hellwig5b794f92022-02-24 18:55:48 +010078 scmd->sense_len);
Christoph Hellwig78011042021-07-24 09:20:23 +020079
Christoph Hellwig5b794f92022-02-24 18:55:48 +010080 if (copy_to_user(uptr64(hdr->response), scmd->sense_buffer,
81 len))
Christoph Hellwig78011042021-07-24 09:20:23 +020082 ret = -EFAULT;
83 else
84 hdr->response_len = len;
85 }
86
87 if (rq_data_dir(rq) == READ)
Christoph Hellwiga9a4ea12022-02-24 18:55:49 +010088 hdr->din_resid = scmd->resid_len;
Christoph Hellwig78011042021-07-24 09:20:23 +020089 else
Christoph Hellwiga9a4ea12022-02-24 18:55:49 +010090 hdr->dout_resid = scmd->resid_len;
Christoph Hellwig78011042021-07-24 09:20:23 +020091
Christoph Hellwig75ca5642021-07-29 08:48:45 +020092 blk_rq_unmap_user(bio);
93
Christoph Hellwig75ca5642021-07-29 08:48:45 +020094out_put_request:
Christoph Hellwig0bf6d962021-10-25 09:05:07 +020095 blk_mq_free_request(rq);
Christoph Hellwig78011042021-07-24 09:20:23 +020096 return ret;
97}
98
Christoph Hellwigead09dd2021-07-29 08:48:42 +020099struct bsg_device *scsi_bsg_register_queue(struct scsi_device *sdev)
Christoph Hellwig78011042021-07-24 09:20:23 +0200100{
Christoph Hellwigead09dd2021-07-29 08:48:42 +0200101 return bsg_register_queue(sdev->request_queue, &sdev->sdev_gendev,
Christoph Hellwig75ca5642021-07-29 08:48:45 +0200102 dev_name(&sdev->sdev_gendev), scsi_bsg_sg_io_fn);
Christoph Hellwig78011042021-07-24 09:20:23 +0200103}