blob: 62707c774d743c31ed04ce86f2efa84abd7f139a [file] [log] [blame]
Christoph Hellwig8c165672019-04-30 14:42:39 -04001// SPDX-License-Identifier: GPL-2.0
Jens Axboe3d6392c2007-07-09 12:38:05 +02002/*
FUJITA Tomonori0c6a89b2007-07-29 23:00:46 +09003 * bsg.c - block layer implementation of the sg v4 interface
Jens Axboe3d6392c2007-07-09 12:38:05 +02004 */
Jens Axboe3d6392c2007-07-09 12:38:05 +02005#include <linux/module.h>
6#include <linux/init.h>
7#include <linux/file.h>
8#include <linux/blkdev.h>
Jens Axboe3d6392c2007-07-09 12:38:05 +02009#include <linux/cdev.h>
Randy Dunlapad5ebd22009-11-11 13:47:45 +010010#include <linux/jiffies.h>
Jens Axboe3d6392c2007-07-09 12:38:05 +020011#include <linux/percpu.h>
FUJITA Tomonori598443a2007-07-23 09:33:26 +090012#include <linux/idr.h>
Jens Axboe3d6392c2007-07-09 12:38:05 +020013#include <linux/bsg.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090014#include <linux/slab.h>
Asutosh Dase48a2622021-01-26 19:30:44 -080015#include <linux/pm_runtime.h>
Jens Axboe3d6392c2007-07-09 12:38:05 +020016
17#include <scsi/scsi.h>
18#include <scsi/scsi_ioctl.h>
19#include <scsi/scsi_cmnd.h>
FUJITA Tomonori4e2872d2007-03-28 13:29:24 +020020#include <scsi/scsi_device.h>
21#include <scsi/scsi_driver.h>
Jens Axboe3d6392c2007-07-09 12:38:05 +020022#include <scsi/sg.h>
23
FUJITA Tomonori0ed081c2007-07-17 12:21:35 +020024#define BSG_DESCRIPTION "Block layer SCSI generic (bsg) driver"
25#define BSG_VERSION "0.4"
Jens Axboe3d6392c2007-07-09 12:38:05 +020026
Johannes Thumshirn3124b652018-01-24 09:50:06 -070027#define bsg_dbg(bd, fmt, ...) \
28 pr_debug("%s: " fmt, (bd)->name, ##__VA_ARGS__)
29
Jens Axboe3d6392c2007-07-09 12:38:05 +020030struct bsg_device {
Jens Axboe165125e2007-07-24 09:28:11 +020031 struct request_queue *queue;
Jens Axboe3d6392c2007-07-09 12:38:05 +020032 spinlock_t lock;
Jens Axboe3d6392c2007-07-09 12:38:05 +020033 struct hlist_node dev_list;
John Pittmandb1939542018-08-27 14:33:05 -040034 refcount_t ref_count;
Kay Sievers3ada8b72009-01-06 10:44:43 -080035 char name[20];
Jens Axboe3d6392c2007-07-09 12:38:05 +020036 int max_queue;
Jens Axboe3d6392c2007-07-09 12:38:05 +020037};
38
Jens Axboe5309cb32007-01-23 16:24:41 +010039#define BSG_DEFAULT_CMDS 64
FUJITA Tomonori292b7f22007-03-28 13:29:58 +020040#define BSG_MAX_DEVS 32768
Jens Axboe3d6392c2007-07-09 12:38:05 +020041
Jens Axboe3d6392c2007-07-09 12:38:05 +020042static DEFINE_MUTEX(bsg_mutex);
FUJITA Tomonori598443a2007-07-23 09:33:26 +090043static DEFINE_IDR(bsg_minor_idr);
Jens Axboe3d6392c2007-07-09 12:38:05 +020044
Jens Axboe25fd1642007-07-17 08:52:29 +020045#define BSG_LIST_ARRAY_SIZE 8
Jens Axboe25fd1642007-07-17 08:52:29 +020046static struct hlist_head bsg_device_list[BSG_LIST_ARRAY_SIZE];
Jens Axboe3d6392c2007-07-09 12:38:05 +020047
48static struct class *bsg_class;
Jens Axboe46f6ef42007-07-17 08:56:10 +020049static int bsg_major;
Jens Axboe3d6392c2007-07-09 12:38:05 +020050
FUJITA Tomonori1c1133e2007-07-17 12:21:15 +020051static inline struct hlist_head *bsg_dev_idx_hash(int index)
Jens Axboe3d6392c2007-07-09 12:38:05 +020052{
FUJITA Tomonori1c1133e2007-07-17 12:21:15 +020053 return &bsg_device_list[index & (BSG_LIST_ARRAY_SIZE - 1)];
Jens Axboe3d6392c2007-07-09 12:38:05 +020054}
55
Christoph Hellwig17cb9602018-03-13 17:28:41 +010056#define uptr64(val) ((void __user *)(uintptr_t)(val))
Christoph Hellwig82ed4db2017-01-27 09:46:29 +010057
Christoph Hellwig17cb9602018-03-13 17:28:41 +010058static int bsg_scsi_check_proto(struct sg_io_v4 *hdr)
59{
60 if (hdr->protocol != BSG_PROTOCOL_SCSI ||
61 hdr->subprotocol != BSG_SUB_PROTOCOL_SCSI_CMD)
62 return -EINVAL;
63 return 0;
64}
65
66static int bsg_scsi_fill_hdr(struct request *rq, struct sg_io_v4 *hdr,
67 fmode_t mode)
68{
69 struct scsi_request *sreq = scsi_req(rq);
70
Christoph Hellwig972248e2019-01-29 09:32:03 +010071 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
Christoph Hellwig17cb9602018-03-13 17:28:41 +010076 sreq->cmd_len = hdr->request_len;
77 if (sreq->cmd_len > BLK_MAX_CDB) {
78 sreq->cmd = kzalloc(sreq->cmd_len, GFP_KERNEL);
79 if (!sreq->cmd)
FUJITA Tomonori9f5de6b2008-04-30 13:16:21 +090080 return -ENOMEM;
81 }
FUJITA Tomonori70e36ec2006-12-20 11:20:15 +010082
Christoph Hellwig17cb9602018-03-13 17:28:41 +010083 if (copy_from_user(sreq->cmd, uptr64(hdr->request), sreq->cmd_len))
FUJITA Tomonori70e36ec2006-12-20 11:20:15 +010084 return -EFAULT;
Christoph Hellwig17cb9602018-03-13 17:28:41 +010085 if (blk_verify_command(sreq->cmd, mode))
FUJITA Tomonori70e36ec2006-12-20 11:20:15 +010086 return -EPERM;
Christoph Hellwig17cb9602018-03-13 17:28:41 +010087 return 0;
88}
89
90static int bsg_scsi_complete_rq(struct request *rq, struct sg_io_v4 *hdr)
91{
92 struct scsi_request *sreq = scsi_req(rq);
93 int ret = 0;
Jens Axboe3d6392c2007-07-09 12:38:05 +020094
95 /*
Christoph Hellwig17cb9602018-03-13 17:28:41 +010096 * fill in all the output members
Jens Axboe3d6392c2007-07-09 12:38:05 +020097 */
Christoph Hellwig17cb9602018-03-13 17:28:41 +010098 hdr->device_status = sreq->result & 0xff;
99 hdr->transport_status = host_byte(sreq->result);
100 hdr->driver_status = driver_byte(sreq->result);
101 hdr->info = 0;
102 if (hdr->device_status || hdr->transport_status || hdr->driver_status)
103 hdr->info |= SG_INFO_CHECK;
104 hdr->response_len = 0;
105
106 if (sreq->sense_len && hdr->response) {
107 int len = min_t(unsigned int, hdr->max_response_len,
108 sreq->sense_len);
109
110 if (copy_to_user(uptr64(hdr->response), sreq->sense, len))
111 ret = -EFAULT;
112 else
113 hdr->response_len = len;
114 }
115
Christoph Hellwig972248e2019-01-29 09:32:03 +0100116 if (rq_data_dir(rq) == READ)
Christoph Hellwig17cb9602018-03-13 17:28:41 +0100117 hdr->din_resid = sreq->resid_len;
Christoph Hellwig972248e2019-01-29 09:32:03 +0100118 else
Christoph Hellwig17cb9602018-03-13 17:28:41 +0100119 hdr->dout_resid = sreq->resid_len;
Christoph Hellwig17cb9602018-03-13 17:28:41 +0100120
121 return ret;
122}
123
124static void bsg_scsi_free_rq(struct request *rq)
125{
126 scsi_req_free_cmd(scsi_req(rq));
127}
128
129static const struct bsg_ops bsg_scsi_ops = {
130 .check_proto = bsg_scsi_check_proto,
131 .fill_hdr = bsg_scsi_fill_hdr,
132 .complete_rq = bsg_scsi_complete_rq,
133 .free_rq = bsg_scsi_free_rq,
134};
135
Christoph Hellwigccf32092018-11-09 20:12:25 +0100136static int bsg_sg_io(struct request_queue *q, fmode_t mode, void __user *uarg)
Christoph Hellwig17cb9602018-03-13 17:28:41 +0100137{
Christoph Hellwig972248e2019-01-29 09:32:03 +0100138 struct request *rq;
139 struct bio *bio;
Christoph Hellwigccf32092018-11-09 20:12:25 +0100140 struct sg_io_v4 hdr;
Christoph Hellwig17cb9602018-03-13 17:28:41 +0100141 int ret;
142
Christoph Hellwigccf32092018-11-09 20:12:25 +0100143 if (copy_from_user(&hdr, uarg, sizeof(hdr)))
144 return -EFAULT;
145
Christoph Hellwig17cb9602018-03-13 17:28:41 +0100146 if (!q->bsg_dev.class_dev)
Christoph Hellwigccf32092018-11-09 20:12:25 +0100147 return -ENXIO;
Christoph Hellwig17cb9602018-03-13 17:28:41 +0100148
Christoph Hellwigccf32092018-11-09 20:12:25 +0100149 if (hdr.guard != 'Q')
150 return -EINVAL;
151 ret = q->bsg_dev.ops->check_proto(&hdr);
Christoph Hellwig17cb9602018-03-13 17:28:41 +0100152 if (ret)
Christoph Hellwigccf32092018-11-09 20:12:25 +0100153 return ret;
Christoph Hellwig17cb9602018-03-13 17:28:41 +0100154
Christoph Hellwigccf32092018-11-09 20:12:25 +0100155 rq = blk_get_request(q, hdr.dout_xfer_len ?
Christoph Hellwigff005a02018-05-09 09:54:05 +0200156 REQ_OP_SCSI_OUT : REQ_OP_SCSI_IN, 0);
Christoph Hellwig17cb9602018-03-13 17:28:41 +0100157 if (IS_ERR(rq))
Christoph Hellwigccf32092018-11-09 20:12:25 +0100158 return PTR_ERR(rq);
Christoph Hellwig17cb9602018-03-13 17:28:41 +0100159
Christoph Hellwigccf32092018-11-09 20:12:25 +0100160 ret = q->bsg_dev.ops->fill_hdr(rq, &hdr, mode);
Pan Bian1c7b7d42021-01-19 04:33:11 -0800161 if (ret) {
162 blk_put_request(rq);
Christoph Hellwig972248e2019-01-29 09:32:03 +0100163 return ret;
Pan Bian1c7b7d42021-01-19 04:33:11 -0800164 }
Jens Axboe3d6392c2007-07-09 12:38:05 +0200165
Christoph Hellwigccf32092018-11-09 20:12:25 +0100166 rq->timeout = msecs_to_jiffies(hdr.timeout);
FUJITA Tomonori70e36ec2006-12-20 11:20:15 +0100167 if (!rq->timeout)
168 rq->timeout = q->sg_timeout;
169 if (!rq->timeout)
170 rq->timeout = BLK_DEFAULT_SG_TIMEOUT;
Linus Torvaldsf2f1fa72008-12-05 14:49:18 -0800171 if (rq->timeout < BLK_MIN_SG_TIMEOUT)
172 rq->timeout = BLK_MIN_SG_TIMEOUT;
Jens Axboe3d6392c2007-07-09 12:38:05 +0200173
Christoph Hellwigccf32092018-11-09 20:12:25 +0100174 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);
Jens Axboe3d6392c2007-07-09 12:38:05 +0200180 }
Boaz Harroshc1c20122009-02-03 07:47:29 +0100181
Christoph Hellwig17cb9602018-03-13 17:28:41 +0100182 if (ret)
Christoph Hellwig972248e2019-01-29 09:32:03 +0100183 goto out_free_rq;
Christoph Hellwigccf32092018-11-09 20:12:25 +0100184
185 bio = rq->bio;
Christoph Hellwigccf32092018-11-09 20:12:25 +0100186
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);
Christoph Hellwigccf32092018-11-09 20:12:25 +0100189 blk_rq_unmap_user(bio);
Christoph Hellwig972248e2019-01-29 09:32:03 +0100190
191out_free_rq:
Christoph Hellwigccf32092018-11-09 20:12:25 +0100192 rq->q->bsg_dev.ops->free_rq(rq);
193 blk_put_request(rq);
Christoph Hellwig972248e2019-01-29 09:32:03 +0100194 if (!ret && copy_to_user(uarg, &hdr, sizeof(hdr)))
Christoph Hellwigccf32092018-11-09 20:12:25 +0100195 return -EFAULT;
196 return ret;
FUJITA Tomonori70e36ec2006-12-20 11:20:15 +0100197}
198
Jens Axboe3d6392c2007-07-09 12:38:05 +0200199static struct bsg_device *bsg_alloc_device(void)
200{
Jens Axboe3d6392c2007-07-09 12:38:05 +0200201 struct bsg_device *bd;
Jens Axboe3d6392c2007-07-09 12:38:05 +0200202
203 bd = kzalloc(sizeof(struct bsg_device), GFP_KERNEL);
204 if (unlikely(!bd))
205 return NULL;
206
207 spin_lock_init(&bd->lock);
Jens Axboe5309cb32007-01-23 16:24:41 +0100208 bd->max_queue = BSG_DEFAULT_CMDS;
Jens Axboe3d6392c2007-07-09 12:38:05 +0200209 INIT_HLIST_NODE(&bd->dev_list);
Jens Axboe3d6392c2007-07-09 12:38:05 +0200210 return bd;
Jens Axboe3d6392c2007-07-09 12:38:05 +0200211}
212
213static int bsg_put_device(struct bsg_device *bd)
214{
FUJITA Tomonori97f46ae2008-04-19 00:43:14 +0900215 struct request_queue *q = bd->queue;
Jens Axboe3d6392c2007-07-09 12:38:05 +0200216
217 mutex_lock(&bsg_mutex);
218
John Pittmandb1939542018-08-27 14:33:05 -0400219 if (!refcount_dec_and_test(&bd->ref_count)) {
FUJITA Tomonori3f27e3e2008-05-29 07:56:55 +0900220 mutex_unlock(&bsg_mutex);
Christoph Hellwig28519c82018-07-12 10:09:59 +0200221 return 0;
FUJITA Tomonori3f27e3e2008-05-29 07:56:55 +0900222 }
223
224 hlist_del(&bd->dev_list);
225 mutex_unlock(&bsg_mutex);
Jens Axboe3d6392c2007-07-09 12:38:05 +0200226
Johannes Thumshirn3124b652018-01-24 09:50:06 -0700227 bsg_dbg(bd, "tearing down\n");
Jens Axboe3d6392c2007-07-09 12:38:05 +0200228
229 /*
230 * close can always block
231 */
Jens Axboe5309cb32007-01-23 16:24:41 +0100232 kfree(bd);
Christoph Hellwig28519c82018-07-12 10:09:59 +0200233 blk_put_queue(q);
234 return 0;
Jens Axboe3d6392c2007-07-09 12:38:05 +0200235}
236
237static struct bsg_device *bsg_add_device(struct inode *inode,
FUJITA Tomonorid351af02007-07-09 12:40:35 +0200238 struct request_queue *rq,
Jens Axboe3d6392c2007-07-09 12:38:05 +0200239 struct file *file)
240{
Jens Axboe25fd1642007-07-17 08:52:29 +0200241 struct bsg_device *bd;
Jens Axboe3d6392c2007-07-09 12:38:05 +0200242 unsigned char buf[32];
Bart Van Assched9f97262017-05-31 14:43:47 -0700243
Anatoliy Glagolevd6c73962018-06-13 15:38:51 -0600244 lockdep_assert_held(&bsg_mutex);
245
Tejun Heo09ac46c2011-12-14 00:33:38 +0100246 if (!blk_get_queue(rq))
FUJITA Tomonoric3ff1b92008-03-31 10:03:39 +0900247 return ERR_PTR(-ENXIO);
Jens Axboe3d6392c2007-07-09 12:38:05 +0200248
249 bd = bsg_alloc_device();
FUJITA Tomonoric3ff1b92008-03-31 10:03:39 +0900250 if (!bd) {
251 blk_put_queue(rq);
Jens Axboe3d6392c2007-07-09 12:38:05 +0200252 return ERR_PTR(-ENOMEM);
FUJITA Tomonoric3ff1b92008-03-31 10:03:39 +0900253 }
Jens Axboe3d6392c2007-07-09 12:38:05 +0200254
FUJITA Tomonorid351af02007-07-09 12:40:35 +0200255 bd->queue = rq;
Adel Gadllah0b07de82008-06-26 13:48:27 +0200256
John Pittmandb1939542018-08-27 14:33:05 -0400257 refcount_set(&bd->ref_count, 1);
FUJITA Tomonori842ea772008-03-31 10:03:41 +0900258 hlist_add_head(&bd->dev_list, bsg_dev_idx_hash(iminor(inode)));
Jens Axboe3d6392c2007-07-09 12:38:05 +0200259
Kay Sievers3ada8b72009-01-06 10:44:43 -0800260 strncpy(bd->name, dev_name(rq->bsg_dev.class_dev), sizeof(bd->name) - 1);
Johannes Thumshirn3124b652018-01-24 09:50:06 -0700261 bsg_dbg(bd, "bound to <%s>, max queue %d\n",
FUJITA Tomonori9e69fbb2006-12-20 11:18:22 +0100262 format_dev_t(buf, inode->i_rdev), bd->max_queue);
Jens Axboe3d6392c2007-07-09 12:38:05 +0200263
Jens Axboe3d6392c2007-07-09 12:38:05 +0200264 return bd;
265}
266
FUJITA Tomonori842ea772008-03-31 10:03:41 +0900267static struct bsg_device *__bsg_get_device(int minor, struct request_queue *q)
Jens Axboe3d6392c2007-07-09 12:38:05 +0200268{
FUJITA Tomonori43ac9e62008-03-31 10:03:40 +0900269 struct bsg_device *bd;
Jens Axboe3d6392c2007-07-09 12:38:05 +0200270
Anatoliy Glagolevd6c73962018-06-13 15:38:51 -0600271 lockdep_assert_held(&bsg_mutex);
Jens Axboe3d6392c2007-07-09 12:38:05 +0200272
Sasha Levinb67bfe02013-02-27 17:06:00 -0800273 hlist_for_each_entry(bd, bsg_dev_idx_hash(minor), dev_list) {
FUJITA Tomonori842ea772008-03-31 10:03:41 +0900274 if (bd->queue == q) {
John Pittmandb1939542018-08-27 14:33:05 -0400275 refcount_inc(&bd->ref_count);
FUJITA Tomonori43ac9e62008-03-31 10:03:40 +0900276 goto found;
Jens Axboe3d6392c2007-07-09 12:38:05 +0200277 }
Jens Axboe3d6392c2007-07-09 12:38:05 +0200278 }
FUJITA Tomonori43ac9e62008-03-31 10:03:40 +0900279 bd = NULL;
280found:
Jens Axboe3d6392c2007-07-09 12:38:05 +0200281 return bd;
282}
283
284static struct bsg_device *bsg_get_device(struct inode *inode, struct file *file)
285{
FUJITA Tomonori598443a2007-07-23 09:33:26 +0900286 struct bsg_device *bd;
287 struct bsg_class_device *bcd;
Jens Axboe3d6392c2007-07-09 12:38:05 +0200288
Jens Axboe3d6392c2007-07-09 12:38:05 +0200289 /*
290 * find the class device
291 */
Jens Axboe3d6392c2007-07-09 12:38:05 +0200292 mutex_lock(&bsg_mutex);
FUJITA Tomonori598443a2007-07-23 09:33:26 +0900293 bcd = idr_find(&bsg_minor_idr, iminor(inode));
Jens Axboe3d6392c2007-07-09 12:38:05 +0200294
Anatoliy Glagolevd6c73962018-06-13 15:38:51 -0600295 if (!bcd) {
296 bd = ERR_PTR(-ENODEV);
297 goto out_unlock;
298 }
Jens Axboe3d6392c2007-07-09 12:38:05 +0200299
FUJITA Tomonori842ea772008-03-31 10:03:41 +0900300 bd = __bsg_get_device(iminor(inode), bcd->queue);
Anatoliy Glagolevd6c73962018-06-13 15:38:51 -0600301 if (!bd)
302 bd = bsg_add_device(inode, bcd->queue, file);
FUJITA Tomonorid45ac4f2008-03-31 10:03:38 +0900303
Anatoliy Glagolevd6c73962018-06-13 15:38:51 -0600304out_unlock:
305 mutex_unlock(&bsg_mutex);
FUJITA Tomonorid45ac4f2008-03-31 10:03:38 +0900306 return bd;
Jens Axboe3d6392c2007-07-09 12:38:05 +0200307}
308
309static int bsg_open(struct inode *inode, struct file *file)
310{
Jonathan Corbet75bd2ef2008-05-15 09:09:23 -0600311 struct bsg_device *bd;
Asutosh Dase48a2622021-01-26 19:30:44 -0800312 struct bsg_class_device *bcd;
Jonathan Corbet75bd2ef2008-05-15 09:09:23 -0600313
Jonathan Corbet75bd2ef2008-05-15 09:09:23 -0600314 bd = bsg_get_device(inode, file);
Jens Axboe3d6392c2007-07-09 12:38:05 +0200315
316 if (IS_ERR(bd))
317 return PTR_ERR(bd);
318
Asutosh Dase48a2622021-01-26 19:30:44 -0800319 bcd = &bd->queue->bsg_dev;
320 pm_runtime_get_sync(bcd->class_dev->parent);
Jens Axboe3d6392c2007-07-09 12:38:05 +0200321 file->private_data = bd;
322 return 0;
323}
324
325static int bsg_release(struct inode *inode, struct file *file)
326{
327 struct bsg_device *bd = file->private_data;
Asutosh Dase48a2622021-01-26 19:30:44 -0800328 struct bsg_class_device *bcd;
Jens Axboe3d6392c2007-07-09 12:38:05 +0200329
330 file->private_data = NULL;
Asutosh Dase48a2622021-01-26 19:30:44 -0800331
332 bcd = &bd->queue->bsg_dev;
333 pm_runtime_put_sync(bcd->class_dev->parent);
Jens Axboe3d6392c2007-07-09 12:38:05 +0200334 return bsg_put_device(bd);
335}
336
Christoph Hellwigccf32092018-11-09 20:12:25 +0100337static int bsg_get_command_q(struct bsg_device *bd, int __user *uarg)
338{
339 return put_user(bd->max_queue, uarg);
340}
341
342static 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;
355}
356
Jens Axboe25fd1642007-07-17 08:52:29 +0200357static long bsg_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
Jens Axboe3d6392c2007-07-09 12:38:05 +0200358{
359 struct bsg_device *bd = file->private_data;
Christoph Hellwigccf32092018-11-09 20:12:25 +0100360 void __user *uarg = (void __user *) arg;
Jens Axboe3d6392c2007-07-09 12:38:05 +0200361
Jens Axboe3d6392c2007-07-09 12:38:05 +0200362 switch (cmd) {
Christoph Hellwigccf32092018-11-09 20:12:25 +0100363 /*
364 * Our own ioctls
365 */
Jens Axboe3d6392c2007-07-09 12:38:05 +0200366 case SG_GET_COMMAND_Q:
Christoph Hellwigccf32092018-11-09 20:12:25 +0100367 return bsg_get_command_q(bd, uarg);
368 case SG_SET_COMMAND_Q:
369 return bsg_set_command_q(bd, uarg);
Jens Axboe3d6392c2007-07-09 12:38:05 +0200370
371 /*
372 * SCSI/sg ioctls
373 */
374 case SG_GET_VERSION_NUM:
375 case SCSI_IOCTL_GET_IDLUN:
376 case SCSI_IOCTL_GET_BUS_NUMBER:
377 case SG_SET_TIMEOUT:
378 case SG_GET_TIMEOUT:
379 case SG_GET_RESERVED_SIZE:
380 case SG_SET_RESERVED_SIZE:
381 case SG_EMULATED_HOST:
Al Viro74f3c8a2007-08-27 15:38:10 -0400382 return scsi_cmd_ioctl(bd->queue, NULL, file->f_mode, cmd, uarg);
Christoph Hellwigccf32092018-11-09 20:12:25 +0100383 case SG_IO:
384 return bsg_sg_io(bd->queue, file->f_mode, uarg);
Christoph Hellwigf1eccc42021-07-24 09:20:10 +0200385 case SCSI_IOCTL_SEND_COMMAND:
386 pr_warn_ratelimited("%s: calling unsupported SCSI_IOCTL_SEND_COMMAND\n",
387 current->comm);
388 return -EINVAL;
Jens Axboe3d6392c2007-07-09 12:38:05 +0200389 default:
Jens Axboe3d6392c2007-07-09 12:38:05 +0200390 return -ENOTTY;
Jens Axboe3d6392c2007-07-09 12:38:05 +0200391 }
392}
393
Arjan van de Ven7344be02007-10-15 11:01:53 +0200394static const struct file_operations bsg_fops = {
Jens Axboe3d6392c2007-07-09 12:38:05 +0200395 .open = bsg_open,
396 .release = bsg_release,
Jens Axboe25fd1642007-07-17 08:52:29 +0200397 .unlocked_ioctl = bsg_ioctl,
Arnd Bergmannfe0da4e2019-03-15 17:13:06 +0100398 .compat_ioctl = compat_ptr_ioctl,
Jens Axboe3d6392c2007-07-09 12:38:05 +0200399 .owner = THIS_MODULE,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200400 .llseek = default_llseek,
Jens Axboe3d6392c2007-07-09 12:38:05 +0200401};
402
FUJITA Tomonorid351af02007-07-09 12:40:35 +0200403void bsg_unregister_queue(struct request_queue *q)
Jens Axboe3d6392c2007-07-09 12:38:05 +0200404{
FUJITA Tomonorid351af02007-07-09 12:40:35 +0200405 struct bsg_class_device *bcd = &q->bsg_dev;
Jens Axboe3d6392c2007-07-09 12:38:05 +0200406
FUJITA Tomonoridf468822007-07-21 13:23:25 +0900407 if (!bcd->class_dev)
408 return;
Jens Axboe3d6392c2007-07-09 12:38:05 +0200409
410 mutex_lock(&bsg_mutex);
FUJITA Tomonori598443a2007-07-23 09:33:26 +0900411 idr_remove(&bsg_minor_idr, bcd->minor);
Stanislaw Gruszka37b40ad2012-02-08 20:02:03 +0100412 if (q->kobj.sd)
413 sysfs_remove_link(&q->kobj, "bsg");
Tony Jonesee959b02008-02-22 00:13:36 +0100414 device_unregister(bcd->class_dev);
Jens Axboe3d6392c2007-07-09 12:38:05 +0200415 bcd->class_dev = NULL;
Jens Axboe3d6392c2007-07-09 12:38:05 +0200416 mutex_unlock(&bsg_mutex);
417}
FUJITA Tomonori4cf07232007-03-30 11:19:39 +0200418EXPORT_SYMBOL_GPL(bsg_unregister_queue);
Jens Axboe3d6392c2007-07-09 12:38:05 +0200419
FUJITA Tomonori97f46ae2008-04-19 00:43:14 +0900420int bsg_register_queue(struct request_queue *q, struct device *parent,
Christoph Hellwig5de815a2018-05-29 08:40:23 +0200421 const char *name, const struct bsg_ops *ops)
Jens Axboe3d6392c2007-07-09 12:38:05 +0200422{
FUJITA Tomonori598443a2007-07-23 09:33:26 +0900423 struct bsg_class_device *bcd;
Jens Axboe3d6392c2007-07-09 12:38:05 +0200424 dev_t dev;
Tejun Heobab998d2013-02-27 17:03:57 -0800425 int ret;
Tony Jonesee959b02008-02-22 00:13:36 +0100426 struct device *class_dev = NULL;
Jens Axboe3d6392c2007-07-09 12:38:05 +0200427
428 /*
429 * we need a proper transport to send commands, not a stacked device
430 */
Jens Axboe344e9ff2018-11-15 12:22:51 -0700431 if (!queue_is_mq(q))
Jens Axboe3d6392c2007-07-09 12:38:05 +0200432 return 0;
433
FUJITA Tomonorid351af02007-07-09 12:40:35 +0200434 bcd = &q->bsg_dev;
Jens Axboe3d6392c2007-07-09 12:38:05 +0200435 memset(bcd, 0, sizeof(*bcd));
Jens Axboe3d6392c2007-07-09 12:38:05 +0200436
437 mutex_lock(&bsg_mutex);
FUJITA Tomonori598443a2007-07-23 09:33:26 +0900438
Tejun Heobab998d2013-02-27 17:03:57 -0800439 ret = idr_alloc(&bsg_minor_idr, bcd, 0, BSG_MAX_DEVS, GFP_KERNEL);
440 if (ret < 0) {
441 if (ret == -ENOSPC) {
442 printk(KERN_ERR "bsg: too many bsg devices\n");
443 ret = -EINVAL;
444 }
FUJITA Tomonori598443a2007-07-23 09:33:26 +0900445 goto unlock;
446 }
447
Tejun Heobab998d2013-02-27 17:03:57 -0800448 bcd->minor = ret;
FUJITA Tomonorid351af02007-07-09 12:40:35 +0200449 bcd->queue = q;
Christoph Hellwig17cb9602018-03-13 17:28:41 +0100450 bcd->ops = ops;
Jens Axboe46f6ef42007-07-17 08:56:10 +0200451 dev = MKDEV(bsg_major, bcd->minor);
Christoph Hellwig5de815a2018-05-29 08:40:23 +0200452 class_dev = device_create(bsg_class, parent, dev, NULL, "%s", name);
FUJITA Tomonori4e2872d2007-03-28 13:29:24 +0200453 if (IS_ERR(class_dev)) {
454 ret = PTR_ERR(class_dev);
Christoph Hellwig5de815a2018-05-29 08:40:23 +0200455 goto idr_remove;
FUJITA Tomonori4e2872d2007-03-28 13:29:24 +0200456 }
457 bcd->class_dev = class_dev;
458
Linus Torvaldsabce8912007-07-16 11:18:23 -0700459 if (q->kobj.sd) {
FUJITA Tomonori4e2872d2007-03-28 13:29:24 +0200460 ret = sysfs_create_link(&q->kobj, &bcd->class_dev->kobj, "bsg");
461 if (ret)
FUJITA Tomonori598443a2007-07-23 09:33:26 +0900462 goto unregister_class_dev;
FUJITA Tomonori4e2872d2007-03-28 13:29:24 +0200463 }
464
Jens Axboe3d6392c2007-07-09 12:38:05 +0200465 mutex_unlock(&bsg_mutex);
466 return 0;
James Bottomley6826ee42007-07-20 16:50:10 -0500467
FUJITA Tomonori598443a2007-07-23 09:33:26 +0900468unregister_class_dev:
Tony Jonesee959b02008-02-22 00:13:36 +0100469 device_unregister(class_dev);
Christoph Hellwig5de815a2018-05-29 08:40:23 +0200470idr_remove:
Tejun Heobab998d2013-02-27 17:03:57 -0800471 idr_remove(&bsg_minor_idr, bcd->minor);
FUJITA Tomonori598443a2007-07-23 09:33:26 +0900472unlock:
Jens Axboe264a0472007-01-23 16:30:17 +0100473 mutex_unlock(&bsg_mutex);
FUJITA Tomonori4e2872d2007-03-28 13:29:24 +0200474 return ret;
Jens Axboe3d6392c2007-07-09 12:38:05 +0200475}
Christoph Hellwig17cb9602018-03-13 17:28:41 +0100476
477int bsg_scsi_register_queue(struct request_queue *q, struct device *parent)
478{
479 if (!blk_queue_scsi_passthrough(q)) {
480 WARN_ONCE(true, "Attempt to register a non-SCSI queue\n");
481 return -EINVAL;
482 }
483
Christoph Hellwig5de815a2018-05-29 08:40:23 +0200484 return bsg_register_queue(q, parent, dev_name(parent), &bsg_scsi_ops);
Christoph Hellwig17cb9602018-03-13 17:28:41 +0100485}
486EXPORT_SYMBOL_GPL(bsg_scsi_register_queue);
Jens Axboe3d6392c2007-07-09 12:38:05 +0200487
Greg Kroah-Hartman7e7654a2007-09-12 15:06:57 -0700488static struct cdev bsg_cdev;
FUJITA Tomonori292b7f22007-03-28 13:29:58 +0200489
Al Viro2c9ede52011-07-23 20:24:48 -0400490static char *bsg_devnode(struct device *dev, umode_t *mode)
Kay Sievers2bdf9142009-04-30 15:23:42 +0200491{
492 return kasprintf(GFP_KERNEL, "bsg/%s", dev_name(dev));
493}
494
Jens Axboe3d6392c2007-07-09 12:38:05 +0200495static int __init bsg_init(void)
496{
497 int ret, i;
Jens Axboe46f6ef42007-07-17 08:56:10 +0200498 dev_t devid;
Jens Axboe3d6392c2007-07-09 12:38:05 +0200499
Jens Axboe25fd1642007-07-17 08:52:29 +0200500 for (i = 0; i < BSG_LIST_ARRAY_SIZE; i++)
Jens Axboe3d6392c2007-07-09 12:38:05 +0200501 INIT_HLIST_HEAD(&bsg_device_list[i]);
502
503 bsg_class = class_create(THIS_MODULE, "bsg");
Christoph Hellwig28519c82018-07-12 10:09:59 +0200504 if (IS_ERR(bsg_class))
505 return PTR_ERR(bsg_class);
Kay Sieverse454cea2009-09-18 23:01:12 +0200506 bsg_class->devnode = bsg_devnode;
Jens Axboe3d6392c2007-07-09 12:38:05 +0200507
Jens Axboe46f6ef42007-07-17 08:56:10 +0200508 ret = alloc_chrdev_region(&devid, 0, BSG_MAX_DEVS, "bsg");
FUJITA Tomonori9b9f7702007-07-17 12:20:46 +0200509 if (ret)
510 goto destroy_bsg_class;
Jens Axboe3d6392c2007-07-09 12:38:05 +0200511
Jens Axboe46f6ef42007-07-17 08:56:10 +0200512 bsg_major = MAJOR(devid);
513
FUJITA Tomonori292b7f22007-03-28 13:29:58 +0200514 cdev_init(&bsg_cdev, &bsg_fops);
Jens Axboe46f6ef42007-07-17 08:56:10 +0200515 ret = cdev_add(&bsg_cdev, MKDEV(bsg_major, 0), BSG_MAX_DEVS);
FUJITA Tomonori9b9f7702007-07-17 12:20:46 +0200516 if (ret)
517 goto unregister_chrdev;
FUJITA Tomonori292b7f22007-03-28 13:29:58 +0200518
Jens Axboe5d3a8cd2007-07-17 15:10:09 +0200519 printk(KERN_INFO BSG_DESCRIPTION " version " BSG_VERSION
FUJITA Tomonori0ed081c2007-07-17 12:21:35 +0200520 " loaded (major %d)\n", bsg_major);
Jens Axboe3d6392c2007-07-09 12:38:05 +0200521 return 0;
FUJITA Tomonori9b9f7702007-07-17 12:20:46 +0200522unregister_chrdev:
523 unregister_chrdev_region(MKDEV(bsg_major, 0), BSG_MAX_DEVS);
524destroy_bsg_class:
525 class_destroy(bsg_class);
FUJITA Tomonori9b9f7702007-07-17 12:20:46 +0200526 return ret;
Jens Axboe3d6392c2007-07-09 12:38:05 +0200527}
528
529MODULE_AUTHOR("Jens Axboe");
FUJITA Tomonori0ed081c2007-07-17 12:21:35 +0200530MODULE_DESCRIPTION(BSG_DESCRIPTION);
Jens Axboe3d6392c2007-07-09 12:38:05 +0200531MODULE_LICENSE("GPL");
532
FUJITA Tomonori4e2872d2007-03-28 13:29:24 +0200533device_initcall(bsg_init);