[go: nahoru, domu]

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