Merge branch 'for-upstream' of git://git.kernel.org/pub/scm/linux/kernel/git/dvrabel/uwb
[linux-2.6-block.git] / drivers / s390 / scsi / zfcp_aux.c
CommitLineData
1da177e4 1/*
553448f6 2 * zfcp device driver
1da177e4 3 *
553448f6 4 * Module interface and handling of zfcp data structures.
1da177e4 5 *
615f59e0 6 * Copyright IBM Corporation 2002, 2010
1da177e4
LT
7 */
8
4a9d2d8b
AH
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
553448f6
CS
21 * Michael Loehr
22 * Swen Schillig
23 * Christof Schmitt
24 * Martin Petermann
25 * Sven Schuetz
4a9d2d8b
AH
26 */
27
ecf39d42
CS
28#define KMSG_COMPONENT "zfcp"
29#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
30
45633fdc 31#include <linux/miscdevice.h>
bd43a42b 32#include <linux/seq_file.h>
1da177e4 33#include "zfcp_ext.h"
dbf5dfe9 34#include "zfcp_fc.h"
b6bd2fb9 35#include "zfcp_reqlist.h"
1da177e4 36
98df67b3
KS
37#define ZFCP_BUS_ID_SIZE 20
38
4a9d2d8b 39MODULE_AUTHOR("IBM Deutschland Entwicklung GmbH - linux390@de.ibm.com");
317e6b65 40MODULE_DESCRIPTION("FCP HBA driver");
1da177e4
LT
41MODULE_LICENSE("GPL");
42
3623ecba
CS
43static char *init_device;
44module_param_named(device, init_device, charp, 0400);
1da177e4
LT
45MODULE_PARM_DESC(device, "specify initial device");
46
a4623c46
SS
47static struct kmem_cache *zfcp_cache_hw_align(const char *name,
48 unsigned long size)
49{
50 return kmem_cache_create(name, size, roundup_pow_of_two(size), 0, NULL);
51}
52
3623ecba 53static void __init zfcp_init_device_configure(char *busid, u64 wwpn, u64 lun)
1da177e4 54{
de3dc572 55 struct ccw_device *cdev;
1da177e4
LT
56 struct zfcp_adapter *adapter;
57 struct zfcp_port *port;
58 struct zfcp_unit *unit;
59
de3dc572
SS
60 cdev = get_ccwdev_by_busid(&zfcp_ccw_driver, busid);
61 if (!cdev)
c5afd81e
CS
62 return;
63
de3dc572
SS
64 if (ccw_device_set_online(cdev))
65 goto out_ccw_device;
1da177e4 66
de3dc572 67 adapter = zfcp_ccw_adapter_by_cdev(cdev);
317e6b65 68 if (!adapter)
de3dc572 69 goto out_ccw_device;
c5afd81e
CS
70
71 port = zfcp_get_port_by_wwpn(adapter, wwpn);
72 if (!port)
1da177e4 73 goto out_port;
c5afd81e 74
3623ecba 75 unit = zfcp_unit_enqueue(port, lun);
317e6b65 76 if (IS_ERR(unit))
1da177e4 77 goto out_unit;
091694a5 78
c5afd81e 79 zfcp_erp_unit_reopen(unit, 0, "auidc_1", NULL);
1da177e4 80 zfcp_erp_wait(adapter);
92d5193b 81 flush_work(&unit->scsi_work);
091694a5 82
317e6b65 83out_unit:
615f59e0 84 put_device(&port->dev);
317e6b65 85out_port:
de3dc572
SS
86 zfcp_ccw_adapter_put(adapter);
87out_ccw_device:
88 put_device(&cdev->dev);
1da177e4
LT
89 return;
90}
91
3623ecba
CS
92static void __init zfcp_init_device_setup(char *devstr)
93{
94 char *token;
d10c0858 95 char *str, *str_saved;
3623ecba
CS
96 char busid[ZFCP_BUS_ID_SIZE];
97 u64 wwpn, lun;
98
99 /* duplicate devstr and keep the original for sysfs presentation*/
d10c0858
HC
100 str_saved = kmalloc(strlen(devstr) + 1, GFP_KERNEL);
101 str = str_saved;
3623ecba
CS
102 if (!str)
103 return;
104
105 strcpy(str, devstr);
106
107 token = strsep(&str, ",");
108 if (!token || strlen(token) >= ZFCP_BUS_ID_SIZE)
109 goto err_out;
110 strncpy(busid, token, ZFCP_BUS_ID_SIZE);
111
112 token = strsep(&str, ",");
113 if (!token || strict_strtoull(token, 0, (unsigned long long *) &wwpn))
114 goto err_out;
115
116 token = strsep(&str, ",");
117 if (!token || strict_strtoull(token, 0, (unsigned long long *) &lun))
118 goto err_out;
119
d10c0858 120 kfree(str_saved);
3623ecba
CS
121 zfcp_init_device_configure(busid, wwpn, lun);
122 return;
123
d10c0858
HC
124err_out:
125 kfree(str_saved);
3623ecba
CS
126 pr_err("%s is not a valid SCSI device\n", devstr);
127}
128
317e6b65 129static int __init zfcp_module_init(void)
1da177e4 130{
dd52e0ea 131 int retval = -ENOMEM;
dd52e0ea 132
a4623c46 133 zfcp_data.gpn_ft_cache = zfcp_cache_hw_align("zfcp_gpn",
dbf5dfe9 134 sizeof(struct zfcp_fc_gpn_ft_req));
a4623c46 135 if (!zfcp_data.gpn_ft_cache)
dd52e0ea 136 goto out;
1da177e4 137
a4623c46
SS
138 zfcp_data.qtcb_cache = zfcp_cache_hw_align("zfcp_qtcb",
139 sizeof(struct fsf_qtcb));
140 if (!zfcp_data.qtcb_cache)
141 goto out_qtcb_cache;
142
143 zfcp_data.sr_buffer_cache = zfcp_cache_hw_align("zfcp_sr",
144 sizeof(struct fsf_status_read_buffer));
dd52e0ea
HC
145 if (!zfcp_data.sr_buffer_cache)
146 goto out_sr_cache;
147
a4623c46 148 zfcp_data.gid_pn_cache = zfcp_cache_hw_align("zfcp_gid",
dbf5dfe9 149 sizeof(struct zfcp_fc_gid_pn));
dd52e0ea
HC
150 if (!zfcp_data.gid_pn_cache)
151 goto out_gid_cache;
1da177e4 152
ee744622
CS
153 zfcp_data.adisc_cache = zfcp_cache_hw_align("zfcp_adisc",
154 sizeof(struct zfcp_fc_els_adisc));
155 if (!zfcp_data.adisc_cache)
156 goto out_adisc_cache;
157
dd52e0ea
HC
158 zfcp_data.scsi_transport_template =
159 fc_attach_transport(&zfcp_transport_functions);
160 if (!zfcp_data.scsi_transport_template)
161 goto out_transport;
1da177e4 162
1da177e4 163 retval = misc_register(&zfcp_cfdc_misc);
317e6b65 164 if (retval) {
ecf39d42 165 pr_err("Registering the misc device zfcp_cfdc failed\n");
dd52e0ea 166 goto out_misc;
1da177e4
LT
167 }
168
c1fad417 169 retval = ccw_driver_register(&zfcp_ccw_driver);
1da177e4 170 if (retval) {
ecf39d42 171 pr_err("The zfcp device driver could not register with "
ff3b24fa 172 "the common I/O layer\n");
1da177e4
LT
173 goto out_ccw_register;
174 }
175
3623ecba
CS
176 if (init_device)
177 zfcp_init_device_setup(init_device);
178 return 0;
1da177e4 179
317e6b65 180out_ccw_register:
1da177e4 181 misc_deregister(&zfcp_cfdc_misc);
317e6b65 182out_misc:
dd52e0ea 183 fc_release_transport(zfcp_data.scsi_transport_template);
317e6b65 184out_transport:
ee744622
CS
185 kmem_cache_destroy(zfcp_data.adisc_cache);
186out_adisc_cache:
dd52e0ea 187 kmem_cache_destroy(zfcp_data.gid_pn_cache);
317e6b65 188out_gid_cache:
dd52e0ea 189 kmem_cache_destroy(zfcp_data.sr_buffer_cache);
317e6b65 190out_sr_cache:
a4623c46
SS
191 kmem_cache_destroy(zfcp_data.qtcb_cache);
192out_qtcb_cache:
193 kmem_cache_destroy(zfcp_data.gpn_ft_cache);
317e6b65 194out:
1da177e4
LT
195 return retval;
196}
197
317e6b65 198module_init(zfcp_module_init);
1da177e4 199
c1fad417
CS
200static void __exit zfcp_module_exit(void)
201{
202 ccw_driver_unregister(&zfcp_ccw_driver);
203 misc_deregister(&zfcp_cfdc_misc);
204 fc_release_transport(zfcp_data.scsi_transport_template);
ee744622 205 kmem_cache_destroy(zfcp_data.adisc_cache);
c1fad417
CS
206 kmem_cache_destroy(zfcp_data.gid_pn_cache);
207 kmem_cache_destroy(zfcp_data.sr_buffer_cache);
208 kmem_cache_destroy(zfcp_data.qtcb_cache);
209 kmem_cache_destroy(zfcp_data.gpn_ft_cache);
210}
211
212module_exit(zfcp_module_exit);
213
1da177e4
LT
214/**
215 * zfcp_get_unit_by_lun - find unit in unit list of port by FCP LUN
216 * @port: pointer to port to search for unit
217 * @fcp_lun: FCP LUN to search for
317e6b65
SS
218 *
219 * Returns: pointer to zfcp_unit or NULL
1da177e4 220 */
7ba58c9c 221struct zfcp_unit *zfcp_get_unit_by_lun(struct zfcp_port *port, u64 fcp_lun)
1da177e4 222{
ecf0c772 223 unsigned long flags;
1da177e4 224 struct zfcp_unit *unit;
1da177e4 225
ecf0c772
SS
226 read_lock_irqsave(&port->unit_list_lock, flags);
227 list_for_each_entry(unit, &port->unit_list, list)
6b183334 228 if (unit->fcp_lun == fcp_lun) {
615f59e0 229 if (!get_device(&unit->dev))
6b183334 230 unit = NULL;
ecf0c772
SS
231 read_unlock_irqrestore(&port->unit_list_lock, flags);
232 return unit;
233 }
234 read_unlock_irqrestore(&port->unit_list_lock, flags);
317e6b65 235 return NULL;
1da177e4
LT
236}
237
238/**
239 * zfcp_get_port_by_wwpn - find port in port list of adapter by wwpn
240 * @adapter: pointer to adapter to search for port
241 * @wwpn: wwpn to search for
317e6b65
SS
242 *
243 * Returns: pointer to zfcp_port or NULL
1da177e4 244 */
317e6b65 245struct zfcp_port *zfcp_get_port_by_wwpn(struct zfcp_adapter *adapter,
7ba58c9c 246 u64 wwpn)
1da177e4 247{
ecf0c772 248 unsigned long flags;
1da177e4 249 struct zfcp_port *port;
1da177e4 250
ecf0c772
SS
251 read_lock_irqsave(&adapter->port_list_lock, flags);
252 list_for_each_entry(port, &adapter->port_list, list)
6b183334 253 if (port->wwpn == wwpn) {
615f59e0 254 if (!get_device(&port->dev))
6b183334 255 port = NULL;
ecf0c772 256 read_unlock_irqrestore(&adapter->port_list_lock, flags);
317e6b65 257 return port;
ecf0c772
SS
258 }
259 read_unlock_irqrestore(&adapter->port_list_lock, flags);
317e6b65 260 return NULL;
1da177e4
LT
261}
262
f3450c7b
SS
263/**
264 * zfcp_unit_release - dequeue unit
265 * @dev: pointer to device
266 *
267 * waits until all work is done on unit and removes it then from the unit->list
268 * of the associated port.
269 */
270static void zfcp_unit_release(struct device *dev)
60221920 271{
615f59e0 272 struct zfcp_unit *unit = container_of(dev, struct zfcp_unit, dev);
f3450c7b 273
615f59e0 274 put_device(&unit->port->dev);
f3450c7b 275 kfree(unit);
60221920
SS
276}
277
1da177e4
LT
278/**
279 * zfcp_unit_enqueue - enqueue unit to unit list of a port.
280 * @port: pointer to port where unit is added
281 * @fcp_lun: FCP LUN of unit to be enqueued
317e6b65 282 * Returns: pointer to enqueued unit on success, ERR_PTR on error
1da177e4
LT
283 *
284 * Sets up some unit internal structures and creates sysfs entry.
285 */
7ba58c9c 286struct zfcp_unit *zfcp_unit_enqueue(struct zfcp_port *port, u64 fcp_lun)
1da177e4 287{
462b7859 288 struct zfcp_unit *unit;
f3450c7b
SS
289 int retval = -ENOMEM;
290
615f59e0 291 get_device(&port->dev);
1da177e4 292
ecf0c772
SS
293 unit = zfcp_get_unit_by_lun(port, fcp_lun);
294 if (unit) {
615f59e0 295 put_device(&unit->dev);
f3450c7b
SS
296 retval = -EEXIST;
297 goto err_out;
0fac3f47 298 }
0fac3f47 299
317e6b65 300 unit = kzalloc(sizeof(struct zfcp_unit), GFP_KERNEL);
1da177e4 301 if (!unit)
f3450c7b 302 goto err_out;
1da177e4
LT
303
304 unit->port = port;
305 unit->fcp_lun = fcp_lun;
615f59e0
CS
306 unit->dev.parent = &port->dev;
307 unit->dev.release = zfcp_unit_release;
1da177e4 308
615f59e0 309 if (dev_set_name(&unit->dev, "0x%016llx",
0fac3f47
CS
310 (unsigned long long) fcp_lun)) {
311 kfree(unit);
f3450c7b 312 goto err_out;
0fac3f47 313 }
f3450c7b 314 retval = -EINVAL;
1da177e4 315
f3450c7b
SS
316 INIT_WORK(&unit->scsi_work, zfcp_scsi_scan);
317
c9615858
CS
318 spin_lock_init(&unit->latencies.lock);
319 unit->latencies.write.channel.min = 0xFFFFFFFF;
320 unit->latencies.write.fabric.min = 0xFFFFFFFF;
321 unit->latencies.read.channel.min = 0xFFFFFFFF;
322 unit->latencies.read.fabric.min = 0xFFFFFFFF;
323 unit->latencies.cmd.channel.min = 0xFFFFFFFF;
324 unit->latencies.cmd.fabric.min = 0xFFFFFFFF;
325
615f59e0
CS
326 if (device_register(&unit->dev)) {
327 put_device(&unit->dev);
f3450c7b 328 goto err_out;
f4395b65 329 }
1da177e4 330
615f59e0 331 if (sysfs_create_group(&unit->dev.kobj, &zfcp_sysfs_unit_attrs))
f3450c7b 332 goto err_out_put;
1da177e4 333
ecf0c772
SS
334 write_lock_irq(&port->unit_list_lock);
335 list_add_tail(&unit->list, &port->unit_list);
336 write_unlock_irq(&port->unit_list_lock);
337
1da177e4 338 atomic_set_mask(ZFCP_STATUS_COMMON_RUNNING, &unit->status);
317e6b65 339
1da177e4 340 return unit;
ecf0c772 341
f3450c7b 342err_out_put:
615f59e0 343 device_unregister(&unit->dev);
f3450c7b 344err_out:
615f59e0 345 put_device(&port->dev);
f3450c7b 346 return ERR_PTR(retval);
1da177e4
LT
347}
348
317e6b65 349static int zfcp_allocate_low_mem_buffers(struct zfcp_adapter *adapter)
1da177e4 350{
a4623c46
SS
351 adapter->pool.erp_req =
352 mempool_create_kmalloc_pool(1, sizeof(struct zfcp_fsf_req));
353 if (!adapter->pool.erp_req)
1da177e4
LT
354 return -ENOMEM;
355
799b76d0
CS
356 adapter->pool.gid_pn_req =
357 mempool_create_kmalloc_pool(1, sizeof(struct zfcp_fsf_req));
358 if (!adapter->pool.gid_pn_req)
359 return -ENOMEM;
360
a4623c46
SS
361 adapter->pool.scsi_req =
362 mempool_create_kmalloc_pool(1, sizeof(struct zfcp_fsf_req));
363 if (!adapter->pool.scsi_req)
1da177e4
LT
364 return -ENOMEM;
365
a4623c46
SS
366 adapter->pool.scsi_abort =
367 mempool_create_kmalloc_pool(1, sizeof(struct zfcp_fsf_req));
368 if (!adapter->pool.scsi_abort)
1da177e4
LT
369 return -ENOMEM;
370
a4623c46 371 adapter->pool.status_read_req =
317e6b65 372 mempool_create_kmalloc_pool(FSF_STATUS_READS_RECOM,
0eaae62a 373 sizeof(struct zfcp_fsf_req));
a4623c46
SS
374 if (!adapter->pool.status_read_req)
375 return -ENOMEM;
376
377 adapter->pool.qtcb_pool =
799b76d0 378 mempool_create_slab_pool(4, zfcp_data.qtcb_cache);
a4623c46 379 if (!adapter->pool.qtcb_pool)
1da177e4
LT
380 return -ENOMEM;
381
a4623c46 382 adapter->pool.status_read_data =
317e6b65 383 mempool_create_slab_pool(FSF_STATUS_READS_RECOM,
dd52e0ea 384 zfcp_data.sr_buffer_cache);
a4623c46 385 if (!adapter->pool.status_read_data)
1da177e4
LT
386 return -ENOMEM;
387
dbf5dfe9 388 adapter->pool.gid_pn =
317e6b65 389 mempool_create_slab_pool(1, zfcp_data.gid_pn_cache);
dbf5dfe9 390 if (!adapter->pool.gid_pn)
1da177e4
LT
391 return -ENOMEM;
392
393 return 0;
394}
395
317e6b65 396static void zfcp_free_low_mem_buffers(struct zfcp_adapter *adapter)
1da177e4 397{
a4623c46
SS
398 if (adapter->pool.erp_req)
399 mempool_destroy(adapter->pool.erp_req);
400 if (adapter->pool.scsi_req)
401 mempool_destroy(adapter->pool.scsi_req);
402 if (adapter->pool.scsi_abort)
403 mempool_destroy(adapter->pool.scsi_abort);
404 if (adapter->pool.qtcb_pool)
405 mempool_destroy(adapter->pool.qtcb_pool);
406 if (adapter->pool.status_read_req)
407 mempool_destroy(adapter->pool.status_read_req);
408 if (adapter->pool.status_read_data)
409 mempool_destroy(adapter->pool.status_read_data);
dbf5dfe9
CS
410 if (adapter->pool.gid_pn)
411 mempool_destroy(adapter->pool.gid_pn);
1da177e4
LT
412}
413
317e6b65
SS
414/**
415 * zfcp_status_read_refill - refill the long running status_read_requests
416 * @adapter: ptr to struct zfcp_adapter for which the buffers should be refilled
417 *
418 * Returns: 0 on success, 1 otherwise
419 *
420 * if there are 16 or more status_read requests missing an adapter_reopen
421 * is triggered
422 */
d26ab06e
SS
423int zfcp_status_read_refill(struct zfcp_adapter *adapter)
424{
425 while (atomic_read(&adapter->stat_miss) > 0)
564e1c86 426 if (zfcp_fsf_status_read(adapter->qdio)) {
7afe29f7 427 if (atomic_read(&adapter->stat_miss) >= 16) {
5ffd51a5
SS
428 zfcp_erp_adapter_reopen(adapter, 0, "axsref1",
429 NULL);
7afe29f7
SS
430 return 1;
431 }
d26ab06e 432 break;
7afe29f7
SS
433 } else
434 atomic_dec(&adapter->stat_miss);
d26ab06e
SS
435 return 0;
436}
437
438static void _zfcp_status_read_scheduler(struct work_struct *work)
439{
440 zfcp_status_read_refill(container_of(work, struct zfcp_adapter,
441 stat_work));
442}
443
bd43a42b
CS
444static void zfcp_print_sl(struct seq_file *m, struct service_level *sl)
445{
446 struct zfcp_adapter *adapter =
447 container_of(sl, struct zfcp_adapter, service_level);
448
449 seq_printf(m, "zfcp: %s microcode level %x\n",
450 dev_name(&adapter->ccw_device->dev),
451 adapter->fsf_lic_version);
452}
453
4544683a
SS
454static int zfcp_setup_adapter_work_queue(struct zfcp_adapter *adapter)
455{
456 char name[TASK_COMM_LEN];
457
458 snprintf(name, sizeof(name), "zfcp_q_%s",
459 dev_name(&adapter->ccw_device->dev));
460 adapter->work_queue = create_singlethread_workqueue(name);
461
462 if (adapter->work_queue)
463 return 0;
464 return -ENOMEM;
465}
466
467static void zfcp_destroy_adapter_work_queue(struct zfcp_adapter *adapter)
468{
469 if (adapter->work_queue)
470 destroy_workqueue(adapter->work_queue);
471 adapter->work_queue = NULL;
472
473}
474
317e6b65
SS
475/**
476 * zfcp_adapter_enqueue - enqueue a new adapter to the list
477 * @ccw_device: pointer to the struct cc_device
478 *
de3dc572 479 * Returns: struct zfcp_adapter*
1da177e4
LT
480 * Enqueues an adapter at the end of the adapter list in the driver data.
481 * All adapter internal structures are set up.
482 * Proc-fs entries are also created.
1da177e4 483 */
de3dc572 484struct zfcp_adapter *zfcp_adapter_enqueue(struct ccw_device *ccw_device)
1da177e4 485{
1da177e4
LT
486 struct zfcp_adapter *adapter;
487
f3450c7b 488 if (!get_device(&ccw_device->dev))
de3dc572 489 return ERR_PTR(-ENODEV);
1da177e4 490
317e6b65 491 adapter = kzalloc(sizeof(struct zfcp_adapter), GFP_KERNEL);
f3450c7b
SS
492 if (!adapter) {
493 put_device(&ccw_device->dev);
de3dc572 494 return ERR_PTR(-ENOMEM);
f3450c7b
SS
495 }
496
497 kref_init(&adapter->ref);
1da177e4
LT
498
499 ccw_device->handler = NULL;
1da177e4 500 adapter->ccw_device = ccw_device;
f3450c7b
SS
501
502 INIT_WORK(&adapter->stat_work, _zfcp_status_read_scheduler);
9eae07ef 503 INIT_WORK(&adapter->scan_work, zfcp_fc_scan_ports);
1da177e4 504
d5a282a1 505 if (zfcp_qdio_setup(adapter))
f3450c7b 506 goto failed;
1da177e4 507
00bab910 508 if (zfcp_allocate_low_mem_buffers(adapter))
f3450c7b 509 goto failed;
1da177e4 510
b6bd2fb9
CS
511 adapter->req_list = zfcp_reqlist_alloc();
512 if (!adapter->req_list)
f3450c7b 513 goto failed;
317e6b65 514
5771710b 515 if (zfcp_dbf_adapter_register(adapter))
f3450c7b 516 goto failed;
317e6b65 517
4544683a 518 if (zfcp_setup_adapter_work_queue(adapter))
f3450c7b 519 goto failed;
4544683a 520
d5a282a1 521 if (zfcp_fc_gs_setup(adapter))
f3450c7b 522 goto failed;
d5a282a1 523
ecf0c772
SS
524 rwlock_init(&adapter->port_list_lock);
525 INIT_LIST_HEAD(&adapter->port_list);
526
347c6a96 527 init_waitqueue_head(&adapter->erp_ready_wq);
317e6b65 528 init_waitqueue_head(&adapter->erp_done_wqh);
1da177e4 529
317e6b65
SS
530 INIT_LIST_HEAD(&adapter->erp_ready_head);
531 INIT_LIST_HEAD(&adapter->erp_running_head);
1da177e4 532
c48a29d0 533 rwlock_init(&adapter->erp_lock);
1da177e4
LT
534 rwlock_init(&adapter->abort_lock);
535
143bb6bf 536 if (zfcp_erp_thread_setup(adapter))
f3450c7b 537 goto failed;
1da177e4 538
bd43a42b
CS
539 adapter->service_level.seq_print = zfcp_print_sl;
540
1da177e4
LT
541 dev_set_drvdata(&ccw_device->dev, adapter);
542
60221920
SS
543 if (sysfs_create_group(&ccw_device->dev.kobj,
544 &zfcp_sysfs_adapter_attrs))
f3450c7b 545 goto failed;
1da177e4 546
1d3aab08 547 if (!zfcp_adapter_scsi_register(adapter))
de3dc572 548 return adapter;
1da177e4 549
f3450c7b 550failed:
de3dc572
SS
551 zfcp_adapter_unregister(adapter);
552 return ERR_PTR(-ENOMEM);
553}
554
555void zfcp_adapter_unregister(struct zfcp_adapter *adapter)
556{
557 struct ccw_device *cdev = adapter->ccw_device;
558
559 cancel_work_sync(&adapter->scan_work);
560 cancel_work_sync(&adapter->stat_work);
561 zfcp_destroy_adapter_work_queue(adapter);
562
563 zfcp_fc_wka_ports_force_offline(adapter->gs);
564 zfcp_adapter_scsi_unregister(adapter);
565 sysfs_remove_group(&cdev->dev.kobj, &zfcp_sysfs_adapter_attrs);
566
567 zfcp_erp_thread_kill(adapter);
568 zfcp_dbf_adapter_unregister(adapter->dbf);
569 zfcp_qdio_destroy(adapter->qdio);
570
571 zfcp_ccw_adapter_put(adapter); /* final put to release */
1da177e4
LT
572}
573
317e6b65 574/**
f3450c7b
SS
575 * zfcp_adapter_release - remove the adapter from the resource list
576 * @ref: pointer to struct kref
1da177e4 577 * locks: adapter list write lock is assumed to be held by caller
1da177e4 578 */
f3450c7b 579void zfcp_adapter_release(struct kref *ref)
1da177e4 580{
f3450c7b
SS
581 struct zfcp_adapter *adapter = container_of(ref, struct zfcp_adapter,
582 ref);
de3dc572 583 struct ccw_device *cdev = adapter->ccw_device;
1da177e4 584
f3450c7b 585 dev_set_drvdata(&adapter->ccw_device->dev, NULL);
d5a282a1 586 zfcp_fc_gs_destroy(adapter);
1da177e4 587 zfcp_free_low_mem_buffers(adapter);
317e6b65 588 kfree(adapter->req_list);
f6cd94b1
AH
589 kfree(adapter->fc_stats);
590 kfree(adapter->stats_reset_data);
1da177e4 591 kfree(adapter);
de3dc572 592 put_device(&cdev->dev);
f3450c7b
SS
593}
594
595/**
596 * zfcp_device_unregister - remove port, unit from system
597 * @dev: reference to device which is to be removed
598 * @grp: related reference to attribute group
599 *
600 * Helper function to unregister port, unit from system
601 */
602void zfcp_device_unregister(struct device *dev,
603 const struct attribute_group *grp)
604{
605 sysfs_remove_group(&dev->kobj, grp);
606 device_unregister(dev);
1da177e4
LT
607}
608
f3450c7b 609static void zfcp_port_release(struct device *dev)
60221920 610{
615f59e0 611 struct zfcp_port *port = container_of(dev, struct zfcp_port, dev);
f3450c7b 612
de3dc572 613 zfcp_ccw_adapter_put(port->adapter);
f3450c7b 614 kfree(port);
60221920
SS
615}
616
1da177e4
LT
617/**
618 * zfcp_port_enqueue - enqueue port to port list of adapter
619 * @adapter: adapter where remote port is added
620 * @wwpn: WWPN of the remote port to be enqueued
621 * @status: initial status for the port
622 * @d_id: destination id of the remote port to be enqueued
317e6b65 623 * Returns: pointer to enqueued port on success, ERR_PTR on error
1da177e4
LT
624 *
625 * All port internal structures are set up and the sysfs entry is generated.
626 * d_id is used to enqueue ports with a well known address like the Directory
627 * Service for nameserver lookup.
628 */
7ba58c9c 629struct zfcp_port *zfcp_port_enqueue(struct zfcp_adapter *adapter, u64 wwpn,
317e6b65 630 u32 status, u32 d_id)
1da177e4 631{
3859f6a2 632 struct zfcp_port *port;
f3450c7b
SS
633 int retval = -ENOMEM;
634
635 kref_get(&adapter->ref);
0fac3f47 636
ecf0c772
SS
637 port = zfcp_get_port_by_wwpn(adapter, wwpn);
638 if (port) {
615f59e0 639 put_device(&port->dev);
f3450c7b
SS
640 retval = -EEXIST;
641 goto err_out;
0fac3f47 642 }
1da177e4 643
317e6b65 644 port = kzalloc(sizeof(struct zfcp_port), GFP_KERNEL);
1da177e4 645 if (!port)
f3450c7b 646 goto err_out;
1da177e4 647
ecf0c772
SS
648 rwlock_init(&port->unit_list_lock);
649 INIT_LIST_HEAD(&port->unit_list);
650
799b76d0 651 INIT_WORK(&port->gid_pn_work, zfcp_fc_port_did_lookup);
8fdf30d5 652 INIT_WORK(&port->test_link_work, zfcp_fc_link_test_work);
a2fa0aed 653 INIT_WORK(&port->rport_work, zfcp_scsi_rport_work);
1da177e4
LT
654
655 port->adapter = adapter;
317e6b65
SS
656 port->d_id = d_id;
657 port->wwpn = wwpn;
a2fa0aed 658 port->rport_task = RPORT_NONE;
615f59e0
CS
659 port->dev.parent = &adapter->ccw_device->dev;
660 port->dev.release = zfcp_port_release;
1da177e4 661
615f59e0 662 if (dev_set_name(&port->dev, "0x%016llx", (unsigned long long)wwpn)) {
0fac3f47 663 kfree(port);
f3450c7b 664 goto err_out;
0fac3f47 665 }
f3450c7b 666 retval = -EINVAL;
1da177e4 667
615f59e0
CS
668 if (device_register(&port->dev)) {
669 put_device(&port->dev);
f3450c7b 670 goto err_out;
f4395b65 671 }
1da177e4 672
615f59e0 673 if (sysfs_create_group(&port->dev.kobj,
f3450c7b
SS
674 &zfcp_sysfs_port_attrs))
675 goto err_out_put;
1da177e4 676
ecf0c772
SS
677 write_lock_irq(&adapter->port_list_lock);
678 list_add_tail(&port->list, &adapter->port_list);
679 write_unlock_irq(&adapter->port_list_lock);
680
6b183334 681 atomic_set_mask(status | ZFCP_STATUS_COMMON_RUNNING, &port->status);
317e6b65 682
1da177e4 683 return port;
1da177e4 684
f3450c7b 685err_out_put:
615f59e0 686 device_unregister(&port->dev);
f3450c7b 687err_out:
de3dc572 688 zfcp_ccw_adapter_put(adapter);
f3450c7b 689 return ERR_PTR(retval);
1da177e4
LT
690}
691
317e6b65
SS
692/**
693 * zfcp_sg_free_table - free memory used by scatterlists
694 * @sg: pointer to scatterlist
695 * @count: number of scatterlist which are to be free'ed
696 * the scatterlist are expected to reference pages always
697 */
45633fdc
CS
698void zfcp_sg_free_table(struct scatterlist *sg, int count)
699{
700 int i;
701
702 for (i = 0; i < count; i++, sg++)
703 if (sg)
704 free_page((unsigned long) sg_virt(sg));
705 else
706 break;
707}
708
317e6b65
SS
709/**
710 * zfcp_sg_setup_table - init scatterlist and allocate, assign buffers
711 * @sg: pointer to struct scatterlist
712 * @count: number of scatterlists which should be assigned with buffers
713 * of size page
714 *
715 * Returns: 0 on success, -ENOMEM otherwise
716 */
45633fdc
CS
717int zfcp_sg_setup_table(struct scatterlist *sg, int count)
718{
719 void *addr;
720 int i;
721
722 sg_init_table(sg, count);
723 for (i = 0; i < count; i++, sg++) {
724 addr = (void *) get_zeroed_page(GFP_KERNEL);
725 if (!addr) {
726 zfcp_sg_free_table(sg, i);
727 return -ENOMEM;
728 }
729 sg_set_buf(sg, addr, PAGE_SIZE);
730 }
731 return 0;
732}