[SCSI] zfcp: (cleanup) kmalloc/kzalloc replacement
[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 = 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         scsi_lun_t 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         /* set FC service class used per default */
995         adapter->fc_service_class = ZFCP_FC_SERVICE_CLASS_DEFAULT;
996
997         sprintf(adapter->name, "%s", zfcp_get_busid_by_adapter(adapter));
998         ASCEBC(adapter->name, strlen(adapter->name));
999
1000         /* mark adapter unusable as long as sysfs registration is not complete */
1001         atomic_set_mask(ZFCP_STATUS_COMMON_REMOVE, &adapter->status);
1002
1003         adapter->ccw_device = ccw_device;
1004         dev_set_drvdata(&ccw_device->dev, adapter);
1005
1006         if (zfcp_sysfs_adapter_create_files(&ccw_device->dev))
1007                 goto sysfs_failed;
1008
1009         adapter->generic_services.parent = &adapter->ccw_device->dev;
1010         adapter->generic_services.release = zfcp_dummy_release;
1011         snprintf(adapter->generic_services.bus_id, BUS_ID_SIZE,
1012                  "generic_services");
1013
1014         if (device_register(&adapter->generic_services))
1015                 goto generic_services_failed;
1016
1017         /* put allocated adapter at list tail */
1018         write_lock_irq(&zfcp_data.config_lock);
1019         atomic_clear_mask(ZFCP_STATUS_COMMON_REMOVE, &adapter->status);
1020         list_add_tail(&adapter->list, &zfcp_data.adapter_list_head);
1021         write_unlock_irq(&zfcp_data.config_lock);
1022
1023         zfcp_data.adapters++;
1024
1025         goto out;
1026
1027  generic_services_failed:
1028         zfcp_sysfs_adapter_remove_files(&adapter->ccw_device->dev);
1029  sysfs_failed:
1030         dev_set_drvdata(&ccw_device->dev, NULL);
1031  failed_low_mem_buffers:
1032         zfcp_free_low_mem_buffers(adapter);
1033         if (qdio_free(ccw_device) != 0)
1034                 ZFCP_LOG_NORMAL("bug: qdio_free for adapter %s failed\n",
1035                                 zfcp_get_busid_by_adapter(adapter));
1036  qdio_allocate_failed:
1037         zfcp_qdio_free_queues(adapter);
1038  queues_alloc_failed:
1039         kfree(adapter);
1040         adapter = NULL;
1041  out:
1042         return adapter;
1043 }
1044
1045 /*
1046  * returns:     0 - struct zfcp_adapter  data structure successfully removed
1047  *              !0 - struct zfcp_adapter  data structure could not be removed
1048  *                      (e.g. still used)
1049  * locks:       adapter list write lock is assumed to be held by caller
1050  *              adapter->fsf_req_list_lock is taken and released within this 
1051  *              function and must not be held on entry
1052  */
1053 void
1054 zfcp_adapter_dequeue(struct zfcp_adapter *adapter)
1055 {
1056         int retval = 0;
1057         unsigned long flags;
1058
1059         device_unregister(&adapter->generic_services);
1060         zfcp_sysfs_adapter_remove_files(&adapter->ccw_device->dev);
1061         dev_set_drvdata(&adapter->ccw_device->dev, NULL);
1062         /* sanity check: no pending FSF requests */
1063         spin_lock_irqsave(&adapter->fsf_req_list_lock, flags);
1064         retval = !list_empty(&adapter->fsf_req_list_head);
1065         spin_unlock_irqrestore(&adapter->fsf_req_list_lock, flags);
1066         if (retval) {
1067                 ZFCP_LOG_NORMAL("bug: adapter %s (%p) still in use, "
1068                                 "%i requests outstanding\n",
1069                                 zfcp_get_busid_by_adapter(adapter), adapter,
1070                                 atomic_read(&adapter->fsf_reqs_active));
1071                 retval = -EBUSY;
1072                 goto out;
1073         }
1074
1075         /* remove specified adapter data structure from list */
1076         write_lock_irq(&zfcp_data.config_lock);
1077         list_del(&adapter->list);
1078         write_unlock_irq(&zfcp_data.config_lock);
1079
1080         /* decrease number of adapters in list */
1081         zfcp_data.adapters--;
1082
1083         ZFCP_LOG_TRACE("adapter %s (%p) removed from list, "
1084                        "%i adapters still in list\n",
1085                        zfcp_get_busid_by_adapter(adapter),
1086                        adapter, zfcp_data.adapters);
1087
1088         retval = qdio_free(adapter->ccw_device);
1089         if (retval)
1090                 ZFCP_LOG_NORMAL("bug: qdio_free for adapter %s failed\n",
1091                                 zfcp_get_busid_by_adapter(adapter));
1092
1093         zfcp_free_low_mem_buffers(adapter);
1094         /* free memory of adapter data structure and queues */
1095         zfcp_qdio_free_queues(adapter);
1096         kfree(adapter->fc_stats);
1097         kfree(adapter->stats_reset_data);
1098         ZFCP_LOG_TRACE("freeing adapter structure\n");
1099         kfree(adapter);
1100  out:
1101         return;
1102 }
1103
1104 /**
1105  * zfcp_port_enqueue - enqueue port to port list of adapter
1106  * @adapter: adapter where remote port is added
1107  * @wwpn: WWPN of the remote port to be enqueued
1108  * @status: initial status for the port
1109  * @d_id: destination id of the remote port to be enqueued
1110  * Return: pointer to enqueued port on success, NULL on error
1111  * Locks: config_sema must be held to serialize changes to the port list
1112  *
1113  * All port internal structures are set up and the sysfs entry is generated.
1114  * d_id is used to enqueue ports with a well known address like the Directory
1115  * Service for nameserver lookup.
1116  */
1117 struct zfcp_port *
1118 zfcp_port_enqueue(struct zfcp_adapter *adapter, wwn_t wwpn, u32 status,
1119                   u32 d_id)
1120 {
1121         struct zfcp_port *port;
1122         int check_wwpn;
1123
1124         check_wwpn = !(status & ZFCP_STATUS_PORT_NO_WWPN);
1125         /*
1126          * check that there is no port with this WWPN already in list
1127          */
1128         if (check_wwpn) {
1129                 read_lock_irq(&zfcp_data.config_lock);
1130                 port = zfcp_get_port_by_wwpn(adapter, wwpn);
1131                 read_unlock_irq(&zfcp_data.config_lock);
1132                 if (port)
1133                         return NULL;
1134         }
1135
1136         port = kzalloc(sizeof (struct zfcp_port), GFP_KERNEL);
1137         if (!port)
1138                 return NULL;
1139
1140         /* initialise reference count stuff */
1141         atomic_set(&port->refcount, 0);
1142         init_waitqueue_head(&port->remove_wq);
1143
1144         INIT_LIST_HEAD(&port->unit_list_head);
1145         INIT_LIST_HEAD(&port->unit_remove_lh);
1146
1147         port->adapter = adapter;
1148
1149         if (check_wwpn)
1150                 port->wwpn = wwpn;
1151
1152         atomic_set_mask(status, &port->status);
1153
1154         /* setup for sysfs registration */
1155         if (status & ZFCP_STATUS_PORT_WKA) {
1156                 switch (d_id) {
1157                 case ZFCP_DID_DIRECTORY_SERVICE:
1158                         snprintf(port->sysfs_device.bus_id, BUS_ID_SIZE,
1159                                  "directory");
1160                         break;
1161                 case ZFCP_DID_MANAGEMENT_SERVICE:
1162                         snprintf(port->sysfs_device.bus_id, BUS_ID_SIZE,
1163                                  "management");
1164                         break;
1165                 case ZFCP_DID_KEY_DISTRIBUTION_SERVICE:
1166                         snprintf(port->sysfs_device.bus_id, BUS_ID_SIZE,
1167                                  "key_distribution");
1168                         break;
1169                 case ZFCP_DID_ALIAS_SERVICE:
1170                         snprintf(port->sysfs_device.bus_id, BUS_ID_SIZE,
1171                                  "alias");
1172                         break;
1173                 case ZFCP_DID_TIME_SERVICE:
1174                         snprintf(port->sysfs_device.bus_id, BUS_ID_SIZE,
1175                                  "time");
1176                         break;
1177                 default:
1178                         kfree(port);
1179                         return NULL;
1180                 }
1181                 port->d_id = d_id;
1182                 port->sysfs_device.parent = &adapter->generic_services;
1183         } else {
1184                 snprintf(port->sysfs_device.bus_id,
1185                          BUS_ID_SIZE, "0x%016llx", wwpn);
1186                 port->sysfs_device.parent = &adapter->ccw_device->dev;
1187         }
1188         port->sysfs_device.release = zfcp_sysfs_port_release;
1189         dev_set_drvdata(&port->sysfs_device, port);
1190
1191         /* mark port unusable as long as sysfs registration is not complete */
1192         atomic_set_mask(ZFCP_STATUS_COMMON_REMOVE, &port->status);
1193
1194         if (device_register(&port->sysfs_device)) {
1195                 kfree(port);
1196                 return NULL;
1197         }
1198
1199         if (zfcp_sysfs_port_create_files(&port->sysfs_device, status)) {
1200                 device_unregister(&port->sysfs_device);
1201                 return NULL;
1202         }
1203
1204         zfcp_port_get(port);
1205
1206         write_lock_irq(&zfcp_data.config_lock);
1207         list_add_tail(&port->list, &adapter->port_list_head);
1208         atomic_clear_mask(ZFCP_STATUS_COMMON_REMOVE, &port->status);
1209         atomic_set_mask(ZFCP_STATUS_COMMON_RUNNING, &port->status);
1210         if (d_id == ZFCP_DID_DIRECTORY_SERVICE)
1211                 if (!adapter->nameserver_port)
1212                         adapter->nameserver_port = port;
1213         adapter->ports++;
1214         write_unlock_irq(&zfcp_data.config_lock);
1215
1216         zfcp_adapter_get(adapter);
1217
1218         return port;
1219 }
1220
1221 void
1222 zfcp_port_dequeue(struct zfcp_port *port)
1223 {
1224         zfcp_port_wait(port);
1225         write_lock_irq(&zfcp_data.config_lock);
1226         list_del(&port->list);
1227         port->adapter->ports--;
1228         write_unlock_irq(&zfcp_data.config_lock);
1229         if (port->rport)
1230                 fc_remote_port_delete(port->rport);
1231         port->rport = NULL;
1232         zfcp_adapter_put(port->adapter);
1233         zfcp_sysfs_port_remove_files(&port->sysfs_device,
1234                                      atomic_read(&port->status));
1235         device_unregister(&port->sysfs_device);
1236 }
1237
1238 /* Enqueues a nameserver port */
1239 int
1240 zfcp_nameserver_enqueue(struct zfcp_adapter *adapter)
1241 {
1242         struct zfcp_port *port;
1243
1244         port = zfcp_port_enqueue(adapter, 0, ZFCP_STATUS_PORT_WKA,
1245                                  ZFCP_DID_DIRECTORY_SERVICE);
1246         if (!port) {
1247                 ZFCP_LOG_INFO("error: enqueue of nameserver port for "
1248                               "adapter %s failed\n",
1249                               zfcp_get_busid_by_adapter(adapter));
1250                 return -ENXIO;
1251         }
1252         zfcp_port_put(port);
1253
1254         return 0;
1255 }
1256
1257 #undef ZFCP_LOG_AREA
1258
1259 /****************************************************************/
1260 /******* Fibre Channel Standard related Functions  **************/
1261 /****************************************************************/
1262
1263 #define ZFCP_LOG_AREA                   ZFCP_LOG_AREA_FC
1264
1265 void
1266 zfcp_fsf_incoming_els_rscn(struct zfcp_adapter *adapter,
1267                            struct fsf_status_read_buffer *status_buffer)
1268 {
1269         struct fcp_rscn_head *fcp_rscn_head;
1270         struct fcp_rscn_element *fcp_rscn_element;
1271         struct zfcp_port *port;
1272         u16 i;
1273         u16 no_entries;
1274         u32 range_mask;
1275         unsigned long flags;
1276
1277         fcp_rscn_head = (struct fcp_rscn_head *) status_buffer->payload;
1278         fcp_rscn_element = (struct fcp_rscn_element *) status_buffer->payload;
1279
1280         /* see FC-FS */
1281         no_entries = (fcp_rscn_head->payload_len / 4);
1282
1283         for (i = 1; i < no_entries; i++) {
1284                 /* skip head and start with 1st element */
1285                 fcp_rscn_element++;
1286                 switch (fcp_rscn_element->addr_format) {
1287                 case ZFCP_PORT_ADDRESS:
1288                         range_mask = ZFCP_PORTS_RANGE_PORT;
1289                         break;
1290                 case ZFCP_AREA_ADDRESS:
1291                         range_mask = ZFCP_PORTS_RANGE_AREA;
1292                         break;
1293                 case ZFCP_DOMAIN_ADDRESS:
1294                         range_mask = ZFCP_PORTS_RANGE_DOMAIN;
1295                         break;
1296                 case ZFCP_FABRIC_ADDRESS:
1297                         range_mask = ZFCP_PORTS_RANGE_FABRIC;
1298                         break;
1299                 default:
1300                         ZFCP_LOG_INFO("incoming RSCN with unknown "
1301                                       "address format\n");
1302                         continue;
1303                 }
1304                 read_lock_irqsave(&zfcp_data.config_lock, flags);
1305                 list_for_each_entry(port, &adapter->port_list_head, list) {
1306                         if (atomic_test_mask
1307                             (ZFCP_STATUS_PORT_WKA, &port->status))
1308                                 continue;
1309                         /* Do we know this port? If not skip it. */
1310                         if (!atomic_test_mask
1311                             (ZFCP_STATUS_PORT_DID_DID, &port->status)) {
1312                                 ZFCP_LOG_INFO("incoming RSCN, trying to open "
1313                                               "port 0x%016Lx\n", port->wwpn);
1314                                 zfcp_erp_port_reopen(port,
1315                                                      ZFCP_STATUS_COMMON_ERP_FAILED);
1316                                 continue;
1317                         }
1318
1319                         /*
1320                          * FIXME: race: d_id might being invalidated
1321                          * (...DID_DID reset)
1322                          */
1323                         if ((port->d_id & range_mask)
1324                             == (fcp_rscn_element->nport_did & range_mask)) {
1325                                 ZFCP_LOG_TRACE("reopen did 0x%08x\n",
1326                                                fcp_rscn_element->nport_did);
1327                                 /*
1328                                  * Unfortunately, an RSCN does not specify the
1329                                  * type of change a target underwent. We assume
1330                                  * that it makes sense to reopen the link.
1331                                  * FIXME: Shall we try to find out more about
1332                                  * the target and link state before closing it?
1333                                  * How to accomplish this? (nameserver?)
1334                                  * Where would such code be put in?
1335                                  * (inside or outside erp)
1336                                  */
1337                                 ZFCP_LOG_INFO("incoming RSCN, trying to open "
1338                                               "port 0x%016Lx\n", port->wwpn);
1339                                 zfcp_test_link(port);
1340                         }
1341                 }
1342                 read_unlock_irqrestore(&zfcp_data.config_lock, flags);
1343         }
1344 }
1345
1346 static void
1347 zfcp_fsf_incoming_els_plogi(struct zfcp_adapter *adapter,
1348                             struct fsf_status_read_buffer *status_buffer)
1349 {
1350         logi *els_logi = (logi *) status_buffer->payload;
1351         struct zfcp_port *port;
1352         unsigned long flags;
1353
1354         read_lock_irqsave(&zfcp_data.config_lock, flags);
1355         list_for_each_entry(port, &adapter->port_list_head, list) {
1356                 if (port->wwpn == (*(wwn_t *) & els_logi->nport_wwn))
1357                         break;
1358         }
1359         read_unlock_irqrestore(&zfcp_data.config_lock, flags);
1360
1361         if (!port || (port->wwpn != (*(wwn_t *) & els_logi->nport_wwn))) {
1362                 ZFCP_LOG_DEBUG("ignored incoming PLOGI for nonexisting port "
1363                                "with d_id 0x%08x on adapter %s\n",
1364                                status_buffer->d_id,
1365                                zfcp_get_busid_by_adapter(adapter));
1366         } else {
1367                 zfcp_erp_port_forced_reopen(port, 0);
1368         }
1369 }
1370
1371 static void
1372 zfcp_fsf_incoming_els_logo(struct zfcp_adapter *adapter,
1373                            struct fsf_status_read_buffer *status_buffer)
1374 {
1375         struct fcp_logo *els_logo = (struct fcp_logo *) status_buffer->payload;
1376         struct zfcp_port *port;
1377         unsigned long flags;
1378
1379         read_lock_irqsave(&zfcp_data.config_lock, flags);
1380         list_for_each_entry(port, &adapter->port_list_head, list) {
1381                 if (port->wwpn == els_logo->nport_wwpn)
1382                         break;
1383         }
1384         read_unlock_irqrestore(&zfcp_data.config_lock, flags);
1385
1386         if (!port || (port->wwpn != els_logo->nport_wwpn)) {
1387                 ZFCP_LOG_DEBUG("ignored incoming LOGO for nonexisting port "
1388                                "with d_id 0x%08x on adapter %s\n",
1389                                status_buffer->d_id,
1390                                zfcp_get_busid_by_adapter(adapter));
1391         } else {
1392                 zfcp_erp_port_forced_reopen(port, 0);
1393         }
1394 }
1395
1396 static void
1397 zfcp_fsf_incoming_els_unknown(struct zfcp_adapter *adapter,
1398                               struct fsf_status_read_buffer *status_buffer)
1399 {
1400         ZFCP_LOG_NORMAL("warning: unknown incoming ELS 0x%08x "
1401                         "for adapter %s\n", *(u32 *) (status_buffer->payload),
1402                         zfcp_get_busid_by_adapter(adapter));
1403
1404 }
1405
1406 void
1407 zfcp_fsf_incoming_els(struct zfcp_fsf_req *fsf_req)
1408 {
1409         struct fsf_status_read_buffer *status_buffer;
1410         u32 els_type;
1411         struct zfcp_adapter *adapter;
1412
1413         status_buffer = (struct fsf_status_read_buffer *) fsf_req->data;
1414         els_type = *(u32 *) (status_buffer->payload);
1415         adapter = fsf_req->adapter;
1416
1417         zfcp_san_dbf_event_incoming_els(fsf_req);
1418         if (els_type == LS_PLOGI)
1419                 zfcp_fsf_incoming_els_plogi(adapter, status_buffer);
1420         else if (els_type == LS_LOGO)
1421                 zfcp_fsf_incoming_els_logo(adapter, status_buffer);
1422         else if ((els_type & 0xffff0000) == LS_RSCN)
1423                 /* we are only concerned with the command, not the length */
1424                 zfcp_fsf_incoming_els_rscn(adapter, status_buffer);
1425         else
1426                 zfcp_fsf_incoming_els_unknown(adapter, status_buffer);
1427 }
1428
1429
1430 /**
1431  * zfcp_gid_pn_buffers_alloc - allocate buffers for GID_PN nameserver request
1432  * @gid_pn: pointer to return pointer to struct zfcp_gid_pn_data
1433  * @pool: pointer to mempool_t if non-null memory pool is used for allocation
1434  */
1435 static int
1436 zfcp_gid_pn_buffers_alloc(struct zfcp_gid_pn_data **gid_pn, mempool_t *pool)
1437 {
1438         struct zfcp_gid_pn_data *data;
1439
1440         if (pool != NULL) {
1441                 data = mempool_alloc(pool, GFP_ATOMIC);
1442                 if (likely(data != NULL)) {
1443                         data->ct.pool = pool;
1444                 }
1445         } else {
1446                 data = kmalloc(sizeof(struct zfcp_gid_pn_data), GFP_ATOMIC);
1447         }
1448
1449         if (NULL == data)
1450                 return -ENOMEM;
1451
1452         memset(data, 0, sizeof(*data));
1453         data->ct.req = &data->req;
1454         data->ct.resp = &data->resp;
1455         data->ct.req_count = data->ct.resp_count = 1;
1456         zfcp_address_to_sg(&data->ct_iu_req, &data->req);
1457         zfcp_address_to_sg(&data->ct_iu_resp, &data->resp);
1458         data->req.length = sizeof(struct ct_iu_gid_pn_req);
1459         data->resp.length = sizeof(struct ct_iu_gid_pn_resp);
1460
1461         *gid_pn = data;
1462         return 0;
1463 }
1464
1465 /**
1466  * zfcp_gid_pn_buffers_free - free buffers for GID_PN nameserver request
1467  * @gid_pn: pointer to struct zfcp_gid_pn_data which has to be freed
1468  */
1469 static void
1470 zfcp_gid_pn_buffers_free(struct zfcp_gid_pn_data *gid_pn)
1471 {
1472         if ((gid_pn->ct.pool != 0))
1473                 mempool_free(gid_pn, gid_pn->ct.pool);
1474         else
1475                 kfree(gid_pn);
1476
1477         return;
1478 }
1479
1480 /**
1481  * zfcp_ns_gid_pn_request - initiate GID_PN nameserver request
1482  * @erp_action: pointer to zfcp_erp_action where GID_PN request is needed
1483  */
1484 int
1485 zfcp_ns_gid_pn_request(struct zfcp_erp_action *erp_action)
1486 {
1487         int ret;
1488         struct ct_iu_gid_pn_req *ct_iu_req;
1489         struct zfcp_gid_pn_data *gid_pn;
1490         struct zfcp_adapter *adapter = erp_action->adapter;
1491
1492         ret = zfcp_gid_pn_buffers_alloc(&gid_pn, adapter->pool.data_gid_pn);
1493         if (ret < 0) {
1494                 ZFCP_LOG_INFO("error: buffer allocation for gid_pn nameserver "
1495                               "request failed for adapter %s\n",
1496                               zfcp_get_busid_by_adapter(adapter));
1497                 goto out;
1498         }
1499
1500         /* setup nameserver request */
1501         ct_iu_req = zfcp_sg_to_address(gid_pn->ct.req);
1502         ct_iu_req->header.revision = ZFCP_CT_REVISION;
1503         ct_iu_req->header.gs_type = ZFCP_CT_DIRECTORY_SERVICE;
1504         ct_iu_req->header.gs_subtype = ZFCP_CT_NAME_SERVER;
1505         ct_iu_req->header.options = ZFCP_CT_SYNCHRONOUS;
1506         ct_iu_req->header.cmd_rsp_code = ZFCP_CT_GID_PN;
1507         ct_iu_req->header.max_res_size = ZFCP_CT_MAX_SIZE;
1508         ct_iu_req->wwpn = erp_action->port->wwpn;
1509
1510         /* setup parameters for send generic command */
1511         gid_pn->ct.port = adapter->nameserver_port;
1512         gid_pn->ct.handler = zfcp_ns_gid_pn_handler;
1513         gid_pn->ct.handler_data = (unsigned long) gid_pn;
1514         gid_pn->ct.timeout = ZFCP_NS_GID_PN_TIMEOUT;
1515         gid_pn->ct.timer = &erp_action->timer;
1516         gid_pn->port = erp_action->port;
1517
1518         ret = zfcp_fsf_send_ct(&gid_pn->ct, adapter->pool.fsf_req_erp,
1519                                erp_action);
1520         if (ret) {
1521                 ZFCP_LOG_INFO("error: initiation of gid_pn nameserver request "
1522                               "failed for adapter %s\n",
1523                               zfcp_get_busid_by_adapter(adapter));
1524
1525                 zfcp_gid_pn_buffers_free(gid_pn);
1526         }
1527
1528  out:
1529         return ret;
1530 }
1531
1532 /**
1533  * zfcp_ns_gid_pn_handler - handler for GID_PN nameserver request
1534  * @data: unsigned long, contains pointer to struct zfcp_gid_pn_data
1535  */
1536 static void zfcp_ns_gid_pn_handler(unsigned long data)
1537 {
1538         struct zfcp_port *port;
1539         struct zfcp_send_ct *ct;
1540         struct ct_iu_gid_pn_req *ct_iu_req;
1541         struct ct_iu_gid_pn_resp *ct_iu_resp;
1542         struct zfcp_gid_pn_data *gid_pn;
1543
1544
1545         gid_pn = (struct zfcp_gid_pn_data *) data;
1546         port = gid_pn->port;
1547         ct = &gid_pn->ct;
1548         ct_iu_req = zfcp_sg_to_address(ct->req);
1549         ct_iu_resp = zfcp_sg_to_address(ct->resp);
1550
1551         if (ct->status != 0)
1552                 goto failed;
1553
1554         if (zfcp_check_ct_response(&ct_iu_resp->header)) {
1555                 /* FIXME: do we need some specific erp entry points */
1556                 atomic_set_mask(ZFCP_STATUS_PORT_INVALID_WWPN, &port->status);
1557                 goto failed;
1558         }
1559         /* paranoia */
1560         if (ct_iu_req->wwpn != port->wwpn) {
1561                 ZFCP_LOG_NORMAL("bug: wwpn 0x%016Lx returned by nameserver "
1562                                 "lookup does not match expected wwpn 0x%016Lx "
1563                                 "for adapter %s\n", ct_iu_req->wwpn, port->wwpn,
1564                                 zfcp_get_busid_by_port(port));
1565                 goto mismatch;
1566         }
1567
1568         /* looks like a valid d_id */
1569         port->d_id = ct_iu_resp->d_id & ZFCP_DID_MASK;
1570         atomic_set_mask(ZFCP_STATUS_PORT_DID_DID, &port->status);
1571         ZFCP_LOG_DEBUG("adapter %s:  wwpn=0x%016Lx ---> d_id=0x%08x\n",
1572                        zfcp_get_busid_by_port(port), port->wwpn, port->d_id);
1573         goto out;
1574
1575  mismatch:
1576         ZFCP_LOG_DEBUG("CT IUs do not match:\n");
1577         ZFCP_HEX_DUMP(ZFCP_LOG_LEVEL_DEBUG, (char *) ct_iu_req,
1578                       sizeof(struct ct_iu_gid_pn_req));
1579         ZFCP_HEX_DUMP(ZFCP_LOG_LEVEL_DEBUG, (char *) ct_iu_resp,
1580                       sizeof(struct ct_iu_gid_pn_resp));
1581
1582  failed:
1583         ZFCP_LOG_NORMAL("warning: failed gid_pn nameserver request for wwpn "
1584                         "0x%016Lx for adapter %s\n",
1585                         port->wwpn, zfcp_get_busid_by_port(port));
1586  out:
1587         zfcp_gid_pn_buffers_free(gid_pn);
1588         return;
1589 }
1590
1591 /* reject CT_IU reason codes acc. to FC-GS-4 */
1592 static const struct zfcp_rc_entry zfcp_ct_rc[] = {
1593         {0x01, "invalid command code"},
1594         {0x02, "invalid version level"},
1595         {0x03, "logical error"},
1596         {0x04, "invalid CT_IU size"},
1597         {0x05, "logical busy"},
1598         {0x07, "protocol error"},
1599         {0x09, "unable to perform command request"},
1600         {0x0b, "command not supported"},
1601         {0x0d, "server not available"},
1602         {0x0e, "session could not be established"},
1603         {0xff, "vendor specific error"},
1604         {0, NULL},
1605 };
1606
1607 /* LS_RJT reason codes acc. to FC-FS */
1608 static const struct zfcp_rc_entry zfcp_ls_rjt_rc[] = {
1609         {0x01, "invalid LS_Command code"},
1610         {0x03, "logical error"},
1611         {0x05, "logical busy"},
1612         {0x07, "protocol error"},
1613         {0x09, "unable to perform command request"},
1614         {0x0b, "command not supported"},
1615         {0x0e, "command already in progress"},
1616         {0xff, "vendor specific error"},
1617         {0, NULL},
1618 };
1619
1620 /* reject reason codes according to FC-PH/FC-FS */
1621 static const struct zfcp_rc_entry zfcp_p_rjt_rc[] = {
1622         {0x01, "invalid D_ID"},
1623         {0x02, "invalid S_ID"},
1624         {0x03, "Nx_Port not available, temporary"},
1625         {0x04, "Nx_Port not available, permament"},
1626         {0x05, "class not supported"},
1627         {0x06, "delimiter usage error"},
1628         {0x07, "TYPE not supported"},
1629         {0x08, "invalid Link_Control"},
1630         {0x09, "invalid R_CTL field"},
1631         {0x0a, "invalid F_CTL field"},
1632         {0x0b, "invalid OX_ID"},
1633         {0x0c, "invalid RX_ID"},
1634         {0x0d, "invalid SEQ_ID"},
1635         {0x0e, "invalid DF_CTL"},
1636         {0x0f, "invalid SEQ_CNT"},
1637         {0x10, "invalid parameter field"},
1638         {0x11, "exchange error"},
1639         {0x12, "protocol error"},
1640         {0x13, "incorrect length"},
1641         {0x14, "unsupported ACK"},
1642         {0x15, "class of service not supported by entity at FFFFFE"},
1643         {0x16, "login required"},
1644         {0x17, "excessive sequences attempted"},
1645         {0x18, "unable to establish exchange"},
1646         {0x1a, "fabric path not available"},
1647         {0x1b, "invalid VC_ID (class 4)"},
1648         {0x1c, "invalid CS_CTL field"},
1649         {0x1d, "insufficient resources for VC (class 4)"},
1650         {0x1f, "invalid class of service"},
1651         {0x20, "preemption request rejected"},
1652         {0x21, "preemption not enabled"},
1653         {0x22, "multicast error"},
1654         {0x23, "multicast error terminate"},
1655         {0x24, "process login required"},
1656         {0xff, "vendor specific reject"},
1657         {0, NULL},
1658 };
1659
1660 /**
1661  * zfcp_rc_description - return description for given reaon code
1662  * @code: reason code
1663  * @rc_table: table of reason codes and descriptions
1664  */
1665 static inline const char *
1666 zfcp_rc_description(u8 code, const struct zfcp_rc_entry *rc_table)
1667 {
1668         const char *descr = "unknown reason code";
1669
1670         do {
1671                 if (code == rc_table->code) {
1672                         descr = rc_table->description;
1673                         break;
1674                 }
1675                 rc_table++;
1676         } while (rc_table->code && rc_table->description);
1677
1678         return descr;
1679 }
1680
1681 /**
1682  * zfcp_check_ct_response - evaluate reason code for CT_IU
1683  * @rjt: response payload to an CT_IU request
1684  * Return: 0 for accept CT_IU, 1 for reject CT_IU or invlid response code
1685  */
1686 int
1687 zfcp_check_ct_response(struct ct_hdr *rjt)
1688 {
1689         if (rjt->cmd_rsp_code == ZFCP_CT_ACCEPT)
1690                 return 0;
1691
1692         if (rjt->cmd_rsp_code != ZFCP_CT_REJECT) {
1693                 ZFCP_LOG_NORMAL("error: invalid Generic Service command/"
1694                                 "response code (0x%04hx)\n",
1695                                 rjt->cmd_rsp_code);
1696                 return 1;
1697         }
1698
1699         ZFCP_LOG_INFO("Generic Service command rejected\n");
1700         ZFCP_LOG_INFO("%s (0x%02x, 0x%02x, 0x%02x)\n",
1701                       zfcp_rc_description(rjt->reason_code, zfcp_ct_rc),
1702                       (u32) rjt->reason_code, (u32) rjt->reason_code_expl,
1703                       (u32) rjt->vendor_unique);
1704
1705         return 1;
1706 }
1707
1708 /**
1709  * zfcp_print_els_rjt - print reject parameter and description for ELS reject
1710  * @rjt_par: reject parameter acc. to FC-PH/FC-FS
1711  * @rc_table: table of reason codes and descriptions
1712  */
1713 static inline void
1714 zfcp_print_els_rjt(struct zfcp_ls_rjt_par *rjt_par,
1715                    const struct zfcp_rc_entry *rc_table)
1716 {
1717         ZFCP_LOG_INFO("%s (%02x %02x %02x %02x)\n",
1718                       zfcp_rc_description(rjt_par->reason_code, rc_table),
1719                       (u32) rjt_par->action, (u32) rjt_par->reason_code,
1720                       (u32) rjt_par->reason_expl, (u32) rjt_par->vendor_unique);
1721 }
1722
1723 /**
1724  * zfcp_fsf_handle_els_rjt - evaluate status qualifier/reason code on ELS reject
1725  * @sq: status qualifier word
1726  * @rjt_par: reject parameter as described in FC-PH and FC-FS
1727  * Return: -EROMTEIO for LS_RJT, -EREMCHG for invalid D_ID, -EIO else
1728  */
1729 int
1730 zfcp_handle_els_rjt(u32 sq, struct zfcp_ls_rjt_par *rjt_par)
1731 {
1732         int ret = -EIO;
1733
1734         if (sq == FSF_IOSTAT_NPORT_RJT) {
1735                 ZFCP_LOG_INFO("ELS rejected (P_RJT)\n");
1736                 zfcp_print_els_rjt(rjt_par, zfcp_p_rjt_rc);
1737                 /* invalid d_id */
1738                 if (rjt_par->reason_code == 0x01)
1739                         ret = -EREMCHG;
1740         } else if (sq == FSF_IOSTAT_FABRIC_RJT) {
1741                 ZFCP_LOG_INFO("ELS rejected (F_RJT)\n");
1742                 zfcp_print_els_rjt(rjt_par, zfcp_p_rjt_rc);
1743                 /* invalid d_id */
1744                 if (rjt_par->reason_code == 0x01)
1745                         ret = -EREMCHG;
1746         } else if (sq == FSF_IOSTAT_LS_RJT) {
1747                 ZFCP_LOG_INFO("ELS rejected (LS_RJT)\n");
1748                 zfcp_print_els_rjt(rjt_par, zfcp_ls_rjt_rc);
1749                 ret = -EREMOTEIO;
1750         } else
1751                 ZFCP_LOG_INFO("unexpected SQ: 0x%02x\n", sq);
1752
1753         return ret;
1754 }
1755
1756 #undef ZFCP_LOG_AREA