zfcp_aux.c 15 KB

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