zfcp_aux.c 20 KB

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