blob: fbd7f9ed1251feb5dab52bf1c92b48c4045b6886 [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
Hannes Reineckeb0ed4332008-03-18 14:32:28 +010024static struct device_type scsi_dev_type;
25
Arjan van de Ven0ad78202005-11-28 16:22:25 +010026static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 enum scsi_device_state value;
28 char *name;
29} sdev_states[] = {
30 { SDEV_CREATED, "created" },
31 { SDEV_RUNNING, "running" },
32 { SDEV_CANCEL, "cancel" },
33 { SDEV_DEL, "deleted" },
34 { SDEV_QUIESCE, "quiesce" },
35 { SDEV_OFFLINE, "offline" },
36 { SDEV_BLOCK, "blocked" },
37};
38
39const char *scsi_device_state_name(enum scsi_device_state state)
40{
41 int i;
42 char *name = NULL;
43
Tobias Klauser6391a112006-06-08 22:23:48 -070044 for (i = 0; i < ARRAY_SIZE(sdev_states); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 if (sdev_states[i].value == state) {
46 name = sdev_states[i].name;
47 break;
48 }
49 }
50 return name;
51}
52
Arjan van de Ven0ad78202005-11-28 16:22:25 +010053static const struct {
Mike Andersond3301872005-06-16 11:12:38 -070054 enum scsi_host_state value;
55 char *name;
56} shost_states[] = {
57 { SHOST_CREATED, "created" },
58 { SHOST_RUNNING, "running" },
59 { SHOST_CANCEL, "cancel" },
60 { SHOST_DEL, "deleted" },
61 { SHOST_RECOVERY, "recovery" },
James Bottomley939647e2005-09-18 15:05:20 -050062 { SHOST_CANCEL_RECOVERY, "cancel/recovery" },
63 { SHOST_DEL_RECOVERY, "deleted/recovery", },
Mike Andersond3301872005-06-16 11:12:38 -070064};
65const char *scsi_host_state_name(enum scsi_host_state state)
66{
67 int i;
68 char *name = NULL;
69
Tobias Klauser6391a112006-06-08 22:23:48 -070070 for (i = 0; i < ARRAY_SIZE(shost_states); i++) {
Mike Andersond3301872005-06-16 11:12:38 -070071 if (shost_states[i].value == state) {
72 name = shost_states[i].name;
73 break;
74 }
75 }
76 return name;
77}
78
Linus Torvalds1da177e2005-04-16 15:20:36 -070079static int check_set(unsigned int *val, char *src)
80{
81 char *last;
82
83 if (strncmp(src, "-", 20) == 0) {
84 *val = SCAN_WILD_CARD;
85 } else {
86 /*
87 * Doesn't check for int overflow
88 */
89 *val = simple_strtoul(src, &last, 0);
90 if (*last != '\0')
91 return 1;
92 }
93 return 0;
94}
95
96static int scsi_scan(struct Scsi_Host *shost, const char *str)
97{
98 char s1[15], s2[15], s3[15], junk;
99 unsigned int channel, id, lun;
100 int res;
101
102 res = sscanf(str, "%10s %10s %10s %c", s1, s2, s3, &junk);
103 if (res != 3)
104 return -EINVAL;
105 if (check_set(&channel, s1))
106 return -EINVAL;
107 if (check_set(&id, s2))
108 return -EINVAL;
109 if (check_set(&lun, s3))
110 return -EINVAL;
Christoph Hellwige02f3f52006-01-13 19:04:00 +0100111 if (shost->transportt->user_scan)
112 res = shost->transportt->user_scan(shost, channel, id, lun);
113 else
114 res = scsi_scan_host_selected(shost, channel, id, lun, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 return res;
116}
117
118/*
119 * shost_show_function: macro to create an attr function that can be used to
120 * show a non-bit field.
121 */
122#define shost_show_function(name, field, format_string) \
123static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +0100124show_##name (struct device *dev, struct device_attribute *attr, \
125 char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126{ \
Tony Jonesee959b02008-02-22 00:13:36 +0100127 struct Scsi_Host *shost = class_to_shost(dev); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 return snprintf (buf, 20, format_string, shost->field); \
129}
130
131/*
132 * shost_rd_attr: macro to create a function and attribute variable for a
133 * read only field.
134 */
135#define shost_rd_attr2(name, field, format_string) \
136 shost_show_function(name, field, format_string) \
Tony Jonesee959b02008-02-22 00:13:36 +0100137static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
139#define shost_rd_attr(field, format_string) \
140shost_rd_attr2(field, field, format_string)
141
142/*
143 * Create the actual show/store functions and data structures.
144 */
145
Tony Jonesee959b02008-02-22 00:13:36 +0100146static ssize_t
147store_scan(struct device *dev, struct device_attribute *attr,
148 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149{
Tony Jonesee959b02008-02-22 00:13:36 +0100150 struct Scsi_Host *shost = class_to_shost(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 int res;
152
153 res = scsi_scan(shost, buf);
154 if (res == 0)
155 res = count;
156 return res;
157};
Tony Jonesee959b02008-02-22 00:13:36 +0100158static DEVICE_ATTR(scan, S_IWUSR, NULL, store_scan);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
Mike Andersond3301872005-06-16 11:12:38 -0700160static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +0100161store_shost_state(struct device *dev, struct device_attribute *attr,
162 const char *buf, size_t count)
Mike Andersond3301872005-06-16 11:12:38 -0700163{
164 int i;
Tony Jonesee959b02008-02-22 00:13:36 +0100165 struct Scsi_Host *shost = class_to_shost(dev);
Mike Andersond3301872005-06-16 11:12:38 -0700166 enum scsi_host_state state = 0;
167
Tobias Klauser6391a112006-06-08 22:23:48 -0700168 for (i = 0; i < ARRAY_SIZE(shost_states); i++) {
Mike Andersond3301872005-06-16 11:12:38 -0700169 const int len = strlen(shost_states[i].name);
170 if (strncmp(shost_states[i].name, buf, len) == 0 &&
171 buf[len] == '\n') {
172 state = shost_states[i].value;
173 break;
174 }
175 }
176 if (!state)
177 return -EINVAL;
178
179 if (scsi_host_set_state(shost, state))
180 return -EINVAL;
181 return count;
182}
183
184static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +0100185show_shost_state(struct device *dev, struct device_attribute *attr, char *buf)
Mike Andersond3301872005-06-16 11:12:38 -0700186{
Tony Jonesee959b02008-02-22 00:13:36 +0100187 struct Scsi_Host *shost = class_to_shost(dev);
Mike Andersond3301872005-06-16 11:12:38 -0700188 const char *name = scsi_host_state_name(shost->shost_state);
189
190 if (!name)
191 return -EINVAL;
192
193 return snprintf(buf, 20, "%s\n", name);
194}
195
Tony Jonesee959b02008-02-22 00:13:36 +0100196/* DEVICE_ATTR(state) clashes with dev_attr_state for sdev */
197struct device_attribute dev_attr_hstate =
198 __ATTR(state, S_IRUGO | S_IWUSR, show_shost_state, store_shost_state);
Mike Andersond3301872005-06-16 11:12:38 -0700199
FUJITA Tomonori5dc2b892007-09-01 02:02:20 +0900200static ssize_t
201show_shost_mode(unsigned int mode, char *buf)
202{
203 ssize_t len = 0;
204
205 if (mode & MODE_INITIATOR)
206 len = sprintf(buf, "%s", "Initiator");
207
208 if (mode & MODE_TARGET)
209 len += sprintf(buf + len, "%s%s", len ? ", " : "", "Target");
210
211 len += sprintf(buf + len, "\n");
212
213 return len;
214}
215
Tony Jonesee959b02008-02-22 00:13:36 +0100216static ssize_t
217show_shost_supported_mode(struct device *dev, struct device_attribute *attr,
218 char *buf)
FUJITA Tomonori5dc2b892007-09-01 02:02:20 +0900219{
Tony Jonesee959b02008-02-22 00:13:36 +0100220 struct Scsi_Host *shost = class_to_shost(dev);
James Bottomley7a39ac32007-09-25 22:45:53 -0500221 unsigned int supported_mode = shost->hostt->supported_mode;
FUJITA Tomonori5dc2b892007-09-01 02:02:20 +0900222
James Bottomley7a39ac32007-09-25 22:45:53 -0500223 if (supported_mode == MODE_UNKNOWN)
224 /* by default this should be initiator */
225 supported_mode = MODE_INITIATOR;
226
227 return show_shost_mode(supported_mode, buf);
FUJITA Tomonori5dc2b892007-09-01 02:02:20 +0900228}
229
Tony Jonesee959b02008-02-22 00:13:36 +0100230static DEVICE_ATTR(supported_mode, S_IRUGO | S_IWUSR, show_shost_supported_mode, NULL);
FUJITA Tomonori5dc2b892007-09-01 02:02:20 +0900231
Tony Jonesee959b02008-02-22 00:13:36 +0100232static ssize_t
233show_shost_active_mode(struct device *dev,
234 struct device_attribute *attr, char *buf)
FUJITA Tomonori5dc2b892007-09-01 02:02:20 +0900235{
Tony Jonesee959b02008-02-22 00:13:36 +0100236 struct Scsi_Host *shost = class_to_shost(dev);
FUJITA Tomonori5dc2b892007-09-01 02:02:20 +0900237
238 if (shost->active_mode == MODE_UNKNOWN)
239 return snprintf(buf, 20, "unknown\n");
240 else
241 return show_shost_mode(shost->active_mode, buf);
242}
243
Tony Jonesee959b02008-02-22 00:13:36 +0100244static DEVICE_ATTR(active_mode, S_IRUGO | S_IWUSR, show_shost_active_mode, NULL);
FUJITA Tomonori5dc2b892007-09-01 02:02:20 +0900245
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246shost_rd_attr(unique_id, "%u\n");
247shost_rd_attr(host_busy, "%hu\n");
248shost_rd_attr(cmd_per_lun, "%hd\n");
James Bottomleyed632da2006-10-16 10:06:27 -0500249shost_rd_attr(can_queue, "%hd\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250shost_rd_attr(sg_tablesize, "%hu\n");
251shost_rd_attr(unchecked_isa_dma, "%d\n");
252shost_rd_attr2(proc_name, hostt->proc_name, "%s\n");
253
Tony Jonesee959b02008-02-22 00:13:36 +0100254static struct device_attribute *scsi_sysfs_shost_attrs[] = {
255 &dev_attr_unique_id,
256 &dev_attr_host_busy,
257 &dev_attr_cmd_per_lun,
258 &dev_attr_can_queue,
259 &dev_attr_sg_tablesize,
260 &dev_attr_unchecked_isa_dma,
261 &dev_attr_proc_name,
262 &dev_attr_scan,
263 &dev_attr_hstate,
264 &dev_attr_supported_mode,
265 &dev_attr_active_mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 NULL
267};
268
Tony Jonesee959b02008-02-22 00:13:36 +0100269static void scsi_device_cls_release(struct device *class_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270{
271 struct scsi_device *sdev;
272
273 sdev = class_to_sdev(class_dev);
274 put_device(&sdev->sdev_gendev);
275}
276
David Howells65f27f32006-11-22 14:55:48 +0000277static void scsi_device_dev_release_usercontext(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278{
279 struct scsi_device *sdev;
280 struct device *parent;
281 struct scsi_target *starget;
Jeff Garzika341cd02007-10-29 17:15:22 -0400282 struct list_head *this, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 unsigned long flags;
284
David Howells65f27f32006-11-22 14:55:48 +0000285 sdev = container_of(work, struct scsi_device, ew.work);
286
287 parent = sdev->sdev_gendev.parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 starget = to_scsi_target(parent);
289
290 spin_lock_irqsave(sdev->host->host_lock, flags);
291 starget->reap_ref++;
292 list_del(&sdev->siblings);
293 list_del(&sdev->same_target_siblings);
294 list_del(&sdev->starved_entry);
295 spin_unlock_irqrestore(sdev->host->host_lock, flags);
296
Jeff Garzika341cd02007-10-29 17:15:22 -0400297 cancel_work_sync(&sdev->event_work);
298
299 list_for_each_safe(this, tmp, &sdev->event_list) {
300 struct scsi_event *evt;
301
302 evt = list_entry(this, struct scsi_event, node);
303 list_del(&evt->node);
304 kfree(evt);
305 }
306
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 if (sdev->request_queue) {
308 sdev->request_queue->queuedata = NULL;
James Bottomley65110b22006-02-14 10:48:46 -0600309 /* user context needed to free queue */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 scsi_free_queue(sdev->request_queue);
c2a93312005-04-12 16:38:09 -0500311 /* temporary expedient, try to catch use of queue lock
312 * after free of sdev */
313 sdev->request_queue = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 }
315
316 scsi_target_reap(scsi_target(sdev));
317
318 kfree(sdev->inquiry);
319 kfree(sdev);
320
321 if (parent)
322 put_device(parent);
323}
324
James Bottomley65110b22006-02-14 10:48:46 -0600325static void scsi_device_dev_release(struct device *dev)
326{
James Bottomleyffedb452006-02-23 14:27:18 -0600327 struct scsi_device *sdp = to_scsi_device(dev);
David Howells65f27f32006-11-22 14:55:48 +0000328 execute_in_process_context(scsi_device_dev_release_usercontext,
James Bottomleyffedb452006-02-23 14:27:18 -0600329 &sdp->ew);
James Bottomley65110b22006-02-14 10:48:46 -0600330}
331
Adrian Bunk52c1da32005-06-23 22:05:33 -0700332static struct class sdev_class = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 .name = "scsi_device",
Tony Jonesee959b02008-02-22 00:13:36 +0100334 .dev_release = scsi_device_cls_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335};
336
337/* all probing is done in the individual ->probe routines */
338static int scsi_bus_match(struct device *dev, struct device_driver *gendrv)
339{
Hannes Reineckeb0ed4332008-03-18 14:32:28 +0100340 struct scsi_device *sdp;
341
342 if (dev->type != &scsi_dev_type)
343 return 0;
344
345 sdp = to_scsi_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 if (sdp->no_uld_attach)
347 return 0;
348 return (sdp->inq_periph_qual == SCSI_INQ_PQ_CON)? 1: 0;
349}
350
Kay Sievers7eff2e72007-08-14 15:15:12 +0200351static int scsi_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
Michael Tokarevd7b8bcb2006-10-27 16:02:37 +0400352{
353 struct scsi_device *sdev = to_scsi_device(dev);
Michael Tokarevd7b8bcb2006-10-27 16:02:37 +0400354
Kay Sievers7eff2e72007-08-14 15:15:12 +0200355 add_uevent_var(env, "MODALIAS=" SCSI_DEVICE_MODALIAS_FMT, sdev->type);
Michael Tokarevd7b8bcb2006-10-27 16:02:37 +0400356 return 0;
357}
358
Jens Axboe9b847542006-01-06 09:28:07 +0100359static int scsi_bus_suspend(struct device * dev, pm_message_t state)
360{
Hannes Reineckeb0ed4332008-03-18 14:32:28 +0100361 struct device_driver *drv;
362 struct scsi_device *sdev;
Jens Axboe9b847542006-01-06 09:28:07 +0100363 int err;
364
Hannes Reineckeb0ed4332008-03-18 14:32:28 +0100365 if (dev->type != &scsi_dev_type)
366 return 0;
367
368 drv = dev->driver;
369 sdev = to_scsi_device(dev);
370
Jens Axboe9b847542006-01-06 09:28:07 +0100371 err = scsi_device_quiesce(sdev);
372 if (err)
373 return err;
374
Tejun Heoc3c94c52007-03-21 00:13:59 +0900375 if (drv && drv->suspend) {
376 err = drv->suspend(dev, state);
377 if (err)
378 return err;
379 }
Jens Axboe9b847542006-01-06 09:28:07 +0100380
Tejun Heoc3c94c52007-03-21 00:13:59 +0900381 return 0;
Jens Axboe9b847542006-01-06 09:28:07 +0100382}
383
384static int scsi_bus_resume(struct device * dev)
385{
Hannes Reineckeb0ed4332008-03-18 14:32:28 +0100386 struct device_driver *drv;
387 struct scsi_device *sdev;
Tejun Heo1dfcda02007-03-21 16:05:16 +0900388 int err = 0;
Jens Axboe9b847542006-01-06 09:28:07 +0100389
Hannes Reineckeb0ed4332008-03-18 14:32:28 +0100390 if (dev->type != &scsi_dev_type)
391 return 0;
392
393 drv = dev->driver;
394 sdev = to_scsi_device(dev);
395
Tejun Heoc3c94c52007-03-21 00:13:59 +0900396 if (drv && drv->resume)
Tejun Heo1dfcda02007-03-21 16:05:16 +0900397 err = drv->resume(dev);
Tejun Heoc3c94c52007-03-21 00:13:59 +0900398
Jens Axboe9b847542006-01-06 09:28:07 +0100399 scsi_device_resume(sdev);
Tejun Heoc3c94c52007-03-21 00:13:59 +0900400
Tejun Heo1dfcda02007-03-21 16:05:16 +0900401 return err;
Jens Axboe9b847542006-01-06 09:28:07 +0100402}
403
James Bottomley751bf4d2008-01-02 11:14:30 -0600404static int scsi_bus_remove(struct device *dev)
405{
406 struct device_driver *drv = dev->driver;
407 struct scsi_device *sdev = to_scsi_device(dev);
408 int err = 0;
409
410 /* reset the prep_fn back to the default since the
411 * driver may have altered it and it's being removed */
412 blk_queue_prep_rq(sdev->request_queue, scsi_prep_fn);
413
414 if (drv && drv->remove)
415 err = drv->remove(dev);
416
417 return 0;
418}
419
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420struct bus_type scsi_bus_type = {
421 .name = "scsi",
422 .match = scsi_bus_match,
Michael Tokarevd7b8bcb2006-10-27 16:02:37 +0400423 .uevent = scsi_bus_uevent,
Jens Axboe9b847542006-01-06 09:28:07 +0100424 .suspend = scsi_bus_suspend,
425 .resume = scsi_bus_resume,
James Bottomley751bf4d2008-01-02 11:14:30 -0600426 .remove = scsi_bus_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427};
428
429int scsi_sysfs_register(void)
430{
431 int error;
432
433 error = bus_register(&scsi_bus_type);
434 if (!error) {
435 error = class_register(&sdev_class);
436 if (error)
437 bus_unregister(&scsi_bus_type);
438 }
439
440 return error;
441}
442
443void scsi_sysfs_unregister(void)
444{
445 class_unregister(&sdev_class);
446 bus_unregister(&scsi_bus_type);
447}
448
449/*
450 * sdev_show_function: macro to create an attr function that can be used to
451 * show a non-bit field.
452 */
453#define sdev_show_function(field, format_string) \
454static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +0100455sdev_show_##field (struct device *dev, struct device_attribute *attr, \
456 char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457{ \
458 struct scsi_device *sdev; \
459 sdev = to_scsi_device(dev); \
460 return snprintf (buf, 20, format_string, sdev->field); \
461} \
462
463/*
464 * sdev_rd_attr: macro to create a function and attribute variable for a
465 * read only field.
466 */
467#define sdev_rd_attr(field, format_string) \
468 sdev_show_function(field, format_string) \
469static DEVICE_ATTR(field, S_IRUGO, sdev_show_##field, NULL);
470
471
472/*
473 * sdev_rd_attr: create a function and attribute variable for a
474 * read/write field.
475 */
476#define sdev_rw_attr(field, format_string) \
477 sdev_show_function(field, format_string) \
478 \
479static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +0100480sdev_store_##field (struct device *dev, struct device_attribute *attr, \
481 const char *buf, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482{ \
483 struct scsi_device *sdev; \
484 sdev = to_scsi_device(dev); \
485 snscanf (buf, 20, format_string, &sdev->field); \
486 return count; \
487} \
488static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, sdev_show_##field, sdev_store_##field);
489
490/* Currently we don't export bit fields, but we might in future,
491 * so leave this code in */
492#if 0
493/*
494 * sdev_rd_attr: create a function and attribute variable for a
495 * read/write bit field.
496 */
497#define sdev_rw_attr_bit(field) \
498 sdev_show_function(field, "%d\n") \
499 \
500static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +0100501sdev_store_##field (struct device *dev, struct device_attribute *attr, \
502 const char *buf, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503{ \
504 int ret; \
505 struct scsi_device *sdev; \
506 ret = scsi_sdev_check_buf_bit(buf); \
507 if (ret >= 0) { \
508 sdev = to_scsi_device(dev); \
509 sdev->field = ret; \
510 ret = count; \
511 } \
512 return ret; \
513} \
514static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, sdev_show_##field, sdev_store_##field);
515
516/*
517 * scsi_sdev_check_buf_bit: return 0 if buf is "0", return 1 if buf is "1",
518 * else return -EINVAL.
519 */
520static int scsi_sdev_check_buf_bit(const char *buf)
521{
522 if ((buf[1] == '\0') || ((buf[1] == '\n') && (buf[2] == '\0'))) {
523 if (buf[0] == '1')
524 return 1;
525 else if (buf[0] == '0')
526 return 0;
527 else
528 return -EINVAL;
529 } else
530 return -EINVAL;
531}
532#endif
533/*
534 * Create the actual show/store functions and data structures.
535 */
536sdev_rd_attr (device_blocked, "%d\n");
537sdev_rd_attr (queue_depth, "%d\n");
538sdev_rd_attr (type, "%d\n");
539sdev_rd_attr (scsi_level, "%d\n");
540sdev_rd_attr (vendor, "%.8s\n");
541sdev_rd_attr (model, "%.16s\n");
542sdev_rd_attr (rev, "%.4s\n");
543
544static ssize_t
Yani Ioannou10523b32005-05-17 06:43:37 -0400545sdev_show_timeout (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546{
547 struct scsi_device *sdev;
548 sdev = to_scsi_device(dev);
549 return snprintf (buf, 20, "%d\n", sdev->timeout / HZ);
550}
551
552static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +0100553sdev_store_timeout (struct device *dev, struct device_attribute *attr,
554 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555{
556 struct scsi_device *sdev;
557 int timeout;
558 sdev = to_scsi_device(dev);
559 sscanf (buf, "%d\n", &timeout);
560 sdev->timeout = timeout * HZ;
561 return count;
562}
563static DEVICE_ATTR(timeout, S_IRUGO | S_IWUSR, sdev_show_timeout, sdev_store_timeout);
564
565static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +0100566store_rescan_field (struct device *dev, struct device_attribute *attr,
567 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568{
569 scsi_rescan_device(dev);
570 return count;
571}
572static DEVICE_ATTR(rescan, S_IWUSR, NULL, store_rescan_field);
573
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400574static void sdev_store_delete_callback(struct device *dev)
575{
576 scsi_remove_device(to_scsi_device(dev));
577}
578
Tony Jonesee959b02008-02-22 00:13:36 +0100579static ssize_t
580sdev_store_delete(struct device *dev, struct device_attribute *attr,
581 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582{
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400583 int rc;
584
585 /* An attribute cannot be unregistered by one of its own methods,
586 * so we have to use this roundabout approach.
587 */
588 rc = device_schedule_callback(dev, sdev_store_delete_callback);
589 if (rc)
590 count = rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 return count;
592};
593static DEVICE_ATTR(delete, S_IWUSR, NULL, sdev_store_delete);
594
595static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +0100596store_state_field(struct device *dev, struct device_attribute *attr,
597 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598{
599 int i;
600 struct scsi_device *sdev = to_scsi_device(dev);
601 enum scsi_device_state state = 0;
602
Tobias Klauser6391a112006-06-08 22:23:48 -0700603 for (i = 0; i < ARRAY_SIZE(sdev_states); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 const int len = strlen(sdev_states[i].name);
605 if (strncmp(sdev_states[i].name, buf, len) == 0 &&
606 buf[len] == '\n') {
607 state = sdev_states[i].value;
608 break;
609 }
610 }
611 if (!state)
612 return -EINVAL;
613
614 if (scsi_device_set_state(sdev, state))
615 return -EINVAL;
616 return count;
617}
618
619static ssize_t
Yani Ioannou10523b32005-05-17 06:43:37 -0400620show_state_field(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621{
622 struct scsi_device *sdev = to_scsi_device(dev);
623 const char *name = scsi_device_state_name(sdev->sdev_state);
624
625 if (!name)
626 return -EINVAL;
627
628 return snprintf(buf, 20, "%s\n", name);
629}
630
631static DEVICE_ATTR(state, S_IRUGO | S_IWUSR, show_state_field, store_state_field);
632
633static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +0100634show_queue_type_field(struct device *dev, struct device_attribute *attr,
635 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636{
637 struct scsi_device *sdev = to_scsi_device(dev);
638 const char *name = "none";
639
640 if (sdev->ordered_tags)
641 name = "ordered";
642 else if (sdev->simple_tags)
643 name = "simple";
644
645 return snprintf(buf, 20, "%s\n", name);
646}
647
648static DEVICE_ATTR(queue_type, S_IRUGO, show_queue_type_field, NULL);
649
650static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +0100651show_iostat_counterbits(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652{
653 return snprintf(buf, 20, "%d\n", (int)sizeof(atomic_t) * 8);
654}
655
656static DEVICE_ATTR(iocounterbits, S_IRUGO, show_iostat_counterbits, NULL);
657
658#define show_sdev_iostat(field) \
659static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +0100660show_iostat_##field(struct device *dev, struct device_attribute *attr, \
661 char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662{ \
663 struct scsi_device *sdev = to_scsi_device(dev); \
664 unsigned long long count = atomic_read(&sdev->field); \
665 return snprintf(buf, 20, "0x%llx\n", count); \
666} \
667static DEVICE_ATTR(field, S_IRUGO, show_iostat_##field, NULL)
668
669show_sdev_iostat(iorequest_cnt);
670show_sdev_iostat(iodone_cnt);
671show_sdev_iostat(ioerr_cnt);
672
Michael Tokarevd7b8bcb2006-10-27 16:02:37 +0400673static ssize_t
674sdev_show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
675{
676 struct scsi_device *sdev;
677 sdev = to_scsi_device(dev);
678 return snprintf (buf, 20, SCSI_DEVICE_MODALIAS_FMT "\n", sdev->type);
679}
680static DEVICE_ATTR(modalias, S_IRUGO, sdev_show_modalias, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
Jeff Garzika341cd02007-10-29 17:15:22 -0400682#define DECLARE_EVT_SHOW(name, Cap_name) \
683static ssize_t \
684sdev_show_evt_##name(struct device *dev, struct device_attribute *attr, \
Tony Jonesee959b02008-02-22 00:13:36 +0100685 char *buf) \
Jeff Garzika341cd02007-10-29 17:15:22 -0400686{ \
687 struct scsi_device *sdev = to_scsi_device(dev); \
688 int val = test_bit(SDEV_EVT_##Cap_name, sdev->supported_events);\
689 return snprintf(buf, 20, "%d\n", val); \
690}
691
692#define DECLARE_EVT_STORE(name, Cap_name) \
693static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +0100694sdev_store_evt_##name(struct device *dev, struct device_attribute *attr,\
Jeff Garzika341cd02007-10-29 17:15:22 -0400695 const char *buf, size_t count) \
696{ \
697 struct scsi_device *sdev = to_scsi_device(dev); \
698 int val = simple_strtoul(buf, NULL, 0); \
699 if (val == 0) \
700 clear_bit(SDEV_EVT_##Cap_name, sdev->supported_events); \
701 else if (val == 1) \
702 set_bit(SDEV_EVT_##Cap_name, sdev->supported_events); \
703 else \
704 return -EINVAL; \
705 return count; \
706}
707
708#define DECLARE_EVT(name, Cap_name) \
709 DECLARE_EVT_SHOW(name, Cap_name) \
710 DECLARE_EVT_STORE(name, Cap_name) \
711 static DEVICE_ATTR(evt_##name, S_IRUGO, sdev_show_evt_##name, \
712 sdev_store_evt_##name);
713#define REF_EVT(name) &dev_attr_evt_##name.attr
714
715DECLARE_EVT(media_change, MEDIA_CHANGE)
716
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717/* Default template for device attributes. May NOT be modified */
Kay Sieversbfd12942007-09-11 17:00:14 +0200718static struct attribute *scsi_sdev_attrs[] = {
719 &dev_attr_device_blocked.attr,
720 &dev_attr_type.attr,
721 &dev_attr_scsi_level.attr,
722 &dev_attr_vendor.attr,
723 &dev_attr_model.attr,
724 &dev_attr_rev.attr,
725 &dev_attr_rescan.attr,
726 &dev_attr_delete.attr,
727 &dev_attr_state.attr,
728 &dev_attr_timeout.attr,
729 &dev_attr_iocounterbits.attr,
730 &dev_attr_iorequest_cnt.attr,
731 &dev_attr_iodone_cnt.attr,
732 &dev_attr_ioerr_cnt.attr,
733 &dev_attr_modalias.attr,
Jeff Garzika341cd02007-10-29 17:15:22 -0400734 REF_EVT(media_change),
Kay Sieversbfd12942007-09-11 17:00:14 +0200735 NULL
736};
737
738static struct attribute_group scsi_sdev_attr_group = {
739 .attrs = scsi_sdev_attrs,
740};
741
742static struct attribute_group *scsi_sdev_attr_groups[] = {
743 &scsi_sdev_attr_group,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 NULL
745};
746
Tony Jonesee959b02008-02-22 00:13:36 +0100747static ssize_t
748sdev_store_queue_depth_rw(struct device *dev, struct device_attribute *attr,
749 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750{
751 int depth, retval;
752 struct scsi_device *sdev = to_scsi_device(dev);
753 struct scsi_host_template *sht = sdev->host->hostt;
754
755 if (!sht->change_queue_depth)
756 return -EINVAL;
757
758 depth = simple_strtoul(buf, NULL, 0);
759
760 if (depth < 1)
761 return -EINVAL;
762
763 retval = sht->change_queue_depth(sdev, depth);
764 if (retval < 0)
765 return retval;
766
767 return count;
768}
769
770static struct device_attribute sdev_attr_queue_depth_rw =
771 __ATTR(queue_depth, S_IRUGO | S_IWUSR, sdev_show_queue_depth,
772 sdev_store_queue_depth_rw);
773
Tony Jonesee959b02008-02-22 00:13:36 +0100774static ssize_t
775sdev_store_queue_type_rw(struct device *dev, struct device_attribute *attr,
776 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777{
778 struct scsi_device *sdev = to_scsi_device(dev);
779 struct scsi_host_template *sht = sdev->host->hostt;
780 int tag_type = 0, retval;
781 int prev_tag_type = scsi_get_tag_type(sdev);
782
783 if (!sdev->tagged_supported || !sht->change_queue_type)
784 return -EINVAL;
785
786 if (strncmp(buf, "ordered", 7) == 0)
787 tag_type = MSG_ORDERED_TAG;
788 else if (strncmp(buf, "simple", 6) == 0)
789 tag_type = MSG_SIMPLE_TAG;
790 else if (strncmp(buf, "none", 4) != 0)
791 return -EINVAL;
792
793 if (tag_type == prev_tag_type)
794 return count;
795
796 retval = sht->change_queue_type(sdev, tag_type);
797 if (retval < 0)
798 return retval;
799
800 return count;
801}
802
803static struct device_attribute sdev_attr_queue_type_rw =
804 __ATTR(queue_type, S_IRUGO | S_IWUSR, show_queue_type_field,
805 sdev_store_queue_type_rw);
806
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807/**
808 * scsi_sysfs_add_sdev - add scsi device to sysfs
809 * @sdev: scsi_device to add
810 *
811 * Return value:
812 * 0 on Success / non-zero on Failure
813 **/
814int scsi_sysfs_add_sdev(struct scsi_device *sdev)
815{
816 int error, i;
James Bottomley80ed71c2007-07-19 10:15:10 -0500817 struct request_queue *rq = sdev->request_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
819 if ((error = scsi_device_set_state(sdev, SDEV_RUNNING)) != 0)
820 return error;
821
822 error = device_add(&sdev->sdev_gendev);
823 if (error) {
824 put_device(sdev->sdev_gendev.parent);
825 printk(KERN_INFO "error 1\n");
826 return error;
827 }
Tony Jonesee959b02008-02-22 00:13:36 +0100828 error = device_add(&sdev->sdev_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 if (error) {
830 printk(KERN_INFO "error 2\n");
831 goto clean_device;
832 }
833
Tony Jonesee959b02008-02-22 00:13:36 +0100834 /* take a reference for the sdev_dev; this is
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 * released by the sdev_class .release */
836 get_device(&sdev->sdev_gendev);
James Bottomley80ed71c2007-07-19 10:15:10 -0500837
Kay Sieversbfd12942007-09-11 17:00:14 +0200838 /* create queue files, which may be writable, depending on the host */
839 if (sdev->host->hostt->change_queue_depth)
840 error = device_create_file(&sdev->sdev_gendev, &sdev_attr_queue_depth_rw);
841 else
842 error = device_create_file(&sdev->sdev_gendev, &dev_attr_queue_depth);
843 if (error) {
844 __scsi_remove_device(sdev);
845 goto out;
846 }
847 if (sdev->host->hostt->change_queue_type)
848 error = device_create_file(&sdev->sdev_gendev, &sdev_attr_queue_type_rw);
849 else
850 error = device_create_file(&sdev->sdev_gendev, &dev_attr_queue_type);
851 if (error) {
852 __scsi_remove_device(sdev);
853 goto out;
854 }
855
James Bottomley39dca552007-07-20 18:22:17 -0500856 error = bsg_register_queue(rq, &sdev->sdev_gendev, NULL);
James Bottomley80ed71c2007-07-19 10:15:10 -0500857
858 if (error)
859 sdev_printk(KERN_INFO, sdev,
860 "Failed to register bsg queue, errno=%d\n", error);
861
862 /* we're treating error on bsg register as non-fatal, so pretend
863 * nothing went wrong */
864 error = 0;
865
Kay Sieversbfd12942007-09-11 17:00:14 +0200866 /* add additional host specific attributes */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 if (sdev->host->hostt->sdev_attrs) {
868 for (i = 0; sdev->host->hostt->sdev_attrs[i]; i++) {
Kay Sieversbfd12942007-09-11 17:00:14 +0200869 error = device_create_file(&sdev->sdev_gendev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 sdev->host->hostt->sdev_attrs[i]);
871 if (error) {
Alan Stern903f4fe2005-07-26 10:20:53 -0400872 __scsi_remove_device(sdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 goto out;
874 }
875 }
876 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
878 transport_add_device(&sdev->sdev_gendev);
879 out:
880 return error;
881
882 clean_device:
883 scsi_device_set_state(sdev, SDEV_CANCEL);
884
885 device_del(&sdev->sdev_gendev);
886 transport_destroy_device(&sdev->sdev_gendev);
887 put_device(&sdev->sdev_gendev);
888
889 return error;
890}
891
Alan Stern903f4fe2005-07-26 10:20:53 -0400892void __scsi_remove_device(struct scsi_device *sdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893{
James Bottomleydf133c22005-11-06 11:47:08 -0600894 struct device *dev = &sdev->sdev_gendev;
895
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 if (scsi_device_set_state(sdev, SDEV_CANCEL) != 0)
Alan Stern903f4fe2005-07-26 10:20:53 -0400897 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898
James Bottomley0feed272008-03-26 09:09:19 -0700899 bsg_unregister_queue(sdev->request_queue);
Tony Jonesee959b02008-02-22 00:13:36 +0100900 device_unregister(&sdev->sdev_dev);
James Bottomleydf133c22005-11-06 11:47:08 -0600901 transport_remove_device(dev);
902 device_del(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 scsi_device_set_state(sdev, SDEV_DEL);
904 if (sdev->host->hostt->slave_destroy)
905 sdev->host->hostt->slave_destroy(sdev);
James Bottomleydf133c22005-11-06 11:47:08 -0600906 transport_destroy_device(dev);
907 put_device(dev);
Alan Stern903f4fe2005-07-26 10:20:53 -0400908}
909
910/**
911 * scsi_remove_device - unregister a device from the scsi bus
912 * @sdev: scsi_device to unregister
913 **/
914void scsi_remove_device(struct scsi_device *sdev)
915{
Alan Stern54195002005-09-15 21:52:51 -0400916 struct Scsi_Host *shost = sdev->host;
917
Arjan van de Ven0b950672006-01-11 13:16:10 +0100918 mutex_lock(&shost->scan_mutex);
Alan Stern903f4fe2005-07-26 10:20:53 -0400919 __scsi_remove_device(sdev);
Arjan van de Ven0b950672006-01-11 13:16:10 +0100920 mutex_unlock(&shost->scan_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921}
922EXPORT_SYMBOL(scsi_remove_device);
923
Adrian Bunk44818ef2007-07-09 11:59:59 -0700924static void __scsi_remove_target(struct scsi_target *starget)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925{
926 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
927 unsigned long flags;
Alan Sterna64358d2005-07-26 10:27:10 -0400928 struct scsi_device *sdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929
930 spin_lock_irqsave(shost->host_lock, flags);
931 starget->reap_ref++;
Alan Sterna64358d2005-07-26 10:27:10 -0400932 restart:
933 list_for_each_entry(sdev, &shost->__devices, siblings) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 if (sdev->channel != starget->channel ||
Alan Sterna64358d2005-07-26 10:27:10 -0400935 sdev->id != starget->id ||
936 sdev->sdev_state == SDEV_DEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 continue;
938 spin_unlock_irqrestore(shost->host_lock, flags);
939 scsi_remove_device(sdev);
940 spin_lock_irqsave(shost->host_lock, flags);
Alan Sterna64358d2005-07-26 10:27:10 -0400941 goto restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 }
943 spin_unlock_irqrestore(shost->host_lock, flags);
944 scsi_target_reap(starget);
945}
946
mochel@digitalimplant.org20b1e672005-03-24 19:03:59 -0800947static int __remove_child (struct device * dev, void * data)
948{
949 if (scsi_is_target_device(dev))
950 __scsi_remove_target(to_scsi_target(dev));
951 return 0;
952}
953
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954/**
955 * scsi_remove_target - try to remove a target and all its devices
956 * @dev: generic starget or parent of generic stargets to be removed
957 *
958 * Note: This is slightly racy. It is possible that if the user
959 * requests the addition of another device then the target won't be
960 * removed.
961 */
962void scsi_remove_target(struct device *dev)
963{
mochel@digitalimplant.org20b1e672005-03-24 19:03:59 -0800964 struct device *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965
966 if (scsi_is_target_device(dev)) {
967 __scsi_remove_target(to_scsi_target(dev));
968 return;
969 }
970
971 rdev = get_device(dev);
mochel@digitalimplant.org20b1e672005-03-24 19:03:59 -0800972 device_for_each_child(dev, NULL, __remove_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 put_device(rdev);
974}
975EXPORT_SYMBOL(scsi_remove_target);
976
977int scsi_register_driver(struct device_driver *drv)
978{
979 drv->bus = &scsi_bus_type;
980
981 return driver_register(drv);
982}
983EXPORT_SYMBOL(scsi_register_driver);
984
985int scsi_register_interface(struct class_interface *intf)
986{
987 intf->class = &sdev_class;
988
989 return class_interface_register(intf);
990}
991EXPORT_SYMBOL(scsi_register_interface);
992
993
Tony Jonesee959b02008-02-22 00:13:36 +0100994static struct device_attribute *class_attr_overridden(
995 struct device_attribute **attrs,
996 struct device_attribute *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997{
998 int i;
999
1000 if (!attrs)
1001 return NULL;
1002 for (i = 0; attrs[i]; i++)
1003 if (!strcmp(attrs[i]->attr.name, attr->attr.name))
1004 return attrs[i];
1005 return NULL;
1006}
1007
Tony Jonesee959b02008-02-22 00:13:36 +01001008static int class_attr_add(struct device *classdev,
1009 struct device_attribute *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010{
Tony Jonesee959b02008-02-22 00:13:36 +01001011 struct device_attribute *base_attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012
1013 /*
1014 * Spare the caller from having to copy things it's not interested in.
1015 */
1016 base_attr = class_attr_overridden(scsi_sysfs_shost_attrs, attr);
1017 if (base_attr) {
1018 /* extend permissions */
1019 attr->attr.mode |= base_attr->attr.mode;
1020
1021 /* override null show/store with default */
1022 if (!attr->show)
1023 attr->show = base_attr->show;
1024 if (!attr->store)
1025 attr->store = base_attr->store;
1026 }
1027
Tony Jonesee959b02008-02-22 00:13:36 +01001028 return device_create_file(classdev, attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029}
1030
1031/**
1032 * scsi_sysfs_add_host - add scsi host to subsystem
1033 * @shost: scsi host struct to add to subsystem
1034 * @dev: parent struct device pointer
1035 **/
1036int scsi_sysfs_add_host(struct Scsi_Host *shost)
1037{
1038 int error, i;
1039
1040 if (shost->hostt->shost_attrs) {
1041 for (i = 0; shost->hostt->shost_attrs[i]; i++) {
Tony Jonesee959b02008-02-22 00:13:36 +01001042 error = class_attr_add(&shost->shost_dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 shost->hostt->shost_attrs[i]);
1044 if (error)
1045 return error;
1046 }
1047 }
1048
1049 for (i = 0; scsi_sysfs_shost_attrs[i]; i++) {
1050 if (!class_attr_overridden(shost->hostt->shost_attrs,
1051 scsi_sysfs_shost_attrs[i])) {
Tony Jonesee959b02008-02-22 00:13:36 +01001052 error = device_create_file(&shost->shost_dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 scsi_sysfs_shost_attrs[i]);
1054 if (error)
1055 return error;
1056 }
1057 }
1058
1059 transport_register_device(&shost->shost_gendev);
James Bottomleyd52b3812008-01-05 09:38:30 -06001060 transport_configure_device(&shost->shost_gendev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 return 0;
1062}
1063
Kay Sieversbfd12942007-09-11 17:00:14 +02001064static struct device_type scsi_dev_type = {
1065 .name = "scsi_device",
1066 .release = scsi_device_dev_release,
1067 .groups = scsi_sdev_attr_groups,
1068};
1069
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070void scsi_sysfs_device_initialize(struct scsi_device *sdev)
1071{
1072 unsigned long flags;
1073 struct Scsi_Host *shost = sdev->host;
1074 struct scsi_target *starget = sdev->sdev_target;
1075
1076 device_initialize(&sdev->sdev_gendev);
1077 sdev->sdev_gendev.bus = &scsi_bus_type;
Kay Sieversbfd12942007-09-11 17:00:14 +02001078 sdev->sdev_gendev.type = &scsi_dev_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 sprintf(sdev->sdev_gendev.bus_id,"%d:%d:%d:%d",
1080 sdev->host->host_no, sdev->channel, sdev->id,
1081 sdev->lun);
1082
Tony Jonesee959b02008-02-22 00:13:36 +01001083 device_initialize(&sdev->sdev_dev);
1084 sdev->sdev_dev.parent = &sdev->sdev_gendev;
1085 sdev->sdev_dev.class = &sdev_class;
1086 snprintf(sdev->sdev_dev.bus_id, BUS_ID_SIZE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 "%d:%d:%d:%d", sdev->host->host_no,
1088 sdev->channel, sdev->id, sdev->lun);
Alan Stern7c9d6f12007-01-08 11:12:32 -05001089 sdev->scsi_level = starget->scsi_level;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 transport_setup_device(&sdev->sdev_gendev);
1091 spin_lock_irqsave(shost->host_lock, flags);
1092 list_add_tail(&sdev->same_target_siblings, &starget->devices);
1093 list_add_tail(&sdev->siblings, &shost->__devices);
1094 spin_unlock_irqrestore(shost->host_lock, flags);
1095}
1096
1097int scsi_is_sdev_device(const struct device *dev)
1098{
Kay Sievers13ba9bc2007-09-26 19:54:49 +02001099 return dev->type == &scsi_dev_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100}
1101EXPORT_SYMBOL(scsi_is_sdev_device);
1102
1103/* A blank transport template that is used in drivers that don't
1104 * yet implement Transport Attributes */
1105struct scsi_transport_template blank_transport_template = { { { {NULL, }, }, }, };