3 * linux/drivers/s390/scsi/zfcp_aux.c
5 * FCP adapter driver for IBM eServer zSeries
7 * (C) Copyright IBM Corp. 2002, 2004
9 * Author(s): Martin Peschke <mpeschke@de.ibm.com>
10 * Raimund Schroeder <raimund.schroeder@de.ibm.com>
13 * Stefan Bader <stefan.bader@de.ibm.com>
14 * Heiko Carstens <heiko.carstens@de.ibm.com>
15 * Andreas Herrmann <aherrman@de.ibm.com>
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; either version 2, or (at your option)
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
27 * You should have received a copy of the GNU General Public License
28 * along with this program; if not, write to the Free Software
29 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
32 #define ZFCP_AUX_REVISION "$Revision: 1.145 $"
36 /* accumulated log level (module parameter) */
37 static u32 loglevel = ZFCP_LOG_LEVEL_DEFAULTS;
39 /*********************** FUNCTION PROTOTYPES *********************************/
41 /* written against the module interface */
42 static int __init zfcp_module_init(void);
45 static void zfcp_ns_gid_pn_handler(unsigned long);
48 static inline int zfcp_sg_list_alloc(struct zfcp_sg_list *, size_t);
49 static inline void zfcp_sg_list_free(struct zfcp_sg_list *);
50 static inline int zfcp_sg_list_copy_from_user(struct zfcp_sg_list *,
51 void __user *, size_t);
52 static inline int zfcp_sg_list_copy_to_user(void __user *,
53 struct zfcp_sg_list *, size_t);
55 static int zfcp_cfdc_dev_ioctl(struct inode *, struct file *,
56 unsigned int, unsigned long);
58 #define ZFCP_CFDC_IOC_MAGIC 0xDD
59 #define ZFCP_CFDC_IOC \
60 _IOWR(ZFCP_CFDC_IOC_MAGIC, 0, struct zfcp_cfdc_sense_data)
63 static struct ioctl_trans zfcp_ioctl_trans = {ZFCP_CFDC_IOC, (void*) sys_ioctl};
66 static struct file_operations zfcp_cfdc_fops = {
67 .ioctl = zfcp_cfdc_dev_ioctl
70 static struct miscdevice zfcp_cfdc_misc = {
71 .minor = ZFCP_CFDC_DEV_MINOR,
72 .name = ZFCP_CFDC_DEV_NAME,
73 .fops = &zfcp_cfdc_fops
76 /*********************** KERNEL/MODULE PARAMETERS ***************************/
78 /* declare driver module init/cleanup functions */
79 module_init(zfcp_module_init);
81 MODULE_AUTHOR("Heiko Carstens <heiko.carstens@de.ibm.com>, "
82 "Andreas Herrman <aherrman@de.ibm.com>, "
83 "Martin Peschke <mpeschke@de.ibm.com>, "
84 "Raimund Schroeder <raimund.schroeder@de.ibm.com>, "
85 "Wolfgang Taphorn <taphorn@de.ibm.com>, "
86 "Aron Zeh <arzeh@de.ibm.com>, "
87 "IBM Deutschland Entwicklung GmbH");
89 ("FCP (SCSI over Fibre Channel) HBA driver for IBM eServer zSeries");
90 MODULE_LICENSE("GPL");
92 module_param(device, charp, 0400);
93 MODULE_PARM_DESC(device, "specify initial device");
95 module_param(loglevel, uint, 0400);
96 MODULE_PARM_DESC(loglevel,
97 "log levels, 8 nibbles: "
98 "FC ERP QDIO CIO Config FSF SCSI Other, "
99 "levels: 0=none 1=normal 2=devel 3=trace");
101 #ifdef ZFCP_PRINT_FLAGS
103 module_param(flags_dump, uint, 0);
106 /****************************************************************/
107 /************** Functions without logging ***********************/
108 /****************************************************************/
111 _zfcp_hex_dump(char *addr, int count)
114 for (i = 0; i < count; i++) {
115 printk("%02x", addr[i]);
121 if (((i-1) % 32) != 31)
125 /****************************************************************/
126 /************** Uncategorised Functions *************************/
127 /****************************************************************/
129 #define ZFCP_LOG_AREA ZFCP_LOG_AREA_OTHER
132 zfcp_fsf_req_is_scsi_cmnd(struct zfcp_fsf_req *fsf_req)
134 return ((fsf_req->fsf_command == FSF_QTCB_FCP_CMND) &&
135 !(fsf_req->status & ZFCP_STATUS_FSFREQ_TASK_MANAGEMENT));
139 zfcp_cmd_dbf_event_fsf(const char *text, struct zfcp_fsf_req *fsf_req,
140 void *add_data, int add_length)
142 struct zfcp_adapter *adapter = fsf_req->adapter;
143 struct scsi_cmnd *scsi_cmnd;
148 spin_lock_irqsave(&adapter->dbf_lock, flags);
149 if (zfcp_fsf_req_is_scsi_cmnd(fsf_req)) {
150 scsi_cmnd = fsf_req->data.send_fcp_command_task.scsi_cmnd;
151 debug_text_event(adapter->cmd_dbf, level, "fsferror");
152 debug_text_event(adapter->cmd_dbf, level, text);
153 debug_event(adapter->cmd_dbf, level, &fsf_req,
154 sizeof (unsigned long));
155 debug_event(adapter->cmd_dbf, level, &fsf_req->seq_no,
157 debug_event(adapter->cmd_dbf, level, &scsi_cmnd,
158 sizeof (unsigned long));
159 debug_event(adapter->cmd_dbf, level, &scsi_cmnd->cmnd,
160 min(ZFCP_CMD_DBF_LENGTH, (int)scsi_cmnd->cmd_len));
161 for (i = 0; i < add_length; i += ZFCP_CMD_DBF_LENGTH)
162 debug_event(adapter->cmd_dbf,
164 (char *) add_data + i,
165 min(ZFCP_CMD_DBF_LENGTH, add_length - i));
167 spin_unlock_irqrestore(&adapter->dbf_lock, flags);
170 /* XXX additionally log unit if available */
171 /* ---> introduce new parameter for unit, see 2.4 code */
173 zfcp_cmd_dbf_event_scsi(const char *text, struct scsi_cmnd *scsi_cmnd)
175 struct zfcp_adapter *adapter;
176 union zfcp_req_data *req_data;
177 struct zfcp_fsf_req *fsf_req;
178 int level = ((host_byte(scsi_cmnd->result) != 0) ? 1 : 5);
181 adapter = (struct zfcp_adapter *) scsi_cmnd->device->host->hostdata[0];
182 req_data = (union zfcp_req_data *) scsi_cmnd->host_scribble;
183 fsf_req = (req_data ? req_data->send_fcp_command_task.fsf_req : NULL);
184 spin_lock_irqsave(&adapter->dbf_lock, flags);
185 debug_text_event(adapter->cmd_dbf, level, "hostbyte");
186 debug_text_event(adapter->cmd_dbf, level, text);
187 debug_event(adapter->cmd_dbf, level, &scsi_cmnd->result, sizeof (u32));
188 debug_event(adapter->cmd_dbf, level, &scsi_cmnd,
189 sizeof (unsigned long));
190 debug_event(adapter->cmd_dbf, level, &scsi_cmnd->cmnd,
191 min(ZFCP_CMD_DBF_LENGTH, (int)scsi_cmnd->cmd_len));
192 if (likely(fsf_req)) {
193 debug_event(adapter->cmd_dbf, level, &fsf_req,
194 sizeof (unsigned long));
195 debug_event(adapter->cmd_dbf, level, &fsf_req->seq_no,
198 debug_text_event(adapter->cmd_dbf, level, "");
199 debug_text_event(adapter->cmd_dbf, level, "");
201 spin_unlock_irqrestore(&adapter->dbf_lock, flags);
205 zfcp_in_els_dbf_event(struct zfcp_adapter *adapter, const char *text,
206 struct fsf_status_read_buffer *status_buffer, int length)
211 debug_text_event(adapter->in_els_dbf, level, text);
212 debug_event(adapter->in_els_dbf, level, &status_buffer->d_id, 8);
213 for (i = 0; i < length; i += ZFCP_IN_ELS_DBF_LENGTH)
214 debug_event(adapter->in_els_dbf,
216 (char *) status_buffer->payload + i,
217 min(ZFCP_IN_ELS_DBF_LENGTH, length - i));
221 * zfcp_device_setup - setup function
222 * @str: pointer to parameter string
224 * Parse "device=..." parameter string.
227 zfcp_device_setup(char *str)
234 tmp = strchr(str, ',');
238 strncpy(zfcp_data.init_busid, str, BUS_ID_SIZE);
239 zfcp_data.init_busid[BUS_ID_SIZE-1] = '\0';
241 zfcp_data.init_wwpn = simple_strtoull(tmp, &tmp, 0);
247 zfcp_data.init_fcp_lun = simple_strtoull(tmp, &tmp, 0);
253 ZFCP_LOG_NORMAL("Parse error for device parameter string %s\n", str);
258 zfcp_init_device_configure(void)
260 struct zfcp_adapter *adapter;
261 struct zfcp_port *port;
262 struct zfcp_unit *unit;
264 down(&zfcp_data.config_sema);
265 read_lock_irq(&zfcp_data.config_lock);
266 adapter = zfcp_get_adapter_by_busid(zfcp_data.init_busid);
268 zfcp_adapter_get(adapter);
269 read_unlock_irq(&zfcp_data.config_lock);
273 port = zfcp_port_enqueue(adapter, zfcp_data.init_wwpn, 0, 0);
276 unit = zfcp_unit_enqueue(port, zfcp_data.init_fcp_lun);
279 up(&zfcp_data.config_sema);
280 ccw_device_set_online(adapter->ccw_device);
281 zfcp_erp_wait(adapter);
282 down(&zfcp_data.config_sema);
287 zfcp_adapter_put(adapter);
289 up(&zfcp_data.config_sema);
294 zfcp_module_init(void)
299 atomic_set(&zfcp_data.loglevel, loglevel);
301 /* initialize adapter list */
302 INIT_LIST_HEAD(&zfcp_data.adapter_list_head);
304 /* initialize adapters to be removed list head */
305 INIT_LIST_HEAD(&zfcp_data.adapter_remove_lh);
307 zfcp_transport_template = fc_attach_transport(&zfcp_transport_functions);
308 if (!zfcp_transport_template)
311 retval = register_ioctl32_conversion(zfcp_ioctl_trans.cmd,
312 zfcp_ioctl_trans.handler);
314 ZFCP_LOG_INFO("registration of ioctl32 conversion failed\n");
318 retval = misc_register(&zfcp_cfdc_misc);
320 ZFCP_LOG_INFO("registration of misc device "
321 "zfcp_cfdc failed\n");
322 goto out_misc_register;
324 ZFCP_LOG_TRACE("major/minor for zfcp_cfdc: %d/%d\n",
325 ZFCP_CFDC_DEV_MAJOR, zfcp_cfdc_misc.minor);
328 /* Initialise proc semaphores */
329 sema_init(&zfcp_data.config_sema, 1);
331 /* initialise configuration rw lock */
332 rwlock_init(&zfcp_data.config_lock);
334 /* save address of data structure managing the driver module */
335 zfcp_data.scsi_host_template.module = THIS_MODULE;
337 /* setup dynamic I/O */
338 retval = zfcp_ccw_register();
340 ZFCP_LOG_NORMAL("registration with common I/O layer failed\n");
341 goto out_ccw_register;
344 if (zfcp_device_setup(device))
345 zfcp_init_device_configure();
350 misc_deregister(&zfcp_cfdc_misc);
352 unregister_ioctl32_conversion(zfcp_ioctl_trans.cmd);
358 * function: zfcp_cfdc_dev_ioctl
360 * purpose: Handle control file upload/download transaction via IOCTL
363 * returns: 0 - Operation completed successfuly
364 * -ENOTTY - Unknown IOCTL command
365 * -EINVAL - Invalid sense data record
366 * -ENXIO - The FCP adapter is not available
367 * -EOPNOTSUPP - The FCP adapter does not have CFDC support
368 * -ENOMEM - Insufficient memory
369 * -EFAULT - User space memory I/O operation fault
370 * -EPERM - Cannot create or queue FSF request or create SBALs
371 * -ERESTARTSYS- Received signal (is mapped to EAGAIN by VFS)
374 zfcp_cfdc_dev_ioctl(struct inode *inode, struct file *file,
375 unsigned int command, unsigned long buffer)
377 struct zfcp_cfdc_sense_data *sense_data, __user *sense_data_user;
378 struct zfcp_adapter *adapter = NULL;
379 struct zfcp_fsf_req *fsf_req = NULL;
380 struct zfcp_sg_list *sg_list = NULL;
381 u32 fsf_command, option;
385 sense_data = kmalloc(sizeof(struct zfcp_cfdc_sense_data), GFP_KERNEL);
386 if (sense_data == NULL) {
391 sg_list = kmalloc(sizeof(struct zfcp_sg_list), GFP_KERNEL);
392 if (sg_list == NULL) {
396 memset(sg_list, 0, sizeof(*sg_list));
398 if (command != ZFCP_CFDC_IOC) {
399 ZFCP_LOG_INFO("IOC request code 0x%x invalid\n", command);
404 if ((sense_data_user = (void __user *) buffer) == NULL) {
405 ZFCP_LOG_INFO("sense data record is required\n");
410 retval = copy_from_user(sense_data, sense_data_user,
411 sizeof(struct zfcp_cfdc_sense_data));
417 if (sense_data->signature != ZFCP_CFDC_SIGNATURE) {
418 ZFCP_LOG_INFO("invalid sense data request signature 0x%08x\n",
419 ZFCP_CFDC_SIGNATURE);
424 switch (sense_data->command) {
426 case ZFCP_CFDC_CMND_DOWNLOAD_NORMAL:
427 fsf_command = FSF_QTCB_DOWNLOAD_CONTROL_FILE;
428 option = FSF_CFDC_OPTION_NORMAL_MODE;
431 case ZFCP_CFDC_CMND_DOWNLOAD_FORCE:
432 fsf_command = FSF_QTCB_DOWNLOAD_CONTROL_FILE;
433 option = FSF_CFDC_OPTION_FORCE;
436 case ZFCP_CFDC_CMND_FULL_ACCESS:
437 fsf_command = FSF_QTCB_DOWNLOAD_CONTROL_FILE;
438 option = FSF_CFDC_OPTION_FULL_ACCESS;
441 case ZFCP_CFDC_CMND_RESTRICTED_ACCESS:
442 fsf_command = FSF_QTCB_DOWNLOAD_CONTROL_FILE;
443 option = FSF_CFDC_OPTION_RESTRICTED_ACCESS;
446 case ZFCP_CFDC_CMND_UPLOAD:
447 fsf_command = FSF_QTCB_UPLOAD_CONTROL_FILE;
452 ZFCP_LOG_INFO("invalid command code 0x%08x\n",
453 sense_data->command);
458 bus_id = kmalloc(BUS_ID_SIZE, GFP_KERNEL);
459 if (bus_id == NULL) {
463 snprintf(bus_id, BUS_ID_SIZE, "%d.%d.%04x",
464 (sense_data->devno >> 24),
465 (sense_data->devno >> 16) & 0xFF,
466 (sense_data->devno & 0xFFFF));
468 read_lock_irq(&zfcp_data.config_lock);
469 adapter = zfcp_get_adapter_by_busid(bus_id);
471 zfcp_adapter_get(adapter);
472 read_unlock_irq(&zfcp_data.config_lock);
476 if (adapter == NULL) {
477 ZFCP_LOG_INFO("invalid adapter\n");
482 if (sense_data->command & ZFCP_CFDC_WITH_CONTROL_FILE) {
483 retval = zfcp_sg_list_alloc(sg_list,
484 ZFCP_CFDC_MAX_CONTROL_FILE_SIZE);
491 if ((sense_data->command & ZFCP_CFDC_DOWNLOAD) &&
492 (sense_data->command & ZFCP_CFDC_WITH_CONTROL_FILE)) {
493 retval = zfcp_sg_list_copy_from_user(
494 sg_list, &sense_data_user->control_file,
495 ZFCP_CFDC_MAX_CONTROL_FILE_SIZE);
502 retval = zfcp_fsf_control_file(adapter, &fsf_req, fsf_command,
507 if ((fsf_req->qtcb->prefix.prot_status != FSF_PROT_GOOD) &&
508 (fsf_req->qtcb->prefix.prot_status != FSF_PROT_FSF_STATUS_PRESENTED)) {
513 sense_data->fsf_status = fsf_req->qtcb->header.fsf_status;
514 memcpy(&sense_data->fsf_status_qual,
515 &fsf_req->qtcb->header.fsf_status_qual,
516 sizeof(union fsf_status_qual));
517 memcpy(&sense_data->payloads, &fsf_req->qtcb->bottom.support.els, 256);
519 retval = copy_to_user(sense_data_user, sense_data,
520 sizeof(struct zfcp_cfdc_sense_data));
526 if (sense_data->command & ZFCP_CFDC_UPLOAD) {
527 retval = zfcp_sg_list_copy_to_user(
528 &sense_data_user->control_file, sg_list,
529 ZFCP_CFDC_MAX_CONTROL_FILE_SIZE);
538 zfcp_fsf_req_cleanup(fsf_req);
540 if ((adapter != NULL) && (retval != -ENXIO))
541 zfcp_adapter_put(adapter);
543 if (sg_list != NULL) {
544 zfcp_sg_list_free(sg_list);
548 if (sense_data != NULL)
556 * zfcp_sg_list_alloc - create a scatter-gather list of the specified size
557 * @sg_list: structure describing a scatter gather list
558 * @size: size of scatter-gather list
559 * Return: 0 on success, else -ENOMEM
561 * In sg_list->sg a pointer to the created scatter-gather list is returned,
562 * or NULL if we run out of memory. sg_list->count specifies the number of
563 * elements of the scatter-gather list. The maximum size of a single element
564 * in the scatter-gather list is PAGE_SIZE.
567 zfcp_sg_list_alloc(struct zfcp_sg_list *sg_list, size_t size)
569 struct scatterlist *sg;
574 BUG_ON(sg_list == NULL);
576 sg_list->count = size >> PAGE_SHIFT;
577 if (size & ~PAGE_MASK)
579 sg_list->sg = kmalloc(sg_list->count * sizeof(struct scatterlist),
581 if (sg_list->sg == NULL) {
586 memset(sg_list->sg, 0, sg_list->count * sizeof(struct scatterlist));
588 for (i = 0, sg = sg_list->sg; i < sg_list->count; i++, sg++) {
589 sg->length = min(size, PAGE_SIZE);
591 address = (void *) get_zeroed_page(GFP_KERNEL);
592 if (address == NULL) {
594 zfcp_sg_list_free(sg_list);
598 zfcp_address_to_sg(address, sg);
608 * zfcp_sg_list_free - free memory of a scatter-gather list
609 * @sg_list: structure describing a scatter-gather list
611 * Memory for each element in the scatter-gather list is freed.
612 * Finally sg_list->sg is freed itself and sg_list->count is reset.
615 zfcp_sg_list_free(struct zfcp_sg_list *sg_list)
617 struct scatterlist *sg;
620 BUG_ON(sg_list == NULL);
622 for (i = 0, sg = sg_list->sg; i < sg_list->count; i++, sg++)
623 free_page((unsigned long) zfcp_sg_to_address(sg));
630 * zfcp_sg_size - determine size of a scatter-gather list
631 * @sg: array of (struct scatterlist)
632 * @sg_count: elements in array
633 * Return: size of entire scatter-gather list
636 zfcp_sg_size(struct scatterlist *sg, unsigned int sg_count)
639 struct scatterlist *p;
643 for (i = 0, p = sg; i < sg_count; i++, p++) {
653 * zfcp_sg_list_copy_from_user -copy data from user space to scatter-gather list
654 * @sg_list: structure describing a scatter-gather list
655 * @user_buffer: pointer to buffer in user space
656 * @size: number of bytes to be copied
657 * Return: 0 on success, -EFAULT if copy_from_user fails.
660 zfcp_sg_list_copy_from_user(struct zfcp_sg_list *sg_list,
661 void __user *user_buffer,
664 struct scatterlist *sg;
669 BUG_ON(sg_list == NULL);
671 if (zfcp_sg_size(sg_list->sg, sg_list->count) < size)
674 for (sg = sg_list->sg; size > 0; sg++) {
675 length = min((unsigned int)size, sg->length);
676 zfcp_buffer = zfcp_sg_to_address(sg);
677 if (copy_from_user(zfcp_buffer, user_buffer, length)) {
681 user_buffer += length;
691 * zfcp_sg_list_copy_to_user - copy data from scatter-gather list to user space
692 * @user_buffer: pointer to buffer in user space
693 * @sg_list: structure describing a scatter-gather list
694 * @size: number of bytes to be copied
695 * Return: 0 on success, -EFAULT if copy_to_user fails
698 zfcp_sg_list_copy_to_user(void __user *user_buffer,
699 struct zfcp_sg_list *sg_list,
702 struct scatterlist *sg;
707 BUG_ON(sg_list == NULL);
709 if (zfcp_sg_size(sg_list->sg, sg_list->count) < size)
712 for (sg = sg_list->sg; size > 0; sg++) {
713 length = min((unsigned int) size, sg->length);
714 zfcp_buffer = zfcp_sg_to_address(sg);
715 if (copy_to_user(user_buffer, zfcp_buffer, length)) {
719 user_buffer += length;
730 /****************************************************************/
731 /****** Functions for configuration/set-up of structures ********/
732 /****************************************************************/
734 #define ZFCP_LOG_AREA ZFCP_LOG_AREA_CONFIG
737 * zfcp_get_unit_by_lun - find unit in unit list of port by FCP LUN
738 * @port: pointer to port to search for unit
739 * @fcp_lun: FCP LUN to search for
740 * Traverse list of all units of a port and return pointer to a unit
741 * with the given FCP LUN.
744 zfcp_get_unit_by_lun(struct zfcp_port *port, fcp_lun_t fcp_lun)
746 struct zfcp_unit *unit;
749 list_for_each_entry(unit, &port->unit_list_head, list) {
750 if ((unit->fcp_lun == fcp_lun) &&
751 !atomic_test_mask(ZFCP_STATUS_COMMON_REMOVE, &unit->status))
757 return found ? unit : NULL;
761 * zfcp_get_port_by_wwpn - find port in port list of adapter by wwpn
762 * @adapter: pointer to adapter to search for port
763 * @wwpn: wwpn to search for
764 * Traverse list of all ports of an adapter and return pointer to a port
765 * with the given wwpn.
768 zfcp_get_port_by_wwpn(struct zfcp_adapter *adapter, wwn_t wwpn)
770 struct zfcp_port *port;
773 list_for_each_entry(port, &adapter->port_list_head, list) {
774 if ((port->wwpn == wwpn) &&
775 !(atomic_read(&port->status) &
776 (ZFCP_STATUS_PORT_NO_WWPN | ZFCP_STATUS_COMMON_REMOVE))) {
781 return found ? port : NULL;
785 * zfcp_get_port_by_did - find port in port list of adapter by d_id
786 * @adapter: pointer to adapter to search for port
787 * @d_id: d_id to search for
788 * Traverse list of all ports of an adapter and return pointer to a port
789 * with the given d_id.
792 zfcp_get_port_by_did(struct zfcp_adapter *adapter, u32 d_id)
794 struct zfcp_port *port;
797 list_for_each_entry(port, &adapter->port_list_head, list) {
798 if ((port->d_id == d_id) &&
799 !atomic_test_mask(ZFCP_STATUS_COMMON_REMOVE, &port->status))
805 return found ? port : NULL;
809 * zfcp_get_adapter_by_busid - find adpater in adapter list by bus_id
810 * @bus_id: bus_id to search for
811 * Traverse list of all adapters and return pointer to an adapter
812 * with the given bus_id.
814 struct zfcp_adapter *
815 zfcp_get_adapter_by_busid(char *bus_id)
817 struct zfcp_adapter *adapter;
820 list_for_each_entry(adapter, &zfcp_data.adapter_list_head, list) {
821 if ((strncmp(bus_id, zfcp_get_busid_by_adapter(adapter),
822 BUS_ID_SIZE) == 0) &&
823 !atomic_test_mask(ZFCP_STATUS_COMMON_REMOVE,
829 return found ? adapter : NULL;
833 * zfcp_unit_enqueue - enqueue unit to unit list of a port.
834 * @port: pointer to port where unit is added
835 * @fcp_lun: FCP LUN of unit to be enqueued
836 * Return: pointer to enqueued unit on success, NULL on error
837 * Locks: config_sema must be held to serialize changes to the unit list
839 * Sets up some unit internal structures and creates sysfs entry.
842 zfcp_unit_enqueue(struct zfcp_port *port, fcp_lun_t fcp_lun)
844 struct zfcp_unit *unit, *tmp_unit;
849 * check that there is no unit with this FCP_LUN already in list
851 * Note: Unlike for the adapter and the port, this is an error
853 read_lock_irq(&zfcp_data.config_lock);
854 unit = zfcp_get_unit_by_lun(port, fcp_lun);
855 read_unlock_irq(&zfcp_data.config_lock);
859 unit = kmalloc(sizeof (struct zfcp_unit), GFP_KERNEL);
862 memset(unit, 0, sizeof (struct zfcp_unit));
864 /* initialise reference count stuff */
865 atomic_set(&unit->refcount, 0);
866 init_waitqueue_head(&unit->remove_wq);
869 unit->fcp_lun = fcp_lun;
871 /* setup for sysfs registration */
872 snprintf(unit->sysfs_device.bus_id, BUS_ID_SIZE, "0x%016llx", fcp_lun);
873 unit->sysfs_device.parent = &port->sysfs_device;
874 unit->sysfs_device.release = zfcp_sysfs_unit_release;
875 dev_set_drvdata(&unit->sysfs_device, unit);
877 /* mark unit unusable as long as sysfs registration is not complete */
878 atomic_set_mask(ZFCP_STATUS_COMMON_REMOVE, &unit->status);
880 if (device_register(&unit->sysfs_device)) {
885 if (zfcp_sysfs_unit_create_files(&unit->sysfs_device)) {
886 device_unregister(&unit->sysfs_device);
894 write_lock_irq(&zfcp_data.config_lock);
895 list_for_each_entry(tmp_unit, &port->unit_list_head, list) {
896 if (tmp_unit->scsi_lun != scsi_lun) {
902 unit->scsi_lun = scsi_lun;
904 list_add_tail(&unit->list, &tmp_unit->list);
906 list_add_tail(&unit->list, &port->unit_list_head);
907 atomic_clear_mask(ZFCP_STATUS_COMMON_REMOVE, &unit->status);
908 atomic_set_mask(ZFCP_STATUS_COMMON_RUNNING, &unit->status);
909 write_unlock_irq(&zfcp_data.config_lock);
918 zfcp_unit_dequeue(struct zfcp_unit *unit)
920 zfcp_unit_wait(unit);
921 write_lock_irq(&zfcp_data.config_lock);
922 list_del(&unit->list);
923 write_unlock_irq(&zfcp_data.config_lock);
925 zfcp_port_put(unit->port);
926 zfcp_sysfs_unit_remove_files(&unit->sysfs_device);
927 device_unregister(&unit->sysfs_device);
931 zfcp_mempool_alloc(unsigned int __nocast gfp_mask, void *size)
933 return kmalloc((size_t) size, gfp_mask);
937 zfcp_mempool_free(void *element, void *size)
943 * Allocates a combined QTCB/fsf_req buffer for erp actions and fcp/SCSI
945 * It also genrates fcp-nameserver request/response buffer and unsolicited
946 * status read fsf_req buffers.
948 * locks: must only be called with zfcp_data.config_sema taken
951 zfcp_allocate_low_mem_buffers(struct zfcp_adapter *adapter)
953 adapter->pool.fsf_req_erp =
954 mempool_create(ZFCP_POOL_FSF_REQ_ERP_NR,
955 zfcp_mempool_alloc, zfcp_mempool_free, (void *)
956 sizeof(struct zfcp_fsf_req_pool_element));
958 if (NULL == adapter->pool.fsf_req_erp)
961 adapter->pool.fsf_req_scsi =
962 mempool_create(ZFCP_POOL_FSF_REQ_SCSI_NR,
963 zfcp_mempool_alloc, zfcp_mempool_free, (void *)
964 sizeof(struct zfcp_fsf_req_pool_element));
966 if (NULL == adapter->pool.fsf_req_scsi)
969 adapter->pool.fsf_req_abort =
970 mempool_create(ZFCP_POOL_FSF_REQ_ABORT_NR,
971 zfcp_mempool_alloc, zfcp_mempool_free, (void *)
972 sizeof(struct zfcp_fsf_req_pool_element));
974 if (NULL == adapter->pool.fsf_req_abort)
977 adapter->pool.fsf_req_status_read =
978 mempool_create(ZFCP_POOL_STATUS_READ_NR,
979 zfcp_mempool_alloc, zfcp_mempool_free,
980 (void *) sizeof(struct zfcp_fsf_req));
982 if (NULL == adapter->pool.fsf_req_status_read)
985 adapter->pool.data_status_read =
986 mempool_create(ZFCP_POOL_STATUS_READ_NR,
987 zfcp_mempool_alloc, zfcp_mempool_free,
988 (void *) sizeof(struct fsf_status_read_buffer));
990 if (NULL == adapter->pool.data_status_read)
993 adapter->pool.data_gid_pn =
994 mempool_create(ZFCP_POOL_DATA_GID_PN_NR,
995 zfcp_mempool_alloc, zfcp_mempool_free, (void *)
996 sizeof(struct zfcp_gid_pn_data));
998 if (NULL == adapter->pool.data_gid_pn)
1005 * zfcp_free_low_mem_buffers - free memory pools of an adapter
1006 * @adapter: pointer to zfcp_adapter for which memory pools should be freed
1007 * locking: zfcp_data.config_sema must be held
1010 zfcp_free_low_mem_buffers(struct zfcp_adapter *adapter)
1012 if (adapter->pool.fsf_req_erp)
1013 mempool_destroy(adapter->pool.fsf_req_erp);
1014 if (adapter->pool.fsf_req_scsi)
1015 mempool_destroy(adapter->pool.fsf_req_scsi);
1016 if (adapter->pool.fsf_req_abort)
1017 mempool_destroy(adapter->pool.fsf_req_abort);
1018 if (adapter->pool.fsf_req_status_read)
1019 mempool_destroy(adapter->pool.fsf_req_status_read);
1020 if (adapter->pool.data_status_read)
1021 mempool_destroy(adapter->pool.data_status_read);
1022 if (adapter->pool.data_gid_pn)
1023 mempool_destroy(adapter->pool.data_gid_pn);
1027 * zfcp_adapter_debug_register - registers debug feature for an adapter
1028 * @adapter: pointer to adapter for which debug features should be registered
1029 * return: -ENOMEM on error, 0 otherwise
1032 zfcp_adapter_debug_register(struct zfcp_adapter *adapter)
1036 /* debug feature area which records SCSI command failures (hostbyte) */
1037 spin_lock_init(&adapter->dbf_lock);
1039 sprintf(dbf_name, ZFCP_CMD_DBF_NAME "%s",
1040 zfcp_get_busid_by_adapter(adapter));
1041 adapter->cmd_dbf = debug_register(dbf_name, ZFCP_CMD_DBF_INDEX,
1043 ZFCP_CMD_DBF_LENGTH);
1044 debug_register_view(adapter->cmd_dbf, &debug_hex_ascii_view);
1045 debug_set_level(adapter->cmd_dbf, ZFCP_CMD_DBF_LEVEL);
1047 /* debug feature area which records SCSI command aborts */
1048 sprintf(dbf_name, ZFCP_ABORT_DBF_NAME "%s",
1049 zfcp_get_busid_by_adapter(adapter));
1050 adapter->abort_dbf = debug_register(dbf_name, ZFCP_ABORT_DBF_INDEX,
1051 ZFCP_ABORT_DBF_AREAS,
1052 ZFCP_ABORT_DBF_LENGTH);
1053 debug_register_view(adapter->abort_dbf, &debug_hex_ascii_view);
1054 debug_set_level(adapter->abort_dbf, ZFCP_ABORT_DBF_LEVEL);
1056 /* debug feature area which records incoming ELS commands */
1057 sprintf(dbf_name, ZFCP_IN_ELS_DBF_NAME "%s",
1058 zfcp_get_busid_by_adapter(adapter));
1059 adapter->in_els_dbf = debug_register(dbf_name, ZFCP_IN_ELS_DBF_INDEX,
1060 ZFCP_IN_ELS_DBF_AREAS,
1061 ZFCP_IN_ELS_DBF_LENGTH);
1062 debug_register_view(adapter->in_els_dbf, &debug_hex_ascii_view);
1063 debug_set_level(adapter->in_els_dbf, ZFCP_IN_ELS_DBF_LEVEL);
1065 /* debug feature area which records erp events */
1066 sprintf(dbf_name, ZFCP_ERP_DBF_NAME "%s",
1067 zfcp_get_busid_by_adapter(adapter));
1068 adapter->erp_dbf = debug_register(dbf_name, ZFCP_ERP_DBF_INDEX,
1070 ZFCP_ERP_DBF_LENGTH);
1071 debug_register_view(adapter->erp_dbf, &debug_hex_ascii_view);
1072 debug_set_level(adapter->erp_dbf, ZFCP_ERP_DBF_LEVEL);
1074 if (!(adapter->cmd_dbf && adapter->abort_dbf &&
1075 adapter->in_els_dbf && adapter->erp_dbf)) {
1076 zfcp_adapter_debug_unregister(adapter);
1085 * zfcp_adapter_debug_unregister - unregisters debug feature for an adapter
1086 * @adapter: pointer to adapter for which debug features should be unregistered
1089 zfcp_adapter_debug_unregister(struct zfcp_adapter *adapter)
1091 debug_unregister(adapter->abort_dbf);
1092 debug_unregister(adapter->cmd_dbf);
1093 debug_unregister(adapter->erp_dbf);
1094 debug_unregister(adapter->in_els_dbf);
1095 adapter->abort_dbf = NULL;
1096 adapter->cmd_dbf = NULL;
1097 adapter->erp_dbf = NULL;
1098 adapter->in_els_dbf = NULL;
1102 zfcp_dummy_release(struct device *dev)
1108 * Enqueues an adapter at the end of the adapter list in the driver data.
1109 * All adapter internal structures are set up.
1110 * Proc-fs entries are also created.
1112 * returns: 0 if a new adapter was successfully enqueued
1113 * ZFCP_KNOWN if an adapter with this devno was already present
1114 * -ENOMEM if alloc failed
1115 * locks: config_sema must be held to serialise changes to the adapter list
1117 struct zfcp_adapter *
1118 zfcp_adapter_enqueue(struct ccw_device *ccw_device)
1121 struct zfcp_adapter *adapter;
1124 * Note: It is safe to release the list_lock, as any list changes
1125 * are protected by the config_sema, which must be held to get here
1128 /* try to allocate new adapter data structure (zeroed) */
1129 adapter = kmalloc(sizeof (struct zfcp_adapter), GFP_KERNEL);
1131 ZFCP_LOG_INFO("error: allocation of base adapter "
1132 "structure failed\n");
1135 memset(adapter, 0, sizeof (struct zfcp_adapter));
1137 ccw_device->handler = NULL;
1139 /* save ccw_device pointer */
1140 adapter->ccw_device = ccw_device;
1142 retval = zfcp_qdio_allocate_queues(adapter);
1144 goto queues_alloc_failed;
1146 retval = zfcp_qdio_allocate(adapter);
1148 goto qdio_allocate_failed;
1150 retval = zfcp_allocate_low_mem_buffers(adapter);
1152 ZFCP_LOG_INFO("error: pool allocation failed\n");
1153 goto failed_low_mem_buffers;
1156 /* initialise reference count stuff */
1157 atomic_set(&adapter->refcount, 0);
1158 init_waitqueue_head(&adapter->remove_wq);
1160 /* initialise list of ports */
1161 INIT_LIST_HEAD(&adapter->port_list_head);
1163 /* initialise list of ports to be removed */
1164 INIT_LIST_HEAD(&adapter->port_remove_lh);
1166 /* initialize list of fsf requests */
1167 rwlock_init(&adapter->fsf_req_list_lock);
1168 INIT_LIST_HEAD(&adapter->fsf_req_list_head);
1170 /* initialize abort lock */
1171 rwlock_init(&adapter->abort_lock);
1173 /* initialise some erp stuff */
1174 init_waitqueue_head(&adapter->erp_thread_wqh);
1175 init_waitqueue_head(&adapter->erp_done_wqh);
1177 /* initialize lock of associated request queue */
1178 rwlock_init(&adapter->request_queue.queue_lock);
1180 /* intitialise SCSI ER timer */
1181 init_timer(&adapter->scsi_er_timer);
1183 /* set FC service class used per default */
1184 adapter->fc_service_class = ZFCP_FC_SERVICE_CLASS_DEFAULT;
1186 sprintf(adapter->name, "%s", zfcp_get_busid_by_adapter(adapter));
1187 ASCEBC(adapter->name, strlen(adapter->name));
1189 /* mark adapter unusable as long as sysfs registration is not complete */
1190 atomic_set_mask(ZFCP_STATUS_COMMON_REMOVE, &adapter->status);
1192 adapter->ccw_device = ccw_device;
1193 dev_set_drvdata(&ccw_device->dev, adapter);
1195 if (zfcp_sysfs_adapter_create_files(&ccw_device->dev))
1198 adapter->generic_services.parent = &adapter->ccw_device->dev;
1199 adapter->generic_services.release = zfcp_dummy_release;
1200 snprintf(adapter->generic_services.bus_id, BUS_ID_SIZE,
1201 "generic_services");
1203 if (device_register(&adapter->generic_services))
1204 goto generic_services_failed;
1206 /* put allocated adapter at list tail */
1207 write_lock_irq(&zfcp_data.config_lock);
1208 atomic_clear_mask(ZFCP_STATUS_COMMON_REMOVE, &adapter->status);
1209 list_add_tail(&adapter->list, &zfcp_data.adapter_list_head);
1210 write_unlock_irq(&zfcp_data.config_lock);
1212 zfcp_data.adapters++;
1216 generic_services_failed:
1217 zfcp_sysfs_adapter_remove_files(&adapter->ccw_device->dev);
1219 dev_set_drvdata(&ccw_device->dev, NULL);
1220 failed_low_mem_buffers:
1221 zfcp_free_low_mem_buffers(adapter);
1222 if (qdio_free(ccw_device) != 0)
1223 ZFCP_LOG_NORMAL("bug: qdio_free for adapter %s failed\n",
1224 zfcp_get_busid_by_adapter(adapter));
1225 qdio_allocate_failed:
1226 zfcp_qdio_free_queues(adapter);
1227 queues_alloc_failed:
1235 * returns: 0 - struct zfcp_adapter data structure successfully removed
1236 * !0 - struct zfcp_adapter data structure could not be removed
1238 * locks: adapter list write lock is assumed to be held by caller
1239 * adapter->fsf_req_list_lock is taken and released within this
1240 * function and must not be held on entry
1243 zfcp_adapter_dequeue(struct zfcp_adapter *adapter)
1246 unsigned long flags;
1248 device_unregister(&adapter->generic_services);
1249 zfcp_sysfs_adapter_remove_files(&adapter->ccw_device->dev);
1250 dev_set_drvdata(&adapter->ccw_device->dev, NULL);
1251 /* sanity check: no pending FSF requests */
1252 read_lock_irqsave(&adapter->fsf_req_list_lock, flags);
1253 retval = !list_empty(&adapter->fsf_req_list_head);
1254 read_unlock_irqrestore(&adapter->fsf_req_list_lock, flags);
1256 ZFCP_LOG_NORMAL("bug: adapter %s (%p) still in use, "
1257 "%i requests outstanding\n",
1258 zfcp_get_busid_by_adapter(adapter), adapter,
1259 atomic_read(&adapter->fsf_reqs_active));
1264 /* remove specified adapter data structure from list */
1265 write_lock_irq(&zfcp_data.config_lock);
1266 list_del(&adapter->list);
1267 write_unlock_irq(&zfcp_data.config_lock);
1269 /* decrease number of adapters in list */
1270 zfcp_data.adapters--;
1272 ZFCP_LOG_TRACE("adapter %s (%p) removed from list, "
1273 "%i adapters still in list\n",
1274 zfcp_get_busid_by_adapter(adapter),
1275 adapter, zfcp_data.adapters);
1277 retval = qdio_free(adapter->ccw_device);
1279 ZFCP_LOG_NORMAL("bug: qdio_free for adapter %s failed\n",
1280 zfcp_get_busid_by_adapter(adapter));
1282 zfcp_free_low_mem_buffers(adapter);
1283 /* free memory of adapter data structure and queues */
1284 zfcp_qdio_free_queues(adapter);
1285 ZFCP_LOG_TRACE("freeing adapter structure\n");
1292 * zfcp_port_enqueue - enqueue port to port list of adapter
1293 * @adapter: adapter where remote port is added
1294 * @wwpn: WWPN of the remote port to be enqueued
1295 * @status: initial status for the port
1296 * @d_id: destination id of the remote port to be enqueued
1297 * Return: pointer to enqueued port on success, NULL on error
1298 * Locks: config_sema must be held to serialize changes to the port list
1300 * All port internal structures are set up and the sysfs entry is generated.
1301 * d_id is used to enqueue ports with a well known address like the Directory
1302 * Service for nameserver lookup.
1305 zfcp_port_enqueue(struct zfcp_adapter *adapter, wwn_t wwpn, u32 status,
1308 struct zfcp_port *port, *tmp_port;
1313 check_wwpn = !(status & ZFCP_STATUS_PORT_NO_WWPN);
1316 * check that there is no port with this WWPN already in list
1319 read_lock_irq(&zfcp_data.config_lock);
1320 port = zfcp_get_port_by_wwpn(adapter, wwpn);
1321 read_unlock_irq(&zfcp_data.config_lock);
1326 port = kmalloc(sizeof (struct zfcp_port), GFP_KERNEL);
1329 memset(port, 0, sizeof (struct zfcp_port));
1331 /* initialise reference count stuff */
1332 atomic_set(&port->refcount, 0);
1333 init_waitqueue_head(&port->remove_wq);
1335 INIT_LIST_HEAD(&port->unit_list_head);
1336 INIT_LIST_HEAD(&port->unit_remove_lh);
1338 port->adapter = adapter;
1343 atomic_set_mask(status, &port->status);
1345 /* setup for sysfs registration */
1346 if (status & ZFCP_STATUS_PORT_WKA) {
1348 case ZFCP_DID_DIRECTORY_SERVICE:
1349 snprintf(port->sysfs_device.bus_id, BUS_ID_SIZE,
1352 case ZFCP_DID_MANAGEMENT_SERVICE:
1353 snprintf(port->sysfs_device.bus_id, BUS_ID_SIZE,
1356 case ZFCP_DID_KEY_DISTRIBUTION_SERVICE:
1357 snprintf(port->sysfs_device.bus_id, BUS_ID_SIZE,
1358 "key_distribution");
1360 case ZFCP_DID_ALIAS_SERVICE:
1361 snprintf(port->sysfs_device.bus_id, BUS_ID_SIZE,
1364 case ZFCP_DID_TIME_SERVICE:
1365 snprintf(port->sysfs_device.bus_id, BUS_ID_SIZE,
1373 port->sysfs_device.parent = &adapter->generic_services;
1375 snprintf(port->sysfs_device.bus_id,
1376 BUS_ID_SIZE, "0x%016llx", wwpn);
1377 port->sysfs_device.parent = &adapter->ccw_device->dev;
1379 port->sysfs_device.release = zfcp_sysfs_port_release;
1380 dev_set_drvdata(&port->sysfs_device, port);
1382 /* mark port unusable as long as sysfs registration is not complete */
1383 atomic_set_mask(ZFCP_STATUS_COMMON_REMOVE, &port->status);
1385 if (device_register(&port->sysfs_device)) {
1390 if (zfcp_sysfs_port_create_files(&port->sysfs_device, status)) {
1391 device_unregister(&port->sysfs_device);
1395 zfcp_port_get(port);
1399 write_lock_irq(&zfcp_data.config_lock);
1400 list_for_each_entry(tmp_port, &adapter->port_list_head, list) {
1401 if (atomic_test_mask(ZFCP_STATUS_PORT_NO_SCSI_ID,
1404 if (tmp_port->scsi_id != scsi_id) {
1410 port->scsi_id = scsi_id;
1412 list_add_tail(&port->list, &tmp_port->list);
1414 list_add_tail(&port->list, &adapter->port_list_head);
1415 atomic_clear_mask(ZFCP_STATUS_COMMON_REMOVE, &port->status);
1416 atomic_set_mask(ZFCP_STATUS_COMMON_RUNNING, &port->status);
1417 if (d_id == ZFCP_DID_DIRECTORY_SERVICE)
1418 if (!adapter->nameserver_port)
1419 adapter->nameserver_port = port;
1421 write_unlock_irq(&zfcp_data.config_lock);
1423 zfcp_adapter_get(adapter);
1429 zfcp_port_dequeue(struct zfcp_port *port)
1431 zfcp_port_wait(port);
1432 write_lock_irq(&zfcp_data.config_lock);
1433 list_del(&port->list);
1434 port->adapter->ports--;
1435 write_unlock_irq(&zfcp_data.config_lock);
1436 zfcp_adapter_put(port->adapter);
1437 zfcp_sysfs_port_remove_files(&port->sysfs_device,
1438 atomic_read(&port->status));
1439 device_unregister(&port->sysfs_device);
1442 /* Enqueues a nameserver port */
1444 zfcp_nameserver_enqueue(struct zfcp_adapter *adapter)
1446 struct zfcp_port *port;
1448 port = zfcp_port_enqueue(adapter, 0, ZFCP_STATUS_PORT_WKA,
1449 ZFCP_DID_DIRECTORY_SERVICE);
1451 ZFCP_LOG_INFO("error: enqueue of nameserver port for "
1452 "adapter %s failed\n",
1453 zfcp_get_busid_by_adapter(adapter));
1456 zfcp_port_put(port);
1461 #undef ZFCP_LOG_AREA
1463 /****************************************************************/
1464 /******* Fibre Channel Standard related Functions **************/
1465 /****************************************************************/
1467 #define ZFCP_LOG_AREA ZFCP_LOG_AREA_FC
1470 zfcp_fsf_incoming_els_rscn(struct zfcp_adapter *adapter,
1471 struct fsf_status_read_buffer *status_buffer)
1473 struct fcp_rscn_head *fcp_rscn_head;
1474 struct fcp_rscn_element *fcp_rscn_element;
1475 struct zfcp_port *port;
1479 unsigned long flags;
1481 fcp_rscn_head = (struct fcp_rscn_head *) status_buffer->payload;
1482 fcp_rscn_element = (struct fcp_rscn_element *) status_buffer->payload;
1485 no_entries = (fcp_rscn_head->payload_len / 4);
1487 zfcp_in_els_dbf_event(adapter, "##rscn", status_buffer,
1488 fcp_rscn_head->payload_len);
1490 debug_text_event(adapter->erp_dbf, 1, "unsol_els_rscn:");
1491 for (i = 1; i < no_entries; i++) {
1492 /* skip head and start with 1st element */
1494 switch (fcp_rscn_element->addr_format) {
1495 case ZFCP_PORT_ADDRESS:
1496 ZFCP_LOG_FLAGS(1, "ZFCP_PORT_ADDRESS\n");
1497 range_mask = ZFCP_PORTS_RANGE_PORT;
1499 case ZFCP_AREA_ADDRESS:
1500 ZFCP_LOG_FLAGS(1, "ZFCP_AREA_ADDRESS\n");
1501 range_mask = ZFCP_PORTS_RANGE_AREA;
1503 case ZFCP_DOMAIN_ADDRESS:
1504 ZFCP_LOG_FLAGS(1, "ZFCP_DOMAIN_ADDRESS\n");
1505 range_mask = ZFCP_PORTS_RANGE_DOMAIN;
1507 case ZFCP_FABRIC_ADDRESS:
1508 ZFCP_LOG_FLAGS(1, "ZFCP_FABRIC_ADDRESS\n");
1509 range_mask = ZFCP_PORTS_RANGE_FABRIC;
1512 ZFCP_LOG_INFO("incoming RSCN with unknown "
1513 "address format\n");
1516 read_lock_irqsave(&zfcp_data.config_lock, flags);
1517 list_for_each_entry(port, &adapter->port_list_head, list) {
1518 if (atomic_test_mask
1519 (ZFCP_STATUS_PORT_WKA, &port->status))
1521 /* Do we know this port? If not skip it. */
1522 if (!atomic_test_mask
1523 (ZFCP_STATUS_PORT_DID_DID, &port->status)) {
1524 ZFCP_LOG_INFO("incoming RSCN, trying to open "
1525 "port 0x%016Lx\n", port->wwpn);
1526 debug_text_event(adapter->erp_dbf, 1,
1527 "unsol_els_rscnu:");
1528 zfcp_erp_port_reopen(port,
1529 ZFCP_STATUS_COMMON_ERP_FAILED);
1534 * FIXME: race: d_id might being invalidated
1535 * (...DID_DID reset)
1537 if ((port->d_id & range_mask)
1538 == (fcp_rscn_element->nport_did & range_mask)) {
1539 ZFCP_LOG_TRACE("reopen did 0x%08x\n",
1540 fcp_rscn_element->nport_did);
1542 * Unfortunately, an RSCN does not specify the
1543 * type of change a target underwent. We assume
1544 * that it makes sense to reopen the link.
1545 * FIXME: Shall we try to find out more about
1546 * the target and link state before closing it?
1547 * How to accomplish this? (nameserver?)
1548 * Where would such code be put in?
1549 * (inside or outside erp)
1551 ZFCP_LOG_INFO("incoming RSCN, trying to open "
1552 "port 0x%016Lx\n", port->wwpn);
1553 debug_text_event(adapter->erp_dbf, 1,
1554 "unsol_els_rscnk:");
1555 zfcp_test_link(port);
1558 read_unlock_irqrestore(&zfcp_data.config_lock, flags);
1563 zfcp_fsf_incoming_els_plogi(struct zfcp_adapter *adapter,
1564 struct fsf_status_read_buffer *status_buffer)
1566 logi *els_logi = (logi *) status_buffer->payload;
1567 struct zfcp_port *port;
1568 unsigned long flags;
1570 zfcp_in_els_dbf_event(adapter, "##plogi", status_buffer, 28);
1572 read_lock_irqsave(&zfcp_data.config_lock, flags);
1573 list_for_each_entry(port, &adapter->port_list_head, list) {
1574 if (port->wwpn == (*(wwn_t *) & els_logi->nport_wwn))
1577 read_unlock_irqrestore(&zfcp_data.config_lock, flags);
1579 if (!port || (port->wwpn != (*(wwn_t *) & els_logi->nport_wwn))) {
1580 ZFCP_LOG_DEBUG("ignored incoming PLOGI for nonexisting port "
1581 "with d_id 0x%08x on adapter %s\n",
1582 status_buffer->d_id,
1583 zfcp_get_busid_by_adapter(adapter));
1585 debug_text_event(adapter->erp_dbf, 1, "unsol_els_plogi:");
1586 debug_event(adapter->erp_dbf, 1, &els_logi->nport_wwn, 8);
1587 zfcp_erp_port_forced_reopen(port, 0);
1592 zfcp_fsf_incoming_els_logo(struct zfcp_adapter *adapter,
1593 struct fsf_status_read_buffer *status_buffer)
1595 struct fcp_logo *els_logo = (struct fcp_logo *) status_buffer->payload;
1596 struct zfcp_port *port;
1597 unsigned long flags;
1599 zfcp_in_els_dbf_event(adapter, "##logo", status_buffer, 16);
1601 read_lock_irqsave(&zfcp_data.config_lock, flags);
1602 list_for_each_entry(port, &adapter->port_list_head, list) {
1603 if (port->wwpn == els_logo->nport_wwpn)
1606 read_unlock_irqrestore(&zfcp_data.config_lock, flags);
1608 if (!port || (port->wwpn != els_logo->nport_wwpn)) {
1609 ZFCP_LOG_DEBUG("ignored incoming LOGO for nonexisting port "
1610 "with d_id 0x%08x on adapter %s\n",
1611 status_buffer->d_id,
1612 zfcp_get_busid_by_adapter(adapter));
1614 debug_text_event(adapter->erp_dbf, 1, "unsol_els_logo:");
1615 debug_event(adapter->erp_dbf, 1, &els_logo->nport_wwpn, 8);
1616 zfcp_erp_port_forced_reopen(port, 0);
1621 zfcp_fsf_incoming_els_unknown(struct zfcp_adapter *adapter,
1622 struct fsf_status_read_buffer *status_buffer)
1624 zfcp_in_els_dbf_event(adapter, "##undef", status_buffer, 24);
1625 ZFCP_LOG_NORMAL("warning: unknown incoming ELS 0x%08x "
1626 "for adapter %s\n", *(u32 *) (status_buffer->payload),
1627 zfcp_get_busid_by_adapter(adapter));
1632 zfcp_fsf_incoming_els(struct zfcp_fsf_req *fsf_req)
1634 struct fsf_status_read_buffer *status_buffer;
1636 struct zfcp_adapter *adapter;
1638 status_buffer = fsf_req->data.status_read.buffer;
1639 els_type = *(u32 *) (status_buffer->payload);
1640 adapter = fsf_req->adapter;
1642 if (els_type == LS_PLOGI)
1643 zfcp_fsf_incoming_els_plogi(adapter, status_buffer);
1644 else if (els_type == LS_LOGO)
1645 zfcp_fsf_incoming_els_logo(adapter, status_buffer);
1646 else if ((els_type & 0xffff0000) == LS_RSCN)
1647 /* we are only concerned with the command, not the length */
1648 zfcp_fsf_incoming_els_rscn(adapter, status_buffer);
1650 zfcp_fsf_incoming_els_unknown(adapter, status_buffer);
1655 * zfcp_gid_pn_buffers_alloc - allocate buffers for GID_PN nameserver request
1656 * @gid_pn: pointer to return pointer to struct zfcp_gid_pn_data
1657 * @pool: pointer to mempool_t if non-null memory pool is used for allocation
1660 zfcp_gid_pn_buffers_alloc(struct zfcp_gid_pn_data **gid_pn, mempool_t *pool)
1662 struct zfcp_gid_pn_data *data;
1665 data = mempool_alloc(pool, GFP_ATOMIC);
1666 if (likely(data != NULL)) {
1667 data->ct.pool = pool;
1670 data = kmalloc(sizeof(struct zfcp_gid_pn_data), GFP_ATOMIC);
1676 memset(data, 0, sizeof(*data));
1677 data->ct.req = &data->req;
1678 data->ct.resp = &data->resp;
1679 data->ct.req_count = data->ct.resp_count = 1;
1680 zfcp_address_to_sg(&data->ct_iu_req, &data->req);
1681 zfcp_address_to_sg(&data->ct_iu_resp, &data->resp);
1682 data->req.length = sizeof(struct ct_iu_gid_pn_req);
1683 data->resp.length = sizeof(struct ct_iu_gid_pn_resp);
1690 * zfcp_gid_pn_buffers_free - free buffers for GID_PN nameserver request
1691 * @gid_pn: pointer to struct zfcp_gid_pn_data which has to be freed
1694 zfcp_gid_pn_buffers_free(struct zfcp_gid_pn_data *gid_pn)
1696 if ((gid_pn->ct.pool != 0))
1697 mempool_free(gid_pn, gid_pn->ct.pool);
1705 * zfcp_ns_gid_pn_request - initiate GID_PN nameserver request
1706 * @erp_action: pointer to zfcp_erp_action where GID_PN request is needed
1709 zfcp_ns_gid_pn_request(struct zfcp_erp_action *erp_action)
1712 struct ct_iu_gid_pn_req *ct_iu_req;
1713 struct zfcp_gid_pn_data *gid_pn;
1714 struct zfcp_adapter *adapter = erp_action->adapter;
1716 ret = zfcp_gid_pn_buffers_alloc(&gid_pn, adapter->pool.data_gid_pn);
1718 ZFCP_LOG_INFO("error: buffer allocation for gid_pn nameserver "
1719 "request failed for adapter %s\n",
1720 zfcp_get_busid_by_adapter(adapter));
1724 /* setup nameserver request */
1725 ct_iu_req = zfcp_sg_to_address(gid_pn->ct.req);
1726 ct_iu_req->header.revision = ZFCP_CT_REVISION;
1727 ct_iu_req->header.gs_type = ZFCP_CT_DIRECTORY_SERVICE;
1728 ct_iu_req->header.gs_subtype = ZFCP_CT_NAME_SERVER;
1729 ct_iu_req->header.options = ZFCP_CT_SYNCHRONOUS;
1730 ct_iu_req->header.cmd_rsp_code = ZFCP_CT_GID_PN;
1731 ct_iu_req->header.max_res_size = ZFCP_CT_MAX_SIZE;
1732 ct_iu_req->wwpn = erp_action->port->wwpn;
1734 /* setup parameters for send generic command */
1735 gid_pn->ct.port = adapter->nameserver_port;
1736 gid_pn->ct.handler = zfcp_ns_gid_pn_handler;
1737 gid_pn->ct.handler_data = (unsigned long) gid_pn;
1738 gid_pn->ct.timeout = ZFCP_NS_GID_PN_TIMEOUT;
1739 gid_pn->ct.timer = &erp_action->timer;
1740 gid_pn->port = erp_action->port;
1742 ret = zfcp_fsf_send_ct(&gid_pn->ct, adapter->pool.fsf_req_erp,
1745 ZFCP_LOG_INFO("error: initiation of gid_pn nameserver request "
1746 "failed for adapter %s\n",
1747 zfcp_get_busid_by_adapter(adapter));
1749 zfcp_gid_pn_buffers_free(gid_pn);
1757 * zfcp_ns_gid_pn_handler - handler for GID_PN nameserver request
1758 * @data: unsigned long, contains pointer to struct zfcp_gid_pn_data
1760 static void zfcp_ns_gid_pn_handler(unsigned long data)
1762 struct zfcp_port *port;
1763 struct zfcp_send_ct *ct;
1764 struct ct_iu_gid_pn_req *ct_iu_req;
1765 struct ct_iu_gid_pn_resp *ct_iu_resp;
1766 struct zfcp_gid_pn_data *gid_pn;
1769 gid_pn = (struct zfcp_gid_pn_data *) data;
1770 port = gid_pn->port;
1772 ct_iu_req = zfcp_sg_to_address(ct->req);
1773 ct_iu_resp = zfcp_sg_to_address(ct->resp);
1775 if ((ct->status != 0) || zfcp_check_ct_response(&ct_iu_resp->header)) {
1776 /* FIXME: do we need some specific erp entry points */
1777 atomic_set_mask(ZFCP_STATUS_PORT_INVALID_WWPN, &port->status);
1781 if (ct_iu_req->wwpn != port->wwpn) {
1782 ZFCP_LOG_NORMAL("bug: wwpn 0x%016Lx returned by nameserver "
1783 "lookup does not match expected wwpn 0x%016Lx "
1784 "for adapter %s\n", ct_iu_req->wwpn, port->wwpn,
1785 zfcp_get_busid_by_port(port));
1789 /* looks like a valid d_id */
1790 port->d_id = ct_iu_resp->d_id & ZFCP_DID_MASK;
1791 atomic_set_mask(ZFCP_STATUS_PORT_DID_DID, &port->status);
1792 ZFCP_LOG_DEBUG("adapter %s: wwpn=0x%016Lx ---> d_id=0x%08x\n",
1793 zfcp_get_busid_by_port(port), port->wwpn, port->d_id);
1797 ZFCP_LOG_DEBUG("CT IUs do not match:\n");
1798 ZFCP_HEX_DUMP(ZFCP_LOG_LEVEL_DEBUG, (char *) ct_iu_req,
1799 sizeof(struct ct_iu_gid_pn_req));
1800 ZFCP_HEX_DUMP(ZFCP_LOG_LEVEL_DEBUG, (char *) ct_iu_resp,
1801 sizeof(struct ct_iu_gid_pn_resp));
1804 ZFCP_LOG_NORMAL("warning: failed gid_pn nameserver request for wwpn "
1805 "0x%016Lx for adapter %s\n",
1806 port->wwpn, zfcp_get_busid_by_port(port));
1808 zfcp_gid_pn_buffers_free(gid_pn);
1812 /* reject CT_IU reason codes acc. to FC-GS-4 */
1813 static const struct zfcp_rc_entry zfcp_ct_rc[] = {
1814 {0x01, "invalid command code"},
1815 {0x02, "invalid version level"},
1816 {0x03, "logical error"},
1817 {0x04, "invalid CT_IU size"},
1818 {0x05, "logical busy"},
1819 {0x07, "protocol error"},
1820 {0x09, "unable to perform command request"},
1821 {0x0b, "command not supported"},
1822 {0x0d, "server not available"},
1823 {0x0e, "session could not be established"},
1824 {0xff, "vendor specific error"},
1828 /* LS_RJT reason codes acc. to FC-FS */
1829 static const struct zfcp_rc_entry zfcp_ls_rjt_rc[] = {
1830 {0x01, "invalid LS_Command code"},
1831 {0x03, "logical error"},
1832 {0x05, "logical busy"},
1833 {0x07, "protocol error"},
1834 {0x09, "unable to perform command request"},
1835 {0x0b, "command not supported"},
1836 {0x0e, "command already in progress"},
1837 {0xff, "vendor specific error"},
1841 /* reject reason codes according to FC-PH/FC-FS */
1842 static const struct zfcp_rc_entry zfcp_p_rjt_rc[] = {
1843 {0x01, "invalid D_ID"},
1844 {0x02, "invalid S_ID"},
1845 {0x03, "Nx_Port not available, temporary"},
1846 {0x04, "Nx_Port not available, permament"},
1847 {0x05, "class not supported"},
1848 {0x06, "delimiter usage error"},
1849 {0x07, "TYPE not supported"},
1850 {0x08, "invalid Link_Control"},
1851 {0x09, "invalid R_CTL field"},
1852 {0x0a, "invalid F_CTL field"},
1853 {0x0b, "invalid OX_ID"},
1854 {0x0c, "invalid RX_ID"},
1855 {0x0d, "invalid SEQ_ID"},
1856 {0x0e, "invalid DF_CTL"},
1857 {0x0f, "invalid SEQ_CNT"},
1858 {0x10, "invalid parameter field"},
1859 {0x11, "exchange error"},
1860 {0x12, "protocol error"},
1861 {0x13, "incorrect length"},
1862 {0x14, "unsupported ACK"},
1863 {0x15, "class of service not supported by entity at FFFFFE"},
1864 {0x16, "login required"},
1865 {0x17, "excessive sequences attempted"},
1866 {0x18, "unable to establish exchange"},
1867 {0x1a, "fabric path not available"},
1868 {0x1b, "invalid VC_ID (class 4)"},
1869 {0x1c, "invalid CS_CTL field"},
1870 {0x1d, "insufficient resources for VC (class 4)"},
1871 {0x1f, "invalid class of service"},
1872 {0x20, "preemption request rejected"},
1873 {0x21, "preemption not enabled"},
1874 {0x22, "multicast error"},
1875 {0x23, "multicast error terminate"},
1876 {0x24, "process login required"},
1877 {0xff, "vendor specific reject"},
1882 * zfcp_rc_description - return description for given reaon code
1883 * @code: reason code
1884 * @rc_table: table of reason codes and descriptions
1886 static inline const char *
1887 zfcp_rc_description(u8 code, const struct zfcp_rc_entry *rc_table)
1889 const char *descr = "unknown reason code";
1892 if (code == rc_table->code) {
1893 descr = rc_table->description;
1897 } while (rc_table->code && rc_table->description);
1903 * zfcp_check_ct_response - evaluate reason code for CT_IU
1904 * @rjt: response payload to an CT_IU request
1905 * Return: 0 for accept CT_IU, 1 for reject CT_IU or invlid response code
1908 zfcp_check_ct_response(struct ct_hdr *rjt)
1910 if (rjt->cmd_rsp_code == ZFCP_CT_ACCEPT)
1913 if (rjt->cmd_rsp_code != ZFCP_CT_REJECT) {
1914 ZFCP_LOG_NORMAL("error: invalid Generic Service command/"
1915 "response code (0x%04hx)\n",
1920 ZFCP_LOG_INFO("Generic Service command rejected\n");
1921 ZFCP_LOG_INFO("%s (0x%02x, 0x%02x, 0x%02x)\n",
1922 zfcp_rc_description(rjt->reason_code, zfcp_ct_rc),
1923 (u32) rjt->reason_code, (u32) rjt->reason_code_expl,
1924 (u32) rjt->vendor_unique);
1930 * zfcp_print_els_rjt - print reject parameter and description for ELS reject
1931 * @rjt_par: reject parameter acc. to FC-PH/FC-FS
1932 * @rc_table: table of reason codes and descriptions
1935 zfcp_print_els_rjt(struct zfcp_ls_rjt_par *rjt_par,
1936 const struct zfcp_rc_entry *rc_table)
1938 ZFCP_LOG_INFO("%s (%02x %02x %02x %02x)\n",
1939 zfcp_rc_description(rjt_par->reason_code, rc_table),
1940 (u32) rjt_par->action, (u32) rjt_par->reason_code,
1941 (u32) rjt_par->reason_expl, (u32) rjt_par->vendor_unique);
1945 * zfcp_fsf_handle_els_rjt - evaluate status qualifier/reason code on ELS reject
1946 * @sq: status qualifier word
1947 * @rjt_par: reject parameter as described in FC-PH and FC-FS
1948 * Return: -EROMTEIO for LS_RJT, -EREMCHG for invalid D_ID, -EIO else
1951 zfcp_handle_els_rjt(u32 sq, struct zfcp_ls_rjt_par *rjt_par)
1955 if (sq == FSF_IOSTAT_NPORT_RJT) {
1956 ZFCP_LOG_INFO("ELS rejected (P_RJT)\n");
1957 zfcp_print_els_rjt(rjt_par, zfcp_p_rjt_rc);
1959 if (rjt_par->reason_code == 0x01)
1961 } else if (sq == FSF_IOSTAT_FABRIC_RJT) {
1962 ZFCP_LOG_INFO("ELS rejected (F_RJT)\n");
1963 zfcp_print_els_rjt(rjt_par, zfcp_p_rjt_rc);
1965 if (rjt_par->reason_code == 0x01)
1967 } else if (sq == FSF_IOSTAT_LS_RJT) {
1968 ZFCP_LOG_INFO("ELS rejected (LS_RJT)\n");
1969 zfcp_print_els_rjt(rjt_par, zfcp_ls_rjt_rc);
1972 ZFCP_LOG_INFO("unexpected SQ: 0x%02x\n", sq);
1977 #undef ZFCP_LOG_AREA