bsg.c 12.5 KB
Newer Older
1
/*
2
 * bsg.c - block layer implementation of the sg v4 interface
3 4 5 6 7 8 9 10 11 12 13 14 15 16
 *
 * Copyright (C) 2004 Jens Axboe <axboe@suse.de> SUSE Labs
 * Copyright (C) 2004 Peter M. Jones <pjones@redhat.com>
 *
 *  This file is subject to the terms and conditions of the GNU General Public
 *  License version 2.  See the file "COPYING" in the main directory of this
 *  archive for more details.
 *
 */
#include <linux/module.h>
#include <linux/init.h>
#include <linux/file.h>
#include <linux/blkdev.h>
#include <linux/cdev.h>
Randy Dunlap's avatar
Randy Dunlap committed
17
#include <linux/jiffies.h>
18
#include <linux/percpu.h>
19
#include <linux/idr.h>
20
#include <linux/bsg.h>
21
#include <linux/slab.h>
22 23 24 25

#include <scsi/scsi.h>
#include <scsi/scsi_ioctl.h>
#include <scsi/scsi_cmnd.h>
26 27
#include <scsi/scsi_device.h>
#include <scsi/scsi_driver.h>
28 29
#include <scsi/sg.h>

FUJITA Tomonori's avatar
FUJITA Tomonori committed
30 31
#define BSG_DESCRIPTION	"Block layer SCSI generic (bsg) driver"
#define BSG_VERSION	"0.4"
32

