zfcp_aux.c 19 KB

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