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