zfcp_aux.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760
  1. /*
  2. * zfcp device driver
  3. *
  4. * Module interface and handling of zfcp data structures.
  5. *
  6. * Copyright IBM Corporation 2002, 2009
  7. */
  8. /*
  9. * Driver authors:
  10. * Martin Peschke (originator of the driver)
  11. * Raimund Schroeder
  12. * Aron Zeh
  13. * Wolfgang Taphorn
  14. * Stefan Bader
  15. * Heiko Carstens (kernel 2.6 port of the driver)
  16. * Andreas Herrmann
  17. * Maxim Shchetynin
  18. * Volker Sameske
  19. * Ralph Wuerthner
  20. * Michael Loehr
  21. * Swen Schillig
  22. * Christof Schmitt
  23. * Martin Petermann
  24. * Sven Schuetz
  25. */
  26. #define KMSG_COMPONENT "zfcp"
  27. #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
  28. #include <linux/miscdevice.h>
  29. #include <linux/seq_file.h>
  30. #include "zfcp_ext.h"
  31. #define ZFCP_BUS_ID_SIZE 20
  32. MODULE_AUTHOR("IBM Deutschland Entwicklung GmbH - linux390@de.ibm.com");
  33. MODULE_DESCRIPTION("FCP HBA driver");
  34. MODULE_LICENSE("GPL");
  35. static char *init_device;
  36. module_param_named(device, init_device, charp, 0400);
  37. MODULE_PARM_DESC(device, "specify initial device");
  38. static struct kmem_cache *zfcp_cache_hw_align(const char *name,
  39. unsigned long size)
  40. {
  41. return kmem_cache_create(name, size, roundup_pow_of_two(size), 0, NULL);
  42. }
  43. static int zfcp_reqlist_alloc(struct zfcp_adapter *adapter)
  44. {
  45. int idx;
  46. adapter->req_list = kcalloc(REQUEST_LIST_SIZE, sizeof(struct list_head),
  47. GFP_KERNEL);
  48. if (!adapter->req_list)
  49. return -ENOMEM;
  50. for (idx = 0; idx < REQUEST_LIST_SIZE; idx++)
  51. INIT_LIST_HEAD(&adapter->req_list[idx]);
  52. return 0;
  53. }
  54. /**
  55. * zfcp_reqlist_isempty - is the request list empty
  56. * @adapter: pointer to struct zfcp_adapter
  57. *
  58. * Returns: true if list is empty, false otherwise
  59. */
  60. int zfcp_reqlist_isempty(struct zfcp_adapter *adapter)
  61. {
  62. unsigned int idx;
  63. for (idx = 0; idx < REQUEST_LIST_SIZE; idx++)
  64. if (!list_empty(&adapter->req_list[idx]))
  65. return 0;
  66. return 1;
  67. }
  68. static void __init zfcp_init_device_configure(char *busid, u64 wwpn, u64 lun)
  69. {
  70. struct zfcp_adapter *adapter;
  71. struct zfcp_port *port;
  72. struct zfcp_unit *unit;
  73. mutex_lock(&zfcp_data.config_mutex);
  74. read_lock_irq(&zfcp_data.config_lock);
  75. adapter = zfcp_get_adapter_by_busid(busid);
  76. if (adapter)
  77. zfcp_adapter_get(adapter);
  78. read_unlock_irq(&zfcp_data.config_lock);
  79. if (!adapter)
  80. goto out_adapter;
  81. port = zfcp_port_enqueue(adapter, wwpn, 0, 0);
  82. if (IS_ERR(port))
  83. goto out_port;
  84. unit = zfcp_unit_enqueue(port, lun);
  85. if (IS_ERR(unit))
  86. goto out_unit;
  87. mutex_unlock(&zfcp_data.config_mutex);
  88. ccw_device_set_online(adapter->ccw_device);
  89. zfcp_erp_wait(adapter);
  90. flush_work(&unit->scsi_work);
  91. mutex_lock(&zfcp_data.config_mutex);
  92. zfcp_unit_put(unit);
  93. out_unit:
  94. zfcp_port_put(port);
  95. out_port:
  96. zfcp_adapter_put(adapter);
  97. out_adapter:
  98. mutex_unlock(&zfcp_data.config_mutex);
  99. return;
  100. }
  101. static void __init zfcp_init_device_setup(char *devstr)
  102. {
  103. char *token;
  104. char *str;
  105. char busid[ZFCP_BUS_ID_SIZE];
  106. u64 wwpn, lun;
  107. /* duplicate devstr and keep the original for sysfs presentation*/
  108. str = kmalloc(strlen(devstr) + 1, GFP_KERNEL);
  109. if (!str)
  110. return;
  111. strcpy(str, devstr);
  112. token = strsep(&str, ",");
  113. if (!token || strlen(token) >= ZFCP_BUS_ID_SIZE)
  114. goto err_out;
  115. strncpy(busid, token, ZFCP_BUS_ID_SIZE);
  116. token = strsep(&str, ",");
  117. if (!token || strict_strtoull(token, 0, (unsigned long long *) &wwpn))
  118. goto err_out;
  119. token = strsep(&str, ",");
  120. if (!token || strict_strtoull(token, 0, (unsigned long long *) &lun))
  121. goto err_out;
  122. kfree(str);
  123. zfcp_init_device_configure(busid, wwpn, lun);
  124. return;
  125. err_out:
  126. kfree(str);
  127. pr_err("%s is not a valid SCSI device\n", devstr);
  128. }
  129. static int __init zfcp_module_init(void)
  130. {
  131. int retval = -ENOMEM;
  132. zfcp_data.gpn_ft_cache = zfcp_cache_hw_align("zfcp_gpn",
  133. sizeof(struct ct_iu_gpn_ft_req));
  134. if (!zfcp_data.gpn_ft_cache)
  135. goto out;
  136. zfcp_data.qtcb_cache = zfcp_cache_hw_align("zfcp_qtcb",
  137. sizeof(struct fsf_qtcb));
  138. if (!zfcp_data.qtcb_cache)
  139. goto out_qtcb_cache;
  140. zfcp_data.sr_buffer_cache = zfcp_cache_hw_align("zfcp_sr",
  141. sizeof(struct fsf_status_read_buffer));
  142. if (!zfcp_data.sr_buffer_cache)
  143. goto out_sr_cache;
  144. zfcp_data.gid_pn_cache = zfcp_cache_hw_align("zfcp_gid",
  145. sizeof(struct zfcp_gid_pn_data));
  146. if (!zfcp_data.gid_pn_cache)
  147. goto out_gid_cache;
  148. mutex_init(&zfcp_data.config_mutex);
  149. rwlock_init(&zfcp_data.config_lock);
  150. zfcp_data.scsi_transport_template =
  151. fc_attach_transport(&zfcp_transport_functions);
  152. if (!zfcp_data.scsi_transport_template)
  153. goto out_transport;
  154. retval = misc_register(&zfcp_cfdc_misc);
  155. if (retval) {
  156. pr_err("Registering the misc device zfcp_cfdc failed\n");
  157. goto out_misc;
  158. }
  159. retval = zfcp_ccw_register();
  160. if (retval) {
  161. pr_err("The zfcp device driver could not register with "
  162. "the common I/O layer\n");
  163. goto out_ccw_register;
  164. }
  165. if (init_device)
  166. zfcp_init_device_setup(init_device);
  167. return 0;
  168. out_ccw_register:
  169. misc_deregister(&zfcp_cfdc_misc);
  170. out_misc:
  171. fc_release_transport(zfcp_data.scsi_transport_template);
  172. out_transport:
  173. kmem_cache_destroy(zfcp_data.gid_pn_cache);
  174. out_gid_cache:
  175. kmem_cache_destroy(zfcp_data.sr_buffer_cache);
  176. out_sr_cache:
  177. kmem_cache_destroy(zfcp_data.qtcb_cache);
  178. out_qtcb_cache:
  179. kmem_cache_destroy(zfcp_data.gpn_ft_cache);
  180. out:
  181. return retval;
  182. }
  183. module_init(zfcp_module_init);
  184. /**
  185. * zfcp_get_unit_by_lun - find unit in unit list of port by FCP LUN
  186. * @port: pointer to port to search for unit
  187. * @fcp_lun: FCP LUN to search for
  188. *
  189. * Returns: pointer to zfcp_unit or NULL
  190. */
  191. struct zfcp_unit *zfcp_get_unit_by_lun(struct zfcp_port *port, u64 fcp_lun)
  192. {
  193. struct zfcp_unit *unit;
  194. list_for_each_entry(unit, &port->unit_list_head, list)
  195. if ((unit->fcp_lun == fcp_lun) &&
  196. !(atomic_read(&unit->status) & ZFCP_STATUS_COMMON_REMOVE))
  197. return unit;
  198. return NULL;
  199. }
  200. /**
  201. * zfcp_get_port_by_wwpn - find port in port list of adapter by wwpn
  202. * @adapter: pointer to adapter to search for port
  203. * @wwpn: wwpn to search for
  204. *
  205. * Returns: pointer to zfcp_port or NULL
  206. */
  207. struct zfcp_port *zfcp_get_port_by_wwpn(struct zfcp_adapter *adapter,
  208. u64 wwpn)
  209. {
  210. struct zfcp_port *port;
  211. list_for_each_entry(port, &adapter->port_list_head, list)
  212. if ((port->wwpn == wwpn) &&
  213. !(atomic_read(&port->status) & ZFCP_STATUS_COMMON_REMOVE))
  214. return port;
  215. return NULL;
  216. }
  217. static void zfcp_sysfs_unit_release(struct device *dev)
  218. {
  219. kfree(container_of(dev, struct zfcp_unit, sysfs_device));
  220. }
  221. /**
  222. * zfcp_unit_enqueue - enqueue unit to unit list of a port.
  223. * @port: pointer to port where unit is added
  224. * @fcp_lun: FCP LUN of unit to be enqueued
  225. * Returns: pointer to enqueued unit on success, ERR_PTR on error
  226. * Locks: config_mutex must be held to serialize changes to the unit list
  227. *
  228. * Sets up some unit internal structures and creates sysfs entry.
  229. */
  230. struct zfcp_unit *zfcp_unit_enqueue(struct zfcp_port *port, u64 fcp_lun)
  231. {
  232. struct zfcp_unit *unit;
  233. read_lock_irq(&zfcp_data.config_lock);
  234. if (zfcp_get_unit_by_lun(port, fcp_lun)) {
  235. read_unlock_irq(&zfcp_data.config_lock);
  236. return ERR_PTR(-EINVAL);
  237. }
  238. read_unlock_irq(&zfcp_data.config_lock);
  239. unit = kzalloc(sizeof(struct zfcp_unit), GFP_KERNEL);
  240. if (!unit)
  241. return ERR_PTR(-ENOMEM);
  242. atomic_set(&unit->refcount, 0);
  243. init_waitqueue_head(&unit->remove_wq);
  244. INIT_WORK(&unit->scsi_work, zfcp_scsi_scan);
  245. unit->port = port;
  246. unit->fcp_lun = fcp_lun;
  247. if (dev_set_name(&unit->sysfs_device, "0x%016llx",
  248. (unsigned long long) fcp_lun)) {
  249. kfree(unit);
  250. return ERR_PTR(-ENOMEM);
  251. }
  252. unit->sysfs_device.parent = &port->sysfs_device;
  253. unit->sysfs_device.release = zfcp_sysfs_unit_release;
  254. dev_set_drvdata(&unit->sysfs_device, unit);
  255. /* mark unit unusable as long as sysfs registration is not complete */
  256. atomic_set_mask(ZFCP_STATUS_COMMON_REMOVE, &unit->status);
  257. spin_lock_init(&unit->latencies.lock);
  258. unit->latencies.write.channel.min = 0xFFFFFFFF;
  259. unit->latencies.write.fabric.min = 0xFFFFFFFF;
  260. unit->latencies.read.channel.min = 0xFFFFFFFF;
  261. unit->latencies.read.fabric.min = 0xFFFFFFFF;
  262. unit->latencies.cmd.channel.min = 0xFFFFFFFF;
  263. unit->latencies.cmd.fabric.min = 0xFFFFFFFF;
  264. if (device_register(&unit->sysfs_device)) {
  265. put_device(&unit->sysfs_device);
  266. return ERR_PTR(-EINVAL);
  267. }
  268. if (sysfs_create_group(&unit->sysfs_device.kobj,
  269. &zfcp_sysfs_unit_attrs)) {
  270. device_unregister(&unit->sysfs_device);
  271. return ERR_PTR(-EINVAL);
  272. }
  273. zfcp_unit_get(unit);
  274. write_lock_irq(&zfcp_data.config_lock);
  275. list_add_tail(&unit->list, &port->unit_list_head);
  276. atomic_clear_mask(ZFCP_STATUS_COMMON_REMOVE, &unit->status);
  277. atomic_set_mask(ZFCP_STATUS_COMMON_RUNNING, &unit->status);
  278. write_unlock_irq(&zfcp_data.config_lock);
  279. zfcp_port_get(port);
  280. return unit;
  281. }
  282. /**
  283. * zfcp_unit_dequeue - dequeue unit
  284. * @unit: pointer to zfcp_unit
  285. *
  286. * waits until all work is done on unit and removes it then from the unit->list
  287. * of the associated port.
  288. */
  289. void zfcp_unit_dequeue(struct zfcp_unit *unit)
  290. {
  291. wait_event(unit->remove_wq, atomic_read(&unit->refcount) == 0);
  292. write_lock_irq(&zfcp_data.config_lock);
  293. list_del(&unit->list);
  294. write_unlock_irq(&zfcp_data.config_lock);
  295. zfcp_port_put(unit->port);
  296. sysfs_remove_group(&unit->sysfs_device.kobj, &zfcp_sysfs_unit_attrs);
  297. device_unregister(&unit->sysfs_device);
  298. }
  299. static int zfcp_allocate_low_mem_buffers(struct zfcp_adapter *adapter)
  300. {
  301. /* must only be called with zfcp_data.config_mutex taken */
  302. adapter->pool.erp_req =
  303. mempool_create_kmalloc_pool(1, sizeof(struct zfcp_fsf_req));
  304. if (!adapter->pool.erp_req)
  305. return -ENOMEM;
  306. adapter->pool.gid_pn_req =
  307. mempool_create_kmalloc_pool(1, sizeof(struct zfcp_fsf_req));
  308. if (!adapter->pool.gid_pn_req)
  309. return -ENOMEM;
  310. adapter->pool.scsi_req =
  311. mempool_create_kmalloc_pool(1, sizeof(struct zfcp_fsf_req));
  312. if (!adapter->pool.scsi_req)
  313. return -ENOMEM;
  314. adapter->pool.scsi_abort =
  315. mempool_create_kmalloc_pool(1, sizeof(struct zfcp_fsf_req));
  316. if (!adapter->pool.scsi_abort)
  317. return -ENOMEM;
  318. adapter->pool.status_read_req =
  319. mempool_create_kmalloc_pool(FSF_STATUS_READS_RECOM,
  320. sizeof(struct zfcp_fsf_req));
  321. if (!adapter->pool.status_read_req)
  322. return -ENOMEM;
  323. adapter->pool.qtcb_pool =
  324. mempool_create_slab_pool(4, zfcp_data.qtcb_cache);
  325. if (!adapter->pool.qtcb_pool)
  326. return -ENOMEM;
  327. adapter->pool.status_read_data =
  328. mempool_create_slab_pool(FSF_STATUS_READS_RECOM,
  329. zfcp_data.sr_buffer_cache);
  330. if (!adapter->pool.status_read_data)
  331. return -ENOMEM;
  332. adapter->pool.gid_pn_data =
  333. mempool_create_slab_pool(1, zfcp_data.gid_pn_cache);
  334. if (!adapter->pool.gid_pn_data)
  335. return -ENOMEM;
  336. return 0;
  337. }
  338. static void zfcp_free_low_mem_buffers(struct zfcp_adapter *adapter)
  339. {
  340. /* zfcp_data.config_mutex must be held */
  341. if (adapter->pool.erp_req)
  342. mempool_destroy(adapter->pool.erp_req);
  343. if (adapter->pool.scsi_req)
  344. mempool_destroy(adapter->pool.scsi_req);
  345. if (adapter->pool.scsi_abort)
  346. mempool_destroy(adapter->pool.scsi_abort);
  347. if (adapter->pool.qtcb_pool)
  348. mempool_destroy(adapter->pool.qtcb_pool);
  349. if (adapter->pool.status_read_req)
  350. mempool_destroy(adapter->pool.status_read_req);
  351. if (adapter->pool.status_read_data)
  352. mempool_destroy(adapter->pool.status_read_data);
  353. if (adapter->pool.gid_pn_data)
  354. mempool_destroy(adapter->pool.gid_pn_data);
  355. }
  356. /**
  357. * zfcp_status_read_refill - refill the long running status_read_requests
  358. * @adapter: ptr to struct zfcp_adapter for which the buffers should be refilled
  359. *
  360. * Returns: 0 on success, 1 otherwise
  361. *
  362. * if there are 16 or more status_read requests missing an adapter_reopen
  363. * is triggered
  364. */
  365. int zfcp_status_read_refill(struct zfcp_adapter *adapter)
  366. {
  367. while (atomic_read(&adapter->stat_miss) > 0)
  368. if (zfcp_fsf_status_read(adapter->qdio)) {
  369. if (atomic_read(&adapter->stat_miss) >= 16) {
  370. zfcp_erp_adapter_reopen(adapter, 0, "axsref1",
  371. NULL);
  372. return 1;
  373. }
  374. break;
  375. } else
  376. atomic_dec(&adapter->stat_miss);
  377. return 0;
  378. }
  379. static void _zfcp_status_read_scheduler(struct work_struct *work)
  380. {
  381. zfcp_status_read_refill(container_of(work, struct zfcp_adapter,
  382. stat_work));
  383. }
  384. static void zfcp_print_sl(struct seq_file *m, struct service_level *sl)
  385. {
  386. struct zfcp_adapter *adapter =
  387. container_of(sl, struct zfcp_adapter, service_level);
  388. seq_printf(m, "zfcp: %s microcode level %x\n",
  389. dev_name(&adapter->ccw_device->dev),
  390. adapter->fsf_lic_version);
  391. }
  392. static int zfcp_setup_adapter_work_queue(struct zfcp_adapter *adapter)
  393. {
  394. char name[TASK_COMM_LEN];
  395. snprintf(name, sizeof(name), "zfcp_q_%s",
  396. dev_name(&adapter->ccw_device->dev));
  397. adapter->work_queue = create_singlethread_workqueue(name);
  398. if (adapter->work_queue)
  399. return 0;
  400. return -ENOMEM;
  401. }
  402. static void zfcp_destroy_adapter_work_queue(struct zfcp_adapter *adapter)
  403. {
  404. if (adapter->work_queue)
  405. destroy_workqueue(adapter->work_queue);
  406. adapter->work_queue = NULL;
  407. }
  408. /**
  409. * zfcp_adapter_enqueue - enqueue a new adapter to the list
  410. * @ccw_device: pointer to the struct cc_device
  411. *
  412. * Returns: 0 if a new adapter was successfully enqueued
  413. * -ENOMEM if alloc failed
  414. * Enqueues an adapter at the end of the adapter list in the driver data.
  415. * All adapter internal structures are set up.
  416. * Proc-fs entries are also created.
  417. * locks: config_mutex must be held to serialize changes to the adapter list
  418. */
  419. int zfcp_adapter_enqueue(struct ccw_device *ccw_device)
  420. {
  421. struct zfcp_adapter *adapter;
  422. /*
  423. * Note: It is safe to release the list_lock, as any list changes
  424. * are protected by the config_mutex, which must be held to get here
  425. */
  426. adapter = kzalloc(sizeof(struct zfcp_adapter), GFP_KERNEL);
  427. if (!adapter)
  428. return -ENOMEM;
  429. ccw_device->handler = NULL;
  430. adapter->ccw_device = ccw_device;
  431. atomic_set(&adapter->refcount, 0);
  432. if (zfcp_qdio_setup(adapter))
  433. goto qdio_failed;
  434. if (zfcp_allocate_low_mem_buffers(adapter))
  435. goto low_mem_buffers_failed;
  436. if (zfcp_reqlist_alloc(adapter))
  437. goto low_mem_buffers_failed;
  438. if (zfcp_dbf_adapter_register(adapter))
  439. goto debug_register_failed;
  440. if (zfcp_setup_adapter_work_queue(adapter))
  441. goto work_queue_failed;
  442. if (zfcp_fc_gs_setup(adapter))
  443. goto generic_services_failed;
  444. init_waitqueue_head(&adapter->remove_wq);
  445. init_waitqueue_head(&adapter->erp_ready_wq);
  446. init_waitqueue_head(&adapter->erp_done_wqh);
  447. INIT_LIST_HEAD(&adapter->port_list_head);
  448. INIT_LIST_HEAD(&adapter->erp_ready_head);
  449. INIT_LIST_HEAD(&adapter->erp_running_head);
  450. spin_lock_init(&adapter->req_list_lock);
  451. rwlock_init(&adapter->erp_lock);
  452. rwlock_init(&adapter->abort_lock);
  453. if (zfcp_erp_thread_setup(adapter))
  454. goto erp_thread_failed;
  455. INIT_WORK(&adapter->stat_work, _zfcp_status_read_scheduler);
  456. INIT_WORK(&adapter->scan_work, _zfcp_fc_scan_ports_later);
  457. adapter->service_level.seq_print = zfcp_print_sl;
  458. /* mark adapter unusable as long as sysfs registration is not complete */
  459. atomic_set_mask(ZFCP_STATUS_COMMON_REMOVE, &adapter->status);
  460. dev_set_drvdata(&ccw_device->dev, adapter);
  461. if (sysfs_create_group(&ccw_device->dev.kobj,
  462. &zfcp_sysfs_adapter_attrs))
  463. goto sysfs_failed;
  464. atomic_clear_mask(ZFCP_STATUS_COMMON_REMOVE, &adapter->status);
  465. if (!zfcp_adapter_scsi_register(adapter))
  466. return 0;
  467. sysfs_failed:
  468. zfcp_erp_thread_kill(adapter);
  469. erp_thread_failed:
  470. zfcp_fc_gs_destroy(adapter);
  471. generic_services_failed:
  472. zfcp_destroy_adapter_work_queue(adapter);
  473. work_queue_failed:
  474. zfcp_dbf_adapter_unregister(adapter->dbf);
  475. debug_register_failed:
  476. dev_set_drvdata(&ccw_device->dev, NULL);
  477. kfree(adapter->req_list);
  478. low_mem_buffers_failed:
  479. zfcp_free_low_mem_buffers(adapter);
  480. qdio_failed:
  481. zfcp_qdio_destroy(adapter->qdio);
  482. kfree(adapter);
  483. return -ENOMEM;
  484. }
  485. /**
  486. * zfcp_adapter_dequeue - remove the adapter from the resource list
  487. * @adapter: pointer to struct zfcp_adapter which should be removed
  488. * locks: adapter list write lock is assumed to be held by caller
  489. */
  490. void zfcp_adapter_dequeue(struct zfcp_adapter *adapter)
  491. {
  492. int retval = 0;
  493. unsigned long flags;
  494. cancel_work_sync(&adapter->scan_work);
  495. cancel_work_sync(&adapter->stat_work);
  496. zfcp_fc_wka_ports_force_offline(adapter->gs);
  497. zfcp_adapter_scsi_unregister(adapter);
  498. sysfs_remove_group(&adapter->ccw_device->dev.kobj,
  499. &zfcp_sysfs_adapter_attrs);
  500. dev_set_drvdata(&adapter->ccw_device->dev, NULL);
  501. /* sanity check: no pending FSF requests */
  502. spin_lock_irqsave(&adapter->req_list_lock, flags);
  503. retval = zfcp_reqlist_isempty(adapter);
  504. spin_unlock_irqrestore(&adapter->req_list_lock, flags);
  505. if (!retval)
  506. return;
  507. zfcp_fc_gs_destroy(adapter);
  508. zfcp_erp_thread_kill(adapter);
  509. zfcp_destroy_adapter_work_queue(adapter);
  510. zfcp_dbf_adapter_unregister(adapter->dbf);
  511. zfcp_free_low_mem_buffers(adapter);
  512. zfcp_qdio_destroy(adapter->qdio);
  513. kfree(adapter->req_list);
  514. kfree(adapter->fc_stats);
  515. kfree(adapter->stats_reset_data);
  516. kfree(adapter);
  517. }
  518. static void zfcp_sysfs_port_release(struct device *dev)
  519. {
  520. kfree(container_of(dev, struct zfcp_port, sysfs_device));
  521. }
  522. /**
  523. * zfcp_port_enqueue - enqueue port to port list of adapter
  524. * @adapter: adapter where remote port is added
  525. * @wwpn: WWPN of the remote port to be enqueued
  526. * @status: initial status for the port
  527. * @d_id: destination id of the remote port to be enqueued
  528. * Returns: pointer to enqueued port on success, ERR_PTR on error
  529. * Locks: config_mutex must be held to serialize changes to the port list
  530. *
  531. * All port internal structures are set up and the sysfs entry is generated.
  532. * d_id is used to enqueue ports with a well known address like the Directory
  533. * Service for nameserver lookup.
  534. */
  535. struct zfcp_port *zfcp_port_enqueue(struct zfcp_adapter *adapter, u64 wwpn,
  536. u32 status, u32 d_id)
  537. {
  538. struct zfcp_port *port;
  539. read_lock_irq(&zfcp_data.config_lock);
  540. if (zfcp_get_port_by_wwpn(adapter, wwpn)) {
  541. read_unlock_irq(&zfcp_data.config_lock);
  542. return ERR_PTR(-EINVAL);
  543. }
  544. read_unlock_irq(&zfcp_data.config_lock);
  545. port = kzalloc(sizeof(struct zfcp_port), GFP_KERNEL);
  546. if (!port)
  547. return ERR_PTR(-ENOMEM);
  548. init_waitqueue_head(&port->remove_wq);
  549. INIT_LIST_HEAD(&port->unit_list_head);
  550. INIT_WORK(&port->gid_pn_work, zfcp_fc_port_did_lookup);
  551. INIT_WORK(&port->test_link_work, zfcp_fc_link_test_work);
  552. INIT_WORK(&port->rport_work, zfcp_scsi_rport_work);
  553. port->adapter = adapter;
  554. port->d_id = d_id;
  555. port->wwpn = wwpn;
  556. port->rport_task = RPORT_NONE;
  557. /* mark port unusable as long as sysfs registration is not complete */
  558. atomic_set_mask(status | ZFCP_STATUS_COMMON_REMOVE, &port->status);
  559. atomic_set(&port->refcount, 0);
  560. if (dev_set_name(&port->sysfs_device, "0x%016llx",
  561. (unsigned long long)wwpn)) {
  562. kfree(port);
  563. return ERR_PTR(-ENOMEM);
  564. }
  565. port->sysfs_device.parent = &adapter->ccw_device->dev;
  566. port->sysfs_device.release = zfcp_sysfs_port_release;
  567. dev_set_drvdata(&port->sysfs_device, port);
  568. if (device_register(&port->sysfs_device)) {
  569. put_device(&port->sysfs_device);
  570. return ERR_PTR(-EINVAL);
  571. }
  572. if (sysfs_create_group(&port->sysfs_device.kobj,
  573. &zfcp_sysfs_port_attrs)) {
  574. device_unregister(&port->sysfs_device);
  575. return ERR_PTR(-EINVAL);
  576. }
  577. zfcp_port_get(port);
  578. write_lock_irq(&zfcp_data.config_lock);
  579. list_add_tail(&port->list, &adapter->port_list_head);
  580. atomic_clear_mask(ZFCP_STATUS_COMMON_REMOVE, &port->status);
  581. atomic_set_mask(ZFCP_STATUS_COMMON_RUNNING, &port->status);
  582. write_unlock_irq(&zfcp_data.config_lock);
  583. zfcp_adapter_get(adapter);
  584. return port;
  585. }
  586. /**
  587. * zfcp_port_dequeue - dequeues a port from the port list of the adapter
  588. * @port: pointer to struct zfcp_port which should be removed
  589. */
  590. void zfcp_port_dequeue(struct zfcp_port *port)
  591. {
  592. write_lock_irq(&zfcp_data.config_lock);
  593. list_del(&port->list);
  594. write_unlock_irq(&zfcp_data.config_lock);
  595. wait_event(port->remove_wq, atomic_read(&port->refcount) == 0);
  596. cancel_work_sync(&port->rport_work); /* usually not necessary */
  597. zfcp_adapter_put(port->adapter);
  598. sysfs_remove_group(&port->sysfs_device.kobj, &zfcp_sysfs_port_attrs);
  599. device_unregister(&port->sysfs_device);
  600. }
  601. /**
  602. * zfcp_sg_free_table - free memory used by scatterlists
  603. * @sg: pointer to scatterlist
  604. * @count: number of scatterlist which are to be free'ed
  605. * the scatterlist are expected to reference pages always
  606. */
  607. void zfcp_sg_free_table(struct scatterlist *sg, int count)
  608. {
  609. int i;
  610. for (i = 0; i < count; i++, sg++)
  611. if (sg)
  612. free_page((unsigned long) sg_virt(sg));
  613. else
  614. break;
  615. }
  616. /**
  617. * zfcp_sg_setup_table - init scatterlist and allocate, assign buffers
  618. * @sg: pointer to struct scatterlist
  619. * @count: number of scatterlists which should be assigned with buffers
  620. * of size page
  621. *
  622. * Returns: 0 on success, -ENOMEM otherwise
  623. */
  624. int zfcp_sg_setup_table(struct scatterlist *sg, int count)
  625. {
  626. void *addr;
  627. int i;
  628. sg_init_table(sg, count);
  629. for (i = 0; i < count; i++, sg++) {
  630. addr = (void *) get_zeroed_page(GFP_KERNEL);
  631. if (!addr) {
  632. zfcp_sg_free_table(sg, i);
  633. return -ENOMEM;
  634. }
  635. sg_set_buf(sg, addr, PAGE_SIZE);
  636. }
  637. return 0;
  638. }