VServer 1.9.2 (patch-2.6.8.1-vs1.9.2.diff)
[linux-2.6.git] / drivers / s390 / scsi / zfcp_sysfs_port.c
1 /*
2  * linux/drivers/s390/scsi/zfcp_sysfs_port.c
3  *
4  * FCP adapter driver for IBM eServer zSeries
5  *
6  * sysfs port 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_PORT_C_REVISION "$Revision: 1.41 $"
30
31 #include "zfcp_ext.h"
32
33 #define ZFCP_LOG_AREA                   ZFCP_LOG_AREA_CONFIG
34
35 /**
36  * zfcp_sysfs_port_release - gets called when a struct device port is released
37  * @dev: pointer to belonging device
38  */
39 void
40 zfcp_sysfs_port_release(struct device *dev)
41 {
42         kfree(dev);
43 }
44
45 /**
46  * ZFCP_DEFINE_PORT_ATTR
47  * @_name:   name of show attribute
48  * @_format: format string
49  * @_value:  value to print
50  *
51  * Generates attributes for a port.
52  */
53 #define ZFCP_DEFINE_PORT_ATTR(_name, _format, _value)                    \
54 static ssize_t zfcp_sysfs_port_##_name##_show(struct device *dev,        \
55                                               char *buf)                 \
56 {                                                                        \
57         struct zfcp_port *port;                                          \
58                                                                          \
59         port = dev_get_drvdata(dev);                                     \
60         return sprintf(buf, _format, _value);                            \
61 }                                                                        \
62                                                                          \
63 static DEVICE_ATTR(_name, S_IRUGO, zfcp_sysfs_port_##_name##_show, NULL);
64
65 ZFCP_DEFINE_PORT_ATTR(status, "0x%08x\n", atomic_read(&port->status));
66 ZFCP_DEFINE_PORT_ATTR(wwnn, "0x%016llx\n", port->wwnn);
67 ZFCP_DEFINE_PORT_ATTR(d_id, "0x%06x\n", port->d_id);
68 ZFCP_DEFINE_PORT_ATTR(scsi_id, "0x%x\n", port->scsi_id);
69
70 /**
71  * zfcp_sysfs_unit_add_store - add a unit to sysfs tree
72  * @dev: pointer to belonging device
73  * @buf: pointer to input buffer
74  * @count: number of bytes in buffer
75  *
76  * Store function of the "unit_add" attribute of a port.
77  */
78 static ssize_t
79 zfcp_sysfs_unit_add_store(struct device *dev, const char *buf, size_t count)
80 {
81         fcp_lun_t fcp_lun;
82         char *endp;
83         struct zfcp_port *port;
84         struct zfcp_unit *unit;
85         int retval = -EINVAL;
86
87         down(&zfcp_data.config_sema);
88
89         port = dev_get_drvdata(dev);
90         if (atomic_test_mask(ZFCP_STATUS_COMMON_REMOVE, &port->status)) {
91                 retval = -EBUSY;
92                 goto out;
93         }
94
95         fcp_lun = simple_strtoull(buf, &endp, 0);
96         if ((endp + 1) < (buf + count))
97                 goto out;
98
99         unit = zfcp_unit_enqueue(port, fcp_lun);
100         if (!unit)
101                 goto out;
102
103         retval = 0;
104
105         zfcp_erp_unit_reopen(unit, 0);
106         zfcp_erp_wait(unit->port->adapter);
107         zfcp_unit_put(unit);
108  out:
109         up(&zfcp_data.config_sema);
110         return retval ? retval : count;
111 }
112
113 static DEVICE_ATTR(unit_add, S_IWUSR, NULL, zfcp_sysfs_unit_add_store);
114
115 /**
116  * zfcp_sysfs_unit_remove_store - remove a unit from sysfs tree
117  * @dev: pointer to belonging device
118  * @buf: pointer to input buffer
119  * @count: number of bytes in buffer
120  */
121 static ssize_t
122 zfcp_sysfs_unit_remove_store(struct device *dev, const char *buf, size_t count)
123 {
124         struct zfcp_port *port;
125         struct zfcp_unit *unit;
126         fcp_lun_t fcp_lun;
127         char *endp;
128         int retval = 0;
129
130         down(&zfcp_data.config_sema);
131
132         port = dev_get_drvdata(dev);
133         if (atomic_test_mask(ZFCP_STATUS_COMMON_REMOVE, &port->status)) {
134                 retval = -EBUSY;
135                 goto out;
136         }
137
138         fcp_lun = simple_strtoull(buf, &endp, 0);
139         if ((endp + 1) < (buf + count)) {
140                 retval = -EINVAL;
141                 goto out;
142         }
143
144         write_lock_irq(&zfcp_data.config_lock);
145         unit = zfcp_get_unit_by_lun(port, fcp_lun);
146         if (unit && (atomic_read(&unit->refcount) == 0)) {
147                 zfcp_unit_get(unit);
148                 atomic_set_mask(ZFCP_STATUS_COMMON_REMOVE, &unit->status);
149                 list_move(&unit->list, &port->unit_remove_lh);
150         }
151         else {
152                 unit = NULL;
153         }
154         write_unlock_irq(&zfcp_data.config_lock);
155
156         if (!unit) {
157                 retval = -ENXIO;
158                 goto out;
159         }
160
161         zfcp_erp_unit_shutdown(unit, 0);
162         zfcp_erp_wait(unit->port->adapter);
163         zfcp_unit_put(unit);
164         zfcp_unit_dequeue(unit);
165  out:
166         up(&zfcp_data.config_sema);
167         return retval ? retval : count;
168 }
169
170 static DEVICE_ATTR(unit_remove, S_IWUSR, NULL, zfcp_sysfs_unit_remove_store);
171
172 /**
173  * zfcp_sysfs_port_failed_store - failed state of port
174  * @dev: pointer to belonging device
175  * @buf: pointer to input buffer
176  * @count: number of bytes in buffer
177  *
178  * Store function of the "failed" attribute of a port.
179  * If a "0" gets written to "failed", error recovery will be
180  * started for the belonging port.
181  */
182 static ssize_t
183 zfcp_sysfs_port_failed_store(struct device *dev, const char *buf, size_t count)
184 {
185         struct zfcp_port *port;
186         unsigned int val;
187         char *endp;
188         int retval = 0;
189
190         down(&zfcp_data.config_sema);
191
192         port = dev_get_drvdata(dev);
193         if (atomic_test_mask(ZFCP_STATUS_COMMON_REMOVE, &port->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_port_status(port, ZFCP_STATUS_COMMON_RUNNING, ZFCP_SET);
205         zfcp_erp_port_reopen(port, ZFCP_STATUS_COMMON_ERP_FAILED);
206         zfcp_erp_wait(port->adapter);
207  out:
208         up(&zfcp_data.config_sema);
209         return retval ? retval : count;
210 }
211
212 /**
213  * zfcp_sysfs_port_failed_show - failed state of port
214  * @dev: pointer to belonging device
215  * @buf: pointer to input buffer
216  *
217  * Show function of "failed" attribute of port. Will be
218  * "0" if port is working, otherwise "1".
219  */
220 static ssize_t
221 zfcp_sysfs_port_failed_show(struct device *dev, char *buf)
222 {
223         struct zfcp_port *port;
224
225         port = dev_get_drvdata(dev);
226         if (atomic_test_mask(ZFCP_STATUS_COMMON_ERP_FAILED, &port->status))
227                 return sprintf(buf, "1\n");
228         else
229                 return sprintf(buf, "0\n");
230 }
231
232 static DEVICE_ATTR(failed, S_IWUSR | S_IRUGO, zfcp_sysfs_port_failed_show,
233                    zfcp_sysfs_port_failed_store);
234
235 /**
236  * zfcp_sysfs_port_in_recovery_show - recovery state of port
237  * @dev: pointer to belonging device
238  * @buf: pointer to input buffer
239  * 
240  * Show function of "in_recovery" attribute of port. Will be
241  * "0" if no error recovery is pending for port, otherwise "1".
242  */
243 static ssize_t
244 zfcp_sysfs_port_in_recovery_show(struct device *dev, char *buf)
245 {
246         struct zfcp_port *port;
247
248         port = dev_get_drvdata(dev);
249         if (atomic_test_mask(ZFCP_STATUS_COMMON_ERP_INUSE, &port->status))
250                 return sprintf(buf, "1\n");
251         else
252                 return sprintf(buf, "0\n");
253 }
254
255 static DEVICE_ATTR(in_recovery, S_IRUGO, zfcp_sysfs_port_in_recovery_show,
256                    NULL);
257
258 /**
259  * zfcp_port_common_attrs
260  * sysfs attributes that are common for all kind of fc ports.
261  */
262 static struct attribute *zfcp_port_common_attrs[] = {
263         &dev_attr_failed.attr,
264         &dev_attr_in_recovery.attr,
265         &dev_attr_status.attr,
266         &dev_attr_wwnn.attr,
267         &dev_attr_d_id.attr,
268         NULL
269 };
270
271 static struct attribute_group zfcp_port_common_attr_group = {
272         .attrs = zfcp_port_common_attrs,
273 };
274
275 /**
276  * zfcp_port_no_ns_attrs
277  * sysfs attributes not to be used for nameserver ports.
278  */
279 static struct attribute *zfcp_port_no_ns_attrs[] = {
280         &dev_attr_unit_add.attr,
281         &dev_attr_unit_remove.attr,
282         &dev_attr_scsi_id.attr,
283         NULL
284 };
285
286 static struct attribute_group zfcp_port_no_ns_attr_group = {
287         .attrs = zfcp_port_no_ns_attrs,
288 };
289
290 /**
291  * zfcp_sysfs_port_create_files - create sysfs port files
292  * @dev: pointer to belonging device
293  *
294  * Create all attributes of the sysfs representation of a port.
295  */
296 int
297 zfcp_sysfs_port_create_files(struct device *dev, u32 flags)
298 {
299         int retval;
300
301         retval = sysfs_create_group(&dev->kobj, &zfcp_port_common_attr_group);
302
303         if ((flags & ZFCP_STATUS_PORT_NAMESERVER) || retval)
304                 return retval;
305
306         retval = sysfs_create_group(&dev->kobj, &zfcp_port_no_ns_attr_group);
307         if (retval)
308                 sysfs_remove_group(&dev->kobj, &zfcp_port_common_attr_group);
309
310         return retval;
311 }
312
313 /**
314  * zfcp_sysfs_port_remove_files - remove sysfs port files
315  * @dev: pointer to belonging device
316  *
317  * Remove all attributes of the sysfs representation of a port.
318  */
319 void
320 zfcp_sysfs_port_remove_files(struct device *dev, u32 flags)
321 {
322         sysfs_remove_group(&dev->kobj, &zfcp_port_common_attr_group);
323         if (!(flags & ZFCP_STATUS_PORT_NAMESERVER))
324                 sysfs_remove_group(&dev->kobj, &zfcp_port_no_ns_attr_group);
325 }
326
327 #undef ZFCP_LOG_AREA