patch-2_6_7-vs1_9_1_12
[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.25 $"
30
31 #include "zfcp_ext.h"
32
33 #define ZFCP_LOG_AREA                   ZFCP_LOG_AREA_CONFIG
34
35 /**
36  * zfcp_sysfs_unit_release - gets called when a struct device unit is released
37  * @dev: pointer to belonging device
38  */
39 void
40 zfcp_sysfs_unit_release(struct device *dev)
41 {
42         kfree(dev);
43 }
44
45 /**
46  * ZFCP_DEFINE_UNIT_ATTR
47  * @_name:   name of show attribute
48  * @_format: format string
49  * @_value:  value to print
50  *
51  * Generates attribute for a unit.
52  */
53 #define ZFCP_DEFINE_UNIT_ATTR(_name, _format, _value)                    \
54 static ssize_t zfcp_sysfs_unit_##_name##_show(struct device *dev,        \
55                                               char *buf)                 \
56 {                                                                        \
57         struct zfcp_unit *unit;                                          \
58                                                                          \
59         unit = dev_get_drvdata(dev);                                     \
60         return sprintf(buf, _format, _value);                            \
61 }                                                                        \
62                                                                          \
63 static DEVICE_ATTR(_name, S_IRUGO, zfcp_sysfs_unit_##_name##_show, NULL);
64
65 ZFCP_DEFINE_UNIT_ATTR(status, "0x%08x\n", atomic_read(&unit->status));
66 ZFCP_DEFINE_UNIT_ATTR(scsi_lun, "0x%x\n", unit->scsi_lun);
67
68 /**
69  * zfcp_sysfs_unit_failed_store - failed state of unit
70  * @dev: pointer to belonging device
71  * @buf: pointer to input buffer
72  * @count: number of bytes in buffer
73  *
74  * Store function of the "failed" attribute of a unit.
75  * If a "0" gets written to "failed", error recovery will be
76  * started for the belonging unit.
77  */
78 static ssize_t
79 zfcp_sysfs_unit_failed_store(struct device *dev, const char *buf, size_t count)
80 {
81         struct zfcp_unit *unit;
82         unsigned int val;
83         char *endp;
84         int retval = 0;
85
86         down(&zfcp_data.config_sema);
87         unit = dev_get_drvdata(dev);
88         if (atomic_test_mask(ZFCP_STATUS_COMMON_REMOVE, &unit->status)) {
89                 retval = -EBUSY;
90                 goto out;
91         }
92
93         val = simple_strtoul(buf, &endp, 0);
94         if (((endp + 1) < (buf + count)) || (val != 0)) {
95                 retval = -EINVAL;
96                 goto out;
97         }
98
99         zfcp_erp_modify_unit_status(unit, ZFCP_STATUS_COMMON_RUNNING, ZFCP_SET);
100         zfcp_erp_unit_reopen(unit, ZFCP_STATUS_COMMON_ERP_FAILED);
101         zfcp_erp_wait(unit->port->adapter);
102  out:
103         up(&zfcp_data.config_sema);
104         return retval ? retval : count;
105 }
106
107 /**
108  * zfcp_sysfs_unit_failed_show - failed state of unit
109  * @dev: pointer to belonging device
110  * @buf: pointer to input buffer
111  *
112  * Show function of "failed" attribute of unit. Will be
113  * "0" if unit is working, otherwise "1".
114  */
115 static ssize_t
116 zfcp_sysfs_unit_failed_show(struct device *dev, char *buf)
117 {
118         struct zfcp_unit *unit;
119
120         unit = dev_get_drvdata(dev);
121         if (atomic_test_mask(ZFCP_STATUS_COMMON_ERP_FAILED, &unit->status))
122                 return sprintf(buf, "1\n");
123         else
124                 return sprintf(buf, "0\n");
125 }
126
127 static DEVICE_ATTR(failed, S_IWUSR | S_IRUGO, zfcp_sysfs_unit_failed_show,
128                    zfcp_sysfs_unit_failed_store);
129
130 /**
131  * zfcp_sysfs_unit_in_recovery_show - recovery state of unit
132  * @dev: pointer to belonging device
133  * @buf: pointer to input buffer
134  *
135  * Show function of "in_recovery" attribute of unit. Will be
136  * "0" if no error recovery is pending for unit, otherwise "1".
137  */
138 static ssize_t
139 zfcp_sysfs_unit_in_recovery_show(struct device *dev, char *buf)
140 {
141         struct zfcp_unit *unit;
142
143         unit = dev_get_drvdata(dev);
144         if (atomic_test_mask(ZFCP_STATUS_COMMON_ERP_INUSE, &unit->status))
145                 return sprintf(buf, "1\n");
146         else
147                 return sprintf(buf, "0\n");
148 }
149
150 static DEVICE_ATTR(in_recovery, S_IRUGO, zfcp_sysfs_unit_in_recovery_show,
151                    NULL);
152
153 static struct attribute *zfcp_unit_attrs[] = {
154         &dev_attr_scsi_lun.attr,
155         &dev_attr_failed.attr,
156         &dev_attr_in_recovery.attr,
157         &dev_attr_status.attr,
158         NULL
159 };
160
161 static struct attribute_group zfcp_unit_attr_group = {
162         .attrs = zfcp_unit_attrs,
163 };
164
165 /** 
166  * zfcp_sysfs_create_unit_files - create sysfs unit files
167  * @dev: pointer to belonging device
168  *
169  * Create all attributes of the sysfs representation of a unit.
170  */
171 int
172 zfcp_sysfs_unit_create_files(struct device *dev)
173 {
174         return sysfs_create_group(&dev->kobj, &zfcp_unit_attr_group);
175 }
176
177 /** 
178  * zfcp_sysfs_remove_unit_files - remove sysfs unit files
179  * @dev: pointer to belonging device
180  *
181  * Remove all attributes of the sysfs representation of a unit.
182  */
183 void
184 zfcp_sysfs_unit_remove_files(struct device *dev)
185 {
186         sysfs_remove_group(&dev->kobj, &zfcp_unit_attr_group);
187 }
188
189 #undef ZFCP_LOG_AREA