2 * This file is part of the zfcp device driver for
3 * FCP adapters for IBM System z9 and zSeries.
5 * (C) Copyright IBM Corp. 2002, 2006
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2, or (at your option)
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 * Martin Peschke (originator of the driver)
29 * Heiko Carstens (kernel 2.6 port of the driver)
38 /* accumulated log level (module parameter) */
39 static u32 loglevel = ZFCP_LOG_LEVEL_DEFAULTS;
41 /*********************** FUNCTION PROTOTYPES *********************************/
43 /* written against the module interface */
44 static int __init zfcp_module_init(void);
47 static int zfcp_sg_list_alloc(struct zfcp_sg_list *, size_t);
48 static void zfcp_sg_list_free(struct zfcp_sg_list *);
49 static int zfcp_sg_list_copy_from_user(struct zfcp_sg_list *,
50 void __user *, size_t);
51 static int zfcp_sg_list_copy_to_user(void __user *,
52 struct zfcp_sg_list *, size_t);
53 static long zfcp_cfdc_dev_ioctl(struct file *, unsigned int, unsigned long);
55 #define ZFCP_CFDC_IOC_MAGIC 0xDD
56 #define ZFCP_CFDC_IOC \
57 _IOWR(ZFCP_CFDC_IOC_MAGIC, 0, struct zfcp_cfdc_sense_data)
60 static const struct file_operations zfcp_cfdc_fops = {
61 .unlocked_ioctl = zfcp_cfdc_dev_ioctl,
63 .compat_ioctl = zfcp_cfdc_dev_ioctl
67 static struct miscdevice zfcp_cfdc_misc = {
68 .minor = ZFCP_CFDC_DEV_MINOR,
69 .name = ZFCP_CFDC_DEV_NAME,
70 .fops = &zfcp_cfdc_fops
73 /*********************** KERNEL/MODULE PARAMETERS ***************************/
75 /* declare driver module init/cleanup functions */
76 module_init(zfcp_module_init);
78 MODULE_AUTHOR("IBM Deutschland Entwicklung GmbH - linux390@de.ibm.com");
80 ("FCP (SCSI over Fibre Channel) HBA driver for IBM System z9 and zSeries");
81 MODULE_LICENSE("GPL");
83 module_param(device, charp, 0400);
84 MODULE_PARM_DESC(device, "specify initial device");
86 module_param(loglevel, uint, 0400);
87 MODULE_PARM_DESC(loglevel,
88 "log levels, 8 nibbles: "
89 "FC ERP QDIO CIO Config FSF SCSI Other, "
90 "levels: 0=none 1=normal 2=devel 3=trace");
92 /****************************************************************/
93 /************** Functions without logging ***********************/
94 /****************************************************************/
97 _zfcp_hex_dump(char *addr, int count)
100 for (i = 0; i < count; i++) {
101 printk("%02x", addr[i]);
107 if (((i-1) % 32) != 31)
112 /****************************************************************/
113 /****** Functions to handle the request ID hash table ********/
114 /****************************************************************/
116 #define ZFCP_LOG_AREA ZFCP_LOG_AREA_FSF
118 static int zfcp_reqlist_alloc(struct zfcp_adapter *adapter)
122 adapter->req_list = kcalloc(REQUEST_LIST_SIZE, sizeof(struct list_head),
124 if (!adapter->req_list)
127 for (idx = 0; idx < REQUEST_LIST_SIZE; idx++)
128 INIT_LIST_HEAD(&adapter->req_list[idx]);
132 static void zfcp_reqlist_free(struct zfcp_adapter *adapter)
134 kfree(adapter->req_list);
137 int zfcp_reqlist_isempty(struct zfcp_adapter *adapter)
141 for (idx = 0; idx < REQUEST_LIST_SIZE; idx++)
142 if (!list_empty(&adapter->req_list[idx]))
149 /****************************************************************/
150 /************** Uncategorised Functions *************************/
151 /****************************************************************/
153 #define ZFCP_LOG_AREA ZFCP_LOG_AREA_OTHER
156 * zfcp_device_setup - setup function
157 * @str: pointer to parameter string
159 * Parse "device=..." parameter string.
162 zfcp_device_setup(char *devstr)
170 len = strlen(devstr) + 1;
171 str = kmalloc(len, GFP_KERNEL);
174 memcpy(str, devstr, len);
176 tmp = strchr(str, ',');
180 strncpy(zfcp_data.init_busid, str, BUS_ID_SIZE);
181 zfcp_data.init_busid[BUS_ID_SIZE-1] = '\0';
183 zfcp_data.init_wwpn = simple_strtoull(tmp, &tmp, 0);
189 zfcp_data.init_fcp_lun = simple_strtoull(tmp, &tmp, 0);
196 ZFCP_LOG_NORMAL("Parse error for device parameter string %s\n", str);
202 zfcp_init_device_configure(void)
204 struct zfcp_adapter *adapter;
205 struct zfcp_port *port;
206 struct zfcp_unit *unit;
208 down(&zfcp_data.config_sema);
209 read_lock_irq(&zfcp_data.config_lock);
210 adapter = zfcp_get_adapter_by_busid(zfcp_data.init_busid);
212 zfcp_adapter_get(adapter);
213 read_unlock_irq(&zfcp_data.config_lock);
217 port = zfcp_port_enqueue(adapter, zfcp_data.init_wwpn, 0, 0);
220 unit = zfcp_unit_enqueue(port, zfcp_data.init_fcp_lun);
223 up(&zfcp_data.config_sema);
224 ccw_device_set_online(adapter->ccw_device);
225 zfcp_erp_wait(adapter);
226 down(&zfcp_data.config_sema);
231 zfcp_adapter_put(adapter);
233 up(&zfcp_data.config_sema);
237 static int calc_alignment(int size)
244 while ((size - align) > 0)
251 zfcp_module_init(void)
253 int retval = -ENOMEM;
256 size = sizeof(struct zfcp_fsf_req_qtcb);
257 align = calc_alignment(size);
258 zfcp_data.fsf_req_qtcb_cache =
259 kmem_cache_create("zfcp_fsf", size, align, 0, NULL);
260 if (!zfcp_data.fsf_req_qtcb_cache)
263 size = sizeof(struct fsf_status_read_buffer);
264 align = calc_alignment(size);
265 zfcp_data.sr_buffer_cache =
266 kmem_cache_create("zfcp_sr", size, align, 0, NULL);
267 if (!zfcp_data.sr_buffer_cache)
270 size = sizeof(struct zfcp_gid_pn_data);
271 align = calc_alignment(size);
272 zfcp_data.gid_pn_cache =
273 kmem_cache_create("zfcp_gid", size, align, 0, NULL);
274 if (!zfcp_data.gid_pn_cache)
277 atomic_set(&zfcp_data.loglevel, loglevel);
279 /* initialize adapter list */
280 INIT_LIST_HEAD(&zfcp_data.adapter_list_head);
282 /* initialize adapters to be removed list head */
283 INIT_LIST_HEAD(&zfcp_data.adapter_remove_lh);
285 zfcp_data.scsi_transport_template =
286 fc_attach_transport(&zfcp_transport_functions);
287 if (!zfcp_data.scsi_transport_template)
290 retval = misc_register(&zfcp_cfdc_misc);
292 ZFCP_LOG_INFO("registration of misc device "
293 "zfcp_cfdc failed\n");
297 ZFCP_LOG_TRACE("major/minor for zfcp_cfdc: %d/%d\n",
298 ZFCP_CFDC_DEV_MAJOR, zfcp_cfdc_misc.minor);
300 /* Initialise proc semaphores */
301 sema_init(&zfcp_data.config_sema, 1);
303 /* initialise configuration rw lock */
304 rwlock_init(&zfcp_data.config_lock);
306 /* setup dynamic I/O */
307 retval = zfcp_ccw_register();
309 ZFCP_LOG_NORMAL("registration with common I/O layer failed\n");
310 goto out_ccw_register;
313 if (zfcp_device_setup(device))
314 zfcp_init_device_configure();
319 misc_deregister(&zfcp_cfdc_misc);
321 fc_release_transport(zfcp_data.scsi_transport_template);
323 kmem_cache_destroy(zfcp_data.gid_pn_cache);
325 kmem_cache_destroy(zfcp_data.sr_buffer_cache);
327 kmem_cache_destroy(zfcp_data.fsf_req_qtcb_cache);
333 * function: zfcp_cfdc_dev_ioctl
335 * purpose: Handle control file upload/download transaction via IOCTL
338 * returns: 0 - Operation completed successfuly
339 * -ENOTTY - Unknown IOCTL command
340 * -EINVAL - Invalid sense data record
341 * -ENXIO - The FCP adapter is not available
342 * -EOPNOTSUPP - The FCP adapter does not have CFDC support
343 * -ENOMEM - Insufficient memory
344 * -EFAULT - User space memory I/O operation fault
345 * -EPERM - Cannot create or queue FSF request or create SBALs
346 * -ERESTARTSYS- Received signal (is mapped to EAGAIN by VFS)
349 zfcp_cfdc_dev_ioctl(struct file *file, unsigned int command,
350 unsigned long buffer)
352 struct zfcp_cfdc_sense_data *sense_data, __user *sense_data_user;
353 struct zfcp_adapter *adapter = NULL;
354 struct zfcp_fsf_req *fsf_req = NULL;
355 struct zfcp_sg_list *sg_list = NULL;
356 u32 fsf_command, option;
360 sense_data = kmalloc(sizeof(struct zfcp_cfdc_sense_data), GFP_KERNEL);
361 if (sense_data == NULL) {
366 sg_list = kzalloc(sizeof(struct zfcp_sg_list), GFP_KERNEL);
367 if (sg_list == NULL) {
372 if (command != ZFCP_CFDC_IOC) {
373 ZFCP_LOG_INFO("IOC request code 0x%x invalid\n", command);
378 if ((sense_data_user = (void __user *) buffer) == NULL) {
379 ZFCP_LOG_INFO("sense data record is required\n");
384 retval = copy_from_user(sense_data, sense_data_user,
385 sizeof(struct zfcp_cfdc_sense_data));
391 if (sense_data->signature != ZFCP_CFDC_SIGNATURE) {
392 ZFCP_LOG_INFO("invalid sense data request signature 0x%08x\n",
393 ZFCP_CFDC_SIGNATURE);
398 switch (sense_data->command) {
400 case ZFCP_CFDC_CMND_DOWNLOAD_NORMAL:
401 fsf_command = FSF_QTCB_DOWNLOAD_CONTROL_FILE;
402 option = FSF_CFDC_OPTION_NORMAL_MODE;
405 case ZFCP_CFDC_CMND_DOWNLOAD_FORCE:
406 fsf_command = FSF_QTCB_DOWNLOAD_CONTROL_FILE;
407 option = FSF_CFDC_OPTION_FORCE;
410 case ZFCP_CFDC_CMND_FULL_ACCESS:
411 fsf_command = FSF_QTCB_DOWNLOAD_CONTROL_FILE;
412 option = FSF_CFDC_OPTION_FULL_ACCESS;
415 case ZFCP_CFDC_CMND_RESTRICTED_ACCESS:
416 fsf_command = FSF_QTCB_DOWNLOAD_CONTROL_FILE;
417 option = FSF_CFDC_OPTION_RESTRICTED_ACCESS;
420 case ZFCP_CFDC_CMND_UPLOAD:
421 fsf_command = FSF_QTCB_UPLOAD_CONTROL_FILE;
426 ZFCP_LOG_INFO("invalid command code 0x%08x\n",
427 sense_data->command);
432 bus_id = kmalloc(BUS_ID_SIZE, GFP_KERNEL);
433 if (bus_id == NULL) {
437 snprintf(bus_id, BUS_ID_SIZE, "%d.%d.%04x",
438 (sense_data->devno >> 24),
439 (sense_data->devno >> 16) & 0xFF,
440 (sense_data->devno & 0xFFFF));
442 read_lock_irq(&zfcp_data.config_lock);
443 adapter = zfcp_get_adapter_by_busid(bus_id);
445 zfcp_adapter_get(adapter);
446 read_unlock_irq(&zfcp_data.config_lock);
450 if (adapter == NULL) {
451 ZFCP_LOG_INFO("invalid adapter\n");
456 if (sense_data->command & ZFCP_CFDC_WITH_CONTROL_FILE) {
457 retval = zfcp_sg_list_alloc(sg_list,
458 ZFCP_CFDC_MAX_CONTROL_FILE_SIZE);
465 if ((sense_data->command & ZFCP_CFDC_DOWNLOAD) &&
466 (sense_data->command & ZFCP_CFDC_WITH_CONTROL_FILE)) {
467 retval = zfcp_sg_list_copy_from_user(
468 sg_list, &sense_data_user->control_file,
469 ZFCP_CFDC_MAX_CONTROL_FILE_SIZE);
476 retval = zfcp_fsf_control_file(adapter, &fsf_req, fsf_command,
481 if ((fsf_req->qtcb->prefix.prot_status != FSF_PROT_GOOD) &&
482 (fsf_req->qtcb->prefix.prot_status != FSF_PROT_FSF_STATUS_PRESENTED)) {
487 sense_data->fsf_status = fsf_req->qtcb->header.fsf_status;
488 memcpy(&sense_data->fsf_status_qual,
489 &fsf_req->qtcb->header.fsf_status_qual,
490 sizeof(union fsf_status_qual));
491 memcpy(&sense_data->payloads, &fsf_req->qtcb->bottom.support.els, 256);
493 retval = copy_to_user(sense_data_user, sense_data,
494 sizeof(struct zfcp_cfdc_sense_data));
500 if (sense_data->command & ZFCP_CFDC_UPLOAD) {
501 retval = zfcp_sg_list_copy_to_user(
502 &sense_data_user->control_file, sg_list,
503 ZFCP_CFDC_MAX_CONTROL_FILE_SIZE);
512 zfcp_fsf_req_free(fsf_req);
514 if ((adapter != NULL) && (retval != -ENXIO))
515 zfcp_adapter_put(adapter);
517 if (sg_list != NULL) {
518 zfcp_sg_list_free(sg_list);
529 * zfcp_sg_list_alloc - create a scatter-gather list of the specified size
530 * @sg_list: structure describing a scatter gather list
531 * @size: size of scatter-gather list
532 * Return: 0 on success, else -ENOMEM
534 * In sg_list->sg a pointer to the created scatter-gather list is returned,
535 * or NULL if we run out of memory. sg_list->count specifies the number of
536 * elements of the scatter-gather list. The maximum size of a single element
537 * in the scatter-gather list is PAGE_SIZE.
540 zfcp_sg_list_alloc(struct zfcp_sg_list *sg_list, size_t size)
542 struct scatterlist *sg;
547 BUG_ON(sg_list == NULL);
549 sg_list->count = size >> PAGE_SHIFT;
550 if (size & ~PAGE_MASK)
552 sg_list->sg = kcalloc(sg_list->count, sizeof(struct scatterlist),
554 if (sg_list->sg == NULL) {
559 sg_init_table(sg_list->sg, sg_list->count);
561 for (i = 0, sg = sg_list->sg; i < sg_list->count; i++, sg++) {
562 address = (void *) get_zeroed_page(GFP_KERNEL);
563 if (address == NULL) {
565 zfcp_sg_list_free(sg_list);
569 zfcp_address_to_sg(address, sg, min(size, PAGE_SIZE));
579 * zfcp_sg_list_free - free memory of a scatter-gather list
580 * @sg_list: structure describing a scatter-gather list
582 * Memory for each element in the scatter-gather list is freed.
583 * Finally sg_list->sg is freed itself and sg_list->count is reset.
586 zfcp_sg_list_free(struct zfcp_sg_list *sg_list)
588 struct scatterlist *sg;
591 BUG_ON(sg_list == NULL);
593 for (i = 0, sg = sg_list->sg; i < sg_list->count; i++, sg++)
594 free_page((unsigned long) zfcp_sg_to_address(sg));
601 * zfcp_sg_size - determine size of a scatter-gather list
602 * @sg: array of (struct scatterlist)
603 * @sg_count: elements in array
604 * Return: size of entire scatter-gather list
606 static size_t zfcp_sg_size(struct scatterlist *sg, unsigned int sg_count)
609 struct scatterlist *p;
613 for (i = 0, p = sg; i < sg_count; i++, p++) {
623 * zfcp_sg_list_copy_from_user -copy data from user space to scatter-gather list
624 * @sg_list: structure describing a scatter-gather list
625 * @user_buffer: pointer to buffer in user space
626 * @size: number of bytes to be copied
627 * Return: 0 on success, -EFAULT if copy_from_user fails.
630 zfcp_sg_list_copy_from_user(struct zfcp_sg_list *sg_list,
631 void __user *user_buffer,
634 struct scatterlist *sg;
639 BUG_ON(sg_list == NULL);
641 if (zfcp_sg_size(sg_list->sg, sg_list->count) < size)
644 for (sg = sg_list->sg; size > 0; sg++) {
645 length = min((unsigned int)size, sg->length);
646 zfcp_buffer = zfcp_sg_to_address(sg);
647 if (copy_from_user(zfcp_buffer, user_buffer, length)) {
651 user_buffer += length;
661 * zfcp_sg_list_copy_to_user - copy data from scatter-gather list to user space
662 * @user_buffer: pointer to buffer in user space
663 * @sg_list: structure describing a scatter-gather list
664 * @size: number of bytes to be copied
665 * Return: 0 on success, -EFAULT if copy_to_user fails
668 zfcp_sg_list_copy_to_user(void __user *user_buffer,
669 struct zfcp_sg_list *sg_list,
672 struct scatterlist *sg;
677 BUG_ON(sg_list == NULL);
679 if (zfcp_sg_size(sg_list->sg, sg_list->count) < size)
682 for (sg = sg_list->sg; size > 0; sg++) {
683 length = min((unsigned int) size, sg->length);
684 zfcp_buffer = zfcp_sg_to_address(sg);
685 if (copy_to_user(user_buffer, zfcp_buffer, length)) {
689 user_buffer += length;
700 /****************************************************************/
701 /****** Functions for configuration/set-up of structures ********/
702 /****************************************************************/
704 #define ZFCP_LOG_AREA ZFCP_LOG_AREA_CONFIG
707 * zfcp_get_unit_by_lun - find unit in unit list of port by FCP LUN
708 * @port: pointer to port to search for unit
709 * @fcp_lun: FCP LUN to search for
710 * Traverse list of all units of a port and return pointer to a unit
711 * with the given FCP LUN.
714 zfcp_get_unit_by_lun(struct zfcp_port *port, fcp_lun_t fcp_lun)
716 struct zfcp_unit *unit;
719 list_for_each_entry(unit, &port->unit_list_head, list) {
720 if ((unit->fcp_lun == fcp_lun) &&
721 !atomic_test_mask(ZFCP_STATUS_COMMON_REMOVE, &unit->status))
727 return found ? unit : NULL;
731 * zfcp_get_port_by_wwpn - find port in port list of adapter by wwpn
732 * @adapter: pointer to adapter to search for port
733 * @wwpn: wwpn to search for
734 * Traverse list of all ports of an adapter and return pointer to a port
735 * with the given wwpn.
738 zfcp_get_port_by_wwpn(struct zfcp_adapter *adapter, wwn_t wwpn)
740 struct zfcp_port *port;
743 list_for_each_entry(port, &adapter->port_list_head, list) {
744 if ((port->wwpn == wwpn) &&
745 !(atomic_read(&port->status) &
746 (ZFCP_STATUS_PORT_NO_WWPN | ZFCP_STATUS_COMMON_REMOVE))) {
751 return found ? port : NULL;
755 * zfcp_get_port_by_did - find port in port list of adapter by d_id
756 * @adapter: pointer to adapter to search for port
757 * @d_id: d_id to search for
758 * Traverse list of all ports of an adapter and return pointer to a port
759 * with the given d_id.
762 zfcp_get_port_by_did(struct zfcp_adapter *adapter, u32 d_id)
764 struct zfcp_port *port;
767 list_for_each_entry(port, &adapter->port_list_head, list) {
768 if ((port->d_id == d_id) &&
769 !atomic_test_mask(ZFCP_STATUS_COMMON_REMOVE, &port->status))
775 return found ? port : NULL;
779 * zfcp_get_adapter_by_busid - find adpater in adapter list by bus_id
780 * @bus_id: bus_id to search for
781 * Traverse list of all adapters and return pointer to an adapter
782 * with the given bus_id.
784 struct zfcp_adapter *
785 zfcp_get_adapter_by_busid(char *bus_id)
787 struct zfcp_adapter *adapter;
790 list_for_each_entry(adapter, &zfcp_data.adapter_list_head, list) {
791 if ((strncmp(bus_id, zfcp_get_busid_by_adapter(adapter),
792 BUS_ID_SIZE) == 0) &&
793 !atomic_test_mask(ZFCP_STATUS_COMMON_REMOVE,
799 return found ? adapter : NULL;
803 * zfcp_unit_enqueue - enqueue unit to unit list of a port.
804 * @port: pointer to port where unit is added
805 * @fcp_lun: FCP LUN of unit to be enqueued
806 * Return: pointer to enqueued unit on success, NULL on error
807 * Locks: config_sema must be held to serialize changes to the unit list
809 * Sets up some unit internal structures and creates sysfs entry.
812 zfcp_unit_enqueue(struct zfcp_port *port, fcp_lun_t fcp_lun)
814 struct zfcp_unit *unit;
817 * check that there is no unit with this FCP_LUN already in list
819 * Note: Unlike for the adapter and the port, this is an error
821 read_lock_irq(&zfcp_data.config_lock);
822 unit = zfcp_get_unit_by_lun(port, fcp_lun);
823 read_unlock_irq(&zfcp_data.config_lock);
827 unit = kzalloc(sizeof (struct zfcp_unit), GFP_KERNEL);
831 /* initialise reference count stuff */
832 atomic_set(&unit->refcount, 0);
833 init_waitqueue_head(&unit->remove_wq);
836 unit->fcp_lun = fcp_lun;
838 /* setup for sysfs registration */
839 snprintf(unit->sysfs_device.bus_id, BUS_ID_SIZE, "0x%016llx", fcp_lun);
840 unit->sysfs_device.parent = &port->sysfs_device;
841 unit->sysfs_device.release = zfcp_sysfs_unit_release;
842 dev_set_drvdata(&unit->sysfs_device, unit);
844 /* mark unit unusable as long as sysfs registration is not complete */
845 atomic_set_mask(ZFCP_STATUS_COMMON_REMOVE, &unit->status);
847 spin_lock_init(&unit->latencies.lock);
848 unit->latencies.write.channel.min = 0xFFFFFFFF;
849 unit->latencies.write.fabric.min = 0xFFFFFFFF;
850 unit->latencies.read.channel.min = 0xFFFFFFFF;
851 unit->latencies.read.fabric.min = 0xFFFFFFFF;
852 unit->latencies.cmd.channel.min = 0xFFFFFFFF;
853 unit->latencies.cmd.fabric.min = 0xFFFFFFFF;
855 if (device_register(&unit->sysfs_device)) {
860 if (zfcp_sysfs_unit_create_files(&unit->sysfs_device)) {
861 device_unregister(&unit->sysfs_device);
866 unit->scsi_lun = scsilun_to_int((struct scsi_lun *)&unit->fcp_lun);
868 write_lock_irq(&zfcp_data.config_lock);
869 list_add_tail(&unit->list, &port->unit_list_head);
870 atomic_clear_mask(ZFCP_STATUS_COMMON_REMOVE, &unit->status);
871 atomic_set_mask(ZFCP_STATUS_COMMON_RUNNING, &unit->status);
872 write_unlock_irq(&zfcp_data.config_lock);
881 zfcp_unit_dequeue(struct zfcp_unit *unit)
883 zfcp_unit_wait(unit);
884 write_lock_irq(&zfcp_data.config_lock);
885 list_del(&unit->list);
886 write_unlock_irq(&zfcp_data.config_lock);
888 zfcp_port_put(unit->port);
889 zfcp_sysfs_unit_remove_files(&unit->sysfs_device);
890 device_unregister(&unit->sysfs_device);
894 * Allocates a combined QTCB/fsf_req buffer for erp actions and fcp/SCSI
896 * It also genrates fcp-nameserver request/response buffer and unsolicited
897 * status read fsf_req buffers.
899 * locks: must only be called with zfcp_data.config_sema taken
902 zfcp_allocate_low_mem_buffers(struct zfcp_adapter *adapter)
904 adapter->pool.fsf_req_erp =
905 mempool_create_slab_pool(ZFCP_POOL_FSF_REQ_ERP_NR,
906 zfcp_data.fsf_req_qtcb_cache);
907 if (!adapter->pool.fsf_req_erp)
910 adapter->pool.fsf_req_scsi =
911 mempool_create_slab_pool(ZFCP_POOL_FSF_REQ_SCSI_NR,
912 zfcp_data.fsf_req_qtcb_cache);
913 if (!adapter->pool.fsf_req_scsi)
916 adapter->pool.fsf_req_abort =
917 mempool_create_slab_pool(ZFCP_POOL_FSF_REQ_ABORT_NR,
918 zfcp_data.fsf_req_qtcb_cache);
919 if (!adapter->pool.fsf_req_abort)
922 adapter->pool.fsf_req_status_read =
923 mempool_create_kmalloc_pool(ZFCP_POOL_STATUS_READ_NR,
924 sizeof(struct zfcp_fsf_req));
925 if (!adapter->pool.fsf_req_status_read)
928 adapter->pool.data_status_read =
929 mempool_create_slab_pool(ZFCP_POOL_STATUS_READ_NR,
930 zfcp_data.sr_buffer_cache);
931 if (!adapter->pool.data_status_read)
934 adapter->pool.data_gid_pn =
935 mempool_create_slab_pool(ZFCP_POOL_DATA_GID_PN_NR,
936 zfcp_data.gid_pn_cache);
937 if (!adapter->pool.data_gid_pn)
944 * zfcp_free_low_mem_buffers - free memory pools of an adapter
945 * @adapter: pointer to zfcp_adapter for which memory pools should be freed
946 * locking: zfcp_data.config_sema must be held
949 zfcp_free_low_mem_buffers(struct zfcp_adapter *adapter)
951 if (adapter->pool.fsf_req_erp)
952 mempool_destroy(adapter->pool.fsf_req_erp);
953 if (adapter->pool.fsf_req_scsi)
954 mempool_destroy(adapter->pool.fsf_req_scsi);
955 if (adapter->pool.fsf_req_abort)
956 mempool_destroy(adapter->pool.fsf_req_abort);
957 if (adapter->pool.fsf_req_status_read)
958 mempool_destroy(adapter->pool.fsf_req_status_read);
959 if (adapter->pool.data_status_read)
960 mempool_destroy(adapter->pool.data_status_read);
961 if (adapter->pool.data_gid_pn)
962 mempool_destroy(adapter->pool.data_gid_pn);
965 static void zfcp_dummy_release(struct device *dev)
970 int zfcp_status_read_refill(struct zfcp_adapter *adapter)
972 while (atomic_read(&adapter->stat_miss) > 0)
973 if (zfcp_fsf_status_read(adapter, ZFCP_WAIT_FOR_SBAL))
976 atomic_dec(&adapter->stat_miss);
978 if (ZFCP_STATUS_READS_RECOM <= atomic_read(&adapter->stat_miss)) {
979 zfcp_erp_adapter_reopen(adapter, 0, 103, NULL);
985 static void _zfcp_status_read_scheduler(struct work_struct *work)
987 zfcp_status_read_refill(container_of(work, struct zfcp_adapter,
992 * Enqueues an adapter at the end of the adapter list in the driver data.
993 * All adapter internal structures are set up.
994 * Proc-fs entries are also created.
996 * returns: 0 if a new adapter was successfully enqueued
997 * ZFCP_KNOWN if an adapter with this devno was already present
998 * -ENOMEM if alloc failed
999 * locks: config_sema must be held to serialise changes to the adapter list
1001 struct zfcp_adapter *
1002 zfcp_adapter_enqueue(struct ccw_device *ccw_device)
1005 struct zfcp_adapter *adapter;
1008 * Note: It is safe to release the list_lock, as any list changes
1009 * are protected by the config_sema, which must be held to get here
1012 /* try to allocate new adapter data structure (zeroed) */
1013 adapter = kzalloc(sizeof (struct zfcp_adapter), GFP_KERNEL);
1015 ZFCP_LOG_INFO("error: allocation of base adapter "
1016 "structure failed\n");
1020 ccw_device->handler = NULL;
1022 /* save ccw_device pointer */
1023 adapter->ccw_device = ccw_device;
1025 retval = zfcp_qdio_allocate_queues(adapter);
1027 goto queues_alloc_failed;
1029 retval = zfcp_qdio_allocate(adapter);
1031 goto qdio_allocate_failed;
1033 retval = zfcp_allocate_low_mem_buffers(adapter);
1035 ZFCP_LOG_INFO("error: pool allocation failed\n");
1036 goto failed_low_mem_buffers;
1039 /* initialise reference count stuff */
1040 atomic_set(&adapter->refcount, 0);
1041 init_waitqueue_head(&adapter->remove_wq);
1043 /* initialise list of ports */
1044 INIT_LIST_HEAD(&adapter->port_list_head);
1046 /* initialise list of ports to be removed */
1047 INIT_LIST_HEAD(&adapter->port_remove_lh);
1049 /* initialize list of fsf requests */
1050 spin_lock_init(&adapter->req_list_lock);
1051 retval = zfcp_reqlist_alloc(adapter);
1053 ZFCP_LOG_INFO("request list initialization failed\n");
1054 goto failed_low_mem_buffers;
1057 /* initialize debug locks */
1059 spin_lock_init(&adapter->hba_dbf_lock);
1060 spin_lock_init(&adapter->san_dbf_lock);
1061 spin_lock_init(&adapter->scsi_dbf_lock);
1062 spin_lock_init(&adapter->rec_dbf_lock);
1064 retval = zfcp_adapter_debug_register(adapter);
1066 goto debug_register_failed;
1068 /* initialize error recovery stuff */
1070 rwlock_init(&adapter->erp_lock);
1071 sema_init(&adapter->erp_ready_sem, 0);
1072 INIT_LIST_HEAD(&adapter->erp_ready_head);
1073 INIT_LIST_HEAD(&adapter->erp_running_head);
1075 /* initialize abort lock */
1076 rwlock_init(&adapter->abort_lock);
1078 /* initialise some erp stuff */
1079 init_waitqueue_head(&adapter->erp_thread_wqh);
1080 init_waitqueue_head(&adapter->erp_done_wqh);
1082 /* initialize lock of associated request queue */
1083 rwlock_init(&adapter->request_queue.queue_lock);
1084 INIT_WORK(&adapter->stat_work, _zfcp_status_read_scheduler);
1086 /* mark adapter unusable as long as sysfs registration is not complete */
1087 atomic_set_mask(ZFCP_STATUS_COMMON_REMOVE, &adapter->status);
1089 dev_set_drvdata(&ccw_device->dev, adapter);
1091 if (zfcp_sysfs_adapter_create_files(&ccw_device->dev))
1094 adapter->generic_services.parent = &adapter->ccw_device->dev;
1095 adapter->generic_services.release = zfcp_dummy_release;
1096 snprintf(adapter->generic_services.bus_id, BUS_ID_SIZE,
1097 "generic_services");
1099 if (device_register(&adapter->generic_services))
1100 goto generic_services_failed;
1102 /* put allocated adapter at list tail */
1103 write_lock_irq(&zfcp_data.config_lock);
1104 atomic_clear_mask(ZFCP_STATUS_COMMON_REMOVE, &adapter->status);
1105 list_add_tail(&adapter->list, &zfcp_data.adapter_list_head);
1106 write_unlock_irq(&zfcp_data.config_lock);
1108 zfcp_data.adapters++;
1112 generic_services_failed:
1113 zfcp_sysfs_adapter_remove_files(&adapter->ccw_device->dev);
1115 zfcp_adapter_debug_unregister(adapter);
1116 debug_register_failed:
1117 dev_set_drvdata(&ccw_device->dev, NULL);
1118 zfcp_reqlist_free(adapter);
1119 failed_low_mem_buffers:
1120 zfcp_free_low_mem_buffers(adapter);
1121 if (qdio_free(ccw_device) != 0)
1122 ZFCP_LOG_NORMAL("bug: qdio_free for adapter %s failed\n",
1123 zfcp_get_busid_by_adapter(adapter));
1124 qdio_allocate_failed:
1125 zfcp_qdio_free_queues(adapter);
1126 queues_alloc_failed:
1134 * returns: 0 - struct zfcp_adapter data structure successfully removed
1135 * !0 - struct zfcp_adapter data structure could not be removed
1137 * locks: adapter list write lock is assumed to be held by caller
1140 zfcp_adapter_dequeue(struct zfcp_adapter *adapter)
1143 unsigned long flags;
1145 cancel_work_sync(&adapter->stat_work);
1146 zfcp_adapter_scsi_unregister(adapter);
1147 device_unregister(&adapter->generic_services);
1148 zfcp_sysfs_adapter_remove_files(&adapter->ccw_device->dev);
1149 dev_set_drvdata(&adapter->ccw_device->dev, NULL);
1150 /* sanity check: no pending FSF requests */
1151 spin_lock_irqsave(&adapter->req_list_lock, flags);
1152 retval = zfcp_reqlist_isempty(adapter);
1153 spin_unlock_irqrestore(&adapter->req_list_lock, flags);
1155 ZFCP_LOG_NORMAL("bug: adapter %s (%p) still in use, "
1156 "%i requests outstanding\n",
1157 zfcp_get_busid_by_adapter(adapter), adapter,
1158 atomic_read(&adapter->reqs_active));
1163 zfcp_adapter_debug_unregister(adapter);
1165 /* remove specified adapter data structure from list */
1166 write_lock_irq(&zfcp_data.config_lock);
1167 list_del(&adapter->list);
1168 write_unlock_irq(&zfcp_data.config_lock);
1170 /* decrease number of adapters in list */
1171 zfcp_data.adapters--;
1173 ZFCP_LOG_TRACE("adapter %s (%p) removed from list, "
1174 "%i adapters still in list\n",
1175 zfcp_get_busid_by_adapter(adapter),
1176 adapter, zfcp_data.adapters);
1178 retval = qdio_free(adapter->ccw_device);
1180 ZFCP_LOG_NORMAL("bug: qdio_free for adapter %s failed\n",
1181 zfcp_get_busid_by_adapter(adapter));
1183 zfcp_free_low_mem_buffers(adapter);
1184 /* free memory of adapter data structure and queues */
1185 zfcp_qdio_free_queues(adapter);
1186 zfcp_reqlist_free(adapter);
1187 kfree(adapter->fc_stats);
1188 kfree(adapter->stats_reset_data);
1189 ZFCP_LOG_TRACE("freeing adapter structure\n");
1196 * zfcp_port_enqueue - enqueue port to port list of adapter
1197 * @adapter: adapter where remote port is added
1198 * @wwpn: WWPN of the remote port to be enqueued
1199 * @status: initial status for the port
1200 * @d_id: destination id of the remote port to be enqueued
1201 * Return: pointer to enqueued port on success, NULL on error
1202 * Locks: config_sema must be held to serialize changes to the port list
1204 * All port internal structures are set up and the sysfs entry is generated.
1205 * d_id is used to enqueue ports with a well known address like the Directory
1206 * Service for nameserver lookup.
1209 zfcp_port_enqueue(struct zfcp_adapter *adapter, wwn_t wwpn, u32 status,
1212 struct zfcp_port *port;
1215 check_wwpn = !(status & ZFCP_STATUS_PORT_NO_WWPN);
1217 * check that there is no port with this WWPN already in list
1220 read_lock_irq(&zfcp_data.config_lock);
1221 port = zfcp_get_port_by_wwpn(adapter, wwpn);
1222 read_unlock_irq(&zfcp_data.config_lock);
1227 port = kzalloc(sizeof (struct zfcp_port), GFP_KERNEL);
1231 /* initialise reference count stuff */
1232 atomic_set(&port->refcount, 0);
1233 init_waitqueue_head(&port->remove_wq);
1235 INIT_LIST_HEAD(&port->unit_list_head);
1236 INIT_LIST_HEAD(&port->unit_remove_lh);
1238 port->adapter = adapter;
1243 atomic_set_mask(status, &port->status);
1245 /* setup for sysfs registration */
1246 if (status & ZFCP_STATUS_PORT_WKA) {
1248 case ZFCP_DID_DIRECTORY_SERVICE:
1249 snprintf(port->sysfs_device.bus_id, BUS_ID_SIZE,
1252 case ZFCP_DID_MANAGEMENT_SERVICE:
1253 snprintf(port->sysfs_device.bus_id, BUS_ID_SIZE,
1256 case ZFCP_DID_KEY_DISTRIBUTION_SERVICE:
1257 snprintf(port->sysfs_device.bus_id, BUS_ID_SIZE,
1258 "key_distribution");
1260 case ZFCP_DID_ALIAS_SERVICE:
1261 snprintf(port->sysfs_device.bus_id, BUS_ID_SIZE,
1264 case ZFCP_DID_TIME_SERVICE:
1265 snprintf(port->sysfs_device.bus_id, BUS_ID_SIZE,
1273 port->sysfs_device.parent = &adapter->generic_services;
1275 snprintf(port->sysfs_device.bus_id,
1276 BUS_ID_SIZE, "0x%016llx", wwpn);
1277 port->sysfs_device.parent = &adapter->ccw_device->dev;
1279 port->sysfs_device.release = zfcp_sysfs_port_release;
1280 dev_set_drvdata(&port->sysfs_device, port);
1282 /* mark port unusable as long as sysfs registration is not complete */
1283 atomic_set_mask(ZFCP_STATUS_COMMON_REMOVE, &port->status);
1285 if (device_register(&port->sysfs_device)) {
1290 if (zfcp_sysfs_port_create_files(&port->sysfs_device, status)) {
1291 device_unregister(&port->sysfs_device);
1295 zfcp_port_get(port);
1297 write_lock_irq(&zfcp_data.config_lock);
1298 list_add_tail(&port->list, &adapter->port_list_head);
1299 atomic_clear_mask(ZFCP_STATUS_COMMON_REMOVE, &port->status);
1300 atomic_set_mask(ZFCP_STATUS_COMMON_RUNNING, &port->status);
1301 if (d_id == ZFCP_DID_DIRECTORY_SERVICE)
1302 if (!adapter->nameserver_port)
1303 adapter->nameserver_port = port;
1305 write_unlock_irq(&zfcp_data.config_lock);
1307 zfcp_adapter_get(adapter);
1313 zfcp_port_dequeue(struct zfcp_port *port)
1315 zfcp_port_wait(port);
1316 write_lock_irq(&zfcp_data.config_lock);
1317 list_del(&port->list);
1318 port->adapter->ports--;
1319 write_unlock_irq(&zfcp_data.config_lock);
1321 fc_remote_port_delete(port->rport);
1323 zfcp_adapter_put(port->adapter);
1324 zfcp_sysfs_port_remove_files(&port->sysfs_device,
1325 atomic_read(&port->status));
1326 device_unregister(&port->sysfs_device);
1329 /* Enqueues a nameserver port */
1331 zfcp_nameserver_enqueue(struct zfcp_adapter *adapter)
1333 struct zfcp_port *port;
1335 port = zfcp_port_enqueue(adapter, 0, ZFCP_STATUS_PORT_WKA,
1336 ZFCP_DID_DIRECTORY_SERVICE);
1338 ZFCP_LOG_INFO("error: enqueue of nameserver port for "
1339 "adapter %s failed\n",
1340 zfcp_get_busid_by_adapter(adapter));
1343 zfcp_port_put(port);
1348 #undef ZFCP_LOG_AREA