Merge master.kernel.org:/pub/scm/linux/kernel/git/davej/x86
[pandora-kernel.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         kfree(sense_data);
454
455         return retval;
456 }
457
458
459 /**
460  * zfcp_sg_list_alloc - create a scatter-gather list of the specified size
461  * @sg_list: structure describing a scatter gather list
462  * @size: size of scatter-gather list
463  * Return: 0 on success, else -ENOMEM
464  *
465  * In sg_list->sg a pointer to the created scatter-gather list is returned,
466  * or NULL if we run out of memory. sg_list->count specifies the number of
467  * elements of the scatter-gather list. The maximum size of a single element
468  * in the scatter-gather list is PAGE_SIZE.
469  */
470 static inline int
471 zfcp_sg_list_alloc(struct zfcp_sg_list *sg_list, size_t size)
472 {
473         struct scatterlist *sg;
474         unsigned int i;
475         int retval = 0;
476         void *address;
477
478         BUG_ON(sg_list == NULL);
479
480         sg_list->count = size >> PAGE_SHIFT;
481         if (size & ~PAGE_MASK)
482                 sg_list->count++;
483         sg_list->sg = kmalloc(sg_list->count * sizeof(struct scatterlist),
484                               GFP_KERNEL);
485         if (sg_list->sg == NULL) {
486                 sg_list->count = 0;
487                 retval = -ENOMEM;
488                 goto out;
489         }
490         memset(sg_list->sg, 0, sg_list->count * sizeof(struct scatterlist));
491
492         for (i = 0, sg = sg_list->sg; i < sg_list->count; i++, sg++) {
493                 sg->length = min(size, PAGE_SIZE);
494                 sg->offset = 0;
495                 address = (void *) get_zeroed_page(GFP_KERNEL);
496                 if (address == NULL) {
497                         sg_list->count = i;
498                         zfcp_sg_list_free(sg_list);
499                         retval = -ENOMEM;
500                         goto out;
501                 }
502                 zfcp_address_to_sg(address, sg);
503                 size -= sg->length;
504         }
505
506  out:
507         return retval;
508 }
509
510
511 /**
512  * zfcp_sg_list_free - free memory of a scatter-gather list
513  * @sg_list: structure describing a scatter-gather list
514  *
515  * Memory for each element in the scatter-gather list is freed.
516  * Finally sg_list->sg is freed itself and sg_list->count is reset.
517  */
518 static inline void
519 zfcp_sg_list_free(struct zfcp_sg_list *sg_list)
520 {
521         struct scatterlist *sg;
522         unsigned int i;
523
524         BUG_ON(sg_list == NULL);
525
526         for (i = 0, sg = sg_list->sg; i < sg_list->count; i++, sg++)
527                 free_page((unsigned long) zfcp_sg_to_address(sg));
528
529         sg_list->count = 0;
530         kfree(sg_list->sg);
531 }
532
533 /**
534  * zfcp_sg_size - determine size of a scatter-gather list
535  * @sg: array of (struct scatterlist)
536  * @sg_count: elements in array
537  * Return: size of entire scatter-gather list
538  */
539 size_t
540 zfcp_sg_size(struct scatterlist *sg, unsigned int sg_count)
541 {
542         unsigned int i;
543         struct scatterlist *p;
544         size_t size;
545
546         size = 0;
547         for (i = 0, p = sg; i < sg_count; i++, p++) {
548                 BUG_ON(p == NULL);
549                 size += p->length;
550         }
551
552         return size;
553 }
554
555
556 /**
557  * zfcp_sg_list_copy_from_user -copy data from user space to scatter-gather list
558  * @sg_list: structure describing a scatter-gather list
559  * @user_buffer: pointer to buffer in user space
560  * @size: number of bytes to be copied
561  * Return: 0 on success, -EFAULT if copy_from_user fails.
562  */
563 static inline int
564 zfcp_sg_list_copy_from_user(struct zfcp_sg_list *sg_list,
565                             void __user *user_buffer,
566                             size_t size)
567 {
568         struct scatterlist *sg;
569         unsigned int length;
570         void *zfcp_buffer;
571         int retval = 0;
572
573         BUG_ON(sg_list == NULL);
574
575         if (zfcp_sg_size(sg_list->sg, sg_list->count) < size)
576                 return -EFAULT;
577
578         for (sg = sg_list->sg; size > 0; sg++) {
579                 length = min((unsigned int)size, sg->length);
580                 zfcp_buffer = zfcp_sg_to_address(sg);
581                 if (copy_from_user(zfcp_buffer, user_buffer, length)) {
582                         retval = -EFAULT;
583                         goto out;
584                 }
585                 user_buffer += length;
586                 size -= length;
587         }
588
589  out:
590         return retval;
591 }
592
593
594 /**
595  * zfcp_sg_list_copy_to_user - copy data from scatter-gather list to user space
596  * @user_buffer: pointer to buffer in user space
597  * @sg_list: structure describing a scatter-gather list
598  * @size: number of bytes to be copied
599  * Return: 0 on success, -EFAULT if copy_to_user fails
600  */
601 static inline int
602 zfcp_sg_list_copy_to_user(void __user  *user_buffer,
603                           struct zfcp_sg_list *sg_list,
604                           size_t size)
605 {
606         struct scatterlist *sg;
607         unsigned int length;
608         void *zfcp_buffer;
609         int retval = 0;
610
611         BUG_ON(sg_list == NULL);
612
613         if (zfcp_sg_size(sg_list->sg, sg_list->count) < size)
614                 return -EFAULT;
615
616         for (sg = sg_list->sg; size > 0; sg++) {
617                 length = min((unsigned int) size, sg->length);
618                 zfcp_buffer = zfcp_sg_to_address(sg);
619                 if (copy_to_user(user_buffer, zfcp_buffer, length)) {
620                         retval = -EFAULT;
621                         goto out;
622                 }
623                 user_buffer += length;
624                 size -= length;
625         }
626
627  out:
628         return retval;
629 }
630
631
632 #undef ZFCP_LOG_AREA
633
634 /****************************************************************/
635 /****** Functions for configuration/set-up of structures ********/
636 /****************************************************************/
637
638 #define ZFCP_LOG_AREA                   ZFCP_LOG_AREA_CONFIG
639
640 /**
641  * zfcp_get_unit_by_lun - find unit in unit list of port by FCP LUN
642  * @port: pointer to port to search for unit
643  * @fcp_lun: FCP LUN to search for
644  * Traverse list of all units of a port and return pointer to a unit
645  * with the given FCP LUN.
646  */
647 struct zfcp_unit *
648 zfcp_get_unit_by_lun(struct zfcp_port *port, fcp_lun_t fcp_lun)
649 {
650         struct zfcp_unit *unit;
651         int found = 0;
652
653         list_for_each_entry(unit, &port->unit_list_head, list) {
654                 if ((unit->fcp_lun == fcp_lun) &&
655                     !atomic_test_mask(ZFCP_STATUS_COMMON_REMOVE, &unit->status))
656                 {
657                         found = 1;
658                         break;
659                 }
660         }
661         return found ? unit : NULL;
662 }
663
664 /**
665  * zfcp_get_port_by_wwpn - find port in port list of adapter by wwpn
666  * @adapter: pointer to adapter to search for port
667  * @wwpn: wwpn to search for
668  * Traverse list of all ports of an adapter and return pointer to a port
669  * with the given wwpn.
670  */
671 struct zfcp_port *
672 zfcp_get_port_by_wwpn(struct zfcp_adapter *adapter, wwn_t wwpn)
673 {
674         struct zfcp_port *port;
675         int found = 0;
676
677         list_for_each_entry(port, &adapter->port_list_head, list) {
678                 if ((port->wwpn == wwpn) &&
679                     !(atomic_read(&port->status) &
680                       (ZFCP_STATUS_PORT_NO_WWPN | ZFCP_STATUS_COMMON_REMOVE))) {
681                         found = 1;
682                         break;
683                 }
684         }
685         return found ? port : NULL;
686 }
687
688 /**
689  * zfcp_get_port_by_did - find port in port list of adapter by d_id
690  * @adapter: pointer to adapter to search for port
691  * @d_id: d_id to search for
692  * Traverse list of all ports of an adapter and return pointer to a port
693  * with the given d_id.
694  */
695 struct zfcp_port *
696 zfcp_get_port_by_did(struct zfcp_adapter *adapter, u32 d_id)
697 {
698         struct zfcp_port *port;
699         int found = 0;
700
701         list_for_each_entry(port, &adapter->port_list_head, list) {
702                 if ((port->d_id == d_id) &&
703                     !atomic_test_mask(ZFCP_STATUS_COMMON_REMOVE, &port->status))
704                 {
705                         found = 1;
706                         break;
707                 }
708         }
709         return found ? port : NULL;
710 }
711
712 /**
713  * zfcp_get_adapter_by_busid - find adpater in adapter list by bus_id
714  * @bus_id: bus_id to search for
715  * Traverse list of all adapters and return pointer to an adapter
716  * with the given bus_id.
717  */
718 struct zfcp_adapter *
719 zfcp_get_adapter_by_busid(char *bus_id)
720 {
721         struct zfcp_adapter *adapter;
722         int found = 0;
723
724         list_for_each_entry(adapter, &zfcp_data.adapter_list_head, list) {
725                 if ((strncmp(bus_id, zfcp_get_busid_by_adapter(adapter),
726                              BUS_ID_SIZE) == 0) &&
727                     !atomic_test_mask(ZFCP_STATUS_COMMON_REMOVE,
728                                       &adapter->status)){
729                         found = 1;
730                         break;
731                 }
732         }
733         return found ? adapter : NULL;
734 }
735
736 /**
737  * zfcp_unit_enqueue - enqueue unit to unit list of a port.
738  * @port: pointer to port where unit is added
739  * @fcp_lun: FCP LUN of unit to be enqueued
740  * Return: pointer to enqueued unit on success, NULL on error
741  * Locks: config_sema must be held to serialize changes to the unit list
742  *
743  * Sets up some unit internal structures and creates sysfs entry.
744  */
745 struct zfcp_unit *
746 zfcp_unit_enqueue(struct zfcp_port *port, fcp_lun_t fcp_lun)
747 {
748         struct zfcp_unit *unit, *tmp_unit;
749         scsi_lun_t scsi_lun;
750         int found;
751
752         /*
753          * check that there is no unit with this FCP_LUN already in list
754          * and enqueue it.
755          * Note: Unlike for the adapter and the port, this is an error
756          */
757         read_lock_irq(&zfcp_data.config_lock);
758         unit = zfcp_get_unit_by_lun(port, fcp_lun);
759         read_unlock_irq(&zfcp_data.config_lock);
760         if (unit)
761                 return NULL;
762
763         unit = kmalloc(sizeof (struct zfcp_unit), GFP_KERNEL);
764         if (!unit)
765                 return NULL;
766         memset(unit, 0, sizeof (struct zfcp_unit));
767
768         /* initialise reference count stuff */
769         atomic_set(&unit->refcount, 0);
770         init_waitqueue_head(&unit->remove_wq);
771
772         unit->port = port;
773         unit->fcp_lun = fcp_lun;
774
775         /* setup for sysfs registration */
776         snprintf(unit->sysfs_device.bus_id, BUS_ID_SIZE, "0x%016llx", fcp_lun);
777         unit->sysfs_device.parent = &port->sysfs_device;
778         unit->sysfs_device.release = zfcp_sysfs_unit_release;
779         dev_set_drvdata(&unit->sysfs_device, unit);
780
781         /* mark unit unusable as long as sysfs registration is not complete */
782         atomic_set_mask(ZFCP_STATUS_COMMON_REMOVE, &unit->status);
783
784         if (device_register(&unit->sysfs_device)) {
785                 kfree(unit);
786                 return NULL;
787         }
788
789         if (zfcp_sysfs_unit_create_files(&unit->sysfs_device)) {
790                 device_unregister(&unit->sysfs_device);
791                 return NULL;
792         }
793
794         zfcp_unit_get(unit);
795
796         scsi_lun = 0;
797         found = 0;
798         write_lock_irq(&zfcp_data.config_lock);
799         list_for_each_entry(tmp_unit, &port->unit_list_head, list) {
800                 if (tmp_unit->scsi_lun != scsi_lun) {
801                         found = 1;
802                         break;
803                 }
804                 scsi_lun++;
805         }
806         unit->scsi_lun = scsi_lun;
807         if (found)
808                 list_add_tail(&unit->list, &tmp_unit->list);
809         else
810                 list_add_tail(&unit->list, &port->unit_list_head);
811         atomic_clear_mask(ZFCP_STATUS_COMMON_REMOVE, &unit->status);
812         atomic_set_mask(ZFCP_STATUS_COMMON_RUNNING, &unit->status);
813         write_unlock_irq(&zfcp_data.config_lock);
814
815         port->units++;
816         zfcp_port_get(port);
817
818         return unit;
819 }
820
821 void
822 zfcp_unit_dequeue(struct zfcp_unit *unit)
823 {
824         zfcp_unit_wait(unit);
825         write_lock_irq(&zfcp_data.config_lock);
826         list_del(&unit->list);
827         write_unlock_irq(&zfcp_data.config_lock);
828         unit->port->units--;
829         zfcp_port_put(unit->port);
830         zfcp_sysfs_unit_remove_files(&unit->sysfs_device);
831         device_unregister(&unit->sysfs_device);
832 }
833
834 static void *
835 zfcp_mempool_alloc(gfp_t gfp_mask, void *size)
836 {
837         return kmalloc((size_t) size, gfp_mask);
838 }
839
840 static void
841 zfcp_mempool_free(void *element, void *size)
842 {
843         kfree(element);
844 }
845
846 /*
847  * Allocates a combined QTCB/fsf_req buffer for erp actions and fcp/SCSI
848  * commands.
849  * It also genrates fcp-nameserver request/response buffer and unsolicited 
850  * status read fsf_req buffers.
851  *
852  * locks:       must only be called with zfcp_data.config_sema taken
853  */
854 static int
855 zfcp_allocate_low_mem_buffers(struct zfcp_adapter *adapter)
856 {
857         adapter->pool.fsf_req_erp =
858                 mempool_create(ZFCP_POOL_FSF_REQ_ERP_NR,
859                                zfcp_mempool_alloc, zfcp_mempool_free, (void *)
860                                sizeof(struct zfcp_fsf_req_pool_element));
861
862         if (NULL == adapter->pool.fsf_req_erp)
863                 return -ENOMEM;
864
865         adapter->pool.fsf_req_scsi =
866                 mempool_create(ZFCP_POOL_FSF_REQ_SCSI_NR,
867                                zfcp_mempool_alloc, zfcp_mempool_free, (void *)
868                                sizeof(struct zfcp_fsf_req_pool_element));
869
870         if (NULL == adapter->pool.fsf_req_scsi)
871                 return -ENOMEM;
872
873         adapter->pool.fsf_req_abort =
874                 mempool_create(ZFCP_POOL_FSF_REQ_ABORT_NR,
875                                zfcp_mempool_alloc, zfcp_mempool_free, (void *)
876                                sizeof(struct zfcp_fsf_req_pool_element));
877
878         if (NULL == adapter->pool.fsf_req_abort)
879                 return -ENOMEM;
880
881         adapter->pool.fsf_req_status_read =
882                 mempool_create(ZFCP_POOL_STATUS_READ_NR,
883                                zfcp_mempool_alloc, zfcp_mempool_free,
884                                (void *) sizeof(struct zfcp_fsf_req));
885
886         if (NULL == adapter->pool.fsf_req_status_read)
887                 return -ENOMEM;
888
889         adapter->pool.data_status_read =
890                 mempool_create(ZFCP_POOL_STATUS_READ_NR,
891                                zfcp_mempool_alloc, zfcp_mempool_free,
892                                (void *) sizeof(struct fsf_status_read_buffer));
893
894         if (NULL == adapter->pool.data_status_read)
895                 return -ENOMEM;
896
897         adapter->pool.data_gid_pn =
898                 mempool_create(ZFCP_POOL_DATA_GID_PN_NR,
899                                zfcp_mempool_alloc, zfcp_mempool_free, (void *)
900                                sizeof(struct zfcp_gid_pn_data));
901
902         if (NULL == adapter->pool.data_gid_pn)
903                 return -ENOMEM;
904
905         return 0;
906 }
907
908 /**
909  * zfcp_free_low_mem_buffers - free memory pools of an adapter
910  * @adapter: pointer to zfcp_adapter for which memory pools should be freed
911  * locking:  zfcp_data.config_sema must be held
912  */
913 static void
914 zfcp_free_low_mem_buffers(struct zfcp_adapter *adapter)
915 {
916         if (adapter->pool.fsf_req_erp)
917                 mempool_destroy(adapter->pool.fsf_req_erp);
918         if (adapter->pool.fsf_req_scsi)
919                 mempool_destroy(adapter->pool.fsf_req_scsi);
920         if (adapter->pool.fsf_req_abort)
921                 mempool_destroy(adapter->pool.fsf_req_abort);
922         if (adapter->pool.fsf_req_status_read)
923                 mempool_destroy(adapter->pool.fsf_req_status_read);
924         if (adapter->pool.data_status_read)
925                 mempool_destroy(adapter->pool.data_status_read);
926         if (adapter->pool.data_gid_pn)
927                 mempool_destroy(adapter->pool.data_gid_pn);
928 }
929
930 void
931 zfcp_dummy_release(struct device *dev)
932 {
933         return;
934 }
935
936 /*
937  * Enqueues an adapter at the end of the adapter list in the driver data.
938  * All adapter internal structures are set up.
939  * Proc-fs entries are also created.
940  *
941  * returns:     0             if a new adapter was successfully enqueued
942  *              ZFCP_KNOWN    if an adapter with this devno was already present
943  *              -ENOMEM       if alloc failed
944  * locks:       config_sema must be held to serialise changes to the adapter list
945  */
946 struct zfcp_adapter *
947 zfcp_adapter_enqueue(struct ccw_device *ccw_device)
948 {
949         int retval = 0;
950         struct zfcp_adapter *adapter;
951
952         /*
953          * Note: It is safe to release the list_lock, as any list changes 
954          * are protected by the config_sema, which must be held to get here
955          */
956
957         /* try to allocate new adapter data structure (zeroed) */
958         adapter = kmalloc(sizeof (struct zfcp_adapter), GFP_KERNEL);
959         if (!adapter) {
960                 ZFCP_LOG_INFO("error: allocation of base adapter "
961                               "structure failed\n");
962                 goto out;
963         }
964         memset(adapter, 0, sizeof (struct zfcp_adapter));
965
966         ccw_device->handler = NULL;
967
968         /* save ccw_device pointer */
969         adapter->ccw_device = ccw_device;
970
971         retval = zfcp_qdio_allocate_queues(adapter);
972         if (retval)
973                 goto queues_alloc_failed;
974
975         retval = zfcp_qdio_allocate(adapter);
976         if (retval)
977                 goto qdio_allocate_failed;
978
979         retval = zfcp_allocate_low_mem_buffers(adapter);
980         if (retval) {
981                 ZFCP_LOG_INFO("error: pool allocation failed\n");
982                 goto failed_low_mem_buffers;
983         }
984
985         /* initialise reference count stuff */
986         atomic_set(&adapter->refcount, 0);
987         init_waitqueue_head(&adapter->remove_wq);
988
989         /* initialise list of ports */
990         INIT_LIST_HEAD(&adapter->port_list_head);
991
992         /* initialise list of ports to be removed */
993         INIT_LIST_HEAD(&adapter->port_remove_lh);
994
995         /* initialize list of fsf requests */
996         spin_lock_init(&adapter->fsf_req_list_lock);
997         INIT_LIST_HEAD(&adapter->fsf_req_list_head);
998
999         /* initialize debug locks */
1000
1001         spin_lock_init(&adapter->erp_dbf_lock);
1002         spin_lock_init(&adapter->hba_dbf_lock);
1003         spin_lock_init(&adapter->san_dbf_lock);
1004         spin_lock_init(&adapter->scsi_dbf_lock);
1005
1006         /* initialize error recovery stuff */
1007
1008         rwlock_init(&adapter->erp_lock);
1009         sema_init(&adapter->erp_ready_sem, 0);
1010         INIT_LIST_HEAD(&adapter->erp_ready_head);
1011         INIT_LIST_HEAD(&adapter->erp_running_head);
1012
1013         /* initialize abort lock */
1014         rwlock_init(&adapter->abort_lock);
1015
1016         /* initialise some erp stuff */
1017         init_waitqueue_head(&adapter->erp_thread_wqh);
1018         init_waitqueue_head(&adapter->erp_done_wqh);
1019
1020         /* initialize lock of associated request queue */
1021         rwlock_init(&adapter->request_queue.queue_lock);
1022
1023         /* intitialise SCSI ER timer */
1024         init_timer(&adapter->scsi_er_timer);
1025
1026         /* set FC service class used per default */
1027         adapter->fc_service_class = ZFCP_FC_SERVICE_CLASS_DEFAULT;
1028
1029         sprintf(adapter->name, "%s", zfcp_get_busid_by_adapter(adapter));
1030         ASCEBC(adapter->name, strlen(adapter->name));
1031
1032         /* mark adapter unusable as long as sysfs registration is not complete */
1033         atomic_set_mask(ZFCP_STATUS_COMMON_REMOVE, &adapter->status);
1034
1035         adapter->ccw_device = ccw_device;
1036         dev_set_drvdata(&ccw_device->dev, adapter);
1037
1038         if (zfcp_sysfs_adapter_create_files(&ccw_device->dev))
1039                 goto sysfs_failed;
1040
1041         adapter->generic_services.parent = &adapter->ccw_device->dev;
1042         adapter->generic_services.release = zfcp_dummy_release;
1043         snprintf(adapter->generic_services.bus_id, BUS_ID_SIZE,
1044                  "generic_services");
1045
1046         if (device_register(&adapter->generic_services))
1047                 goto generic_services_failed;
1048
1049         /* put allocated adapter at list tail */
1050         write_lock_irq(&zfcp_data.config_lock);
1051         atomic_clear_mask(ZFCP_STATUS_COMMON_REMOVE, &adapter->status);
1052         list_add_tail(&adapter->list, &zfcp_data.adapter_list_head);
1053         write_unlock_irq(&zfcp_data.config_lock);
1054
1055         zfcp_data.adapters++;
1056
1057         goto out;
1058
1059  generic_services_failed:
1060         zfcp_sysfs_adapter_remove_files(&adapter->ccw_device->dev);
1061  sysfs_failed:
1062         dev_set_drvdata(&ccw_device->dev, NULL);
1063  failed_low_mem_buffers:
1064         zfcp_free_low_mem_buffers(adapter);
1065         if (qdio_free(ccw_device) != 0)
1066                 ZFCP_LOG_NORMAL("bug: qdio_free for adapter %s failed\n",
1067                                 zfcp_get_busid_by_adapter(adapter));
1068  qdio_allocate_failed:
1069         zfcp_qdio_free_queues(adapter);
1070  queues_alloc_failed:
1071         kfree(adapter);
1072         adapter = NULL;
1073  out:
1074         return adapter;
1075 }
1076
1077 /*
1078  * returns:     0 - struct zfcp_adapter  data structure successfully removed
1079  *              !0 - struct zfcp_adapter  data structure could not be removed
1080  *                      (e.g. still used)
1081  * locks:       adapter list write lock is assumed to be held by caller
1082  *              adapter->fsf_req_list_lock is taken and released within this 
1083  *              function and must not be held on entry
1084  */
1085 void
1086 zfcp_adapter_dequeue(struct zfcp_adapter *adapter)
1087 {
1088         int retval = 0;
1089         unsigned long flags;
1090
1091         device_unregister(&adapter->generic_services);
1092         zfcp_sysfs_adapter_remove_files(&adapter->ccw_device->dev);
1093         dev_set_drvdata(&adapter->ccw_device->dev, NULL);
1094         /* sanity check: no pending FSF requests */
1095         spin_lock_irqsave(&adapter->fsf_req_list_lock, flags);
1096         retval = !list_empty(&adapter->fsf_req_list_head);
1097         spin_unlock_irqrestore(&adapter->fsf_req_list_lock, flags);
1098         if (retval) {
1099                 ZFCP_LOG_NORMAL("bug: adapter %s (%p) still in use, "
1100                                 "%i requests outstanding\n",
1101                                 zfcp_get_busid_by_adapter(adapter), adapter,
1102                                 atomic_read(&adapter->fsf_reqs_active));
1103                 retval = -EBUSY;
1104                 goto out;
1105         }
1106
1107         /* remove specified adapter data structure from list */
1108         write_lock_irq(&zfcp_data.config_lock);
1109         list_del(&adapter->list);
1110         write_unlock_irq(&zfcp_data.config_lock);
1111
1112         /* decrease number of adapters in list */
1113         zfcp_data.adapters--;
1114
1115         ZFCP_LOG_TRACE("adapter %s (%p) removed from list, "
1116                        "%i adapters still in list\n",
1117                        zfcp_get_busid_by_adapter(adapter),
1118                        adapter, zfcp_data.adapters);
1119
1120         retval = qdio_free(adapter->ccw_device);
1121         if (retval)
1122                 ZFCP_LOG_NORMAL("bug: qdio_free for adapter %s failed\n",
1123                                 zfcp_get_busid_by_adapter(adapter));
1124
1125         zfcp_free_low_mem_buffers(adapter);
1126         /* free memory of adapter data structure and queues */
1127         zfcp_qdio_free_queues(adapter);
1128         kfree(adapter->fc_stats);
1129         kfree(adapter->stats_reset_data);
1130         ZFCP_LOG_TRACE("freeing adapter structure\n");
1131         kfree(adapter);
1132  out:
1133         return;
1134 }
1135
1136 /**
1137  * zfcp_port_enqueue - enqueue port to port list of adapter
1138  * @adapter: adapter where remote port is added
1139  * @wwpn: WWPN of the remote port to be enqueued
1140  * @status: initial status for the port
1141  * @d_id: destination id of the remote port to be enqueued
1142  * Return: pointer to enqueued port on success, NULL on error
1143  * Locks: config_sema must be held to serialize changes to the port list
1144  *
1145  * All port internal structures are set up and the sysfs entry is generated.
1146  * d_id is used to enqueue ports with a well known address like the Directory
1147  * Service for nameserver lookup.
1148  */
1149 struct zfcp_port *
1150 zfcp_port_enqueue(struct zfcp_adapter *adapter, wwn_t wwpn, u32 status,
1151                   u32 d_id)
1152 {
1153         struct zfcp_port *port;
1154         int check_wwpn;
1155
1156         check_wwpn = !(status & ZFCP_STATUS_PORT_NO_WWPN);
1157         /*
1158          * check that there is no port with this WWPN already in list
1159          */
1160         if (check_wwpn) {
1161                 read_lock_irq(&zfcp_data.config_lock);
1162                 port = zfcp_get_port_by_wwpn(adapter, wwpn);
1163                 read_unlock_irq(&zfcp_data.config_lock);
1164                 if (port)
1165                         return NULL;
1166         }
1167
1168         port = kmalloc(sizeof (struct zfcp_port), GFP_KERNEL);
1169         if (!port)
1170                 return NULL;
1171         memset(port, 0, sizeof (struct zfcp_port));
1172
1173         /* initialise reference count stuff */
1174         atomic_set(&port->refcount, 0);
1175         init_waitqueue_head(&port->remove_wq);
1176
1177         INIT_LIST_HEAD(&port->unit_list_head);
1178         INIT_LIST_HEAD(&port->unit_remove_lh);
1179
1180         port->adapter = adapter;
1181
1182         if (check_wwpn)
1183                 port->wwpn = wwpn;
1184
1185         atomic_set_mask(status, &port->status);
1186
1187         /* setup for sysfs registration */
1188         if (status & ZFCP_STATUS_PORT_WKA) {
1189                 switch (d_id) {
1190                 case ZFCP_DID_DIRECTORY_SERVICE:
1191                         snprintf(port->sysfs_device.bus_id, BUS_ID_SIZE,
1192                                  "directory");
1193                         break;
1194                 case ZFCP_DID_MANAGEMENT_SERVICE:
1195                         snprintf(port->sysfs_device.bus_id, BUS_ID_SIZE,
1196                                  "management");
1197                         break;
1198                 case ZFCP_DID_KEY_DISTRIBUTION_SERVICE:
1199                         snprintf(port->sysfs_device.bus_id, BUS_ID_SIZE,
1200                                  "key_distribution");
1201                         break;
1202                 case ZFCP_DID_ALIAS_SERVICE:
1203                         snprintf(port->sysfs_device.bus_id, BUS_ID_SIZE,
1204                                  "alias");
1205                         break;
1206                 case ZFCP_DID_TIME_SERVICE:
1207                         snprintf(port->sysfs_device.bus_id, BUS_ID_SIZE,
1208                                  "time");
1209                         break;
1210                 default:
1211                         kfree(port);
1212                         return NULL;
1213                 }
1214                 port->d_id = d_id;
1215                 port->sysfs_device.parent = &adapter->generic_services;
1216         } else {
1217                 snprintf(port->sysfs_device.bus_id,
1218                          BUS_ID_SIZE, "0x%016llx", wwpn);
1219                 port->sysfs_device.parent = &adapter->ccw_device->dev;
1220         }
1221         port->sysfs_device.release = zfcp_sysfs_port_release;
1222         dev_set_drvdata(&port->sysfs_device, port);
1223
1224         /* mark port unusable as long as sysfs registration is not complete */
1225         atomic_set_mask(ZFCP_STATUS_COMMON_REMOVE, &port->status);
1226
1227         if (device_register(&port->sysfs_device)) {
1228                 kfree(port);
1229                 return NULL;
1230         }
1231
1232         if (zfcp_sysfs_port_create_files(&port->sysfs_device, status)) {
1233                 device_unregister(&port->sysfs_device);
1234                 return NULL;
1235         }
1236
1237         zfcp_port_get(port);
1238
1239         write_lock_irq(&zfcp_data.config_lock);
1240         list_add_tail(&port->list, &adapter->port_list_head);
1241         atomic_clear_mask(ZFCP_STATUS_COMMON_REMOVE, &port->status);
1242         atomic_set_mask(ZFCP_STATUS_COMMON_RUNNING, &port->status);
1243         if (d_id == ZFCP_DID_DIRECTORY_SERVICE)
1244                 if (!adapter->nameserver_port)
1245                         adapter->nameserver_port = port;
1246         adapter->ports++;
1247         write_unlock_irq(&zfcp_data.config_lock);
1248
1249         zfcp_adapter_get(adapter);
1250
1251         return port;
1252 }
1253
1254 void
1255 zfcp_port_dequeue(struct zfcp_port *port)
1256 {
1257         zfcp_port_wait(port);
1258         write_lock_irq(&zfcp_data.config_lock);
1259         list_del(&port->list);
1260         port->adapter->ports--;
1261         write_unlock_irq(&zfcp_data.config_lock);
1262         if (port->rport)
1263                 fc_remote_port_delete(port->rport);
1264         port->rport = NULL;
1265         zfcp_adapter_put(port->adapter);
1266         zfcp_sysfs_port_remove_files(&port->sysfs_device,
1267                                      atomic_read(&port->status));
1268         device_unregister(&port->sysfs_device);
1269 }
1270
1271 /* Enqueues a nameserver port */
1272 int
1273 zfcp_nameserver_enqueue(struct zfcp_adapter *adapter)
1274 {
1275         struct zfcp_port *port;
1276
1277         port = zfcp_port_enqueue(adapter, 0, ZFCP_STATUS_PORT_WKA,
1278                                  ZFCP_DID_DIRECTORY_SERVICE);
1279         if (!port) {
1280                 ZFCP_LOG_INFO("error: enqueue of nameserver port for "
1281                               "adapter %s failed\n",
1282                               zfcp_get_busid_by_adapter(adapter));
1283                 return -ENXIO;
1284         }
1285         zfcp_port_put(port);
1286
1287         return 0;
1288 }
1289
1290 #undef ZFCP_LOG_AREA
1291
1292 /****************************************************************/
1293 /******* Fibre Channel Standard related Functions  **************/
1294 /****************************************************************/
1295
1296 #define ZFCP_LOG_AREA                   ZFCP_LOG_AREA_FC
1297
1298 void
1299 zfcp_fsf_incoming_els_rscn(struct zfcp_adapter *adapter,
1300                            struct fsf_status_read_buffer *status_buffer)
1301 {
1302         struct fcp_rscn_head *fcp_rscn_head;
1303         struct fcp_rscn_element *fcp_rscn_element;
1304         struct zfcp_port *port;
1305         u16 i;
1306         u16 no_entries;
1307         u32 range_mask;
1308         unsigned long flags;
1309
1310         fcp_rscn_head = (struct fcp_rscn_head *) status_buffer->payload;
1311         fcp_rscn_element = (struct fcp_rscn_element *) status_buffer->payload;
1312
1313         /* see FC-FS */
1314         no_entries = (fcp_rscn_head->payload_len / 4);
1315
1316         for (i = 1; i < no_entries; i++) {
1317                 /* skip head and start with 1st element */
1318                 fcp_rscn_element++;
1319                 switch (fcp_rscn_element->addr_format) {
1320                 case ZFCP_PORT_ADDRESS:
1321                         range_mask = ZFCP_PORTS_RANGE_PORT;
1322                         break;
1323                 case ZFCP_AREA_ADDRESS:
1324                         range_mask = ZFCP_PORTS_RANGE_AREA;
1325                         break;
1326                 case ZFCP_DOMAIN_ADDRESS:
1327                         range_mask = ZFCP_PORTS_RANGE_DOMAIN;
1328                         break;
1329                 case ZFCP_FABRIC_ADDRESS:
1330                         range_mask = ZFCP_PORTS_RANGE_FABRIC;
1331                         break;
1332                 default:
1333                         ZFCP_LOG_INFO("incoming RSCN with unknown "
1334                                       "address format\n");
1335                         continue;
1336                 }
1337                 read_lock_irqsave(&zfcp_data.config_lock, flags);
1338                 list_for_each_entry(port, &adapter->port_list_head, list) {
1339                         if (atomic_test_mask
1340                             (ZFCP_STATUS_PORT_WKA, &port->status))
1341                                 continue;
1342                         /* Do we know this port? If not skip it. */
1343                         if (!atomic_test_mask
1344                             (ZFCP_STATUS_PORT_DID_DID, &port->status)) {
1345                                 ZFCP_LOG_INFO("incoming RSCN, trying to open "
1346                                               "port 0x%016Lx\n", port->wwpn);
1347                                 zfcp_erp_port_reopen(port,
1348                                                      ZFCP_STATUS_COMMON_ERP_FAILED);
1349                                 continue;
1350                         }
1351
1352                         /*
1353                          * FIXME: race: d_id might being invalidated
1354                          * (...DID_DID reset)
1355                          */
1356                         if ((port->d_id & range_mask)
1357                             == (fcp_rscn_element->nport_did & range_mask)) {
1358                                 ZFCP_LOG_TRACE("reopen did 0x%08x\n",
1359                                                fcp_rscn_element->nport_did);
1360                                 /*
1361                                  * Unfortunately, an RSCN does not specify the
1362                                  * type of change a target underwent. We assume
1363                                  * that it makes sense to reopen the link.
1364                                  * FIXME: Shall we try to find out more about
1365                                  * the target and link state before closing it?
1366                                  * How to accomplish this? (nameserver?)
1367                                  * Where would such code be put in?
1368                                  * (inside or outside erp)
1369                                  */
1370                                 ZFCP_LOG_INFO("incoming RSCN, trying to open "
1371                                               "port 0x%016Lx\n", port->wwpn);
1372                                 zfcp_test_link(port);
1373                         }
1374                 }
1375                 read_unlock_irqrestore(&zfcp_data.config_lock, flags);
1376         }
1377 }
1378
1379 static void
1380 zfcp_fsf_incoming_els_plogi(struct zfcp_adapter *adapter,
1381                             struct fsf_status_read_buffer *status_buffer)
1382 {
1383         logi *els_logi = (logi *) status_buffer->payload;
1384         struct zfcp_port *port;
1385         unsigned long flags;
1386
1387         read_lock_irqsave(&zfcp_data.config_lock, flags);
1388         list_for_each_entry(port, &adapter->port_list_head, list) {
1389                 if (port->wwpn == (*(wwn_t *) & els_logi->nport_wwn))
1390                         break;
1391         }
1392         read_unlock_irqrestore(&zfcp_data.config_lock, flags);
1393
1394         if (!port || (port->wwpn != (*(wwn_t *) & els_logi->nport_wwn))) {
1395                 ZFCP_LOG_DEBUG("ignored incoming PLOGI for nonexisting port "
1396                                "with d_id 0x%08x on adapter %s\n",
1397                                status_buffer->d_id,
1398                                zfcp_get_busid_by_adapter(adapter));
1399         } else {
1400                 zfcp_erp_port_forced_reopen(port, 0);
1401         }
1402 }
1403
1404 static void
1405 zfcp_fsf_incoming_els_logo(struct zfcp_adapter *adapter,
1406                            struct fsf_status_read_buffer *status_buffer)
1407 {
1408         struct fcp_logo *els_logo = (struct fcp_logo *) status_buffer->payload;
1409         struct zfcp_port *port;
1410         unsigned long flags;
1411
1412         read_lock_irqsave(&zfcp_data.config_lock, flags);
1413         list_for_each_entry(port, &adapter->port_list_head, list) {
1414                 if (port->wwpn == els_logo->nport_wwpn)
1415                         break;
1416         }
1417         read_unlock_irqrestore(&zfcp_data.config_lock, flags);
1418
1419         if (!port || (port->wwpn != els_logo->nport_wwpn)) {
1420                 ZFCP_LOG_DEBUG("ignored incoming LOGO for nonexisting port "
1421                                "with d_id 0x%08x on adapter %s\n",
1422                                status_buffer->d_id,
1423                                zfcp_get_busid_by_adapter(adapter));
1424         } else {
1425                 zfcp_erp_port_forced_reopen(port, 0);
1426         }
1427 }
1428
1429 static void
1430 zfcp_fsf_incoming_els_unknown(struct zfcp_adapter *adapter,
1431                               struct fsf_status_read_buffer *status_buffer)
1432 {
1433         ZFCP_LOG_NORMAL("warning: unknown incoming ELS 0x%08x "
1434                         "for adapter %s\n", *(u32 *) (status_buffer->payload),
1435                         zfcp_get_busid_by_adapter(adapter));
1436
1437 }
1438
1439 void
1440 zfcp_fsf_incoming_els(struct zfcp_fsf_req *fsf_req)
1441 {
1442         struct fsf_status_read_buffer *status_buffer;
1443         u32 els_type;
1444         struct zfcp_adapter *adapter;
1445
1446         status_buffer = (struct fsf_status_read_buffer *) fsf_req->data;
1447         els_type = *(u32 *) (status_buffer->payload);
1448         adapter = fsf_req->adapter;
1449
1450         zfcp_san_dbf_event_incoming_els(fsf_req);
1451         if (els_type == LS_PLOGI)
1452                 zfcp_fsf_incoming_els_plogi(adapter, status_buffer);
1453         else if (els_type == LS_LOGO)
1454                 zfcp_fsf_incoming_els_logo(adapter, status_buffer);
1455         else if ((els_type & 0xffff0000) == LS_RSCN)
1456                 /* we are only concerned with the command, not the length */
1457                 zfcp_fsf_incoming_els_rscn(adapter, status_buffer);
1458         else
1459                 zfcp_fsf_incoming_els_unknown(adapter, status_buffer);
1460 }
1461
1462
1463 /**
1464  * zfcp_gid_pn_buffers_alloc - allocate buffers for GID_PN nameserver request
1465  * @gid_pn: pointer to return pointer to struct zfcp_gid_pn_data
1466  * @pool: pointer to mempool_t if non-null memory pool is used for allocation
1467  */
1468 static int
1469 zfcp_gid_pn_buffers_alloc(struct zfcp_gid_pn_data **gid_pn, mempool_t *pool)
1470 {
1471         struct zfcp_gid_pn_data *data;
1472
1473         if (pool != NULL) {
1474                 data = mempool_alloc(pool, GFP_ATOMIC);
1475                 if (likely(data != NULL)) {
1476                         data->ct.pool = pool;
1477                 }
1478         } else {
1479                 data = kmalloc(sizeof(struct zfcp_gid_pn_data), GFP_ATOMIC);
1480         }
1481
1482         if (NULL == data)
1483                 return -ENOMEM;
1484
1485         memset(data, 0, sizeof(*data));
1486         data->ct.req = &data->req;
1487         data->ct.resp = &data->resp;
1488         data->ct.req_count = data->ct.resp_count = 1;
1489         zfcp_address_to_sg(&data->ct_iu_req, &data->req);
1490         zfcp_address_to_sg(&data->ct_iu_resp, &data->resp);
1491         data->req.length = sizeof(struct ct_iu_gid_pn_req);
1492         data->resp.length = sizeof(struct ct_iu_gid_pn_resp);
1493
1494         *gid_pn = data;
1495         return 0;
1496 }
1497
1498 /**
1499  * zfcp_gid_pn_buffers_free - free buffers for GID_PN nameserver request
1500  * @gid_pn: pointer to struct zfcp_gid_pn_data which has to be freed
1501  */
1502 static void
1503 zfcp_gid_pn_buffers_free(struct zfcp_gid_pn_data *gid_pn)
1504 {
1505         if ((gid_pn->ct.pool != 0))
1506                 mempool_free(gid_pn, gid_pn->ct.pool);
1507         else
1508                 kfree(gid_pn);
1509
1510         return;
1511 }
1512
1513 /**
1514  * zfcp_ns_gid_pn_request - initiate GID_PN nameserver request
1515  * @erp_action: pointer to zfcp_erp_action where GID_PN request is needed
1516  */
1517 int
1518 zfcp_ns_gid_pn_request(struct zfcp_erp_action *erp_action)
1519 {
1520         int ret;
1521         struct ct_iu_gid_pn_req *ct_iu_req;
1522         struct zfcp_gid_pn_data *gid_pn;
1523         struct zfcp_adapter *adapter = erp_action->adapter;
1524
1525         ret = zfcp_gid_pn_buffers_alloc(&gid_pn, adapter->pool.data_gid_pn);
1526         if (ret < 0) {
1527                 ZFCP_LOG_INFO("error: buffer allocation for gid_pn nameserver "
1528                               "request failed for adapter %s\n",
1529                               zfcp_get_busid_by_adapter(adapter));
1530                 goto out;
1531         }
1532
1533         /* setup nameserver request */
1534         ct_iu_req = zfcp_sg_to_address(gid_pn->ct.req);
1535         ct_iu_req->header.revision = ZFCP_CT_REVISION;
1536         ct_iu_req->header.gs_type = ZFCP_CT_DIRECTORY_SERVICE;
1537         ct_iu_req->header.gs_subtype = ZFCP_CT_NAME_SERVER;
1538         ct_iu_req->header.options = ZFCP_CT_SYNCHRONOUS;
1539         ct_iu_req->header.cmd_rsp_code = ZFCP_CT_GID_PN;
1540         ct_iu_req->header.max_res_size = ZFCP_CT_MAX_SIZE;
1541         ct_iu_req->wwpn = erp_action->port->wwpn;
1542
1543         /* setup parameters for send generic command */
1544         gid_pn->ct.port = adapter->nameserver_port;
1545         gid_pn->ct.handler = zfcp_ns_gid_pn_handler;
1546         gid_pn->ct.handler_data = (unsigned long) gid_pn;
1547         gid_pn->ct.timeout = ZFCP_NS_GID_PN_TIMEOUT;
1548         gid_pn->ct.timer = &erp_action->timer;
1549         gid_pn->port = erp_action->port;
1550
1551         ret = zfcp_fsf_send_ct(&gid_pn->ct, adapter->pool.fsf_req_erp,
1552                                erp_action);
1553         if (ret) {
1554                 ZFCP_LOG_INFO("error: initiation of gid_pn nameserver request "
1555                               "failed for adapter %s\n",
1556                               zfcp_get_busid_by_adapter(adapter));
1557
1558                 zfcp_gid_pn_buffers_free(gid_pn);
1559         }
1560
1561  out:
1562         return ret;
1563 }
1564
1565 /**
1566  * zfcp_ns_gid_pn_handler - handler for GID_PN nameserver request
1567  * @data: unsigned long, contains pointer to struct zfcp_gid_pn_data
1568  */
1569 static void zfcp_ns_gid_pn_handler(unsigned long data)
1570 {
1571         struct zfcp_port *port;
1572         struct zfcp_send_ct *ct;
1573         struct ct_iu_gid_pn_req *ct_iu_req;
1574         struct ct_iu_gid_pn_resp *ct_iu_resp;
1575         struct zfcp_gid_pn_data *gid_pn;
1576
1577
1578         gid_pn = (struct zfcp_gid_pn_data *) data;
1579         port = gid_pn->port;
1580         ct = &gid_pn->ct;
1581         ct_iu_req = zfcp_sg_to_address(ct->req);
1582         ct_iu_resp = zfcp_sg_to_address(ct->resp);
1583
1584         if (ct->status != 0)
1585                 goto failed;
1586
1587         if (zfcp_check_ct_response(&ct_iu_resp->header)) {
1588                 /* FIXME: do we need some specific erp entry points */
1589                 atomic_set_mask(ZFCP_STATUS_PORT_INVALID_WWPN, &port->status);
1590                 goto failed;
1591         }
1592         /* paranoia */
1593         if (ct_iu_req->wwpn != port->wwpn) {
1594                 ZFCP_LOG_NORMAL("bug: wwpn 0x%016Lx returned by nameserver "
1595                                 "lookup does not match expected wwpn 0x%016Lx "
1596                                 "for adapter %s\n", ct_iu_req->wwpn, port->wwpn,
1597                                 zfcp_get_busid_by_port(port));
1598                 goto mismatch;
1599         }
1600
1601         /* looks like a valid d_id */
1602         port->d_id = ct_iu_resp->d_id & ZFCP_DID_MASK;
1603         atomic_set_mask(ZFCP_STATUS_PORT_DID_DID, &port->status);
1604         ZFCP_LOG_DEBUG("adapter %s:  wwpn=0x%016Lx ---> d_id=0x%08x\n",
1605                        zfcp_get_busid_by_port(port), port->wwpn, port->d_id);
1606         goto out;
1607
1608  mismatch:
1609         ZFCP_LOG_DEBUG("CT IUs do not match:\n");
1610         ZFCP_HEX_DUMP(ZFCP_LOG_LEVEL_DEBUG, (char *) ct_iu_req,
1611                       sizeof(struct ct_iu_gid_pn_req));
1612         ZFCP_HEX_DUMP(ZFCP_LOG_LEVEL_DEBUG, (char *) ct_iu_resp,
1613                       sizeof(struct ct_iu_gid_pn_resp));
1614
1615  failed:
1616         ZFCP_LOG_NORMAL("warning: failed gid_pn nameserver request for wwpn "
1617                         "0x%016Lx for adapter %s\n",
1618                         port->wwpn, zfcp_get_busid_by_port(port));
1619  out:
1620         zfcp_gid_pn_buffers_free(gid_pn);
1621         return;
1622 }
1623
1624 /* reject CT_IU reason codes acc. to FC-GS-4 */
1625 static const struct zfcp_rc_entry zfcp_ct_rc[] = {
1626         {0x01, "invalid command code"},
1627         {0x02, "invalid version level"},
1628         {0x03, "logical error"},
1629         {0x04, "invalid CT_IU size"},
1630         {0x05, "logical busy"},
1631         {0x07, "protocol error"},
1632         {0x09, "unable to perform command request"},
1633         {0x0b, "command not supported"},
1634         {0x0d, "server not available"},
1635         {0x0e, "session could not be established"},
1636         {0xff, "vendor specific error"},
1637         {0, NULL},
1638 };
1639
1640 /* LS_RJT reason codes acc. to FC-FS */
1641 static const struct zfcp_rc_entry zfcp_ls_rjt_rc[] = {
1642         {0x01, "invalid LS_Command code"},
1643         {0x03, "logical error"},
1644         {0x05, "logical busy"},
1645         {0x07, "protocol error"},
1646         {0x09, "unable to perform command request"},
1647         {0x0b, "command not supported"},
1648         {0x0e, "command already in progress"},
1649         {0xff, "vendor specific error"},
1650         {0, NULL},
1651 };
1652
1653 /* reject reason codes according to FC-PH/FC-FS */
1654 static const struct zfcp_rc_entry zfcp_p_rjt_rc[] = {
1655         {0x01, "invalid D_ID"},
1656         {0x02, "invalid S_ID"},
1657         {0x03, "Nx_Port not available, temporary"},
1658         {0x04, "Nx_Port not available, permament"},
1659         {0x05, "class not supported"},
1660         {0x06, "delimiter usage error"},
1661         {0x07, "TYPE not supported"},
1662         {0x08, "invalid Link_Control"},
1663         {0x09, "invalid R_CTL field"},
1664         {0x0a, "invalid F_CTL field"},
1665         {0x0b, "invalid OX_ID"},
1666         {0x0c, "invalid RX_ID"},
1667         {0x0d, "invalid SEQ_ID"},
1668         {0x0e, "invalid DF_CTL"},
1669         {0x0f, "invalid SEQ_CNT"},
1670         {0x10, "invalid parameter field"},
1671         {0x11, "exchange error"},
1672         {0x12, "protocol error"},
1673         {0x13, "incorrect length"},
1674         {0x14, "unsupported ACK"},
1675         {0x15, "class of service not supported by entity at FFFFFE"},
1676         {0x16, "login required"},
1677         {0x17, "excessive sequences attempted"},
1678         {0x18, "unable to establish exchange"},
1679         {0x1a, "fabric path not available"},
1680         {0x1b, "invalid VC_ID (class 4)"},
1681         {0x1c, "invalid CS_CTL field"},
1682         {0x1d, "insufficient resources for VC (class 4)"},
1683         {0x1f, "invalid class of service"},
1684         {0x20, "preemption request rejected"},
1685         {0x21, "preemption not enabled"},
1686         {0x22, "multicast error"},
1687         {0x23, "multicast error terminate"},
1688         {0x24, "process login required"},
1689         {0xff, "vendor specific reject"},
1690         {0, NULL},
1691 };
1692
1693 /**
1694  * zfcp_rc_description - return description for given reaon code
1695  * @code: reason code
1696  * @rc_table: table of reason codes and descriptions
1697  */
1698 static inline const char *
1699 zfcp_rc_description(u8 code, const struct zfcp_rc_entry *rc_table)
1700 {
1701         const char *descr = "unknown reason code";
1702
1703         do {
1704                 if (code == rc_table->code) {
1705                         descr = rc_table->description;
1706                         break;
1707                 }
1708                 rc_table++;
1709         } while (rc_table->code && rc_table->description);
1710
1711         return descr;
1712 }
1713
1714 /**
1715  * zfcp_check_ct_response - evaluate reason code for CT_IU
1716  * @rjt: response payload to an CT_IU request
1717  * Return: 0 for accept CT_IU, 1 for reject CT_IU or invlid response code
1718  */
1719 int
1720 zfcp_check_ct_response(struct ct_hdr *rjt)
1721 {
1722         if (rjt->cmd_rsp_code == ZFCP_CT_ACCEPT)
1723                 return 0;
1724
1725         if (rjt->cmd_rsp_code != ZFCP_CT_REJECT) {
1726                 ZFCP_LOG_NORMAL("error: invalid Generic Service command/"
1727                                 "response code (0x%04hx)\n",
1728                                 rjt->cmd_rsp_code);
1729                 return 1;
1730         }
1731
1732         ZFCP_LOG_INFO("Generic Service command rejected\n");
1733         ZFCP_LOG_INFO("%s (0x%02x, 0x%02x, 0x%02x)\n",
1734                       zfcp_rc_description(rjt->reason_code, zfcp_ct_rc),
1735                       (u32) rjt->reason_code, (u32) rjt->reason_code_expl,
1736                       (u32) rjt->vendor_unique);
1737
1738         return 1;
1739 }
1740
1741 /**
1742  * zfcp_print_els_rjt - print reject parameter and description for ELS reject
1743  * @rjt_par: reject parameter acc. to FC-PH/FC-FS
1744  * @rc_table: table of reason codes and descriptions
1745  */
1746 static inline void
1747 zfcp_print_els_rjt(struct zfcp_ls_rjt_par *rjt_par,
1748                    const struct zfcp_rc_entry *rc_table)
1749 {
1750         ZFCP_LOG_INFO("%s (%02x %02x %02x %02x)\n",
1751                       zfcp_rc_description(rjt_par->reason_code, rc_table),
1752                       (u32) rjt_par->action, (u32) rjt_par->reason_code,
1753                       (u32) rjt_par->reason_expl, (u32) rjt_par->vendor_unique);
1754 }
1755
1756 /**
1757  * zfcp_fsf_handle_els_rjt - evaluate status qualifier/reason code on ELS reject
1758  * @sq: status qualifier word
1759  * @rjt_par: reject parameter as described in FC-PH and FC-FS
1760  * Return: -EROMTEIO for LS_RJT, -EREMCHG for invalid D_ID, -EIO else
1761  */
1762 int
1763 zfcp_handle_els_rjt(u32 sq, struct zfcp_ls_rjt_par *rjt_par)
1764 {
1765         int ret = -EIO;
1766
1767         if (sq == FSF_IOSTAT_NPORT_RJT) {
1768                 ZFCP_LOG_INFO("ELS rejected (P_RJT)\n");
1769                 zfcp_print_els_rjt(rjt_par, zfcp_p_rjt_rc);
1770                 /* invalid d_id */
1771                 if (rjt_par->reason_code == 0x01)
1772                         ret = -EREMCHG;
1773         } else if (sq == FSF_IOSTAT_FABRIC_RJT) {
1774                 ZFCP_LOG_INFO("ELS rejected (F_RJT)\n");
1775                 zfcp_print_els_rjt(rjt_par, zfcp_p_rjt_rc);
1776                 /* invalid d_id */
1777                 if (rjt_par->reason_code == 0x01)
1778                         ret = -EREMCHG;
1779         } else if (sq == FSF_IOSTAT_LS_RJT) {
1780                 ZFCP_LOG_INFO("ELS rejected (LS_RJT)\n");
1781                 zfcp_print_els_rjt(rjt_par, zfcp_ls_rjt_rc);
1782                 ret = -EREMOTEIO;
1783         } else
1784                 ZFCP_LOG_INFO("unexpected SQ: 0x%02x\n", sq);
1785
1786         return ret;
1787 }
1788
1789 #undef ZFCP_LOG_AREA