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