zfcp_sysfs_port.c 8.4 KB

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