zfcp_ccw.c 9.6 KB

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