zfcp_sysfs_port.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /*
  2. * This file is part of the zfcp device driver for
  3. * FCP adapters for IBM System z9 and zSeries.
  4. *
  5. * (C) Copyright IBM Corp. 2002, 2006
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2, or (at your option)
  10. * any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21. #include "zfcp_ext.h"
  22. #define ZFCP_LOG_AREA ZFCP_LOG_AREA_CONFIG
  23. /**
  24. * zfcp_sysfs_port_release - gets called when a struct device port is released
  25. * @dev: pointer to belonging device
  26. */
  27. void
  28. zfcp_sysfs_port_release(struct device *dev)
  29. {
  30. kfree(dev);
  31. }
  32. /**
  33. * ZFCP_DEFINE_PORT_ATTR
  34. * @_name: name of show attribute
  35. * @_format: format string
  36. * @_value: value to print
  37. *
  38. * Generates attributes for a port.
  39. */
  40. #define ZFCP_DEFINE_PORT_ATTR(_name, _format, _value) \
  41. static ssize_t zfcp_sysfs_port_##_name##_show(struct device *dev, struct device_attribute *attr, \
  42. char *buf) \
  43. { \
  44. struct zfcp_port *port; \
  45. \
  46. port = dev_get_drvdata(dev); \
  47. return sprintf(buf, _format, _value); \
  48. } \
  49. \
  50. static DEVICE_ATTR(_name, S_IRUGO, zfcp_sysfs_port_##_name##_show, NULL);
  51. ZFCP_DEFINE_PORT_ATTR(status, "0x%08x\n", atomic_read(&port->status));
  52. ZFCP_DEFINE_PORT_ATTR(in_recovery, "%d\n", atomic_test_mask
  53. (ZFCP_STATUS_COMMON_ERP_INUSE, &port->status));
  54. ZFCP_DEFINE_PORT_ATTR(access_denied, "%d\n", atomic_test_mask
  55. (ZFCP_STATUS_COMMON_ACCESS_DENIED, &port->status));
  56. /**
  57. * zfcp_sysfs_unit_add_store - add a unit to sysfs tree
  58. * @dev: pointer to belonging device
  59. * @buf: pointer to input buffer
  60. * @count: number of bytes in buffer
  61. *
  62. * Store function of the "unit_add" attribute of a port.
  63. */
  64. static ssize_t
  65. zfcp_sysfs_unit_add_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  66. {
  67. fcp_lun_t fcp_lun;
  68. char *endp;
  69. struct zfcp_port *port;
  70. struct zfcp_unit *unit;
  71. int retval = -EINVAL;
  72. down(&zfcp_data.config_sema);
  73. port = dev_get_drvdata(dev);
  74. if (atomic_test_mask(ZFCP_STATUS_COMMON_REMOVE, &port->status)) {
  75. retval = -EBUSY;
  76. goto out;
  77. }
  78. fcp_lun = simple_strtoull(buf, &endp, 0);
  79. if ((endp + 1) < (buf + count))
  80. goto out;
  81. unit = zfcp_unit_enqueue(port, fcp_lun);
  82. if (!unit)
  83. goto out;
  84. retval = 0;
  85. zfcp_erp_unit_reopen(unit, 0);
  86. zfcp_erp_wait(unit->port->adapter);
  87. zfcp_unit_put(unit);
  88. out:
  89. up(&zfcp_data.config_sema);
  90. return retval ? retval : (ssize_t) count;
  91. }
  92. static DEVICE_ATTR(unit_add, S_IWUSR, NULL, zfcp_sysfs_unit_add_store);
  93. /**
  94. * zfcp_sysfs_unit_remove_store - remove a unit from sysfs tree
  95. * @dev: pointer to belonging device
  96. * @buf: pointer to input buffer
  97. * @count: number of bytes in buffer
  98. */
  99. static ssize_t
  100. zfcp_sysfs_unit_remove_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  101. {
  102. struct zfcp_port *port;
  103. struct zfcp_unit *unit;
  104. fcp_lun_t fcp_lun;
  105. char *endp;
  106. int retval = 0;
  107. down(&zfcp_data.config_sema);
  108. port = dev_get_drvdata(dev);
  109. if (atomic_test_mask(ZFCP_STATUS_COMMON_REMOVE, &port->status)) {
  110. retval = -EBUSY;
  111. goto out;
  112. }
  113. fcp_lun = simple_strtoull(buf, &endp, 0);
  114. if ((endp + 1) < (buf + count)) {
  115. retval = -EINVAL;
  116. goto out;
  117. }
  118. write_lock_irq(&zfcp_data.config_lock);
  119. unit = zfcp_get_unit_by_lun(port, fcp_lun);
  120. if (unit && (atomic_read(&unit->refcount) == 0)) {
  121. zfcp_unit_get(unit);
  122. atomic_set_mask(ZFCP_STATUS_COMMON_REMOVE, &unit->status);
  123. list_move(&unit->list, &port->unit_remove_lh);
  124. }
  125. else {
  126. unit = NULL;
  127. }
  128. write_unlock_irq(&zfcp_data.config_lock);
  129. if (!unit) {
  130. retval = -ENXIO;
  131. goto out;
  132. }
  133. zfcp_erp_unit_shutdown(unit, 0);
  134. zfcp_erp_wait(unit->port->adapter);
  135. zfcp_unit_put(unit);
  136. zfcp_unit_dequeue(unit);
  137. out:
  138. up(&zfcp_data.config_sema);
  139. return retval ? retval : (ssize_t) count;
  140. }
  141. static DEVICE_ATTR(unit_remove, S_IWUSR, NULL, zfcp_sysfs_unit_remove_store);
  142. /**
  143. * zfcp_sysfs_port_failed_store - failed state of port
  144. * @dev: pointer to belonging device
  145. * @buf: pointer to input buffer
  146. * @count: number of bytes in buffer
  147. *
  148. * Store function of the "failed" attribute of a port.
  149. * If a "0" gets written to "failed", error recovery will be
  150. * started for the belonging port.
  151. */
  152. static ssize_t
  153. zfcp_sysfs_port_failed_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  154. {
  155. struct zfcp_port *port;
  156. unsigned int val;
  157. char *endp;
  158. int retval = 0;
  159. down(&zfcp_data.config_sema);
  160. port = dev_get_drvdata(dev);
  161. if (atomic_test_mask(ZFCP_STATUS_COMMON_REMOVE, &port->status)) {
  162. retval = -EBUSY;
  163. goto out;
  164. }
  165. val = simple_strtoul(buf, &endp, 0);
  166. if (((endp + 1) < (buf + count)) || (val != 0)) {
  167. retval = -EINVAL;
  168. goto out;
  169. }
  170. zfcp_erp_modify_port_status(port, ZFCP_STATUS_COMMON_RUNNING, ZFCP_SET);
  171. zfcp_erp_port_reopen(port, ZFCP_STATUS_COMMON_ERP_FAILED);
  172. zfcp_erp_wait(port->adapter);
  173. out:
  174. up(&zfcp_data.config_sema);
  175. return retval ? retval : (ssize_t) count;
  176. }
  177. /**
  178. * zfcp_sysfs_port_failed_show - failed state of port
  179. * @dev: pointer to belonging device
  180. * @buf: pointer to input buffer
  181. *
  182. * Show function of "failed" attribute of port. Will be
  183. * "0" if port is working, otherwise "1".
  184. */
  185. static ssize_t
  186. zfcp_sysfs_port_failed_show(struct device *dev, struct device_attribute *attr, char *buf)
  187. {
  188. struct zfcp_port *port;
  189. port = dev_get_drvdata(dev);
  190. if (atomic_test_mask(ZFCP_STATUS_COMMON_ERP_FAILED, &port->status))
  191. return sprintf(buf, "1\n");
  192. else
  193. return sprintf(buf, "0\n");
  194. }
  195. static DEVICE_ATTR(failed, S_IWUSR | S_IRUGO, zfcp_sysfs_port_failed_show,
  196. zfcp_sysfs_port_failed_store);
  197. /**
  198. * zfcp_port_common_attrs
  199. * sysfs attributes that are common for all kind of fc ports.
  200. */
  201. static struct attribute *zfcp_port_common_attrs[] = {
  202. &dev_attr_failed.attr,
  203. &dev_attr_in_recovery.attr,
  204. &dev_attr_status.attr,
  205. &dev_attr_access_denied.attr,
  206. NULL
  207. };
  208. static struct attribute_group zfcp_port_common_attr_group = {
  209. .attrs = zfcp_port_common_attrs,
  210. };
  211. /**
  212. * zfcp_port_no_ns_attrs
  213. * sysfs attributes not to be used for nameserver ports.
  214. */
  215. static struct attribute *zfcp_port_no_ns_attrs[] = {
  216. &dev_attr_unit_add.attr,
  217. &dev_attr_unit_remove.attr,
  218. NULL
  219. };
  220. static struct attribute_group zfcp_port_no_ns_attr_group = {
  221. .attrs = zfcp_port_no_ns_attrs,
  222. };
  223. /**
  224. * zfcp_sysfs_port_create_files - create sysfs port files
  225. * @dev: pointer to belonging device
  226. *
  227. * Create all attributes of the sysfs representation of a port.
  228. */
  229. int
  230. zfcp_sysfs_port_create_files(struct device *dev, u32 flags)
  231. {
  232. int retval;
  233. retval = sysfs_create_group(&dev->kobj, &zfcp_port_common_attr_group);
  234. if ((flags & ZFCP_STATUS_PORT_WKA) || retval)
  235. return retval;
  236. retval = sysfs_create_group(&dev->kobj, &zfcp_port_no_ns_attr_group);
  237. if (retval)
  238. sysfs_remove_group(&dev->kobj, &zfcp_port_common_attr_group);
  239. return retval;
  240. }
  241. /**
  242. * zfcp_sysfs_port_remove_files - remove sysfs port files
  243. * @dev: pointer to belonging device
  244. *
  245. * Remove all attributes of the sysfs representation of a port.
  246. */
  247. void
  248. zfcp_sysfs_port_remove_files(struct device *dev, u32 flags)
  249. {
  250. sysfs_remove_group(&dev->kobj, &zfcp_port_common_attr_group);
  251. if (!(flags & ZFCP_STATUS_PORT_WKA))
  252. sysfs_remove_group(&dev->kobj, &zfcp_port_no_ns_attr_group);
  253. }
  254. #undef ZFCP_LOG_AREA