zfcp_ccw.c 9.6 KB

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