zfcp_aux.c 19 KB

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