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