scsi_dh: Implement generic device table handling
Subsystems (2)
PATCH 1/5 scsi_dh: Implement generic device table handling
Instead of having each and every driver maintaining and scanning their own device table we should rather have a general list and just add any new entries from the device handlers. Plus we now just have a general callback instead of several for each handler.
Signed-off-by Hannes Reinecke <[email protected]>
--- drivers/scsi/device_handler/scsi_dh.c | 176 +++++++++++++++++++++++++-- drivers/scsi/device_handler/scsi_dh_emc.c | 102 +++++++--------- drivers/scsi/device_handler/scsi_dh_hp_sw.c | 89 ++++++-------- drivers/scsi/device_handler/scsi_dh_rdac.c | 106 +++++++---------- include/scsi/scsi_device.h | 9 ++- 5 files changed, 302 insertions(+), 180 deletions(-)
drivers/scsi/device_handler/scsi_dh.c +167 -9
@@ -26,6 +26,18 @@static DEFINE_SPINLOCK (list_lock);static LIST_HEAD (scsi_dh_list); +static LIST_HEAD (scsi_dh_dev_list); + +struct scsi_dh_devinfo_list { +struct list_head node; +char vendor[9 ]; +char model[17 ]; +struct scsi_device_handler *handler; +}; + +int scsi_dh_handler_attach (struct scsi_device *sdev, +struct scsi_device_handler *scsi_dh); +int scsi_dh_handler_detach (struct scsi_device *sdev);static struct scsi_device_handler *get_device_handler (const char *name) {
@@ -33,7 +45,7 @@ static struct scsi_device_handler *get_device_handler(const char *name)spin_lock (&list_lock);list_for_each_entry (tmp, &scsi_dh_list, list) { -if (!strcmp (tmp->name, name)) { +if (!strncmp (tmp->name, name,strlen (tmp->name))) { found = tmp;break ; }
@@ -42,11 +54,98 @@ static struct scsi_device_handler *get_device_handler(const char *name)return found; } +/* +* scsi_dh_handler_attach - Attach a device handler to a device +* @sdev - SCSI device the device handler should attach to +* @scsi_dh - The device handler to attach +*/ +int scsi_dh_handler_attach (struct scsi_device *sdev, +struct scsi_device_handler *scsi_dh) +{ +int err = -EBUSY ; + +if (sdev->scsi_dh_data) +return err; + + err = scsi_dh->attach (sdev); + +return err; +} + +/* +* scsi_dh_handler_detach - Detach a device handler to a device +* @sdev - SCSI device the device handler should be detached from +*/ +int scsi_dh_handler_detach (struct scsi_device *sdev) +{ +struct scsi_device_handler *scsi_dh; + +if (!sdev->scsi_dh_data) +return -ENODEV ; + + scsi_dh = sdev->scsi_dh_data->scsi_dh; + + scsi_dh->detach (sdev); + +return 0 ; +} + +static int scsi_dh_notifier (struct notifier_block *nb, +unsigned long action,void *data) +{ +struct device *dev = data; +struct scsi_device *sdev; +struct scsi_dh_devinfo_list *tmp, *devinfo =NULL ; + +if (!scsi_is_sdev_device (dev)) +return 0 ; + + sdev =to_scsi_device (dev); + +list_for_each_entry (tmp, &scsi_dh_dev_list, node) { +if (!strncmp (sdev->vendor, tmp->vendor,strlen (tmp->vendor)) && + !strncmp (sdev->model, tmp->model,strlen (tmp->model))) { + devinfo = tmp; +break ; + } + } + +if (!devinfo) + goto out; + +if (action ==BUS_NOTIFY_ADD_DEVICE ) { +scsi_dh_handler_attach (sdev, devinfo->handler); + }else if (action ==BUS_NOTIFY_DEL_DEVICE ) { +if (sdev->scsi_dh_data ==NULL ) + goto out; +scsi_dh_handler_detach (sdev); + } +out: +return 0 ; +} +static int scsi_dh_notifier_add (struct device *dev,void *data) {struct scsi_device_handler *scsi_dh = data; +struct scsi_device *sdev; +struct scsi_dh_devinfo_list *tmp; + +if (!scsi_is_sdev_device (dev)) +return 0 ; + + sdev =to_scsi_device (dev); + +list_for_each_entry (tmp, &scsi_dh_dev_list, node) { +if (tmp->handler != scsi_dh) +continue ; + +if (!strncmp (sdev->vendor, tmp->vendor,strlen (tmp->vendor)) && + !strncmp (sdev->model, tmp->model,strlen (tmp->model))) { +scsi_dh_handler_attach (sdev, scsi_dh); +break ; + } + } - scsi_dh->nb.notifier_call (&scsi_dh->nb,BUS_NOTIFY_ADD_DEVICE , dev);return 0 ; }
@@ -60,18 +159,37 @@ static int scsi_dh_notifier_add(struct device *dev, void *data)int scsi_register_device_handler (struct scsi_device_handler *scsi_dh) {int ret = -EBUSY ; +int i;struct scsi_device_handler *tmp; +struct scsi_dh_devinfo_list *devinfo; tmp =get_device_handler (scsi_dh->name);if (tmp) goto done; - ret =bus_register_notifier (&scsi_bus_type, &scsi_dh->nb); - -bus_for_each_dev (&scsi_bus_type,NULL , scsi_dh, scsi_dh_notifier_add);spin_lock (&list_lock); +for (i =0 ; scsi_dh->devlist[i].vendor; i++) { + devinfo =kmalloc (sizeof (*devinfo),GFP_KERNEL ); +if (!devinfo) { +printk (KERN_ERR "%s: no memory\n" , __FUNCTION__); + ret = -ENOMEM ; + goto done; + } +strncpy (devinfo->vendor, scsi_dh->devlist[i].vendor,8 ); +strncpy (devinfo->model, scsi_dh->devlist[i].model,16 ); + devinfo->vendor[8 ] ='\0' ; + devinfo->model[16 ] ='\0' ; + devinfo->handler = scsi_dh; +list_add (&devinfo->node, &scsi_dh_dev_list); +printk (KERN_INFO "%s: registered device '%s/%s'\n" , + scsi_dh->name, devinfo->vendor, devinfo->model); + } +list_add (&scsi_dh->list, &scsi_dh_list);spin_unlock (&list_lock); +bus_for_each_dev (&scsi_bus_type,NULL , scsi_dh, scsi_dh_notifier_add); +printk (KERN_INFO "%s: registered\n" , scsi_dh->name); + ret =SCSI_DH_OK ; done:return ret;
@@ -81,8 +199,18 @@ EXPORT_SYMBOL_GPL(scsi_register_device_handler);static int scsi_dh_notifier_remove (struct device *dev,void *data) {struct scsi_device_handler *scsi_dh = data; +struct scsi_device *sdev; + +if (!scsi_is_sdev_device (dev)) +return 0 ; + + sdev =to_scsi_device (dev); + +if (!sdev->scsi_dh_data || sdev->scsi_dh_data->scsi_dh != scsi_dh) +return 0 ; + +scsi_dh_handler_detach (sdev); - scsi_dh->nb.notifier_call (&scsi_dh->nb,BUS_NOTIFY_DEL_DEVICE , dev);return 0 ; }
@@ -97,18 +225,27 @@ int scsi_unregister_device_handler(struct scsi_device_handler *scsi_dh) {int ret = -ENODEV ;struct scsi_device_handler *tmp; +struct scsi_dh_devinfo_list *devinfo, *tmpdev; tmp =get_device_handler (scsi_dh->name);if (!tmp) goto done; - ret =bus_unregister_notifier (&scsi_bus_type, &scsi_dh->nb); -bus_for_each_dev (&scsi_bus_type,NULL , scsi_dh, - scsi_dh_notifier_remove); + scsi_dh_notifier_remove); +spin_lock (&list_lock); +list_for_each_entry_safe (devinfo, tmpdev, &scsi_dh_dev_list, node) { +if (devinfo->handler != scsi_dh) +continue ; + +list_del_init (&devinfo->node); +kfree (devinfo); + }list_del (&scsi_dh->list);spin_unlock (&list_lock); +printk (KERN_INFO "%s: unregistered\n" , scsi_dh->name); + ret =SCSI_DH_OK ; done:return ret;
@@ -157,6 +294,27 @@ int scsi_dh_handler_exist(const char *name) }EXPORT_SYMBOL_GPL (scsi_dh_handler_exist); +static struct notifier_block scsi_dh_nb = { + .notifier_call = scsi_dh_notifier +}; + +static int __init scsi_dh_init (void ) +{ +int r; + + r =bus_register_notifier (&scsi_bus_type, &scsi_dh_nb); + +return r; +} + +static void __exit scsi_dh_exit(void) +{ +bus_unregister_notifier (&scsi_bus_type, &scsi_dh_nb); +} + +module_init (scsi_dh_init); +module_exit (scsi_dh_exit); +MODULE_DESCRIPTION ("SCSI device handler" );MODULE_AUTHOR ("Chandra Seetharaman <[email protected]>" );MODULE_LICENSE ("GPL" );
drivers/scsi/device_handler/scsi_dh_emc.c +46 -56
142 lines not on this page · raw message
drivers/scsi/device_handler/scsi_dh_hp_sw.c +38 -51
118 lines not on this page · raw message
drivers/scsi/device_handler/scsi_dh_rdac.c +43 -63
137 lines not on this page · raw message
include/scsi/scsi_device.h +8 -1
@@ -166,15 +166,22 @@ struct scsi_device {unsigned long sdev_data[0 ]; } __attribute__((aligned(sizeof(unsigned long)))); +struct scsi_dh_devlist { +char *vendor; +char *model; +}; +struct scsi_device_handler {/* Used by the infrastructure */ struct list_head list;/* list of scsi_device_handlers */ -struct notifier_block nb;/* Filled by the hardware handler */ struct module *module;const char *name; +const struct scsi_dh_devlist *devlist;int (*check_sense)(struct scsi_device *,struct scsi_sense_hdr *); +int (*attach)(struct scsi_device *); +void (*detach)(struct scsi_device *);int (*activate)(struct scsi_device *);int (*prep_fn)(struct scsi_device *,struct request *); };
Signature · 1 line
1.5.2.4