zfcp_ccw.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /*
  2. * zfcp device driver
  3. *
  4. * Registration and callback for the s390 common I/O layer.
  5. *
  6. * Copyright IBM Corporation 2002, 2009
  7. */
  8. #define KMSG_COMPONENT "zfcp"
  9. #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
  10. #include "zfcp_ext.h"
  11. #define ZFCP_MODEL_PRIV 0x4
  12. static DEFINE_SPINLOCK(zfcp_ccw_adapter_ref_lock);
  13. struct zfcp_adapter *zfcp_ccw_adapter_by_cdev(struct ccw_device *cdev)
  14. {
  15. struct zfcp_adapter *adapter;
  16. unsigned long flags;
  17. spin_lock_irqsave(&zfcp_ccw_adapter_ref_lock, flags);
  18. adapter = dev_get_drvdata(&cdev->dev);
  19. if (adapter)
  20. kref_get(&adapter->ref);
  21. spin_unlock_irqrestore(&zfcp_ccw_adapter_ref_lock, flags);
  22. return adapter;
  23. }
  24. void zfcp_ccw_adapter_put(struct zfcp_adapter *adapter)
  25. {
  26. unsigned long flags;
  27. spin_lock_irqsave(&zfcp_ccw_adapter_ref_lock, flags);
  28. kref_put(&adapter->ref, zfcp_adapter_release);
  29. spin_unlock_irqrestore(&zfcp_ccw_adapter_ref_lock, flags);
  30. }
  31. static int zfcp_ccw_suspend(struct ccw_device *cdev)
  32. {
  33. struct zfcp_adapter *adapter = zfcp_ccw_adapter_by_cdev(cdev);
  34. if (!adapter)
  35. return 0;
  36. zfcp_erp_adapter_shutdown(adapter, 0, "ccsusp1", NULL);
  37. zfcp_erp_wait(adapter);
  38. zfcp_ccw_adapter_put(adapter);
  39. return 0;
  40. }
  41. static int zfcp_ccw_activate(struct ccw_device *cdev)
  42. {
  43. struct zfcp_adapter *adapter = zfcp_ccw_adapter_by_cdev(cdev);
  44. if (!adapter)
  45. return 0;
  46. zfcp_erp_modify_adapter_status(adapter, "ccresu1", NULL,
  47. ZFCP_STATUS_COMMON_RUNNING, ZFCP_SET);
  48. zfcp_erp_adapter_reopen(adapter, ZFCP_STATUS_COMMON_ERP_FAILED,
  49. "ccresu2", NULL);
  50. zfcp_erp_wait(adapter);
  51. flush_work(&adapter->scan_work);
  52. zfcp_ccw_adapter_put(adapter);
  53. return 0;
  54. }
  55. static struct ccw_device_id zfcp_ccw_device_id[] = {
  56. { CCW_DEVICE_DEVTYPE(0x1731, 0x3, 0x1732, 0x3) },
  57. { CCW_DEVICE_DEVTYPE(0x1731, 0x3, 0x1732, ZFCP_MODEL_PRIV) },
  58. {},
  59. };
  60. MODULE_DEVICE_TABLE(ccw, zfcp_ccw_device_id);
  61. /**
  62. * zfcp_ccw_priv_sch - check if subchannel is privileged
  63. * @adapter: Adapter/Subchannel to check
  64. */
  65. int zfcp_ccw_priv_sch(struct zfcp_adapter *adapter)
  66. {
  67. return adapter->ccw_device->id.dev_model == ZFCP_MODEL_PRIV;
  68. }
  69. /**
  70. * zfcp_ccw_probe - probe function of zfcp driver
  71. * @cdev: pointer to belonging ccw device
  72. *
  73. * This function gets called by the common i/o layer for each FCP
  74. * device found on the current system. This is only a stub to make cio
  75. * work: To only allocate adapter resources for devices actually used,
  76. * the allocation is deferred to the first call to ccw_set_online.
  77. */
  78. static int zfcp_ccw_probe(struct ccw_device *cdev)
  79. {
  80. return 0;
  81. }
  82. /**
  83. * zfcp_ccw_remove - remove function of zfcp driver
  84. * @cdev: pointer to belonging ccw device
  85. *
  86. * This function gets called by the common i/o layer and removes an adapter
  87. * from the system. Task of this function is to get rid of all units and
  88. * ports that belong to this adapter. And in addition all resources of this
  89. * adapter will be freed too.
  90. */
  91. static void zfcp_ccw_remove(struct ccw_device *cdev)
  92. {
  93. struct zfcp_adapter *adapter;
  94. struct zfcp_port *port, *p;
  95. struct zfcp_unit *unit, *u;
  96. LIST_HEAD(unit_remove_lh);
  97. LIST_HEAD(port_remove_lh);
  98. ccw_device_set_offline(cdev);
  99. adapter = zfcp_ccw_adapter_by_cdev(cdev);
  100. if (!adapter)
  101. return;
  102. write_lock_irq(&adapter->port_list_lock);
  103. list_for_each_entry_safe(port, p, &adapter->port_list, list) {
  104. write_lock(&port->unit_list_lock);
  105. list_for_each_entry_safe(unit, u, &port->unit_list, list) {
  106. atomic_set_mask(ZFCP_STATUS_COMMON_REMOVE,
  107. &unit->status);
  108. list_move(&unit->list, &unit_remove_lh);
  109. }
  110. write_unlock(&port->unit_list_lock);
  111. atomic_set_mask(ZFCP_STATUS_COMMON_REMOVE, &port->status);
  112. list_move(&port->list, &port_remove_lh);
  113. }
  114. atomic_set_mask(ZFCP_STATUS_COMMON_REMOVE, &adapter->status);
  115. write_unlock_irq(&adapter->port_list_lock);
  116. zfcp_ccw_adapter_put(adapter); /* put from zfcp_ccw_adapter_by_cdev */
  117. list_for_each_entry_safe(unit, u, &unit_remove_lh, list)
  118. zfcp_device_unregister(&unit->sysfs_device,
  119. &zfcp_sysfs_unit_attrs);
  120. list_for_each_entry_safe(port, p, &port_remove_lh, list)
  121. zfcp_device_unregister(&port->sysfs_device,
  122. &zfcp_sysfs_port_attrs);
  123. zfcp_adapter_unregister(adapter);
  124. }
  125. /**
  126. * zfcp_ccw_set_online - set_online function of zfcp driver
  127. * @cdev: pointer to belonging ccw device
  128. *
  129. * This function gets called by the common i/o layer and sets an
  130. * adapter into state online. The first call will allocate all
  131. * adapter resources that will be retained until the device is removed
  132. * via zfcp_ccw_remove.
  133. *
  134. * Setting an fcp device online means that it will be registered with
  135. * the SCSI stack, that the QDIO queues will be set up and that the
  136. * adapter will be opened.
  137. */
  138. static int zfcp_ccw_set_online(struct ccw_device *cdev)
  139. {
  140. struct zfcp_adapter *adapter = zfcp_ccw_adapter_by_cdev(cdev);
  141. if (!adapter) {
  142. adapter = zfcp_adapter_enqueue(cdev);
  143. if (IS_ERR(adapter)) {
  144. dev_err(&cdev->dev,
  145. "Setting up data structures for the "
  146. "FCP adapter failed\n");
  147. return PTR_ERR(adapter);
  148. }
  149. kref_get(&adapter->ref);
  150. }
  151. /* initialize request counter */
  152. BUG_ON(!zfcp_reqlist_isempty(adapter));
  153. adapter->req_no = 0;
  154. zfcp_erp_modify_adapter_status(adapter, "ccsonl1", NULL,
  155. ZFCP_STATUS_COMMON_RUNNING, ZFCP_SET);
  156. zfcp_erp_adapter_reopen(adapter, ZFCP_STATUS_COMMON_ERP_FAILED,
  157. "ccsonl2", NULL);
  158. zfcp_erp_wait(adapter);
  159. flush_work(&adapter->scan_work);
  160. zfcp_ccw_adapter_put(adapter);
  161. return 0;
  162. }
  163. /**
  164. * zfcp_ccw_set_offline - set_offline function of zfcp driver
  165. * @cdev: pointer to belonging ccw device
  166. *
  167. * This function gets called by the common i/o layer and sets an adapter
  168. * into state offline.
  169. */
  170. static int zfcp_ccw_set_offline(struct ccw_device *cdev)
  171. {
  172. struct zfcp_adapter *adapter = zfcp_ccw_adapter_by_cdev(cdev);
  173. if (!adapter)
  174. return 0;
  175. zfcp_erp_adapter_shutdown(adapter, 0, "ccsoff1", NULL);
  176. zfcp_erp_wait(adapter);
  177. zfcp_ccw_adapter_put(adapter);
  178. return 0;
  179. }
  180. /**
  181. * zfcp_ccw_notify - ccw notify function
  182. * @cdev: pointer to belonging ccw device
  183. * @event: indicates if adapter was detached or attached
  184. *
  185. * This function gets called by the common i/o layer if an adapter has gone
  186. * or reappeared.
  187. */
  188. static int zfcp_ccw_notify(struct ccw_device *cdev, int event)
  189. {
  190. struct zfcp_adapter *adapter = zfcp_ccw_adapter_by_cdev(cdev);
  191. if (!adapter)
  192. return 1;
  193. switch (event) {
  194. case CIO_GONE:
  195. dev_warn(&cdev->dev, "The FCP device has been detached\n");
  196. zfcp_erp_adapter_shutdown(adapter, 0, "ccnoti1", NULL);
  197. break;
  198. case CIO_NO_PATH:
  199. dev_warn(&cdev->dev,
  200. "The CHPID for the FCP device is offline\n");
  201. zfcp_erp_adapter_shutdown(adapter, 0, "ccnoti2", NULL);
  202. break;
  203. case CIO_OPER:
  204. dev_info(&cdev->dev, "The FCP device is operational again\n");
  205. zfcp_erp_modify_adapter_status(adapter, "ccnoti3", NULL,
  206. ZFCP_STATUS_COMMON_RUNNING,
  207. ZFCP_SET);
  208. zfcp_erp_adapter_reopen(adapter, ZFCP_STATUS_COMMON_ERP_FAILED,
  209. "ccnoti4", NULL);
  210. break;
  211. case CIO_BOXED:
  212. dev_warn(&cdev->dev, "The FCP device did not respond within "
  213. "the specified time\n");
  214. zfcp_erp_adapter_shutdown(adapter, 0, "ccnoti5", NULL);
  215. break;
  216. }
  217. zfcp_ccw_adapter_put(adapter);
  218. return 1;
  219. }
  220. /**
  221. * zfcp_ccw_shutdown - handle shutdown from cio
  222. * @cdev: device for adapter to shutdown.
  223. */
  224. static void zfcp_ccw_shutdown(struct ccw_device *cdev)
  225. {
  226. struct zfcp_adapter *adapter = zfcp_ccw_adapter_by_cdev(cdev);
  227. if (!adapter)
  228. return;
  229. zfcp_erp_adapter_shutdown(adapter, 0, "ccshut1", NULL);
  230. zfcp_erp_wait(adapter);
  231. zfcp_erp_thread_kill(adapter);
  232. zfcp_ccw_adapter_put(adapter);
  233. }
  234. struct ccw_driver zfcp_ccw_driver = {
  235. .owner = THIS_MODULE,
  236. .name = "zfcp",
  237. .ids = zfcp_ccw_device_id,
  238. .probe = zfcp_ccw_probe,
  239. .remove = zfcp_ccw_remove,
  240. .set_online = zfcp_ccw_set_online,
  241. .set_offline = zfcp_ccw_set_offline,
  242. .notify = zfcp_ccw_notify,
  243. .shutdown = zfcp_ccw_shutdown,
  244. .freeze = zfcp_ccw_suspend,
  245. .thaw = zfcp_ccw_activate,
  246. .restore = zfcp_ccw_activate,
  247. };
  248. /**
  249. * zfcp_ccw_register - ccw register function
  250. *
  251. * Registers the driver at the common i/o layer. This function will be called
  252. * at module load time/system start.
  253. */
  254. int __init zfcp_ccw_register(void)
  255. {
  256. return ccw_driver_register(&zfcp_ccw_driver);
  257. }