zfcp_aux.c 20 KB

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