ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / s390 / scsi / zfcp_sysfs_unit.c
1 /*
2  * linux/drivers/s390/scsi/zfcp_sysfs_unit.c
3  *
4  * FCP adapter driver for IBM eServer zSeries
5  *
6  * sysfs unit related routines
7  *
8  * (C) Copyright IBM Corp. 2003, 2004
9  *
10  * Authors:
11  *      Martin Peschke <mpeschke@de.ibm.com>
12  *      Heiko Carstens <heiko.carstens@de.ibm.com>
13  *
14  * This program is free software; you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License as published by
16  * the Free Software Foundation; either version 2, or (at your option)
17  * any later version.
18  *
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU General Public License for more details.
23  *
24  * You should have received a copy of the GNU General Public License
25  * along with this program; if not, write to the Free Software
26  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27  */
28
29 #define ZFCP_SYSFS_UNIT_C_REVISION "$Revision: 1.24 $"
30
31 #include <linux/init.h>
32 #include <linux/module.h>
33 #include <asm/ccwdev.h>
34 #include "zfcp_ext.h"
35 #include "zfcp_def.h"
36
37 #define ZFCP_LOG_AREA                   ZFCP_LOG_AREA_CONFIG
38
39 /**
40  * zfcp_sysfs_unit_release - gets called when a struct device unit is released
41  * @dev: pointer to belonging device
42  */
43 void
44 zfcp_sysfs_unit_release(struct device *dev)
45 {
46         kfree(dev);
47 }
48
49 /**
50  * ZFCP_DEFINE_UNIT_ATTR
51  * @_name:   name of show attribute
52  * @_format: format string
53  * @_value:  value to print
54  *
55  * Generates attribute for a unit.
56  */
57 #define ZFCP_DEFINE_UNIT_ATTR(_name, _format, _value)                    \
58 static ssize_t zfcp_sysfs_unit_##_name##_show(struct device *dev,        \
59                                               char *buf)                 \
60 {                                                                        \
61         struct zfcp_unit *unit;                                          \
62                                                                          \
63         unit = dev_get_drvdata(dev);                                     \
64         return sprintf(buf, _format, _value);                            \
65 }                                                                        \
66                                                                          \
67 static DEVICE_ATTR(_name, S_IRUGO, zfcp_sysfs_unit_##_name##_show, NULL);
68
69 ZFCP_DEFINE_UNIT_ATTR(status, "0x%08x\n", atomic_read(&unit->status));
70 ZFCP_DEFINE_UNIT_ATTR(scsi_lun, "0x%x\n", unit->scsi_lun);
71
72 /**
73  * zfcp_sysfs_unit_failed_store - failed state of unit
74  * @dev: pointer to belonging device
75  * @buf: pointer to input buffer
76  * @count: number of bytes in buffer
77  *
78  * Store function of the "failed" attribute of a unit.
79  * If a "0" gets written to "failed", error recovery will be
80  * started for the belonging unit.
81  */
82 static ssize_t
83 zfcp_sysfs_unit_failed_store(struct device *dev, const char *buf, size_t count)
84 {
85         struct zfcp_unit *unit;
86         unsigned int val;
87         char *endp;
88         int retval = 0;
89
90         down(&zfcp_data.config_sema);
91         unit = dev_get_drvdata(dev);
92         if (atomic_test_mask(ZFCP_STATUS_COMMON_REMOVE, &unit->status)) {
93                 retval = -EBUSY;
94                 goto out;
95         }
96
97         val = simple_strtoul(buf, &endp, 0);
98         if (((endp + 1) < (buf + count)) || (val != 0)) {
99                 retval = -EINVAL;
100                 goto out;
101         }
102
103         zfcp_erp_modify_unit_status(unit, ZFCP_STATUS_COMMON_RUNNING, ZFCP_SET);
104         zfcp_erp_unit_reopen(unit, ZFCP_STATUS_COMMON_ERP_FAILED);
105         zfcp_erp_wait(unit->port->adapter);
106  out:
107         up(&zfcp_data.config_sema);
108         return retval ? retval : count;
109 }
110
111 /**
112  * zfcp_sysfs_unit_failed_show - failed state of unit
113  * @dev: pointer to belonging device
114  * @buf: pointer to input buffer
115  *
116  * Show function of "failed" attribute of unit. Will be
117  * "0" if unit is working, otherwise "1".
118  */
119 static ssize_t
120 zfcp_sysfs_unit_failed_show(struct device *dev, char *buf)
121 {
122         struct zfcp_unit *unit;
123
124         unit = dev_get_drvdata(dev);
125         if (atomic_test_mask(ZFCP_STATUS_COMMON_ERP_FAILED, &unit->status))
126                 return sprintf(buf, "1\n");
127         else
128                 return sprintf(buf, "0\n");
129 }
130
131 static DEVICE_ATTR(failed, S_IWUSR | S_IRUGO, zfcp_sysfs_unit_failed_show,
132                    zfcp_sysfs_unit_failed_store);
133
134 /**
135  * zfcp_sysfs_unit_in_recovery_show - recovery state of unit
136  * @dev: pointer to belonging device
137  * @buf: pointer to input buffer
138  *
139  * Show function of "in_recovery" attribute of unit. Will be
140  * "0" if no error recovery is pending for unit, otherwise "1".
141  */
142 static ssize_t
143 zfcp_sysfs_unit_in_recovery_show(struct device *dev, char *buf)
144 {
145         struct zfcp_unit *unit;
146
147         unit = dev_get_drvdata(dev);
148         if (atomic_test_mask(ZFCP_STATUS_COMMON_ERP_INUSE, &unit->status))
149                 return sprintf(buf, "1\n");
150         else
151                 return sprintf(buf, "0\n");
152 }
153
154 static DEVICE_ATTR(in_recovery, S_IRUGO, zfcp_sysfs_unit_in_recovery_show,
155                    NULL);
156
157 static struct attribute *zfcp_unit_attrs[] = {
158         &dev_attr_scsi_lun.attr,
159         &dev_attr_failed.attr,
160         &dev_attr_in_recovery.attr,
161         &dev_attr_status.attr,
162         NULL
163 };
164
165 static struct attribute_group zfcp_unit_attr_group = {
166         .attrs = zfcp_unit_attrs,
167 };
168
169 /** 
170  * zfcp_sysfs_create_unit_files - create sysfs unit files
171  * @dev: pointer to belonging device
172  *
173  * Create all attributes of the sysfs representation of a unit.
174  */
175 int
176 zfcp_sysfs_unit_create_files(struct device *dev)
177 {
178         return sysfs_create_group(&dev->kobj, &zfcp_unit_attr_group);
179 }
180
181 /** 
182  * zfcp_sysfs_remove_unit_files - remove sysfs unit files
183  * @dev: pointer to belonging device
184  *
185  * Remove all attributes of the sysfs representation of a unit.
186  */
187 void
188 zfcp_sysfs_unit_remove_files(struct device *dev)
189 {
190         sysfs_remove_group(&dev->kobj, &zfcp_unit_attr_group);
191 }
192
193 #undef ZFCP_LOG_AREA