aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/s390/scsi/zfcp_aux.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/s390/scsi/zfcp_aux.c')
-rw-r--r--drivers/s390/scsi/zfcp_aux.c1689
1 files changed, 326 insertions, 1363 deletions
diff --git a/drivers/s390/scsi/zfcp_aux.c b/drivers/s390/scsi/zfcp_aux.c
index 8c7e2b778ef1..90abfd06ed55 100644
--- a/drivers/s390/scsi/zfcp_aux.c
+++ b/drivers/s390/scsi/zfcp_aux.c
@@ -1,22 +1,9 @@
1/* 1/*
2 * This file is part of the zfcp device driver for 2 * zfcp device driver
3 * FCP adapters for IBM System z9 and zSeries.
4 * 3 *
5 * (C) Copyright IBM Corp. 2002, 2006 4 * Module interface and handling of zfcp data structures.
6 * 5 *
7 * This program is free software; you can redistribute it and/or modify 6 * Copyright IBM Corporation 2002, 2008
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2, or (at your option)
10 * any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */ 7 */
21 8
22/* 9/*
@@ -31,93 +18,25 @@
31 * Maxim Shchetynin 18 * Maxim Shchetynin
32 * Volker Sameske 19 * Volker Sameske
33 * Ralph Wuerthner 20 * Ralph Wuerthner
21 * Michael Loehr
22 * Swen Schillig
23 * Christof Schmitt
24 * Martin Petermann
25 * Sven Schuetz
34 */ 26 */
35 27
28#include <linux/miscdevice.h>
36#include "zfcp_ext.h" 29#include "zfcp_ext.h"
37 30
38/* accumulated log level (module parameter) */
39static u32 loglevel = ZFCP_LOG_LEVEL_DEFAULTS;
40static char *device; 31static char *device;
41/*********************** FUNCTION PROTOTYPES *********************************/
42
43/* written against the module interface */
44static int __init zfcp_module_init(void);
45
46/* FCP related */
47static void zfcp_ns_gid_pn_handler(unsigned long);
48
49/* miscellaneous */
50static int zfcp_sg_list_alloc(struct zfcp_sg_list *, size_t);
51static void zfcp_sg_list_free(struct zfcp_sg_list *);
52static int zfcp_sg_list_copy_from_user(struct zfcp_sg_list *,
53 void __user *, size_t);
54static int zfcp_sg_list_copy_to_user(void __user *,
55 struct zfcp_sg_list *, size_t);
56static long zfcp_cfdc_dev_ioctl(struct file *, unsigned int, unsigned long);
57
58#define ZFCP_CFDC_IOC_MAGIC 0xDD
59#define ZFCP_CFDC_IOC \
60 _IOWR(ZFCP_CFDC_IOC_MAGIC, 0, struct zfcp_cfdc_sense_data)
61
62
63static const struct file_operations zfcp_cfdc_fops = {
64 .unlocked_ioctl = zfcp_cfdc_dev_ioctl,
65#ifdef CONFIG_COMPAT
66 .compat_ioctl = zfcp_cfdc_dev_ioctl
67#endif
68};
69
70static struct miscdevice zfcp_cfdc_misc = {
71 .minor = ZFCP_CFDC_DEV_MINOR,
72 .name = ZFCP_CFDC_DEV_NAME,
73 .fops = &zfcp_cfdc_fops
74};
75
76/*********************** KERNEL/MODULE PARAMETERS ***************************/
77
78/* declare driver module init/cleanup functions */
79module_init(zfcp_module_init);
80 32
81MODULE_AUTHOR("IBM Deutschland Entwicklung GmbH - linux390@de.ibm.com"); 33MODULE_AUTHOR("IBM Deutschland Entwicklung GmbH - linux390@de.ibm.com");
82MODULE_DESCRIPTION 34MODULE_DESCRIPTION("FCP HBA driver");
83 ("FCP (SCSI over Fibre Channel) HBA driver for IBM System z9 and zSeries");
84MODULE_LICENSE("GPL"); 35MODULE_LICENSE("GPL");
85 36
86module_param(device, charp, 0400); 37module_param(device, charp, 0400);
87MODULE_PARM_DESC(device, "specify initial device"); 38MODULE_PARM_DESC(device, "specify initial device");
88 39
89module_param(loglevel, uint, 0400);
90MODULE_PARM_DESC(loglevel,
91 "log levels, 8 nibbles: "
92 "FC ERP QDIO CIO Config FSF SCSI Other, "
93 "levels: 0=none 1=normal 2=devel 3=trace");
94
95/****************************************************************/
96/************** Functions without logging ***********************/
97/****************************************************************/
98
99void
100_zfcp_hex_dump(char *addr, int count)
101{
102 int i;
103 for (i = 0; i < count; i++) {
104 printk("%02x", addr[i]);
105 if ((i % 4) == 3)
106 printk(" ");
107 if ((i % 32) == 31)
108 printk("\n");
109 }
110 if (((i-1) % 32) != 31)
111 printk("\n");
112}
113
114
115/****************************************************************/
116/****** Functions to handle the request ID hash table ********/
117/****************************************************************/
118
119#define ZFCP_LOG_AREA ZFCP_LOG_AREA_FSF
120
121static int zfcp_reqlist_alloc(struct zfcp_adapter *adapter) 40static int zfcp_reqlist_alloc(struct zfcp_adapter *adapter)
122{ 41{
123 int idx; 42 int idx;
@@ -132,11 +51,12 @@ static int zfcp_reqlist_alloc(struct zfcp_adapter *adapter)
132 return 0; 51 return 0;
133} 52}
134 53
135static void zfcp_reqlist_free(struct zfcp_adapter *adapter) 54/**
136{ 55 * zfcp_reqlist_isempty - is the request list empty
137 kfree(adapter->req_list); 56 * @adapter: pointer to struct zfcp_adapter
138} 57 *
139 58 * Returns: true if list is empty, false otherwise
59 */
140int zfcp_reqlist_isempty(struct zfcp_adapter *adapter) 60int zfcp_reqlist_isempty(struct zfcp_adapter *adapter)
141{ 61{
142 unsigned int idx; 62 unsigned int idx;
@@ -147,62 +67,58 @@ int zfcp_reqlist_isempty(struct zfcp_adapter *adapter)
147 return 1; 67 return 1;
148} 68}
149 69
150#undef ZFCP_LOG_AREA 70static int __init zfcp_device_setup(char *devstr)
151
152/****************************************************************/
153/************** Uncategorised Functions *************************/
154/****************************************************************/
155
156#define ZFCP_LOG_AREA ZFCP_LOG_AREA_OTHER
157
158/**
159 * zfcp_device_setup - setup function
160 * @str: pointer to parameter string
161 *
162 * Parse "device=..." parameter string.
163 */
164static int __init
165zfcp_device_setup(char *devstr)
166{ 71{
167 char *tmp, *str; 72 char *token;
168 size_t len; 73 char *str;
169 74
170 if (!devstr) 75 if (!devstr)
171 return 0; 76 return 0;
172 77
173 len = strlen(devstr) + 1; 78 /* duplicate devstr and keep the original for sysfs presentation*/
174 str = kmalloc(len, GFP_KERNEL); 79 str = kmalloc(strlen(devstr) + 1, GFP_KERNEL);
175 if (!str) 80 if (!str)
176 goto err_out; 81 return 0;
177 memcpy(str, devstr, len);
178 82
179 tmp = strchr(str, ','); 83 strcpy(str, devstr);
180 if (!tmp)
181 goto err_out;
182 *tmp++ = '\0';
183 strncpy(zfcp_data.init_busid, str, BUS_ID_SIZE);
184 zfcp_data.init_busid[BUS_ID_SIZE-1] = '\0';
185 84
186 zfcp_data.init_wwpn = simple_strtoull(tmp, &tmp, 0); 85 token = strsep(&str, ",");
187 if (*tmp++ != ',') 86 if (!token || strlen(token) >= BUS_ID_SIZE)
188 goto err_out; 87 goto err_out;
189 if (*tmp == '\0') 88 strncpy(zfcp_data.init_busid, token, BUS_ID_SIZE);
89
90 token = strsep(&str, ",");
91 if (!token || strict_strtoull(token, 0, &zfcp_data.init_wwpn))
190 goto err_out; 92 goto err_out;
191 93
192 zfcp_data.init_fcp_lun = simple_strtoull(tmp, &tmp, 0); 94 token = strsep(&str, ",");
193 if (*tmp != '\0') 95 if (!token || strict_strtoull(token, 0, &zfcp_data.init_fcp_lun))
194 goto err_out; 96 goto err_out;
97
195 kfree(str); 98 kfree(str);
196 return 1; 99 return 1;
197 100
198 err_out: 101 err_out:
199 ZFCP_LOG_NORMAL("Parse error for device parameter string %s\n", str);
200 kfree(str); 102 kfree(str);
103 pr_err("zfcp: Parse error for device parameter string %s, "
104 "device not attached.\n", devstr);
201 return 0; 105 return 0;
202} 106}
203 107
204static void __init 108static struct zfcp_adapter *zfcp_get_adapter_by_busid(char *bus_id)
205zfcp_init_device_configure(void) 109{
110 struct zfcp_adapter *adapter;
111
112 list_for_each_entry(adapter, &zfcp_data.adapter_list_head, list)
113 if ((strncmp(bus_id, adapter->ccw_device->dev.bus_id,
114 BUS_ID_SIZE) == 0) &&
115 !(atomic_read(&adapter->status) &
116 ZFCP_STATUS_COMMON_REMOVE))
117 return adapter;
118 return NULL;
119}
120
121static void __init zfcp_init_device_configure(void)
206{ 122{
207 struct zfcp_adapter *adapter; 123 struct zfcp_adapter *adapter;
208 struct zfcp_port *port; 124 struct zfcp_port *port;
@@ -215,101 +131,75 @@ zfcp_init_device_configure(void)
215 zfcp_adapter_get(adapter); 131 zfcp_adapter_get(adapter);
216 read_unlock_irq(&zfcp_data.config_lock); 132 read_unlock_irq(&zfcp_data.config_lock);
217 133
218 if (adapter == NULL) 134 if (!adapter)
219 goto out_adapter; 135 goto out_adapter;
220 port = zfcp_port_enqueue(adapter, zfcp_data.init_wwpn, 0, 0); 136 port = zfcp_port_enqueue(adapter, zfcp_data.init_wwpn, 0, 0);
221 if (!port) 137 if (IS_ERR(port))
222 goto out_port; 138 goto out_port;
223 unit = zfcp_unit_enqueue(port, zfcp_data.init_fcp_lun); 139 unit = zfcp_unit_enqueue(port, zfcp_data.init_fcp_lun);
224 if (!unit) 140 if (IS_ERR(unit))
225 goto out_unit; 141 goto out_unit;
226 up(&zfcp_data.config_sema); 142 up(&zfcp_data.config_sema);
227 ccw_device_set_online(adapter->ccw_device); 143 ccw_device_set_online(adapter->ccw_device);
228 zfcp_erp_wait(adapter); 144 zfcp_erp_wait(adapter);
229 down(&zfcp_data.config_sema); 145 down(&zfcp_data.config_sema);
230 zfcp_unit_put(unit); 146 zfcp_unit_put(unit);
231 out_unit: 147out_unit:
232 zfcp_port_put(port); 148 zfcp_port_put(port);
233 out_port: 149out_port:
234 zfcp_adapter_put(adapter); 150 zfcp_adapter_put(adapter);
235 out_adapter: 151out_adapter:
236 up(&zfcp_data.config_sema); 152 up(&zfcp_data.config_sema);
237 return; 153 return;
238} 154}
239 155
240static int calc_alignment(int size) 156static struct kmem_cache *zfcp_cache_create(int size, char *name)
241{ 157{
242 int align = 1; 158 int align = 1;
243
244 if (!size)
245 return 0;
246
247 while ((size - align) > 0) 159 while ((size - align) > 0)
248 align <<= 1; 160 align <<= 1;
249 161 return kmem_cache_create(name , size, align, 0, NULL);
250 return align;
251} 162}
252 163
253static int __init 164static int __init zfcp_module_init(void)
254zfcp_module_init(void)
255{ 165{
256 int retval = -ENOMEM; 166 int retval = -ENOMEM;
257 int size, align;
258 167
259 size = sizeof(struct zfcp_fsf_req_qtcb); 168 zfcp_data.fsf_req_qtcb_cache = zfcp_cache_create(
260 align = calc_alignment(size); 169 sizeof(struct zfcp_fsf_req_qtcb), "zfcp_fsf");
261 zfcp_data.fsf_req_qtcb_cache =
262 kmem_cache_create("zfcp_fsf", size, align, 0, NULL);
263 if (!zfcp_data.fsf_req_qtcb_cache) 170 if (!zfcp_data.fsf_req_qtcb_cache)
264 goto out; 171 goto out;
265 172
266 size = sizeof(struct fsf_status_read_buffer); 173 zfcp_data.sr_buffer_cache = zfcp_cache_create(
267 align = calc_alignment(size); 174 sizeof(struct fsf_status_read_buffer), "zfcp_sr");
268 zfcp_data.sr_buffer_cache =
269 kmem_cache_create("zfcp_sr", size, align, 0, NULL);
270 if (!zfcp_data.sr_buffer_cache) 175 if (!zfcp_data.sr_buffer_cache)
271 goto out_sr_cache; 176 goto out_sr_cache;
272 177
273 size = sizeof(struct zfcp_gid_pn_data); 178 zfcp_data.gid_pn_cache = zfcp_cache_create(
274 align = calc_alignment(size); 179 sizeof(struct zfcp_gid_pn_data), "zfcp_gid");
275 zfcp_data.gid_pn_cache =
276 kmem_cache_create("zfcp_gid", size, align, 0, NULL);
277 if (!zfcp_data.gid_pn_cache) 180 if (!zfcp_data.gid_pn_cache)
278 goto out_gid_cache; 181 goto out_gid_cache;
279 182
280 atomic_set(&zfcp_data.loglevel, loglevel);
281
282 /* initialize adapter list */
283 INIT_LIST_HEAD(&zfcp_data.adapter_list_head); 183 INIT_LIST_HEAD(&zfcp_data.adapter_list_head);
284
285 /* initialize adapters to be removed list head */
286 INIT_LIST_HEAD(&zfcp_data.adapter_remove_lh); 184 INIT_LIST_HEAD(&zfcp_data.adapter_remove_lh);
287 185
186 sema_init(&zfcp_data.config_sema, 1);
187 rwlock_init(&zfcp_data.config_lock);
188
288 zfcp_data.scsi_transport_template = 189 zfcp_data.scsi_transport_template =
289 fc_attach_transport(&zfcp_transport_functions); 190 fc_attach_transport(&zfcp_transport_functions);
290 if (!zfcp_data.scsi_transport_template) 191 if (!zfcp_data.scsi_transport_template)
291 goto out_transport; 192 goto out_transport;
292 193
293 retval = misc_register(&zfcp_cfdc_misc); 194 retval = misc_register(&zfcp_cfdc_misc);
294 if (retval != 0) { 195 if (retval) {
295 ZFCP_LOG_INFO("registration of misc device " 196 pr_err("zfcp: registration of misc device zfcp_cfdc failed\n");
296 "zfcp_cfdc failed\n");
297 goto out_misc; 197 goto out_misc;
298 } 198 }
299 199
300 ZFCP_LOG_TRACE("major/minor for zfcp_cfdc: %d/%d\n",
301 ZFCP_CFDC_DEV_MAJOR, zfcp_cfdc_misc.minor);
302
303 /* Initialise proc semaphores */
304 sema_init(&zfcp_data.config_sema, 1);
305
306 /* initialise configuration rw lock */
307 rwlock_init(&zfcp_data.config_lock);
308
309 /* setup dynamic I/O */
310 retval = zfcp_ccw_register(); 200 retval = zfcp_ccw_register();
311 if (retval) { 201 if (retval) {
312 ZFCP_LOG_NORMAL("registration with common I/O layer failed\n"); 202 pr_err("zfcp: Registration with common I/O layer failed.\n");
313 goto out_ccw_register; 203 goto out_ccw_register;
314 } 204 }
315 205
@@ -318,527 +208,88 @@ zfcp_module_init(void)
318 208
319 goto out; 209 goto out;
320 210
321 out_ccw_register: 211out_ccw_register:
322 misc_deregister(&zfcp_cfdc_misc); 212 misc_deregister(&zfcp_cfdc_misc);
323 out_misc: 213out_misc:
324 fc_release_transport(zfcp_data.scsi_transport_template); 214 fc_release_transport(zfcp_data.scsi_transport_template);
325 out_transport: 215out_transport:
326 kmem_cache_destroy(zfcp_data.gid_pn_cache); 216 kmem_cache_destroy(zfcp_data.gid_pn_cache);
327 out_gid_cache: 217out_gid_cache:
328 kmem_cache_destroy(zfcp_data.sr_buffer_cache); 218 kmem_cache_destroy(zfcp_data.sr_buffer_cache);
329 out_sr_cache: 219out_sr_cache:
330 kmem_cache_destroy(zfcp_data.fsf_req_qtcb_cache); 220 kmem_cache_destroy(zfcp_data.fsf_req_qtcb_cache);
331 out: 221out:
332 return retval; 222 return retval;
333} 223}
334 224
335/* 225module_init(zfcp_module_init);
336 * function: zfcp_cfdc_dev_ioctl
337 *
338 * purpose: Handle control file upload/download transaction via IOCTL
339 * interface
340 *
341 * returns: 0 - Operation completed successfuly
342 * -ENOTTY - Unknown IOCTL command
343 * -EINVAL - Invalid sense data record
344 * -ENXIO - The FCP adapter is not available
345 * -EOPNOTSUPP - The FCP adapter does not have CFDC support
346 * -ENOMEM - Insufficient memory
347 * -EFAULT - User space memory I/O operation fault
348 * -EPERM - Cannot create or queue FSF request or create SBALs
349 * -ERESTARTSYS- Received signal (is mapped to EAGAIN by VFS)
350 */
351static long
352zfcp_cfdc_dev_ioctl(struct file *file, unsigned int command,
353 unsigned long buffer)
354{
355 struct zfcp_cfdc_sense_data *sense_data, __user *sense_data_user;
356 struct zfcp_adapter *adapter = NULL;
357 struct zfcp_fsf_req *fsf_req = NULL;
358 struct zfcp_sg_list *sg_list = NULL;
359 u32 fsf_command, option;
360 char *bus_id = NULL;
361 int retval = 0;
362
363 sense_data = kmalloc(sizeof(struct zfcp_cfdc_sense_data), GFP_KERNEL);
364 if (sense_data == NULL) {
365 retval = -ENOMEM;
366 goto out;
367 }
368
369 sg_list = kzalloc(sizeof(struct zfcp_sg_list), GFP_KERNEL);
370 if (sg_list == NULL) {
371 retval = -ENOMEM;
372 goto out;
373 }
374
375 if (command != ZFCP_CFDC_IOC) {
376 ZFCP_LOG_INFO("IOC request code 0x%x invalid\n", command);
377 retval = -ENOTTY;
378 goto out;
379 }
380
381 if ((sense_data_user = (void __user *) buffer) == NULL) {
382 ZFCP_LOG_INFO("sense data record is required\n");
383 retval = -EINVAL;
384 goto out;
385 }
386
387 retval = copy_from_user(sense_data, sense_data_user,
388 sizeof(struct zfcp_cfdc_sense_data));
389 if (retval) {
390 retval = -EFAULT;
391 goto out;
392 }
393
394 if (sense_data->signature != ZFCP_CFDC_SIGNATURE) {
395 ZFCP_LOG_INFO("invalid sense data request signature 0x%08x\n",
396 ZFCP_CFDC_SIGNATURE);
397 retval = -EINVAL;
398 goto out;
399 }
400
401 switch (sense_data->command) {
402
403 case ZFCP_CFDC_CMND_DOWNLOAD_NORMAL:
404 fsf_command = FSF_QTCB_DOWNLOAD_CONTROL_FILE;
405 option = FSF_CFDC_OPTION_NORMAL_MODE;
406 break;
407
408 case ZFCP_CFDC_CMND_DOWNLOAD_FORCE:
409 fsf_command = FSF_QTCB_DOWNLOAD_CONTROL_FILE;
410 option = FSF_CFDC_OPTION_FORCE;
411 break;
412
413 case ZFCP_CFDC_CMND_FULL_ACCESS:
414 fsf_command = FSF_QTCB_DOWNLOAD_CONTROL_FILE;
415 option = FSF_CFDC_OPTION_FULL_ACCESS;
416 break;
417
418 case ZFCP_CFDC_CMND_RESTRICTED_ACCESS:
419 fsf_command = FSF_QTCB_DOWNLOAD_CONTROL_FILE;
420 option = FSF_CFDC_OPTION_RESTRICTED_ACCESS;
421 break;
422
423 case ZFCP_CFDC_CMND_UPLOAD:
424 fsf_command = FSF_QTCB_UPLOAD_CONTROL_FILE;
425 option = 0;
426 break;
427
428 default:
429 ZFCP_LOG_INFO("invalid command code 0x%08x\n",
430 sense_data->command);
431 retval = -EINVAL;
432 goto out;
433 }
434
435 bus_id = kmalloc(BUS_ID_SIZE, GFP_KERNEL);
436 if (bus_id == NULL) {
437 retval = -ENOMEM;
438 goto out;
439 }
440 snprintf(bus_id, BUS_ID_SIZE, "%d.%d.%04x",
441 (sense_data->devno >> 24),
442 (sense_data->devno >> 16) & 0xFF,
443 (sense_data->devno & 0xFFFF));
444
445 read_lock_irq(&zfcp_data.config_lock);
446 adapter = zfcp_get_adapter_by_busid(bus_id);
447 if (adapter)
448 zfcp_adapter_get(adapter);
449 read_unlock_irq(&zfcp_data.config_lock);
450
451 kfree(bus_id);
452
453 if (adapter == NULL) {
454 ZFCP_LOG_INFO("invalid adapter\n");
455 retval = -ENXIO;
456 goto out;
457 }
458
459 if (sense_data->command & ZFCP_CFDC_WITH_CONTROL_FILE) {
460 retval = zfcp_sg_list_alloc(sg_list,
461 ZFCP_CFDC_MAX_CONTROL_FILE_SIZE);
462 if (retval) {
463 retval = -ENOMEM;
464 goto out;
465 }
466 }
467
468 if ((sense_data->command & ZFCP_CFDC_DOWNLOAD) &&
469 (sense_data->command & ZFCP_CFDC_WITH_CONTROL_FILE)) {
470 retval = zfcp_sg_list_copy_from_user(
471 sg_list, &sense_data_user->control_file,
472 ZFCP_CFDC_MAX_CONTROL_FILE_SIZE);
473 if (retval) {
474 retval = -EFAULT;
475 goto out;
476 }
477 }
478
479 retval = zfcp_fsf_control_file(adapter, &fsf_req, fsf_command,
480 option, sg_list);
481 if (retval)
482 goto out;
483
484 if ((fsf_req->qtcb->prefix.prot_status != FSF_PROT_GOOD) &&
485 (fsf_req->qtcb->prefix.prot_status != FSF_PROT_FSF_STATUS_PRESENTED)) {
486 retval = -ENXIO;
487 goto out;
488 }
489
490 sense_data->fsf_status = fsf_req->qtcb->header.fsf_status;
491 memcpy(&sense_data->fsf_status_qual,
492 &fsf_req->qtcb->header.fsf_status_qual,
493 sizeof(union fsf_status_qual));
494 memcpy(&sense_data->payloads, &fsf_req->qtcb->bottom.support.els, 256);
495
496 retval = copy_to_user(sense_data_user, sense_data,
497 sizeof(struct zfcp_cfdc_sense_data));
498 if (retval) {
499 retval = -EFAULT;
500 goto out;
501 }
502
503 if (sense_data->command & ZFCP_CFDC_UPLOAD) {
504 retval = zfcp_sg_list_copy_to_user(
505 &sense_data_user->control_file, sg_list,
506 ZFCP_CFDC_MAX_CONTROL_FILE_SIZE);
507 if (retval) {
508 retval = -EFAULT;
509 goto out;
510 }
511 }
512
513 out:
514 if (fsf_req != NULL)
515 zfcp_fsf_req_free(fsf_req);
516
517 if ((adapter != NULL) && (retval != -ENXIO))
518 zfcp_adapter_put(adapter);
519
520 if (sg_list != NULL) {
521 zfcp_sg_list_free(sg_list);
522 kfree(sg_list);
523 }
524
525 kfree(sense_data);
526
527 return retval;
528}
529
530
531/**
532 * zfcp_sg_list_alloc - create a scatter-gather list of the specified size
533 * @sg_list: structure describing a scatter gather list
534 * @size: size of scatter-gather list
535 * Return: 0 on success, else -ENOMEM
536 *
537 * In sg_list->sg a pointer to the created scatter-gather list is returned,
538 * or NULL if we run out of memory. sg_list->count specifies the number of
539 * elements of the scatter-gather list. The maximum size of a single element
540 * in the scatter-gather list is PAGE_SIZE.
541 */
542static int
543zfcp_sg_list_alloc(struct zfcp_sg_list *sg_list, size_t size)
544{
545 struct scatterlist *sg;
546 unsigned int i;
547 int retval = 0;
548 void *address;
549
550 BUG_ON(sg_list == NULL);
551
552 sg_list->count = size >> PAGE_SHIFT;
553 if (size & ~PAGE_MASK)
554 sg_list->count++;
555 sg_list->sg = kcalloc(sg_list->count, sizeof(struct scatterlist),
556 GFP_KERNEL);
557 if (sg_list->sg == NULL) {
558 sg_list->count = 0;
559 retval = -ENOMEM;
560 goto out;
561 }
562 sg_init_table(sg_list->sg, sg_list->count);
563
564 for (i = 0, sg = sg_list->sg; i < sg_list->count; i++, sg++) {
565 address = (void *) get_zeroed_page(GFP_KERNEL);
566 if (address == NULL) {
567 sg_list->count = i;
568 zfcp_sg_list_free(sg_list);
569 retval = -ENOMEM;
570 goto out;
571 }
572 zfcp_address_to_sg(address, sg, min(size, PAGE_SIZE));
573 size -= sg->length;
574 }
575
576 out:
577 return retval;
578}
579
580
581/**
582 * zfcp_sg_list_free - free memory of a scatter-gather list
583 * @sg_list: structure describing a scatter-gather list
584 *
585 * Memory for each element in the scatter-gather list is freed.
586 * Finally sg_list->sg is freed itself and sg_list->count is reset.
587 */
588static void
589zfcp_sg_list_free(struct zfcp_sg_list *sg_list)
590{
591 struct scatterlist *sg;
592 unsigned int i;
593
594 BUG_ON(sg_list == NULL);
595
596 for (i = 0, sg = sg_list->sg; i < sg_list->count; i++, sg++)
597 free_page((unsigned long) zfcp_sg_to_address(sg));
598
599 sg_list->count = 0;
600 kfree(sg_list->sg);
601}
602
603/**
604 * zfcp_sg_size - determine size of a scatter-gather list
605 * @sg: array of (struct scatterlist)
606 * @sg_count: elements in array
607 * Return: size of entire scatter-gather list
608 */
609static size_t zfcp_sg_size(struct scatterlist *sg, unsigned int sg_count)
610{
611 unsigned int i;
612 struct scatterlist *p;
613 size_t size;
614
615 size = 0;
616 for (i = 0, p = sg; i < sg_count; i++, p++) {
617 BUG_ON(p == NULL);
618 size += p->length;
619 }
620
621 return size;
622}
623
624
625/**
626 * zfcp_sg_list_copy_from_user -copy data from user space to scatter-gather list
627 * @sg_list: structure describing a scatter-gather list
628 * @user_buffer: pointer to buffer in user space
629 * @size: number of bytes to be copied
630 * Return: 0 on success, -EFAULT if copy_from_user fails.
631 */
632static int
633zfcp_sg_list_copy_from_user(struct zfcp_sg_list *sg_list,
634 void __user *user_buffer,
635 size_t size)
636{
637 struct scatterlist *sg;
638 unsigned int length;
639 void *zfcp_buffer;
640 int retval = 0;
641
642 BUG_ON(sg_list == NULL);
643
644 if (zfcp_sg_size(sg_list->sg, sg_list->count) < size)
645 return -EFAULT;
646
647 for (sg = sg_list->sg; size > 0; sg++) {
648 length = min((unsigned int)size, sg->length);
649 zfcp_buffer = zfcp_sg_to_address(sg);
650 if (copy_from_user(zfcp_buffer, user_buffer, length)) {
651 retval = -EFAULT;
652 goto out;
653 }
654 user_buffer += length;
655 size -= length;
656 }
657
658 out:
659 return retval;
660}
661
662
663/**
664 * zfcp_sg_list_copy_to_user - copy data from scatter-gather list to user space
665 * @user_buffer: pointer to buffer in user space
666 * @sg_list: structure describing a scatter-gather list
667 * @size: number of bytes to be copied
668 * Return: 0 on success, -EFAULT if copy_to_user fails
669 */
670static int
671zfcp_sg_list_copy_to_user(void __user *user_buffer,
672 struct zfcp_sg_list *sg_list,
673 size_t size)
674{
675 struct scatterlist *sg;
676 unsigned int length;
677 void *zfcp_buffer;
678 int retval = 0;
679
680 BUG_ON(sg_list == NULL);
681
682 if (zfcp_sg_size(sg_list->sg, sg_list->count) < size)
683 return -EFAULT;
684
685 for (sg = sg_list->sg; size > 0; sg++) {
686 length = min((unsigned int) size, sg->length);
687 zfcp_buffer = zfcp_sg_to_address(sg);
688 if (copy_to_user(user_buffer, zfcp_buffer, length)) {
689 retval = -EFAULT;
690 goto out;
691 }
692 user_buffer += length;
693 size -= length;
694 }
695
696 out:
697 return retval;
698}
699
700
701#undef ZFCP_LOG_AREA
702
703/****************************************************************/
704/****** Functions for configuration/set-up of structures ********/
705/****************************************************************/
706
707#define ZFCP_LOG_AREA ZFCP_LOG_AREA_CONFIG
708 226
709/** 227/**
710 * zfcp_get_unit_by_lun - find unit in unit list of port by FCP LUN 228 * zfcp_get_unit_by_lun - find unit in unit list of port by FCP LUN
711 * @port: pointer to port to search for unit 229 * @port: pointer to port to search for unit
712 * @fcp_lun: FCP LUN to search for 230 * @fcp_lun: FCP LUN to search for
713 * Traverse list of all units of a port and return pointer to a unit 231 *
714 * with the given FCP LUN. 232 * Returns: pointer to zfcp_unit or NULL
715 */ 233 */
716struct zfcp_unit * 234struct zfcp_unit *zfcp_get_unit_by_lun(struct zfcp_port *port,
717zfcp_get_unit_by_lun(struct zfcp_port *port, fcp_lun_t fcp_lun) 235 fcp_lun_t fcp_lun)
718{ 236{
719 struct zfcp_unit *unit; 237 struct zfcp_unit *unit;
720 int found = 0;
721 238
722 list_for_each_entry(unit, &port->unit_list_head, list) { 239 list_for_each_entry(unit, &port->unit_list_head, list)
723 if ((unit->fcp_lun == fcp_lun) && 240 if ((unit->fcp_lun == fcp_lun) &&
724 !atomic_test_mask(ZFCP_STATUS_COMMON_REMOVE, &unit->status)) 241 !(atomic_read(&unit->status) & ZFCP_STATUS_COMMON_REMOVE))
725 { 242 return unit;
726 found = 1; 243 return NULL;
727 break;
728 }
729 }
730 return found ? unit : NULL;
731} 244}
732 245
733/** 246/**
734 * zfcp_get_port_by_wwpn - find port in port list of adapter by wwpn 247 * zfcp_get_port_by_wwpn - find port in port list of adapter by wwpn
735 * @adapter: pointer to adapter to search for port 248 * @adapter: pointer to adapter to search for port
736 * @wwpn: wwpn to search for 249 * @wwpn: wwpn to search for
737 * Traverse list of all ports of an adapter and return pointer to a port 250 *
738 * with the given wwpn. 251 * Returns: pointer to zfcp_port or NULL
739 */
740struct zfcp_port *
741zfcp_get_port_by_wwpn(struct zfcp_adapter *adapter, wwn_t wwpn)
742{
743 struct zfcp_port *port;
744 int found = 0;
745
746 list_for_each_entry(port, &adapter->port_list_head, list) {
747 if ((port->wwpn == wwpn) &&
748 !(atomic_read(&port->status) &
749 (ZFCP_STATUS_PORT_NO_WWPN | ZFCP_STATUS_COMMON_REMOVE))) {
750 found = 1;
751 break;
752 }
753 }
754 return found ? port : NULL;
755}
756
757/**
758 * zfcp_get_port_by_did - find port in port list of adapter by d_id
759 * @adapter: pointer to adapter to search for port
760 * @d_id: d_id to search for
761 * Traverse list of all ports of an adapter and return pointer to a port
762 * with the given d_id.
763 */ 252 */
764struct zfcp_port * 253struct zfcp_port *zfcp_get_port_by_wwpn(struct zfcp_adapter *adapter,
765zfcp_get_port_by_did(struct zfcp_adapter *adapter, u32 d_id) 254 wwn_t wwpn)
766{ 255{
767 struct zfcp_port *port; 256 struct zfcp_port *port;
768 int found = 0;
769 257
770 list_for_each_entry(port, &adapter->port_list_head, list) { 258 list_for_each_entry(port, &adapter->port_list_head, list)
771 if ((port->d_id == d_id) && 259 if ((port->wwpn == wwpn) && !(atomic_read(&port->status) &
772 !atomic_test_mask(ZFCP_STATUS_COMMON_REMOVE, &port->status)) 260 (ZFCP_STATUS_PORT_NO_WWPN | ZFCP_STATUS_COMMON_REMOVE)))
773 { 261 return port;
774 found = 1; 262 return NULL;
775 break;
776 }
777 }
778 return found ? port : NULL;
779} 263}
780 264
781/** 265static void zfcp_sysfs_unit_release(struct device *dev)
782 * zfcp_get_adapter_by_busid - find adpater in adapter list by bus_id
783 * @bus_id: bus_id to search for
784 * Traverse list of all adapters and return pointer to an adapter
785 * with the given bus_id.
786 */
787struct zfcp_adapter *
788zfcp_get_adapter_by_busid(char *bus_id)
789{ 266{
790 struct zfcp_adapter *adapter; 267 kfree(container_of(dev, struct zfcp_unit, sysfs_device));
791 int found = 0;
792
793 list_for_each_entry(adapter, &zfcp_data.adapter_list_head, list) {
794 if ((strncmp(bus_id, zfcp_get_busid_by_adapter(adapter),
795 BUS_ID_SIZE) == 0) &&
796 !atomic_test_mask(ZFCP_STATUS_COMMON_REMOVE,
797 &adapter->status)){
798 found = 1;
799 break;
800 }
801 }
802 return found ? adapter : NULL;
803} 268}
804 269
805/** 270/**
806 * zfcp_unit_enqueue - enqueue unit to unit list of a port. 271 * zfcp_unit_enqueue - enqueue unit to unit list of a port.
807 * @port: pointer to port where unit is added 272 * @port: pointer to port where unit is added
808 * @fcp_lun: FCP LUN of unit to be enqueued 273 * @fcp_lun: FCP LUN of unit to be enqueued
809 * Return: pointer to enqueued unit on success, NULL on error 274 * Returns: pointer to enqueued unit on success, ERR_PTR on error
810 * Locks: config_sema must be held to serialize changes to the unit list 275 * Locks: config_sema must be held to serialize changes to the unit list
811 * 276 *
812 * Sets up some unit internal structures and creates sysfs entry. 277 * Sets up some unit internal structures and creates sysfs entry.
813 */ 278 */
814struct zfcp_unit * 279struct zfcp_unit *zfcp_unit_enqueue(struct zfcp_port *port, fcp_lun_t fcp_lun)
815zfcp_unit_enqueue(struct zfcp_port *port, fcp_lun_t fcp_lun)
816{ 280{
817 struct zfcp_unit *unit; 281 struct zfcp_unit *unit;
818 282
819 /* 283 unit = kzalloc(sizeof(struct zfcp_unit), GFP_KERNEL);
820 * check that there is no unit with this FCP_LUN already in list
821 * and enqueue it.
822 * Note: Unlike for the adapter and the port, this is an error
823 */
824 read_lock_irq(&zfcp_data.config_lock);
825 unit = zfcp_get_unit_by_lun(port, fcp_lun);
826 read_unlock_irq(&zfcp_data.config_lock);
827 if (unit)
828 return NULL;
829
830 unit = kzalloc(sizeof (struct zfcp_unit), GFP_KERNEL);
831 if (!unit) 284 if (!unit)
832 return NULL; 285 return ERR_PTR(-ENOMEM);
833 286
834 /* initialise reference count stuff */
835 atomic_set(&unit->refcount, 0); 287 atomic_set(&unit->refcount, 0);
836 init_waitqueue_head(&unit->remove_wq); 288 init_waitqueue_head(&unit->remove_wq);
837 289
838 unit->port = port; 290 unit->port = port;
839 unit->fcp_lun = fcp_lun; 291 unit->fcp_lun = fcp_lun;
840 292
841 /* setup for sysfs registration */
842 snprintf(unit->sysfs_device.bus_id, BUS_ID_SIZE, "0x%016llx", fcp_lun); 293 snprintf(unit->sysfs_device.bus_id, BUS_ID_SIZE, "0x%016llx", fcp_lun);
843 unit->sysfs_device.parent = &port->sysfs_device; 294 unit->sysfs_device.parent = &port->sysfs_device;
844 unit->sysfs_device.release = zfcp_sysfs_unit_release; 295 unit->sysfs_device.release = zfcp_sysfs_unit_release;
@@ -847,14 +298,28 @@ zfcp_unit_enqueue(struct zfcp_port *port, fcp_lun_t fcp_lun)
847 /* mark unit unusable as long as sysfs registration is not complete */ 298 /* mark unit unusable as long as sysfs registration is not complete */
848 atomic_set_mask(ZFCP_STATUS_COMMON_REMOVE, &unit->status); 299 atomic_set_mask(ZFCP_STATUS_COMMON_REMOVE, &unit->status);
849 300
850 if (device_register(&unit->sysfs_device)) { 301 spin_lock_init(&unit->latencies.lock);
851 kfree(unit); 302 unit->latencies.write.channel.min = 0xFFFFFFFF;
852 return NULL; 303 unit->latencies.write.fabric.min = 0xFFFFFFFF;
304 unit->latencies.read.channel.min = 0xFFFFFFFF;
305 unit->latencies.read.fabric.min = 0xFFFFFFFF;
306 unit->latencies.cmd.channel.min = 0xFFFFFFFF;
307 unit->latencies.cmd.fabric.min = 0xFFFFFFFF;
308
309 read_lock_irq(&zfcp_data.config_lock);
310 if (zfcp_get_unit_by_lun(port, fcp_lun)) {
311 read_unlock_irq(&zfcp_data.config_lock);
312 goto err_out_free;
853 } 313 }
314 read_unlock_irq(&zfcp_data.config_lock);
854 315
855 if (zfcp_sysfs_unit_create_files(&unit->sysfs_device)) { 316 if (device_register(&unit->sysfs_device))
317 goto err_out_free;
318
319 if (sysfs_create_group(&unit->sysfs_device.kobj,
320 &zfcp_sysfs_unit_attrs)) {
856 device_unregister(&unit->sysfs_device); 321 device_unregister(&unit->sysfs_device);
857 return NULL; 322 return ERR_PTR(-EIO);
858 } 323 }
859 324
860 zfcp_unit_get(unit); 325 zfcp_unit_get(unit);
@@ -864,16 +329,27 @@ zfcp_unit_enqueue(struct zfcp_port *port, fcp_lun_t fcp_lun)
864 list_add_tail(&unit->list, &port->unit_list_head); 329 list_add_tail(&unit->list, &port->unit_list_head);
865 atomic_clear_mask(ZFCP_STATUS_COMMON_REMOVE, &unit->status); 330 atomic_clear_mask(ZFCP_STATUS_COMMON_REMOVE, &unit->status);
866 atomic_set_mask(ZFCP_STATUS_COMMON_RUNNING, &unit->status); 331 atomic_set_mask(ZFCP_STATUS_COMMON_RUNNING, &unit->status);
332
867 write_unlock_irq(&zfcp_data.config_lock); 333 write_unlock_irq(&zfcp_data.config_lock);
868 334
869 port->units++; 335 port->units++;
870 zfcp_port_get(port); 336 zfcp_port_get(port);
871 337
872 return unit; 338 return unit;
339
340err_out_free:
341 kfree(unit);
342 return ERR_PTR(-EINVAL);
873} 343}
874 344
875void 345/**
876zfcp_unit_dequeue(struct zfcp_unit *unit) 346 * zfcp_unit_dequeue - dequeue unit
347 * @unit: pointer to zfcp_unit
348 *
349 * waits until all work is done on unit and removes it then from the unit->list
350 * of the associated port.
351 */
352void zfcp_unit_dequeue(struct zfcp_unit *unit)
877{ 353{
878 zfcp_unit_wait(unit); 354 zfcp_unit_wait(unit);
879 write_lock_irq(&zfcp_data.config_lock); 355 write_lock_irq(&zfcp_data.config_lock);
@@ -881,68 +357,51 @@ zfcp_unit_dequeue(struct zfcp_unit *unit)
881 write_unlock_irq(&zfcp_data.config_lock); 357 write_unlock_irq(&zfcp_data.config_lock);
882 unit->port->units--; 358 unit->port->units--;
883 zfcp_port_put(unit->port); 359 zfcp_port_put(unit->port);
884 zfcp_sysfs_unit_remove_files(&unit->sysfs_device); 360 sysfs_remove_group(&unit->sysfs_device.kobj, &zfcp_sysfs_unit_attrs);
885 device_unregister(&unit->sysfs_device); 361 device_unregister(&unit->sysfs_device);
886} 362}
887 363
888/* 364static int zfcp_allocate_low_mem_buffers(struct zfcp_adapter *adapter)
889 * Allocates a combined QTCB/fsf_req buffer for erp actions and fcp/SCSI
890 * commands.
891 * It also genrates fcp-nameserver request/response buffer and unsolicited
892 * status read fsf_req buffers.
893 *
894 * locks: must only be called with zfcp_data.config_sema taken
895 */
896static int
897zfcp_allocate_low_mem_buffers(struct zfcp_adapter *adapter)
898{ 365{
366 /* must only be called with zfcp_data.config_sema taken */
899 adapter->pool.fsf_req_erp = 367 adapter->pool.fsf_req_erp =
900 mempool_create_slab_pool(ZFCP_POOL_FSF_REQ_ERP_NR, 368 mempool_create_slab_pool(1, zfcp_data.fsf_req_qtcb_cache);
901 zfcp_data.fsf_req_qtcb_cache);
902 if (!adapter->pool.fsf_req_erp) 369 if (!adapter->pool.fsf_req_erp)
903 return -ENOMEM; 370 return -ENOMEM;
904 371
905 adapter->pool.fsf_req_scsi = 372 adapter->pool.fsf_req_scsi =
906 mempool_create_slab_pool(ZFCP_POOL_FSF_REQ_SCSI_NR, 373 mempool_create_slab_pool(1, zfcp_data.fsf_req_qtcb_cache);
907 zfcp_data.fsf_req_qtcb_cache);
908 if (!adapter->pool.fsf_req_scsi) 374 if (!adapter->pool.fsf_req_scsi)
909 return -ENOMEM; 375 return -ENOMEM;
910 376
911 adapter->pool.fsf_req_abort = 377 adapter->pool.fsf_req_abort =
912 mempool_create_slab_pool(ZFCP_POOL_FSF_REQ_ABORT_NR, 378 mempool_create_slab_pool(1, zfcp_data.fsf_req_qtcb_cache);
913 zfcp_data.fsf_req_qtcb_cache);
914 if (!adapter->pool.fsf_req_abort) 379 if (!adapter->pool.fsf_req_abort)
915 return -ENOMEM; 380 return -ENOMEM;
916 381
917 adapter->pool.fsf_req_status_read = 382 adapter->pool.fsf_req_status_read =
918 mempool_create_kmalloc_pool(ZFCP_POOL_STATUS_READ_NR, 383 mempool_create_kmalloc_pool(FSF_STATUS_READS_RECOM,
919 sizeof(struct zfcp_fsf_req)); 384 sizeof(struct zfcp_fsf_req));
920 if (!adapter->pool.fsf_req_status_read) 385 if (!adapter->pool.fsf_req_status_read)
921 return -ENOMEM; 386 return -ENOMEM;
922 387
923 adapter->pool.data_status_read = 388 adapter->pool.data_status_read =
924 mempool_create_slab_pool(ZFCP_POOL_STATUS_READ_NR, 389 mempool_create_slab_pool(FSF_STATUS_READS_RECOM,
925 zfcp_data.sr_buffer_cache); 390 zfcp_data.sr_buffer_cache);
926 if (!adapter->pool.data_status_read) 391 if (!adapter->pool.data_status_read)
927 return -ENOMEM; 392 return -ENOMEM;
928 393
929 adapter->pool.data_gid_pn = 394 adapter->pool.data_gid_pn =
930 mempool_create_slab_pool(ZFCP_POOL_DATA_GID_PN_NR, 395 mempool_create_slab_pool(1, zfcp_data.gid_pn_cache);
931 zfcp_data.gid_pn_cache);
932 if (!adapter->pool.data_gid_pn) 396 if (!adapter->pool.data_gid_pn)
933 return -ENOMEM; 397 return -ENOMEM;
934 398
935 return 0; 399 return 0;
936} 400}
937 401
938/** 402static void zfcp_free_low_mem_buffers(struct zfcp_adapter *adapter)
939 * zfcp_free_low_mem_buffers - free memory pools of an adapter
940 * @adapter: pointer to zfcp_adapter for which memory pools should be freed
941 * locking: zfcp_data.config_sema must be held
942 */
943static void
944zfcp_free_low_mem_buffers(struct zfcp_adapter *adapter)
945{ 403{
404 /* zfcp_data.config_sema must be held */
946 if (adapter->pool.fsf_req_erp) 405 if (adapter->pool.fsf_req_erp)
947 mempool_destroy(adapter->pool.fsf_req_erp); 406 mempool_destroy(adapter->pool.fsf_req_erp);
948 if (adapter->pool.fsf_req_scsi) 407 if (adapter->pool.fsf_req_scsi)
@@ -962,20 +421,61 @@ static void zfcp_dummy_release(struct device *dev)
962 return; 421 return;
963} 422}
964 423
965/* 424/**
425 * zfcp_status_read_refill - refill the long running status_read_requests
426 * @adapter: ptr to struct zfcp_adapter for which the buffers should be refilled
427 *
428 * Returns: 0 on success, 1 otherwise
429 *
430 * if there are 16 or more status_read requests missing an adapter_reopen
431 * is triggered
432 */
433int zfcp_status_read_refill(struct zfcp_adapter *adapter)
434{
435 while (atomic_read(&adapter->stat_miss) > 0)
436 if (zfcp_fsf_status_read(adapter)) {
437 if (atomic_read(&adapter->stat_miss) >= 16) {
438 zfcp_erp_adapter_reopen(adapter, 0, 103, NULL);
439 return 1;
440 }
441 break;
442 } else
443 atomic_dec(&adapter->stat_miss);
444 return 0;
445}
446
447static void _zfcp_status_read_scheduler(struct work_struct *work)
448{
449 zfcp_status_read_refill(container_of(work, struct zfcp_adapter,
450 stat_work));
451}
452
453static int zfcp_nameserver_enqueue(struct zfcp_adapter *adapter)
454{
455 struct zfcp_port *port;
456
457 port = zfcp_port_enqueue(adapter, 0, ZFCP_STATUS_PORT_WKA,
458 ZFCP_DID_DIRECTORY_SERVICE);
459 if (IS_ERR(port))
460 return PTR_ERR(port);
461 zfcp_port_put(port);
462
463 return 0;
464}
465
466/**
467 * zfcp_adapter_enqueue - enqueue a new adapter to the list
468 * @ccw_device: pointer to the struct cc_device
469 *
470 * Returns: 0 if a new adapter was successfully enqueued
471 * -ENOMEM if alloc failed
966 * Enqueues an adapter at the end of the adapter list in the driver data. 472 * Enqueues an adapter at the end of the adapter list in the driver data.
967 * All adapter internal structures are set up. 473 * All adapter internal structures are set up.
968 * Proc-fs entries are also created. 474 * Proc-fs entries are also created.
969 *
970 * returns: 0 if a new adapter was successfully enqueued
971 * ZFCP_KNOWN if an adapter with this devno was already present
972 * -ENOMEM if alloc failed
973 * locks: config_sema must be held to serialise changes to the adapter list 475 * locks: config_sema must be held to serialise changes to the adapter list
974 */ 476 */
975struct zfcp_adapter * 477int zfcp_adapter_enqueue(struct ccw_device *ccw_device)
976zfcp_adapter_enqueue(struct ccw_device *ccw_device)
977{ 478{
978 int retval = 0;
979 struct zfcp_adapter *adapter; 479 struct zfcp_adapter *adapter;
980 480
981 /* 481 /*
@@ -983,85 +483,58 @@ zfcp_adapter_enqueue(struct ccw_device *ccw_device)
983 * are protected by the config_sema, which must be held to get here 483 * are protected by the config_sema, which must be held to get here
984 */ 484 */
985 485
986 /* try to allocate new adapter data structure (zeroed) */ 486 adapter = kzalloc(sizeof(struct zfcp_adapter), GFP_KERNEL);
987 adapter = kzalloc(sizeof (struct zfcp_adapter), GFP_KERNEL); 487 if (!adapter)
988 if (!adapter) { 488 return -ENOMEM;
989 ZFCP_LOG_INFO("error: allocation of base adapter "
990 "structure failed\n");
991 goto out;
992 }
993 489
994 ccw_device->handler = NULL; 490 ccw_device->handler = NULL;
995
996 /* save ccw_device pointer */
997 adapter->ccw_device = ccw_device; 491 adapter->ccw_device = ccw_device;
492 atomic_set(&adapter->refcount, 0);
998 493
999 retval = zfcp_qdio_allocate_queues(adapter); 494 if (zfcp_qdio_allocate(adapter))
1000 if (retval)
1001 goto queues_alloc_failed;
1002
1003 retval = zfcp_qdio_allocate(adapter);
1004 if (retval)
1005 goto qdio_allocate_failed; 495 goto qdio_allocate_failed;
1006 496
1007 retval = zfcp_allocate_low_mem_buffers(adapter); 497 if (zfcp_allocate_low_mem_buffers(adapter))
1008 if (retval) {
1009 ZFCP_LOG_INFO("error: pool allocation failed\n");
1010 goto failed_low_mem_buffers; 498 goto failed_low_mem_buffers;
1011 }
1012 499
1013 /* initialise reference count stuff */ 500 if (zfcp_reqlist_alloc(adapter))
1014 atomic_set(&adapter->refcount, 0); 501 goto failed_low_mem_buffers;
502
503 if (zfcp_adapter_debug_register(adapter))
504 goto debug_register_failed;
505
1015 init_waitqueue_head(&adapter->remove_wq); 506 init_waitqueue_head(&adapter->remove_wq);
507 init_waitqueue_head(&adapter->erp_thread_wqh);
508 init_waitqueue_head(&adapter->erp_done_wqh);
1016 509
1017 /* initialise list of ports */
1018 INIT_LIST_HEAD(&adapter->port_list_head); 510 INIT_LIST_HEAD(&adapter->port_list_head);
1019
1020 /* initialise list of ports to be removed */
1021 INIT_LIST_HEAD(&adapter->port_remove_lh); 511 INIT_LIST_HEAD(&adapter->port_remove_lh);
512 INIT_LIST_HEAD(&adapter->erp_ready_head);
513 INIT_LIST_HEAD(&adapter->erp_running_head);
1022 514
1023 /* initialize list of fsf requests */
1024 spin_lock_init(&adapter->req_list_lock); 515 spin_lock_init(&adapter->req_list_lock);
1025 retval = zfcp_reqlist_alloc(adapter);
1026 if (retval) {
1027 ZFCP_LOG_INFO("request list initialization failed\n");
1028 goto failed_low_mem_buffers;
1029 }
1030
1031 /* initialize debug locks */
1032 516
1033 spin_lock_init(&adapter->hba_dbf_lock); 517 spin_lock_init(&adapter->hba_dbf_lock);
1034 spin_lock_init(&adapter->san_dbf_lock); 518 spin_lock_init(&adapter->san_dbf_lock);
1035 spin_lock_init(&adapter->scsi_dbf_lock); 519 spin_lock_init(&adapter->scsi_dbf_lock);
1036 spin_lock_init(&adapter->rec_dbf_lock); 520 spin_lock_init(&adapter->rec_dbf_lock);
1037 521 spin_lock_init(&adapter->req_q.lock);
1038 retval = zfcp_adapter_debug_register(adapter);
1039 if (retval)
1040 goto debug_register_failed;
1041
1042 /* initialize error recovery stuff */
1043 522
1044 rwlock_init(&adapter->erp_lock); 523 rwlock_init(&adapter->erp_lock);
1045 sema_init(&adapter->erp_ready_sem, 0);
1046 INIT_LIST_HEAD(&adapter->erp_ready_head);
1047 INIT_LIST_HEAD(&adapter->erp_running_head);
1048
1049 /* initialize abort lock */
1050 rwlock_init(&adapter->abort_lock); 524 rwlock_init(&adapter->abort_lock);
1051 525
1052 /* initialise some erp stuff */ 526 sema_init(&adapter->erp_ready_sem, 0);
1053 init_waitqueue_head(&adapter->erp_thread_wqh);
1054 init_waitqueue_head(&adapter->erp_done_wqh);
1055 527
1056 /* initialize lock of associated request queue */ 528 INIT_WORK(&adapter->stat_work, _zfcp_status_read_scheduler);
1057 rwlock_init(&adapter->request_queue.queue_lock); 529 INIT_WORK(&adapter->scan_work, _zfcp_scan_ports_later);
1058 530
1059 /* mark adapter unusable as long as sysfs registration is not complete */ 531 /* mark adapter unusable as long as sysfs registration is not complete */
1060 atomic_set_mask(ZFCP_STATUS_COMMON_REMOVE, &adapter->status); 532 atomic_set_mask(ZFCP_STATUS_COMMON_REMOVE, &adapter->status);
1061 533
1062 dev_set_drvdata(&ccw_device->dev, adapter); 534 dev_set_drvdata(&ccw_device->dev, adapter);
1063 535
1064 if (zfcp_sysfs_adapter_create_files(&ccw_device->dev)) 536 if (sysfs_create_group(&ccw_device->dev.kobj,
537 &zfcp_sysfs_adapter_attrs))
1065 goto sysfs_failed; 538 goto sysfs_failed;
1066 539
1067 adapter->generic_services.parent = &adapter->ccw_device->dev; 540 adapter->generic_services.parent = &adapter->ccw_device->dev;
@@ -1072,7 +545,6 @@ zfcp_adapter_enqueue(struct ccw_device *ccw_device)
1072 if (device_register(&adapter->generic_services)) 545 if (device_register(&adapter->generic_services))
1073 goto generic_services_failed; 546 goto generic_services_failed;
1074 547
1075 /* put allocated adapter at list tail */
1076 write_lock_irq(&zfcp_data.config_lock); 548 write_lock_irq(&zfcp_data.config_lock);
1077 atomic_clear_mask(ZFCP_STATUS_COMMON_REMOVE, &adapter->status); 549 atomic_clear_mask(ZFCP_STATUS_COMMON_REMOVE, &adapter->status);
1078 list_add_tail(&adapter->list, &zfcp_data.adapter_list_head); 550 list_add_tail(&adapter->list, &zfcp_data.adapter_list_head);
@@ -1080,57 +552,49 @@ zfcp_adapter_enqueue(struct ccw_device *ccw_device)
1080 552
1081 zfcp_data.adapters++; 553 zfcp_data.adapters++;
1082 554
1083 goto out; 555 zfcp_nameserver_enqueue(adapter);
556
557 return 0;
1084 558
1085 generic_services_failed: 559generic_services_failed:
1086 zfcp_sysfs_adapter_remove_files(&adapter->ccw_device->dev); 560 sysfs_remove_group(&ccw_device->dev.kobj,
1087 sysfs_failed: 561 &zfcp_sysfs_adapter_attrs);
562sysfs_failed:
1088 zfcp_adapter_debug_unregister(adapter); 563 zfcp_adapter_debug_unregister(adapter);
1089 debug_register_failed: 564debug_register_failed:
1090 dev_set_drvdata(&ccw_device->dev, NULL); 565 dev_set_drvdata(&ccw_device->dev, NULL);
1091 zfcp_reqlist_free(adapter); 566 kfree(adapter->req_list);
1092 failed_low_mem_buffers: 567failed_low_mem_buffers:
1093 zfcp_free_low_mem_buffers(adapter); 568 zfcp_free_low_mem_buffers(adapter);
1094 if (qdio_free(ccw_device) != 0) 569qdio_allocate_failed:
1095 ZFCP_LOG_NORMAL("bug: qdio_free for adapter %s failed\n", 570 zfcp_qdio_free(adapter);
1096 zfcp_get_busid_by_adapter(adapter));
1097 qdio_allocate_failed:
1098 zfcp_qdio_free_queues(adapter);
1099 queues_alloc_failed:
1100 kfree(adapter); 571 kfree(adapter);
1101 adapter = NULL; 572 return -ENOMEM;
1102 out:
1103 return adapter;
1104} 573}
1105 574
1106/* 575/**
1107 * returns: 0 - struct zfcp_adapter data structure successfully removed 576 * zfcp_adapter_dequeue - remove the adapter from the resource list
1108 * !0 - struct zfcp_adapter data structure could not be removed 577 * @adapter: pointer to struct zfcp_adapter which should be removed
1109 * (e.g. still used)
1110 * locks: adapter list write lock is assumed to be held by caller 578 * locks: adapter list write lock is assumed to be held by caller
1111 */ 579 */
1112void 580void zfcp_adapter_dequeue(struct zfcp_adapter *adapter)
1113zfcp_adapter_dequeue(struct zfcp_adapter *adapter)
1114{ 581{
1115 int retval = 0; 582 int retval = 0;
1116 unsigned long flags; 583 unsigned long flags;
1117 584
585 cancel_work_sync(&adapter->scan_work);
586 cancel_work_sync(&adapter->stat_work);
1118 zfcp_adapter_scsi_unregister(adapter); 587 zfcp_adapter_scsi_unregister(adapter);
1119 device_unregister(&adapter->generic_services); 588 device_unregister(&adapter->generic_services);
1120 zfcp_sysfs_adapter_remove_files(&adapter->ccw_device->dev); 589 sysfs_remove_group(&adapter->ccw_device->dev.kobj,
590 &zfcp_sysfs_adapter_attrs);
1121 dev_set_drvdata(&adapter->ccw_device->dev, NULL); 591 dev_set_drvdata(&adapter->ccw_device->dev, NULL);
1122 /* sanity check: no pending FSF requests */ 592 /* sanity check: no pending FSF requests */
1123 spin_lock_irqsave(&adapter->req_list_lock, flags); 593 spin_lock_irqsave(&adapter->req_list_lock, flags);
1124 retval = zfcp_reqlist_isempty(adapter); 594 retval = zfcp_reqlist_isempty(adapter);
1125 spin_unlock_irqrestore(&adapter->req_list_lock, flags); 595 spin_unlock_irqrestore(&adapter->req_list_lock, flags);
1126 if (!retval) { 596 if (!retval)
1127 ZFCP_LOG_NORMAL("bug: adapter %s (%p) still in use, " 597 return;
1128 "%i requests outstanding\n",
1129 zfcp_get_busid_by_adapter(adapter), adapter,
1130 atomic_read(&adapter->reqs_active));
1131 retval = -EBUSY;
1132 goto out;
1133 }
1134 598
1135 zfcp_adapter_debug_unregister(adapter); 599 zfcp_adapter_debug_unregister(adapter);
1136 600
@@ -1142,26 +606,18 @@ zfcp_adapter_dequeue(struct zfcp_adapter *adapter)
1142 /* decrease number of adapters in list */ 606 /* decrease number of adapters in list */
1143 zfcp_data.adapters--; 607 zfcp_data.adapters--;
1144 608
1145 ZFCP_LOG_TRACE("adapter %s (%p) removed from list, " 609 zfcp_qdio_free(adapter);
1146 "%i adapters still in list\n",
1147 zfcp_get_busid_by_adapter(adapter),
1148 adapter, zfcp_data.adapters);
1149
1150 retval = qdio_free(adapter->ccw_device);
1151 if (retval)
1152 ZFCP_LOG_NORMAL("bug: qdio_free for adapter %s failed\n",
1153 zfcp_get_busid_by_adapter(adapter));
1154 610
1155 zfcp_free_low_mem_buffers(adapter); 611 zfcp_free_low_mem_buffers(adapter);
1156 /* free memory of adapter data structure and queues */ 612 kfree(adapter->req_list);
1157 zfcp_qdio_free_queues(adapter);
1158 zfcp_reqlist_free(adapter);
1159 kfree(adapter->fc_stats); 613 kfree(adapter->fc_stats);
1160 kfree(adapter->stats_reset_data); 614 kfree(adapter->stats_reset_data);
1161 ZFCP_LOG_TRACE("freeing adapter structure\n");
1162 kfree(adapter); 615 kfree(adapter);
1163 out: 616}
1164 return; 617
618static void zfcp_sysfs_port_release(struct device *dev)
619{
620 kfree(container_of(dev, struct zfcp_port, sysfs_device));
1165} 621}
1166 622
1167/** 623/**
@@ -1170,98 +626,90 @@ zfcp_adapter_dequeue(struct zfcp_adapter *adapter)
1170 * @wwpn: WWPN of the remote port to be enqueued 626 * @wwpn: WWPN of the remote port to be enqueued
1171 * @status: initial status for the port 627 * @status: initial status for the port
1172 * @d_id: destination id of the remote port to be enqueued 628 * @d_id: destination id of the remote port to be enqueued
1173 * Return: pointer to enqueued port on success, NULL on error 629 * Returns: pointer to enqueued port on success, ERR_PTR on error
1174 * Locks: config_sema must be held to serialize changes to the port list 630 * Locks: config_sema must be held to serialize changes to the port list
1175 * 631 *
1176 * All port internal structures are set up and the sysfs entry is generated. 632 * All port internal structures are set up and the sysfs entry is generated.
1177 * d_id is used to enqueue ports with a well known address like the Directory 633 * d_id is used to enqueue ports with a well known address like the Directory
1178 * Service for nameserver lookup. 634 * Service for nameserver lookup.
1179 */ 635 */
1180struct zfcp_port * 636struct zfcp_port *zfcp_port_enqueue(struct zfcp_adapter *adapter, wwn_t wwpn,
1181zfcp_port_enqueue(struct zfcp_adapter *adapter, wwn_t wwpn, u32 status, 637 u32 status, u32 d_id)
1182 u32 d_id)
1183{ 638{
1184 struct zfcp_port *port; 639 struct zfcp_port *port;
1185 int check_wwpn; 640 int retval;
1186 641 char *bus_id;
1187 check_wwpn = !(status & ZFCP_STATUS_PORT_NO_WWPN);
1188 /*
1189 * check that there is no port with this WWPN already in list
1190 */
1191 if (check_wwpn) {
1192 read_lock_irq(&zfcp_data.config_lock);
1193 port = zfcp_get_port_by_wwpn(adapter, wwpn);
1194 read_unlock_irq(&zfcp_data.config_lock);
1195 if (port)
1196 return NULL;
1197 }
1198 642
1199 port = kzalloc(sizeof (struct zfcp_port), GFP_KERNEL); 643 port = kzalloc(sizeof(struct zfcp_port), GFP_KERNEL);
1200 if (!port) 644 if (!port)
1201 return NULL; 645 return ERR_PTR(-ENOMEM);
1202 646
1203 /* initialise reference count stuff */
1204 atomic_set(&port->refcount, 0);
1205 init_waitqueue_head(&port->remove_wq); 647 init_waitqueue_head(&port->remove_wq);
1206 648
1207 INIT_LIST_HEAD(&port->unit_list_head); 649 INIT_LIST_HEAD(&port->unit_list_head);
1208 INIT_LIST_HEAD(&port->unit_remove_lh); 650 INIT_LIST_HEAD(&port->unit_remove_lh);
1209 651
1210 port->adapter = adapter; 652 port->adapter = adapter;
653 port->d_id = d_id;
654 port->wwpn = wwpn;
1211 655
1212 if (check_wwpn) 656 /* mark port unusable as long as sysfs registration is not complete */
1213 port->wwpn = wwpn; 657 atomic_set_mask(status | ZFCP_STATUS_COMMON_REMOVE, &port->status);
1214 658 atomic_set(&port->refcount, 0);
1215 atomic_set_mask(status, &port->status);
1216 659
1217 /* setup for sysfs registration */
1218 if (status & ZFCP_STATUS_PORT_WKA) { 660 if (status & ZFCP_STATUS_PORT_WKA) {
1219 switch (d_id) { 661 switch (d_id) {
1220 case ZFCP_DID_DIRECTORY_SERVICE: 662 case ZFCP_DID_DIRECTORY_SERVICE:
1221 snprintf(port->sysfs_device.bus_id, BUS_ID_SIZE, 663 bus_id = "directory";
1222 "directory");
1223 break; 664 break;
1224 case ZFCP_DID_MANAGEMENT_SERVICE: 665 case ZFCP_DID_MANAGEMENT_SERVICE:
1225 snprintf(port->sysfs_device.bus_id, BUS_ID_SIZE, 666 bus_id = "management";
1226 "management");
1227 break; 667 break;
1228 case ZFCP_DID_KEY_DISTRIBUTION_SERVICE: 668 case ZFCP_DID_KEY_DISTRIBUTION_SERVICE:
1229 snprintf(port->sysfs_device.bus_id, BUS_ID_SIZE, 669 bus_id = "key_distribution";
1230 "key_distribution");
1231 break; 670 break;
1232 case ZFCP_DID_ALIAS_SERVICE: 671 case ZFCP_DID_ALIAS_SERVICE:
1233 snprintf(port->sysfs_device.bus_id, BUS_ID_SIZE, 672 bus_id = "alias";
1234 "alias");
1235 break; 673 break;
1236 case ZFCP_DID_TIME_SERVICE: 674 case ZFCP_DID_TIME_SERVICE:
1237 snprintf(port->sysfs_device.bus_id, BUS_ID_SIZE, 675 bus_id = "time";
1238 "time");
1239 break; 676 break;
1240 default: 677 default:
1241 kfree(port); 678 kfree(port);
1242 return NULL; 679 return ERR_PTR(-EINVAL);
1243 } 680 }
1244 port->d_id = d_id; 681 snprintf(port->sysfs_device.bus_id, BUS_ID_SIZE, "%s", bus_id);
1245 port->sysfs_device.parent = &adapter->generic_services; 682 port->sysfs_device.parent = &adapter->generic_services;
1246 } else { 683 } else {
1247 snprintf(port->sysfs_device.bus_id, 684 snprintf(port->sysfs_device.bus_id,
1248 BUS_ID_SIZE, "0x%016llx", wwpn); 685 BUS_ID_SIZE, "0x%016llx", wwpn);
1249 port->sysfs_device.parent = &adapter->ccw_device->dev; 686 port->sysfs_device.parent = &adapter->ccw_device->dev;
1250 } 687 }
688
1251 port->sysfs_device.release = zfcp_sysfs_port_release; 689 port->sysfs_device.release = zfcp_sysfs_port_release;
1252 dev_set_drvdata(&port->sysfs_device, port); 690 dev_set_drvdata(&port->sysfs_device, port);
1253 691
1254 /* mark port unusable as long as sysfs registration is not complete */ 692 read_lock_irq(&zfcp_data.config_lock);
1255 atomic_set_mask(ZFCP_STATUS_COMMON_REMOVE, &port->status); 693 if (!(status & ZFCP_STATUS_PORT_NO_WWPN))
694 if (zfcp_get_port_by_wwpn(adapter, wwpn)) {
695 read_unlock_irq(&zfcp_data.config_lock);
696 goto err_out_free;
697 }
698 read_unlock_irq(&zfcp_data.config_lock);
1256 699
1257 if (device_register(&port->sysfs_device)) { 700 if (device_register(&port->sysfs_device))
1258 kfree(port); 701 goto err_out_free;
1259 return NULL; 702
1260 } 703 if (status & ZFCP_STATUS_PORT_WKA)
704 retval = sysfs_create_group(&port->sysfs_device.kobj,
705 &zfcp_sysfs_ns_port_attrs);
706 else
707 retval = sysfs_create_group(&port->sysfs_device.kobj,
708 &zfcp_sysfs_port_attrs);
1261 709
1262 if (zfcp_sysfs_port_create_files(&port->sysfs_device, status)) { 710 if (retval) {
1263 device_unregister(&port->sysfs_device); 711 device_unregister(&port->sysfs_device);
1264 return NULL; 712 goto err_out;
1265 } 713 }
1266 714
1267 zfcp_port_get(port); 715 zfcp_port_get(port);
@@ -1274,15 +722,23 @@ zfcp_port_enqueue(struct zfcp_adapter *adapter, wwn_t wwpn, u32 status,
1274 if (!adapter->nameserver_port) 722 if (!adapter->nameserver_port)
1275 adapter->nameserver_port = port; 723 adapter->nameserver_port = port;
1276 adapter->ports++; 724 adapter->ports++;
725
1277 write_unlock_irq(&zfcp_data.config_lock); 726 write_unlock_irq(&zfcp_data.config_lock);
1278 727
1279 zfcp_adapter_get(adapter); 728 zfcp_adapter_get(adapter);
1280
1281 return port; 729 return port;
730
731err_out_free:
732 kfree(port);
733err_out:
734 return ERR_PTR(-EINVAL);
1282} 735}
1283 736
1284void 737/**
1285zfcp_port_dequeue(struct zfcp_port *port) 738 * zfcp_port_dequeue - dequeues a port from the port list of the adapter
739 * @port: pointer to struct zfcp_port which should be removed
740 */
741void zfcp_port_dequeue(struct zfcp_port *port)
1286{ 742{
1287 zfcp_port_wait(port); 743 zfcp_port_wait(port);
1288 write_lock_irq(&zfcp_data.config_lock); 744 write_lock_irq(&zfcp_data.config_lock);
@@ -1293,546 +749,53 @@ zfcp_port_dequeue(struct zfcp_port *port)
1293 fc_remote_port_delete(port->rport); 749 fc_remote_port_delete(port->rport);
1294 port->rport = NULL; 750 port->rport = NULL;
1295 zfcp_adapter_put(port->adapter); 751 zfcp_adapter_put(port->adapter);
1296 zfcp_sysfs_port_remove_files(&port->sysfs_device, 752 if (atomic_read(&port->status) & ZFCP_STATUS_PORT_WKA)
1297 atomic_read(&port->status)); 753 sysfs_remove_group(&port->sysfs_device.kobj,
1298 device_unregister(&port->sysfs_device); 754 &zfcp_sysfs_ns_port_attrs);
1299}
1300
1301/* Enqueues a nameserver port */
1302int
1303zfcp_nameserver_enqueue(struct zfcp_adapter *adapter)
1304{
1305 struct zfcp_port *port;
1306
1307 port = zfcp_port_enqueue(adapter, 0, ZFCP_STATUS_PORT_WKA,
1308 ZFCP_DID_DIRECTORY_SERVICE);
1309 if (!port) {
1310 ZFCP_LOG_INFO("error: enqueue of nameserver port for "
1311 "adapter %s failed\n",
1312 zfcp_get_busid_by_adapter(adapter));
1313 return -ENXIO;
1314 }
1315 zfcp_port_put(port);
1316
1317 return 0;
1318}
1319
1320#undef ZFCP_LOG_AREA
1321
1322/****************************************************************/
1323/******* Fibre Channel Standard related Functions **************/
1324/****************************************************************/
1325
1326#define ZFCP_LOG_AREA ZFCP_LOG_AREA_FC
1327
1328static void zfcp_fsf_incoming_els_rscn(struct zfcp_fsf_req *fsf_req)
1329{
1330 struct fsf_status_read_buffer *status_buffer = (void*)fsf_req->data;
1331 struct zfcp_adapter *adapter = fsf_req->adapter;
1332 struct fcp_rscn_head *fcp_rscn_head;
1333 struct fcp_rscn_element *fcp_rscn_element;
1334 struct zfcp_port *port;
1335 u16 i;
1336 u16 no_entries;
1337 u32 range_mask;
1338 unsigned long flags;
1339
1340 fcp_rscn_head = (struct fcp_rscn_head *) status_buffer->payload;
1341 fcp_rscn_element = (struct fcp_rscn_element *) status_buffer->payload;
1342
1343 /* see FC-FS */
1344 no_entries = (fcp_rscn_head->payload_len / 4);
1345
1346 for (i = 1; i < no_entries; i++) {
1347 /* skip head and start with 1st element */
1348 fcp_rscn_element++;
1349 switch (fcp_rscn_element->addr_format) {
1350 case ZFCP_PORT_ADDRESS:
1351 range_mask = ZFCP_PORTS_RANGE_PORT;
1352 break;
1353 case ZFCP_AREA_ADDRESS:
1354 range_mask = ZFCP_PORTS_RANGE_AREA;
1355 break;
1356 case ZFCP_DOMAIN_ADDRESS:
1357 range_mask = ZFCP_PORTS_RANGE_DOMAIN;
1358 break;
1359 case ZFCP_FABRIC_ADDRESS:
1360 range_mask = ZFCP_PORTS_RANGE_FABRIC;
1361 break;
1362 default:
1363 ZFCP_LOG_INFO("incoming RSCN with unknown "
1364 "address format\n");
1365 continue;
1366 }
1367 read_lock_irqsave(&zfcp_data.config_lock, flags);
1368 list_for_each_entry(port, &adapter->port_list_head, list) {
1369 if (atomic_test_mask
1370 (ZFCP_STATUS_PORT_WKA, &port->status))
1371 continue;
1372 /* Do we know this port? If not skip it. */
1373 if (!atomic_test_mask
1374 (ZFCP_STATUS_PORT_DID_DID, &port->status)) {
1375 ZFCP_LOG_INFO("incoming RSCN, trying to open "
1376 "port 0x%016Lx\n", port->wwpn);
1377 zfcp_erp_port_reopen(port,
1378 ZFCP_STATUS_COMMON_ERP_FAILED,
1379 82, fsf_req);
1380 continue;
1381 }
1382
1383 /*
1384 * FIXME: race: d_id might being invalidated
1385 * (...DID_DID reset)
1386 */
1387 if ((port->d_id & range_mask)
1388 == (fcp_rscn_element->nport_did & range_mask)) {
1389 ZFCP_LOG_TRACE("reopen did 0x%08x\n",
1390 fcp_rscn_element->nport_did);
1391 /*
1392 * Unfortunately, an RSCN does not specify the
1393 * type of change a target underwent. We assume
1394 * that it makes sense to reopen the link.
1395 * FIXME: Shall we try to find out more about
1396 * the target and link state before closing it?
1397 * How to accomplish this? (nameserver?)
1398 * Where would such code be put in?
1399 * (inside or outside erp)
1400 */
1401 ZFCP_LOG_INFO("incoming RSCN, trying to open "
1402 "port 0x%016Lx\n", port->wwpn);
1403 zfcp_test_link(port);
1404 }
1405 }
1406 read_unlock_irqrestore(&zfcp_data.config_lock, flags);
1407 }
1408}
1409
1410static void zfcp_fsf_incoming_els_plogi(struct zfcp_fsf_req *fsf_req)
1411{
1412 struct fsf_status_read_buffer *status_buffer = (void*)fsf_req->data;
1413 struct zfcp_adapter *adapter = fsf_req->adapter;
1414 struct fsf_plogi *els_plogi;
1415 struct zfcp_port *port;
1416 unsigned long flags;
1417
1418 els_plogi = (struct fsf_plogi *) status_buffer->payload;
1419 read_lock_irqsave(&zfcp_data.config_lock, flags);
1420 list_for_each_entry(port, &adapter->port_list_head, list) {
1421 if (port->wwpn == (*(wwn_t *) &els_plogi->serv_param.wwpn))
1422 break;
1423 }
1424 read_unlock_irqrestore(&zfcp_data.config_lock, flags);
1425
1426 if (!port || (port->wwpn != (*(wwn_t *) &els_plogi->serv_param.wwpn))) {
1427 ZFCP_LOG_DEBUG("ignored incoming PLOGI for nonexisting port "
1428 "with d_id 0x%06x on adapter %s\n",
1429 status_buffer->d_id,
1430 zfcp_get_busid_by_adapter(adapter));
1431 } else {
1432 zfcp_erp_port_forced_reopen(port, 0, 83, fsf_req);
1433 }
1434}
1435
1436static void zfcp_fsf_incoming_els_logo(struct zfcp_fsf_req *fsf_req)
1437{
1438 struct fsf_status_read_buffer *status_buffer = (void*)fsf_req->data;
1439 struct zfcp_adapter *adapter = fsf_req->adapter;
1440 struct fcp_logo *els_logo = (struct fcp_logo *) status_buffer->payload;
1441 struct zfcp_port *port;
1442 unsigned long flags;
1443
1444 read_lock_irqsave(&zfcp_data.config_lock, flags);
1445 list_for_each_entry(port, &adapter->port_list_head, list) {
1446 if (port->wwpn == els_logo->nport_wwpn)
1447 break;
1448 }
1449 read_unlock_irqrestore(&zfcp_data.config_lock, flags);
1450
1451 if (!port || (port->wwpn != els_logo->nport_wwpn)) {
1452 ZFCP_LOG_DEBUG("ignored incoming LOGO for nonexisting port "
1453 "with d_id 0x%06x on adapter %s\n",
1454 status_buffer->d_id,
1455 zfcp_get_busid_by_adapter(adapter));
1456 } else {
1457 zfcp_erp_port_forced_reopen(port, 0, 84, fsf_req);
1458 }
1459}
1460
1461static void
1462zfcp_fsf_incoming_els_unknown(struct zfcp_adapter *adapter,
1463 struct fsf_status_read_buffer *status_buffer)
1464{
1465 ZFCP_LOG_NORMAL("warning: unknown incoming ELS 0x%08x "
1466 "for adapter %s\n", *(u32 *) (status_buffer->payload),
1467 zfcp_get_busid_by_adapter(adapter));
1468
1469}
1470
1471void
1472zfcp_fsf_incoming_els(struct zfcp_fsf_req *fsf_req)
1473{
1474 struct fsf_status_read_buffer *status_buffer;
1475 u32 els_type;
1476 struct zfcp_adapter *adapter;
1477
1478 status_buffer = (struct fsf_status_read_buffer *) fsf_req->data;
1479 els_type = *(u32 *) (status_buffer->payload);
1480 adapter = fsf_req->adapter;
1481
1482 zfcp_san_dbf_event_incoming_els(fsf_req);
1483 if (els_type == LS_PLOGI)
1484 zfcp_fsf_incoming_els_plogi(fsf_req);
1485 else if (els_type == LS_LOGO)
1486 zfcp_fsf_incoming_els_logo(fsf_req);
1487 else if ((els_type & 0xffff0000) == LS_RSCN)
1488 /* we are only concerned with the command, not the length */
1489 zfcp_fsf_incoming_els_rscn(fsf_req);
1490 else
1491 zfcp_fsf_incoming_els_unknown(adapter, status_buffer);
1492}
1493
1494
1495/**
1496 * zfcp_gid_pn_buffers_alloc - allocate buffers for GID_PN nameserver request
1497 * @gid_pn: pointer to return pointer to struct zfcp_gid_pn_data
1498 * @pool: pointer to mempool_t if non-null memory pool is used for allocation
1499 */
1500static int
1501zfcp_gid_pn_buffers_alloc(struct zfcp_gid_pn_data **gid_pn, mempool_t *pool)
1502{
1503 struct zfcp_gid_pn_data *data;
1504
1505 if (pool != NULL) {
1506 data = mempool_alloc(pool, GFP_ATOMIC);
1507 if (likely(data != NULL)) {
1508 data->ct.pool = pool;
1509 }
1510 } else {
1511 data = kmem_cache_alloc(zfcp_data.gid_pn_cache, GFP_ATOMIC);
1512 }
1513
1514 if (NULL == data)
1515 return -ENOMEM;
1516
1517 memset(data, 0, sizeof(*data));
1518 sg_init_table(&data->req , 1);
1519 sg_init_table(&data->resp , 1);
1520 data->ct.req = &data->req;
1521 data->ct.resp = &data->resp;
1522 data->ct.req_count = data->ct.resp_count = 1;
1523 zfcp_address_to_sg(&data->ct_iu_req, &data->req, sizeof(struct ct_iu_gid_pn_req));
1524 zfcp_address_to_sg(&data->ct_iu_resp, &data->resp, sizeof(struct ct_iu_gid_pn_resp));
1525
1526 *gid_pn = data;
1527 return 0;
1528}
1529
1530/**
1531 * zfcp_gid_pn_buffers_free - free buffers for GID_PN nameserver request
1532 * @gid_pn: pointer to struct zfcp_gid_pn_data which has to be freed
1533 */
1534static void zfcp_gid_pn_buffers_free(struct zfcp_gid_pn_data *gid_pn)
1535{
1536 if (gid_pn->ct.pool)
1537 mempool_free(gid_pn, gid_pn->ct.pool);
1538 else 755 else
1539 kmem_cache_free(zfcp_data.gid_pn_cache, gid_pn); 756 sysfs_remove_group(&port->sysfs_device.kobj,
1540} 757 &zfcp_sysfs_port_attrs);
1541 758 device_unregister(&port->sysfs_device);
1542/**
1543 * zfcp_ns_gid_pn_request - initiate GID_PN nameserver request
1544 * @erp_action: pointer to zfcp_erp_action where GID_PN request is needed
1545 */
1546int
1547zfcp_ns_gid_pn_request(struct zfcp_erp_action *erp_action)
1548{
1549 int ret;
1550 struct ct_iu_gid_pn_req *ct_iu_req;
1551 struct zfcp_gid_pn_data *gid_pn;
1552 struct zfcp_adapter *adapter = erp_action->adapter;
1553
1554 ret = zfcp_gid_pn_buffers_alloc(&gid_pn, adapter->pool.data_gid_pn);
1555 if (ret < 0) {
1556 ZFCP_LOG_INFO("error: buffer allocation for gid_pn nameserver "
1557 "request failed for adapter %s\n",
1558 zfcp_get_busid_by_adapter(adapter));
1559 goto out;
1560 }
1561
1562 /* setup nameserver request */
1563 ct_iu_req = zfcp_sg_to_address(gid_pn->ct.req);
1564 ct_iu_req->header.revision = ZFCP_CT_REVISION;
1565 ct_iu_req->header.gs_type = ZFCP_CT_DIRECTORY_SERVICE;
1566 ct_iu_req->header.gs_subtype = ZFCP_CT_NAME_SERVER;
1567 ct_iu_req->header.options = ZFCP_CT_SYNCHRONOUS;
1568 ct_iu_req->header.cmd_rsp_code = ZFCP_CT_GID_PN;
1569 ct_iu_req->header.max_res_size = ZFCP_CT_MAX_SIZE;
1570 ct_iu_req->wwpn = erp_action->port->wwpn;
1571
1572 /* setup parameters for send generic command */
1573 gid_pn->ct.port = adapter->nameserver_port;
1574 gid_pn->ct.handler = zfcp_ns_gid_pn_handler;
1575 gid_pn->ct.handler_data = (unsigned long) gid_pn;
1576 gid_pn->ct.timeout = ZFCP_NS_GID_PN_TIMEOUT;
1577 gid_pn->port = erp_action->port;
1578
1579 ret = zfcp_fsf_send_ct(&gid_pn->ct, adapter->pool.fsf_req_erp,
1580 erp_action);
1581 if (ret) {
1582 ZFCP_LOG_INFO("error: initiation of gid_pn nameserver request "
1583 "failed for adapter %s\n",
1584 zfcp_get_busid_by_adapter(adapter));
1585
1586 zfcp_gid_pn_buffers_free(gid_pn);
1587 }
1588
1589 out:
1590 return ret;
1591}
1592
1593/**
1594 * zfcp_ns_gid_pn_handler - handler for GID_PN nameserver request
1595 * @data: unsigned long, contains pointer to struct zfcp_gid_pn_data
1596 */
1597static void zfcp_ns_gid_pn_handler(unsigned long data)
1598{
1599 struct zfcp_port *port;
1600 struct zfcp_send_ct *ct;
1601 struct ct_iu_gid_pn_req *ct_iu_req;
1602 struct ct_iu_gid_pn_resp *ct_iu_resp;
1603 struct zfcp_gid_pn_data *gid_pn;
1604
1605
1606 gid_pn = (struct zfcp_gid_pn_data *) data;
1607 port = gid_pn->port;
1608 ct = &gid_pn->ct;
1609 ct_iu_req = zfcp_sg_to_address(ct->req);
1610 ct_iu_resp = zfcp_sg_to_address(ct->resp);
1611
1612 if (ct->status != 0)
1613 goto failed;
1614
1615 if (zfcp_check_ct_response(&ct_iu_resp->header)) {
1616 /* FIXME: do we need some specific erp entry points */
1617 atomic_set_mask(ZFCP_STATUS_PORT_INVALID_WWPN, &port->status);
1618 goto failed;
1619 }
1620 /* paranoia */
1621 if (ct_iu_req->wwpn != port->wwpn) {
1622 ZFCP_LOG_NORMAL("bug: wwpn 0x%016Lx returned by nameserver "
1623 "lookup does not match expected wwpn 0x%016Lx "
1624 "for adapter %s\n", ct_iu_req->wwpn, port->wwpn,
1625 zfcp_get_busid_by_port(port));
1626 goto mismatch;
1627 }
1628
1629 /* looks like a valid d_id */
1630 port->d_id = ct_iu_resp->d_id & ZFCP_DID_MASK;
1631 atomic_set_mask(ZFCP_STATUS_PORT_DID_DID, &port->status);
1632 ZFCP_LOG_DEBUG("adapter %s: wwpn=0x%016Lx ---> d_id=0x%06x\n",
1633 zfcp_get_busid_by_port(port), port->wwpn, port->d_id);
1634 goto out;
1635
1636 mismatch:
1637 ZFCP_LOG_DEBUG("CT IUs do not match:\n");
1638 ZFCP_HEX_DUMP(ZFCP_LOG_LEVEL_DEBUG, (char *) ct_iu_req,
1639 sizeof(struct ct_iu_gid_pn_req));
1640 ZFCP_HEX_DUMP(ZFCP_LOG_LEVEL_DEBUG, (char *) ct_iu_resp,
1641 sizeof(struct ct_iu_gid_pn_resp));
1642
1643 failed:
1644 ZFCP_LOG_NORMAL("warning: failed gid_pn nameserver request for wwpn "
1645 "0x%016Lx for adapter %s\n",
1646 port->wwpn, zfcp_get_busid_by_port(port));
1647 out:
1648 zfcp_gid_pn_buffers_free(gid_pn);
1649 return;
1650} 759}
1651 760
1652/* reject CT_IU reason codes acc. to FC-GS-4 */
1653static const struct zfcp_rc_entry zfcp_ct_rc[] = {
1654 {0x01, "invalid command code"},
1655 {0x02, "invalid version level"},
1656 {0x03, "logical error"},
1657 {0x04, "invalid CT_IU size"},
1658 {0x05, "logical busy"},
1659 {0x07, "protocol error"},
1660 {0x09, "unable to perform command request"},
1661 {0x0b, "command not supported"},
1662 {0x0d, "server not available"},
1663 {0x0e, "session could not be established"},
1664 {0xff, "vendor specific error"},
1665 {0, NULL},
1666};
1667
1668/* LS_RJT reason codes acc. to FC-FS */
1669static const struct zfcp_rc_entry zfcp_ls_rjt_rc[] = {
1670 {0x01, "invalid LS_Command code"},
1671 {0x03, "logical error"},
1672 {0x05, "logical busy"},
1673 {0x07, "protocol error"},
1674 {0x09, "unable to perform command request"},
1675 {0x0b, "command not supported"},
1676 {0x0e, "command already in progress"},
1677 {0xff, "vendor specific error"},
1678 {0, NULL},
1679};
1680
1681/* reject reason codes according to FC-PH/FC-FS */
1682static const struct zfcp_rc_entry zfcp_p_rjt_rc[] = {
1683 {0x01, "invalid D_ID"},
1684 {0x02, "invalid S_ID"},
1685 {0x03, "Nx_Port not available, temporary"},
1686 {0x04, "Nx_Port not available, permament"},
1687 {0x05, "class not supported"},
1688 {0x06, "delimiter usage error"},
1689 {0x07, "TYPE not supported"},
1690 {0x08, "invalid Link_Control"},
1691 {0x09, "invalid R_CTL field"},
1692 {0x0a, "invalid F_CTL field"},
1693 {0x0b, "invalid OX_ID"},
1694 {0x0c, "invalid RX_ID"},
1695 {0x0d, "invalid SEQ_ID"},
1696 {0x0e, "invalid DF_CTL"},
1697 {0x0f, "invalid SEQ_CNT"},
1698 {0x10, "invalid parameter field"},
1699 {0x11, "exchange error"},
1700 {0x12, "protocol error"},
1701 {0x13, "incorrect length"},
1702 {0x14, "unsupported ACK"},
1703 {0x15, "class of service not supported by entity at FFFFFE"},
1704 {0x16, "login required"},
1705 {0x17, "excessive sequences attempted"},
1706 {0x18, "unable to establish exchange"},
1707 {0x1a, "fabric path not available"},
1708 {0x1b, "invalid VC_ID (class 4)"},
1709 {0x1c, "invalid CS_CTL field"},
1710 {0x1d, "insufficient resources for VC (class 4)"},
1711 {0x1f, "invalid class of service"},
1712 {0x20, "preemption request rejected"},
1713 {0x21, "preemption not enabled"},
1714 {0x22, "multicast error"},
1715 {0x23, "multicast error terminate"},
1716 {0x24, "process login required"},
1717 {0xff, "vendor specific reject"},
1718 {0, NULL},
1719};
1720
1721/** 761/**
1722 * zfcp_rc_description - return description for given reaon code 762 * zfcp_sg_free_table - free memory used by scatterlists
1723 * @code: reason code 763 * @sg: pointer to scatterlist
1724 * @rc_table: table of reason codes and descriptions 764 * @count: number of scatterlist which are to be free'ed
765 * the scatterlist are expected to reference pages always
1725 */ 766 */
1726static const char * 767void zfcp_sg_free_table(struct scatterlist *sg, int count)
1727zfcp_rc_description(u8 code, const struct zfcp_rc_entry *rc_table)
1728{ 768{
1729 const char *descr = "unknown reason code"; 769 int i;
1730 770
1731 do { 771 for (i = 0; i < count; i++, sg++)
1732 if (code == rc_table->code) { 772 if (sg)
1733 descr = rc_table->description; 773 free_page((unsigned long) sg_virt(sg));
774 else
1734 break; 775 break;
1735 }
1736 rc_table++;
1737 } while (rc_table->code && rc_table->description);
1738
1739 return descr;
1740} 776}
1741 777
1742/** 778/**
1743 * zfcp_check_ct_response - evaluate reason code for CT_IU 779 * zfcp_sg_setup_table - init scatterlist and allocate, assign buffers
1744 * @rjt: response payload to an CT_IU request 780 * @sg: pointer to struct scatterlist
1745 * Return: 0 for accept CT_IU, 1 for reject CT_IU or invlid response code 781 * @count: number of scatterlists which should be assigned with buffers
782 * of size page
783 *
784 * Returns: 0 on success, -ENOMEM otherwise
1746 */ 785 */
1747int 786int zfcp_sg_setup_table(struct scatterlist *sg, int count)
1748zfcp_check_ct_response(struct ct_hdr *rjt)
1749{ 787{
1750 if (rjt->cmd_rsp_code == ZFCP_CT_ACCEPT) 788 void *addr;
1751 return 0; 789 int i;
1752 790
1753 if (rjt->cmd_rsp_code != ZFCP_CT_REJECT) { 791 sg_init_table(sg, count);
1754 ZFCP_LOG_NORMAL("error: invalid Generic Service command/" 792 for (i = 0; i < count; i++, sg++) {
1755 "response code (0x%04hx)\n", 793 addr = (void *) get_zeroed_page(GFP_KERNEL);
1756 rjt->cmd_rsp_code); 794 if (!addr) {
1757 return 1; 795 zfcp_sg_free_table(sg, i);
796 return -ENOMEM;
797 }
798 sg_set_buf(sg, addr, PAGE_SIZE);
1758 } 799 }
1759 800 return 0;
1760 ZFCP_LOG_INFO("Generic Service command rejected\n");
1761 ZFCP_LOG_INFO("%s (0x%02x, 0x%02x, 0x%02x)\n",
1762 zfcp_rc_description(rjt->reason_code, zfcp_ct_rc),
1763 (u32) rjt->reason_code, (u32) rjt->reason_code_expl,
1764 (u32) rjt->vendor_unique);
1765
1766 return 1;
1767}
1768
1769/**
1770 * zfcp_print_els_rjt - print reject parameter and description for ELS reject
1771 * @rjt_par: reject parameter acc. to FC-PH/FC-FS
1772 * @rc_table: table of reason codes and descriptions
1773 */
1774static void
1775zfcp_print_els_rjt(struct zfcp_ls_rjt_par *rjt_par,
1776 const struct zfcp_rc_entry *rc_table)
1777{
1778 ZFCP_LOG_INFO("%s (%02x %02x %02x %02x)\n",
1779 zfcp_rc_description(rjt_par->reason_code, rc_table),
1780 (u32) rjt_par->action, (u32) rjt_par->reason_code,
1781 (u32) rjt_par->reason_expl, (u32) rjt_par->vendor_unique);
1782}
1783
1784/**
1785 * zfcp_fsf_handle_els_rjt - evaluate status qualifier/reason code on ELS reject
1786 * @sq: status qualifier word
1787 * @rjt_par: reject parameter as described in FC-PH and FC-FS
1788 * Return: -EROMTEIO for LS_RJT, -EREMCHG for invalid D_ID, -EIO else
1789 */
1790int
1791zfcp_handle_els_rjt(u32 sq, struct zfcp_ls_rjt_par *rjt_par)
1792{
1793 int ret = -EIO;
1794
1795 if (sq == FSF_IOSTAT_NPORT_RJT) {
1796 ZFCP_LOG_INFO("ELS rejected (P_RJT)\n");
1797 zfcp_print_els_rjt(rjt_par, zfcp_p_rjt_rc);
1798 /* invalid d_id */
1799 if (rjt_par->reason_code == 0x01)
1800 ret = -EREMCHG;
1801 } else if (sq == FSF_IOSTAT_FABRIC_RJT) {
1802 ZFCP_LOG_INFO("ELS rejected (F_RJT)\n");
1803 zfcp_print_els_rjt(rjt_par, zfcp_p_rjt_rc);
1804 /* invalid d_id */
1805 if (rjt_par->reason_code == 0x01)
1806 ret = -EREMCHG;
1807 } else if (sq == FSF_IOSTAT_LS_RJT) {
1808 ZFCP_LOG_INFO("ELS rejected (LS_RJT)\n");
1809 zfcp_print_els_rjt(rjt_par, zfcp_ls_rjt_rc);
1810 ret = -EREMOTEIO;
1811 } else
1812 ZFCP_LOG_INFO("unexpected SQ: 0x%02x\n", sq);
1813
1814 return ret;
1815}
1816
1817/**
1818 * zfcp_plogi_evaluate - evaluate PLOGI playload and copy important fields
1819 * into zfcp_port structure
1820 * @port: zfcp_port structure
1821 * @plogi: plogi payload
1822 */
1823void
1824zfcp_plogi_evaluate(struct zfcp_port *port, struct fsf_plogi *plogi)
1825{
1826 port->maxframe_size = plogi->serv_param.common_serv_param[7] |
1827 ((plogi->serv_param.common_serv_param[6] & 0x0F) << 8);
1828 if (plogi->serv_param.class1_serv_param[0] & 0x80)
1829 port->supported_classes |= FC_COS_CLASS1;
1830 if (plogi->serv_param.class2_serv_param[0] & 0x80)
1831 port->supported_classes |= FC_COS_CLASS2;
1832 if (plogi->serv_param.class3_serv_param[0] & 0x80)
1833 port->supported_classes |= FC_COS_CLASS3;
1834 if (plogi->serv_param.class4_serv_param[0] & 0x80)
1835 port->supported_classes |= FC_COS_CLASS4;
1836} 801}
1837
1838#undef ZFCP_LOG_AREA