[SCSI] zfcp: attach and release SAN nameserver port on demand
[linux-2.6] / drivers / s390 / scsi / zfcp_aux.c
1 /*
2  * zfcp device driver
3  *
4  * Module interface and handling of zfcp data structures.
5  *
6  * Copyright IBM Corporation 2002, 2008
7  */
8
9 /*
10  * Driver authors:
11  *            Martin Peschke (originator of the driver)
12  *            Raimund Schroeder
13  *            Aron Zeh
14  *            Wolfgang Taphorn
15  *            Stefan Bader
16  *            Heiko Carstens (kernel 2.6 port of the driver)
17  *            Andreas Herrmann
18  *            Maxim Shchetynin
19  *            Volker Sameske
20  *            Ralph Wuerthner
21  *            Michael Loehr
22  *            Swen Schillig
23  *            Christof Schmitt
24  *            Martin Petermann
25  *            Sven Schuetz
26  */
27
28 #include <linux/miscdevice.h>
29 #include "zfcp_ext.h"
30
31 static char *device;
32
33 MODULE_AUTHOR("IBM Deutschland Entwicklung GmbH - linux390@de.ibm.com");
34 MODULE_DESCRIPTION("FCP HBA driver");
35 MODULE_LICENSE("GPL");
36
37 module_param(device, charp, 0400);
38 MODULE_PARM_DESC(device, "specify initial device");
39
40 static int zfcp_reqlist_alloc(struct zfcp_adapter *adapter)
41 {
42         int idx;
43
44         adapter->req_list = kcalloc(REQUEST_LIST_SIZE, sizeof(struct list_head),
45                                     GFP_KERNEL);
46         if (!adapter->req_list)
47                 return -ENOMEM;
48
49         for (idx = 0; idx < REQUEST_LIST_SIZE; idx++)
50                 INIT_LIST_HEAD(&adapter->req_list[idx]);
51         return 0;
52 }
53
54 /**
55  * zfcp_reqlist_isempty - is the request list empty
56  * @adapter: pointer to struct zfcp_adapter
57  *
58  * Returns: true if list is empty, false otherwise
59  */
60 int zfcp_reqlist_isempty(struct zfcp_adapter *adapter)
61 {
62         unsigned int idx;
63
64         for (idx = 0; idx < REQUEST_LIST_SIZE; idx++)
65                 if (!list_empty(&adapter->req_list[idx]))
66                         return 0;
67         return 1;
68 }
69
70 static int __init zfcp_device_setup(char *devstr)
71 {
72         char *token;
73         char *str;
74
75         if (!devstr)
76                 return 0;
77
78         /* duplicate devstr and keep the original for sysfs presentation*/
79         str = kmalloc(strlen(devstr) + 1, GFP_KERNEL);
80         if (!str)
81                 return 0;
82
83         strcpy(str, devstr);
84
85         token = strsep(&str, ",");
86         if (!token || strlen(token) >= BUS_ID_SIZE)
87                 goto err_out;
88         strncpy(zfcp_data.init_busid, token, BUS_ID_SIZE);
89
90         token = strsep(&str, ",");
91         if (!token || strict_strtoull(token, 0, &zfcp_data.init_wwpn))
92                 goto err_out;
93
94         token = strsep(&str, ",");
95         if (!token || strict_strtoull(token, 0, &zfcp_data.init_fcp_lun))
96                 goto err_out;
97
98         kfree(str);
99         return 1;
100
101  err_out:
102         kfree(str);
103         pr_err("zfcp: %s is not a valid SCSI device\n", devstr);
104         return 0;
105 }
106
107 static struct zfcp_adapter *zfcp_get_adapter_by_busid(char *bus_id)
108 {
109         struct zfcp_adapter *adapter;
110
111         list_for_each_entry(adapter, &zfcp_data.adapter_list_head, list)
112                 if ((strncmp(bus_id, adapter->ccw_device->dev.bus_id,
113                              BUS_ID_SIZE) == 0) &&
114                     !(atomic_read(&adapter->status) &
115                       ZFCP_STATUS_COMMON_REMOVE))
116                     return adapter;
117         return NULL;
118 }
119
120 static void __init zfcp_init_device_configure(void)
121 {
122         struct zfcp_adapter *adapter;
123         struct zfcp_port *port;
124         struct zfcp_unit *unit;
125
126         down(&zfcp_data.config_sema);
127         read_lock_irq(&zfcp_data.config_lock);
128         adapter = zfcp_get_adapter_by_busid(zfcp_data.init_busid);
129         if (adapter)
130                 zfcp_adapter_get(adapter);
131         read_unlock_irq(&zfcp_data.config_lock);
132
133         if (!adapter)
134                 goto out_adapter;
135         port = zfcp_port_enqueue(adapter, zfcp_data.init_wwpn, 0, 0);
136         if (IS_ERR(port))
137                 goto out_port;
138         unit = zfcp_unit_enqueue(port, zfcp_data.init_fcp_lun);
139         if (IS_ERR(unit))
140                 goto out_unit;
141         up(&zfcp_data.config_sema);
142         ccw_device_set_online(adapter->ccw_device);
143         zfcp_erp_wait(adapter);
144         down(&zfcp_data.config_sema);
145         zfcp_unit_put(unit);
146 out_unit:
147         zfcp_port_put(port);
148 out_port:
149         zfcp_adapter_put(adapter);
150 out_adapter:
151         up(&zfcp_data.config_sema);
152         return;
153 }
154
155 static struct kmem_cache *zfcp_cache_create(int size, char *name)
156 {
157         int align = 1;
158         while ((size - align) > 0)
159                 align <<= 1;
160         return kmem_cache_create(name , size, align, 0, NULL);
161 }
162
163 static int __init zfcp_module_init(void)
164 {
165         int retval = -ENOMEM;
166
167         zfcp_data.fsf_req_qtcb_cache = zfcp_cache_create(
168                         sizeof(struct zfcp_fsf_req_qtcb), "zfcp_fsf");
169         if (!zfcp_data.fsf_req_qtcb_cache)
170                 goto out;
171
172         zfcp_data.sr_buffer_cache = zfcp_cache_create(
173                         sizeof(struct fsf_status_read_buffer), "zfcp_sr");
174         if (!zfcp_data.sr_buffer_cache)
175                 goto out_sr_cache;
176
177         zfcp_data.gid_pn_cache = zfcp_cache_create(
178                         sizeof(struct zfcp_gid_pn_data), "zfcp_gid");
179         if (!zfcp_data.gid_pn_cache)
180                 goto out_gid_cache;
181
182         INIT_LIST_HEAD(&zfcp_data.adapter_list_head);
183         INIT_LIST_HEAD(&zfcp_data.adapter_remove_lh);
184
185         sema_init(&zfcp_data.config_sema, 1);
186         rwlock_init(&zfcp_data.config_lock);
187
188         zfcp_data.scsi_transport_template =
189                 fc_attach_transport(&zfcp_transport_functions);
190         if (!zfcp_data.scsi_transport_template)
191                 goto out_transport;
192
193         retval = misc_register(&zfcp_cfdc_misc);
194         if (retval) {
195                 pr_err("zfcp: Registering the misc device zfcp_cfdc failed\n");
196                 goto out_misc;
197         }
198
199         retval = zfcp_ccw_register();
200         if (retval) {
201                 pr_err("zfcp: The zfcp device driver could not register with "
202                        "the common I/O layer\n");
203                 goto out_ccw_register;
204         }
205
206         if (zfcp_device_setup(device))
207                 zfcp_init_device_configure();
208
209         goto out;
210
211 out_ccw_register:
212         misc_deregister(&zfcp_cfdc_misc);
213 out_misc:
214         fc_release_transport(zfcp_data.scsi_transport_template);
215 out_transport:
216         kmem_cache_destroy(zfcp_data.gid_pn_cache);
217 out_gid_cache:
218         kmem_cache_destroy(zfcp_data.sr_buffer_cache);
219 out_sr_cache:
220         kmem_cache_destroy(zfcp_data.fsf_req_qtcb_cache);
221 out:
222         return retval;
223 }
224
225 module_init(zfcp_module_init);
226
227 /**
228  * zfcp_get_unit_by_lun - find unit in unit list of port by FCP LUN
229  * @port: pointer to port to search for unit
230  * @fcp_lun: FCP LUN to search for
231  *
232  * Returns: pointer to zfcp_unit or NULL
233  */
234 struct zfcp_unit *zfcp_get_unit_by_lun(struct zfcp_port *port,
235                                        fcp_lun_t fcp_lun)
236 {
237         struct zfcp_unit *unit;
238
239         list_for_each_entry(unit, &port->unit_list_head, list)
240                 if ((unit->fcp_lun == fcp_lun) &&
241                     !(atomic_read(&unit->status) & ZFCP_STATUS_COMMON_REMOVE))
242                     return unit;
243         return NULL;
244 }
245
246 /**
247  * zfcp_get_port_by_wwpn - find port in port list of adapter by wwpn
248  * @adapter: pointer to adapter to search for port
249  * @wwpn: wwpn to search for
250  *
251  * Returns: pointer to zfcp_port or NULL
252  */
253 struct zfcp_port *zfcp_get_port_by_wwpn(struct zfcp_adapter *adapter,
254                                         wwn_t wwpn)
255 {
256         struct zfcp_port *port;
257
258         list_for_each_entry(port, &adapter->port_list_head, list)
259                 if ((port->wwpn == wwpn) && !(atomic_read(&port->status) &
260                       (ZFCP_STATUS_PORT_NO_WWPN | ZFCP_STATUS_COMMON_REMOVE)))
261                         return port;
262         return NULL;
263 }
264
265 static void zfcp_sysfs_unit_release(struct device *dev)
266 {
267         kfree(container_of(dev, struct zfcp_unit, sysfs_device));
268 }
269
270 /**
271  * zfcp_unit_enqueue - enqueue unit to unit list of a port.
272  * @port: pointer to port where unit is added
273  * @fcp_lun: FCP LUN of unit to be enqueued
274  * Returns: pointer to enqueued unit on success, ERR_PTR on error
275  * Locks: config_sema must be held to serialize changes to the unit list
276  *
277  * Sets up some unit internal structures and creates sysfs entry.
278  */
279 struct zfcp_unit *zfcp_unit_enqueue(struct zfcp_port *port, fcp_lun_t fcp_lun)
280 {
281         struct zfcp_unit *unit;
282
283         unit = kzalloc(sizeof(struct zfcp_unit), GFP_KERNEL);
284         if (!unit)
285                 return ERR_PTR(-ENOMEM);
286
287         atomic_set(&unit->refcount, 0);
288         init_waitqueue_head(&unit->remove_wq);
289
290         unit->port = port;
291         unit->fcp_lun = fcp_lun;
292
293         snprintf(unit->sysfs_device.bus_id, BUS_ID_SIZE, "0x%016llx", fcp_lun);
294         unit->sysfs_device.parent = &port->sysfs_device;
295         unit->sysfs_device.release = zfcp_sysfs_unit_release;
296         dev_set_drvdata(&unit->sysfs_device, unit);
297
298         /* mark unit unusable as long as sysfs registration is not complete */
299         atomic_set_mask(ZFCP_STATUS_COMMON_REMOVE, &unit->status);
300
301         spin_lock_init(&unit->latencies.lock);
302         unit->latencies.write.channel.min = 0xFFFFFFFF;
303         unit->latencies.write.fabric.min = 0xFFFFFFFF;
304         unit->latencies.read.channel.min = 0xFFFFFFFF;
305         unit->latencies.read.fabric.min = 0xFFFFFFFF;
306         unit->latencies.cmd.channel.min = 0xFFFFFFFF;
307         unit->latencies.cmd.fabric.min = 0xFFFFFFFF;
308
309         read_lock_irq(&zfcp_data.config_lock);
310         if (zfcp_get_unit_by_lun(port, fcp_lun)) {
311                 read_unlock_irq(&zfcp_data.config_lock);
312                 goto err_out_free;
313         }
314         read_unlock_irq(&zfcp_data.config_lock);
315
316         if (device_register(&unit->sysfs_device))
317                 goto err_out_free;
318
319         if (sysfs_create_group(&unit->sysfs_device.kobj,
320                                &zfcp_sysfs_unit_attrs)) {
321                 device_unregister(&unit->sysfs_device);
322                 return ERR_PTR(-EIO);
323         }
324
325         zfcp_unit_get(unit);
326         unit->scsi_lun = scsilun_to_int((struct scsi_lun *)&unit->fcp_lun);
327
328         write_lock_irq(&zfcp_data.config_lock);
329         list_add_tail(&unit->list, &port->unit_list_head);
330         atomic_clear_mask(ZFCP_STATUS_COMMON_REMOVE, &unit->status);
331         atomic_set_mask(ZFCP_STATUS_COMMON_RUNNING, &unit->status);
332
333         write_unlock_irq(&zfcp_data.config_lock);
334
335         port->units++;
336         zfcp_port_get(port);
337
338         return unit;
339
340 err_out_free:
341         kfree(unit);
342         return ERR_PTR(-EINVAL);
343 }
344
345 /**
346  * zfcp_unit_dequeue - dequeue unit
347  * @unit: pointer to zfcp_unit
348  *
349  * waits until all work is done on unit and removes it then from the unit->list
350  * of the associated port.
351  */
352 void zfcp_unit_dequeue(struct zfcp_unit *unit)
353 {
354         wait_event(unit->remove_wq, atomic_read(&unit->refcount) == 0);
355         write_lock_irq(&zfcp_data.config_lock);
356         list_del(&unit->list);
357         write_unlock_irq(&zfcp_data.config_lock);
358         unit->port->units--;
359         zfcp_port_put(unit->port);
360         sysfs_remove_group(&unit->sysfs_device.kobj, &zfcp_sysfs_unit_attrs);
361         device_unregister(&unit->sysfs_device);
362 }
363
364 static int zfcp_allocate_low_mem_buffers(struct zfcp_adapter *adapter)
365 {
366         /* must only be called with zfcp_data.config_sema taken */
367         adapter->pool.fsf_req_erp =
368                 mempool_create_slab_pool(1, zfcp_data.fsf_req_qtcb_cache);
369         if (!adapter->pool.fsf_req_erp)
370                 return -ENOMEM;
371
372         adapter->pool.fsf_req_scsi =
373                 mempool_create_slab_pool(1, zfcp_data.fsf_req_qtcb_cache);
374         if (!adapter->pool.fsf_req_scsi)
375                 return -ENOMEM;
376
377         adapter->pool.fsf_req_abort =
378                 mempool_create_slab_pool(1, zfcp_data.fsf_req_qtcb_cache);
379         if (!adapter->pool.fsf_req_abort)
380                 return -ENOMEM;
381
382         adapter->pool.fsf_req_status_read =
383                 mempool_create_kmalloc_pool(FSF_STATUS_READS_RECOM,
384                                             sizeof(struct zfcp_fsf_req));
385         if (!adapter->pool.fsf_req_status_read)
386                 return -ENOMEM;
387
388         adapter->pool.data_status_read =
389                 mempool_create_slab_pool(FSF_STATUS_READS_RECOM,
390                                          zfcp_data.sr_buffer_cache);
391         if (!adapter->pool.data_status_read)
392                 return -ENOMEM;
393
394         adapter->pool.data_gid_pn =
395                 mempool_create_slab_pool(1, zfcp_data.gid_pn_cache);
396         if (!adapter->pool.data_gid_pn)
397                 return -ENOMEM;
398
399         return 0;
400 }
401
402 static void zfcp_free_low_mem_buffers(struct zfcp_adapter *adapter)
403 {
404         /* zfcp_data.config_sema must be held */
405         if (adapter->pool.fsf_req_erp)
406                 mempool_destroy(adapter->pool.fsf_req_erp);
407         if (adapter->pool.fsf_req_scsi)
408                 mempool_destroy(adapter->pool.fsf_req_scsi);
409         if (adapter->pool.fsf_req_abort)
410                 mempool_destroy(adapter->pool.fsf_req_abort);
411         if (adapter->pool.fsf_req_status_read)
412                 mempool_destroy(adapter->pool.fsf_req_status_read);
413         if (adapter->pool.data_status_read)
414                 mempool_destroy(adapter->pool.data_status_read);
415         if (adapter->pool.data_gid_pn)
416                 mempool_destroy(adapter->pool.data_gid_pn);
417 }
418
419 static void zfcp_dummy_release(struct device *dev)
420 {
421         return;
422 }
423
424 /**
425  * zfcp_status_read_refill - refill the long running status_read_requests
426  * @adapter: ptr to struct zfcp_adapter for which the buffers should be refilled
427  *
428  * Returns: 0 on success, 1 otherwise
429  *
430  * if there are 16 or more status_read requests missing an adapter_reopen
431  * is triggered
432  */
433 int zfcp_status_read_refill(struct zfcp_adapter *adapter)
434 {
435         while (atomic_read(&adapter->stat_miss) > 0)
436                 if (zfcp_fsf_status_read(adapter)) {
437                         if (atomic_read(&adapter->stat_miss) >= 16) {
438                                 zfcp_erp_adapter_reopen(adapter, 0, 103, NULL);
439                                 return 1;
440                         }
441                         break;
442                 } else
443                         atomic_dec(&adapter->stat_miss);
444         return 0;
445 }
446
447 static void _zfcp_status_read_scheduler(struct work_struct *work)
448 {
449         zfcp_status_read_refill(container_of(work, struct zfcp_adapter,
450                                              stat_work));
451 }
452
453 /**
454  * zfcp_adapter_enqueue - enqueue a new adapter to the list
455  * @ccw_device: pointer to the struct cc_device
456  *
457  * Returns:     0             if a new adapter was successfully enqueued
458  *              -ENOMEM       if alloc failed
459  * Enqueues an adapter at the end of the adapter list in the driver data.
460  * All adapter internal structures are set up.
461  * Proc-fs entries are also created.
462  * locks:       config_sema must be held to serialise changes to the adapter list
463  */
464 int zfcp_adapter_enqueue(struct ccw_device *ccw_device)
465 {
466         struct zfcp_adapter *adapter;
467
468         /*
469          * Note: It is safe to release the list_lock, as any list changes
470          * are protected by the config_sema, which must be held to get here
471          */
472
473         adapter = kzalloc(sizeof(struct zfcp_adapter), GFP_KERNEL);
474         if (!adapter)
475                 return -ENOMEM;
476
477         ccw_device->handler = NULL;
478         adapter->ccw_device = ccw_device;
479         atomic_set(&adapter->refcount, 0);
480
481         if (zfcp_qdio_allocate(adapter))
482                 goto qdio_allocate_failed;
483
484         if (zfcp_allocate_low_mem_buffers(adapter))
485                 goto failed_low_mem_buffers;
486
487         if (zfcp_reqlist_alloc(adapter))
488                 goto failed_low_mem_buffers;
489
490         if (zfcp_adapter_debug_register(adapter))
491                 goto debug_register_failed;
492
493         init_waitqueue_head(&adapter->remove_wq);
494         init_waitqueue_head(&adapter->erp_thread_wqh);
495         init_waitqueue_head(&adapter->erp_done_wqh);
496
497         INIT_LIST_HEAD(&adapter->port_list_head);
498         INIT_LIST_HEAD(&adapter->port_remove_lh);
499         INIT_LIST_HEAD(&adapter->erp_ready_head);
500         INIT_LIST_HEAD(&adapter->erp_running_head);
501
502         spin_lock_init(&adapter->req_list_lock);
503
504         spin_lock_init(&adapter->hba_dbf_lock);
505         spin_lock_init(&adapter->san_dbf_lock);
506         spin_lock_init(&adapter->scsi_dbf_lock);
507         spin_lock_init(&adapter->rec_dbf_lock);
508         spin_lock_init(&adapter->req_q.lock);
509
510         rwlock_init(&adapter->erp_lock);
511         rwlock_init(&adapter->abort_lock);
512
513         sema_init(&adapter->erp_ready_sem, 0);
514
515         INIT_WORK(&adapter->stat_work, _zfcp_status_read_scheduler);
516         INIT_WORK(&adapter->scan_work, _zfcp_scan_ports_later);
517
518         /* mark adapter unusable as long as sysfs registration is not complete */
519         atomic_set_mask(ZFCP_STATUS_COMMON_REMOVE, &adapter->status);
520
521         dev_set_drvdata(&ccw_device->dev, adapter);
522
523         if (sysfs_create_group(&ccw_device->dev.kobj,
524                                &zfcp_sysfs_adapter_attrs))
525                 goto sysfs_failed;
526
527         adapter->generic_services.parent = &adapter->ccw_device->dev;
528         adapter->generic_services.release = zfcp_dummy_release;
529         snprintf(adapter->generic_services.bus_id, BUS_ID_SIZE,
530                  "generic_services");
531
532         if (device_register(&adapter->generic_services))
533                 goto generic_services_failed;
534
535         write_lock_irq(&zfcp_data.config_lock);
536         atomic_clear_mask(ZFCP_STATUS_COMMON_REMOVE, &adapter->status);
537         list_add_tail(&adapter->list, &zfcp_data.adapter_list_head);
538         write_unlock_irq(&zfcp_data.config_lock);
539
540         zfcp_data.adapters++;
541
542         zfcp_fc_nameserver_init(adapter);
543
544         return 0;
545
546 generic_services_failed:
547         sysfs_remove_group(&ccw_device->dev.kobj,
548                            &zfcp_sysfs_adapter_attrs);
549 sysfs_failed:
550         zfcp_adapter_debug_unregister(adapter);
551 debug_register_failed:
552         dev_set_drvdata(&ccw_device->dev, NULL);
553         kfree(adapter->req_list);
554 failed_low_mem_buffers:
555         zfcp_free_low_mem_buffers(adapter);
556 qdio_allocate_failed:
557         zfcp_qdio_free(adapter);
558         kfree(adapter);
559         return -ENOMEM;
560 }
561
562 /**
563  * zfcp_adapter_dequeue - remove the adapter from the resource list
564  * @adapter: pointer to struct zfcp_adapter which should be removed
565  * locks:       adapter list write lock is assumed to be held by caller
566  */
567 void zfcp_adapter_dequeue(struct zfcp_adapter *adapter)
568 {
569         int retval = 0;
570         unsigned long flags;
571
572         cancel_work_sync(&adapter->scan_work);
573         cancel_work_sync(&adapter->stat_work);
574         zfcp_adapter_scsi_unregister(adapter);
575         device_unregister(&adapter->generic_services);
576         sysfs_remove_group(&adapter->ccw_device->dev.kobj,
577                            &zfcp_sysfs_adapter_attrs);
578         dev_set_drvdata(&adapter->ccw_device->dev, NULL);
579         /* sanity check: no pending FSF requests */
580         spin_lock_irqsave(&adapter->req_list_lock, flags);
581         retval = zfcp_reqlist_isempty(adapter);
582         spin_unlock_irqrestore(&adapter->req_list_lock, flags);
583         if (!retval)
584                 return;
585
586         zfcp_adapter_debug_unregister(adapter);
587
588         /* remove specified adapter data structure from list */
589         write_lock_irq(&zfcp_data.config_lock);
590         list_del(&adapter->list);
591         write_unlock_irq(&zfcp_data.config_lock);
592
593         /* decrease number of adapters in list */
594         zfcp_data.adapters--;
595
596         zfcp_qdio_free(adapter);
597
598         zfcp_free_low_mem_buffers(adapter);
599         kfree(adapter->req_list);
600         kfree(adapter->fc_stats);
601         kfree(adapter->stats_reset_data);
602         kfree(adapter);
603 }
604
605 static void zfcp_sysfs_port_release(struct device *dev)
606 {
607         kfree(container_of(dev, struct zfcp_port, sysfs_device));
608 }
609
610 /**
611  * zfcp_port_enqueue - enqueue port to port list of adapter
612  * @adapter: adapter where remote port is added
613  * @wwpn: WWPN of the remote port to be enqueued
614  * @status: initial status for the port
615  * @d_id: destination id of the remote port to be enqueued
616  * Returns: pointer to enqueued port on success, ERR_PTR on error
617  * Locks: config_sema must be held to serialize changes to the port list
618  *
619  * All port internal structures are set up and the sysfs entry is generated.
620  * d_id is used to enqueue ports with a well known address like the Directory
621  * Service for nameserver lookup.
622  */
623 struct zfcp_port *zfcp_port_enqueue(struct zfcp_adapter *adapter, wwn_t wwpn,
624                                      u32 status, u32 d_id)
625 {
626         struct zfcp_port *port;
627         int retval;
628
629         port = kzalloc(sizeof(struct zfcp_port), GFP_KERNEL);
630         if (!port)
631                 return ERR_PTR(-ENOMEM);
632
633         init_waitqueue_head(&port->remove_wq);
634
635         INIT_LIST_HEAD(&port->unit_list_head);
636         INIT_LIST_HEAD(&port->unit_remove_lh);
637         INIT_WORK(&port->gid_pn_work, zfcp_erp_port_strategy_open_lookup);
638
639         port->adapter = adapter;
640         port->d_id = d_id;
641         port->wwpn = wwpn;
642
643         /* mark port unusable as long as sysfs registration is not complete */
644         atomic_set_mask(status | ZFCP_STATUS_COMMON_REMOVE, &port->status);
645         atomic_set(&port->refcount, 0);
646
647         snprintf(port->sysfs_device.bus_id, BUS_ID_SIZE, "0x%016llx", wwpn);
648         port->sysfs_device.parent = &adapter->ccw_device->dev;
649
650         port->sysfs_device.release = zfcp_sysfs_port_release;
651         dev_set_drvdata(&port->sysfs_device, port);
652
653         read_lock_irq(&zfcp_data.config_lock);
654         if (!(status & ZFCP_STATUS_PORT_NO_WWPN))
655                 if (zfcp_get_port_by_wwpn(adapter, wwpn)) {
656                         read_unlock_irq(&zfcp_data.config_lock);
657                         goto err_out_free;
658                 }
659         read_unlock_irq(&zfcp_data.config_lock);
660
661         if (device_register(&port->sysfs_device))
662                 goto err_out_free;
663
664         retval = sysfs_create_group(&port->sysfs_device.kobj,
665                                     &zfcp_sysfs_port_attrs);
666
667         if (retval) {
668                 device_unregister(&port->sysfs_device);
669                 goto err_out;
670         }
671
672         zfcp_port_get(port);
673
674         write_lock_irq(&zfcp_data.config_lock);
675         list_add_tail(&port->list, &adapter->port_list_head);
676         atomic_clear_mask(ZFCP_STATUS_COMMON_REMOVE, &port->status);
677         atomic_set_mask(ZFCP_STATUS_COMMON_RUNNING, &port->status);
678         adapter->ports++;
679
680         write_unlock_irq(&zfcp_data.config_lock);
681
682         zfcp_adapter_get(adapter);
683         return port;
684
685 err_out_free:
686         kfree(port);
687 err_out:
688         return ERR_PTR(-EINVAL);
689 }
690
691 /**
692  * zfcp_port_dequeue - dequeues a port from the port list of the adapter
693  * @port: pointer to struct zfcp_port which should be removed
694  */
695 void zfcp_port_dequeue(struct zfcp_port *port)
696 {
697         wait_event(port->remove_wq, atomic_read(&port->refcount) == 0);
698         write_lock_irq(&zfcp_data.config_lock);
699         list_del(&port->list);
700         port->adapter->ports--;
701         write_unlock_irq(&zfcp_data.config_lock);
702         if (port->rport)
703                 fc_remote_port_delete(port->rport);
704         port->rport = NULL;
705         zfcp_adapter_put(port->adapter);
706         sysfs_remove_group(&port->sysfs_device.kobj, &zfcp_sysfs_port_attrs);
707         device_unregister(&port->sysfs_device);
708 }
709
710 /**
711  * zfcp_sg_free_table - free memory used by scatterlists
712  * @sg: pointer to scatterlist
713  * @count: number of scatterlist which are to be free'ed
714  * the scatterlist are expected to reference pages always
715  */
716 void zfcp_sg_free_table(struct scatterlist *sg, int count)
717 {
718         int i;
719
720         for (i = 0; i < count; i++, sg++)
721                 if (sg)
722                         free_page((unsigned long) sg_virt(sg));
723                 else
724                         break;
725 }
726
727 /**
728  * zfcp_sg_setup_table - init scatterlist and allocate, assign buffers
729  * @sg: pointer to struct scatterlist
730  * @count: number of scatterlists which should be assigned with buffers
731  * of size page
732  *
733  * Returns: 0 on success, -ENOMEM otherwise
734  */
735 int zfcp_sg_setup_table(struct scatterlist *sg, int count)
736 {
737         void *addr;
738         int i;
739
740         sg_init_table(sg, count);
741         for (i = 0; i < count; i++, sg++) {
742                 addr = (void *) get_zeroed_page(GFP_KERNEL);
743                 if (!addr) {
744                         zfcp_sg_free_table(sg, i);
745                         return -ENOMEM;
746                 }
747                 sg_set_buf(sg, addr, PAGE_SIZE);
748         }
749         return 0;
750 }