zfcp_aux.c 19 KB

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