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 zfcp_adapter *adapter;
84 struct zfcp_port *port;
85 struct zfcp_unit *unit;
87 down(&zfcp_data.config_sema);
88 read_lock_irq(&zfcp_data.config_lock);
89 adapter = zfcp_get_adapter_by_busid(busid);
91 zfcp_adapter_get(adapter);
92 read_unlock_irq(&zfcp_data.config_lock);
96 port = zfcp_port_enqueue(adapter, wwpn, 0, 0);
99 unit = zfcp_unit_enqueue(port, lun);
102 up(&zfcp_data.config_sema);
103 ccw_device_set_online(adapter->ccw_device);
105 zfcp_erp_wait(adapter);
106 flush_work(&unit->scsi_work);
108 down(&zfcp_data.config_sema);
113 zfcp_adapter_put(adapter);
115 up(&zfcp_data.config_sema);
119 static void __init zfcp_init_device_setup(char *devstr)
123 char busid[ZFCP_BUS_ID_SIZE];
126 /* duplicate devstr and keep the original for sysfs presentation*/
127 str = kmalloc(strlen(devstr) + 1, GFP_KERNEL);
133 token = strsep(&str, ",");
134 if (!token || strlen(token) >= ZFCP_BUS_ID_SIZE)
136 strncpy(busid, token, ZFCP_BUS_ID_SIZE);
138 token = strsep(&str, ",");
139 if (!token || strict_strtoull(token, 0, (unsigned long long *) &wwpn))
142 token = strsep(&str, ",");
143 if (!token || strict_strtoull(token, 0, (unsigned long long *) &lun))
147 zfcp_init_device_configure(busid, wwpn, lun);
152 pr_err("%s is not a valid SCSI device\n", devstr);
155 static int __init zfcp_module_init(void)
157 int retval = -ENOMEM;
159 zfcp_data.gpn_ft_cache = zfcp_cache_hw_align("zfcp_gpn",
160 sizeof(struct ct_iu_gpn_ft_req));
161 if (!zfcp_data.gpn_ft_cache)
164 zfcp_data.qtcb_cache = zfcp_cache_hw_align("zfcp_qtcb",
165 sizeof(struct fsf_qtcb));
166 if (!zfcp_data.qtcb_cache)
169 zfcp_data.sr_buffer_cache = zfcp_cache_hw_align("zfcp_sr",
170 sizeof(struct fsf_status_read_buffer));
171 if (!zfcp_data.sr_buffer_cache)
174 zfcp_data.gid_pn_cache = zfcp_cache_hw_align("zfcp_gid",
175 sizeof(struct zfcp_gid_pn_data));
176 if (!zfcp_data.gid_pn_cache)
179 sema_init(&zfcp_data.config_sema, 1);
180 rwlock_init(&zfcp_data.config_lock);
182 zfcp_data.scsi_transport_template =
183 fc_attach_transport(&zfcp_transport_functions);
184 if (!zfcp_data.scsi_transport_template)
187 retval = misc_register(&zfcp_cfdc_misc);
189 pr_err("Registering the misc device zfcp_cfdc failed\n");
193 retval = zfcp_ccw_register();
195 pr_err("The zfcp device driver could not register with "
196 "the common I/O layer\n");
197 goto out_ccw_register;
201 zfcp_init_device_setup(init_device);
205 misc_deregister(&zfcp_cfdc_misc);
207 fc_release_transport(zfcp_data.scsi_transport_template);
209 kmem_cache_destroy(zfcp_data.gid_pn_cache);
211 kmem_cache_destroy(zfcp_data.sr_buffer_cache);
213 kmem_cache_destroy(zfcp_data.qtcb_cache);
215 kmem_cache_destroy(zfcp_data.gpn_ft_cache);
220 module_init(zfcp_module_init);
223 * zfcp_get_unit_by_lun - find unit in unit list of port by FCP LUN
224 * @port: pointer to port to search for unit
225 * @fcp_lun: FCP LUN to search for
227 * Returns: pointer to zfcp_unit or NULL
229 struct zfcp_unit *zfcp_get_unit_by_lun(struct zfcp_port *port, u64 fcp_lun)
231 struct zfcp_unit *unit;
233 list_for_each_entry(unit, &port->unit_list_head, list)
234 if ((unit->fcp_lun == fcp_lun) &&
235 !(atomic_read(&unit->status) & ZFCP_STATUS_COMMON_REMOVE))
241 * zfcp_get_port_by_wwpn - find port in port list of adapter by wwpn
242 * @adapter: pointer to adapter to search for port
243 * @wwpn: wwpn to search for
245 * Returns: pointer to zfcp_port or NULL
247 struct zfcp_port *zfcp_get_port_by_wwpn(struct zfcp_adapter *adapter,
250 struct zfcp_port *port;
252 list_for_each_entry(port, &adapter->port_list_head, list)
253 if ((port->wwpn == wwpn) &&
254 !(atomic_read(&port->status) & ZFCP_STATUS_COMMON_REMOVE))
259 static void zfcp_sysfs_unit_release(struct device *dev)
261 kfree(container_of(dev, struct zfcp_unit, sysfs_device));
265 * zfcp_unit_enqueue - enqueue unit to unit list of a port.
266 * @port: pointer to port where unit is added
267 * @fcp_lun: FCP LUN of unit to be enqueued
268 * Returns: pointer to enqueued unit on success, ERR_PTR on error
269 * Locks: config_sema must be held to serialize changes to the unit list
271 * Sets up some unit internal structures and creates sysfs entry.
273 struct zfcp_unit *zfcp_unit_enqueue(struct zfcp_port *port, u64 fcp_lun)
275 struct zfcp_unit *unit;
277 unit = kzalloc(sizeof(struct zfcp_unit), GFP_KERNEL);
279 return ERR_PTR(-ENOMEM);
281 atomic_set(&unit->refcount, 0);
282 init_waitqueue_head(&unit->remove_wq);
283 INIT_WORK(&unit->scsi_work, zfcp_scsi_scan);
286 unit->fcp_lun = fcp_lun;
288 dev_set_name(&unit->sysfs_device, "0x%016llx",
289 (unsigned long long) fcp_lun);
290 unit->sysfs_device.parent = &port->sysfs_device;
291 unit->sysfs_device.release = zfcp_sysfs_unit_release;
292 dev_set_drvdata(&unit->sysfs_device, unit);
294 /* mark unit unusable as long as sysfs registration is not complete */
295 atomic_set_mask(ZFCP_STATUS_COMMON_REMOVE, &unit->status);
297 spin_lock_init(&unit->latencies.lock);
298 unit->latencies.write.channel.min = 0xFFFFFFFF;
299 unit->latencies.write.fabric.min = 0xFFFFFFFF;
300 unit->latencies.read.channel.min = 0xFFFFFFFF;
301 unit->latencies.read.fabric.min = 0xFFFFFFFF;
302 unit->latencies.cmd.channel.min = 0xFFFFFFFF;
303 unit->latencies.cmd.fabric.min = 0xFFFFFFFF;
305 read_lock_irq(&zfcp_data.config_lock);
306 if (zfcp_get_unit_by_lun(port, fcp_lun)) {
307 read_unlock_irq(&zfcp_data.config_lock);
310 read_unlock_irq(&zfcp_data.config_lock);
312 if (device_register(&unit->sysfs_device))
315 if (sysfs_create_group(&unit->sysfs_device.kobj,
316 &zfcp_sysfs_unit_attrs)) {
317 device_unregister(&unit->sysfs_device);
318 return ERR_PTR(-EIO);
323 write_lock_irq(&zfcp_data.config_lock);
324 list_add_tail(&unit->list, &port->unit_list_head);
325 atomic_clear_mask(ZFCP_STATUS_COMMON_REMOVE, &unit->status);
326 atomic_set_mask(ZFCP_STATUS_COMMON_RUNNING, &unit->status);
328 write_unlock_irq(&zfcp_data.config_lock);
336 return ERR_PTR(-EINVAL);
340 * zfcp_unit_dequeue - dequeue unit
341 * @unit: pointer to zfcp_unit
343 * waits until all work is done on unit and removes it then from the unit->list
344 * of the associated port.
346 void zfcp_unit_dequeue(struct zfcp_unit *unit)
348 wait_event(unit->remove_wq, atomic_read(&unit->refcount) == 0);
349 write_lock_irq(&zfcp_data.config_lock);
350 list_del(&unit->list);
351 write_unlock_irq(&zfcp_data.config_lock);
352 zfcp_port_put(unit->port);
353 sysfs_remove_group(&unit->sysfs_device.kobj, &zfcp_sysfs_unit_attrs);
354 device_unregister(&unit->sysfs_device);
357 static int zfcp_allocate_low_mem_buffers(struct zfcp_adapter *adapter)
359 /* must only be called with zfcp_data.config_sema taken */
360 adapter->pool.erp_req =
361 mempool_create_kmalloc_pool(1, sizeof(struct zfcp_fsf_req));
362 if (!adapter->pool.erp_req)
365 adapter->pool.scsi_req =
366 mempool_create_kmalloc_pool(1, sizeof(struct zfcp_fsf_req));
367 if (!adapter->pool.scsi_req)
370 adapter->pool.scsi_abort =
371 mempool_create_kmalloc_pool(1, sizeof(struct zfcp_fsf_req));
372 if (!adapter->pool.scsi_abort)
375 adapter->pool.status_read_req =
376 mempool_create_kmalloc_pool(FSF_STATUS_READS_RECOM,
377 sizeof(struct zfcp_fsf_req));
378 if (!adapter->pool.status_read_req)
381 adapter->pool.qtcb_pool =
382 mempool_create_slab_pool(3, zfcp_data.qtcb_cache);
383 if (!adapter->pool.qtcb_pool)
386 adapter->pool.status_read_data =
387 mempool_create_slab_pool(FSF_STATUS_READS_RECOM,
388 zfcp_data.sr_buffer_cache);
389 if (!adapter->pool.status_read_data)
392 adapter->pool.gid_pn_data =
393 mempool_create_slab_pool(1, zfcp_data.gid_pn_cache);
394 if (!adapter->pool.gid_pn_data)
400 static void zfcp_free_low_mem_buffers(struct zfcp_adapter *adapter)
402 /* zfcp_data.config_sema must be held */
403 if (adapter->pool.erp_req)
404 mempool_destroy(adapter->pool.erp_req);
405 if (adapter->pool.scsi_req)
406 mempool_destroy(adapter->pool.scsi_req);
407 if (adapter->pool.scsi_abort)
408 mempool_destroy(adapter->pool.scsi_abort);
409 if (adapter->pool.qtcb_pool)
410 mempool_destroy(adapter->pool.qtcb_pool);
411 if (adapter->pool.status_read_req)
412 mempool_destroy(adapter->pool.status_read_req);
413 if (adapter->pool.status_read_data)
414 mempool_destroy(adapter->pool.status_read_data);
415 if (adapter->pool.gid_pn_data)
416 mempool_destroy(adapter->pool.gid_pn_data);
420 * zfcp_status_read_refill - refill the long running status_read_requests
421 * @adapter: ptr to struct zfcp_adapter for which the buffers should be refilled
423 * Returns: 0 on success, 1 otherwise
425 * if there are 16 or more status_read requests missing an adapter_reopen
428 int zfcp_status_read_refill(struct zfcp_adapter *adapter)
430 while (atomic_read(&adapter->stat_miss) > 0)
431 if (zfcp_fsf_status_read(adapter)) {
432 if (atomic_read(&adapter->stat_miss) >= 16) {
433 zfcp_erp_adapter_reopen(adapter, 0, "axsref1",
439 atomic_dec(&adapter->stat_miss);
443 static void _zfcp_status_read_scheduler(struct work_struct *work)
445 zfcp_status_read_refill(container_of(work, struct zfcp_adapter,
449 static void zfcp_print_sl(struct seq_file *m, struct service_level *sl)
451 struct zfcp_adapter *adapter =
452 container_of(sl, struct zfcp_adapter, service_level);
454 seq_printf(m, "zfcp: %s microcode level %x\n",
455 dev_name(&adapter->ccw_device->dev),
456 adapter->fsf_lic_version);
459 static int zfcp_setup_adapter_work_queue(struct zfcp_adapter *adapter)
461 char name[TASK_COMM_LEN];
463 snprintf(name, sizeof(name), "zfcp_q_%s",
464 dev_name(&adapter->ccw_device->dev));
465 adapter->work_queue = create_singlethread_workqueue(name);
467 if (adapter->work_queue)
472 static void zfcp_destroy_adapter_work_queue(struct zfcp_adapter *adapter)
474 if (adapter->work_queue)
475 destroy_workqueue(adapter->work_queue);
476 adapter->work_queue = NULL;
481 * zfcp_adapter_enqueue - enqueue a new adapter to the list
482 * @ccw_device: pointer to the struct cc_device
484 * Returns: 0 if a new adapter was successfully enqueued
485 * -ENOMEM if alloc failed
486 * Enqueues an adapter at the end of the adapter list in the driver data.
487 * All adapter internal structures are set up.
488 * Proc-fs entries are also created.
489 * locks: config_sema must be held to serialise changes to the adapter list
491 int zfcp_adapter_enqueue(struct ccw_device *ccw_device)
493 struct zfcp_adapter *adapter;
496 * Note: It is safe to release the list_lock, as any list changes
497 * are protected by the config_sema, which must be held to get here
500 adapter = kzalloc(sizeof(struct zfcp_adapter), GFP_KERNEL);
504 adapter->gs = kzalloc(sizeof(struct zfcp_wka_ports), GFP_KERNEL);
510 ccw_device->handler = NULL;
511 adapter->ccw_device = ccw_device;
512 atomic_set(&adapter->refcount, 0);
514 if (zfcp_qdio_allocate(adapter))
515 goto qdio_allocate_failed;
517 if (zfcp_allocate_low_mem_buffers(adapter))
518 goto failed_low_mem_buffers;
520 if (zfcp_reqlist_alloc(adapter))
521 goto failed_low_mem_buffers;
523 if (zfcp_adapter_debug_register(adapter))
524 goto debug_register_failed;
526 if (zfcp_setup_adapter_work_queue(adapter))
527 goto work_queue_failed;
529 init_waitqueue_head(&adapter->remove_wq);
530 init_waitqueue_head(&adapter->erp_thread_wqh);
531 init_waitqueue_head(&adapter->erp_done_wqh);
533 INIT_LIST_HEAD(&adapter->port_list_head);
534 INIT_LIST_HEAD(&adapter->erp_ready_head);
535 INIT_LIST_HEAD(&adapter->erp_running_head);
537 spin_lock_init(&adapter->req_list_lock);
539 spin_lock_init(&adapter->req_q_lock);
540 spin_lock_init(&adapter->qdio_stat_lock);
542 rwlock_init(&adapter->erp_lock);
543 rwlock_init(&adapter->abort_lock);
545 sema_init(&adapter->erp_ready_sem, 0);
547 INIT_WORK(&adapter->stat_work, _zfcp_status_read_scheduler);
548 INIT_WORK(&adapter->scan_work, _zfcp_scan_ports_later);
550 adapter->service_level.seq_print = zfcp_print_sl;
552 /* mark adapter unusable as long as sysfs registration is not complete */
553 atomic_set_mask(ZFCP_STATUS_COMMON_REMOVE, &adapter->status);
555 dev_set_drvdata(&ccw_device->dev, adapter);
557 if (sysfs_create_group(&ccw_device->dev.kobj,
558 &zfcp_sysfs_adapter_attrs))
561 atomic_clear_mask(ZFCP_STATUS_COMMON_REMOVE, &adapter->status);
562 zfcp_fc_wka_ports_init(adapter);
564 if (!zfcp_adapter_scsi_register(adapter))
568 zfcp_destroy_adapter_work_queue(adapter);
570 zfcp_adapter_debug_unregister(adapter);
571 debug_register_failed:
572 dev_set_drvdata(&ccw_device->dev, NULL);
573 kfree(adapter->req_list);
574 failed_low_mem_buffers:
575 zfcp_free_low_mem_buffers(adapter);
576 qdio_allocate_failed:
577 zfcp_qdio_free(adapter);
583 * zfcp_adapter_dequeue - remove the adapter from the resource list
584 * @adapter: pointer to struct zfcp_adapter which should be removed
585 * locks: adapter list write lock is assumed to be held by caller
587 void zfcp_adapter_dequeue(struct zfcp_adapter *adapter)
592 cancel_work_sync(&adapter->scan_work);
593 cancel_work_sync(&adapter->stat_work);
594 zfcp_fc_wka_ports_force_offline(adapter->gs);
595 zfcp_adapter_scsi_unregister(adapter);
596 sysfs_remove_group(&adapter->ccw_device->dev.kobj,
597 &zfcp_sysfs_adapter_attrs);
598 dev_set_drvdata(&adapter->ccw_device->dev, NULL);
599 /* sanity check: no pending FSF requests */
600 spin_lock_irqsave(&adapter->req_list_lock, flags);
601 retval = zfcp_reqlist_isempty(adapter);
602 spin_unlock_irqrestore(&adapter->req_list_lock, flags);
606 zfcp_destroy_adapter_work_queue(adapter);
607 zfcp_adapter_debug_unregister(adapter);
608 zfcp_qdio_free(adapter);
609 zfcp_free_low_mem_buffers(adapter);
610 kfree(adapter->req_list);
611 kfree(adapter->fc_stats);
612 kfree(adapter->stats_reset_data);
617 static void zfcp_sysfs_port_release(struct device *dev)
619 kfree(container_of(dev, struct zfcp_port, sysfs_device));
623 * zfcp_port_enqueue - enqueue port to port list of adapter
624 * @adapter: adapter where remote port is added
625 * @wwpn: WWPN of the remote port to be enqueued
626 * @status: initial status for the port
627 * @d_id: destination id of the remote port to be enqueued
628 * Returns: pointer to enqueued port on success, ERR_PTR on error
629 * Locks: config_sema must be held to serialize changes to the port list
631 * All port internal structures are set up and the sysfs entry is generated.
632 * d_id is used to enqueue ports with a well known address like the Directory
633 * Service for nameserver lookup.
635 struct zfcp_port *zfcp_port_enqueue(struct zfcp_adapter *adapter, u64 wwpn,
636 u32 status, u32 d_id)
638 struct zfcp_port *port;
641 port = kzalloc(sizeof(struct zfcp_port), GFP_KERNEL);
643 return ERR_PTR(-ENOMEM);
645 init_waitqueue_head(&port->remove_wq);
646 INIT_LIST_HEAD(&port->unit_list_head);
647 INIT_WORK(&port->gid_pn_work, zfcp_erp_port_strategy_open_lookup);
648 INIT_WORK(&port->test_link_work, zfcp_fc_link_test_work);
649 INIT_WORK(&port->rport_work, zfcp_scsi_rport_work);
651 port->adapter = adapter;
654 port->rport_task = RPORT_NONE;
656 /* mark port unusable as long as sysfs registration is not complete */
657 atomic_set_mask(status | ZFCP_STATUS_COMMON_REMOVE, &port->status);
658 atomic_set(&port->refcount, 0);
660 dev_set_name(&port->sysfs_device, "0x%016llx",
661 (unsigned long long)wwpn);
662 port->sysfs_device.parent = &adapter->ccw_device->dev;
664 port->sysfs_device.release = zfcp_sysfs_port_release;
665 dev_set_drvdata(&port->sysfs_device, port);
667 read_lock_irq(&zfcp_data.config_lock);
668 if (zfcp_get_port_by_wwpn(adapter, wwpn)) {
669 read_unlock_irq(&zfcp_data.config_lock);
672 read_unlock_irq(&zfcp_data.config_lock);
674 if (device_register(&port->sysfs_device))
677 retval = sysfs_create_group(&port->sysfs_device.kobj,
678 &zfcp_sysfs_port_attrs);
681 device_unregister(&port->sysfs_device);
687 write_lock_irq(&zfcp_data.config_lock);
688 list_add_tail(&port->list, &adapter->port_list_head);
689 atomic_clear_mask(ZFCP_STATUS_COMMON_REMOVE, &port->status);
690 atomic_set_mask(ZFCP_STATUS_COMMON_RUNNING, &port->status);
692 write_unlock_irq(&zfcp_data.config_lock);
694 zfcp_adapter_get(adapter);
700 return ERR_PTR(-EINVAL);
704 * zfcp_port_dequeue - dequeues a port from the port list of the adapter
705 * @port: pointer to struct zfcp_port which should be removed
707 void zfcp_port_dequeue(struct zfcp_port *port)
709 write_lock_irq(&zfcp_data.config_lock);
710 list_del(&port->list);
711 write_unlock_irq(&zfcp_data.config_lock);
713 port->rport->dd_data = NULL;
716 wait_event(port->remove_wq, atomic_read(&port->refcount) == 0);
717 cancel_work_sync(&port->rport_work); /* usually not necessary */
718 zfcp_adapter_put(port->adapter);
719 sysfs_remove_group(&port->sysfs_device.kobj, &zfcp_sysfs_port_attrs);
720 device_unregister(&port->sysfs_device);
724 * zfcp_sg_free_table - free memory used by scatterlists
725 * @sg: pointer to scatterlist
726 * @count: number of scatterlist which are to be free'ed
727 * the scatterlist are expected to reference pages always
729 void zfcp_sg_free_table(struct scatterlist *sg, int count)
733 for (i = 0; i < count; i++, sg++)
735 free_page((unsigned long) sg_virt(sg));
741 * zfcp_sg_setup_table - init scatterlist and allocate, assign buffers
742 * @sg: pointer to struct scatterlist
743 * @count: number of scatterlists which should be assigned with buffers
746 * Returns: 0 on success, -ENOMEM otherwise
748 int zfcp_sg_setup_table(struct scatterlist *sg, int count)
753 sg_init_table(sg, count);
754 for (i = 0; i < count; i++, sg++) {
755 addr = (void *) get_zeroed_page(GFP_KERNEL);
757 zfcp_sg_free_table(sg, i);
760 sg_set_buf(sg, addr, PAGE_SIZE);