zfcp_aux.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981
  1. /*
  2. * This file is part of the zfcp device driver for
  3. * FCP adapters for IBM System z9 and zSeries.
  4. *
  5. * (C) Copyright IBM Corp. 2002, 2006
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2, or (at your option)
  10. * any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21. /*
  22. * Driver authors:
  23. * Martin Peschke (originator of the driver)
  24. * Raimund Schroeder
  25. * Aron Zeh
  26. * Wolfgang Taphorn
  27. * Stefan Bader
  28. * Heiko Carstens (kernel 2.6 port of the driver)
  29. * Andreas Herrmann
  30. * Maxim Shchetynin
  31. * Volker Sameske
  32. * Ralph Wuerthner
  33. */
  34. #include <linux/miscdevice.h>
  35. #include "zfcp_ext.h"
  36. /* accumulated log level (module parameter) */
  37. static u32 loglevel = ZFCP_LOG_LEVEL_DEFAULTS;
  38. static char *device;
  39. /*********************** FUNCTION PROTOTYPES *********************************/
  40. /* written against the module interface */
  41. static int __init zfcp_module_init(void);
  42. /*********************** KERNEL/MODULE PARAMETERS ***************************/
  43. /* declare driver module init/cleanup functions */
  44. module_init(zfcp_module_init);
  45. MODULE_AUTHOR("IBM Deutschland Entwicklung GmbH - linux390@de.ibm.com");
  46. MODULE_DESCRIPTION
  47. ("FCP (SCSI over Fibre Channel) HBA driver for IBM System z9 and zSeries");
  48. MODULE_LICENSE("GPL");
  49. module_param(device, charp, 0400);
  50. MODULE_PARM_DESC(device, "specify initial device");
  51. module_param(loglevel, uint, 0400);
  52. MODULE_PARM_DESC(loglevel,
  53. "log levels, 8 nibbles: "
  54. "FC ERP QDIO CIO Config FSF SCSI Other, "
  55. "levels: 0=none 1=normal 2=devel 3=trace");
  56. /****************************************************************/
  57. /************** Functions without logging ***********************/
  58. /****************************************************************/
  59. void
  60. _zfcp_hex_dump(char *addr, int count)
  61. {
  62. int i;
  63. for (i = 0; i < count; i++) {
  64. printk("%02x", addr[i]);
  65. if ((i % 4) == 3)
  66. printk(" ");
  67. if ((i % 32) == 31)
  68. printk("\n");
  69. }
  70. if (((i-1) % 32) != 31)
  71. printk("\n");
  72. }
  73. /****************************************************************/
  74. /****** Functions to handle the request ID hash table ********/
  75. /****************************************************************/
  76. #define ZFCP_LOG_AREA ZFCP_LOG_AREA_FSF
  77. static int zfcp_reqlist_alloc(struct zfcp_adapter *adapter)
  78. {
  79. int idx;
  80. adapter->req_list = kcalloc(REQUEST_LIST_SIZE, sizeof(struct list_head),
  81. GFP_KERNEL);
  82. if (!adapter->req_list)
  83. return -ENOMEM;
  84. for (idx = 0; idx < REQUEST_LIST_SIZE; idx++)
  85. INIT_LIST_HEAD(&adapter->req_list[idx]);
  86. return 0;
  87. }
  88. static void zfcp_reqlist_free(struct zfcp_adapter *adapter)
  89. {
  90. kfree(adapter->req_list);
  91. }
  92. int zfcp_reqlist_isempty(struct zfcp_adapter *adapter)
  93. {
  94. unsigned int idx;
  95. for (idx = 0; idx < REQUEST_LIST_SIZE; idx++)
  96. if (!list_empty(&adapter->req_list[idx]))
  97. return 0;
  98. return 1;
  99. }
  100. #undef ZFCP_LOG_AREA
  101. /****************************************************************/
  102. /************** Uncategorised Functions *************************/
  103. /****************************************************************/
  104. #define ZFCP_LOG_AREA ZFCP_LOG_AREA_OTHER
  105. /**
  106. * zfcp_device_setup - setup function
  107. * @str: pointer to parameter string
  108. *
  109. * Parse "device=..." parameter string.
  110. */
  111. static int __init
  112. zfcp_device_setup(char *devstr)
  113. {
  114. char *tmp, *str;
  115. size_t len;
  116. if (!devstr)
  117. return 0;
  118. len = strlen(devstr) + 1;
  119. str = kmalloc(len, GFP_KERNEL);
  120. if (!str)
  121. goto err_out;
  122. memcpy(str, devstr, len);
  123. tmp = strchr(str, ',');
  124. if (!tmp)
  125. goto err_out;
  126. *tmp++ = '\0';
  127. strncpy(zfcp_data.init_busid, str, BUS_ID_SIZE);
  128. zfcp_data.init_busid[BUS_ID_SIZE-1] = '\0';
  129. zfcp_data.init_wwpn = simple_strtoull(tmp, &tmp, 0);
  130. if (*tmp++ != ',')
  131. goto err_out;
  132. if (*tmp == '\0')
  133. goto err_out;
  134. zfcp_data.init_fcp_lun = simple_strtoull(tmp, &tmp, 0);
  135. if (*tmp != '\0')
  136. goto err_out;
  137. kfree(str);
  138. return 1;
  139. err_out:
  140. ZFCP_LOG_NORMAL("Parse error for device parameter string %s\n", str);
  141. kfree(str);
  142. return 0;
  143. }
  144. static void __init
  145. zfcp_init_device_configure(void)
  146. {
  147. struct zfcp_adapter *adapter;
  148. struct zfcp_port *port;
  149. struct zfcp_unit *unit;
  150. down(&zfcp_data.config_sema);
  151. read_lock_irq(&zfcp_data.config_lock);
  152. adapter = zfcp_get_adapter_by_busid(zfcp_data.init_busid);
  153. if (adapter)
  154. zfcp_adapter_get(adapter);
  155. read_unlock_irq(&zfcp_data.config_lock);
  156. if (adapter == NULL)
  157. goto out_adapter;
  158. port = zfcp_port_enqueue(adapter, zfcp_data.init_wwpn, 0, 0);
  159. if (!port)
  160. goto out_port;
  161. unit = zfcp_unit_enqueue(port, zfcp_data.init_fcp_lun);
  162. if (!unit)
  163. goto out_unit;
  164. up(&zfcp_data.config_sema);
  165. ccw_device_set_online(adapter->ccw_device);
  166. zfcp_erp_wait(adapter);
  167. down(&zfcp_data.config_sema);
  168. zfcp_unit_put(unit);
  169. out_unit:
  170. zfcp_port_put(port);
  171. out_port:
  172. zfcp_adapter_put(adapter);
  173. out_adapter:
  174. up(&zfcp_data.config_sema);
  175. return;
  176. }
  177. static int calc_alignment(int size)
  178. {
  179. int align = 1;
  180. if (!size)
  181. return 0;
  182. while ((size - align) > 0)
  183. align <<= 1;
  184. return align;
  185. }
  186. static int __init
  187. zfcp_module_init(void)
  188. {
  189. int retval = -ENOMEM;
  190. int size, align;
  191. size = sizeof(struct zfcp_fsf_req_qtcb);
  192. align = calc_alignment(size);
  193. zfcp_data.fsf_req_qtcb_cache =
  194. kmem_cache_create("zfcp_fsf", size, align, 0, NULL);
  195. if (!zfcp_data.fsf_req_qtcb_cache)
  196. goto out;
  197. size = sizeof(struct fsf_status_read_buffer);
  198. align = calc_alignment(size);
  199. zfcp_data.sr_buffer_cache =
  200. kmem_cache_create("zfcp_sr", size, align, 0, NULL);
  201. if (!zfcp_data.sr_buffer_cache)
  202. goto out_sr_cache;
  203. size = sizeof(struct zfcp_gid_pn_data);
  204. align = calc_alignment(size);
  205. zfcp_data.gid_pn_cache =
  206. kmem_cache_create("zfcp_gid", size, align, 0, NULL);
  207. if (!zfcp_data.gid_pn_cache)
  208. goto out_gid_cache;
  209. atomic_set(&zfcp_data.loglevel, loglevel);
  210. /* initialize adapter list */
  211. INIT_LIST_HEAD(&zfcp_data.adapter_list_head);
  212. /* initialize adapters to be removed list head */
  213. INIT_LIST_HEAD(&zfcp_data.adapter_remove_lh);
  214. zfcp_data.scsi_transport_template =
  215. fc_attach_transport(&zfcp_transport_functions);
  216. if (!zfcp_data.scsi_transport_template)
  217. goto out_transport;
  218. retval = misc_register(&zfcp_cfdc_misc);
  219. if (retval != 0) {
  220. ZFCP_LOG_INFO("registration of misc device "
  221. "zfcp_cfdc failed\n");
  222. goto out_misc;
  223. }
  224. /* Initialise proc semaphores */
  225. sema_init(&zfcp_data.config_sema, 1);
  226. /* initialise configuration rw lock */
  227. rwlock_init(&zfcp_data.config_lock);
  228. /* setup dynamic I/O */
  229. retval = zfcp_ccw_register();
  230. if (retval) {
  231. ZFCP_LOG_NORMAL("registration with common I/O layer failed\n");
  232. goto out_ccw_register;
  233. }
  234. if (zfcp_device_setup(device))
  235. zfcp_init_device_configure();
  236. goto out;
  237. out_ccw_register:
  238. misc_deregister(&zfcp_cfdc_misc);
  239. out_misc:
  240. fc_release_transport(zfcp_data.scsi_transport_template);
  241. out_transport:
  242. kmem_cache_destroy(zfcp_data.gid_pn_cache);
  243. out_gid_cache:
  244. kmem_cache_destroy(zfcp_data.sr_buffer_cache);
  245. out_sr_cache:
  246. kmem_cache_destroy(zfcp_data.fsf_req_qtcb_cache);
  247. out:
  248. return retval;
  249. }
  250. #undef ZFCP_LOG_AREA
  251. /****************************************************************/
  252. /****** Functions for configuration/set-up of structures ********/
  253. /****************************************************************/
  254. #define ZFCP_LOG_AREA ZFCP_LOG_AREA_CONFIG
  255. /**
  256. * zfcp_get_unit_by_lun - find unit in unit list of port by FCP LUN
  257. * @port: pointer to port to search for unit
  258. * @fcp_lun: FCP LUN to search for
  259. * Traverse list of all units of a port and return pointer to a unit
  260. * with the given FCP LUN.
  261. */
  262. struct zfcp_unit *
  263. zfcp_get_unit_by_lun(struct zfcp_port *port, fcp_lun_t fcp_lun)
  264. {
  265. struct zfcp_unit *unit;
  266. int found = 0;
  267. list_for_each_entry(unit, &port->unit_list_head, list) {
  268. if ((unit->fcp_lun == fcp_lun) &&
  269. !atomic_test_mask(ZFCP_STATUS_COMMON_REMOVE, &unit->status))
  270. {
  271. found = 1;
  272. break;
  273. }
  274. }
  275. return found ? unit : NULL;
  276. }
  277. /**
  278. * zfcp_get_port_by_wwpn - find port in port list of adapter by wwpn
  279. * @adapter: pointer to adapter to search for port
  280. * @wwpn: wwpn to search for
  281. * Traverse list of all ports of an adapter and return pointer to a port
  282. * with the given wwpn.
  283. */
  284. struct zfcp_port *
  285. zfcp_get_port_by_wwpn(struct zfcp_adapter *adapter, wwn_t wwpn)
  286. {
  287. struct zfcp_port *port;
  288. int found = 0;
  289. list_for_each_entry(port, &adapter->port_list_head, list) {
  290. if ((port->wwpn == wwpn) &&
  291. !(atomic_read(&port->status) &
  292. (ZFCP_STATUS_PORT_NO_WWPN | ZFCP_STATUS_COMMON_REMOVE))) {
  293. found = 1;
  294. break;
  295. }
  296. }
  297. return found ? port : NULL;
  298. }
  299. /**
  300. * zfcp_get_port_by_did - find port in port list of adapter by d_id
  301. * @adapter: pointer to adapter to search for port
  302. * @d_id: d_id to search for
  303. * Traverse list of all ports of an adapter and return pointer to a port
  304. * with the given d_id.
  305. */
  306. struct zfcp_port *
  307. zfcp_get_port_by_did(struct zfcp_adapter *adapter, u32 d_id)
  308. {
  309. struct zfcp_port *port;
  310. int found = 0;
  311. list_for_each_entry(port, &adapter->port_list_head, list) {
  312. if ((port->d_id == d_id) &&
  313. !atomic_test_mask(ZFCP_STATUS_COMMON_REMOVE, &port->status))
  314. {
  315. found = 1;
  316. break;
  317. }
  318. }
  319. return found ? port : NULL;
  320. }
  321. /**
  322. * zfcp_get_adapter_by_busid - find adpater in adapter list by bus_id
  323. * @bus_id: bus_id to search for
  324. * Traverse list of all adapters and return pointer to an adapter
  325. * with the given bus_id.
  326. */
  327. struct zfcp_adapter *
  328. zfcp_get_adapter_by_busid(char *bus_id)
  329. {
  330. struct zfcp_adapter *adapter;
  331. int found = 0;
  332. list_for_each_entry(adapter, &zfcp_data.adapter_list_head, list) {
  333. if ((strncmp(bus_id, zfcp_get_busid_by_adapter(adapter),
  334. BUS_ID_SIZE) == 0) &&
  335. !atomic_test_mask(ZFCP_STATUS_COMMON_REMOVE,
  336. &adapter->status)){
  337. found = 1;
  338. break;
  339. }
  340. }
  341. return found ? adapter : NULL;
  342. }
  343. /**
  344. * zfcp_unit_enqueue - enqueue unit to unit list of a port.
  345. * @port: pointer to port where unit is added
  346. * @fcp_lun: FCP LUN of unit to be enqueued
  347. * Return: pointer to enqueued unit on success, NULL on error
  348. * Locks: config_sema must be held to serialize changes to the unit list
  349. *
  350. * Sets up some unit internal structures and creates sysfs entry.
  351. */
  352. struct zfcp_unit *
  353. zfcp_unit_enqueue(struct zfcp_port *port, fcp_lun_t fcp_lun)
  354. {
  355. struct zfcp_unit *unit;
  356. /*
  357. * check that there is no unit with this FCP_LUN already in list
  358. * and enqueue it.
  359. * Note: Unlike for the adapter and the port, this is an error
  360. */
  361. read_lock_irq(&zfcp_data.config_lock);
  362. unit = zfcp_get_unit_by_lun(port, fcp_lun);
  363. read_unlock_irq(&zfcp_data.config_lock);
  364. if (unit)
  365. return NULL;
  366. unit = kzalloc(sizeof (struct zfcp_unit), GFP_KERNEL);
  367. if (!unit)
  368. return NULL;
  369. /* initialise reference count stuff */
  370. atomic_set(&unit->refcount, 0);
  371. init_waitqueue_head(&unit->remove_wq);
  372. unit->port = port;
  373. unit->fcp_lun = fcp_lun;
  374. /* setup for sysfs registration */
  375. snprintf(unit->sysfs_device.bus_id, BUS_ID_SIZE, "0x%016llx", fcp_lun);
  376. unit->sysfs_device.parent = &port->sysfs_device;
  377. unit->sysfs_device.release = zfcp_sysfs_unit_release;
  378. dev_set_drvdata(&unit->sysfs_device, unit);
  379. /* mark unit unusable as long as sysfs registration is not complete */
  380. atomic_set_mask(ZFCP_STATUS_COMMON_REMOVE, &unit->status);
  381. spin_lock_init(&unit->latencies.lock);
  382. unit->latencies.write.channel.min = 0xFFFFFFFF;
  383. unit->latencies.write.fabric.min = 0xFFFFFFFF;
  384. unit->latencies.read.channel.min = 0xFFFFFFFF;
  385. unit->latencies.read.fabric.min = 0xFFFFFFFF;
  386. unit->latencies.cmd.channel.min = 0xFFFFFFFF;
  387. unit->latencies.cmd.fabric.min = 0xFFFFFFFF;
  388. if (device_register(&unit->sysfs_device)) {
  389. kfree(unit);
  390. return NULL;
  391. }
  392. if (zfcp_sysfs_unit_create_files(&unit->sysfs_device)) {
  393. device_unregister(&unit->sysfs_device);
  394. return NULL;
  395. }
  396. zfcp_unit_get(unit);
  397. unit->scsi_lun = scsilun_to_int((struct scsi_lun *)&unit->fcp_lun);
  398. write_lock_irq(&zfcp_data.config_lock);
  399. list_add_tail(&unit->list, &port->unit_list_head);
  400. atomic_clear_mask(ZFCP_STATUS_COMMON_REMOVE, &unit->status);
  401. atomic_set_mask(ZFCP_STATUS_COMMON_RUNNING, &unit->status);
  402. write_unlock_irq(&zfcp_data.config_lock);
  403. port->units++;
  404. zfcp_port_get(port);
  405. return unit;
  406. }
  407. void
  408. zfcp_unit_dequeue(struct zfcp_unit *unit)
  409. {
  410. zfcp_unit_wait(unit);
  411. write_lock_irq(&zfcp_data.config_lock);
  412. list_del(&unit->list);
  413. write_unlock_irq(&zfcp_data.config_lock);
  414. unit->port->units--;
  415. zfcp_port_put(unit->port);
  416. zfcp_sysfs_unit_remove_files(&unit->sysfs_device);
  417. device_unregister(&unit->sysfs_device);
  418. }
  419. /*
  420. * Allocates a combined QTCB/fsf_req buffer for erp actions and fcp/SCSI
  421. * commands.
  422. * It also genrates fcp-nameserver request/response buffer and unsolicited
  423. * status read fsf_req buffers.
  424. *
  425. * locks: must only be called with zfcp_data.config_sema taken
  426. */
  427. static int
  428. zfcp_allocate_low_mem_buffers(struct zfcp_adapter *adapter)
  429. {
  430. adapter->pool.fsf_req_erp =
  431. mempool_create_slab_pool(ZFCP_POOL_FSF_REQ_ERP_NR,
  432. zfcp_data.fsf_req_qtcb_cache);
  433. if (!adapter->pool.fsf_req_erp)
  434. return -ENOMEM;
  435. adapter->pool.fsf_req_scsi =
  436. mempool_create_slab_pool(ZFCP_POOL_FSF_REQ_SCSI_NR,
  437. zfcp_data.fsf_req_qtcb_cache);
  438. if (!adapter->pool.fsf_req_scsi)
  439. return -ENOMEM;
  440. adapter->pool.fsf_req_abort =
  441. mempool_create_slab_pool(ZFCP_POOL_FSF_REQ_ABORT_NR,
  442. zfcp_data.fsf_req_qtcb_cache);
  443. if (!adapter->pool.fsf_req_abort)
  444. return -ENOMEM;
  445. adapter->pool.fsf_req_status_read =
  446. mempool_create_kmalloc_pool(ZFCP_POOL_STATUS_READ_NR,
  447. sizeof(struct zfcp_fsf_req));
  448. if (!adapter->pool.fsf_req_status_read)
  449. return -ENOMEM;
  450. adapter->pool.data_status_read =
  451. mempool_create_slab_pool(ZFCP_POOL_STATUS_READ_NR,
  452. zfcp_data.sr_buffer_cache);
  453. if (!adapter->pool.data_status_read)
  454. return -ENOMEM;
  455. adapter->pool.data_gid_pn =
  456. mempool_create_slab_pool(ZFCP_POOL_DATA_GID_PN_NR,
  457. zfcp_data.gid_pn_cache);
  458. if (!adapter->pool.data_gid_pn)
  459. return -ENOMEM;
  460. return 0;
  461. }
  462. /**
  463. * zfcp_free_low_mem_buffers - free memory pools of an adapter
  464. * @adapter: pointer to zfcp_adapter for which memory pools should be freed
  465. * locking: zfcp_data.config_sema must be held
  466. */
  467. static void
  468. zfcp_free_low_mem_buffers(struct zfcp_adapter *adapter)
  469. {
  470. if (adapter->pool.fsf_req_erp)
  471. mempool_destroy(adapter->pool.fsf_req_erp);
  472. if (adapter->pool.fsf_req_scsi)
  473. mempool_destroy(adapter->pool.fsf_req_scsi);
  474. if (adapter->pool.fsf_req_abort)
  475. mempool_destroy(adapter->pool.fsf_req_abort);
  476. if (adapter->pool.fsf_req_status_read)
  477. mempool_destroy(adapter->pool.fsf_req_status_read);
  478. if (adapter->pool.data_status_read)
  479. mempool_destroy(adapter->pool.data_status_read);
  480. if (adapter->pool.data_gid_pn)
  481. mempool_destroy(adapter->pool.data_gid_pn);
  482. }
  483. static void zfcp_dummy_release(struct device *dev)
  484. {
  485. return;
  486. }
  487. int zfcp_status_read_refill(struct zfcp_adapter *adapter)
  488. {
  489. while (atomic_read(&adapter->stat_miss) > 0)
  490. if (zfcp_fsf_status_read(adapter, ZFCP_WAIT_FOR_SBAL))
  491. break;
  492. else
  493. atomic_dec(&adapter->stat_miss);
  494. if (ZFCP_STATUS_READS_RECOM <= atomic_read(&adapter->stat_miss)) {
  495. zfcp_erp_adapter_reopen(adapter, 0, 103, NULL);
  496. return 1;
  497. }
  498. return 0;
  499. }
  500. static void _zfcp_status_read_scheduler(struct work_struct *work)
  501. {
  502. zfcp_status_read_refill(container_of(work, struct zfcp_adapter,
  503. stat_work));
  504. }
  505. /*
  506. * Enqueues an adapter at the end of the adapter list in the driver data.
  507. * All adapter internal structures are set up.
  508. * Proc-fs entries are also created.
  509. *
  510. * returns: 0 if a new adapter was successfully enqueued
  511. * ZFCP_KNOWN if an adapter with this devno was already present
  512. * -ENOMEM if alloc failed
  513. * locks: config_sema must be held to serialise changes to the adapter list
  514. */
  515. struct zfcp_adapter *
  516. zfcp_adapter_enqueue(struct ccw_device *ccw_device)
  517. {
  518. int retval = 0;
  519. struct zfcp_adapter *adapter;
  520. /*
  521. * Note: It is safe to release the list_lock, as any list changes
  522. * are protected by the config_sema, which must be held to get here
  523. */
  524. /* try to allocate new adapter data structure (zeroed) */
  525. adapter = kzalloc(sizeof (struct zfcp_adapter), GFP_KERNEL);
  526. if (!adapter) {
  527. ZFCP_LOG_INFO("error: allocation of base adapter "
  528. "structure failed\n");
  529. goto out;
  530. }
  531. ccw_device->handler = NULL;
  532. /* save ccw_device pointer */
  533. adapter->ccw_device = ccw_device;
  534. retval = zfcp_qdio_allocate_queues(adapter);
  535. if (retval)
  536. goto queues_alloc_failed;
  537. retval = zfcp_qdio_allocate(adapter);
  538. if (retval)
  539. goto qdio_allocate_failed;
  540. retval = zfcp_allocate_low_mem_buffers(adapter);
  541. if (retval) {
  542. ZFCP_LOG_INFO("error: pool allocation failed\n");
  543. goto failed_low_mem_buffers;
  544. }
  545. /* initialise reference count stuff */
  546. atomic_set(&adapter->refcount, 0);
  547. init_waitqueue_head(&adapter->remove_wq);
  548. /* initialise list of ports */
  549. INIT_LIST_HEAD(&adapter->port_list_head);
  550. /* initialise list of ports to be removed */
  551. INIT_LIST_HEAD(&adapter->port_remove_lh);
  552. /* initialize list of fsf requests */
  553. spin_lock_init(&adapter->req_list_lock);
  554. retval = zfcp_reqlist_alloc(adapter);
  555. if (retval) {
  556. ZFCP_LOG_INFO("request list initialization failed\n");
  557. goto failed_low_mem_buffers;
  558. }
  559. /* initialize debug locks */
  560. spin_lock_init(&adapter->hba_dbf_lock);
  561. spin_lock_init(&adapter->san_dbf_lock);
  562. spin_lock_init(&adapter->scsi_dbf_lock);
  563. spin_lock_init(&adapter->rec_dbf_lock);
  564. retval = zfcp_adapter_debug_register(adapter);
  565. if (retval)
  566. goto debug_register_failed;
  567. /* initialize error recovery stuff */
  568. rwlock_init(&adapter->erp_lock);
  569. sema_init(&adapter->erp_ready_sem, 0);
  570. INIT_LIST_HEAD(&adapter->erp_ready_head);
  571. INIT_LIST_HEAD(&adapter->erp_running_head);
  572. /* initialize abort lock */
  573. rwlock_init(&adapter->abort_lock);
  574. /* initialise some erp stuff */
  575. init_waitqueue_head(&adapter->erp_thread_wqh);
  576. init_waitqueue_head(&adapter->erp_done_wqh);
  577. /* initialize lock of associated request queue */
  578. rwlock_init(&adapter->request_queue.queue_lock);
  579. INIT_WORK(&adapter->stat_work, _zfcp_status_read_scheduler);
  580. /* mark adapter unusable as long as sysfs registration is not complete */
  581. atomic_set_mask(ZFCP_STATUS_COMMON_REMOVE, &adapter->status);
  582. dev_set_drvdata(&ccw_device->dev, adapter);
  583. if (zfcp_sysfs_adapter_create_files(&ccw_device->dev))
  584. goto sysfs_failed;
  585. adapter->generic_services.parent = &adapter->ccw_device->dev;
  586. adapter->generic_services.release = zfcp_dummy_release;
  587. snprintf(adapter->generic_services.bus_id, BUS_ID_SIZE,
  588. "generic_services");
  589. if (device_register(&adapter->generic_services))
  590. goto generic_services_failed;
  591. /* put allocated adapter at list tail */
  592. write_lock_irq(&zfcp_data.config_lock);
  593. atomic_clear_mask(ZFCP_STATUS_COMMON_REMOVE, &adapter->status);
  594. list_add_tail(&adapter->list, &zfcp_data.adapter_list_head);
  595. write_unlock_irq(&zfcp_data.config_lock);
  596. zfcp_data.adapters++;
  597. goto out;
  598. generic_services_failed:
  599. zfcp_sysfs_adapter_remove_files(&adapter->ccw_device->dev);
  600. sysfs_failed:
  601. zfcp_adapter_debug_unregister(adapter);
  602. debug_register_failed:
  603. dev_set_drvdata(&ccw_device->dev, NULL);
  604. zfcp_reqlist_free(adapter);
  605. failed_low_mem_buffers:
  606. zfcp_free_low_mem_buffers(adapter);
  607. if (qdio_free(ccw_device) != 0)
  608. ZFCP_LOG_NORMAL("bug: qdio_free for adapter %s failed\n",
  609. zfcp_get_busid_by_adapter(adapter));
  610. qdio_allocate_failed:
  611. zfcp_qdio_free_queues(adapter);
  612. queues_alloc_failed:
  613. kfree(adapter);
  614. adapter = NULL;
  615. out:
  616. return adapter;
  617. }
  618. /*
  619. * returns: 0 - struct zfcp_adapter data structure successfully removed
  620. * !0 - struct zfcp_adapter data structure could not be removed
  621. * (e.g. still used)
  622. * locks: adapter list write lock is assumed to be held by caller
  623. */
  624. void
  625. zfcp_adapter_dequeue(struct zfcp_adapter *adapter)
  626. {
  627. int retval = 0;
  628. unsigned long flags;
  629. cancel_work_sync(&adapter->stat_work);
  630. zfcp_adapter_scsi_unregister(adapter);
  631. device_unregister(&adapter->generic_services);
  632. zfcp_sysfs_adapter_remove_files(&adapter->ccw_device->dev);
  633. dev_set_drvdata(&adapter->ccw_device->dev, NULL);
  634. /* sanity check: no pending FSF requests */
  635. spin_lock_irqsave(&adapter->req_list_lock, flags);
  636. retval = zfcp_reqlist_isempty(adapter);
  637. spin_unlock_irqrestore(&adapter->req_list_lock, flags);
  638. if (!retval) {
  639. ZFCP_LOG_NORMAL("bug: adapter %s (%p) still in use, "
  640. "%i requests outstanding\n",
  641. zfcp_get_busid_by_adapter(adapter), adapter,
  642. atomic_read(&adapter->reqs_active));
  643. retval = -EBUSY;
  644. goto out;
  645. }
  646. zfcp_adapter_debug_unregister(adapter);
  647. /* remove specified adapter data structure from list */
  648. write_lock_irq(&zfcp_data.config_lock);
  649. list_del(&adapter->list);
  650. write_unlock_irq(&zfcp_data.config_lock);
  651. /* decrease number of adapters in list */
  652. zfcp_data.adapters--;
  653. ZFCP_LOG_TRACE("adapter %s (%p) removed from list, "
  654. "%i adapters still in list\n",
  655. zfcp_get_busid_by_adapter(adapter),
  656. adapter, zfcp_data.adapters);
  657. retval = qdio_free(adapter->ccw_device);
  658. if (retval)
  659. ZFCP_LOG_NORMAL("bug: qdio_free for adapter %s failed\n",
  660. zfcp_get_busid_by_adapter(adapter));
  661. zfcp_free_low_mem_buffers(adapter);
  662. /* free memory of adapter data structure and queues */
  663. zfcp_qdio_free_queues(adapter);
  664. zfcp_reqlist_free(adapter);
  665. kfree(adapter->fc_stats);
  666. kfree(adapter->stats_reset_data);
  667. ZFCP_LOG_TRACE("freeing adapter structure\n");
  668. kfree(adapter);
  669. out:
  670. return;
  671. }
  672. /**
  673. * zfcp_port_enqueue - enqueue port to port list of adapter
  674. * @adapter: adapter where remote port is added
  675. * @wwpn: WWPN of the remote port to be enqueued
  676. * @status: initial status for the port
  677. * @d_id: destination id of the remote port to be enqueued
  678. * Return: pointer to enqueued port on success, NULL on error
  679. * Locks: config_sema must be held to serialize changes to the port list
  680. *
  681. * All port internal structures are set up and the sysfs entry is generated.
  682. * d_id is used to enqueue ports with a well known address like the Directory
  683. * Service for nameserver lookup.
  684. */
  685. struct zfcp_port *
  686. zfcp_port_enqueue(struct zfcp_adapter *adapter, wwn_t wwpn, u32 status,
  687. u32 d_id)
  688. {
  689. struct zfcp_port *port;
  690. int check_wwpn;
  691. check_wwpn = !(status & ZFCP_STATUS_PORT_NO_WWPN);
  692. /*
  693. * check that there is no port with this WWPN already in list
  694. */
  695. if (check_wwpn) {
  696. read_lock_irq(&zfcp_data.config_lock);
  697. port = zfcp_get_port_by_wwpn(adapter, wwpn);
  698. read_unlock_irq(&zfcp_data.config_lock);
  699. if (port)
  700. return NULL;
  701. }
  702. port = kzalloc(sizeof (struct zfcp_port), GFP_KERNEL);
  703. if (!port)
  704. return NULL;
  705. /* initialise reference count stuff */
  706. atomic_set(&port->refcount, 0);
  707. init_waitqueue_head(&port->remove_wq);
  708. INIT_LIST_HEAD(&port->unit_list_head);
  709. INIT_LIST_HEAD(&port->unit_remove_lh);
  710. port->adapter = adapter;
  711. if (check_wwpn)
  712. port->wwpn = wwpn;
  713. atomic_set_mask(status, &port->status);
  714. /* setup for sysfs registration */
  715. if (status & ZFCP_STATUS_PORT_WKA) {
  716. switch (d_id) {
  717. case ZFCP_DID_DIRECTORY_SERVICE:
  718. snprintf(port->sysfs_device.bus_id, BUS_ID_SIZE,
  719. "directory");
  720. break;
  721. case ZFCP_DID_MANAGEMENT_SERVICE:
  722. snprintf(port->sysfs_device.bus_id, BUS_ID_SIZE,
  723. "management");
  724. break;
  725. case ZFCP_DID_KEY_DISTRIBUTION_SERVICE:
  726. snprintf(port->sysfs_device.bus_id, BUS_ID_SIZE,
  727. "key_distribution");
  728. break;
  729. case ZFCP_DID_ALIAS_SERVICE:
  730. snprintf(port->sysfs_device.bus_id, BUS_ID_SIZE,
  731. "alias");
  732. break;
  733. case ZFCP_DID_TIME_SERVICE:
  734. snprintf(port->sysfs_device.bus_id, BUS_ID_SIZE,
  735. "time");
  736. break;
  737. default:
  738. kfree(port);
  739. return NULL;
  740. }
  741. port->d_id = d_id;
  742. port->sysfs_device.parent = &adapter->generic_services;
  743. } else {
  744. snprintf(port->sysfs_device.bus_id,
  745. BUS_ID_SIZE, "0x%016llx", wwpn);
  746. port->sysfs_device.parent = &adapter->ccw_device->dev;
  747. }
  748. port->sysfs_device.release = zfcp_sysfs_port_release;
  749. dev_set_drvdata(&port->sysfs_device, port);
  750. /* mark port unusable as long as sysfs registration is not complete */
  751. atomic_set_mask(ZFCP_STATUS_COMMON_REMOVE, &port->status);
  752. if (device_register(&port->sysfs_device)) {
  753. kfree(port);
  754. return NULL;
  755. }
  756. if (zfcp_sysfs_port_create_files(&port->sysfs_device, status)) {
  757. device_unregister(&port->sysfs_device);
  758. return NULL;
  759. }
  760. zfcp_port_get(port);
  761. write_lock_irq(&zfcp_data.config_lock);
  762. list_add_tail(&port->list, &adapter->port_list_head);
  763. atomic_clear_mask(ZFCP_STATUS_COMMON_REMOVE, &port->status);
  764. atomic_set_mask(ZFCP_STATUS_COMMON_RUNNING, &port->status);
  765. if (d_id == ZFCP_DID_DIRECTORY_SERVICE)
  766. if (!adapter->nameserver_port)
  767. adapter->nameserver_port = port;
  768. adapter->ports++;
  769. write_unlock_irq(&zfcp_data.config_lock);
  770. zfcp_adapter_get(adapter);
  771. return port;
  772. }
  773. void
  774. zfcp_port_dequeue(struct zfcp_port *port)
  775. {
  776. zfcp_port_wait(port);
  777. write_lock_irq(&zfcp_data.config_lock);
  778. list_del(&port->list);
  779. port->adapter->ports--;
  780. write_unlock_irq(&zfcp_data.config_lock);
  781. if (port->rport)
  782. fc_remote_port_delete(port->rport);
  783. port->rport = NULL;
  784. zfcp_adapter_put(port->adapter);
  785. zfcp_sysfs_port_remove_files(&port->sysfs_device,
  786. atomic_read(&port->status));
  787. device_unregister(&port->sysfs_device);
  788. }
  789. /* Enqueues a nameserver port */
  790. int
  791. zfcp_nameserver_enqueue(struct zfcp_adapter *adapter)
  792. {
  793. struct zfcp_port *port;
  794. port = zfcp_port_enqueue(adapter, 0, ZFCP_STATUS_PORT_WKA,
  795. ZFCP_DID_DIRECTORY_SERVICE);
  796. if (!port) {
  797. ZFCP_LOG_INFO("error: enqueue of nameserver port for "
  798. "adapter %s failed\n",
  799. zfcp_get_busid_by_adapter(adapter));
  800. return -ENXIO;
  801. }
  802. zfcp_port_put(port);
  803. return 0;
  804. }
  805. void zfcp_sg_free_table(struct scatterlist *sg, int count)
  806. {
  807. int i;
  808. for (i = 0; i < count; i++, sg++)
  809. if (sg)
  810. free_page((unsigned long) sg_virt(sg));
  811. else
  812. break;
  813. }
  814. int zfcp_sg_setup_table(struct scatterlist *sg, int count)
  815. {
  816. void *addr;
  817. int i;
  818. sg_init_table(sg, count);
  819. for (i = 0; i < count; i++, sg++) {
  820. addr = (void *) get_zeroed_page(GFP_KERNEL);
  821. if (!addr) {
  822. zfcp_sg_free_table(sg, i);
  823. return -ENOMEM;
  824. }
  825. sg_set_buf(sg, addr, PAGE_SIZE);
  826. }
  827. return 0;
  828. }
  829. #undef ZFCP_LOG_AREA