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