zfcp_ccw.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /*
  2. * This file is part of the zfcp device driver for
  3. * FCP adapters for IBM System z9 and zSeries.
  4. *
  5. * (C) Copyright IBM Corp. 2002, 2006
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2, or (at your option)
  10. * any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21. #include "zfcp_ext.h"
  22. #define ZFCP_LOG_AREA ZFCP_LOG_AREA_CONFIG
  23. static int zfcp_ccw_probe(struct ccw_device *);
  24. static void zfcp_ccw_remove(struct ccw_device *);
  25. static int zfcp_ccw_set_online(struct ccw_device *);
  26. static int zfcp_ccw_set_offline(struct ccw_device *);
  27. static int zfcp_ccw_notify(struct ccw_device *, int);
  28. static void zfcp_ccw_shutdown(struct device *);
  29. static struct ccw_device_id zfcp_ccw_device_id[] = {
  30. {CCW_DEVICE_DEVTYPE(ZFCP_CONTROL_UNIT_TYPE,
  31. ZFCP_CONTROL_UNIT_MODEL,
  32. ZFCP_DEVICE_TYPE,
  33. ZFCP_DEVICE_MODEL)},
  34. {CCW_DEVICE_DEVTYPE(ZFCP_CONTROL_UNIT_TYPE,
  35. ZFCP_CONTROL_UNIT_MODEL,
  36. ZFCP_DEVICE_TYPE,
  37. ZFCP_DEVICE_MODEL_PRIV)},
  38. {},
  39. };
  40. static struct ccw_driver zfcp_ccw_driver = {
  41. .owner = THIS_MODULE,
  42. .name = ZFCP_NAME,
  43. .ids = zfcp_ccw_device_id,
  44. .probe = zfcp_ccw_probe,
  45. .remove = zfcp_ccw_remove,
  46. .set_online = zfcp_ccw_set_online,
  47. .set_offline = zfcp_ccw_set_offline,
  48. .notify = zfcp_ccw_notify,
  49. .driver = {
  50. .shutdown = zfcp_ccw_shutdown,
  51. },
  52. };
  53. MODULE_DEVICE_TABLE(ccw, zfcp_ccw_device_id);
  54. /**
  55. * zfcp_ccw_probe - probe function of zfcp driver
  56. * @ccw_device: pointer to belonging ccw device
  57. *
  58. * This function gets called by the common i/o layer and sets up the initial
  59. * data structures for each fcp adapter, which was detected by the system.
  60. * Also the sysfs files for this adapter will be created by this function.
  61. * In addition the nameserver port will be added to the ports of the adapter
  62. * and its sysfs representation will be created too.
  63. */
  64. static int
  65. zfcp_ccw_probe(struct ccw_device *ccw_device)
  66. {
  67. struct zfcp_adapter *adapter;
  68. int retval = 0;
  69. down(&zfcp_data.config_sema);
  70. adapter = zfcp_adapter_enqueue(ccw_device);
  71. if (!adapter)
  72. retval = -EINVAL;
  73. else
  74. ZFCP_LOG_DEBUG("Probed adapter %s\n",
  75. zfcp_get_busid_by_adapter(adapter));
  76. up(&zfcp_data.config_sema);
  77. return retval;
  78. }
  79. /**
  80. * zfcp_ccw_remove - remove function of zfcp driver
  81. * @ccw_device: pointer to belonging ccw device
  82. *
  83. * This function gets called by the common i/o layer and removes an adapter
  84. * from the system. Task of this function is to get rid of all units and
  85. * ports that belong to this adapter. And in addition all resources of this
  86. * adapter will be freed too.
  87. */
  88. static void
  89. zfcp_ccw_remove(struct ccw_device *ccw_device)
  90. {
  91. struct zfcp_adapter *adapter;
  92. struct zfcp_port *port, *p;
  93. struct zfcp_unit *unit, *u;
  94. ccw_device_set_offline(ccw_device);
  95. down(&zfcp_data.config_sema);
  96. adapter = dev_get_drvdata(&ccw_device->dev);
  97. ZFCP_LOG_DEBUG("Removing adapter %s\n",
  98. zfcp_get_busid_by_adapter(adapter));
  99. write_lock_irq(&zfcp_data.config_lock);
  100. list_for_each_entry_safe(port, p, &adapter->port_list_head, list) {
  101. list_for_each_entry_safe(unit, u, &port->unit_list_head, list) {
  102. list_move(&unit->list, &port->unit_remove_lh);
  103. atomic_set_mask(ZFCP_STATUS_COMMON_REMOVE,
  104. &unit->status);
  105. }
  106. list_move(&port->list, &adapter->port_remove_lh);
  107. atomic_set_mask(ZFCP_STATUS_COMMON_REMOVE, &port->status);
  108. }
  109. atomic_set_mask(ZFCP_STATUS_COMMON_REMOVE, &adapter->status);
  110. write_unlock_irq(&zfcp_data.config_lock);
  111. list_for_each_entry_safe(port, p, &adapter->port_remove_lh, list) {
  112. list_for_each_entry_safe(unit, u, &port->unit_remove_lh, list) {
  113. zfcp_unit_dequeue(unit);
  114. }
  115. zfcp_port_dequeue(port);
  116. }
  117. zfcp_adapter_wait(adapter);
  118. zfcp_adapter_dequeue(adapter);
  119. up(&zfcp_data.config_sema);
  120. }
  121. /**
  122. * zfcp_ccw_set_online - set_online function of zfcp driver
  123. * @ccw_device: pointer to belonging ccw device
  124. *
  125. * This function gets called by the common i/o layer and sets an adapter
  126. * into state online. Setting an fcp device online means that it will be
  127. * registered with the SCSI stack, that the QDIO queues will be set up
  128. * and that the adapter will be opened (asynchronously).
  129. */
  130. static int
  131. zfcp_ccw_set_online(struct ccw_device *ccw_device)
  132. {
  133. struct zfcp_adapter *adapter;
  134. int retval;
  135. down(&zfcp_data.config_sema);
  136. adapter = dev_get_drvdata(&ccw_device->dev);
  137. retval = zfcp_adapter_debug_register(adapter);
  138. if (retval)
  139. goto out;
  140. retval = zfcp_erp_thread_setup(adapter);
  141. if (retval) {
  142. ZFCP_LOG_INFO("error: start of error recovery thread for "
  143. "adapter %s failed\n",
  144. zfcp_get_busid_by_adapter(adapter));
  145. goto out_erp_thread;
  146. }
  147. retval = zfcp_adapter_scsi_register(adapter);
  148. if (retval)
  149. goto out_scsi_register;
  150. /* initialize request counter */
  151. BUG_ON(!zfcp_reqlist_isempty(adapter));
  152. adapter->req_no = 0;
  153. zfcp_erp_modify_adapter_status(adapter, ZFCP_STATUS_COMMON_RUNNING,
  154. ZFCP_SET);
  155. zfcp_erp_adapter_reopen(adapter, ZFCP_STATUS_COMMON_ERP_FAILED);
  156. zfcp_erp_wait(adapter);
  157. goto out;
  158. out_scsi_register:
  159. zfcp_erp_thread_kill(adapter);
  160. out_erp_thread:
  161. zfcp_adapter_debug_unregister(adapter);
  162. out:
  163. up(&zfcp_data.config_sema);
  164. return retval;
  165. }
  166. /**
  167. * zfcp_ccw_set_offline - set_offline function of zfcp driver
  168. * @ccw_device: pointer to belonging ccw device
  169. *
  170. * This function gets called by the common i/o layer and sets an adapter
  171. * into state offline. Setting an fcp device offline means that it will be
  172. * unregistered from the SCSI stack and that the adapter will be shut down
  173. * asynchronously.
  174. */
  175. static int
  176. zfcp_ccw_set_offline(struct ccw_device *ccw_device)
  177. {
  178. struct zfcp_adapter *adapter;
  179. down(&zfcp_data.config_sema);
  180. adapter = dev_get_drvdata(&ccw_device->dev);
  181. zfcp_erp_adapter_shutdown(adapter, 0);
  182. zfcp_erp_wait(adapter);
  183. zfcp_adapter_scsi_unregister(adapter);
  184. zfcp_erp_thread_kill(adapter);
  185. zfcp_adapter_debug_unregister(adapter);
  186. up(&zfcp_data.config_sema);
  187. return 0;
  188. }
  189. /**
  190. * zfcp_ccw_notify
  191. * @ccw_device: pointer to belonging ccw device
  192. * @event: indicates if adapter was detached or attached
  193. *
  194. * This function gets called by the common i/o layer if an adapter has gone
  195. * or reappeared.
  196. */
  197. static int
  198. zfcp_ccw_notify(struct ccw_device *ccw_device, int event)
  199. {
  200. struct zfcp_adapter *adapter;
  201. down(&zfcp_data.config_sema);
  202. adapter = dev_get_drvdata(&ccw_device->dev);
  203. switch (event) {
  204. case CIO_GONE:
  205. ZFCP_LOG_NORMAL("adapter %s: device gone\n",
  206. zfcp_get_busid_by_adapter(adapter));
  207. debug_text_event(adapter->erp_dbf,1,"dev_gone");
  208. zfcp_erp_adapter_shutdown(adapter, 0);
  209. break;
  210. case CIO_NO_PATH:
  211. ZFCP_LOG_NORMAL("adapter %s: no path\n",
  212. zfcp_get_busid_by_adapter(adapter));
  213. debug_text_event(adapter->erp_dbf,1,"no_path");
  214. zfcp_erp_adapter_shutdown(adapter, 0);
  215. break;
  216. case CIO_OPER:
  217. ZFCP_LOG_NORMAL("adapter %s: operational again\n",
  218. zfcp_get_busid_by_adapter(adapter));
  219. debug_text_event(adapter->erp_dbf,1,"dev_oper");
  220. zfcp_erp_modify_adapter_status(adapter,
  221. ZFCP_STATUS_COMMON_RUNNING,
  222. ZFCP_SET);
  223. zfcp_erp_adapter_reopen(adapter,
  224. ZFCP_STATUS_COMMON_ERP_FAILED);
  225. break;
  226. }
  227. zfcp_erp_wait(adapter);
  228. up(&zfcp_data.config_sema);
  229. return 1;
  230. }
  231. /**
  232. * zfcp_ccw_register - ccw register function
  233. *
  234. * Registers the driver at the common i/o layer. This function will be called
  235. * at module load time/system start.
  236. */
  237. int __init
  238. zfcp_ccw_register(void)
  239. {
  240. int retval;
  241. retval = ccw_driver_register(&zfcp_ccw_driver);
  242. if (retval)
  243. goto out;
  244. retval = zfcp_sysfs_driver_create_files(&zfcp_ccw_driver.driver);
  245. if (retval)
  246. ccw_driver_unregister(&zfcp_ccw_driver);
  247. out:
  248. return retval;
  249. }
  250. /**
  251. * zfcp_ccw_shutdown - gets called on reboot/shutdown
  252. *
  253. * Makes sure that QDIO queues are down when the system gets stopped.
  254. */
  255. static void
  256. zfcp_ccw_shutdown(struct device *dev)
  257. {
  258. struct zfcp_adapter *adapter;
  259. down(&zfcp_data.config_sema);
  260. adapter = dev_get_drvdata(dev);
  261. zfcp_erp_adapter_shutdown(adapter, 0);
  262. zfcp_erp_wait(adapter);
  263. up(&zfcp_data.config_sema);
  264. }
  265. #undef ZFCP_LOG_AREA