140bf411eSGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
21da177e4SLinus Torvalds /*
3553448f6SChristof Schmitt * zfcp device driver
41da177e4SLinus Torvalds *
5553448f6SChristof Schmitt * Module interface and handling of zfcp data structures.
61da177e4SLinus Torvalds *
7d0dff2acSBenjamin Block * Copyright IBM Corp. 2002, 2020
81da177e4SLinus Torvalds */
91da177e4SLinus Torvalds
104a9d2d8bSAndreas Herrmann /*
114a9d2d8bSAndreas Herrmann * Driver authors:
124a9d2d8bSAndreas Herrmann * Martin Peschke (originator of the driver)
134a9d2d8bSAndreas Herrmann * Raimund Schroeder
144a9d2d8bSAndreas Herrmann * Aron Zeh
154a9d2d8bSAndreas Herrmann * Wolfgang Taphorn
164a9d2d8bSAndreas Herrmann * Stefan Bader
174a9d2d8bSAndreas Herrmann * Heiko Carstens (kernel 2.6 port of the driver)
184a9d2d8bSAndreas Herrmann * Andreas Herrmann
194a9d2d8bSAndreas Herrmann * Maxim Shchetynin
204a9d2d8bSAndreas Herrmann * Volker Sameske
214a9d2d8bSAndreas Herrmann * Ralph Wuerthner
22553448f6SChristof Schmitt * Michael Loehr
23553448f6SChristof Schmitt * Swen Schillig
24553448f6SChristof Schmitt * Christof Schmitt
25553448f6SChristof Schmitt * Martin Petermann
26553448f6SChristof Schmitt * Sven Schuetz
279edf7d75SSteffen Maier * Steffen Maier
287e418833SBenjamin Block * Benjamin Block
294a9d2d8bSAndreas Herrmann */
304a9d2d8bSAndreas Herrmann
31ecf39d42SChristof Schmitt #define KMSG_COMPONENT "zfcp"
32ecf39d42SChristof Schmitt #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
33ecf39d42SChristof Schmitt
34bd43a42bSChristof Schmitt #include <linux/seq_file.h>
355a0e3ad6STejun Heo #include <linux/slab.h>
363a4c5d59SHeiko Carstens #include <linux/module.h>
371da177e4SLinus Torvalds #include "zfcp_ext.h"
38dbf5dfe9SChristof Schmitt #include "zfcp_fc.h"
39b6bd2fb9SChristof Schmitt #include "zfcp_reqlist.h"
407e418833SBenjamin Block #include "zfcp_diag.h"
411da177e4SLinus Torvalds
4298df67b3SKay Sievers #define ZFCP_BUS_ID_SIZE 20
4398df67b3SKay Sievers
444a9d2d8bSAndreas Herrmann MODULE_AUTHOR("IBM Deutschland Entwicklung GmbH - linux390@de.ibm.com");
45317e6b65SSwen Schillig MODULE_DESCRIPTION("FCP HBA driver");
461da177e4SLinus Torvalds MODULE_LICENSE("GPL");
471da177e4SLinus Torvalds
483623ecbaSChristof Schmitt static char *init_device;
493623ecbaSChristof Schmitt module_param_named(device, init_device, charp, 0400);
501da177e4SLinus Torvalds MODULE_PARM_DESC(device, "specify initial device");
511da177e4SLinus Torvalds
zfcp_cache_hw_align(const char * name,unsigned long size)5251780d2cSChristof Schmitt static struct kmem_cache * __init zfcp_cache_hw_align(const char *name,
53a4623c46SSwen Schillig unsigned long size)
54a4623c46SSwen Schillig {
55a4623c46SSwen Schillig return kmem_cache_create(name, size, roundup_pow_of_two(size), 0, NULL);
56a4623c46SSwen Schillig }
57a4623c46SSwen Schillig
zfcp_init_device_configure(char * busid,u64 wwpn,u64 lun)583623ecbaSChristof Schmitt static void __init zfcp_init_device_configure(char *busid, u64 wwpn, u64 lun)
591da177e4SLinus Torvalds {
60de3dc572SSwen Schillig struct ccw_device *cdev;
611da177e4SLinus Torvalds struct zfcp_adapter *adapter;
621da177e4SLinus Torvalds struct zfcp_port *port;
631da177e4SLinus Torvalds
64de3dc572SSwen Schillig cdev = get_ccwdev_by_busid(&zfcp_ccw_driver, busid);
65de3dc572SSwen Schillig if (!cdev)
66c5afd81eSChristof Schmitt return;
671da177e4SLinus Torvalds
68de3dc572SSwen Schillig if (ccw_device_set_online(cdev))
69de3dc572SSwen Schillig goto out_ccw_device;
70c5afd81eSChristof Schmitt
71de3dc572SSwen Schillig adapter = zfcp_ccw_adapter_by_cdev(cdev);
72317e6b65SSwen Schillig if (!adapter)
73de3dc572SSwen Schillig goto out_ccw_device;
74c5afd81eSChristof Schmitt
75c5afd81eSChristof Schmitt port = zfcp_get_port_by_wwpn(adapter, wwpn);
76c5afd81eSChristof Schmitt if (!port)
771da177e4SLinus Torvalds goto out_port;
7891978465SChristof Schmitt flush_work(&port->rport_work);
79091694a5SSwen Schillig
801daa4eb5SChristof Schmitt zfcp_unit_add(port, lun);
81615f59e0SChristof Schmitt put_device(&port->dev);
821daa4eb5SChristof Schmitt
831da177e4SLinus Torvalds out_port:
84de3dc572SSwen Schillig zfcp_ccw_adapter_put(adapter);
85de3dc572SSwen Schillig out_ccw_device:
86de3dc572SSwen Schillig put_device(&cdev->dev);
871da177e4SLinus Torvalds return;
881da177e4SLinus Torvalds }
891da177e4SLinus Torvalds
zfcp_init_device_setup(char * devstr)903623ecbaSChristof Schmitt static void __init zfcp_init_device_setup(char *devstr)
913623ecbaSChristof Schmitt {
923623ecbaSChristof Schmitt char *token;
93d10c0858SHeiko Carstens char *str, *str_saved;
943623ecbaSChristof Schmitt char busid[ZFCP_BUS_ID_SIZE];
953623ecbaSChristof Schmitt u64 wwpn, lun;
963623ecbaSChristof Schmitt
973623ecbaSChristof Schmitt /* duplicate devstr and keep the original for sysfs presentation*/
98674c3a99SChristof Schmitt str_saved = kstrdup(devstr, GFP_KERNEL);
99d10c0858SHeiko Carstens str = str_saved;
1003623ecbaSChristof Schmitt if (!str)
1013623ecbaSChristof Schmitt return;
1023623ecbaSChristof Schmitt
1033623ecbaSChristof Schmitt token = strsep(&str, ",");
1043623ecbaSChristof Schmitt if (!token || strlen(token) >= ZFCP_BUS_ID_SIZE)
1053623ecbaSChristof Schmitt goto err_out;
106820109fbSWolfram Sang strscpy(busid, token, ZFCP_BUS_ID_SIZE);
1073623ecbaSChristof Schmitt
1083623ecbaSChristof Schmitt token = strsep(&str, ",");
109ee732ea8SMartin Peschke if (!token || kstrtoull(token, 0, (unsigned long long *) &wwpn))
1103623ecbaSChristof Schmitt goto err_out;
1113623ecbaSChristof Schmitt
1123623ecbaSChristof Schmitt token = strsep(&str, ",");
113ee732ea8SMartin Peschke if (!token || kstrtoull(token, 0, (unsigned long long *) &lun))
1143623ecbaSChristof Schmitt goto err_out;
1153623ecbaSChristof Schmitt
116d10c0858SHeiko Carstens kfree(str_saved);
1173623ecbaSChristof Schmitt zfcp_init_device_configure(busid, wwpn, lun);
1183623ecbaSChristof Schmitt return;
1193623ecbaSChristof Schmitt
1203623ecbaSChristof Schmitt err_out:
121d10c0858SHeiko Carstens kfree(str_saved);
1223623ecbaSChristof Schmitt pr_err("%s is not a valid SCSI device\n", devstr);
1233623ecbaSChristof Schmitt }
1243623ecbaSChristof Schmitt
zfcp_module_init(void)125317e6b65SSwen Schillig static int __init zfcp_module_init(void)
1261da177e4SLinus Torvalds {
127dd52e0eaSHeiko Carstens int retval = -ENOMEM;
1281da177e4SLinus Torvalds
129636db60bSFedor Loshakov if (zfcp_experimental_dix)
130636db60bSFedor Loshakov pr_warn("DIX is enabled. It is experimental and might cause problems\n");
131636db60bSFedor Loshakov
132259afe2eSChristof Schmitt zfcp_fsf_qtcb_cache = zfcp_cache_hw_align("zfcp_fsf_qtcb",
133a4623c46SSwen Schillig sizeof(struct fsf_qtcb));
134259afe2eSChristof Schmitt if (!zfcp_fsf_qtcb_cache)
135a4623c46SSwen Schillig goto out_qtcb_cache;
136a4623c46SSwen Schillig
137087897e3SChristof Schmitt zfcp_fc_req_cache = zfcp_cache_hw_align("zfcp_fc_req",
138087897e3SChristof Schmitt sizeof(struct zfcp_fc_req));
139087897e3SChristof Schmitt if (!zfcp_fc_req_cache)
140087897e3SChristof Schmitt goto out_fc_cache;
141ee744622SChristof Schmitt
1421947c72aSChristof Schmitt zfcp_scsi_transport_template =
143dd52e0eaSHeiko Carstens fc_attach_transport(&zfcp_transport_functions);
1441947c72aSChristof Schmitt if (!zfcp_scsi_transport_template)
145dd52e0eaSHeiko Carstens goto out_transport;
1461947c72aSChristof Schmitt scsi_transport_reserve_device(zfcp_scsi_transport_template,
14757c23773SChristof Schmitt sizeof(struct zfcp_scsi_dev));
14857c23773SChristof Schmitt
149c1fad417SChristof Schmitt retval = ccw_driver_register(&zfcp_ccw_driver);
1501da177e4SLinus Torvalds if (retval) {
151ecf39d42SChristof Schmitt pr_err("The zfcp device driver could not register with "
152ff3b24faSChristof Schmitt "the common I/O layer\n");
1531da177e4SLinus Torvalds goto out_ccw_register;
1541da177e4SLinus Torvalds }
1551da177e4SLinus Torvalds
1563623ecbaSChristof Schmitt if (init_device)
1573623ecbaSChristof Schmitt zfcp_init_device_setup(init_device);
1583623ecbaSChristof Schmitt return 0;
1591da177e4SLinus Torvalds
1601da177e4SLinus Torvalds out_ccw_register:
1611947c72aSChristof Schmitt fc_release_transport(zfcp_scsi_transport_template);
162dd52e0eaSHeiko Carstens out_transport:
163087897e3SChristof Schmitt kmem_cache_destroy(zfcp_fc_req_cache);
164087897e3SChristof Schmitt out_fc_cache:
165259afe2eSChristof Schmitt kmem_cache_destroy(zfcp_fsf_qtcb_cache);
166a4623c46SSwen Schillig out_qtcb_cache:
1671da177e4SLinus Torvalds return retval;
1681da177e4SLinus Torvalds }
1691da177e4SLinus Torvalds
170317e6b65SSwen Schillig module_init(zfcp_module_init);
1711da177e4SLinus Torvalds
zfcp_module_exit(void)172c1fad417SChristof Schmitt static void __exit zfcp_module_exit(void)
173c1fad417SChristof Schmitt {
174c1fad417SChristof Schmitt ccw_driver_unregister(&zfcp_ccw_driver);
1751947c72aSChristof Schmitt fc_release_transport(zfcp_scsi_transport_template);
176087897e3SChristof Schmitt kmem_cache_destroy(zfcp_fc_req_cache);
177259afe2eSChristof Schmitt kmem_cache_destroy(zfcp_fsf_qtcb_cache);
178c1fad417SChristof Schmitt }
179c1fad417SChristof Schmitt
180c1fad417SChristof Schmitt module_exit(zfcp_module_exit);
181c1fad417SChristof Schmitt
1821da177e4SLinus Torvalds /**
1831da177e4SLinus Torvalds * zfcp_get_port_by_wwpn - find port in port list of adapter by wwpn
1841da177e4SLinus Torvalds * @adapter: pointer to adapter to search for port
1851da177e4SLinus Torvalds * @wwpn: wwpn to search for
186317e6b65SSwen Schillig *
187317e6b65SSwen Schillig * Returns: pointer to zfcp_port or NULL
1881da177e4SLinus Torvalds */
zfcp_get_port_by_wwpn(struct zfcp_adapter * adapter,u64 wwpn)189317e6b65SSwen Schillig struct zfcp_port *zfcp_get_port_by_wwpn(struct zfcp_adapter *adapter,
1907ba58c9cSSwen Schillig u64 wwpn)
1911da177e4SLinus Torvalds {
192ecf0c772SSwen Schillig unsigned long flags;
1931da177e4SLinus Torvalds struct zfcp_port *port;
1941da177e4SLinus Torvalds
195ecf0c772SSwen Schillig read_lock_irqsave(&adapter->port_list_lock, flags);
196ecf0c772SSwen Schillig list_for_each_entry(port, &adapter->port_list, list)
1976b183334SSwen Schillig if (port->wwpn == wwpn) {
198615f59e0SChristof Schmitt if (!get_device(&port->dev))
1996b183334SSwen Schillig port = NULL;
200ecf0c772SSwen Schillig read_unlock_irqrestore(&adapter->port_list_lock, flags);
201317e6b65SSwen Schillig return port;
202ecf0c772SSwen Schillig }
203ecf0c772SSwen Schillig read_unlock_irqrestore(&adapter->port_list_lock, flags);
204317e6b65SSwen Schillig return NULL;
2051da177e4SLinus Torvalds }
2061da177e4SLinus Torvalds
zfcp_allocate_low_mem_buffers(struct zfcp_adapter * adapter)207317e6b65SSwen Schillig static int zfcp_allocate_low_mem_buffers(struct zfcp_adapter *adapter)
2081da177e4SLinus Torvalds {
209a4623c46SSwen Schillig adapter->pool.erp_req =
210a4623c46SSwen Schillig mempool_create_kmalloc_pool(1, sizeof(struct zfcp_fsf_req));
211a4623c46SSwen Schillig if (!adapter->pool.erp_req)
2121da177e4SLinus Torvalds return -ENOMEM;
2131da177e4SLinus Torvalds
214799b76d0SChristof Schmitt adapter->pool.gid_pn_req =
215799b76d0SChristof Schmitt mempool_create_kmalloc_pool(1, sizeof(struct zfcp_fsf_req));
216799b76d0SChristof Schmitt if (!adapter->pool.gid_pn_req)
217799b76d0SChristof Schmitt return -ENOMEM;
218799b76d0SChristof Schmitt
219a4623c46SSwen Schillig adapter->pool.scsi_req =
220a4623c46SSwen Schillig mempool_create_kmalloc_pool(1, sizeof(struct zfcp_fsf_req));
221a4623c46SSwen Schillig if (!adapter->pool.scsi_req)
2221da177e4SLinus Torvalds return -ENOMEM;
2231da177e4SLinus Torvalds
224a4623c46SSwen Schillig adapter->pool.scsi_abort =
225a4623c46SSwen Schillig mempool_create_kmalloc_pool(1, sizeof(struct zfcp_fsf_req));
226a4623c46SSwen Schillig if (!adapter->pool.scsi_abort)
2271da177e4SLinus Torvalds return -ENOMEM;
2281da177e4SLinus Torvalds
229a4623c46SSwen Schillig adapter->pool.status_read_req =
230317e6b65SSwen Schillig mempool_create_kmalloc_pool(FSF_STATUS_READS_RECOM,
2310eaae62aSMatthew Dobson sizeof(struct zfcp_fsf_req));
232a4623c46SSwen Schillig if (!adapter->pool.status_read_req)
2331da177e4SLinus Torvalds return -ENOMEM;
2341da177e4SLinus Torvalds
235a4623c46SSwen Schillig adapter->pool.qtcb_pool =
236259afe2eSChristof Schmitt mempool_create_slab_pool(4, zfcp_fsf_qtcb_cache);
237a4623c46SSwen Schillig if (!adapter->pool.qtcb_pool)
238a4623c46SSwen Schillig return -ENOMEM;
239a4623c46SSwen Schillig
240c7b279aeSChristof Schmitt BUILD_BUG_ON(sizeof(struct fsf_status_read_buffer) > PAGE_SIZE);
241c7b279aeSChristof Schmitt adapter->pool.sr_data =
242c7b279aeSChristof Schmitt mempool_create_page_pool(FSF_STATUS_READS_RECOM, 0);
243c7b279aeSChristof Schmitt if (!adapter->pool.sr_data)
2441da177e4SLinus Torvalds return -ENOMEM;
2451da177e4SLinus Torvalds
246dbf5dfe9SChristof Schmitt adapter->pool.gid_pn =
247fcf7e614SChristof Schmitt mempool_create_slab_pool(1, zfcp_fc_req_cache);
248dbf5dfe9SChristof Schmitt if (!adapter->pool.gid_pn)
2491da177e4SLinus Torvalds return -ENOMEM;
2501da177e4SLinus Torvalds
2511da177e4SLinus Torvalds return 0;
2521da177e4SLinus Torvalds }
2531da177e4SLinus Torvalds
zfcp_free_low_mem_buffers(struct zfcp_adapter * adapter)254317e6b65SSwen Schillig static void zfcp_free_low_mem_buffers(struct zfcp_adapter *adapter)
2551da177e4SLinus Torvalds {
256a4623c46SSwen Schillig mempool_destroy(adapter->pool.erp_req);
257a4623c46SSwen Schillig mempool_destroy(adapter->pool.scsi_req);
258a4623c46SSwen Schillig mempool_destroy(adapter->pool.scsi_abort);
259a4623c46SSwen Schillig mempool_destroy(adapter->pool.qtcb_pool);
260a4623c46SSwen Schillig mempool_destroy(adapter->pool.status_read_req);
261c7b279aeSChristof Schmitt mempool_destroy(adapter->pool.sr_data);
262dbf5dfe9SChristof Schmitt mempool_destroy(adapter->pool.gid_pn);
2631da177e4SLinus Torvalds }
2641da177e4SLinus Torvalds
265317e6b65SSwen Schillig /**
266317e6b65SSwen Schillig * zfcp_status_read_refill - refill the long running status_read_requests
267317e6b65SSwen Schillig * @adapter: ptr to struct zfcp_adapter for which the buffers should be refilled
268317e6b65SSwen Schillig *
26971714553SSteffen Maier * Return:
27071714553SSteffen Maier * * 0 on success meaning at least one status read is pending
27171714553SSteffen Maier * * 1 if posting failed and not a single status read buffer is pending,
27271714553SSteffen Maier * also triggers adapter reopen recovery
273317e6b65SSwen Schillig */
zfcp_status_read_refill(struct zfcp_adapter * adapter)274d26ab06eSSwen Schillig int zfcp_status_read_refill(struct zfcp_adapter *adapter)
275d26ab06eSSwen Schillig {
27660a161b7SSteffen Maier while (atomic_add_unless(&adapter->stat_miss, -1, 0))
277564e1c86SSwen Schillig if (zfcp_fsf_status_read(adapter->qdio)) {
27860a161b7SSteffen Maier atomic_inc(&adapter->stat_miss); /* undo add -1 */
27964deb6efSChristof Schmitt if (atomic_read(&adapter->stat_miss) >=
28064deb6efSChristof Schmitt adapter->stat_read_buf_num) {
281ea4a3a6aSSwen Schillig zfcp_erp_adapter_reopen(adapter, 0, "axsref1");
282d26ab06eSSwen Schillig return 1;
283d26ab06eSSwen Schillig }
2847afe29f7SSwen Schillig break;
28560a161b7SSteffen Maier }
286d26ab06eSSwen Schillig return 0;
287d26ab06eSSwen Schillig }
288d26ab06eSSwen Schillig
_zfcp_status_read_scheduler(struct work_struct * work)289d26ab06eSSwen Schillig static void _zfcp_status_read_scheduler(struct work_struct *work)
290d26ab06eSSwen Schillig {
291d26ab06eSSwen Schillig zfcp_status_read_refill(container_of(work, struct zfcp_adapter,
292d26ab06eSSwen Schillig stat_work));
293d26ab06eSSwen Schillig }
294d26ab06eSSwen Schillig
zfcp_version_change_lost_work(struct work_struct * work)295d9019631SJulian Wiedmann static void zfcp_version_change_lost_work(struct work_struct *work)
296d9019631SJulian Wiedmann {
297d9019631SJulian Wiedmann struct zfcp_adapter *adapter = container_of(work, struct zfcp_adapter,
298d9019631SJulian Wiedmann version_change_lost_work);
299d9019631SJulian Wiedmann
300d9019631SJulian Wiedmann zfcp_fsf_exchange_config_data_sync(adapter->qdio, NULL);
301d9019631SJulian Wiedmann }
302d9019631SJulian Wiedmann
zfcp_print_sl(struct seq_file * m,struct service_level * sl)303bd43a42bSChristof Schmitt static void zfcp_print_sl(struct seq_file *m, struct service_level *sl)
304bd43a42bSChristof Schmitt {
305bd43a42bSChristof Schmitt struct zfcp_adapter *adapter =
306bd43a42bSChristof Schmitt container_of(sl, struct zfcp_adapter, service_level);
307bd43a42bSChristof Schmitt
308bd43a42bSChristof Schmitt seq_printf(m, "zfcp: %s microcode level %x\n",
309bd43a42bSChristof Schmitt dev_name(&adapter->ccw_device->dev),
310bd43a42bSChristof Schmitt adapter->fsf_lic_version);
311bd43a42bSChristof Schmitt }
312bd43a42bSChristof Schmitt
zfcp_setup_adapter_work_queue(struct zfcp_adapter * adapter)3134544683aSSwen Schillig static int zfcp_setup_adapter_work_queue(struct zfcp_adapter *adapter)
3144544683aSSwen Schillig {
3154544683aSSwen Schillig char name[TASK_COMM_LEN];
3164544683aSSwen Schillig
3174544683aSSwen Schillig snprintf(name, sizeof(name), "zfcp_q_%s",
3184544683aSSwen Schillig dev_name(&adapter->ccw_device->dev));
319e68f1d4cSBhaktipriya Shridhar adapter->work_queue = alloc_ordered_workqueue(name, WQ_MEM_RECLAIM);
3204544683aSSwen Schillig
3214544683aSSwen Schillig if (adapter->work_queue)
3224544683aSSwen Schillig return 0;
3234544683aSSwen Schillig return -ENOMEM;
3244544683aSSwen Schillig }
3254544683aSSwen Schillig
zfcp_destroy_adapter_work_queue(struct zfcp_adapter * adapter)3264544683aSSwen Schillig static void zfcp_destroy_adapter_work_queue(struct zfcp_adapter *adapter)
3274544683aSSwen Schillig {
3284544683aSSwen Schillig if (adapter->work_queue)
3294544683aSSwen Schillig destroy_workqueue(adapter->work_queue);
3304544683aSSwen Schillig adapter->work_queue = NULL;
3314544683aSSwen Schillig
3324544683aSSwen Schillig }
3334544683aSSwen Schillig
334317e6b65SSwen Schillig /**
335317e6b65SSwen Schillig * zfcp_adapter_enqueue - enqueue a new adapter to the list
336317e6b65SSwen Schillig * @ccw_device: pointer to the struct cc_device
337317e6b65SSwen Schillig *
338de3dc572SSwen Schillig * Returns: struct zfcp_adapter*
3391da177e4SLinus Torvalds * Enqueues an adapter at the end of the adapter list in the driver data.
3401da177e4SLinus Torvalds * All adapter internal structures are set up.
3411da177e4SLinus Torvalds * Proc-fs entries are also created.
3421da177e4SLinus Torvalds */
zfcp_adapter_enqueue(struct ccw_device * ccw_device)343de3dc572SSwen Schillig struct zfcp_adapter *zfcp_adapter_enqueue(struct ccw_device *ccw_device)
3441da177e4SLinus Torvalds {
3451da177e4SLinus Torvalds struct zfcp_adapter *adapter;
3461da177e4SLinus Torvalds
347f3450c7bSSwen Schillig if (!get_device(&ccw_device->dev))
348de3dc572SSwen Schillig return ERR_PTR(-ENODEV);
3491da177e4SLinus Torvalds
350ec4081c6SAndreas Herrmann adapter = kzalloc(sizeof(struct zfcp_adapter), GFP_KERNEL);
351f3450c7bSSwen Schillig if (!adapter) {
352f3450c7bSSwen Schillig put_device(&ccw_device->dev);
353de3dc572SSwen Schillig return ERR_PTR(-ENOMEM);
354f3450c7bSSwen Schillig }
355f3450c7bSSwen Schillig
356f3450c7bSSwen Schillig kref_init(&adapter->ref);
3571da177e4SLinus Torvalds
3581da177e4SLinus Torvalds ccw_device->handler = NULL;
3591da177e4SLinus Torvalds adapter->ccw_device = ccw_device;
360f3450c7bSSwen Schillig
361f3450c7bSSwen Schillig INIT_WORK(&adapter->stat_work, _zfcp_status_read_scheduler);
36218f87a67SMartin Peschke INIT_DELAYED_WORK(&adapter->scan_work, zfcp_fc_scan_ports);
363038d9446SChristof Schmitt INIT_WORK(&adapter->ns_up_work, zfcp_fc_sym_name_update);
364d9019631SJulian Wiedmann INIT_WORK(&adapter->version_change_lost_work,
365d9019631SJulian Wiedmann zfcp_version_change_lost_work);
3661da177e4SLinus Torvalds
36718f87a67SMartin Peschke adapter->next_port_scan = jiffies;
36818f87a67SMartin Peschke
369ab31fd0cSSteffen Maier adapter->erp_action.adapter = adapter;
370ab31fd0cSSteffen Maier
3717e418833SBenjamin Block if (zfcp_diag_adapter_setup(adapter))
3727e418833SBenjamin Block goto failed;
3737e418833SBenjamin Block
374d5a282a1SSwen Schillig if (zfcp_qdio_setup(adapter))
375f3450c7bSSwen Schillig goto failed;
3761da177e4SLinus Torvalds
37700bab910SSwen Schillig if (zfcp_allocate_low_mem_buffers(adapter))
378f3450c7bSSwen Schillig goto failed;
3791da177e4SLinus Torvalds
380b6bd2fb9SChristof Schmitt adapter->req_list = zfcp_reqlist_alloc();
381b6bd2fb9SChristof Schmitt if (!adapter->req_list)
382f3450c7bSSwen Schillig goto failed;
3831da177e4SLinus Torvalds
3845771710bSSwen Schillig if (zfcp_dbf_adapter_register(adapter))
385f3450c7bSSwen Schillig goto failed;
386317e6b65SSwen Schillig
3874544683aSSwen Schillig if (zfcp_setup_adapter_work_queue(adapter))
388f3450c7bSSwen Schillig goto failed;
3894544683aSSwen Schillig
390d5a282a1SSwen Schillig if (zfcp_fc_gs_setup(adapter))
391f3450c7bSSwen Schillig goto failed;
392d5a282a1SSwen Schillig
393ecf0c772SSwen Schillig rwlock_init(&adapter->port_list_lock);
394ecf0c772SSwen Schillig INIT_LIST_HEAD(&adapter->port_list);
395ecf0c772SSwen Schillig
3962d1e547fSSven Schuetz INIT_LIST_HEAD(&adapter->events.list);
3972d1e547fSSven Schuetz INIT_WORK(&adapter->events.work, zfcp_fc_post_event);
3982d1e547fSSven Schuetz spin_lock_init(&adapter->events.list_lock);
3992d1e547fSSven Schuetz
400347c6a96SChristof Schmitt init_waitqueue_head(&adapter->erp_ready_wq);
401317e6b65SSwen Schillig init_waitqueue_head(&adapter->erp_done_wqh);
402317e6b65SSwen Schillig
403317e6b65SSwen Schillig INIT_LIST_HEAD(&adapter->erp_ready_head);
404317e6b65SSwen Schillig INIT_LIST_HEAD(&adapter->erp_running_head);
405317e6b65SSwen Schillig
406c48a29d0SHeiko Carstens rwlock_init(&adapter->erp_lock);
4071da177e4SLinus Torvalds rwlock_init(&adapter->abort_lock);
408317e6b65SSwen Schillig
409143bb6bfSChristof Schmitt if (zfcp_erp_thread_setup(adapter))
410f3450c7bSSwen Schillig goto failed;
4111da177e4SLinus Torvalds
412bd43a42bSChristof Schmitt adapter->service_level.seq_print = zfcp_print_sl;
413bd43a42bSChristof Schmitt
4141da177e4SLinus Torvalds dev_set_drvdata(&ccw_device->dev, adapter);
4151da177e4SLinus Torvalds
41620540a56SJulian Wiedmann if (device_add_groups(&ccw_device->dev, zfcp_sysfs_adapter_attr_groups))
41720540a56SJulian Wiedmann goto err_sysfs;
4186028f7c4SBenjamin Block
41968322984SChristof Schmitt /* report size limit per scatter-gather segment */
42068322984SChristof Schmitt adapter->ccw_device->dev.dma_parms = &adapter->dma_parms;
42168322984SChristof Schmitt
4229edf7d75SSteffen Maier adapter->stat_read_buf_num = FSF_STATUS_READS_RECOM;
4239edf7d75SSteffen Maier
424de3dc572SSwen Schillig return adapter;
4251da177e4SLinus Torvalds
42620540a56SJulian Wiedmann err_sysfs:
427f3450c7bSSwen Schillig failed:
428ab1fa880SJulian Wiedmann /* TODO: make this more fine-granular */
429ab1fa880SJulian Wiedmann cancel_delayed_work_sync(&adapter->scan_work);
430ab1fa880SJulian Wiedmann cancel_work_sync(&adapter->stat_work);
431ab1fa880SJulian Wiedmann cancel_work_sync(&adapter->ns_up_work);
432ab1fa880SJulian Wiedmann cancel_work_sync(&adapter->version_change_lost_work);
433ab1fa880SJulian Wiedmann zfcp_destroy_adapter_work_queue(adapter);
434ab1fa880SJulian Wiedmann
435ab1fa880SJulian Wiedmann zfcp_fc_wka_ports_force_offline(adapter->gs);
436ab1fa880SJulian Wiedmann zfcp_scsi_adapter_unregister(adapter);
437ab1fa880SJulian Wiedmann
438ab1fa880SJulian Wiedmann zfcp_erp_thread_kill(adapter);
439ab1fa880SJulian Wiedmann zfcp_dbf_adapter_unregister(adapter);
440ab1fa880SJulian Wiedmann zfcp_qdio_destroy(adapter->qdio);
441ab1fa880SJulian Wiedmann
442ab1fa880SJulian Wiedmann zfcp_ccw_adapter_put(adapter); /* final put to release */
443de3dc572SSwen Schillig return ERR_PTR(-ENOMEM);
444de3dc572SSwen Schillig }
445de3dc572SSwen Schillig
zfcp_adapter_unregister(struct zfcp_adapter * adapter)446de3dc572SSwen Schillig void zfcp_adapter_unregister(struct zfcp_adapter *adapter)
447de3dc572SSwen Schillig {
448de3dc572SSwen Schillig struct ccw_device *cdev = adapter->ccw_device;
449de3dc572SSwen Schillig
45018f87a67SMartin Peschke cancel_delayed_work_sync(&adapter->scan_work);
451de3dc572SSwen Schillig cancel_work_sync(&adapter->stat_work);
452038d9446SChristof Schmitt cancel_work_sync(&adapter->ns_up_work);
453d9019631SJulian Wiedmann cancel_work_sync(&adapter->version_change_lost_work);
454de3dc572SSwen Schillig zfcp_destroy_adapter_work_queue(adapter);
455de3dc572SSwen Schillig
456de3dc572SSwen Schillig zfcp_fc_wka_ports_force_offline(adapter->gs);
4571947c72aSChristof Schmitt zfcp_scsi_adapter_unregister(adapter);
45820540a56SJulian Wiedmann device_remove_groups(&cdev->dev, zfcp_sysfs_adapter_attr_groups);
459de3dc572SSwen Schillig
460de3dc572SSwen Schillig zfcp_erp_thread_kill(adapter);
461ea4a3a6aSSwen Schillig zfcp_dbf_adapter_unregister(adapter);
462de3dc572SSwen Schillig zfcp_qdio_destroy(adapter->qdio);
463de3dc572SSwen Schillig
464de3dc572SSwen Schillig zfcp_ccw_adapter_put(adapter); /* final put to release */
4651da177e4SLinus Torvalds }
4661da177e4SLinus Torvalds
467317e6b65SSwen Schillig /**
468f3450c7bSSwen Schillig * zfcp_adapter_release - remove the adapter from the resource list
469f3450c7bSSwen Schillig * @ref: pointer to struct kref
4701da177e4SLinus Torvalds * locks: adapter list write lock is assumed to be held by caller
4711da177e4SLinus Torvalds */
zfcp_adapter_release(struct kref * ref)472f3450c7bSSwen Schillig void zfcp_adapter_release(struct kref *ref)
4731da177e4SLinus Torvalds {
474f3450c7bSSwen Schillig struct zfcp_adapter *adapter = container_of(ref, struct zfcp_adapter,
475f3450c7bSSwen Schillig ref);
476de3dc572SSwen Schillig struct ccw_device *cdev = adapter->ccw_device;
477f3450c7bSSwen Schillig
478f3450c7bSSwen Schillig dev_set_drvdata(&adapter->ccw_device->dev, NULL);
479d5a282a1SSwen Schillig zfcp_fc_gs_destroy(adapter);
4801da177e4SLinus Torvalds zfcp_free_low_mem_buffers(adapter);
4817e418833SBenjamin Block zfcp_diag_adapter_free(adapter);
482317e6b65SSwen Schillig kfree(adapter->req_list);
483f6cd94b1SAndreas Herrmann kfree(adapter->fc_stats);
484f6cd94b1SAndreas Herrmann kfree(adapter->stats_reset_data);
4851da177e4SLinus Torvalds kfree(adapter);
486de3dc572SSwen Schillig put_device(&cdev->dev);
4871da177e4SLinus Torvalds }
4881da177e4SLinus Torvalds
zfcp_port_release(struct device * dev)489f3450c7bSSwen Schillig static void zfcp_port_release(struct device *dev)
490f3450c7bSSwen Schillig {
491615f59e0SChristof Schmitt struct zfcp_port *port = container_of(dev, struct zfcp_port, dev);
492f3450c7bSSwen Schillig
493de3dc572SSwen Schillig zfcp_ccw_adapter_put(port->adapter);
494f3450c7bSSwen Schillig kfree(port);
49560221920SSwen Schillig }
49660221920SSwen Schillig
4971da177e4SLinus Torvalds /**
4981da177e4SLinus Torvalds * zfcp_port_enqueue - enqueue port to port list of adapter
4991da177e4SLinus Torvalds * @adapter: adapter where remote port is added
5001da177e4SLinus Torvalds * @wwpn: WWPN of the remote port to be enqueued
5011da177e4SLinus Torvalds * @status: initial status for the port
5021da177e4SLinus Torvalds * @d_id: destination id of the remote port to be enqueued
503317e6b65SSwen Schillig * Returns: pointer to enqueued port on success, ERR_PTR on error
5041da177e4SLinus Torvalds *
5051da177e4SLinus Torvalds * All port internal structures are set up and the sysfs entry is generated.
5061da177e4SLinus Torvalds * d_id is used to enqueue ports with a well known address like the Directory
5071da177e4SLinus Torvalds * Service for nameserver lookup.
5081da177e4SLinus Torvalds */
zfcp_port_enqueue(struct zfcp_adapter * adapter,u64 wwpn,u32 status,u32 d_id)5097ba58c9cSSwen Schillig struct zfcp_port *zfcp_port_enqueue(struct zfcp_adapter *adapter, u64 wwpn,
510317e6b65SSwen Schillig u32 status, u32 d_id)
5111da177e4SLinus Torvalds {
5123859f6a2SAndreas Herrmann struct zfcp_port *port;
513f3450c7bSSwen Schillig int retval = -ENOMEM;
514f3450c7bSSwen Schillig
515f3450c7bSSwen Schillig kref_get(&adapter->ref);
5160fac3f47SChristof Schmitt
517ecf0c772SSwen Schillig port = zfcp_get_port_by_wwpn(adapter, wwpn);
518ecf0c772SSwen Schillig if (port) {
519615f59e0SChristof Schmitt put_device(&port->dev);
520f3450c7bSSwen Schillig retval = -EEXIST;
521*b481f644SDinghao Liu goto err_put;
5220fac3f47SChristof Schmitt }
5231da177e4SLinus Torvalds
524ec4081c6SAndreas Herrmann port = kzalloc(sizeof(struct zfcp_port), GFP_KERNEL);
5251da177e4SLinus Torvalds if (!port)
526*b481f644SDinghao Liu goto err_put;
5271da177e4SLinus Torvalds
528ecf0c772SSwen Schillig rwlock_init(&port->unit_list_lock);
529ecf0c772SSwen Schillig INIT_LIST_HEAD(&port->unit_list);
530d99b601bSSteffen Maier atomic_set(&port->units, 0);
531ecf0c772SSwen Schillig
532799b76d0SChristof Schmitt INIT_WORK(&port->gid_pn_work, zfcp_fc_port_did_lookup);
5338fdf30d5SChristof Schmitt INIT_WORK(&port->test_link_work, zfcp_fc_link_test_work);
534a2fa0aedSChristof Schmitt INIT_WORK(&port->rport_work, zfcp_scsi_rport_work);
5351da177e4SLinus Torvalds
5361da177e4SLinus Torvalds port->adapter = adapter;
537317e6b65SSwen Schillig port->d_id = d_id;
5381da177e4SLinus Torvalds port->wwpn = wwpn;
539a2fa0aedSChristof Schmitt port->rport_task = RPORT_NONE;
540615f59e0SChristof Schmitt port->dev.parent = &adapter->ccw_device->dev;
54183d4e1c3SSebastian Ott port->dev.groups = zfcp_port_attr_groups;
542615f59e0SChristof Schmitt port->dev.release = zfcp_port_release;
5431da177e4SLinus Torvalds
544ab31fd0cSSteffen Maier port->erp_action.adapter = adapter;
545ab31fd0cSSteffen Maier port->erp_action.port = port;
546ab31fd0cSSteffen Maier
547615f59e0SChristof Schmitt if (dev_set_name(&port->dev, "0x%016llx", (unsigned long long)wwpn)) {
5480fac3f47SChristof Schmitt kfree(port);
549*b481f644SDinghao Liu goto err_put;
5500fac3f47SChristof Schmitt }
551f3450c7bSSwen Schillig retval = -EINVAL;
5521da177e4SLinus Torvalds
553615f59e0SChristof Schmitt if (device_register(&port->dev)) {
554615f59e0SChristof Schmitt put_device(&port->dev);
555f3450c7bSSwen Schillig goto err_out;
556f4395b65SSebastian Ott }
5571da177e4SLinus Torvalds
558ecf0c772SSwen Schillig write_lock_irq(&adapter->port_list_lock);
559ecf0c772SSwen Schillig list_add_tail(&port->list, &adapter->port_list);
560ecf0c772SSwen Schillig write_unlock_irq(&adapter->port_list_lock);
561ecf0c772SSwen Schillig
562805de8f4SPeter Zijlstra atomic_or(status | ZFCP_STATUS_COMMON_RUNNING, &port->status);
563317e6b65SSwen Schillig
5641da177e4SLinus Torvalds return port;
5651da177e4SLinus Torvalds
566*b481f644SDinghao Liu err_put:
567de3dc572SSwen Schillig zfcp_ccw_adapter_put(adapter);
568*b481f644SDinghao Liu err_out:
569f3450c7bSSwen Schillig return ERR_PTR(retval);
5701da177e4SLinus Torvalds }
571