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