4 * Module interface and handling of zfcp data structures.
6 * Copyright IBM Corporation 2002, 2009
11 * Martin Peschke (originator of the driver)
16 * Heiko Carstens (kernel 2.6 port of the driver)
28 #define KMSG_COMPONENT "zfcp"
29 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
31 #include <linux/miscdevice.h>
32 #include <linux/seq_file.h>
35 #define ZFCP_BUS_ID_SIZE 20
37 MODULE_AUTHOR("IBM Deutschland Entwicklung GmbH - linux390@de.ibm.com");
38 MODULE_DESCRIPTION("FCP HBA driver");
39 MODULE_LICENSE("GPL");
41 static char *init_device;
42 module_param_named(device, init_device, charp, 0400);
43 MODULE_PARM_DESC(device, "specify initial device");
45 static struct kmem_cache *zfcp_cache_hw_align(const char *name,
48 return kmem_cache_create(name, size, roundup_pow_of_two(size), 0, NULL);
51 static int zfcp_reqlist_alloc(struct zfcp_adapter *adapter)
55 adapter->req_list = kcalloc(REQUEST_LIST_SIZE, sizeof(struct list_head),
57 if (!adapter->req_list)
60 for (idx = 0; idx < REQUEST_LIST_SIZE; idx++)
61 INIT_LIST_HEAD(&adapter->req_list[idx]);
66 * zfcp_reqlist_isempty - is the request list empty
67 * @adapter: pointer to struct zfcp_adapter
69 * Returns: true if list is empty, false otherwise
71 int zfcp_reqlist_isempty(struct zfcp_adapter *adapter)
75 for (idx = 0; idx < REQUEST_LIST_SIZE; idx++)
76 if (!list_empty(&adapter->req_list[idx]))
81 static void __init zfcp_init_device_configure(char *busid, u64 wwpn, u64 lun)
83 struct ccw_device *ccwdev;
84 struct zfcp_adapter *adapter;
85 struct zfcp_port *port;
86 struct zfcp_unit *unit;
88 ccwdev = get_ccwdev_by_busid(&zfcp_ccw_driver, busid);
92 if (ccw_device_set_online(ccwdev))
95 mutex_lock(&zfcp_data.config_mutex);
96 adapter = dev_get_drvdata(&ccwdev->dev);
99 zfcp_adapter_get(adapter);
101 port = zfcp_get_port_by_wwpn(adapter, wwpn);
106 unit = zfcp_unit_enqueue(port, lun);
109 mutex_unlock(&zfcp_data.config_mutex);
111 zfcp_erp_unit_reopen(unit, 0, "auidc_1", NULL);
112 zfcp_erp_wait(adapter);
113 flush_work(&unit->scsi_work);
115 mutex_lock(&zfcp_data.config_mutex);
120 zfcp_adapter_put(adapter);
122 mutex_unlock(&zfcp_data.config_mutex);
124 put_device(&ccwdev->dev);
128 static void __init zfcp_init_device_setup(char *devstr)
132 char busid[ZFCP_BUS_ID_SIZE];
135 /* duplicate devstr and keep the original for sysfs presentation*/
136 str = kmalloc(strlen(devstr) + 1, GFP_KERNEL);
142 token = strsep(&str, ",");
143 if (!token || strlen(token) >= ZFCP_BUS_ID_SIZE)
145 strncpy(busid, token, ZFCP_BUS_ID_SIZE);
147 token = strsep(&str, ",");
148 if (!token || strict_strtoull(token, 0, (unsigned long long *) &wwpn))
151 token = strsep(&str, ",");
152 if (!token || strict_strtoull(token, 0, (unsigned long long *) &lun))
156 zfcp_init_device_configure(busid, wwpn, lun);
161 pr_err("%s is not a valid SCSI device\n", devstr);
164 static int __init zfcp_module_init(void)
166 int retval = -ENOMEM;
168 zfcp_data.gpn_ft_cache = zfcp_cache_hw_align("zfcp_gpn",
169 sizeof(struct ct_iu_gpn_ft_req));
170 if (!zfcp_data.gpn_ft_cache)
173 zfcp_data.qtcb_cache = zfcp_cache_hw_align("zfcp_qtcb",
174 sizeof(struct fsf_qtcb));
175 if (!zfcp_data.qtcb_cache)
178 zfcp_data.sr_buffer_cache = zfcp_cache_hw_align("zfcp_sr",
179 sizeof(struct fsf_status_read_buffer));
180 if (!zfcp_data.sr_buffer_cache)
183 zfcp_data.gid_pn_cache = zfcp_cache_hw_align("zfcp_gid",
184 sizeof(struct zfcp_gid_pn_data));
185 if (!zfcp_data.gid_pn_cache)
188 mutex_init(&zfcp_data.config_mutex);
189 rwlock_init(&zfcp_data.config_lock);
191 zfcp_data.scsi_transport_template =
192 fc_attach_transport(&zfcp_transport_functions);
193 if (!zfcp_data.scsi_transport_template)
196 retval = misc_register(&zfcp_cfdc_misc);
198 pr_err("Registering the misc device zfcp_cfdc failed\n");
202 retval = zfcp_ccw_register();
204 pr_err("The zfcp device driver could not register with "
205 "the common I/O layer\n");
206 goto out_ccw_register;
210 zfcp_init_device_setup(init_device);
214 misc_deregister(&zfcp_cfdc_misc);
216 fc_release_transport(zfcp_data.scsi_transport_template);
218 kmem_cache_destroy(zfcp_data.gid_pn_cache);
220 kmem_cache_destroy(zfcp_data.sr_buffer_cache);
222 kmem_cache_destroy(zfcp_data.qtcb_cache);
224 kmem_cache_destroy(zfcp_data.gpn_ft_cache);
229 module_init(zfcp_module_init);
232 * zfcp_get_unit_by_lun - find unit in unit list of port by FCP LUN
233 * @port: pointer to port to search for unit
234 * @fcp_lun: FCP LUN to search for
236 * Returns: pointer to zfcp_unit or NULL
238 struct zfcp_unit *zfcp_get_unit_by_lun(struct zfcp_port *port, u64 fcp_lun)
240 struct zfcp_unit *unit;
242 list_for_each_entry(unit, &port->unit_list_head, list)
243 if ((unit->fcp_lun == fcp_lun) &&
244 !(atomic_read(&unit->status) & ZFCP_STATUS_COMMON_REMOVE))
250 * zfcp_get_port_by_wwpn - find port in port list of adapter by wwpn
251 * @adapter: pointer to adapter to search for port
252 * @wwpn: wwpn to search for
254 * Returns: pointer to zfcp_port or NULL
256 struct zfcp_port *zfcp_get_port_by_wwpn(struct zfcp_adapter *adapter,
259 struct zfcp_port *port;
261 list_for_each_entry(port, &adapter->port_list_head, list)
262 if ((port->wwpn == wwpn) &&
263 !(atomic_read(&port->status) & ZFCP_STATUS_COMMON_REMOVE))
268 static void zfcp_sysfs_unit_release(struct device *dev)
270 kfree(container_of(dev, struct zfcp_unit, sysfs_device));
274 * zfcp_unit_enqueue - enqueue unit to unit list of a port.
275 * @port: pointer to port where unit is added
276 * @fcp_lun: FCP LUN of unit to be enqueued
277 * Returns: pointer to enqueued unit on success, ERR_PTR on error
278 * Locks: config_mutex must be held to serialize changes to the unit list
280 * Sets up some unit internal structures and creates sysfs entry.
282 struct zfcp_unit *zfcp_unit_enqueue(struct zfcp_port *port, u64 fcp_lun)
284 struct zfcp_unit *unit;
286 read_lock_irq(&zfcp_data.config_lock);
287 if (zfcp_get_unit_by_lun(port, fcp_lun)) {
288 read_unlock_irq(&zfcp_data.config_lock);
289 return ERR_PTR(-EINVAL);
291 read_unlock_irq(&zfcp_data.config_lock);
293 unit = kzalloc(sizeof(struct zfcp_unit), GFP_KERNEL);
295 return ERR_PTR(-ENOMEM);
297 atomic_set(&unit->refcount, 0);
298 init_waitqueue_head(&unit->remove_wq);
299 INIT_WORK(&unit->scsi_work, zfcp_scsi_scan);
302 unit->fcp_lun = fcp_lun;
304 if (dev_set_name(&unit->sysfs_device, "0x%016llx",
305 (unsigned long long) fcp_lun)) {
307 return ERR_PTR(-ENOMEM);
309 unit->sysfs_device.parent = &port->sysfs_device;
310 unit->sysfs_device.release = zfcp_sysfs_unit_release;
311 dev_set_drvdata(&unit->sysfs_device, unit);
313 /* mark unit unusable as long as sysfs registration is not complete */
314 atomic_set_mask(ZFCP_STATUS_COMMON_REMOVE, &unit->status);
316 spin_lock_init(&unit->latencies.lock);
317 unit->latencies.write.channel.min = 0xFFFFFFFF;
318 unit->latencies.write.fabric.min = 0xFFFFFFFF;
319 unit->latencies.read.channel.min = 0xFFFFFFFF;
320 unit->latencies.read.fabric.min = 0xFFFFFFFF;
321 unit->latencies.cmd.channel.min = 0xFFFFFFFF;
322 unit->latencies.cmd.fabric.min = 0xFFFFFFFF;
324 if (device_register(&unit->sysfs_device)) {
325 put_device(&unit->sysfs_device);
326 return ERR_PTR(-EINVAL);
329 if (sysfs_create_group(&unit->sysfs_device.kobj,
330 &zfcp_sysfs_unit_attrs)) {
331 device_unregister(&unit->sysfs_device);
332 return ERR_PTR(-EINVAL);
337 write_lock_irq(&zfcp_data.config_lock);
338 list_add_tail(&unit->list, &port->unit_list_head);
339 atomic_clear_mask(ZFCP_STATUS_COMMON_REMOVE, &unit->status);
340 atomic_set_mask(ZFCP_STATUS_COMMON_RUNNING, &unit->status);
342 write_unlock_irq(&zfcp_data.config_lock);
350 * zfcp_unit_dequeue - dequeue unit
351 * @unit: pointer to zfcp_unit
353 * waits until all work is done on unit and removes it then from the unit->list
354 * of the associated port.
356 void zfcp_unit_dequeue(struct zfcp_unit *unit)
358 wait_event(unit->remove_wq, atomic_read(&unit->refcount) == 0);
359 write_lock_irq(&zfcp_data.config_lock);
360 list_del(&unit->list);
361 write_unlock_irq(&zfcp_data.config_lock);
362 zfcp_port_put(unit->port);
363 sysfs_remove_group(&unit->sysfs_device.kobj, &zfcp_sysfs_unit_attrs);
364 device_unregister(&unit->sysfs_device);
367 static int zfcp_allocate_low_mem_buffers(struct zfcp_adapter *adapter)
369 /* must only be called with zfcp_data.config_mutex taken */
370 adapter->pool.erp_req =
371 mempool_create_kmalloc_pool(1, sizeof(struct zfcp_fsf_req));
372 if (!adapter->pool.erp_req)
375 adapter->pool.gid_pn_req =
376 mempool_create_kmalloc_pool(1, sizeof(struct zfcp_fsf_req));
377 if (!adapter->pool.gid_pn_req)
380 adapter->pool.scsi_req =
381 mempool_create_kmalloc_pool(1, sizeof(struct zfcp_fsf_req));
382 if (!adapter->pool.scsi_req)
385 adapter->pool.scsi_abort =
386 mempool_create_kmalloc_pool(1, sizeof(struct zfcp_fsf_req));
387 if (!adapter->pool.scsi_abort)
390 adapter->pool.status_read_req =
391 mempool_create_kmalloc_pool(FSF_STATUS_READS_RECOM,
392 sizeof(struct zfcp_fsf_req));
393 if (!adapter->pool.status_read_req)
396 adapter->pool.qtcb_pool =
397 mempool_create_slab_pool(4, zfcp_data.qtcb_cache);
398 if (!adapter->pool.qtcb_pool)
401 adapter->pool.status_read_data =
402 mempool_create_slab_pool(FSF_STATUS_READS_RECOM,
403 zfcp_data.sr_buffer_cache);
404 if (!adapter->pool.status_read_data)
407 adapter->pool.gid_pn_data =
408 mempool_create_slab_pool(1, zfcp_data.gid_pn_cache);
409 if (!adapter->pool.gid_pn_data)
415 static void zfcp_free_low_mem_buffers(struct zfcp_adapter *adapter)
417 /* zfcp_data.config_mutex must be held */
418 if (adapter->pool.erp_req)
419 mempool_destroy(adapter->pool.erp_req);
420 if (adapter->pool.scsi_req)
421 mempool_destroy(adapter->pool.scsi_req);
422 if (adapter->pool.scsi_abort)
423 mempool_destroy(adapter->pool.scsi_abort);
424 if (adapter->pool.qtcb_pool)
425 mempool_destroy(adapter->pool.qtcb_pool);
426 if (adapter->pool.status_read_req)
427 mempool_destroy(adapter->pool.status_read_req);
428 if (adapter->pool.status_read_data)
429 mempool_destroy(adapter->pool.status_read_data);
430 if (adapter->pool.gid_pn_data)
431 mempool_destroy(adapter->pool.gid_pn_data);
435 * zfcp_status_read_refill - refill the long running status_read_requests
436 * @adapter: ptr to struct zfcp_adapter for which the buffers should be refilled
438 * Returns: 0 on success, 1 otherwise
440 * if there are 16 or more status_read requests missing an adapter_reopen
443 int zfcp_status_read_refill(struct zfcp_adapter *adapter)
445 while (atomic_read(&adapter->stat_miss) > 0)
446 if (zfcp_fsf_status_read(adapter->qdio)) {
447 if (atomic_read(&adapter->stat_miss) >= 16) {
448 zfcp_erp_adapter_reopen(adapter, 0, "axsref1",
454 atomic_dec(&adapter->stat_miss);
458 static void _zfcp_status_read_scheduler(struct work_struct *work)
460 zfcp_status_read_refill(container_of(work, struct zfcp_adapter,
464 static void zfcp_print_sl(struct seq_file *m, struct service_level *sl)
466 struct zfcp_adapter *adapter =
467 container_of(sl, struct zfcp_adapter, service_level);
469 seq_printf(m, "zfcp: %s microcode level %x\n",
470 dev_name(&adapter->ccw_device->dev),
471 adapter->fsf_lic_version);
474 static int zfcp_setup_adapter_work_queue(struct zfcp_adapter *adapter)
476 char name[TASK_COMM_LEN];
478 snprintf(name, sizeof(name), "zfcp_q_%s",
479 dev_name(&adapter->ccw_device->dev));
480 adapter->work_queue = create_singlethread_workqueue(name);
482 if (adapter->work_queue)
487 static void zfcp_destroy_adapter_work_queue(struct zfcp_adapter *adapter)
489 if (adapter->work_queue)
490 destroy_workqueue(adapter->work_queue);
491 adapter->work_queue = NULL;
496 * zfcp_adapter_enqueue - enqueue a new adapter to the list
497 * @ccw_device: pointer to the struct cc_device
499 * Returns: 0 if a new adapter was successfully enqueued
500 * -ENOMEM if alloc failed
501 * Enqueues an adapter at the end of the adapter list in the driver data.
502 * All adapter internal structures are set up.
503 * Proc-fs entries are also created.
504 * locks: config_mutex must be held to serialize changes to the adapter list
506 int zfcp_adapter_enqueue(struct ccw_device *ccw_device)
508 struct zfcp_adapter *adapter;
511 * Note: It is safe to release the list_lock, as any list changes
512 * are protected by the config_mutex, which must be held to get here
515 adapter = kzalloc(sizeof(struct zfcp_adapter), GFP_KERNEL);
519 ccw_device->handler = NULL;
520 adapter->ccw_device = ccw_device;
521 atomic_set(&adapter->refcount, 0);
523 if (zfcp_qdio_setup(adapter))
526 if (zfcp_allocate_low_mem_buffers(adapter))
527 goto low_mem_buffers_failed;
529 if (zfcp_reqlist_alloc(adapter))
530 goto low_mem_buffers_failed;
532 if (zfcp_dbf_adapter_register(adapter))
533 goto debug_register_failed;
535 if (zfcp_setup_adapter_work_queue(adapter))
536 goto work_queue_failed;
538 if (zfcp_fc_gs_setup(adapter))
539 goto generic_services_failed;
541 init_waitqueue_head(&adapter->remove_wq);
542 init_waitqueue_head(&adapter->erp_ready_wq);
543 init_waitqueue_head(&adapter->erp_done_wqh);
545 INIT_LIST_HEAD(&adapter->port_list_head);
546 INIT_LIST_HEAD(&adapter->erp_ready_head);
547 INIT_LIST_HEAD(&adapter->erp_running_head);
549 spin_lock_init(&adapter->req_list_lock);
551 rwlock_init(&adapter->erp_lock);
552 rwlock_init(&adapter->abort_lock);
554 if (zfcp_erp_thread_setup(adapter))
555 goto erp_thread_failed;
557 INIT_WORK(&adapter->stat_work, _zfcp_status_read_scheduler);
558 INIT_WORK(&adapter->scan_work, _zfcp_fc_scan_ports_later);
560 adapter->service_level.seq_print = zfcp_print_sl;
562 /* mark adapter unusable as long as sysfs registration is not complete */
563 atomic_set_mask(ZFCP_STATUS_COMMON_REMOVE, &adapter->status);
565 dev_set_drvdata(&ccw_device->dev, adapter);
567 if (sysfs_create_group(&ccw_device->dev.kobj,
568 &zfcp_sysfs_adapter_attrs))
571 atomic_clear_mask(ZFCP_STATUS_COMMON_REMOVE, &adapter->status);
573 if (!zfcp_adapter_scsi_register(adapter))
577 zfcp_erp_thread_kill(adapter);
579 zfcp_fc_gs_destroy(adapter);
580 generic_services_failed:
581 zfcp_destroy_adapter_work_queue(adapter);
583 zfcp_dbf_adapter_unregister(adapter->dbf);
584 debug_register_failed:
585 dev_set_drvdata(&ccw_device->dev, NULL);
586 kfree(adapter->req_list);
587 low_mem_buffers_failed:
588 zfcp_free_low_mem_buffers(adapter);
590 zfcp_qdio_destroy(adapter->qdio);
596 * zfcp_adapter_dequeue - remove the adapter from the resource list
597 * @adapter: pointer to struct zfcp_adapter which should be removed
598 * locks: adapter list write lock is assumed to be held by caller
600 void zfcp_adapter_dequeue(struct zfcp_adapter *adapter)
605 cancel_work_sync(&adapter->stat_work);
606 zfcp_fc_wka_ports_force_offline(adapter->gs);
607 sysfs_remove_group(&adapter->ccw_device->dev.kobj,
608 &zfcp_sysfs_adapter_attrs);
609 dev_set_drvdata(&adapter->ccw_device->dev, NULL);
610 /* sanity check: no pending FSF requests */
611 spin_lock_irqsave(&adapter->req_list_lock, flags);
612 retval = zfcp_reqlist_isempty(adapter);
613 spin_unlock_irqrestore(&adapter->req_list_lock, flags);
617 zfcp_fc_gs_destroy(adapter);
618 zfcp_erp_thread_kill(adapter);
619 zfcp_destroy_adapter_work_queue(adapter);
620 zfcp_dbf_adapter_unregister(adapter->dbf);
621 zfcp_free_low_mem_buffers(adapter);
622 zfcp_qdio_destroy(adapter->qdio);
623 kfree(adapter->req_list);
624 kfree(adapter->fc_stats);
625 kfree(adapter->stats_reset_data);
629 static void zfcp_sysfs_port_release(struct device *dev)
631 kfree(container_of(dev, struct zfcp_port, sysfs_device));
635 * zfcp_port_enqueue - enqueue port to port list of adapter
636 * @adapter: adapter where remote port is added
637 * @wwpn: WWPN of the remote port to be enqueued
638 * @status: initial status for the port
639 * @d_id: destination id of the remote port to be enqueued
640 * Returns: pointer to enqueued port on success, ERR_PTR on error
641 * Locks: config_mutex must be held to serialize changes to the port list
643 * All port internal structures are set up and the sysfs entry is generated.
644 * d_id is used to enqueue ports with a well known address like the Directory
645 * Service for nameserver lookup.
647 struct zfcp_port *zfcp_port_enqueue(struct zfcp_adapter *adapter, u64 wwpn,
648 u32 status, u32 d_id)
650 struct zfcp_port *port;
652 read_lock_irq(&zfcp_data.config_lock);
653 if (zfcp_get_port_by_wwpn(adapter, wwpn)) {
654 read_unlock_irq(&zfcp_data.config_lock);
655 return ERR_PTR(-EINVAL);
657 read_unlock_irq(&zfcp_data.config_lock);
659 port = kzalloc(sizeof(struct zfcp_port), GFP_KERNEL);
661 return ERR_PTR(-ENOMEM);
663 init_waitqueue_head(&port->remove_wq);
664 INIT_LIST_HEAD(&port->unit_list_head);
665 INIT_WORK(&port->gid_pn_work, zfcp_fc_port_did_lookup);
666 INIT_WORK(&port->test_link_work, zfcp_fc_link_test_work);
667 INIT_WORK(&port->rport_work, zfcp_scsi_rport_work);
669 port->adapter = adapter;
672 port->rport_task = RPORT_NONE;
674 /* mark port unusable as long as sysfs registration is not complete */
675 atomic_set_mask(status | ZFCP_STATUS_COMMON_REMOVE, &port->status);
676 atomic_set(&port->refcount, 0);
678 if (dev_set_name(&port->sysfs_device, "0x%016llx",
679 (unsigned long long)wwpn)) {
681 return ERR_PTR(-ENOMEM);
683 port->sysfs_device.parent = &adapter->ccw_device->dev;
684 port->sysfs_device.release = zfcp_sysfs_port_release;
685 dev_set_drvdata(&port->sysfs_device, port);
687 if (device_register(&port->sysfs_device)) {
688 put_device(&port->sysfs_device);
689 return ERR_PTR(-EINVAL);
692 if (sysfs_create_group(&port->sysfs_device.kobj,
693 &zfcp_sysfs_port_attrs)) {
694 device_unregister(&port->sysfs_device);
695 return ERR_PTR(-EINVAL);
700 write_lock_irq(&zfcp_data.config_lock);
701 list_add_tail(&port->list, &adapter->port_list_head);
702 atomic_clear_mask(ZFCP_STATUS_COMMON_REMOVE, &port->status);
703 atomic_set_mask(ZFCP_STATUS_COMMON_RUNNING, &port->status);
705 write_unlock_irq(&zfcp_data.config_lock);
707 zfcp_adapter_get(adapter);
712 * zfcp_port_dequeue - dequeues a port from the port list of the adapter
713 * @port: pointer to struct zfcp_port which should be removed
715 void zfcp_port_dequeue(struct zfcp_port *port)
717 write_lock_irq(&zfcp_data.config_lock);
718 list_del(&port->list);
719 write_unlock_irq(&zfcp_data.config_lock);
720 wait_event(port->remove_wq, atomic_read(&port->refcount) == 0);
721 cancel_work_sync(&port->rport_work); /* usually not necessary */
722 zfcp_adapter_put(port->adapter);
723 sysfs_remove_group(&port->sysfs_device.kobj, &zfcp_sysfs_port_attrs);
724 device_unregister(&port->sysfs_device);
728 * zfcp_sg_free_table - free memory used by scatterlists
729 * @sg: pointer to scatterlist
730 * @count: number of scatterlist which are to be free'ed
731 * the scatterlist are expected to reference pages always
733 void zfcp_sg_free_table(struct scatterlist *sg, int count)
737 for (i = 0; i < count; i++, sg++)
739 free_page((unsigned long) sg_virt(sg));
745 * zfcp_sg_setup_table - init scatterlist and allocate, assign buffers
746 * @sg: pointer to struct scatterlist
747 * @count: number of scatterlists which should be assigned with buffers
750 * Returns: 0 on success, -ENOMEM otherwise
752 int zfcp_sg_setup_table(struct scatterlist *sg, int count)
757 sg_init_table(sg, count);
758 for (i = 0; i < count; i++, sg++) {
759 addr = (void *) get_zeroed_page(GFP_KERNEL);
761 zfcp_sg_free_table(sg, i);
764 sg_set_buf(sg, addr, PAGE_SIZE);