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