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