zfcp_aux.c 13.7 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0
L
Linus Torvalds 已提交
2
/*
C
Christof Schmitt 已提交
3
 * zfcp device driver
L
Linus Torvalds 已提交
4
 *
C
Christof Schmitt 已提交
5
 * Module interface and handling of zfcp data structures.
L
Linus Torvalds 已提交
6
 *
7
 * Copyright IBM Corp. 2002, 2017
L
Linus Torvalds 已提交
8 9
 */

10 11 12 13 14 15 16 17 18 19 20 21
/*
 * Driver authors:
 *            Martin Peschke (originator of the driver)
 *            Raimund Schroeder
 *            Aron Zeh
 *            Wolfgang Taphorn
 *            Stefan Bader
 *            Heiko Carstens (kernel 2.6 port of the driver)
 *            Andreas Herrmann
 *            Maxim Shchetynin
 *            Volker Sameske
 *            Ralph Wuerthner
C
Christof Schmitt 已提交
22 23 24 25 26
 *            Michael Loehr
 *            Swen Schillig
 *            Christof Schmitt
 *            Martin Petermann
 *            Sven Schuetz
27
 *            Steffen Maier
28 29
 */

30 31 32
#define KMSG_COMPONENT "zfcp"
#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt

33
#include <linux/seq_file.h>
34
#include <linux/slab.h>
35
#include <linux/module.h>
L
Linus Torvalds 已提交
36
#include "zfcp_ext.h"
37
#include "zfcp_fc.h"
38
#include "zfcp_reqlist.h"
L
Linus Torvalds 已提交
39

40 41
#define ZFCP_BUS_ID_SIZE	20

42
MODULE_AUTHOR("IBM Deutschland Entwicklung GmbH - linux390@de.ibm.com");
43
MODULE_DESCRIPTION("FCP HBA driver");
L
Linus Torvalds 已提交
44 45
MODULE_LICENSE("GPL");

46 47
static char *init_device;
module_param_named(device, init_device, charp, 0400);
L
Linus Torvalds 已提交
48 49
MODULE_PARM_DESC(device, "specify initial device");

50 51
static struct kmem_cache * __init zfcp_cache_hw_align(const char *name,
						      unsigned long size)
52 53 54 55
{
	return kmem_cache_create(name, size, roundup_pow_of_two(size), 0, NULL);
}

56
static void __init zfcp_init_device_configure(char *busid, u64 wwpn, u64 lun)
L
Linus Torvalds 已提交
57
{
58
	struct ccw_device *cdev;
L
Linus Torvalds 已提交
59 60 61
	struct zfcp_adapter *adapter;
	struct zfcp_port *port;

62 63
	cdev = get_ccwdev_by_busid(&zfcp_ccw_driver, busid);
	if (!cdev)
64 65
		return;

66 67
	if (ccw_device_set_online(cdev))
		goto out_ccw_device;
L
Linus Torvalds 已提交
68

69
	adapter = zfcp_ccw_adapter_by_cdev(cdev);
70
	if (!adapter)
71
		goto out_ccw_device;
72 73 74

	port = zfcp_get_port_by_wwpn(adapter, wwpn);
	if (!port)
L
Linus Torvalds 已提交
75
		goto out_port;
76
	flush_work(&port->rport_work);
77

78
	zfcp_unit_add(port, lun);
79
	put_device(&port->dev);
80

81
out_port:
82 83 84
	zfcp_ccw_adapter_put(adapter);
out_ccw_device:
	put_device(&cdev->dev);
L
Linus Torvalds 已提交
85 86 87
	return;
}

