Merge branch 'origin'
[pandora-kernel.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  *      Andreas Herrmann <aherrman@de.ibm.com>
14  *      Volker Sameske <sameske@de.ibm.com>
15  *
16  * This program is free software; you can redistribute it and/or modify
17  * it under the terms of the GNU General Public License as published by
18  * the Free Software Foundation; either version 2, or (at your option)
19  * any later version.
20  *
21  * This program is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24  * GNU General Public License for more details.
25  *
26  * You should have received a copy of the GNU General Public License
27  * along with this program; if not, write to the Free Software
28  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29  */
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, struct device_attribute *attr,        \
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(in_recovery, "%d\n", atomic_test_mask
67                       (ZFCP_STATUS_COMMON_ERP_INUSE, &port->status));
68 ZFCP_DEFINE_PORT_ATTR(access_denied, "%d\n", atomic_test_mask
69                       (ZFCP_STATUS_COMMON_ACCESS_DENIED, &port->status));
70
71 /**
72  * zfcp_sysfs_unit_add_store - add a unit to sysfs tree
73  * @dev: pointer to belonging device
74  * @buf: pointer to input buffer
75  * @count: number of bytes in buffer
76  *
77  * Store function of the "unit_add" attribute of a port.
78  */
79 static ssize_t
80 zfcp_sysfs_unit_add_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
81 {
82         fcp_lun_t fcp_lun;
83         char *endp;
84         struct zfcp_port *port;
85         struct zfcp_unit *unit;
86         int retval = -EINVAL;
87
88         down(&zfcp_data.config_sema);
89
90         port = dev_get_drvdata(dev);
91         if (atomic_test_mask(ZFCP_STATUS_COMMON_REMOVE, &port->status)) {
92                 retval = -EBUSY;
93                 goto out;
94         }
95
96         fcp_lun = simple_strtoull(buf, &endp, 0);
97         if ((endp + 1) < (buf + count))
98                 goto out;
99
100         unit = zfcp_unit_enqueue(port, fcp_lun);
101         if (!unit)
102                 goto out;
103
104         retval = 0;
105
106         zfcp_erp_unit_reopen(unit, 0);
107         zfcp_erp_wait(unit->port->adapter);
108         zfcp_unit_put(unit);
109  out:
110         up(&zfcp_data.config_sema);
111         return retval ? retval : (ssize_t) count;
112 }
113
114 static DEVICE_ATTR(unit_add, S_IWUSR, NULL, zfcp_sysfs_unit_add_store);
115
116 /**
117  * zfcp_sysfs_unit_remove_store - remove a unit from sysfs tree
118  * @dev: pointer to belonging device
119  * @buf: pointer to input buffer
120  * @count: number of bytes in buffer
121  */
122 static ssize_t
123 zfcp_sysfs_unit_remove_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
124 {
125         struct zfcp_port *port;
126         struct zfcp_unit *unit;
127         fcp_lun_t fcp_lun;
128         char *endp;
129         int retval = 0;
130
131         down(&zfcp_data.config_sema);
132
133         port = dev_get_drvdata(dev);
134         if (atomic_test_mask(ZFCP_STATUS_COMMON_REMOVE, &port->status)) {
135                 retval = -EBUSY;
136                 goto out;
137         }
138
139         fcp_lun = 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         unit = zfcp_get_unit_by_lun(port, fcp_lun);
147         if (unit && (atomic_read(&unit->refcount) == 0)) {
148                 zfcp_unit_get(unit);
149                 atomic_set_mask(ZFCP_STATUS_COMMON_REMOVE, &unit->status);
150                 list_move(&unit->list, &port->unit_remove_lh);
151         }
152         else {
153                 unit = NULL;
154         }
155         write_unlock_irq(&zfcp_data.config_lock);
156
157         if (!unit) {
158                 retval = -ENXIO;
159                 goto out;
160         }
161
162         zfcp_erp_unit_shutdown(unit, 0);
163         zfcp_erp_wait(unit->port->adapter);
164         zfcp_unit_put(unit);
165         zfcp_unit_dequeue(unit);
166  out:
167         up(&zfcp_data.config_sema);
168         return retval ? retval : (ssize_t) count;
169 }
170
171 static DEVICE_ATTR(unit_remove, S_IWUSR, NULL, zfcp_sysfs_unit_remove_store);
172
173 /**
174  * zfcp_sysfs_port_failed_store - failed state of port
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 a port.
180  * If a "0" gets written to "failed", error recovery will be
181  * started for the belonging port.
182  */
183 static ssize_t
184 zfcp_sysfs_port_failed_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
185 {
186         struct zfcp_port *port;
187         unsigned int val;
188         char *endp;
189         int retval = 0;
190
191         down(&zfcp_data.config_sema);
192
193         port = dev_get_drvdata(dev);
194         if (atomic_test_mask(ZFCP_STATUS_COMMON_REMOVE, &port->status)) {
195                 retval = -EBUSY;
196                 goto out;
197         }
198
199         val = simple_strtoul(buf, &endp, 0);
200         if (((endp + 1) < (buf + count)) || (val != 0)) {
201                 retval = -EINVAL;
202                 goto out;
203         }
204
205         zfcp_erp_modify_port_status(port, ZFCP_STATUS_COMMON_RUNNING, ZFCP_SET);
206         zfcp_erp_port_reopen(port, ZFCP_STATUS_COMMON_ERP_FAILED);
207         zfcp_erp_wait(port->adapter);
208  out:
209         up(&zfcp_data.config_sema);
210         return retval ? retval : (ssize_t) count;
211 }
212
213 /**
214  * zfcp_sysfs_port_failed_show - failed state of port
215  * @dev: pointer to belonging device
216  * @buf: pointer to input buffer
217  *
218  * Show function of "failed" attribute of port. Will be
219  * "0" if port is working, otherwise "1".
220  */
221 static ssize_t
222 zfcp_sysfs_port_failed_show(struct device *dev, struct device_attribute *attr, char *buf)
223 {
224         struct zfcp_port *port;
225
226         port = dev_get_drvdata(dev);
227         if (atomic_test_mask(ZFCP_STATUS_COMMON_ERP_FAILED, &port->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_port_failed_show,
234                    zfcp_sysfs_port_failed_store);
235
236 /**
237  * zfcp_port_common_attrs
238  * sysfs attributes that are common for all kind of fc ports.
239  */
240 static struct attribute *zfcp_port_common_attrs[] = {
241         &dev_attr_failed.attr,
242         &dev_attr_in_recovery.attr,
243         &dev_attr_status.attr,
244         &dev_attr_access_denied.attr,
245         NULL
246 };
247
248 static struct attribute_group zfcp_port_common_attr_group = {
249         .attrs = zfcp_port_common_attrs,
250 };
251
252 /**
253  * zfcp_port_no_ns_attrs
254  * sysfs attributes not to be used for nameserver ports.
255  */
256 static struct attribute *zfcp_port_no_ns_attrs[] = {
257         &dev_attr_unit_add.attr,
258         &dev_attr_unit_remove.attr,
259         NULL
260 };
261
262 static struct attribute_group zfcp_port_no_ns_attr_group = {
263         .attrs = zfcp_port_no_ns_attrs,
264 };
265
266 /**
267  * zfcp_sysfs_port_create_files - create sysfs port files
268  * @dev: pointer to belonging device
269  *
270  * Create all attributes of the sysfs representation of a port.
271  */
272 int
273 zfcp_sysfs_port_create_files(struct device *dev, u32 flags)
274 {
275         int retval;
276
277         retval = sysfs_create_group(&dev->kobj, &zfcp_port_common_attr_group);
278
279         if ((flags & ZFCP_STATUS_PORT_WKA) || retval)
280                 return retval;
281
282         retval = sysfs_create_group(&dev->kobj, &zfcp_port_no_ns_attr_group);
283         if (retval)
284                 sysfs_remove_group(&dev->kobj, &zfcp_port_common_attr_group);
285
286         return retval;
287 }
288
289 /**
290  * zfcp_sysfs_port_remove_files - remove sysfs port files
291  * @dev: pointer to belonging device
292  *
293  * Remove all attributes of the sysfs representation of a port.
294  */
295 void
296 zfcp_sysfs_port_remove_files(struct device *dev, u32 flags)
297 {
298         sysfs_remove_group(&dev->kobj, &zfcp_port_common_attr_group);
299         if (!(flags & ZFCP_STATUS_PORT_WKA))
300                 sysfs_remove_group(&dev->kobj, &zfcp_port_no_ns_attr_group);
301 }
302
303 #undef ZFCP_LOG_AREA