zfcp_sysfs_port.c 8.2 KB

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