blob: ed7211ef04eb829e5531e8843a698ed7656e0c17 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Christof Schmitt553448f2008-06-10 18:20:58 +02002 * zfcp device driver
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
Christof Schmitt553448f2008-06-10 18:20:58 +02004 * Module interface and handling of zfcp data structures.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
Christof Schmitta2fa0ae2009-03-02 13:09:08 +01006 * Copyright IBM Corporation 2002, 2009
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 */
8
Andreas Herrmann4a9d2d82006-05-22 18:14:08 +02009/*
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
Christof Schmitt553448f2008-06-10 18:20:58 +020021 * Michael Loehr
22 * Swen Schillig
23 * Christof Schmitt
24 * Martin Petermann
25 * Sven Schuetz
Andreas Herrmann4a9d2d82006-05-22 18:14:08 +020026 */
27
Christof Schmittecf39d42008-12-25 13:39:53 +010028#define KMSG_COMPONENT "zfcp"
29#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
30
Christof Schmitt45633fd2008-06-10 18:20:55 +020031#include <linux/miscdevice.h>
Christof Schmittbd43a42b72008-12-25 13:38:50 +010032#include <linux/seq_file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include "zfcp_ext.h"
34
Kay Sievers98df67b2008-12-25 13:38:55 +010035#define ZFCP_BUS_ID_SIZE 20
36
Andreas Herrmann4a9d2d82006-05-22 18:14:08 +020037MODULE_AUTHOR("IBM Deutschland Entwicklung GmbH - linux390@de.ibm.com");
Swen Schillig317e6b62008-07-02 10:56:37 +020038MODULE_DESCRIPTION("FCP HBA driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -070039MODULE_LICENSE("GPL");
40
Christof Schmitt3623ecb2008-12-19 16:56:57 +010041static char *init_device;
42module_param_named(device, init_device, charp, 0400);
Linus Torvalds1da177e2005-04-16 15:20:36 -070043MODULE_PARM_DESC(device, "specify initial device");
44
Swen Schilliga4623c42009-08-18 15:43:15 +020045static 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
Heiko Carstensca2d02c2007-05-08 11:17:54 +020051static int zfcp_reqlist_alloc(struct zfcp_adapter *adapter)
Volker Sameskefea9d6c2006-08-02 11:05:16 +020052{
Heiko Carstensca2d02c2007-05-08 11:17:54 +020053 int idx;
Volker Sameskefea9d6c2006-08-02 11:05:16 +020054
55 adapter->req_list = kcalloc(REQUEST_LIST_SIZE, sizeof(struct list_head),
56 GFP_KERNEL);
Volker Sameskefea9d6c2006-08-02 11:05:16 +020057 if (!adapter->req_list)
58 return -ENOMEM;
59
Heiko Carstensca2d02c2007-05-08 11:17:54 +020060 for (idx = 0; idx < REQUEST_LIST_SIZE; idx++)
61 INIT_LIST_HEAD(&adapter->req_list[idx]);
Volker Sameskefea9d6c2006-08-02 11:05:16 +020062 return 0;
63}
64
Swen Schillig317e6b62008-07-02 10:56:37 +020065/**
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 */
Volker Sameskefea9d6c2006-08-02 11:05:16 +020071int zfcp_reqlist_isempty(struct zfcp_adapter *adapter)
72{
Heiko Carstensca2d02c2007-05-08 11:17:54 +020073 unsigned int idx;
Volker Sameskefea9d6c2006-08-02 11:05:16 +020074
Heiko Carstensca2d02c2007-05-08 11:17:54 +020075 for (idx = 0; idx < REQUEST_LIST_SIZE; idx++)
76 if (!list_empty(&adapter->req_list[idx]))
Volker Sameskefea9d6c2006-08-02 11:05:16 +020077 return 0;
Volker Sameskefea9d6c2006-08-02 11:05:16 +020078 return 1;
79}
80
Christof Schmitt3623ecb2008-12-19 16:56:57 +010081static void __init zfcp_init_device_configure(char *busid, u64 wwpn, u64 lun)
Linus Torvalds1da177e2005-04-16 15:20:36 -070082{
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);
Christof Schmitt3623ecb2008-12-19 16:56:57 +010089 adapter = zfcp_get_adapter_by_busid(busid);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 if (adapter)
91 zfcp_adapter_get(adapter);
92 read_unlock_irq(&zfcp_data.config_lock);
93
Swen Schillig317e6b62008-07-02 10:56:37 +020094 if (!adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 goto out_adapter;
Christof Schmitt3623ecb2008-12-19 16:56:57 +010096 port = zfcp_port_enqueue(adapter, wwpn, 0, 0);
Swen Schillig317e6b62008-07-02 10:56:37 +020097 if (IS_ERR(port))
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 goto out_port;
Christof Schmitt3623ecb2008-12-19 16:56:57 +010099 unit = zfcp_unit_enqueue(port, lun);
Swen Schillig317e6b62008-07-02 10:56:37 +0200100 if (IS_ERR(unit))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 goto out_unit;
102 up(&zfcp_data.config_sema);
103 ccw_device_set_online(adapter->ccw_device);
Swen Schillig091694a2008-10-01 12:42:25 +0200104
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 zfcp_erp_wait(adapter);
Swen Schillig92d51932009-04-17 15:08:04 +0200106 flush_work(&unit->scsi_work);
Swen Schillig091694a2008-10-01 12:42:25 +0200107
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 down(&zfcp_data.config_sema);
109 zfcp_unit_put(unit);
Swen Schillig317e6b62008-07-02 10:56:37 +0200110out_unit:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 zfcp_port_put(port);
Swen Schillig317e6b62008-07-02 10:56:37 +0200112out_port:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 zfcp_adapter_put(adapter);
Swen Schillig317e6b62008-07-02 10:56:37 +0200114out_adapter:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 up(&zfcp_data.config_sema);
116 return;
117}
118
Christof Schmitt3623ecb2008-12-19 16:56:57 +0100119static 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
Swen Schillig317e6b62008-07-02 10:56:37 +0200155static int __init zfcp_module_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156{
Heiko Carstensdd52e0e2006-09-18 22:28:49 +0200157 int retval = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
Swen Schilliga4623c42009-08-18 15:43:15 +0200159 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)
Heiko Carstensdd52e0e2006-09-18 22:28:49 +0200162 goto out;
163
Swen Schilliga4623c42009-08-18 15:43:15 +0200164 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));
Heiko Carstensdd52e0e2006-09-18 22:28:49 +0200171 if (!zfcp_data.sr_buffer_cache)
172 goto out_sr_cache;
173
Swen Schilliga4623c42009-08-18 15:43:15 +0200174 zfcp_data.gid_pn_cache = zfcp_cache_hw_align("zfcp_gid",
175 sizeof(struct zfcp_gid_pn_data));
Heiko Carstensdd52e0e2006-09-18 22:28:49 +0200176 if (!zfcp_data.gid_pn_cache)
177 goto out_gid_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
Swen Schillig317e6b62008-07-02 10:56:37 +0200179 sema_init(&zfcp_data.config_sema, 1);
180 rwlock_init(&zfcp_data.config_lock);
181
Heiko Carstensdd52e0e2006-09-18 22:28:49 +0200182 zfcp_data.scsi_transport_template =
183 fc_attach_transport(&zfcp_transport_functions);
184 if (!zfcp_data.scsi_transport_template)
185 goto out_transport;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 retval = misc_register(&zfcp_cfdc_misc);
Swen Schillig317e6b62008-07-02 10:56:37 +0200188 if (retval) {
Christof Schmittecf39d42008-12-25 13:39:53 +0100189 pr_err("Registering the misc device zfcp_cfdc failed\n");
Heiko Carstensdd52e0e2006-09-18 22:28:49 +0200190 goto out_misc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 }
192
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 retval = zfcp_ccw_register();
194 if (retval) {
Christof Schmittecf39d42008-12-25 13:39:53 +0100195 pr_err("The zfcp device driver could not register with "
Christof Schmittff3b24f2008-10-01 12:42:15 +0200196 "the common I/O layer\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 goto out_ccw_register;
198 }
199
Christof Schmitt3623ecb2008-12-19 16:56:57 +0100200 if (init_device)
201 zfcp_init_device_setup(init_device);
202 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
Swen Schillig317e6b62008-07-02 10:56:37 +0200204out_ccw_register:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 misc_deregister(&zfcp_cfdc_misc);
Swen Schillig317e6b62008-07-02 10:56:37 +0200206out_misc:
Heiko Carstensdd52e0e2006-09-18 22:28:49 +0200207 fc_release_transport(zfcp_data.scsi_transport_template);
Swen Schillig317e6b62008-07-02 10:56:37 +0200208out_transport:
Heiko Carstensdd52e0e2006-09-18 22:28:49 +0200209 kmem_cache_destroy(zfcp_data.gid_pn_cache);
Swen Schillig317e6b62008-07-02 10:56:37 +0200210out_gid_cache:
Heiko Carstensdd52e0e2006-09-18 22:28:49 +0200211 kmem_cache_destroy(zfcp_data.sr_buffer_cache);
Swen Schillig317e6b62008-07-02 10:56:37 +0200212out_sr_cache:
Swen Schilliga4623c42009-08-18 15:43:15 +0200213 kmem_cache_destroy(zfcp_data.qtcb_cache);
214out_qtcb_cache:
215 kmem_cache_destroy(zfcp_data.gpn_ft_cache);
Swen Schillig317e6b62008-07-02 10:56:37 +0200216out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 return retval;
218}
219
Swen Schillig317e6b62008-07-02 10:56:37 +0200220module_init(zfcp_module_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222/**
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
Swen Schillig317e6b62008-07-02 10:56:37 +0200226 *
227 * Returns: pointer to zfcp_unit or NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 */
Swen Schillig7ba58c92008-10-01 12:42:18 +0200229struct zfcp_unit *zfcp_get_unit_by_lun(struct zfcp_port *port, u64 fcp_lun)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230{
231 struct zfcp_unit *unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
Swen Schillig317e6b62008-07-02 10:56:37 +0200233 list_for_each_entry(unit, &port->unit_list_head, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 if ((unit->fcp_lun == fcp_lun) &&
Swen Schillig317e6b62008-07-02 10:56:37 +0200235 !(atomic_read(&unit->status) & ZFCP_STATUS_COMMON_REMOVE))
236 return unit;
237 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238}
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
Swen Schillig317e6b62008-07-02 10:56:37 +0200244 *
245 * Returns: pointer to zfcp_port or NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 */
Swen Schillig317e6b62008-07-02 10:56:37 +0200247struct zfcp_port *zfcp_get_port_by_wwpn(struct zfcp_adapter *adapter,
Swen Schillig7ba58c92008-10-01 12:42:18 +0200248 u64 wwpn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249{
250 struct zfcp_port *port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
Swen Schillig317e6b62008-07-02 10:56:37 +0200252 list_for_each_entry(port, &adapter->port_list_head, list)
Christof Schmitta5b11dd2009-03-02 13:08:54 +0100253 if ((port->wwpn == wwpn) &&
254 !(atomic_read(&port->status) & ZFCP_STATUS_COMMON_REMOVE))
Swen Schillig317e6b62008-07-02 10:56:37 +0200255 return port;
256 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257}
258
Swen Schillig60221922008-07-02 10:56:38 +0200259static void zfcp_sysfs_unit_release(struct device *dev)
260{
261 kfree(container_of(dev, struct zfcp_unit, sysfs_device));
262}
263
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264/**
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
Swen Schillig317e6b62008-07-02 10:56:37 +0200268 * Returns: pointer to enqueued unit on success, ERR_PTR on error
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 * 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 */
Swen Schillig7ba58c92008-10-01 12:42:18 +0200273struct zfcp_unit *zfcp_unit_enqueue(struct zfcp_port *port, u64 fcp_lun)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274{
Christof Schmitt462b7852007-06-19 10:25:30 +0200275 struct zfcp_unit *unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
Swen Schillig317e6b62008-07-02 10:56:37 +0200277 unit = kzalloc(sizeof(struct zfcp_unit), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 if (!unit)
Swen Schillig317e6b62008-07-02 10:56:37 +0200279 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 atomic_set(&unit->refcount, 0);
282 init_waitqueue_head(&unit->remove_wq);
Swen Schillig92d51932009-04-17 15:08:04 +0200283 INIT_WORK(&unit->scsi_work, zfcp_scsi_scan);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
285 unit->port = port;
286 unit->fcp_lun = fcp_lun;
287
Cornelia Huck1bf5b282008-10-10 21:33:10 +0200288 dev_set_name(&unit->sysfs_device, "0x%016llx",
289 (unsigned long long) fcp_lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 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
Christof Schmittc9615852008-05-06 11:00:05 +0200297 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
Swen Schillig317e6b62008-07-02 10:56:37 +0200305 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 }
Swen Schillig317e6b62008-07-02 10:56:37 +0200310 read_unlock_irq(&zfcp_data.config_lock);
311
312 if (device_register(&unit->sysfs_device))
313 goto err_out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
Swen Schillig60221922008-07-02 10:56:38 +0200315 if (sysfs_create_group(&unit->sysfs_device.kobj,
316 &zfcp_sysfs_unit_attrs)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 device_unregister(&unit->sysfs_device);
Swen Schillig317e6b62008-07-02 10:56:37 +0200318 return ERR_PTR(-EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 }
320
321 zfcp_unit_get(unit);
322
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 write_lock_irq(&zfcp_data.config_lock);
Christof Schmitt462b7852007-06-19 10:25:30 +0200324 list_add_tail(&unit->list, &port->unit_list_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 atomic_clear_mask(ZFCP_STATUS_COMMON_REMOVE, &unit->status);
326 atomic_set_mask(ZFCP_STATUS_COMMON_RUNNING, &unit->status);
Swen Schillig317e6b62008-07-02 10:56:37 +0200327
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 write_unlock_irq(&zfcp_data.config_lock);
329
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 zfcp_port_get(port);
331
332 return unit;
Swen Schillig317e6b62008-07-02 10:56:37 +0200333
334err_out_free:
335 kfree(unit);
336 return ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337}
338
Swen Schillig317e6b62008-07-02 10:56:37 +0200339/**
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)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347{
Swen Schillig44cc76f2008-10-01 12:42:16 +0200348 wait_event(unit->remove_wq, atomic_read(&unit->refcount) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 write_lock_irq(&zfcp_data.config_lock);
350 list_del(&unit->list);
351 write_unlock_irq(&zfcp_data.config_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 zfcp_port_put(unit->port);
Swen Schillig60221922008-07-02 10:56:38 +0200353 sysfs_remove_group(&unit->sysfs_device.kobj, &zfcp_sysfs_unit_attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 device_unregister(&unit->sysfs_device);
355}
356
Swen Schillig317e6b62008-07-02 10:56:37 +0200357static int zfcp_allocate_low_mem_buffers(struct zfcp_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358{
Swen Schillig317e6b62008-07-02 10:56:37 +0200359 /* must only be called with zfcp_data.config_sema taken */
Swen Schilliga4623c42009-08-18 15:43:15 +0200360 adapter->pool.erp_req =
361 mempool_create_kmalloc_pool(1, sizeof(struct zfcp_fsf_req));
362 if (!adapter->pool.erp_req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 return -ENOMEM;
364
Swen Schilliga4623c42009-08-18 15:43:15 +0200365 adapter->pool.scsi_req =
366 mempool_create_kmalloc_pool(1, sizeof(struct zfcp_fsf_req));
367 if (!adapter->pool.scsi_req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 return -ENOMEM;
369
Swen Schilliga4623c42009-08-18 15:43:15 +0200370 adapter->pool.scsi_abort =
371 mempool_create_kmalloc_pool(1, sizeof(struct zfcp_fsf_req));
372 if (!adapter->pool.scsi_abort)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 return -ENOMEM;
374
Swen Schilliga4623c42009-08-18 15:43:15 +0200375 adapter->pool.status_read_req =
Swen Schillig317e6b62008-07-02 10:56:37 +0200376 mempool_create_kmalloc_pool(FSF_STATUS_READS_RECOM,
Matthew Dobson0eaae62a2006-03-26 01:37:47 -0800377 sizeof(struct zfcp_fsf_req));
Swen Schilliga4623c42009-08-18 15:43:15 +0200378 if (!adapter->pool.status_read_req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 return -ENOMEM;
380
Swen Schilliga4623c42009-08-18 15:43:15 +0200381 adapter->pool.qtcb_pool =
382 mempool_create_slab_pool(3, zfcp_data.qtcb_cache);
383 if (!adapter->pool.qtcb_pool)
384 return -ENOMEM;
385
386 adapter->pool.status_read_data =
Swen Schillig317e6b62008-07-02 10:56:37 +0200387 mempool_create_slab_pool(FSF_STATUS_READS_RECOM,
Heiko Carstensdd52e0e2006-09-18 22:28:49 +0200388 zfcp_data.sr_buffer_cache);
Swen Schilliga4623c42009-08-18 15:43:15 +0200389 if (!adapter->pool.status_read_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 return -ENOMEM;
391
Swen Schilliga4623c42009-08-18 15:43:15 +0200392 adapter->pool.gid_pn_data =
Swen Schillig317e6b62008-07-02 10:56:37 +0200393 mempool_create_slab_pool(1, zfcp_data.gid_pn_cache);
Swen Schilliga4623c42009-08-18 15:43:15 +0200394 if (!adapter->pool.gid_pn_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 return -ENOMEM;
396
397 return 0;
398}
399
Swen Schillig317e6b62008-07-02 10:56:37 +0200400static void zfcp_free_low_mem_buffers(struct zfcp_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401{
Swen Schillig317e6b62008-07-02 10:56:37 +0200402 /* zfcp_data.config_sema must be held */
Swen Schilliga4623c42009-08-18 15:43:15 +0200403 if (adapter->pool.erp_req)
404 mempool_destroy(adapter->pool.erp_req);
405 if (adapter->pool.scsi_req)
406 mempool_destroy(adapter->pool.scsi_req);
407 if (adapter->pool.scsi_abort)
408 mempool_destroy(adapter->pool.scsi_abort);
409 if (adapter->pool.qtcb_pool)
410 mempool_destroy(adapter->pool.qtcb_pool);
411 if (adapter->pool.status_read_req)
412 mempool_destroy(adapter->pool.status_read_req);
413 if (adapter->pool.status_read_data)
414 mempool_destroy(adapter->pool.status_read_data);
415 if (adapter->pool.gid_pn_data)
416 mempool_destroy(adapter->pool.gid_pn_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417}
418
Swen Schillig317e6b62008-07-02 10:56:37 +0200419/**
420 * zfcp_status_read_refill - refill the long running status_read_requests
421 * @adapter: ptr to struct zfcp_adapter for which the buffers should be refilled
422 *
423 * Returns: 0 on success, 1 otherwise
424 *
425 * if there are 16 or more status_read requests missing an adapter_reopen
426 * is triggered
427 */
Swen Schilligd26ab062008-05-19 12:17:37 +0200428int zfcp_status_read_refill(struct zfcp_adapter *adapter)
429{
430 while (atomic_read(&adapter->stat_miss) > 0)
Swen Schillig564e1c82009-08-18 15:43:19 +0200431 if (zfcp_fsf_status_read(adapter->qdio)) {
Swen Schillig7afe29f2008-07-02 10:56:33 +0200432 if (atomic_read(&adapter->stat_miss) >= 16) {
Swen Schillig5ffd51a2009-03-02 13:09:04 +0100433 zfcp_erp_adapter_reopen(adapter, 0, "axsref1",
434 NULL);
Swen Schillig7afe29f2008-07-02 10:56:33 +0200435 return 1;
436 }
Swen Schilligd26ab062008-05-19 12:17:37 +0200437 break;
Swen Schillig7afe29f2008-07-02 10:56:33 +0200438 } else
439 atomic_dec(&adapter->stat_miss);
Swen Schilligd26ab062008-05-19 12:17:37 +0200440 return 0;
441}
442
443static void _zfcp_status_read_scheduler(struct work_struct *work)
444{
445 zfcp_status_read_refill(container_of(work, struct zfcp_adapter,
446 stat_work));
447}
448
Christof Schmittbd43a42b72008-12-25 13:38:50 +0100449static void zfcp_print_sl(struct seq_file *m, struct service_level *sl)
450{
451 struct zfcp_adapter *adapter =
452 container_of(sl, struct zfcp_adapter, service_level);
453
454 seq_printf(m, "zfcp: %s microcode level %x\n",
455 dev_name(&adapter->ccw_device->dev),
456 adapter->fsf_lic_version);
457}
458
Swen Schillig45446832009-08-18 15:43:17 +0200459static int zfcp_setup_adapter_work_queue(struct zfcp_adapter *adapter)
460{
461 char name[TASK_COMM_LEN];
462
463 snprintf(name, sizeof(name), "zfcp_q_%s",
464 dev_name(&adapter->ccw_device->dev));
465 adapter->work_queue = create_singlethread_workqueue(name);
466
467 if (adapter->work_queue)
468 return 0;
469 return -ENOMEM;
470}
471
472static void zfcp_destroy_adapter_work_queue(struct zfcp_adapter *adapter)
473{
474 if (adapter->work_queue)
475 destroy_workqueue(adapter->work_queue);
476 adapter->work_queue = NULL;
477
478}
479
Swen Schillig317e6b62008-07-02 10:56:37 +0200480/**
481 * zfcp_adapter_enqueue - enqueue a new adapter to the list
482 * @ccw_device: pointer to the struct cc_device
483 *
484 * Returns: 0 if a new adapter was successfully enqueued
485 * -ENOMEM if alloc failed
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 * Enqueues an adapter at the end of the adapter list in the driver data.
487 * All adapter internal structures are set up.
488 * Proc-fs entries are also created.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 * locks: config_sema must be held to serialise changes to the adapter list
490 */
Swen Schillig317e6b62008-07-02 10:56:37 +0200491int zfcp_adapter_enqueue(struct ccw_device *ccw_device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 struct zfcp_adapter *adapter;
494
495 /*
Swen Schillig41fa2ada2007-09-07 09:15:31 +0200496 * Note: It is safe to release the list_lock, as any list changes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 * are protected by the config_sema, which must be held to get here
498 */
499
Swen Schillig317e6b62008-07-02 10:56:37 +0200500 adapter = kzalloc(sizeof(struct zfcp_adapter), GFP_KERNEL);
Christof Schmitt553448f2008-06-10 18:20:58 +0200501 if (!adapter)
Swen Schillig317e6b62008-07-02 10:56:37 +0200502 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
Sven Schuetz9d544f22009-04-06 18:31:47 +0200504 adapter->gs = kzalloc(sizeof(struct zfcp_wka_ports), GFP_KERNEL);
505 if (!adapter->gs) {
506 kfree(adapter);
507 return -ENOMEM;
508 }
509
Swen Schillig564e1c82009-08-18 15:43:19 +0200510 adapter->qdio = kzalloc(sizeof(struct zfcp_qdio), GFP_KERNEL);
511 if (!adapter->qdio)
512 goto qdio_mem_failed;
513
514 adapter->qdio->adapter = adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 ccw_device->handler = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 adapter->ccw_device = ccw_device;
Swen Schillig317e6b62008-07-02 10:56:37 +0200517 atomic_set(&adapter->refcount, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
Swen Schillig564e1c82009-08-18 15:43:19 +0200519 if (zfcp_qdio_allocate(adapter->qdio, ccw_device))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 goto qdio_allocate_failed;
521
Swen Schillig00bab912008-06-10 18:20:57 +0200522 if (zfcp_allocate_low_mem_buffers(adapter))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 goto failed_low_mem_buffers;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
Swen Schillig00bab912008-06-10 18:20:57 +0200525 if (zfcp_reqlist_alloc(adapter))
Volker Sameskefea9d6c2006-08-02 11:05:16 +0200526 goto failed_low_mem_buffers;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
Swen Schillig317e6b62008-07-02 10:56:37 +0200528 if (zfcp_adapter_debug_register(adapter))
529 goto debug_register_failed;
530
Swen Schillig45446832009-08-18 15:43:17 +0200531 if (zfcp_setup_adapter_work_queue(adapter))
532 goto work_queue_failed;
533
Swen Schillig317e6b62008-07-02 10:56:37 +0200534 init_waitqueue_head(&adapter->remove_wq);
535 init_waitqueue_head(&adapter->erp_thread_wqh);
536 init_waitqueue_head(&adapter->erp_done_wqh);
537
538 INIT_LIST_HEAD(&adapter->port_list_head);
Swen Schillig317e6b62008-07-02 10:56:37 +0200539 INIT_LIST_HEAD(&adapter->erp_ready_head);
540 INIT_LIST_HEAD(&adapter->erp_running_head);
541
542 spin_lock_init(&adapter->req_list_lock);
Heiko Carstensc48a29d2005-12-01 02:46:32 +0100543
Swen Schillig564e1c82009-08-18 15:43:19 +0200544 spin_lock_init(&adapter->qdio->req_q_lock);
545 spin_lock_init(&adapter->qdio->stat_lock);
Heiko Carstensc48a29d2005-12-01 02:46:32 +0100546
Heiko Carstensc48a29d2005-12-01 02:46:32 +0100547 rwlock_init(&adapter->erp_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 rwlock_init(&adapter->abort_lock);
Swen Schillig317e6b62008-07-02 10:56:37 +0200549
550 sema_init(&adapter->erp_ready_sem, 0);
551
Swen Schilligd26ab062008-05-19 12:17:37 +0200552 INIT_WORK(&adapter->stat_work, _zfcp_status_read_scheduler);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200553 INIT_WORK(&adapter->scan_work, _zfcp_scan_ports_later);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
Christof Schmittbd43a42b72008-12-25 13:38:50 +0100555 adapter->service_level.seq_print = zfcp_print_sl;
556
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 /* mark adapter unusable as long as sysfs registration is not complete */
558 atomic_set_mask(ZFCP_STATUS_COMMON_REMOVE, &adapter->status);
559
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 dev_set_drvdata(&ccw_device->dev, adapter);
561
Swen Schillig60221922008-07-02 10:56:38 +0200562 if (sysfs_create_group(&ccw_device->dev.kobj,
563 &zfcp_sysfs_adapter_attrs))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 goto sysfs_failed;
565
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 atomic_clear_mask(ZFCP_STATUS_COMMON_REMOVE, &adapter->status);
Sven Schuetz9d544f22009-04-06 18:31:47 +0200567 zfcp_fc_wka_ports_init(adapter);
Swen Schillig828bc122009-04-17 15:08:05 +0200568
Swen Schillig1d3aab02008-12-19 16:56:53 +0100569 if (!zfcp_adapter_scsi_register(adapter))
570 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
Swen Schillig317e6b62008-07-02 10:56:37 +0200572sysfs_failed:
Swen Schillig45446832009-08-18 15:43:17 +0200573 zfcp_destroy_adapter_work_queue(adapter);
574work_queue_failed:
Christof Schmittff17a292007-08-28 09:31:41 +0200575 zfcp_adapter_debug_unregister(adapter);
Swen Schillig317e6b62008-07-02 10:56:37 +0200576debug_register_failed:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 dev_set_drvdata(&ccw_device->dev, NULL);
Swen Schillig317e6b62008-07-02 10:56:37 +0200578 kfree(adapter->req_list);
579failed_low_mem_buffers:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 zfcp_free_low_mem_buffers(adapter);
Swen Schillig317e6b62008-07-02 10:56:37 +0200581qdio_allocate_failed:
Swen Schillig564e1c82009-08-18 15:43:19 +0200582 zfcp_qdio_free(adapter->qdio);
583 kfree(adapter->qdio);
584qdio_mem_failed:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 kfree(adapter);
Swen Schillig317e6b62008-07-02 10:56:37 +0200586 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587}
588
Swen Schillig317e6b62008-07-02 10:56:37 +0200589/**
590 * zfcp_adapter_dequeue - remove the adapter from the resource list
591 * @adapter: pointer to struct zfcp_adapter which should be removed
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 * locks: adapter list write lock is assumed to be held by caller
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 */
Swen Schillig317e6b62008-07-02 10:56:37 +0200594void zfcp_adapter_dequeue(struct zfcp_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595{
596 int retval = 0;
597 unsigned long flags;
598
Swen Schilligcc8c2822008-06-10 18:21:00 +0200599 cancel_work_sync(&adapter->scan_work);
Swen Schilligd26ab062008-05-19 12:17:37 +0200600 cancel_work_sync(&adapter->stat_work);
Christof Schmitt55c770f2009-08-18 15:43:12 +0200601 zfcp_fc_wka_ports_force_offline(adapter->gs);
Michael Loehr9f287452007-05-09 11:01:24 +0200602 zfcp_adapter_scsi_unregister(adapter);
Swen Schillig60221922008-07-02 10:56:38 +0200603 sysfs_remove_group(&adapter->ccw_device->dev.kobj,
604 &zfcp_sysfs_adapter_attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 dev_set_drvdata(&adapter->ccw_device->dev, NULL);
606 /* sanity check: no pending FSF requests */
Volker Sameskefea9d6c2006-08-02 11:05:16 +0200607 spin_lock_irqsave(&adapter->req_list_lock, flags);
608 retval = zfcp_reqlist_isempty(adapter);
609 spin_unlock_irqrestore(&adapter->req_list_lock, flags);
Swen Schillig317e6b62008-07-02 10:56:37 +0200610 if (!retval)
611 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
Swen Schillig45446832009-08-18 15:43:17 +0200613 zfcp_destroy_adapter_work_queue(adapter);
Christof Schmittff17a292007-08-28 09:31:41 +0200614 zfcp_adapter_debug_unregister(adapter);
Swen Schillig564e1c82009-08-18 15:43:19 +0200615 zfcp_qdio_free(adapter->qdio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 zfcp_free_low_mem_buffers(adapter);
Swen Schillig317e6b62008-07-02 10:56:37 +0200617 kfree(adapter->req_list);
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100618 kfree(adapter->fc_stats);
619 kfree(adapter->stats_reset_data);
Sven Schuetz9d544f22009-04-06 18:31:47 +0200620 kfree(adapter->gs);
Swen Schillig564e1c82009-08-18 15:43:19 +0200621 kfree(adapter->qdio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 kfree(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623}
624
Swen Schillig60221922008-07-02 10:56:38 +0200625static void zfcp_sysfs_port_release(struct device *dev)
626{
627 kfree(container_of(dev, struct zfcp_port, sysfs_device));
628}
629
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630/**
631 * zfcp_port_enqueue - enqueue port to port list of adapter
632 * @adapter: adapter where remote port is added
633 * @wwpn: WWPN of the remote port to be enqueued
634 * @status: initial status for the port
635 * @d_id: destination id of the remote port to be enqueued
Swen Schillig317e6b62008-07-02 10:56:37 +0200636 * Returns: pointer to enqueued port on success, ERR_PTR on error
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 * Locks: config_sema must be held to serialize changes to the port list
638 *
639 * All port internal structures are set up and the sysfs entry is generated.
640 * d_id is used to enqueue ports with a well known address like the Directory
641 * Service for nameserver lookup.
642 */
Swen Schillig7ba58c92008-10-01 12:42:18 +0200643struct zfcp_port *zfcp_port_enqueue(struct zfcp_adapter *adapter, u64 wwpn,
Swen Schillig317e6b62008-07-02 10:56:37 +0200644 u32 status, u32 d_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645{
Andreas Herrmann3859f6a2005-08-27 11:07:54 -0700646 struct zfcp_port *port;
Swen Schillig60221922008-07-02 10:56:38 +0200647 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
Swen Schillig317e6b62008-07-02 10:56:37 +0200649 port = kzalloc(sizeof(struct zfcp_port), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 if (!port)
Swen Schillig317e6b62008-07-02 10:56:37 +0200651 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 init_waitqueue_head(&port->remove_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 INIT_LIST_HEAD(&port->unit_list_head);
Swen Schillig5ab944f2008-10-01 12:42:17 +0200655 INIT_WORK(&port->gid_pn_work, zfcp_erp_port_strategy_open_lookup);
Christof Schmitt8fdf30d2009-03-02 13:09:01 +0100656 INIT_WORK(&port->test_link_work, zfcp_fc_link_test_work);
Christof Schmitta2fa0ae2009-03-02 13:09:08 +0100657 INIT_WORK(&port->rport_work, zfcp_scsi_rport_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
659 port->adapter = adapter;
Swen Schillig317e6b62008-07-02 10:56:37 +0200660 port->d_id = d_id;
661 port->wwpn = wwpn;
Christof Schmitta2fa0ae2009-03-02 13:09:08 +0100662 port->rport_task = RPORT_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663
Swen Schillig317e6b62008-07-02 10:56:37 +0200664 /* mark port unusable as long as sysfs registration is not complete */
665 atomic_set_mask(status | ZFCP_STATUS_COMMON_REMOVE, &port->status);
666 atomic_set(&port->refcount, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
Christof Schmittadc90da2008-11-04 16:35:09 +0100668 dev_set_name(&port->sysfs_device, "0x%016llx",
669 (unsigned long long)wwpn);
Swen Schillig5ab944f2008-10-01 12:42:17 +0200670 port->sysfs_device.parent = &adapter->ccw_device->dev;
Swen Schilligcc8c2822008-06-10 18:21:00 +0200671
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 port->sysfs_device.release = zfcp_sysfs_port_release;
673 dev_set_drvdata(&port->sysfs_device, port);
674
Swen Schillig317e6b62008-07-02 10:56:37 +0200675 read_lock_irq(&zfcp_data.config_lock);
Christof Schmitta5b11dd2009-03-02 13:08:54 +0100676 if (zfcp_get_port_by_wwpn(adapter, wwpn)) {
677 read_unlock_irq(&zfcp_data.config_lock);
678 goto err_out_free;
679 }
Swen Schillig317e6b62008-07-02 10:56:37 +0200680 read_unlock_irq(&zfcp_data.config_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
Swen Schillig317e6b62008-07-02 10:56:37 +0200682 if (device_register(&port->sysfs_device))
683 goto err_out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
Swen Schillig5ab944f2008-10-01 12:42:17 +0200685 retval = sysfs_create_group(&port->sysfs_device.kobj,
686 &zfcp_sysfs_port_attrs);
Swen Schillig60221922008-07-02 10:56:38 +0200687
688 if (retval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 device_unregister(&port->sysfs_device);
Swen Schillig317e6b62008-07-02 10:56:37 +0200690 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 }
692
693 zfcp_port_get(port);
694
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 write_lock_irq(&zfcp_data.config_lock);
Andreas Herrmann3859f6a2005-08-27 11:07:54 -0700696 list_add_tail(&port->list, &adapter->port_list_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 atomic_clear_mask(ZFCP_STATUS_COMMON_REMOVE, &port->status);
698 atomic_set_mask(ZFCP_STATUS_COMMON_RUNNING, &port->status);
Swen Schillig317e6b62008-07-02 10:56:37 +0200699
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 write_unlock_irq(&zfcp_data.config_lock);
701
702 zfcp_adapter_get(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 return port;
Swen Schillig317e6b62008-07-02 10:56:37 +0200704
705err_out_free:
706 kfree(port);
707err_out:
708 return ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709}
710
Swen Schillig317e6b62008-07-02 10:56:37 +0200711/**
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 */
715void zfcp_port_dequeue(struct zfcp_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 write_lock_irq(&zfcp_data.config_lock);
718 list_del(&port->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 write_unlock_irq(&zfcp_data.config_lock);
Swen Schilliga67417a2009-08-18 15:43:06 +0200720 if (port->rport) {
Christof Schmitt70932932009-04-17 15:08:15 +0200721 port->rport->dd_data = NULL;
Swen Schilliga67417a2009-08-18 15:43:06 +0200722 port->rport = NULL;
723 }
724 wait_event(port->remove_wq, atomic_read(&port->refcount) == 0);
725 cancel_work_sync(&port->rport_work); /* usually not necessary */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 zfcp_adapter_put(port->adapter);
Swen Schillig5ab944f2008-10-01 12:42:17 +0200727 sysfs_remove_group(&port->sysfs_device.kobj, &zfcp_sysfs_port_attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 device_unregister(&port->sysfs_device);
729}
730
Swen Schillig317e6b62008-07-02 10:56:37 +0200731/**
732 * zfcp_sg_free_table - free memory used by scatterlists
733 * @sg: pointer to scatterlist
734 * @count: number of scatterlist which are to be free'ed
735 * the scatterlist are expected to reference pages always
736 */
Christof Schmitt45633fd2008-06-10 18:20:55 +0200737void zfcp_sg_free_table(struct scatterlist *sg, int count)
738{
739 int i;
740
741 for (i = 0; i < count; i++, sg++)
742 if (sg)
743 free_page((unsigned long) sg_virt(sg));
744 else
745 break;
746}
747
Swen Schillig317e6b62008-07-02 10:56:37 +0200748/**
749 * zfcp_sg_setup_table - init scatterlist and allocate, assign buffers
750 * @sg: pointer to struct scatterlist
751 * @count: number of scatterlists which should be assigned with buffers
752 * of size page
753 *
754 * Returns: 0 on success, -ENOMEM otherwise
755 */
Christof Schmitt45633fd2008-06-10 18:20:55 +0200756int zfcp_sg_setup_table(struct scatterlist *sg, int count)
757{
758 void *addr;
759 int i;
760
761 sg_init_table(sg, count);
762 for (i = 0; i < count; i++, sg++) {
763 addr = (void *) get_zeroed_page(GFP_KERNEL);
764 if (!addr) {
765 zfcp_sg_free_table(sg, i);
766 return -ENOMEM;
767 }
768 sg_set_buf(sg, addr, PAGE_SIZE);
769 }
770 return 0;
771}