zfcp_aux.c 20 KB

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