vserver 1.9.3
[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.37 $"
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 ZFCP_DEFINE_ADAPTER_ATTR(in_recovery, "%d\n", atomic_test_mask
78                          (ZFCP_STATUS_COMMON_ERP_INUSE, &adapter->status));
79
80 /**
81  * zfcp_sysfs_port_add_store - add a port to sysfs tree
82  * @dev: pointer to belonging device
83  * @buf: pointer to input buffer
84  * @count: number of bytes in buffer
85  *
86  * Store function of the "port_add" attribute of an adapter.
87  */
88 static ssize_t
89 zfcp_sysfs_port_add_store(struct device *dev, const char *buf, size_t count)
90 {
91         wwn_t wwpn;
92         char *endp;
93         struct zfcp_adapter *adapter;
94         struct zfcp_port *port;
95         int retval = -EINVAL;
96
97         down(&zfcp_data.config_sema);
98
99         adapter = dev_get_drvdata(dev);
100         if (atomic_test_mask(ZFCP_STATUS_COMMON_REMOVE, &adapter->status)) {
101                 retval = -EBUSY;
102                 goto out;
103         }
104
105         wwpn = simple_strtoull(buf, &endp, 0);
106         if ((endp + 1) < (buf + count))
107                 goto out;
108
109         port = zfcp_port_enqueue(adapter, wwpn, 0, 0);
110         if (!port)
111                 goto out;
112
113         retval = 0;
114
115         zfcp_erp_port_reopen(port, 0);
116         zfcp_erp_wait(port->adapter);
117         zfcp_port_put(port);
118  out:
119         up(&zfcp_data.config_sema);
120         return retval ? retval : (ssize_t) count;
121 }
122
123 static DEVICE_ATTR(port_add, S_IWUSR, NULL, zfcp_sysfs_port_add_store);
124
125 /**
126  * zfcp_sysfs_port_remove_store - remove a port from sysfs tree
127  * @dev: pointer to belonging device
128  * @buf: pointer to input buffer
129  * @count: number of bytes in buffer
130  *
131  * Store function of the "port_remove" attribute of an adapter.
132  */
133 static ssize_t
134 zfcp_sysfs_port_remove_store(struct device *dev, const char *buf, size_t count)
135 {
136         struct zfcp_adapter *adapter;
137         struct zfcp_port *port;
138         wwn_t wwpn;
139         char *endp;
140         int retval = 0;
141
142         down(&zfcp_data.config_sema);
143
144         adapter = dev_get_drvdata(dev);
145         if (atomic_test_mask(ZFCP_STATUS_COMMON_REMOVE, &adapter->status)) {
146                 retval = -EBUSY;
147                 goto out;
148         }
149
150         wwpn = simple_strtoull(buf, &endp, 0);
151         if ((endp + 1) < (buf + count)) {
152                 retval = -EINVAL;
153                 goto out;
154         }
155
156         write_lock_irq(&zfcp_data.config_lock);
157         port = zfcp_get_port_by_wwpn(adapter, wwpn);
158         if (port && (atomic_read(&port->refcount) == 0)) {
159                 zfcp_port_get(port);
160                 atomic_set_mask(ZFCP_STATUS_COMMON_REMOVE, &port->status);
161                 list_move(&port->list, &adapter->port_remove_lh);
162         }
163         else {
164                 port = NULL;
165         }
166         write_unlock_irq(&zfcp_data.config_lock);
167
168         if (!port) {
169                 retval = -ENXIO;
170                 goto out;
171         }
172
173         zfcp_erp_port_shutdown(port, 0);
174         zfcp_erp_wait(adapter);
175         zfcp_port_put(port);
176         zfcp_port_dequeue(port);
177  out:
178         up(&zfcp_data.config_sema);
179         return retval ? retval : (ssize_t) count;
180 }
181
182 static DEVICE_ATTR(port_remove, S_IWUSR, NULL, zfcp_sysfs_port_remove_store);
183
184 /**
185  * zfcp_sysfs_adapter_failed_store - failed state of adapter
186  * @dev: pointer to belonging device
187  * @buf: pointer to input buffer
188  * @count: number of bytes in buffer
189  *
190  * Store function of the "failed" attribute of an adapter.
191  * If a "0" gets written to "failed", error recovery will be
192  * started for the belonging adapter.
193  */
194 static ssize_t
195 zfcp_sysfs_adapter_failed_store(struct device *dev,
196                                 const char *buf, size_t count)
197 {
198         struct zfcp_adapter *adapter;
199         unsigned int val;
200         char *endp;
201         int retval = 0;
202
203         down(&zfcp_data.config_sema);
204
205         adapter = dev_get_drvdata(dev);
206         if (atomic_test_mask(ZFCP_STATUS_COMMON_REMOVE, &adapter->status)) {
207                 retval = -EBUSY;
208                 goto out;
209         }
210
211         val = simple_strtoul(buf, &endp, 0);
212         if (((endp + 1) < (buf + count)) || (val != 0)) {
213                 retval = -EINVAL;
214                 goto out;
215         }
216
217         zfcp_erp_modify_adapter_status(adapter, ZFCP_STATUS_COMMON_RUNNING,
218                                        ZFCP_SET);
219         zfcp_erp_adapter_reopen(adapter, ZFCP_STATUS_COMMON_ERP_FAILED);
220         zfcp_erp_wait(adapter);
221  out:
222         up(&zfcp_data.config_sema);
223         return retval ? retval : (ssize_t) count;
224 }
225
226 /**
227  * zfcp_sysfs_adapter_failed_show - failed state of adapter
228  * @dev: pointer to belonging device
229  * @buf: pointer to input buffer
230  *
231  * Show function of "failed" attribute of adapter. Will be
232  * "0" if adapter is working, otherwise "1".
233  */
234 static ssize_t
235 zfcp_sysfs_adapter_failed_show(struct device *dev, char *buf)
236 {
237         struct zfcp_adapter *adapter;
238
239         adapter = dev_get_drvdata(dev);
240         if (atomic_test_mask(ZFCP_STATUS_COMMON_ERP_FAILED, &adapter->status))
241                 return sprintf(buf, "1\n");
242         else
243                 return sprintf(buf, "0\n");
244 }
245
246 static DEVICE_ATTR(failed, S_IWUSR | S_IRUGO, zfcp_sysfs_adapter_failed_show,
247                    zfcp_sysfs_adapter_failed_store);
248
249 static struct attribute *zfcp_adapter_attrs[] = {
250         &dev_attr_failed.attr,
251         &dev_attr_in_recovery.attr,
252         &dev_attr_port_remove.attr,
253         &dev_attr_port_add.attr,
254         &dev_attr_wwnn.attr,
255         &dev_attr_wwpn.attr,
256         &dev_attr_s_id.attr,
257         &dev_attr_card_version.attr,
258         &dev_attr_lic_version.attr,
259         &dev_attr_fc_link_speed.attr,
260         &dev_attr_fc_service_class.attr,
261         &dev_attr_fc_topology.attr,
262         &dev_attr_scsi_host_no.attr,
263         &dev_attr_status.attr,
264         &dev_attr_hardware_version.attr,
265         &dev_attr_serial_number.attr,
266         NULL
267 };
268
269 static struct attribute_group zfcp_adapter_attr_group = {
270         .attrs = zfcp_adapter_attrs,
271 };
272
273 /**
274  * zfcp_sysfs_create_adapter_files - create sysfs adapter files
275  * @dev: pointer to belonging device
276  *
277  * Create all attributes of the sysfs representation of an adapter.
278  */
279 int
280 zfcp_sysfs_adapter_create_files(struct device *dev)
281 {
282         return sysfs_create_group(&dev->kobj, &zfcp_adapter_attr_group);
283 }
284
285 /**
286  * zfcp_sysfs_remove_adapter_files - remove sysfs adapter files
287  * @dev: pointer to belonging device
288  *
289  * Remove all attributes of the sysfs representation of an adapter.
290  */
291 void
292 zfcp_sysfs_adapter_remove_files(struct device *dev)
293 {
294         sysfs_remove_group(&dev->kobj, &zfcp_adapter_attr_group);
295 }
296
297 #undef ZFCP_LOG_AREA