88 89 90
static void __init zfcp_init_device_setup(char *devstr)
{
	char *token;
91
	char *str, *str_saved;
92 93 94 95
	char busid[ZFCP_BUS_ID_SIZE];
	u64 wwpn, lun;

	/* duplicate devstr and keep the original for sysfs presentation*/
96
	str_saved = kstrdup(devstr, GFP_KERNEL);
97
	str = str_saved;
98 99 100 101 102 103
	if (!str)
		return;

	token = strsep(&str, ",");
	if (!token || strlen(token) >= ZFCP_BUS_ID_SIZE)
		goto err_out;
104
	strlcpy(busid, token, ZFCP_BUS_ID_SIZE);
105 106

	token = strsep(&str, ",");
107
	if (!token || kstrtoull(token, 0, (unsigned long long *) &wwpn))
108 109 110
		goto err_out;

	token = strsep(&str, ",");
111
	if (!token || kstrtoull(token, 0, (unsigned long long *) &lun))
112 113
		goto err_out;

114
	kfree(str_saved);
115 116 117
	zfcp_init_device_configure(busid, wwpn, lun);
	return;

118 119
err_out:
	kfree(str_saved);
120 121 122
	pr_err("%s is not a valid SCSI device\n", devstr);
}

123
static int __init zfcp_module_init(void)
L
Linus Torvalds 已提交
124
{
125 126
	int retval = -ENOMEM;

127 128 129
	if (zfcp_experimental_dix)
		pr_warn("DIX is enabled. It is experimental and might cause problems\n");

130 131 132
	zfcp_fsf_qtcb_cache = zfcp_cache_hw_align("zfcp_fsf_qtcb",
						  sizeof(struct fsf_qtcb));
	if (!zfcp_fsf_qtcb_cache)
133 134
		goto out_qtcb_cache;

135 136 137 138
	zfcp_fc_req_cache = zfcp_cache_hw_align("zfcp_fc_req",
						sizeof(struct zfcp_fc_req));
	if (!zfcp_fc_req_cache)
		goto out_fc_cache;
139

140
	zfcp_scsi_transport_template =
141
		fc_attach_transport(&zfcp_transport_functions);
142
	if (!zfcp_scsi_transport_template)
143
		goto out_transport;
144
	scsi_transport_reserve_device(zfcp_scsi_transport_template,
145 146
				      sizeof(struct zfcp_scsi_dev));

147
	retval = ccw_driver_register(&zfcp_ccw_driver);
L
Linus Torvalds 已提交
148
	if (retval) {
149
		pr_err("The zfcp device driver could not register with "
150
		       "the common I/O layer\n");
L
Linus Torvalds 已提交
151 152 153
		goto out_ccw_register;
	}

154 155 156
	if (init_device)
		zfcp_init_device_setup(init_device);
	return 0;
L
Linus Torvalds 已提交
157

158
out_ccw_register:
159
	fc_release_transport(zfcp_scsi_transport_template);
160
out_transport:
161 162
	kmem_cache_destroy(zfcp_fc_req_cache);
out_fc_cache:
163
	kmem_cache_destroy(zfcp_fsf_qtcb_cache);
164
out_qtcb_cache:
L
Linus Torvalds 已提交
165 166 167
	return retval;
}

168
module_init(zfcp_module_init);
L
Linus Torvalds 已提交
169

170 171 172
static void __exit zfcp_module_exit(void)
{
	ccw_driver_unregister(&zfcp_ccw_driver);
173
	fc_release_transport(zfcp_scsi_transport_template);
174
	kmem_cache_destroy(zfcp_fc_req_cache);
175
	kmem_cache_destroy(zfcp_fsf_qtcb_cache);
176 177 178 179
}

module_exit(zfcp_module_exit);

L
Linus Torvalds 已提交
180 181 182 183
/**
 * zfcp_get_port_by_wwpn - find port in port list of adapter by wwpn
 * @adapter: pointer to adapter to search for port
 * @wwpn: wwpn to search for
184 185
 *
 * Returns: pointer to zfcp_port or NULL
L
Linus Torvalds 已提交
186
 */
187
struct zfcp_port *zfcp_get_port_by_wwpn(struct zfcp_adapter *adapter,
188
					u64 wwpn)
