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