patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / drivers / s390 / scsi / zfcp_sysfs_adapter.c
1 /*
2  * linux/drivers/s390/scsi/zfcp_sysfs_adapter.c
3  *
4  * FCP adapter driver for IBM eServer zSeries
5  *
6  * sysfs adapter 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_ADAPTER_C_REVISION "$Revision: 1.33 $"
30
31 #include "zfcp_ext.h"
32
33 #define ZFCP_LOG_AREA                   ZFCP_LOG_AREA_CONFIG
34
35 static const char fc_topologies[5][25] = {
36         {"<error>"},
37         {"point-to-point"},
38         {"fabric"},
39         {"arbitrated loop"},
40         {"fabric (virt. adapter)"}
41 };
42
43 /**
44  * ZFCP_DEFINE_ADAPTER_ATTR
45  * @_name:   name of show attribute
46  * @_format: format string
47  * @_value:  value to print
48  *
49  * Generates attributes for an adapter.
50  */
51 #define ZFCP_DEFINE_ADAPTER_ATTR(_name, _format, _value)                      \
52 static ssize_t zfcp_sysfs_adapter_##_name##_show(struct device *dev,          \
53                                                  char *buf)                   \
54 {                                                                             \
55         struct zfcp_adapter *adapter;                                         \
56                                                                               \
57         adapter = dev_get_drvdata(dev);                                       \
58         return sprintf(buf, _format, _value);                                 \
59 }                                                                             \
60                                                                               \
61 static DEVICE_ATTR(_name, S_IRUGO, zfcp_sysfs_adapter_##_name##_show, NULL);
62
63 ZFCP_DEFINE_ADAPTER_ATTR(status, "0x%08x\n", atomic_read(&adapter->status));
64 ZFCP_DEFINE_ADAPTER_ATTR(wwnn, "0x%016llx\n", adapter->wwnn);
65 ZFCP_DEFINE_ADAPTER_ATTR(wwpn, "0x%016llx\n", adapter->wwpn);
66 ZFCP_DEFINE_ADAPTER_ATTR(s_id, "0x%06x\n", adapter->s_id);
67 ZFCP_DEFINE_ADAPTER_ATTR(card_version, "0x%04x\n", adapter->hydra_version);
68 ZFCP_DEFINE_ADAPTER_ATTR(lic_version, "0x%08x\n", adapter->fsf_lic_version);
69 ZFCP_DEFINE_ADAPTER_ATTR(fc_link_speed, "%d Gb/s\n", adapter->fc_link_speed);
70 ZFCP_DEFINE_ADAPTER_ATTR(fc_service_class, "%d\n", adapter->fc_service_class);
71 ZFCP_DEFINE_ADAPTER_ATTR(fc_topology, "%s\n",
72                          fc_topologies[adapter->fc_topology]);
73 ZFCP_DEFINE_ADAPTER_ATTR(hardware_version, "0x%08x\n",
74                          adapter->hardware_version);
75 ZFCP_DEFINE_ADAPTER_ATTR(serial_number, "%17s\n", adapter->serial_number);
76 ZFCP_DEFINE_ADAPTER_ATTR(scsi_host_no, "0x%x\n", adapter->scsi_host_no);
77
78 /**
79  * zfcp_sysfs_adapter_in_recovery_show - recovery state of adapter
80  * @dev: pointer to belonging device
81  * @buf: pointer to input buffer
82  *
83  * Show function of "in_recovery" attribute of adapter. Will be
84  * "0" if no error recovery is pending for adapter, otherwise "1".
85  */
86 static ssize_t
87 zfcp_sysfs_adapter_in_recovery_show(struct device *dev, char *buf)
88 {
89         struct zfcp_adapter *adapter;
90
91         adapter = dev_get_drvdata(dev);
92         if (atomic_test_mask(ZFCP_STATUS_COMMON_ERP_INUSE, &adapter->status))
93                 return sprintf(buf, "1\n");
94         else
95                 return sprintf(buf, "0\n");
96 }
97
98 static DEVICE_ATTR(in_recovery, S_IRUGO,
99                    zfcp_sysfs_adapter_in_recovery_show, NULL);
100
101 /**
102  * zfcp_sysfs_port_add_store - add a port to sysfs tree
103  * @dev: pointer to belonging device
104  * @buf: pointer to input buffer
105  * @count: number of bytes in buffer
106  *
107  * Store function of the "port_add" attribute of an adapter.
108  */
109 static ssize_t
110 zfcp_sysfs_port_add_store(struct device *dev, const char *buf, size_t count)
111 {
112         wwn_t wwpn;
113         char *endp;
114         struct zfcp_adapter *adapter;
115         struct zfcp_port *port;
116         int retval = -EINVAL;
117
118         down(&zfcp_data.config_sema);
119
120         adapter = dev_get_drvdata(dev);
121         if (atomic_test_mask(ZFCP_STATUS_COMMON_REMOVE, &adapter->status)) {
122                 retval = -EBUSY;
123                 goto out;
124         }
125
126         wwpn = simple_strtoull(buf, &endp, 0);
127         if ((endp + 1) < (buf + count))
128                 goto out;
129
130         port = zfcp_port_enqueue(adapter, wwpn, 0);
131         if (!port)
132                 goto out;
133
134         retval = 0;
135
136         zfcp_erp_port_reopen(port, 0);
137         zfcp_erp_wait(port->adapter);
138         zfcp_port_put(port);
139  out:
140         up(&zfcp_data.config_sema);
141         return retval ? retval : count;
142 }
143
144 static DEVICE_ATTR(port_add, S_IWUSR, NULL, zfcp_sysfs_port_add_store);
145
146 /**
147  * zfcp_sysfs_port_remove_store - remove a port from sysfs tree
148  * @dev: pointer to belonging device
149  * @buf: pointer to input buffer
150  * @count: number of bytes in buffer
151  *
152  * Store function of the "port_remove" attribute of an adapter.
153  */
154 static ssize_t
155 zfcp_sysfs_port_remove_store(struct device *dev, const char *buf, size_t count)
156 {
157         struct zfcp_adapter *adapter;
158         struct zfcp_port *port;
159         wwn_t wwpn;
160         char *endp;
161         int retval = 0;
162
163         down(&zfcp_data.config_sema);
164
165         adapter = dev_get_drvdata(dev);
166         if (atomic_test_mask(ZFCP_STATUS_COMMON_REMOVE, &adapter->status)) {
167                 retval = -EBUSY;
168                 goto out;
169         }
170
171         wwpn = simple_strtoull(buf, &endp, 0);
172         if ((endp + 1) < (buf + count)) {
173                 retval = -EINVAL;
174                 goto out;
175         }
176
177         write_lock_irq(&zfcp_data.config_lock);
178         port = zfcp_get_port_by_wwpn(adapter, wwpn);
179         if (port && (atomic_read(&port->refcount) == 0)) {
180                 zfcp_port_get(port);
181                 atomic_set_mask(ZFCP_STATUS_COMMON_REMOVE, &port->status);
182                 list_move(&port->list, &adapter->port_remove_lh);
183         }
184         else {
185                 port = NULL;
186         }
187         write_unlock_irq(&zfcp_data.config_lock);
188
189         if (!port) {
190                 retval = -ENXIO;
191                 goto out;
192         }
193
194         zfcp_erp_port_shutdown(port, 0);
195         zfcp_erp_wait(adapter);
196         zfcp_port_put(port);
197         zfcp_port_dequeue(port);
198  out:
199         up(&zfcp_data.config_sema);
200         return retval ? retval : count;
201 }
202
203 static DEVICE_ATTR(port_remove, S_IWUSR, NULL, zfcp_sysfs_port_remove_store);
204
205 /**
206  * zfcp_sysfs_adapter_failed_store - failed state of adapter
207  * @dev: pointer to belonging device
208  * @buf: pointer to input buffer
209  * @count: number of bytes in buffer
210  *
211  * Store function of the "failed" attribute of an adapter.
212  * If a "0" gets written to "failed", error recovery will be
213  * started for the belonging adapter.
214  */
215 static ssize_t
216 zfcp_sysfs_adapter_failed_store(struct device *dev,
217                                 const char *buf, size_t count)
218 {
219         struct zfcp_adapter *adapter;
220         unsigned int val;
221         char *endp;
222         int retval = 0;
223
224         down(&zfcp_data.config_sema);
225
226         adapter = dev_get_drvdata(dev);
227         if (atomic_test_mask(ZFCP_STATUS_COMMON_REMOVE, &adapter->status)) {
228                 retval = -EBUSY;
229                 goto out;
230         }
231
232         val = simple_strtoul(buf, &endp, 0);
233         if (((endp + 1) < (buf + count)) || (val != 0)) {
234                 retval = -EINVAL;
235                 goto out;
236         }
237
238         zfcp_erp_modify_adapter_status(adapter, ZFCP_STATUS_COMMON_RUNNING,
239                                        ZFCP_SET);
240         zfcp_erp_adapter_reopen(adapter, ZFCP_STATUS_COMMON_ERP_FAILED);
241         zfcp_erp_wait(adapter);
242  out:
243         up(&zfcp_data.config_sema);
244         return retval ? retval : count;
245 }
246
247 /**
248  * zfcp_sysfs_adapter_failed_show - failed state of adapter
249  * @dev: pointer to belonging device
250  * @buf: pointer to input buffer
251  *
252  * Show function of "failed" attribute of adapter. Will be
253  * "0" if adapter is working, otherwise "1".
254  */
255 static ssize_t
256 zfcp_sysfs_adapter_failed_show(struct device *dev, char *buf)
257 {
258         struct zfcp_adapter *adapter;
259
260         adapter = dev_get_drvdata(dev);
261         if (atomic_test_mask(ZFCP_STATUS_COMMON_ERP_FAILED, &adapter->status))
262                 return sprintf(buf, "1\n");
263         else
264                 return sprintf(buf, "0\n");
265 }
266
267 static DEVICE_ATTR(failed, S_IWUSR | S_IRUGO, zfcp_sysfs_adapter_failed_show,
268                    zfcp_sysfs_adapter_failed_store);
269
270 static struct attribute *zfcp_adapter_attrs[] = {
271         &dev_attr_failed.attr,
272         &dev_attr_in_recovery.attr,
273         &dev_attr_port_remove.attr,
274         &dev_attr_port_add.attr,
275         &dev_attr_wwnn.attr,
276         &dev_attr_wwpn.attr,
277         &dev_attr_s_id.attr,
278         &dev_attr_card_version.attr,
279         &dev_attr_lic_version.attr,
280         &dev_attr_fc_link_speed.attr,
281         &dev_attr_fc_service_class.attr,
282         &dev_attr_fc_topology.attr,
283         &dev_attr_scsi_host_no.attr,
284         &dev_attr_status.attr,
285         &dev_attr_hardware_version.attr,
286         &dev_attr_serial_number.attr,
287         NULL
288 };
289
290 static struct attribute_group zfcp_adapter_attr_group = {
291         .attrs = zfcp_adapter_attrs,
292 };
293
294 /**
295  * zfcp_sysfs_create_adapter_files - create sysfs adapter files
296  * @dev: pointer to belonging device
297  *
298  * Create all attributes of the sysfs representation of an adapter.
299  */
300 int
301 zfcp_sysfs_adapter_create_files(struct device *dev)
302 {
303         return sysfs_create_group(&dev->kobj, &zfcp_adapter_attr_group);
304 }
305
306 /**
307  * zfcp_sysfs_remove_adapter_files - remove sysfs adapter files
308  * @dev: pointer to belonging device
309  *
310  * Remove all attributes of the sysfs representation of an adapter.
311  */
312 void
313 zfcp_sysfs_adapter_remove_files(struct device *dev)
314 {
315         sysfs_remove_group(&dev->kobj, &zfcp_adapter_attr_group);
316 }
317
318 #undef ZFCP_LOG_AREA