L
Linus Torvalds 已提交
189
{
190
	unsigned long flags;
L
Linus Torvalds 已提交
191 192
	struct zfcp_port *port;

193 194
	read_lock_irqsave(&adapter->port_list_lock, flags);
	list_for_each_entry(port, &adapter->port_list, list)
195
		if (port->wwpn == wwpn) {
196
			if (!get_device(&port->dev))
197
				port = NULL;
198
			read_unlock_irqrestore(&adapter->port_list_lock, flags);
199
			return port;
200 201
		}
	read_unlock_irqrestore(&adapter->port_list_lock, flags);
202
	return NULL;
L
Linus Torvalds 已提交
203 204
}

205
static int zfcp_allocate_low_mem_buffers(struct zfcp_adapter *adapter)
L
Linus Torvalds 已提交
206
{
207 208 209
	adapter->pool.erp_req =
		mempool_create_kmalloc_pool(1, sizeof(struct zfcp_fsf_req));
	if (!adapter->pool.erp_req)
L
Linus Torvalds 已提交
210 211
		return -ENOMEM;

212 213 214 215 216
	adapter->pool.gid_pn_req =
		mempool_create_kmalloc_pool(1, sizeof(struct zfcp_fsf_req));
	if (!adapter->pool.gid_pn_req)
		return -ENOMEM;

217 218 219
	adapter->pool.scsi_req =
		mempool_create_kmalloc_pool(1, sizeof(struct zfcp_fsf_req));
	if (!adapter->pool.scsi_req)
L
Linus Torvalds 已提交
220 221
		return -ENOMEM;

222 223 224
	adapter->pool.scsi_abort =
		mempool_create_kmalloc_pool(1, sizeof(struct zfcp_fsf_req));
	if (!adapter->pool.scsi_abort)
L
Linus Torvalds 已提交
225 226
		return -ENOMEM;

227
	adapter->pool.status_read_req =
228
		mempool_create_kmalloc_pool(FSF_STATUS_READS_RECOM,
229
					    sizeof(struct zfcp_fsf_req));
230 231 232 233
	if (!adapter->pool.status_read_req)
		return -ENOMEM;

	adapter->pool.qtcb_pool =
234
		mempool_create_slab_pool(4, zfcp_fsf_qtcb_cache);
235
	if (!adapter->pool.qtcb_pool)
L
Linus Torvalds 已提交
236 237
		return -ENOMEM;

238 239 240 241
	BUILD_BUG_ON(sizeof(struct fsf_status_read_buffer) > PAGE_SIZE);
	adapter->pool.sr_data =
		mempool_create_page_pool(FSF_STATUS_READS_RECOM, 0);
	if (!adapter->pool.sr_data)
L
Linus Torvalds 已提交
242 243
		return -ENOMEM;

244
	adapter->pool.gid_pn =
245
		mempool_create_slab_pool(1, zfcp_fc_req_cache);
246
	if (!adapter->pool.gid_pn)
L
Linus Torvalds 已提交
247 248 249 250 251
		return -ENOMEM;

	return 0;
}

252
static void zfcp_free_low_mem_buffers(struct zfcp_adapter *adapter)
L
Linus Torvalds 已提交
253
{
254 255 256 257 258 259 260
	mempool_destroy(adapter->pool.erp_req);
	mempool_destroy(adapter->pool.scsi_req);
	mempool_destroy(adapter->pool.scsi_abort);
	mempool_destroy(adapter->pool.qtcb_pool);
	mempool_destroy(adapter->pool.status_read_req);
	mempool_destroy(adapter->pool.sr_data);
	mempool_destroy(adapter->pool.gid_pn);
L
Linus Torvalds 已提交
261 262
}

263 264 265 266
/**
 * zfcp_status_read_refill - refill the long running status_read_requests
 * @adapter: ptr to struct zfcp_adapter for which the buffers should be refilled
 *
267 268 269 270
 * Return:
 * * 0 on success meaning at least one status read is pending
 * * 1 if posting failed and not a single status read buffer is pending,
 *     also triggers adapter reopen recovery
271
 */
