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