[SCSI] scsi_dh: create lookup cache
[pandora-kernel.git] / 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 struct scsi_dh_devinfo_list {
28         struct list_head node;
29         char vendor[9];
30         char model[17];
31         struct scsi_device_handler *handler;
32 };
33
34 static DEFINE_SPINLOCK(list_lock);
35 static LIST_HEAD(scsi_dh_list);
36 static LIST_HEAD(scsi_dh_dev_list);
37
38 static struct scsi_device_handler *get_device_handler(const char *name)
39 {
40         struct scsi_device_handler *tmp, *found = NULL;
41
42         spin_lock(&list_lock);
43         list_for_each_entry(tmp, &scsi_dh_list, list) {
44                 if (!strncmp(tmp->name, name, strlen(tmp->name))) {
45                         found = tmp;
46                         break;
47                 }
48         }
49         spin_unlock(&list_lock);
50         return found;
51 }
52
53
54 static struct scsi_device_handler *
55 scsi_dh_cache_lookup(struct scsi_device *sdev)
56 {
57         struct scsi_dh_devinfo_list *tmp;
58         struct scsi_device_handler *found_dh = NULL;
59
60         spin_lock(&list_lock);
61         list_for_each_entry(tmp, &scsi_dh_dev_list, node) {
62                 if (!strncmp(sdev->vendor, tmp->vendor, strlen(tmp->vendor)) &&
63                     !strncmp(sdev->model, tmp->model, strlen(tmp->model))) {
64                         found_dh = tmp->handler;
65                         break;
66                 }
67         }
68         spin_unlock(&list_lock);
69
70         return found_dh;
71 }
72
73 static int scsi_dh_handler_lookup(struct scsi_device_handler *scsi_dh,
74                                   struct scsi_device *sdev)
75 {
76         int i, found = 0;
77
78         for(i = 0; scsi_dh->devlist[i].vendor; i++) {
79                 if (!strncmp(sdev->vendor, scsi_dh->devlist[i].vendor,
80                              strlen(scsi_dh->devlist[i].vendor)) &&
81                     !strncmp(sdev->model, scsi_dh->devlist[i].model,
82                              strlen(scsi_dh->devlist[i].model))) {
83                         found = 1;
84                         break;
85                 }
86         }
87         return found;
88 }
89
90 /*
91  * device_handler_match - Attach a device handler to a device
92  * @scsi_dh - The device handler to match against or NULL
93  * @sdev - SCSI device to be tested against @scsi_dh
94  *
95  * Tests @sdev against the device handler @scsi_dh or against
96  * all registered device_handler if @scsi_dh == NULL.
97  * Returns the found device handler or NULL if not found.
98  */
99 static struct scsi_device_handler *
100 device_handler_match(struct scsi_device_handler *scsi_dh,
101                      struct scsi_device *sdev)
102 {
103         struct scsi_device_handler *found_dh = NULL;
104         struct scsi_dh_devinfo_list *tmp;
105
106         found_dh = scsi_dh_cache_lookup(sdev);
107         if (found_dh)
108                 return found_dh;
109
110         if (scsi_dh) {
111                 if (scsi_dh_handler_lookup(scsi_dh, sdev))
112                         found_dh = scsi_dh;
113         } else {
114                 struct scsi_device_handler *tmp_dh;
115
116                 spin_lock(&list_lock);
117                 list_for_each_entry(tmp_dh, &scsi_dh_list, list) {
118                         if (scsi_dh_handler_lookup(tmp_dh, sdev))
119                                 found_dh = tmp_dh;
120                 }
121                 spin_unlock(&list_lock);
122         }
123
124         if (found_dh) { /* If device is found, add it to the cache */
125                 tmp = kmalloc(sizeof(*tmp), GFP_KERNEL);
126                 if (tmp) {
127                         strncpy(tmp->vendor, sdev->vendor, 8);
128                         strncpy(tmp->model, sdev->model, 16);
129                         tmp->vendor[8] = '\0';
130                         tmp->model[16] = '\0';
131                         tmp->handler = found_dh;
132                         spin_lock(&list_lock);
133                         list_add(&tmp->node, &scsi_dh_dev_list);
134                         spin_unlock(&list_lock);
135                 } else {
136                         found_dh = NULL;
137                 }
138         }
139
140         return found_dh;
141 }
142
143 /*
144  * scsi_dh_handler_attach - Attach a device handler to a device
145  * @sdev - SCSI device the device handler should attach to
146  * @scsi_dh - The device handler to attach
147  */
148 static int scsi_dh_handler_attach(struct scsi_device *sdev,
149                                   struct scsi_device_handler *scsi_dh)
150 {
151         int err = 0;
152
153         if (sdev->scsi_dh_data) {
154                 if (sdev->scsi_dh_data->scsi_dh != scsi_dh)
155                         err = -EBUSY;
156         } else if (scsi_dh->attach)
157                 err = scsi_dh->attach(sdev);
158
159         return err;
160 }
161
162 /*
163  * scsi_dh_handler_detach - Detach a device handler from a device
164  * @sdev - SCSI device the device handler should be detached from
165  * @scsi_dh - Device handler to be detached
166  *
167  * Detach from a device handler. If a device handler is specified,
168  * only detach if the currently attached handler matches @scsi_dh.
169  */
170 static void scsi_dh_handler_detach(struct scsi_device *sdev,
171                                    struct scsi_device_handler *scsi_dh)
172 {
173         if (!sdev->scsi_dh_data)
174                 return;
175
176         if (scsi_dh && scsi_dh != sdev->scsi_dh_data->scsi_dh)
177                 return;
178
179         if (!scsi_dh)
180                 scsi_dh = sdev->scsi_dh_data->scsi_dh;
181
182         if (scsi_dh && scsi_dh->detach)
183                 scsi_dh->detach(sdev);
184 }
185
186 /*
187  * Functions for sysfs attribute 'dh_state'
188  */
189 static ssize_t
190 store_dh_state(struct device *dev, struct device_attribute *attr,
191                const char *buf, size_t count)
192 {
193         struct scsi_device *sdev = to_scsi_device(dev);
194         struct scsi_device_handler *scsi_dh;
195         int err = -EINVAL;
196
197         if (!sdev->scsi_dh_data) {
198                 /*
199                  * Attach to a device handler
200                  */
201                 if (!(scsi_dh = get_device_handler(buf)))
202                         return err;
203                 err = scsi_dh_handler_attach(sdev, scsi_dh);
204         } else {
205                 scsi_dh = sdev->scsi_dh_data->scsi_dh;
206                 if (!strncmp(buf, "detach", 6)) {
207                         /*
208                          * Detach from a device handler
209                          */
210                         scsi_dh_handler_detach(sdev, scsi_dh);
211                         err = 0;
212                 } else if (!strncmp(buf, "activate", 8)) {
213                         /*
214                          * Activate a device handler
215                          */
216                         if (scsi_dh->activate)
217                                 err = scsi_dh->activate(sdev);
218                         else
219                                 err = 0;
220                 }
221         }
222
223         return err<0?err:count;
224 }
225
226 static ssize_t
227 show_dh_state(struct device *dev, struct device_attribute *attr, char *buf)
228 {
229         struct scsi_device *sdev = to_scsi_device(dev);
230
231         if (!sdev->scsi_dh_data)
232                 return snprintf(buf, 20, "detached\n");
233
234         return snprintf(buf, 20, "%s\n", sdev->scsi_dh_data->scsi_dh->name);
235 }
236
237 static struct device_attribute scsi_dh_state_attr =
238         __ATTR(dh_state, S_IRUGO | S_IWUSR, show_dh_state,
239                store_dh_state);
240
241 /*
242  * scsi_dh_sysfs_attr_add - Callback for scsi_init_dh
243  */
244 static int scsi_dh_sysfs_attr_add(struct device *dev, void *data)
245 {
246         struct scsi_device *sdev;
247         int err;
248
249         if (!scsi_is_sdev_device(dev))
250                 return 0;
251
252         sdev = to_scsi_device(dev);
253
254         err = device_create_file(&sdev->sdev_gendev,
255                                  &scsi_dh_state_attr);
256
257         return 0;
258 }
259
260 /*
261  * scsi_dh_sysfs_attr_remove - Callback for scsi_exit_dh
262  */
263 static int scsi_dh_sysfs_attr_remove(struct device *dev, void *data)
264 {
265         struct scsi_device *sdev;
266
267         if (!scsi_is_sdev_device(dev))
268                 return 0;
269
270         sdev = to_scsi_device(dev);
271
272         device_remove_file(&sdev->sdev_gendev,
273                            &scsi_dh_state_attr);
274
275         return 0;
276 }
277
278 /*
279  * scsi_dh_notifier - notifier chain callback
280  */
281 static int scsi_dh_notifier(struct notifier_block *nb,
282                             unsigned long action, void *data)
283 {
284         struct device *dev = data;
285         struct scsi_device *sdev;
286         int err = 0;
287         struct scsi_device_handler *devinfo = NULL;
288
289         if (!scsi_is_sdev_device(dev))
290                 return 0;
291
292         sdev = to_scsi_device(dev);
293
294         if (action == BUS_NOTIFY_ADD_DEVICE) {
295                 devinfo = device_handler_match(NULL, sdev);
296                 if (!devinfo)
297                         goto out;
298
299                 err = scsi_dh_handler_attach(sdev, devinfo);
300                 if (!err)
301                         err = device_create_file(dev, &scsi_dh_state_attr);
302         } else if (action == BUS_NOTIFY_DEL_DEVICE) {
303                 device_remove_file(dev, &scsi_dh_state_attr);
304                 scsi_dh_handler_detach(sdev, NULL);
305         }
306 out:
307         return err;
308 }
309
310 /*
311  * scsi_dh_notifier_add - Callback for scsi_register_device_handler
312  */
313 static int scsi_dh_notifier_add(struct device *dev, void *data)
314 {
315         struct scsi_device_handler *scsi_dh = data;
316         struct scsi_device *sdev;
317
318         if (!scsi_is_sdev_device(dev))
319                 return 0;
320
321         if (!get_device(dev))
322                 return 0;
323
324         sdev = to_scsi_device(dev);
325
326         if (device_handler_match(scsi_dh, sdev))
327                 scsi_dh_handler_attach(sdev, scsi_dh);
328
329         put_device(dev);
330
331         return 0;
332 }
333
334 /*
335  * scsi_dh_notifier_remove - Callback for scsi_unregister_device_handler
336  */
337 static int scsi_dh_notifier_remove(struct device *dev, void *data)
338 {
339         struct scsi_device_handler *scsi_dh = data;
340         struct scsi_device *sdev;
341
342         if (!scsi_is_sdev_device(dev))
343                 return 0;
344
345         if (!get_device(dev))
346                 return 0;
347
348         sdev = to_scsi_device(dev);
349
350         scsi_dh_handler_detach(sdev, scsi_dh);
351
352         put_device(dev);
353
354         return 0;
355 }
356
357 /*
358  * scsi_register_device_handler - register a device handler personality
359  *      module.
360  * @scsi_dh - device handler to be registered.
361  *
362  * Returns 0 on success, -EBUSY if handler already registered.
363  */
364 int scsi_register_device_handler(struct scsi_device_handler *scsi_dh)
365 {
366         if (get_device_handler(scsi_dh->name))
367                 return -EBUSY;
368
369         spin_lock(&list_lock);
370         list_add(&scsi_dh->list, &scsi_dh_list);
371         spin_unlock(&list_lock);
372         bus_for_each_dev(&scsi_bus_type, NULL, scsi_dh, scsi_dh_notifier_add);
373         printk(KERN_INFO "%s: device handler registered\n", scsi_dh->name);
374
375         return SCSI_DH_OK;
376 }
377 EXPORT_SYMBOL_GPL(scsi_register_device_handler);
378
379 /*
380  * scsi_unregister_device_handler - register a device handler personality
381  *      module.
382  * @scsi_dh - device handler to be unregistered.
383  *
384  * Returns 0 on success, -ENODEV if handler not registered.
385  */
386 int scsi_unregister_device_handler(struct scsi_device_handler *scsi_dh)
387 {
388         struct scsi_dh_devinfo_list *tmp, *pos;
389
390         if (!get_device_handler(scsi_dh->name))
391                 return -ENODEV;
392
393         bus_for_each_dev(&scsi_bus_type, NULL, scsi_dh,
394                          scsi_dh_notifier_remove);
395
396         spin_lock(&list_lock);
397         list_del(&scsi_dh->list);
398         list_for_each_entry_safe(pos, tmp, &scsi_dh_dev_list, node) {
399                 if (pos->handler == scsi_dh) {
400                         list_del(&pos->node);
401                         kfree(pos);
402                 }
403         }
404         spin_unlock(&list_lock);
405         printk(KERN_INFO "%s: device handler unregistered\n", scsi_dh->name);
406
407         return SCSI_DH_OK;
408 }
409 EXPORT_SYMBOL_GPL(scsi_unregister_device_handler);
410
411 /*
412  * scsi_dh_activate - activate the path associated with the scsi_device
413  *      corresponding to the given request queue.
414  * @q - Request queue that is associated with the scsi_device to be
415  *      activated.
416  */
417 int scsi_dh_activate(struct request_queue *q)
418 {
419         int err = 0;
420         unsigned long flags;
421         struct scsi_device *sdev;
422         struct scsi_device_handler *scsi_dh = NULL;
423
424         spin_lock_irqsave(q->queue_lock, flags);
425         sdev = q->queuedata;
426         if (sdev && sdev->scsi_dh_data)
427                 scsi_dh = sdev->scsi_dh_data->scsi_dh;
428         if (!scsi_dh || !get_device(&sdev->sdev_gendev))
429                 err = SCSI_DH_NOSYS;
430         spin_unlock_irqrestore(q->queue_lock, flags);
431
432         if (err)
433                 return err;
434
435         if (scsi_dh->activate)
436                 err = scsi_dh->activate(sdev);
437         put_device(&sdev->sdev_gendev);
438         return err;
439 }
440 EXPORT_SYMBOL_GPL(scsi_dh_activate);
441
442 /*
443  * scsi_dh_handler_exist - Return TRUE(1) if a device handler exists for
444  *      the given name. FALSE(0) otherwise.
445  * @name - name of the device handler.
446  */
447 int scsi_dh_handler_exist(const char *name)
448 {
449         return (get_device_handler(name) != NULL);
450 }
451 EXPORT_SYMBOL_GPL(scsi_dh_handler_exist);
452
453 /*
454  * scsi_dh_handler_attach - Attach device handler
455  * @sdev - sdev the handler should be attached to
456  * @name - name of the handler to attach
457  */
458 int scsi_dh_attach(struct request_queue *q, const char *name)
459 {
460         unsigned long flags;
461         struct scsi_device *sdev;
462         struct scsi_device_handler *scsi_dh;
463         int err = 0;
464
465         scsi_dh = get_device_handler(name);
466         if (!scsi_dh)
467                 return -EINVAL;
468
469         spin_lock_irqsave(q->queue_lock, flags);
470         sdev = q->queuedata;
471         if (!sdev || !get_device(&sdev->sdev_gendev))
472                 err = -ENODEV;
473         spin_unlock_irqrestore(q->queue_lock, flags);
474
475         if (!err) {
476                 err = scsi_dh_handler_attach(sdev, scsi_dh);
477
478                 put_device(&sdev->sdev_gendev);
479         }
480         return err;
481 }
482 EXPORT_SYMBOL_GPL(scsi_dh_attach);
483
484 /*
485  * scsi_dh_handler_detach - Detach device handler
486  * @sdev - sdev the handler should be detached from
487  *
488  * This function will detach the device handler only
489  * if the sdev is not part of the internal list, ie
490  * if it has been attached manually.
491  */
492 void scsi_dh_detach(struct request_queue *q)
493 {
494         unsigned long flags;
495         struct scsi_device *sdev;
496         struct scsi_device_handler *scsi_dh = NULL;
497
498         spin_lock_irqsave(q->queue_lock, flags);
499         sdev = q->queuedata;
500         if (!sdev || !get_device(&sdev->sdev_gendev))
501                 sdev = NULL;
502         spin_unlock_irqrestore(q->queue_lock, flags);
503
504         if (!sdev)
505                 return;
506
507         if (sdev->scsi_dh_data) {
508                 /* if sdev is not on internal list, detach */
509                 scsi_dh = sdev->scsi_dh_data->scsi_dh;
510                 if (!device_handler_match(scsi_dh, sdev))
511                         scsi_dh_handler_detach(sdev, scsi_dh);
512         }
513         put_device(&sdev->sdev_gendev);
514 }
515 EXPORT_SYMBOL_GPL(scsi_dh_detach);
516
517 static struct notifier_block scsi_dh_nb = {
518         .notifier_call = scsi_dh_notifier
519 };
520
521 static int __init scsi_dh_init(void)
522 {
523         int r;
524
525         r = bus_register_notifier(&scsi_bus_type, &scsi_dh_nb);
526
527         if (!r)
528                 bus_for_each_dev(&scsi_bus_type, NULL, NULL,
529                                  scsi_dh_sysfs_attr_add);
530
531         return r;
532 }
533
534 static void __exit scsi_dh_exit(void)
535 {
536         bus_for_each_dev(&scsi_bus_type, NULL, NULL,
537                          scsi_dh_sysfs_attr_remove);
538         bus_unregister_notifier(&scsi_bus_type, &scsi_dh_nb);
539 }
540
541 module_init(scsi_dh_init);
542 module_exit(scsi_dh_exit);
543
544 MODULE_DESCRIPTION("SCSI device handler");
545 MODULE_AUTHOR("Chandra Seetharaman <sekharan@us.ibm.com>");
546 MODULE_LICENSE("GPL");