USB: usb-storage: unusual_devs update for Super TOP SATA bridge
[linux/fpc-iii.git] / drivers / scsi / scsi_sysfs.c
blob72ca515a430411e90234fe37c4ad6b91fd67b68d
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>
14 #include <linux/pm_runtime.h>
16 #include <scsi/scsi.h>
17 #include <scsi/scsi_device.h>
18 #include <scsi/scsi_host.h>
19 #include <scsi/scsi_tcq.h>
20 #include <scsi/scsi_transport.h>
21 #include <scsi/scsi_driver.h>
23 #include "scsi_priv.h"
24 #include "scsi_logging.h"
26 static struct device_type scsi_dev_type;
28 static const struct {
29 enum scsi_device_state value;
30 char *name;
31 } sdev_states[] = {
32 { SDEV_CREATED, "created" },
33 { SDEV_RUNNING, "running" },
34 { SDEV_CANCEL, "cancel" },
35 { SDEV_DEL, "deleted" },
36 { SDEV_QUIESCE, "quiesce" },
37 { SDEV_OFFLINE, "offline" },
38 { SDEV_BLOCK, "blocked" },
39 { SDEV_CREATED_BLOCK, "created-blocked" },
42 const char *scsi_device_state_name(enum scsi_device_state state)
44 int i;
45 char *name = NULL;
47 for (i = 0; i < ARRAY_SIZE(sdev_states); i++) {
48 if (sdev_states[i].value == state) {
49 name = sdev_states[i].name;
50 break;
53 return name;
56 static const struct {
57 enum scsi_host_state value;
58 char *name;
59 } shost_states[] = {
60 { SHOST_CREATED, "created" },
61 { SHOST_RUNNING, "running" },
62 { SHOST_CANCEL, "cancel" },
63 { SHOST_DEL, "deleted" },
64 { SHOST_RECOVERY, "recovery" },
65 { SHOST_CANCEL_RECOVERY, "cancel/recovery" },
66 { SHOST_DEL_RECOVERY, "deleted/recovery", },
68 const char *scsi_host_state_name(enum scsi_host_state state)
70 int i;
71 char *name = NULL;
73 for (i = 0; i < ARRAY_SIZE(shost_states); i++) {
74 if (shost_states[i].value == state) {
75 name = shost_states[i].name;
76 break;
79 return name;
82 static int check_set(unsigned int *val, char *src)
84 char *last;
86 if (strncmp(src, "-", 20) == 0) {
87 *val = SCAN_WILD_CARD;
88 } else {
90 * Doesn't check for int overflow
92 *val = simple_strtoul(src, &last, 0);
93 if (*last != '\0')
94 return 1;
96 return 0;
99 static int scsi_scan(struct Scsi_Host *shost, const char *str)
101 char s1[15], s2[15], s3[15], junk;
102 unsigned int channel, id, lun;
103 int res;
105 res = sscanf(str, "%10s %10s %10s %c", s1, s2, s3, &junk);
106 if (res != 3)
107 return -EINVAL;
108 if (check_set(&channel, s1))
109 return -EINVAL;
110 if (check_set(&id, s2))
111 return -EINVAL;
112 if (check_set(&lun, s3))
113 return -EINVAL;
114 if (shost->transportt->user_scan)
115 res = shost->transportt->user_scan(shost, channel, id, lun);
116 else
117 res = scsi_scan_host_selected(shost, channel, id, lun, 1);
118 return res;
122 * shost_show_function: macro to create an attr function that can be used to
123 * show a non-bit field.
125 #define shost_show_function(name, field, format_string) \
126 static ssize_t \
127 show_##name (struct device *dev, struct device_attribute *attr, \
128 char *buf) \
130 struct Scsi_Host *shost = class_to_shost(dev); \
131 return snprintf (buf, 20, format_string, shost->field); \
135 * shost_rd_attr: macro to create a function and attribute variable for a
136 * read only field.
138 #define shost_rd_attr2(name, field, format_string) \
139 shost_show_function(name, field, format_string) \
140 static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL);
142 #define shost_rd_attr(field, format_string) \
143 shost_rd_attr2(field, field, format_string)
146 * Create the actual show/store functions and data structures.
149 static ssize_t
150 store_scan(struct device *dev, struct device_attribute *attr,
151 const char *buf, size_t count)
153 struct Scsi_Host *shost = class_to_shost(dev);
154 int res;
156 res = scsi_scan(shost, buf);
157 if (res == 0)
158 res = count;
159 return res;
161 static DEVICE_ATTR(scan, S_IWUSR, NULL, store_scan);
163 static ssize_t
164 store_shost_state(struct device *dev, struct device_attribute *attr,
165 const char *buf, size_t count)
167 int i;
168 struct Scsi_Host *shost = class_to_shost(dev);
169 enum scsi_host_state state = 0;
171 for (i = 0; i < ARRAY_SIZE(shost_states); i++) {
172 const int len = strlen(shost_states[i].name);
173 if (strncmp(shost_states[i].name, buf, len) == 0 &&
174 buf[len] == '\n') {
175 state = shost_states[i].value;
176 break;
179 if (!state)
180 return -EINVAL;
182 if (scsi_host_set_state(shost, state))
183 return -EINVAL;
184 return count;
187 static ssize_t
188 show_shost_state(struct device *dev, struct device_attribute *attr, char *buf)
190 struct Scsi_Host *shost = class_to_shost(dev);
191 const char *name = scsi_host_state_name(shost->shost_state);
193 if (!name)
194 return -EINVAL;
196 return snprintf(buf, 20, "%s\n", name);
199 /* DEVICE_ATTR(state) clashes with dev_attr_state for sdev */
200 struct device_attribute dev_attr_hstate =
201 __ATTR(state, S_IRUGO | S_IWUSR, show_shost_state, store_shost_state);
203 static ssize_t
204 show_shost_mode(unsigned int mode, char *buf)
206 ssize_t len = 0;
208 if (mode & MODE_INITIATOR)
209 len = sprintf(buf, "%s", "Initiator");
211 if (mode & MODE_TARGET)
212 len += sprintf(buf + len, "%s%s", len ? ", " : "", "Target");
214 len += sprintf(buf + len, "\n");
216 return len;
219 static ssize_t
220 show_shost_supported_mode(struct device *dev, struct device_attribute *attr,
221 char *buf)
223 struct Scsi_Host *shost = class_to_shost(dev);
224 unsigned int supported_mode = shost->hostt->supported_mode;
226 if (supported_mode == MODE_UNKNOWN)
227 /* by default this should be initiator */
228 supported_mode = MODE_INITIATOR;
230 return show_shost_mode(supported_mode, buf);
233 static DEVICE_ATTR(supported_mode, S_IRUGO | S_IWUSR, show_shost_supported_mode, NULL);
235 static ssize_t
236 show_shost_active_mode(struct device *dev,
237 struct device_attribute *attr, char *buf)
239 struct Scsi_Host *shost = class_to_shost(dev);
241 if (shost->active_mode == MODE_UNKNOWN)
242 return snprintf(buf, 20, "unknown\n");
243 else
244 return show_shost_mode(shost->active_mode, buf);
247 static DEVICE_ATTR(active_mode, S_IRUGO | S_IWUSR, show_shost_active_mode, NULL);
249 static int check_reset_type(const char *str)
251 if (sysfs_streq(str, "adapter"))
252 return SCSI_ADAPTER_RESET;
253 else if (sysfs_streq(str, "firmware"))
254 return SCSI_FIRMWARE_RESET;
255 else
256 return 0;
259 static ssize_t
260 store_host_reset(struct device *dev, struct device_attribute *attr,
261 const char *buf, size_t count)
263 struct Scsi_Host *shost = class_to_shost(dev);
264 struct scsi_host_template *sht = shost->hostt;
265 int ret = -EINVAL;
266 int type;
268 type = check_reset_type(buf);
269 if (!type)
270 goto exit_store_host_reset;
272 if (sht->host_reset)
273 ret = sht->host_reset(shost, type);
275 exit_store_host_reset:
276 if (ret == 0)
277 ret = count;
278 return ret;
281 static DEVICE_ATTR(host_reset, S_IWUSR, NULL, store_host_reset);
283 shost_rd_attr(unique_id, "%u\n");
284 shost_rd_attr(host_busy, "%hu\n");
285 shost_rd_attr(cmd_per_lun, "%hd\n");
286 shost_rd_attr(can_queue, "%hd\n");
287 shost_rd_attr(sg_tablesize, "%hu\n");
288 shost_rd_attr(sg_prot_tablesize, "%hu\n");
289 shost_rd_attr(unchecked_isa_dma, "%d\n");
290 shost_rd_attr(prot_capabilities, "%u\n");
291 shost_rd_attr(prot_guard_type, "%hd\n");
292 shost_rd_attr2(proc_name, hostt->proc_name, "%s\n");
294 static struct attribute *scsi_sysfs_shost_attrs[] = {
295 &dev_attr_unique_id.attr,
296 &dev_attr_host_busy.attr,
297 &dev_attr_cmd_per_lun.attr,
298 &dev_attr_can_queue.attr,
299 &dev_attr_sg_tablesize.attr,
300 &dev_attr_sg_prot_tablesize.attr,
301 &dev_attr_unchecked_isa_dma.attr,
302 &dev_attr_proc_name.attr,
303 &dev_attr_scan.attr,
304 &dev_attr_hstate.attr,
305 &dev_attr_supported_mode.attr,
306 &dev_attr_active_mode.attr,
307 &dev_attr_prot_capabilities.attr,
308 &dev_attr_prot_guard_type.attr,
309 &dev_attr_host_reset.attr,
310 NULL
313 struct attribute_group scsi_shost_attr_group = {
314 .attrs = scsi_sysfs_shost_attrs,
317 const struct attribute_group *scsi_sysfs_shost_attr_groups[] = {
318 &scsi_shost_attr_group,
319 NULL
322 static void scsi_device_cls_release(struct device *class_dev)
324 struct scsi_device *sdev;
326 sdev = class_to_sdev(class_dev);
327 put_device(&sdev->sdev_gendev);
330 static void scsi_device_dev_release_usercontext(struct work_struct *work)
332 struct scsi_device *sdev;
333 struct device *parent;
334 struct scsi_target *starget;
335 struct list_head *this, *tmp;
336 unsigned long flags;
338 sdev = container_of(work, struct scsi_device, ew.work);
340 parent = sdev->sdev_gendev.parent;
341 starget = to_scsi_target(parent);
343 spin_lock_irqsave(sdev->host->host_lock, flags);
344 starget->reap_ref++;
345 list_del(&sdev->siblings);
346 list_del(&sdev->same_target_siblings);
347 list_del(&sdev->starved_entry);
348 spin_unlock_irqrestore(sdev->host->host_lock, flags);
350 cancel_work_sync(&sdev->event_work);
352 list_for_each_safe(this, tmp, &sdev->event_list) {
353 struct scsi_event *evt;
355 evt = list_entry(this, struct scsi_event, node);
356 list_del(&evt->node);
357 kfree(evt);
360 blk_put_queue(sdev->request_queue);
361 /* NULL queue means the device can't be used */
362 sdev->request_queue = NULL;
364 scsi_target_reap(scsi_target(sdev));
366 kfree(sdev->inquiry);
367 kfree(sdev);
369 if (parent)
370 put_device(parent);
373 static void scsi_device_dev_release(struct device *dev)
375 struct scsi_device *sdp = to_scsi_device(dev);
376 execute_in_process_context(scsi_device_dev_release_usercontext,
377 &sdp->ew);
380 static struct class sdev_class = {
381 .name = "scsi_device",
382 .dev_release = scsi_device_cls_release,
385 /* all probing is done in the individual ->probe routines */
386 static int scsi_bus_match(struct device *dev, struct device_driver *gendrv)
388 struct scsi_device *sdp;
390 if (dev->type != &scsi_dev_type)
391 return 0;
393 sdp = to_scsi_device(dev);
394 if (sdp->no_uld_attach)
395 return 0;
396 return (sdp->inq_periph_qual == SCSI_INQ_PQ_CON)? 1: 0;
399 static int scsi_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
401 struct scsi_device *sdev;
403 if (dev->type != &scsi_dev_type)
404 return 0;
406 sdev = to_scsi_device(dev);
408 add_uevent_var(env, "MODALIAS=" SCSI_DEVICE_MODALIAS_FMT, sdev->type);
409 return 0;
412 struct bus_type scsi_bus_type = {
413 .name = "scsi",
414 .match = scsi_bus_match,
415 .uevent = scsi_bus_uevent,
416 #ifdef CONFIG_PM
417 .pm = &scsi_bus_pm_ops,
418 #endif
420 EXPORT_SYMBOL_GPL(scsi_bus_type);
422 int scsi_sysfs_register(void)
424 int error;
426 error = bus_register(&scsi_bus_type);
427 if (!error) {
428 error = class_register(&sdev_class);
429 if (error)
430 bus_unregister(&scsi_bus_type);
433 return error;
436 void scsi_sysfs_unregister(void)
438 class_unregister(&sdev_class);
439 bus_unregister(&scsi_bus_type);
443 * sdev_show_function: macro to create an attr function that can be used to
444 * show a non-bit field.
446 #define sdev_show_function(field, format_string) \
447 static ssize_t \
448 sdev_show_##field (struct device *dev, struct device_attribute *attr, \
449 char *buf) \
451 struct scsi_device *sdev; \
452 sdev = to_scsi_device(dev); \
453 return snprintf (buf, 20, format_string, sdev->field); \
457 * sdev_rd_attr: macro to create a function and attribute variable for a
458 * read only field.
460 #define sdev_rd_attr(field, format_string) \
461 sdev_show_function(field, format_string) \
462 static DEVICE_ATTR(field, S_IRUGO, sdev_show_##field, NULL);
466 * sdev_rw_attr: create a function and attribute variable for a
467 * read/write field.
469 #define sdev_rw_attr(field, format_string) \
470 sdev_show_function(field, format_string) \
472 static ssize_t \
473 sdev_store_##field (struct device *dev, struct device_attribute *attr, \
474 const char *buf, size_t count) \
476 struct scsi_device *sdev; \
477 sdev = to_scsi_device(dev); \
478 sscanf (buf, format_string, &sdev->field); \
479 return count; \
481 static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, sdev_show_##field, sdev_store_##field);
483 /* Currently we don't export bit fields, but we might in future,
484 * so leave this code in */
485 #if 0
487 * sdev_rd_attr: create a function and attribute variable for a
488 * read/write bit field.
490 #define sdev_rw_attr_bit(field) \
491 sdev_show_function(field, "%d\n") \
493 static ssize_t \
494 sdev_store_##field (struct device *dev, struct device_attribute *attr, \
495 const char *buf, size_t count) \
497 int ret; \
498 struct scsi_device *sdev; \
499 ret = scsi_sdev_check_buf_bit(buf); \
500 if (ret >= 0) { \
501 sdev = to_scsi_device(dev); \
502 sdev->field = ret; \
503 ret = count; \
505 return ret; \
507 static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, sdev_show_##field, sdev_store_##field);
510 * scsi_sdev_check_buf_bit: return 0 if buf is "0", return 1 if buf is "1",
511 * else return -EINVAL.
513 static int scsi_sdev_check_buf_bit(const char *buf)
515 if ((buf[1] == '\0') || ((buf[1] == '\n') && (buf[2] == '\0'))) {
516 if (buf[0] == '1')
517 return 1;
518 else if (buf[0] == '0')
519 return 0;
520 else
521 return -EINVAL;
522 } else
523 return -EINVAL;
525 #endif
527 * Create the actual show/store functions and data structures.
529 sdev_rd_attr (device_blocked, "%d\n");
530 sdev_rd_attr (queue_depth, "%d\n");
531 sdev_rd_attr (type, "%d\n");
532 sdev_rd_attr (scsi_level, "%d\n");
533 sdev_rd_attr (vendor, "%.8s\n");
534 sdev_rd_attr (model, "%.16s\n");
535 sdev_rd_attr (rev, "%.4s\n");
538 * TODO: can we make these symlinks to the block layer ones?
540 static ssize_t
541 sdev_show_timeout (struct device *dev, struct device_attribute *attr, char *buf)
543 struct scsi_device *sdev;
544 sdev = to_scsi_device(dev);
545 return snprintf(buf, 20, "%d\n", sdev->request_queue->rq_timeout / HZ);
548 static ssize_t
549 sdev_store_timeout (struct device *dev, struct device_attribute *attr,
550 const char *buf, size_t count)
552 struct scsi_device *sdev;
553 int timeout;
554 sdev = to_scsi_device(dev);
555 sscanf (buf, "%d\n", &timeout);
556 blk_queue_rq_timeout(sdev->request_queue, timeout * HZ);
557 return count;
559 static DEVICE_ATTR(timeout, S_IRUGO | S_IWUSR, sdev_show_timeout, sdev_store_timeout);
561 static ssize_t
562 store_rescan_field (struct device *dev, struct device_attribute *attr,
563 const char *buf, size_t count)
565 scsi_rescan_device(dev);
566 return count;
568 static DEVICE_ATTR(rescan, S_IWUSR, NULL, store_rescan_field);
570 static void sdev_store_delete_callback(struct device *dev)
572 scsi_remove_device(to_scsi_device(dev));
575 static ssize_t
576 sdev_store_delete(struct device *dev, struct device_attribute *attr,
577 const char *buf, size_t count)
579 int rc;
581 /* An attribute cannot be unregistered by one of its own methods,
582 * so we have to use this roundabout approach.
584 rc = device_schedule_callback(dev, sdev_store_delete_callback);
585 if (rc)
586 count = rc;
587 return count;
589 static DEVICE_ATTR(delete, S_IWUSR, NULL, sdev_store_delete);
591 static ssize_t
592 store_state_field(struct device *dev, struct device_attribute *attr,
593 const char *buf, size_t count)
595 int i;
596 struct scsi_device *sdev = to_scsi_device(dev);
597 enum scsi_device_state state = 0;
599 for (i = 0; i < ARRAY_SIZE(sdev_states); i++) {
600 const int len = strlen(sdev_states[i].name);
601 if (strncmp(sdev_states[i].name, buf, len) == 0 &&
602 buf[len] == '\n') {
603 state = sdev_states[i].value;
604 break;
607 if (!state)
608 return -EINVAL;
610 if (scsi_device_set_state(sdev, state))
611 return -EINVAL;
612 return count;
615 static ssize_t
616 show_state_field(struct device *dev, struct device_attribute *attr, char *buf)
618 struct scsi_device *sdev = to_scsi_device(dev);
619 const char *name = scsi_device_state_name(sdev->sdev_state);
621 if (!name)
622 return -EINVAL;
624 return snprintf(buf, 20, "%s\n", name);
627 static DEVICE_ATTR(state, S_IRUGO | S_IWUSR, show_state_field, store_state_field);
629 static ssize_t
630 show_queue_type_field(struct device *dev, struct device_attribute *attr,
631 char *buf)
633 struct scsi_device *sdev = to_scsi_device(dev);
634 const char *name = "none";
636 if (sdev->ordered_tags)
637 name = "ordered";
638 else if (sdev->simple_tags)
639 name = "simple";
641 return snprintf(buf, 20, "%s\n", name);
644 static DEVICE_ATTR(queue_type, S_IRUGO, show_queue_type_field, NULL);
646 static ssize_t
647 show_iostat_counterbits(struct device *dev, struct device_attribute *attr, char *buf)
649 return snprintf(buf, 20, "%d\n", (int)sizeof(atomic_t) * 8);
652 static DEVICE_ATTR(iocounterbits, S_IRUGO, show_iostat_counterbits, NULL);
654 #define show_sdev_iostat(field) \
655 static ssize_t \
656 show_iostat_##field(struct device *dev, struct device_attribute *attr, \
657 char *buf) \
659 struct scsi_device *sdev = to_scsi_device(dev); \
660 unsigned long long count = atomic_read(&sdev->field); \
661 return snprintf(buf, 20, "0x%llx\n", count); \
663 static DEVICE_ATTR(field, S_IRUGO, show_iostat_##field, NULL)
665 show_sdev_iostat(iorequest_cnt);
666 show_sdev_iostat(iodone_cnt);
667 show_sdev_iostat(ioerr_cnt);
669 static ssize_t
670 sdev_show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
672 struct scsi_device *sdev;
673 sdev = to_scsi_device(dev);
674 return snprintf (buf, 20, SCSI_DEVICE_MODALIAS_FMT "\n", sdev->type);
676 static DEVICE_ATTR(modalias, S_IRUGO, sdev_show_modalias, NULL);
678 #define DECLARE_EVT_SHOW(name, Cap_name) \
679 static ssize_t \
680 sdev_show_evt_##name(struct device *dev, struct device_attribute *attr, \
681 char *buf) \
683 struct scsi_device *sdev = to_scsi_device(dev); \
684 int val = test_bit(SDEV_EVT_##Cap_name, sdev->supported_events);\
685 return snprintf(buf, 20, "%d\n", val); \
688 #define DECLARE_EVT_STORE(name, Cap_name) \
689 static ssize_t \
690 sdev_store_evt_##name(struct device *dev, struct device_attribute *attr,\
691 const char *buf, size_t count) \
693 struct scsi_device *sdev = to_scsi_device(dev); \
694 int val = simple_strtoul(buf, NULL, 0); \
695 if (val == 0) \
696 clear_bit(SDEV_EVT_##Cap_name, sdev->supported_events); \
697 else if (val == 1) \
698 set_bit(SDEV_EVT_##Cap_name, sdev->supported_events); \
699 else \
700 return -EINVAL; \
701 return count; \
704 #define DECLARE_EVT(name, Cap_name) \
705 DECLARE_EVT_SHOW(name, Cap_name) \
706 DECLARE_EVT_STORE(name, Cap_name) \
707 static DEVICE_ATTR(evt_##name, S_IRUGO, sdev_show_evt_##name, \
708 sdev_store_evt_##name);
709 #define REF_EVT(name) &dev_attr_evt_##name.attr
711 DECLARE_EVT(media_change, MEDIA_CHANGE)
713 /* Default template for device attributes. May NOT be modified */
714 static struct attribute *scsi_sdev_attrs[] = {
715 &dev_attr_device_blocked.attr,
716 &dev_attr_type.attr,
717 &dev_attr_scsi_level.attr,
718 &dev_attr_vendor.attr,
719 &dev_attr_model.attr,
720 &dev_attr_rev.attr,
721 &dev_attr_rescan.attr,
722 &dev_attr_delete.attr,
723 &dev_attr_state.attr,
724 &dev_attr_timeout.attr,
725 &dev_attr_iocounterbits.attr,
726 &dev_attr_iorequest_cnt.attr,
727 &dev_attr_iodone_cnt.attr,
728 &dev_attr_ioerr_cnt.attr,
729 &dev_attr_modalias.attr,
730 REF_EVT(media_change),
731 NULL
734 static struct attribute_group scsi_sdev_attr_group = {
735 .attrs = scsi_sdev_attrs,
738 static const struct attribute_group *scsi_sdev_attr_groups[] = {
739 &scsi_sdev_attr_group,
740 NULL
743 static ssize_t
744 sdev_store_queue_depth_rw(struct device *dev, struct device_attribute *attr,
745 const char *buf, size_t count)
747 int depth, retval;
748 struct scsi_device *sdev = to_scsi_device(dev);
749 struct scsi_host_template *sht = sdev->host->hostt;
751 if (!sht->change_queue_depth)
752 return -EINVAL;
754 depth = simple_strtoul(buf, NULL, 0);
756 if (depth < 1)
757 return -EINVAL;
759 retval = sht->change_queue_depth(sdev, depth,
760 SCSI_QDEPTH_DEFAULT);
761 if (retval < 0)
762 return retval;
764 sdev->max_queue_depth = sdev->queue_depth;
766 return count;
769 static struct device_attribute sdev_attr_queue_depth_rw =
770 __ATTR(queue_depth, S_IRUGO | S_IWUSR, sdev_show_queue_depth,
771 sdev_store_queue_depth_rw);
773 static ssize_t
774 sdev_show_queue_ramp_up_period(struct device *dev,
775 struct device_attribute *attr,
776 char *buf)
778 struct scsi_device *sdev;
779 sdev = to_scsi_device(dev);
780 return snprintf(buf, 20, "%u\n",
781 jiffies_to_msecs(sdev->queue_ramp_up_period));
784 static ssize_t
785 sdev_store_queue_ramp_up_period(struct device *dev,
786 struct device_attribute *attr,
787 const char *buf, size_t count)
789 struct scsi_device *sdev = to_scsi_device(dev);
790 unsigned long period;
792 if (strict_strtoul(buf, 10, &period))
793 return -EINVAL;
795 sdev->queue_ramp_up_period = msecs_to_jiffies(period);
796 return period;
799 static struct device_attribute sdev_attr_queue_ramp_up_period =
800 __ATTR(queue_ramp_up_period, S_IRUGO | S_IWUSR,
801 sdev_show_queue_ramp_up_period,
802 sdev_store_queue_ramp_up_period);
804 static ssize_t
805 sdev_store_queue_type_rw(struct device *dev, struct device_attribute *attr,
806 const char *buf, size_t count)
808 struct scsi_device *sdev = to_scsi_device(dev);
809 struct scsi_host_template *sht = sdev->host->hostt;
810 int tag_type = 0, retval;
811 int prev_tag_type = scsi_get_tag_type(sdev);
813 if (!sdev->tagged_supported || !sht->change_queue_type)
814 return -EINVAL;
816 if (strncmp(buf, "ordered", 7) == 0)
817 tag_type = MSG_ORDERED_TAG;
818 else if (strncmp(buf, "simple", 6) == 0)
819 tag_type = MSG_SIMPLE_TAG;
820 else if (strncmp(buf, "none", 4) != 0)
821 return -EINVAL;
823 if (tag_type == prev_tag_type)
824 return count;
826 retval = sht->change_queue_type(sdev, tag_type);
827 if (retval < 0)
828 return retval;
830 return count;
833 static int scsi_target_add(struct scsi_target *starget)
835 int error;
837 if (starget->state != STARGET_CREATED)
838 return 0;
840 error = device_add(&starget->dev);
841 if (error) {
842 dev_err(&starget->dev, "target device_add failed, error %d\n", error);
843 return error;
845 transport_add_device(&starget->dev);
846 starget->state = STARGET_RUNNING;
848 pm_runtime_set_active(&starget->dev);
849 pm_runtime_enable(&starget->dev);
850 device_enable_async_suspend(&starget->dev);
852 return 0;
855 static struct device_attribute sdev_attr_queue_type_rw =
856 __ATTR(queue_type, S_IRUGO | S_IWUSR, show_queue_type_field,
857 sdev_store_queue_type_rw);
860 * scsi_sysfs_add_sdev - add scsi device to sysfs
861 * @sdev: scsi_device to add
863 * Return value:
864 * 0 on Success / non-zero on Failure
866 int scsi_sysfs_add_sdev(struct scsi_device *sdev)
868 int error, i;
869 struct request_queue *rq = sdev->request_queue;
870 struct scsi_target *starget = sdev->sdev_target;
872 error = scsi_device_set_state(sdev, SDEV_RUNNING);
873 if (error)
874 return error;
876 error = scsi_target_add(starget);
877 if (error)
878 return error;
880 transport_configure_device(&starget->dev);
882 device_enable_async_suspend(&sdev->sdev_gendev);
883 scsi_autopm_get_target(starget);
884 pm_runtime_set_active(&sdev->sdev_gendev);
885 pm_runtime_forbid(&sdev->sdev_gendev);
886 pm_runtime_enable(&sdev->sdev_gendev);
887 scsi_autopm_put_target(starget);
889 /* The following call will keep sdev active indefinitely, until
890 * its driver does a corresponding scsi_autopm_pm_device(). Only
891 * drivers supporting autosuspend will do this.
893 scsi_autopm_get_device(sdev);
895 error = device_add(&sdev->sdev_gendev);
896 if (error) {
897 sdev_printk(KERN_INFO, sdev,
898 "failed to add device: %d\n", error);
899 return error;
901 device_enable_async_suspend(&sdev->sdev_dev);
902 error = device_add(&sdev->sdev_dev);
903 if (error) {
904 sdev_printk(KERN_INFO, sdev,
905 "failed to add class device: %d\n", error);
906 device_del(&sdev->sdev_gendev);
907 return error;
909 transport_add_device(&sdev->sdev_gendev);
910 sdev->is_visible = 1;
912 /* create queue files, which may be writable, depending on the host */
913 if (sdev->host->hostt->change_queue_depth) {
914 error = device_create_file(&sdev->sdev_gendev,
915 &sdev_attr_queue_depth_rw);
916 error = device_create_file(&sdev->sdev_gendev,
917 &sdev_attr_queue_ramp_up_period);
919 else
920 error = device_create_file(&sdev->sdev_gendev, &dev_attr_queue_depth);
921 if (error)
922 return error;
924 if (sdev->host->hostt->change_queue_type)
925 error = device_create_file(&sdev->sdev_gendev, &sdev_attr_queue_type_rw);
926 else
927 error = device_create_file(&sdev->sdev_gendev, &dev_attr_queue_type);
928 if (error)
929 return error;
931 error = bsg_register_queue(rq, &sdev->sdev_gendev, NULL, NULL);
933 if (error)
934 /* we're treating error on bsg register as non-fatal,
935 * so pretend nothing went wrong */
936 sdev_printk(KERN_INFO, sdev,
937 "Failed to register bsg queue, errno=%d\n", error);
939 /* add additional host specific attributes */
940 if (sdev->host->hostt->sdev_attrs) {
941 for (i = 0; sdev->host->hostt->sdev_attrs[i]; i++) {
942 error = device_create_file(&sdev->sdev_gendev,
943 sdev->host->hostt->sdev_attrs[i]);
944 if (error)
945 return error;
949 return error;
952 void __scsi_remove_device(struct scsi_device *sdev)
954 struct device *dev = &sdev->sdev_gendev;
956 if (sdev->is_visible) {
957 if (scsi_device_set_state(sdev, SDEV_CANCEL) != 0)
958 return;
960 bsg_unregister_queue(sdev->request_queue);
961 device_unregister(&sdev->sdev_dev);
962 transport_remove_device(dev);
963 device_del(dev);
964 } else
965 put_device(&sdev->sdev_dev);
966 scsi_device_set_state(sdev, SDEV_DEL);
967 if (sdev->host->hostt->slave_destroy)
968 sdev->host->hostt->slave_destroy(sdev);
969 transport_destroy_device(dev);
971 /* Freeing the queue signals to block that we're done */
972 blk_cleanup_queue(sdev->request_queue);
973 put_device(dev);
977 * scsi_remove_device - unregister a device from the scsi bus
978 * @sdev: scsi_device to unregister
980 void scsi_remove_device(struct scsi_device *sdev)
982 struct Scsi_Host *shost = sdev->host;
984 mutex_lock(&shost->scan_mutex);
985 __scsi_remove_device(sdev);
986 mutex_unlock(&shost->scan_mutex);
988 EXPORT_SYMBOL(scsi_remove_device);
990 static void __scsi_remove_target(struct scsi_target *starget)
992 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
993 unsigned long flags;
994 struct scsi_device *sdev;
996 spin_lock_irqsave(shost->host_lock, flags);
997 restart:
998 list_for_each_entry(sdev, &shost->__devices, siblings) {
999 if (sdev->channel != starget->channel ||
1000 sdev->id != starget->id ||
1001 scsi_device_get(sdev))
1002 continue;
1003 spin_unlock_irqrestore(shost->host_lock, flags);
1004 scsi_remove_device(sdev);
1005 scsi_device_put(sdev);
1006 spin_lock_irqsave(shost->host_lock, flags);
1007 goto restart;
1009 spin_unlock_irqrestore(shost->host_lock, flags);
1013 * scsi_remove_target - try to remove a target and all its devices
1014 * @dev: generic starget or parent of generic stargets to be removed
1016 * Note: This is slightly racy. It is possible that if the user
1017 * requests the addition of another device then the target won't be
1018 * removed.
1020 void scsi_remove_target(struct device *dev)
1022 struct Scsi_Host *shost = dev_to_shost(dev->parent);
1023 struct scsi_target *starget, *last = NULL;
1024 unsigned long flags;
1026 /* remove targets being careful to lookup next entry before
1027 * deleting the last
1029 spin_lock_irqsave(shost->host_lock, flags);
1030 list_for_each_entry(starget, &shost->__targets, siblings) {
1031 if (starget->state == STARGET_DEL)
1032 continue;
1033 if (starget->dev.parent == dev || &starget->dev == dev) {
1034 /* assuming new targets arrive at the end */
1035 starget->reap_ref++;
1036 spin_unlock_irqrestore(shost->host_lock, flags);
1037 if (last)
1038 scsi_target_reap(last);
1039 last = starget;
1040 __scsi_remove_target(starget);
1041 spin_lock_irqsave(shost->host_lock, flags);
1044 spin_unlock_irqrestore(shost->host_lock, flags);
1046 if (last)
1047 scsi_target_reap(last);
1049 EXPORT_SYMBOL(scsi_remove_target);
1051 int scsi_register_driver(struct device_driver *drv)
1053 drv->bus = &scsi_bus_type;
1055 return driver_register(drv);
1057 EXPORT_SYMBOL(scsi_register_driver);
1059 int scsi_register_interface(struct class_interface *intf)
1061 intf->class = &sdev_class;
1063 return class_interface_register(intf);
1065 EXPORT_SYMBOL(scsi_register_interface);
1068 * scsi_sysfs_add_host - add scsi host to subsystem
1069 * @shost: scsi host struct to add to subsystem
1071 int scsi_sysfs_add_host(struct Scsi_Host *shost)
1073 int error, i;
1075 /* add host specific attributes */
1076 if (shost->hostt->shost_attrs) {
1077 for (i = 0; shost->hostt->shost_attrs[i]; i++) {
1078 error = device_create_file(&shost->shost_dev,
1079 shost->hostt->shost_attrs[i]);
1080 if (error)
1081 return error;
1085 transport_register_device(&shost->shost_gendev);
1086 transport_configure_device(&shost->shost_gendev);
1087 return 0;
1090 static struct device_type scsi_dev_type = {
1091 .name = "scsi_device",
1092 .release = scsi_device_dev_release,
1093 .groups = scsi_sdev_attr_groups,
1096 void scsi_sysfs_device_initialize(struct scsi_device *sdev)
1098 unsigned long flags;
1099 struct Scsi_Host *shost = sdev->host;
1100 struct scsi_target *starget = sdev->sdev_target;
1102 device_initialize(&sdev->sdev_gendev);
1103 sdev->sdev_gendev.bus = &scsi_bus_type;
1104 sdev->sdev_gendev.type = &scsi_dev_type;
1105 dev_set_name(&sdev->sdev_gendev, "%d:%d:%d:%d",
1106 sdev->host->host_no, sdev->channel, sdev->id, sdev->lun);
1108 device_initialize(&sdev->sdev_dev);
1109 sdev->sdev_dev.parent = get_device(&sdev->sdev_gendev);
1110 sdev->sdev_dev.class = &sdev_class;
1111 dev_set_name(&sdev->sdev_dev, "%d:%d:%d:%d",
1112 sdev->host->host_no, sdev->channel, sdev->id, sdev->lun);
1113 sdev->scsi_level = starget->scsi_level;
1114 transport_setup_device(&sdev->sdev_gendev);
1115 spin_lock_irqsave(shost->host_lock, flags);
1116 list_add_tail(&sdev->same_target_siblings, &starget->devices);
1117 list_add_tail(&sdev->siblings, &shost->__devices);
1118 spin_unlock_irqrestore(shost->host_lock, flags);
1121 int scsi_is_sdev_device(const struct device *dev)
1123 return dev->type == &scsi_dev_type;
1125 EXPORT_SYMBOL(scsi_is_sdev_device);
1127 /* A blank transport template that is used in drivers that don't
1128 * yet implement Transport Attributes */
1129 struct scsi_transport_template blank_transport_template = { { { {NULL, }, }, }, };