[SCSI] zfcp: Apply common naming conventions to zfcp_fc
[deliverable/linux.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
LT
82{
83 struct zfcp_adapter *adapter;
84 struct zfcp_port *port;
85 struct zfcp_unit *unit;
86
87 down(&zfcp_data.config_sema);
88 read_lock_irq(&zfcp_data.config_lock);
3623ecba 89 adapter = zfcp_get_adapter_by_busid(busid);
1da177e4
LT
90 if (adapter)
91 zfcp_adapter_get(adapter);
92 read_unlock_irq(&zfcp_data.config_lock);
93
317e6b65 94 if (!adapter)
1da177e4 95 goto out_adapter;
3623ecba 96 port = zfcp_port_enqueue(adapter, wwpn, 0, 0);
317e6b65 97 if (IS_ERR(port))
1da177e4 98 goto out_port;
3623ecba 99 unit = zfcp_unit_enqueue(port, lun);
317e6b65 100 if (IS_ERR(unit))
1da177e4
LT
101 goto out_unit;
102 up(&zfcp_data.config_sema);
103 ccw_device_set_online(adapter->ccw_device);
091694a5 104
1da177e4 105 zfcp_erp_wait(adapter);
92d5193b 106 flush_work(&unit->scsi_work);
091694a5 107
1da177e4
LT
108 down(&zfcp_data.config_sema);
109 zfcp_unit_put(unit);
317e6b65 110out_unit:
1da177e4 111 zfcp_port_put(port);
317e6b65 112out_port:
1da177e4 113 zfcp_adapter_put(adapter);
317e6b65 114out_adapter:
1da177e4
LT
115 up(&zfcp_data.config_sema);
116 return;
117}
118
3623ecba
CS
119static 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
317e6b65 155static int __init zfcp_module_init(void)
1da177e4 156{
dd52e0ea 157 int retval = -ENOMEM;
dd52e0ea 158
a4623c46
SS
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)
dd52e0ea 162 goto out;
1da177e4 163
a4623c46
SS
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));
dd52e0ea
HC
171 if (!zfcp_data.sr_buffer_cache)
172 goto out_sr_cache;
173
a4623c46
SS
174 zfcp_data.gid_pn_cache = zfcp_cache_hw_align("zfcp_gid",
175 sizeof(struct zfcp_gid_pn_data));
dd52e0ea
HC
176 if (!zfcp_data.gid_pn_cache)
177 goto out_gid_cache;
1da177e4 178
317e6b65
SS
179 sema_init(&zfcp_data.config_sema, 1);
180 rwlock_init(&zfcp_data.config_lock);
181
dd52e0ea
HC
182 zfcp_data.scsi_transport_template =
183 fc_attach_transport(&zfcp_transport_functions);
184 if (!zfcp_data.scsi_transport_template)
185 goto out_transport;
1da177e4 186
1da177e4 187 retval = misc_register(&zfcp_cfdc_misc);
317e6b65 188 if (retval) {
ecf39d42 189 pr_err("Registering the misc device zfcp_cfdc failed\n");
dd52e0ea 190 goto out_misc;
1da177e4
LT
191 }
192
1da177e4
LT
193 retval = zfcp_ccw_register();
194 if (retval) {
ecf39d42 195 pr_err("The zfcp device driver could not register with "
ff3b24fa 196 "the common I/O layer\n");
1da177e4
LT
197 goto out_ccw_register;
198 }
199
3623ecba
CS
200 if (init_device)
201 zfcp_init_device_setup(init_device);
202 return 0;
1da177e4 203
317e6b65 204out_ccw_register:
1da177e4 205 misc_deregister(&zfcp_cfdc_misc);
317e6b65 206out_misc:
dd52e0ea 207 fc_release_transport(zfcp_data.scsi_transport_template);
317e6b65 208out_transport:
dd52e0ea 209 kmem_cache_destroy(zfcp_data.gid_pn_cache);
317e6b65 210out_gid_cache:
dd52e0ea 211 kmem_cache_destroy(zfcp_data.sr_buffer_cache);
317e6b65 212out_sr_cache:
a4623c46
SS
213 kmem_cache_destroy(zfcp_data.qtcb_cache);
214out_qtcb_cache:
215 kmem_cache_destroy(zfcp_data.gpn_ft_cache);
317e6b65 216out:
1da177e4
LT
217 return retval;
218}
219
317e6b65 220module_init(zfcp_module_init);
1da177e4 221
1da177e4
LT
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
317e6b65
SS
226 *
227 * Returns: pointer to zfcp_unit or NULL
1da177e4 228 */
7ba58c9c 229struct zfcp_unit *zfcp_get_unit_by_lun(struct zfcp_port *port, u64 fcp_lun)
1da177e4
LT
230{
231 struct zfcp_unit *unit;
1da177e4 232
317e6b65 233 list_for_each_entry(unit, &port->unit_list_head, list)
1da177e4 234 if ((unit->fcp_lun == fcp_lun) &&
317e6b65
SS
235 !(atomic_read(&unit->status) & ZFCP_STATUS_COMMON_REMOVE))
236 return unit;
237 return NULL;
1da177e4
LT
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
317e6b65
SS
244 *
245 * Returns: pointer to zfcp_port or NULL
1da177e4 246 */
317e6b65 247struct zfcp_port *zfcp_get_port_by_wwpn(struct zfcp_adapter *adapter,
7ba58c9c 248 u64 wwpn)
1da177e4
LT
249{
250 struct zfcp_port *port;
1da177e4 251
317e6b65 252 list_for_each_entry(port, &adapter->port_list_head, list)
a5b11dda
CS
253 if ((port->wwpn == wwpn) &&
254 !(atomic_read(&port->status) & ZFCP_STATUS_COMMON_REMOVE))
317e6b65
SS
255 return port;
256 return NULL;
1da177e4
LT
257}
258
60221920
SS
259static void zfcp_sysfs_unit_release(struct device *dev)
260{
261 kfree(container_of(dev, struct zfcp_unit, sysfs_device));
262}
263
1da177e4
LT
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
317e6b65 268 * Returns: pointer to enqueued unit on success, ERR_PTR on error
1da177e4
LT
269 * Locks: config_sema must be held to serialize changes to the unit list
270 *
271 * Sets up some unit internal structures and creates sysfs entry.
272 */
7ba58c9c 273struct zfcp_unit *zfcp_unit_enqueue(struct zfcp_port *port, u64 fcp_lun)
1da177e4 274{
462b7859 275 struct zfcp_unit *unit;
1da177e4 276
317e6b65 277 unit = kzalloc(sizeof(struct zfcp_unit), GFP_KERNEL);
1da177e4 278 if (!unit)
317e6b65 279 return ERR_PTR(-ENOMEM);
1da177e4 280
1da177e4
LT
281 atomic_set(&unit->refcount, 0);
282 init_waitqueue_head(&unit->remove_wq);
92d5193b 283 INIT_WORK(&unit->scsi_work, zfcp_scsi_scan);
1da177e4
LT
284
285 unit->port = port;
286 unit->fcp_lun = fcp_lun;
287
1bf5b285
CH
288 dev_set_name(&unit->sysfs_device, "0x%016llx",
289 (unsigned long long) fcp_lun);
1da177e4
LT
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
c9615858
CS
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
317e6b65
SS
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;
1da177e4 309 }
317e6b65
SS
310 read_unlock_irq(&zfcp_data.config_lock);
311
312 if (device_register(&unit->sysfs_device))
313 goto err_out_free;
1da177e4 314
60221920
SS
315 if (sysfs_create_group(&unit->sysfs_device.kobj,
316 &zfcp_sysfs_unit_attrs)) {
1da177e4 317 device_unregister(&unit->sysfs_device);
317e6b65 318 return ERR_PTR(-EIO);
1da177e4
LT
319 }
320
321 zfcp_unit_get(unit);
322
1da177e4 323 write_lock_irq(&zfcp_data.config_lock);
462b7859 324 list_add_tail(&unit->list, &port->unit_list_head);
1da177e4
LT
325 atomic_clear_mask(ZFCP_STATUS_COMMON_REMOVE, &unit->status);
326 atomic_set_mask(ZFCP_STATUS_COMMON_RUNNING, &unit->status);
317e6b65 327
1da177e4
LT
328 write_unlock_irq(&zfcp_data.config_lock);
329
1da177e4
LT
330 zfcp_port_get(port);
331
332 return unit;
317e6b65
SS
333
334err_out_free:
335 kfree(unit);
336 return ERR_PTR(-EINVAL);
1da177e4
LT
337}
338
317e6b65
SS
339/**
340 * zfcp_unit_dequeue - dequeue unit
341 * @unit: pointer to zfcp_unit
342 *
343 * waits until all work is done on unit and removes it then from the unit->list
344 * of the associated port.
345 */
346void zfcp_unit_dequeue(struct zfcp_unit *unit)
1da177e4 347{
44cc76f2 348 wait_event(unit->remove_wq, atomic_read(&unit->refcount) == 0);
1da177e4
LT
349 write_lock_irq(&zfcp_data.config_lock);
350 list_del(&unit->list);
351 write_unlock_irq(&zfcp_data.config_lock);
1da177e4 352 zfcp_port_put(unit->port);
60221920 353 sysfs_remove_group(&unit->sysfs_device.kobj, &zfcp_sysfs_unit_attrs);
1da177e4
LT
354 device_unregister(&unit->sysfs_device);
355}
356
317e6b65 357static int zfcp_allocate_low_mem_buffers(struct zfcp_adapter *adapter)
1da177e4 358{
317e6b65 359 /* must only be called with zfcp_data.config_sema taken */
a4623c46
SS
360 adapter->pool.erp_req =
361 mempool_create_kmalloc_pool(1, sizeof(struct zfcp_fsf_req));
362 if (!adapter->pool.erp_req)
1da177e4
LT
363 return -ENOMEM;
364
799b76d0
CS
365 adapter->pool.gid_pn_req =
366 mempool_create_kmalloc_pool(1, sizeof(struct zfcp_fsf_req));
367 if (!adapter->pool.gid_pn_req)
368 return -ENOMEM;
369
a4623c46
SS
370 adapter->pool.scsi_req =
371 mempool_create_kmalloc_pool(1, sizeof(struct zfcp_fsf_req));
372 if (!adapter->pool.scsi_req)
1da177e4
LT
373 return -ENOMEM;
374
a4623c46
SS
375 adapter->pool.scsi_abort =
376 mempool_create_kmalloc_pool(1, sizeof(struct zfcp_fsf_req));
377 if (!adapter->pool.scsi_abort)
1da177e4
LT
378 return -ENOMEM;
379
a4623c46 380 adapter->pool.status_read_req =
317e6b65 381 mempool_create_kmalloc_pool(FSF_STATUS_READS_RECOM,
0eaae62a 382 sizeof(struct zfcp_fsf_req));
a4623c46
SS
383 if (!adapter->pool.status_read_req)
384 return -ENOMEM;
385
386 adapter->pool.qtcb_pool =
799b76d0 387 mempool_create_slab_pool(4, zfcp_data.qtcb_cache);
a4623c46 388 if (!adapter->pool.qtcb_pool)
1da177e4
LT
389 return -ENOMEM;
390
a4623c46 391 adapter->pool.status_read_data =
317e6b65 392 mempool_create_slab_pool(FSF_STATUS_READS_RECOM,
dd52e0ea 393 zfcp_data.sr_buffer_cache);
a4623c46 394 if (!adapter->pool.status_read_data)
1da177e4
LT
395 return -ENOMEM;
396
a4623c46 397 adapter->pool.gid_pn_data =
317e6b65 398 mempool_create_slab_pool(1, zfcp_data.gid_pn_cache);
a4623c46 399 if (!adapter->pool.gid_pn_data)
1da177e4
LT
400 return -ENOMEM;
401
402 return 0;
403}
404
317e6b65 405static void zfcp_free_low_mem_buffers(struct zfcp_adapter *adapter)
1da177e4 406{
317e6b65 407 /* zfcp_data.config_sema must be held */
a4623c46
SS
408 if (adapter->pool.erp_req)
409 mempool_destroy(adapter->pool.erp_req);
410 if (adapter->pool.scsi_req)
411 mempool_destroy(adapter->pool.scsi_req);
412 if (adapter->pool.scsi_abort)
413 mempool_destroy(adapter->pool.scsi_abort);
414 if (adapter->pool.qtcb_pool)
415 mempool_destroy(adapter->pool.qtcb_pool);
416 if (adapter->pool.status_read_req)
417 mempool_destroy(adapter->pool.status_read_req);
418 if (adapter->pool.status_read_data)
419 mempool_destroy(adapter->pool.status_read_data);
420 if (adapter->pool.gid_pn_data)
421 mempool_destroy(adapter->pool.gid_pn_data);
1da177e4
LT
422}
423
317e6b65
SS
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 */
d26ab06e
SS
433int zfcp_status_read_refill(struct zfcp_adapter *adapter)
434{
435 while (atomic_read(&adapter->stat_miss) > 0)
564e1c86 436 if (zfcp_fsf_status_read(adapter->qdio)) {
7afe29f7 437 if (atomic_read(&adapter->stat_miss) >= 16) {
5ffd51a5
SS
438 zfcp_erp_adapter_reopen(adapter, 0, "axsref1",
439 NULL);
7afe29f7
SS
440 return 1;
441 }
d26ab06e 442 break;
7afe29f7
SS
443 } else
444 atomic_dec(&adapter->stat_miss);
d26ab06e
SS
445 return 0;
446}
447
448static void _zfcp_status_read_scheduler(struct work_struct *work)
449{
450 zfcp_status_read_refill(container_of(work, struct zfcp_adapter,
451 stat_work));
452}
453
bd43a42b
CS
454static void zfcp_print_sl(struct seq_file *m, struct service_level *sl)
455{
456 struct zfcp_adapter *adapter =
457 container_of(sl, struct zfcp_adapter, service_level);
458
459 seq_printf(m, "zfcp: %s microcode level %x\n",
460 dev_name(&adapter->ccw_device->dev),
461 adapter->fsf_lic_version);
462}
463
4544683a
SS
464static int zfcp_setup_adapter_work_queue(struct zfcp_adapter *adapter)
465{
466 char name[TASK_COMM_LEN];
467
468 snprintf(name, sizeof(name), "zfcp_q_%s",
469 dev_name(&adapter->ccw_device->dev));
470 adapter->work_queue = create_singlethread_workqueue(name);
471
472 if (adapter->work_queue)
473 return 0;
474 return -ENOMEM;
475}
476
477static void zfcp_destroy_adapter_work_queue(struct zfcp_adapter *adapter)
478{
479 if (adapter->work_queue)
480 destroy_workqueue(adapter->work_queue);
481 adapter->work_queue = NULL;
482
483}
484
317e6b65
SS
485/**
486 * zfcp_adapter_enqueue - enqueue a new adapter to the list
487 * @ccw_device: pointer to the struct cc_device
488 *
489 * Returns: 0 if a new adapter was successfully enqueued
490 * -ENOMEM if alloc failed
1da177e4
LT
491 * Enqueues an adapter at the end of the adapter list in the driver data.
492 * All adapter internal structures are set up.
493 * Proc-fs entries are also created.
1da177e4
LT
494 * locks: config_sema must be held to serialise changes to the adapter list
495 */
317e6b65 496int zfcp_adapter_enqueue(struct ccw_device *ccw_device)
1da177e4 497{
1da177e4
LT
498 struct zfcp_adapter *adapter;
499
500 /*
41fa2ada 501 * Note: It is safe to release the list_lock, as any list changes
1da177e4
LT
502 * are protected by the config_sema, which must be held to get here
503 */
504
317e6b65 505 adapter = kzalloc(sizeof(struct zfcp_adapter), GFP_KERNEL);
553448f6 506 if (!adapter)
317e6b65 507 return -ENOMEM;
1da177e4
LT
508
509 ccw_device->handler = NULL;
1da177e4 510 adapter->ccw_device = ccw_device;
317e6b65 511 atomic_set(&adapter->refcount, 0);
1da177e4 512
d5a282a1
SS
513 if (zfcp_qdio_setup(adapter))
514 goto qdio_failed;
1da177e4 515
00bab910 516 if (zfcp_allocate_low_mem_buffers(adapter))
d5a282a1 517 goto low_mem_buffers_failed;
1da177e4 518
317e6b65 519 if (zfcp_reqlist_alloc(adapter))
d5a282a1 520 goto low_mem_buffers_failed;
317e6b65 521
5771710b 522 if (zfcp_dbf_adapter_register(adapter))
317e6b65
SS
523 goto debug_register_failed;
524
4544683a
SS
525 if (zfcp_setup_adapter_work_queue(adapter))
526 goto work_queue_failed;
527
d5a282a1
SS
528 if (zfcp_fc_gs_setup(adapter))
529 goto generic_services_failed;
530
1da177e4 531 init_waitqueue_head(&adapter->remove_wq);
317e6b65
SS
532 init_waitqueue_head(&adapter->erp_thread_wqh);
533 init_waitqueue_head(&adapter->erp_done_wqh);
1da177e4 534
1da177e4 535 INIT_LIST_HEAD(&adapter->port_list_head);
317e6b65
SS
536 INIT_LIST_HEAD(&adapter->erp_ready_head);
537 INIT_LIST_HEAD(&adapter->erp_running_head);
1da177e4 538
fea9d6c7 539 spin_lock_init(&adapter->req_list_lock);
c48a29d0 540
c48a29d0 541 rwlock_init(&adapter->erp_lock);
1da177e4
LT
542 rwlock_init(&adapter->abort_lock);
543
317e6b65 544 sema_init(&adapter->erp_ready_sem, 0);
1da177e4 545
d26ab06e 546 INIT_WORK(&adapter->stat_work, _zfcp_status_read_scheduler);
6f53a2d2 547 INIT_WORK(&adapter->scan_work, _zfcp_fc_scan_ports_later);
1da177e4 548
bd43a42b
CS
549 adapter->service_level.seq_print = zfcp_print_sl;
550
1da177e4
LT
551 /* mark adapter unusable as long as sysfs registration is not complete */
552 atomic_set_mask(ZFCP_STATUS_COMMON_REMOVE, &adapter->status);
553
1da177e4
LT
554 dev_set_drvdata(&ccw_device->dev, adapter);
555
60221920
SS
556 if (sysfs_create_group(&ccw_device->dev.kobj,
557 &zfcp_sysfs_adapter_attrs))
1da177e4
LT
558 goto sysfs_failed;
559
1da177e4 560 atomic_clear_mask(ZFCP_STATUS_COMMON_REMOVE, &adapter->status);
828bc121 561
1d3aab08
SS
562 if (!zfcp_adapter_scsi_register(adapter))
563 return 0;
1da177e4 564
317e6b65 565sysfs_failed:
d5a282a1
SS
566 zfcp_fc_gs_destroy(adapter);
567generic_services_failed:
4544683a
SS
568 zfcp_destroy_adapter_work_queue(adapter);
569work_queue_failed:
5771710b 570 zfcp_dbf_adapter_unregister(adapter->dbf);
317e6b65 571debug_register_failed:
1da177e4 572 dev_set_drvdata(&ccw_device->dev, NULL);
317e6b65 573 kfree(adapter->req_list);
d5a282a1 574low_mem_buffers_failed:
1da177e4 575 zfcp_free_low_mem_buffers(adapter);
d5a282a1
SS
576qdio_failed:
577 zfcp_qdio_destroy(adapter->qdio);
1da177e4 578 kfree(adapter);
317e6b65 579 return -ENOMEM;
1da177e4
LT
580}
581
317e6b65
SS
582/**
583 * zfcp_adapter_dequeue - remove the adapter from the resource list
584 * @adapter: pointer to struct zfcp_adapter which should be removed
1da177e4 585 * locks: adapter list write lock is assumed to be held by caller
1da177e4 586 */
317e6b65 587void zfcp_adapter_dequeue(struct zfcp_adapter *adapter)
1da177e4
LT
588{
589 int retval = 0;
590 unsigned long flags;
591
cc8c2829 592 cancel_work_sync(&adapter->scan_work);
d26ab06e 593 cancel_work_sync(&adapter->stat_work);
55c770fa 594 zfcp_fc_wka_ports_force_offline(adapter->gs);
9f28745a 595 zfcp_adapter_scsi_unregister(adapter);
60221920
SS
596 sysfs_remove_group(&adapter->ccw_device->dev.kobj,
597 &zfcp_sysfs_adapter_attrs);
1da177e4
LT
598 dev_set_drvdata(&adapter->ccw_device->dev, NULL);
599 /* sanity check: no pending FSF requests */
fea9d6c7
VS
600 spin_lock_irqsave(&adapter->req_list_lock, flags);
601 retval = zfcp_reqlist_isempty(adapter);
602 spin_unlock_irqrestore(&adapter->req_list_lock, flags);
317e6b65
SS
603 if (!retval)
604 return;
1da177e4 605
d5a282a1 606 zfcp_fc_gs_destroy(adapter);
4544683a 607 zfcp_destroy_adapter_work_queue(adapter);
5771710b 608 zfcp_dbf_adapter_unregister(adapter->dbf);
1da177e4 609 zfcp_free_low_mem_buffers(adapter);
d5a282a1 610 zfcp_qdio_destroy(adapter->qdio);
317e6b65 611 kfree(adapter->req_list);
f6cd94b1
AH
612 kfree(adapter->fc_stats);
613 kfree(adapter->stats_reset_data);
1da177e4 614 kfree(adapter);
1da177e4
LT
615}
616
60221920
SS
617static void zfcp_sysfs_port_release(struct device *dev)
618{
619 kfree(container_of(dev, struct zfcp_port, sysfs_device));
620}
621
1da177e4
LT
622/**
623 * zfcp_port_enqueue - enqueue port to port list of adapter
624 * @adapter: adapter where remote port is added
625 * @wwpn: WWPN of the remote port to be enqueued
626 * @status: initial status for the port
627 * @d_id: destination id of the remote port to be enqueued
317e6b65 628 * Returns: pointer to enqueued port on success, ERR_PTR on error
1da177e4
LT
629 * Locks: config_sema must be held to serialize changes to the port list
630 *
631 * All port internal structures are set up and the sysfs entry is generated.
632 * d_id is used to enqueue ports with a well known address like the Directory
633 * Service for nameserver lookup.
634 */
7ba58c9c 635struct zfcp_port *zfcp_port_enqueue(struct zfcp_adapter *adapter, u64 wwpn,
317e6b65 636 u32 status, u32 d_id)
1da177e4 637{
3859f6a2 638 struct zfcp_port *port;
60221920 639 int retval;
1da177e4 640
317e6b65 641 port = kzalloc(sizeof(struct zfcp_port), GFP_KERNEL);
1da177e4 642 if (!port)
317e6b65 643 return ERR_PTR(-ENOMEM);
1da177e4 644
1da177e4 645 init_waitqueue_head(&port->remove_wq);
1da177e4 646 INIT_LIST_HEAD(&port->unit_list_head);
799b76d0 647 INIT_WORK(&port->gid_pn_work, zfcp_fc_port_did_lookup);
8fdf30d5 648 INIT_WORK(&port->test_link_work, zfcp_fc_link_test_work);
a2fa0aed 649 INIT_WORK(&port->rport_work, zfcp_scsi_rport_work);
1da177e4
LT
650
651 port->adapter = adapter;
317e6b65
SS
652 port->d_id = d_id;
653 port->wwpn = wwpn;
a2fa0aed 654 port->rport_task = RPORT_NONE;
1da177e4 655
317e6b65
SS
656 /* mark port unusable as long as sysfs registration is not complete */
657 atomic_set_mask(status | ZFCP_STATUS_COMMON_REMOVE, &port->status);
658 atomic_set(&port->refcount, 0);
1da177e4 659
adc90daf
CS
660 dev_set_name(&port->sysfs_device, "0x%016llx",
661 (unsigned long long)wwpn);
5ab944f9 662 port->sysfs_device.parent = &adapter->ccw_device->dev;
cc8c2829 663
1da177e4
LT
664 port->sysfs_device.release = zfcp_sysfs_port_release;
665 dev_set_drvdata(&port->sysfs_device, port);
666
317e6b65 667 read_lock_irq(&zfcp_data.config_lock);
a5b11dda
CS
668 if (zfcp_get_port_by_wwpn(adapter, wwpn)) {
669 read_unlock_irq(&zfcp_data.config_lock);
670 goto err_out_free;
671 }
317e6b65 672 read_unlock_irq(&zfcp_data.config_lock);
1da177e4 673
317e6b65
SS
674 if (device_register(&port->sysfs_device))
675 goto err_out_free;
1da177e4 676
5ab944f9
SS
677 retval = sysfs_create_group(&port->sysfs_device.kobj,
678 &zfcp_sysfs_port_attrs);
60221920
SS
679
680 if (retval) {
1da177e4 681 device_unregister(&port->sysfs_device);
317e6b65 682 goto err_out;
1da177e4
LT
683 }
684
685 zfcp_port_get(port);
686
1da177e4 687 write_lock_irq(&zfcp_data.config_lock);
3859f6a2 688 list_add_tail(&port->list, &adapter->port_list_head);
1da177e4
LT
689 atomic_clear_mask(ZFCP_STATUS_COMMON_REMOVE, &port->status);
690 atomic_set_mask(ZFCP_STATUS_COMMON_RUNNING, &port->status);
317e6b65 691
1da177e4
LT
692 write_unlock_irq(&zfcp_data.config_lock);
693
694 zfcp_adapter_get(adapter);
1da177e4 695 return port;
317e6b65
SS
696
697err_out_free:
698 kfree(port);
699err_out:
700 return ERR_PTR(-EINVAL);
1da177e4
LT
701}
702
317e6b65
SS
703/**
704 * zfcp_port_dequeue - dequeues a port from the port list of the adapter
705 * @port: pointer to struct zfcp_port which should be removed
706 */
707void zfcp_port_dequeue(struct zfcp_port *port)
1da177e4 708{
1da177e4
LT
709 write_lock_irq(&zfcp_data.config_lock);
710 list_del(&port->list);
1da177e4 711 write_unlock_irq(&zfcp_data.config_lock);
a67417ab 712 if (port->rport) {
70932935 713 port->rport->dd_data = NULL;
a67417ab
SS
714 port->rport = NULL;
715 }
716 wait_event(port->remove_wq, atomic_read(&port->refcount) == 0);
717 cancel_work_sync(&port->rport_work); /* usually not necessary */
1da177e4 718 zfcp_adapter_put(port->adapter);
5ab944f9 719 sysfs_remove_group(&port->sysfs_device.kobj, &zfcp_sysfs_port_attrs);
1da177e4
LT
720 device_unregister(&port->sysfs_device);
721}
722
317e6b65
SS
723/**
724 * zfcp_sg_free_table - free memory used by scatterlists
725 * @sg: pointer to scatterlist
726 * @count: number of scatterlist which are to be free'ed
727 * the scatterlist are expected to reference pages always
728 */
45633fdc
CS
729void zfcp_sg_free_table(struct scatterlist *sg, int count)
730{
731 int i;
732
733 for (i = 0; i < count; i++, sg++)
734 if (sg)
735 free_page((unsigned long) sg_virt(sg));
736 else
737 break;
738}
739
317e6b65
SS
740/**
741 * zfcp_sg_setup_table - init scatterlist and allocate, assign buffers
742 * @sg: pointer to struct scatterlist
743 * @count: number of scatterlists which should be assigned with buffers
744 * of size page
745 *
746 * Returns: 0 on success, -ENOMEM otherwise
747 */
45633fdc
CS
748int zfcp_sg_setup_table(struct scatterlist *sg, int count)
749{
750 void *addr;
751 int i;
752
753 sg_init_table(sg, count);
754 for (i = 0; i < count; i++, sg++) {
755 addr = (void *) get_zeroed_page(GFP_KERNEL);
756 if (!addr) {
757 zfcp_sg_free_table(sg, i);
758 return -ENOMEM;
759 }
760 sg_set_buf(sg, addr, PAGE_SIZE);
761 }
762 return 0;
763}
This page took 0.733154 seconds and 5 git commands to generate.