[SCSI] scsi_dh: attach to hardware handler from dm-mpath
[safe/jmp/linux-2.6] / drivers / scsi / device_handler / scsi_dh.c
1 /*
2  * SCSI device handler infrastruture.
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License as published by the
6  * Free Software Foundation; either version 2 of the License, or (at your
7  * option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17  *
18  * Copyright IBM Corporation, 2007
19  *      Authors:
20  *               Chandra Seetharaman <sekharan@us.ibm.com>
21  *               Mike Anderson <andmike@linux.vnet.ibm.com>
22  */
23
24 #include <scsi/scsi_dh.h>
25 #include "../scsi_priv.h"
26
27 static DEFINE_SPINLOCK(list_lock);
28 static LIST_HEAD(scsi_dh_list);
29
30 static struct scsi_device_handler *get_device_handler(const char *name)
31 {
32         struct scsi_device_handler *tmp, *found = NULL;
33
34         spin_lock(&list_lock);
35         list_for_each_entry(tmp, &scsi_dh_list, list) {
36                 if (!strncmp(tmp->name, name, strlen(tmp->name))) {
37                         found = tmp;
38                         break;
39                 }
40         }
41         spin_unlock(&list_lock);
42         return found;
43 }
44
45 static int device_handler_match(struct scsi_device_handler *tmp,
46                                 struct scsi_device *sdev)
47 {
48         int i;
49
50         for(i = 0; tmp->devlist[i].vendor; i++) {
51                 if (!strncmp(sdev->vendor, tmp->devlist[i].vendor,
52                              strlen(tmp->devlist[i].vendor)) &&
53                     !strncmp(sdev->model, tmp->devlist[i].model,
54                              strlen(tmp->devlist[i].model))) {
55                         return 1;
56                 }
57         }
58
59         return 0;
60 }
61
62 /*
63  * scsi_dh_handler_attach - Attach a device handler to a device
64  * @sdev - SCSI device the device handler should attach to
65  * @scsi_dh - The device handler to attach
66  */
67 static int scsi_dh_handler_attach(struct scsi_device *sdev,
68                                   struct scsi_device_handler *scsi_dh)
69 {
70         int err = 0;
71
72         if (sdev->scsi_dh_data) {
73                 if (sdev->scsi_dh_data->scsi_dh != scsi_dh)
74                         err = -EBUSY;
75         } else if (scsi_dh->attach)
76                 err = scsi_dh->attach(sdev);
77
78         return err;
79 }
80
81 /*
82  * scsi_dh_handler_detach - Detach a device handler from a device
83  * @sdev - SCSI device the device handler should be detached from
84  * @scsi_dh - Device handler to be detached
85  *
86  * Detach from a device handler. If a device handler is specified,
87  * only detach if the currently attached handler matches @scsi_dh.
88  */
89 static void scsi_dh_handler_detach(struct scsi_device *sdev,
90                                    struct scsi_device_handler *scsi_dh)
91 {
92         if (!sdev->scsi_dh_data)
93                 return;
94
95         if (scsi_dh && scsi_dh != sdev->scsi_dh_data->scsi_dh)
96                 return;
97
98         if (!scsi_dh)
99                 scsi_dh = sdev->scsi_dh_data->scsi_dh;
100
101         if (scsi_dh && scsi_dh->detach)
102                 scsi_dh->detach(sdev);
103 }
104
105 /*
106  * Functions for sysfs attribute 'dh_state'
107  */
108 static ssize_t
109 store_dh_state(struct device *dev, struct device_attribute *attr,
110                const char *buf, size_t count)
111 {
112         struct scsi_device *sdev = to_scsi_device(dev);
113         struct scsi_device_handler *scsi_dh;
114         int err = -EINVAL;
115
116         if (!sdev->scsi_dh_data) {
117                 /*
118                  * Attach to a device handler
119                  */
120                 if (!(scsi_dh = get_device_handler(buf)))
121                         return err;
122                 err = scsi_dh_handler_attach(sdev, scsi_dh);
123         } else {
124                 scsi_dh = sdev->scsi_dh_data->scsi_dh;
125                 if (!strncmp(buf, "detach", 6)) {
126                         /*
127                          * Detach from a device handler
128                          */
129                         scsi_dh_handler_detach(sdev, scsi_dh);
130                         err = 0;
131                 } else if (!strncmp(buf, "activate", 8)) {
132                         /*
133                          * Activate a device handler
134                          */
135                         if (scsi_dh->activate)
136                                 err = scsi_dh->activate(sdev);
137                         else
138                                 err = 0;
139                 }
140         }
141
142         return err<0?err:count;
143 }
144
145 static ssize_t
146 show_dh_state(struct device *dev, struct device_attribute *attr, char *buf)
147 {
148         struct scsi_device *sdev = to_scsi_device(dev);
149
150         if (!sdev->scsi_dh_data)
151                 return snprintf(buf, 20, "detached\n");
152
153         return snprintf(buf, 20, "%s\n", sdev->scsi_dh_data->scsi_dh->name);
154 }
155
156 static struct device_attribute scsi_dh_state_attr =
157         __ATTR(dh_state, S_IRUGO | S_IWUSR, show_dh_state,
158                store_dh_state);
159
160 /*
161  * scsi_dh_sysfs_attr_add - Callback for scsi_init_dh
162  */
163 static int scsi_dh_sysfs_attr_add(struct device *dev, void *data)
164 {
165         struct scsi_device *sdev;
166         int err;
167
168         if (!scsi_is_sdev_device(dev))
169                 return 0;
170
171         sdev = to_scsi_device(dev);
172
173         err = device_create_file(&sdev->sdev_gendev,
174                                  &scsi_dh_state_attr);
175
176         return 0;
177 }
178
179 /*
180  * scsi_dh_sysfs_attr_remove - Callback for scsi_exit_dh
181  */
182 static int scsi_dh_sysfs_attr_remove(struct device *dev, void *data)
183 {
184         struct scsi_device *sdev;
185
186         if (!scsi_is_sdev_device(dev))
187                 return 0;
188
189         sdev = to_scsi_device(dev);
190
191         device_remove_file(&sdev->sdev_gendev,
192                            &scsi_dh_state_attr);
193
194         return 0;
195 }
196
197 /*
198  * scsi_dh_notifier - notifier chain callback
199  */
200 static int scsi_dh_notifier(struct notifier_block *nb,
201                             unsigned long action, void *data)
202 {
203         struct device *dev = data;
204         struct scsi_device *sdev;
205         int err = 0;
206         struct scsi_device_handler *tmp, *devinfo = NULL;
207
208         if (!scsi_is_sdev_device(dev))
209                 return 0;
210
211         sdev = to_scsi_device(dev);
212
213         spin_lock(&list_lock);
214         list_for_each_entry(tmp, &scsi_dh_list, list) {
215                 if (device_handler_match(tmp, sdev)) {
216                         devinfo = tmp;
217                         break;
218                 }
219         }
220         spin_unlock(&list_lock);
221
222         if (!devinfo)
223                 goto out;
224
225         if (action == BUS_NOTIFY_ADD_DEVICE) {
226                 err = scsi_dh_handler_attach(sdev, devinfo);
227                 if (!err)
228                         err = device_create_file(dev, &scsi_dh_state_attr);
229         } else if (action == BUS_NOTIFY_DEL_DEVICE) {
230                 device_remove_file(dev, &scsi_dh_state_attr);
231                 scsi_dh_handler_detach(sdev, NULL);
232         }
233 out:
234         return err;
235 }
236
237 /*
238  * scsi_dh_notifier_add - Callback for scsi_register_device_handler
239  */
240 static int scsi_dh_notifier_add(struct device *dev, void *data)
241 {
242         struct scsi_device_handler *scsi_dh = data;
243         struct scsi_device *sdev;
244
245         if (!scsi_is_sdev_device(dev))
246                 return 0;
247
248         if (!get_device(dev))
249                 return 0;
250
251         sdev = to_scsi_device(dev);
252
253         if (device_handler_match(scsi_dh, sdev))
254                 scsi_dh_handler_attach(sdev, scsi_dh);
255
256         put_device(dev);
257
258         return 0;
259 }
260
261 /*
262  * scsi_dh_notifier_remove - Callback for scsi_unregister_device_handler
263  */
264 static int scsi_dh_notifier_remove(struct device *dev, void *data)
265 {
266         struct scsi_device_handler *scsi_dh = data;
267         struct scsi_device *sdev;
268
269         if (!scsi_is_sdev_device(dev))
270                 return 0;
271
272         if (!get_device(dev))
273                 return 0;
274
275         sdev = to_scsi_device(dev);
276
277         scsi_dh_handler_detach(sdev, scsi_dh);
278
279         put_device(dev);
280
281         return 0;
282 }
283
284 /*
285  * scsi_register_device_handler - register a device handler personality
286  *      module.
287  * @scsi_dh - device handler to be registered.
288  *
289  * Returns 0 on success, -EBUSY if handler already registered.
290  */
291 int scsi_register_device_handler(struct scsi_device_handler *scsi_dh)
292 {
293         if (get_device_handler(scsi_dh->name))
294                 return -EBUSY;
295
296         spin_lock(&list_lock);
297         list_add(&scsi_dh->list, &scsi_dh_list);
298         spin_unlock(&list_lock);
299         bus_for_each_dev(&scsi_bus_type, NULL, scsi_dh, scsi_dh_notifier_add);
300         printk(KERN_INFO "%s: device handler registered\n", scsi_dh->name);
301
302         return SCSI_DH_OK;
303 }
304 EXPORT_SYMBOL_GPL(scsi_register_device_handler);
305
306 /*
307  * scsi_unregister_device_handler - register a device handler personality
308  *      module.
309  * @scsi_dh - device handler to be unregistered.
310  *
311  * Returns 0 on success, -ENODEV if handler not registered.
312  */
313 int scsi_unregister_device_handler(struct scsi_device_handler *scsi_dh)
314 {
315         if (!get_device_handler(scsi_dh->name))
316                 return -ENODEV;
317
318         bus_for_each_dev(&scsi_bus_type, NULL, scsi_dh,
319                          scsi_dh_notifier_remove);
320
321         spin_lock(&list_lock);
322         list_del(&scsi_dh->list);
323         spin_unlock(&list_lock);
324         printk(KERN_INFO "%s: device handler unregistered\n", scsi_dh->name);
325
326         return SCSI_DH_OK;
327 }
328 EXPORT_SYMBOL_GPL(scsi_unregister_device_handler);
329
330 /*
331  * scsi_dh_activate - activate the path associated with the scsi_device
332  *      corresponding to the given request queue.
333  * @q - Request queue that is associated with the scsi_device to be
334  *      activated.
335  */
336 int scsi_dh_activate(struct request_queue *q)
337 {
338         int err = 0;
339         unsigned long flags;
340         struct scsi_device *sdev;
341         struct scsi_device_handler *scsi_dh = NULL;
342
343         spin_lock_irqsave(q->queue_lock, flags);
344         sdev = q->queuedata;
345         if (sdev && sdev->scsi_dh_data)
346                 scsi_dh = sdev->scsi_dh_data->scsi_dh;
347         if (!scsi_dh || !get_device(&sdev->sdev_gendev))
348                 err = SCSI_DH_NOSYS;
349         spin_unlock_irqrestore(q->queue_lock, flags);
350
351         if (err)
352                 return err;
353
354         if (scsi_dh->activate)
355                 err = scsi_dh->activate(sdev);
356         put_device(&sdev->sdev_gendev);
357         return err;
358 }
359 EXPORT_SYMBOL_GPL(scsi_dh_activate);
360
361 /*
362  * scsi_dh_handler_exist - Return TRUE(1) if a device handler exists for
363  *      the given name. FALSE(0) otherwise.
364  * @name - name of the device handler.
365  */
366 int scsi_dh_handler_exist(const char *name)
367 {
368         return (get_device_handler(name) != NULL);
369 }
370 EXPORT_SYMBOL_GPL(scsi_dh_handler_exist);
371
372 /*
373  * scsi_dh_handler_attach - Attach device handler
374  * @sdev - sdev the handler should be attached to
375  * @name - name of the handler to attach
376  */
377 int scsi_dh_attach(struct request_queue *q, const char *name)
378 {
379         unsigned long flags;
380         struct scsi_device *sdev;
381         struct scsi_device_handler *scsi_dh;
382         int err = 0;
383
384         scsi_dh = get_device_handler(name);
385         if (!scsi_dh)
386                 return -EINVAL;
387
388         spin_lock_irqsave(q->queue_lock, flags);
389         sdev = q->queuedata;
390         if (!sdev || !get_device(&sdev->sdev_gendev))
391                 err = -ENODEV;
392         spin_unlock_irqrestore(q->queue_lock, flags);
393
394         if (!err) {
395                 err = scsi_dh_handler_attach(sdev, scsi_dh);
396
397                 put_device(&sdev->sdev_gendev);
398         }
399         return err;
400 }
401 EXPORT_SYMBOL_GPL(scsi_dh_attach);
402
403 /*
404  * scsi_dh_handler_detach - Detach device handler
405  * @sdev - sdev the handler should be detached from
406  *
407  * This function will detach the device handler only
408  * if the sdev is not part of the internal list, ie
409  * if it has been attached manually.
410  */
411 void scsi_dh_detach(struct request_queue *q)
412 {
413         unsigned long flags;
414         struct scsi_device *sdev;
415         struct scsi_device_handler *scsi_dh = NULL;
416
417         spin_lock_irqsave(q->queue_lock, flags);
418         sdev = q->queuedata;
419         if (!sdev || !get_device(&sdev->sdev_gendev))
420                 sdev = NULL;
421         spin_unlock_irqrestore(q->queue_lock, flags);
422
423         if (!sdev)
424                 return;
425
426         if (sdev->scsi_dh_data) {
427                 /* if sdev is not on internal list, detach */
428                 scsi_dh = sdev->scsi_dh_data->scsi_dh;
429                 if (!device_handler_match(scsi_dh, sdev))
430                         scsi_dh_handler_detach(sdev, scsi_dh);
431         }
432         put_device(&sdev->sdev_gendev);
433 }
434 EXPORT_SYMBOL_GPL(scsi_dh_detach);
435
436 static struct notifier_block scsi_dh_nb = {
437         .notifier_call = scsi_dh_notifier
438 };
439
440 static int __init scsi_dh_init(void)
441 {
442         int r;
443
444         r = bus_register_notifier(&scsi_bus_type, &scsi_dh_nb);
445
446         if (!r)
447                 bus_for_each_dev(&scsi_bus_type, NULL, NULL,
448                                  scsi_dh_sysfs_attr_add);
449
450         return r;
451 }
452
453 static void __exit scsi_dh_exit(void)
454 {
455         bus_for_each_dev(&scsi_bus_type, NULL, NULL,
456                          scsi_dh_sysfs_attr_remove);
457         bus_unregister_notifier(&scsi_bus_type, &scsi_dh_nb);
458 }
459
460 module_init(scsi_dh_init);
461 module_exit(scsi_dh_exit);
462
463 MODULE_DESCRIPTION("SCSI device handler");
464 MODULE_AUTHOR("Chandra Seetharaman <sekharan@us.ibm.com>");
465 MODULE_LICENSE("GPL");