Input: release pressed keys when resuming device
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / scsi / scsi_sysfs.c
blobc23ab978c3ba8e15b728a070869db6ec0466c9d2
1 /*
2 * scsi_sysfs.c
4 * SCSI sysfs interface routines.
6 * Created to pull SCSI mid layer sysfs routines into one file.
7 */
9 #include <linux/module.h>
10 #include <linux/slab.h>
11 #include <linux/init.h>
12 #include <linux/blkdev.h>
13 #include <linux/device.h>
15 #include <scsi/scsi.h>
16 #include <scsi/scsi_device.h>
17 #include <scsi/scsi_host.h>
18 #include <scsi/scsi_tcq.h>
19 #include <scsi/scsi_transport.h>
20 #include <scsi/scsi_driver.h>
22 #include "scsi_priv.h"
23 #include "scsi_logging.h"
25 static struct device_type scsi_dev_type;
27 static const struct {
28 enum scsi_device_state value;
29 char *name;
30 } sdev_states[] = {
31 { SDEV_CREATED, "created" },
32 { SDEV_RUNNING, "running" },
33 { SDEV_CANCEL, "cancel" },
34 { SDEV_DEL, "deleted" },
35 { SDEV_QUIESCE, "quiesce" },
36 { SDEV_OFFLINE, "offline" },
37 { SDEV_BLOCK, "blocked" },
38 { SDEV_CREATED_BLOCK, "created-blocked" },
41 const char *scsi_device_state_name(enum scsi_device_state state)
43 int i;
44 char *name = NULL;
46 for (i = 0; i < ARRAY_SIZE(sdev_states); i++) {
47 if (sdev_states[i].value == state) {
48 name = sdev_states[i].name;
49 break;
52 return name;
55 static const struct {
56 enum scsi_host_state value;
57 char *name;
58 } shost_states[] = {
59 { SHOST_CREATED, "created" },
60 { SHOST_RUNNING, "running" },
61 { SHOST_CANCEL, "cancel" },
62 { SHOST_DEL, "deleted" },
63 { SHOST_RECOVERY, "recovery" },
64 { SHOST_CANCEL_RECOVERY, "cancel/recovery" },
65 { SHOST_DEL_RECOVERY, "deleted/recovery", },
67 const char *scsi_host_state_name(enum scsi_host_state state)
69 int i;
70 char *name = NULL;
72 for (i = 0; i < ARRAY_SIZE(shost_states); i++) {
73 if (shost_states[i].value == state) {
74 name = shost_states[i].name;
75 break;
78 return name;
81 static int check_set(unsigned int *val, char *src)
83 char *last;
85 if (strncmp(src, "-", 20) == 0) {
86 *val = SCAN_WILD_CARD;
87 } else {
89 * Doesn't check for int overflow
91 *val = simple_strtoul(src, &last, 0);
92 if (*last != '\0')
93 return 1;
95 return 0;
98 static int scsi_scan(struct Scsi_Host *shost, const char *str)
100 char s1[15], s2[15], s3[15], junk;
101 unsigned int channel, id, lun;
102 int res;
104 res = sscanf(str, "%10s %10s %10s %c", s1, s2, s3, &junk);
105 if (res != 3)
106 return -EINVAL;
107 if (check_set(&channel, s1))
108 return -EINVAL;
109 if (check_set(&id, s2))
110 return -EINVAL;
111 if (check_set(&lun, s3))
112 return -EINVAL;
113 if (shost->transportt->user_scan)
114 res = shost->transportt->user_scan(shost, channel, id, lun);
115 else
116 res = scsi_scan_host_selected(shost, channel, id, lun, 1);
117 return res;
121 * shost_show_function: macro to create an attr function that can be used to
122 * show a non-bit field.
124 #define shost_show_function(name, field, format_string) \
125 static ssize_t \
126 show_##name (struct device *dev, struct device_attribute *attr, \
127 char *buf) \
129 struct Scsi_Host *shost = class_to_shost(dev); \
130 return snprintf (buf, 20, format_string, shost->field); \
134 * shost_rd_attr: macro to create a function and attribute variable for a
135 * read only field.
137 #define shost_rd_attr2(name, field, format_string) \
138 shost_show_function(name, field, format_string) \
139 static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL);
141 #define shost_rd_attr(field, format_string) \
142 shost_rd_attr2(field, field, format_string)
145 * Create the actual show/store functions and data structures.
148 static ssize_t
149 store_scan(struct device *dev, struct device_attribute *attr,
150 const char *buf, size_t count)
152 struct Scsi_Host *shost = class_to_shost(dev);
153 int res;
155 res = scsi_scan(shost, buf);
156 if (res == 0)
157 res = count;
158 return res;
160 static DEVICE_ATTR(scan, S_IWUSR, NULL, store_scan);
162 static ssize_t
163 store_shost_state(struct device *dev, struct device_attribute *attr,
164 const char *buf, size_t count)
166 int i;
167 struct Scsi_Host *shost = class_to_shost(dev);
168 enum scsi_host_state state = 0;
170 for (i = 0; i < ARRAY_SIZE(shost_states); i++) {
171 const int len = strlen(shost_states[i].name);
172 if (strncmp(shost_states[i].name, buf, len) == 0 &&
173 buf[len] == '\n') {
174 state = shost_states[i].value;
175 break;
178 if (!state)
179 return -EINVAL;
181 if (scsi_host_set_state(shost, state))
182 return -EINVAL;
183 return count;
186 static ssize_t
187 show_shost_state(struct device *dev, struct device_attribute *attr, char *buf)
189 struct Scsi_Host *shost = class_to_shost(dev);
190 const char *name = scsi_host_state_name(shost->shost_state);
192 if (!name)
193 return -EINVAL;
195 return snprintf(buf, 20, "%s\n", name);
198 /* DEVICE_ATTR(state) clashes with dev_attr_state for sdev */
199 struct device_attribute dev_attr_hstate =
200 __ATTR(state, S_IRUGO | S_IWUSR, show_shost_state, store_shost_state);
202 static ssize_t
203 show_shost_mode(unsigned int mode, char *buf)
205 ssize_t len = 0;
207 if (mode & MODE_INITIATOR)
208 len = sprintf(buf, "%s", "Initiator");
210 if (mode & MODE_TARGET)
211 len += sprintf(buf + len, "%s%s", len ? ", " : "", "Target");
213 len += sprintf(buf + len, "\n");
215 return len;
218 static ssize_t
219 show_shost_supported_mode(struct device *dev, struct device_attribute *attr,
220 char *buf)
222 struct Scsi_Host *shost = class_to_shost(dev);
223 unsigned int supported_mode = shost->hostt->supported_mode;
225 if (supported_mode == MODE_UNKNOWN)
226 /* by default this should be initiator */
227 supported_mode = MODE_INITIATOR;
229 return show_shost_mode(supported_mode, buf);
232 static DEVICE_ATTR(supported_mode, S_IRUGO | S_IWUSR, show_shost_supported_mode, NULL);
234 static ssize_t
235 show_shost_active_mode(struct device *dev,
236 struct device_attribute *attr, char *buf)
238 struct Scsi_Host *shost = class_to_shost(dev);
240 if (shost->active_mode == MODE_UNKNOWN)
241 return snprintf(buf, 20, "unknown\n");
242 else
243 return show_shost_mode(shost->active_mode, buf);
246 static DEVICE_ATTR(active_mode, S_IRUGO | S_IWUSR, show_shost_active_mode, NULL);
248 shost_rd_attr(unique_id, "%u\n");
249 shost_rd_attr(host_busy, "%hu\n");
250 shost_rd_attr(cmd_per_lun, "%hd\n");
251 shost_rd_attr(can_queue, "%hd\n");
252 shost_rd_attr(sg_tablesize, "%hu\n");
253 shost_rd_attr(unchecked_isa_dma, "%d\n");
254 shost_rd_attr(prot_capabilities, "%u\n");
255 shost_rd_attr(prot_guard_type, "%hd\n");
256 shost_rd_attr2(proc_name, hostt->proc_name, "%s\n");
258 static struct attribute *scsi_sysfs_shost_attrs[] = {
259 &dev_attr_unique_id.attr,
260 &dev_attr_host_busy.attr,
261 &dev_attr_cmd_per_lun.attr,
262 &dev_attr_can_queue.attr,
263 &dev_attr_sg_tablesize.attr,
264 &dev_attr_unchecked_isa_dma.attr,
265 &dev_attr_proc_name.attr,
266 &dev_attr_scan.attr,
267 &dev_attr_hstate.attr,
268 &dev_attr_supported_mode.attr,
269 &dev_attr_active_mode.attr,
270 &dev_attr_prot_capabilities.attr,
271 &dev_attr_prot_guard_type.attr,
272 NULL
275 struct attribute_group scsi_shost_attr_group = {
276 .attrs = scsi_sysfs_shost_attrs,
279 const struct attribute_group *scsi_sysfs_shost_attr_groups[] = {
280 &scsi_shost_attr_group,
281 NULL
284 static void scsi_device_cls_release(struct device *class_dev)
286 struct scsi_device *sdev;
288 sdev = class_to_sdev(class_dev);
289 put_device(&sdev->sdev_gendev);
292 static void scsi_device_dev_release_usercontext(struct work_struct *work)
294 struct scsi_device *sdev;
295 struct device *parent;
296 struct scsi_target *starget;
297 struct list_head *this, *tmp;
298 unsigned long flags;
300 sdev = container_of(work, struct scsi_device, ew.work);
302 parent = sdev->sdev_gendev.parent;
303 starget = to_scsi_target(parent);
305 spin_lock_irqsave(sdev->host->host_lock, flags);
306 starget->reap_ref++;
307 list_del(&sdev->siblings);
308 list_del(&sdev->same_target_siblings);
309 list_del(&sdev->starved_entry);
310 spin_unlock_irqrestore(sdev->host->host_lock, flags);
312 cancel_work_sync(&sdev->event_work);
314 list_for_each_safe(this, tmp, &sdev->event_list) {
315 struct scsi_event *evt;
317 evt = list_entry(this, struct scsi_event, node);
318 list_del(&evt->node);
319 kfree(evt);
322 if (sdev->request_queue) {
323 sdev->request_queue->queuedata = NULL;
324 /* user context needed to free queue */
325 scsi_free_queue(sdev->request_queue);
326 /* temporary expedient, try to catch use of queue lock
327 * after free of sdev */
328 sdev->request_queue = NULL;
331 scsi_target_reap(scsi_target(sdev));
333 kfree(sdev->inquiry);
334 kfree(sdev);
336 if (parent)
337 put_device(parent);
340 static void scsi_device_dev_release(struct device *dev)
342 struct scsi_device *sdp = to_scsi_device(dev);
343 execute_in_process_context(scsi_device_dev_release_usercontext,
344 &sdp->ew);
347 static struct class sdev_class = {
348 .name = "scsi_device",
349 .dev_release = scsi_device_cls_release,
352 /* all probing is done in the individual ->probe routines */
353 static int scsi_bus_match(struct device *dev, struct device_driver *gendrv)
355 struct scsi_device *sdp;
357 if (dev->type != &scsi_dev_type)
358 return 0;
360 sdp = to_scsi_device(dev);
361 if (sdp->no_uld_attach)
362 return 0;
363 return (sdp->inq_periph_qual == SCSI_INQ_PQ_CON)? 1: 0;
366 static int scsi_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
368 struct scsi_device *sdev;
370 if (dev->type != &scsi_dev_type)
371 return 0;
373 sdev = to_scsi_device(dev);
375 add_uevent_var(env, "MODALIAS=" SCSI_DEVICE_MODALIAS_FMT, sdev->type);
376 return 0;
379 static int scsi_bus_suspend(struct device * dev, pm_message_t state)
381 struct device_driver *drv;
382 struct scsi_device *sdev;
383 int err;
385 if (dev->type != &scsi_dev_type)
386 return 0;
388 drv = dev->driver;
389 sdev = to_scsi_device(dev);
391 err = scsi_device_quiesce(sdev);
392 if (err)
393 return err;
395 if (drv && drv->suspend) {
396 err = drv->suspend(dev, state);
397 if (err)
398 return err;
401 return 0;
404 static int scsi_bus_resume(struct device * dev)
406 struct device_driver *drv;
407 struct scsi_device *sdev;
408 int err = 0;
410 if (dev->type != &scsi_dev_type)
411 return 0;
413 drv = dev->driver;
414 sdev = to_scsi_device(dev);
416 if (drv && drv->resume)
417 err = drv->resume(dev);
419 scsi_device_resume(sdev);
421 return err;
424 struct bus_type scsi_bus_type = {
425 .name = "scsi",
426 .match = scsi_bus_match,
427 .uevent = scsi_bus_uevent,
428 .suspend = scsi_bus_suspend,
429 .resume = scsi_bus_resume,
431 EXPORT_SYMBOL_GPL(scsi_bus_type);
433 int scsi_sysfs_register(void)
435 int error;
437 error = bus_register(&scsi_bus_type);
438 if (!error) {
439 error = class_register(&sdev_class);
440 if (error)
441 bus_unregister(&scsi_bus_type);
444 return error;
447 void scsi_sysfs_unregister(void)
449 class_unregister(&sdev_class);
450 bus_unregister(&scsi_bus_type);
454 * sdev_show_function: macro to create an attr function that can be used to
455 * show a non-bit field.
457 #define sdev_show_function(field, format_string) \
458 static ssize_t \
459 sdev_show_##field (struct device *dev, struct device_attribute *attr, \
460 char *buf) \
462 struct scsi_device *sdev; \
463 sdev = to_scsi_device(dev); \
464 return snprintf (buf, 20, format_string, sdev->field); \
468 * sdev_rd_attr: macro to create a function and attribute variable for a
469 * read only field.
471 #define sdev_rd_attr(field, format_string) \
472 sdev_show_function(field, format_string) \
473 static DEVICE_ATTR(field, S_IRUGO, sdev_show_##field, NULL);
477 * sdev_rw_attr: create a function and attribute variable for a
478 * read/write field.
480 #define sdev_rw_attr(field, format_string) \
481 sdev_show_function(field, format_string) \
483 static ssize_t \
484 sdev_store_##field (struct device *dev, struct device_attribute *attr, \
485 const char *buf, size_t count) \
487 struct scsi_device *sdev; \
488 sdev = to_scsi_device(dev); \
489 sscanf (buf, format_string, &sdev->field); \
490 return count; \
492 static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, sdev_show_##field, sdev_store_##field);
494 /* Currently we don't export bit fields, but we might in future,
495 * so leave this code in */
496 #if 0
498 * sdev_rd_attr: create a function and attribute variable for a
499 * read/write bit field.
501 #define sdev_rw_attr_bit(field) \
502 sdev_show_function(field, "%d\n") \
504 static ssize_t \
505 sdev_store_##field (struct device *dev, struct device_attribute *attr, \
506 const char *buf, size_t count) \
508 int ret; \
509 struct scsi_device *sdev; \
510 ret = scsi_sdev_check_buf_bit(buf); \
511 if (ret >= 0) { \
512 sdev = to_scsi_device(dev); \
513 sdev->field = ret; \
514 ret = count; \
516 return ret; \
518 static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, sdev_show_##field, sdev_store_##field);
521 * scsi_sdev_check_buf_bit: return 0 if buf is "0", return 1 if buf is "1",
522 * else return -EINVAL.
524 static int scsi_sdev_check_buf_bit(const char *buf)
526 if ((buf[1] == '\0') || ((buf[1] == '\n') && (buf[2] == '\0'))) {
527 if (buf[0] == '1')
528 return 1;
529 else if (buf[0] == '0')
530 return 0;
531 else
532 return -EINVAL;
533 } else
534 return -EINVAL;
536 #endif
538 * Create the actual show/store functions and data structures.
540 sdev_rd_attr (device_blocked, "%d\n");
541 sdev_rd_attr (queue_depth, "%d\n");
542 sdev_rd_attr (type, "%d\n");
543 sdev_rd_attr (scsi_level, "%d\n");
544 sdev_rd_attr (vendor, "%.8s\n");
545 sdev_rd_attr (model, "%.16s\n");
546 sdev_rd_attr (rev, "%.4s\n");
549 * TODO: can we make these symlinks to the block layer ones?
551 static ssize_t
552 sdev_show_timeout (struct device *dev, struct device_attribute *attr, char *buf)
554 struct scsi_device *sdev;
555 sdev = to_scsi_device(dev);
556 return snprintf(buf, 20, "%d\n", sdev->request_queue->rq_timeout / HZ);
559 static ssize_t
560 sdev_store_timeout (struct device *dev, struct device_attribute *attr,
561 const char *buf, size_t count)
563 struct scsi_device *sdev;
564 int timeout;
565 sdev = to_scsi_device(dev);
566 sscanf (buf, "%d\n", &timeout);
567 blk_queue_rq_timeout(sdev->request_queue, timeout * HZ);
568 return count;
570 static DEVICE_ATTR(timeout, S_IRUGO | S_IWUSR, sdev_show_timeout, sdev_store_timeout);
572 static ssize_t
573 store_rescan_field (struct device *dev, struct device_attribute *attr,
574 const char *buf, size_t count)
576 scsi_rescan_device(dev);
577 return count;
579 static DEVICE_ATTR(rescan, S_IWUSR, NULL, store_rescan_field);
581 static void sdev_store_delete_callback(struct device *dev)
583 scsi_remove_device(to_scsi_device(dev));
586 static ssize_t
587 sdev_store_delete(struct device *dev, struct device_attribute *attr,
588 const char *buf, size_t count)
590 int rc;
592 /* An attribute cannot be unregistered by one of its own methods,
593 * so we have to use this roundabout approach.
595 rc = device_schedule_callback(dev, sdev_store_delete_callback);
596 if (rc)
597 count = rc;
598 return count;
600 static DEVICE_ATTR(delete, S_IWUSR, NULL, sdev_store_delete);
602 static ssize_t
603 store_state_field(struct device *dev, struct device_attribute *attr,
604 const char *buf, size_t count)
606 int i;
607 struct scsi_device *sdev = to_scsi_device(dev);
608 enum scsi_device_state state = 0;
610 for (i = 0; i < ARRAY_SIZE(sdev_states); i++) {
611 const int len = strlen(sdev_states[i].name);
612 if (strncmp(sdev_states[i].name, buf, len) == 0 &&
613 buf[len] == '\n') {
614 state = sdev_states[i].value;
615 break;
618 if (!state)
619 return -EINVAL;
621 if (scsi_device_set_state(sdev, state))
622 return -EINVAL;
623 return count;
626 static ssize_t
627 show_state_field(struct device *dev, struct device_attribute *attr, char *buf)
629 struct scsi_device *sdev = to_scsi_device(dev);
630 const char *name = scsi_device_state_name(sdev->sdev_state);
632 if (!name)
633 return -EINVAL;
635 return snprintf(buf, 20, "%s\n", name);
638 static DEVICE_ATTR(state, S_IRUGO | S_IWUSR, show_state_field, store_state_field);
640 static ssize_t
641 show_queue_type_field(struct device *dev, struct device_attribute *attr,
642 char *buf)
644 struct scsi_device *sdev = to_scsi_device(dev);
645 const char *name = "none";
647 if (sdev->ordered_tags)
648 name = "ordered";
649 else if (sdev->simple_tags)
650 name = "simple";
652 return snprintf(buf, 20, "%s\n", name);
655 static DEVICE_ATTR(queue_type, S_IRUGO, show_queue_type_field, NULL);
657 static ssize_t
658 show_iostat_counterbits(struct device *dev, struct device_attribute *attr, char *buf)
660 return snprintf(buf, 20, "%d\n", (int)sizeof(atomic_t) * 8);
663 static DEVICE_ATTR(iocounterbits, S_IRUGO, show_iostat_counterbits, NULL);
665 #define show_sdev_iostat(field) \
666 static ssize_t \
667 show_iostat_##field(struct device *dev, struct device_attribute *attr, \
668 char *buf) \
670 struct scsi_device *sdev = to_scsi_device(dev); \
671 unsigned long long count = atomic_read(&sdev->field); \
672 return snprintf(buf, 20, "0x%llx\n", count); \
674 static DEVICE_ATTR(field, S_IRUGO, show_iostat_##field, NULL)
676 show_sdev_iostat(iorequest_cnt);
677 show_sdev_iostat(iodone_cnt);
678 show_sdev_iostat(ioerr_cnt);
680 static ssize_t
681 sdev_show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
683 struct scsi_device *sdev;
684 sdev = to_scsi_device(dev);
685 return snprintf (buf, 20, SCSI_DEVICE_MODALIAS_FMT "\n", sdev->type);
687 static DEVICE_ATTR(modalias, S_IRUGO, sdev_show_modalias, NULL);
689 #define DECLARE_EVT_SHOW(name, Cap_name) \
690 static ssize_t \
691 sdev_show_evt_##name(struct device *dev, struct device_attribute *attr, \
692 char *buf) \
694 struct scsi_device *sdev = to_scsi_device(dev); \
695 int val = test_bit(SDEV_EVT_##Cap_name, sdev->supported_events);\
696 return snprintf(buf, 20, "%d\n", val); \
699 #define DECLARE_EVT_STORE(name, Cap_name) \
700 static ssize_t \
701 sdev_store_evt_##name(struct device *dev, struct device_attribute *attr,\
702 const char *buf, size_t count) \
704 struct scsi_device *sdev = to_scsi_device(dev); \
705 int val = simple_strtoul(buf, NULL, 0); \
706 if (val == 0) \
707 clear_bit(SDEV_EVT_##Cap_name, sdev->supported_events); \
708 else if (val == 1) \
709 set_bit(SDEV_EVT_##Cap_name, sdev->supported_events); \
710 else \
711 return -EINVAL; \
712 return count; \
715 #define DECLARE_EVT(name, Cap_name) \
716 DECLARE_EVT_SHOW(name, Cap_name) \
717 DECLARE_EVT_STORE(name, Cap_name) \
718 static DEVICE_ATTR(evt_##name, S_IRUGO, sdev_show_evt_##name, \
719 sdev_store_evt_##name);
720 #define REF_EVT(name) &dev_attr_evt_##name.attr
722 DECLARE_EVT(media_change, MEDIA_CHANGE)
724 /* Default template for device attributes. May NOT be modified */
725 static struct attribute *scsi_sdev_attrs[] = {
726 &dev_attr_device_blocked.attr,
727 &dev_attr_type.attr,
728 &dev_attr_scsi_level.attr,
729 &dev_attr_vendor.attr,
730 &dev_attr_model.attr,
731 &dev_attr_rev.attr,
732 &dev_attr_rescan.attr,
733 &dev_attr_delete.attr,
734 &dev_attr_state.attr,
735 &dev_attr_timeout.attr,
736 &dev_attr_iocounterbits.attr,
737 &dev_attr_iorequest_cnt.attr,
738 &dev_attr_iodone_cnt.attr,
739 &dev_attr_ioerr_cnt.attr,
740 &dev_attr_modalias.attr,
741 REF_EVT(media_change),
742 NULL
745 static struct attribute_group scsi_sdev_attr_group = {
746 .attrs = scsi_sdev_attrs,
749 static const struct attribute_group *scsi_sdev_attr_groups[] = {
750 &scsi_sdev_attr_group,
751 NULL
754 static ssize_t
755 sdev_store_queue_depth_rw(struct device *dev, struct device_attribute *attr,
756 const char *buf, size_t count)
758 int depth, retval;
759 struct scsi_device *sdev = to_scsi_device(dev);
760 struct scsi_host_template *sht = sdev->host->hostt;
762 if (!sht->change_queue_depth)
763 return -EINVAL;
765 depth = simple_strtoul(buf, NULL, 0);
767 if (depth < 1)
768 return -EINVAL;
770 retval = sht->change_queue_depth(sdev, depth,
771 SCSI_QDEPTH_DEFAULT);
772 if (retval < 0)
773 return retval;
775 sdev->max_queue_depth = sdev->queue_depth;
777 return count;
780 static struct device_attribute sdev_attr_queue_depth_rw =
781 __ATTR(queue_depth, S_IRUGO | S_IWUSR, sdev_show_queue_depth,
782 sdev_store_queue_depth_rw);
784 static ssize_t
785 sdev_show_queue_ramp_up_period(struct device *dev,
786 struct device_attribute *attr,
787 char *buf)
789 struct scsi_device *sdev;
790 sdev = to_scsi_device(dev);
791 return snprintf(buf, 20, "%u\n",
792 jiffies_to_msecs(sdev->queue_ramp_up_period));
795 static ssize_t
796 sdev_store_queue_ramp_up_period(struct device *dev,
797 struct device_attribute *attr,
798 const char *buf, size_t count)
800 struct scsi_device *sdev = to_scsi_device(dev);
801 unsigned long period;
803 if (strict_strtoul(buf, 10, &period))
804 return -EINVAL;
806 sdev->queue_ramp_up_period = msecs_to_jiffies(period);
807 return period;
810 static struct device_attribute sdev_attr_queue_ramp_up_period =
811 __ATTR(queue_ramp_up_period, S_IRUGO | S_IWUSR,
812 sdev_show_queue_ramp_up_period,
813 sdev_store_queue_ramp_up_period);
815 static ssize_t
816 sdev_store_queue_type_rw(struct device *dev, struct device_attribute *attr,
817 const char *buf, size_t count)
819 struct scsi_device *sdev = to_scsi_device(dev);
820 struct scsi_host_template *sht = sdev->host->hostt;
821 int tag_type = 0, retval;
822 int prev_tag_type = scsi_get_tag_type(sdev);
824 if (!sdev->tagged_supported || !sht->change_queue_type)
825 return -EINVAL;
827 if (strncmp(buf, "ordered", 7) == 0)
828 tag_type = MSG_ORDERED_TAG;
829 else if (strncmp(buf, "simple", 6) == 0)
830 tag_type = MSG_SIMPLE_TAG;
831 else if (strncmp(buf, "none", 4) != 0)
832 return -EINVAL;
834 if (tag_type == prev_tag_type)
835 return count;
837 retval = sht->change_queue_type(sdev, tag_type);
838 if (retval < 0)
839 return retval;
841 return count;
844 static int scsi_target_add(struct scsi_target *starget)
846 int error;
848 if (starget->state != STARGET_CREATED)
849 return 0;
851 device_enable_async_suspend(&starget->dev);
853 error = device_add(&starget->dev);
854 if (error) {
855 dev_err(&starget->dev, "target device_add failed, error %d\n", error);
856 return error;
858 transport_add_device(&starget->dev);
859 starget->state = STARGET_RUNNING;
861 return 0;
864 static struct device_attribute sdev_attr_queue_type_rw =
865 __ATTR(queue_type, S_IRUGO | S_IWUSR, show_queue_type_field,
866 sdev_store_queue_type_rw);
869 * scsi_sysfs_add_sdev - add scsi device to sysfs
870 * @sdev: scsi_device to add
872 * Return value:
873 * 0 on Success / non-zero on Failure
875 int scsi_sysfs_add_sdev(struct scsi_device *sdev)
877 int error, i;
878 struct request_queue *rq = sdev->request_queue;
879 struct scsi_target *starget = sdev->sdev_target;
881 error = scsi_device_set_state(sdev, SDEV_RUNNING);
882 if (error)
883 return error;
885 error = scsi_target_add(starget);
886 if (error)
887 return error;
889 transport_configure_device(&starget->dev);
890 device_enable_async_suspend(&sdev->sdev_gendev);
891 error = device_add(&sdev->sdev_gendev);
892 if (error) {
893 printk(KERN_INFO "error 1\n");
894 return error;
896 device_enable_async_suspend(&sdev->sdev_dev);
897 error = device_add(&sdev->sdev_dev);
898 if (error) {
899 printk(KERN_INFO "error 2\n");
900 device_del(&sdev->sdev_gendev);
901 return error;
903 transport_add_device(&sdev->sdev_gendev);
904 sdev->is_visible = 1;
906 /* create queue files, which may be writable, depending on the host */
907 if (sdev->host->hostt->change_queue_depth) {
908 error = device_create_file(&sdev->sdev_gendev,
909 &sdev_attr_queue_depth_rw);
910 error = device_create_file(&sdev->sdev_gendev,
911 &sdev_attr_queue_ramp_up_period);
913 else
914 error = device_create_file(&sdev->sdev_gendev, &dev_attr_queue_depth);
915 if (error)
916 return error;
918 if (sdev->host->hostt->change_queue_type)
919 error = device_create_file(&sdev->sdev_gendev, &sdev_attr_queue_type_rw);
920 else
921 error = device_create_file(&sdev->sdev_gendev, &dev_attr_queue_type);
922 if (error)
923 return error;
925 error = bsg_register_queue(rq, &sdev->sdev_gendev, NULL, NULL);
927 if (error)
928 /* we're treating error on bsg register as non-fatal,
929 * so pretend nothing went wrong */
930 sdev_printk(KERN_INFO, sdev,
931 "Failed to register bsg queue, errno=%d\n", error);
933 /* add additional host specific attributes */
934 if (sdev->host->hostt->sdev_attrs) {
935 for (i = 0; sdev->host->hostt->sdev_attrs[i]; i++) {
936 error = device_create_file(&sdev->sdev_gendev,
937 sdev->host->hostt->sdev_attrs[i]);
938 if (error)
939 return error;
943 return error;
946 void __scsi_remove_device(struct scsi_device *sdev)
948 struct device *dev = &sdev->sdev_gendev;
950 if (sdev->is_visible) {
951 if (scsi_device_set_state(sdev, SDEV_CANCEL) != 0)
952 return;
954 bsg_unregister_queue(sdev->request_queue);
955 device_unregister(&sdev->sdev_dev);
956 transport_remove_device(dev);
957 device_del(dev);
958 } else
959 put_device(&sdev->sdev_dev);
960 scsi_device_set_state(sdev, SDEV_DEL);
961 if (sdev->host->hostt->slave_destroy)
962 sdev->host->hostt->slave_destroy(sdev);
963 transport_destroy_device(dev);
964 put_device(dev);
968 * scsi_remove_device - unregister a device from the scsi bus
969 * @sdev: scsi_device to unregister
971 void scsi_remove_device(struct scsi_device *sdev)
973 struct Scsi_Host *shost = sdev->host;
975 mutex_lock(&shost->scan_mutex);
976 __scsi_remove_device(sdev);
977 mutex_unlock(&shost->scan_mutex);
979 EXPORT_SYMBOL(scsi_remove_device);
981 static void __scsi_remove_target(struct scsi_target *starget)
983 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
984 unsigned long flags;
985 struct scsi_device *sdev;
987 spin_lock_irqsave(shost->host_lock, flags);
988 starget->reap_ref++;
989 restart:
990 list_for_each_entry(sdev, &shost->__devices, siblings) {
991 if (sdev->channel != starget->channel ||
992 sdev->id != starget->id ||
993 sdev->sdev_state == SDEV_DEL)
994 continue;
995 spin_unlock_irqrestore(shost->host_lock, flags);
996 scsi_remove_device(sdev);
997 spin_lock_irqsave(shost->host_lock, flags);
998 goto restart;
1000 spin_unlock_irqrestore(shost->host_lock, flags);
1001 scsi_target_reap(starget);
1004 static int __remove_child (struct device * dev, void * data)
1006 if (scsi_is_target_device(dev))
1007 __scsi_remove_target(to_scsi_target(dev));
1008 return 0;
1012 * scsi_remove_target - try to remove a target and all its devices
1013 * @dev: generic starget or parent of generic stargets to be removed
1015 * Note: This is slightly racy. It is possible that if the user
1016 * requests the addition of another device then the target won't be
1017 * removed.
1019 void scsi_remove_target(struct device *dev)
1021 struct device *rdev;
1023 if (scsi_is_target_device(dev)) {
1024 __scsi_remove_target(to_scsi_target(dev));
1025 return;
1028 rdev = get_device(dev);
1029 device_for_each_child(dev, NULL, __remove_child);
1030 put_device(rdev);
1032 EXPORT_SYMBOL(scsi_remove_target);
1034 int scsi_register_driver(struct device_driver *drv)
1036 drv->bus = &scsi_bus_type;
1038 return driver_register(drv);
1040 EXPORT_SYMBOL(scsi_register_driver);
1042 int scsi_register_interface(struct class_interface *intf)
1044 intf->class = &sdev_class;
1046 return class_interface_register(intf);
1048 EXPORT_SYMBOL(scsi_register_interface);
1051 * scsi_sysfs_add_host - add scsi host to subsystem
1052 * @shost: scsi host struct to add to subsystem
1054 int scsi_sysfs_add_host(struct Scsi_Host *shost)
1056 int error, i;
1058 /* add host specific attributes */
1059 if (shost->hostt->shost_attrs) {
1060 for (i = 0; shost->hostt->shost_attrs[i]; i++) {
1061 error = device_create_file(&shost->shost_dev,
1062 shost->hostt->shost_attrs[i]);
1063 if (error)
1064 return error;
1068 transport_register_device(&shost->shost_gendev);
1069 transport_configure_device(&shost->shost_gendev);
1070 return 0;
1073 static struct device_type scsi_dev_type = {
1074 .name = "scsi_device",
1075 .release = scsi_device_dev_release,
1076 .groups = scsi_sdev_attr_groups,
1079 void scsi_sysfs_device_initialize(struct scsi_device *sdev)
1081 unsigned long flags;
1082 struct Scsi_Host *shost = sdev->host;
1083 struct scsi_target *starget = sdev->sdev_target;
1085 device_initialize(&sdev->sdev_gendev);
1086 sdev->sdev_gendev.bus = &scsi_bus_type;
1087 sdev->sdev_gendev.type = &scsi_dev_type;
1088 dev_set_name(&sdev->sdev_gendev, "%d:%d:%d:%d",
1089 sdev->host->host_no, sdev->channel, sdev->id, sdev->lun);
1091 device_initialize(&sdev->sdev_dev);
1092 sdev->sdev_dev.parent = get_device(&sdev->sdev_gendev);
1093 sdev->sdev_dev.class = &sdev_class;
1094 dev_set_name(&sdev->sdev_dev, "%d:%d:%d:%d",
1095 sdev->host->host_no, sdev->channel, sdev->id, sdev->lun);
1096 sdev->scsi_level = starget->scsi_level;
1097 transport_setup_device(&sdev->sdev_gendev);
1098 spin_lock_irqsave(shost->host_lock, flags);
1099 list_add_tail(&sdev->same_target_siblings, &starget->devices);
1100 list_add_tail(&sdev->siblings, &shost->__devices);
1101 spin_unlock_irqrestore(shost->host_lock, flags);
1104 int scsi_is_sdev_device(const struct device *dev)
1106 return dev->type == &scsi_dev_type;
1108 EXPORT_SYMBOL(scsi_is_sdev_device);
1110 /* A blank transport template that is used in drivers that don't
1111 * yet implement Transport Attributes */
1112 struct scsi_transport_template blank_transport_template = { { { {NULL, }, }, }, };