272 273
int zfcp_status_read_refill(struct zfcp_adapter *adapter)
{
274
	while (atomic_add_unless(&adapter->stat_miss, -1, 0))
275
		if (zfcp_fsf_status_read(adapter->qdio)) {
276
			atomic_inc(&adapter->stat_miss); /* undo add -1 */
277 278
			if (atomic_read(&adapter->stat_miss) >=
			    adapter->stat_read_buf_num) {
279
				zfcp_erp_adapter_reopen(adapter, 0, "axsref1");
280 281
				return 1;
			}
282
			break;
283
		}
284 285 286 287 288 289 290 291 292
	return 0;
}

static void _zfcp_status_read_scheduler(struct work_struct *work)
{
	zfcp_status_read_refill(container_of(work, struct zfcp_adapter,
					     stat_work));
}

293 294 295 296 297 298 299 300 301 302
static void zfcp_print_sl(struct seq_file *m, struct service_level *sl)
{
	struct zfcp_adapter *adapter =
		container_of(sl, struct zfcp_adapter, service_level);

	seq_printf(m, "zfcp: %s microcode level %x\n",
		   dev_name(&adapter->ccw_device->dev),
		   adapter->fsf_lic_version);
}

303 304 305 306 307 308
static int zfcp_setup_adapter_work_queue(struct zfcp_adapter *adapter)
{
	char name[TASK_COMM_LEN];

	snprintf(name, sizeof(name), "zfcp_q_%s",
		 dev_name(&adapter->ccw_device->dev));
309
	adapter->work_queue = alloc_ordered_workqueue(name, WQ_MEM_RECLAIM);
310 311 312 313 314 315 316 317 318 319 320 321 322 323

	if (adapter->work_queue)
		return 0;
	return -ENOMEM;
}

static void zfcp_destroy_adapter_work_queue(struct zfcp_adapter *adapter)
{
	if (adapter->work_queue)
		destroy_workqueue(adapter->work_queue);
	adapter->work_queue = NULL;

}

324 325 326 327
/**
 * zfcp_adapter_enqueue - enqueue a new adapter to the list
 * @ccw_device: pointer to the struct cc_device
 *
328
 * Returns:	struct zfcp_adapter*
L
Linus Torvalds 已提交
329 330 331 332
 * Enqueues an adapter at the end of the adapter list in the driver data.
 * All adapter internal structures are set up.
 * Proc-fs entries are also created.
 */
