MIPS: Yosemite, Emma: Fix off-by-two in arcs_cmdline buffer size check
[linux-2.6/linux-mips.git] / drivers / scsi / device_handler / scsi_dh.c
blob7c05fd9dccfd1259133adaa1691df648cff56521
1 /*
2 * SCSI device handler infrastruture.
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.
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.
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.
18 * Copyright IBM Corporation, 2007
19 * Authors:
20 * Chandra Seetharaman <sekharan@us.ibm.com>
21 * Mike Anderson <andmike@linux.vnet.ibm.com>
24 #include <linux/slab.h>
25 #include <scsi/scsi_dh.h>
26 #include "../scsi_priv.h"
28 static DEFINE_SPINLOCK(list_lock);
29 static LIST_HEAD(scsi_dh_list);
30 static int scsi_dh_list_idx = 1;
32 static struct scsi_device_handler *get_device_handler(const char *name)
34 struct scsi_device_handler *tmp, *found = NULL;
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;
43 spin_unlock(&list_lock);
44 return found;
47 static struct scsi_device_handler *get_device_handler_by_idx(int idx)
49 struct scsi_device_handler *tmp, *found = NULL;
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;
58 spin_unlock(&list_lock);
59 return found;
63 * device_handler_match_function - Match a device handler to a device
64 * @sdev - SCSI device to be tested
66 * Tests @sdev against the match function of all registered device_handler.
67 * Returns the found device handler or NULL if not found.
69 static struct scsi_device_handler *
70 device_handler_match_function(struct scsi_device *sdev)
72 struct scsi_device_handler *tmp_dh, *found_dh = NULL;
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;
81 spin_unlock(&list_lock);
82 return found_dh;
86 * device_handler_match_devlist - Match a device handler to a device
87 * @sdev - SCSI device to be tested
89 * Tests @sdev against all device_handler registered in the devlist.
90 * Returns the found device handler or NULL if not found.
92 static struct scsi_device_handler *
93 device_handler_match_devlist(struct scsi_device *sdev)
95 int idx;
97 idx = scsi_get_device_flags_keyed(sdev, sdev->vendor, sdev->model,
98 SCSI_DEVINFO_DH);
99 return get_device_handler_by_idx(idx);
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
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.
111 static struct scsi_device_handler *
112 device_handler_match(struct scsi_device_handler *scsi_dh,
113 struct scsi_device *sdev)
115 struct scsi_device_handler *found_dh;
117 found_dh = device_handler_match_function(sdev);
118 if (!found_dh)
119 found_dh = device_handler_match_devlist(sdev);
121 if (scsi_dh && found_dh != scsi_dh)
122 found_dh = NULL;
124 return found_dh;
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
132 static int scsi_dh_handler_attach(struct scsi_device *sdev,
133 struct scsi_device_handler *scsi_dh)
135 int err = 0;
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;
149 return err;
152 static void __detach_handler (struct kref *kref)
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);
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
163 * Detach from a device handler. If a device handler is specified,
164 * only detach if the currently attached handler matches @scsi_dh.
166 static void scsi_dh_handler_detach(struct scsi_device *sdev,
167 struct scsi_device_handler *scsi_dh)
169 if (!sdev->scsi_dh_data)
170 return;
172 if (scsi_dh && scsi_dh != sdev->scsi_dh_data->scsi_dh)
173 return;
175 if (!scsi_dh)
176 scsi_dh = sdev->scsi_dh_data->scsi_dh;
178 if (scsi_dh && scsi_dh->detach)
179 kref_put(&sdev->scsi_dh_data->kref, __detach_handler);
183 * Functions for sysfs attribute 'dh_state'
185 static ssize_t
186 store_dh_state(struct device *dev, struct device_attribute *attr,
187 const char *buf, size_t count)
189 struct scsi_device *sdev = to_scsi_device(dev);
190 struct scsi_device_handler *scsi_dh;
191 int err = -EINVAL;
193 if (sdev->sdev_state == SDEV_CANCEL ||
194 sdev->sdev_state == SDEV_DEL)
195 return -ENODEV;
197 if (!sdev->scsi_dh_data) {
199 * Attach to a device handler
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)) {
208 * Detach from a device handler
210 scsi_dh_handler_detach(sdev, scsi_dh);
211 err = 0;
212 } else if (!strncmp(buf, "activate", 8)) {
214 * Activate a device handler
216 if (scsi_dh->activate)
217 err = scsi_dh->activate(sdev, NULL, NULL);
218 else
219 err = 0;
223 return err<0?err:count;
226 static ssize_t
227 show_dh_state(struct device *dev, struct device_attribute *attr, char *buf)
229 struct scsi_device *sdev = to_scsi_device(dev);
231 if (!sdev->scsi_dh_data)
232 return snprintf(buf, 20, "detached\n");
234 return snprintf(buf, 20, "%s\n", sdev->scsi_dh_data->scsi_dh->name);
237 static struct device_attribute scsi_dh_state_attr =
238 __ATTR(dh_state, S_IRUGO | S_IWUSR, show_dh_state,
239 store_dh_state);
242 * scsi_dh_sysfs_attr_add - Callback for scsi_init_dh
244 static int scsi_dh_sysfs_attr_add(struct device *dev, void *data)
246 struct scsi_device *sdev;
247 int err;
249 if (!scsi_is_sdev_device(dev))
250 return 0;
252 sdev = to_scsi_device(dev);
254 err = device_create_file(&sdev->sdev_gendev,
255 &scsi_dh_state_attr);
257 return 0;
261 * scsi_dh_sysfs_attr_remove - Callback for scsi_exit_dh
263 static int scsi_dh_sysfs_attr_remove(struct device *dev, void *data)
265 struct scsi_device *sdev;
267 if (!scsi_is_sdev_device(dev))
268 return 0;
270 sdev = to_scsi_device(dev);
272 device_remove_file(&sdev->sdev_gendev,
273 &scsi_dh_state_attr);
275 return 0;
279 * scsi_dh_notifier - notifier chain callback
281 static int scsi_dh_notifier(struct notifier_block *nb,
282 unsigned long action, void *data)
284 struct device *dev = data;
285 struct scsi_device *sdev;
286 int err = 0;
287 struct scsi_device_handler *devinfo = NULL;
289 if (!scsi_is_sdev_device(dev))
290 return 0;
292 sdev = to_scsi_device(dev);
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);
304 return err;
308 * scsi_dh_notifier_add - Callback for scsi_register_device_handler
310 static int scsi_dh_notifier_add(struct device *dev, void *data)
312 struct scsi_device_handler *scsi_dh = data;
313 struct scsi_device *sdev;
315 if (!scsi_is_sdev_device(dev))
316 return 0;
318 if (!get_device(dev))
319 return 0;
321 sdev = to_scsi_device(dev);
323 if (device_handler_match(scsi_dh, sdev))
324 scsi_dh_handler_attach(sdev, scsi_dh);
326 put_device(dev);
328 return 0;
332 * scsi_dh_notifier_remove - Callback for scsi_unregister_device_handler
334 static int scsi_dh_notifier_remove(struct device *dev, void *data)
336 struct scsi_device_handler *scsi_dh = data;
337 struct scsi_device *sdev;
339 if (!scsi_is_sdev_device(dev))
340 return 0;
342 if (!get_device(dev))
343 return 0;
345 sdev = to_scsi_device(dev);
347 scsi_dh_handler_detach(sdev, scsi_dh);
349 put_device(dev);
351 return 0;
355 * scsi_register_device_handler - register a device handler personality
356 * module.
357 * @scsi_dh - device handler to be registered.
359 * Returns 0 on success, -EBUSY if handler already registered.
361 int scsi_register_device_handler(struct scsi_device_handler *scsi_dh)
363 int i;
365 if (get_device_handler(scsi_dh->name))
366 return -EBUSY;
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);
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);
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);
385 return SCSI_DH_OK;
387 EXPORT_SYMBOL_GPL(scsi_register_device_handler);
390 * scsi_unregister_device_handler - register a device handler personality
391 * module.
392 * @scsi_dh - device handler to be unregistered.
394 * Returns 0 on success, -ENODEV if handler not registered.
396 int scsi_unregister_device_handler(struct scsi_device_handler *scsi_dh)
398 int i;
400 if (!get_device_handler(scsi_dh->name))
401 return -ENODEV;
403 bus_for_each_dev(&scsi_bus_type, NULL, scsi_dh,
404 scsi_dh_notifier_remove);
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);
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);
417 return SCSI_DH_OK;
419 EXPORT_SYMBOL_GPL(scsi_unregister_device_handler);
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.
434 int scsi_dh_activate(struct request_queue *q, activate_complete fn, void *data)
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;
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);
455 if (err) {
456 if (fn)
457 fn(data, err);
458 goto out;
461 if (scsi_dh->activate)
462 err = scsi_dh->activate(sdev, fn, data);
463 out:
464 put_device(dev);
465 return err;
467 EXPORT_SYMBOL_GPL(scsi_dh_activate);
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".
479 int scsi_dh_set_params(struct request_queue *q, const char *params)
481 int err = -SCSI_DH_NOSYS;
482 unsigned long flags;
483 struct scsi_device *sdev;
484 struct scsi_device_handler *scsi_dh = NULL;
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);
494 if (err)
495 return err;
496 err = scsi_dh->set_params(sdev, params);
497 put_device(&sdev->sdev_gendev);
498 return err;
500 EXPORT_SYMBOL_GPL(scsi_dh_set_params);
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.
507 int scsi_dh_handler_exist(const char *name)
509 return (get_device_handler(name) != NULL);
511 EXPORT_SYMBOL_GPL(scsi_dh_handler_exist);
514 * scsi_dh_attach - Attach device handler
515 * @sdev - sdev the handler should be attached to
516 * @name - name of the handler to attach
518 int scsi_dh_attach(struct request_queue *q, const char *name)
520 unsigned long flags;
521 struct scsi_device *sdev;
522 struct scsi_device_handler *scsi_dh;
523 int err = 0;
525 scsi_dh = get_device_handler(name);
526 if (!scsi_dh)
527 return -EINVAL;
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);
535 if (!err) {
536 err = scsi_dh_handler_attach(sdev, scsi_dh);
537 put_device(&sdev->sdev_gendev);
539 return err;
541 EXPORT_SYMBOL_GPL(scsi_dh_attach);
544 * scsi_dh_detach - Detach device handler
545 * @sdev - sdev the handler should be detached from
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.
551 void scsi_dh_detach(struct request_queue *q)
553 unsigned long flags;
554 struct scsi_device *sdev;
555 struct scsi_device_handler *scsi_dh = NULL;
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);
563 if (!sdev)
564 return;
566 if (sdev->scsi_dh_data) {
567 scsi_dh = sdev->scsi_dh_data->scsi_dh;
568 scsi_dh_handler_detach(sdev, scsi_dh);
570 put_device(&sdev->sdev_gendev);
572 EXPORT_SYMBOL_GPL(scsi_dh_detach);
574 static struct notifier_block scsi_dh_nb = {
575 .notifier_call = scsi_dh_notifier
578 static int __init scsi_dh_init(void)
580 int r;
582 r = scsi_dev_info_add_list(SCSI_DEVINFO_DH, "SCSI Device Handler");
583 if (r)
584 return r;
586 r = bus_register_notifier(&scsi_bus_type, &scsi_dh_nb);
588 if (!r)
589 bus_for_each_dev(&scsi_bus_type, NULL, NULL,
590 scsi_dh_sysfs_attr_add);
592 return r;
595 static void __exit scsi_dh_exit(void)
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);
603 module_init(scsi_dh_init);
604 module_exit(scsi_dh_exit);
606 MODULE_DESCRIPTION("SCSI device handler");
607 MODULE_AUTHOR("Chandra Seetharaman <sekharan@us.ibm.com>");
608 MODULE_LICENSE("GPL");