12 #include <linux/module.h>
16 #include <linux/poll.h>
20 #include <linux/uio.h>
22 #include <linux/bsg.h>
23 #include <linux/slab.h>
25 #include <scsi/scsi.h>
32 #define BSG_DESCRIPTION "Block layer SCSI generic (bsg) driver"
33 #define BSG_VERSION "0.4"
55 #define BSG_DEFAULT_CMDS 64
56 #define BSG_MAX_DEVS 32768
61 #define dprintk(fmt, args...) printk(KERN_ERR "%s: " fmt, __func__, ##args)
63 #define dprintk(fmt, args...)
69 #define BSG_LIST_ARRAY_SIZE 8
72 static struct class *bsg_class;
100 spin_unlock_irqrestore(&bd->
lock, flags);
109 spin_lock_irq(&bd->
lock);
115 spin_unlock_irq(&bd->
lock);
117 bc = kmem_cache_zalloc(bsg_cmd_cachep,
GFP_KERNEL);
119 spin_lock_irq(&bd->
lock);
126 INIT_LIST_HEAD(&bc->
list);
127 dprintk(
"%s: returning free cmd %p\n", bd->
name, bc);
130 spin_unlock_irq(&bd->
lock);
139 static int bsg_io_schedule(
struct bsg_device *bd)
144 spin_lock_irq(&bd->
lock);
165 spin_unlock_irq(&bd->
lock);
171 spin_unlock_irq(&bd->
lock);
199 rq->cmd_type = REQ_TYPE_BLOCK_PC;
203 rq->timeout = q->sg_timeout;
205 rq->timeout = BLK_DEFAULT_SG_TIMEOUT;
206 if (rq->timeout < BLK_MIN_SG_TIMEOUT)
207 rq->timeout = BLK_MIN_SG_TIMEOUT;
220 if (hdr->
guard !=
'Q')
251 unsigned int dxfer_len;
253 struct bsg_class_device *bcd = &q->bsg_dev;
260 return ERR_PTR(-
ENXIO);
266 ret = bsg_validate_sgv4_hdr(q, hdr, &rw);
276 ret = blk_fill_sgv4_hdr_rq(q, rq, hdr, bd, has_write_perm);
281 if (!
test_bit(QUEUE_FLAG_BIDI, &q->queue_flags)) {
291 rq->next_rq = next_rq;
292 next_rq->cmd_type = rq->cmd_type;
322 if (rq->cmd != rq->__cmd)
336 static void bsg_rq_end_io(
struct request *rq,
int uptodate)
342 dprintk(
"%s: finished rq %p bc %p, bio %p stat %d\n",
343 bd->
name, rq, bc, bc->
bio, uptodate);
350 spin_unlock_irqrestore(&bd->
lock, flags);
372 spin_lock_irq(&bd->
lock);
374 spin_unlock_irq(&bd->
lock);
376 dprintk(
"%s: queueing rq %p, bc %p\n", bd->
name, rq, bc);
378 rq->end_io_data =
bc;
386 spin_lock_irq(&bd->
lock);
392 spin_unlock_irq(&bd->
lock);
406 bc = bsg_next_done_cmd(bd);
427 static int blk_complete_sgv4_hdr_rq(
struct request *rq,
struct sg_io_v4 *hdr,
432 dprintk(
"rq %p bio %p 0x%x\n", rq, bio, rq->errors);
444 if (rq->sense_len && hdr->
response) {
461 }
else if (rq_data_dir(rq) ==
READ)
472 if (!ret && rq->errors < 0)
476 if (rq->cmd != rq->__cmd)
483 static int bsg_complete_all_commands(
struct bsg_device *bd)
495 ret = bsg_io_schedule(bd);
510 spin_lock_irq(&bd->
lock);
512 spin_unlock_irq(&bd->
lock);
515 spin_unlock_irq(&bd->
lock);
517 bc = bsg_get_done_cmd(bd);
521 tret = blk_complete_sgv4_hdr_rq(bc->
rq, &bc->
hdr, bc->
bio,
526 bsg_free_command(bc);
537 int nr_commands,
ret;
539 if (count %
sizeof(
struct sg_io_v4))
543 nr_commands = count /
sizeof(
struct sg_io_v4);
544 while (nr_commands) {
545 bc = bsg_get_done_cmd(bd);
556 ret = blk_complete_sgv4_hdr_rq(bc->
rq, &bc->
hdr, bc->
bio,
562 bsg_free_command(bc);
568 *bytes_read +=
sizeof(
struct sg_io_v4);
586 static inline int err_block_err(
int ret)
595 bsg_read(
struct file *file,
char __user *buf,
size_t count, loff_t *ppos)
603 bsg_set_block(bd, file);
606 ret = __bsg_read(buf, count, bd,
NULL, &bytes_read);
609 if (!bytes_read || err_block_err(ret))
615 static int __bsg_write(
struct bsg_device *bd,
const char __user *buf,
616 size_t count,
ssize_t *bytes_written,
621 int ret, nr_commands;
623 if (count %
sizeof(
struct sg_io_v4))
626 nr_commands = count /
sizeof(
struct sg_io_v4);
630 while (nr_commands) {
633 bc = bsg_alloc_command(bd);
648 rq = bsg_map_hdr(bd, &bc->
hdr, has_write_perm, bc->
sense);
655 bsg_add_command(bd, q, bc, rq);
660 *bytes_written +=
sizeof(
struct sg_io_v4);
664 bsg_free_command(bc);
670 bsg_write(
struct file *file,
const char __user *buf,
size_t count, loff_t *ppos)
678 bsg_set_block(bd, file);
681 ret = __bsg_write(bd, buf, count, &bytes_written,
684 *ppos = bytes_written;
689 if (!bytes_written || err_block_err(ret))
692 dprintk(
"%s: returning %Zd\n", bd->
name, bytes_written);
693 return bytes_written;
696 static struct bsg_device *bsg_alloc_device(
void)
717 static void bsg_kref_release_function(
struct kref *
kref)
719 struct bsg_class_device *bcd =
724 bcd->release(bcd->parent);
729 static int bsg_put_device(
struct bsg_device *bd)
731 int ret = 0, do_free;
757 ret = bsg_complete_all_commands(bd);
761 kref_put(&q->bsg_dev.ref, bsg_kref_release_function);
773 unsigned char buf[32];
776 return ERR_PTR(-
ENXIO);
778 bd = bsg_alloc_device();
786 bsg_set_block(bd, file);
790 hlist_add_head(&bd->
dev_list, bsg_dev_idx_hash(iminor(inode)));
792 strncpy(bd->
name, dev_name(rq->bsg_dev.class_dev),
sizeof(bd->
name) - 1);
793 dprintk(
"bound to <%s>, max queue %d\n",
808 if (bd->
queue == q) {
819 static struct bsg_device *bsg_get_device(
struct inode *inode,
struct file *file)
822 struct bsg_class_device *bcd;
828 bcd =
idr_find(&bsg_minor_idr, iminor(inode));
836 bd = __bsg_get_device(iminor(inode), bcd->queue);
840 bd = bsg_add_device(inode, bcd->queue, file);
842 kref_put(&bcd->ref, bsg_kref_release_function);
847 static int bsg_open(
struct inode *inode,
struct file *file)
851 bd = bsg_get_device(inode, file);
860 static int bsg_release(
struct inode *inode,
struct file *file)
865 return bsg_put_device(bd);
871 unsigned int mask = 0;
873 poll_wait(file, &bd->
wq_done, wait);
874 poll_wait(file, &bd->
wq_free, wait);
876 spin_lock_irq(&bd->
lock);
881 spin_unlock_irq(&bd->
lock);
886 static long bsg_ioctl(
struct file *file,
unsigned int cmd,
unsigned long arg)
906 spin_lock_irq(&bd->
lock);
908 spin_unlock_irq(&bd->
lock);
929 struct bio *bio, *bidi_bio =
NULL;
943 bidi_bio = rq->next_rq->bio;
947 ret = blk_complete_sgv4_hdr_rq(rq, &hdr, bio, bidi_bio);
971 .release = bsg_release,
972 .unlocked_ioctl = bsg_ioctl,
979 struct bsg_class_device *bcd = &q->bsg_dev;
989 bcd->class_dev =
NULL;
990 kref_put(&bcd->ref, bsg_kref_release_function);
998 struct bsg_class_device *bcd;
1002 const char *devname;
1007 devname = dev_name(parent);
1016 memset(bcd, 0,
sizeof(*bcd));
1040 kref_init(&bcd->ref);
1041 dev =
MKDEV(bsg_major, bcd->minor);
1043 if (IS_ERR(class_dev)) {
1044 ret = PTR_ERR(class_dev);
1047 bcd->class_dev = class_dev;
1052 goto unregister_class_dev;
1058 unregister_class_dev:
1070 static struct cdev bsg_cdev;
1077 static int __init bsg_init(
void)
1084 if (!bsg_cmd_cachep) {
1093 if (IS_ERR(bsg_class)) {
1094 ret = PTR_ERR(bsg_class);
1095 goto destroy_kmemcache;
1097 bsg_class->
devnode = bsg_devnode;
1101 goto destroy_bsg_class;
1103 bsg_major =
MAJOR(devid);
1108 goto unregister_chrdev;
1111 " loaded (major %d)\n", bsg_major);