aic94xx: Skip reading user settings if flash is not found
[linux-2.6-block.git] / drivers / scsi / device_handler / scsi_dh.c
CommitLineData
a6a8d9f8
CS
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
5a0e3ad6 24#include <linux/slab.h>
acf3368f 25#include <linux/module.h>
a6a8d9f8
CS
26#include <scsi/scsi_dh.h>
27#include "../scsi_priv.h"
28
29static DEFINE_SPINLOCK(list_lock);
30static LIST_HEAD(scsi_dh_list);
31
32static 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) {
765cbc6d 38 if (!strncmp(tmp->name, name, strlen(tmp->name))) {
a6a8d9f8
CS
39 found = tmp;
40 break;
41 }
42 }
43 spin_unlock(&list_lock);
44 return found;
45}
46
6c3633d0
HR
47/*
48 * device_handler_match_function - Match a device handler to a device
49 * @sdev - SCSI device to be tested
50 *
51 * Tests @sdev against the match function of all registered device_handler.
52 * Returns the found device handler or NULL if not found.
53 */
54static struct scsi_device_handler *
55device_handler_match_function(struct scsi_device *sdev)
56{
57 struct scsi_device_handler *tmp_dh, *found_dh = NULL;
58
59 spin_lock(&list_lock);
60 list_for_each_entry(tmp_dh, &scsi_dh_list, list) {
61 if (tmp_dh->match && tmp_dh->match(sdev)) {
62 found_dh = tmp_dh;
63 break;
64 }
65 }
66 spin_unlock(&list_lock);
67 return found_dh;
68}
69
7c32c7a2
HR
70/*
71 * device_handler_match - Attach a device handler to a device
72 * @scsi_dh - The device handler to match against or NULL
73 * @sdev - SCSI device to be tested against @scsi_dh
74 *
75 * Tests @sdev against the device handler @scsi_dh or against
76 * all registered device_handler if @scsi_dh == NULL.
77 * Returns the found device handler or NULL if not found.
78 */
79static struct scsi_device_handler *
80device_handler_match(struct scsi_device_handler *scsi_dh,
81 struct scsi_device *sdev)
82{
6c3633d0 83 struct scsi_device_handler *found_dh;
7c32c7a2 84
6c3633d0 85 found_dh = device_handler_match_function(sdev);
7c32c7a2 86
940d7faa
PJ
87 if (scsi_dh && found_dh != scsi_dh)
88 found_dh = NULL;
7c32c7a2
HR
89
90 return found_dh;
a6a8d9f8
CS
91}
92
93/*
765cbc6d
HR
94 * scsi_dh_handler_attach - Attach a device handler to a device
95 * @sdev - SCSI device the device handler should attach to
96 * @scsi_dh - The device handler to attach
97 */
98static int scsi_dh_handler_attach(struct scsi_device *sdev,
99 struct scsi_device_handler *scsi_dh)
100{
1d520328 101 struct scsi_dh_data *d;
765cbc6d
HR
102
103 if (sdev->scsi_dh_data) {
104 if (sdev->scsi_dh_data->scsi_dh != scsi_dh)
27c888f0
CH
105 return -EBUSY;
106
107 kref_get(&sdev->scsi_dh_data->kref);
108 return 0;
109 }
110
1f12ffa5
CH
111 if (!try_module_get(scsi_dh->module))
112 return -EINVAL;
27c888f0 113
1d520328
CH
114 d = scsi_dh->attach(sdev);
115 if (IS_ERR(d)) {
116 sdev_printk(KERN_ERR, sdev, "%s: Attach failed (%ld)\n",
117 scsi_dh->name, PTR_ERR(d));
1f12ffa5 118 module_put(scsi_dh->module);
1d520328 119 return PTR_ERR(d);
6c10db72 120 }
1f12ffa5 121
1d520328
CH
122 d->scsi_dh = scsi_dh;
123 kref_init(&d->kref);
124 d->sdev = sdev;
125
126 spin_lock_irq(sdev->request_queue->queue_lock);
127 sdev->scsi_dh_data = d;
128 spin_unlock_irq(sdev->request_queue->queue_lock);
129 return 0;
765cbc6d
HR
130}
131
6c10db72
CS
132static void __detach_handler (struct kref *kref)
133{
27c888f0
CH
134 struct scsi_dh_data *scsi_dh_data =
135 container_of(kref, struct scsi_dh_data, kref);
136 struct scsi_device_handler *scsi_dh = scsi_dh_data->scsi_dh;
1d520328
CH
137 struct scsi_device *sdev = scsi_dh_data->sdev;
138
28072ad5
MC
139 scsi_dh->detach(sdev);
140
1d520328
CH
141 spin_lock_irq(sdev->request_queue->queue_lock);
142 sdev->scsi_dh_data = NULL;
143 spin_unlock_irq(sdev->request_queue->queue_lock);
27c888f0 144
1d520328 145 sdev_printk(KERN_NOTICE, sdev, "%s: Detached\n", scsi_dh->name);
27c888f0 146 module_put(scsi_dh->module);
6c10db72
CS
147}
148
765cbc6d
HR
149/*
150 * scsi_dh_handler_detach - Detach a device handler from a device
151 * @sdev - SCSI device the device handler should be detached from
152 * @scsi_dh - Device handler to be detached
a6a8d9f8 153 *
765cbc6d 154 * Detach from a device handler. If a device handler is specified,
4c05ae52 155 * only detach if the currently attached handler matches @scsi_dh.
a6a8d9f8 156 */
765cbc6d
HR
157static void scsi_dh_handler_detach(struct scsi_device *sdev,
158 struct scsi_device_handler *scsi_dh)
a6a8d9f8 159{
765cbc6d
HR
160 if (!sdev->scsi_dh_data)
161 return;
a6a8d9f8 162
765cbc6d
HR
163 if (scsi_dh && scsi_dh != sdev->scsi_dh_data->scsi_dh)
164 return;
a6a8d9f8 165
765cbc6d
HR
166 if (!scsi_dh)
167 scsi_dh = sdev->scsi_dh_data->scsi_dh;
168
1f12ffa5 169 if (scsi_dh)
6c10db72 170 kref_put(&sdev->scsi_dh_data->kref, __detach_handler);
765cbc6d
HR
171}
172
4c05ae52
HR
173/*
174 * Functions for sysfs attribute 'dh_state'
175 */
176static ssize_t
177store_dh_state(struct device *dev, struct device_attribute *attr,
178 const char *buf, size_t count)
179{
180 struct scsi_device *sdev = to_scsi_device(dev);
181 struct scsi_device_handler *scsi_dh;
182 int err = -EINVAL;
183
6bc8d2a0
HR
184 if (sdev->sdev_state == SDEV_CANCEL ||
185 sdev->sdev_state == SDEV_DEL)
186 return -ENODEV;
187
4c05ae52
HR
188 if (!sdev->scsi_dh_data) {
189 /*
190 * Attach to a device handler
191 */
192 if (!(scsi_dh = get_device_handler(buf)))
193 return err;
194 err = scsi_dh_handler_attach(sdev, scsi_dh);
195 } else {
196 scsi_dh = sdev->scsi_dh_data->scsi_dh;
197 if (!strncmp(buf, "detach", 6)) {
198 /*
199 * Detach from a device handler
200 */
201 scsi_dh_handler_detach(sdev, scsi_dh);
202 err = 0;
203 } else if (!strncmp(buf, "activate", 8)) {
204 /*
205 * Activate a device handler
206 */
207 if (scsi_dh->activate)
3ae31f6a 208 err = scsi_dh->activate(sdev, NULL, NULL);
4c05ae52
HR
209 else
210 err = 0;
211 }
212 }
213
214 return err<0?err:count;
215}
216
217static ssize_t
218show_dh_state(struct device *dev, struct device_attribute *attr, char *buf)
219{
220 struct scsi_device *sdev = to_scsi_device(dev);
221
222 if (!sdev->scsi_dh_data)
223 return snprintf(buf, 20, "detached\n");
224
225 return snprintf(buf, 20, "%s\n", sdev->scsi_dh_data->scsi_dh->name);
226}
227
228static struct device_attribute scsi_dh_state_attr =
229 __ATTR(dh_state, S_IRUGO | S_IWUSR, show_dh_state,
230 store_dh_state);
231
232/*
233 * scsi_dh_sysfs_attr_add - Callback for scsi_init_dh
234 */
235static int scsi_dh_sysfs_attr_add(struct device *dev, void *data)
236{
237 struct scsi_device *sdev;
238 int err;
239
240 if (!scsi_is_sdev_device(dev))
241 return 0;
242
243 sdev = to_scsi_device(dev);
244
245 err = device_create_file(&sdev->sdev_gendev,
246 &scsi_dh_state_attr);
247
248 return 0;
249}
250
251/*
252 * scsi_dh_sysfs_attr_remove - Callback for scsi_exit_dh
253 */
254static int scsi_dh_sysfs_attr_remove(struct device *dev, void *data)
255{
256 struct scsi_device *sdev;
257
258 if (!scsi_is_sdev_device(dev))
259 return 0;
260
261 sdev = to_scsi_device(dev);
262
263 device_remove_file(&sdev->sdev_gendev,
264 &scsi_dh_state_attr);
265
266 return 0;
267}
268
765cbc6d
HR
269/*
270 * scsi_dh_notifier - notifier chain callback
271 */
272static int scsi_dh_notifier(struct notifier_block *nb,
273 unsigned long action, void *data)
274{
275 struct device *dev = data;
276 struct scsi_device *sdev;
277 int err = 0;
7c32c7a2 278 struct scsi_device_handler *devinfo = NULL;
765cbc6d
HR
279
280 if (!scsi_is_sdev_device(dev))
281 return 0;
282
283 sdev = to_scsi_device(dev);
a6a8d9f8 284
765cbc6d 285 if (action == BUS_NOTIFY_ADD_DEVICE) {
5917290c
CS
286 err = device_create_file(dev, &scsi_dh_state_attr);
287 /* don't care about err */
7c32c7a2 288 devinfo = device_handler_match(NULL, sdev);
5917290c
CS
289 if (devinfo)
290 err = scsi_dh_handler_attach(sdev, devinfo);
765cbc6d 291 } else if (action == BUS_NOTIFY_DEL_DEVICE) {
4c05ae52 292 device_remove_file(dev, &scsi_dh_state_attr);
765cbc6d
HR
293 scsi_dh_handler_detach(sdev, NULL);
294 }
765cbc6d 295 return err;
a6a8d9f8 296}
a6a8d9f8 297
765cbc6d
HR
298/*
299 * scsi_dh_notifier_add - Callback for scsi_register_device_handler
300 */
301static int scsi_dh_notifier_add(struct device *dev, void *data)
302{
303 struct scsi_device_handler *scsi_dh = data;
304 struct scsi_device *sdev;
305
306 if (!scsi_is_sdev_device(dev))
307 return 0;
308
309 if (!get_device(dev))
310 return 0;
311
312 sdev = to_scsi_device(dev);
313
314 if (device_handler_match(scsi_dh, sdev))
315 scsi_dh_handler_attach(sdev, scsi_dh);
316
317 put_device(dev);
318
319 return 0;
320}
321
322/*
323 * scsi_dh_notifier_remove - Callback for scsi_unregister_device_handler
324 */
a6a8d9f8
CS
325static int scsi_dh_notifier_remove(struct device *dev, void *data)
326{
327 struct scsi_device_handler *scsi_dh = data;
765cbc6d
HR
328 struct scsi_device *sdev;
329
330 if (!scsi_is_sdev_device(dev))
331 return 0;
332
333 if (!get_device(dev))
334 return 0;
335
336 sdev = to_scsi_device(dev);
337
338 scsi_dh_handler_detach(sdev, scsi_dh);
339
340 put_device(dev);
a6a8d9f8 341
a6a8d9f8
CS
342 return 0;
343}
344
765cbc6d
HR
345/*
346 * scsi_register_device_handler - register a device handler personality
347 * module.
348 * @scsi_dh - device handler to be registered.
349 *
350 * Returns 0 on success, -EBUSY if handler already registered.
351 */
352int scsi_register_device_handler(struct scsi_device_handler *scsi_dh)
353{
940d7faa 354
765cbc6d
HR
355 if (get_device_handler(scsi_dh->name))
356 return -EBUSY;
357
1f12ffa5
CH
358 if (!scsi_dh->attach || !scsi_dh->detach)
359 return -EINVAL;
360
765cbc6d
HR
361 spin_lock(&list_lock);
362 list_add(&scsi_dh->list, &scsi_dh_list);
363 spin_unlock(&list_lock);
940d7faa 364
765cbc6d
HR
365 bus_for_each_dev(&scsi_bus_type, NULL, scsi_dh, scsi_dh_notifier_add);
366 printk(KERN_INFO "%s: device handler registered\n", scsi_dh->name);
367
368 return SCSI_DH_OK;
369}
370EXPORT_SYMBOL_GPL(scsi_register_device_handler);
371
a6a8d9f8
CS
372/*
373 * scsi_unregister_device_handler - register a device handler personality
374 * module.
375 * @scsi_dh - device handler to be unregistered.
376 *
377 * Returns 0 on success, -ENODEV if handler not registered.
378 */
379int scsi_unregister_device_handler(struct scsi_device_handler *scsi_dh)
380{
7c32c7a2 381
765cbc6d
HR
382 if (!get_device_handler(scsi_dh->name))
383 return -ENODEV;
a6a8d9f8
CS
384
385 bus_for_each_dev(&scsi_bus_type, NULL, scsi_dh,
765cbc6d
HR
386 scsi_dh_notifier_remove);
387
a6a8d9f8
CS
388 spin_lock(&list_lock);
389 list_del(&scsi_dh->list);
390 spin_unlock(&list_lock);
765cbc6d 391 printk(KERN_INFO "%s: device handler unregistered\n", scsi_dh->name);
a6a8d9f8 392
765cbc6d 393 return SCSI_DH_OK;
a6a8d9f8
CS
394}
395EXPORT_SYMBOL_GPL(scsi_unregister_device_handler);
396
397/*
398 * scsi_dh_activate - activate the path associated with the scsi_device
399 * corresponding to the given request queue.
3ae31f6a
CS
400 * Returns immediately without waiting for activation to be completed.
401 * @q - Request queue that is associated with the scsi_device to be
402 * activated.
403 * @fn - Function to be called upon completion of the activation.
404 * Function fn is called with data (below) and the error code.
405 * Function fn may be called from the same calling context. So,
406 * do not hold the lock in the caller which may be needed in fn.
407 * @data - data passed to the function fn upon completion.
408 *
a6a8d9f8 409 */
3ae31f6a 410int scsi_dh_activate(struct request_queue *q, activate_complete fn, void *data)
a6a8d9f8
CS
411{
412 int err = 0;
413 unsigned long flags;
414 struct scsi_device *sdev;
415 struct scsi_device_handler *scsi_dh = NULL;
0b839357 416 struct device *dev = NULL;
a6a8d9f8
CS
417
418 spin_lock_irqsave(q->queue_lock, flags);
419 sdev = q->queuedata;
a18a920c
MB
420 if (!sdev) {
421 spin_unlock_irqrestore(q->queue_lock, flags);
422 err = SCSI_DH_NOSYS;
423 if (fn)
424 fn(data, err);
425 return err;
426 }
427
428 if (sdev->scsi_dh_data)
a6a8d9f8 429 scsi_dh = sdev->scsi_dh_data->scsi_dh;
0b839357
MS
430 dev = get_device(&sdev->sdev_gendev);
431 if (!scsi_dh || !dev ||
db422318
MH
432 sdev->sdev_state == SDEV_CANCEL ||
433 sdev->sdev_state == SDEV_DEL)
a6a8d9f8 434 err = SCSI_DH_NOSYS;
db422318
MH
435 if (sdev->sdev_state == SDEV_OFFLINE)
436 err = SCSI_DH_DEV_OFFLINED;
a6a8d9f8
CS
437 spin_unlock_irqrestore(q->queue_lock, flags);
438
db422318
MH
439 if (err) {
440 if (fn)
441 fn(data, err);
0b839357 442 goto out;
db422318 443 }
a6a8d9f8
CS
444
445 if (scsi_dh->activate)
3ae31f6a 446 err = scsi_dh->activate(sdev, fn, data);
0b839357
MS
447out:
448 put_device(dev);
a6a8d9f8
CS
449 return err;
450}
451EXPORT_SYMBOL_GPL(scsi_dh_activate);
452
18ee70c9
CS
453/*
454 * scsi_dh_set_params - set the parameters for the device as per the
455 * string specified in params.
456 * @q - Request queue that is associated with the scsi_device for
457 * which the parameters to be set.
458 * @params - parameters in the following format
459 * "no_of_params\0param1\0param2\0param3\0...\0"
460 * for example, string for 2 parameters with value 10 and 21
461 * is specified as "2\010\021\0".
462 */
463int scsi_dh_set_params(struct request_queue *q, const char *params)
464{
465 int err = -SCSI_DH_NOSYS;
466 unsigned long flags;
467 struct scsi_device *sdev;
468 struct scsi_device_handler *scsi_dh = NULL;
469
470 spin_lock_irqsave(q->queue_lock, flags);
471 sdev = q->queuedata;
472 if (sdev && sdev->scsi_dh_data)
473 scsi_dh = sdev->scsi_dh_data->scsi_dh;
474 if (scsi_dh && scsi_dh->set_params && get_device(&sdev->sdev_gendev))
475 err = 0;
476 spin_unlock_irqrestore(q->queue_lock, flags);
477
478 if (err)
479 return err;
480 err = scsi_dh->set_params(sdev, params);
481 put_device(&sdev->sdev_gendev);
482 return err;
483}
484EXPORT_SYMBOL_GPL(scsi_dh_set_params);
485
a6a8d9f8
CS
486/*
487 * scsi_dh_handler_exist - Return TRUE(1) if a device handler exists for
488 * the given name. FALSE(0) otherwise.
489 * @name - name of the device handler.
490 */
491int scsi_dh_handler_exist(const char *name)
492{
493 return (get_device_handler(name) != NULL);
494}
495EXPORT_SYMBOL_GPL(scsi_dh_handler_exist);
496
ae11b1b3 497/*
2a9ab40f 498 * scsi_dh_attach - Attach device handler
7e8a74b1
MS
499 * @q - Request queue that is associated with the scsi_device
500 * the handler should be attached to
ae11b1b3
HR
501 * @name - name of the handler to attach
502 */
503int scsi_dh_attach(struct request_queue *q, const char *name)
504{
505 unsigned long flags;
506 struct scsi_device *sdev;
507 struct scsi_device_handler *scsi_dh;
508 int err = 0;
509
510 scsi_dh = get_device_handler(name);
511 if (!scsi_dh)
512 return -EINVAL;
513
514 spin_lock_irqsave(q->queue_lock, flags);
515 sdev = q->queuedata;
516 if (!sdev || !get_device(&sdev->sdev_gendev))
517 err = -ENODEV;
518 spin_unlock_irqrestore(q->queue_lock, flags);
519
520 if (!err) {
521 err = scsi_dh_handler_attach(sdev, scsi_dh);
ae11b1b3
HR
522 put_device(&sdev->sdev_gendev);
523 }
524 return err;
525}
526EXPORT_SYMBOL_GPL(scsi_dh_attach);
527
528/*
2a9ab40f 529 * scsi_dh_detach - Detach device handler
7e8a74b1
MS
530 * @q - Request queue that is associated with the scsi_device
531 * the handler should be detached from
ae11b1b3
HR
532 *
533 * This function will detach the device handler only
534 * if the sdev is not part of the internal list, ie
535 * if it has been attached manually.
536 */
537void scsi_dh_detach(struct request_queue *q)
538{
539 unsigned long flags;
540 struct scsi_device *sdev;
541 struct scsi_device_handler *scsi_dh = NULL;
542
543 spin_lock_irqsave(q->queue_lock, flags);
544 sdev = q->queuedata;
545 if (!sdev || !get_device(&sdev->sdev_gendev))
546 sdev = NULL;
547 spin_unlock_irqrestore(q->queue_lock, flags);
548
549 if (!sdev)
550 return;
551
552 if (sdev->scsi_dh_data) {
ae11b1b3 553 scsi_dh = sdev->scsi_dh_data->scsi_dh;
6c10db72 554 scsi_dh_handler_detach(sdev, scsi_dh);
ae11b1b3
HR
555 }
556 put_device(&sdev->sdev_gendev);
557}
558EXPORT_SYMBOL_GPL(scsi_dh_detach);
559
7e8a74b1
MS
560/*
561 * scsi_dh_attached_handler_name - Get attached device handler's name
562 * @q - Request queue that is associated with the scsi_device
563 * that may have a device handler attached
564 * @gfp - the GFP mask used in the kmalloc() call when allocating memory
565 *
566 * Returns name of attached handler, NULL if no handler is attached.
567 * Caller must take care to free the returned string.
568 */
569const char *scsi_dh_attached_handler_name(struct request_queue *q, gfp_t gfp)
570{
571 unsigned long flags;
572 struct scsi_device *sdev;
573 const char *handler_name = NULL;
574
575 spin_lock_irqsave(q->queue_lock, flags);
576 sdev = q->queuedata;
577 if (!sdev || !get_device(&sdev->sdev_gendev))
578 sdev = NULL;
579 spin_unlock_irqrestore(q->queue_lock, flags);
580
581 if (!sdev)
582 return NULL;
583
584 if (sdev->scsi_dh_data)
585 handler_name = kstrdup(sdev->scsi_dh_data->scsi_dh->name, gfp);
586
587 put_device(&sdev->sdev_gendev);
588 return handler_name;
589}
590EXPORT_SYMBOL_GPL(scsi_dh_attached_handler_name);
591
765cbc6d
HR
592static struct notifier_block scsi_dh_nb = {
593 .notifier_call = scsi_dh_notifier
594};
595
596static int __init scsi_dh_init(void)
597{
598 int r;
599
600 r = bus_register_notifier(&scsi_bus_type, &scsi_dh_nb);
601
4c05ae52
HR
602 if (!r)
603 bus_for_each_dev(&scsi_bus_type, NULL, NULL,
604 scsi_dh_sysfs_attr_add);
605
765cbc6d
HR
606 return r;
607}
608
609static void __exit scsi_dh_exit(void)
610{
4c05ae52
HR
611 bus_for_each_dev(&scsi_bus_type, NULL, NULL,
612 scsi_dh_sysfs_attr_remove);
765cbc6d
HR
613 bus_unregister_notifier(&scsi_bus_type, &scsi_dh_nb);
614}
615
616module_init(scsi_dh_init);
617module_exit(scsi_dh_exit);
618
a6a8d9f8
CS
619MODULE_DESCRIPTION("SCSI device handler");
620MODULE_AUTHOR("Chandra Seetharaman <sekharan@us.ibm.com>");
621MODULE_LICENSE("GPL");