blob: ab3c71869be55d9deeb8ca101c59faf80e140561 [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");
Martin K. Petersen4469f982008-07-17 04:28:30 -0400252shost_rd_attr(prot_capabilities, "%u\n");
253shost_rd_attr(prot_guard_type, "%hd\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254shost_rd_attr2(proc_name, hostt->proc_name, "%s\n");
255
Hannes Reineckef7120a42008-03-18 14:32:28 +0100256static struct attribute *scsi_sysfs_shost_attrs[] = {
257 &dev_attr_unique_id.attr,
258 &dev_attr_host_busy.attr,
259 &dev_attr_cmd_per_lun.attr,
260 &dev_attr_can_queue.attr,
261 &dev_attr_sg_tablesize.attr,
262 &dev_attr_unchecked_isa_dma.attr,
263 &dev_attr_proc_name.attr,
264 &dev_attr_scan.attr,
265 &dev_attr_hstate.attr,
266 &dev_attr_supported_mode.attr,
267 &dev_attr_active_mode.attr,
Martin K. Petersen4469f982008-07-17 04:28:30 -0400268 &dev_attr_prot_capabilities.attr,
269 &dev_attr_prot_guard_type.attr,
Hannes Reineckef7120a42008-03-18 14:32:28 +0100270 NULL
271};
272
273struct attribute_group scsi_shost_attr_group = {
274 .attrs = scsi_sysfs_shost_attrs,
275};
276
277struct attribute_group *scsi_sysfs_shost_attr_groups[] = {
278 &scsi_shost_attr_group,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 NULL
280};
281
Tony Jonesee959b02008-02-22 00:13:36 +0100282static void scsi_device_cls_release(struct device *class_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283{
284 struct scsi_device *sdev;
285
286 sdev = class_to_sdev(class_dev);
287 put_device(&sdev->sdev_gendev);
288}
289
David Howells65f27f32006-11-22 14:55:48 +0000290static void scsi_device_dev_release_usercontext(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291{
292 struct scsi_device *sdev;
293 struct device *parent;
294 struct scsi_target *starget;
Jeff Garzika341cd02007-10-29 17:15:22 -0400295 struct list_head *this, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 unsigned long flags;
297
David Howells65f27f32006-11-22 14:55:48 +0000298 sdev = container_of(work, struct scsi_device, ew.work);
299
300 parent = sdev->sdev_gendev.parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 starget = to_scsi_target(parent);
302
303 spin_lock_irqsave(sdev->host->host_lock, flags);
304 starget->reap_ref++;
305 list_del(&sdev->siblings);
306 list_del(&sdev->same_target_siblings);
307 list_del(&sdev->starved_entry);
308 spin_unlock_irqrestore(sdev->host->host_lock, flags);
309
Jeff Garzika341cd02007-10-29 17:15:22 -0400310 cancel_work_sync(&sdev->event_work);
311
312 list_for_each_safe(this, tmp, &sdev->event_list) {
313 struct scsi_event *evt;
314
315 evt = list_entry(this, struct scsi_event, node);
316 list_del(&evt->node);
317 kfree(evt);
318 }
319
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 if (sdev->request_queue) {
321 sdev->request_queue->queuedata = NULL;
James Bottomley65110b22006-02-14 10:48:46 -0600322 /* user context needed to free queue */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 scsi_free_queue(sdev->request_queue);
c2a93312005-04-12 16:38:09 -0500324 /* temporary expedient, try to catch use of queue lock
325 * after free of sdev */
326 sdev->request_queue = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 }
328
329 scsi_target_reap(scsi_target(sdev));
330
331 kfree(sdev->inquiry);
332 kfree(sdev);
333
334 if (parent)
335 put_device(parent);
336}
337
James Bottomley65110b22006-02-14 10:48:46 -0600338static void scsi_device_dev_release(struct device *dev)
339{
James Bottomleyffedb452006-02-23 14:27:18 -0600340 struct scsi_device *sdp = to_scsi_device(dev);
David Howells65f27f32006-11-22 14:55:48 +0000341 execute_in_process_context(scsi_device_dev_release_usercontext,
James Bottomleyffedb452006-02-23 14:27:18 -0600342 &sdp->ew);
James Bottomley65110b22006-02-14 10:48:46 -0600343}
344
Adrian Bunk52c1da32005-06-23 22:05:33 -0700345static struct class sdev_class = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 .name = "scsi_device",
Tony Jonesee959b02008-02-22 00:13:36 +0100347 .dev_release = scsi_device_cls_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348};
349
350/* all probing is done in the individual ->probe routines */
351static int scsi_bus_match(struct device *dev, struct device_driver *gendrv)
352{
Hannes Reineckeb0ed4332008-03-18 14:32:28 +0100353 struct scsi_device *sdp;
354
355 if (dev->type != &scsi_dev_type)
356 return 0;
357
358 sdp = to_scsi_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 if (sdp->no_uld_attach)
360 return 0;
361 return (sdp->inq_periph_qual == SCSI_INQ_PQ_CON)? 1: 0;
362}
363
Kay Sievers7eff2e72007-08-14 15:15:12 +0200364static int scsi_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
Michael Tokarevd7b8bcb2006-10-27 16:02:37 +0400365{
James Bottomley1f42ea72008-05-22 12:34:41 +0100366 struct scsi_device *sdev;
367
368 if (dev->type != &scsi_dev_type)
369 return 0;
370
371 sdev = to_scsi_device(dev);
Michael Tokarevd7b8bcb2006-10-27 16:02:37 +0400372
Kay Sievers7eff2e72007-08-14 15:15:12 +0200373 add_uevent_var(env, "MODALIAS=" SCSI_DEVICE_MODALIAS_FMT, sdev->type);
Michael Tokarevd7b8bcb2006-10-27 16:02:37 +0400374 return 0;
375}
376
Jens Axboe9b847542006-01-06 09:28:07 +0100377static int scsi_bus_suspend(struct device * dev, pm_message_t state)
378{
Hannes Reineckeb0ed4332008-03-18 14:32:28 +0100379 struct device_driver *drv;
380 struct scsi_device *sdev;
Jens Axboe9b847542006-01-06 09:28:07 +0100381 int err;
382
Hannes Reineckeb0ed4332008-03-18 14:32:28 +0100383 if (dev->type != &scsi_dev_type)
384 return 0;
385
386 drv = dev->driver;
387 sdev = to_scsi_device(dev);
388
Jens Axboe9b847542006-01-06 09:28:07 +0100389 err = scsi_device_quiesce(sdev);
390 if (err)
391 return err;
392
Tejun Heoc3c94c52007-03-21 00:13:59 +0900393 if (drv && drv->suspend) {
394 err = drv->suspend(dev, state);
395 if (err)
396 return err;
397 }
Jens Axboe9b847542006-01-06 09:28:07 +0100398
Tejun Heoc3c94c52007-03-21 00:13:59 +0900399 return 0;
Jens Axboe9b847542006-01-06 09:28:07 +0100400}
401
402static int scsi_bus_resume(struct device * dev)
403{
Hannes Reineckeb0ed4332008-03-18 14:32:28 +0100404 struct device_driver *drv;
405 struct scsi_device *sdev;
Tejun Heo1dfcda02007-03-21 16:05:16 +0900406 int err = 0;
Jens Axboe9b847542006-01-06 09:28:07 +0100407
Hannes Reineckeb0ed4332008-03-18 14:32:28 +0100408 if (dev->type != &scsi_dev_type)
409 return 0;
410
411 drv = dev->driver;
412 sdev = to_scsi_device(dev);
413
Tejun Heoc3c94c52007-03-21 00:13:59 +0900414 if (drv && drv->resume)
Tejun Heo1dfcda02007-03-21 16:05:16 +0900415 err = drv->resume(dev);
Tejun Heoc3c94c52007-03-21 00:13:59 +0900416
Jens Axboe9b847542006-01-06 09:28:07 +0100417 scsi_device_resume(sdev);
Tejun Heoc3c94c52007-03-21 00:13:59 +0900418
Tejun Heo1dfcda02007-03-21 16:05:16 +0900419 return err;
Jens Axboe9b847542006-01-06 09:28:07 +0100420}
421
James Bottomley751bf4d2008-01-02 11:14:30 -0600422static int scsi_bus_remove(struct device *dev)
423{
424 struct device_driver *drv = dev->driver;
425 struct scsi_device *sdev = to_scsi_device(dev);
426 int err = 0;
427
428 /* reset the prep_fn back to the default since the
429 * driver may have altered it and it's being removed */
430 blk_queue_prep_rq(sdev->request_queue, scsi_prep_fn);
431
432 if (drv && drv->remove)
433 err = drv->remove(dev);
434
435 return 0;
436}
437
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438struct bus_type scsi_bus_type = {
439 .name = "scsi",
440 .match = scsi_bus_match,
Michael Tokarevd7b8bcb2006-10-27 16:02:37 +0400441 .uevent = scsi_bus_uevent,
Jens Axboe9b847542006-01-06 09:28:07 +0100442 .suspend = scsi_bus_suspend,
443 .resume = scsi_bus_resume,
James Bottomley751bf4d2008-01-02 11:14:30 -0600444 .remove = scsi_bus_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445};
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -0700446EXPORT_SYMBOL_GPL(scsi_bus_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
448int scsi_sysfs_register(void)
449{
450 int error;
451
452 error = bus_register(&scsi_bus_type);
453 if (!error) {
454 error = class_register(&sdev_class);
455 if (error)
456 bus_unregister(&scsi_bus_type);
457 }
458
459 return error;
460}
461
462void scsi_sysfs_unregister(void)
463{
464 class_unregister(&sdev_class);
465 bus_unregister(&scsi_bus_type);
466}
467
468/*
469 * sdev_show_function: macro to create an attr function that can be used to
470 * show a non-bit field.
471 */
472#define sdev_show_function(field, format_string) \
473static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +0100474sdev_show_##field (struct device *dev, struct device_attribute *attr, \
475 char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476{ \
477 struct scsi_device *sdev; \
478 sdev = to_scsi_device(dev); \
479 return snprintf (buf, 20, format_string, sdev->field); \
480} \
481
482/*
483 * sdev_rd_attr: macro to create a function and attribute variable for a
484 * read only field.
485 */
486#define sdev_rd_attr(field, format_string) \
487 sdev_show_function(field, format_string) \
488static DEVICE_ATTR(field, S_IRUGO, sdev_show_##field, NULL);
489
490
491/*
492 * sdev_rd_attr: create a function and attribute variable for a
493 * read/write field.
494 */
495#define sdev_rw_attr(field, format_string) \
496 sdev_show_function(field, format_string) \
497 \
498static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +0100499sdev_store_##field (struct device *dev, struct device_attribute *attr, \
500 const char *buf, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501{ \
502 struct scsi_device *sdev; \
503 sdev = to_scsi_device(dev); \
504 snscanf (buf, 20, format_string, &sdev->field); \
505 return count; \
506} \
507static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, sdev_show_##field, sdev_store_##field);
508
509/* Currently we don't export bit fields, but we might in future,
510 * so leave this code in */
511#if 0
512/*
513 * sdev_rd_attr: create a function and attribute variable for a
514 * read/write bit field.
515 */
516#define sdev_rw_attr_bit(field) \
517 sdev_show_function(field, "%d\n") \
518 \
519static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +0100520sdev_store_##field (struct device *dev, struct device_attribute *attr, \
521 const char *buf, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522{ \
523 int ret; \
524 struct scsi_device *sdev; \
525 ret = scsi_sdev_check_buf_bit(buf); \
526 if (ret >= 0) { \
527 sdev = to_scsi_device(dev); \
528 sdev->field = ret; \
529 ret = count; \
530 } \
531 return ret; \
532} \
533static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, sdev_show_##field, sdev_store_##field);
534
535/*
536 * scsi_sdev_check_buf_bit: return 0 if buf is "0", return 1 if buf is "1",
537 * else return -EINVAL.
538 */
539static int scsi_sdev_check_buf_bit(const char *buf)
540{
541 if ((buf[1] == '\0') || ((buf[1] == '\n') && (buf[2] == '\0'))) {
542 if (buf[0] == '1')
543 return 1;
544 else if (buf[0] == '0')
545 return 0;
546 else
547 return -EINVAL;
548 } else
549 return -EINVAL;
550}
551#endif
552/*
553 * Create the actual show/store functions and data structures.
554 */
555sdev_rd_attr (device_blocked, "%d\n");
556sdev_rd_attr (queue_depth, "%d\n");
557sdev_rd_attr (type, "%d\n");
558sdev_rd_attr (scsi_level, "%d\n");
559sdev_rd_attr (vendor, "%.8s\n");
560sdev_rd_attr (model, "%.16s\n");
561sdev_rd_attr (rev, "%.4s\n");
562
563static ssize_t
Yani Ioannou10523b32005-05-17 06:43:37 -0400564sdev_show_timeout (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565{
566 struct scsi_device *sdev;
567 sdev = to_scsi_device(dev);
568 return snprintf (buf, 20, "%d\n", sdev->timeout / HZ);
569}
570
571static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +0100572sdev_store_timeout (struct device *dev, struct device_attribute *attr,
573 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574{
575 struct scsi_device *sdev;
576 int timeout;
577 sdev = to_scsi_device(dev);
578 sscanf (buf, "%d\n", &timeout);
579 sdev->timeout = timeout * HZ;
580 return count;
581}
582static DEVICE_ATTR(timeout, S_IRUGO | S_IWUSR, sdev_show_timeout, sdev_store_timeout);
583
584static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +0100585store_rescan_field (struct device *dev, struct device_attribute *attr,
586 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587{
588 scsi_rescan_device(dev);
589 return count;
590}
591static DEVICE_ATTR(rescan, S_IWUSR, NULL, store_rescan_field);
592
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400593static void sdev_store_delete_callback(struct device *dev)
594{
595 scsi_remove_device(to_scsi_device(dev));
596}
597
Tony Jonesee959b02008-02-22 00:13:36 +0100598static ssize_t
599sdev_store_delete(struct device *dev, struct device_attribute *attr,
600 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601{
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400602 int rc;
603
604 /* An attribute cannot be unregistered by one of its own methods,
605 * so we have to use this roundabout approach.
606 */
607 rc = device_schedule_callback(dev, sdev_store_delete_callback);
608 if (rc)
609 count = rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 return count;
611};
612static DEVICE_ATTR(delete, S_IWUSR, NULL, sdev_store_delete);
613
614static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +0100615store_state_field(struct device *dev, struct device_attribute *attr,
616 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617{
618 int i;
619 struct scsi_device *sdev = to_scsi_device(dev);
620 enum scsi_device_state state = 0;
621
Tobias Klauser6391a112006-06-08 22:23:48 -0700622 for (i = 0; i < ARRAY_SIZE(sdev_states); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 const int len = strlen(sdev_states[i].name);
624 if (strncmp(sdev_states[i].name, buf, len) == 0 &&
625 buf[len] == '\n') {
626 state = sdev_states[i].value;
627 break;
628 }
629 }
630 if (!state)
631 return -EINVAL;
632
633 if (scsi_device_set_state(sdev, state))
634 return -EINVAL;
635 return count;
636}
637
638static ssize_t
Yani Ioannou10523b32005-05-17 06:43:37 -0400639show_state_field(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640{
641 struct scsi_device *sdev = to_scsi_device(dev);
642 const char *name = scsi_device_state_name(sdev->sdev_state);
643
644 if (!name)
645 return -EINVAL;
646
647 return snprintf(buf, 20, "%s\n", name);
648}
649
650static DEVICE_ATTR(state, S_IRUGO | S_IWUSR, show_state_field, store_state_field);
651
652static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +0100653show_queue_type_field(struct device *dev, struct device_attribute *attr,
654 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655{
656 struct scsi_device *sdev = to_scsi_device(dev);
657 const char *name = "none";
658
659 if (sdev->ordered_tags)
660 name = "ordered";
661 else if (sdev->simple_tags)
662 name = "simple";
663
664 return snprintf(buf, 20, "%s\n", name);
665}
666
667static DEVICE_ATTR(queue_type, S_IRUGO, show_queue_type_field, NULL);
668
669static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +0100670show_iostat_counterbits(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671{
672 return snprintf(buf, 20, "%d\n", (int)sizeof(atomic_t) * 8);
673}
674
675static DEVICE_ATTR(iocounterbits, S_IRUGO, show_iostat_counterbits, NULL);
676
677#define show_sdev_iostat(field) \
678static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +0100679show_iostat_##field(struct device *dev, struct device_attribute *attr, \
680 char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681{ \
682 struct scsi_device *sdev = to_scsi_device(dev); \
683 unsigned long long count = atomic_read(&sdev->field); \
684 return snprintf(buf, 20, "0x%llx\n", count); \
685} \
686static DEVICE_ATTR(field, S_IRUGO, show_iostat_##field, NULL)
687
688show_sdev_iostat(iorequest_cnt);
689show_sdev_iostat(iodone_cnt);
690show_sdev_iostat(ioerr_cnt);
691
Michael Tokarevd7b8bcb2006-10-27 16:02:37 +0400692static ssize_t
693sdev_show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
694{
695 struct scsi_device *sdev;
696 sdev = to_scsi_device(dev);
697 return snprintf (buf, 20, SCSI_DEVICE_MODALIAS_FMT "\n", sdev->type);
698}
699static DEVICE_ATTR(modalias, S_IRUGO, sdev_show_modalias, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
Jeff Garzika341cd02007-10-29 17:15:22 -0400701#define DECLARE_EVT_SHOW(name, Cap_name) \
702static ssize_t \
703sdev_show_evt_##name(struct device *dev, struct device_attribute *attr, \
Tony Jonesee959b02008-02-22 00:13:36 +0100704 char *buf) \
Jeff Garzika341cd02007-10-29 17:15:22 -0400705{ \
706 struct scsi_device *sdev = to_scsi_device(dev); \
707 int val = test_bit(SDEV_EVT_##Cap_name, sdev->supported_events);\
708 return snprintf(buf, 20, "%d\n", val); \
709}
710
711#define DECLARE_EVT_STORE(name, Cap_name) \
712static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +0100713sdev_store_evt_##name(struct device *dev, struct device_attribute *attr,\
Jeff Garzika341cd02007-10-29 17:15:22 -0400714 const char *buf, size_t count) \
715{ \
716 struct scsi_device *sdev = to_scsi_device(dev); \
717 int val = simple_strtoul(buf, NULL, 0); \
718 if (val == 0) \
719 clear_bit(SDEV_EVT_##Cap_name, sdev->supported_events); \
720 else if (val == 1) \
721 set_bit(SDEV_EVT_##Cap_name, sdev->supported_events); \
722 else \
723 return -EINVAL; \
724 return count; \
725}
726
727#define DECLARE_EVT(name, Cap_name) \
728 DECLARE_EVT_SHOW(name, Cap_name) \
729 DECLARE_EVT_STORE(name, Cap_name) \
730 static DEVICE_ATTR(evt_##name, S_IRUGO, sdev_show_evt_##name, \
731 sdev_store_evt_##name);
732#define REF_EVT(name) &dev_attr_evt_##name.attr
733
734DECLARE_EVT(media_change, MEDIA_CHANGE)
735
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736/* Default template for device attributes. May NOT be modified */
Kay Sieversbfd12942007-09-11 17:00:14 +0200737static struct attribute *scsi_sdev_attrs[] = {
738 &dev_attr_device_blocked.attr,
739 &dev_attr_type.attr,
740 &dev_attr_scsi_level.attr,
741 &dev_attr_vendor.attr,
742 &dev_attr_model.attr,
743 &dev_attr_rev.attr,
744 &dev_attr_rescan.attr,
745 &dev_attr_delete.attr,
746 &dev_attr_state.attr,
747 &dev_attr_timeout.attr,
748 &dev_attr_iocounterbits.attr,
749 &dev_attr_iorequest_cnt.attr,
750 &dev_attr_iodone_cnt.attr,
751 &dev_attr_ioerr_cnt.attr,
752 &dev_attr_modalias.attr,
Jeff Garzika341cd02007-10-29 17:15:22 -0400753 REF_EVT(media_change),
Kay Sieversbfd12942007-09-11 17:00:14 +0200754 NULL
755};
756
757static struct attribute_group scsi_sdev_attr_group = {
758 .attrs = scsi_sdev_attrs,
759};
760
761static struct attribute_group *scsi_sdev_attr_groups[] = {
762 &scsi_sdev_attr_group,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 NULL
764};
765
Tony Jonesee959b02008-02-22 00:13:36 +0100766static ssize_t
767sdev_store_queue_depth_rw(struct device *dev, struct device_attribute *attr,
768 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769{
770 int depth, retval;
771 struct scsi_device *sdev = to_scsi_device(dev);
772 struct scsi_host_template *sht = sdev->host->hostt;
773
774 if (!sht->change_queue_depth)
775 return -EINVAL;
776
777 depth = simple_strtoul(buf, NULL, 0);
778
779 if (depth < 1)
780 return -EINVAL;
781
782 retval = sht->change_queue_depth(sdev, depth);
783 if (retval < 0)
784 return retval;
785
786 return count;
787}
788
789static struct device_attribute sdev_attr_queue_depth_rw =
790 __ATTR(queue_depth, S_IRUGO | S_IWUSR, sdev_show_queue_depth,
791 sdev_store_queue_depth_rw);
792
Tony Jonesee959b02008-02-22 00:13:36 +0100793static ssize_t
794sdev_store_queue_type_rw(struct device *dev, struct device_attribute *attr,
795 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796{
797 struct scsi_device *sdev = to_scsi_device(dev);
798 struct scsi_host_template *sht = sdev->host->hostt;
799 int tag_type = 0, retval;
800 int prev_tag_type = scsi_get_tag_type(sdev);
801
802 if (!sdev->tagged_supported || !sht->change_queue_type)
803 return -EINVAL;
804
805 if (strncmp(buf, "ordered", 7) == 0)
806 tag_type = MSG_ORDERED_TAG;
807 else if (strncmp(buf, "simple", 6) == 0)
808 tag_type = MSG_SIMPLE_TAG;
809 else if (strncmp(buf, "none", 4) != 0)
810 return -EINVAL;
811
812 if (tag_type == prev_tag_type)
813 return count;
814
815 retval = sht->change_queue_type(sdev, tag_type);
816 if (retval < 0)
817 return retval;
818
819 return count;
820}
821
James Bottomley643eb2d2008-03-22 22:42:27 -0500822static int scsi_target_add(struct scsi_target *starget)
823{
824 int error;
825
826 if (starget->state != STARGET_CREATED)
827 return 0;
828
829 error = device_add(&starget->dev);
830 if (error) {
831 dev_err(&starget->dev, "target device_add failed, error %d\n", error);
832 get_device(&starget->dev);
833 scsi_target_reap(starget);
834 put_device(&starget->dev);
835 return error;
836 }
837 transport_add_device(&starget->dev);
838 starget->state = STARGET_RUNNING;
839
840 return 0;
841}
842
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843static struct device_attribute sdev_attr_queue_type_rw =
844 __ATTR(queue_type, S_IRUGO | S_IWUSR, show_queue_type_field,
845 sdev_store_queue_type_rw);
846
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847/**
848 * scsi_sysfs_add_sdev - add scsi device to sysfs
849 * @sdev: scsi_device to add
850 *
851 * Return value:
852 * 0 on Success / non-zero on Failure
853 **/
854int scsi_sysfs_add_sdev(struct scsi_device *sdev)
855{
856 int error, i;
James Bottomley80ed71c2007-07-19 10:15:10 -0500857 struct request_queue *rq = sdev->request_queue;
James Bottomley643eb2d2008-03-22 22:42:27 -0500858 struct scsi_target *starget = sdev->sdev_target;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859
860 if ((error = scsi_device_set_state(sdev, SDEV_RUNNING)) != 0)
861 return error;
862
James Bottomley643eb2d2008-03-22 22:42:27 -0500863 error = scsi_target_add(starget);
864 if (error)
865 return error;
866
867 transport_configure_device(&starget->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 error = device_add(&sdev->sdev_gendev);
869 if (error) {
870 put_device(sdev->sdev_gendev.parent);
871 printk(KERN_INFO "error 1\n");
872 return error;
873 }
Tony Jonesee959b02008-02-22 00:13:36 +0100874 error = device_add(&sdev->sdev_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 if (error) {
876 printk(KERN_INFO "error 2\n");
877 goto clean_device;
878 }
879
Tony Jonesee959b02008-02-22 00:13:36 +0100880 /* take a reference for the sdev_dev; this is
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 * released by the sdev_class .release */
882 get_device(&sdev->sdev_gendev);
James Bottomley80ed71c2007-07-19 10:15:10 -0500883
Kay Sieversbfd12942007-09-11 17:00:14 +0200884 /* create queue files, which may be writable, depending on the host */
885 if (sdev->host->hostt->change_queue_depth)
886 error = device_create_file(&sdev->sdev_gendev, &sdev_attr_queue_depth_rw);
887 else
888 error = device_create_file(&sdev->sdev_gendev, &dev_attr_queue_depth);
889 if (error) {
890 __scsi_remove_device(sdev);
891 goto out;
892 }
893 if (sdev->host->hostt->change_queue_type)
894 error = device_create_file(&sdev->sdev_gendev, &sdev_attr_queue_type_rw);
895 else
896 error = device_create_file(&sdev->sdev_gendev, &dev_attr_queue_type);
897 if (error) {
898 __scsi_remove_device(sdev);
899 goto out;
900 }
901
FUJITA Tomonori97f46ae2008-04-19 00:43:14 +0900902 error = bsg_register_queue(rq, &sdev->sdev_gendev, NULL, NULL);
James Bottomley80ed71c2007-07-19 10:15:10 -0500903
904 if (error)
905 sdev_printk(KERN_INFO, sdev,
906 "Failed to register bsg queue, errno=%d\n", error);
907
908 /* we're treating error on bsg register as non-fatal, so pretend
909 * nothing went wrong */
910 error = 0;
911
Kay Sieversbfd12942007-09-11 17:00:14 +0200912 /* add additional host specific attributes */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 if (sdev->host->hostt->sdev_attrs) {
914 for (i = 0; sdev->host->hostt->sdev_attrs[i]; i++) {
Kay Sieversbfd12942007-09-11 17:00:14 +0200915 error = device_create_file(&sdev->sdev_gendev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 sdev->host->hostt->sdev_attrs[i]);
917 if (error) {
Alan Stern903f4fe2005-07-26 10:20:53 -0400918 __scsi_remove_device(sdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 goto out;
920 }
921 }
922 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923
924 transport_add_device(&sdev->sdev_gendev);
925 out:
926 return error;
927
928 clean_device:
929 scsi_device_set_state(sdev, SDEV_CANCEL);
930
931 device_del(&sdev->sdev_gendev);
932 transport_destroy_device(&sdev->sdev_gendev);
933 put_device(&sdev->sdev_gendev);
934
935 return error;
936}
937
Alan Stern903f4fe2005-07-26 10:20:53 -0400938void __scsi_remove_device(struct scsi_device *sdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939{
James Bottomleydf133c22005-11-06 11:47:08 -0600940 struct device *dev = &sdev->sdev_gendev;
941
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 if (scsi_device_set_state(sdev, SDEV_CANCEL) != 0)
Alan Stern903f4fe2005-07-26 10:20:53 -0400943 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
James Bottomley0feed272008-03-26 09:09:19 -0700945 bsg_unregister_queue(sdev->request_queue);
Tony Jonesee959b02008-02-22 00:13:36 +0100946 device_unregister(&sdev->sdev_dev);
James Bottomleydf133c22005-11-06 11:47:08 -0600947 transport_remove_device(dev);
948 device_del(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 scsi_device_set_state(sdev, SDEV_DEL);
950 if (sdev->host->hostt->slave_destroy)
951 sdev->host->hostt->slave_destroy(sdev);
James Bottomleydf133c22005-11-06 11:47:08 -0600952 transport_destroy_device(dev);
953 put_device(dev);
Alan Stern903f4fe2005-07-26 10:20:53 -0400954}
955
956/**
957 * scsi_remove_device - unregister a device from the scsi bus
958 * @sdev: scsi_device to unregister
959 **/
960void scsi_remove_device(struct scsi_device *sdev)
961{
Alan Stern54195002005-09-15 21:52:51 -0400962 struct Scsi_Host *shost = sdev->host;
963
Arjan van de Ven0b950672006-01-11 13:16:10 +0100964 mutex_lock(&shost->scan_mutex);
Alan Stern903f4fe2005-07-26 10:20:53 -0400965 __scsi_remove_device(sdev);
Arjan van de Ven0b950672006-01-11 13:16:10 +0100966 mutex_unlock(&shost->scan_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967}
968EXPORT_SYMBOL(scsi_remove_device);
969
Adrian Bunk44818ef2007-07-09 11:59:59 -0700970static void __scsi_remove_target(struct scsi_target *starget)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971{
972 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
973 unsigned long flags;
Alan Sterna64358d2005-07-26 10:27:10 -0400974 struct scsi_device *sdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975
976 spin_lock_irqsave(shost->host_lock, flags);
977 starget->reap_ref++;
Alan Sterna64358d2005-07-26 10:27:10 -0400978 restart:
979 list_for_each_entry(sdev, &shost->__devices, siblings) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 if (sdev->channel != starget->channel ||
Alan Sterna64358d2005-07-26 10:27:10 -0400981 sdev->id != starget->id ||
982 sdev->sdev_state == SDEV_DEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 continue;
984 spin_unlock_irqrestore(shost->host_lock, flags);
985 scsi_remove_device(sdev);
986 spin_lock_irqsave(shost->host_lock, flags);
Alan Sterna64358d2005-07-26 10:27:10 -0400987 goto restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 }
989 spin_unlock_irqrestore(shost->host_lock, flags);
990 scsi_target_reap(starget);
991}
992
mochel@digitalimplant.org20b1e672005-03-24 19:03:59 -0800993static int __remove_child (struct device * dev, void * data)
994{
995 if (scsi_is_target_device(dev))
996 __scsi_remove_target(to_scsi_target(dev));
997 return 0;
998}
999
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000/**
1001 * scsi_remove_target - try to remove a target and all its devices
1002 * @dev: generic starget or parent of generic stargets to be removed
1003 *
1004 * Note: This is slightly racy. It is possible that if the user
1005 * requests the addition of another device then the target won't be
1006 * removed.
1007 */
1008void scsi_remove_target(struct device *dev)
1009{
mochel@digitalimplant.org20b1e672005-03-24 19:03:59 -08001010 struct device *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011
1012 if (scsi_is_target_device(dev)) {
1013 __scsi_remove_target(to_scsi_target(dev));
1014 return;
1015 }
1016
1017 rdev = get_device(dev);
mochel@digitalimplant.org20b1e672005-03-24 19:03:59 -08001018 device_for_each_child(dev, NULL, __remove_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 put_device(rdev);
1020}
1021EXPORT_SYMBOL(scsi_remove_target);
1022
1023int scsi_register_driver(struct device_driver *drv)
1024{
1025 drv->bus = &scsi_bus_type;
1026
1027 return driver_register(drv);
1028}
1029EXPORT_SYMBOL(scsi_register_driver);
1030
1031int scsi_register_interface(struct class_interface *intf)
1032{
1033 intf->class = &sdev_class;
1034
1035 return class_interface_register(intf);
1036}
1037EXPORT_SYMBOL(scsi_register_interface);
1038
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039/**
1040 * scsi_sysfs_add_host - add scsi host to subsystem
1041 * @shost: scsi host struct to add to subsystem
1042 * @dev: parent struct device pointer
1043 **/
1044int scsi_sysfs_add_host(struct Scsi_Host *shost)
1045{
1046 int error, i;
1047
Hannes Reineckef7120a42008-03-18 14:32:28 +01001048 /* add host specific attributes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 if (shost->hostt->shost_attrs) {
1050 for (i = 0; shost->hostt->shost_attrs[i]; i++) {
Tony Jonesee959b02008-02-22 00:13:36 +01001051 error = device_create_file(&shost->shost_dev,
Hannes Reineckef7120a42008-03-18 14:32:28 +01001052 shost->hostt->shost_attrs[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 if (error)
1054 return error;
1055 }
1056 }
1057
1058 transport_register_device(&shost->shost_gendev);
James Bottomleyd52b3812008-01-05 09:38:30 -06001059 transport_configure_device(&shost->shost_gendev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 return 0;
1061}
1062
Kay Sieversbfd12942007-09-11 17:00:14 +02001063static struct device_type scsi_dev_type = {
1064 .name = "scsi_device",
1065 .release = scsi_device_dev_release,
1066 .groups = scsi_sdev_attr_groups,
1067};
1068
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069void scsi_sysfs_device_initialize(struct scsi_device *sdev)
1070{
1071 unsigned long flags;
1072 struct Scsi_Host *shost = sdev->host;
1073 struct scsi_target *starget = sdev->sdev_target;
1074
1075 device_initialize(&sdev->sdev_gendev);
1076 sdev->sdev_gendev.bus = &scsi_bus_type;
Kay Sieversbfd12942007-09-11 17:00:14 +02001077 sdev->sdev_gendev.type = &scsi_dev_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 sprintf(sdev->sdev_gendev.bus_id,"%d:%d:%d:%d",
1079 sdev->host->host_no, sdev->channel, sdev->id,
1080 sdev->lun);
1081
Tony Jonesee959b02008-02-22 00:13:36 +01001082 device_initialize(&sdev->sdev_dev);
1083 sdev->sdev_dev.parent = &sdev->sdev_gendev;
1084 sdev->sdev_dev.class = &sdev_class;
1085 snprintf(sdev->sdev_dev.bus_id, BUS_ID_SIZE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 "%d:%d:%d:%d", sdev->host->host_no,
1087 sdev->channel, sdev->id, sdev->lun);
Alan Stern7c9d6f12007-01-08 11:12:32 -05001088 sdev->scsi_level = starget->scsi_level;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 transport_setup_device(&sdev->sdev_gendev);
1090 spin_lock_irqsave(shost->host_lock, flags);
1091 list_add_tail(&sdev->same_target_siblings, &starget->devices);
1092 list_add_tail(&sdev->siblings, &shost->__devices);
1093 spin_unlock_irqrestore(shost->host_lock, flags);
1094}
1095
1096int scsi_is_sdev_device(const struct device *dev)
1097{
Kay Sievers13ba9bc2007-09-26 19:54:49 +02001098 return dev->type == &scsi_dev_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099}
1100EXPORT_SYMBOL(scsi_is_sdev_device);
1101
1102/* A blank transport template that is used in drivers that don't
1103 * yet implement Transport Attributes */
1104struct scsi_transport_template blank_transport_template = { { { {NULL, }, }, }, };