blob: 50e5f8f666f2a907d99d17e2fb8ca311a8365a98 [file] [log] [blame]
Jens Axboe3d6392c2007-07-09 12:38:05 +02001/*
FUJITA Tomonori0c6a89b2007-07-29 23:00:46 +09002 * bsg.c - block layer implementation of the sg v4 interface
Jens Axboe3d6392c2007-07-09 12:38:05 +02003 *
4 * Copyright (C) 2004 Jens Axboe <axboe@suse.de> SUSE Labs
5 * Copyright (C) 2004 Peter M. Jones <pjones@redhat.com>
6 *
7 * This file is subject to the terms and conditions of the GNU General Public
8 * License version 2. See the file "COPYING" in the main directory of this
9 * archive for more details.
10 *
11 */
Jens Axboe3d6392c2007-07-09 12:38:05 +020012#include <linux/module.h>
13#include <linux/init.h>
14#include <linux/file.h>
15#include <linux/blkdev.h>
Jens Axboe3d6392c2007-07-09 12:38:05 +020016#include <linux/cdev.h>
Randy Dunlapad5ebd22009-11-11 13:47:45 +010017#include <linux/jiffies.h>
Jens Axboe3d6392c2007-07-09 12:38:05 +020018#include <linux/percpu.h>
FUJITA Tomonori598443a2007-07-23 09:33:26 +090019#include <linux/idr.h>
Jens Axboe3d6392c2007-07-09 12:38:05 +020020#include <linux/bsg.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090021#include <linux/slab.h>
Jens Axboe3d6392c2007-07-09 12:38:05 +020022
23#include <scsi/scsi.h>
24#include <scsi/scsi_ioctl.h>
25#include <scsi/scsi_cmnd.h>
FUJITA Tomonori4e2872d2007-03-28 13:29:24 +020026#include <scsi/scsi_device.h>
27#include <scsi/scsi_driver.h>
Jens Axboe3d6392c2007-07-09 12:38:05 +020028#include <scsi/sg.h>
29
FUJITA Tomonori0ed081c2007-07-17 12:21:35 +020030#define BSG_DESCRIPTION "Block layer SCSI generic (bsg) driver"
31#define BSG_VERSION "0.4"
Jens Axboe3d6392c2007-07-09 12:38:05 +020032
Johannes Thumshirn3124b652018-01-24 09:50:06 -070033#define bsg_dbg(bd, fmt, ...) \
34 pr_debug("%s: " fmt, (bd)->name, ##__VA_ARGS__)
35
Jens Axboe3d6392c2007-07-09 12:38:05 +020036struct bsg_device {
Jens Axboe165125e2007-07-24 09:28:11 +020037 struct request_queue *queue;
Jens Axboe3d6392c2007-07-09 12:38:05 +020038 spinlock_t lock;
Jens Axboe3d6392c2007-07-09 12:38:05 +020039 struct hlist_node dev_list;
John Pittmandb193952018-08-27 14:33:05 -040040 refcount_t ref_count;
Kay Sievers3ada8b72009-01-06 10:44:43 -080041 char name[20];
Jens Axboe3d6392c2007-07-09 12:38:05 +020042 int max_queue;
Jens Axboe3d6392c2007-07-09 12:38:05 +020043};
44
Jens Axboe5309cb32007-01-23 16:24:41 +010045#define BSG_DEFAULT_CMDS 64
FUJITA Tomonori292b7f22007-03-28 13:29:58 +020046#define BSG_MAX_DEVS 32768
Jens Axboe3d6392c2007-07-09 12:38:05 +020047
Jens Axboe3d6392c2007-07-09 12:38:05 +020048static DEFINE_MUTEX(bsg_mutex);
FUJITA Tomonori598443a2007-07-23 09:33:26 +090049static DEFINE_IDR(bsg_minor_idr);
Jens Axboe3d6392c2007-07-09 12:38:05 +020050
Jens Axboe25fd1642007-07-17 08:52:29 +020051#define BSG_LIST_ARRAY_SIZE 8
Jens Axboe25fd1642007-07-17 08:52:29 +020052static struct hlist_head bsg_device_list[BSG_LIST_ARRAY_SIZE];
Jens Axboe3d6392c2007-07-09 12:38:05 +020053
54static struct class *bsg_class;
Jens Axboe46f6ef42007-07-17 08:56:10 +020055static int bsg_major;
Jens Axboe3d6392c2007-07-09 12:38:05 +020056
FUJITA Tomonori1c1133e2007-07-17 12:21:15 +020057static inline struct hlist_head *bsg_dev_idx_hash(int index)
Jens Axboe3d6392c2007-07-09 12:38:05 +020058{
FUJITA Tomonori1c1133e2007-07-17 12:21:15 +020059 return &bsg_device_list[index & (BSG_LIST_ARRAY_SIZE - 1)];
Jens Axboe3d6392c2007-07-09 12:38:05 +020060}
61
Christoph Hellwig17cb9602018-03-13 17:28:41 +010062#define uptr64(val) ((void __user *)(uintptr_t)(val))
Christoph Hellwig82ed4db2017-01-27 09:46:29 +010063
Christoph Hellwig17cb9602018-03-13 17:28:41 +010064static int bsg_scsi_check_proto(struct sg_io_v4 *hdr)
65{
66 if (hdr->protocol != BSG_PROTOCOL_SCSI ||
67 hdr->subprotocol != BSG_SUB_PROTOCOL_SCSI_CMD)
68 return -EINVAL;
69 return 0;
70}
71
72static int bsg_scsi_fill_hdr(struct request *rq, struct sg_io_v4 *hdr,
73 fmode_t mode)
74{
75 struct scsi_request *sreq = scsi_req(rq);
76
77 sreq->cmd_len = hdr->request_len;
78 if (sreq->cmd_len > BLK_MAX_CDB) {
79 sreq->cmd = kzalloc(sreq->cmd_len, GFP_KERNEL);
80 if (!sreq->cmd)
FUJITA Tomonori9f5de6b2008-04-30 13:16:21 +090081 return -ENOMEM;
82 }
FUJITA Tomonori70e36ec2006-12-20 11:20:15 +010083
Christoph Hellwig17cb9602018-03-13 17:28:41 +010084 if (copy_from_user(sreq->cmd, uptr64(hdr->request), sreq->cmd_len))
FUJITA Tomonori70e36ec2006-12-20 11:20:15 +010085 return -EFAULT;
Christoph Hellwig17cb9602018-03-13 17:28:41 +010086 if (blk_verify_command(sreq->cmd, mode))
FUJITA Tomonori70e36ec2006-12-20 11:20:15 +010087 return -EPERM;
Christoph Hellwig17cb9602018-03-13 17:28:41 +010088 return 0;
89}
90
91static int bsg_scsi_complete_rq(struct request *rq, struct sg_io_v4 *hdr)
92{
93 struct scsi_request *sreq = scsi_req(rq);
94 int ret = 0;
Jens Axboe3d6392c2007-07-09 12:38:05 +020095
96 /*
Christoph Hellwig17cb9602018-03-13 17:28:41 +010097 * fill in all the output members
Jens Axboe3d6392c2007-07-09 12:38:05 +020098 */
Christoph Hellwig17cb9602018-03-13 17:28:41 +010099 hdr->device_status = sreq->result & 0xff;
100 hdr->transport_status = host_byte(sreq->result);
101 hdr->driver_status = driver_byte(sreq->result);
102 hdr->info = 0;
103 if (hdr->device_status || hdr->transport_status || hdr->driver_status)
104 hdr->info |= SG_INFO_CHECK;
105 hdr->response_len = 0;
106
107 if (sreq->sense_len && hdr->response) {
108 int len = min_t(unsigned int, hdr->max_response_len,
109 sreq->sense_len);
110
111 if (copy_to_user(uptr64(hdr->response), sreq->sense, len))
112 ret = -EFAULT;
113 else
114 hdr->response_len = len;
115 }
116
117 if (rq->next_rq) {
118 hdr->dout_resid = sreq->resid_len;
119 hdr->din_resid = scsi_req(rq->next_rq)->resid_len;
120 } else if (rq_data_dir(rq) == READ) {
121 hdr->din_resid = sreq->resid_len;
122 } else {
123 hdr->dout_resid = sreq->resid_len;
124 }
125
126 return ret;
127}
128
129static void bsg_scsi_free_rq(struct request *rq)
130{
131 scsi_req_free_cmd(scsi_req(rq));
132}
133
134static const struct bsg_ops bsg_scsi_ops = {
135 .check_proto = bsg_scsi_check_proto,
136 .fill_hdr = bsg_scsi_fill_hdr,
137 .complete_rq = bsg_scsi_complete_rq,
138 .free_rq = bsg_scsi_free_rq,
139};
140
141static struct request *
142bsg_map_hdr(struct request_queue *q, struct sg_io_v4 *hdr, fmode_t mode)
143{
144 struct request *rq, *next_rq = NULL;
145 int ret;
146
147 if (!q->bsg_dev.class_dev)
148 return ERR_PTR(-ENXIO);
149
150 if (hdr->guard != 'Q')
151 return ERR_PTR(-EINVAL);
152
153 ret = q->bsg_dev.ops->check_proto(hdr);
154 if (ret)
155 return ERR_PTR(ret);
156
157 rq = blk_get_request(q, hdr->dout_xfer_len ?
Christoph Hellwigff005a02018-05-09 09:54:05 +0200158 REQ_OP_SCSI_OUT : REQ_OP_SCSI_IN, 0);
Christoph Hellwig17cb9602018-03-13 17:28:41 +0100159 if (IS_ERR(rq))
160 return rq;
161
162 ret = q->bsg_dev.ops->fill_hdr(rq, hdr, mode);
163 if (ret)
164 goto out;
Jens Axboe3d6392c2007-07-09 12:38:05 +0200165
Randy Dunlapad5ebd22009-11-11 13:47:45 +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 Hellwig17cb9602018-03-13 17:28:41 +0100174 if (hdr->dout_xfer_len && hdr->din_xfer_len) {
FUJITA Tomonori2c9ecdf2007-07-16 08:52:15 +0200175 if (!test_bit(QUEUE_FLAG_BIDI, &q->queue_flags)) {
176 ret = -EOPNOTSUPP;
177 goto out;
178 }
179
Christoph Hellwig2e5b2d72018-12-20 18:36:20 +0100180 pr_warn_once(
181 "BIDI support in bsg has been deprecated and might be removed. "
182 "Please report your use case to linux-scsi@vger.kernel.org\n");
183
Christoph Hellwigff005a02018-05-09 09:54:05 +0200184 next_rq = blk_get_request(q, REQ_OP_SCSI_IN, 0);
Joe Lawrencea492f072014-08-28 08:15:21 -0600185 if (IS_ERR(next_rq)) {
186 ret = PTR_ERR(next_rq);
FUJITA Tomonori2c9ecdf2007-07-16 08:52:15 +0200187 goto out;
188 }
FUJITA Tomonori2c9ecdf2007-07-16 08:52:15 +0200189
Christoph Hellwig17cb9602018-03-13 17:28:41 +0100190 rq->next_rq = next_rq;
191 ret = blk_rq_map_user(q, next_rq, NULL, uptr64(hdr->din_xferp),
FUJITA Tomonori152e2832008-08-28 16:17:06 +0900192 hdr->din_xfer_len, GFP_KERNEL);
FUJITA Tomonori2c9ecdf2007-07-16 08:52:15 +0200193 if (ret)
Christoph Hellwig17cb9602018-03-13 17:28:41 +0100194 goto out_free_nextrq;
Jens Axboe3d6392c2007-07-09 12:38:05 +0200195 }
196
FUJITA Tomonori70e36ec2006-12-20 11:20:15 +0100197 if (hdr->dout_xfer_len) {
Christoph Hellwig17cb9602018-03-13 17:28:41 +0100198 ret = blk_rq_map_user(q, rq, NULL, uptr64(hdr->dout_xferp),
199 hdr->dout_xfer_len, GFP_KERNEL);
FUJITA Tomonori70e36ec2006-12-20 11:20:15 +0100200 } else if (hdr->din_xfer_len) {
Christoph Hellwig17cb9602018-03-13 17:28:41 +0100201 ret = blk_rq_map_user(q, rq, NULL, uptr64(hdr->din_xferp),
202 hdr->din_xfer_len, GFP_KERNEL);
Jens Axboe3d6392c2007-07-09 12:38:05 +0200203 }
Boaz Harroshc1c20122009-02-03 07:47:29 +0100204
Christoph Hellwig17cb9602018-03-13 17:28:41 +0100205 if (ret)
206 goto out_unmap_nextrq;
Jens Axboe3d6392c2007-07-09 12:38:05 +0200207 return rq;
Christoph Hellwig17cb9602018-03-13 17:28:41 +0100208
209out_unmap_nextrq:
210 if (rq->next_rq)
211 blk_rq_unmap_user(rq->next_rq->bio);
212out_free_nextrq:
213 if (rq->next_rq)
214 blk_put_request(rq->next_rq);
FUJITA Tomonori2c9ecdf2007-07-16 08:52:15 +0200215out:
Christoph Hellwig17cb9602018-03-13 17:28:41 +0100216 q->bsg_dev.ops->free_rq(rq);
FUJITA Tomonori2c9ecdf2007-07-16 08:52:15 +0200217 blk_put_request(rq);
FUJITA Tomonori2c9ecdf2007-07-16 08:52:15 +0200218 return ERR_PTR(ret);
Jens Axboe3d6392c2007-07-09 12:38:05 +0200219}
220
FUJITA Tomonori70e36ec2006-12-20 11:20:15 +0100221static int blk_complete_sgv4_hdr_rq(struct request *rq, struct sg_io_v4 *hdr,
FUJITA Tomonori2c9ecdf2007-07-16 08:52:15 +0200222 struct bio *bio, struct bio *bidi_bio)
FUJITA Tomonori70e36ec2006-12-20 11:20:15 +0100223{
Christoph Hellwig17cb9602018-03-13 17:28:41 +0100224 int ret;
FUJITA Tomonori70e36ec2006-12-20 11:20:15 +0100225
Christoph Hellwig17cb9602018-03-13 17:28:41 +0100226 ret = rq->q->bsg_dev.ops->complete_rq(rq, hdr);
FUJITA Tomonori70e36ec2006-12-20 11:20:15 +0100227
FUJITA Tomonori2c9ecdf2007-07-16 08:52:15 +0200228 if (rq->next_rq) {
229 blk_rq_unmap_user(bidi_bio);
230 blk_put_request(rq->next_rq);
Christoph Hellwig17cb9602018-03-13 17:28:41 +0100231 }
James Bottomley2d507a02007-12-29 10:59:53 -0600232
FUJITA Tomonori70e36ec2006-12-20 11:20:15 +0100233 blk_rq_unmap_user(bio);
Christoph Hellwig17cb9602018-03-13 17:28:41 +0100234 rq->q->bsg_dev.ops->free_rq(rq);
FUJITA Tomonori70e36ec2006-12-20 11:20:15 +0100235 blk_put_request(rq);
FUJITA Tomonori70e36ec2006-12-20 11:20:15 +0100236 return ret;
237}
238
Jens Axboe3d6392c2007-07-09 12:38:05 +0200239static struct bsg_device *bsg_alloc_device(void)
240{
Jens Axboe3d6392c2007-07-09 12:38:05 +0200241 struct bsg_device *bd;
Jens Axboe3d6392c2007-07-09 12:38:05 +0200242
243 bd = kzalloc(sizeof(struct bsg_device), GFP_KERNEL);
244 if (unlikely(!bd))
245 return NULL;
246
247 spin_lock_init(&bd->lock);
Jens Axboe5309cb32007-01-23 16:24:41 +0100248 bd->max_queue = BSG_DEFAULT_CMDS;
Jens Axboe3d6392c2007-07-09 12:38:05 +0200249 INIT_HLIST_NODE(&bd->dev_list);
Jens Axboe3d6392c2007-07-09 12:38:05 +0200250 return bd;
Jens Axboe3d6392c2007-07-09 12:38:05 +0200251}
252
253static int bsg_put_device(struct bsg_device *bd)
254{
FUJITA Tomonori97f46ae2008-04-19 00:43:14 +0900255 struct request_queue *q = bd->queue;
Jens Axboe3d6392c2007-07-09 12:38:05 +0200256
257 mutex_lock(&bsg_mutex);
258
John Pittmandb193952018-08-27 14:33:05 -0400259 if (!refcount_dec_and_test(&bd->ref_count)) {
FUJITA Tomonori3f27e3e2008-05-29 07:56:55 +0900260 mutex_unlock(&bsg_mutex);
Christoph Hellwig28519c82018-07-12 10:09:59 +0200261 return 0;
FUJITA Tomonori3f27e3e2008-05-29 07:56:55 +0900262 }
263
264 hlist_del(&bd->dev_list);
265 mutex_unlock(&bsg_mutex);
Jens Axboe3d6392c2007-07-09 12:38:05 +0200266
Johannes Thumshirn3124b652018-01-24 09:50:06 -0700267 bsg_dbg(bd, "tearing down\n");
Jens Axboe3d6392c2007-07-09 12:38:05 +0200268
269 /*
270 * close can always block
271 */
Jens Axboe5309cb32007-01-23 16:24:41 +0100272 kfree(bd);
Christoph Hellwig28519c82018-07-12 10:09:59 +0200273 blk_put_queue(q);
274 return 0;
Jens Axboe3d6392c2007-07-09 12:38:05 +0200275}
276
277static struct bsg_device *bsg_add_device(struct inode *inode,
FUJITA Tomonorid351af02007-07-09 12:40:35 +0200278 struct request_queue *rq,
Jens Axboe3d6392c2007-07-09 12:38:05 +0200279 struct file *file)
280{
Jens Axboe25fd1642007-07-17 08:52:29 +0200281 struct bsg_device *bd;
Jens Axboe3d6392c2007-07-09 12:38:05 +0200282 unsigned char buf[32];
Bart Van Assched9f97262017-05-31 14:43:47 -0700283
Anatoliy Glagolevd6c73962018-06-13 15:38:51 -0600284 lockdep_assert_held(&bsg_mutex);
285
Tejun Heo09ac46c2011-12-14 00:33:38 +0100286 if (!blk_get_queue(rq))
FUJITA Tomonoric3ff1b92008-03-31 10:03:39 +0900287 return ERR_PTR(-ENXIO);
Jens Axboe3d6392c2007-07-09 12:38:05 +0200288
289 bd = bsg_alloc_device();
FUJITA Tomonoric3ff1b92008-03-31 10:03:39 +0900290 if (!bd) {
291 blk_put_queue(rq);
Jens Axboe3d6392c2007-07-09 12:38:05 +0200292 return ERR_PTR(-ENOMEM);
FUJITA Tomonoric3ff1b92008-03-31 10:03:39 +0900293 }
Jens Axboe3d6392c2007-07-09 12:38:05 +0200294
FUJITA Tomonorid351af02007-07-09 12:40:35 +0200295 bd->queue = rq;
Adel Gadllah0b07de82008-06-26 13:48:27 +0200296
John Pittmandb193952018-08-27 14:33:05 -0400297 refcount_set(&bd->ref_count, 1);
FUJITA Tomonori842ea772008-03-31 10:03:41 +0900298 hlist_add_head(&bd->dev_list, bsg_dev_idx_hash(iminor(inode)));
Jens Axboe3d6392c2007-07-09 12:38:05 +0200299
Kay Sievers3ada8b72009-01-06 10:44:43 -0800300 strncpy(bd->name, dev_name(rq->bsg_dev.class_dev), sizeof(bd->name) - 1);
Johannes Thumshirn3124b652018-01-24 09:50:06 -0700301 bsg_dbg(bd, "bound to <%s>, max queue %d\n",
FUJITA Tomonori9e69fbb2006-12-20 11:18:22 +0100302 format_dev_t(buf, inode->i_rdev), bd->max_queue);
Jens Axboe3d6392c2007-07-09 12:38:05 +0200303
Jens Axboe3d6392c2007-07-09 12:38:05 +0200304 return bd;
305}
306
FUJITA Tomonori842ea772008-03-31 10:03:41 +0900307static struct bsg_device *__bsg_get_device(int minor, struct request_queue *q)
Jens Axboe3d6392c2007-07-09 12:38:05 +0200308{
FUJITA Tomonori43ac9e62008-03-31 10:03:40 +0900309 struct bsg_device *bd;
Jens Axboe3d6392c2007-07-09 12:38:05 +0200310
Anatoliy Glagolevd6c73962018-06-13 15:38:51 -0600311 lockdep_assert_held(&bsg_mutex);
Jens Axboe3d6392c2007-07-09 12:38:05 +0200312
Sasha Levinb67bfe02013-02-27 17:06:00 -0800313 hlist_for_each_entry(bd, bsg_dev_idx_hash(minor), dev_list) {
FUJITA Tomonori842ea772008-03-31 10:03:41 +0900314 if (bd->queue == q) {
John Pittmandb193952018-08-27 14:33:05 -0400315 refcount_inc(&bd->ref_count);
FUJITA Tomonori43ac9e62008-03-31 10:03:40 +0900316 goto found;
Jens Axboe3d6392c2007-07-09 12:38:05 +0200317 }
Jens Axboe3d6392c2007-07-09 12:38:05 +0200318 }
FUJITA Tomonori43ac9e62008-03-31 10:03:40 +0900319 bd = NULL;
320found:
Jens Axboe3d6392c2007-07-09 12:38:05 +0200321 return bd;
322}
323
324static struct bsg_device *bsg_get_device(struct inode *inode, struct file *file)
325{
FUJITA Tomonori598443a2007-07-23 09:33:26 +0900326 struct bsg_device *bd;
327 struct bsg_class_device *bcd;
Jens Axboe3d6392c2007-07-09 12:38:05 +0200328
Jens Axboe3d6392c2007-07-09 12:38:05 +0200329 /*
330 * find the class device
331 */
Jens Axboe3d6392c2007-07-09 12:38:05 +0200332 mutex_lock(&bsg_mutex);
FUJITA Tomonori598443a2007-07-23 09:33:26 +0900333 bcd = idr_find(&bsg_minor_idr, iminor(inode));
Jens Axboe3d6392c2007-07-09 12:38:05 +0200334
Anatoliy Glagolevd6c73962018-06-13 15:38:51 -0600335 if (!bcd) {
336 bd = ERR_PTR(-ENODEV);
337 goto out_unlock;
338 }
Jens Axboe3d6392c2007-07-09 12:38:05 +0200339
FUJITA Tomonori842ea772008-03-31 10:03:41 +0900340 bd = __bsg_get_device(iminor(inode), bcd->queue);
Anatoliy Glagolevd6c73962018-06-13 15:38:51 -0600341 if (!bd)
342 bd = bsg_add_device(inode, bcd->queue, file);
FUJITA Tomonorid45ac4f2008-03-31 10:03:38 +0900343
Anatoliy Glagolevd6c73962018-06-13 15:38:51 -0600344out_unlock:
345 mutex_unlock(&bsg_mutex);
FUJITA Tomonorid45ac4f2008-03-31 10:03:38 +0900346 return bd;
Jens Axboe3d6392c2007-07-09 12:38:05 +0200347}
348
349static int bsg_open(struct inode *inode, struct file *file)
350{
Jonathan Corbet75bd2ef2008-05-15 09:09:23 -0600351 struct bsg_device *bd;
352
Jonathan Corbet75bd2ef2008-05-15 09:09:23 -0600353 bd = bsg_get_device(inode, file);
Jens Axboe3d6392c2007-07-09 12:38:05 +0200354
355 if (IS_ERR(bd))
356 return PTR_ERR(bd);
357
358 file->private_data = bd;
359 return 0;
360}
361
362static int bsg_release(struct inode *inode, struct file *file)
363{
364 struct bsg_device *bd = file->private_data;
365
366 file->private_data = NULL;
367 return bsg_put_device(bd);
368}
369
Jens Axboe25fd1642007-07-17 08:52:29 +0200370static long bsg_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
Jens Axboe3d6392c2007-07-09 12:38:05 +0200371{
372 struct bsg_device *bd = file->private_data;
373 int __user *uarg = (int __user *) arg;
James Bottomley2d507a02007-12-29 10:59:53 -0600374 int ret;
Jens Axboe3d6392c2007-07-09 12:38:05 +0200375
Jens Axboe3d6392c2007-07-09 12:38:05 +0200376 switch (cmd) {
377 /*
378 * our own ioctls
379 */
380 case SG_GET_COMMAND_Q:
381 return put_user(bd->max_queue, uarg);
Jens Axboe5309cb32007-01-23 16:24:41 +0100382 case SG_SET_COMMAND_Q: {
Jens Axboe3d6392c2007-07-09 12:38:05 +0200383 int queue;
384
385 if (get_user(queue, uarg))
386 return -EFAULT;
Jens Axboe5309cb32007-01-23 16:24:41 +0100387 if (queue < 1)
Jens Axboe3d6392c2007-07-09 12:38:05 +0200388 return -EINVAL;
389
Jens Axboe5309cb32007-01-23 16:24:41 +0100390 spin_lock_irq(&bd->lock);
Jens Axboe3d6392c2007-07-09 12:38:05 +0200391 bd->max_queue = queue;
Jens Axboe5309cb32007-01-23 16:24:41 +0100392 spin_unlock_irq(&bd->lock);
Jens Axboe3d6392c2007-07-09 12:38:05 +0200393 return 0;
394 }
395
396 /*
397 * SCSI/sg ioctls
398 */
399 case SG_GET_VERSION_NUM:
400 case SCSI_IOCTL_GET_IDLUN:
401 case SCSI_IOCTL_GET_BUS_NUMBER:
402 case SG_SET_TIMEOUT:
403 case SG_GET_TIMEOUT:
404 case SG_GET_RESERVED_SIZE:
405 case SG_SET_RESERVED_SIZE:
406 case SG_EMULATED_HOST:
Jens Axboe3d6392c2007-07-09 12:38:05 +0200407 case SCSI_IOCTL_SEND_COMMAND: {
408 void __user *uarg = (void __user *) arg;
Al Viro74f3c8a2007-08-27 15:38:10 -0400409 return scsi_cmd_ioctl(bd->queue, NULL, file->f_mode, cmd, uarg);
Jens Axboe3d6392c2007-07-09 12:38:05 +0200410 }
FUJITA Tomonori10e88552006-12-20 11:20:57 +0100411 case SG_IO: {
412 struct request *rq;
FUJITA Tomonori2c9ecdf2007-07-16 08:52:15 +0200413 struct bio *bio, *bidi_bio = NULL;
FUJITA Tomonori10e88552006-12-20 11:20:57 +0100414 struct sg_io_v4 hdr;
Boaz Harrosh05378942009-03-24 12:23:40 +0100415 int at_head;
FUJITA Tomonori10e88552006-12-20 11:20:57 +0100416
417 if (copy_from_user(&hdr, uarg, sizeof(hdr)))
418 return -EFAULT;
419
Christoph Hellwig17cb9602018-03-13 17:28:41 +0100420 rq = bsg_map_hdr(bd->queue, &hdr, file->f_mode);
FUJITA Tomonori10e88552006-12-20 11:20:57 +0100421 if (IS_ERR(rq))
422 return PTR_ERR(rq);
423
424 bio = rq->bio;
FUJITA Tomonori2c9ecdf2007-07-16 08:52:15 +0200425 if (rq->next_rq)
426 bidi_bio = rq->next_rq->bio;
Boaz Harrosh05378942009-03-24 12:23:40 +0100427
428 at_head = (0 == (hdr.flags & BSG_FLAG_Q_AT_TAIL));
429 blk_execute_rq(bd->queue, NULL, rq, at_head);
James Bottomley2d507a02007-12-29 10:59:53 -0600430 ret = blk_complete_sgv4_hdr_rq(rq, &hdr, bio, bidi_bio);
FUJITA Tomonori10e88552006-12-20 11:20:57 +0100431
432 if (copy_to_user(uarg, &hdr, sizeof(hdr)))
433 return -EFAULT;
Jens Axboeb711afa2006-12-20 11:25:23 +0100434
James Bottomley2d507a02007-12-29 10:59:53 -0600435 return ret;
FUJITA Tomonori10e88552006-12-20 11:20:57 +0100436 }
Jens Axboe3d6392c2007-07-09 12:38:05 +0200437 default:
Jens Axboe3d6392c2007-07-09 12:38:05 +0200438 return -ENOTTY;
Jens Axboe3d6392c2007-07-09 12:38:05 +0200439 }
440}
441
Arjan van de Ven7344be02007-10-15 11:01:53 +0200442static const struct file_operations bsg_fops = {
Jens Axboe3d6392c2007-07-09 12:38:05 +0200443 .open = bsg_open,
444 .release = bsg_release,
Jens Axboe25fd1642007-07-17 08:52:29 +0200445 .unlocked_ioctl = bsg_ioctl,
Jens Axboe3d6392c2007-07-09 12:38:05 +0200446 .owner = THIS_MODULE,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200447 .llseek = default_llseek,
Jens Axboe3d6392c2007-07-09 12:38:05 +0200448};
449
FUJITA Tomonorid351af02007-07-09 12:40:35 +0200450void bsg_unregister_queue(struct request_queue *q)
Jens Axboe3d6392c2007-07-09 12:38:05 +0200451{
FUJITA Tomonorid351af02007-07-09 12:40:35 +0200452 struct bsg_class_device *bcd = &q->bsg_dev;
Jens Axboe3d6392c2007-07-09 12:38:05 +0200453
FUJITA Tomonoridf468822007-07-21 13:23:25 +0900454 if (!bcd->class_dev)
455 return;
Jens Axboe3d6392c2007-07-09 12:38:05 +0200456
457 mutex_lock(&bsg_mutex);
FUJITA Tomonori598443a2007-07-23 09:33:26 +0900458 idr_remove(&bsg_minor_idr, bcd->minor);
Stanislaw Gruszka37b40ad2012-02-08 20:02:03 +0100459 if (q->kobj.sd)
460 sysfs_remove_link(&q->kobj, "bsg");
Tony Jonesee959b02008-02-22 00:13:36 +0100461 device_unregister(bcd->class_dev);
Jens Axboe3d6392c2007-07-09 12:38:05 +0200462 bcd->class_dev = NULL;
Jens Axboe3d6392c2007-07-09 12:38:05 +0200463 mutex_unlock(&bsg_mutex);
464}
FUJITA Tomonori4cf07232007-03-30 11:19:39 +0200465EXPORT_SYMBOL_GPL(bsg_unregister_queue);
Jens Axboe3d6392c2007-07-09 12:38:05 +0200466
FUJITA Tomonori97f46ae2008-04-19 00:43:14 +0900467int bsg_register_queue(struct request_queue *q, struct device *parent,
Christoph Hellwig5de815a2018-05-29 08:40:23 +0200468 const char *name, const struct bsg_ops *ops)
Jens Axboe3d6392c2007-07-09 12:38:05 +0200469{
FUJITA Tomonori598443a2007-07-23 09:33:26 +0900470 struct bsg_class_device *bcd;
Jens Axboe3d6392c2007-07-09 12:38:05 +0200471 dev_t dev;
Tejun Heobab998d2013-02-27 17:03:57 -0800472 int ret;
Tony Jonesee959b02008-02-22 00:13:36 +0100473 struct device *class_dev = NULL;
Jens Axboe3d6392c2007-07-09 12:38:05 +0200474
475 /*
476 * we need a proper transport to send commands, not a stacked device
477 */
Jens Axboe344e9ff2018-11-15 12:22:51 -0700478 if (!queue_is_mq(q))
Jens Axboe3d6392c2007-07-09 12:38:05 +0200479 return 0;
480
FUJITA Tomonorid351af02007-07-09 12:40:35 +0200481 bcd = &q->bsg_dev;
Jens Axboe3d6392c2007-07-09 12:38:05 +0200482 memset(bcd, 0, sizeof(*bcd));
Jens Axboe3d6392c2007-07-09 12:38:05 +0200483
484 mutex_lock(&bsg_mutex);
FUJITA Tomonori598443a2007-07-23 09:33:26 +0900485
Tejun Heobab998d2013-02-27 17:03:57 -0800486 ret = idr_alloc(&bsg_minor_idr, bcd, 0, BSG_MAX_DEVS, GFP_KERNEL);
487 if (ret < 0) {
488 if (ret == -ENOSPC) {
489 printk(KERN_ERR "bsg: too many bsg devices\n");
490 ret = -EINVAL;
491 }
FUJITA Tomonori598443a2007-07-23 09:33:26 +0900492 goto unlock;
493 }
494
Tejun Heobab998d2013-02-27 17:03:57 -0800495 bcd->minor = ret;
FUJITA Tomonorid351af02007-07-09 12:40:35 +0200496 bcd->queue = q;
Christoph Hellwig17cb9602018-03-13 17:28:41 +0100497 bcd->ops = ops;
Jens Axboe46f6ef42007-07-17 08:56:10 +0200498 dev = MKDEV(bsg_major, bcd->minor);
Christoph Hellwig5de815a2018-05-29 08:40:23 +0200499 class_dev = device_create(bsg_class, parent, dev, NULL, "%s", name);
FUJITA Tomonori4e2872d2007-03-28 13:29:24 +0200500 if (IS_ERR(class_dev)) {
501 ret = PTR_ERR(class_dev);
Christoph Hellwig5de815a2018-05-29 08:40:23 +0200502 goto idr_remove;
FUJITA Tomonori4e2872d2007-03-28 13:29:24 +0200503 }
504 bcd->class_dev = class_dev;
505
Linus Torvaldsabce8912007-07-16 11:18:23 -0700506 if (q->kobj.sd) {
FUJITA Tomonori4e2872d2007-03-28 13:29:24 +0200507 ret = sysfs_create_link(&q->kobj, &bcd->class_dev->kobj, "bsg");
508 if (ret)
FUJITA Tomonori598443a2007-07-23 09:33:26 +0900509 goto unregister_class_dev;
FUJITA Tomonori4e2872d2007-03-28 13:29:24 +0200510 }
511
Jens Axboe3d6392c2007-07-09 12:38:05 +0200512 mutex_unlock(&bsg_mutex);
513 return 0;
James Bottomley6826ee42007-07-20 16:50:10 -0500514
FUJITA Tomonori598443a2007-07-23 09:33:26 +0900515unregister_class_dev:
Tony Jonesee959b02008-02-22 00:13:36 +0100516 device_unregister(class_dev);
Christoph Hellwig5de815a2018-05-29 08:40:23 +0200517idr_remove:
Tejun Heobab998d2013-02-27 17:03:57 -0800518 idr_remove(&bsg_minor_idr, bcd->minor);
FUJITA Tomonori598443a2007-07-23 09:33:26 +0900519unlock:
Jens Axboe264a0472007-01-23 16:30:17 +0100520 mutex_unlock(&bsg_mutex);
FUJITA Tomonori4e2872d2007-03-28 13:29:24 +0200521 return ret;
Jens Axboe3d6392c2007-07-09 12:38:05 +0200522}
Christoph Hellwig17cb9602018-03-13 17:28:41 +0100523
524int bsg_scsi_register_queue(struct request_queue *q, struct device *parent)
525{
526 if (!blk_queue_scsi_passthrough(q)) {
527 WARN_ONCE(true, "Attempt to register a non-SCSI queue\n");
528 return -EINVAL;
529 }
530
Christoph Hellwig5de815a2018-05-29 08:40:23 +0200531 return bsg_register_queue(q, parent, dev_name(parent), &bsg_scsi_ops);
Christoph Hellwig17cb9602018-03-13 17:28:41 +0100532}
533EXPORT_SYMBOL_GPL(bsg_scsi_register_queue);
Jens Axboe3d6392c2007-07-09 12:38:05 +0200534
Greg Kroah-Hartman7e7654a2007-09-12 15:06:57 -0700535static struct cdev bsg_cdev;
FUJITA Tomonori292b7f22007-03-28 13:29:58 +0200536
Al Viro2c9ede52011-07-23 20:24:48 -0400537static char *bsg_devnode(struct device *dev, umode_t *mode)
Kay Sievers2bdf9142009-04-30 15:23:42 +0200538{
539 return kasprintf(GFP_KERNEL, "bsg/%s", dev_name(dev));
540}
541
Jens Axboe3d6392c2007-07-09 12:38:05 +0200542static int __init bsg_init(void)
543{
544 int ret, i;
Jens Axboe46f6ef42007-07-17 08:56:10 +0200545 dev_t devid;
Jens Axboe3d6392c2007-07-09 12:38:05 +0200546
Jens Axboe25fd1642007-07-17 08:52:29 +0200547 for (i = 0; i < BSG_LIST_ARRAY_SIZE; i++)
Jens Axboe3d6392c2007-07-09 12:38:05 +0200548 INIT_HLIST_HEAD(&bsg_device_list[i]);
549
550 bsg_class = class_create(THIS_MODULE, "bsg");
Christoph Hellwig28519c82018-07-12 10:09:59 +0200551 if (IS_ERR(bsg_class))
552 return PTR_ERR(bsg_class);
Kay Sieverse454cea2009-09-18 23:01:12 +0200553 bsg_class->devnode = bsg_devnode;
Jens Axboe3d6392c2007-07-09 12:38:05 +0200554
Jens Axboe46f6ef42007-07-17 08:56:10 +0200555 ret = alloc_chrdev_region(&devid, 0, BSG_MAX_DEVS, "bsg");
FUJITA Tomonori9b9f7702007-07-17 12:20:46 +0200556 if (ret)
557 goto destroy_bsg_class;
Jens Axboe3d6392c2007-07-09 12:38:05 +0200558
Jens Axboe46f6ef42007-07-17 08:56:10 +0200559 bsg_major = MAJOR(devid);
560
FUJITA Tomonori292b7f22007-03-28 13:29:58 +0200561 cdev_init(&bsg_cdev, &bsg_fops);
Jens Axboe46f6ef42007-07-17 08:56:10 +0200562 ret = cdev_add(&bsg_cdev, MKDEV(bsg_major, 0), BSG_MAX_DEVS);
FUJITA Tomonori9b9f7702007-07-17 12:20:46 +0200563 if (ret)
564 goto unregister_chrdev;
FUJITA Tomonori292b7f22007-03-28 13:29:58 +0200565
Jens Axboe5d3a8cd2007-07-17 15:10:09 +0200566 printk(KERN_INFO BSG_DESCRIPTION " version " BSG_VERSION
FUJITA Tomonori0ed081c2007-07-17 12:21:35 +0200567 " loaded (major %d)\n", bsg_major);
Jens Axboe3d6392c2007-07-09 12:38:05 +0200568 return 0;
FUJITA Tomonori9b9f7702007-07-17 12:20:46 +0200569unregister_chrdev:
570 unregister_chrdev_region(MKDEV(bsg_major, 0), BSG_MAX_DEVS);
571destroy_bsg_class:
572 class_destroy(bsg_class);
FUJITA Tomonori9b9f7702007-07-17 12:20:46 +0200573 return ret;
Jens Axboe3d6392c2007-07-09 12:38:05 +0200574}
575
576MODULE_AUTHOR("Jens Axboe");
FUJITA Tomonori0ed081c2007-07-17 12:21:35 +0200577MODULE_DESCRIPTION(BSG_DESCRIPTION);
Jens Axboe3d6392c2007-07-09 12:38:05 +0200578MODULE_LICENSE("GPL");
579
FUJITA Tomonori4e2872d2007-03-28 13:29:24 +0200580device_initcall(bsg_init);