Commit 1d520328 authored by Christoph Hellwig's avatar Christoph Hellwig

scsi: handle more device handler setup/teardown in common code

Move all code to set up and tear down sdev->scsi_dh_data to common code.
Signed-off-by: default avatarChristoph Hellwig <hch@lst.de>
Reviewed-by: default avatarMike Christie <michaelc@cs.wisc.edu>
Reviewed-by: default avatarHannes Reinecke <hare@suse.de>
parent 1f12ffa5
...@@ -98,7 +98,7 @@ device_handler_match(struct scsi_device_handler *scsi_dh, ...@@ -98,7 +98,7 @@ device_handler_match(struct scsi_device_handler *scsi_dh,
static int scsi_dh_handler_attach(struct scsi_device *sdev, static int scsi_dh_handler_attach(struct scsi_device *sdev,
struct scsi_device_handler *scsi_dh) struct scsi_device_handler *scsi_dh)
{ {
int err = 0; struct scsi_dh_data *d;
if (sdev->scsi_dh_data) { if (sdev->scsi_dh_data) {
if (sdev->scsi_dh_data->scsi_dh != scsi_dh) if (sdev->scsi_dh_data->scsi_dh != scsi_dh)
...@@ -111,15 +111,22 @@ static int scsi_dh_handler_attach(struct scsi_device *sdev, ...@@ -111,15 +111,22 @@ static int scsi_dh_handler_attach(struct scsi_device *sdev,
if (!try_module_get(scsi_dh->module)) if (!try_module_get(scsi_dh->module))
return -EINVAL; return -EINVAL;
err = scsi_dh->attach(sdev); d = scsi_dh->attach(sdev);
if (err) { if (IS_ERR(d)) {
sdev_printk(KERN_ERR, sdev, "%s: Attach failed (%ld)\n",
scsi_dh->name, PTR_ERR(d));
module_put(scsi_dh->module); module_put(scsi_dh->module);
return err; return PTR_ERR(d);
} }
kref_init(&sdev->scsi_dh_data->kref); d->scsi_dh = scsi_dh;
sdev->scsi_dh_data->sdev = sdev; kref_init(&d->kref);
return err; d->sdev = sdev;
spin_lock_irq(sdev->request_queue->queue_lock);
sdev->scsi_dh_data = d;
spin_unlock_irq(sdev->request_queue->queue_lock);
return 0;
} }
static void __detach_handler (struct kref *kref) static void __detach_handler (struct kref *kref)
...@@ -127,8 +134,14 @@ static void __detach_handler (struct kref *kref) ...@@ -127,8 +134,14 @@ static void __detach_handler (struct kref *kref)
struct scsi_dh_data *scsi_dh_data = struct scsi_dh_data *scsi_dh_data =
container_of(kref, struct scsi_dh_data, kref); container_of(kref, struct scsi_dh_data, kref);
struct scsi_device_handler *scsi_dh = scsi_dh_data->scsi_dh; struct scsi_device_handler *scsi_dh = scsi_dh_data->scsi_dh;
struct scsi_device *sdev = scsi_dh_data->sdev;
spin_lock_irq(sdev->request_queue->queue_lock);
sdev->scsi_dh_data = NULL;
spin_unlock_irq(sdev->request_queue->queue_lock);
scsi_dh->detach(scsi_dh_data->sdev); scsi_dh->detach(sdev);
sdev_printk(KERN_NOTICE, sdev, "%s: Detached\n", scsi_dh->name);
module_put(scsi_dh->module); module_put(scsi_dh->module);
} }
......
...@@ -824,39 +824,18 @@ static bool alua_match(struct scsi_device *sdev) ...@@ -824,39 +824,18 @@ static bool alua_match(struct scsi_device *sdev)
return (scsi_device_tpgs(sdev) != 0); return (scsi_device_tpgs(sdev) != 0);
} }
static int alua_bus_attach(struct scsi_device *sdev);
static void alua_bus_detach(struct scsi_device *sdev);
static struct scsi_device_handler alua_dh = {
.name = ALUA_DH_NAME,
.module = THIS_MODULE,
.attach = alua_bus_attach,
.detach = alua_bus_detach,
.prep_fn = alua_prep_fn,
.check_sense = alua_check_sense,
.activate = alua_activate,
.set_params = alua_set_params,
.match = alua_match,
};
/* /*
* alua_bus_attach - Attach device handler * alua_bus_attach - Attach device handler
* @sdev: device to be attached to * @sdev: device to be attached to
*/ */
static int alua_bus_attach(struct scsi_device *sdev) static struct scsi_dh_data *alua_bus_attach(struct scsi_device *sdev)
{ {
struct alua_dh_data *h; struct alua_dh_data *h;
unsigned long flags; int err;
int err = SCSI_DH_OK;
h = kzalloc(sizeof(*h) , GFP_KERNEL); h = kzalloc(sizeof(*h) , GFP_KERNEL);
if (!h) { if (!h)
sdev_printk(KERN_ERR, sdev, "%s: Attach failed\n", return ERR_PTR(-ENOMEM);
ALUA_DH_NAME);
return -ENOMEM;
}
h->dh_data.scsi_dh = &alua_dh;
h->tpgs = TPGS_MODE_UNINITIALIZED; h->tpgs = TPGS_MODE_UNINITIALIZED;
h->state = TPGS_STATE_OPTIMIZED; h->state = TPGS_STATE_OPTIMIZED;
h->group_id = -1; h->group_id = -1;
...@@ -866,20 +845,14 @@ static int alua_bus_attach(struct scsi_device *sdev) ...@@ -866,20 +845,14 @@ static int alua_bus_attach(struct scsi_device *sdev)
h->sdev = sdev; h->sdev = sdev;
err = alua_initialize(sdev, h); err = alua_initialize(sdev, h);
if ((err != SCSI_DH_OK) && (err != SCSI_DH_DEV_OFFLINED)) if (err != SCSI_DH_OK && err != SCSI_DH_DEV_OFFLINED)
goto failed; goto failed;
spin_lock_irqsave(sdev->request_queue->queue_lock, flags);
sdev->scsi_dh_data = &h->dh_data;
spin_unlock_irqrestore(sdev->request_queue->queue_lock, flags);
sdev_printk(KERN_NOTICE, sdev, "%s: Attached\n", ALUA_DH_NAME); sdev_printk(KERN_NOTICE, sdev, "%s: Attached\n", ALUA_DH_NAME);
return &h->dh_data;
return 0;
failed: failed:
kfree(h); kfree(h);
sdev_printk(KERN_ERR, sdev, "%s: not attached\n", ALUA_DH_NAME); return ERR_PTR(-EINVAL);
return -EINVAL;
} }
/* /*
...@@ -889,18 +862,24 @@ static int alua_bus_attach(struct scsi_device *sdev) ...@@ -889,18 +862,24 @@ static int alua_bus_attach(struct scsi_device *sdev)
static void alua_bus_detach(struct scsi_device *sdev) static void alua_bus_detach(struct scsi_device *sdev)
{ {
struct alua_dh_data *h = get_alua_data(sdev); struct alua_dh_data *h = get_alua_data(sdev);
unsigned long flags;
spin_lock_irqsave(sdev->request_queue->queue_lock, flags);
sdev->scsi_dh_data = NULL;
spin_unlock_irqrestore(sdev->request_queue->queue_lock, flags);
if (h->buff && h->inq != h->buff) if (h->buff && h->inq != h->buff)
kfree(h->buff); kfree(h->buff);
kfree(h); kfree(h);
sdev_printk(KERN_NOTICE, sdev, "%s: Detached\n", ALUA_DH_NAME);
} }
static struct scsi_device_handler alua_dh = {
.name = ALUA_DH_NAME,
.module = THIS_MODULE,
.attach = alua_bus_attach,
.detach = alua_bus_detach,
.prep_fn = alua_prep_fn,
.check_sense = alua_check_sense,
.activate = alua_activate,
.set_params = alua_set_params,
.match = alua_match,
};
static int __init alua_init(void) static int __init alua_init(void)
{ {
int r; int r;
......
...@@ -650,35 +650,14 @@ static bool clariion_match(struct scsi_device *sdev) ...@@ -650,35 +650,14 @@ static bool clariion_match(struct scsi_device *sdev)
return false; return false;
} }
static int clariion_bus_attach(struct scsi_device *sdev); static struct scsi_dh_data *clariion_bus_attach(struct scsi_device *sdev)
static void clariion_bus_detach(struct scsi_device *sdev);
static struct scsi_device_handler clariion_dh = {
.name = CLARIION_NAME,
.module = THIS_MODULE,
.attach = clariion_bus_attach,
.detach = clariion_bus_detach,
.check_sense = clariion_check_sense,
.activate = clariion_activate,
.prep_fn = clariion_prep_fn,
.set_params = clariion_set_params,
.match = clariion_match,
};
static int clariion_bus_attach(struct scsi_device *sdev)
{ {
struct clariion_dh_data *h; struct clariion_dh_data *h;
unsigned long flags;
int err; int err;
h = kzalloc(sizeof(*h) , GFP_KERNEL); h = kzalloc(sizeof(*h) , GFP_KERNEL);
if (!h) { if (!h)
sdev_printk(KERN_ERR, sdev, "%s: Attach failed\n", return ERR_PTR(-ENOMEM);
CLARIION_NAME);
return -ENOMEM;
}
h->dh_data.scsi_dh = &clariion_dh;
h->lun_state = CLARIION_LUN_UNINITIALIZED; h->lun_state = CLARIION_LUN_UNINITIALIZED;
h->default_sp = CLARIION_UNBOUND_LU; h->default_sp = CLARIION_UNBOUND_LU;
h->current_sp = CLARIION_UNBOUND_LU; h->current_sp = CLARIION_UNBOUND_LU;
...@@ -691,40 +670,37 @@ static int clariion_bus_attach(struct scsi_device *sdev) ...@@ -691,40 +670,37 @@ static int clariion_bus_attach(struct scsi_device *sdev)
if (err != SCSI_DH_OK) if (err != SCSI_DH_OK)
goto failed; goto failed;
spin_lock_irqsave(sdev->request_queue->queue_lock, flags);
sdev->scsi_dh_data = &h->dh_data;
spin_unlock_irqrestore(sdev->request_queue->queue_lock, flags);
sdev_printk(KERN_INFO, sdev, sdev_printk(KERN_INFO, sdev,
"%s: connected to SP %c Port %d (%s, default SP %c)\n", "%s: connected to SP %c Port %d (%s, default SP %c)\n",
CLARIION_NAME, h->current_sp + 'A', CLARIION_NAME, h->current_sp + 'A',
h->port, lun_state[h->lun_state], h->port, lun_state[h->lun_state],
h->default_sp + 'A'); h->default_sp + 'A');
return &h->dh_data;
return 0;
failed: failed:
kfree(h); kfree(h);
sdev_printk(KERN_ERR, sdev, "%s: not attached\n", return ERR_PTR(-EINVAL);
CLARIION_NAME);
return -EINVAL;
} }
static void clariion_bus_detach(struct scsi_device *sdev) static void clariion_bus_detach(struct scsi_device *sdev)
{ {
struct clariion_dh_data *h = get_clariion_data(sdev); struct clariion_dh_data *h = get_clariion_data(sdev);
unsigned long flags;
spin_lock_irqsave(sdev->request_queue->queue_lock, flags);
sdev->scsi_dh_data = NULL;
spin_unlock_irqrestore(sdev->request_queue->queue_lock, flags);
sdev_printk(KERN_NOTICE, sdev, "%s: Detached\n",
CLARIION_NAME);
kfree(h); kfree(h);
} }
static struct scsi_device_handler clariion_dh = {
.name = CLARIION_NAME,
.module = THIS_MODULE,
.attach = clariion_bus_attach,
.detach = clariion_bus_detach,
.check_sense = clariion_check_sense,
.activate = clariion_activate,
.prep_fn = clariion_prep_fn,
.set_params = clariion_set_params,
.match = clariion_match,
};
static int __init clariion_init(void) static int __init clariion_init(void)
{ {
int r; int r;
......
...@@ -340,33 +340,14 @@ static bool hp_sw_match(struct scsi_device *sdev) ...@@ -340,33 +340,14 @@ static bool hp_sw_match(struct scsi_device *sdev)
return false; return false;
} }
static int hp_sw_bus_attach(struct scsi_device *sdev); static struct scsi_dh_data *hp_sw_bus_attach(struct scsi_device *sdev)
static void hp_sw_bus_detach(struct scsi_device *sdev);
static struct scsi_device_handler hp_sw_dh = {
.name = HP_SW_NAME,
.module = THIS_MODULE,
.attach = hp_sw_bus_attach,
.detach = hp_sw_bus_detach,
.activate = hp_sw_activate,
.prep_fn = hp_sw_prep_fn,
.match = hp_sw_match,
};
static int hp_sw_bus_attach(struct scsi_device *sdev)
{ {
struct hp_sw_dh_data *h; struct hp_sw_dh_data *h;
unsigned long flags;
int ret; int ret;
h = kzalloc(sizeof(*h), GFP_KERNEL); h = kzalloc(sizeof(*h), GFP_KERNEL);
if (!h) { if (!h)
sdev_printk(KERN_ERR, sdev, "%s: Attach Failed\n", return ERR_PTR(-ENOMEM);
HP_SW_NAME);
return -ENOMEM;
}
h->dh_data.scsi_dh = &hp_sw_dh;
h->path_state = HP_SW_PATH_UNINITIALIZED; h->path_state = HP_SW_PATH_UNINITIALIZED;
h->retries = HP_SW_RETRIES; h->retries = HP_SW_RETRIES;
h->sdev = sdev; h->sdev = sdev;
...@@ -375,37 +356,32 @@ static int hp_sw_bus_attach(struct scsi_device *sdev) ...@@ -375,37 +356,32 @@ static int hp_sw_bus_attach(struct scsi_device *sdev)
if (ret != SCSI_DH_OK || h->path_state == HP_SW_PATH_UNINITIALIZED) if (ret != SCSI_DH_OK || h->path_state == HP_SW_PATH_UNINITIALIZED)
goto failed; goto failed;
spin_lock_irqsave(sdev->request_queue->queue_lock, flags);
sdev->scsi_dh_data = &h->dh_data;
spin_unlock_irqrestore(sdev->request_queue->queue_lock, flags);
sdev_printk(KERN_INFO, sdev, "%s: attached to %s path\n", sdev_printk(KERN_INFO, sdev, "%s: attached to %s path\n",
HP_SW_NAME, h->path_state == HP_SW_PATH_ACTIVE? HP_SW_NAME, h->path_state == HP_SW_PATH_ACTIVE?
"active":"passive"); "active":"passive");
return &h->dh_data;
return 0;
failed: failed:
kfree(h); kfree(h);
sdev_printk(KERN_ERR, sdev, "%s: not attached\n", return ERR_PTR(-EINVAL);
HP_SW_NAME);
return -EINVAL;
} }
static void hp_sw_bus_detach( struct scsi_device *sdev ) static void hp_sw_bus_detach( struct scsi_device *sdev )
{ {
struct hp_sw_dh_data *h = get_hp_sw_data(sdev); struct hp_sw_dh_data *h = get_hp_sw_data(sdev);
unsigned long flags;
spin_lock_irqsave(sdev->request_queue->queue_lock, flags);
sdev->scsi_dh_data = NULL;
spin_unlock_irqrestore(sdev->request_queue->queue_lock, flags);
sdev_printk(KERN_NOTICE, sdev, "%s: Detached\n", HP_SW_NAME);
kfree(h); kfree(h);
} }
static struct scsi_device_handler hp_sw_dh = {
.name = HP_SW_NAME,
.module = THIS_MODULE,
.attach = hp_sw_bus_attach,
.detach = hp_sw_bus_detach,
.activate = hp_sw_activate,
.prep_fn = hp_sw_prep_fn,
.match = hp_sw_match,
};
static int __init hp_sw_init(void) static int __init hp_sw_init(void)
{ {
return scsi_register_device_handler(&hp_sw_dh); return scsi_register_device_handler(&hp_sw_dh);
......
...@@ -827,36 +827,16 @@ static bool rdac_match(struct scsi_device *sdev) ...@@ -827,36 +827,16 @@ static bool rdac_match(struct scsi_device *sdev)
return false; return false;
} }
static int rdac_bus_attach(struct scsi_device *sdev); static struct scsi_dh_data *rdac_bus_attach(struct scsi_device *sdev)
static void rdac_bus_detach(struct scsi_device *sdev);
static struct scsi_device_handler rdac_dh = {
.name = RDAC_NAME,
.module = THIS_MODULE,
.prep_fn = rdac_prep_fn,
.check_sense = rdac_check_sense,
.attach = rdac_bus_attach,
.detach = rdac_bus_detach,
.activate = rdac_activate,
.match = rdac_match,
};
static int rdac_bus_attach(struct scsi_device *sdev)
{ {
struct rdac_dh_data *h; struct rdac_dh_data *h;
unsigned long flags;
int err; int err;
char array_name[ARRAY_LABEL_LEN]; char array_name[ARRAY_LABEL_LEN];
char array_id[UNIQUE_ID_LEN]; char array_id[UNIQUE_ID_LEN];
h = kzalloc(sizeof(*h) , GFP_KERNEL); h = kzalloc(sizeof(*h) , GFP_KERNEL);
if (!h) { if (!h)
sdev_printk(KERN_ERR, sdev, "%s: Attach failed\n", return ERR_PTR(-ENOMEM);
RDAC_NAME);
return -ENOMEM;
}
h->dh_data.scsi_dh = &rdac_dh;
h->lun = UNINITIALIZED_LUN; h->lun = UNINITIALIZED_LUN;
h->state = RDAC_STATE_ACTIVE; h->state = RDAC_STATE_ACTIVE;
...@@ -876,16 +856,12 @@ static int rdac_bus_attach(struct scsi_device *sdev) ...@@ -876,16 +856,12 @@ static int rdac_bus_attach(struct scsi_device *sdev)
if (err != SCSI_DH_OK) if (err != SCSI_DH_OK)
goto clean_ctlr; goto clean_ctlr;
spin_lock_irqsave(sdev->request_queue->queue_lock, flags);
sdev->scsi_dh_data = &h->dh_data;
spin_unlock_irqrestore(sdev->request_queue->queue_lock, flags);
sdev_printk(KERN_NOTICE, sdev, sdev_printk(KERN_NOTICE, sdev,
"%s: LUN %d (%s) (%s)\n", "%s: LUN %d (%s) (%s)\n",
RDAC_NAME, h->lun, mode[(int)h->mode], RDAC_NAME, h->lun, mode[(int)h->mode],
lun_state[(int)h->lun_state]); lun_state[(int)h->lun_state]);
return 0; return &h->dh_data;
clean_ctlr: clean_ctlr:
spin_lock(&list_lock); spin_lock(&list_lock);
...@@ -894,32 +870,33 @@ static int rdac_bus_attach(struct scsi_device *sdev) ...@@ -894,32 +870,33 @@ static int rdac_bus_attach(struct scsi_device *sdev)
failed: failed:
kfree(h); kfree(h);
sdev_printk(KERN_ERR, sdev, "%s: not attached\n", return ERR_PTR(-EINVAL);
RDAC_NAME);
return -EINVAL;
} }
static void rdac_bus_detach( struct scsi_device *sdev ) static void rdac_bus_detach( struct scsi_device *sdev )
{ {
struct rdac_dh_data *h = get_rdac_data(sdev); struct rdac_dh_data *h = get_rdac_data(sdev);
unsigned long flags;
if (h->ctlr && h->ctlr->ms_queued) if (h->ctlr && h->ctlr->ms_queued)
flush_workqueue(kmpath_rdacd); flush_workqueue(kmpath_rdacd);
spin_lock_irqsave(sdev->request_queue->queue_lock, flags);
sdev->scsi_dh_data = NULL;
spin_unlock_irqrestore(sdev->request_queue->queue_lock, flags);
spin_lock(&list_lock); spin_lock(&list_lock);
if (h->ctlr) if (h->ctlr)
kref_put(&h->ctlr->kref, release_controller); kref_put(&h->ctlr->kref, release_controller);
spin_unlock(&list_lock); spin_unlock(&list_lock);
kfree(h); kfree(h);
sdev_printk(KERN_NOTICE, sdev, "%s: Detached\n", RDAC_NAME);
} }
static struct scsi_device_handler rdac_dh = {
.name = RDAC_NAME,
.module = THIS_MODULE,
.prep_fn = rdac_prep_fn,
.check_sense = rdac_check_sense,
.attach = rdac_bus_attach,
.detach = rdac_bus_detach,
.activate = rdac_activate,
.match = rdac_match,
};
static int __init rdac_init(void) static int __init rdac_init(void)
{ {
......
...@@ -210,7 +210,7 @@ struct scsi_device_handler { ...@@ -210,7 +210,7 @@ struct scsi_device_handler {
struct module *module; struct module *module;
const char *name; const char *name;
int (*check_sense)(struct scsi_device *, struct scsi_sense_hdr *); int (*check_sense)(struct scsi_device *, struct scsi_sense_hdr *);
int (*attach)(struct scsi_device *); struct scsi_dh_data *(*attach)(struct scsi_device *);
void (*detach)(struct scsi_device *); void (*detach)(struct scsi_device *);
int (*activate)(struct scsi_device *, activate_complete, void *); int (*activate)(struct scsi_device *, activate_complete, void *);
int (*prep_fn)(struct scsi_device *, struct request *); int (*prep_fn)(struct scsi_device *, struct request *);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment