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