Fedora kernel-2.6.17-1.2142_FC4 patched with stable patch-2.6.17.4-vs2.0.2-rc26.diff
[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  *      Andreas Herrmann <aherrman@de.ibm.com>
14  *
15  * This program is free software; you can redistribute it and/or modify
16  * it under the terms of the GNU General Public License as published by
17  * the Free Software Foundation; either version 2, or (at your option)
18  * any later version.
19  *
20  * This program is distributed in the hope that it will be useful,
21  * but WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23  * GNU General Public License for more details.
24  *
25  * You should have received a copy of the GNU General Public License
26  * along with this program; if not, write to the Free Software
27  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28  */
29
30 #include "zfcp_ext.h"
31
32 #define ZFCP_LOG_AREA                   ZFCP_LOG_AREA_CONFIG
33
34 /**
35  * ZFCP_DEFINE_ADAPTER_ATTR
36  * @_name:   name of show attribute
37  * @_format: format string
38  * @_value:  value to print
39  *
40  * Generates attributes for an adapter.
41  */
42 #define ZFCP_DEFINE_ADAPTER_ATTR(_name, _format, _value)                      \
43 static ssize_t zfcp_sysfs_adapter_##_name##_show(struct device *dev, struct device_attribute *attr,          \
44                                                  char *buf)                   \
45 {                                                                             \
46         struct zfcp_adapter *adapter;                                         \
47                                                                               \
48         adapter = dev_get_drvdata(dev);                                       \
49         return sprintf(buf, _format, _value);                                 \
50 }                                                                             \
51                                                                               \
52 static DEVICE_ATTR(_name, S_IRUGO, zfcp_sysfs_adapter_##_name##_show, NULL);
53
54 ZFCP_DEFINE_ADAPTER_ATTR(status, "0x%08x\n", atomic_read(&adapter->status));
55 ZFCP_DEFINE_ADAPTER_ATTR(peer_wwnn, "0x%016llx\n", adapter->peer_wwnn);
56 ZFCP_DEFINE_ADAPTER_ATTR(peer_wwpn, "0x%016llx\n", adapter->peer_wwpn);
57 ZFCP_DEFINE_ADAPTER_ATTR(peer_d_id, "0x%06x\n", adapter->peer_d_id);
58 ZFCP_DEFINE_ADAPTER_ATTR(card_version, "0x%04x\n", adapter->hydra_version);
59 ZFCP_DEFINE_ADAPTER_ATTR(lic_version, "0x%08x\n", adapter->fsf_lic_version);
60 ZFCP_DEFINE_ADAPTER_ATTR(hardware_version, "0x%08x\n",
61                          adapter->hardware_version);
62 ZFCP_DEFINE_ADAPTER_ATTR(in_recovery, "%d\n", atomic_test_mask
63                          (ZFCP_STATUS_COMMON_ERP_INUSE, &adapter->status));
64
65 /**
66  * zfcp_sysfs_port_add_store - add a port to sysfs tree
67  * @dev: pointer to belonging device
68  * @buf: pointer to input buffer
69  * @count: number of bytes in buffer
70  *
71  * Store function of the "port_add" attribute of an adapter.
72  */
73 static ssize_t
74 zfcp_sysfs_port_add_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
75 {
76         wwn_t wwpn;
77         char *endp;
78         struct zfcp_adapter *adapter;
79         struct zfcp_port *port;
80         int retval = -EINVAL;
81
82         down(&zfcp_data.config_sema);
83
84         adapter = dev_get_drvdata(dev);
85         if (atomic_test_mask(ZFCP_STATUS_COMMON_REMOVE, &adapter->status)) {
86                 retval = -EBUSY;
87                 goto out;
88         }
89
90         wwpn = simple_strtoull(buf, &endp, 0);
91         if ((endp + 1) < (buf + count))
92                 goto out;
93
94         port = zfcp_port_enqueue(adapter, wwpn, 0, 0);
95         if (!port)
96                 goto out;
97
98         retval = 0;
99
100         zfcp_erp_port_reopen(port, 0);
101         zfcp_erp_wait(port->adapter);
102         zfcp_port_put(port);
103  out:
104         up(&zfcp_data.config_sema);
105         return retval ? retval : (ssize_t) count;
106 }
107
108 static DEVICE_ATTR(port_add, S_IWUSR, NULL, zfcp_sysfs_port_add_store);
109
110 /**
111  * zfcp_sysfs_port_remove_store - remove a port from sysfs tree
112  * @dev: pointer to belonging device
113  * @buf: pointer to input buffer
114  * @count: number of bytes in buffer
115  *
116  * Store function of the "port_remove" attribute of an adapter.
117  */
118 static ssize_t
119 zfcp_sysfs_port_remove_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
120 {
121         struct zfcp_adapter *adapter;
122         struct zfcp_port *port;
123         wwn_t wwpn;
124         char *endp;
125         int retval = 0;
126
127         down(&zfcp_data.config_sema);
128
129         adapter = dev_get_drvdata(dev);
130         if (atomic_test_mask(ZFCP_STATUS_COMMON_REMOVE, &adapter->status)) {
131                 retval = -EBUSY;
132                 goto out;
133         }
134
135         wwpn = simple_strtoull(buf, &endp, 0);
136         if ((endp + 1) < (buf + count)) {
137                 retval = -EINVAL;
138                 goto out;
139         }
140
141         write_lock_irq(&zfcp_data.config_lock);
142         port = zfcp_get_port_by_wwpn(adapter, wwpn);
143         if (port && (atomic_read(&port->refcount) == 0)) {
144                 zfcp_port_get(port);
145                 atomic_set_mask(ZFCP_STATUS_COMMON_REMOVE, &port->status);
146                 list_move(&port->list, &adapter->port_remove_lh);
147         }
148         else {
149                 port = NULL;
150         }
151         write_unlock_irq(&zfcp_data.config_lock);
152
153         if (!port) {
154                 retval = -ENXIO;
155                 goto out;
156         }
157
158         zfcp_erp_port_shutdown(port, 0);
159         zfcp_erp_wait(adapter);
160         zfcp_port_put(port);
161         zfcp_port_dequeue(port);
162  out:
163         up(&zfcp_data.config_sema);
164         return retval ? retval : (ssize_t) count;
165 }
166
167 static DEVICE_ATTR(port_remove, S_IWUSR, NULL, zfcp_sysfs_port_remove_store);
168
169 /**
170  * zfcp_sysfs_adapter_failed_store - failed state of adapter
171  * @dev: pointer to belonging device
172  * @buf: pointer to input buffer
173  * @count: number of bytes in buffer
174  *
175  * Store function of the "failed" attribute of an adapter.
176  * If a "0" gets written to "failed", error recovery will be
177  * started for the belonging adapter.
178  */
179 static ssize_t
180 zfcp_sysfs_adapter_failed_store(struct device *dev, struct device_attribute *attr,
181                                 const char *buf, size_t count)
182 {
183         struct zfcp_adapter *adapter;
184         unsigned int val;
185         char *endp;
186         int retval = 0;
187
188         down(&zfcp_data.config_sema);
189
190         adapter = dev_get_drvdata(dev);
191         if (atomic_test_mask(ZFCP_STATUS_COMMON_REMOVE, &adapter->status)) {
192                 retval = -EBUSY;
193                 goto out;
194         }
195
196         val = simple_strtoul(buf, &endp, 0);
197         if (((endp + 1) < (buf + count)) || (val != 0)) {
198                 retval = -EINVAL;
199                 goto out;
200         }
201
202         zfcp_erp_modify_adapter_status(adapter, ZFCP_STATUS_COMMON_RUNNING,
203                                        ZFCP_SET);
204         zfcp_erp_adapter_reopen(adapter, ZFCP_STATUS_COMMON_ERP_FAILED);
205         zfcp_erp_wait(adapter);
206  out:
207         up(&zfcp_data.config_sema);
208         return retval ? retval : (ssize_t) count;
209 }
210
211 /**
212  * zfcp_sysfs_adapter_failed_show - failed state of adapter
213  * @dev: pointer to belonging device
214  * @buf: pointer to input buffer
215  *
216  * Show function of "failed" attribute of adapter. Will be
217  * "0" if adapter is working, otherwise "1".
218  */
219 static ssize_t
220 zfcp_sysfs_adapter_failed_show(struct device *dev, struct device_attribute *attr, char *buf)
221 {
222         struct zfcp_adapter *adapter;
223
224         adapter = dev_get_drvdata(dev);
225         if (atomic_test_mask(ZFCP_STATUS_COMMON_ERP_FAILED, &adapter->status))
226                 return sprintf(buf, "1\n");
227         else
228                 return sprintf(buf, "0\n");
229 }
230
231 static DEVICE_ATTR(failed, S_IWUSR | S_IRUGO, zfcp_sysfs_adapter_failed_show,
232                    zfcp_sysfs_adapter_failed_store);
233
234 static struct attribute *zfcp_adapter_attrs[] = {
235         &dev_attr_failed.attr,
236         &dev_attr_in_recovery.attr,
237         &dev_attr_port_remove.attr,
238         &dev_attr_port_add.attr,
239         &dev_attr_peer_wwnn.attr,
240         &dev_attr_peer_wwpn.attr,
241         &dev_attr_peer_d_id.attr,
242         &dev_attr_card_version.attr,
243         &dev_attr_lic_version.attr,
244         &dev_attr_status.attr,
245         &dev_attr_hardware_version.attr,
246         NULL
247 };
248
249 static struct attribute_group zfcp_adapter_attr_group = {
250         .attrs = zfcp_adapter_attrs,
251 };
252
253 /**
254  * zfcp_sysfs_create_adapter_files - create sysfs adapter files
255  * @dev: pointer to belonging device
256  *
257  * Create all attributes of the sysfs representation of an adapter.
258  */
259 int
260 zfcp_sysfs_adapter_create_files(struct device *dev)
261 {
262         return sysfs_create_group(&dev->kobj, &zfcp_adapter_attr_group);
263 }
264
265 /**
266  * zfcp_sysfs_remove_adapter_files - remove sysfs adapter files
267  * @dev: pointer to belonging device
268  *
269  * Remove all attributes of the sysfs representation of an adapter.
270  */
271 void
272 zfcp_sysfs_adapter_remove_files(struct device *dev)
273 {
274         sysfs_remove_group(&dev->kobj, &zfcp_adapter_attr_group);
275 }
276
277 #undef ZFCP_LOG_AREA