Commit 4e2872d6b0252d33f28ea67f33704208ca781978

Authored by FUJITA Tomonori
Committed by Jens Axboe
1 parent d351af01b9

bind bsg to all SCSI devices

This patch binds bsg to all SCSI devices (their request queues) like
the current sg driver does. We can send SCSI commands to non disk and
cdrom scsi devices like OSD via bsg.

This patch removes bsg_register_queue from blk_register_queue so bsg
devices aren't bound to non SCSI block devices. If they want bsg, I'll
send a patch to do that.

Signed-off-by: FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>
Signed-off-by: Jens Axboe <jens.axboe@oracle.com>

Showing 2 changed files with 52 additions and 15 deletions Side-by-side Diff

... ... @@ -29,6 +29,8 @@
29 29 #include <scsi/scsi.h>
30 30 #include <scsi/scsi_ioctl.h>
31 31 #include <scsi/scsi_cmnd.h>
  32 +#include <scsi/scsi_device.h>
  33 +#include <scsi/scsi_driver.h>
32 34 #include <scsi/sg.h>
33 35  
34 36 static char bsg_version[] = "block layer sg (bsg) 0.4";
... ... @@ -962,6 +964,8 @@
962 964 {
963 965 struct bsg_class_device *bcd;
964 966 dev_t dev;
  967 + int ret;
  968 + struct class_device *class_dev = NULL;
965 969  
966 970 /*
967 971 * we need a proper transport to send commands, not a stacked device
968 972  
969 973  
970 974  
971 975  
972 976  
... ... @@ -978,22 +982,54 @@
978 982 bcd->minor = bsg_device_nr;
979 983 bsg_device_nr++;
980 984 bcd->queue = q;
981   - bcd->class_dev = class_device_create(bsg_class, NULL, dev, bcd->dev, "%s", name);
982   - if (!bcd->class_dev)
  985 + class_dev = class_device_create(bsg_class, NULL, dev, bcd->dev, "%s", name);
  986 + if (IS_ERR(class_dev)) {
  987 + ret = PTR_ERR(class_dev);
983 988 goto err;
  989 + }
  990 + bcd->class_dev = class_dev;
  991 +
  992 + if (q->kobj.dentry) {
  993 + ret = sysfs_create_link(&q->kobj, &bcd->class_dev->kobj, "bsg");
  994 + if (ret)
  995 + goto err;
  996 + }
  997 +
984 998 list_add_tail(&bcd->list, &bsg_class_list);
985   - if (sysfs_create_link(&q->kobj, &bcd->class_dev->kobj, "bsg"))
986   - goto err;
  999 +
987 1000 mutex_unlock(&bsg_mutex);
988 1001 return 0;
989 1002 err:
990 1003 bsg_device_nr--;
991   - if (bcd->class_dev)
  1004 + if (class_dev)
992 1005 class_device_destroy(bsg_class, MKDEV(BSG_MAJOR, bcd->minor));
993 1006 mutex_unlock(&bsg_mutex);
994   - return -ENOMEM;
  1007 + return ret;
995 1008 }
996 1009  
  1010 +static int bsg_add(struct class_device *cl_dev, struct class_interface *cl_intf)
  1011 +{
  1012 + int ret;
  1013 + struct scsi_device *sdp = to_scsi_device(cl_dev->dev);
  1014 + struct request_queue *rq = sdp->request_queue;
  1015 +
  1016 + if (rq->kobj.parent)
  1017 + ret = bsg_register_queue(rq, kobject_name(rq->kobj.parent));
  1018 + else
  1019 + ret = bsg_register_queue(rq, kobject_name(&sdp->sdev_gendev.kobj));
  1020 + return ret;
  1021 +}
  1022 +
  1023 +static void bsg_remove(struct class_device *cl_dev, struct class_interface *cl_intf)
  1024 +{
  1025 + bsg_unregister_queue(to_scsi_device(cl_dev->dev)->request_queue);
  1026 +}
  1027 +
  1028 +static struct class_interface bsg_intf = {
  1029 + .add = bsg_add,
  1030 + .remove = bsg_remove,
  1031 +};
  1032 +
997 1033 static int __init bsg_init(void)
998 1034 {
999 1035 int ret, i;
... ... @@ -1021,6 +1057,15 @@
1021 1057 return ret;
1022 1058 }
1023 1059  
  1060 + ret = scsi_register_interface(&bsg_intf);
  1061 + if (ret) {
  1062 + printk(KERN_ERR "bsg: failed register scsi interface %d\n", ret);
  1063 + kmem_cache_destroy(bsg_cmd_cachep);
  1064 + class_destroy(bsg_class);
  1065 + unregister_chrdev(BSG_MAJOR, "bsg");
  1066 + return ret;
  1067 + }
  1068 +
1024 1069 printk(KERN_INFO "%s loaded\n", bsg_version);
1025 1070 return 0;
1026 1071 }
... ... @@ -1029,5 +1074,5 @@
1029 1074 MODULE_DESCRIPTION("Block layer SGSI generic (sg) driver");
1030 1075 MODULE_LICENSE("GPL");
1031 1076  
1032   -subsys_initcall(bsg_init);
  1077 +device_initcall(bsg_init);
... ... @@ -4091,13 +4091,6 @@
4091 4091 return ret;
4092 4092 }
4093 4093  
4094   - ret = bsg_register_queue(q, disk->disk_name);
4095   - if (ret) {
4096   - elv_unregister_queue(q);
4097   - kobject_unregister(&q->kobj);
4098   - return ret;
4099   - }
4100   -
4101 4094 return 0;
4102 4095 }
4103 4096  
... ... @@ -4106,7 +4099,6 @@
4106 4099 request_queue_t *q = disk->queue;
4107 4100  
4108 4101 if (q && q->request_fn) {
4109   - bsg_unregister_queue(q);
4110 4102 elv_unregister_queue(q);
4111 4103  
4112 4104 kobject_uevent(&q->kobj, KOBJ_REMOVE);