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