zfcp_ccw.c 8.7 KB

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