zfcp_sysfs_port.c 8.5 KB

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