zfcp_aux.c 20 KB

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