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