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