]> pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/s390/scsi/zfcp_aux.c
[SCSI] zfcp: Move FC code to new file
[linux-2.6-omap-h63xx.git] / 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 /* miscellaneous */
47 static int zfcp_sg_list_alloc(struct zfcp_sg_list *, size_t);
48 static void zfcp_sg_list_free(struct zfcp_sg_list *);
49 static int zfcp_sg_list_copy_from_user(struct zfcp_sg_list *,
50                                        void __user *, size_t);
51 static int zfcp_sg_list_copy_to_user(void __user *,
52                                      struct zfcp_sg_list *, size_t);
53 static long zfcp_cfdc_dev_ioctl(struct file *, unsigned int, unsigned long);
54
55 #define ZFCP_CFDC_IOC_MAGIC                     0xDD
56 #define ZFCP_CFDC_IOC \
57         _IOWR(ZFCP_CFDC_IOC_MAGIC, 0, struct zfcp_cfdc_sense_data)
58
59
60 static const struct file_operations zfcp_cfdc_fops = {
61         .unlocked_ioctl = zfcp_cfdc_dev_ioctl,
62 #ifdef CONFIG_COMPAT
63         .compat_ioctl = zfcp_cfdc_dev_ioctl
64 #endif
65 };
66
67 static struct miscdevice zfcp_cfdc_misc = {
68         .minor = ZFCP_CFDC_DEV_MINOR,
69         .name = ZFCP_CFDC_DEV_NAME,
70         .fops = &zfcp_cfdc_fops
71 };
72
73 /*********************** KERNEL/MODULE PARAMETERS  ***************************/
74
75 /* declare driver module init/cleanup functions */
76 module_init(zfcp_module_init);
77
78 MODULE_AUTHOR("IBM Deutschland Entwicklung GmbH - linux390@de.ibm.com");
79 MODULE_DESCRIPTION
80     ("FCP (SCSI over Fibre Channel) HBA driver for IBM System z9 and zSeries");
81 MODULE_LICENSE("GPL");
82
83 module_param(device, charp, 0400);
84 MODULE_PARM_DESC(device, "specify initial device");
85
86 module_param(loglevel, uint, 0400);
87 MODULE_PARM_DESC(loglevel,
88                  "log levels, 8 nibbles: "
89                  "FC ERP QDIO CIO Config FSF SCSI Other, "
90                  "levels: 0=none 1=normal 2=devel 3=trace");
91
92 /****************************************************************/
93 /************** Functions without logging ***********************/
94 /****************************************************************/
95
96 void
97 _zfcp_hex_dump(char *addr, int count)
98 {
99         int i;
100         for (i = 0; i < count; i++) {
101                 printk("%02x", addr[i]);
102                 if ((i % 4) == 3)
103                         printk(" ");
104                 if ((i % 32) == 31)
105                         printk("\n");
106         }
107         if (((i-1) % 32) != 31)
108                 printk("\n");
109 }
110
111
112 /****************************************************************/
113 /****** Functions to handle the request ID hash table    ********/
114 /****************************************************************/
115
116 #define ZFCP_LOG_AREA                   ZFCP_LOG_AREA_FSF
117
118 static int zfcp_reqlist_alloc(struct zfcp_adapter *adapter)
119 {
120         int idx;
121
122         adapter->req_list = kcalloc(REQUEST_LIST_SIZE, sizeof(struct list_head),
123                                     GFP_KERNEL);
124         if (!adapter->req_list)
125                 return -ENOMEM;
126
127         for (idx = 0; idx < REQUEST_LIST_SIZE; idx++)
128                 INIT_LIST_HEAD(&adapter->req_list[idx]);
129         return 0;
130 }
131
132 static void zfcp_reqlist_free(struct zfcp_adapter *adapter)
133 {
134         kfree(adapter->req_list);
135 }
136
137 int zfcp_reqlist_isempty(struct zfcp_adapter *adapter)
138 {
139         unsigned int idx;
140
141         for (idx = 0; idx < REQUEST_LIST_SIZE; idx++)
142                 if (!list_empty(&adapter->req_list[idx]))
143                         return 0;
144         return 1;
145 }
146
147 #undef ZFCP_LOG_AREA
148
149 /****************************************************************/
150 /************** Uncategorised Functions *************************/
151 /****************************************************************/
152
153 #define ZFCP_LOG_AREA                   ZFCP_LOG_AREA_OTHER
154
155 /**
156  * zfcp_device_setup - setup function
157  * @str: pointer to parameter string
158  *
159  * Parse "device=..." parameter string.
160  */
161 static int __init
162 zfcp_device_setup(char *devstr)
163 {
164         char *tmp, *str;
165         size_t len;
166
167         if (!devstr)
168                 return 0;
169
170         len = strlen(devstr) + 1;
171         str = kmalloc(len, GFP_KERNEL);
172         if (!str)
173                 goto err_out;
174         memcpy(str, devstr, len);
175
176         tmp = strchr(str, ',');
177         if (!tmp)
178                 goto err_out;
179         *tmp++ = '\0';
180         strncpy(zfcp_data.init_busid, str, BUS_ID_SIZE);
181         zfcp_data.init_busid[BUS_ID_SIZE-1] = '\0';
182
183         zfcp_data.init_wwpn = simple_strtoull(tmp, &tmp, 0);
184         if (*tmp++ != ',')
185                 goto err_out;
186         if (*tmp == '\0')
187                 goto err_out;
188
189         zfcp_data.init_fcp_lun = simple_strtoull(tmp, &tmp, 0);
190         if (*tmp != '\0')
191                 goto err_out;
192         kfree(str);
193         return 1;
194
195  err_out:
196         ZFCP_LOG_NORMAL("Parse error for device parameter string %s\n", str);
197         kfree(str);
198         return 0;
199 }
200
201 static void __init
202 zfcp_init_device_configure(void)
203 {
204         struct zfcp_adapter *adapter;
205         struct zfcp_port *port;
206         struct zfcp_unit *unit;
207
208         down(&zfcp_data.config_sema);
209         read_lock_irq(&zfcp_data.config_lock);
210         adapter = zfcp_get_adapter_by_busid(zfcp_data.init_busid);
211         if (adapter)
212                 zfcp_adapter_get(adapter);
213         read_unlock_irq(&zfcp_data.config_lock);
214
215         if (adapter == NULL)
216                 goto out_adapter;
217         port = zfcp_port_enqueue(adapter, zfcp_data.init_wwpn, 0, 0);
218         if (!port)
219                 goto out_port;
220         unit = zfcp_unit_enqueue(port, zfcp_data.init_fcp_lun);
221         if (!unit)
222                 goto out_unit;
223         up(&zfcp_data.config_sema);
224         ccw_device_set_online(adapter->ccw_device);
225         zfcp_erp_wait(adapter);
226         down(&zfcp_data.config_sema);
227         zfcp_unit_put(unit);
228  out_unit:
229         zfcp_port_put(port);
230  out_port:
231         zfcp_adapter_put(adapter);
232  out_adapter:
233         up(&zfcp_data.config_sema);
234         return;
235 }
236
237 static int calc_alignment(int size)
238 {
239         int align = 1;
240
241         if (!size)
242                 return 0;
243
244         while ((size - align) > 0)
245                 align <<= 1;
246
247         return align;
248 }
249
250 static int __init
251 zfcp_module_init(void)
252 {
253         int retval = -ENOMEM;
254         int size, align;
255
256         size = sizeof(struct zfcp_fsf_req_qtcb);
257         align = calc_alignment(size);
258         zfcp_data.fsf_req_qtcb_cache =
259                 kmem_cache_create("zfcp_fsf", size, align, 0, NULL);
260         if (!zfcp_data.fsf_req_qtcb_cache)
261                 goto out;
262
263         size = sizeof(struct fsf_status_read_buffer);
264         align = calc_alignment(size);
265         zfcp_data.sr_buffer_cache =
266                 kmem_cache_create("zfcp_sr", size, align, 0, NULL);
267         if (!zfcp_data.sr_buffer_cache)
268                 goto out_sr_cache;
269
270         size = sizeof(struct zfcp_gid_pn_data);
271         align = calc_alignment(size);
272         zfcp_data.gid_pn_cache =
273                 kmem_cache_create("zfcp_gid", size, align, 0, NULL);
274         if (!zfcp_data.gid_pn_cache)
275                 goto out_gid_cache;
276
277         atomic_set(&zfcp_data.loglevel, loglevel);
278
279         /* initialize adapter list */
280         INIT_LIST_HEAD(&zfcp_data.adapter_list_head);
281
282         /* initialize adapters to be removed list head */
283         INIT_LIST_HEAD(&zfcp_data.adapter_remove_lh);
284
285         zfcp_data.scsi_transport_template =
286                 fc_attach_transport(&zfcp_transport_functions);
287         if (!zfcp_data.scsi_transport_template)
288                 goto out_transport;
289
290         retval = misc_register(&zfcp_cfdc_misc);
291         if (retval != 0) {
292                 ZFCP_LOG_INFO("registration of misc device "
293                               "zfcp_cfdc failed\n");
294                 goto out_misc;
295         }
296
297         ZFCP_LOG_TRACE("major/minor for zfcp_cfdc: %d/%d\n",
298                        ZFCP_CFDC_DEV_MAJOR, zfcp_cfdc_misc.minor);
299
300         /* Initialise proc semaphores */
301         sema_init(&zfcp_data.config_sema, 1);
302
303         /* initialise configuration rw lock */
304         rwlock_init(&zfcp_data.config_lock);
305
306         /* setup dynamic I/O */
307         retval = zfcp_ccw_register();
308         if (retval) {
309                 ZFCP_LOG_NORMAL("registration with common I/O layer failed\n");
310                 goto out_ccw_register;
311         }
312
313         if (zfcp_device_setup(device))
314                 zfcp_init_device_configure();
315
316         goto out;
317
318  out_ccw_register:
319         misc_deregister(&zfcp_cfdc_misc);
320  out_misc:
321         fc_release_transport(zfcp_data.scsi_transport_template);
322  out_transport:
323         kmem_cache_destroy(zfcp_data.gid_pn_cache);
324  out_gid_cache:
325         kmem_cache_destroy(zfcp_data.sr_buffer_cache);
326  out_sr_cache:
327         kmem_cache_destroy(zfcp_data.fsf_req_qtcb_cache);
328  out:
329         return retval;
330 }
331
332 /*
333  * function:    zfcp_cfdc_dev_ioctl
334  *
335  * purpose:     Handle control file upload/download transaction via IOCTL
336  *              interface
337  *
338  * returns:     0           - Operation completed successfuly
339  *              -ENOTTY     - Unknown IOCTL command
340  *              -EINVAL     - Invalid sense data record
341  *              -ENXIO      - The FCP adapter is not available
342  *              -EOPNOTSUPP - The FCP adapter does not have CFDC support
343  *              -ENOMEM     - Insufficient memory
344  *              -EFAULT     - User space memory I/O operation fault
345  *              -EPERM      - Cannot create or queue FSF request or create SBALs
346  *              -ERESTARTSYS- Received signal (is mapped to EAGAIN by VFS)
347  */
348 static long
349 zfcp_cfdc_dev_ioctl(struct file *file, unsigned int command,
350                     unsigned long buffer)
351 {
352         struct zfcp_cfdc_sense_data *sense_data, __user *sense_data_user;
353         struct zfcp_adapter *adapter = NULL;
354         struct zfcp_fsf_req *fsf_req = NULL;
355         struct zfcp_sg_list *sg_list = NULL;
356         u32 fsf_command, option;
357         char *bus_id = NULL;
358         int retval = 0;
359
360         sense_data = kmalloc(sizeof(struct zfcp_cfdc_sense_data), GFP_KERNEL);
361         if (sense_data == NULL) {
362                 retval = -ENOMEM;
363                 goto out;
364         }
365
366         sg_list = kzalloc(sizeof(struct zfcp_sg_list), GFP_KERNEL);
367         if (sg_list == NULL) {
368                 retval = -ENOMEM;
369                 goto out;
370         }
371
372         if (command != ZFCP_CFDC_IOC) {
373                 ZFCP_LOG_INFO("IOC request code 0x%x invalid\n", command);
374                 retval = -ENOTTY;
375                 goto out;
376         }
377
378         if ((sense_data_user = (void __user *) buffer) == NULL) {
379                 ZFCP_LOG_INFO("sense data record is required\n");
380                 retval = -EINVAL;
381                 goto out;
382         }
383
384         retval = copy_from_user(sense_data, sense_data_user,
385                                 sizeof(struct zfcp_cfdc_sense_data));
386         if (retval) {
387                 retval = -EFAULT;
388                 goto out;
389         }
390
391         if (sense_data->signature != ZFCP_CFDC_SIGNATURE) {
392                 ZFCP_LOG_INFO("invalid sense data request signature 0x%08x\n",
393                               ZFCP_CFDC_SIGNATURE);
394                 retval = -EINVAL;
395                 goto out;
396         }
397
398         switch (sense_data->command) {
399
400         case ZFCP_CFDC_CMND_DOWNLOAD_NORMAL:
401                 fsf_command = FSF_QTCB_DOWNLOAD_CONTROL_FILE;
402                 option = FSF_CFDC_OPTION_NORMAL_MODE;
403                 break;
404
405         case ZFCP_CFDC_CMND_DOWNLOAD_FORCE:
406                 fsf_command = FSF_QTCB_DOWNLOAD_CONTROL_FILE;
407                 option = FSF_CFDC_OPTION_FORCE;
408                 break;
409
410         case ZFCP_CFDC_CMND_FULL_ACCESS:
411                 fsf_command = FSF_QTCB_DOWNLOAD_CONTROL_FILE;
412                 option = FSF_CFDC_OPTION_FULL_ACCESS;
413                 break;
414
415         case ZFCP_CFDC_CMND_RESTRICTED_ACCESS:
416                 fsf_command = FSF_QTCB_DOWNLOAD_CONTROL_FILE;
417                 option = FSF_CFDC_OPTION_RESTRICTED_ACCESS;
418                 break;
419
420         case ZFCP_CFDC_CMND_UPLOAD:
421                 fsf_command = FSF_QTCB_UPLOAD_CONTROL_FILE;
422                 option = 0;
423                 break;
424
425         default:
426                 ZFCP_LOG_INFO("invalid command code 0x%08x\n",
427                               sense_data->command);
428                 retval = -EINVAL;
429                 goto out;
430         }
431
432         bus_id = kmalloc(BUS_ID_SIZE, GFP_KERNEL);
433         if (bus_id == NULL) {
434                 retval = -ENOMEM;
435                 goto out;
436         }
437         snprintf(bus_id, BUS_ID_SIZE, "%d.%d.%04x",
438                 (sense_data->devno >> 24),
439                 (sense_data->devno >> 16) & 0xFF,
440                 (sense_data->devno & 0xFFFF));
441
442         read_lock_irq(&zfcp_data.config_lock);
443         adapter = zfcp_get_adapter_by_busid(bus_id);
444         if (adapter)
445                 zfcp_adapter_get(adapter);
446         read_unlock_irq(&zfcp_data.config_lock);
447
448         kfree(bus_id);
449
450         if (adapter == NULL) {
451                 ZFCP_LOG_INFO("invalid adapter\n");
452                 retval = -ENXIO;
453                 goto out;
454         }
455
456         if (sense_data->command & ZFCP_CFDC_WITH_CONTROL_FILE) {
457                 retval = zfcp_sg_list_alloc(sg_list,
458                                             ZFCP_CFDC_MAX_CONTROL_FILE_SIZE);
459                 if (retval) {
460                         retval = -ENOMEM;
461                         goto out;
462                 }
463         }
464
465         if ((sense_data->command & ZFCP_CFDC_DOWNLOAD) &&
466             (sense_data->command & ZFCP_CFDC_WITH_CONTROL_FILE)) {
467                 retval = zfcp_sg_list_copy_from_user(
468                         sg_list, &sense_data_user->control_file,
469                         ZFCP_CFDC_MAX_CONTROL_FILE_SIZE);
470                 if (retval) {
471                         retval = -EFAULT;
472                         goto out;
473                 }
474         }
475
476         retval = zfcp_fsf_control_file(adapter, &fsf_req, fsf_command,
477                                        option, sg_list);
478         if (retval)
479                 goto out;
480
481         if ((fsf_req->qtcb->prefix.prot_status != FSF_PROT_GOOD) &&
482             (fsf_req->qtcb->prefix.prot_status != FSF_PROT_FSF_STATUS_PRESENTED)) {
483                 retval = -ENXIO;
484                 goto out;
485         }
486
487         sense_data->fsf_status = fsf_req->qtcb->header.fsf_status;
488         memcpy(&sense_data->fsf_status_qual,
489                &fsf_req->qtcb->header.fsf_status_qual,
490                sizeof(union fsf_status_qual));
491         memcpy(&sense_data->payloads, &fsf_req->qtcb->bottom.support.els, 256);
492
493         retval = copy_to_user(sense_data_user, sense_data,
494                 sizeof(struct zfcp_cfdc_sense_data));
495         if (retval) {
496                 retval = -EFAULT;
497                 goto out;
498         }
499
500         if (sense_data->command & ZFCP_CFDC_UPLOAD) {
501                 retval = zfcp_sg_list_copy_to_user(
502                         &sense_data_user->control_file, sg_list,
503                         ZFCP_CFDC_MAX_CONTROL_FILE_SIZE);
504                 if (retval) {
505                         retval = -EFAULT;
506                         goto out;
507                 }
508         }
509
510  out:
511         if (fsf_req != NULL)
512                 zfcp_fsf_req_free(fsf_req);
513
514         if ((adapter != NULL) && (retval != -ENXIO))
515                 zfcp_adapter_put(adapter);
516
517         if (sg_list != NULL) {
518                 zfcp_sg_list_free(sg_list);
519                 kfree(sg_list);
520         }
521
522         kfree(sense_data);
523
524         return retval;
525 }
526
527
528 /**
529  * zfcp_sg_list_alloc - create a scatter-gather list of the specified size
530  * @sg_list: structure describing a scatter gather list
531  * @size: size of scatter-gather list
532  * Return: 0 on success, else -ENOMEM
533  *
534  * In sg_list->sg a pointer to the created scatter-gather list is returned,
535  * or NULL if we run out of memory. sg_list->count specifies the number of
536  * elements of the scatter-gather list. The maximum size of a single element
537  * in the scatter-gather list is PAGE_SIZE.
538  */
539 static int
540 zfcp_sg_list_alloc(struct zfcp_sg_list *sg_list, size_t size)
541 {
542         struct scatterlist *sg;
543         unsigned int i;
544         int retval = 0;
545         void *address;
546
547         BUG_ON(sg_list == NULL);
548
549         sg_list->count = size >> PAGE_SHIFT;
550         if (size & ~PAGE_MASK)
551                 sg_list->count++;
552         sg_list->sg = kcalloc(sg_list->count, sizeof(struct scatterlist),
553                               GFP_KERNEL);
554         if (sg_list->sg == NULL) {
555                 sg_list->count = 0;
556                 retval = -ENOMEM;
557                 goto out;
558         }
559         sg_init_table(sg_list->sg, sg_list->count);
560
561         for (i = 0, sg = sg_list->sg; i < sg_list->count; i++, sg++) {
562                 address = (void *) get_zeroed_page(GFP_KERNEL);
563                 if (address == NULL) {
564                         sg_list->count = i;
565                         zfcp_sg_list_free(sg_list);
566                         retval = -ENOMEM;
567                         goto out;
568                 }
569                 zfcp_address_to_sg(address, sg, min(size, PAGE_SIZE));
570                 size -= sg->length;
571         }
572
573  out:
574         return retval;
575 }
576
577
578 /**
579  * zfcp_sg_list_free - free memory of a scatter-gather list
580  * @sg_list: structure describing a scatter-gather list
581  *
582  * Memory for each element in the scatter-gather list is freed.
583  * Finally sg_list->sg is freed itself and sg_list->count is reset.
584  */
585 static void
586 zfcp_sg_list_free(struct zfcp_sg_list *sg_list)
587 {
588         struct scatterlist *sg;
589         unsigned int i;
590
591         BUG_ON(sg_list == NULL);
592
593         for (i = 0, sg = sg_list->sg; i < sg_list->count; i++, sg++)
594                 free_page((unsigned long) zfcp_sg_to_address(sg));
595
596         sg_list->count = 0;
597         kfree(sg_list->sg);
598 }
599
600 /**
601  * zfcp_sg_size - determine size of a scatter-gather list
602  * @sg: array of (struct scatterlist)
603  * @sg_count: elements in array
604  * Return: size of entire scatter-gather list
605  */
606 static size_t zfcp_sg_size(struct scatterlist *sg, unsigned int sg_count)
607 {
608         unsigned int i;
609         struct scatterlist *p;
610         size_t size;
611
612         size = 0;
613         for (i = 0, p = sg; i < sg_count; i++, p++) {
614                 BUG_ON(p == NULL);
615                 size += p->length;
616         }
617
618         return size;
619 }
620
621
622 /**
623  * zfcp_sg_list_copy_from_user -copy data from user space to scatter-gather list
624  * @sg_list: structure describing a scatter-gather list
625  * @user_buffer: pointer to buffer in user space
626  * @size: number of bytes to be copied
627  * Return: 0 on success, -EFAULT if copy_from_user fails.
628  */
629 static int
630 zfcp_sg_list_copy_from_user(struct zfcp_sg_list *sg_list,
631                             void __user *user_buffer,
632                             size_t size)
633 {
634         struct scatterlist *sg;
635         unsigned int length;
636         void *zfcp_buffer;
637         int retval = 0;
638
639         BUG_ON(sg_list == NULL);
640
641         if (zfcp_sg_size(sg_list->sg, sg_list->count) < size)
642                 return -EFAULT;
643
644         for (sg = sg_list->sg; size > 0; sg++) {
645                 length = min((unsigned int)size, sg->length);
646                 zfcp_buffer = zfcp_sg_to_address(sg);
647                 if (copy_from_user(zfcp_buffer, user_buffer, length)) {
648                         retval = -EFAULT;
649                         goto out;
650                 }
651                 user_buffer += length;
652                 size -= length;
653         }
654
655  out:
656         return retval;
657 }
658
659
660 /**
661  * zfcp_sg_list_copy_to_user - copy data from scatter-gather list to user space
662  * @user_buffer: pointer to buffer in user space
663  * @sg_list: structure describing a scatter-gather list
664  * @size: number of bytes to be copied
665  * Return: 0 on success, -EFAULT if copy_to_user fails
666  */
667 static int
668 zfcp_sg_list_copy_to_user(void __user  *user_buffer,
669                           struct zfcp_sg_list *sg_list,
670                           size_t size)
671 {
672         struct scatterlist *sg;
673         unsigned int length;
674         void *zfcp_buffer;
675         int retval = 0;
676
677         BUG_ON(sg_list == NULL);
678
679         if (zfcp_sg_size(sg_list->sg, sg_list->count) < size)
680                 return -EFAULT;
681
682         for (sg = sg_list->sg; size > 0; sg++) {
683                 length = min((unsigned int) size, sg->length);
684                 zfcp_buffer = zfcp_sg_to_address(sg);
685                 if (copy_to_user(user_buffer, zfcp_buffer, length)) {
686                         retval = -EFAULT;
687                         goto out;
688                 }
689                 user_buffer += length;
690                 size -= length;
691         }
692
693  out:
694         return retval;
695 }
696
697
698 #undef ZFCP_LOG_AREA
699
700 /****************************************************************/
701 /****** Functions for configuration/set-up of structures ********/
702 /****************************************************************/
703
704 #define ZFCP_LOG_AREA                   ZFCP_LOG_AREA_CONFIG
705
706 /**
707  * zfcp_get_unit_by_lun - find unit in unit list of port by FCP LUN
708  * @port: pointer to port to search for unit
709  * @fcp_lun: FCP LUN to search for
710  * Traverse list of all units of a port and return pointer to a unit
711  * with the given FCP LUN.
712  */
713 struct zfcp_unit *
714 zfcp_get_unit_by_lun(struct zfcp_port *port, fcp_lun_t fcp_lun)
715 {
716         struct zfcp_unit *unit;
717         int found = 0;
718
719         list_for_each_entry(unit, &port->unit_list_head, list) {
720                 if ((unit->fcp_lun == fcp_lun) &&
721                     !atomic_test_mask(ZFCP_STATUS_COMMON_REMOVE, &unit->status))
722                 {
723                         found = 1;
724                         break;
725                 }
726         }
727         return found ? unit : NULL;
728 }
729
730 /**
731  * zfcp_get_port_by_wwpn - find port in port list of adapter by wwpn
732  * @adapter: pointer to adapter to search for port
733  * @wwpn: wwpn to search for
734  * Traverse list of all ports of an adapter and return pointer to a port
735  * with the given wwpn.
736  */
737 struct zfcp_port *
738 zfcp_get_port_by_wwpn(struct zfcp_adapter *adapter, wwn_t wwpn)
739 {
740         struct zfcp_port *port;
741         int found = 0;
742
743         list_for_each_entry(port, &adapter->port_list_head, list) {
744                 if ((port->wwpn == wwpn) &&
745                     !(atomic_read(&port->status) &
746                       (ZFCP_STATUS_PORT_NO_WWPN | ZFCP_STATUS_COMMON_REMOVE))) {
747                         found = 1;
748                         break;
749                 }
750         }
751         return found ? port : NULL;
752 }
753
754 /**
755  * zfcp_get_port_by_did - find port in port list of adapter by d_id
756  * @adapter: pointer to adapter to search for port
757  * @d_id: d_id to search for
758  * Traverse list of all ports of an adapter and return pointer to a port
759  * with the given d_id.
760  */
761 struct zfcp_port *
762 zfcp_get_port_by_did(struct zfcp_adapter *adapter, u32 d_id)
763 {
764         struct zfcp_port *port;
765         int found = 0;
766
767         list_for_each_entry(port, &adapter->port_list_head, list) {
768                 if ((port->d_id == d_id) &&
769                     !atomic_test_mask(ZFCP_STATUS_COMMON_REMOVE, &port->status))
770                 {
771                         found = 1;
772                         break;
773                 }
774         }
775         return found ? port : NULL;
776 }
777
778 /**
779  * zfcp_get_adapter_by_busid - find adpater in adapter list by bus_id
780  * @bus_id: bus_id to search for
781  * Traverse list of all adapters and return pointer to an adapter
782  * with the given bus_id.
783  */
784 struct zfcp_adapter *
785 zfcp_get_adapter_by_busid(char *bus_id)
786 {
787         struct zfcp_adapter *adapter;
788         int found = 0;
789
790         list_for_each_entry(adapter, &zfcp_data.adapter_list_head, list) {
791                 if ((strncmp(bus_id, zfcp_get_busid_by_adapter(adapter),
792                              BUS_ID_SIZE) == 0) &&
793                     !atomic_test_mask(ZFCP_STATUS_COMMON_REMOVE,
794                                       &adapter->status)){
795                         found = 1;
796                         break;
797                 }
798         }
799         return found ? adapter : NULL;
800 }
801
802 /**
803  * zfcp_unit_enqueue - enqueue unit to unit list of a port.
804  * @port: pointer to port where unit is added
805  * @fcp_lun: FCP LUN of unit to be enqueued
806  * Return: pointer to enqueued unit on success, NULL on error
807  * Locks: config_sema must be held to serialize changes to the unit list
808  *
809  * Sets up some unit internal structures and creates sysfs entry.
810  */
811 struct zfcp_unit *
812 zfcp_unit_enqueue(struct zfcp_port *port, fcp_lun_t fcp_lun)
813 {
814         struct zfcp_unit *unit;
815
816         /*
817          * check that there is no unit with this FCP_LUN already in list
818          * and enqueue it.
819          * Note: Unlike for the adapter and the port, this is an error
820          */
821         read_lock_irq(&zfcp_data.config_lock);
822         unit = zfcp_get_unit_by_lun(port, fcp_lun);
823         read_unlock_irq(&zfcp_data.config_lock);
824         if (unit)
825                 return NULL;
826
827         unit = kzalloc(sizeof (struct zfcp_unit), GFP_KERNEL);
828         if (!unit)
829                 return NULL;
830
831         /* initialise reference count stuff */
832         atomic_set(&unit->refcount, 0);
833         init_waitqueue_head(&unit->remove_wq);
834
835         unit->port = port;
836         unit->fcp_lun = fcp_lun;
837
838         /* setup for sysfs registration */
839         snprintf(unit->sysfs_device.bus_id, BUS_ID_SIZE, "0x%016llx", fcp_lun);
840         unit->sysfs_device.parent = &port->sysfs_device;
841         unit->sysfs_device.release = zfcp_sysfs_unit_release;
842         dev_set_drvdata(&unit->sysfs_device, unit);
843
844         /* mark unit unusable as long as sysfs registration is not complete */
845         atomic_set_mask(ZFCP_STATUS_COMMON_REMOVE, &unit->status);
846
847         spin_lock_init(&unit->latencies.lock);
848         unit->latencies.write.channel.min = 0xFFFFFFFF;
849         unit->latencies.write.fabric.min = 0xFFFFFFFF;
850         unit->latencies.read.channel.min = 0xFFFFFFFF;
851         unit->latencies.read.fabric.min = 0xFFFFFFFF;
852         unit->latencies.cmd.channel.min = 0xFFFFFFFF;
853         unit->latencies.cmd.fabric.min = 0xFFFFFFFF;
854
855         if (device_register(&unit->sysfs_device)) {
856                 kfree(unit);
857                 return NULL;
858         }
859
860         if (zfcp_sysfs_unit_create_files(&unit->sysfs_device)) {
861                 device_unregister(&unit->sysfs_device);
862                 return NULL;
863         }
864
865         zfcp_unit_get(unit);
866         unit->scsi_lun = scsilun_to_int((struct scsi_lun *)&unit->fcp_lun);
867
868         write_lock_irq(&zfcp_data.config_lock);
869         list_add_tail(&unit->list, &port->unit_list_head);
870         atomic_clear_mask(ZFCP_STATUS_COMMON_REMOVE, &unit->status);
871         atomic_set_mask(ZFCP_STATUS_COMMON_RUNNING, &unit->status);
872         write_unlock_irq(&zfcp_data.config_lock);
873
874         port->units++;
875         zfcp_port_get(port);
876
877         return unit;
878 }
879
880 void
881 zfcp_unit_dequeue(struct zfcp_unit *unit)
882 {
883         zfcp_unit_wait(unit);
884         write_lock_irq(&zfcp_data.config_lock);
885         list_del(&unit->list);
886         write_unlock_irq(&zfcp_data.config_lock);
887         unit->port->units--;
888         zfcp_port_put(unit->port);
889         zfcp_sysfs_unit_remove_files(&unit->sysfs_device);
890         device_unregister(&unit->sysfs_device);
891 }
892
893 /*
894  * Allocates a combined QTCB/fsf_req buffer for erp actions and fcp/SCSI
895  * commands.
896  * It also genrates fcp-nameserver request/response buffer and unsolicited
897  * status read fsf_req buffers.
898  *
899  * locks:       must only be called with zfcp_data.config_sema taken
900  */
901 static int
902 zfcp_allocate_low_mem_buffers(struct zfcp_adapter *adapter)
903 {
904         adapter->pool.fsf_req_erp =
905                 mempool_create_slab_pool(ZFCP_POOL_FSF_REQ_ERP_NR,
906                                          zfcp_data.fsf_req_qtcb_cache);
907         if (!adapter->pool.fsf_req_erp)
908                 return -ENOMEM;
909
910         adapter->pool.fsf_req_scsi =
911                 mempool_create_slab_pool(ZFCP_POOL_FSF_REQ_SCSI_NR,
912                                          zfcp_data.fsf_req_qtcb_cache);
913         if (!adapter->pool.fsf_req_scsi)
914                 return -ENOMEM;
915
916         adapter->pool.fsf_req_abort =
917                 mempool_create_slab_pool(ZFCP_POOL_FSF_REQ_ABORT_NR,
918                                          zfcp_data.fsf_req_qtcb_cache);
919         if (!adapter->pool.fsf_req_abort)
920                 return -ENOMEM;
921
922         adapter->pool.fsf_req_status_read =
923                 mempool_create_kmalloc_pool(ZFCP_POOL_STATUS_READ_NR,
924                                             sizeof(struct zfcp_fsf_req));
925         if (!adapter->pool.fsf_req_status_read)
926                 return -ENOMEM;
927
928         adapter->pool.data_status_read =
929                 mempool_create_slab_pool(ZFCP_POOL_STATUS_READ_NR,
930                                          zfcp_data.sr_buffer_cache);
931         if (!adapter->pool.data_status_read)
932                 return -ENOMEM;
933
934         adapter->pool.data_gid_pn =
935                 mempool_create_slab_pool(ZFCP_POOL_DATA_GID_PN_NR,
936                                          zfcp_data.gid_pn_cache);
937         if (!adapter->pool.data_gid_pn)
938                 return -ENOMEM;
939
940         return 0;
941 }
942
943 /**
944  * zfcp_free_low_mem_buffers - free memory pools of an adapter
945  * @adapter: pointer to zfcp_adapter for which memory pools should be freed
946  * locking:  zfcp_data.config_sema must be held
947  */
948 static void
949 zfcp_free_low_mem_buffers(struct zfcp_adapter *adapter)
950 {
951         if (adapter->pool.fsf_req_erp)
952                 mempool_destroy(adapter->pool.fsf_req_erp);
953         if (adapter->pool.fsf_req_scsi)
954                 mempool_destroy(adapter->pool.fsf_req_scsi);
955         if (adapter->pool.fsf_req_abort)
956                 mempool_destroy(adapter->pool.fsf_req_abort);
957         if (adapter->pool.fsf_req_status_read)
958                 mempool_destroy(adapter->pool.fsf_req_status_read);
959         if (adapter->pool.data_status_read)
960                 mempool_destroy(adapter->pool.data_status_read);
961         if (adapter->pool.data_gid_pn)
962                 mempool_destroy(adapter->pool.data_gid_pn);
963 }
964
965 static void zfcp_dummy_release(struct device *dev)
966 {
967         return;
968 }
969
970 int zfcp_status_read_refill(struct zfcp_adapter *adapter)
971 {
972         while (atomic_read(&adapter->stat_miss) > 0)
973                 if (zfcp_fsf_status_read(adapter, ZFCP_WAIT_FOR_SBAL))
974                         break;
975         else
976                 atomic_dec(&adapter->stat_miss);
977
978         if (ZFCP_STATUS_READS_RECOM <= atomic_read(&adapter->stat_miss)) {
979                 zfcp_erp_adapter_reopen(adapter, 0, 103, NULL);
980                 return 1;
981         }
982         return 0;
983 }
984
985 static void _zfcp_status_read_scheduler(struct work_struct *work)
986 {
987         zfcp_status_read_refill(container_of(work, struct zfcp_adapter,
988                                              stat_work));
989 }
990
991 /*
992  * Enqueues an adapter at the end of the adapter list in the driver data.
993  * All adapter internal structures are set up.
994  * Proc-fs entries are also created.
995  *
996  * returns:     0             if a new adapter was successfully enqueued
997  *              ZFCP_KNOWN    if an adapter with this devno was already present
998  *              -ENOMEM       if alloc failed
999  * locks:       config_sema must be held to serialise changes to the adapter list
1000  */
1001 struct zfcp_adapter *
1002 zfcp_adapter_enqueue(struct ccw_device *ccw_device)
1003 {
1004         int retval = 0;
1005         struct zfcp_adapter *adapter;
1006
1007         /*
1008          * Note: It is safe to release the list_lock, as any list changes
1009          * are protected by the config_sema, which must be held to get here
1010          */
1011
1012         /* try to allocate new adapter data structure (zeroed) */
1013         adapter = kzalloc(sizeof (struct zfcp_adapter), GFP_KERNEL);
1014         if (!adapter) {
1015                 ZFCP_LOG_INFO("error: allocation of base adapter "
1016                               "structure failed\n");
1017                 goto out;
1018         }
1019
1020         ccw_device->handler = NULL;
1021
1022         /* save ccw_device pointer */
1023         adapter->ccw_device = ccw_device;
1024
1025         retval = zfcp_qdio_allocate_queues(adapter);
1026         if (retval)
1027                 goto queues_alloc_failed;
1028
1029         retval = zfcp_qdio_allocate(adapter);
1030         if (retval)
1031                 goto qdio_allocate_failed;
1032
1033         retval = zfcp_allocate_low_mem_buffers(adapter);
1034         if (retval) {
1035                 ZFCP_LOG_INFO("error: pool allocation failed\n");
1036                 goto failed_low_mem_buffers;
1037         }
1038
1039         /* initialise reference count stuff */
1040         atomic_set(&adapter->refcount, 0);
1041         init_waitqueue_head(&adapter->remove_wq);
1042
1043         /* initialise list of ports */
1044         INIT_LIST_HEAD(&adapter->port_list_head);
1045
1046         /* initialise list of ports to be removed */
1047         INIT_LIST_HEAD(&adapter->port_remove_lh);
1048
1049         /* initialize list of fsf requests */
1050         spin_lock_init(&adapter->req_list_lock);
1051         retval = zfcp_reqlist_alloc(adapter);
1052         if (retval) {
1053                 ZFCP_LOG_INFO("request list initialization failed\n");
1054                 goto failed_low_mem_buffers;
1055         }
1056
1057         /* initialize debug locks */
1058
1059         spin_lock_init(&adapter->hba_dbf_lock);
1060         spin_lock_init(&adapter->san_dbf_lock);
1061         spin_lock_init(&adapter->scsi_dbf_lock);
1062         spin_lock_init(&adapter->rec_dbf_lock);
1063
1064         retval = zfcp_adapter_debug_register(adapter);
1065         if (retval)
1066                 goto debug_register_failed;
1067
1068         /* initialize error recovery stuff */
1069
1070         rwlock_init(&adapter->erp_lock);
1071         sema_init(&adapter->erp_ready_sem, 0);
1072         INIT_LIST_HEAD(&adapter->erp_ready_head);
1073         INIT_LIST_HEAD(&adapter->erp_running_head);
1074
1075         /* initialize abort lock */
1076         rwlock_init(&adapter->abort_lock);
1077
1078         /* initialise some erp stuff */
1079         init_waitqueue_head(&adapter->erp_thread_wqh);
1080         init_waitqueue_head(&adapter->erp_done_wqh);
1081
1082         /* initialize lock of associated request queue */
1083         rwlock_init(&adapter->request_queue.queue_lock);
1084         INIT_WORK(&adapter->stat_work, _zfcp_status_read_scheduler);
1085
1086         /* mark adapter unusable as long as sysfs registration is not complete */
1087         atomic_set_mask(ZFCP_STATUS_COMMON_REMOVE, &adapter->status);
1088
1089         dev_set_drvdata(&ccw_device->dev, adapter);
1090
1091         if (zfcp_sysfs_adapter_create_files(&ccw_device->dev))
1092                 goto sysfs_failed;
1093
1094         adapter->generic_services.parent = &adapter->ccw_device->dev;
1095         adapter->generic_services.release = zfcp_dummy_release;
1096         snprintf(adapter->generic_services.bus_id, BUS_ID_SIZE,
1097                  "generic_services");
1098
1099         if (device_register(&adapter->generic_services))
1100                 goto generic_services_failed;
1101
1102         /* put allocated adapter at list tail */
1103         write_lock_irq(&zfcp_data.config_lock);
1104         atomic_clear_mask(ZFCP_STATUS_COMMON_REMOVE, &adapter->status);
1105         list_add_tail(&adapter->list, &zfcp_data.adapter_list_head);
1106         write_unlock_irq(&zfcp_data.config_lock);
1107
1108         zfcp_data.adapters++;
1109
1110         goto out;
1111
1112  generic_services_failed:
1113         zfcp_sysfs_adapter_remove_files(&adapter->ccw_device->dev);
1114  sysfs_failed:
1115         zfcp_adapter_debug_unregister(adapter);
1116  debug_register_failed:
1117         dev_set_drvdata(&ccw_device->dev, NULL);
1118         zfcp_reqlist_free(adapter);
1119  failed_low_mem_buffers:
1120         zfcp_free_low_mem_buffers(adapter);
1121         if (qdio_free(ccw_device) != 0)
1122                 ZFCP_LOG_NORMAL("bug: qdio_free for adapter %s failed\n",
1123                                 zfcp_get_busid_by_adapter(adapter));
1124  qdio_allocate_failed:
1125         zfcp_qdio_free_queues(adapter);
1126  queues_alloc_failed:
1127         kfree(adapter);
1128         adapter = NULL;
1129  out:
1130         return adapter;
1131 }
1132
1133 /*
1134  * returns:     0 - struct zfcp_adapter  data structure successfully removed
1135  *              !0 - struct zfcp_adapter  data structure could not be removed
1136  *                      (e.g. still used)
1137  * locks:       adapter list write lock is assumed to be held by caller
1138  */
1139 void
1140 zfcp_adapter_dequeue(struct zfcp_adapter *adapter)
1141 {
1142         int retval = 0;
1143         unsigned long flags;
1144
1145         cancel_work_sync(&adapter->stat_work);
1146         zfcp_adapter_scsi_unregister(adapter);
1147         device_unregister(&adapter->generic_services);
1148         zfcp_sysfs_adapter_remove_files(&adapter->ccw_device->dev);
1149         dev_set_drvdata(&adapter->ccw_device->dev, NULL);
1150         /* sanity check: no pending FSF requests */
1151         spin_lock_irqsave(&adapter->req_list_lock, flags);
1152         retval = zfcp_reqlist_isempty(adapter);
1153         spin_unlock_irqrestore(&adapter->req_list_lock, flags);
1154         if (!retval) {
1155                 ZFCP_LOG_NORMAL("bug: adapter %s (%p) still in use, "
1156                                 "%i requests outstanding\n",
1157                                 zfcp_get_busid_by_adapter(adapter), adapter,
1158                                 atomic_read(&adapter->reqs_active));
1159                 retval = -EBUSY;
1160                 goto out;
1161         }
1162
1163         zfcp_adapter_debug_unregister(adapter);
1164
1165         /* remove specified adapter data structure from list */
1166         write_lock_irq(&zfcp_data.config_lock);
1167         list_del(&adapter->list);
1168         write_unlock_irq(&zfcp_data.config_lock);
1169
1170         /* decrease number of adapters in list */
1171         zfcp_data.adapters--;
1172
1173         ZFCP_LOG_TRACE("adapter %s (%p) removed from list, "
1174                        "%i adapters still in list\n",
1175                        zfcp_get_busid_by_adapter(adapter),
1176                        adapter, zfcp_data.adapters);
1177
1178         retval = qdio_free(adapter->ccw_device);
1179         if (retval)
1180                 ZFCP_LOG_NORMAL("bug: qdio_free for adapter %s failed\n",
1181                                 zfcp_get_busid_by_adapter(adapter));
1182
1183         zfcp_free_low_mem_buffers(adapter);
1184         /* free memory of adapter data structure and queues */
1185         zfcp_qdio_free_queues(adapter);
1186         zfcp_reqlist_free(adapter);
1187         kfree(adapter->fc_stats);
1188         kfree(adapter->stats_reset_data);
1189         ZFCP_LOG_TRACE("freeing adapter structure\n");
1190         kfree(adapter);
1191  out:
1192         return;
1193 }
1194
1195 /**
1196  * zfcp_port_enqueue - enqueue port to port list of adapter
1197  * @adapter: adapter where remote port is added
1198  * @wwpn: WWPN of the remote port to be enqueued
1199  * @status: initial status for the port
1200  * @d_id: destination id of the remote port to be enqueued
1201  * Return: pointer to enqueued port on success, NULL on error
1202  * Locks: config_sema must be held to serialize changes to the port list
1203  *
1204  * All port internal structures are set up and the sysfs entry is generated.
1205  * d_id is used to enqueue ports with a well known address like the Directory
1206  * Service for nameserver lookup.
1207  */
1208 struct zfcp_port *
1209 zfcp_port_enqueue(struct zfcp_adapter *adapter, wwn_t wwpn, u32 status,
1210                   u32 d_id)
1211 {
1212         struct zfcp_port *port;
1213         int check_wwpn;
1214
1215         check_wwpn = !(status & ZFCP_STATUS_PORT_NO_WWPN);
1216         /*
1217          * check that there is no port with this WWPN already in list
1218          */
1219         if (check_wwpn) {
1220                 read_lock_irq(&zfcp_data.config_lock);
1221                 port = zfcp_get_port_by_wwpn(adapter, wwpn);
1222                 read_unlock_irq(&zfcp_data.config_lock);
1223                 if (port)
1224                         return NULL;
1225         }
1226
1227         port = kzalloc(sizeof (struct zfcp_port), GFP_KERNEL);
1228         if (!port)
1229                 return NULL;
1230
1231         /* initialise reference count stuff */
1232         atomic_set(&port->refcount, 0);
1233         init_waitqueue_head(&port->remove_wq);
1234
1235         INIT_LIST_HEAD(&port->unit_list_head);
1236         INIT_LIST_HEAD(&port->unit_remove_lh);
1237
1238         port->adapter = adapter;
1239
1240         if (check_wwpn)
1241                 port->wwpn = wwpn;
1242
1243         atomic_set_mask(status, &port->status);
1244
1245         /* setup for sysfs registration */
1246         if (status & ZFCP_STATUS_PORT_WKA) {
1247                 switch (d_id) {
1248                 case ZFCP_DID_DIRECTORY_SERVICE:
1249                         snprintf(port->sysfs_device.bus_id, BUS_ID_SIZE,
1250                                  "directory");
1251                         break;
1252                 case ZFCP_DID_MANAGEMENT_SERVICE:
1253                         snprintf(port->sysfs_device.bus_id, BUS_ID_SIZE,
1254                                  "management");
1255                         break;
1256                 case ZFCP_DID_KEY_DISTRIBUTION_SERVICE:
1257                         snprintf(port->sysfs_device.bus_id, BUS_ID_SIZE,
1258                                  "key_distribution");
1259                         break;
1260                 case ZFCP_DID_ALIAS_SERVICE:
1261                         snprintf(port->sysfs_device.bus_id, BUS_ID_SIZE,
1262                                  "alias");
1263                         break;
1264                 case ZFCP_DID_TIME_SERVICE:
1265                         snprintf(port->sysfs_device.bus_id, BUS_ID_SIZE,
1266                                  "time");
1267                         break;
1268                 default:
1269                         kfree(port);
1270                         return NULL;
1271                 }
1272                 port->d_id = d_id;
1273                 port->sysfs_device.parent = &adapter->generic_services;
1274         } else {
1275                 snprintf(port->sysfs_device.bus_id,
1276                          BUS_ID_SIZE, "0x%016llx", wwpn);
1277                 port->sysfs_device.parent = &adapter->ccw_device->dev;
1278         }
1279         port->sysfs_device.release = zfcp_sysfs_port_release;
1280         dev_set_drvdata(&port->sysfs_device, port);
1281
1282         /* mark port unusable as long as sysfs registration is not complete */
1283         atomic_set_mask(ZFCP_STATUS_COMMON_REMOVE, &port->status);
1284
1285         if (device_register(&port->sysfs_device)) {
1286                 kfree(port);
1287                 return NULL;
1288         }
1289
1290         if (zfcp_sysfs_port_create_files(&port->sysfs_device, status)) {
1291                 device_unregister(&port->sysfs_device);
1292                 return NULL;
1293         }
1294
1295         zfcp_port_get(port);
1296
1297         write_lock_irq(&zfcp_data.config_lock);
1298         list_add_tail(&port->list, &adapter->port_list_head);
1299         atomic_clear_mask(ZFCP_STATUS_COMMON_REMOVE, &port->status);
1300         atomic_set_mask(ZFCP_STATUS_COMMON_RUNNING, &port->status);
1301         if (d_id == ZFCP_DID_DIRECTORY_SERVICE)
1302                 if (!adapter->nameserver_port)
1303                         adapter->nameserver_port = port;
1304         adapter->ports++;
1305         write_unlock_irq(&zfcp_data.config_lock);
1306
1307         zfcp_adapter_get(adapter);
1308
1309         return port;
1310 }
1311
1312 void
1313 zfcp_port_dequeue(struct zfcp_port *port)
1314 {
1315         zfcp_port_wait(port);
1316         write_lock_irq(&zfcp_data.config_lock);
1317         list_del(&port->list);
1318         port->adapter->ports--;
1319         write_unlock_irq(&zfcp_data.config_lock);
1320         if (port->rport)
1321                 fc_remote_port_delete(port->rport);
1322         port->rport = NULL;
1323         zfcp_adapter_put(port->adapter);
1324         zfcp_sysfs_port_remove_files(&port->sysfs_device,
1325                                      atomic_read(&port->status));
1326         device_unregister(&port->sysfs_device);
1327 }
1328
1329 /* Enqueues a nameserver port */
1330 int
1331 zfcp_nameserver_enqueue(struct zfcp_adapter *adapter)
1332 {
1333         struct zfcp_port *port;
1334
1335         port = zfcp_port_enqueue(adapter, 0, ZFCP_STATUS_PORT_WKA,
1336                                  ZFCP_DID_DIRECTORY_SERVICE);
1337         if (!port) {
1338                 ZFCP_LOG_INFO("error: enqueue of nameserver port for "
1339                               "adapter %s failed\n",
1340                               zfcp_get_busid_by_adapter(adapter));
1341                 return -ENXIO;
1342         }
1343         zfcp_port_put(port);
1344
1345         return 0;
1346 }
1347
1348 #undef ZFCP_LOG_AREA