blob: 429c9b73e3e4de02254ac71af588f334a21e877b [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090010#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/init.h>
12#include <linux/blkdev.h>
13#include <linux/device.h>
14
15#include <scsi/scsi.h>
16#include <scsi/scsi_device.h>
17#include <scsi/scsi_host.h>
18#include <scsi/scsi_tcq.h>
19#include <scsi/scsi_transport.h>
Adrian Bunk44818ef2007-07-09 11:59:59 -070020#include <scsi/scsi_driver.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
22#include "scsi_priv.h"
23#include "scsi_logging.h"
24
Hannes Reineckeb0ed4332008-03-18 14:32:28 +010025static struct device_type scsi_dev_type;
26
Arjan van de Ven0ad78202005-11-28 16:22:25 +010027static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 enum scsi_device_state value;
29 char *name;
30} sdev_states[] = {
31 { SDEV_CREATED, "created" },
32 { SDEV_RUNNING, "running" },
33 { SDEV_CANCEL, "cancel" },
34 { SDEV_DEL, "deleted" },
35 { SDEV_QUIESCE, "quiesce" },
36 { SDEV_OFFLINE, "offline" },
37 { SDEV_BLOCK, "blocked" },
James Bottomley6f4267e2008-08-22 16:53:31 -050038 { SDEV_CREATED_BLOCK, "created-blocked" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070039};
40
41const char *scsi_device_state_name(enum scsi_device_state state)
42{
43 int i;
44 char *name = NULL;
45
Tobias Klauser6391a112006-06-08 22:23:48 -070046 for (i = 0; i < ARRAY_SIZE(sdev_states); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 if (sdev_states[i].value == state) {
48 name = sdev_states[i].name;
49 break;
50 }
51 }
52 return name;
53}
54
Arjan van de Ven0ad78202005-11-28 16:22:25 +010055static const struct {
Mike Andersond3301872005-06-16 11:12:38 -070056 enum scsi_host_state value;
57 char *name;
58} shost_states[] = {
59 { SHOST_CREATED, "created" },
60 { SHOST_RUNNING, "running" },
61 { SHOST_CANCEL, "cancel" },
62 { SHOST_DEL, "deleted" },
63 { SHOST_RECOVERY, "recovery" },
James Bottomley939647e2005-09-18 15:05:20 -050064 { SHOST_CANCEL_RECOVERY, "cancel/recovery" },
65 { SHOST_DEL_RECOVERY, "deleted/recovery", },
Mike Andersond3301872005-06-16 11:12:38 -070066};
67const char *scsi_host_state_name(enum scsi_host_state state)
68{
69 int i;
70 char *name = NULL;
71
Tobias Klauser6391a112006-06-08 22:23:48 -070072 for (i = 0; i < ARRAY_SIZE(shost_states); i++) {
Mike Andersond3301872005-06-16 11:12:38 -070073 if (shost_states[i].value == state) {
74 name = shost_states[i].name;
75 break;
76 }
77 }
78 return name;
79}
80
Linus Torvalds1da177e2005-04-16 15:20:36 -070081static int check_set(unsigned int *val, char *src)
82{
83 char *last;
84
85 if (strncmp(src, "-", 20) == 0) {
86 *val = SCAN_WILD_CARD;
87 } else {
88 /*
89 * Doesn't check for int overflow
90 */
91 *val = simple_strtoul(src, &last, 0);
92 if (*last != '\0')
93 return 1;
94 }
95 return 0;
96}
97
98static int scsi_scan(struct Scsi_Host *shost, const char *str)
99{
100 char s1[15], s2[15], s3[15], junk;
101 unsigned int channel, id, lun;
102 int res;
103
104 res = sscanf(str, "%10s %10s %10s %c", s1, s2, s3, &junk);
105 if (res != 3)
106 return -EINVAL;
107 if (check_set(&channel, s1))
108 return -EINVAL;
109 if (check_set(&id, s2))
110 return -EINVAL;
111 if (check_set(&lun, s3))
112 return -EINVAL;
Christoph Hellwige02f3f52006-01-13 19:04:00 +0100113 if (shost->transportt->user_scan)
114 res = shost->transportt->user_scan(shost, channel, id, lun);
115 else
116 res = scsi_scan_host_selected(shost, channel, id, lun, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 return res;
118}
119
120/*
121 * shost_show_function: macro to create an attr function that can be used to
122 * show a non-bit field.
123 */
124#define shost_show_function(name, field, format_string) \
125static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +0100126show_##name (struct device *dev, struct device_attribute *attr, \
127 char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128{ \
Tony Jonesee959b02008-02-22 00:13:36 +0100129 struct Scsi_Host *shost = class_to_shost(dev); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 return snprintf (buf, 20, format_string, shost->field); \
131}
132
133/*
134 * shost_rd_attr: macro to create a function and attribute variable for a
135 * read only field.
136 */
137#define shost_rd_attr2(name, field, format_string) \
138 shost_show_function(name, field, format_string) \
Tony Jonesee959b02008-02-22 00:13:36 +0100139static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
141#define shost_rd_attr(field, format_string) \
142shost_rd_attr2(field, field, format_string)
143
144/*
145 * Create the actual show/store functions and data structures.
146 */
147
Tony Jonesee959b02008-02-22 00:13:36 +0100148static ssize_t
149store_scan(struct device *dev, struct device_attribute *attr,
150 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151{
Tony Jonesee959b02008-02-22 00:13:36 +0100152 struct Scsi_Host *shost = class_to_shost(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 int res;
154
155 res = scsi_scan(shost, buf);
156 if (res == 0)
157 res = count;
158 return res;
159};
Tony Jonesee959b02008-02-22 00:13:36 +0100160static DEVICE_ATTR(scan, S_IWUSR, NULL, store_scan);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
Mike Andersond3301872005-06-16 11:12:38 -0700162static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +0100163store_shost_state(struct device *dev, struct device_attribute *attr,
164 const char *buf, size_t count)
Mike Andersond3301872005-06-16 11:12:38 -0700165{
166 int i;
Tony Jonesee959b02008-02-22 00:13:36 +0100167 struct Scsi_Host *shost = class_to_shost(dev);
Mike Andersond3301872005-06-16 11:12:38 -0700168 enum scsi_host_state state = 0;
169
Tobias Klauser6391a112006-06-08 22:23:48 -0700170 for (i = 0; i < ARRAY_SIZE(shost_states); i++) {
Mike Andersond3301872005-06-16 11:12:38 -0700171 const int len = strlen(shost_states[i].name);
172 if (strncmp(shost_states[i].name, buf, len) == 0 &&
173 buf[len] == '\n') {
174 state = shost_states[i].value;
175 break;
176 }
177 }
178 if (!state)
179 return -EINVAL;
180
181 if (scsi_host_set_state(shost, state))
182 return -EINVAL;
183 return count;
184}
185
186static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +0100187show_shost_state(struct device *dev, struct device_attribute *attr, char *buf)
Mike Andersond3301872005-06-16 11:12:38 -0700188{
Tony Jonesee959b02008-02-22 00:13:36 +0100189 struct Scsi_Host *shost = class_to_shost(dev);
Mike Andersond3301872005-06-16 11:12:38 -0700190 const char *name = scsi_host_state_name(shost->shost_state);
191
192 if (!name)
193 return -EINVAL;
194
195 return snprintf(buf, 20, "%s\n", name);
196}
197
Tony Jonesee959b02008-02-22 00:13:36 +0100198/* DEVICE_ATTR(state) clashes with dev_attr_state for sdev */
199struct device_attribute dev_attr_hstate =
200 __ATTR(state, S_IRUGO | S_IWUSR, show_shost_state, store_shost_state);
Mike Andersond3301872005-06-16 11:12:38 -0700201
FUJITA Tomonori5dc2b892007-09-01 02:02:20 +0900202static ssize_t
203show_shost_mode(unsigned int mode, char *buf)
204{
205 ssize_t len = 0;
206
207 if (mode & MODE_INITIATOR)
208 len = sprintf(buf, "%s", "Initiator");
209
210 if (mode & MODE_TARGET)
211 len += sprintf(buf + len, "%s%s", len ? ", " : "", "Target");
212
213 len += sprintf(buf + len, "\n");
214
215 return len;
216}
217
Tony Jonesee959b02008-02-22 00:13:36 +0100218static ssize_t
219show_shost_supported_mode(struct device *dev, struct device_attribute *attr,
220 char *buf)
FUJITA Tomonori5dc2b892007-09-01 02:02:20 +0900221{
Tony Jonesee959b02008-02-22 00:13:36 +0100222 struct Scsi_Host *shost = class_to_shost(dev);
James Bottomley7a39ac32007-09-25 22:45:53 -0500223 unsigned int supported_mode = shost->hostt->supported_mode;
FUJITA Tomonori5dc2b892007-09-01 02:02:20 +0900224
James Bottomley7a39ac32007-09-25 22:45:53 -0500225 if (supported_mode == MODE_UNKNOWN)
226 /* by default this should be initiator */
227 supported_mode = MODE_INITIATOR;
228
229 return show_shost_mode(supported_mode, buf);
FUJITA Tomonori5dc2b892007-09-01 02:02:20 +0900230}
231
Tony Jonesee959b02008-02-22 00:13:36 +0100232static DEVICE_ATTR(supported_mode, S_IRUGO | S_IWUSR, show_shost_supported_mode, NULL);
FUJITA Tomonori5dc2b892007-09-01 02:02:20 +0900233
Tony Jonesee959b02008-02-22 00:13:36 +0100234static ssize_t
235show_shost_active_mode(struct device *dev,
236 struct device_attribute *attr, char *buf)
FUJITA Tomonori5dc2b892007-09-01 02:02:20 +0900237{
Tony Jonesee959b02008-02-22 00:13:36 +0100238 struct Scsi_Host *shost = class_to_shost(dev);
FUJITA Tomonori5dc2b892007-09-01 02:02:20 +0900239
240 if (shost->active_mode == MODE_UNKNOWN)
241 return snprintf(buf, 20, "unknown\n");
242 else
243 return show_shost_mode(shost->active_mode, buf);
244}
245
Tony Jonesee959b02008-02-22 00:13:36 +0100246static DEVICE_ATTR(active_mode, S_IRUGO | S_IWUSR, show_shost_active_mode, NULL);
FUJITA Tomonori5dc2b892007-09-01 02:02:20 +0900247
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248shost_rd_attr(unique_id, "%u\n");
249shost_rd_attr(host_busy, "%hu\n");
250shost_rd_attr(cmd_per_lun, "%hd\n");
James Bottomleyed632da2006-10-16 10:06:27 -0500251shost_rd_attr(can_queue, "%hd\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252shost_rd_attr(sg_tablesize, "%hu\n");
253shost_rd_attr(unchecked_isa_dma, "%d\n");
Martin K. Petersen4469f982008-07-17 04:28:30 -0400254shost_rd_attr(prot_capabilities, "%u\n");
255shost_rd_attr(prot_guard_type, "%hd\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256shost_rd_attr2(proc_name, hostt->proc_name, "%s\n");
257
Hannes Reineckef7120a42008-03-18 14:32:28 +0100258static struct attribute *scsi_sysfs_shost_attrs[] = {
259 &dev_attr_unique_id.attr,
260 &dev_attr_host_busy.attr,
261 &dev_attr_cmd_per_lun.attr,
262 &dev_attr_can_queue.attr,
263 &dev_attr_sg_tablesize.attr,
264 &dev_attr_unchecked_isa_dma.attr,
265 &dev_attr_proc_name.attr,
266 &dev_attr_scan.attr,
267 &dev_attr_hstate.attr,
268 &dev_attr_supported_mode.attr,
269 &dev_attr_active_mode.attr,
Martin K. Petersen4469f982008-07-17 04:28:30 -0400270 &dev_attr_prot_capabilities.attr,
271 &dev_attr_prot_guard_type.attr,
Hannes Reineckef7120a42008-03-18 14:32:28 +0100272 NULL
273};
274
275struct attribute_group scsi_shost_attr_group = {
276 .attrs = scsi_sysfs_shost_attrs,
277};
278
David Brownella4dbd672009-06-24 10:06:31 -0700279const struct attribute_group *scsi_sysfs_shost_attr_groups[] = {
Hannes Reineckef7120a42008-03-18 14:32:28 +0100280 &scsi_shost_attr_group,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 NULL
282};
283
Tony Jonesee959b02008-02-22 00:13:36 +0100284static void scsi_device_cls_release(struct device *class_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285{
286 struct scsi_device *sdev;
287
288 sdev = class_to_sdev(class_dev);
289 put_device(&sdev->sdev_gendev);
290}
291
David Howells65f27f32006-11-22 14:55:48 +0000292static void scsi_device_dev_release_usercontext(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293{
294 struct scsi_device *sdev;
295 struct device *parent;
296 struct scsi_target *starget;
Jeff Garzika341cd02007-10-29 17:15:22 -0400297 struct list_head *this, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 unsigned long flags;
299
David Howells65f27f32006-11-22 14:55:48 +0000300 sdev = container_of(work, struct scsi_device, ew.work);
301
302 parent = sdev->sdev_gendev.parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 starget = to_scsi_target(parent);
304
305 spin_lock_irqsave(sdev->host->host_lock, flags);
306 starget->reap_ref++;
307 list_del(&sdev->siblings);
308 list_del(&sdev->same_target_siblings);
309 list_del(&sdev->starved_entry);
310 spin_unlock_irqrestore(sdev->host->host_lock, flags);
311
Jeff Garzika341cd02007-10-29 17:15:22 -0400312 cancel_work_sync(&sdev->event_work);
313
314 list_for_each_safe(this, tmp, &sdev->event_list) {
315 struct scsi_event *evt;
316
317 evt = list_entry(this, struct scsi_event, node);
318 list_del(&evt->node);
319 kfree(evt);
320 }
321
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 if (sdev->request_queue) {
323 sdev->request_queue->queuedata = NULL;
James Bottomley65110b22006-02-14 10:48:46 -0600324 /* user context needed to free queue */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 scsi_free_queue(sdev->request_queue);
c2a93312005-04-12 16:38:09 -0500326 /* temporary expedient, try to catch use of queue lock
327 * after free of sdev */
328 sdev->request_queue = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 }
330
331 scsi_target_reap(scsi_target(sdev));
332
333 kfree(sdev->inquiry);
334 kfree(sdev);
335
336 if (parent)
337 put_device(parent);
338}
339
James Bottomley65110b22006-02-14 10:48:46 -0600340static void scsi_device_dev_release(struct device *dev)
341{
James Bottomleyffedb452006-02-23 14:27:18 -0600342 struct scsi_device *sdp = to_scsi_device(dev);
David Howells65f27f32006-11-22 14:55:48 +0000343 execute_in_process_context(scsi_device_dev_release_usercontext,
James Bottomleyffedb452006-02-23 14:27:18 -0600344 &sdp->ew);
James Bottomley65110b22006-02-14 10:48:46 -0600345}
346
Adrian Bunk52c1da32005-06-23 22:05:33 -0700347static struct class sdev_class = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 .name = "scsi_device",
Tony Jonesee959b02008-02-22 00:13:36 +0100349 .dev_release = scsi_device_cls_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350};
351
352/* all probing is done in the individual ->probe routines */
353static int scsi_bus_match(struct device *dev, struct device_driver *gendrv)
354{
Hannes Reineckeb0ed4332008-03-18 14:32:28 +0100355 struct scsi_device *sdp;
356
357 if (dev->type != &scsi_dev_type)
358 return 0;
359
360 sdp = to_scsi_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 if (sdp->no_uld_attach)
362 return 0;
363 return (sdp->inq_periph_qual == SCSI_INQ_PQ_CON)? 1: 0;
364}
365
Kay Sievers7eff2e72007-08-14 15:15:12 +0200366static int scsi_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
Michael Tokarevd7b8bcb2006-10-27 16:02:37 +0400367{
James Bottomley1f42ea72008-05-22 12:34:41 +0100368 struct scsi_device *sdev;
369
370 if (dev->type != &scsi_dev_type)
371 return 0;
372
373 sdev = to_scsi_device(dev);
Michael Tokarevd7b8bcb2006-10-27 16:02:37 +0400374
Kay Sievers7eff2e72007-08-14 15:15:12 +0200375 add_uevent_var(env, "MODALIAS=" SCSI_DEVICE_MODALIAS_FMT, sdev->type);
Michael Tokarevd7b8bcb2006-10-27 16:02:37 +0400376 return 0;
377}
378
Jens Axboe9b847542006-01-06 09:28:07 +0100379static int scsi_bus_suspend(struct device * dev, pm_message_t state)
380{
Hannes Reineckeb0ed4332008-03-18 14:32:28 +0100381 struct device_driver *drv;
382 struct scsi_device *sdev;
Jens Axboe9b847542006-01-06 09:28:07 +0100383 int err;
384
Hannes Reineckeb0ed4332008-03-18 14:32:28 +0100385 if (dev->type != &scsi_dev_type)
386 return 0;
387
388 drv = dev->driver;
389 sdev = to_scsi_device(dev);
390
Jens Axboe9b847542006-01-06 09:28:07 +0100391 err = scsi_device_quiesce(sdev);
392 if (err)
393 return err;
394
Tejun Heoc3c94c52007-03-21 00:13:59 +0900395 if (drv && drv->suspend) {
396 err = drv->suspend(dev, state);
397 if (err)
398 return err;
399 }
Jens Axboe9b847542006-01-06 09:28:07 +0100400
Tejun Heoc3c94c52007-03-21 00:13:59 +0900401 return 0;
Jens Axboe9b847542006-01-06 09:28:07 +0100402}
403
404static int scsi_bus_resume(struct device * dev)
405{
Hannes Reineckeb0ed4332008-03-18 14:32:28 +0100406 struct device_driver *drv;
407 struct scsi_device *sdev;
Tejun Heo1dfcda02007-03-21 16:05:16 +0900408 int err = 0;
Jens Axboe9b847542006-01-06 09:28:07 +0100409
Hannes Reineckeb0ed4332008-03-18 14:32:28 +0100410 if (dev->type != &scsi_dev_type)
411 return 0;
412
413 drv = dev->driver;
414 sdev = to_scsi_device(dev);
415
Tejun Heoc3c94c52007-03-21 00:13:59 +0900416 if (drv && drv->resume)
Tejun Heo1dfcda02007-03-21 16:05:16 +0900417 err = drv->resume(dev);
Tejun Heoc3c94c52007-03-21 00:13:59 +0900418
Jens Axboe9b847542006-01-06 09:28:07 +0100419 scsi_device_resume(sdev);
Tejun Heoc3c94c52007-03-21 00:13:59 +0900420
Tejun Heo1dfcda02007-03-21 16:05:16 +0900421 return err;
Jens Axboe9b847542006-01-06 09:28:07 +0100422}
423
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424struct bus_type scsi_bus_type = {
425 .name = "scsi",
426 .match = scsi_bus_match,
Michael Tokarevd7b8bcb2006-10-27 16:02:37 +0400427 .uevent = scsi_bus_uevent,
Jens Axboe9b847542006-01-06 09:28:07 +0100428 .suspend = scsi_bus_suspend,
429 .resume = scsi_bus_resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430};
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -0700431EXPORT_SYMBOL_GPL(scsi_bus_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
433int scsi_sysfs_register(void)
434{
435 int error;
436
437 error = bus_register(&scsi_bus_type);
438 if (!error) {
439 error = class_register(&sdev_class);
440 if (error)
441 bus_unregister(&scsi_bus_type);
442 }
443
444 return error;
445}
446
447void scsi_sysfs_unregister(void)
448{
449 class_unregister(&sdev_class);
450 bus_unregister(&scsi_bus_type);
451}
452
453/*
454 * sdev_show_function: macro to create an attr function that can be used to
455 * show a non-bit field.
456 */
457#define sdev_show_function(field, format_string) \
458static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +0100459sdev_show_##field (struct device *dev, struct device_attribute *attr, \
460 char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461{ \
462 struct scsi_device *sdev; \
463 sdev = to_scsi_device(dev); \
464 return snprintf (buf, 20, format_string, sdev->field); \
465} \
466
467/*
468 * sdev_rd_attr: macro to create a function and attribute variable for a
469 * read only field.
470 */
471#define sdev_rd_attr(field, format_string) \
472 sdev_show_function(field, format_string) \
473static DEVICE_ATTR(field, S_IRUGO, sdev_show_##field, NULL);
474
475
476/*
477 * sdev_rd_attr: create a function and attribute variable for a
478 * read/write field.
479 */
480#define sdev_rw_attr(field, format_string) \
481 sdev_show_function(field, format_string) \
482 \
483static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +0100484sdev_store_##field (struct device *dev, struct device_attribute *attr, \
485 const char *buf, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486{ \
487 struct scsi_device *sdev; \
488 sdev = to_scsi_device(dev); \
489 snscanf (buf, 20, format_string, &sdev->field); \
490 return count; \
491} \
492static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, sdev_show_##field, sdev_store_##field);
493
494/* Currently we don't export bit fields, but we might in future,
495 * so leave this code in */
496#if 0
497/*
498 * sdev_rd_attr: create a function and attribute variable for a
499 * read/write bit field.
500 */
501#define sdev_rw_attr_bit(field) \
502 sdev_show_function(field, "%d\n") \
503 \
504static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +0100505sdev_store_##field (struct device *dev, struct device_attribute *attr, \
506 const char *buf, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507{ \
508 int ret; \
509 struct scsi_device *sdev; \
510 ret = scsi_sdev_check_buf_bit(buf); \
511 if (ret >= 0) { \
512 sdev = to_scsi_device(dev); \
513 sdev->field = ret; \
514 ret = count; \
515 } \
516 return ret; \
517} \
518static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, sdev_show_##field, sdev_store_##field);
519
520/*
521 * scsi_sdev_check_buf_bit: return 0 if buf is "0", return 1 if buf is "1",
522 * else return -EINVAL.
523 */
524static int scsi_sdev_check_buf_bit(const char *buf)
525{
526 if ((buf[1] == '\0') || ((buf[1] == '\n') && (buf[2] == '\0'))) {
527 if (buf[0] == '1')
528 return 1;
529 else if (buf[0] == '0')
530 return 0;
531 else
532 return -EINVAL;
533 } else
534 return -EINVAL;
535}
536#endif
537/*
538 * Create the actual show/store functions and data structures.
539 */
540sdev_rd_attr (device_blocked, "%d\n");
541sdev_rd_attr (queue_depth, "%d\n");
542sdev_rd_attr (type, "%d\n");
543sdev_rd_attr (scsi_level, "%d\n");
544sdev_rd_attr (vendor, "%.8s\n");
545sdev_rd_attr (model, "%.16s\n");
546sdev_rd_attr (rev, "%.4s\n");
547
Jens Axboe242f9dc2008-09-14 05:55:09 -0700548/*
549 * TODO: can we make these symlinks to the block layer ones?
550 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551static ssize_t
Yani Ioannou10523b32005-05-17 06:43:37 -0400552sdev_show_timeout (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553{
554 struct scsi_device *sdev;
555 sdev = to_scsi_device(dev);
Jens Axboe242f9dc2008-09-14 05:55:09 -0700556 return snprintf(buf, 20, "%d\n", sdev->request_queue->rq_timeout / HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557}
558
559static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +0100560sdev_store_timeout (struct device *dev, struct device_attribute *attr,
561 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562{
563 struct scsi_device *sdev;
564 int timeout;
565 sdev = to_scsi_device(dev);
566 sscanf (buf, "%d\n", &timeout);
Jens Axboe242f9dc2008-09-14 05:55:09 -0700567 blk_queue_rq_timeout(sdev->request_queue, timeout * HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 return count;
569}
570static DEVICE_ATTR(timeout, S_IRUGO | S_IWUSR, sdev_show_timeout, sdev_store_timeout);
571
572static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +0100573store_rescan_field (struct device *dev, struct device_attribute *attr,
574 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575{
576 scsi_rescan_device(dev);
577 return count;
578}
579static DEVICE_ATTR(rescan, S_IWUSR, NULL, store_rescan_field);
580
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400581static void sdev_store_delete_callback(struct device *dev)
582{
583 scsi_remove_device(to_scsi_device(dev));
584}
585
Tony Jonesee959b02008-02-22 00:13:36 +0100586static ssize_t
587sdev_store_delete(struct device *dev, struct device_attribute *attr,
588 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589{
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400590 int rc;
591
592 /* An attribute cannot be unregistered by one of its own methods,
593 * so we have to use this roundabout approach.
594 */
595 rc = device_schedule_callback(dev, sdev_store_delete_callback);
596 if (rc)
597 count = rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 return count;
599};
600static DEVICE_ATTR(delete, S_IWUSR, NULL, sdev_store_delete);
601
602static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +0100603store_state_field(struct device *dev, struct device_attribute *attr,
604 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605{
606 int i;
607 struct scsi_device *sdev = to_scsi_device(dev);
608 enum scsi_device_state state = 0;
609
Tobias Klauser6391a112006-06-08 22:23:48 -0700610 for (i = 0; i < ARRAY_SIZE(sdev_states); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 const int len = strlen(sdev_states[i].name);
612 if (strncmp(sdev_states[i].name, buf, len) == 0 &&
613 buf[len] == '\n') {
614 state = sdev_states[i].value;
615 break;
616 }
617 }
618 if (!state)
619 return -EINVAL;
620
621 if (scsi_device_set_state(sdev, state))
622 return -EINVAL;
623 return count;
624}
625
626static ssize_t
Yani Ioannou10523b32005-05-17 06:43:37 -0400627show_state_field(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628{
629 struct scsi_device *sdev = to_scsi_device(dev);
630 const char *name = scsi_device_state_name(sdev->sdev_state);
631
632 if (!name)
633 return -EINVAL;
634
635 return snprintf(buf, 20, "%s\n", name);
636}
637
638static DEVICE_ATTR(state, S_IRUGO | S_IWUSR, show_state_field, store_state_field);
639
640static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +0100641show_queue_type_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 const char *name = "none";
646
647 if (sdev->ordered_tags)
648 name = "ordered";
649 else if (sdev->simple_tags)
650 name = "simple";
651
652 return snprintf(buf, 20, "%s\n", name);
653}
654
655static DEVICE_ATTR(queue_type, S_IRUGO, show_queue_type_field, NULL);
656
657static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +0100658show_iostat_counterbits(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659{
660 return snprintf(buf, 20, "%d\n", (int)sizeof(atomic_t) * 8);
661}
662
663static DEVICE_ATTR(iocounterbits, S_IRUGO, show_iostat_counterbits, NULL);
664
665#define show_sdev_iostat(field) \
666static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +0100667show_iostat_##field(struct device *dev, struct device_attribute *attr, \
668 char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669{ \
670 struct scsi_device *sdev = to_scsi_device(dev); \
671 unsigned long long count = atomic_read(&sdev->field); \
672 return snprintf(buf, 20, "0x%llx\n", count); \
673} \
674static DEVICE_ATTR(field, S_IRUGO, show_iostat_##field, NULL)
675
676show_sdev_iostat(iorequest_cnt);
677show_sdev_iostat(iodone_cnt);
678show_sdev_iostat(ioerr_cnt);
679
Michael Tokarevd7b8bcb2006-10-27 16:02:37 +0400680static ssize_t
681sdev_show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
682{
683 struct scsi_device *sdev;
684 sdev = to_scsi_device(dev);
685 return snprintf (buf, 20, SCSI_DEVICE_MODALIAS_FMT "\n", sdev->type);
686}
687static DEVICE_ATTR(modalias, S_IRUGO, sdev_show_modalias, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
Jeff Garzika341cd02007-10-29 17:15:22 -0400689#define DECLARE_EVT_SHOW(name, Cap_name) \
690static ssize_t \
691sdev_show_evt_##name(struct device *dev, struct device_attribute *attr, \
Tony Jonesee959b02008-02-22 00:13:36 +0100692 char *buf) \
Jeff Garzika341cd02007-10-29 17:15:22 -0400693{ \
694 struct scsi_device *sdev = to_scsi_device(dev); \
695 int val = test_bit(SDEV_EVT_##Cap_name, sdev->supported_events);\
696 return snprintf(buf, 20, "%d\n", val); \
697}
698
699#define DECLARE_EVT_STORE(name, Cap_name) \
700static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +0100701sdev_store_evt_##name(struct device *dev, struct device_attribute *attr,\
Jeff Garzika341cd02007-10-29 17:15:22 -0400702 const char *buf, size_t count) \
703{ \
704 struct scsi_device *sdev = to_scsi_device(dev); \
705 int val = simple_strtoul(buf, NULL, 0); \
706 if (val == 0) \
707 clear_bit(SDEV_EVT_##Cap_name, sdev->supported_events); \
708 else if (val == 1) \
709 set_bit(SDEV_EVT_##Cap_name, sdev->supported_events); \
710 else \
711 return -EINVAL; \
712 return count; \
713}
714
715#define DECLARE_EVT(name, Cap_name) \
716 DECLARE_EVT_SHOW(name, Cap_name) \
717 DECLARE_EVT_STORE(name, Cap_name) \
718 static DEVICE_ATTR(evt_##name, S_IRUGO, sdev_show_evt_##name, \
719 sdev_store_evt_##name);
720#define REF_EVT(name) &dev_attr_evt_##name.attr
721
722DECLARE_EVT(media_change, MEDIA_CHANGE)
723
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724/* Default template for device attributes. May NOT be modified */
Kay Sieversbfd12942007-09-11 17:00:14 +0200725static struct attribute *scsi_sdev_attrs[] = {
726 &dev_attr_device_blocked.attr,
727 &dev_attr_type.attr,
728 &dev_attr_scsi_level.attr,
729 &dev_attr_vendor.attr,
730 &dev_attr_model.attr,
731 &dev_attr_rev.attr,
732 &dev_attr_rescan.attr,
733 &dev_attr_delete.attr,
734 &dev_attr_state.attr,
735 &dev_attr_timeout.attr,
736 &dev_attr_iocounterbits.attr,
737 &dev_attr_iorequest_cnt.attr,
738 &dev_attr_iodone_cnt.attr,
739 &dev_attr_ioerr_cnt.attr,
740 &dev_attr_modalias.attr,
Jeff Garzika341cd02007-10-29 17:15:22 -0400741 REF_EVT(media_change),
Kay Sieversbfd12942007-09-11 17:00:14 +0200742 NULL
743};
744
745static struct attribute_group scsi_sdev_attr_group = {
746 .attrs = scsi_sdev_attrs,
747};
748
David Brownella4dbd672009-06-24 10:06:31 -0700749static const struct attribute_group *scsi_sdev_attr_groups[] = {
Kay Sieversbfd12942007-09-11 17:00:14 +0200750 &scsi_sdev_attr_group,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 NULL
752};
753
Tony Jonesee959b02008-02-22 00:13:36 +0100754static ssize_t
755sdev_store_queue_depth_rw(struct device *dev, struct device_attribute *attr,
756 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757{
758 int depth, retval;
759 struct scsi_device *sdev = to_scsi_device(dev);
760 struct scsi_host_template *sht = sdev->host->hostt;
761
762 if (!sht->change_queue_depth)
763 return -EINVAL;
764
765 depth = simple_strtoul(buf, NULL, 0);
766
767 if (depth < 1)
768 return -EINVAL;
769
Mike Christiee881a172009-10-15 17:46:39 -0700770 retval = sht->change_queue_depth(sdev, depth,
771 SCSI_QDEPTH_DEFAULT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 if (retval < 0)
773 return retval;
774
Vasu Dev4a840672009-10-22 15:46:33 -0700775 sdev->max_queue_depth = sdev->queue_depth;
776
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 return count;
778}
779
780static struct device_attribute sdev_attr_queue_depth_rw =
781 __ATTR(queue_depth, S_IRUGO | S_IWUSR, sdev_show_queue_depth,
782 sdev_store_queue_depth_rw);
783
Tony Jonesee959b02008-02-22 00:13:36 +0100784static ssize_t
Vasu Dev4a840672009-10-22 15:46:33 -0700785sdev_show_queue_ramp_up_period(struct device *dev,
786 struct device_attribute *attr,
787 char *buf)
788{
789 struct scsi_device *sdev;
790 sdev = to_scsi_device(dev);
791 return snprintf(buf, 20, "%u\n",
792 jiffies_to_msecs(sdev->queue_ramp_up_period));
793}
794
795static ssize_t
796sdev_store_queue_ramp_up_period(struct device *dev,
797 struct device_attribute *attr,
798 const char *buf, size_t count)
799{
800 struct scsi_device *sdev = to_scsi_device(dev);
801 unsigned long period;
802
803 if (strict_strtoul(buf, 10, &period))
804 return -EINVAL;
805
806 sdev->queue_ramp_up_period = msecs_to_jiffies(period);
807 return period;
808}
809
810static struct device_attribute sdev_attr_queue_ramp_up_period =
811 __ATTR(queue_ramp_up_period, S_IRUGO | S_IWUSR,
812 sdev_show_queue_ramp_up_period,
813 sdev_store_queue_ramp_up_period);
814
815static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +0100816sdev_store_queue_type_rw(struct device *dev, struct device_attribute *attr,
817 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818{
819 struct scsi_device *sdev = to_scsi_device(dev);
820 struct scsi_host_template *sht = sdev->host->hostt;
821 int tag_type = 0, retval;
822 int prev_tag_type = scsi_get_tag_type(sdev);
823
824 if (!sdev->tagged_supported || !sht->change_queue_type)
825 return -EINVAL;
826
827 if (strncmp(buf, "ordered", 7) == 0)
828 tag_type = MSG_ORDERED_TAG;
829 else if (strncmp(buf, "simple", 6) == 0)
830 tag_type = MSG_SIMPLE_TAG;
831 else if (strncmp(buf, "none", 4) != 0)
832 return -EINVAL;
833
834 if (tag_type == prev_tag_type)
835 return count;
836
837 retval = sht->change_queue_type(sdev, tag_type);
838 if (retval < 0)
839 return retval;
840
841 return count;
842}
843
James Bottomley643eb2d2008-03-22 22:42:27 -0500844static int scsi_target_add(struct scsi_target *starget)
845{
846 int error;
847
848 if (starget->state != STARGET_CREATED)
849 return 0;
850
Rafael J. Wysocki4cb077d2010-02-08 19:18:26 +0100851 device_enable_async_suspend(&starget->dev);
852
James Bottomley643eb2d2008-03-22 22:42:27 -0500853 error = device_add(&starget->dev);
854 if (error) {
855 dev_err(&starget->dev, "target device_add failed, error %d\n", error);
856 get_device(&starget->dev);
857 scsi_target_reap(starget);
858 put_device(&starget->dev);
859 return error;
860 }
861 transport_add_device(&starget->dev);
862 starget->state = STARGET_RUNNING;
863
864 return 0;
865}
866
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867static struct device_attribute sdev_attr_queue_type_rw =
868 __ATTR(queue_type, S_IRUGO | S_IWUSR, show_queue_type_field,
869 sdev_store_queue_type_rw);
870
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871/**
872 * scsi_sysfs_add_sdev - add scsi device to sysfs
873 * @sdev: scsi_device to add
874 *
875 * Return value:
876 * 0 on Success / non-zero on Failure
877 **/
878int scsi_sysfs_add_sdev(struct scsi_device *sdev)
879{
880 int error, i;
James Bottomley80ed71c2007-07-19 10:15:10 -0500881 struct request_queue *rq = sdev->request_queue;
James Bottomley643eb2d2008-03-22 22:42:27 -0500882 struct scsi_target *starget = sdev->sdev_target;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883
Alan Sternee37e092010-02-12 12:13:55 -0500884 error = scsi_device_set_state(sdev, SDEV_RUNNING);
885 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 return error;
887
James Bottomley643eb2d2008-03-22 22:42:27 -0500888 error = scsi_target_add(starget);
889 if (error)
890 return error;
891
892 transport_configure_device(&starget->dev);
Rafael J. Wysocki4cb077d2010-02-08 19:18:26 +0100893 device_enable_async_suspend(&sdev->sdev_gendev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 error = device_add(&sdev->sdev_gendev);
895 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 printk(KERN_INFO "error 1\n");
Alan Sternee37e092010-02-12 12:13:55 -0500897 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 }
Rafael J. Wysocki4cb077d2010-02-08 19:18:26 +0100899 device_enable_async_suspend(&sdev->sdev_dev);
Tony Jonesee959b02008-02-22 00:13:36 +0100900 error = device_add(&sdev->sdev_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 if (error) {
902 printk(KERN_INFO "error 2\n");
James Bottomley860dc732009-11-19 17:48:29 -0500903 device_del(&sdev->sdev_gendev);
Alan Sternee37e092010-02-12 12:13:55 -0500904 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 }
James Bottomley860dc732009-11-19 17:48:29 -0500906 transport_add_device(&sdev->sdev_gendev);
907 sdev->is_visible = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908
Kay Sieversbfd12942007-09-11 17:00:14 +0200909 /* create queue files, which may be writable, depending on the host */
Vasu Dev4a840672009-10-22 15:46:33 -0700910 if (sdev->host->hostt->change_queue_depth) {
911 error = device_create_file(&sdev->sdev_gendev,
912 &sdev_attr_queue_depth_rw);
913 error = device_create_file(&sdev->sdev_gendev,
914 &sdev_attr_queue_ramp_up_period);
915 }
Kay Sieversbfd12942007-09-11 17:00:14 +0200916 else
917 error = device_create_file(&sdev->sdev_gendev, &dev_attr_queue_depth);
James Bottomley860dc732009-11-19 17:48:29 -0500918 if (error)
Alan Sternee37e092010-02-12 12:13:55 -0500919 return error;
James Bottomley860dc732009-11-19 17:48:29 -0500920
Kay Sieversbfd12942007-09-11 17:00:14 +0200921 if (sdev->host->hostt->change_queue_type)
922 error = device_create_file(&sdev->sdev_gendev, &sdev_attr_queue_type_rw);
923 else
924 error = device_create_file(&sdev->sdev_gendev, &dev_attr_queue_type);
James Bottomley860dc732009-11-19 17:48:29 -0500925 if (error)
Alan Sternee37e092010-02-12 12:13:55 -0500926 return error;
Kay Sieversbfd12942007-09-11 17:00:14 +0200927
FUJITA Tomonori97f46ae2008-04-19 00:43:14 +0900928 error = bsg_register_queue(rq, &sdev->sdev_gendev, NULL, NULL);
James Bottomley80ed71c2007-07-19 10:15:10 -0500929
930 if (error)
James Bottomley860dc732009-11-19 17:48:29 -0500931 /* we're treating error on bsg register as non-fatal,
932 * so pretend nothing went wrong */
James Bottomley80ed71c2007-07-19 10:15:10 -0500933 sdev_printk(KERN_INFO, sdev,
934 "Failed to register bsg queue, errno=%d\n", error);
935
Kay Sieversbfd12942007-09-11 17:00:14 +0200936 /* add additional host specific attributes */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 if (sdev->host->hostt->sdev_attrs) {
938 for (i = 0; sdev->host->hostt->sdev_attrs[i]; i++) {
Kay Sieversbfd12942007-09-11 17:00:14 +0200939 error = device_create_file(&sdev->sdev_gendev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 sdev->host->hostt->sdev_attrs[i]);
James Bottomley860dc732009-11-19 17:48:29 -0500941 if (error)
Alan Sternee37e092010-02-12 12:13:55 -0500942 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 }
944 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947}
948
Alan Stern903f4fe2005-07-26 10:20:53 -0400949void __scsi_remove_device(struct scsi_device *sdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950{
James Bottomleydf133c22005-11-06 11:47:08 -0600951 struct device *dev = &sdev->sdev_gendev;
952
James Bottomley860dc732009-11-19 17:48:29 -0500953 if (sdev->is_visible) {
954 if (scsi_device_set_state(sdev, SDEV_CANCEL) != 0)
955 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956
James Bottomley860dc732009-11-19 17:48:29 -0500957 bsg_unregister_queue(sdev->request_queue);
958 device_unregister(&sdev->sdev_dev);
959 transport_remove_device(dev);
960 device_del(dev);
961 } else
962 put_device(&sdev->sdev_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 scsi_device_set_state(sdev, SDEV_DEL);
964 if (sdev->host->hostt->slave_destroy)
965 sdev->host->hostt->slave_destroy(sdev);
James Bottomleydf133c22005-11-06 11:47:08 -0600966 transport_destroy_device(dev);
967 put_device(dev);
Alan Stern903f4fe2005-07-26 10:20:53 -0400968}
969
970/**
971 * scsi_remove_device - unregister a device from the scsi bus
972 * @sdev: scsi_device to unregister
973 **/
974void scsi_remove_device(struct scsi_device *sdev)
975{
Alan Stern54195002005-09-15 21:52:51 -0400976 struct Scsi_Host *shost = sdev->host;
977
Arjan van de Ven0b950672006-01-11 13:16:10 +0100978 mutex_lock(&shost->scan_mutex);
Alan Stern903f4fe2005-07-26 10:20:53 -0400979 __scsi_remove_device(sdev);
Arjan van de Ven0b950672006-01-11 13:16:10 +0100980 mutex_unlock(&shost->scan_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981}
982EXPORT_SYMBOL(scsi_remove_device);
983
Adrian Bunk44818ef2007-07-09 11:59:59 -0700984static void __scsi_remove_target(struct scsi_target *starget)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985{
986 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
987 unsigned long flags;
Alan Sterna64358d2005-07-26 10:27:10 -0400988 struct scsi_device *sdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989
990 spin_lock_irqsave(shost->host_lock, flags);
991 starget->reap_ref++;
Alan Sterna64358d2005-07-26 10:27:10 -0400992 restart:
993 list_for_each_entry(sdev, &shost->__devices, siblings) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 if (sdev->channel != starget->channel ||
Alan Sterna64358d2005-07-26 10:27:10 -0400995 sdev->id != starget->id ||
996 sdev->sdev_state == SDEV_DEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 continue;
998 spin_unlock_irqrestore(shost->host_lock, flags);
999 scsi_remove_device(sdev);
1000 spin_lock_irqsave(shost->host_lock, flags);
Alan Sterna64358d2005-07-26 10:27:10 -04001001 goto restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 }
1003 spin_unlock_irqrestore(shost->host_lock, flags);
1004 scsi_target_reap(starget);
1005}
1006
mochel@digitalimplant.org20b1e672005-03-24 19:03:59 -08001007static int __remove_child (struct device * dev, void * data)
1008{
1009 if (scsi_is_target_device(dev))
1010 __scsi_remove_target(to_scsi_target(dev));
1011 return 0;
1012}
1013
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014/**
1015 * scsi_remove_target - try to remove a target and all its devices
1016 * @dev: generic starget or parent of generic stargets to be removed
1017 *
1018 * Note: This is slightly racy. It is possible that if the user
1019 * requests the addition of another device then the target won't be
1020 * removed.
1021 */
1022void scsi_remove_target(struct device *dev)
1023{
mochel@digitalimplant.org20b1e672005-03-24 19:03:59 -08001024 struct device *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025
1026 if (scsi_is_target_device(dev)) {
1027 __scsi_remove_target(to_scsi_target(dev));
1028 return;
1029 }
1030
1031 rdev = get_device(dev);
mochel@digitalimplant.org20b1e672005-03-24 19:03:59 -08001032 device_for_each_child(dev, NULL, __remove_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 put_device(rdev);
1034}
1035EXPORT_SYMBOL(scsi_remove_target);
1036
1037int scsi_register_driver(struct device_driver *drv)
1038{
1039 drv->bus = &scsi_bus_type;
1040
1041 return driver_register(drv);
1042}
1043EXPORT_SYMBOL(scsi_register_driver);
1044
1045int scsi_register_interface(struct class_interface *intf)
1046{
1047 intf->class = &sdev_class;
1048
1049 return class_interface_register(intf);
1050}
1051EXPORT_SYMBOL(scsi_register_interface);
1052
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053/**
1054 * scsi_sysfs_add_host - add scsi host to subsystem
1055 * @shost: scsi host struct to add to subsystem
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 **/
1057int scsi_sysfs_add_host(struct Scsi_Host *shost)
1058{
1059 int error, i;
1060
Hannes Reineckef7120a42008-03-18 14:32:28 +01001061 /* add host specific attributes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 if (shost->hostt->shost_attrs) {
1063 for (i = 0; shost->hostt->shost_attrs[i]; i++) {
Tony Jonesee959b02008-02-22 00:13:36 +01001064 error = device_create_file(&shost->shost_dev,
Hannes Reineckef7120a42008-03-18 14:32:28 +01001065 shost->hostt->shost_attrs[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 if (error)
1067 return error;
1068 }
1069 }
1070
1071 transport_register_device(&shost->shost_gendev);
James Bottomleyd52b3812008-01-05 09:38:30 -06001072 transport_configure_device(&shost->shost_gendev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 return 0;
1074}
1075
Kay Sieversbfd12942007-09-11 17:00:14 +02001076static struct device_type scsi_dev_type = {
1077 .name = "scsi_device",
1078 .release = scsi_device_dev_release,
1079 .groups = scsi_sdev_attr_groups,
1080};
1081
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082void scsi_sysfs_device_initialize(struct scsi_device *sdev)
1083{
1084 unsigned long flags;
1085 struct Scsi_Host *shost = sdev->host;
1086 struct scsi_target *starget = sdev->sdev_target;
1087
1088 device_initialize(&sdev->sdev_gendev);
1089 sdev->sdev_gendev.bus = &scsi_bus_type;
Kay Sieversbfd12942007-09-11 17:00:14 +02001090 sdev->sdev_gendev.type = &scsi_dev_type;
Kay Sievers71610f52008-12-03 22:41:36 +01001091 dev_set_name(&sdev->sdev_gendev, "%d:%d:%d:%d",
1092 sdev->host->host_no, sdev->channel, sdev->id, sdev->lun);
1093
Tony Jonesee959b02008-02-22 00:13:36 +01001094 device_initialize(&sdev->sdev_dev);
James Bottomley37e6ba02009-10-02 13:30:08 -05001095 sdev->sdev_dev.parent = get_device(&sdev->sdev_gendev);
Tony Jonesee959b02008-02-22 00:13:36 +01001096 sdev->sdev_dev.class = &sdev_class;
Kay Sievers71610f52008-12-03 22:41:36 +01001097 dev_set_name(&sdev->sdev_dev, "%d:%d:%d:%d",
1098 sdev->host->host_no, sdev->channel, sdev->id, sdev->lun);
Alan Stern7c9d6f12007-01-08 11:12:32 -05001099 sdev->scsi_level = starget->scsi_level;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 transport_setup_device(&sdev->sdev_gendev);
1101 spin_lock_irqsave(shost->host_lock, flags);
1102 list_add_tail(&sdev->same_target_siblings, &starget->devices);
1103 list_add_tail(&sdev->siblings, &shost->__devices);
1104 spin_unlock_irqrestore(shost->host_lock, flags);
1105}
1106
1107int scsi_is_sdev_device(const struct device *dev)
1108{
Kay Sievers13ba9bc2007-09-26 19:54:49 +02001109 return dev->type == &scsi_dev_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110}
1111EXPORT_SYMBOL(scsi_is_sdev_device);
1112
1113/* A blank transport template that is used in drivers that don't
1114 * yet implement Transport Attributes */
1115struct scsi_transport_template blank_transport_template = { { { {NULL, }, }, }, };