zfcp_aux.c 19 KB

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