blob: 2889e5f2dfd3f95ff0558f476bb6e68620f05191 [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 Schmittbd43a422008-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{
Christof Schmittc5afd812009-09-24 10:23:22 +020083 struct ccw_device *ccwdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 struct zfcp_adapter *adapter;
85 struct zfcp_port *port;
86 struct zfcp_unit *unit;
87
Christof Schmittc5afd812009-09-24 10:23:22 +020088 ccwdev = get_ccwdev_by_busid(&zfcp_ccw_driver, busid);
89 if (!ccwdev)
90 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Christof Schmittc5afd812009-09-24 10:23:22 +020092 if (ccw_device_set_online(ccwdev))
93 goto out_ccwdev;
94
95 mutex_lock(&zfcp_data.config_mutex);
96 adapter = dev_get_drvdata(&ccwdev->dev);
Swen Schillig317e6b62008-07-02 10:56:37 +020097 if (!adapter)
Christof Schmittc5afd812009-09-24 10:23:22 +020098 goto out_unlock;
99 zfcp_adapter_get(adapter);
100
101 port = zfcp_get_port_by_wwpn(adapter, wwpn);
102 if (!port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 goto out_port;
Christof Schmittc5afd812009-09-24 10:23:22 +0200104
105 zfcp_port_get(port);
Christof Schmitt3623ecb2008-12-19 16:56:57 +0100106 unit = zfcp_unit_enqueue(port, lun);
Swen Schillig317e6b62008-07-02 10:56:37 +0200107 if (IS_ERR(unit))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 goto out_unit;
Christof Schmitt24680de2009-08-18 15:43:28 +0200109 mutex_unlock(&zfcp_data.config_mutex);
Swen Schillig091694a2008-10-01 12:42:25 +0200110
Christof Schmittc5afd812009-09-24 10:23:22 +0200111 zfcp_erp_unit_reopen(unit, 0, "auidc_1", NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 zfcp_erp_wait(adapter);
Swen Schillig92d51932009-04-17 15:08:04 +0200113 flush_work(&unit->scsi_work);
Swen Schillig091694a2008-10-01 12:42:25 +0200114
Christof Schmitt24680de2009-08-18 15:43:28 +0200115 mutex_lock(&zfcp_data.config_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 zfcp_unit_put(unit);
Swen Schillig317e6b62008-07-02 10:56:37 +0200117out_unit:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 zfcp_port_put(port);
Swen Schillig317e6b62008-07-02 10:56:37 +0200119out_port:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 zfcp_adapter_put(adapter);
Christof Schmittc5afd812009-09-24 10:23:22 +0200121out_unlock:
Christof Schmitt24680de2009-08-18 15:43:28 +0200122 mutex_unlock(&zfcp_data.config_mutex);
Christof Schmittc5afd812009-09-24 10:23:22 +0200123out_ccwdev:
124 put_device(&ccwdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 return;
126}
127
Christof Schmitt3623ecb2008-12-19 16:56:57 +0100128static void __init zfcp_init_device_setup(char *devstr)
129{
130 char *token;
Heiko Carstensd10c0852009-10-13 10:44:07 +0200131 char *str, *str_saved;
Christof Schmitt3623ecb2008-12-19 16:56:57 +0100132 char busid[ZFCP_BUS_ID_SIZE];
133 u64 wwpn, lun;
134
135 /* duplicate devstr and keep the original for sysfs presentation*/
Heiko Carstensd10c0852009-10-13 10:44:07 +0200136 str_saved = kmalloc(strlen(devstr) + 1, GFP_KERNEL);
137 str = str_saved;
Christof Schmitt3623ecb2008-12-19 16:56:57 +0100138 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
Heiko Carstensd10c0852009-10-13 10:44:07 +0200156 kfree(str_saved);
Christof Schmitt3623ecb2008-12-19 16:56:57 +0100157 zfcp_init_device_configure(busid, wwpn, lun);
158 return;
159
Heiko Carstensd10c0852009-10-13 10:44:07 +0200160err_out:
161 kfree(str_saved);
Christof Schmitt3623ecb2008-12-19 16:56:57 +0100162 pr_err("%s is not a valid SCSI device\n", devstr);
163}
164
Swen Schillig317e6b62008-07-02 10:56:37 +0200165static int __init zfcp_module_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166{
Heiko Carstensdd52e0e2006-09-18 22:28:49 +0200167 int retval = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
Swen Schilliga4623c42009-08-18 15:43:15 +0200169 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)
Heiko Carstensdd52e0e2006-09-18 22:28:49 +0200172 goto out;
173
Swen Schilliga4623c42009-08-18 15:43:15 +0200174 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));
Heiko Carstensdd52e0e2006-09-18 22:28:49 +0200181 if (!zfcp_data.sr_buffer_cache)
182 goto out_sr_cache;
183
Swen Schilliga4623c42009-08-18 15:43:15 +0200184 zfcp_data.gid_pn_cache = zfcp_cache_hw_align("zfcp_gid",
185 sizeof(struct zfcp_gid_pn_data));
Heiko Carstensdd52e0e2006-09-18 22:28:49 +0200186 if (!zfcp_data.gid_pn_cache)
187 goto out_gid_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
Christof Schmitt24680de2009-08-18 15:43:28 +0200189 mutex_init(&zfcp_data.config_mutex);
Swen Schillig317e6b62008-07-02 10:56:37 +0200190 rwlock_init(&zfcp_data.config_lock);
191
Heiko Carstensdd52e0e2006-09-18 22:28:49 +0200192 zfcp_data.scsi_transport_template =
193 fc_attach_transport(&zfcp_transport_functions);
194 if (!zfcp_data.scsi_transport_template)
195 goto out_transport;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 retval = misc_register(&zfcp_cfdc_misc);
Swen Schillig317e6b62008-07-02 10:56:37 +0200198 if (retval) {
Christof Schmittecf39d42008-12-25 13:39:53 +0100199 pr_err("Registering the misc device zfcp_cfdc failed\n");
Heiko Carstensdd52e0e2006-09-18 22:28:49 +0200200 goto out_misc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 }
202
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 retval = zfcp_ccw_register();
204 if (retval) {
Christof Schmittecf39d42008-12-25 13:39:53 +0100205 pr_err("The zfcp device driver could not register with "
Christof Schmittff3b24f2008-10-01 12:42:15 +0200206 "the common I/O layer\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 goto out_ccw_register;
208 }
209
Christof Schmitt3623ecb2008-12-19 16:56:57 +0100210 if (init_device)
211 zfcp_init_device_setup(init_device);
212 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
Swen Schillig317e6b62008-07-02 10:56:37 +0200214out_ccw_register:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 misc_deregister(&zfcp_cfdc_misc);
Swen Schillig317e6b62008-07-02 10:56:37 +0200216out_misc:
Heiko Carstensdd52e0e2006-09-18 22:28:49 +0200217 fc_release_transport(zfcp_data.scsi_transport_template);
Swen Schillig317e6b62008-07-02 10:56:37 +0200218out_transport:
Heiko Carstensdd52e0e2006-09-18 22:28:49 +0200219 kmem_cache_destroy(zfcp_data.gid_pn_cache);
Swen Schillig317e6b62008-07-02 10:56:37 +0200220out_gid_cache:
Heiko Carstensdd52e0e2006-09-18 22:28:49 +0200221 kmem_cache_destroy(zfcp_data.sr_buffer_cache);
Swen Schillig317e6b62008-07-02 10:56:37 +0200222out_sr_cache:
Swen Schilliga4623c42009-08-18 15:43:15 +0200223 kmem_cache_destroy(zfcp_data.qtcb_cache);
224out_qtcb_cache:
225 kmem_cache_destroy(zfcp_data.gpn_ft_cache);
Swen Schillig317e6b62008-07-02 10:56:37 +0200226out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 return retval;
228}
229
Swen Schillig317e6b62008-07-02 10:56:37 +0200230module_init(zfcp_module_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232/**
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
Swen Schillig317e6b62008-07-02 10:56:37 +0200236 *
237 * Returns: pointer to zfcp_unit or NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 */
Swen Schillig7ba58c92008-10-01 12:42:18 +0200239struct zfcp_unit *zfcp_get_unit_by_lun(struct zfcp_port *port, u64 fcp_lun)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240{
241 struct zfcp_unit *unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
Swen Schillig317e6b62008-07-02 10:56:37 +0200243 list_for_each_entry(unit, &port->unit_list_head, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 if ((unit->fcp_lun == fcp_lun) &&
Swen Schillig317e6b62008-07-02 10:56:37 +0200245 !(atomic_read(&unit->status) & ZFCP_STATUS_COMMON_REMOVE))
246 return unit;
247 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248}
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
Swen Schillig317e6b62008-07-02 10:56:37 +0200254 *
255 * Returns: pointer to zfcp_port or NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 */
Swen Schillig317e6b62008-07-02 10:56:37 +0200257struct zfcp_port *zfcp_get_port_by_wwpn(struct zfcp_adapter *adapter,
Swen Schillig7ba58c92008-10-01 12:42:18 +0200258 u64 wwpn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259{
260 struct zfcp_port *port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
Swen Schillig317e6b62008-07-02 10:56:37 +0200262 list_for_each_entry(port, &adapter->port_list_head, list)
Christof Schmitta5b11dd2009-03-02 13:08:54 +0100263 if ((port->wwpn == wwpn) &&
264 !(atomic_read(&port->status) & ZFCP_STATUS_COMMON_REMOVE))
Swen Schillig317e6b62008-07-02 10:56:37 +0200265 return port;
266 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267}
268
Swen Schillig60221922008-07-02 10:56:38 +0200269static void zfcp_sysfs_unit_release(struct device *dev)
270{
271 kfree(container_of(dev, struct zfcp_unit, sysfs_device));
272}
273
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274/**
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
Swen Schillig317e6b62008-07-02 10:56:37 +0200278 * Returns: pointer to enqueued unit on success, ERR_PTR on error
Christof Schmitt24680de2009-08-18 15:43:28 +0200279 * Locks: config_mutex must be held to serialize changes to the unit list
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 *
281 * Sets up some unit internal structures and creates sysfs entry.
282 */
Swen Schillig7ba58c92008-10-01 12:42:18 +0200283struct zfcp_unit *zfcp_unit_enqueue(struct zfcp_port *port, u64 fcp_lun)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284{
Christof Schmitt462b7852007-06-19 10:25:30 +0200285 struct zfcp_unit *unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
Christof Schmitt0fac3f42009-08-18 15:43:30 +0200287 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
Swen Schillig317e6b62008-07-02 10:56:37 +0200294 unit = kzalloc(sizeof(struct zfcp_unit), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 if (!unit)
Swen Schillig317e6b62008-07-02 10:56:37 +0200296 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 atomic_set(&unit->refcount, 0);
299 init_waitqueue_head(&unit->remove_wq);
Swen Schillig92d51932009-04-17 15:08:04 +0200300 INIT_WORK(&unit->scsi_work, zfcp_scsi_scan);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
302 unit->port = port;
303 unit->fcp_lun = fcp_lun;
304
Christof Schmitt0fac3f42009-08-18 15:43:30 +0200305 if (dev_set_name(&unit->sysfs_device, "0x%016llx",
306 (unsigned long long) fcp_lun)) {
307 kfree(unit);
308 return ERR_PTR(-ENOMEM);
309 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 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
Christof Schmittc9615852008-05-06 11:00:05 +0200317 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
Sebastian Ottf4395b62009-08-18 15:43:29 +0200325 if (device_register(&unit->sysfs_device)) {
326 put_device(&unit->sysfs_device);
327 return ERR_PTR(-EINVAL);
328 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
Swen Schillig60221922008-07-02 10:56:38 +0200330 if (sysfs_create_group(&unit->sysfs_device.kobj,
331 &zfcp_sysfs_unit_attrs)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 device_unregister(&unit->sysfs_device);
Christof Schmitt0fac3f42009-08-18 15:43:30 +0200333 return ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 }
335
336 zfcp_unit_get(unit);
337
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 write_lock_irq(&zfcp_data.config_lock);
Christof Schmitt462b7852007-06-19 10:25:30 +0200339 list_add_tail(&unit->list, &port->unit_list_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 atomic_clear_mask(ZFCP_STATUS_COMMON_REMOVE, &unit->status);
341 atomic_set_mask(ZFCP_STATUS_COMMON_RUNNING, &unit->status);
Swen Schillig317e6b62008-07-02 10:56:37 +0200342
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 write_unlock_irq(&zfcp_data.config_lock);
344
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 zfcp_port_get(port);
346
347 return unit;
348}
349
Swen Schillig317e6b62008-07-02 10:56:37 +0200350/**
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)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358{
Swen Schillig44cc76f2008-10-01 12:42:16 +0200359 wait_event(unit->remove_wq, atomic_read(&unit->refcount) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 write_lock_irq(&zfcp_data.config_lock);
361 list_del(&unit->list);
362 write_unlock_irq(&zfcp_data.config_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 zfcp_port_put(unit->port);
Swen Schillig60221922008-07-02 10:56:38 +0200364 sysfs_remove_group(&unit->sysfs_device.kobj, &zfcp_sysfs_unit_attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 device_unregister(&unit->sysfs_device);
366}
367
Swen Schillig317e6b62008-07-02 10:56:37 +0200368static int zfcp_allocate_low_mem_buffers(struct zfcp_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369{
Christof Schmitt24680de2009-08-18 15:43:28 +0200370 /* must only be called with zfcp_data.config_mutex taken */
Swen Schilliga4623c42009-08-18 15:43:15 +0200371 adapter->pool.erp_req =
372 mempool_create_kmalloc_pool(1, sizeof(struct zfcp_fsf_req));
373 if (!adapter->pool.erp_req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 return -ENOMEM;
375
Christof Schmitt799b76d2009-08-18 15:43:20 +0200376 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
Swen Schilliga4623c42009-08-18 15:43:15 +0200381 adapter->pool.scsi_req =
382 mempool_create_kmalloc_pool(1, sizeof(struct zfcp_fsf_req));
383 if (!adapter->pool.scsi_req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 return -ENOMEM;
385
Swen Schilliga4623c42009-08-18 15:43:15 +0200386 adapter->pool.scsi_abort =
387 mempool_create_kmalloc_pool(1, sizeof(struct zfcp_fsf_req));
388 if (!adapter->pool.scsi_abort)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 return -ENOMEM;
390
Swen Schilliga4623c42009-08-18 15:43:15 +0200391 adapter->pool.status_read_req =
Swen Schillig317e6b62008-07-02 10:56:37 +0200392 mempool_create_kmalloc_pool(FSF_STATUS_READS_RECOM,
Matthew Dobson0eaae62a2006-03-26 01:37:47 -0800393 sizeof(struct zfcp_fsf_req));
Swen Schilliga4623c42009-08-18 15:43:15 +0200394 if (!adapter->pool.status_read_req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 return -ENOMEM;
396
Swen Schilliga4623c42009-08-18 15:43:15 +0200397 adapter->pool.qtcb_pool =
Christof Schmitt799b76d2009-08-18 15:43:20 +0200398 mempool_create_slab_pool(4, zfcp_data.qtcb_cache);
Swen Schilliga4623c42009-08-18 15:43:15 +0200399 if (!adapter->pool.qtcb_pool)
400 return -ENOMEM;
401
402 adapter->pool.status_read_data =
Swen Schillig317e6b62008-07-02 10:56:37 +0200403 mempool_create_slab_pool(FSF_STATUS_READS_RECOM,
Heiko Carstensdd52e0e2006-09-18 22:28:49 +0200404 zfcp_data.sr_buffer_cache);
Swen Schilliga4623c42009-08-18 15:43:15 +0200405 if (!adapter->pool.status_read_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 return -ENOMEM;
407
Swen Schilliga4623c42009-08-18 15:43:15 +0200408 adapter->pool.gid_pn_data =
Swen Schillig317e6b62008-07-02 10:56:37 +0200409 mempool_create_slab_pool(1, zfcp_data.gid_pn_cache);
Swen Schilliga4623c42009-08-18 15:43:15 +0200410 if (!adapter->pool.gid_pn_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 return -ENOMEM;
412
413 return 0;
414}
415
Swen Schillig317e6b62008-07-02 10:56:37 +0200416static void zfcp_free_low_mem_buffers(struct zfcp_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417{
Christof Schmitt24680de2009-08-18 15:43:28 +0200418 /* zfcp_data.config_mutex must be held */
Swen Schilliga4623c42009-08-18 15:43:15 +0200419 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433}
434
Swen Schillig317e6b62008-07-02 10:56:37 +0200435/**
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 */
Swen Schilligd26ab062008-05-19 12:17:37 +0200444int zfcp_status_read_refill(struct zfcp_adapter *adapter)
445{
446 while (atomic_read(&adapter->stat_miss) > 0)
Swen Schillig564e1c82009-08-18 15:43:19 +0200447 if (zfcp_fsf_status_read(adapter->qdio)) {
Swen Schillig7afe29f2008-07-02 10:56:33 +0200448 if (atomic_read(&adapter->stat_miss) >= 16) {
Swen Schillig5ffd51a2009-03-02 13:09:04 +0100449 zfcp_erp_adapter_reopen(adapter, 0, "axsref1",
450 NULL);
Swen Schillig7afe29f2008-07-02 10:56:33 +0200451 return 1;
452 }
Swen Schilligd26ab062008-05-19 12:17:37 +0200453 break;
Swen Schillig7afe29f2008-07-02 10:56:33 +0200454 } else
455 atomic_dec(&adapter->stat_miss);
Swen Schilligd26ab062008-05-19 12:17:37 +0200456 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
Christof Schmittbd43a422008-12-25 13:38:50 +0100465static 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
Swen Schillig45446832009-08-18 15:43:17 +0200475static 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
Swen Schillig317e6b62008-07-02 10:56:37 +0200496/**
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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 * 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.
Christof Schmitt24680de2009-08-18 15:43:28 +0200505 * locks: config_mutex must be held to serialize changes to the adapter list
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 */
Swen Schillig317e6b62008-07-02 10:56:37 +0200507int zfcp_adapter_enqueue(struct ccw_device *ccw_device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 struct zfcp_adapter *adapter;
510
511 /*
Swen Schillig41fa2ad2007-09-07 09:15:31 +0200512 * Note: It is safe to release the list_lock, as any list changes
Christof Schmitt24680de2009-08-18 15:43:28 +0200513 * are protected by the config_mutex, which must be held to get here
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 */
515
Swen Schillig317e6b62008-07-02 10:56:37 +0200516 adapter = kzalloc(sizeof(struct zfcp_adapter), GFP_KERNEL);
Christof Schmitt553448f2008-06-10 18:20:58 +0200517 if (!adapter)
Swen Schillig317e6b62008-07-02 10:56:37 +0200518 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
520 ccw_device->handler = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 adapter->ccw_device = ccw_device;
Swen Schillig317e6b62008-07-02 10:56:37 +0200522 atomic_set(&adapter->refcount, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
Swen Schilligd5a282a2009-08-18 15:43:22 +0200524 if (zfcp_qdio_setup(adapter))
525 goto qdio_failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
Swen Schillig00bab912008-06-10 18:20:57 +0200527 if (zfcp_allocate_low_mem_buffers(adapter))
Swen Schilligd5a282a2009-08-18 15:43:22 +0200528 goto low_mem_buffers_failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
Swen Schillig00bab912008-06-10 18:20:57 +0200530 if (zfcp_reqlist_alloc(adapter))
Swen Schilligd5a282a2009-08-18 15:43:22 +0200531 goto low_mem_buffers_failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
Swen Schillig57717102009-08-18 15:43:21 +0200533 if (zfcp_dbf_adapter_register(adapter))
Swen Schillig317e6b62008-07-02 10:56:37 +0200534 goto debug_register_failed;
535
Swen Schillig45446832009-08-18 15:43:17 +0200536 if (zfcp_setup_adapter_work_queue(adapter))
537 goto work_queue_failed;
538
Swen Schilligd5a282a2009-08-18 15:43:22 +0200539 if (zfcp_fc_gs_setup(adapter))
540 goto generic_services_failed;
541
Swen Schillig317e6b62008-07-02 10:56:37 +0200542 init_waitqueue_head(&adapter->remove_wq);
Christof Schmitt347c6a92009-08-18 15:43:25 +0200543 init_waitqueue_head(&adapter->erp_ready_wq);
Swen Schillig317e6b62008-07-02 10:56:37 +0200544 init_waitqueue_head(&adapter->erp_done_wqh);
545
546 INIT_LIST_HEAD(&adapter->port_list_head);
Swen Schillig317e6b62008-07-02 10:56:37 +0200547 INIT_LIST_HEAD(&adapter->erp_ready_head);
548 INIT_LIST_HEAD(&adapter->erp_running_head);
549
550 spin_lock_init(&adapter->req_list_lock);
Heiko Carstensc48a29d2005-12-01 02:46:32 +0100551
Heiko Carstensc48a29d2005-12-01 02:46:32 +0100552 rwlock_init(&adapter->erp_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 rwlock_init(&adapter->abort_lock);
Swen Schillig317e6b62008-07-02 10:56:37 +0200554
Christof Schmitt143bb6b2009-08-18 15:43:27 +0200555 if (zfcp_erp_thread_setup(adapter))
556 goto erp_thread_failed;
557
Swen Schilligd26ab062008-05-19 12:17:37 +0200558 INIT_WORK(&adapter->stat_work, _zfcp_status_read_scheduler);
Swen Schillig6f53a2d2009-08-18 15:43:23 +0200559 INIT_WORK(&adapter->scan_work, _zfcp_fc_scan_ports_later);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
Christof Schmittbd43a422008-12-25 13:38:50 +0100561 adapter->service_level.seq_print = zfcp_print_sl;
562
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 /* mark adapter unusable as long as sysfs registration is not complete */
564 atomic_set_mask(ZFCP_STATUS_COMMON_REMOVE, &adapter->status);
565
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 dev_set_drvdata(&ccw_device->dev, adapter);
567
Swen Schillig60221922008-07-02 10:56:38 +0200568 if (sysfs_create_group(&ccw_device->dev.kobj,
569 &zfcp_sysfs_adapter_attrs))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 goto sysfs_failed;
571
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 atomic_clear_mask(ZFCP_STATUS_COMMON_REMOVE, &adapter->status);
Swen Schillig828bc122009-04-17 15:08:05 +0200573
Swen Schillig1d3aab02008-12-19 16:56:53 +0100574 if (!zfcp_adapter_scsi_register(adapter))
575 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
Swen Schillig317e6b62008-07-02 10:56:37 +0200577sysfs_failed:
Christof Schmitt143bb6b2009-08-18 15:43:27 +0200578 zfcp_erp_thread_kill(adapter);
579erp_thread_failed:
Swen Schilligd5a282a2009-08-18 15:43:22 +0200580 zfcp_fc_gs_destroy(adapter);
581generic_services_failed:
Swen Schillig45446832009-08-18 15:43:17 +0200582 zfcp_destroy_adapter_work_queue(adapter);
583work_queue_failed:
Swen Schillig57717102009-08-18 15:43:21 +0200584 zfcp_dbf_adapter_unregister(adapter->dbf);
Swen Schillig317e6b62008-07-02 10:56:37 +0200585debug_register_failed:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 dev_set_drvdata(&ccw_device->dev, NULL);
Swen Schillig317e6b62008-07-02 10:56:37 +0200587 kfree(adapter->req_list);
Swen Schilligd5a282a2009-08-18 15:43:22 +0200588low_mem_buffers_failed:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 zfcp_free_low_mem_buffers(adapter);
Swen Schilligd5a282a2009-08-18 15:43:22 +0200590qdio_failed:
591 zfcp_qdio_destroy(adapter->qdio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 kfree(adapter);
Swen Schillig317e6b62008-07-02 10:56:37 +0200593 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594}
595
Swen Schillig317e6b62008-07-02 10:56:37 +0200596/**
597 * zfcp_adapter_dequeue - remove the adapter from the resource list
598 * @adapter: pointer to struct zfcp_adapter which should be removed
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 * locks: adapter list write lock is assumed to be held by caller
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 */
Swen Schillig317e6b62008-07-02 10:56:37 +0200601void zfcp_adapter_dequeue(struct zfcp_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602{
603 int retval = 0;
604 unsigned long flags;
605
Swen Schilligd26ab062008-05-19 12:17:37 +0200606 cancel_work_sync(&adapter->stat_work);
Christof Schmitt55c770f2009-08-18 15:43:12 +0200607 zfcp_fc_wka_ports_force_offline(adapter->gs);
Swen Schillig60221922008-07-02 10:56:38 +0200608 sysfs_remove_group(&adapter->ccw_device->dev.kobj,
609 &zfcp_sysfs_adapter_attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 dev_set_drvdata(&adapter->ccw_device->dev, NULL);
611 /* sanity check: no pending FSF requests */
Volker Sameskefea9d6c2006-08-02 11:05:16 +0200612 spin_lock_irqsave(&adapter->req_list_lock, flags);
613 retval = zfcp_reqlist_isempty(adapter);
614 spin_unlock_irqrestore(&adapter->req_list_lock, flags);
Swen Schillig317e6b62008-07-02 10:56:37 +0200615 if (!retval)
616 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
Swen Schilligd5a282a2009-08-18 15:43:22 +0200618 zfcp_fc_gs_destroy(adapter);
Christof Schmitt143bb6b2009-08-18 15:43:27 +0200619 zfcp_erp_thread_kill(adapter);
Swen Schillig45446832009-08-18 15:43:17 +0200620 zfcp_destroy_adapter_work_queue(adapter);
Swen Schillig57717102009-08-18 15:43:21 +0200621 zfcp_dbf_adapter_unregister(adapter->dbf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 zfcp_free_low_mem_buffers(adapter);
Swen Schilligd5a282a2009-08-18 15:43:22 +0200623 zfcp_qdio_destroy(adapter->qdio);
Swen Schillig317e6b62008-07-02 10:56:37 +0200624 kfree(adapter->req_list);
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100625 kfree(adapter->fc_stats);
626 kfree(adapter->stats_reset_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 kfree(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628}
629
Swen Schillig60221922008-07-02 10:56:38 +0200630static void zfcp_sysfs_port_release(struct device *dev)
631{
632 kfree(container_of(dev, struct zfcp_port, sysfs_device));
633}
634
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635/**
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
Swen Schillig317e6b62008-07-02 10:56:37 +0200641 * Returns: pointer to enqueued port on success, ERR_PTR on error
Christof Schmitt24680de2009-08-18 15:43:28 +0200642 * Locks: config_mutex must be held to serialize changes to the port list
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 *
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 */
Swen Schillig7ba58c92008-10-01 12:42:18 +0200648struct zfcp_port *zfcp_port_enqueue(struct zfcp_adapter *adapter, u64 wwpn,
Swen Schillig317e6b62008-07-02 10:56:37 +0200649 u32 status, u32 d_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650{
Andreas Herrmann3859f6a2005-08-27 11:07:54 -0700651 struct zfcp_port *port;
Christof Schmitt0fac3f42009-08-18 15:43:30 +0200652
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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
Swen Schillig317e6b62008-07-02 10:56:37 +0200660 port = kzalloc(sizeof(struct zfcp_port), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 if (!port)
Swen Schillig317e6b62008-07-02 10:56:37 +0200662 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 init_waitqueue_head(&port->remove_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 INIT_LIST_HEAD(&port->unit_list_head);
Christof Schmitt799b76d2009-08-18 15:43:20 +0200666 INIT_WORK(&port->gid_pn_work, zfcp_fc_port_did_lookup);
Christof Schmitt8fdf30d2009-03-02 13:09:01 +0100667 INIT_WORK(&port->test_link_work, zfcp_fc_link_test_work);
Christof Schmitta2fa0ae2009-03-02 13:09:08 +0100668 INIT_WORK(&port->rport_work, zfcp_scsi_rport_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
670 port->adapter = adapter;
Swen Schillig317e6b62008-07-02 10:56:37 +0200671 port->d_id = d_id;
672 port->wwpn = wwpn;
Christof Schmitta2fa0ae2009-03-02 13:09:08 +0100673 port->rport_task = RPORT_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
Swen Schillig317e6b62008-07-02 10:56:37 +0200675 /* 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
Christof Schmitt0fac3f42009-08-18 15:43:30 +0200679 if (dev_set_name(&port->sysfs_device, "0x%016llx",
680 (unsigned long long)wwpn)) {
681 kfree(port);
682 return ERR_PTR(-ENOMEM);
683 }
Swen Schillig5ab944f2008-10-01 12:42:17 +0200684 port->sysfs_device.parent = &adapter->ccw_device->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 port->sysfs_device.release = zfcp_sysfs_port_release;
686 dev_set_drvdata(&port->sysfs_device, port);
687
Sebastian Ottf4395b62009-08-18 15:43:29 +0200688 if (device_register(&port->sysfs_device)) {
689 put_device(&port->sysfs_device);
Christof Schmitt0fac3f42009-08-18 15:43:30 +0200690 return ERR_PTR(-EINVAL);
Sebastian Ottf4395b62009-08-18 15:43:29 +0200691 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
Christof Schmitt0fac3f42009-08-18 15:43:30 +0200693 if (sysfs_create_group(&port->sysfs_device.kobj,
694 &zfcp_sysfs_port_attrs)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 device_unregister(&port->sysfs_device);
Christof Schmitt0fac3f42009-08-18 15:43:30 +0200696 return ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 }
698
699 zfcp_port_get(port);
700
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 write_lock_irq(&zfcp_data.config_lock);
Andreas Herrmann3859f6a2005-08-27 11:07:54 -0700702 list_add_tail(&port->list, &adapter->port_list_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 atomic_clear_mask(ZFCP_STATUS_COMMON_REMOVE, &port->status);
704 atomic_set_mask(ZFCP_STATUS_COMMON_RUNNING, &port->status);
Swen Schillig317e6b62008-07-02 10:56:37 +0200705
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 write_unlock_irq(&zfcp_data.config_lock);
707
708 zfcp_adapter_get(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 return port;
710}
711
Swen Schillig317e6b62008-07-02 10:56:37 +0200712/**
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)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 write_lock_irq(&zfcp_data.config_lock);
719 list_del(&port->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 write_unlock_irq(&zfcp_data.config_lock);
Swen Schilliga67417a2009-08-18 15:43:06 +0200721 wait_event(port->remove_wq, atomic_read(&port->refcount) == 0);
722 cancel_work_sync(&port->rport_work); /* usually not necessary */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 zfcp_adapter_put(port->adapter);
Swen Schillig5ab944f2008-10-01 12:42:17 +0200724 sysfs_remove_group(&port->sysfs_device.kobj, &zfcp_sysfs_port_attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 device_unregister(&port->sysfs_device);
726}
727
Swen Schillig317e6b62008-07-02 10:56:37 +0200728/**
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 */
Christof Schmitt45633fd2008-06-10 18:20:55 +0200734void 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
Swen Schillig317e6b62008-07-02 10:56:37 +0200745/**
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 */
Christof Schmitt45633fd2008-06-10 18:20:55 +0200753int 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}