zfcp_sysfs_adapter.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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_DEFINE_ADAPTER_ATTR
  25. * @_name: name of show attribute
  26. * @_format: format string
  27. * @_value: value to print
  28. *
  29. * Generates attributes for an adapter.
  30. */
  31. #define ZFCP_DEFINE_ADAPTER_ATTR(_name, _format, _value) \
  32. static ssize_t zfcp_sysfs_adapter_##_name##_show(struct device *dev, struct device_attribute *attr, \
  33. char *buf) \
  34. { \
  35. struct zfcp_adapter *adapter; \
  36. \
  37. adapter = dev_get_drvdata(dev); \
  38. return sprintf(buf, _format, _value); \
  39. } \
  40. \
  41. static DEVICE_ATTR(_name, S_IRUGO, zfcp_sysfs_adapter_##_name##_show, NULL);
  42. ZFCP_DEFINE_ADAPTER_ATTR(status, "0x%08x\n", atomic_read(&adapter->status));
  43. ZFCP_DEFINE_ADAPTER_ATTR(peer_wwnn, "0x%016llx\n", adapter->peer_wwnn);
  44. ZFCP_DEFINE_ADAPTER_ATTR(peer_wwpn, "0x%016llx\n", adapter->peer_wwpn);
  45. ZFCP_DEFINE_ADAPTER_ATTR(peer_d_id, "0x%06x\n", adapter->peer_d_id);
  46. ZFCP_DEFINE_ADAPTER_ATTR(card_version, "0x%04x\n", adapter->hydra_version);
  47. ZFCP_DEFINE_ADAPTER_ATTR(lic_version, "0x%08x\n", adapter->fsf_lic_version);
  48. ZFCP_DEFINE_ADAPTER_ATTR(hardware_version, "0x%08x\n",
  49. adapter->hardware_version);
  50. ZFCP_DEFINE_ADAPTER_ATTR(in_recovery, "%d\n", atomic_test_mask
  51. (ZFCP_STATUS_COMMON_ERP_INUSE, &adapter->status));
  52. /**
  53. * zfcp_sysfs_port_add_store - add a port to sysfs tree
  54. * @dev: pointer to belonging device
  55. * @buf: pointer to input buffer
  56. * @count: number of bytes in buffer
  57. *
  58. * Store function of the "port_add" attribute of an adapter.
  59. */
  60. static ssize_t
  61. zfcp_sysfs_port_add_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  62. {
  63. wwn_t wwpn;
  64. char *endp;
  65. struct zfcp_adapter *adapter;
  66. struct zfcp_port *port;
  67. int retval = -EINVAL;
  68. down(&zfcp_data.config_sema);
  69. adapter = dev_get_drvdata(dev);
  70. if (atomic_test_mask(ZFCP_STATUS_COMMON_REMOVE, &adapter->status)) {
  71. retval = -EBUSY;
  72. goto out;
  73. }
  74. wwpn = simple_strtoull(buf, &endp, 0);
  75. if ((endp + 1) < (buf + count))
  76. goto out;
  77. port = zfcp_port_enqueue(adapter, wwpn, 0, 0);
  78. if (!port)
  79. goto out;
  80. retval = 0;
  81. zfcp_erp_port_reopen(port, 0);
  82. zfcp_erp_wait(port->adapter);
  83. zfcp_port_put(port);
  84. out:
  85. up(&zfcp_data.config_sema);
  86. return retval ? retval : (ssize_t) count;
  87. }
  88. static DEVICE_ATTR(port_add, S_IWUSR, NULL, zfcp_sysfs_port_add_store);
  89. /**
  90. * zfcp_sysfs_port_remove_store - remove a port from sysfs tree
  91. * @dev: pointer to belonging device
  92. * @buf: pointer to input buffer
  93. * @count: number of bytes in buffer
  94. *
  95. * Store function of the "port_remove" attribute of an adapter.
  96. */
  97. static ssize_t
  98. zfcp_sysfs_port_remove_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  99. {
  100. struct zfcp_adapter *adapter;
  101. struct zfcp_port *port;
  102. wwn_t wwpn;
  103. char *endp;
  104. int retval = 0;
  105. down(&zfcp_data.config_sema);
  106. adapter = dev_get_drvdata(dev);
  107. if (atomic_test_mask(ZFCP_STATUS_COMMON_REMOVE, &adapter->status)) {
  108. retval = -EBUSY;
  109. goto out;
  110. }
  111. wwpn = simple_strtoull(buf, &endp, 0);
  112. if ((endp + 1) < (buf + count)) {
  113. retval = -EINVAL;
  114. goto out;
  115. }
  116. write_lock_irq(&zfcp_data.config_lock);
  117. port = zfcp_get_port_by_wwpn(adapter, wwpn);
  118. if (port && (atomic_read(&port->refcount) == 0)) {
  119. zfcp_port_get(port);
  120. atomic_set_mask(ZFCP_STATUS_COMMON_REMOVE, &port->status);
  121. list_move(&port->list, &adapter->port_remove_lh);
  122. }
  123. else {
  124. port = NULL;
  125. }
  126. write_unlock_irq(&zfcp_data.config_lock);
  127. if (!port) {
  128. retval = -ENXIO;
  129. goto out;
  130. }
  131. zfcp_erp_port_shutdown(port, 0);
  132. zfcp_erp_wait(adapter);
  133. zfcp_port_put(port);
  134. zfcp_port_dequeue(port);
  135. out:
  136. up(&zfcp_data.config_sema);
  137. return retval ? retval : (ssize_t) count;
  138. }
  139. static DEVICE_ATTR(port_remove, S_IWUSR, NULL, zfcp_sysfs_port_remove_store);
  140. /**
  141. * zfcp_sysfs_adapter_failed_store - failed state of adapter
  142. * @dev: pointer to belonging device
  143. * @buf: pointer to input buffer
  144. * @count: number of bytes in buffer
  145. *
  146. * Store function of the "failed" attribute of an adapter.
  147. * If a "0" gets written to "failed", error recovery will be
  148. * started for the belonging adapter.
  149. */
  150. static ssize_t
  151. zfcp_sysfs_adapter_failed_store(struct device *dev, struct device_attribute *attr,
  152. const char *buf, size_t count)
  153. {
  154. struct zfcp_adapter *adapter;
  155. unsigned int val;
  156. char *endp;
  157. int retval = 0;
  158. down(&zfcp_data.config_sema);
  159. adapter = dev_get_drvdata(dev);
  160. if (atomic_test_mask(ZFCP_STATUS_COMMON_REMOVE, &adapter->status)) {
  161. retval = -EBUSY;
  162. goto out;
  163. }
  164. val = simple_strtoul(buf, &endp, 0);
  165. if (((endp + 1) < (buf + count)) || (val != 0)) {
  166. retval = -EINVAL;
  167. goto out;
  168. }
  169. zfcp_erp_modify_adapter_status(adapter, ZFCP_STATUS_COMMON_RUNNING,
  170. ZFCP_SET);
  171. zfcp_erp_adapter_reopen(adapter, ZFCP_STATUS_COMMON_ERP_FAILED);
  172. zfcp_erp_wait(adapter);
  173. out:
  174. up(&zfcp_data.config_sema);
  175. return retval ? retval : (ssize_t) count;
  176. }
  177. /**
  178. * zfcp_sysfs_adapter_failed_show - failed state of adapter
  179. * @dev: pointer to belonging device
  180. * @buf: pointer to input buffer
  181. *
  182. * Show function of "failed" attribute of adapter. Will be
  183. * "0" if adapter is working, otherwise "1".
  184. */
  185. static ssize_t
  186. zfcp_sysfs_adapter_failed_show(struct device *dev, struct device_attribute *attr, char *buf)
  187. {
  188. struct zfcp_adapter *adapter;
  189. adapter = dev_get_drvdata(dev);
  190. if (atomic_test_mask(ZFCP_STATUS_COMMON_ERP_FAILED, &adapter->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_adapter_failed_show,
  196. zfcp_sysfs_adapter_failed_store);
  197. static struct attribute *zfcp_adapter_attrs[] = {
  198. &dev_attr_failed.attr,
  199. &dev_attr_in_recovery.attr,
  200. &dev_attr_port_remove.attr,
  201. &dev_attr_port_add.attr,
  202. &dev_attr_peer_wwnn.attr,
  203. &dev_attr_peer_wwpn.attr,
  204. &dev_attr_peer_d_id.attr,
  205. &dev_attr_card_version.attr,
  206. &dev_attr_lic_version.attr,
  207. &dev_attr_status.attr,
  208. &dev_attr_hardware_version.attr,
  209. NULL
  210. };
  211. static struct attribute_group zfcp_adapter_attr_group = {
  212. .attrs = zfcp_adapter_attrs,
  213. };
  214. /**
  215. * zfcp_sysfs_create_adapter_files - create sysfs adapter files
  216. * @dev: pointer to belonging device
  217. *
  218. * Create all attributes of the sysfs representation of an adapter.
  219. */
  220. int
  221. zfcp_sysfs_adapter_create_files(struct device *dev)
  222. {
  223. return sysfs_create_group(&dev->kobj, &zfcp_adapter_attr_group);
  224. }
  225. /**
  226. * zfcp_sysfs_remove_adapter_files - remove sysfs adapter files
  227. * @dev: pointer to belonging device
  228. *
  229. * Remove all attributes of the sysfs representation of an adapter.
  230. */
  231. void
  232. zfcp_sysfs_adapter_remove_files(struct device *dev)
  233. {
  234. sysfs_remove_group(&dev->kobj, &zfcp_adapter_attr_group);
  235. }
  236. #undef ZFCP_LOG_AREA