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