zfcp_aux.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801
  1. /*
  2. * zfcp device driver
  3. *
  4. * Module interface and handling of zfcp data structures.
  5. *
  6. * Copyright IBM Corporation 2002, 2008
  7. */
  8. /*
  9. * Driver authors:
  10. * Martin Peschke (originator of the driver)
  11. * Raimund Schroeder
  12. * Aron Zeh
  13. * Wolfgang Taphorn
  14. * Stefan Bader
  15. * Heiko Carstens (kernel 2.6 port of the driver)
  16. * Andreas Herrmann
  17. * Maxim Shchetynin
  18. * Volker Sameske
  19. * Ralph Wuerthner
  20. * Michael Loehr
  21. * Swen Schillig
  22. * Christof Schmitt
  23. * Martin Petermann
  24. * Sven Schuetz
  25. */
  26. #include <linux/miscdevice.h>
  27. #include "zfcp_ext.h"
  28. static char *device;
  29. MODULE_AUTHOR("IBM Deutschland Entwicklung GmbH - linux390@de.ibm.com");
  30. MODULE_DESCRIPTION("FCP HBA driver");
  31. MODULE_LICENSE("GPL");
  32. module_param(device, charp, 0400);
  33. MODULE_PARM_DESC(device, "specify initial device");
  34. static int zfcp_reqlist_alloc(struct zfcp_adapter *adapter)
  35. {
  36. int idx;
  37. adapter->req_list = kcalloc(REQUEST_LIST_SIZE, sizeof(struct list_head),
  38. GFP_KERNEL);
  39. if (!adapter->req_list)
  40. return -ENOMEM;
  41. for (idx = 0; idx < REQUEST_LIST_SIZE; idx++)
  42. INIT_LIST_HEAD(&adapter->req_list[idx]);
  43. return 0;
  44. }
  45. /**
  46. * zfcp_reqlist_isempty - is the request list empty
  47. * @adapter: pointer to struct zfcp_adapter
  48. *
  49. * Returns: true if list is empty, false otherwise
  50. */
  51. int zfcp_reqlist_isempty(struct zfcp_adapter *adapter)
  52. {
  53. unsigned int idx;
  54. for (idx = 0; idx < REQUEST_LIST_SIZE; idx++)
  55. if (!list_empty(&adapter->req_list[idx]))
  56. return 0;
  57. return 1;
  58. }
  59. static int __init zfcp_device_setup(char *devstr)
  60. {
  61. char *token;
  62. char *str;
  63. if (!devstr)
  64. return 0;
  65. /* duplicate devstr and keep the original for sysfs presentation*/
  66. str = kmalloc(strlen(devstr) + 1, GFP_KERNEL);
  67. if (!str)
  68. return 0;
  69. strcpy(str, devstr);
  70. token = strsep(&str, ",");
  71. if (!token || strlen(token) >= BUS_ID_SIZE)
  72. goto err_out;
  73. strncpy(zfcp_data.init_busid, token, BUS_ID_SIZE);
  74. token = strsep(&str, ",");
  75. if (!token || strict_strtoull(token, 0, &zfcp_data.init_wwpn))
  76. goto err_out;
  77. token = strsep(&str, ",");
  78. if (!token || strict_strtoull(token, 0, &zfcp_data.init_fcp_lun))
  79. goto err_out;
  80. kfree(str);
  81. return 1;
  82. err_out:
  83. kfree(str);
  84. pr_err("zfcp: Parse error for device parameter string %s, "
  85. "device not attached.\n", devstr);
  86. return 0;
  87. }
  88. static struct zfcp_adapter *zfcp_get_adapter_by_busid(char *bus_id)
  89. {
  90. struct zfcp_adapter *adapter;
  91. list_for_each_entry(adapter, &zfcp_data.adapter_list_head, list)
  92. if ((strncmp(bus_id, adapter->ccw_device->dev.bus_id,
  93. BUS_ID_SIZE) == 0) &&
  94. !(atomic_read(&adapter->status) &
  95. ZFCP_STATUS_COMMON_REMOVE))
  96. return adapter;
  97. return NULL;
  98. }
  99. static void __init zfcp_init_device_configure(void)
  100. {
  101. struct zfcp_adapter *adapter;
  102. struct zfcp_port *port;
  103. struct zfcp_unit *unit;
  104. down(&zfcp_data.config_sema);
  105. read_lock_irq(&zfcp_data.config_lock);
  106. adapter = zfcp_get_adapter_by_busid(zfcp_data.init_busid);
  107. if (adapter)
  108. zfcp_adapter_get(adapter);
  109. read_unlock_irq(&zfcp_data.config_lock);
  110. if (!adapter)
  111. goto out_adapter;
  112. port = zfcp_port_enqueue(adapter, zfcp_data.init_wwpn, 0, 0);
  113. if (IS_ERR(port))
  114. goto out_port;
  115. unit = zfcp_unit_enqueue(port, zfcp_data.init_fcp_lun);
  116. if (IS_ERR(unit))
  117. goto out_unit;
  118. up(&zfcp_data.config_sema);
  119. ccw_device_set_online(adapter->ccw_device);
  120. zfcp_erp_wait(adapter);
  121. down(&zfcp_data.config_sema);
  122. zfcp_unit_put(unit);
  123. out_unit:
  124. zfcp_port_put(port);
  125. out_port:
  126. zfcp_adapter_put(adapter);
  127. out_adapter:
  128. up(&zfcp_data.config_sema);
  129. return;
  130. }
  131. static struct kmem_cache *zfcp_cache_create(int size, char *name)
  132. {
  133. int align = 1;
  134. while ((size - align) > 0)
  135. align <<= 1;
  136. return kmem_cache_create(name , size, align, 0, NULL);
  137. }
  138. static int __init zfcp_module_init(void)
  139. {
  140. int retval = -ENOMEM;
  141. zfcp_data.fsf_req_qtcb_cache = zfcp_cache_create(
  142. sizeof(struct zfcp_fsf_req_qtcb), "zfcp_fsf");
  143. if (!zfcp_data.fsf_req_qtcb_cache)
  144. goto out;
  145. zfcp_data.sr_buffer_cache = zfcp_cache_create(
  146. sizeof(struct fsf_status_read_buffer), "zfcp_sr");
  147. if (!zfcp_data.sr_buffer_cache)
  148. goto out_sr_cache;
  149. zfcp_data.gid_pn_cache = zfcp_cache_create(
  150. sizeof(struct zfcp_gid_pn_data), "zfcp_gid");
  151. if (!zfcp_data.gid_pn_cache)
  152. goto out_gid_cache;
  153. INIT_LIST_HEAD(&zfcp_data.adapter_list_head);
  154. INIT_LIST_HEAD(&zfcp_data.adapter_remove_lh);
  155. sema_init(&zfcp_data.config_sema, 1);
  156. rwlock_init(&zfcp_data.config_lock);
  157. zfcp_data.scsi_transport_template =
  158. fc_attach_transport(&zfcp_transport_functions);
  159. if (!zfcp_data.scsi_transport_template)
  160. goto out_transport;
  161. retval = misc_register(&zfcp_cfdc_misc);
  162. if (retval) {
  163. pr_err("zfcp: registration of misc device zfcp_cfdc failed\n");
  164. goto out_misc;
  165. }
  166. retval = zfcp_ccw_register();
  167. if (retval) {
  168. pr_err("zfcp: Registration with common I/O layer failed.\n");
  169. goto out_ccw_register;
  170. }
  171. if (zfcp_device_setup(device))
  172. zfcp_init_device_configure();
  173. goto out;
  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.fsf_req_qtcb_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,
  196. fcp_lun_t fcp_lun)
  197. {
  198. struct zfcp_unit *unit;
  199. list_for_each_entry(unit, &port->unit_list_head, list)
  200. if ((unit->fcp_lun == fcp_lun) &&
  201. !(atomic_read(&unit->status) & ZFCP_STATUS_COMMON_REMOVE))
  202. return unit;
  203. return NULL;
  204. }
  205. /**
  206. * zfcp_get_port_by_wwpn - find port in port list of adapter by wwpn
  207. * @adapter: pointer to adapter to search for port
  208. * @wwpn: wwpn to search for
  209. *
  210. * Returns: pointer to zfcp_port or NULL
  211. */
  212. struct zfcp_port *zfcp_get_port_by_wwpn(struct zfcp_adapter *adapter,
  213. wwn_t wwpn)
  214. {
  215. struct zfcp_port *port;
  216. list_for_each_entry(port, &adapter->port_list_head, list)
  217. if ((port->wwpn == wwpn) && !(atomic_read(&port->status) &
  218. (ZFCP_STATUS_PORT_NO_WWPN | ZFCP_STATUS_COMMON_REMOVE)))
  219. return port;
  220. return NULL;
  221. }
  222. static void zfcp_sysfs_unit_release(struct device *dev)
  223. {
  224. kfree(container_of(dev, struct zfcp_unit, sysfs_device));
  225. }
  226. /**
  227. * zfcp_unit_enqueue - enqueue unit to unit list of a port.
  228. * @port: pointer to port where unit is added
  229. * @fcp_lun: FCP LUN of unit to be enqueued
  230. * Returns: pointer to enqueued unit on success, ERR_PTR on error
  231. * Locks: config_sema must be held to serialize changes to the unit list
  232. *
  233. * Sets up some unit internal structures and creates sysfs entry.
  234. */
  235. struct zfcp_unit *zfcp_unit_enqueue(struct zfcp_port *port, fcp_lun_t fcp_lun)
  236. {
  237. struct zfcp_unit *unit;
  238. unit = kzalloc(sizeof(struct zfcp_unit), GFP_KERNEL);
  239. if (!unit)
  240. return ERR_PTR(-ENOMEM);
  241. atomic_set(&unit->refcount, 0);
  242. init_waitqueue_head(&unit->remove_wq);
  243. unit->port = port;
  244. unit->fcp_lun = fcp_lun;
  245. snprintf(unit->sysfs_device.bus_id, BUS_ID_SIZE, "0x%016llx", fcp_lun);
  246. unit->sysfs_device.parent = &port->sysfs_device;
  247. unit->sysfs_device.release = zfcp_sysfs_unit_release;
  248. dev_set_drvdata(&unit->sysfs_device, unit);
  249. /* mark unit unusable as long as sysfs registration is not complete */
  250. atomic_set_mask(ZFCP_STATUS_COMMON_REMOVE, &unit->status);
  251. spin_lock_init(&unit->latencies.lock);
  252. unit->latencies.write.channel.min = 0xFFFFFFFF;
  253. unit->latencies.write.fabric.min = 0xFFFFFFFF;
  254. unit->latencies.read.channel.min = 0xFFFFFFFF;
  255. unit->latencies.read.fabric.min = 0xFFFFFFFF;
  256. unit->latencies.cmd.channel.min = 0xFFFFFFFF;
  257. unit->latencies.cmd.fabric.min = 0xFFFFFFFF;
  258. read_lock_irq(&zfcp_data.config_lock);
  259. if (zfcp_get_unit_by_lun(port, fcp_lun)) {
  260. read_unlock_irq(&zfcp_data.config_lock);
  261. goto err_out_free;
  262. }
  263. read_unlock_irq(&zfcp_data.config_lock);
  264. if (device_register(&unit->sysfs_device))
  265. goto err_out_free;
  266. if (sysfs_create_group(&unit->sysfs_device.kobj,
  267. &zfcp_sysfs_unit_attrs)) {
  268. device_unregister(&unit->sysfs_device);
  269. return ERR_PTR(-EIO);
  270. }
  271. zfcp_unit_get(unit);
  272. unit->scsi_lun = scsilun_to_int((struct scsi_lun *)&unit->fcp_lun);
  273. write_lock_irq(&zfcp_data.config_lock);
  274. list_add_tail(&unit->list, &port->unit_list_head);
  275. atomic_clear_mask(ZFCP_STATUS_COMMON_REMOVE, &unit->status);
  276. atomic_set_mask(ZFCP_STATUS_COMMON_RUNNING, &unit->status);
  277. write_unlock_irq(&zfcp_data.config_lock);
  278. port->units++;
  279. zfcp_port_get(port);
  280. return unit;
  281. err_out_free:
  282. kfree(unit);
  283. return ERR_PTR(-EINVAL);
  284. }
  285. /**
  286. * zfcp_unit_dequeue - dequeue unit
  287. * @unit: pointer to zfcp_unit
  288. *
  289. * waits until all work is done on unit and removes it then from the unit->list
  290. * of the associated port.
  291. */
  292. void zfcp_unit_dequeue(struct zfcp_unit *unit)
  293. {
  294. zfcp_unit_wait(unit);
  295. write_lock_irq(&zfcp_data.config_lock);
  296. list_del(&unit->list);
  297. write_unlock_irq(&zfcp_data.config_lock);
  298. unit->port->units--;
  299. zfcp_port_put(unit->port);
  300. sysfs_remove_group(&unit->sysfs_device.kobj, &zfcp_sysfs_unit_attrs);
  301. device_unregister(&unit->sysfs_device);
  302. }
  303. static int zfcp_allocate_low_mem_buffers(struct zfcp_adapter *adapter)
  304. {
  305. /* must only be called with zfcp_data.config_sema taken */
  306. adapter->pool.fsf_req_erp =
  307. mempool_create_slab_pool(1, zfcp_data.fsf_req_qtcb_cache);
  308. if (!adapter->pool.fsf_req_erp)
  309. return -ENOMEM;
  310. adapter->pool.fsf_req_scsi =
  311. mempool_create_slab_pool(1, zfcp_data.fsf_req_qtcb_cache);
  312. if (!adapter->pool.fsf_req_scsi)
  313. return -ENOMEM;
  314. adapter->pool.fsf_req_abort =
  315. mempool_create_slab_pool(1, zfcp_data.fsf_req_qtcb_cache);
  316. if (!adapter->pool.fsf_req_abort)
  317. return -ENOMEM;
  318. adapter->pool.fsf_req_status_read =
  319. mempool_create_kmalloc_pool(FSF_STATUS_READS_RECOM,
  320. sizeof(struct zfcp_fsf_req));
  321. if (!adapter->pool.fsf_req_status_read)
  322. return -ENOMEM;
  323. adapter->pool.data_status_read =
  324. mempool_create_slab_pool(FSF_STATUS_READS_RECOM,
  325. zfcp_data.sr_buffer_cache);
  326. if (!adapter->pool.data_status_read)
  327. return -ENOMEM;
  328. adapter->pool.data_gid_pn =
  329. mempool_create_slab_pool(1, zfcp_data.gid_pn_cache);
  330. if (!adapter->pool.data_gid_pn)
  331. return -ENOMEM;
  332. return 0;
  333. }
  334. static void zfcp_free_low_mem_buffers(struct zfcp_adapter *adapter)
  335. {
  336. /* zfcp_data.config_sema must be held */
  337. if (adapter->pool.fsf_req_erp)
  338. mempool_destroy(adapter->pool.fsf_req_erp);
  339. if (adapter->pool.fsf_req_scsi)
  340. mempool_destroy(adapter->pool.fsf_req_scsi);
  341. if (adapter->pool.fsf_req_abort)
  342. mempool_destroy(adapter->pool.fsf_req_abort);
  343. if (adapter->pool.fsf_req_status_read)
  344. mempool_destroy(adapter->pool.fsf_req_status_read);
  345. if (adapter->pool.data_status_read)
  346. mempool_destroy(adapter->pool.data_status_read);
  347. if (adapter->pool.data_gid_pn)
  348. mempool_destroy(adapter->pool.data_gid_pn);
  349. }
  350. static void zfcp_dummy_release(struct device *dev)
  351. {
  352. return;
  353. }
  354. /**
  355. * zfcp_status_read_refill - refill the long running status_read_requests
  356. * @adapter: ptr to struct zfcp_adapter for which the buffers should be refilled
  357. *
  358. * Returns: 0 on success, 1 otherwise
  359. *
  360. * if there are 16 or more status_read requests missing an adapter_reopen
  361. * is triggered
  362. */
  363. int zfcp_status_read_refill(struct zfcp_adapter *adapter)
  364. {
  365. while (atomic_read(&adapter->stat_miss) > 0)
  366. if (zfcp_fsf_status_read(adapter)) {
  367. if (atomic_read(&adapter->stat_miss) >= 16) {
  368. zfcp_erp_adapter_reopen(adapter, 0, 103, NULL);
  369. return 1;
  370. }
  371. break;
  372. } else
  373. atomic_dec(&adapter->stat_miss);
  374. return 0;
  375. }
  376. static void _zfcp_status_read_scheduler(struct work_struct *work)
  377. {
  378. zfcp_status_read_refill(container_of(work, struct zfcp_adapter,
  379. stat_work));
  380. }
  381. static int zfcp_nameserver_enqueue(struct zfcp_adapter *adapter)
  382. {
  383. struct zfcp_port *port;
  384. port = zfcp_port_enqueue(adapter, 0, ZFCP_STATUS_PORT_WKA,
  385. ZFCP_DID_DIRECTORY_SERVICE);
  386. if (IS_ERR(port))
  387. return PTR_ERR(port);
  388. zfcp_port_put(port);
  389. return 0;
  390. }
  391. /**
  392. * zfcp_adapter_enqueue - enqueue a new adapter to the list
  393. * @ccw_device: pointer to the struct cc_device
  394. *
  395. * Returns: 0 if a new adapter was successfully enqueued
  396. * -ENOMEM if alloc failed
  397. * Enqueues an adapter at the end of the adapter list in the driver data.
  398. * All adapter internal structures are set up.
  399. * Proc-fs entries are also created.
  400. * locks: config_sema must be held to serialise changes to the adapter list
  401. */
  402. int zfcp_adapter_enqueue(struct ccw_device *ccw_device)
  403. {
  404. struct zfcp_adapter *adapter;
  405. /*
  406. * Note: It is safe to release the list_lock, as any list changes
  407. * are protected by the config_sema, which must be held to get here
  408. */
  409. adapter = kzalloc(sizeof(struct zfcp_adapter), GFP_KERNEL);
  410. if (!adapter)
  411. return -ENOMEM;
  412. ccw_device->handler = NULL;
  413. adapter->ccw_device = ccw_device;
  414. atomic_set(&adapter->refcount, 0);
  415. if (zfcp_qdio_allocate(adapter))
  416. goto qdio_allocate_failed;
  417. if (zfcp_allocate_low_mem_buffers(adapter))
  418. goto failed_low_mem_buffers;
  419. if (zfcp_reqlist_alloc(adapter))
  420. goto failed_low_mem_buffers;
  421. if (zfcp_adapter_debug_register(adapter))
  422. goto debug_register_failed;
  423. init_waitqueue_head(&adapter->remove_wq);
  424. init_waitqueue_head(&adapter->erp_thread_wqh);
  425. init_waitqueue_head(&adapter->erp_done_wqh);
  426. INIT_LIST_HEAD(&adapter->port_list_head);
  427. INIT_LIST_HEAD(&adapter->port_remove_lh);
  428. INIT_LIST_HEAD(&adapter->erp_ready_head);
  429. INIT_LIST_HEAD(&adapter->erp_running_head);
  430. spin_lock_init(&adapter->req_list_lock);
  431. spin_lock_init(&adapter->hba_dbf_lock);
  432. spin_lock_init(&adapter->san_dbf_lock);
  433. spin_lock_init(&adapter->scsi_dbf_lock);
  434. spin_lock_init(&adapter->rec_dbf_lock);
  435. spin_lock_init(&adapter->req_q.lock);
  436. rwlock_init(&adapter->erp_lock);
  437. rwlock_init(&adapter->abort_lock);
  438. sema_init(&adapter->erp_ready_sem, 0);
  439. INIT_WORK(&adapter->stat_work, _zfcp_status_read_scheduler);
  440. INIT_WORK(&adapter->scan_work, _zfcp_scan_ports_later);
  441. /* mark adapter unusable as long as sysfs registration is not complete */
  442. atomic_set_mask(ZFCP_STATUS_COMMON_REMOVE, &adapter->status);
  443. dev_set_drvdata(&ccw_device->dev, adapter);
  444. if (sysfs_create_group(&ccw_device->dev.kobj,
  445. &zfcp_sysfs_adapter_attrs))
  446. goto sysfs_failed;
  447. adapter->generic_services.parent = &adapter->ccw_device->dev;
  448. adapter->generic_services.release = zfcp_dummy_release;
  449. snprintf(adapter->generic_services.bus_id, BUS_ID_SIZE,
  450. "generic_services");
  451. if (device_register(&adapter->generic_services))
  452. goto generic_services_failed;
  453. write_lock_irq(&zfcp_data.config_lock);
  454. atomic_clear_mask(ZFCP_STATUS_COMMON_REMOVE, &adapter->status);
  455. list_add_tail(&adapter->list, &zfcp_data.adapter_list_head);
  456. write_unlock_irq(&zfcp_data.config_lock);
  457. zfcp_data.adapters++;
  458. zfcp_nameserver_enqueue(adapter);
  459. return 0;
  460. generic_services_failed:
  461. sysfs_remove_group(&ccw_device->dev.kobj,
  462. &zfcp_sysfs_adapter_attrs);
  463. sysfs_failed:
  464. zfcp_adapter_debug_unregister(adapter);
  465. debug_register_failed:
  466. dev_set_drvdata(&ccw_device->dev, NULL);
  467. kfree(adapter->req_list);
  468. failed_low_mem_buffers:
  469. zfcp_free_low_mem_buffers(adapter);
  470. qdio_allocate_failed:
  471. zfcp_qdio_free(adapter);
  472. kfree(adapter);
  473. return -ENOMEM;
  474. }
  475. /**
  476. * zfcp_adapter_dequeue - remove the adapter from the resource list
  477. * @adapter: pointer to struct zfcp_adapter which should be removed
  478. * locks: adapter list write lock is assumed to be held by caller
  479. */
  480. void zfcp_adapter_dequeue(struct zfcp_adapter *adapter)
  481. {
  482. int retval = 0;
  483. unsigned long flags;
  484. cancel_work_sync(&adapter->scan_work);
  485. cancel_work_sync(&adapter->stat_work);
  486. zfcp_adapter_scsi_unregister(adapter);
  487. device_unregister(&adapter->generic_services);
  488. sysfs_remove_group(&adapter->ccw_device->dev.kobj,
  489. &zfcp_sysfs_adapter_attrs);
  490. dev_set_drvdata(&adapter->ccw_device->dev, NULL);
  491. /* sanity check: no pending FSF requests */
  492. spin_lock_irqsave(&adapter->req_list_lock, flags);
  493. retval = zfcp_reqlist_isempty(adapter);
  494. spin_unlock_irqrestore(&adapter->req_list_lock, flags);
  495. if (!retval)
  496. return;
  497. zfcp_adapter_debug_unregister(adapter);
  498. /* remove specified adapter data structure from list */
  499. write_lock_irq(&zfcp_data.config_lock);
  500. list_del(&adapter->list);
  501. write_unlock_irq(&zfcp_data.config_lock);
  502. /* decrease number of adapters in list */
  503. zfcp_data.adapters--;
  504. zfcp_qdio_free(adapter);
  505. zfcp_free_low_mem_buffers(adapter);
  506. kfree(adapter->req_list);
  507. kfree(adapter->fc_stats);
  508. kfree(adapter->stats_reset_data);
  509. kfree(adapter);
  510. }
  511. static void zfcp_sysfs_port_release(struct device *dev)
  512. {
  513. kfree(container_of(dev, struct zfcp_port, sysfs_device));
  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. * Locks: config_sema must be held to serialize changes to the port list
  523. *
  524. * All port internal structures are set up and the sysfs entry is generated.
  525. * d_id is used to enqueue ports with a well known address like the Directory
  526. * Service for nameserver lookup.
  527. */
  528. struct zfcp_port *zfcp_port_enqueue(struct zfcp_adapter *adapter, wwn_t wwpn,
  529. u32 status, u32 d_id)
  530. {
  531. struct zfcp_port *port;
  532. int retval;
  533. char *bus_id;
  534. port = kzalloc(sizeof(struct zfcp_port), GFP_KERNEL);
  535. if (!port)
  536. return ERR_PTR(-ENOMEM);
  537. init_waitqueue_head(&port->remove_wq);
  538. INIT_LIST_HEAD(&port->unit_list_head);
  539. INIT_LIST_HEAD(&port->unit_remove_lh);
  540. port->adapter = adapter;
  541. port->d_id = d_id;
  542. port->wwpn = wwpn;
  543. /* mark port unusable as long as sysfs registration is not complete */
  544. atomic_set_mask(status | ZFCP_STATUS_COMMON_REMOVE, &port->status);
  545. atomic_set(&port->refcount, 0);
  546. if (status & ZFCP_STATUS_PORT_WKA) {
  547. switch (d_id) {
  548. case ZFCP_DID_DIRECTORY_SERVICE:
  549. bus_id = "directory";
  550. break;
  551. case ZFCP_DID_MANAGEMENT_SERVICE:
  552. bus_id = "management";
  553. break;
  554. case ZFCP_DID_KEY_DISTRIBUTION_SERVICE:
  555. bus_id = "key_distribution";
  556. break;
  557. case ZFCP_DID_ALIAS_SERVICE:
  558. bus_id = "alias";
  559. break;
  560. case ZFCP_DID_TIME_SERVICE:
  561. bus_id = "time";
  562. break;
  563. default:
  564. kfree(port);
  565. return ERR_PTR(-EINVAL);
  566. }
  567. snprintf(port->sysfs_device.bus_id, BUS_ID_SIZE, "%s", bus_id);
  568. port->sysfs_device.parent = &adapter->generic_services;
  569. } else {
  570. snprintf(port->sysfs_device.bus_id,
  571. BUS_ID_SIZE, "0x%016llx", wwpn);
  572. port->sysfs_device.parent = &adapter->ccw_device->dev;
  573. }
  574. port->sysfs_device.release = zfcp_sysfs_port_release;
  575. dev_set_drvdata(&port->sysfs_device, port);
  576. read_lock_irq(&zfcp_data.config_lock);
  577. if (!(status & ZFCP_STATUS_PORT_NO_WWPN))
  578. if (zfcp_get_port_by_wwpn(adapter, wwpn)) {
  579. read_unlock_irq(&zfcp_data.config_lock);
  580. goto err_out_free;
  581. }
  582. read_unlock_irq(&zfcp_data.config_lock);
  583. if (device_register(&port->sysfs_device))
  584. goto err_out_free;
  585. if (status & ZFCP_STATUS_PORT_WKA)
  586. retval = sysfs_create_group(&port->sysfs_device.kobj,
  587. &zfcp_sysfs_ns_port_attrs);
  588. else
  589. retval = sysfs_create_group(&port->sysfs_device.kobj,
  590. &zfcp_sysfs_port_attrs);
  591. if (retval) {
  592. device_unregister(&port->sysfs_device);
  593. goto err_out;
  594. }
  595. zfcp_port_get(port);
  596. write_lock_irq(&zfcp_data.config_lock);
  597. list_add_tail(&port->list, &adapter->port_list_head);
  598. atomic_clear_mask(ZFCP_STATUS_COMMON_REMOVE, &port->status);
  599. atomic_set_mask(ZFCP_STATUS_COMMON_RUNNING, &port->status);
  600. if (d_id == ZFCP_DID_DIRECTORY_SERVICE)
  601. if (!adapter->nameserver_port)
  602. adapter->nameserver_port = port;
  603. adapter->ports++;
  604. write_unlock_irq(&zfcp_data.config_lock);
  605. zfcp_adapter_get(adapter);
  606. return port;
  607. err_out_free:
  608. kfree(port);
  609. err_out:
  610. return ERR_PTR(-EINVAL);
  611. }
  612. /**
  613. * zfcp_port_dequeue - dequeues a port from the port list of the adapter
  614. * @port: pointer to struct zfcp_port which should be removed
  615. */
  616. void zfcp_port_dequeue(struct zfcp_port *port)
  617. {
  618. zfcp_port_wait(port);
  619. write_lock_irq(&zfcp_data.config_lock);
  620. list_del(&port->list);
  621. port->adapter->ports--;
  622. write_unlock_irq(&zfcp_data.config_lock);
  623. if (port->rport)
  624. fc_remote_port_delete(port->rport);
  625. port->rport = NULL;
  626. zfcp_adapter_put(port->adapter);
  627. if (atomic_read(&port->status) & ZFCP_STATUS_PORT_WKA)
  628. sysfs_remove_group(&port->sysfs_device.kobj,
  629. &zfcp_sysfs_ns_port_attrs);
  630. else
  631. sysfs_remove_group(&port->sysfs_device.kobj,
  632. &zfcp_sysfs_port_attrs);
  633. device_unregister(&port->sysfs_device);
  634. }
  635. /**
  636. * zfcp_sg_free_table - free memory used by scatterlists
  637. * @sg: pointer to scatterlist
  638. * @count: number of scatterlist which are to be free'ed
  639. * the scatterlist are expected to reference pages always
  640. */
  641. void zfcp_sg_free_table(struct scatterlist *sg, int count)
  642. {
  643. int i;
  644. for (i = 0; i < count; i++, sg++)
  645. if (sg)
  646. free_page((unsigned long) sg_virt(sg));
  647. else
  648. break;
  649. }
  650. /**
  651. * zfcp_sg_setup_table - init scatterlist and allocate, assign buffers
  652. * @sg: pointer to struct scatterlist
  653. * @count: number of scatterlists which should be assigned with buffers
  654. * of size page
  655. *
  656. * Returns: 0 on success, -ENOMEM otherwise
  657. */
  658. int zfcp_sg_setup_table(struct scatterlist *sg, int count)
  659. {
  660. void *addr;
  661. int i;
  662. sg_init_table(sg, count);
  663. for (i = 0; i < count; i++, sg++) {
  664. addr = (void *) get_zeroed_page(GFP_KERNEL);
  665. if (!addr) {
  666. zfcp_sg_free_table(sg, i);
  667. return -ENOMEM;
  668. }
  669. sg_set_buf(sg, addr, PAGE_SIZE);
  670. }
  671. return 0;
  672. }