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