blob: fd2f1c31bd2197cc58b3b74e6e9c9d8d3b7d5440 [file] [log] [blame]
Greg Kroah-Hartman40bf4112017-11-14 18:38:05 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Christof Schmitt553448f2008-06-10 18:20:58 +02003 * zfcp device driver
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Christof Schmitt553448f2008-06-10 18:20:58 +02005 * Module interface and handling of zfcp data structures.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
Benjamin Blockd0dff2a2020-05-08 19:23:35 +02007 * Copyright IBM Corp. 2002, 2020
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 */
9
Andreas Herrmann4a9d2d82006-05-22 18:14:08 +020010/*
11 * Driver authors:
12 * Martin Peschke (originator of the driver)
13 * Raimund Schroeder
14 * Aron Zeh
15 * Wolfgang Taphorn
16 * Stefan Bader
17 * Heiko Carstens (kernel 2.6 port of the driver)
18 * Andreas Herrmann
19 * Maxim Shchetynin
20 * Volker Sameske
21 * Ralph Wuerthner
Christof Schmitt553448f2008-06-10 18:20:58 +020022 * Michael Loehr
23 * Swen Schillig
24 * Christof Schmitt
25 * Martin Petermann
26 * Sven Schuetz
Steffen Maier9edf7d72013-04-26 17:34:54 +020027 * Steffen Maier
Benjamin Block7e418832019-10-25 18:12:44 +020028 * Benjamin Block
Andreas Herrmann4a9d2d82006-05-22 18:14:08 +020029 */
30
Christof Schmittecf39d42008-12-25 13:39:53 +010031#define KMSG_COMPONENT "zfcp"
32#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
33
Christof Schmittbd43a42b72008-12-25 13:38:50 +010034#include <linux/seq_file.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090035#include <linux/slab.h>
Heiko Carstens3a4c5d52011-07-30 09:25:15 +020036#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include "zfcp_ext.h"
Christof Schmittdbf5dfe2009-11-24 16:54:10 +010038#include "zfcp_fc.h"
Christof Schmittb6bd2fb92010-02-17 11:18:50 +010039#include "zfcp_reqlist.h"
Benjamin Block7e418832019-10-25 18:12:44 +020040#include "zfcp_diag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Kay Sievers98df67b2008-12-25 13:38:55 +010042#define ZFCP_BUS_ID_SIZE 20
43
Andreas Herrmann4a9d2d82006-05-22 18:14:08 +020044MODULE_AUTHOR("IBM Deutschland Entwicklung GmbH - linux390@de.ibm.com");
Swen Schillig317e6b62008-07-02 10:56:37 +020045MODULE_DESCRIPTION("FCP HBA driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -070046MODULE_LICENSE("GPL");
47
Christof Schmitt3623ecb2008-12-19 16:56:57 +010048static char *init_device;
49module_param_named(device, init_device, charp, 0400);
Linus Torvalds1da177e2005-04-16 15:20:36 -070050MODULE_PARM_DESC(device, "specify initial device");
51
Christof Schmitt51780d22010-12-02 15:16:18 +010052static struct kmem_cache * __init zfcp_cache_hw_align(const char *name,
53 unsigned long size)
Swen Schilliga4623c42009-08-18 15:43:15 +020054{
55 return kmem_cache_create(name, size, roundup_pow_of_two(size), 0, NULL);
56}
57
Christof Schmitt3623ecb2008-12-19 16:56:57 +010058static void __init zfcp_init_device_configure(char *busid, u64 wwpn, u64 lun)
Linus Torvalds1da177e2005-04-16 15:20:36 -070059{
Swen Schilligde3dc572009-11-24 16:54:00 +010060 struct ccw_device *cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 struct zfcp_adapter *adapter;
62 struct zfcp_port *port;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Swen Schilligde3dc572009-11-24 16:54:00 +010064 cdev = get_ccwdev_by_busid(&zfcp_ccw_driver, busid);
65 if (!cdev)
Christof Schmittc5afd81e2009-09-24 10:23:22 +020066 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
Swen Schilligde3dc572009-11-24 16:54:00 +010068 if (ccw_device_set_online(cdev))
69 goto out_ccw_device;
Christof Schmittc5afd81e2009-09-24 10:23:22 +020070
Swen Schilligde3dc572009-11-24 16:54:00 +010071 adapter = zfcp_ccw_adapter_by_cdev(cdev);
Swen Schillig317e6b62008-07-02 10:56:37 +020072 if (!adapter)
Swen Schilligde3dc572009-11-24 16:54:00 +010073 goto out_ccw_device;
Christof Schmittc5afd81e2009-09-24 10:23:22 +020074
75 port = zfcp_get_port_by_wwpn(adapter, wwpn);
76 if (!port)
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 goto out_port;
Christof Schmitt91978462010-08-30 10:55:09 +020078 flush_work(&port->rport_work);
Swen Schillig091694a2008-10-01 12:42:25 +020079
Christof Schmitt1daa4eb2010-09-08 14:39:52 +020080 zfcp_unit_add(port, lun);
Christof Schmitt615f59e2010-02-17 11:18:56 +010081 put_device(&port->dev);
Christof Schmitt1daa4eb2010-09-08 14:39:52 +020082
Swen Schillig317e6b62008-07-02 10:56:37 +020083out_port:
Swen Schilligde3dc572009-11-24 16:54:00 +010084 zfcp_ccw_adapter_put(adapter);
85out_ccw_device:
86 put_device(&cdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 return;
88}
89
Christof Schmitt3623ecb2008-12-19 16:56:57 +010090static void __init zfcp_init_device_setup(char *devstr)
91{
92 char *token;
Heiko Carstensd10c0852009-10-13 10:44:07 +020093 char *str, *str_saved;
Christof Schmitt3623ecb2008-12-19 16:56:57 +010094 char busid[ZFCP_BUS_ID_SIZE];
95 u64 wwpn, lun;
96
97 /* duplicate devstr and keep the original for sysfs presentation*/
Christof Schmitt674c3a92010-07-16 15:37:34 +020098 str_saved = kstrdup(devstr, GFP_KERNEL);
Heiko Carstensd10c0852009-10-13 10:44:07 +020099 str = str_saved;
Christof Schmitt3623ecb2008-12-19 16:56:57 +0100100 if (!str)
101 return;
102
Christof Schmitt3623ecb2008-12-19 16:56:57 +0100103 token = strsep(&str, ",");
104 if (!token || strlen(token) >= ZFCP_BUS_ID_SIZE)
105 goto err_out;
Vasily Gorbikc6756b72018-06-17 11:56:17 +0200106 strlcpy(busid, token, ZFCP_BUS_ID_SIZE);
Christof Schmitt3623ecb2008-12-19 16:56:57 +0100107
108 token = strsep(&str, ",");
Martin Peschkeee732ea2013-08-22 17:49:32 +0200109 if (!token || kstrtoull(token, 0, (unsigned long long *) &wwpn))
Christof Schmitt3623ecb2008-12-19 16:56:57 +0100110 goto err_out;
111
112 token = strsep(&str, ",");
Martin Peschkeee732ea2013-08-22 17:49:32 +0200113 if (!token || kstrtoull(token, 0, (unsigned long long *) &lun))
Christof Schmitt3623ecb2008-12-19 16:56:57 +0100114 goto err_out;
115
Heiko Carstensd10c0852009-10-13 10:44:07 +0200116 kfree(str_saved);
Christof Schmitt3623ecb2008-12-19 16:56:57 +0100117 zfcp_init_device_configure(busid, wwpn, lun);
118 return;
119
Heiko Carstensd10c0852009-10-13 10:44:07 +0200120err_out:
121 kfree(str_saved);
Christof Schmitt3623ecb2008-12-19 16:56:57 +0100122 pr_err("%s is not a valid SCSI device\n", devstr);
123}
124
Swen Schillig317e6b62008-07-02 10:56:37 +0200125static int __init zfcp_module_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126{
Heiko Carstensdd52e0e2006-09-18 22:28:49 +0200127 int retval = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
Fedor Loshakov636db602018-11-29 13:09:56 +0100129 if (zfcp_experimental_dix)
130 pr_warn("DIX is enabled. It is experimental and might cause problems\n");
131
Christof Schmitt259afe22011-02-22 19:54:44 +0100132 zfcp_fsf_qtcb_cache = zfcp_cache_hw_align("zfcp_fsf_qtcb",
133 sizeof(struct fsf_qtcb));
134 if (!zfcp_fsf_qtcb_cache)
Swen Schilliga4623c42009-08-18 15:43:15 +0200135 goto out_qtcb_cache;
136
Christof Schmitt087897e2011-02-22 19:54:41 +0100137 zfcp_fc_req_cache = zfcp_cache_hw_align("zfcp_fc_req",
138 sizeof(struct zfcp_fc_req));
139 if (!zfcp_fc_req_cache)
140 goto out_fc_cache;
Christof Schmittee744622009-11-24 16:54:14 +0100141
Christof Schmitt1947c722011-02-22 19:54:46 +0100142 zfcp_scsi_transport_template =
Heiko Carstensdd52e0e2006-09-18 22:28:49 +0200143 fc_attach_transport(&zfcp_transport_functions);
Christof Schmitt1947c722011-02-22 19:54:46 +0100144 if (!zfcp_scsi_transport_template)
Heiko Carstensdd52e0e2006-09-18 22:28:49 +0200145 goto out_transport;
Christof Schmitt1947c722011-02-22 19:54:46 +0100146 scsi_transport_reserve_device(zfcp_scsi_transport_template,
Christof Schmitt57c23772010-09-08 14:39:51 +0200147 sizeof(struct zfcp_scsi_dev));
148
Christof Schmittc1fad412009-11-24 16:54:04 +0100149 retval = ccw_driver_register(&zfcp_ccw_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 if (retval) {
Christof Schmittecf39d42008-12-25 13:39:53 +0100151 pr_err("The zfcp device driver could not register with "
Christof Schmittff3b24f2008-10-01 12:42:15 +0200152 "the common I/O layer\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 goto out_ccw_register;
154 }
155
Christof Schmitt3623ecb2008-12-19 16:56:57 +0100156 if (init_device)
157 zfcp_init_device_setup(init_device);
158 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
Swen Schillig317e6b62008-07-02 10:56:37 +0200160out_ccw_register:
Christof Schmitt1947c722011-02-22 19:54:46 +0100161 fc_release_transport(zfcp_scsi_transport_template);
Swen Schillig317e6b62008-07-02 10:56:37 +0200162out_transport:
Christof Schmitt087897e2011-02-22 19:54:41 +0100163 kmem_cache_destroy(zfcp_fc_req_cache);
164out_fc_cache:
Christof Schmitt259afe22011-02-22 19:54:44 +0100165 kmem_cache_destroy(zfcp_fsf_qtcb_cache);
Swen Schilliga4623c42009-08-18 15:43:15 +0200166out_qtcb_cache:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 return retval;
168}
169
Swen Schillig317e6b62008-07-02 10:56:37 +0200170module_init(zfcp_module_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
Christof Schmittc1fad412009-11-24 16:54:04 +0100172static void __exit zfcp_module_exit(void)
173{
174 ccw_driver_unregister(&zfcp_ccw_driver);
Christof Schmitt1947c722011-02-22 19:54:46 +0100175 fc_release_transport(zfcp_scsi_transport_template);
Christof Schmitt087897e2011-02-22 19:54:41 +0100176 kmem_cache_destroy(zfcp_fc_req_cache);
Christof Schmitt259afe22011-02-22 19:54:44 +0100177 kmem_cache_destroy(zfcp_fsf_qtcb_cache);
Christof Schmittc1fad412009-11-24 16:54:04 +0100178}
179
180module_exit(zfcp_module_exit);
181
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 * zfcp_get_port_by_wwpn - find port in port list of adapter by wwpn
184 * @adapter: pointer to adapter to search for port
185 * @wwpn: wwpn to search for
Swen Schillig317e6b62008-07-02 10:56:37 +0200186 *
187 * Returns: pointer to zfcp_port or NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 */
Swen Schillig317e6b62008-07-02 10:56:37 +0200189struct zfcp_port *zfcp_get_port_by_wwpn(struct zfcp_adapter *adapter,
Swen Schillig7ba58c92008-10-01 12:42:18 +0200190 u64 wwpn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191{
Swen Schilligecf0c772009-11-24 16:53:58 +0100192 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 struct zfcp_port *port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
Swen Schilligecf0c772009-11-24 16:53:58 +0100195 read_lock_irqsave(&adapter->port_list_lock, flags);
196 list_for_each_entry(port, &adapter->port_list, list)
Swen Schillig6b1833342009-11-24 16:54:05 +0100197 if (port->wwpn == wwpn) {
Christof Schmitt615f59e2010-02-17 11:18:56 +0100198 if (!get_device(&port->dev))
Swen Schillig6b1833342009-11-24 16:54:05 +0100199 port = NULL;
Swen Schilligecf0c772009-11-24 16:53:58 +0100200 read_unlock_irqrestore(&adapter->port_list_lock, flags);
Swen Schillig317e6b62008-07-02 10:56:37 +0200201 return port;
Swen Schilligecf0c772009-11-24 16:53:58 +0100202 }
203 read_unlock_irqrestore(&adapter->port_list_lock, flags);
Swen Schillig317e6b62008-07-02 10:56:37 +0200204 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205}
206
Swen Schillig317e6b62008-07-02 10:56:37 +0200207static int zfcp_allocate_low_mem_buffers(struct zfcp_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208{
Swen Schilliga4623c42009-08-18 15:43:15 +0200209 adapter->pool.erp_req =
210 mempool_create_kmalloc_pool(1, sizeof(struct zfcp_fsf_req));
211 if (!adapter->pool.erp_req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 return -ENOMEM;
213
Christof Schmitt799b76d2009-08-18 15:43:20 +0200214 adapter->pool.gid_pn_req =
215 mempool_create_kmalloc_pool(1, sizeof(struct zfcp_fsf_req));
216 if (!adapter->pool.gid_pn_req)
217 return -ENOMEM;
218
Swen Schilliga4623c42009-08-18 15:43:15 +0200219 adapter->pool.scsi_req =
220 mempool_create_kmalloc_pool(1, sizeof(struct zfcp_fsf_req));
221 if (!adapter->pool.scsi_req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 return -ENOMEM;
223
Swen Schilliga4623c42009-08-18 15:43:15 +0200224 adapter->pool.scsi_abort =
225 mempool_create_kmalloc_pool(1, sizeof(struct zfcp_fsf_req));
226 if (!adapter->pool.scsi_abort)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 return -ENOMEM;
228
Swen Schilliga4623c42009-08-18 15:43:15 +0200229 adapter->pool.status_read_req =
Swen Schillig317e6b62008-07-02 10:56:37 +0200230 mempool_create_kmalloc_pool(FSF_STATUS_READS_RECOM,
Matthew Dobson0eaae62a2006-03-26 01:37:47 -0800231 sizeof(struct zfcp_fsf_req));
Swen Schilliga4623c42009-08-18 15:43:15 +0200232 if (!adapter->pool.status_read_req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 return -ENOMEM;
234
Swen Schilliga4623c42009-08-18 15:43:15 +0200235 adapter->pool.qtcb_pool =
Christof Schmitt259afe22011-02-22 19:54:44 +0100236 mempool_create_slab_pool(4, zfcp_fsf_qtcb_cache);
Swen Schilliga4623c42009-08-18 15:43:15 +0200237 if (!adapter->pool.qtcb_pool)
238 return -ENOMEM;
239
Christof Schmittc7b279a2011-02-22 19:54:40 +0100240 BUILD_BUG_ON(sizeof(struct fsf_status_read_buffer) > PAGE_SIZE);
241 adapter->pool.sr_data =
242 mempool_create_page_pool(FSF_STATUS_READS_RECOM, 0);
243 if (!adapter->pool.sr_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 return -ENOMEM;
245
Christof Schmittdbf5dfe2009-11-24 16:54:10 +0100246 adapter->pool.gid_pn =
Christof Schmittfcf7e612011-02-22 19:54:42 +0100247 mempool_create_slab_pool(1, zfcp_fc_req_cache);
Christof Schmittdbf5dfe2009-11-24 16:54:10 +0100248 if (!adapter->pool.gid_pn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 return -ENOMEM;
250
251 return 0;
252}
253
Swen Schillig317e6b62008-07-02 10:56:37 +0200254static void zfcp_free_low_mem_buffers(struct zfcp_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255{
zhong jiang6be55222018-11-08 15:44:37 +0100256 mempool_destroy(adapter->pool.erp_req);
257 mempool_destroy(adapter->pool.scsi_req);
258 mempool_destroy(adapter->pool.scsi_abort);
259 mempool_destroy(adapter->pool.qtcb_pool);
260 mempool_destroy(adapter->pool.status_read_req);
261 mempool_destroy(adapter->pool.sr_data);
262 mempool_destroy(adapter->pool.gid_pn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263}
264
Swen Schillig317e6b62008-07-02 10:56:37 +0200265/**
266 * zfcp_status_read_refill - refill the long running status_read_requests
267 * @adapter: ptr to struct zfcp_adapter for which the buffers should be refilled
268 *
Steffen Maier71714552018-12-06 17:31:21 +0100269 * Return:
270 * * 0 on success meaning at least one status read is pending
271 * * 1 if posting failed and not a single status read buffer is pending,
272 * also triggers adapter reopen recovery
Swen Schillig317e6b62008-07-02 10:56:37 +0200273 */
Swen Schilligd26ab062008-05-19 12:17:37 +0200274int zfcp_status_read_refill(struct zfcp_adapter *adapter)
275{
Steffen Maier60a161b2018-12-06 17:31:20 +0100276 while (atomic_add_unless(&adapter->stat_miss, -1, 0))
Swen Schillig564e1c82009-08-18 15:43:19 +0200277 if (zfcp_fsf_status_read(adapter->qdio)) {
Steffen Maier60a161b2018-12-06 17:31:20 +0100278 atomic_inc(&adapter->stat_miss); /* undo add -1 */
Christof Schmitt64deb6e2010-04-30 18:09:36 +0200279 if (atomic_read(&adapter->stat_miss) >=
280 adapter->stat_read_buf_num) {
Swen Schilligea4a3a62010-12-02 15:16:16 +0100281 zfcp_erp_adapter_reopen(adapter, 0, "axsref1");
Swen Schillig7afe29f2008-07-02 10:56:33 +0200282 return 1;
283 }
Swen Schilligd26ab062008-05-19 12:17:37 +0200284 break;
Steffen Maier60a161b2018-12-06 17:31:20 +0100285 }
Swen Schilligd26ab062008-05-19 12:17:37 +0200286 return 0;
287}
288
289static void _zfcp_status_read_scheduler(struct work_struct *work)
290{
291 zfcp_status_read_refill(container_of(work, struct zfcp_adapter,
292 stat_work));
293}
294
Julian Wiedmannd9019632020-10-28 19:30:52 +0100295static void zfcp_version_change_lost_work(struct work_struct *work)
296{
297 struct zfcp_adapter *adapter = container_of(work, struct zfcp_adapter,
298 version_change_lost_work);
299
300 zfcp_fsf_exchange_config_data_sync(adapter->qdio, NULL);
301}
302
Christof Schmittbd43a42b72008-12-25 13:38:50 +0100303static void zfcp_print_sl(struct seq_file *m, struct service_level *sl)
304{
305 struct zfcp_adapter *adapter =
306 container_of(sl, struct zfcp_adapter, service_level);
307
308 seq_printf(m, "zfcp: %s microcode level %x\n",
309 dev_name(&adapter->ccw_device->dev),
310 adapter->fsf_lic_version);
311}
312
Swen Schillig45446832009-08-18 15:43:17 +0200313static int zfcp_setup_adapter_work_queue(struct zfcp_adapter *adapter)
314{
315 char name[TASK_COMM_LEN];
316
317 snprintf(name, sizeof(name), "zfcp_q_%s",
318 dev_name(&adapter->ccw_device->dev));
Bhaktipriya Shridhare68f1d42016-08-31 01:57:20 +0530319 adapter->work_queue = alloc_ordered_workqueue(name, WQ_MEM_RECLAIM);
Swen Schillig45446832009-08-18 15:43:17 +0200320
321 if (adapter->work_queue)
322 return 0;
323 return -ENOMEM;
324}
325
326static void zfcp_destroy_adapter_work_queue(struct zfcp_adapter *adapter)
327{
328 if (adapter->work_queue)
329 destroy_workqueue(adapter->work_queue);
330 adapter->work_queue = NULL;
331
332}
333
Swen Schillig317e6b62008-07-02 10:56:37 +0200334/**
335 * zfcp_adapter_enqueue - enqueue a new adapter to the list
336 * @ccw_device: pointer to the struct cc_device
337 *
Swen Schilligde3dc572009-11-24 16:54:00 +0100338 * Returns: struct zfcp_adapter*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 * Enqueues an adapter at the end of the adapter list in the driver data.
340 * All adapter internal structures are set up.
341 * Proc-fs entries are also created.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 */
Swen Schilligde3dc572009-11-24 16:54:00 +0100343struct zfcp_adapter *zfcp_adapter_enqueue(struct ccw_device *ccw_device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 struct zfcp_adapter *adapter;
346
Swen Schilligf3450c72009-11-24 16:53:59 +0100347 if (!get_device(&ccw_device->dev))
Swen Schilligde3dc572009-11-24 16:54:00 +0100348 return ERR_PTR(-ENODEV);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
Swen Schillig317e6b62008-07-02 10:56:37 +0200350 adapter = kzalloc(sizeof(struct zfcp_adapter), GFP_KERNEL);
Swen Schilligf3450c72009-11-24 16:53:59 +0100351 if (!adapter) {
352 put_device(&ccw_device->dev);
Swen Schilligde3dc572009-11-24 16:54:00 +0100353 return ERR_PTR(-ENOMEM);
Swen Schilligf3450c72009-11-24 16:53:59 +0100354 }
355
356 kref_init(&adapter->ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
358 ccw_device->handler = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 adapter->ccw_device = ccw_device;
Swen Schilligf3450c72009-11-24 16:53:59 +0100360
361 INIT_WORK(&adapter->stat_work, _zfcp_status_read_scheduler);
Martin Peschke18f87a62014-11-13 14:59:48 +0100362 INIT_DELAYED_WORK(&adapter->scan_work, zfcp_fc_scan_ports);
Christof Schmitt038d9442011-02-22 19:54:48 +0100363 INIT_WORK(&adapter->ns_up_work, zfcp_fc_sym_name_update);
Julian Wiedmannd9019632020-10-28 19:30:52 +0100364 INIT_WORK(&adapter->version_change_lost_work,
365 zfcp_version_change_lost_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
Martin Peschke18f87a62014-11-13 14:59:48 +0100367 adapter->next_port_scan = jiffies;
368
Steffen Maierab31fd02017-10-13 15:40:07 +0200369 adapter->erp_action.adapter = adapter;
370
Benjamin Block7e418832019-10-25 18:12:44 +0200371 if (zfcp_diag_adapter_setup(adapter))
372 goto failed;
373
Swen Schilligd5a282a2009-08-18 15:43:22 +0200374 if (zfcp_qdio_setup(adapter))
Swen Schilligf3450c72009-11-24 16:53:59 +0100375 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
Swen Schillig00bab912008-06-10 18:20:57 +0200377 if (zfcp_allocate_low_mem_buffers(adapter))
Swen Schilligf3450c72009-11-24 16:53:59 +0100378 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
Christof Schmittb6bd2fb92010-02-17 11:18:50 +0100380 adapter->req_list = zfcp_reqlist_alloc();
381 if (!adapter->req_list)
Swen Schilligf3450c72009-11-24 16:53:59 +0100382 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
Swen Schillig57717102009-08-18 15:43:21 +0200384 if (zfcp_dbf_adapter_register(adapter))
Swen Schilligf3450c72009-11-24 16:53:59 +0100385 goto failed;
Swen Schillig317e6b62008-07-02 10:56:37 +0200386
Swen Schillig45446832009-08-18 15:43:17 +0200387 if (zfcp_setup_adapter_work_queue(adapter))
Swen Schilligf3450c72009-11-24 16:53:59 +0100388 goto failed;
Swen Schillig45446832009-08-18 15:43:17 +0200389
Swen Schilligd5a282a2009-08-18 15:43:22 +0200390 if (zfcp_fc_gs_setup(adapter))
Swen Schilligf3450c72009-11-24 16:53:59 +0100391 goto failed;
Swen Schilligd5a282a2009-08-18 15:43:22 +0200392
Swen Schilligecf0c772009-11-24 16:53:58 +0100393 rwlock_init(&adapter->port_list_lock);
394 INIT_LIST_HEAD(&adapter->port_list);
395
Sven Schuetz2d1e5472010-07-16 15:37:39 +0200396 INIT_LIST_HEAD(&adapter->events.list);
397 INIT_WORK(&adapter->events.work, zfcp_fc_post_event);
398 spin_lock_init(&adapter->events.list_lock);
399
Christof Schmitt347c6a92009-08-18 15:43:25 +0200400 init_waitqueue_head(&adapter->erp_ready_wq);
Swen Schillig317e6b62008-07-02 10:56:37 +0200401 init_waitqueue_head(&adapter->erp_done_wqh);
402
Swen Schillig317e6b62008-07-02 10:56:37 +0200403 INIT_LIST_HEAD(&adapter->erp_ready_head);
404 INIT_LIST_HEAD(&adapter->erp_running_head);
405
Heiko Carstensc48a29d2005-12-01 02:46:32 +0100406 rwlock_init(&adapter->erp_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 rwlock_init(&adapter->abort_lock);
Swen Schillig317e6b62008-07-02 10:56:37 +0200408
Christof Schmitt143bb6b2009-08-18 15:43:27 +0200409 if (zfcp_erp_thread_setup(adapter))
Swen Schilligf3450c72009-11-24 16:53:59 +0100410 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
Christof Schmittbd43a42b72008-12-25 13:38:50 +0100412 adapter->service_level.seq_print = zfcp_print_sl;
413
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 dev_set_drvdata(&ccw_device->dev, adapter);
415
Julian Wiedmann20540a52021-04-14 19:08:02 +0200416 if (device_add_groups(&ccw_device->dev, zfcp_sysfs_adapter_attr_groups))
417 goto err_sysfs;
Benjamin Block6028f7c2019-10-25 18:12:47 +0200418
Christof Schmitt68322982010-04-30 18:09:33 +0200419 /* report size limit per scatter-gather segment */
Christof Schmitt68322982010-04-30 18:09:33 +0200420 adapter->ccw_device->dev.dma_parms = &adapter->dma_parms;
421
Steffen Maier9edf7d72013-04-26 17:34:54 +0200422 adapter->stat_read_buf_num = FSF_STATUS_READS_RECOM;
423
Benjamin Blockd0dff2a2020-05-08 19:23:35 +0200424 return adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
Julian Wiedmann20540a52021-04-14 19:08:02 +0200426err_sysfs:
Swen Schilligf3450c72009-11-24 16:53:59 +0100427failed:
Julian Wiedmannab1fa882021-04-14 19:08:01 +0200428 /* TODO: make this more fine-granular */
429 cancel_delayed_work_sync(&adapter->scan_work);
430 cancel_work_sync(&adapter->stat_work);
431 cancel_work_sync(&adapter->ns_up_work);
432 cancel_work_sync(&adapter->version_change_lost_work);
433 zfcp_destroy_adapter_work_queue(adapter);
434
435 zfcp_fc_wka_ports_force_offline(adapter->gs);
436 zfcp_scsi_adapter_unregister(adapter);
437
438 zfcp_erp_thread_kill(adapter);
439 zfcp_dbf_adapter_unregister(adapter);
440 zfcp_qdio_destroy(adapter->qdio);
441
442 zfcp_ccw_adapter_put(adapter); /* final put to release */
Swen Schilligde3dc572009-11-24 16:54:00 +0100443 return ERR_PTR(-ENOMEM);
444}
445
446void zfcp_adapter_unregister(struct zfcp_adapter *adapter)
447{
448 struct ccw_device *cdev = adapter->ccw_device;
449
Martin Peschke18f87a62014-11-13 14:59:48 +0100450 cancel_delayed_work_sync(&adapter->scan_work);
Swen Schilligde3dc572009-11-24 16:54:00 +0100451 cancel_work_sync(&adapter->stat_work);
Christof Schmitt038d9442011-02-22 19:54:48 +0100452 cancel_work_sync(&adapter->ns_up_work);
Julian Wiedmannd9019632020-10-28 19:30:52 +0100453 cancel_work_sync(&adapter->version_change_lost_work);
Swen Schilligde3dc572009-11-24 16:54:00 +0100454 zfcp_destroy_adapter_work_queue(adapter);
455
456 zfcp_fc_wka_ports_force_offline(adapter->gs);
Christof Schmitt1947c722011-02-22 19:54:46 +0100457 zfcp_scsi_adapter_unregister(adapter);
Julian Wiedmann20540a52021-04-14 19:08:02 +0200458 device_remove_groups(&cdev->dev, zfcp_sysfs_adapter_attr_groups);
Swen Schilligde3dc572009-11-24 16:54:00 +0100459
460 zfcp_erp_thread_kill(adapter);
Swen Schilligea4a3a62010-12-02 15:16:16 +0100461 zfcp_dbf_adapter_unregister(adapter);
Swen Schilligde3dc572009-11-24 16:54:00 +0100462 zfcp_qdio_destroy(adapter->qdio);
463
464 zfcp_ccw_adapter_put(adapter); /* final put to release */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465}
466
Swen Schillig317e6b62008-07-02 10:56:37 +0200467/**
Swen Schilligf3450c72009-11-24 16:53:59 +0100468 * zfcp_adapter_release - remove the adapter from the resource list
469 * @ref: pointer to struct kref
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 * locks: adapter list write lock is assumed to be held by caller
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 */
Swen Schilligf3450c72009-11-24 16:53:59 +0100472void zfcp_adapter_release(struct kref *ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473{
Swen Schilligf3450c72009-11-24 16:53:59 +0100474 struct zfcp_adapter *adapter = container_of(ref, struct zfcp_adapter,
475 ref);
Swen Schilligde3dc572009-11-24 16:54:00 +0100476 struct ccw_device *cdev = adapter->ccw_device;
Swen Schilligf3450c72009-11-24 16:53:59 +0100477
478 dev_set_drvdata(&adapter->ccw_device->dev, NULL);
Swen Schilligd5a282a2009-08-18 15:43:22 +0200479 zfcp_fc_gs_destroy(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 zfcp_free_low_mem_buffers(adapter);
Benjamin Block7e418832019-10-25 18:12:44 +0200481 zfcp_diag_adapter_free(adapter);
Swen Schillig317e6b62008-07-02 10:56:37 +0200482 kfree(adapter->req_list);
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100483 kfree(adapter->fc_stats);
484 kfree(adapter->stats_reset_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 kfree(adapter);
Swen Schilligde3dc572009-11-24 16:54:00 +0100486 put_device(&cdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487}
488
Swen Schilligf3450c72009-11-24 16:53:59 +0100489static void zfcp_port_release(struct device *dev)
490{
Christof Schmitt615f59e2010-02-17 11:18:56 +0100491 struct zfcp_port *port = container_of(dev, struct zfcp_port, dev);
Swen Schilligf3450c72009-11-24 16:53:59 +0100492
Swen Schilligde3dc572009-11-24 16:54:00 +0100493 zfcp_ccw_adapter_put(port->adapter);
Swen Schilligf3450c72009-11-24 16:53:59 +0100494 kfree(port);
Swen Schillig60221922008-07-02 10:56:38 +0200495}
496
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497/**
498 * zfcp_port_enqueue - enqueue port to port list of adapter
499 * @adapter: adapter where remote port is added
500 * @wwpn: WWPN of the remote port to be enqueued
501 * @status: initial status for the port
502 * @d_id: destination id of the remote port to be enqueued
Swen Schillig317e6b62008-07-02 10:56:37 +0200503 * Returns: pointer to enqueued port on success, ERR_PTR on error
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 *
505 * All port internal structures are set up and the sysfs entry is generated.
506 * d_id is used to enqueue ports with a well known address like the Directory
507 * Service for nameserver lookup.
508 */
Swen Schillig7ba58c92008-10-01 12:42:18 +0200509struct zfcp_port *zfcp_port_enqueue(struct zfcp_adapter *adapter, u64 wwpn,
Swen Schillig317e6b62008-07-02 10:56:37 +0200510 u32 status, u32 d_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511{
Andreas Herrmann3859f6a2005-08-27 11:07:54 -0700512 struct zfcp_port *port;
Swen Schilligf3450c72009-11-24 16:53:59 +0100513 int retval = -ENOMEM;
514
515 kref_get(&adapter->ref);
Christof Schmitt0fac3f42009-08-18 15:43:30 +0200516
Swen Schilligecf0c772009-11-24 16:53:58 +0100517 port = zfcp_get_port_by_wwpn(adapter, wwpn);
518 if (port) {
Christof Schmitt615f59e2010-02-17 11:18:56 +0100519 put_device(&port->dev);
Swen Schilligf3450c72009-11-24 16:53:59 +0100520 retval = -EEXIST;
521 goto err_out;
Christof Schmitt0fac3f42009-08-18 15:43:30 +0200522 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
Swen Schillig317e6b62008-07-02 10:56:37 +0200524 port = kzalloc(sizeof(struct zfcp_port), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 if (!port)
Swen Schilligf3450c72009-11-24 16:53:59 +0100526 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
Swen Schilligecf0c772009-11-24 16:53:58 +0100528 rwlock_init(&port->unit_list_lock);
529 INIT_LIST_HEAD(&port->unit_list);
Steffen Maierd99b6012012-09-04 15:23:34 +0200530 atomic_set(&port->units, 0);
Swen Schilligecf0c772009-11-24 16:53:58 +0100531
Christof Schmitt799b76d2009-08-18 15:43:20 +0200532 INIT_WORK(&port->gid_pn_work, zfcp_fc_port_did_lookup);
Christof Schmitt8fdf30d2009-03-02 13:09:01 +0100533 INIT_WORK(&port->test_link_work, zfcp_fc_link_test_work);
Christof Schmitta2fa0ae2009-03-02 13:09:08 +0100534 INIT_WORK(&port->rport_work, zfcp_scsi_rport_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
536 port->adapter = adapter;
Swen Schillig317e6b62008-07-02 10:56:37 +0200537 port->d_id = d_id;
538 port->wwpn = wwpn;
Christof Schmitta2fa0ae2009-03-02 13:09:08 +0100539 port->rport_task = RPORT_NONE;
Christof Schmitt615f59e2010-02-17 11:18:56 +0100540 port->dev.parent = &adapter->ccw_device->dev;
Sebastian Ott83d4e1c2013-04-26 16:13:48 +0200541 port->dev.groups = zfcp_port_attr_groups;
Christof Schmitt615f59e2010-02-17 11:18:56 +0100542 port->dev.release = zfcp_port_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
Steffen Maierab31fd02017-10-13 15:40:07 +0200544 port->erp_action.adapter = adapter;
545 port->erp_action.port = port;
546
Christof Schmitt615f59e2010-02-17 11:18:56 +0100547 if (dev_set_name(&port->dev, "0x%016llx", (unsigned long long)wwpn)) {
Christof Schmitt0fac3f42009-08-18 15:43:30 +0200548 kfree(port);
Swen Schilligf3450c72009-11-24 16:53:59 +0100549 goto err_out;
Christof Schmitt0fac3f42009-08-18 15:43:30 +0200550 }
Swen Schilligf3450c72009-11-24 16:53:59 +0100551 retval = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
Christof Schmitt615f59e2010-02-17 11:18:56 +0100553 if (device_register(&port->dev)) {
554 put_device(&port->dev);
Swen Schilligf3450c72009-11-24 16:53:59 +0100555 goto err_out;
Sebastian Ottf4395b62009-08-18 15:43:29 +0200556 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
Swen Schilligecf0c772009-11-24 16:53:58 +0100558 write_lock_irq(&adapter->port_list_lock);
559 list_add_tail(&port->list, &adapter->port_list);
560 write_unlock_irq(&adapter->port_list_lock);
561
Peter Zijlstra805de8f42015-04-24 01:12:32 +0200562 atomic_or(status | ZFCP_STATUS_COMMON_RUNNING, &port->status);
Swen Schillig317e6b62008-07-02 10:56:37 +0200563
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 return port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
Swen Schilligf3450c72009-11-24 16:53:59 +0100566err_out:
Swen Schilligde3dc572009-11-24 16:54:00 +0100567 zfcp_ccw_adapter_put(adapter);
Swen Schilligf3450c72009-11-24 16:53:59 +0100568 return ERR_PTR(retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569}