333
struct zfcp_adapter *zfcp_adapter_enqueue(struct ccw_device *ccw_device)
L
Linus Torvalds 已提交
334 335 336
{
	struct zfcp_adapter *adapter;

337
	if (!get_device(&ccw_device->dev))
338
		return ERR_PTR(-ENODEV);
L
Linus Torvalds 已提交
339

340
	adapter = kzalloc(sizeof(struct zfcp_adapter), GFP_KERNEL);
341 342
	if (!adapter) {
		put_device(&ccw_device->dev);
343
		return ERR_PTR(-ENOMEM);
344 345 346
	}

	kref_init(&adapter->ref);
L
Linus Torvalds 已提交
347 348 349

	ccw_device->handler = NULL;
	adapter->ccw_device = ccw_device;
350 351

	INIT_WORK(&adapter->stat_work, _zfcp_status_read_scheduler);
M
Martin Peschke 已提交
352
	INIT_DELAYED_WORK(&adapter->scan_work, zfcp_fc_scan_ports);
353
	INIT_WORK(&adapter->ns_up_work, zfcp_fc_sym_name_update);
L
Linus Torvalds 已提交
354

M
Martin Peschke 已提交
355 356
	adapter->next_port_scan = jiffies;

357 358
	adapter->erp_action.adapter = adapter;

359
	if (zfcp_qdio_setup(adapter))
360
		goto failed;
L
Linus Torvalds 已提交
361

S
Swen Schillig 已提交
362
	if (zfcp_allocate_low_mem_buffers(adapter))
363
		goto failed;
L
Linus Torvalds 已提交
364

365 366
	adapter->req_list = zfcp_reqlist_alloc();
	if (!adapter->req_list)
367
		goto failed;
368

S
Swen Schillig 已提交
369
	if (zfcp_dbf_adapter_register(adapter))
370
		goto failed;
371

372
	if (zfcp_setup_adapter_work_queue(adapter))
373
		goto failed;
374

375
	if (zfcp_fc_gs_setup(adapter))
376
		goto failed;
377

378 379 380
	rwlock_init(&adapter->port_list_lock);
	INIT_LIST_HEAD(&adapter->port_list);

381 382 383 384
	INIT_LIST_HEAD(&adapter->events.list);
	INIT_WORK(&adapter->events.work, zfcp_fc_post_event);
	spin_lock_init(&adapter->events.list_lock);

385
	init_waitqueue_head(&adapter->erp_ready_wq);
386
	init_waitqueue_head(&adapter->erp_done_wqh);
L
Linus Torvalds 已提交
387

388 389
	INIT_LIST_HEAD(&adapter->erp_ready_head);
	INIT_LIST_HEAD(&adapter->erp_running_head);
L
Linus Torvalds 已提交
390

391
	rwlock_init(&adapter->erp_lock);
L
Linus Torvalds 已提交
392 393
	rwlock_init(&adapter->abort_lock);

394
	if (zfcp_erp_thread_setup(adapter))
395
		goto failed;
L
Linus Torvalds 已提交
396

397 398
	adapter->service_level.seq_print = zfcp_print_sl;

L
Linus Torvalds 已提交
399 400
	dev_set_drvdata(&ccw_device->dev, adapter);

401 402
	if (sysfs_create_group(&ccw_device->dev.kobj,
			       &zfcp_sysfs_adapter_attrs))
403
		goto failed;
L
Linus Torvalds 已提交
404

405 406 407
	/* report size limit per scatter-gather segment */
	adapter->ccw_device->dev.dma_parms = &adapter->dma_parms;

408 409
	adapter->stat_read_buf_num = FSF_STATUS_READS_RECOM;

410
	if (!zfcp_scsi_adapter_register(adapter))
411
		return adapter;
L
Linus Torvalds 已提交
412

413
failed:
414 415 416 417 418 419 420 421
	zfcp_adapter_unregister(adapter);
	return ERR_PTR(-ENOMEM);
}

void zfcp_adapter_unregister(struct zfcp_adapter *adapter)
{
	struct ccw_device *cdev = adapter->ccw_device;

M
Martin Peschke 已提交
422
	cancel_delayed_work_sync(&adapter->scan_work);
423
	cancel_work_sync(&adapter->stat_work);
424
	cancel_work_sync(&adapter->ns_up_work);
425 426 427
	zfcp_destroy_adapter_work_queue(adapter);

	zfcp_fc_wka_ports_force_offline(adapter->gs);
428
	zfcp_scsi_adapter_unregister(adapter);
429 430 431
	sysfs_remove_group(&cdev->dev.kobj, &zfcp_sysfs_adapter_attrs);

	zfcp_erp_thread_kill(adapter);
432
	zfcp_dbf_adapter_unregister(adapter);
433 434 435
	zfcp_qdio_destroy(adapter->qdio);

	zfcp_ccw_adapter_put(adapter); /* final put to release */
L
Linus Torvalds 已提交
436 437
}

438
/**
439 440
 * zfcp_adapter_release - remove the adapter from the resource list
 * @ref: pointer to struct kref
L
Linus Torvalds 已提交
441 442
 * locks:	adapter list write lock is assumed to be held by caller
 */
443
void zfcp_adapter_release(struct kref *ref)
L
Linus Torvalds 已提交
444
{
445 446
	struct zfcp_adapter *adapter = container_of(ref, struct zfcp_adapter,
						    ref);
447
	struct ccw_device *cdev = adapter->ccw_device;
L
Linus Torvalds 已提交
448

449
	dev_set_drvdata(&adapter->ccw_device->dev, NULL);
450
	zfcp_fc_gs_destroy(adapter);
L
Linus Torvalds 已提交
451
	zfcp_free_low_mem_buffers(adapter);
452
	kfree(adapter->req_list);
453 454
	kfree(adapter->fc_stats);
	kfree(adapter->stats_reset_data);
L
Linus Torvalds 已提交
455
	kfree(adapter);
456
	put_device(&cdev->dev);
457 458 459
}

