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