Pull bugzilla-5764 into release branch
[linux-2.6/verdex.git] / drivers / s390 / scsi / zfcp_sysfs_port.c
blobf401d42db21c26674fb147b599a4aabfa71cf5ac
1 /*
2 * linux/drivers/s390/scsi/zfcp_sysfs_port.c
4 * FCP adapter driver for IBM eServer zSeries
6 * sysfs port related routines
8 * (C) Copyright IBM Corp. 2003, 2004
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>
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.
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.
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.
31 #include "zfcp_ext.h"
33 #define ZFCP_LOG_AREA ZFCP_LOG_AREA_CONFIG
35 /**
36 * zfcp_sysfs_port_release - gets called when a struct device port is released
37 * @dev: pointer to belonging device
39 void
40 zfcp_sysfs_port_release(struct device *dev)
42 kfree(dev);
45 /**
46 * ZFCP_DEFINE_PORT_ATTR
47 * @_name: name of show attribute
48 * @_format: format string
49 * @_value: value to print
51 * Generates attributes for a port.
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; \
59 port = dev_get_drvdata(dev); \
60 return sprintf(buf, _format, _value); \
61 } \
63 static DEVICE_ATTR(_name, S_IRUGO, zfcp_sysfs_port_##_name##_show, NULL);
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));
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
77 * Store function of the "unit_add" attribute of a port.
79 static ssize_t
80 zfcp_sysfs_unit_add_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
82 fcp_lun_t fcp_lun;
83 char *endp;
84 struct zfcp_port *port;
85 struct zfcp_unit *unit;
86 int retval = -EINVAL;
88 down(&zfcp_data.config_sema);
90 port = dev_get_drvdata(dev);
91 if (atomic_test_mask(ZFCP_STATUS_COMMON_REMOVE, &port->status)) {
92 retval = -EBUSY;
93 goto out;
96 fcp_lun = simple_strtoull(buf, &endp, 0);
97 if ((endp + 1) < (buf + count))
98 goto out;
100 unit = zfcp_unit_enqueue(port, fcp_lun);
101 if (!unit)
102 goto out;
104 retval = 0;
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;
114 static DEVICE_ATTR(unit_add, S_IWUSR, NULL, zfcp_sysfs_unit_add_store);
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
122 static ssize_t
123 zfcp_sysfs_unit_remove_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
125 struct zfcp_port *port;
126 struct zfcp_unit *unit;
127 fcp_lun_t fcp_lun;
128 char *endp;
129 int retval = 0;
131 down(&zfcp_data.config_sema);
133 port = dev_get_drvdata(dev);
134 if (atomic_test_mask(ZFCP_STATUS_COMMON_REMOVE, &port->status)) {
135 retval = -EBUSY;
136 goto out;
139 fcp_lun = simple_strtoull(buf, &endp, 0);
140 if ((endp + 1) < (buf + count)) {
141 retval = -EINVAL;
142 goto out;
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);
152 else {
153 unit = NULL;
155 write_unlock_irq(&zfcp_data.config_lock);
157 if (!unit) {
158 retval = -ENXIO;
159 goto out;
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;
171 static DEVICE_ATTR(unit_remove, S_IWUSR, NULL, zfcp_sysfs_unit_remove_store);
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
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.
183 static ssize_t
184 zfcp_sysfs_port_failed_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
186 struct zfcp_port *port;
187 unsigned int val;
188 char *endp;
189 int retval = 0;
191 down(&zfcp_data.config_sema);
193 port = dev_get_drvdata(dev);
194 if (atomic_test_mask(ZFCP_STATUS_COMMON_REMOVE, &port->status)) {
195 retval = -EBUSY;
196 goto out;
199 val = simple_strtoul(buf, &endp, 0);
200 if (((endp + 1) < (buf + count)) || (val != 0)) {
201 retval = -EINVAL;
202 goto out;
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;
214 * zfcp_sysfs_port_failed_show - failed state of port
215 * @dev: pointer to belonging device
216 * @buf: pointer to input buffer
218 * Show function of "failed" attribute of port. Will be
219 * "0" if port is working, otherwise "1".
221 static ssize_t
222 zfcp_sysfs_port_failed_show(struct device *dev, struct device_attribute *attr, char *buf)
224 struct zfcp_port *port;
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");
233 static DEVICE_ATTR(failed, S_IWUSR | S_IRUGO, zfcp_sysfs_port_failed_show,
234 zfcp_sysfs_port_failed_store);
237 * zfcp_port_common_attrs
238 * sysfs attributes that are common for all kind of fc ports.
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
248 static struct attribute_group zfcp_port_common_attr_group = {
249 .attrs = zfcp_port_common_attrs,
253 * zfcp_port_no_ns_attrs
254 * sysfs attributes not to be used for nameserver ports.
256 static struct attribute *zfcp_port_no_ns_attrs[] = {
257 &dev_attr_unit_add.attr,
258 &dev_attr_unit_remove.attr,
259 NULL
262 static struct attribute_group zfcp_port_no_ns_attr_group = {
263 .attrs = zfcp_port_no_ns_attrs,
267 * zfcp_sysfs_port_create_files - create sysfs port files
268 * @dev: pointer to belonging device
270 * Create all attributes of the sysfs representation of a port.
273 zfcp_sysfs_port_create_files(struct device *dev, u32 flags)
275 int retval;
277 retval = sysfs_create_group(&dev->kobj, &zfcp_port_common_attr_group);
279 if ((flags & ZFCP_STATUS_PORT_WKA) || retval)
280 return retval;
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);
286 return retval;
290 * zfcp_sysfs_port_remove_files - remove sysfs port files
291 * @dev: pointer to belonging device
293 * Remove all attributes of the sysfs representation of a port.
295 void
296 zfcp_sysfs_port_remove_files(struct device *dev, u32 flags)
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);
303 #undef ZFCP_LOG_AREA