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