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