static void zfcp_port_release(struct device *dev)
460
{
461
	struct zfcp_port *port = container_of(dev, struct zfcp_port, dev);
462

463
	zfcp_ccw_adapter_put(port->adapter);
464
	kfree(port);
465 466
}

L
Linus Torvalds 已提交
467 468 469 470 471 472
/**
 * zfcp_port_enqueue - enqueue port to port list of adapter
 * @adapter: adapter where remote port is added
 * @wwpn: WWPN of the remote port to be enqueued
 * @status: initial status for the port
 * @d_id: destination id of the remote port to be enqueued
473
 * Returns: pointer to enqueued port on success, ERR_PTR on error
L
Linus Torvalds 已提交
474 475 476 477 478
 *
 * All port internal structures are set up and the sysfs entry is generated.
 * d_id is used to enqueue ports with a well known address like the Directory
 * Service for nameserver lookup.
 */
479
struct zfcp_port *zfcp_port_enqueue(struct zfcp_adapter *adapter, u64 wwpn,
480
				     u32 status, u32 d_id)
L
Linus Torvalds 已提交
481
{
482
	struct zfcp_port *port;
483 484 485
	int retval = -ENOMEM;

	kref_get(&adapter->ref);
486

487 488
	port = zfcp_get_port_by_wwpn(adapter, wwpn);
	if (port) {
489
		put_device(&port->dev);
490 491
		retval = -EEXIST;
		goto err_out;
492
	}
L
Linus Torvalds 已提交
493

494
	port = kzalloc(sizeof(struct zfcp_port), GFP_KERNEL);
L
Linus Torvalds 已提交
495
	if (!port)
496
		goto err_out;
L
Linus Torvalds 已提交
497

498 499
	rwlock_init(&port->unit_list_lock);
	INIT_LIST_HEAD(&port->unit_list);
500
	atomic_set(&port->units, 0);
501

502
	INIT_WORK(&port->gid_pn_work, zfcp_fc_port_did_lookup);
503
	INIT_WORK(&port->test_link_work, zfcp_fc_link_test_work);
504
	INIT_WORK(&port->rport_work, zfcp_scsi_rport_work);
L
Linus Torvalds 已提交
505 506

	port->adapter = adapter;
507 508
	port->d_id = d_id;
	port->wwpn = wwpn;
509
	port->rport_task = RPORT_NONE;
510
	port->dev.parent = &adapter->ccw_device->dev;
511
	port->dev.groups = zfcp_port_attr_groups;
512
	port->dev.release = zfcp_port_release;
L
Linus Torvalds 已提交
513

514 515 516
	port->erp_action.adapter = adapter;
	port->erp_action.port = port;

517
	if (dev_set_name(&port->dev, "0x%016llx", (unsigned long long)wwpn)) {
518
		kfree(port);
519
		goto err_out;
520
	}
521
	retval = -EINVAL;
L
Linus Torvalds 已提交
522

523 524
	if (device_register(&port->dev)) {
		put_device(&port->dev);
525
		goto err_out;
526
	}
L
Linus Torvalds 已提交
527

528 529 530 531
	write_lock_irq(&adapter->port_list_lock);
	list_add_tail(&port->list, &adapter->port_list);
	write_unlock_irq(&adapter->port_list_lock);

532
	atomic_or(status | ZFCP_STATUS_COMMON_RUNNING, &port->status);
533

L
Linus Torvalds 已提交
534 535
	return port;

536
err_out:
537
	zfcp_ccw_adapter_put(adapter);
538
	return ERR_PTR(retval);
L
Linus Torvalds 已提交
539
}