zfcp_sysfs_port.c 8.3 KB

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