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