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