zfcp_aux.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625
  1. /*
  2. * zfcp device driver
  3. *
  4. * Module interface and handling of zfcp data structures.
  5. *
  6. * Copyright IBM Corporation 2002, 2010
  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 <linux/slab.h>
  31. #include "zfcp_ext.h"
  32. #include "zfcp_fc.h"
  33. #include "zfcp_reqlist.h"
  34. #define ZFCP_BUS_ID_SIZE 20
  35. MODULE_AUTHOR("IBM Deutschland Entwicklung GmbH - linux390@de.ibm.com");
  36. MODULE_DESCRIPTION("FCP HBA driver");
  37. MODULE_LICENSE("GPL");
  38. static char *init_device;
  39. module_param_named(device, init_device, charp, 0400);
  40. MODULE_PARM_DESC(device, "specify initial device");
  41. static struct kmem_cache * __init zfcp_cache_hw_align(const char *name,
  42. unsigned long size)
  43. {
  44. return kmem_cache_create(name, size, roundup_pow_of_two(size), 0, NULL);
  45. }
  46. static void __init zfcp_init_device_configure(char *busid, u64 wwpn, u64 lun)
  47. {
  48. struct ccw_device *cdev;
  49. struct zfcp_adapter *adapter;
  50. struct zfcp_port *port;
  51. cdev = get_ccwdev_by_busid(&zfcp_ccw_driver, busid);
  52. if (!cdev)
  53. return;
  54. if (ccw_device_set_online(cdev))
  55. goto out_ccw_device;
  56. adapter = zfcp_ccw_adapter_by_cdev(cdev);
  57. if (!adapter)
  58. goto out_ccw_device;
  59. port = zfcp_get_port_by_wwpn(adapter, wwpn);
  60. if (!port)
  61. goto out_port;
  62. flush_work(&port->rport_work);
  63. zfcp_unit_add(port, lun);
  64. put_device(&port->dev);
  65. out_port:
  66. zfcp_ccw_adapter_put(adapter);
  67. out_ccw_device:
  68. put_device(&cdev->dev);
  69. return;
  70. }
  71. static void __init zfcp_init_device_setup(char *devstr)
  72. {
  73. char *token;
  74. char *str, *str_saved;
  75. char busid[ZFCP_BUS_ID_SIZE];
  76. u64 wwpn, lun;
  77. /* duplicate devstr and keep the original for sysfs presentation*/
  78. str_saved = kstrdup(devstr, GFP_KERNEL);
  79. str = str_saved;
  80. if (!str)
  81. return;
  82. token = strsep(&str, ",");
  83. if (!token || strlen(token) >= ZFCP_BUS_ID_SIZE)
  84. goto err_out;
  85. strncpy(busid, token, ZFCP_BUS_ID_SIZE);
  86. token = strsep(&str, ",");
  87. if (!token || strict_strtoull(token, 0, (unsigned long long *) &wwpn))
  88. goto err_out;
  89. token = strsep(&str, ",");
  90. if (!token || strict_strtoull(token, 0, (unsigned long long *) &lun))
  91. goto err_out;
  92. kfree(str_saved);
  93. zfcp_init_device_configure(busid, wwpn, lun);
  94. return;
  95. err_out:
  96. kfree(str_saved);
  97. pr_err("%s is not a valid SCSI device\n", devstr);
  98. }
  99. static int __init zfcp_module_init(void)
  100. {
  101. int retval = -ENOMEM;
  102. zfcp_data.gpn_ft_cache = zfcp_cache_hw_align("zfcp_gpn",
  103. sizeof(struct zfcp_fc_gpn_ft_req));
  104. if (!zfcp_data.gpn_ft_cache)
  105. goto out;
  106. zfcp_data.qtcb_cache = zfcp_cache_hw_align("zfcp_qtcb",
  107. sizeof(struct fsf_qtcb));
  108. if (!zfcp_data.qtcb_cache)
  109. goto out_qtcb_cache;
  110. zfcp_data.sr_buffer_cache = zfcp_cache_hw_align("zfcp_sr",
  111. sizeof(struct fsf_status_read_buffer));
  112. if (!zfcp_data.sr_buffer_cache)
  113. goto out_sr_cache;
  114. zfcp_data.gid_pn_cache = zfcp_cache_hw_align("zfcp_gid",
  115. sizeof(struct zfcp_fc_gid_pn));
  116. if (!zfcp_data.gid_pn_cache)
  117. goto out_gid_cache;
  118. zfcp_data.adisc_cache = zfcp_cache_hw_align("zfcp_adisc",
  119. sizeof(struct zfcp_fc_els_adisc));
  120. if (!zfcp_data.adisc_cache)
  121. goto out_adisc_cache;
  122. zfcp_data.scsi_transport_template =
  123. fc_attach_transport(&zfcp_transport_functions);
  124. if (!zfcp_data.scsi_transport_template)
  125. goto out_transport;
  126. scsi_transport_reserve_device(zfcp_data.scsi_transport_template,
  127. sizeof(struct zfcp_scsi_dev));
  128. retval = misc_register(&zfcp_cfdc_misc);
  129. if (retval) {
  130. pr_err("Registering the misc device zfcp_cfdc failed\n");
  131. goto out_misc;
  132. }
  133. retval = ccw_driver_register(&zfcp_ccw_driver);
  134. if (retval) {
  135. pr_err("The zfcp device driver could not register with "
  136. "the common I/O layer\n");
  137. goto out_ccw_register;
  138. }
  139. if (init_device)
  140. zfcp_init_device_setup(init_device);
  141. return 0;
  142. out_ccw_register:
  143. misc_deregister(&zfcp_cfdc_misc);
  144. out_misc:
  145. fc_release_transport(zfcp_data.scsi_transport_template);
  146. out_transport:
  147. kmem_cache_destroy(zfcp_data.adisc_cache);
  148. out_adisc_cache:
  149. kmem_cache_destroy(zfcp_data.gid_pn_cache);
  150. out_gid_cache:
  151. kmem_cache_destroy(zfcp_data.sr_buffer_cache);
  152. out_sr_cache:
  153. kmem_cache_destroy(zfcp_data.qtcb_cache);
  154. out_qtcb_cache:
  155. kmem_cache_destroy(zfcp_data.gpn_ft_cache);
  156. out:
  157. return retval;
  158. }
  159. module_init(zfcp_module_init);
  160. static void __exit zfcp_module_exit(void)
  161. {
  162. ccw_driver_unregister(&zfcp_ccw_driver);
  163. misc_deregister(&zfcp_cfdc_misc);
  164. fc_release_transport(zfcp_data.scsi_transport_template);
  165. kmem_cache_destroy(zfcp_data.adisc_cache);
  166. kmem_cache_destroy(zfcp_data.gid_pn_cache);
  167. kmem_cache_destroy(zfcp_data.sr_buffer_cache);
  168. kmem_cache_destroy(zfcp_data.qtcb_cache);
  169. kmem_cache_destroy(zfcp_data.gpn_ft_cache);
  170. }
  171. module_exit(zfcp_module_exit);
  172. /**
  173. * zfcp_get_port_by_wwpn - find port in port list of adapter by wwpn
  174. * @adapter: pointer to adapter to search for port
  175. * @wwpn: wwpn to search for
  176. *
  177. * Returns: pointer to zfcp_port or NULL
  178. */
  179. struct zfcp_port *zfcp_get_port_by_wwpn(struct zfcp_adapter *adapter,
  180. u64 wwpn)
  181. {
  182. unsigned long flags;
  183. struct zfcp_port *port;
  184. read_lock_irqsave(&adapter->port_list_lock, flags);
  185. list_for_each_entry(port, &adapter->port_list, list)
  186. if (port->wwpn == wwpn) {
  187. if (!get_device(&port->dev))
  188. port = NULL;
  189. read_unlock_irqrestore(&adapter->port_list_lock, flags);
  190. return port;
  191. }
  192. read_unlock_irqrestore(&adapter->port_list_lock, flags);
  193. return NULL;
  194. }
  195. static int zfcp_allocate_low_mem_buffers(struct zfcp_adapter *adapter)
  196. {
  197. adapter->pool.erp_req =
  198. mempool_create_kmalloc_pool(1, sizeof(struct zfcp_fsf_req));
  199. if (!adapter->pool.erp_req)
  200. return -ENOMEM;
  201. adapter->pool.gid_pn_req =
  202. mempool_create_kmalloc_pool(1, sizeof(struct zfcp_fsf_req));
  203. if (!adapter->pool.gid_pn_req)
  204. return -ENOMEM;
  205. adapter->pool.scsi_req =
  206. mempool_create_kmalloc_pool(1, sizeof(struct zfcp_fsf_req));
  207. if (!adapter->pool.scsi_req)
  208. return -ENOMEM;
  209. adapter->pool.scsi_abort =
  210. mempool_create_kmalloc_pool(1, sizeof(struct zfcp_fsf_req));
  211. if (!adapter->pool.scsi_abort)
  212. return -ENOMEM;
  213. adapter->pool.status_read_req =
  214. mempool_create_kmalloc_pool(FSF_STATUS_READS_RECOM,
  215. sizeof(struct zfcp_fsf_req));
  216. if (!adapter->pool.status_read_req)
  217. return -ENOMEM;
  218. adapter->pool.qtcb_pool =
  219. mempool_create_slab_pool(4, zfcp_data.qtcb_cache);
  220. if (!adapter->pool.qtcb_pool)
  221. return -ENOMEM;
  222. adapter->pool.status_read_data =
  223. mempool_create_slab_pool(FSF_STATUS_READS_RECOM,
  224. zfcp_data.sr_buffer_cache);
  225. if (!adapter->pool.status_read_data)
  226. return -ENOMEM;
  227. adapter->pool.gid_pn =
  228. mempool_create_slab_pool(1, zfcp_data.gid_pn_cache);
  229. if (!adapter->pool.gid_pn)
  230. return -ENOMEM;
  231. return 0;
  232. }
  233. static void zfcp_free_low_mem_buffers(struct zfcp_adapter *adapter)
  234. {
  235. if (adapter->pool.erp_req)
  236. mempool_destroy(adapter->pool.erp_req);
  237. if (adapter->pool.scsi_req)
  238. mempool_destroy(adapter->pool.scsi_req);
  239. if (adapter->pool.scsi_abort)
  240. mempool_destroy(adapter->pool.scsi_abort);
  241. if (adapter->pool.qtcb_pool)
  242. mempool_destroy(adapter->pool.qtcb_pool);
  243. if (adapter->pool.status_read_req)
  244. mempool_destroy(adapter->pool.status_read_req);
  245. if (adapter->pool.status_read_data)
  246. mempool_destroy(adapter->pool.status_read_data);
  247. if (adapter->pool.gid_pn)
  248. mempool_destroy(adapter->pool.gid_pn);
  249. }
  250. /**
  251. * zfcp_status_read_refill - refill the long running status_read_requests
  252. * @adapter: ptr to struct zfcp_adapter for which the buffers should be refilled
  253. *
  254. * Returns: 0 on success, 1 otherwise
  255. *
  256. * if there are 16 or more status_read requests missing an adapter_reopen
  257. * is triggered
  258. */
  259. int zfcp_status_read_refill(struct zfcp_adapter *adapter)
  260. {
  261. while (atomic_read(&adapter->stat_miss) > 0)
  262. if (zfcp_fsf_status_read(adapter->qdio)) {
  263. if (atomic_read(&adapter->stat_miss) >=
  264. adapter->stat_read_buf_num) {
  265. zfcp_erp_adapter_reopen(adapter, 0, "axsref1");
  266. return 1;
  267. }
  268. break;
  269. } else
  270. atomic_dec(&adapter->stat_miss);
  271. return 0;
  272. }
  273. static void _zfcp_status_read_scheduler(struct work_struct *work)
  274. {
  275. zfcp_status_read_refill(container_of(work, struct zfcp_adapter,
  276. stat_work));
  277. }
  278. static void zfcp_print_sl(struct seq_file *m, struct service_level *sl)
  279. {
  280. struct zfcp_adapter *adapter =
  281. container_of(sl, struct zfcp_adapter, service_level);
  282. seq_printf(m, "zfcp: %s microcode level %x\n",
  283. dev_name(&adapter->ccw_device->dev),
  284. adapter->fsf_lic_version);
  285. }
  286. static int zfcp_setup_adapter_work_queue(struct zfcp_adapter *adapter)
  287. {
  288. char name[TASK_COMM_LEN];
  289. snprintf(name, sizeof(name), "zfcp_q_%s",
  290. dev_name(&adapter->ccw_device->dev));
  291. adapter->work_queue = create_singlethread_workqueue(name);
  292. if (adapter->work_queue)
  293. return 0;
  294. return -ENOMEM;
  295. }
  296. static void zfcp_destroy_adapter_work_queue(struct zfcp_adapter *adapter)
  297. {
  298. if (adapter->work_queue)
  299. destroy_workqueue(adapter->work_queue);
  300. adapter->work_queue = NULL;
  301. }
  302. /**
  303. * zfcp_adapter_enqueue - enqueue a new adapter to the list
  304. * @ccw_device: pointer to the struct cc_device
  305. *
  306. * Returns: struct zfcp_adapter*
  307. * Enqueues an adapter at the end of the adapter list in the driver data.
  308. * All adapter internal structures are set up.
  309. * Proc-fs entries are also created.
  310. */
  311. struct zfcp_adapter *zfcp_adapter_enqueue(struct ccw_device *ccw_device)
  312. {
  313. struct zfcp_adapter *adapter;
  314. if (!get_device(&ccw_device->dev))
  315. return ERR_PTR(-ENODEV);
  316. adapter = kzalloc(sizeof(struct zfcp_adapter), GFP_KERNEL);
  317. if (!adapter) {
  318. put_device(&ccw_device->dev);
  319. return ERR_PTR(-ENOMEM);
  320. }
  321. kref_init(&adapter->ref);
  322. ccw_device->handler = NULL;
  323. adapter->ccw_device = ccw_device;
  324. INIT_WORK(&adapter->stat_work, _zfcp_status_read_scheduler);
  325. INIT_WORK(&adapter->scan_work, zfcp_fc_scan_ports);
  326. if (zfcp_qdio_setup(adapter))
  327. goto failed;
  328. if (zfcp_allocate_low_mem_buffers(adapter))
  329. goto failed;
  330. adapter->req_list = zfcp_reqlist_alloc();
  331. if (!adapter->req_list)
  332. goto failed;
  333. if (zfcp_dbf_adapter_register(adapter))
  334. goto failed;
  335. if (zfcp_setup_adapter_work_queue(adapter))
  336. goto failed;
  337. if (zfcp_fc_gs_setup(adapter))
  338. goto failed;
  339. rwlock_init(&adapter->port_list_lock);
  340. INIT_LIST_HEAD(&adapter->port_list);
  341. INIT_LIST_HEAD(&adapter->events.list);
  342. INIT_WORK(&adapter->events.work, zfcp_fc_post_event);
  343. spin_lock_init(&adapter->events.list_lock);
  344. init_waitqueue_head(&adapter->erp_ready_wq);
  345. init_waitqueue_head(&adapter->erp_done_wqh);
  346. INIT_LIST_HEAD(&adapter->erp_ready_head);
  347. INIT_LIST_HEAD(&adapter->erp_running_head);
  348. rwlock_init(&adapter->erp_lock);
  349. rwlock_init(&adapter->abort_lock);
  350. if (zfcp_erp_thread_setup(adapter))
  351. goto failed;
  352. adapter->service_level.seq_print = zfcp_print_sl;
  353. dev_set_drvdata(&ccw_device->dev, adapter);
  354. if (sysfs_create_group(&ccw_device->dev.kobj,
  355. &zfcp_sysfs_adapter_attrs))
  356. goto failed;
  357. /* report size limit per scatter-gather segment */
  358. adapter->dma_parms.max_segment_size = ZFCP_QDIO_SBALE_LEN;
  359. adapter->ccw_device->dev.dma_parms = &adapter->dma_parms;
  360. if (!zfcp_adapter_scsi_register(adapter))
  361. return adapter;
  362. failed:
  363. zfcp_adapter_unregister(adapter);
  364. return ERR_PTR(-ENOMEM);
  365. }
  366. void zfcp_adapter_unregister(struct zfcp_adapter *adapter)
  367. {
  368. struct ccw_device *cdev = adapter->ccw_device;
  369. cancel_work_sync(&adapter->scan_work);
  370. cancel_work_sync(&adapter->stat_work);
  371. zfcp_destroy_adapter_work_queue(adapter);
  372. zfcp_fc_wka_ports_force_offline(adapter->gs);
  373. zfcp_adapter_scsi_unregister(adapter);
  374. sysfs_remove_group(&cdev->dev.kobj, &zfcp_sysfs_adapter_attrs);
  375. zfcp_erp_thread_kill(adapter);
  376. zfcp_dbf_adapter_unregister(adapter);
  377. zfcp_qdio_destroy(adapter->qdio);
  378. zfcp_ccw_adapter_put(adapter); /* final put to release */
  379. }
  380. /**
  381. * zfcp_adapter_release - remove the adapter from the resource list
  382. * @ref: pointer to struct kref
  383. * locks: adapter list write lock is assumed to be held by caller
  384. */
  385. void zfcp_adapter_release(struct kref *ref)
  386. {
  387. struct zfcp_adapter *adapter = container_of(ref, struct zfcp_adapter,
  388. ref);
  389. struct ccw_device *cdev = adapter->ccw_device;
  390. dev_set_drvdata(&adapter->ccw_device->dev, NULL);
  391. zfcp_fc_gs_destroy(adapter);
  392. zfcp_free_low_mem_buffers(adapter);
  393. kfree(adapter->req_list);
  394. kfree(adapter->fc_stats);
  395. kfree(adapter->stats_reset_data);
  396. kfree(adapter);
  397. put_device(&cdev->dev);
  398. }
  399. /**
  400. * zfcp_device_unregister - remove port, unit from system
  401. * @dev: reference to device which is to be removed
  402. * @grp: related reference to attribute group
  403. *
  404. * Helper function to unregister port, unit from system
  405. */
  406. void zfcp_device_unregister(struct device *dev,
  407. const struct attribute_group *grp)
  408. {
  409. sysfs_remove_group(&dev->kobj, grp);
  410. device_unregister(dev);
  411. }
  412. static void zfcp_port_release(struct device *dev)
  413. {
  414. struct zfcp_port *port = container_of(dev, struct zfcp_port, dev);
  415. zfcp_ccw_adapter_put(port->adapter);
  416. kfree(port);
  417. }
  418. /**
  419. * zfcp_port_enqueue - enqueue port to port list of adapter
  420. * @adapter: adapter where remote port is added
  421. * @wwpn: WWPN of the remote port to be enqueued
  422. * @status: initial status for the port
  423. * @d_id: destination id of the remote port to be enqueued
  424. * Returns: pointer to enqueued port on success, ERR_PTR on error
  425. *
  426. * All port internal structures are set up and the sysfs entry is generated.
  427. * d_id is used to enqueue ports with a well known address like the Directory
  428. * Service for nameserver lookup.
  429. */
  430. struct zfcp_port *zfcp_port_enqueue(struct zfcp_adapter *adapter, u64 wwpn,
  431. u32 status, u32 d_id)
  432. {
  433. struct zfcp_port *port;
  434. int retval = -ENOMEM;
  435. kref_get(&adapter->ref);
  436. port = zfcp_get_port_by_wwpn(adapter, wwpn);
  437. if (port) {
  438. put_device(&port->dev);
  439. retval = -EEXIST;
  440. goto err_out;
  441. }
  442. port = kzalloc(sizeof(struct zfcp_port), GFP_KERNEL);
  443. if (!port)
  444. goto err_out;
  445. rwlock_init(&port->unit_list_lock);
  446. INIT_LIST_HEAD(&port->unit_list);
  447. INIT_WORK(&port->gid_pn_work, zfcp_fc_port_did_lookup);
  448. INIT_WORK(&port->test_link_work, zfcp_fc_link_test_work);
  449. INIT_WORK(&port->rport_work, zfcp_scsi_rport_work);
  450. port->adapter = adapter;
  451. port->d_id = d_id;
  452. port->wwpn = wwpn;
  453. port->rport_task = RPORT_NONE;
  454. port->dev.parent = &adapter->ccw_device->dev;
  455. port->dev.release = zfcp_port_release;
  456. if (dev_set_name(&port->dev, "0x%016llx", (unsigned long long)wwpn)) {
  457. kfree(port);
  458. goto err_out;
  459. }
  460. retval = -EINVAL;
  461. if (device_register(&port->dev)) {
  462. put_device(&port->dev);
  463. goto err_out;
  464. }
  465. if (sysfs_create_group(&port->dev.kobj,
  466. &zfcp_sysfs_port_attrs))
  467. goto err_out_put;
  468. write_lock_irq(&adapter->port_list_lock);
  469. list_add_tail(&port->list, &adapter->port_list);
  470. write_unlock_irq(&adapter->port_list_lock);
  471. atomic_set_mask(status | ZFCP_STATUS_COMMON_RUNNING, &port->status);
  472. return port;
  473. err_out_put:
  474. device_unregister(&port->dev);
  475. err_out:
  476. zfcp_ccw_adapter_put(adapter);
  477. return ERR_PTR(retval);
  478. }
  479. /**
  480. * zfcp_sg_free_table - free memory used by scatterlists
  481. * @sg: pointer to scatterlist
  482. * @count: number of scatterlist which are to be free'ed
  483. * the scatterlist are expected to reference pages always
  484. */
  485. void zfcp_sg_free_table(struct scatterlist *sg, int count)
  486. {
  487. int i;
  488. for (i = 0; i < count; i++, sg++)
  489. if (sg)
  490. free_page((unsigned long) sg_virt(sg));
  491. else
  492. break;
  493. }
  494. /**
  495. * zfcp_sg_setup_table - init scatterlist and allocate, assign buffers
  496. * @sg: pointer to struct scatterlist
  497. * @count: number of scatterlists which should be assigned with buffers
  498. * of size page
  499. *
  500. * Returns: 0 on success, -ENOMEM otherwise
  501. */
  502. int zfcp_sg_setup_table(struct scatterlist *sg, int count)
  503. {
  504. void *addr;
  505. int i;
  506. sg_init_table(sg, count);
  507. for (i = 0; i < count; i++, sg++) {
  508. addr = (void *) get_zeroed_page(GFP_KERNEL);
  509. if (!addr) {
  510. zfcp_sg_free_table(sg, i);
  511. return -ENOMEM;
  512. }
  513. sg_set_buf(sg, addr, PAGE_SIZE);
  514. }
  515. return 0;
  516. }