xref: /linux/drivers/s390/scsi/zfcp_aux.c (revision 60b2737de1b1ddfdb90f3ba622634eb49d6f3603)
1 /*
2  *
3  * linux/drivers/s390/scsi/zfcp_aux.c
4  *
5  * FCP adapter driver for IBM eServer zSeries
6  *
7  * (C) Copyright IBM Corp. 2002, 2004
8  *
9  * Author(s): Martin Peschke <mpeschke@de.ibm.com>
10  *            Raimund Schroeder <raimund.schroeder@de.ibm.com>
11  *            Aron Zeh
12  *            Wolfgang Taphorn
13  *            Stefan Bader <stefan.bader@de.ibm.com>
14  *            Heiko Carstens <heiko.carstens@de.ibm.com>
15  *            Andreas Herrmann <aherrman@de.ibm.com>
16  *
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)
20  * any later version.
21  *
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.
26  *
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.
30  */
31 
32 #define ZFCP_AUX_REVISION "$Revision: 1.145 $"
33 
34 #include "zfcp_ext.h"
35 
36 /* accumulated log level (module parameter) */
37 static u32 loglevel = ZFCP_LOG_LEVEL_DEFAULTS;
38 static char *device;
39 /*********************** FUNCTION PROTOTYPES *********************************/
40 
41 /* written against the module interface */
42 static int __init  zfcp_module_init(void);
43 
44 /* FCP related */
45 static void zfcp_ns_gid_pn_handler(unsigned long);
46 
47 /* miscellaneous */
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);
54 
55 static long zfcp_cfdc_dev_ioctl(struct file *, unsigned int, unsigned long);
56 
57 #define ZFCP_CFDC_IOC_MAGIC                     0xDD
58 #define ZFCP_CFDC_IOC \
59 	_IOWR(ZFCP_CFDC_IOC_MAGIC, 0, struct zfcp_cfdc_sense_data)
60 
61 
62 static struct file_operations zfcp_cfdc_fops = {
63 	.unlocked_ioctl = zfcp_cfdc_dev_ioctl,
64 #ifdef CONFIG_COMPAT
65 	.compat_ioctl = zfcp_cfdc_dev_ioctl
66 #endif
67 };
68 
69 static struct miscdevice zfcp_cfdc_misc = {
70 	.minor = ZFCP_CFDC_DEV_MINOR,
71 	.name = ZFCP_CFDC_DEV_NAME,
72 	.fops = &zfcp_cfdc_fops
73 };
74 
75 /*********************** KERNEL/MODULE PARAMETERS  ***************************/
76 
77 /* declare driver module init/cleanup functions */
78 module_init(zfcp_module_init);
79 
80 MODULE_AUTHOR("Heiko Carstens <heiko.carstens@de.ibm.com>, "
81 	      "Andreas Herrman <aherrman@de.ibm.com>, "
82 	      "Martin Peschke <mpeschke@de.ibm.com>, "
83 	      "Raimund Schroeder <raimund.schroeder@de.ibm.com>, "
84 	      "Wolfgang Taphorn <taphorn@de.ibm.com>, "
85 	      "Aron Zeh <arzeh@de.ibm.com>, "
86 	      "IBM Deutschland Entwicklung GmbH");
87 MODULE_DESCRIPTION
88     ("FCP (SCSI over Fibre Channel) HBA driver for IBM eServer zSeries");
89 MODULE_LICENSE("GPL");
90 
91 module_param(device, charp, 0400);
92 MODULE_PARM_DESC(device, "specify initial device");
93 
94 module_param(loglevel, uint, 0400);
95 MODULE_PARM_DESC(loglevel,
96 		 "log levels, 8 nibbles: "
97 		 "FC ERP QDIO CIO Config FSF SCSI Other, "
98 		 "levels: 0=none 1=normal 2=devel 3=trace");
99 
100 /****************************************************************/
101 /************** Functions without logging ***********************/
102 /****************************************************************/
103 
104 void
105 _zfcp_hex_dump(char *addr, int count)
106 {
107 	int i;
108 	for (i = 0; i < count; i++) {
109 		printk("%02x", addr[i]);
110 		if ((i % 4) == 3)
111 			printk(" ");
112 		if ((i % 32) == 31)
113 			printk("\n");
114 	}
115 	if (((i-1) % 32) != 31)
116 		printk("\n");
117 }
118 
119 /****************************************************************/
120 /************** Uncategorised Functions *************************/
121 /****************************************************************/
122 
123 #define ZFCP_LOG_AREA			ZFCP_LOG_AREA_OTHER
124 
125 static inline int
126 zfcp_fsf_req_is_scsi_cmnd(struct zfcp_fsf_req *fsf_req)
127 {
128 	return ((fsf_req->fsf_command == FSF_QTCB_FCP_CMND) &&
129 		!(fsf_req->status & ZFCP_STATUS_FSFREQ_TASK_MANAGEMENT));
130 }
131 
132 void
133 zfcp_cmd_dbf_event_fsf(const char *text, struct zfcp_fsf_req *fsf_req,
134 		       void *add_data, int add_length)
135 {
136 	struct zfcp_adapter *adapter = fsf_req->adapter;
137 	struct scsi_cmnd *scsi_cmnd;
138 	int level = 3;
139 	int i;
140 	unsigned long flags;
141 
142 	spin_lock_irqsave(&adapter->dbf_lock, flags);
143 	if (zfcp_fsf_req_is_scsi_cmnd(fsf_req)) {
144 		scsi_cmnd = fsf_req->data.send_fcp_command_task.scsi_cmnd;
145 		debug_text_event(adapter->cmd_dbf, level, "fsferror");
146 		debug_text_event(adapter->cmd_dbf, level, text);
147 		debug_event(adapter->cmd_dbf, level, &fsf_req,
148 			    sizeof (unsigned long));
149 		debug_event(adapter->cmd_dbf, level, &fsf_req->seq_no,
150 			    sizeof (u32));
151 		debug_event(adapter->cmd_dbf, level, &scsi_cmnd,
152 			    sizeof (unsigned long));
153 		debug_event(adapter->cmd_dbf, level, &scsi_cmnd->cmnd,
154 			    min(ZFCP_CMD_DBF_LENGTH, (int)scsi_cmnd->cmd_len));
155 		for (i = 0; i < add_length; i += ZFCP_CMD_DBF_LENGTH)
156 			debug_event(adapter->cmd_dbf,
157 				    level,
158 				    (char *) add_data + i,
159 				    min(ZFCP_CMD_DBF_LENGTH, add_length - i));
160 	}
161 	spin_unlock_irqrestore(&adapter->dbf_lock, flags);
162 }
163 
164 /* XXX additionally log unit if available */
165 /* ---> introduce new parameter for unit, see 2.4 code */
166 void
167 zfcp_cmd_dbf_event_scsi(const char *text, struct scsi_cmnd *scsi_cmnd)
168 {
169 	struct zfcp_adapter *adapter;
170 	union zfcp_req_data *req_data;
171 	struct zfcp_fsf_req *fsf_req;
172 	int level = ((host_byte(scsi_cmnd->result) != 0) ? 1 : 5);
173 	unsigned long flags;
174 
175 	adapter = (struct zfcp_adapter *) scsi_cmnd->device->host->hostdata[0];
176 	req_data = (union zfcp_req_data *) scsi_cmnd->host_scribble;
177 	fsf_req = (req_data ? req_data->send_fcp_command_task.fsf_req : NULL);
178 	spin_lock_irqsave(&adapter->dbf_lock, flags);
179 	debug_text_event(adapter->cmd_dbf, level, "hostbyte");
180 	debug_text_event(adapter->cmd_dbf, level, text);
181 	debug_event(adapter->cmd_dbf, level, &scsi_cmnd->result, sizeof (u32));
182 	debug_event(adapter->cmd_dbf, level, &scsi_cmnd,
183 		    sizeof (unsigned long));
184 	debug_event(adapter->cmd_dbf, level, &scsi_cmnd->cmnd,
185 		    min(ZFCP_CMD_DBF_LENGTH, (int)scsi_cmnd->cmd_len));
186 	if (likely(fsf_req)) {
187 		debug_event(adapter->cmd_dbf, level, &fsf_req,
188 			    sizeof (unsigned long));
189 		debug_event(adapter->cmd_dbf, level, &fsf_req->seq_no,
190 			    sizeof (u32));
191 	} else {
192 		debug_text_event(adapter->cmd_dbf, level, "");
193 		debug_text_event(adapter->cmd_dbf, level, "");
194 	}
195 	spin_unlock_irqrestore(&adapter->dbf_lock, flags);
196 }
197 
198 void
199 zfcp_in_els_dbf_event(struct zfcp_adapter *adapter, const char *text,
200 		      struct fsf_status_read_buffer *status_buffer, int length)
201 {
202 	int level = 1;
203 	int i;
204 
205 	debug_text_event(adapter->in_els_dbf, level, text);
206 	debug_event(adapter->in_els_dbf, level, &status_buffer->d_id, 8);
207 	for (i = 0; i < length; i += ZFCP_IN_ELS_DBF_LENGTH)
208 		debug_event(adapter->in_els_dbf,
209 			    level,
210 			    (char *) status_buffer->payload + i,
211 			    min(ZFCP_IN_ELS_DBF_LENGTH, length - i));
212 }
213 
214 /**
215  * zfcp_device_setup - setup function
216  * @str: pointer to parameter string
217  *
218  * Parse "device=..." parameter string.
219  */
220 static int __init
221 zfcp_device_setup(char *devstr)
222 {
223 	char *tmp, *str;
224 	size_t len;
225 
226 	if (!devstr)
227 		return 0;
228 
229 	len = strlen(devstr) + 1;
230 	str = (char *) kmalloc(len, GFP_KERNEL);
231 	if (!str)
232 		goto err_out;
233 	memcpy(str, devstr, len);
234 
235 	tmp = strchr(str, ',');
236 	if (!tmp)
237 		goto err_out;
238 	*tmp++ = '\0';
239 	strncpy(zfcp_data.init_busid, str, BUS_ID_SIZE);
240 	zfcp_data.init_busid[BUS_ID_SIZE-1] = '\0';
241 
242 	zfcp_data.init_wwpn = simple_strtoull(tmp, &tmp, 0);
243 	if (*tmp++ != ',')
244 		goto err_out;
245 	if (*tmp == '\0')
246 		goto err_out;
247 
248 	zfcp_data.init_fcp_lun = simple_strtoull(tmp, &tmp, 0);
249 	if (*tmp != '\0')
250 		goto err_out;
251 	kfree(str);
252 	return 1;
253 
254  err_out:
255 	ZFCP_LOG_NORMAL("Parse error for device parameter string %s\n", str);
256 	kfree(str);
257 	return 0;
258 }
259 
260 static void __init
261 zfcp_init_device_configure(void)
262 {
263 	struct zfcp_adapter *adapter;
264 	struct zfcp_port *port;
265 	struct zfcp_unit *unit;
266 
267 	down(&zfcp_data.config_sema);
268 	read_lock_irq(&zfcp_data.config_lock);
269 	adapter = zfcp_get_adapter_by_busid(zfcp_data.init_busid);
270 	if (adapter)
271 		zfcp_adapter_get(adapter);
272 	read_unlock_irq(&zfcp_data.config_lock);
273 
274 	if (adapter == NULL)
275 		goto out_adapter;
276 	port = zfcp_port_enqueue(adapter, zfcp_data.init_wwpn, 0, 0);
277 	if (!port)
278 		goto out_port;
279 	unit = zfcp_unit_enqueue(port, zfcp_data.init_fcp_lun);
280 	if (!unit)
281 		goto out_unit;
282 	up(&zfcp_data.config_sema);
283 	ccw_device_set_online(adapter->ccw_device);
284 	zfcp_erp_wait(adapter);
285 	down(&zfcp_data.config_sema);
286 	zfcp_unit_put(unit);
287  out_unit:
288 	zfcp_port_put(port);
289  out_port:
290 	zfcp_adapter_put(adapter);
291  out_adapter:
292 	up(&zfcp_data.config_sema);
293 	return;
294 }
295 
296 static int __init
297 zfcp_module_init(void)
298 {
299 
300 	int retval = 0;
301 
302 	atomic_set(&zfcp_data.loglevel, loglevel);
303 
304 	/* initialize adapter list */
305 	INIT_LIST_HEAD(&zfcp_data.adapter_list_head);
306 
307 	/* initialize adapters to be removed list head */
308 	INIT_LIST_HEAD(&zfcp_data.adapter_remove_lh);
309 
310 	zfcp_transport_template = fc_attach_transport(&zfcp_transport_functions);
311 	if (!zfcp_transport_template)
312 		return -ENODEV;
313 
314 	retval = misc_register(&zfcp_cfdc_misc);
315 	if (retval != 0) {
316 		ZFCP_LOG_INFO("registration of misc device "
317 			      "zfcp_cfdc failed\n");
318 		goto out;
319 	}
320 
321 	ZFCP_LOG_TRACE("major/minor for zfcp_cfdc: %d/%d\n",
322 		       ZFCP_CFDC_DEV_MAJOR, zfcp_cfdc_misc.minor);
323 
324 	/* Initialise proc semaphores */
325 	sema_init(&zfcp_data.config_sema, 1);
326 
327 	/* initialise configuration rw lock */
328 	rwlock_init(&zfcp_data.config_lock);
329 
330 	/* save address of data structure managing the driver module */
331 	zfcp_data.scsi_host_template.module = THIS_MODULE;
332 
333 	/* setup dynamic I/O */
334 	retval = zfcp_ccw_register();
335 	if (retval) {
336 		ZFCP_LOG_NORMAL("registration with common I/O layer failed\n");
337 		goto out_ccw_register;
338 	}
339 
340 	if (zfcp_device_setup(device))
341 		zfcp_init_device_configure();
342 
343 	goto out;
344 
345  out_ccw_register:
346 	misc_deregister(&zfcp_cfdc_misc);
347  out:
348 	return retval;
349 }
350 
351 /*
352  * function:    zfcp_cfdc_dev_ioctl
353  *
354  * purpose:     Handle control file upload/download transaction via IOCTL
355  *		interface
356  *
357  * returns:     0           - Operation completed successfuly
358  *              -ENOTTY     - Unknown IOCTL command
359  *              -EINVAL     - Invalid sense data record
360  *              -ENXIO      - The FCP adapter is not available
361  *              -EOPNOTSUPP - The FCP adapter does not have CFDC support
362  *              -ENOMEM     - Insufficient memory
363  *              -EFAULT     - User space memory I/O operation fault
364  *              -EPERM      - Cannot create or queue FSF request or create SBALs
365  *              -ERESTARTSYS- Received signal (is mapped to EAGAIN by VFS)
366  */
367 static long
368 zfcp_cfdc_dev_ioctl(struct file *file, unsigned int command,
369 		    unsigned long buffer)
370 {
371 	struct zfcp_cfdc_sense_data *sense_data, __user *sense_data_user;
372 	struct zfcp_adapter *adapter = NULL;
373 	struct zfcp_fsf_req *fsf_req = NULL;
374 	struct zfcp_sg_list *sg_list = NULL;
375 	u32 fsf_command, option;
376 	char *bus_id = NULL;
377 	int retval = 0;
378 
379 	sense_data = kmalloc(sizeof(struct zfcp_cfdc_sense_data), GFP_KERNEL);
380 	if (sense_data == NULL) {
381 		retval = -ENOMEM;
382 		goto out;
383 	}
384 
385 	sg_list = kmalloc(sizeof(struct zfcp_sg_list), GFP_KERNEL);
386 	if (sg_list == NULL) {
387 		retval = -ENOMEM;
388 		goto out;
389 	}
390 	memset(sg_list, 0, sizeof(*sg_list));
391 
392 	if (command != ZFCP_CFDC_IOC) {
393 		ZFCP_LOG_INFO("IOC request code 0x%x invalid\n", command);
394 		retval = -ENOTTY;
395 		goto out;
396 	}
397 
398 	if ((sense_data_user = (void __user *) buffer) == NULL) {
399 		ZFCP_LOG_INFO("sense data record is required\n");
400 		retval = -EINVAL;
401 		goto out;
402 	}
403 
404 	retval = copy_from_user(sense_data, sense_data_user,
405 				sizeof(struct zfcp_cfdc_sense_data));
406 	if (retval) {
407 		retval = -EFAULT;
408 		goto out;
409 	}
410 
411 	if (sense_data->signature != ZFCP_CFDC_SIGNATURE) {
412 		ZFCP_LOG_INFO("invalid sense data request signature 0x%08x\n",
413 			      ZFCP_CFDC_SIGNATURE);
414 		retval = -EINVAL;
415 		goto out;
416 	}
417 
418 	switch (sense_data->command) {
419 
420 	case ZFCP_CFDC_CMND_DOWNLOAD_NORMAL:
421 		fsf_command = FSF_QTCB_DOWNLOAD_CONTROL_FILE;
422 		option = FSF_CFDC_OPTION_NORMAL_MODE;
423 		break;
424 
425 	case ZFCP_CFDC_CMND_DOWNLOAD_FORCE:
426 		fsf_command = FSF_QTCB_DOWNLOAD_CONTROL_FILE;
427 		option = FSF_CFDC_OPTION_FORCE;
428 		break;
429 
430 	case ZFCP_CFDC_CMND_FULL_ACCESS:
431 		fsf_command = FSF_QTCB_DOWNLOAD_CONTROL_FILE;
432 		option = FSF_CFDC_OPTION_FULL_ACCESS;
433 		break;
434 
435 	case ZFCP_CFDC_CMND_RESTRICTED_ACCESS:
436 		fsf_command = FSF_QTCB_DOWNLOAD_CONTROL_FILE;
437 		option = FSF_CFDC_OPTION_RESTRICTED_ACCESS;
438 		break;
439 
440 	case ZFCP_CFDC_CMND_UPLOAD:
441 		fsf_command = FSF_QTCB_UPLOAD_CONTROL_FILE;
442 		option = 0;
443 		break;
444 
445 	default:
446 		ZFCP_LOG_INFO("invalid command code 0x%08x\n",
447 			      sense_data->command);
448 		retval = -EINVAL;
449 		goto out;
450 	}
451 
452 	bus_id = kmalloc(BUS_ID_SIZE, GFP_KERNEL);
453 	if (bus_id == NULL) {
454 		retval = -ENOMEM;
455 		goto out;
456 	}
457 	snprintf(bus_id, BUS_ID_SIZE, "%d.%d.%04x",
458 		(sense_data->devno >> 24),
459 		(sense_data->devno >> 16) & 0xFF,
460 		(sense_data->devno & 0xFFFF));
461 
462 	read_lock_irq(&zfcp_data.config_lock);
463 	adapter = zfcp_get_adapter_by_busid(bus_id);
464 	if (adapter)
465 		zfcp_adapter_get(adapter);
466 	read_unlock_irq(&zfcp_data.config_lock);
467 
468 	kfree(bus_id);
469 
470 	if (adapter == NULL) {
471 		ZFCP_LOG_INFO("invalid adapter\n");
472 		retval = -ENXIO;
473 		goto out;
474 	}
475 
476 	if (sense_data->command & ZFCP_CFDC_WITH_CONTROL_FILE) {
477 		retval = zfcp_sg_list_alloc(sg_list,
478 					    ZFCP_CFDC_MAX_CONTROL_FILE_SIZE);
479 		if (retval) {
480 			retval = -ENOMEM;
481 			goto out;
482 		}
483 	}
484 
485 	if ((sense_data->command & ZFCP_CFDC_DOWNLOAD) &&
486 	    (sense_data->command & ZFCP_CFDC_WITH_CONTROL_FILE)) {
487 		retval = zfcp_sg_list_copy_from_user(
488 			sg_list, &sense_data_user->control_file,
489 			ZFCP_CFDC_MAX_CONTROL_FILE_SIZE);
490 		if (retval) {
491 			retval = -EFAULT;
492 			goto out;
493 		}
494 	}
495 
496 	retval = zfcp_fsf_control_file(adapter, &fsf_req, fsf_command,
497 				       option, sg_list);
498 	if (retval)
499 		goto out;
500 
501 	if ((fsf_req->qtcb->prefix.prot_status != FSF_PROT_GOOD) &&
502 	    (fsf_req->qtcb->prefix.prot_status != FSF_PROT_FSF_STATUS_PRESENTED)) {
503 		retval = -ENXIO;
504 		goto out;
505 	}
506 
507 	sense_data->fsf_status = fsf_req->qtcb->header.fsf_status;
508 	memcpy(&sense_data->fsf_status_qual,
509 	       &fsf_req->qtcb->header.fsf_status_qual,
510 	       sizeof(union fsf_status_qual));
511 	memcpy(&sense_data->payloads, &fsf_req->qtcb->bottom.support.els, 256);
512 
513 	retval = copy_to_user(sense_data_user, sense_data,
514 		sizeof(struct zfcp_cfdc_sense_data));
515 	if (retval) {
516 		retval = -EFAULT;
517 		goto out;
518 	}
519 
520 	if (sense_data->command & ZFCP_CFDC_UPLOAD) {
521 		retval = zfcp_sg_list_copy_to_user(
522 			&sense_data_user->control_file, sg_list,
523 			ZFCP_CFDC_MAX_CONTROL_FILE_SIZE);
524 		if (retval) {
525 			retval = -EFAULT;
526 			goto out;
527 		}
528 	}
529 
530  out:
531 	if (fsf_req != NULL)
532 		zfcp_fsf_req_free(fsf_req);
533 
534 	if ((adapter != NULL) && (retval != -ENXIO))
535 		zfcp_adapter_put(adapter);
536 
537 	if (sg_list != NULL) {
538 		zfcp_sg_list_free(sg_list);
539 		kfree(sg_list);
540 	}
541 
542 	if (sense_data != NULL)
543 		kfree(sense_data);
544 
545 	return retval;
546 }
547 
548 
549 /**
550  * zfcp_sg_list_alloc - create a scatter-gather list of the specified size
551  * @sg_list: structure describing a scatter gather list
552  * @size: size of scatter-gather list
553  * Return: 0 on success, else -ENOMEM
554  *
555  * In sg_list->sg a pointer to the created scatter-gather list is returned,
556  * or NULL if we run out of memory. sg_list->count specifies the number of
557  * elements of the scatter-gather list. The maximum size of a single element
558  * in the scatter-gather list is PAGE_SIZE.
559  */
560 static inline int
561 zfcp_sg_list_alloc(struct zfcp_sg_list *sg_list, size_t size)
562 {
563 	struct scatterlist *sg;
564 	unsigned int i;
565 	int retval = 0;
566 	void *address;
567 
568 	BUG_ON(sg_list == NULL);
569 
570 	sg_list->count = size >> PAGE_SHIFT;
571 	if (size & ~PAGE_MASK)
572 		sg_list->count++;
573 	sg_list->sg = kmalloc(sg_list->count * sizeof(struct scatterlist),
574 			      GFP_KERNEL);
575 	if (sg_list->sg == NULL) {
576 		sg_list->count = 0;
577 		retval = -ENOMEM;
578 		goto out;
579 	}
580 	memset(sg_list->sg, 0, sg_list->count * sizeof(struct scatterlist));
581 
582 	for (i = 0, sg = sg_list->sg; i < sg_list->count; i++, sg++) {
583 		sg->length = min(size, PAGE_SIZE);
584 		sg->offset = 0;
585 		address = (void *) get_zeroed_page(GFP_KERNEL);
586 		if (address == NULL) {
587 			sg_list->count = i;
588 			zfcp_sg_list_free(sg_list);
589 			retval = -ENOMEM;
590 			goto out;
591 		}
592 		zfcp_address_to_sg(address, sg);
593 		size -= sg->length;
594 	}
595 
596  out:
597 	return retval;
598 }
599 
600 
601 /**
602  * zfcp_sg_list_free - free memory of a scatter-gather list
603  * @sg_list: structure describing a scatter-gather list
604  *
605  * Memory for each element in the scatter-gather list is freed.
606  * Finally sg_list->sg is freed itself and sg_list->count is reset.
607  */
608 static inline void
609 zfcp_sg_list_free(struct zfcp_sg_list *sg_list)
610 {
611 	struct scatterlist *sg;
612 	unsigned int i;
613 
614 	BUG_ON(sg_list == NULL);
615 
616 	for (i = 0, sg = sg_list->sg; i < sg_list->count; i++, sg++)
617 		free_page((unsigned long) zfcp_sg_to_address(sg));
618 
619 	sg_list->count = 0;
620 	kfree(sg_list->sg);
621 }
622 
623 /**
624  * zfcp_sg_size - determine size of a scatter-gather list
625  * @sg: array of (struct scatterlist)
626  * @sg_count: elements in array
627  * Return: size of entire scatter-gather list
628  */
629 size_t
630 zfcp_sg_size(struct scatterlist *sg, unsigned int sg_count)
631 {
632 	unsigned int i;
633 	struct scatterlist *p;
634 	size_t size;
635 
636 	size = 0;
637 	for (i = 0, p = sg; i < sg_count; i++, p++) {
638 		BUG_ON(p == NULL);
639 		size += p->length;
640 	}
641 
642 	return size;
643 }
644 
645 
646 /**
647  * zfcp_sg_list_copy_from_user -copy data from user space to scatter-gather list
648  * @sg_list: structure describing a scatter-gather list
649  * @user_buffer: pointer to buffer in user space
650  * @size: number of bytes to be copied
651  * Return: 0 on success, -EFAULT if copy_from_user fails.
652  */
653 static inline int
654 zfcp_sg_list_copy_from_user(struct zfcp_sg_list *sg_list,
655 			    void __user *user_buffer,
656                             size_t size)
657 {
658 	struct scatterlist *sg;
659 	unsigned int length;
660 	void *zfcp_buffer;
661 	int retval = 0;
662 
663 	BUG_ON(sg_list == NULL);
664 
665 	if (zfcp_sg_size(sg_list->sg, sg_list->count) < size)
666 		return -EFAULT;
667 
668 	for (sg = sg_list->sg; size > 0; sg++) {
669 		length = min((unsigned int)size, sg->length);
670 		zfcp_buffer = zfcp_sg_to_address(sg);
671 		if (copy_from_user(zfcp_buffer, user_buffer, length)) {
672 			retval = -EFAULT;
673 			goto out;
674 		}
675 		user_buffer += length;
676 		size -= length;
677 	}
678 
679  out:
680 	return retval;
681 }
682 
683 
684 /**
685  * zfcp_sg_list_copy_to_user - copy data from scatter-gather list to user space
686  * @user_buffer: pointer to buffer in user space
687  * @sg_list: structure describing a scatter-gather list
688  * @size: number of bytes to be copied
689  * Return: 0 on success, -EFAULT if copy_to_user fails
690  */
691 static inline int
692 zfcp_sg_list_copy_to_user(void __user  *user_buffer,
693 			  struct zfcp_sg_list *sg_list,
694                           size_t size)
695 {
696 	struct scatterlist *sg;
697 	unsigned int length;
698 	void *zfcp_buffer;
699 	int retval = 0;
700 
701 	BUG_ON(sg_list == NULL);
702 
703 	if (zfcp_sg_size(sg_list->sg, sg_list->count) < size)
704 		return -EFAULT;
705 
706 	for (sg = sg_list->sg; size > 0; sg++) {
707 		length = min((unsigned int) size, sg->length);
708 		zfcp_buffer = zfcp_sg_to_address(sg);
709 		if (copy_to_user(user_buffer, zfcp_buffer, length)) {
710 			retval = -EFAULT;
711 			goto out;
712 		}
713 		user_buffer += length;
714 		size -= length;
715 	}
716 
717  out:
718 	return retval;
719 }
720 
721 
722 #undef ZFCP_LOG_AREA
723 
724 /****************************************************************/
725 /****** Functions for configuration/set-up of structures ********/
726 /****************************************************************/
727 
728 #define ZFCP_LOG_AREA			ZFCP_LOG_AREA_CONFIG
729 
730 /**
731  * zfcp_get_unit_by_lun - find unit in unit list of port by FCP LUN
732  * @port: pointer to port to search for unit
733  * @fcp_lun: FCP LUN to search for
734  * Traverse list of all units of a port and return pointer to a unit
735  * with the given FCP LUN.
736  */
737 struct zfcp_unit *
738 zfcp_get_unit_by_lun(struct zfcp_port *port, fcp_lun_t fcp_lun)
739 {
740 	struct zfcp_unit *unit;
741 	int found = 0;
742 
743 	list_for_each_entry(unit, &port->unit_list_head, list) {
744 		if ((unit->fcp_lun == fcp_lun) &&
745 		    !atomic_test_mask(ZFCP_STATUS_COMMON_REMOVE, &unit->status))
746 		{
747 			found = 1;
748 			break;
749 		}
750 	}
751 	return found ? unit : NULL;
752 }
753 
754 /**
755  * zfcp_get_port_by_wwpn - find port in port list of adapter by wwpn
756  * @adapter: pointer to adapter to search for port
757  * @wwpn: wwpn to search for
758  * Traverse list of all ports of an adapter and return pointer to a port
759  * with the given wwpn.
760  */
761 struct zfcp_port *
762 zfcp_get_port_by_wwpn(struct zfcp_adapter *adapter, wwn_t wwpn)
763 {
764 	struct zfcp_port *port;
765 	int found = 0;
766 
767 	list_for_each_entry(port, &adapter->port_list_head, list) {
768 		if ((port->wwpn == wwpn) &&
769 		    !(atomic_read(&port->status) &
770 		      (ZFCP_STATUS_PORT_NO_WWPN | ZFCP_STATUS_COMMON_REMOVE))) {
771 			found = 1;
772 			break;
773 		}
774 	}
775 	return found ? port : NULL;
776 }
777 
778 /**
779  * zfcp_get_port_by_did - find port in port list of adapter by d_id
780  * @adapter: pointer to adapter to search for port
781  * @d_id: d_id to search for
782  * Traverse list of all ports of an adapter and return pointer to a port
783  * with the given d_id.
784  */
785 struct zfcp_port *
786 zfcp_get_port_by_did(struct zfcp_adapter *adapter, u32 d_id)
787 {
788 	struct zfcp_port *port;
789 	int found = 0;
790 
791 	list_for_each_entry(port, &adapter->port_list_head, list) {
792 		if ((port->d_id == d_id) &&
793 		    !atomic_test_mask(ZFCP_STATUS_COMMON_REMOVE, &port->status))
794 		{
795 			found = 1;
796 			break;
797 		}
798 	}
799 	return found ? port : NULL;
800 }
801 
802 /**
803  * zfcp_get_adapter_by_busid - find adpater in adapter list by bus_id
804  * @bus_id: bus_id to search for
805  * Traverse list of all adapters and return pointer to an adapter
806  * with the given bus_id.
807  */
808 struct zfcp_adapter *
809 zfcp_get_adapter_by_busid(char *bus_id)
810 {
811 	struct zfcp_adapter *adapter;
812 	int found = 0;
813 
814 	list_for_each_entry(adapter, &zfcp_data.adapter_list_head, list) {
815 		if ((strncmp(bus_id, zfcp_get_busid_by_adapter(adapter),
816 			     BUS_ID_SIZE) == 0) &&
817 		    !atomic_test_mask(ZFCP_STATUS_COMMON_REMOVE,
818 				      &adapter->status)){
819 			found = 1;
820 			break;
821 		}
822 	}
823 	return found ? adapter : NULL;
824 }
825 
826 /**
827  * zfcp_unit_enqueue - enqueue unit to unit list of a port.
828  * @port: pointer to port where unit is added
829  * @fcp_lun: FCP LUN of unit to be enqueued
830  * Return: pointer to enqueued unit on success, NULL on error
831  * Locks: config_sema must be held to serialize changes to the unit list
832  *
833  * Sets up some unit internal structures and creates sysfs entry.
834  */
835 struct zfcp_unit *
836 zfcp_unit_enqueue(struct zfcp_port *port, fcp_lun_t fcp_lun)
837 {
838 	struct zfcp_unit *unit, *tmp_unit;
839 	scsi_lun_t scsi_lun;
840 	int found;
841 
842 	/*
843 	 * check that there is no unit with this FCP_LUN already in list
844 	 * and enqueue it.
845 	 * Note: Unlike for the adapter and the port, this is an error
846 	 */
847 	read_lock_irq(&zfcp_data.config_lock);
848 	unit = zfcp_get_unit_by_lun(port, fcp_lun);
849 	read_unlock_irq(&zfcp_data.config_lock);
850 	if (unit)
851 		return NULL;
852 
853 	unit = kmalloc(sizeof (struct zfcp_unit), GFP_KERNEL);
854 	if (!unit)
855 		return NULL;
856 	memset(unit, 0, sizeof (struct zfcp_unit));
857 
858 	/* initialise reference count stuff */
859 	atomic_set(&unit->refcount, 0);
860 	init_waitqueue_head(&unit->remove_wq);
861 
862 	unit->port = port;
863 	unit->fcp_lun = fcp_lun;
864 
865 	/* setup for sysfs registration */
866 	snprintf(unit->sysfs_device.bus_id, BUS_ID_SIZE, "0x%016llx", fcp_lun);
867 	unit->sysfs_device.parent = &port->sysfs_device;
868 	unit->sysfs_device.release = zfcp_sysfs_unit_release;
869 	dev_set_drvdata(&unit->sysfs_device, unit);
870 
871 	/* mark unit unusable as long as sysfs registration is not complete */
872 	atomic_set_mask(ZFCP_STATUS_COMMON_REMOVE, &unit->status);
873 
874 	if (device_register(&unit->sysfs_device)) {
875 		kfree(unit);
876 		return NULL;
877 	}
878 
879 	if (zfcp_sysfs_unit_create_files(&unit->sysfs_device)) {
880 		device_unregister(&unit->sysfs_device);
881 		return NULL;
882 	}
883 
884 	zfcp_unit_get(unit);
885 
886 	scsi_lun = 0;
887 	found = 0;
888 	write_lock_irq(&zfcp_data.config_lock);
889 	list_for_each_entry(tmp_unit, &port->unit_list_head, list) {
890 		if (tmp_unit->scsi_lun != scsi_lun) {
891 			found = 1;
892 			break;
893 		}
894 		scsi_lun++;
895 	}
896 	unit->scsi_lun = scsi_lun;
897 	if (found)
898 		list_add_tail(&unit->list, &tmp_unit->list);
899 	else
900 		list_add_tail(&unit->list, &port->unit_list_head);
901 	atomic_clear_mask(ZFCP_STATUS_COMMON_REMOVE, &unit->status);
902 	atomic_set_mask(ZFCP_STATUS_COMMON_RUNNING, &unit->status);
903 	write_unlock_irq(&zfcp_data.config_lock);
904 
905 	port->units++;
906 	zfcp_port_get(port);
907 
908 	return unit;
909 }
910 
911 void
912 zfcp_unit_dequeue(struct zfcp_unit *unit)
913 {
914 	zfcp_unit_wait(unit);
915 	write_lock_irq(&zfcp_data.config_lock);
916 	list_del(&unit->list);
917 	write_unlock_irq(&zfcp_data.config_lock);
918 	unit->port->units--;
919 	zfcp_port_put(unit->port);
920 	zfcp_sysfs_unit_remove_files(&unit->sysfs_device);
921 	device_unregister(&unit->sysfs_device);
922 }
923 
924 static void *
925 zfcp_mempool_alloc(unsigned int __nocast gfp_mask, void *size)
926 {
927 	return kmalloc((size_t) size, gfp_mask);
928 }
929 
930 static void
931 zfcp_mempool_free(void *element, void *size)
932 {
933 	kfree(element);
934 }
935 
936 /*
937  * Allocates a combined QTCB/fsf_req buffer for erp actions and fcp/SCSI
938  * commands.
939  * It also genrates fcp-nameserver request/response buffer and unsolicited
940  * status read fsf_req buffers.
941  *
942  * locks:       must only be called with zfcp_data.config_sema taken
943  */
944 static int
945 zfcp_allocate_low_mem_buffers(struct zfcp_adapter *adapter)
946 {
947 	adapter->pool.fsf_req_erp =
948 		mempool_create(ZFCP_POOL_FSF_REQ_ERP_NR,
949 			       zfcp_mempool_alloc, zfcp_mempool_free, (void *)
950 			       sizeof(struct zfcp_fsf_req_pool_element));
951 
952 	if (NULL == adapter->pool.fsf_req_erp)
953 		return -ENOMEM;
954 
955 	adapter->pool.fsf_req_scsi =
956 		mempool_create(ZFCP_POOL_FSF_REQ_SCSI_NR,
957 			       zfcp_mempool_alloc, zfcp_mempool_free, (void *)
958 			       sizeof(struct zfcp_fsf_req_pool_element));
959 
960 	if (NULL == adapter->pool.fsf_req_scsi)
961 		return -ENOMEM;
962 
963 	adapter->pool.fsf_req_abort =
964 		mempool_create(ZFCP_POOL_FSF_REQ_ABORT_NR,
965 			       zfcp_mempool_alloc, zfcp_mempool_free, (void *)
966 			       sizeof(struct zfcp_fsf_req_pool_element));
967 
968 	if (NULL == adapter->pool.fsf_req_abort)
969 		return -ENOMEM;
970 
971 	adapter->pool.fsf_req_status_read =
972 		mempool_create(ZFCP_POOL_STATUS_READ_NR,
973 			       zfcp_mempool_alloc, zfcp_mempool_free,
974 			       (void *) sizeof(struct zfcp_fsf_req));
975 
976 	if (NULL == adapter->pool.fsf_req_status_read)
977 		return -ENOMEM;
978 
979 	adapter->pool.data_status_read =
980 		mempool_create(ZFCP_POOL_STATUS_READ_NR,
981 			       zfcp_mempool_alloc, zfcp_mempool_free,
982 			       (void *) sizeof(struct fsf_status_read_buffer));
983 
984 	if (NULL == adapter->pool.data_status_read)
985 		return -ENOMEM;
986 
987 	adapter->pool.data_gid_pn =
988 		mempool_create(ZFCP_POOL_DATA_GID_PN_NR,
989 			       zfcp_mempool_alloc, zfcp_mempool_free, (void *)
990 			       sizeof(struct zfcp_gid_pn_data));
991 
992 	if (NULL == adapter->pool.data_gid_pn)
993 		return -ENOMEM;
994 
995 	return 0;
996 }
997 
998 /**
999  * zfcp_free_low_mem_buffers - free memory pools of an adapter
1000  * @adapter: pointer to zfcp_adapter for which memory pools should be freed
1001  * locking:  zfcp_data.config_sema must be held
1002  */
1003 static void
1004 zfcp_free_low_mem_buffers(struct zfcp_adapter *adapter)
1005 {
1006 	if (adapter->pool.fsf_req_erp)
1007 		mempool_destroy(adapter->pool.fsf_req_erp);
1008 	if (adapter->pool.fsf_req_scsi)
1009 		mempool_destroy(adapter->pool.fsf_req_scsi);
1010 	if (adapter->pool.fsf_req_abort)
1011 		mempool_destroy(adapter->pool.fsf_req_abort);
1012 	if (adapter->pool.fsf_req_status_read)
1013 		mempool_destroy(adapter->pool.fsf_req_status_read);
1014 	if (adapter->pool.data_status_read)
1015 		mempool_destroy(adapter->pool.data_status_read);
1016 	if (adapter->pool.data_gid_pn)
1017 		mempool_destroy(adapter->pool.data_gid_pn);
1018 }
1019 
1020 /**
1021  * zfcp_adapter_debug_register - registers debug feature for an adapter
1022  * @adapter: pointer to adapter for which debug features should be registered
1023  * return: -ENOMEM on error, 0 otherwise
1024  */
1025 int
1026 zfcp_adapter_debug_register(struct zfcp_adapter *adapter)
1027 {
1028 	char dbf_name[20];
1029 
1030 	/* debug feature area which records SCSI command failures (hostbyte) */
1031 	spin_lock_init(&adapter->dbf_lock);
1032 
1033 	sprintf(dbf_name, ZFCP_CMD_DBF_NAME "%s",
1034 		zfcp_get_busid_by_adapter(adapter));
1035 	adapter->cmd_dbf = debug_register(dbf_name, ZFCP_CMD_DBF_INDEX,
1036 					  ZFCP_CMD_DBF_AREAS,
1037 					  ZFCP_CMD_DBF_LENGTH);
1038 	debug_register_view(adapter->cmd_dbf, &debug_hex_ascii_view);
1039 	debug_set_level(adapter->cmd_dbf, ZFCP_CMD_DBF_LEVEL);
1040 
1041 	/* debug feature area which records SCSI command aborts */
1042 	sprintf(dbf_name, ZFCP_ABORT_DBF_NAME "%s",
1043 		zfcp_get_busid_by_adapter(adapter));
1044 	adapter->abort_dbf = debug_register(dbf_name, ZFCP_ABORT_DBF_INDEX,
1045 					    ZFCP_ABORT_DBF_AREAS,
1046 					    ZFCP_ABORT_DBF_LENGTH);
1047 	debug_register_view(adapter->abort_dbf, &debug_hex_ascii_view);
1048 	debug_set_level(adapter->abort_dbf, ZFCP_ABORT_DBF_LEVEL);
1049 
1050 	/* debug feature area which records incoming ELS commands */
1051 	sprintf(dbf_name, ZFCP_IN_ELS_DBF_NAME "%s",
1052 		zfcp_get_busid_by_adapter(adapter));
1053 	adapter->in_els_dbf = debug_register(dbf_name, ZFCP_IN_ELS_DBF_INDEX,
1054 					     ZFCP_IN_ELS_DBF_AREAS,
1055 					     ZFCP_IN_ELS_DBF_LENGTH);
1056 	debug_register_view(adapter->in_els_dbf, &debug_hex_ascii_view);
1057 	debug_set_level(adapter->in_els_dbf, ZFCP_IN_ELS_DBF_LEVEL);
1058 
1059 	/* debug feature area which records erp events */
1060 	sprintf(dbf_name, ZFCP_ERP_DBF_NAME "%s",
1061 		zfcp_get_busid_by_adapter(adapter));
1062 	adapter->erp_dbf = debug_register(dbf_name, ZFCP_ERP_DBF_INDEX,
1063 					  ZFCP_ERP_DBF_AREAS,
1064 					  ZFCP_ERP_DBF_LENGTH);
1065 	debug_register_view(adapter->erp_dbf, &debug_hex_ascii_view);
1066 	debug_set_level(adapter->erp_dbf, ZFCP_ERP_DBF_LEVEL);
1067 
1068 	if (!(adapter->cmd_dbf && adapter->abort_dbf &&
1069 	      adapter->in_els_dbf && adapter->erp_dbf)) {
1070 		zfcp_adapter_debug_unregister(adapter);
1071 		return -ENOMEM;
1072 	}
1073 
1074 	return 0;
1075 
1076 }
1077 
1078 /**
1079  * zfcp_adapter_debug_unregister - unregisters debug feature for an adapter
1080  * @adapter: pointer to adapter for which debug features should be unregistered
1081  */
1082 void
1083 zfcp_adapter_debug_unregister(struct zfcp_adapter *adapter)
1084 {
1085  	debug_unregister(adapter->abort_dbf);
1086  	debug_unregister(adapter->cmd_dbf);
1087  	debug_unregister(adapter->erp_dbf);
1088  	debug_unregister(adapter->in_els_dbf);
1089 	adapter->abort_dbf = NULL;
1090 	adapter->cmd_dbf = NULL;
1091 	adapter->erp_dbf = NULL;
1092 	adapter->in_els_dbf = NULL;
1093 }
1094 
1095 void
1096 zfcp_dummy_release(struct device *dev)
1097 {
1098 	return;
1099 }
1100 
1101 /*
1102  * Enqueues an adapter at the end of the adapter list in the driver data.
1103  * All adapter internal structures are set up.
1104  * Proc-fs entries are also created.
1105  *
1106  * returns:	0             if a new adapter was successfully enqueued
1107  *              ZFCP_KNOWN    if an adapter with this devno was already present
1108  *		-ENOMEM       if alloc failed
1109  * locks:	config_sema must be held to serialise changes to the adapter list
1110  */
1111 struct zfcp_adapter *
1112 zfcp_adapter_enqueue(struct ccw_device *ccw_device)
1113 {
1114 	int retval = 0;
1115 	struct zfcp_adapter *adapter;
1116 
1117 	/*
1118 	 * Note: It is safe to release the list_lock, as any list changes
1119 	 * are protected by the config_sema, which must be held to get here
1120 	 */
1121 
1122 	/* try to allocate new adapter data structure (zeroed) */
1123 	adapter = kmalloc(sizeof (struct zfcp_adapter), GFP_KERNEL);
1124 	if (!adapter) {
1125 		ZFCP_LOG_INFO("error: allocation of base adapter "
1126 			      "structure failed\n");
1127 		goto out;
1128 	}
1129 	memset(adapter, 0, sizeof (struct zfcp_adapter));
1130 
1131 	ccw_device->handler = NULL;
1132 
1133 	/* save ccw_device pointer */
1134 	adapter->ccw_device = ccw_device;
1135 
1136 	retval = zfcp_qdio_allocate_queues(adapter);
1137 	if (retval)
1138 		goto queues_alloc_failed;
1139 
1140 	retval = zfcp_qdio_allocate(adapter);
1141 	if (retval)
1142 		goto qdio_allocate_failed;
1143 
1144 	retval = zfcp_allocate_low_mem_buffers(adapter);
1145 	if (retval) {
1146 		ZFCP_LOG_INFO("error: pool allocation failed\n");
1147 		goto failed_low_mem_buffers;
1148 	}
1149 
1150 	/* initialise reference count stuff */
1151 	atomic_set(&adapter->refcount, 0);
1152 	init_waitqueue_head(&adapter->remove_wq);
1153 
1154 	/* initialise list of ports */
1155 	INIT_LIST_HEAD(&adapter->port_list_head);
1156 
1157 	/* initialise list of ports to be removed */
1158 	INIT_LIST_HEAD(&adapter->port_remove_lh);
1159 
1160 	/* initialize list of fsf requests */
1161 	spin_lock_init(&adapter->fsf_req_list_lock);
1162 	INIT_LIST_HEAD(&adapter->fsf_req_list_head);
1163 
1164 	/* initialize abort lock */
1165 	rwlock_init(&adapter->abort_lock);
1166 
1167 	/* initialise some erp stuff */
1168 	init_waitqueue_head(&adapter->erp_thread_wqh);
1169 	init_waitqueue_head(&adapter->erp_done_wqh);
1170 
1171 	/* initialize lock of associated request queue */
1172 	rwlock_init(&adapter->request_queue.queue_lock);
1173 
1174 	/* intitialise SCSI ER timer */
1175 	init_timer(&adapter->scsi_er_timer);
1176 
1177 	/* set FC service class used per default */
1178 	adapter->fc_service_class = ZFCP_FC_SERVICE_CLASS_DEFAULT;
1179 
1180 	sprintf(adapter->name, "%s", zfcp_get_busid_by_adapter(adapter));
1181 	ASCEBC(adapter->name, strlen(adapter->name));
1182 
1183 	/* mark adapter unusable as long as sysfs registration is not complete */
1184 	atomic_set_mask(ZFCP_STATUS_COMMON_REMOVE, &adapter->status);
1185 
1186 	adapter->ccw_device = ccw_device;
1187 	dev_set_drvdata(&ccw_device->dev, adapter);
1188 
1189 	if (zfcp_sysfs_adapter_create_files(&ccw_device->dev))
1190 		goto sysfs_failed;
1191 
1192 	adapter->generic_services.parent = &adapter->ccw_device->dev;
1193 	adapter->generic_services.release = zfcp_dummy_release;
1194 	snprintf(adapter->generic_services.bus_id, BUS_ID_SIZE,
1195 		 "generic_services");
1196 
1197 	if (device_register(&adapter->generic_services))
1198 		goto generic_services_failed;
1199 
1200 	/* put allocated adapter at list tail */
1201 	write_lock_irq(&zfcp_data.config_lock);
1202 	atomic_clear_mask(ZFCP_STATUS_COMMON_REMOVE, &adapter->status);
1203 	list_add_tail(&adapter->list, &zfcp_data.adapter_list_head);
1204 	write_unlock_irq(&zfcp_data.config_lock);
1205 
1206 	zfcp_data.adapters++;
1207 
1208 	goto out;
1209 
1210  generic_services_failed:
1211 	zfcp_sysfs_adapter_remove_files(&adapter->ccw_device->dev);
1212  sysfs_failed:
1213 	dev_set_drvdata(&ccw_device->dev, NULL);
1214  failed_low_mem_buffers:
1215 	zfcp_free_low_mem_buffers(adapter);
1216 	if (qdio_free(ccw_device) != 0)
1217 		ZFCP_LOG_NORMAL("bug: qdio_free for adapter %s failed\n",
1218 				zfcp_get_busid_by_adapter(adapter));
1219  qdio_allocate_failed:
1220 	zfcp_qdio_free_queues(adapter);
1221  queues_alloc_failed:
1222 	kfree(adapter);
1223 	adapter = NULL;
1224  out:
1225 	return adapter;
1226 }
1227 
1228 /*
1229  * returns:	0 - struct zfcp_adapter  data structure successfully removed
1230  *		!0 - struct zfcp_adapter  data structure could not be removed
1231  *			(e.g. still used)
1232  * locks:	adapter list write lock is assumed to be held by caller
1233  *              adapter->fsf_req_list_lock is taken and released within this
1234  *              function and must not be held on entry
1235  */
1236 void
1237 zfcp_adapter_dequeue(struct zfcp_adapter *adapter)
1238 {
1239 	int retval = 0;
1240 	unsigned long flags;
1241 
1242 	device_unregister(&adapter->generic_services);
1243 	zfcp_sysfs_adapter_remove_files(&adapter->ccw_device->dev);
1244 	dev_set_drvdata(&adapter->ccw_device->dev, NULL);
1245 	/* sanity check: no pending FSF requests */
1246 	spin_lock_irqsave(&adapter->fsf_req_list_lock, flags);
1247 	retval = !list_empty(&adapter->fsf_req_list_head);
1248 	spin_unlock_irqrestore(&adapter->fsf_req_list_lock, flags);
1249 	if (retval) {
1250 		ZFCP_LOG_NORMAL("bug: adapter %s (%p) still in use, "
1251 				"%i requests outstanding\n",
1252 				zfcp_get_busid_by_adapter(adapter), adapter,
1253 				atomic_read(&adapter->fsf_reqs_active));
1254 		retval = -EBUSY;
1255 		goto out;
1256 	}
1257 
1258 	/* remove specified adapter data structure from list */
1259 	write_lock_irq(&zfcp_data.config_lock);
1260 	list_del(&adapter->list);
1261 	write_unlock_irq(&zfcp_data.config_lock);
1262 
1263 	/* decrease number of adapters in list */
1264 	zfcp_data.adapters--;
1265 
1266 	ZFCP_LOG_TRACE("adapter %s (%p) removed from list, "
1267 		       "%i adapters still in list\n",
1268 		       zfcp_get_busid_by_adapter(adapter),
1269 		       adapter, zfcp_data.adapters);
1270 
1271 	retval = qdio_free(adapter->ccw_device);
1272 	if (retval)
1273 		ZFCP_LOG_NORMAL("bug: qdio_free for adapter %s failed\n",
1274 				zfcp_get_busid_by_adapter(adapter));
1275 
1276 	zfcp_free_low_mem_buffers(adapter);
1277 	/* free memory of adapter data structure and queues */
1278 	zfcp_qdio_free_queues(adapter);
1279 	ZFCP_LOG_TRACE("freeing adapter structure\n");
1280 	kfree(adapter);
1281  out:
1282 	return;
1283 }
1284 
1285 /**
1286  * zfcp_port_enqueue - enqueue port to port list of adapter
1287  * @adapter: adapter where remote port is added
1288  * @wwpn: WWPN of the remote port to be enqueued
1289  * @status: initial status for the port
1290  * @d_id: destination id of the remote port to be enqueued
1291  * Return: pointer to enqueued port on success, NULL on error
1292  * Locks: config_sema must be held to serialize changes to the port list
1293  *
1294  * All port internal structures are set up and the sysfs entry is generated.
1295  * d_id is used to enqueue ports with a well known address like the Directory
1296  * Service for nameserver lookup.
1297  */
1298 struct zfcp_port *
1299 zfcp_port_enqueue(struct zfcp_adapter *adapter, wwn_t wwpn, u32 status,
1300 		  u32 d_id)
1301 {
1302 	struct zfcp_port *port, *tmp_port;
1303 	int check_wwpn;
1304 	scsi_id_t scsi_id;
1305 	int found;
1306 
1307 	check_wwpn = !(status & ZFCP_STATUS_PORT_NO_WWPN);
1308 
1309 	/*
1310 	 * check that there is no port with this WWPN already in list
1311 	 */
1312 	if (check_wwpn) {
1313 		read_lock_irq(&zfcp_data.config_lock);
1314 		port = zfcp_get_port_by_wwpn(adapter, wwpn);
1315 		read_unlock_irq(&zfcp_data.config_lock);
1316 		if (port)
1317 			return NULL;
1318 	}
1319 
1320 	port = kmalloc(sizeof (struct zfcp_port), GFP_KERNEL);
1321 	if (!port)
1322 		return NULL;
1323 	memset(port, 0, sizeof (struct zfcp_port));
1324 
1325 	/* initialise reference count stuff */
1326 	atomic_set(&port->refcount, 0);
1327 	init_waitqueue_head(&port->remove_wq);
1328 
1329 	INIT_LIST_HEAD(&port->unit_list_head);
1330 	INIT_LIST_HEAD(&port->unit_remove_lh);
1331 
1332 	port->adapter = adapter;
1333 
1334 	if (check_wwpn)
1335 		port->wwpn = wwpn;
1336 
1337 	atomic_set_mask(status, &port->status);
1338 
1339 	/* setup for sysfs registration */
1340 	if (status & ZFCP_STATUS_PORT_WKA) {
1341 		switch (d_id) {
1342 		case ZFCP_DID_DIRECTORY_SERVICE:
1343 			snprintf(port->sysfs_device.bus_id, BUS_ID_SIZE,
1344 				 "directory");
1345 			break;
1346 		case ZFCP_DID_MANAGEMENT_SERVICE:
1347 			snprintf(port->sysfs_device.bus_id, BUS_ID_SIZE,
1348 				 "management");
1349 			break;
1350 		case ZFCP_DID_KEY_DISTRIBUTION_SERVICE:
1351 			snprintf(port->sysfs_device.bus_id, BUS_ID_SIZE,
1352 				 "key_distribution");
1353 			break;
1354 		case ZFCP_DID_ALIAS_SERVICE:
1355 			snprintf(port->sysfs_device.bus_id, BUS_ID_SIZE,
1356 				 "alias");
1357 			break;
1358 		case ZFCP_DID_TIME_SERVICE:
1359 			snprintf(port->sysfs_device.bus_id, BUS_ID_SIZE,
1360 				 "time");
1361 			break;
1362 		default:
1363 			kfree(port);
1364 			return NULL;
1365 		}
1366 		port->d_id = d_id;
1367 		port->sysfs_device.parent = &adapter->generic_services;
1368 	} else {
1369 		snprintf(port->sysfs_device.bus_id,
1370 			 BUS_ID_SIZE, "0x%016llx", wwpn);
1371 	port->sysfs_device.parent = &adapter->ccw_device->dev;
1372 	}
1373 	port->sysfs_device.release = zfcp_sysfs_port_release;
1374 	dev_set_drvdata(&port->sysfs_device, port);
1375 
1376 	/* mark port unusable as long as sysfs registration is not complete */
1377 	atomic_set_mask(ZFCP_STATUS_COMMON_REMOVE, &port->status);
1378 
1379 	if (device_register(&port->sysfs_device)) {
1380 		kfree(port);
1381 		return NULL;
1382 	}
1383 
1384 	if (zfcp_sysfs_port_create_files(&port->sysfs_device, status)) {
1385 		device_unregister(&port->sysfs_device);
1386 		return NULL;
1387 	}
1388 
1389 	zfcp_port_get(port);
1390 
1391 	scsi_id = 1;
1392 	found = 0;
1393 	write_lock_irq(&zfcp_data.config_lock);
1394 	list_for_each_entry(tmp_port, &adapter->port_list_head, list) {
1395 		if (atomic_test_mask(ZFCP_STATUS_PORT_NO_SCSI_ID,
1396 				     &tmp_port->status))
1397 			continue;
1398 		if (tmp_port->scsi_id != scsi_id) {
1399 			found = 1;
1400 			break;
1401 		}
1402 		scsi_id++;
1403 	}
1404 	port->scsi_id = scsi_id;
1405 	if (found)
1406 		list_add_tail(&port->list, &tmp_port->list);
1407 	else
1408 		list_add_tail(&port->list, &adapter->port_list_head);
1409 	atomic_clear_mask(ZFCP_STATUS_COMMON_REMOVE, &port->status);
1410 	atomic_set_mask(ZFCP_STATUS_COMMON_RUNNING, &port->status);
1411 	if (d_id == ZFCP_DID_DIRECTORY_SERVICE)
1412 		if (!adapter->nameserver_port)
1413 			adapter->nameserver_port = port;
1414 	adapter->ports++;
1415 	write_unlock_irq(&zfcp_data.config_lock);
1416 
1417 	zfcp_adapter_get(adapter);
1418 
1419 	return port;
1420 }
1421 
1422 void
1423 zfcp_port_dequeue(struct zfcp_port *port)
1424 {
1425 	zfcp_port_wait(port);
1426 	write_lock_irq(&zfcp_data.config_lock);
1427 	list_del(&port->list);
1428 	port->adapter->ports--;
1429 	write_unlock_irq(&zfcp_data.config_lock);
1430 	zfcp_adapter_put(port->adapter);
1431 	zfcp_sysfs_port_remove_files(&port->sysfs_device,
1432 				     atomic_read(&port->status));
1433 	device_unregister(&port->sysfs_device);
1434 }
1435 
1436 /* Enqueues a nameserver port */
1437 int
1438 zfcp_nameserver_enqueue(struct zfcp_adapter *adapter)
1439 {
1440 	struct zfcp_port *port;
1441 
1442 	port = zfcp_port_enqueue(adapter, 0, ZFCP_STATUS_PORT_WKA,
1443 				 ZFCP_DID_DIRECTORY_SERVICE);
1444 	if (!port) {
1445 		ZFCP_LOG_INFO("error: enqueue of nameserver port for "
1446 			      "adapter %s failed\n",
1447 			      zfcp_get_busid_by_adapter(adapter));
1448 		return -ENXIO;
1449 	}
1450 	zfcp_port_put(port);
1451 
1452 	return 0;
1453 }
1454 
1455 #undef ZFCP_LOG_AREA
1456 
1457 /****************************************************************/
1458 /******* Fibre Channel Standard related Functions  **************/
1459 /****************************************************************/
1460 
1461 #define ZFCP_LOG_AREA                   ZFCP_LOG_AREA_FC
1462 
1463 void
1464 zfcp_fsf_incoming_els_rscn(struct zfcp_adapter *adapter,
1465 			   struct fsf_status_read_buffer *status_buffer)
1466 {
1467 	struct fcp_rscn_head *fcp_rscn_head;
1468 	struct fcp_rscn_element *fcp_rscn_element;
1469 	struct zfcp_port *port;
1470 	u16 i;
1471 	u16 no_entries;
1472 	u32 range_mask;
1473 	unsigned long flags;
1474 
1475 	fcp_rscn_head = (struct fcp_rscn_head *) status_buffer->payload;
1476 	fcp_rscn_element = (struct fcp_rscn_element *) status_buffer->payload;
1477 
1478 	/* see FC-FS */
1479 	no_entries = (fcp_rscn_head->payload_len / 4);
1480 
1481 	zfcp_in_els_dbf_event(adapter, "##rscn", status_buffer,
1482 			      fcp_rscn_head->payload_len);
1483 
1484 	debug_text_event(adapter->erp_dbf, 1, "unsol_els_rscn:");
1485 	for (i = 1; i < no_entries; i++) {
1486 		/* skip head and start with 1st element */
1487 		fcp_rscn_element++;
1488 		switch (fcp_rscn_element->addr_format) {
1489 		case ZFCP_PORT_ADDRESS:
1490 			range_mask = ZFCP_PORTS_RANGE_PORT;
1491 			break;
1492 		case ZFCP_AREA_ADDRESS:
1493 			range_mask = ZFCP_PORTS_RANGE_AREA;
1494 			break;
1495 		case ZFCP_DOMAIN_ADDRESS:
1496 			range_mask = ZFCP_PORTS_RANGE_DOMAIN;
1497 			break;
1498 		case ZFCP_FABRIC_ADDRESS:
1499 			range_mask = ZFCP_PORTS_RANGE_FABRIC;
1500 			break;
1501 		default:
1502 			ZFCP_LOG_INFO("incoming RSCN with unknown "
1503 				      "address format\n");
1504 			continue;
1505 		}
1506 		read_lock_irqsave(&zfcp_data.config_lock, flags);
1507 		list_for_each_entry(port, &adapter->port_list_head, list) {
1508 			if (atomic_test_mask
1509 			    (ZFCP_STATUS_PORT_WKA, &port->status))
1510 				continue;
1511 			/* Do we know this port? If not skip it. */
1512 			if (!atomic_test_mask
1513 			    (ZFCP_STATUS_PORT_DID_DID, &port->status)) {
1514 				ZFCP_LOG_INFO("incoming RSCN, trying to open "
1515 					      "port 0x%016Lx\n", port->wwpn);
1516 				debug_text_event(adapter->erp_dbf, 1,
1517 						 "unsol_els_rscnu:");
1518 				zfcp_erp_port_reopen(port,
1519 						     ZFCP_STATUS_COMMON_ERP_FAILED);
1520 				continue;
1521 			}
1522 
1523 			/*
1524 			 * FIXME: race: d_id might being invalidated
1525 			 * (...DID_DID reset)
1526 			 */
1527 			if ((port->d_id & range_mask)
1528 			    == (fcp_rscn_element->nport_did & range_mask)) {
1529 				ZFCP_LOG_TRACE("reopen did 0x%08x\n",
1530 					       fcp_rscn_element->nport_did);
1531 				/*
1532 				 * Unfortunately, an RSCN does not specify the
1533 				 * type of change a target underwent. We assume
1534 				 * that it makes sense to reopen the link.
1535 				 * FIXME: Shall we try to find out more about
1536 				 * the target and link state before closing it?
1537 				 * How to accomplish this? (nameserver?)
1538 				 * Where would such code be put in?
1539 				 * (inside or outside erp)
1540 				 */
1541 				ZFCP_LOG_INFO("incoming RSCN, trying to open "
1542 					      "port 0x%016Lx\n", port->wwpn);
1543 				debug_text_event(adapter->erp_dbf, 1,
1544 						 "unsol_els_rscnk:");
1545 				zfcp_test_link(port);
1546 			}
1547 		}
1548 		read_unlock_irqrestore(&zfcp_data.config_lock, flags);
1549 	}
1550 }
1551 
1552 static void
1553 zfcp_fsf_incoming_els_plogi(struct zfcp_adapter *adapter,
1554 			    struct fsf_status_read_buffer *status_buffer)
1555 {
1556 	logi *els_logi = (logi *) status_buffer->payload;
1557 	struct zfcp_port *port;
1558 	unsigned long flags;
1559 
1560 	zfcp_in_els_dbf_event(adapter, "##plogi", status_buffer, 28);
1561 
1562 	read_lock_irqsave(&zfcp_data.config_lock, flags);
1563 	list_for_each_entry(port, &adapter->port_list_head, list) {
1564 		if (port->wwpn == (*(wwn_t *) & els_logi->nport_wwn))
1565 			break;
1566 	}
1567 	read_unlock_irqrestore(&zfcp_data.config_lock, flags);
1568 
1569 	if (!port || (port->wwpn != (*(wwn_t *) & els_logi->nport_wwn))) {
1570 		ZFCP_LOG_DEBUG("ignored incoming PLOGI for nonexisting port "
1571 			       "with d_id 0x%08x on adapter %s\n",
1572 			       status_buffer->d_id,
1573 			       zfcp_get_busid_by_adapter(adapter));
1574 	} else {
1575 		debug_text_event(adapter->erp_dbf, 1, "unsol_els_plogi:");
1576 		debug_event(adapter->erp_dbf, 1, &els_logi->nport_wwn, 8);
1577 		zfcp_erp_port_forced_reopen(port, 0);
1578 	}
1579 }
1580 
1581 static void
1582 zfcp_fsf_incoming_els_logo(struct zfcp_adapter *adapter,
1583 			   struct fsf_status_read_buffer *status_buffer)
1584 {
1585 	struct fcp_logo *els_logo = (struct fcp_logo *) status_buffer->payload;
1586 	struct zfcp_port *port;
1587 	unsigned long flags;
1588 
1589 	zfcp_in_els_dbf_event(adapter, "##logo", status_buffer, 16);
1590 
1591 	read_lock_irqsave(&zfcp_data.config_lock, flags);
1592 	list_for_each_entry(port, &adapter->port_list_head, list) {
1593 		if (port->wwpn == els_logo->nport_wwpn)
1594 			break;
1595 	}
1596 	read_unlock_irqrestore(&zfcp_data.config_lock, flags);
1597 
1598 	if (!port || (port->wwpn != els_logo->nport_wwpn)) {
1599 		ZFCP_LOG_DEBUG("ignored incoming LOGO for nonexisting port "
1600 			       "with d_id 0x%08x on adapter %s\n",
1601 			       status_buffer->d_id,
1602 			       zfcp_get_busid_by_adapter(adapter));
1603 	} else {
1604 		debug_text_event(adapter->erp_dbf, 1, "unsol_els_logo:");
1605 		debug_event(adapter->erp_dbf, 1, &els_logo->nport_wwpn, 8);
1606 		zfcp_erp_port_forced_reopen(port, 0);
1607 	}
1608 }
1609 
1610 static void
1611 zfcp_fsf_incoming_els_unknown(struct zfcp_adapter *adapter,
1612 			      struct fsf_status_read_buffer *status_buffer)
1613 {
1614 	zfcp_in_els_dbf_event(adapter, "##undef", status_buffer, 24);
1615 	ZFCP_LOG_NORMAL("warning: unknown incoming ELS 0x%08x "
1616 			"for adapter %s\n", *(u32 *) (status_buffer->payload),
1617 			zfcp_get_busid_by_adapter(adapter));
1618 
1619 }
1620 
1621 void
1622 zfcp_fsf_incoming_els(struct zfcp_fsf_req *fsf_req)
1623 {
1624 	struct fsf_status_read_buffer *status_buffer;
1625 	u32 els_type;
1626 	struct zfcp_adapter *adapter;
1627 
1628 	status_buffer = fsf_req->data.status_read.buffer;
1629 	els_type = *(u32 *) (status_buffer->payload);
1630 	adapter = fsf_req->adapter;
1631 
1632 	if (els_type == LS_PLOGI)
1633 		zfcp_fsf_incoming_els_plogi(adapter, status_buffer);
1634 	else if (els_type == LS_LOGO)
1635 		zfcp_fsf_incoming_els_logo(adapter, status_buffer);
1636 	else if ((els_type & 0xffff0000) == LS_RSCN)
1637 		/* we are only concerned with the command, not the length */
1638 		zfcp_fsf_incoming_els_rscn(adapter, status_buffer);
1639 	else
1640 		zfcp_fsf_incoming_els_unknown(adapter, status_buffer);
1641 }
1642 
1643 
1644 /**
1645  * zfcp_gid_pn_buffers_alloc - allocate buffers for GID_PN nameserver request
1646  * @gid_pn: pointer to return pointer to struct zfcp_gid_pn_data
1647  * @pool: pointer to mempool_t if non-null memory pool is used for allocation
1648  */
1649 static int
1650 zfcp_gid_pn_buffers_alloc(struct zfcp_gid_pn_data **gid_pn, mempool_t *pool)
1651 {
1652 	struct zfcp_gid_pn_data *data;
1653 
1654 	if (pool != NULL) {
1655 		data = mempool_alloc(pool, GFP_ATOMIC);
1656 		if (likely(data != NULL)) {
1657 			data->ct.pool = pool;
1658 		}
1659 	} else {
1660 		data = kmalloc(sizeof(struct zfcp_gid_pn_data), GFP_ATOMIC);
1661 	}
1662 
1663         if (NULL == data)
1664                 return -ENOMEM;
1665 
1666 	memset(data, 0, sizeof(*data));
1667         data->ct.req = &data->req;
1668         data->ct.resp = &data->resp;
1669 	data->ct.req_count = data->ct.resp_count = 1;
1670 	zfcp_address_to_sg(&data->ct_iu_req, &data->req);
1671         zfcp_address_to_sg(&data->ct_iu_resp, &data->resp);
1672         data->req.length = sizeof(struct ct_iu_gid_pn_req);
1673         data->resp.length = sizeof(struct ct_iu_gid_pn_resp);
1674 
1675 	*gid_pn = data;
1676 	return 0;
1677 }
1678 
1679 /**
1680  * zfcp_gid_pn_buffers_free - free buffers for GID_PN nameserver request
1681  * @gid_pn: pointer to struct zfcp_gid_pn_data which has to be freed
1682  */
1683 static void
1684 zfcp_gid_pn_buffers_free(struct zfcp_gid_pn_data *gid_pn)
1685 {
1686         if ((gid_pn->ct.pool != 0))
1687 		mempool_free(gid_pn, gid_pn->ct.pool);
1688 	else
1689                 kfree(gid_pn);
1690 
1691 	return;
1692 }
1693 
1694 /**
1695  * zfcp_ns_gid_pn_request - initiate GID_PN nameserver request
1696  * @erp_action: pointer to zfcp_erp_action where GID_PN request is needed
1697  */
1698 int
1699 zfcp_ns_gid_pn_request(struct zfcp_erp_action *erp_action)
1700 {
1701 	int ret;
1702         struct ct_iu_gid_pn_req *ct_iu_req;
1703         struct zfcp_gid_pn_data *gid_pn;
1704         struct zfcp_adapter *adapter = erp_action->adapter;
1705 
1706 	ret = zfcp_gid_pn_buffers_alloc(&gid_pn, adapter->pool.data_gid_pn);
1707 	if (ret < 0) {
1708 		ZFCP_LOG_INFO("error: buffer allocation for gid_pn nameserver "
1709 			      "request failed for adapter %s\n",
1710 			      zfcp_get_busid_by_adapter(adapter));
1711 		goto out;
1712 	}
1713 
1714 	/* setup nameserver request */
1715         ct_iu_req = zfcp_sg_to_address(gid_pn->ct.req);
1716         ct_iu_req->header.revision = ZFCP_CT_REVISION;
1717         ct_iu_req->header.gs_type = ZFCP_CT_DIRECTORY_SERVICE;
1718         ct_iu_req->header.gs_subtype = ZFCP_CT_NAME_SERVER;
1719         ct_iu_req->header.options = ZFCP_CT_SYNCHRONOUS;
1720         ct_iu_req->header.cmd_rsp_code = ZFCP_CT_GID_PN;
1721         ct_iu_req->header.max_res_size = ZFCP_CT_MAX_SIZE;
1722 	ct_iu_req->wwpn = erp_action->port->wwpn;
1723 
1724         /* setup parameters for send generic command */
1725         gid_pn->ct.port = adapter->nameserver_port;
1726 	gid_pn->ct.handler = zfcp_ns_gid_pn_handler;
1727 	gid_pn->ct.handler_data = (unsigned long) gid_pn;
1728         gid_pn->ct.timeout = ZFCP_NS_GID_PN_TIMEOUT;
1729         gid_pn->ct.timer = &erp_action->timer;
1730 	gid_pn->port = erp_action->port;
1731 
1732 	ret = zfcp_fsf_send_ct(&gid_pn->ct, adapter->pool.fsf_req_erp,
1733 			       erp_action);
1734 	if (ret) {
1735 		ZFCP_LOG_INFO("error: initiation of gid_pn nameserver request "
1736                               "failed for adapter %s\n",
1737 			      zfcp_get_busid_by_adapter(adapter));
1738 
1739                 zfcp_gid_pn_buffers_free(gid_pn);
1740 	}
1741 
1742  out:
1743 	return ret;
1744 }
1745 
1746 /**
1747  * zfcp_ns_gid_pn_handler - handler for GID_PN nameserver request
1748  * @data: unsigned long, contains pointer to struct zfcp_gid_pn_data
1749  */
1750 static void zfcp_ns_gid_pn_handler(unsigned long data)
1751 {
1752 	struct zfcp_port *port;
1753         struct zfcp_send_ct *ct;
1754 	struct ct_iu_gid_pn_req *ct_iu_req;
1755 	struct ct_iu_gid_pn_resp *ct_iu_resp;
1756         struct zfcp_gid_pn_data *gid_pn;
1757 
1758 
1759 	gid_pn = (struct zfcp_gid_pn_data *) data;
1760 	port = gid_pn->port;
1761         ct = &gid_pn->ct;
1762 	ct_iu_req = zfcp_sg_to_address(ct->req);
1763 	ct_iu_resp = zfcp_sg_to_address(ct->resp);
1764 
1765 	if (ct->status != 0)
1766 		goto failed;
1767 
1768 	if (zfcp_check_ct_response(&ct_iu_resp->header)) {
1769 		/* FIXME: do we need some specific erp entry points */
1770 		atomic_set_mask(ZFCP_STATUS_PORT_INVALID_WWPN, &port->status);
1771 		goto failed;
1772 	}
1773 	/* paranoia */
1774 	if (ct_iu_req->wwpn != port->wwpn) {
1775 		ZFCP_LOG_NORMAL("bug: wwpn 0x%016Lx returned by nameserver "
1776 				"lookup does not match expected wwpn 0x%016Lx "
1777 				"for adapter %s\n", ct_iu_req->wwpn, port->wwpn,
1778 				zfcp_get_busid_by_port(port));
1779 		goto mismatch;
1780 	}
1781 
1782 	/* looks like a valid d_id */
1783         port->d_id = ct_iu_resp->d_id & ZFCP_DID_MASK;
1784 	atomic_set_mask(ZFCP_STATUS_PORT_DID_DID, &port->status);
1785 	ZFCP_LOG_DEBUG("adapter %s:  wwpn=0x%016Lx ---> d_id=0x%08x\n",
1786 		       zfcp_get_busid_by_port(port), port->wwpn, port->d_id);
1787 	goto out;
1788 
1789  mismatch:
1790 	ZFCP_LOG_DEBUG("CT IUs do not match:\n");
1791 	ZFCP_HEX_DUMP(ZFCP_LOG_LEVEL_DEBUG, (char *) ct_iu_req,
1792 		      sizeof(struct ct_iu_gid_pn_req));
1793 	ZFCP_HEX_DUMP(ZFCP_LOG_LEVEL_DEBUG, (char *) ct_iu_resp,
1794 		      sizeof(struct ct_iu_gid_pn_resp));
1795 
1796  failed:
1797 	ZFCP_LOG_NORMAL("warning: failed gid_pn nameserver request for wwpn "
1798 			"0x%016Lx for adapter %s\n",
1799 			port->wwpn, zfcp_get_busid_by_port(port));
1800  out:
1801         zfcp_gid_pn_buffers_free(gid_pn);
1802 	return;
1803 }
1804 
1805 /* reject CT_IU reason codes acc. to FC-GS-4 */
1806 static const struct zfcp_rc_entry zfcp_ct_rc[] = {
1807 	{0x01, "invalid command code"},
1808 	{0x02, "invalid version level"},
1809 	{0x03, "logical error"},
1810 	{0x04, "invalid CT_IU size"},
1811 	{0x05, "logical busy"},
1812 	{0x07, "protocol error"},
1813 	{0x09, "unable to perform command request"},
1814 	{0x0b, "command not supported"},
1815 	{0x0d, "server not available"},
1816 	{0x0e, "session could not be established"},
1817 	{0xff, "vendor specific error"},
1818 	{0, NULL},
1819 };
1820 
1821 /* LS_RJT reason codes acc. to FC-FS */
1822 static const struct zfcp_rc_entry zfcp_ls_rjt_rc[] = {
1823 	{0x01, "invalid LS_Command code"},
1824 	{0x03, "logical error"},
1825 	{0x05, "logical busy"},
1826 	{0x07, "protocol error"},
1827 	{0x09, "unable to perform command request"},
1828 	{0x0b, "command not supported"},
1829 	{0x0e, "command already in progress"},
1830 	{0xff, "vendor specific error"},
1831 	{0, NULL},
1832 };
1833 
1834 /* reject reason codes according to FC-PH/FC-FS */
1835 static const struct zfcp_rc_entry zfcp_p_rjt_rc[] = {
1836 	{0x01, "invalid D_ID"},
1837 	{0x02, "invalid S_ID"},
1838 	{0x03, "Nx_Port not available, temporary"},
1839 	{0x04, "Nx_Port not available, permament"},
1840 	{0x05, "class not supported"},
1841 	{0x06, "delimiter usage error"},
1842 	{0x07, "TYPE not supported"},
1843 	{0x08, "invalid Link_Control"},
1844 	{0x09, "invalid R_CTL field"},
1845 	{0x0a, "invalid F_CTL field"},
1846 	{0x0b, "invalid OX_ID"},
1847 	{0x0c, "invalid RX_ID"},
1848 	{0x0d, "invalid SEQ_ID"},
1849 	{0x0e, "invalid DF_CTL"},
1850 	{0x0f, "invalid SEQ_CNT"},
1851 	{0x10, "invalid parameter field"},
1852 	{0x11, "exchange error"},
1853 	{0x12, "protocol error"},
1854 	{0x13, "incorrect length"},
1855 	{0x14, "unsupported ACK"},
1856 	{0x15, "class of service not supported by entity at FFFFFE"},
1857 	{0x16, "login required"},
1858 	{0x17, "excessive sequences attempted"},
1859 	{0x18, "unable to establish exchange"},
1860 	{0x1a, "fabric path not available"},
1861 	{0x1b, "invalid VC_ID (class 4)"},
1862 	{0x1c, "invalid CS_CTL field"},
1863 	{0x1d, "insufficient resources for VC (class 4)"},
1864 	{0x1f, "invalid class of service"},
1865 	{0x20, "preemption request rejected"},
1866 	{0x21, "preemption not enabled"},
1867 	{0x22, "multicast error"},
1868 	{0x23, "multicast error terminate"},
1869 	{0x24, "process login required"},
1870 	{0xff, "vendor specific reject"},
1871 	{0, NULL},
1872 };
1873 
1874 /**
1875  * zfcp_rc_description - return description for given reaon code
1876  * @code: reason code
1877  * @rc_table: table of reason codes and descriptions
1878  */
1879 static inline const char *
1880 zfcp_rc_description(u8 code, const struct zfcp_rc_entry *rc_table)
1881 {
1882 	const char *descr = "unknown reason code";
1883 
1884 	do {
1885 		if (code == rc_table->code) {
1886 			descr = rc_table->description;
1887 			break;
1888 		}
1889 		rc_table++;
1890 	} while (rc_table->code && rc_table->description);
1891 
1892 	return descr;
1893 }
1894 
1895 /**
1896  * zfcp_check_ct_response - evaluate reason code for CT_IU
1897  * @rjt: response payload to an CT_IU request
1898  * Return: 0 for accept CT_IU, 1 for reject CT_IU or invlid response code
1899  */
1900 int
1901 zfcp_check_ct_response(struct ct_hdr *rjt)
1902 {
1903 	if (rjt->cmd_rsp_code == ZFCP_CT_ACCEPT)
1904 		return 0;
1905 
1906 	if (rjt->cmd_rsp_code != ZFCP_CT_REJECT) {
1907 		ZFCP_LOG_NORMAL("error: invalid Generic Service command/"
1908 				"response code (0x%04hx)\n",
1909 				rjt->cmd_rsp_code);
1910 		return 1;
1911 	}
1912 
1913 	ZFCP_LOG_INFO("Generic Service command rejected\n");
1914 	ZFCP_LOG_INFO("%s (0x%02x, 0x%02x, 0x%02x)\n",
1915 		      zfcp_rc_description(rjt->reason_code, zfcp_ct_rc),
1916 		      (u32) rjt->reason_code, (u32) rjt->reason_code_expl,
1917 		      (u32) rjt->vendor_unique);
1918 
1919 	return 1;
1920 }
1921 
1922 /**
1923  * zfcp_print_els_rjt - print reject parameter and description for ELS reject
1924  * @rjt_par: reject parameter acc. to FC-PH/FC-FS
1925  * @rc_table: table of reason codes and descriptions
1926  */
1927 static inline void
1928 zfcp_print_els_rjt(struct zfcp_ls_rjt_par *rjt_par,
1929 		   const struct zfcp_rc_entry *rc_table)
1930 {
1931 	ZFCP_LOG_INFO("%s (%02x %02x %02x %02x)\n",
1932 		      zfcp_rc_description(rjt_par->reason_code, rc_table),
1933 		      (u32) rjt_par->action, (u32) rjt_par->reason_code,
1934 		      (u32) rjt_par->reason_expl, (u32) rjt_par->vendor_unique);
1935 }
1936 
1937 /**
1938  * zfcp_fsf_handle_els_rjt - evaluate status qualifier/reason code on ELS reject
1939  * @sq: status qualifier word
1940  * @rjt_par: reject parameter as described in FC-PH and FC-FS
1941  * Return: -EROMTEIO for LS_RJT, -EREMCHG for invalid D_ID, -EIO else
1942  */
1943 int
1944 zfcp_handle_els_rjt(u32 sq, struct zfcp_ls_rjt_par *rjt_par)
1945 {
1946 	int ret = -EIO;
1947 
1948 	if (sq == FSF_IOSTAT_NPORT_RJT) {
1949 		ZFCP_LOG_INFO("ELS rejected (P_RJT)\n");
1950 		zfcp_print_els_rjt(rjt_par, zfcp_p_rjt_rc);
1951 		/* invalid d_id */
1952 		if (rjt_par->reason_code == 0x01)
1953 			ret = -EREMCHG;
1954 	} else if (sq == FSF_IOSTAT_FABRIC_RJT) {
1955 		ZFCP_LOG_INFO("ELS rejected (F_RJT)\n");
1956 		zfcp_print_els_rjt(rjt_par, zfcp_p_rjt_rc);
1957 		/* invalid d_id */
1958 		if (rjt_par->reason_code == 0x01)
1959 			ret = -EREMCHG;
1960 	} else if (sq == FSF_IOSTAT_LS_RJT) {
1961 		ZFCP_LOG_INFO("ELS rejected (LS_RJT)\n");
1962 		zfcp_print_els_rjt(rjt_par, zfcp_ls_rjt_rc);
1963 		ret = -EREMOTEIO;
1964 	} else
1965 		ZFCP_LOG_INFO("unexpected SQ: 0x%02x\n", sq);
1966 
1967 	return ret;
1968 }
1969 
1970 #undef ZFCP_LOG_AREA
1971