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