blob: 3d78e843a83f65469c0eeb19ebea9c28a06d2529 [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>
Jens Axboe3d6392c2007-07-09 12:38:05 +020015
16#include <scsi/scsi.h>
17#include <scsi/scsi_ioctl.h>
18#include <scsi/scsi_cmnd.h>
FUJITA Tomonori4e2872d2007-03-28 13:29:24 +020019#include <scsi/scsi_device.h>
20#include <scsi/scsi_driver.h>
Jens Axboe3d6392c2007-07-09 12:38:05 +020021#include <scsi/sg.h>
22
FUJITA Tomonori0ed081c2007-07-17 12:21:35 +020023#define BSG_DESCRIPTION "Block layer SCSI generic (bsg) driver"
24#define BSG_VERSION "0.4"
Jens Axboe3d6392c2007-07-09 12:38:05 +020025
Johannes Thumshirn3124b652018-01-24 09:50:06 -070026#define bsg_dbg(bd, fmt, ...) \
27 pr_debug("%s: " fmt, (bd)->name, ##__VA_ARGS__)
28
Jens Axboe3d6392c2007-07-09 12:38:05 +020029struct bsg_device {
Jens Axboe165125e2007-07-24 09:28:11 +020030 struct request_queue *queue;
Jens Axboe3d6392c2007-07-09 12:38:05 +020031 spinlock_t lock;
Jens Axboe3d6392c2007-07-09 12:38:05 +020032 struct hlist_node dev_list;
John Pittmandb1939542018-08-27 14:33:05 -040033 refcount_t ref_count;
Kay Sievers3ada8b72009-01-06 10:44:43 -080034 char name[20];
Jens Axboe3d6392c2007-07-09 12:38:05 +020035 int max_queue;
Jens Axboe3d6392c2007-07-09 12:38:05 +020036};
37
Jens Axboe5309cb32007-01-23 16:24:41 +010038#define BSG_DEFAULT_CMDS 64
FUJITA Tomonori292b7f22007-03-28 13:29:58 +020039#define BSG_MAX_DEVS 32768
Jens Axboe3d6392c2007-07-09 12:38:05 +020040
Jens Axboe3d6392c2007-07-09 12:38:05 +020041static DEFINE_MUTEX(bsg_mutex);
FUJITA Tomonori598443a2007-07-23 09:33:26 +090042static DEFINE_IDR(bsg_minor_idr);
Jens Axboe3d6392c2007-07-09 12:38:05 +020043
Jens Axboe25fd1642007-07-17 08:52:29 +020044#define BSG_LIST_ARRAY_SIZE 8
Jens Axboe25fd1642007-07-17 08:52:29 +020045static struct hlist_head bsg_device_list[BSG_LIST_ARRAY_SIZE];
Jens Axboe3d6392c2007-07-09 12:38:05 +020046
47static struct class *bsg_class;
Jens Axboe46f6ef42007-07-17 08:56:10 +020048static int bsg_major;
Jens Axboe3d6392c2007-07-09 12:38:05 +020049
FUJITA Tomonori1c1133e2007-07-17 12:21:15 +020050static inline struct hlist_head *bsg_dev_idx_hash(int index)
Jens Axboe3d6392c2007-07-09 12:38:05 +020051{
FUJITA Tomonori1c1133e2007-07-17 12:21:15 +020052 return &bsg_device_list[index & (BSG_LIST_ARRAY_SIZE - 1)];
Jens Axboe3d6392c2007-07-09 12:38:05 +020053}
54
Christoph Hellwig17cb9602018-03-13 17:28:41 +010055#define uptr64(val) ((void __user *)(uintptr_t)(val))
Christoph Hellwig82ed4db2017-01-27 09:46:29 +010056
Christoph Hellwig17cb9602018-03-13 17:28:41 +010057static int bsg_scsi_check_proto(struct sg_io_v4 *hdr)
58{
59 if (hdr->protocol != BSG_PROTOCOL_SCSI ||
60 hdr->subprotocol != BSG_SUB_PROTOCOL_SCSI_CMD)
61 return -EINVAL;
62 return 0;
63}
64
65static int bsg_scsi_fill_hdr(struct request *rq, struct sg_io_v4 *hdr,
66 fmode_t mode)
67{
68 struct scsi_request *sreq = scsi_req(rq);
69
Christoph Hellwig972248e2019-01-29 09:32:03 +010070 if (hdr->dout_xfer_len && hdr->din_xfer_len) {
71 pr_warn_once("BIDI support in bsg has been removed.\n");
72 return -EOPNOTSUPP;
73 }
74
Christoph Hellwig17cb9602018-03-13 17:28:41 +010075 sreq->cmd_len = hdr->request_len;
76 if (sreq->cmd_len > BLK_MAX_CDB) {
77 sreq->cmd = kzalloc(sreq->cmd_len, GFP_KERNEL);
78 if (!sreq->cmd)
FUJITA Tomonori9f5de6b2008-04-30 13:16:21 +090079 return -ENOMEM;
80 }
FUJITA Tomonori70e36ec2006-12-20 11:20:15 +010081
Christoph Hellwig17cb9602018-03-13 17:28:41 +010082 if (copy_from_user(sreq->cmd, uptr64(hdr->request), sreq->cmd_len))
FUJITA Tomonori70e36ec2006-12-20 11:20:15 +010083 return -EFAULT;
Christoph Hellwig17cb9602018-03-13 17:28:41 +010084 if (blk_verify_command(sreq->cmd, mode))
FUJITA Tomonori70e36ec2006-12-20 11:20:15 +010085 return -EPERM;
Christoph Hellwig17cb9602018-03-13 17:28:41 +010086 return 0;
87}
88
89static int bsg_scsi_complete_rq(struct request *rq, struct sg_io_v4 *hdr)
90{
91 struct scsi_request *sreq = scsi_req(rq);
92 int ret = 0;
Jens Axboe3d6392c2007-07-09 12:38:05 +020093
94 /*
Christoph Hellwig17cb9602018-03-13 17:28:41 +010095 * fill in all the output members
Jens Axboe3d6392c2007-07-09 12:38:05 +020096 */
Christoph Hellwig17cb9602018-03-13 17:28:41 +010097 hdr->device_status = sreq->result & 0xff;
98 hdr->transport_status = host_byte(sreq->result);
99 hdr->driver_status = driver_byte(sreq->result);
100 hdr->info = 0;
101 if (hdr->device_status || hdr->transport_status || hdr->driver_status)
102 hdr->info |= SG_INFO_CHECK;
103 hdr->response_len = 0;
104
105 if (sreq->sense_len && hdr->response) {
106 int len = min_t(unsigned int, hdr->max_response_len,
107 sreq->sense_len);
108
109 if (copy_to_user(uptr64(hdr->response), sreq->sense, len))
110 ret = -EFAULT;
111 else
112 hdr->response_len = len;
113 }
114
Christoph Hellwig972248e2019-01-29 09:32:03 +0100115 if (rq_data_dir(rq) == READ)
Christoph Hellwig17cb9602018-03-13 17:28:41 +0100116 hdr->din_resid = sreq->resid_len;
Christoph Hellwig972248e2019-01-29 09:32:03 +0100117 else
Christoph Hellwig17cb9602018-03-13 17:28:41 +0100118 hdr->dout_resid = sreq->resid_len;
Christoph Hellwig17cb9602018-03-13 17:28:41 +0100119
120 return ret;
121}
122
123static void bsg_scsi_free_rq(struct request *rq)
124{
125 scsi_req_free_cmd(scsi_req(rq));
126}
127
128static const struct bsg_ops bsg_scsi_ops = {
129 .check_proto = bsg_scsi_check_proto,
130 .fill_hdr = bsg_scsi_fill_hdr,
131 .complete_rq = bsg_scsi_complete_rq,
132 .free_rq = bsg_scsi_free_rq,
133};
134
Christoph Hellwigccf32092018-11-09 20:12:25 +0100135static int bsg_sg_io(struct request_queue *q, fmode_t mode, void __user *uarg)
Christoph Hellwig17cb9602018-03-13 17:28:41 +0100136{
Christoph Hellwig972248e2019-01-29 09:32:03 +0100137 struct request *rq;
138 struct bio *bio;
Christoph Hellwigccf32092018-11-09 20:12:25 +0100139 struct sg_io_v4 hdr;
Christoph Hellwig17cb9602018-03-13 17:28:41 +0100140 int ret;
141
Christoph Hellwigccf32092018-11-09 20:12:25 +0100142 if (copy_from_user(&hdr, uarg, sizeof(hdr)))
143 return -EFAULT;
144
Christoph Hellwig17cb9602018-03-13 17:28:41 +0100145 if (!q->bsg_dev.class_dev)
Christoph Hellwigccf32092018-11-09 20:12:25 +0100146 return -ENXIO;
Christoph Hellwig17cb9602018-03-13 17:28:41 +0100147
Christoph Hellwigccf32092018-11-09 20:12:25 +0100148 if (hdr.guard != 'Q')
149 return -EINVAL;
150 ret = q->bsg_dev.ops->check_proto(&hdr);
Christoph Hellwig17cb9602018-03-13 17:28:41 +0100151 if (ret)
Christoph Hellwigccf32092018-11-09 20:12:25 +0100152 return ret;
Christoph Hellwig17cb9602018-03-13 17:28:41 +0100153
Christoph Hellwigccf32092018-11-09 20:12:25 +0100154 rq = blk_get_request(q, hdr.dout_xfer_len ?
Christoph Hellwigff005a02018-05-09 09:54:05 +0200155 REQ_OP_SCSI_OUT : REQ_OP_SCSI_IN, 0);
Christoph Hellwig17cb9602018-03-13 17:28:41 +0100156 if (IS_ERR(rq))
Christoph Hellwigccf32092018-11-09 20:12:25 +0100157 return PTR_ERR(rq);
Christoph Hellwig17cb9602018-03-13 17:28:41 +0100158
Christoph Hellwigccf32092018-11-09 20:12:25 +0100159 ret = q->bsg_dev.ops->fill_hdr(rq, &hdr, mode);
Pan Bian1c7b7d42021-01-19 04:33:11 -0800160 if (ret) {
161 blk_put_request(rq);
Christoph Hellwig972248e2019-01-29 09:32:03 +0100162 return ret;
Pan Bian1c7b7d42021-01-19 04:33:11 -0800163 }
Jens Axboe3d6392c2007-07-09 12:38:05 +0200164
Christoph Hellwigccf32092018-11-09 20:12:25 +0100165 rq->timeout = msecs_to_jiffies(hdr.timeout);
FUJITA Tomonori70e36ec2006-12-20 11:20:15 +0100166 if (!rq->timeout)
167 rq->timeout = q->sg_timeout;
168 if (!rq->timeout)
169 rq->timeout = BLK_DEFAULT_SG_TIMEOUT;
Linus Torvaldsf2f1fa72008-12-05 14:49:18 -0800170 if (rq->timeout < BLK_MIN_SG_TIMEOUT)
171 rq->timeout = BLK_MIN_SG_TIMEOUT;
Jens Axboe3d6392c2007-07-09 12:38:05 +0200172
Christoph Hellwigccf32092018-11-09 20:12:25 +0100173 if (hdr.dout_xfer_len) {
174 ret = blk_rq_map_user(q, rq, NULL, uptr64(hdr.dout_xferp),
175 hdr.dout_xfer_len, GFP_KERNEL);
176 } else if (hdr.din_xfer_len) {
177 ret = blk_rq_map_user(q, rq, NULL, uptr64(hdr.din_xferp),
178 hdr.din_xfer_len, GFP_KERNEL);
Jens Axboe3d6392c2007-07-09 12:38:05 +0200179 }
Boaz Harroshc1c20122009-02-03 07:47:29 +0100180
Christoph Hellwig17cb9602018-03-13 17:28:41 +0100181 if (ret)
Christoph Hellwig972248e2019-01-29 09:32:03 +0100182 goto out_free_rq;
Christoph Hellwigccf32092018-11-09 20:12:25 +0100183
184 bio = rq->bio;
Christoph Hellwigccf32092018-11-09 20:12:25 +0100185
186 blk_execute_rq(q, NULL, rq, !(hdr.flags & BSG_FLAG_Q_AT_TAIL));
187 ret = rq->q->bsg_dev.ops->complete_rq(rq, &hdr);
Christoph Hellwigccf32092018-11-09 20:12:25 +0100188 blk_rq_unmap_user(bio);
Christoph Hellwig972248e2019-01-29 09:32:03 +0100189
190out_free_rq:
Christoph Hellwigccf32092018-11-09 20:12:25 +0100191 rq->q->bsg_dev.ops->free_rq(rq);
192 blk_put_request(rq);
Christoph Hellwig972248e2019-01-29 09:32:03 +0100193 if (!ret && copy_to_user(uarg, &hdr, sizeof(hdr)))
Christoph Hellwigccf32092018-11-09 20:12:25 +0100194 return -EFAULT;
195 return ret;
FUJITA Tomonori70e36ec2006-12-20 11:20:15 +0100196}
197
Jens Axboe3d6392c2007-07-09 12:38:05 +0200198static struct bsg_device *bsg_alloc_device(void)
199{
Jens Axboe3d6392c2007-07-09 12:38:05 +0200200 struct bsg_device *bd;
Jens Axboe3d6392c2007-07-09 12:38:05 +0200201
202 bd = kzalloc(sizeof(struct bsg_device), GFP_KERNEL);
203 if (unlikely(!bd))
204 return NULL;
205
206 spin_lock_init(&bd->lock);
Jens Axboe5309cb32007-01-23 16:24:41 +0100207 bd->max_queue = BSG_DEFAULT_CMDS;
Jens Axboe3d6392c2007-07-09 12:38:05 +0200208 INIT_HLIST_NODE(&bd->dev_list);
Jens Axboe3d6392c2007-07-09 12:38:05 +0200209 return bd;
Jens Axboe3d6392c2007-07-09 12:38:05 +0200210}
211
212static int bsg_put_device(struct bsg_device *bd)
213{
FUJITA Tomonori97f46ae2008-04-19 00:43:14 +0900214 struct request_queue *q = bd->queue;
Jens Axboe3d6392c2007-07-09 12:38:05 +0200215
216 mutex_lock(&bsg_mutex);
217
John Pittmandb1939542018-08-27 14:33:05 -0400218 if (!refcount_dec_and_test(&bd->ref_count)) {
FUJITA Tomonori3f27e3e2008-05-29 07:56:55 +0900219 mutex_unlock(&bsg_mutex);
Christoph Hellwig28519c82018-07-12 10:09:59 +0200220 return 0;
FUJITA Tomonori3f27e3e2008-05-29 07:56:55 +0900221 }
222
223 hlist_del(&bd->dev_list);
224 mutex_unlock(&bsg_mutex);
Jens Axboe3d6392c2007-07-09 12:38:05 +0200225
Johannes Thumshirn3124b652018-01-24 09:50:06 -0700226 bsg_dbg(bd, "tearing down\n");
Jens Axboe3d6392c2007-07-09 12:38:05 +0200227
228 /*
229 * close can always block
230 */
Jens Axboe5309cb32007-01-23 16:24:41 +0100231 kfree(bd);
Christoph Hellwig28519c82018-07-12 10:09:59 +0200232 blk_put_queue(q);
233 return 0;
Jens Axboe3d6392c2007-07-09 12:38:05 +0200234}
235
236static struct bsg_device *bsg_add_device(struct inode *inode,
FUJITA Tomonorid351af02007-07-09 12:40:35 +0200237 struct request_queue *rq,
Jens Axboe3d6392c2007-07-09 12:38:05 +0200238 struct file *file)
239{
Jens Axboe25fd1642007-07-17 08:52:29 +0200240 struct bsg_device *bd;
Jens Axboe3d6392c2007-07-09 12:38:05 +0200241 unsigned char buf[32];
Bart Van Assched9f97262017-05-31 14:43:47 -0700242
Anatoliy Glagolevd6c73962018-06-13 15:38:51 -0600243 lockdep_assert_held(&bsg_mutex);
244
Tejun Heo09ac46c2011-12-14 00:33:38 +0100245 if (!blk_get_queue(rq))
FUJITA Tomonoric3ff1b92008-03-31 10:03:39 +0900246 return ERR_PTR(-ENXIO);
Jens Axboe3d6392c2007-07-09 12:38:05 +0200247
248 bd = bsg_alloc_device();
FUJITA Tomonoric3ff1b92008-03-31 10:03:39 +0900249 if (!bd) {
250 blk_put_queue(rq);
Jens Axboe3d6392c2007-07-09 12:38:05 +0200251 return ERR_PTR(-ENOMEM);
FUJITA Tomonoric3ff1b92008-03-31 10:03:39 +0900252 }
Jens Axboe3d6392c2007-07-09 12:38:05 +0200253
FUJITA Tomonorid351af02007-07-09 12:40:35 +0200254 bd->queue = rq;
Adel Gadllah0b07de82008-06-26 13:48:27 +0200255
John Pittmandb1939542018-08-27 14:33:05 -0400256 refcount_set(&bd->ref_count, 1);
FUJITA Tomonori842ea772008-03-31 10:03:41 +0900257 hlist_add_head(&bd->dev_list, bsg_dev_idx_hash(iminor(inode)));
Jens Axboe3d6392c2007-07-09 12:38:05 +0200258
Kay Sievers3ada8b72009-01-06 10:44:43 -0800259 strncpy(bd->name, dev_name(rq->bsg_dev.class_dev), sizeof(bd->name) - 1);
Johannes Thumshirn3124b652018-01-24 09:50:06 -0700260 bsg_dbg(bd, "bound to <%s>, max queue %d\n",
FUJITA Tomonori9e69fbb2006-12-20 11:18:22 +0100261 format_dev_t(buf, inode->i_rdev), bd->max_queue);
Jens Axboe3d6392c2007-07-09 12:38:05 +0200262
Jens Axboe3d6392c2007-07-09 12:38:05 +0200263 return bd;
264}
265
FUJITA Tomonori842ea772008-03-31 10:03:41 +0900266static struct bsg_device *__bsg_get_device(int minor, struct request_queue *q)
Jens Axboe3d6392c2007-07-09 12:38:05 +0200267{
FUJITA Tomonori43ac9e62008-03-31 10:03:40 +0900268 struct bsg_device *bd;
Jens Axboe3d6392c2007-07-09 12:38:05 +0200269
Anatoliy Glagolevd6c73962018-06-13 15:38:51 -0600270 lockdep_assert_held(&bsg_mutex);
Jens Axboe3d6392c2007-07-09 12:38:05 +0200271
Sasha Levinb67bfe02013-02-27 17:06:00 -0800272 hlist_for_each_entry(bd, bsg_dev_idx_hash(minor), dev_list) {
FUJITA Tomonori842ea772008-03-31 10:03:41 +0900273 if (bd->queue == q) {
John Pittmandb1939542018-08-27 14:33:05 -0400274 refcount_inc(&bd->ref_count);
FUJITA Tomonori43ac9e62008-03-31 10:03:40 +0900275 goto found;
Jens Axboe3d6392c2007-07-09 12:38:05 +0200276 }
Jens Axboe3d6392c2007-07-09 12:38:05 +0200277 }
FUJITA Tomonori43ac9e62008-03-31 10:03:40 +0900278 bd = NULL;
279found:
Jens Axboe3d6392c2007-07-09 12:38:05 +0200280 return bd;
281}
282
283static struct bsg_device *bsg_get_device(struct inode *inode, struct file *file)
284{
FUJITA Tomonori598443a2007-07-23 09:33:26 +0900285 struct bsg_device *bd;
286 struct bsg_class_device *bcd;
Jens Axboe3d6392c2007-07-09 12:38:05 +0200287
Jens Axboe3d6392c2007-07-09 12:38:05 +0200288 /*
289 * find the class device
290 */
Jens Axboe3d6392c2007-07-09 12:38:05 +0200291 mutex_lock(&bsg_mutex);
FUJITA Tomonori598443a2007-07-23 09:33:26 +0900292 bcd = idr_find(&bsg_minor_idr, iminor(inode));
Jens Axboe3d6392c2007-07-09 12:38:05 +0200293
Anatoliy Glagolevd6c73962018-06-13 15:38:51 -0600294 if (!bcd) {
295 bd = ERR_PTR(-ENODEV);
296 goto out_unlock;
297 }
Jens Axboe3d6392c2007-07-09 12:38:05 +0200298
FUJITA Tomonori842ea772008-03-31 10:03:41 +0900299 bd = __bsg_get_device(iminor(inode), bcd->queue);
Anatoliy Glagolevd6c73962018-06-13 15:38:51 -0600300 if (!bd)
301 bd = bsg_add_device(inode, bcd->queue, file);
FUJITA Tomonorid45ac4f2008-03-31 10:03:38 +0900302
Anatoliy Glagolevd6c73962018-06-13 15:38:51 -0600303out_unlock:
304 mutex_unlock(&bsg_mutex);
FUJITA Tomonorid45ac4f2008-03-31 10:03:38 +0900305 return bd;
Jens Axboe3d6392c2007-07-09 12:38:05 +0200306}
307
308static int bsg_open(struct inode *inode, struct file *file)
309{
Jonathan Corbet75bd2ef2008-05-15 09:09:23 -0600310 struct bsg_device *bd;
311
Jonathan Corbet75bd2ef2008-05-15 09:09:23 -0600312 bd = bsg_get_device(inode, file);
Jens Axboe3d6392c2007-07-09 12:38:05 +0200313
314 if (IS_ERR(bd))
315 return PTR_ERR(bd);
316
317 file->private_data = bd;
318 return 0;
319}
320
321static int bsg_release(struct inode *inode, struct file *file)
322{
323 struct bsg_device *bd = file->private_data;
324
325 file->private_data = NULL;
326 return bsg_put_device(bd);
327}
328
Christoph Hellwigccf32092018-11-09 20:12:25 +0100329static int bsg_get_command_q(struct bsg_device *bd, int __user *uarg)
330{
331 return put_user(bd->max_queue, uarg);
332}
333
334static int bsg_set_command_q(struct bsg_device *bd, int __user *uarg)
335{
336 int queue;
337
338 if (get_user(queue, uarg))
339 return -EFAULT;
340 if (queue < 1)
341 return -EINVAL;
342
343 spin_lock_irq(&bd->lock);
344 bd->max_queue = queue;
345 spin_unlock_irq(&bd->lock);
346 return 0;
347}
348
Jens Axboe25fd1642007-07-17 08:52:29 +0200349static long bsg_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
Jens Axboe3d6392c2007-07-09 12:38:05 +0200350{
351 struct bsg_device *bd = file->private_data;
Christoph Hellwigccf32092018-11-09 20:12:25 +0100352 void __user *uarg = (void __user *) arg;
Jens Axboe3d6392c2007-07-09 12:38:05 +0200353
Jens Axboe3d6392c2007-07-09 12:38:05 +0200354 switch (cmd) {
Christoph Hellwigccf32092018-11-09 20:12:25 +0100355 /*
356 * Our own ioctls
357 */
Jens Axboe3d6392c2007-07-09 12:38:05 +0200358 case SG_GET_COMMAND_Q:
Christoph Hellwigccf32092018-11-09 20:12:25 +0100359 return bsg_get_command_q(bd, uarg);
360 case SG_SET_COMMAND_Q:
361 return bsg_set_command_q(bd, uarg);
Jens Axboe3d6392c2007-07-09 12:38:05 +0200362
363 /*
364 * SCSI/sg ioctls
365 */
366 case SG_GET_VERSION_NUM:
367 case SCSI_IOCTL_GET_IDLUN:
368 case SCSI_IOCTL_GET_BUS_NUMBER:
369 case SG_SET_TIMEOUT:
370 case SG_GET_TIMEOUT:
371 case SG_GET_RESERVED_SIZE:
372 case SG_SET_RESERVED_SIZE:
373 case SG_EMULATED_HOST:
Christoph Hellwigccf32092018-11-09 20:12:25 +0100374 case SCSI_IOCTL_SEND_COMMAND:
Al Viro74f3c8a2007-08-27 15:38:10 -0400375 return scsi_cmd_ioctl(bd->queue, NULL, file->f_mode, cmd, uarg);
Christoph Hellwigccf32092018-11-09 20:12:25 +0100376 case SG_IO:
377 return bsg_sg_io(bd->queue, file->f_mode, uarg);
Jens Axboe3d6392c2007-07-09 12:38:05 +0200378 default:
Jens Axboe3d6392c2007-07-09 12:38:05 +0200379 return -ENOTTY;
Jens Axboe3d6392c2007-07-09 12:38:05 +0200380 }
381}
382
Arjan van de Ven7344be02007-10-15 11:01:53 +0200383static const struct file_operations bsg_fops = {
Jens Axboe3d6392c2007-07-09 12:38:05 +0200384 .open = bsg_open,
385 .release = bsg_release,
Jens Axboe25fd1642007-07-17 08:52:29 +0200386 .unlocked_ioctl = bsg_ioctl,
Arnd Bergmannfe0da4e2019-03-15 17:13:06 +0100387 .compat_ioctl = compat_ptr_ioctl,
Jens Axboe3d6392c2007-07-09 12:38:05 +0200388 .owner = THIS_MODULE,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200389 .llseek = default_llseek,
Jens Axboe3d6392c2007-07-09 12:38:05 +0200390};
391
FUJITA Tomonorid351af02007-07-09 12:40:35 +0200392void bsg_unregister_queue(struct request_queue *q)
Jens Axboe3d6392c2007-07-09 12:38:05 +0200393{
FUJITA Tomonorid351af02007-07-09 12:40:35 +0200394 struct bsg_class_device *bcd = &q->bsg_dev;
Jens Axboe3d6392c2007-07-09 12:38:05 +0200395
FUJITA Tomonoridf468822007-07-21 13:23:25 +0900396 if (!bcd->class_dev)
397 return;
Jens Axboe3d6392c2007-07-09 12:38:05 +0200398
399 mutex_lock(&bsg_mutex);
FUJITA Tomonori598443a2007-07-23 09:33:26 +0900400 idr_remove(&bsg_minor_idr, bcd->minor);
Stanislaw Gruszka37b40ad2012-02-08 20:02:03 +0100401 if (q->kobj.sd)
402 sysfs_remove_link(&q->kobj, "bsg");
Tony Jonesee959b02008-02-22 00:13:36 +0100403 device_unregister(bcd->class_dev);
Jens Axboe3d6392c2007-07-09 12:38:05 +0200404 bcd->class_dev = NULL;
Jens Axboe3d6392c2007-07-09 12:38:05 +0200405 mutex_unlock(&bsg_mutex);
406}
FUJITA Tomonori4cf07232007-03-30 11:19:39 +0200407EXPORT_SYMBOL_GPL(bsg_unregister_queue);
Jens Axboe3d6392c2007-07-09 12:38:05 +0200408
FUJITA Tomonori97f46ae2008-04-19 00:43:14 +0900409int bsg_register_queue(struct request_queue *q, struct device *parent,
Christoph Hellwig5de815a2018-05-29 08:40:23 +0200410 const char *name, const struct bsg_ops *ops)
Jens Axboe3d6392c2007-07-09 12:38:05 +0200411{
FUJITA Tomonori598443a2007-07-23 09:33:26 +0900412 struct bsg_class_device *bcd;
Jens Axboe3d6392c2007-07-09 12:38:05 +0200413 dev_t dev;
Tejun Heobab998d2013-02-27 17:03:57 -0800414 int ret;
Tony Jonesee959b02008-02-22 00:13:36 +0100415 struct device *class_dev = NULL;
Jens Axboe3d6392c2007-07-09 12:38:05 +0200416
417 /*
418 * we need a proper transport to send commands, not a stacked device
419 */
Jens Axboe344e9ff2018-11-15 12:22:51 -0700420 if (!queue_is_mq(q))
Jens Axboe3d6392c2007-07-09 12:38:05 +0200421 return 0;
422
FUJITA Tomonorid351af02007-07-09 12:40:35 +0200423 bcd = &q->bsg_dev;
Jens Axboe3d6392c2007-07-09 12:38:05 +0200424 memset(bcd, 0, sizeof(*bcd));
Jens Axboe3d6392c2007-07-09 12:38:05 +0200425
426 mutex_lock(&bsg_mutex);
FUJITA Tomonori598443a2007-07-23 09:33:26 +0900427
Tejun Heobab998d2013-02-27 17:03:57 -0800428 ret = idr_alloc(&bsg_minor_idr, bcd, 0, BSG_MAX_DEVS, GFP_KERNEL);
429 if (ret < 0) {
430 if (ret == -ENOSPC) {
431 printk(KERN_ERR "bsg: too many bsg devices\n");
432 ret = -EINVAL;
433 }
FUJITA Tomonori598443a2007-07-23 09:33:26 +0900434 goto unlock;
435 }
436
Tejun Heobab998d2013-02-27 17:03:57 -0800437 bcd->minor = ret;
FUJITA Tomonorid351af02007-07-09 12:40:35 +0200438 bcd->queue = q;
Christoph Hellwig17cb9602018-03-13 17:28:41 +0100439 bcd->ops = ops;
Jens Axboe46f6ef42007-07-17 08:56:10 +0200440 dev = MKDEV(bsg_major, bcd->minor);
Christoph Hellwig5de815a2018-05-29 08:40:23 +0200441 class_dev = device_create(bsg_class, parent, dev, NULL, "%s", name);
FUJITA Tomonori4e2872d2007-03-28 13:29:24 +0200442 if (IS_ERR(class_dev)) {
443 ret = PTR_ERR(class_dev);
Christoph Hellwig5de815a2018-05-29 08:40:23 +0200444 goto idr_remove;
FUJITA Tomonori4e2872d2007-03-28 13:29:24 +0200445 }
446 bcd->class_dev = class_dev;
447
Linus Torvaldsabce8912007-07-16 11:18:23 -0700448 if (q->kobj.sd) {
FUJITA Tomonori4e2872d2007-03-28 13:29:24 +0200449 ret = sysfs_create_link(&q->kobj, &bcd->class_dev->kobj, "bsg");
450 if (ret)
FUJITA Tomonori598443a2007-07-23 09:33:26 +0900451 goto unregister_class_dev;
FUJITA Tomonori4e2872d2007-03-28 13:29:24 +0200452 }
453
Jens Axboe3d6392c2007-07-09 12:38:05 +0200454 mutex_unlock(&bsg_mutex);
455 return 0;
James Bottomley6826ee42007-07-20 16:50:10 -0500456
FUJITA Tomonori598443a2007-07-23 09:33:26 +0900457unregister_class_dev:
Tony Jonesee959b02008-02-22 00:13:36 +0100458 device_unregister(class_dev);
Christoph Hellwig5de815a2018-05-29 08:40:23 +0200459idr_remove:
Tejun Heobab998d2013-02-27 17:03:57 -0800460 idr_remove(&bsg_minor_idr, bcd->minor);
FUJITA Tomonori598443a2007-07-23 09:33:26 +0900461unlock:
Jens Axboe264a0472007-01-23 16:30:17 +0100462 mutex_unlock(&bsg_mutex);
FUJITA Tomonori4e2872d2007-03-28 13:29:24 +0200463 return ret;
Jens Axboe3d6392c2007-07-09 12:38:05 +0200464}
Christoph Hellwig17cb9602018-03-13 17:28:41 +0100465
466int bsg_scsi_register_queue(struct request_queue *q, struct device *parent)
467{
468 if (!blk_queue_scsi_passthrough(q)) {
469 WARN_ONCE(true, "Attempt to register a non-SCSI queue\n");
470 return -EINVAL;
471 }
472
Christoph Hellwig5de815a2018-05-29 08:40:23 +0200473 return bsg_register_queue(q, parent, dev_name(parent), &bsg_scsi_ops);
Christoph Hellwig17cb9602018-03-13 17:28:41 +0100474}
475EXPORT_SYMBOL_GPL(bsg_scsi_register_queue);
Jens Axboe3d6392c2007-07-09 12:38:05 +0200476
Greg Kroah-Hartman7e7654a2007-09-12 15:06:57 -0700477static struct cdev bsg_cdev;
FUJITA Tomonori292b7f22007-03-28 13:29:58 +0200478
Al Viro2c9ede52011-07-23 20:24:48 -0400479static char *bsg_devnode(struct device *dev, umode_t *mode)
Kay Sievers2bdf9142009-04-30 15:23:42 +0200480{
481 return kasprintf(GFP_KERNEL, "bsg/%s", dev_name(dev));
482}
483
Jens Axboe3d6392c2007-07-09 12:38:05 +0200484static int __init bsg_init(void)
485{
486 int ret, i;
Jens Axboe46f6ef42007-07-17 08:56:10 +0200487 dev_t devid;
Jens Axboe3d6392c2007-07-09 12:38:05 +0200488
Jens Axboe25fd1642007-07-17 08:52:29 +0200489 for (i = 0; i < BSG_LIST_ARRAY_SIZE; i++)
Jens Axboe3d6392c2007-07-09 12:38:05 +0200490 INIT_HLIST_HEAD(&bsg_device_list[i]);
491
492 bsg_class = class_create(THIS_MODULE, "bsg");
Christoph Hellwig28519c82018-07-12 10:09:59 +0200493 if (IS_ERR(bsg_class))
494 return PTR_ERR(bsg_class);
Kay Sieverse454cea2009-09-18 23:01:12 +0200495 bsg_class->devnode = bsg_devnode;
Jens Axboe3d6392c2007-07-09 12:38:05 +0200496
Jens Axboe46f6ef42007-07-17 08:56:10 +0200497 ret = alloc_chrdev_region(&devid, 0, BSG_MAX_DEVS, "bsg");
FUJITA Tomonori9b9f7702007-07-17 12:20:46 +0200498 if (ret)
499 goto destroy_bsg_class;
Jens Axboe3d6392c2007-07-09 12:38:05 +0200500
Jens Axboe46f6ef42007-07-17 08:56:10 +0200501 bsg_major = MAJOR(devid);
502
FUJITA Tomonori292b7f22007-03-28 13:29:58 +0200503 cdev_init(&bsg_cdev, &bsg_fops);
Jens Axboe46f6ef42007-07-17 08:56:10 +0200504 ret = cdev_add(&bsg_cdev, MKDEV(bsg_major, 0), BSG_MAX_DEVS);
FUJITA Tomonori9b9f7702007-07-17 12:20:46 +0200505 if (ret)
506 goto unregister_chrdev;
FUJITA Tomonori292b7f22007-03-28 13:29:58 +0200507
Jens Axboe5d3a8cd2007-07-17 15:10:09 +0200508 printk(KERN_INFO BSG_DESCRIPTION " version " BSG_VERSION
FUJITA Tomonori0ed081c2007-07-17 12:21:35 +0200509 " loaded (major %d)\n", bsg_major);
Jens Axboe3d6392c2007-07-09 12:38:05 +0200510 return 0;
FUJITA Tomonori9b9f7702007-07-17 12:20:46 +0200511unregister_chrdev:
512 unregister_chrdev_region(MKDEV(bsg_major, 0), BSG_MAX_DEVS);
513destroy_bsg_class:
514 class_destroy(bsg_class);
FUJITA Tomonori9b9f7702007-07-17 12:20:46 +0200515 return ret;
Jens Axboe3d6392c2007-07-09 12:38:05 +0200516}
517
518MODULE_AUTHOR("Jens Axboe");
FUJITA Tomonori0ed081c2007-07-17 12:21:35 +0200519MODULE_DESCRIPTION(BSG_DESCRIPTION);
Jens Axboe3d6392c2007-07-09 12:38:05 +0200520MODULE_LICENSE("GPL");
521
FUJITA Tomonori4e2872d2007-03-28 13:29:24 +0200522device_initcall(bsg_init);