Merge branch 'osd-devel' into nfs-for-next
[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 <linux/slab.h>
25 #include <scsi/scsi_dh.h>
26 #include "../scsi_priv.h"
27
28 static DEFINE_SPINLOCK(list_lock);
29 static LIST_HEAD(scsi_dh_list);
30 static int scsi_dh_list_idx = 1;
31
32 static struct scsi_device_handler *get_device_handler(const char *name)
33 {
34         struct scsi_device_handler *tmp, *found = NULL;
35
36         spin_lock(&list_lock);
37         list_for_each_entry(tmp, &scsi_dh_list, list) {
38                 if (!strncmp(tmp->name, name, strlen(tmp->name))) {
39                         found = tmp;
40                         break;
41                 }
42         }
43         spin_unlock(&list_lock);
44         return found;
45 }
46
47 static struct scsi_device_handler *get_device_handler_by_idx(int idx)
48 {
49         struct scsi_device_handler *tmp, *found = NULL;
50
51         spin_lock(&list_lock);
52         list_for_each_entry(tmp, &scsi_dh_list, list) {
53                 if (tmp->idx == idx) {
54                         found = tmp;
55                         break;
56                 }
57         }
58         spin_unlock(&list_lock);
59         return found;
60 }
61
62 /*
63  * device_handler_match_function - Match a device handler to a device
64  * @sdev - SCSI device to be tested
65  *
66  * Tests @sdev against the match function of all registered device_handler.
67  * Returns the found device handler or NULL if not found.
68  */
69 static struct scsi_device_handler *
70 device_handler_match_function(struct scsi_device *sdev)
71 {
72         struct scsi_device_handler *tmp_dh, *found_dh = NULL;
73
74         spin_lock(&list_lock);
75         list_for_each_entry(tmp_dh, &scsi_dh_list, list) {
76                 if (tmp_dh->match && tmp_dh->match(sdev)) {
77                         found_dh = tmp_dh;
78                         break;
79                 }
80         }
81         spin_unlock(&list_lock);
82         return found_dh;
83 }
84
85 /*
86  * device_handler_match_devlist - Match a device handler to a device
87  * @sdev - SCSI device to be tested
88  *
89  * Tests @sdev against all device_handler registered in the devlist.
90  * Returns the found device handler or NULL if not found.
91  */
92 static struct scsi_device_handler *
93 device_handler_match_devlist(struct scsi_device *sdev)
94 {
95         int idx;
96
97         idx = scsi_get_device_flags_keyed(sdev, sdev->vendor, sdev->model,
98                                           SCSI_DEVINFO_DH);
99         return get_device_handler_by_idx(idx);
100 }
101
102 /*
103  * device_handler_match - Attach a device handler to a device
104  * @scsi_dh - The device handler to match against or NULL
105  * @sdev - SCSI device to be tested against @scsi_dh
106  *
107  * Tests @sdev against the device handler @scsi_dh or against
108  * all registered device_handler if @scsi_dh == NULL.
109  * Returns the found device handler or NULL if not found.
110  */
111 static struct scsi_device_handler *
112 device_handler_match(struct scsi_device_handler *scsi_dh,
113                      struct scsi_device *sdev)
114 {
115         struct scsi_device_handler *found_dh;
116
117         found_dh = device_handler_match_function(sdev);
118         if (!found_dh)
119                 found_dh = device_handler_match_devlist(sdev);
120
121         if (scsi_dh && found_dh != scsi_dh)
122                 found_dh = NULL;
123
124         return found_dh;
125 }
126
127 /*
128  * scsi_dh_handler_attach - Attach a device handler to a device
129  * @sdev - SCSI device the device handler should attach to
130  * @scsi_dh - The device handler to attach
131  */
132 static int scsi_dh_handler_attach(struct scsi_device *sdev,
133                                   struct scsi_device_handler *scsi_dh)
134 {
135         int err = 0;
136
137         if (sdev->scsi_dh_data) {
138                 if (sdev->scsi_dh_data->scsi_dh != scsi_dh)
139                         err = -EBUSY;
140                 else
141                         kref_get(&sdev->scsi_dh_data->kref);
142         } else if (scsi_dh->attach) {
143                 err = scsi_dh->attach(sdev);
144                 if (!err) {
145                         kref_init(&sdev->scsi_dh_data->kref);
146                         sdev->scsi_dh_data->sdev = sdev;
147                 }
148         }
149         return err;
150 }
151
152 static void __detach_handler (struct kref *kref)
153 {
154         struct scsi_dh_data *scsi_dh_data = container_of(kref, struct scsi_dh_data, kref);
155         scsi_dh_data->scsi_dh->detach(scsi_dh_data->sdev);
156 }
157
158 /*
159  * scsi_dh_handler_detach - Detach a device handler from a device
160  * @sdev - SCSI device the device handler should be detached from
161  * @scsi_dh - Device handler to be detached
162  *
163  * Detach from a device handler. If a device handler is specified,
164  * only detach if the currently attached handler matches @scsi_dh.
165  */
166 static void scsi_dh_handler_detach(struct scsi_device *sdev,
167                                    struct scsi_device_handler *scsi_dh)
168 {
169         if (!sdev->scsi_dh_data)
170                 return;
171
172         if (scsi_dh && scsi_dh != sdev->scsi_dh_data->scsi_dh)
173                 return;
174
175         if (!scsi_dh)
176                 scsi_dh = sdev->scsi_dh_data->scsi_dh;
177
178         if (scsi_dh && scsi_dh->detach)
179                 kref_put(&sdev->scsi_dh_data->kref, __detach_handler);
180 }
181
182 /*
183  * Functions for sysfs attribute 'dh_state'
184  */
185 static ssize_t
186 store_dh_state(struct device *dev, struct device_attribute *attr,
187                const char *buf, size_t count)
188 {
189         struct scsi_device *sdev = to_scsi_device(dev);
190         struct scsi_device_handler *scsi_dh;
191         int err = -EINVAL;
192
193         if (sdev->sdev_state == SDEV_CANCEL ||
194             sdev->sdev_state == SDEV_DEL)
195                 return -ENODEV;
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, NULL, NULL);
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                 err = device_create_file(dev, &scsi_dh_state_attr);
296                 /* don't care about err */
297                 devinfo = device_handler_match(NULL, sdev);
298                 if (devinfo)
299                         err = scsi_dh_handler_attach(sdev, devinfo);
300         } else if (action == BUS_NOTIFY_DEL_DEVICE) {
301                 device_remove_file(dev, &scsi_dh_state_attr);
302                 scsi_dh_handler_detach(sdev, NULL);
303         }
304         return err;
305 }
306
307 /*
308  * scsi_dh_notifier_add - Callback for scsi_register_device_handler
309  */
310 static int scsi_dh_notifier_add(struct device *dev, void *data)
311 {
312         struct scsi_device_handler *scsi_dh = data;
313         struct scsi_device *sdev;
314
315         if (!scsi_is_sdev_device(dev))
316                 return 0;
317
318         if (!get_device(dev))
319                 return 0;
320
321         sdev = to_scsi_device(dev);
322
323         if (device_handler_match(scsi_dh, sdev))
324                 scsi_dh_handler_attach(sdev, scsi_dh);
325
326         put_device(dev);
327
328         return 0;
329 }
330
331 /*
332  * scsi_dh_notifier_remove - Callback for scsi_unregister_device_handler
333  */
334 static int scsi_dh_notifier_remove(struct device *dev, void *data)
335 {
336         struct scsi_device_handler *scsi_dh = data;
337         struct scsi_device *sdev;
338
339         if (!scsi_is_sdev_device(dev))
340                 return 0;
341
342         if (!get_device(dev))
343                 return 0;
344
345         sdev = to_scsi_device(dev);
346
347         scsi_dh_handler_detach(sdev, scsi_dh);
348
349         put_device(dev);
350
351         return 0;
352 }
353
354 /*
355  * scsi_register_device_handler - register a device handler personality
356  *      module.
357  * @scsi_dh - device handler to be registered.
358  *
359  * Returns 0 on success, -EBUSY if handler already registered.
360  */
361 int scsi_register_device_handler(struct scsi_device_handler *scsi_dh)
362 {
363         int i;
364
365         if (get_device_handler(scsi_dh->name))
366                 return -EBUSY;
367
368         spin_lock(&list_lock);
369         scsi_dh->idx = scsi_dh_list_idx++;
370         list_add(&scsi_dh->list, &scsi_dh_list);
371         spin_unlock(&list_lock);
372
373         for (i = 0; scsi_dh->devlist && scsi_dh->devlist[i].vendor; i++) {
374                 scsi_dev_info_list_add_keyed(0,
375                                         scsi_dh->devlist[i].vendor,
376                                         scsi_dh->devlist[i].model,
377                                         NULL,
378                                         scsi_dh->idx,
379                                         SCSI_DEVINFO_DH);
380         }
381
382         bus_for_each_dev(&scsi_bus_type, NULL, scsi_dh, scsi_dh_notifier_add);
383         printk(KERN_INFO "%s: device handler registered\n", scsi_dh->name);
384
385         return SCSI_DH_OK;
386 }
387 EXPORT_SYMBOL_GPL(scsi_register_device_handler);
388
389 /*
390  * scsi_unregister_device_handler - register a device handler personality
391  *      module.
392  * @scsi_dh - device handler to be unregistered.
393  *
394  * Returns 0 on success, -ENODEV if handler not registered.
395  */
396 int scsi_unregister_device_handler(struct scsi_device_handler *scsi_dh)
397 {
398         int i;
399
400         if (!get_device_handler(scsi_dh->name))
401                 return -ENODEV;
402
403         bus_for_each_dev(&scsi_bus_type, NULL, scsi_dh,
404                          scsi_dh_notifier_remove);
405
406         for (i = 0; scsi_dh->devlist && scsi_dh->devlist[i].vendor; i++) {
407                 scsi_dev_info_list_del_keyed(scsi_dh->devlist[i].vendor,
408                                              scsi_dh->devlist[i].model,
409                                              SCSI_DEVINFO_DH);
410         }
411
412         spin_lock(&list_lock);
413         list_del(&scsi_dh->list);
414         spin_unlock(&list_lock);
415         printk(KERN_INFO "%s: device handler unregistered\n", scsi_dh->name);
416
417         return SCSI_DH_OK;
418 }
419 EXPORT_SYMBOL_GPL(scsi_unregister_device_handler);
420
421 /*
422  * scsi_dh_activate - activate the path associated with the scsi_device
423  *      corresponding to the given request queue.
424  *     Returns immediately without waiting for activation to be completed.
425  * @q    - Request queue that is associated with the scsi_device to be
426  *         activated.
427  * @fn   - Function to be called upon completion of the activation.
428  *         Function fn is called with data (below) and the error code.
429  *         Function fn may be called from the same calling context. So,
430  *         do not hold the lock in the caller which may be needed in fn.
431  * @data - data passed to the function fn upon completion.
432  *
433  */
434 int scsi_dh_activate(struct request_queue *q, activate_complete fn, void *data)
435 {
436         int err = 0;
437         unsigned long flags;
438         struct scsi_device *sdev;
439         struct scsi_device_handler *scsi_dh = NULL;
440         struct device *dev = NULL;
441
442         spin_lock_irqsave(q->queue_lock, flags);
443         sdev = q->queuedata;
444         if (sdev && sdev->scsi_dh_data)
445                 scsi_dh = sdev->scsi_dh_data->scsi_dh;
446         dev = get_device(&sdev->sdev_gendev);
447         if (!scsi_dh || !dev ||
448             sdev->sdev_state == SDEV_CANCEL ||
449             sdev->sdev_state == SDEV_DEL)
450                 err = SCSI_DH_NOSYS;
451         if (sdev->sdev_state == SDEV_OFFLINE)
452                 err = SCSI_DH_DEV_OFFLINED;
453         spin_unlock_irqrestore(q->queue_lock, flags);
454
455         if (err) {
456                 if (fn)
457                         fn(data, err);
458                 goto out;
459         }
460
461         if (scsi_dh->activate)
462                 err = scsi_dh->activate(sdev, fn, data);
463 out:
464         put_device(dev);
465         return err;
466 }
467 EXPORT_SYMBOL_GPL(scsi_dh_activate);
468
469 /*
470  * scsi_dh_set_params - set the parameters for the device as per the
471  *      string specified in params.
472  * @q - Request queue that is associated with the scsi_device for
473  *      which the parameters to be set.
474  * @params - parameters in the following format
475  *      "no_of_params\0param1\0param2\0param3\0...\0"
476  *      for example, string for 2 parameters with value 10 and 21
477  *      is specified as "2\010\021\0".
478  */
479 int scsi_dh_set_params(struct request_queue *q, const char *params)
480 {
481         int err = -SCSI_DH_NOSYS;
482         unsigned long flags;
483         struct scsi_device *sdev;
484         struct scsi_device_handler *scsi_dh = NULL;
485
486         spin_lock_irqsave(q->queue_lock, flags);
487         sdev = q->queuedata;
488         if (sdev && sdev->scsi_dh_data)
489                 scsi_dh = sdev->scsi_dh_data->scsi_dh;
490         if (scsi_dh && scsi_dh->set_params && get_device(&sdev->sdev_gendev))
491                 err = 0;
492         spin_unlock_irqrestore(q->queue_lock, flags);
493
494         if (err)
495                 return err;
496         err = scsi_dh->set_params(sdev, params);
497         put_device(&sdev->sdev_gendev);
498         return err;
499 }
500 EXPORT_SYMBOL_GPL(scsi_dh_set_params);
501
502 /*
503  * scsi_dh_handler_exist - Return TRUE(1) if a device handler exists for
504  *      the given name. FALSE(0) otherwise.
505  * @name - name of the device handler.
506  */
507 int scsi_dh_handler_exist(const char *name)
508 {
509         return (get_device_handler(name) != NULL);
510 }
511 EXPORT_SYMBOL_GPL(scsi_dh_handler_exist);
512
513 /*
514  * scsi_dh_attach - Attach device handler
515  * @sdev - sdev the handler should be attached to
516  * @name - name of the handler to attach
517  */
518 int scsi_dh_attach(struct request_queue *q, const char *name)
519 {
520         unsigned long flags;
521         struct scsi_device *sdev;
522         struct scsi_device_handler *scsi_dh;
523         int err = 0;
524
525         scsi_dh = get_device_handler(name);
526         if (!scsi_dh)
527                 return -EINVAL;
528
529         spin_lock_irqsave(q->queue_lock, flags);
530         sdev = q->queuedata;
531         if (!sdev || !get_device(&sdev->sdev_gendev))
532                 err = -ENODEV;
533         spin_unlock_irqrestore(q->queue_lock, flags);
534
535         if (!err) {
536                 err = scsi_dh_handler_attach(sdev, scsi_dh);
537                 put_device(&sdev->sdev_gendev);
538         }
539         return err;
540 }
541 EXPORT_SYMBOL_GPL(scsi_dh_attach);
542
543 /*
544  * scsi_dh_detach - Detach device handler
545  * @sdev - sdev the handler should be detached from
546  *
547  * This function will detach the device handler only
548  * if the sdev is not part of the internal list, ie
549  * if it has been attached manually.
550  */
551 void scsi_dh_detach(struct request_queue *q)
552 {
553         unsigned long flags;
554         struct scsi_device *sdev;
555         struct scsi_device_handler *scsi_dh = NULL;
556
557         spin_lock_irqsave(q->queue_lock, flags);
558         sdev = q->queuedata;
559         if (!sdev || !get_device(&sdev->sdev_gendev))
560                 sdev = NULL;
561         spin_unlock_irqrestore(q->queue_lock, flags);
562
563         if (!sdev)
564                 return;
565
566         if (sdev->scsi_dh_data) {
567                 scsi_dh = sdev->scsi_dh_data->scsi_dh;
568                 scsi_dh_handler_detach(sdev, scsi_dh);
569         }
570         put_device(&sdev->sdev_gendev);
571 }
572 EXPORT_SYMBOL_GPL(scsi_dh_detach);
573
574 static struct notifier_block scsi_dh_nb = {
575         .notifier_call = scsi_dh_notifier
576 };
577
578 static int __init scsi_dh_init(void)
579 {
580         int r;
581
582         r = scsi_dev_info_add_list(SCSI_DEVINFO_DH, "SCSI Device Handler");
583         if (r)
584                 return r;
585
586         r = bus_register_notifier(&scsi_bus_type, &scsi_dh_nb);
587
588         if (!r)
589                 bus_for_each_dev(&scsi_bus_type, NULL, NULL,
590                                  scsi_dh_sysfs_attr_add);
591
592         return r;
593 }
594
595 static void __exit scsi_dh_exit(void)
596 {
597         bus_for_each_dev(&scsi_bus_type, NULL, NULL,
598                          scsi_dh_sysfs_attr_remove);
599         bus_unregister_notifier(&scsi_bus_type, &scsi_dh_nb);
600         scsi_dev_info_remove_list(SCSI_DEVINFO_DH);
601 }
602
603 module_init(scsi_dh_init);
604 module_exit(scsi_dh_exit);
605
606 MODULE_DESCRIPTION("SCSI device handler");
607 MODULE_AUTHOR("Chandra Seetharaman <sekharan@us.ibm.com>");
608 MODULE_LICENSE("GPL");