blob: 67bb20ed45d23ef431280a2c63a03e14ea252731 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * scsi_sysfs.c
3 *
4 * SCSI sysfs interface routines.
5 *
6 * Created to pull SCSI mid layer sysfs routines into one file.
7 */
8
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/module.h>
10#include <linux/init.h>
11#include <linux/blkdev.h>
12#include <linux/device.h>
13
14#include <scsi/scsi.h>
15#include <scsi/scsi_device.h>
16#include <scsi/scsi_host.h>
17#include <scsi/scsi_tcq.h>
18#include <scsi/scsi_transport.h>
Adrian Bunk44818ef2007-07-09 11:59:59 -070019#include <scsi/scsi_driver.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
21#include "scsi_priv.h"
22#include "scsi_logging.h"
23
Arjan van de Ven0ad78202005-11-28 16:22:25 +010024static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 enum scsi_device_state value;
26 char *name;
27} sdev_states[] = {
28 { SDEV_CREATED, "created" },
29 { SDEV_RUNNING, "running" },
30 { SDEV_CANCEL, "cancel" },
31 { SDEV_DEL, "deleted" },
32 { SDEV_QUIESCE, "quiesce" },
33 { SDEV_OFFLINE, "offline" },
34 { SDEV_BLOCK, "blocked" },
35};
36
37const char *scsi_device_state_name(enum scsi_device_state state)
38{
39 int i;
40 char *name = NULL;
41
Tobias Klauser6391a112006-06-08 22:23:48 -070042 for (i = 0; i < ARRAY_SIZE(sdev_states); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 if (sdev_states[i].value == state) {
44 name = sdev_states[i].name;
45 break;
46 }
47 }
48 return name;
49}
50
Arjan van de Ven0ad78202005-11-28 16:22:25 +010051static const struct {
Mike Andersond3301872005-06-16 11:12:38 -070052 enum scsi_host_state value;
53 char *name;
54} shost_states[] = {
55 { SHOST_CREATED, "created" },
56 { SHOST_RUNNING, "running" },
57 { SHOST_CANCEL, "cancel" },
58 { SHOST_DEL, "deleted" },
59 { SHOST_RECOVERY, "recovery" },
James Bottomley939647e2005-09-18 15:05:20 -050060 { SHOST_CANCEL_RECOVERY, "cancel/recovery" },
61 { SHOST_DEL_RECOVERY, "deleted/recovery", },
Mike Andersond3301872005-06-16 11:12:38 -070062};
63const char *scsi_host_state_name(enum scsi_host_state state)
64{
65 int i;
66 char *name = NULL;
67
Tobias Klauser6391a112006-06-08 22:23:48 -070068 for (i = 0; i < ARRAY_SIZE(shost_states); i++) {
Mike Andersond3301872005-06-16 11:12:38 -070069 if (shost_states[i].value == state) {
70 name = shost_states[i].name;
71 break;
72 }
73 }
74 return name;
75}
76
Linus Torvalds1da177e2005-04-16 15:20:36 -070077static int check_set(unsigned int *val, char *src)
78{
79 char *last;
80
81 if (strncmp(src, "-", 20) == 0) {
82 *val = SCAN_WILD_CARD;
83 } else {
84 /*
85 * Doesn't check for int overflow
86 */
87 *val = simple_strtoul(src, &last, 0);
88 if (*last != '\0')
89 return 1;
90 }
91 return 0;
92}
93
94static int scsi_scan(struct Scsi_Host *shost, const char *str)
95{
96 char s1[15], s2[15], s3[15], junk;
97 unsigned int channel, id, lun;
98 int res;
99
100 res = sscanf(str, "%10s %10s %10s %c", s1, s2, s3, &junk);
101 if (res != 3)
102 return -EINVAL;
103 if (check_set(&channel, s1))
104 return -EINVAL;
105 if (check_set(&id, s2))
106 return -EINVAL;
107 if (check_set(&lun, s3))
108 return -EINVAL;
Christoph Hellwige02f3f52006-01-13 19:04:00 +0100109 if (shost->transportt->user_scan)
110 res = shost->transportt->user_scan(shost, channel, id, lun);
111 else
112 res = scsi_scan_host_selected(shost, channel, id, lun, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 return res;
114}
115
116/*
117 * shost_show_function: macro to create an attr function that can be used to
118 * show a non-bit field.
119 */
120#define shost_show_function(name, field, format_string) \
121static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +0100122show_##name (struct device *dev, struct device_attribute *attr, \
123 char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124{ \
Tony Jonesee959b02008-02-22 00:13:36 +0100125 struct Scsi_Host *shost = class_to_shost(dev); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 return snprintf (buf, 20, format_string, shost->field); \
127}
128
129/*
130 * shost_rd_attr: macro to create a function and attribute variable for a
131 * read only field.
132 */
133#define shost_rd_attr2(name, field, format_string) \
134 shost_show_function(name, field, format_string) \
Tony Jonesee959b02008-02-22 00:13:36 +0100135static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
137#define shost_rd_attr(field, format_string) \
138shost_rd_attr2(field, field, format_string)
139
140/*
141 * Create the actual show/store functions and data structures.
142 */
143
Tony Jonesee959b02008-02-22 00:13:36 +0100144static ssize_t
145store_scan(struct device *dev, struct device_attribute *attr,
146 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147{
Tony Jonesee959b02008-02-22 00:13:36 +0100148 struct Scsi_Host *shost = class_to_shost(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 int res;
150
151 res = scsi_scan(shost, buf);
152 if (res == 0)
153 res = count;
154 return res;
155};
Tony Jonesee959b02008-02-22 00:13:36 +0100156static DEVICE_ATTR(scan, S_IWUSR, NULL, store_scan);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
Mike Andersond3301872005-06-16 11:12:38 -0700158static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +0100159store_shost_state(struct device *dev, struct device_attribute *attr,
160 const char *buf, size_t count)
Mike Andersond3301872005-06-16 11:12:38 -0700161{
162 int i;
Tony Jonesee959b02008-02-22 00:13:36 +0100163 struct Scsi_Host *shost = class_to_shost(dev);
Mike Andersond3301872005-06-16 11:12:38 -0700164 enum scsi_host_state state = 0;
165
Tobias Klauser6391a112006-06-08 22:23:48 -0700166 for (i = 0; i < ARRAY_SIZE(shost_states); i++) {
Mike Andersond3301872005-06-16 11:12:38 -0700167 const int len = strlen(shost_states[i].name);
168 if (strncmp(shost_states[i].name, buf, len) == 0 &&
169 buf[len] == '\n') {
170 state = shost_states[i].value;
171 break;
172 }
173 }
174 if (!state)
175 return -EINVAL;
176
177 if (scsi_host_set_state(shost, state))
178 return -EINVAL;
179 return count;
180}
181
182static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +0100183show_shost_state(struct device *dev, struct device_attribute *attr, char *buf)
Mike Andersond3301872005-06-16 11:12:38 -0700184{
Tony Jonesee959b02008-02-22 00:13:36 +0100185 struct Scsi_Host *shost = class_to_shost(dev);
Mike Andersond3301872005-06-16 11:12:38 -0700186 const char *name = scsi_host_state_name(shost->shost_state);
187
188 if (!name)
189 return -EINVAL;
190
191 return snprintf(buf, 20, "%s\n", name);
192}
193
Tony Jonesee959b02008-02-22 00:13:36 +0100194/* DEVICE_ATTR(state) clashes with dev_attr_state for sdev */
195struct device_attribute dev_attr_hstate =
196 __ATTR(state, S_IRUGO | S_IWUSR, show_shost_state, store_shost_state);
Mike Andersond3301872005-06-16 11:12:38 -0700197
FUJITA Tomonori5dc2b892007-09-01 02:02:20 +0900198static ssize_t
199show_shost_mode(unsigned int mode, char *buf)
200{
201 ssize_t len = 0;
202
203 if (mode & MODE_INITIATOR)
204 len = sprintf(buf, "%s", "Initiator");
205
206 if (mode & MODE_TARGET)
207 len += sprintf(buf + len, "%s%s", len ? ", " : "", "Target");
208
209 len += sprintf(buf + len, "\n");
210
211 return len;
212}
213
Tony Jonesee959b02008-02-22 00:13:36 +0100214static ssize_t
215show_shost_supported_mode(struct device *dev, struct device_attribute *attr,
216 char *buf)
FUJITA Tomonori5dc2b892007-09-01 02:02:20 +0900217{
Tony Jonesee959b02008-02-22 00:13:36 +0100218 struct Scsi_Host *shost = class_to_shost(dev);
James Bottomley7a39ac32007-09-25 22:45:53 -0500219 unsigned int supported_mode = shost->hostt->supported_mode;
FUJITA Tomonori5dc2b892007-09-01 02:02:20 +0900220
James Bottomley7a39ac32007-09-25 22:45:53 -0500221 if (supported_mode == MODE_UNKNOWN)
222 /* by default this should be initiator */
223 supported_mode = MODE_INITIATOR;
224
225 return show_shost_mode(supported_mode, buf);
FUJITA Tomonori5dc2b892007-09-01 02:02:20 +0900226}
227
Tony Jonesee959b02008-02-22 00:13:36 +0100228static DEVICE_ATTR(supported_mode, S_IRUGO | S_IWUSR, show_shost_supported_mode, NULL);
FUJITA Tomonori5dc2b892007-09-01 02:02:20 +0900229
Tony Jonesee959b02008-02-22 00:13:36 +0100230static ssize_t
231show_shost_active_mode(struct device *dev,
232 struct device_attribute *attr, char *buf)
FUJITA Tomonori5dc2b892007-09-01 02:02:20 +0900233{
Tony Jonesee959b02008-02-22 00:13:36 +0100234 struct Scsi_Host *shost = class_to_shost(dev);
FUJITA Tomonori5dc2b892007-09-01 02:02:20 +0900235
236 if (shost->active_mode == MODE_UNKNOWN)
237 return snprintf(buf, 20, "unknown\n");
238 else
239 return show_shost_mode(shost->active_mode, buf);
240}
241
Tony Jonesee959b02008-02-22 00:13:36 +0100242static DEVICE_ATTR(active_mode, S_IRUGO | S_IWUSR, show_shost_active_mode, NULL);
FUJITA Tomonori5dc2b892007-09-01 02:02:20 +0900243
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244shost_rd_attr(unique_id, "%u\n");
245shost_rd_attr(host_busy, "%hu\n");
246shost_rd_attr(cmd_per_lun, "%hd\n");
James Bottomleyed632da2006-10-16 10:06:27 -0500247shost_rd_attr(can_queue, "%hd\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248shost_rd_attr(sg_tablesize, "%hu\n");
249shost_rd_attr(unchecked_isa_dma, "%d\n");
250shost_rd_attr2(proc_name, hostt->proc_name, "%s\n");
251
Tony Jonesee959b02008-02-22 00:13:36 +0100252static struct device_attribute *scsi_sysfs_shost_attrs[] = {
253 &dev_attr_unique_id,
254 &dev_attr_host_busy,
255 &dev_attr_cmd_per_lun,
256 &dev_attr_can_queue,
257 &dev_attr_sg_tablesize,
258 &dev_attr_unchecked_isa_dma,
259 &dev_attr_proc_name,
260 &dev_attr_scan,
261 &dev_attr_hstate,
262 &dev_attr_supported_mode,
263 &dev_attr_active_mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 NULL
265};
266
Tony Jonesee959b02008-02-22 00:13:36 +0100267static void scsi_device_cls_release(struct device *class_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268{
269 struct scsi_device *sdev;
270
271 sdev = class_to_sdev(class_dev);
272 put_device(&sdev->sdev_gendev);
273}
274
David Howells65f27f32006-11-22 14:55:48 +0000275static void scsi_device_dev_release_usercontext(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276{
277 struct scsi_device *sdev;
278 struct device *parent;
279 struct scsi_target *starget;
Jeff Garzika341cd02007-10-29 17:15:22 -0400280 struct list_head *this, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 unsigned long flags;
282
David Howells65f27f32006-11-22 14:55:48 +0000283 sdev = container_of(work, struct scsi_device, ew.work);
284
285 parent = sdev->sdev_gendev.parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 starget = to_scsi_target(parent);
287
288 spin_lock_irqsave(sdev->host->host_lock, flags);
289 starget->reap_ref++;
290 list_del(&sdev->siblings);
291 list_del(&sdev->same_target_siblings);
292 list_del(&sdev->starved_entry);
293 spin_unlock_irqrestore(sdev->host->host_lock, flags);
294
Jeff Garzika341cd02007-10-29 17:15:22 -0400295 cancel_work_sync(&sdev->event_work);
296
297 list_for_each_safe(this, tmp, &sdev->event_list) {
298 struct scsi_event *evt;
299
300 evt = list_entry(this, struct scsi_event, node);
301 list_del(&evt->node);
302 kfree(evt);
303 }
304
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 if (sdev->request_queue) {
306 sdev->request_queue->queuedata = NULL;
James Bottomley65110b22006-02-14 10:48:46 -0600307 /* user context needed to free queue */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 scsi_free_queue(sdev->request_queue);
c2a93312005-04-12 16:38:09 -0500309 /* temporary expedient, try to catch use of queue lock
310 * after free of sdev */
311 sdev->request_queue = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 }
313
314 scsi_target_reap(scsi_target(sdev));
315
316 kfree(sdev->inquiry);
317 kfree(sdev);
318
319 if (parent)
320 put_device(parent);
321}
322
James Bottomley65110b22006-02-14 10:48:46 -0600323static void scsi_device_dev_release(struct device *dev)
324{
James Bottomleyffedb452006-02-23 14:27:18 -0600325 struct scsi_device *sdp = to_scsi_device(dev);
David Howells65f27f32006-11-22 14:55:48 +0000326 execute_in_process_context(scsi_device_dev_release_usercontext,
James Bottomleyffedb452006-02-23 14:27:18 -0600327 &sdp->ew);
James Bottomley65110b22006-02-14 10:48:46 -0600328}
329
Adrian Bunk52c1da32005-06-23 22:05:33 -0700330static struct class sdev_class = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 .name = "scsi_device",
Tony Jonesee959b02008-02-22 00:13:36 +0100332 .dev_release = scsi_device_cls_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333};
334
335/* all probing is done in the individual ->probe routines */
336static int scsi_bus_match(struct device *dev, struct device_driver *gendrv)
337{
338 struct scsi_device *sdp = to_scsi_device(dev);
339 if (sdp->no_uld_attach)
340 return 0;
341 return (sdp->inq_periph_qual == SCSI_INQ_PQ_CON)? 1: 0;
342}
343
Kay Sievers7eff2e72007-08-14 15:15:12 +0200344static int scsi_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
Michael Tokarevd7b8bcb2006-10-27 16:02:37 +0400345{
346 struct scsi_device *sdev = to_scsi_device(dev);
Michael Tokarevd7b8bcb2006-10-27 16:02:37 +0400347
Kay Sievers7eff2e72007-08-14 15:15:12 +0200348 add_uevent_var(env, "MODALIAS=" SCSI_DEVICE_MODALIAS_FMT, sdev->type);
Michael Tokarevd7b8bcb2006-10-27 16:02:37 +0400349 return 0;
350}
351
Jens Axboe9b847542006-01-06 09:28:07 +0100352static int scsi_bus_suspend(struct device * dev, pm_message_t state)
353{
Tejun Heoc3c94c52007-03-21 00:13:59 +0900354 struct device_driver *drv = dev->driver;
Jens Axboe9b847542006-01-06 09:28:07 +0100355 struct scsi_device *sdev = to_scsi_device(dev);
Jens Axboe9b847542006-01-06 09:28:07 +0100356 int err;
357
358 err = scsi_device_quiesce(sdev);
359 if (err)
360 return err;
361
Tejun Heoc3c94c52007-03-21 00:13:59 +0900362 if (drv && drv->suspend) {
363 err = drv->suspend(dev, state);
364 if (err)
365 return err;
366 }
Jens Axboe9b847542006-01-06 09:28:07 +0100367
Tejun Heoc3c94c52007-03-21 00:13:59 +0900368 return 0;
Jens Axboe9b847542006-01-06 09:28:07 +0100369}
370
371static int scsi_bus_resume(struct device * dev)
372{
Tejun Heoc3c94c52007-03-21 00:13:59 +0900373 struct device_driver *drv = dev->driver;
Jens Axboe9b847542006-01-06 09:28:07 +0100374 struct scsi_device *sdev = to_scsi_device(dev);
Tejun Heo1dfcda02007-03-21 16:05:16 +0900375 int err = 0;
Jens Axboe9b847542006-01-06 09:28:07 +0100376
Tejun Heoc3c94c52007-03-21 00:13:59 +0900377 if (drv && drv->resume)
Tejun Heo1dfcda02007-03-21 16:05:16 +0900378 err = drv->resume(dev);
Tejun Heoc3c94c52007-03-21 00:13:59 +0900379
Jens Axboe9b847542006-01-06 09:28:07 +0100380 scsi_device_resume(sdev);
Tejun Heoc3c94c52007-03-21 00:13:59 +0900381
Tejun Heo1dfcda02007-03-21 16:05:16 +0900382 return err;
Jens Axboe9b847542006-01-06 09:28:07 +0100383}
384
James Bottomley751bf4d2008-01-02 11:14:30 -0600385static int scsi_bus_remove(struct device *dev)
386{
387 struct device_driver *drv = dev->driver;
388 struct scsi_device *sdev = to_scsi_device(dev);
389 int err = 0;
390
391 /* reset the prep_fn back to the default since the
392 * driver may have altered it and it's being removed */
393 blk_queue_prep_rq(sdev->request_queue, scsi_prep_fn);
394
395 if (drv && drv->remove)
396 err = drv->remove(dev);
397
398 return 0;
399}
400
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401struct bus_type scsi_bus_type = {
402 .name = "scsi",
403 .match = scsi_bus_match,
Michael Tokarevd7b8bcb2006-10-27 16:02:37 +0400404 .uevent = scsi_bus_uevent,
Jens Axboe9b847542006-01-06 09:28:07 +0100405 .suspend = scsi_bus_suspend,
406 .resume = scsi_bus_resume,
James Bottomley751bf4d2008-01-02 11:14:30 -0600407 .remove = scsi_bus_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408};
409
410int scsi_sysfs_register(void)
411{
412 int error;
413
414 error = bus_register(&scsi_bus_type);
415 if (!error) {
416 error = class_register(&sdev_class);
417 if (error)
418 bus_unregister(&scsi_bus_type);
419 }
420
421 return error;
422}
423
424void scsi_sysfs_unregister(void)
425{
426 class_unregister(&sdev_class);
427 bus_unregister(&scsi_bus_type);
428}
429
430/*
431 * sdev_show_function: macro to create an attr function that can be used to
432 * show a non-bit field.
433 */
434#define sdev_show_function(field, format_string) \
435static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +0100436sdev_show_##field (struct device *dev, struct device_attribute *attr, \
437 char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438{ \
439 struct scsi_device *sdev; \
440 sdev = to_scsi_device(dev); \
441 return snprintf (buf, 20, format_string, sdev->field); \
442} \
443
444/*
445 * sdev_rd_attr: macro to create a function and attribute variable for a
446 * read only field.
447 */
448#define sdev_rd_attr(field, format_string) \
449 sdev_show_function(field, format_string) \
450static DEVICE_ATTR(field, S_IRUGO, sdev_show_##field, NULL);
451
452
453/*
454 * sdev_rd_attr: create a function and attribute variable for a
455 * read/write field.
456 */
457#define sdev_rw_attr(field, format_string) \
458 sdev_show_function(field, format_string) \
459 \
460static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +0100461sdev_store_##field (struct device *dev, struct device_attribute *attr, \
462 const char *buf, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463{ \
464 struct scsi_device *sdev; \
465 sdev = to_scsi_device(dev); \
466 snscanf (buf, 20, format_string, &sdev->field); \
467 return count; \
468} \
469static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, sdev_show_##field, sdev_store_##field);
470
471/* Currently we don't export bit fields, but we might in future,
472 * so leave this code in */
473#if 0
474/*
475 * sdev_rd_attr: create a function and attribute variable for a
476 * read/write bit field.
477 */
478#define sdev_rw_attr_bit(field) \
479 sdev_show_function(field, "%d\n") \
480 \
481static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +0100482sdev_store_##field (struct device *dev, struct device_attribute *attr, \
483 const char *buf, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484{ \
485 int ret; \
486 struct scsi_device *sdev; \
487 ret = scsi_sdev_check_buf_bit(buf); \
488 if (ret >= 0) { \
489 sdev = to_scsi_device(dev); \
490 sdev->field = ret; \
491 ret = count; \
492 } \
493 return ret; \
494} \
495static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, sdev_show_##field, sdev_store_##field);
496
497/*
498 * scsi_sdev_check_buf_bit: return 0 if buf is "0", return 1 if buf is "1",
499 * else return -EINVAL.
500 */
501static int scsi_sdev_check_buf_bit(const char *buf)
502{
503 if ((buf[1] == '\0') || ((buf[1] == '\n') && (buf[2] == '\0'))) {
504 if (buf[0] == '1')
505 return 1;
506 else if (buf[0] == '0')
507 return 0;
508 else
509 return -EINVAL;
510 } else
511 return -EINVAL;
512}
513#endif
514/*
515 * Create the actual show/store functions and data structures.
516 */
517sdev_rd_attr (device_blocked, "%d\n");
518sdev_rd_attr (queue_depth, "%d\n");
519sdev_rd_attr (type, "%d\n");
520sdev_rd_attr (scsi_level, "%d\n");
521sdev_rd_attr (vendor, "%.8s\n");
522sdev_rd_attr (model, "%.16s\n");
523sdev_rd_attr (rev, "%.4s\n");
524
525static ssize_t
Yani Ioannou10523b32005-05-17 06:43:37 -0400526sdev_show_timeout (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527{
528 struct scsi_device *sdev;
529 sdev = to_scsi_device(dev);
530 return snprintf (buf, 20, "%d\n", sdev->timeout / HZ);
531}
532
533static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +0100534sdev_store_timeout (struct device *dev, struct device_attribute *attr,
535 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536{
537 struct scsi_device *sdev;
538 int timeout;
539 sdev = to_scsi_device(dev);
540 sscanf (buf, "%d\n", &timeout);
541 sdev->timeout = timeout * HZ;
542 return count;
543}
544static DEVICE_ATTR(timeout, S_IRUGO | S_IWUSR, sdev_show_timeout, sdev_store_timeout);
545
546static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +0100547store_rescan_field (struct device *dev, struct device_attribute *attr,
548 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549{
550 scsi_rescan_device(dev);
551 return count;
552}
553static DEVICE_ATTR(rescan, S_IWUSR, NULL, store_rescan_field);
554
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400555static void sdev_store_delete_callback(struct device *dev)
556{
557 scsi_remove_device(to_scsi_device(dev));
558}
559
Tony Jonesee959b02008-02-22 00:13:36 +0100560static ssize_t
561sdev_store_delete(struct device *dev, struct device_attribute *attr,
562 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563{
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400564 int rc;
565
566 /* An attribute cannot be unregistered by one of its own methods,
567 * so we have to use this roundabout approach.
568 */
569 rc = device_schedule_callback(dev, sdev_store_delete_callback);
570 if (rc)
571 count = rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 return count;
573};
574static DEVICE_ATTR(delete, S_IWUSR, NULL, sdev_store_delete);
575
576static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +0100577store_state_field(struct device *dev, struct device_attribute *attr,
578 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579{
580 int i;
581 struct scsi_device *sdev = to_scsi_device(dev);
582 enum scsi_device_state state = 0;
583
Tobias Klauser6391a112006-06-08 22:23:48 -0700584 for (i = 0; i < ARRAY_SIZE(sdev_states); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 const int len = strlen(sdev_states[i].name);
586 if (strncmp(sdev_states[i].name, buf, len) == 0 &&
587 buf[len] == '\n') {
588 state = sdev_states[i].value;
589 break;
590 }
591 }
592 if (!state)
593 return -EINVAL;
594
595 if (scsi_device_set_state(sdev, state))
596 return -EINVAL;
597 return count;
598}
599
600static ssize_t
Yani Ioannou10523b32005-05-17 06:43:37 -0400601show_state_field(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602{
603 struct scsi_device *sdev = to_scsi_device(dev);
604 const char *name = scsi_device_state_name(sdev->sdev_state);
605
606 if (!name)
607 return -EINVAL;
608
609 return snprintf(buf, 20, "%s\n", name);
610}
611
612static DEVICE_ATTR(state, S_IRUGO | S_IWUSR, show_state_field, store_state_field);
613
614static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +0100615show_queue_type_field(struct device *dev, struct device_attribute *attr,
616 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617{
618 struct scsi_device *sdev = to_scsi_device(dev);
619 const char *name = "none";
620
621 if (sdev->ordered_tags)
622 name = "ordered";
623 else if (sdev->simple_tags)
624 name = "simple";
625
626 return snprintf(buf, 20, "%s\n", name);
627}
628
629static DEVICE_ATTR(queue_type, S_IRUGO, show_queue_type_field, NULL);
630
631static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +0100632show_iostat_counterbits(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633{
634 return snprintf(buf, 20, "%d\n", (int)sizeof(atomic_t) * 8);
635}
636
637static DEVICE_ATTR(iocounterbits, S_IRUGO, show_iostat_counterbits, NULL);
638
639#define show_sdev_iostat(field) \
640static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +0100641show_iostat_##field(struct device *dev, struct device_attribute *attr, \
642 char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643{ \
644 struct scsi_device *sdev = to_scsi_device(dev); \
645 unsigned long long count = atomic_read(&sdev->field); \
646 return snprintf(buf, 20, "0x%llx\n", count); \
647} \
648static DEVICE_ATTR(field, S_IRUGO, show_iostat_##field, NULL)
649
650show_sdev_iostat(iorequest_cnt);
651show_sdev_iostat(iodone_cnt);
652show_sdev_iostat(ioerr_cnt);
653
Michael Tokarevd7b8bcb2006-10-27 16:02:37 +0400654static ssize_t
655sdev_show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
656{
657 struct scsi_device *sdev;
658 sdev = to_scsi_device(dev);
659 return snprintf (buf, 20, SCSI_DEVICE_MODALIAS_FMT "\n", sdev->type);
660}
661static DEVICE_ATTR(modalias, S_IRUGO, sdev_show_modalias, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
Jeff Garzika341cd02007-10-29 17:15:22 -0400663#define DECLARE_EVT_SHOW(name, Cap_name) \
664static ssize_t \
665sdev_show_evt_##name(struct device *dev, struct device_attribute *attr, \
Tony Jonesee959b02008-02-22 00:13:36 +0100666 char *buf) \
Jeff Garzika341cd02007-10-29 17:15:22 -0400667{ \
668 struct scsi_device *sdev = to_scsi_device(dev); \
669 int val = test_bit(SDEV_EVT_##Cap_name, sdev->supported_events);\
670 return snprintf(buf, 20, "%d\n", val); \
671}
672
673#define DECLARE_EVT_STORE(name, Cap_name) \
674static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +0100675sdev_store_evt_##name(struct device *dev, struct device_attribute *attr,\
Jeff Garzika341cd02007-10-29 17:15:22 -0400676 const char *buf, size_t count) \
677{ \
678 struct scsi_device *sdev = to_scsi_device(dev); \
679 int val = simple_strtoul(buf, NULL, 0); \
680 if (val == 0) \
681 clear_bit(SDEV_EVT_##Cap_name, sdev->supported_events); \
682 else if (val == 1) \
683 set_bit(SDEV_EVT_##Cap_name, sdev->supported_events); \
684 else \
685 return -EINVAL; \
686 return count; \
687}
688
689#define DECLARE_EVT(name, Cap_name) \
690 DECLARE_EVT_SHOW(name, Cap_name) \
691 DECLARE_EVT_STORE(name, Cap_name) \
692 static DEVICE_ATTR(evt_##name, S_IRUGO, sdev_show_evt_##name, \
693 sdev_store_evt_##name);
694#define REF_EVT(name) &dev_attr_evt_##name.attr
695
696DECLARE_EVT(media_change, MEDIA_CHANGE)
697
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698/* Default template for device attributes. May NOT be modified */
Kay Sieversbfd12942007-09-11 17:00:14 +0200699static struct attribute *scsi_sdev_attrs[] = {
700 &dev_attr_device_blocked.attr,
701 &dev_attr_type.attr,
702 &dev_attr_scsi_level.attr,
703 &dev_attr_vendor.attr,
704 &dev_attr_model.attr,
705 &dev_attr_rev.attr,
706 &dev_attr_rescan.attr,
707 &dev_attr_delete.attr,
708 &dev_attr_state.attr,
709 &dev_attr_timeout.attr,
710 &dev_attr_iocounterbits.attr,
711 &dev_attr_iorequest_cnt.attr,
712 &dev_attr_iodone_cnt.attr,
713 &dev_attr_ioerr_cnt.attr,
714 &dev_attr_modalias.attr,
Jeff Garzika341cd02007-10-29 17:15:22 -0400715 REF_EVT(media_change),
Kay Sieversbfd12942007-09-11 17:00:14 +0200716 NULL
717};
718
719static struct attribute_group scsi_sdev_attr_group = {
720 .attrs = scsi_sdev_attrs,
721};
722
723static struct attribute_group *scsi_sdev_attr_groups[] = {
724 &scsi_sdev_attr_group,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 NULL
726};
727
Tony Jonesee959b02008-02-22 00:13:36 +0100728static ssize_t
729sdev_store_queue_depth_rw(struct device *dev, struct device_attribute *attr,
730 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731{
732 int depth, retval;
733 struct scsi_device *sdev = to_scsi_device(dev);
734 struct scsi_host_template *sht = sdev->host->hostt;
735
736 if (!sht->change_queue_depth)
737 return -EINVAL;
738
739 depth = simple_strtoul(buf, NULL, 0);
740
741 if (depth < 1)
742 return -EINVAL;
743
744 retval = sht->change_queue_depth(sdev, depth);
745 if (retval < 0)
746 return retval;
747
748 return count;
749}
750
751static struct device_attribute sdev_attr_queue_depth_rw =
752 __ATTR(queue_depth, S_IRUGO | S_IWUSR, sdev_show_queue_depth,
753 sdev_store_queue_depth_rw);
754
Tony Jonesee959b02008-02-22 00:13:36 +0100755static ssize_t
756sdev_store_queue_type_rw(struct device *dev, struct device_attribute *attr,
757 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758{
759 struct scsi_device *sdev = to_scsi_device(dev);
760 struct scsi_host_template *sht = sdev->host->hostt;
761 int tag_type = 0, retval;
762 int prev_tag_type = scsi_get_tag_type(sdev);
763
764 if (!sdev->tagged_supported || !sht->change_queue_type)
765 return -EINVAL;
766
767 if (strncmp(buf, "ordered", 7) == 0)
768 tag_type = MSG_ORDERED_TAG;
769 else if (strncmp(buf, "simple", 6) == 0)
770 tag_type = MSG_SIMPLE_TAG;
771 else if (strncmp(buf, "none", 4) != 0)
772 return -EINVAL;
773
774 if (tag_type == prev_tag_type)
775 return count;
776
777 retval = sht->change_queue_type(sdev, tag_type);
778 if (retval < 0)
779 return retval;
780
781 return count;
782}
783
784static struct device_attribute sdev_attr_queue_type_rw =
785 __ATTR(queue_type, S_IRUGO | S_IWUSR, show_queue_type_field,
786 sdev_store_queue_type_rw);
787
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788/**
789 * scsi_sysfs_add_sdev - add scsi device to sysfs
790 * @sdev: scsi_device to add
791 *
792 * Return value:
793 * 0 on Success / non-zero on Failure
794 **/
795int scsi_sysfs_add_sdev(struct scsi_device *sdev)
796{
797 int error, i;
James Bottomley80ed71c2007-07-19 10:15:10 -0500798 struct request_queue *rq = sdev->request_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
800 if ((error = scsi_device_set_state(sdev, SDEV_RUNNING)) != 0)
801 return error;
802
803 error = device_add(&sdev->sdev_gendev);
804 if (error) {
805 put_device(sdev->sdev_gendev.parent);
806 printk(KERN_INFO "error 1\n");
807 return error;
808 }
Tony Jonesee959b02008-02-22 00:13:36 +0100809 error = device_add(&sdev->sdev_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 if (error) {
811 printk(KERN_INFO "error 2\n");
812 goto clean_device;
813 }
814
Tony Jonesee959b02008-02-22 00:13:36 +0100815 /* take a reference for the sdev_dev; this is
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 * released by the sdev_class .release */
817 get_device(&sdev->sdev_gendev);
James Bottomley80ed71c2007-07-19 10:15:10 -0500818
Kay Sieversbfd12942007-09-11 17:00:14 +0200819 /* create queue files, which may be writable, depending on the host */
820 if (sdev->host->hostt->change_queue_depth)
821 error = device_create_file(&sdev->sdev_gendev, &sdev_attr_queue_depth_rw);
822 else
823 error = device_create_file(&sdev->sdev_gendev, &dev_attr_queue_depth);
824 if (error) {
825 __scsi_remove_device(sdev);
826 goto out;
827 }
828 if (sdev->host->hostt->change_queue_type)
829 error = device_create_file(&sdev->sdev_gendev, &sdev_attr_queue_type_rw);
830 else
831 error = device_create_file(&sdev->sdev_gendev, &dev_attr_queue_type);
832 if (error) {
833 __scsi_remove_device(sdev);
834 goto out;
835 }
836
James Bottomley39dca552007-07-20 18:22:17 -0500837 error = bsg_register_queue(rq, &sdev->sdev_gendev, NULL);
James Bottomley80ed71c2007-07-19 10:15:10 -0500838
839 if (error)
840 sdev_printk(KERN_INFO, sdev,
841 "Failed to register bsg queue, errno=%d\n", error);
842
843 /* we're treating error on bsg register as non-fatal, so pretend
844 * nothing went wrong */
845 error = 0;
846
Kay Sieversbfd12942007-09-11 17:00:14 +0200847 /* add additional host specific attributes */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 if (sdev->host->hostt->sdev_attrs) {
849 for (i = 0; sdev->host->hostt->sdev_attrs[i]; i++) {
Kay Sieversbfd12942007-09-11 17:00:14 +0200850 error = device_create_file(&sdev->sdev_gendev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 sdev->host->hostt->sdev_attrs[i]);
852 if (error) {
Alan Stern903f4fe2005-07-26 10:20:53 -0400853 __scsi_remove_device(sdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 goto out;
855 }
856 }
857 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858
859 transport_add_device(&sdev->sdev_gendev);
860 out:
861 return error;
862
863 clean_device:
864 scsi_device_set_state(sdev, SDEV_CANCEL);
865
866 device_del(&sdev->sdev_gendev);
867 transport_destroy_device(&sdev->sdev_gendev);
868 put_device(&sdev->sdev_gendev);
869
870 return error;
871}
872
Alan Stern903f4fe2005-07-26 10:20:53 -0400873void __scsi_remove_device(struct scsi_device *sdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874{
James Bottomleydf133c22005-11-06 11:47:08 -0600875 struct device *dev = &sdev->sdev_gendev;
876
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 if (scsi_device_set_state(sdev, SDEV_CANCEL) != 0)
Alan Stern903f4fe2005-07-26 10:20:53 -0400878 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879
James Bottomley0feed272008-03-26 09:09:19 -0700880 bsg_unregister_queue(sdev->request_queue);
Tony Jonesee959b02008-02-22 00:13:36 +0100881 device_unregister(&sdev->sdev_dev);
James Bottomleydf133c22005-11-06 11:47:08 -0600882 transport_remove_device(dev);
883 device_del(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 scsi_device_set_state(sdev, SDEV_DEL);
885 if (sdev->host->hostt->slave_destroy)
886 sdev->host->hostt->slave_destroy(sdev);
James Bottomleydf133c22005-11-06 11:47:08 -0600887 transport_destroy_device(dev);
888 put_device(dev);
Alan Stern903f4fe2005-07-26 10:20:53 -0400889}
890
891/**
892 * scsi_remove_device - unregister a device from the scsi bus
893 * @sdev: scsi_device to unregister
894 **/
895void scsi_remove_device(struct scsi_device *sdev)
896{
Alan Stern54195002005-09-15 21:52:51 -0400897 struct Scsi_Host *shost = sdev->host;
898
Arjan van de Ven0b950672006-01-11 13:16:10 +0100899 mutex_lock(&shost->scan_mutex);
Alan Stern903f4fe2005-07-26 10:20:53 -0400900 __scsi_remove_device(sdev);
Arjan van de Ven0b950672006-01-11 13:16:10 +0100901 mutex_unlock(&shost->scan_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902}
903EXPORT_SYMBOL(scsi_remove_device);
904
Adrian Bunk44818ef2007-07-09 11:59:59 -0700905static void __scsi_remove_target(struct scsi_target *starget)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906{
907 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
908 unsigned long flags;
Alan Sterna64358d2005-07-26 10:27:10 -0400909 struct scsi_device *sdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910
911 spin_lock_irqsave(shost->host_lock, flags);
912 starget->reap_ref++;
Alan Sterna64358d2005-07-26 10:27:10 -0400913 restart:
914 list_for_each_entry(sdev, &shost->__devices, siblings) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 if (sdev->channel != starget->channel ||
Alan Sterna64358d2005-07-26 10:27:10 -0400916 sdev->id != starget->id ||
917 sdev->sdev_state == SDEV_DEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 continue;
919 spin_unlock_irqrestore(shost->host_lock, flags);
920 scsi_remove_device(sdev);
921 spin_lock_irqsave(shost->host_lock, flags);
Alan Sterna64358d2005-07-26 10:27:10 -0400922 goto restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 }
924 spin_unlock_irqrestore(shost->host_lock, flags);
925 scsi_target_reap(starget);
926}
927
mochel@digitalimplant.org20b1e672005-03-24 19:03:59 -0800928static int __remove_child (struct device * dev, void * data)
929{
930 if (scsi_is_target_device(dev))
931 __scsi_remove_target(to_scsi_target(dev));
932 return 0;
933}
934
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935/**
936 * scsi_remove_target - try to remove a target and all its devices
937 * @dev: generic starget or parent of generic stargets to be removed
938 *
939 * Note: This is slightly racy. It is possible that if the user
940 * requests the addition of another device then the target won't be
941 * removed.
942 */
943void scsi_remove_target(struct device *dev)
944{
mochel@digitalimplant.org20b1e672005-03-24 19:03:59 -0800945 struct device *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946
947 if (scsi_is_target_device(dev)) {
948 __scsi_remove_target(to_scsi_target(dev));
949 return;
950 }
951
952 rdev = get_device(dev);
mochel@digitalimplant.org20b1e672005-03-24 19:03:59 -0800953 device_for_each_child(dev, NULL, __remove_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 put_device(rdev);
955}
956EXPORT_SYMBOL(scsi_remove_target);
957
958int scsi_register_driver(struct device_driver *drv)
959{
960 drv->bus = &scsi_bus_type;
961
962 return driver_register(drv);
963}
964EXPORT_SYMBOL(scsi_register_driver);
965
966int scsi_register_interface(struct class_interface *intf)
967{
968 intf->class = &sdev_class;
969
970 return class_interface_register(intf);
971}
972EXPORT_SYMBOL(scsi_register_interface);
973
974
Tony Jonesee959b02008-02-22 00:13:36 +0100975static struct device_attribute *class_attr_overridden(
976 struct device_attribute **attrs,
977 struct device_attribute *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978{
979 int i;
980
981 if (!attrs)
982 return NULL;
983 for (i = 0; attrs[i]; i++)
984 if (!strcmp(attrs[i]->attr.name, attr->attr.name))
985 return attrs[i];
986 return NULL;
987}
988
Tony Jonesee959b02008-02-22 00:13:36 +0100989static int class_attr_add(struct device *classdev,
990 struct device_attribute *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991{
Tony Jonesee959b02008-02-22 00:13:36 +0100992 struct device_attribute *base_attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993
994 /*
995 * Spare the caller from having to copy things it's not interested in.
996 */
997 base_attr = class_attr_overridden(scsi_sysfs_shost_attrs, attr);
998 if (base_attr) {
999 /* extend permissions */
1000 attr->attr.mode |= base_attr->attr.mode;
1001
1002 /* override null show/store with default */
1003 if (!attr->show)
1004 attr->show = base_attr->show;
1005 if (!attr->store)
1006 attr->store = base_attr->store;
1007 }
1008
Tony Jonesee959b02008-02-22 00:13:36 +01001009 return device_create_file(classdev, attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010}
1011
1012/**
1013 * scsi_sysfs_add_host - add scsi host to subsystem
1014 * @shost: scsi host struct to add to subsystem
1015 * @dev: parent struct device pointer
1016 **/
1017int scsi_sysfs_add_host(struct Scsi_Host *shost)
1018{
1019 int error, i;
1020
1021 if (shost->hostt->shost_attrs) {
1022 for (i = 0; shost->hostt->shost_attrs[i]; i++) {
Tony Jonesee959b02008-02-22 00:13:36 +01001023 error = class_attr_add(&shost->shost_dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 shost->hostt->shost_attrs[i]);
1025 if (error)
1026 return error;
1027 }
1028 }
1029
1030 for (i = 0; scsi_sysfs_shost_attrs[i]; i++) {
1031 if (!class_attr_overridden(shost->hostt->shost_attrs,
1032 scsi_sysfs_shost_attrs[i])) {
Tony Jonesee959b02008-02-22 00:13:36 +01001033 error = device_create_file(&shost->shost_dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 scsi_sysfs_shost_attrs[i]);
1035 if (error)
1036 return error;
1037 }
1038 }
1039
1040 transport_register_device(&shost->shost_gendev);
James Bottomleyd52b3812008-01-05 09:38:30 -06001041 transport_configure_device(&shost->shost_gendev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 return 0;
1043}
1044
Kay Sieversbfd12942007-09-11 17:00:14 +02001045static struct device_type scsi_dev_type = {
1046 .name = "scsi_device",
1047 .release = scsi_device_dev_release,
1048 .groups = scsi_sdev_attr_groups,
1049};
1050
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051void scsi_sysfs_device_initialize(struct scsi_device *sdev)
1052{
1053 unsigned long flags;
1054 struct Scsi_Host *shost = sdev->host;
1055 struct scsi_target *starget = sdev->sdev_target;
1056
1057 device_initialize(&sdev->sdev_gendev);
1058 sdev->sdev_gendev.bus = &scsi_bus_type;
Kay Sieversbfd12942007-09-11 17:00:14 +02001059 sdev->sdev_gendev.type = &scsi_dev_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 sprintf(sdev->sdev_gendev.bus_id,"%d:%d:%d:%d",
1061 sdev->host->host_no, sdev->channel, sdev->id,
1062 sdev->lun);
1063
Tony Jonesee959b02008-02-22 00:13:36 +01001064 device_initialize(&sdev->sdev_dev);
1065 sdev->sdev_dev.parent = &sdev->sdev_gendev;
1066 sdev->sdev_dev.class = &sdev_class;
1067 snprintf(sdev->sdev_dev.bus_id, BUS_ID_SIZE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 "%d:%d:%d:%d", sdev->host->host_no,
1069 sdev->channel, sdev->id, sdev->lun);
Alan Stern7c9d6f12007-01-08 11:12:32 -05001070 sdev->scsi_level = starget->scsi_level;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 transport_setup_device(&sdev->sdev_gendev);
1072 spin_lock_irqsave(shost->host_lock, flags);
1073 list_add_tail(&sdev->same_target_siblings, &starget->devices);
1074 list_add_tail(&sdev->siblings, &shost->__devices);
1075 spin_unlock_irqrestore(shost->host_lock, flags);
1076}
1077
1078int scsi_is_sdev_device(const struct device *dev)
1079{
Kay Sievers13ba9bc2007-09-26 19:54:49 +02001080 return dev->type == &scsi_dev_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081}
1082EXPORT_SYMBOL(scsi_is_sdev_device);
1083
1084/* A blank transport template that is used in drivers that don't
1085 * yet implement Transport Attributes */
1086struct scsi_transport_template blank_transport_template = { { { {NULL, }, }, }, };