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