33 34 35
#define bsg_dbg(bd, fmt, ...) \
	pr_debug("%s: " fmt, (bd)->name, ##__VA_ARGS__)

36
struct bsg_device {
37
	struct request_queue *queue;
38 39
	spinlock_t lock;
	struct hlist_node dev_list;
40
	refcount_t ref_count;
41
	char name[20];
42 43 44
	int max_queue;
};

Jens Axboe's avatar
Jens Axboe committed
45
#define BSG_DEFAULT_CMDS	64
46
#define BSG_MAX_DEVS		32768
47 48

static DEFINE_MUTEX(bsg_mutex);
49
static DEFINE_IDR(bsg_minor_idr);
50

51 52
#define BSG_LIST_ARRAY_SIZE	8
static struct hlist_head bsg_device_list[BSG_LIST_ARRAY_SIZE];
53 54

static struct class *bsg_class;
55
static int bsg_major;
56

57
static inline struct hlist_head *bsg_dev_idx_hash(int index)
58
{
59
	return &bsg_device_list[index & (BSG_LIST_ARRAY_SIZE - 1)];
60 61
}

62 63 64 65 66 67 68 69 70 71 72 73
#define uptr64(val) ((void __user *)(uintptr_t)(val))

static int bsg_scsi_check_proto(struct sg_io_v4 *hdr)
{
	if (hdr->protocol != BSG_PROTOCOL_SCSI  ||
	    hdr->subprotocol != BSG_SUB_PROTOCOL_SCSI_CMD)
		return -EINVAL;
	return 0;
}

static int bsg_scsi_fill_hdr(struct request *rq, struct sg_io_v4 *hdr,
		fmode_t mode)
74
{
75
	struct scsi_request *sreq = scsi_req(rq);
76

77 78 79 80
	sreq->cmd_len = hdr->request_len;
	if (sreq->cmd_len > BLK_MAX_CDB) {
		sreq->cmd = kzalloc(sreq->cmd_len, GFP_KERNEL);
		if (!sreq->cmd)
81 82
			return -ENOMEM;
	}
83

84
	if (copy_from_user(sreq->cmd, uptr64(hdr->request), sreq->cmd_len))
85
		return -EFAULT;
86
	if (blk_verify_command(sreq->cmd, mode))
87 88 89 90
		return -EPERM;
	return 0;
}

91
static int bsg_scsi_complete_rq(struct request *rq, struct sg_io_v4 *hdr)
92
{
93
	struct scsi_request *sreq = scsi_req(rq);
94 95
	int ret = 0;

96 97 98 99 100 101 102 103 104 105
	/*
	 * fill in all the output members
	 */
	hdr->device_status = sreq->result & 0xff;
	hdr->transport_status = host_byte(sreq->result);
	hdr->driver_status = driver_byte(sreq->result);
	hdr->info = 0;
	if (hdr->device_status || hdr->transport_status || hdr->driver_status)
		hdr->info |= SG_INFO_CHECK;
	hdr->response_len = 0;
106

107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
	if (sreq->sense_len && hdr->response) {
		int len = min_t(unsigned int, hdr->max_response_len,
					sreq->sense_len);

		if (copy_to_user(uptr64(hdr->response), sreq->sense, len))
			ret = -EFAULT;
		else
			hdr->response_len = len;
	}

	if (rq->next_rq) {
		hdr->dout_resid = sreq->resid_len;
		hdr->din_resid = scsi_req(rq->next_rq)->resid_len;
	} else if (rq_data_dir(rq) == READ) {
		hdr->din_resid = sreq->resid_len;
	} else {
		hdr->dout_resid = sreq->resid_len;
124
	}
125

126
	return ret;
127 128
}

129 130 131 132 133 134 135 136 137 138 139 140
static void bsg_scsi_free_rq(struct request *rq)
{
	scsi_req_free_cmd(scsi_req(rq));
}

static const struct bsg_ops bsg_scsi_ops = {
	.check_proto		= bsg_scsi_check_proto,
	.fill_hdr		= bsg_scsi_fill_hdr,
	.complete_rq		= bsg_scsi_complete_rq,
	.free_rq		= bsg_scsi_free_rq,
};

141
static struct request *
142
bsg_map_hdr(struct request_queue *q, struct sg_io_v4 *hdr, fmode_t mode)
143
{
FUJITA Tomonori's avatar
FUJITA Tomonori committed
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 151
	if (hdr->guard != 'Q')
		return ERR_PTR(-EINVAL);
152

153
	ret = q->bsg_dev.ops->check_proto(hdr);
154 155 156
	if (ret)
		return ERR_PTR(ret);

157
	rq = blk_get_request(q, hdr->dout_xfer_len ?
158
			REQ_OP_SCSI_OUT : REQ_OP_SCSI_IN, 0);
159 160
	if (IS_ERR(rq))
		return rq;
161

162
	ret = q->bsg_dev.ops->fill_hdr(rq, hdr, mode);
FUJITA Tomonori's avatar
FUJITA Tomonori committed
163 164 165
	if (ret)
		goto out;

166 167 168 169 170 171 172 173 174
	rq->timeout = msecs_to_jiffies(hdr->timeout);
	if (!rq->timeout)
		rq->timeout = q->sg_timeout;
	if (!rq->timeout)
		rq->timeout = BLK_DEFAULT_SG_TIMEOUT;
	if (rq->timeout < BLK_MIN_SG_TIMEOUT)
		rq->timeout = BLK_MIN_SG_TIMEOUT;

	if (hdr->dout_xfer_len && hdr->din_xfer_len) {
FUJITA Tomonori's avatar
FUJITA Tomonori committed
175 176 177 178 179
		if (!test_bit(QUEUE_FLAG_BIDI, &q->queue_flags)) {
			ret = -EOPNOTSUPP;
			goto out;
		}

180 181 182 183
		pr_warn_once(
			"BIDI support in bsg has been deprecated and might be removed. "
			"Please report your use case to linux-scsi@vger.kernel.org\n");

184
		next_rq = blk_get_request(q, REQ_OP_SCSI_IN, 0);
185 186
		if (IS_ERR(next_rq)) {
			ret = PTR_ERR(next_rq);
FUJITA Tomonori's avatar
FUJITA Tomonori committed
187 188 189
			goto out;
		}

190 191
		rq->next_rq = next_rq;
		ret = blk_rq_map_user(q, next_rq, NULL, uptr64(hdr->din_xferp),
192
				       hdr->din_xfer_len, GFP_KERNEL);
FUJITA Tomonori's avatar
FUJITA Tomonori committed
193
		if (ret)
194
			goto out_free_nextrq;
195 196
	}

197
	if (hdr->dout_xfer_len) {
198 199
		ret = blk_rq_map_user(q, rq, NULL, uptr64(hdr->dout_xferp),
				hdr->dout_xfer_len, GFP_KERNEL);
200
	} else if (hdr->din_xfer_len) {
201 202
		ret = blk_rq_map_user(q, rq, NULL, uptr64(hdr->din_xferp),
				hdr->din_xfer_len, GFP_KERNEL);
203
	}
204

205 206
	if (ret)
		goto out_unmap_nextrq;
207
	return rq;
208 209 210 211 212 213 214

out_unmap_nextrq:
	if (rq->next_rq)
		blk_rq_unmap_user(rq->next_rq->bio);
out_free_nextrq:
	if (rq->next_rq)
		blk_put_request(rq->next_rq);
FUJITA Tomonori's avatar
FUJITA Tomonori committed
215
out:
216
	q->bsg_dev.ops->free_rq(rq);
FUJITA Tomonori's avatar
FUJITA Tomonori committed
217 218
	blk_put_request(rq);
	return ERR_PTR(ret);
219 220
}

221
static int blk_complete_sgv4_hdr_rq(struct request *rq, struct sg_io_v4 *hdr,
FUJITA Tomonori's avatar
FUJITA Tomonori committed
222
				    struct bio *bio, struct bio *bidi_bio)
223
{
224
	int ret;
225

226
	ret = rq->q->bsg_dev.ops->complete_rq(rq, hdr);
227

FUJITA Tomonori's avatar
FUJITA Tomonori committed
228 229 230
	if (rq->next_rq) {
		blk_rq_unmap_user(bidi_bio);
		blk_put_request(rq->next_rq);
231
	}
232

233
	blk_rq_unmap_user(bio);
234
	rq->q->bsg_dev.ops->free_rq(rq);
235 236 237 238
	blk_put_request(rq);
	return ret;
}

239 240 241 242 243 244 245 246 247
static struct bsg_device *bsg_alloc_device(void)
{
	struct bsg_device *bd;

	bd = kzalloc(sizeof(struct bsg_device), GFP_KERNEL);
	if (unlikely(!bd))
		return NULL;

	spin_lock_init(&bd->lock);
Jens Axboe's avatar
Jens Axboe committed
248
	bd->max_queue = BSG_DEFAULT_CMDS;
249 250 251 252 253 254
	INIT_HLIST_NODE(&bd->dev_list);
	return bd;
}

static int bsg_put_device(struct bsg_device *bd)
{
255
	struct request_queue *q = bd->queue;
256 257 258

	mutex_lock(&bsg_mutex);

259
	if (!refcount_dec_and_test(&bd->ref_count)) {
260
		mutex_unlock(&bsg_mutex);
261
		return 0;
262 263 264 265
	}

	hlist_del(&bd->dev_list);
	mutex_unlock(&bsg_mutex);
266

267
	bsg_dbg(bd, "tearing down\n");
268 269 270 271

	/*
	 * close can always block
	 */
Jens Axboe's avatar
Jens Axboe committed
272
	kfree(bd);
273 274
	blk_put_queue(q);
	return 0;
275 276 277
}

static struct bsg_device *bsg_add_device(struct inode *inode,
278
					 struct request_queue *rq,
279 280
					 struct file *file)
{
281
	struct bsg_device *bd;
282
	unsigned char buf[32];
283

284 285
	lockdep_assert_held(&bsg_mutex);

286
	if (!blk_get_queue(rq))
287
		return ERR_PTR(-ENXIO);
288 289

	bd = bsg_alloc_device();
290 291
	if (!bd) {
		blk_put_queue(rq);
292
		return ERR_PTR(-ENOMEM);
293
	}
294

295
	bd->queue = rq;
296

297
	refcount_set(&bd->ref_count, 1);
298
	hlist_add_head(&bd->dev_list, bsg_dev_idx_hash(iminor(inode)));
299

300
	strncpy(bd->name, dev_name(rq->bsg_dev.class_dev), sizeof(bd->name) - 1);
301
	bsg_dbg(bd, "bound to <%s>, max queue %d\n",
FUJITA Tomonori's avatar
FUJITA Tomonori committed
302
		format_dev_t(buf, inode->i_rdev), bd->max_queue);
303 304 305 306

	return bd;
}

307
static struct bsg_device *__bsg_get_device(int minor, struct request_queue *q)
308
{
309
	struct bsg_device *bd;
310

311
	lockdep_assert_held(&bsg_mutex);
312

313
	hlist_for_each_entry(bd, bsg_dev_idx_hash(minor), dev_list) {
314
		if (bd->queue == q) {
315
			refcount_inc(&bd->ref_count);
316
			goto found;
317 318
		}
	}
319 320
	bd = NULL;
found:
321 322 323 324 325
	return bd;
}

static struct bsg_device *bsg_get_device(struct inode *inode, struct file *file)
{
326 327
	struct bsg_device *bd;
	struct bsg_class_device *bcd;
328 329 330 331 332

	/*
	 * find the class device
	 */
	mutex_lock(&bsg_mutex);
333
	bcd = idr_find(&bsg_minor_idr, iminor(inode));
334

335 336 337 338
	if (!bcd) {
		bd = ERR_PTR(-ENODEV);
		goto out_unlock;
	}
339

340
	bd = __bsg_get_device(iminor(inode), bcd->queue);
341 342
	if (!bd)
		bd = bsg_add_device(inode, bcd->queue, file);
343

344 345
out_unlock:
	mutex_unlock(&bsg_mutex);
346
	return bd;
347 348 349 350
}

static int bsg_open(struct inode *inode, struct file *file)
{
351 352 353
	struct bsg_device *bd;

	bd = bsg_get_device(inode, file);
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369

	if (IS_ERR(bd))
		return PTR_ERR(bd);

	file->private_data = bd;
	return 0;
}

static int bsg_release(struct inode *inode, struct file *file)
{
	struct bsg_device *bd = file->private_data;

	file->private_data = NULL;
	return bsg_put_device(bd);
}

370
static long bsg_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
371 372 373
{
	struct bsg_device *bd = file->private_data;
	int __user *uarg = (int __user *) arg;
374
	int ret;
375 376 377 378 379 380 381

	switch (cmd) {
		/*
		 * our own ioctls
		 */
	case SG_GET_COMMAND_Q:
		return put_user(bd->max_queue, uarg);
Jens Axboe's avatar
Jens Axboe committed
382
	case SG_SET_COMMAND_Q: {
383 384 385 386
		int queue;

		if (get_user(queue, uarg))
			return -EFAULT;
Jens Axboe's avatar
Jens Axboe committed
387
		if (queue < 1)
388 389
			return -EINVAL;

Jens Axboe's avatar
Jens Axboe committed
390
		spin_lock_irq(&bd->lock);
391
		bd->max_queue = queue;
Jens Axboe's avatar
Jens Axboe committed
392
		spin_unlock_irq(&bd->lock);
393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408
		return 0;
	}

	/*
	 * SCSI/sg ioctls
	 */
	case SG_GET_VERSION_NUM:
	case SCSI_IOCTL_GET_IDLUN:
	case SCSI_IOCTL_GET_BUS_NUMBER:
	case SG_SET_TIMEOUT:
	case SG_GET_TIMEOUT:
	case SG_GET_RESERVED_SIZE:
	case SG_SET_RESERVED_SIZE:
	case SG_EMULATED_HOST:
	case SCSI_IOCTL_SEND_COMMAND: {
		void __user *uarg = (void __user *) arg;
409
		return scsi_cmd_ioctl(bd->queue, NULL, file->f_mode, cmd, uarg);
410
	}
411 412
	case SG_IO: {
		struct request *rq;
FUJITA Tomonori's avatar
FUJITA Tomonori committed
413
		struct bio *bio, *bidi_bio = NULL;
414
		struct sg_io_v4 hdr;
415
		int at_head;
416 417 418 419

		if (copy_from_user(&hdr, uarg, sizeof(hdr)))
			return -EFAULT;

420
		rq = bsg_map_hdr(bd->queue, &hdr, file->f_mode);
421 422 423 424
		if (IS_ERR(rq))
			return PTR_ERR(rq);

		bio = rq->bio;
FUJITA Tomonori's avatar
FUJITA Tomonori committed
425 426
		if (rq->next_rq)
			bidi_bio = rq->next_rq->bio;
427 428 429

		at_head = (0 == (hdr.flags & BSG_FLAG_Q_AT_TAIL));
		blk_execute_rq(bd->queue, NULL, rq, at_head);
430
		ret = blk_complete_sgv4_hdr_rq(rq, &hdr, bio, bidi_bio);
431 432 433

		if (copy_to_user(uarg, &hdr, sizeof(hdr)))
			return -EFAULT;
Jens Axboe's avatar
Jens Axboe committed
434

435
		return ret;
436
	}
437 438 439 440 441
	default:
		return -ENOTTY;
	}
}

442
static const struct file_operations bsg_fops = {
443 444
	.open		=	bsg_open,
	.release	=	bsg_release,
445
	.unlocked_ioctl	=	bsg_ioctl,
446
	.owner		=	THIS_MODULE,
447
	.llseek		=	default_llseek,
448 449
};

450
void bsg_unregister_queue(struct request_queue *q)
451
{
452
	struct bsg_class_device *bcd = &q->bsg_dev;
453

454 455
	if (!bcd->class_dev)
		return;
456 457

	mutex_lock(&bsg_mutex);
458
	idr_remove(&bsg_minor_idr, bcd->minor);
459 460
	if (q->kobj.sd)
		sysfs_remove_link(&q->kobj, "bsg");
461
	device_unregister(bcd->class_dev);
462 463 464
	bcd->class_dev = NULL;
	mutex_unlock(&bsg_mutex);
}
FUJITA Tomonori's avatar
FUJITA Tomonori committed
465
EXPORT_SYMBOL_GPL(bsg_unregister_queue);
466

467
int bsg_register_queue(struct request_queue *q, struct device *parent,
468
		const char *name, const struct bsg_ops *ops)
469
{
470
	struct bsg_class_device *bcd;
471
	dev_t dev;
472
	int ret;
473
	struct device *class_dev = NULL;
474 475 476 477

	/*
	 * we need a proper transport to send commands, not a stacked device
	 */
478
	if (!queue_is_mq(q))
479 480
		return 0;

481
	bcd = &q->bsg_dev;
482 483 484
	memset(bcd, 0, sizeof(*bcd));

	mutex_lock(&bsg_mutex);
485

486 487 488 489 490 491
	ret = idr_alloc(&bsg_minor_idr, bcd, 0, BSG_MAX_DEVS, GFP_KERNEL);
	if (ret < 0) {
		if (ret == -ENOSPC) {
			printk(KERN_ERR "bsg: too many bsg devices\n");
			ret = -EINVAL;
		}
492 493 494
		goto unlock;
	}

495
	bcd->minor = ret;
496
	bcd->queue = q;
497
	bcd->ops = ops;
498
	dev = MKDEV(bsg_major, bcd->minor);
499
	class_dev = device_create(bsg_class, parent, dev, NULL, "%s", name);
500 501
	if (IS_ERR(class_dev)) {
		ret = PTR_ERR(class_dev);
502
		goto idr_remove;
503 504 505
	}
	bcd->class_dev = class_dev;

506
	if (q->kobj.sd) {
507 508
		ret = sysfs_create_link(&q->kobj, &bcd->class_dev->kobj, "bsg");
		if (ret)
509
			goto unregister_class_dev;
510 511
	}

512 513
	mutex_unlock(&bsg_mutex);
	return 0;
514

515
unregister_class_dev:
516
	device_unregister(class_dev);
517
idr_remove:
518
	idr_remove(&bsg_minor_idr, bcd->minor);
519
unlock:
520
	mutex_unlock(&bsg_mutex);
521 522
	return ret;
}
523 524 525 526 527 528 529 530

int bsg_scsi_register_queue(struct request_queue *q, struct device *parent)
{
	if (!blk_queue_scsi_passthrough(q)) {
		WARN_ONCE(true, "Attempt to register a non-SCSI queue\n");
		return -EINVAL;
	}

531
	return bsg_register_queue(q, parent, dev_name(parent), &bsg_scsi_ops);
532 533
}
EXPORT_SYMBOL_GPL(bsg_scsi_register_queue);
534

535
static struct cdev bsg_cdev;
536

537
static char *bsg_devnode(struct device *dev, umode_t *mode)
538 539 540 541
{
	return kasprintf(GFP_KERNEL, "bsg/%s", dev_name(dev));
}

542 543 544
static int __init bsg_init(void)
{
	int ret, i;
545
	dev_t devid;
546

547
	for (i = 0; i < BSG_LIST_ARRAY_SIZE; i++)
548 549 550
		INIT_HLIST_HEAD(&bsg_device_list[i]);

	bsg_class = class_create(THIS_MODULE, "bsg");
551 552
	if (IS_ERR(bsg_class))
		return PTR_ERR(bsg_class);
553
	bsg_class->devnode = bsg_devnode;
554

555
	ret = alloc_chrdev_region(&devid, 0, BSG_MAX_DEVS, "bsg");
556 557
	if (ret)
		goto destroy_bsg_class;
558

559 560
	bsg_major = MAJOR(devid);

561
	cdev_init(&bsg_cdev, &bsg_fops);
562
	ret = cdev_add(&bsg_cdev, MKDEV(bsg_major, 0), BSG_MAX_DEVS);
563 564
	if (ret)
		goto unregister_chrdev;
565

566
	printk(KERN_INFO BSG_DESCRIPTION " version " BSG_VERSION
FUJITA Tomonori's avatar
FUJITA Tomonori committed
567
	       " loaded (major %d)\n", bsg_major);
568
	return 0;
569 570 571 572 573
unregister_chrdev:
	unregister_chrdev_region(MKDEV(bsg_major, 0), BSG_MAX_DEVS);
destroy_bsg_class:
	class_destroy(bsg_class);
	return ret;
574 575 576
}

MODULE_AUTHOR("Jens Axboe");
FUJITA Tomonori's avatar
FUJITA Tomonori committed
577
MODULE_DESCRIPTION(BSG_DESCRIPTION);
578 579
MODULE_LICENSE("GPL");

580
device_initcall(bsg_init);