Merge branch 'upstream-fixes' of master.kernel.org:/pub/scm/linux/kernel/git/jikos/hid
[deliverable/linux.git] / drivers / s390 / scsi / zfcp_aux.c
1 /*
2 * This file is part of the zfcp device driver for
3 * FCP adapters for IBM System z9 and zSeries.
4 *
5 * (C) Copyright IBM Corp. 2002, 2006
6 *
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
22 /*
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
36 #include "zfcp_ext.h"
37
38 /* accumulated log level (module parameter) */
39 static u32 loglevel = ZFCP_LOG_LEVEL_DEFAULTS;
40 static char *device;
41 /*********************** FUNCTION PROTOTYPES *********************************/
42
43 /* written against the module interface */
44 static int __init zfcp_module_init(void);
45
46 /* FCP related */
47 static void zfcp_ns_gid_pn_handler(unsigned long);
48
49 /* miscellaneous */
50 static int zfcp_sg_list_alloc(struct zfcp_sg_list *, size_t);
51 static void zfcp_sg_list_free(struct zfcp_sg_list *);
52 static int zfcp_sg_list_copy_from_user(struct zfcp_sg_list *,
53 void __user *, size_t);
54 static int zfcp_sg_list_copy_to_user(void __user *,
55 struct zfcp_sg_list *, size_t);
56 static 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
63 static 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
70 static 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 */
79 module_init(zfcp_module_init);
80
81 MODULE_AUTHOR("IBM Deutschland Entwicklung GmbH - linux390@de.ibm.com");
82 MODULE_DESCRIPTION
83 ("FCP (SCSI over Fibre Channel) HBA driver for IBM System z9 and zSeries");
84 MODULE_LICENSE("GPL");
85
86 module_param(device, charp, 0400);
87 MODULE_PARM_DESC(device, "specify initial device");
88
89 module_param(loglevel, uint, 0400);
90 MODULE_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
99 void
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
121 static int zfcp_reqlist_alloc(struct zfcp_adapter *adapter)
122 {
123 int idx;
124
125 adapter->req_list = kcalloc(REQUEST_LIST_SIZE, sizeof(struct list_head),
126 GFP_KERNEL);
127 if (!adapter->req_list)
128 return -ENOMEM;
129
130 for (idx = 0; idx < REQUEST_LIST_SIZE; idx++)
131 INIT_LIST_HEAD(&adapter->req_list[idx]);
132 return 0;
133 }
134
135 static void zfcp_reqlist_free(struct zfcp_adapter *adapter)
136 {
137 kfree(adapter->req_list);
138 }
139
140 int zfcp_reqlist_isempty(struct zfcp_adapter *adapter)
141 {
142 unsigned int idx;
143
144 for (idx = 0; idx < REQUEST_LIST_SIZE; idx++)
145 if (!list_empty(&adapter->req_list[idx]))
146 return 0;
147 return 1;
148 }
149
150 #undef ZFCP_LOG_AREA
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 */
164 static int __init
165 zfcp_device_setup(char *devstr)
166 {
167 char *tmp, *str;
168 size_t len;
169
170 if (!devstr)
171 return 0;
172
173 len = strlen(devstr) + 1;
174 str = kmalloc(len, GFP_KERNEL);
175 if (!str)
176 goto err_out;
177 memcpy(str, devstr, len);
178
179 tmp = strchr(str, ',');
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
186 zfcp_data.init_wwpn = simple_strtoull(tmp, &tmp, 0);
187 if (*tmp++ != ',')
188 goto err_out;
189 if (*tmp == '\0')
190 goto err_out;
191
192 zfcp_data.init_fcp_lun = simple_strtoull(tmp, &tmp, 0);
193 if (*tmp != '\0')
194 goto err_out;
195 kfree(str);
196 return 1;
197
198 err_out:
199 ZFCP_LOG_NORMAL("Parse error for device parameter string %s\n", str);
200 kfree(str);
201 return 0;
202 }
203
204 static void __init
205 zfcp_init_device_configure(void)
206 {
207 struct zfcp_adapter *adapter;
208 struct zfcp_port *port;
209 struct zfcp_unit *unit;
210
211 down(&zfcp_data.config_sema);
212 read_lock_irq(&zfcp_data.config_lock);
213 adapter = zfcp_get_adapter_by_busid(zfcp_data.init_busid);
214 if (adapter)
215 zfcp_adapter_get(adapter);
216 read_unlock_irq(&zfcp_data.config_lock);
217
218 if (adapter == NULL)
219 goto out_adapter;
220 port = zfcp_port_enqueue(adapter, zfcp_data.init_wwpn, 0, 0);
221 if (!port)
222 goto out_port;
223 unit = zfcp_unit_enqueue(port, zfcp_data.init_fcp_lun);
224 if (!unit)
225 goto out_unit;
226 up(&zfcp_data.config_sema);
227 ccw_device_set_online(adapter->ccw_device);
228 zfcp_erp_wait(adapter);
229 down(&zfcp_data.config_sema);
230 zfcp_unit_put(unit);
231 out_unit:
232 zfcp_port_put(port);
233 out_port:
234 zfcp_adapter_put(adapter);
235 out_adapter:
236 up(&zfcp_data.config_sema);
237 return;
238 }
239
240 static int calc_alignment(int size)
241 {
242 int align = 1;
243
244 if (!size)
245 return 0;
246
247 while ((size - align) > 0)
248 align <<= 1;
249
250 return align;
251 }
252
253 static int __init
254 zfcp_module_init(void)
255 {
256 int retval = -ENOMEM;
257 int size, align;
258
259 size = sizeof(struct zfcp_fsf_req_qtcb);
260 align = calc_alignment(size);
261 zfcp_data.fsf_req_qtcb_cache =
262 kmem_cache_create("zfcp_fsf", size, align, 0, NULL, NULL);
263 if (!zfcp_data.fsf_req_qtcb_cache)
264 goto out;
265
266 size = sizeof(struct fsf_status_read_buffer);
267 align = calc_alignment(size);
268 zfcp_data.sr_buffer_cache =
269 kmem_cache_create("zfcp_sr", size, align, 0, NULL, NULL);
270 if (!zfcp_data.sr_buffer_cache)
271 goto out_sr_cache;
272
273 size = sizeof(struct zfcp_gid_pn_data);
274 align = calc_alignment(size);
275 zfcp_data.gid_pn_cache =
276 kmem_cache_create("zfcp_gid", size, align, 0, NULL, NULL);
277 if (!zfcp_data.gid_pn_cache)
278 goto out_gid_cache;
279
280 atomic_set(&zfcp_data.loglevel, loglevel);
281
282 /* initialize adapter list */
283 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);
287
288 zfcp_data.scsi_transport_template =
289 fc_attach_transport(&zfcp_transport_functions);
290 if (!zfcp_data.scsi_transport_template)
291 goto out_transport;
292
293 retval = misc_register(&zfcp_cfdc_misc);
294 if (retval != 0) {
295 ZFCP_LOG_INFO("registration of misc device "
296 "zfcp_cfdc failed\n");
297 goto out_misc;
298 }
299
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();
311 if (retval) {
312 ZFCP_LOG_NORMAL("registration with common I/O layer failed\n");
313 goto out_ccw_register;
314 }
315
316 if (zfcp_device_setup(device))
317 zfcp_init_device_configure();
318
319 goto out;
320
321 out_ccw_register:
322 misc_deregister(&zfcp_cfdc_misc);
323 out_misc:
324 fc_release_transport(zfcp_data.scsi_transport_template);
325 out_transport:
326 kmem_cache_destroy(zfcp_data.gid_pn_cache);
327 out_gid_cache:
328 kmem_cache_destroy(zfcp_data.sr_buffer_cache);
329 out_sr_cache:
330 kmem_cache_destroy(zfcp_data.fsf_req_qtcb_cache);
331 out:
332 return retval;
333 }
334
335 /*
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 */
351 static long
352 zfcp_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 */
542 static int
543 zfcp_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
563 for (i = 0, sg = sg_list->sg; i < sg_list->count; i++, sg++) {
564 sg->length = min(size, PAGE_SIZE);
565 sg->offset = 0;
566 address = (void *) get_zeroed_page(GFP_KERNEL);
567 if (address == NULL) {
568 sg_list->count = i;
569 zfcp_sg_list_free(sg_list);
570 retval = -ENOMEM;
571 goto out;
572 }
573 zfcp_address_to_sg(address, sg);
574 size -= sg->length;
575 }
576
577 out:
578 return retval;
579 }
580
581
582 /**
583 * zfcp_sg_list_free - free memory of a scatter-gather list
584 * @sg_list: structure describing a scatter-gather list
585 *
586 * Memory for each element in the scatter-gather list is freed.
587 * Finally sg_list->sg is freed itself and sg_list->count is reset.
588 */
589 static void
590 zfcp_sg_list_free(struct zfcp_sg_list *sg_list)
591 {
592 struct scatterlist *sg;
593 unsigned int i;
594
595 BUG_ON(sg_list == NULL);
596
597 for (i = 0, sg = sg_list->sg; i < sg_list->count; i++, sg++)
598 free_page((unsigned long) zfcp_sg_to_address(sg));
599
600 sg_list->count = 0;
601 kfree(sg_list->sg);
602 }
603
604 /**
605 * zfcp_sg_size - determine size of a scatter-gather list
606 * @sg: array of (struct scatterlist)
607 * @sg_count: elements in array
608 * Return: size of entire scatter-gather list
609 */
610 static size_t zfcp_sg_size(struct scatterlist *sg, unsigned int sg_count)
611 {
612 unsigned int i;
613 struct scatterlist *p;
614 size_t size;
615
616 size = 0;
617 for (i = 0, p = sg; i < sg_count; i++, p++) {
618 BUG_ON(p == NULL);
619 size += p->length;
620 }
621
622 return size;
623 }
624
625
626 /**
627 * zfcp_sg_list_copy_from_user -copy data from user space to scatter-gather list
628 * @sg_list: structure describing a scatter-gather list
629 * @user_buffer: pointer to buffer in user space
630 * @size: number of bytes to be copied
631 * Return: 0 on success, -EFAULT if copy_from_user fails.
632 */
633 static int
634 zfcp_sg_list_copy_from_user(struct zfcp_sg_list *sg_list,
635 void __user *user_buffer,
636 size_t size)
637 {
638 struct scatterlist *sg;
639 unsigned int length;
640 void *zfcp_buffer;
641 int retval = 0;
642
643 BUG_ON(sg_list == NULL);
644
645 if (zfcp_sg_size(sg_list->sg, sg_list->count) < size)
646 return -EFAULT;
647
648 for (sg = sg_list->sg; size > 0; sg++) {
649 length = min((unsigned int)size, sg->length);
650 zfcp_buffer = zfcp_sg_to_address(sg);
651 if (copy_from_user(zfcp_buffer, user_buffer, length)) {
652 retval = -EFAULT;
653 goto out;
654 }
655 user_buffer += length;
656 size -= length;
657 }
658
659 out:
660 return retval;
661 }
662
663
664 /**
665 * zfcp_sg_list_copy_to_user - copy data from scatter-gather list to user space
666 * @user_buffer: pointer to buffer in user space
667 * @sg_list: structure describing a scatter-gather list
668 * @size: number of bytes to be copied
669 * Return: 0 on success, -EFAULT if copy_to_user fails
670 */
671 static int
672 zfcp_sg_list_copy_to_user(void __user *user_buffer,
673 struct zfcp_sg_list *sg_list,
674 size_t size)
675 {
676 struct scatterlist *sg;
677 unsigned int length;
678 void *zfcp_buffer;
679 int retval = 0;
680
681 BUG_ON(sg_list == NULL);
682
683 if (zfcp_sg_size(sg_list->sg, sg_list->count) < size)
684 return -EFAULT;
685
686 for (sg = sg_list->sg; size > 0; sg++) {
687 length = min((unsigned int) size, sg->length);
688 zfcp_buffer = zfcp_sg_to_address(sg);
689 if (copy_to_user(user_buffer, zfcp_buffer, length)) {
690 retval = -EFAULT;
691 goto out;
692 }
693 user_buffer += length;
694 size -= length;
695 }
696
697 out:
698 return retval;
699 }
700
701
702 #undef ZFCP_LOG_AREA
703
704 /****************************************************************/
705 /****** Functions for configuration/set-up of structures ********/
706 /****************************************************************/
707
708 #define ZFCP_LOG_AREA ZFCP_LOG_AREA_CONFIG
709
710 /**
711 * zfcp_get_unit_by_lun - find unit in unit list of port by FCP LUN
712 * @port: pointer to port to search for unit
713 * @fcp_lun: FCP LUN to search for
714 * Traverse list of all units of a port and return pointer to a unit
715 * with the given FCP LUN.
716 */
717 struct zfcp_unit *
718 zfcp_get_unit_by_lun(struct zfcp_port *port, fcp_lun_t fcp_lun)
719 {
720 struct zfcp_unit *unit;
721 int found = 0;
722
723 list_for_each_entry(unit, &port->unit_list_head, list) {
724 if ((unit->fcp_lun == fcp_lun) &&
725 !atomic_test_mask(ZFCP_STATUS_COMMON_REMOVE, &unit->status))
726 {
727 found = 1;
728 break;
729 }
730 }
731 return found ? unit : NULL;
732 }
733
734 /**
735 * zfcp_get_port_by_wwpn - find port in port list of adapter by wwpn
736 * @adapter: pointer to adapter to search for port
737 * @wwpn: wwpn to search for
738 * Traverse list of all ports of an adapter and return pointer to a port
739 * with the given wwpn.
740 */
741 struct zfcp_port *
742 zfcp_get_port_by_wwpn(struct zfcp_adapter *adapter, wwn_t wwpn)
743 {
744 struct zfcp_port *port;
745 int found = 0;
746
747 list_for_each_entry(port, &adapter->port_list_head, list) {
748 if ((port->wwpn == wwpn) &&
749 !(atomic_read(&port->status) &
750 (ZFCP_STATUS_PORT_NO_WWPN | ZFCP_STATUS_COMMON_REMOVE))) {
751 found = 1;
752 break;
753 }
754 }
755 return found ? port : NULL;
756 }
757
758 /**
759 * zfcp_get_port_by_did - find port in port list of adapter by d_id
760 * @adapter: pointer to adapter to search for port
761 * @d_id: d_id to search for
762 * Traverse list of all ports of an adapter and return pointer to a port
763 * with the given d_id.
764 */
765 struct zfcp_port *
766 zfcp_get_port_by_did(struct zfcp_adapter *adapter, u32 d_id)
767 {
768 struct zfcp_port *port;
769 int found = 0;
770
771 list_for_each_entry(port, &adapter->port_list_head, list) {
772 if ((port->d_id == d_id) &&
773 !atomic_test_mask(ZFCP_STATUS_COMMON_REMOVE, &port->status))
774 {
775 found = 1;
776 break;
777 }
778 }
779 return found ? port : NULL;
780 }
781
782 /**
783 * zfcp_get_adapter_by_busid - find adpater in adapter list by bus_id
784 * @bus_id: bus_id to search for
785 * Traverse list of all adapters and return pointer to an adapter
786 * with the given bus_id.
787 */
788 struct zfcp_adapter *
789 zfcp_get_adapter_by_busid(char *bus_id)
790 {
791 struct zfcp_adapter *adapter;
792 int found = 0;
793
794 list_for_each_entry(adapter, &zfcp_data.adapter_list_head, list) {
795 if ((strncmp(bus_id, zfcp_get_busid_by_adapter(adapter),
796 BUS_ID_SIZE) == 0) &&
797 !atomic_test_mask(ZFCP_STATUS_COMMON_REMOVE,
798 &adapter->status)){
799 found = 1;
800 break;
801 }
802 }
803 return found ? adapter : NULL;
804 }
805
806 /**
807 * zfcp_unit_enqueue - enqueue unit to unit list of a port.
808 * @port: pointer to port where unit is added
809 * @fcp_lun: FCP LUN of unit to be enqueued
810 * Return: pointer to enqueued unit on success, NULL on error
811 * Locks: config_sema must be held to serialize changes to the unit list
812 *
813 * Sets up some unit internal structures and creates sysfs entry.
814 */
815 struct zfcp_unit *
816 zfcp_unit_enqueue(struct zfcp_port *port, fcp_lun_t fcp_lun)
817 {
818 struct zfcp_unit *unit, *tmp_unit;
819 unsigned int scsi_lun;
820 int found;
821
822 /*
823 * check that there is no unit with this FCP_LUN already in list
824 * and enqueue it.
825 * Note: Unlike for the adapter and the port, this is an error
826 */
827 read_lock_irq(&zfcp_data.config_lock);
828 unit = zfcp_get_unit_by_lun(port, fcp_lun);
829 read_unlock_irq(&zfcp_data.config_lock);
830 if (unit)
831 return NULL;
832
833 unit = kzalloc(sizeof (struct zfcp_unit), GFP_KERNEL);
834 if (!unit)
835 return NULL;
836
837 /* initialise reference count stuff */
838 atomic_set(&unit->refcount, 0);
839 init_waitqueue_head(&unit->remove_wq);
840
841 unit->port = port;
842 unit->fcp_lun = fcp_lun;
843
844 /* setup for sysfs registration */
845 snprintf(unit->sysfs_device.bus_id, BUS_ID_SIZE, "0x%016llx", fcp_lun);
846 unit->sysfs_device.parent = &port->sysfs_device;
847 unit->sysfs_device.release = zfcp_sysfs_unit_release;
848 dev_set_drvdata(&unit->sysfs_device, unit);
849
850 init_waitqueue_head(&unit->scsi_scan_wq);
851
852 /* mark unit unusable as long as sysfs registration is not complete */
853 atomic_set_mask(ZFCP_STATUS_COMMON_REMOVE, &unit->status);
854
855 if (device_register(&unit->sysfs_device)) {
856 kfree(unit);
857 return NULL;
858 }
859
860 if (zfcp_sysfs_unit_create_files(&unit->sysfs_device)) {
861 device_unregister(&unit->sysfs_device);
862 return NULL;
863 }
864
865 zfcp_unit_get(unit);
866
867 scsi_lun = 0;
868 found = 0;
869 write_lock_irq(&zfcp_data.config_lock);
870 list_for_each_entry(tmp_unit, &port->unit_list_head, list) {
871 if (tmp_unit->scsi_lun != scsi_lun) {
872 found = 1;
873 break;
874 }
875 scsi_lun++;
876 }
877 unit->scsi_lun = scsi_lun;
878 if (found)
879 list_add_tail(&unit->list, &tmp_unit->list);
880 else
881 list_add_tail(&unit->list, &port->unit_list_head);
882 atomic_clear_mask(ZFCP_STATUS_COMMON_REMOVE, &unit->status);
883 atomic_set_mask(ZFCP_STATUS_COMMON_RUNNING, &unit->status);
884 write_unlock_irq(&zfcp_data.config_lock);
885
886 port->units++;
887 zfcp_port_get(port);
888
889 return unit;
890 }
891
892 void
893 zfcp_unit_dequeue(struct zfcp_unit *unit)
894 {
895 zfcp_unit_wait(unit);
896 write_lock_irq(&zfcp_data.config_lock);
897 list_del(&unit->list);
898 write_unlock_irq(&zfcp_data.config_lock);
899 unit->port->units--;
900 zfcp_port_put(unit->port);
901 zfcp_sysfs_unit_remove_files(&unit->sysfs_device);
902 device_unregister(&unit->sysfs_device);
903 }
904
905 /*
906 * Allocates a combined QTCB/fsf_req buffer for erp actions and fcp/SCSI
907 * commands.
908 * It also genrates fcp-nameserver request/response buffer and unsolicited
909 * status read fsf_req buffers.
910 *
911 * locks: must only be called with zfcp_data.config_sema taken
912 */
913 static int
914 zfcp_allocate_low_mem_buffers(struct zfcp_adapter *adapter)
915 {
916 adapter->pool.fsf_req_erp =
917 mempool_create_slab_pool(ZFCP_POOL_FSF_REQ_ERP_NR,
918 zfcp_data.fsf_req_qtcb_cache);
919 if (!adapter->pool.fsf_req_erp)
920 return -ENOMEM;
921
922 adapter->pool.fsf_req_scsi =
923 mempool_create_slab_pool(ZFCP_POOL_FSF_REQ_SCSI_NR,
924 zfcp_data.fsf_req_qtcb_cache);
925 if (!adapter->pool.fsf_req_scsi)
926 return -ENOMEM;
927
928 adapter->pool.fsf_req_abort =
929 mempool_create_slab_pool(ZFCP_POOL_FSF_REQ_ABORT_NR,
930 zfcp_data.fsf_req_qtcb_cache);
931 if (!adapter->pool.fsf_req_abort)
932 return -ENOMEM;
933
934 adapter->pool.fsf_req_status_read =
935 mempool_create_kmalloc_pool(ZFCP_POOL_STATUS_READ_NR,
936 sizeof(struct zfcp_fsf_req));
937 if (!adapter->pool.fsf_req_status_read)
938 return -ENOMEM;
939
940 adapter->pool.data_status_read =
941 mempool_create_slab_pool(ZFCP_POOL_STATUS_READ_NR,
942 zfcp_data.sr_buffer_cache);
943 if (!adapter->pool.data_status_read)
944 return -ENOMEM;
945
946 adapter->pool.data_gid_pn =
947 mempool_create_slab_pool(ZFCP_POOL_DATA_GID_PN_NR,
948 zfcp_data.gid_pn_cache);
949 if (!adapter->pool.data_gid_pn)
950 return -ENOMEM;
951
952 return 0;
953 }
954
955 /**
956 * zfcp_free_low_mem_buffers - free memory pools of an adapter
957 * @adapter: pointer to zfcp_adapter for which memory pools should be freed
958 * locking: zfcp_data.config_sema must be held
959 */
960 static void
961 zfcp_free_low_mem_buffers(struct zfcp_adapter *adapter)
962 {
963 if (adapter->pool.fsf_req_erp)
964 mempool_destroy(adapter->pool.fsf_req_erp);
965 if (adapter->pool.fsf_req_scsi)
966 mempool_destroy(adapter->pool.fsf_req_scsi);
967 if (adapter->pool.fsf_req_abort)
968 mempool_destroy(adapter->pool.fsf_req_abort);
969 if (adapter->pool.fsf_req_status_read)
970 mempool_destroy(adapter->pool.fsf_req_status_read);
971 if (adapter->pool.data_status_read)
972 mempool_destroy(adapter->pool.data_status_read);
973 if (adapter->pool.data_gid_pn)
974 mempool_destroy(adapter->pool.data_gid_pn);
975 }
976
977 static void zfcp_dummy_release(struct device *dev)
978 {
979 return;
980 }
981
982 /*
983 * Enqueues an adapter at the end of the adapter list in the driver data.
984 * All adapter internal structures are set up.
985 * Proc-fs entries are also created.
986 *
987 * returns: 0 if a new adapter was successfully enqueued
988 * ZFCP_KNOWN if an adapter with this devno was already present
989 * -ENOMEM if alloc failed
990 * locks: config_sema must be held to serialise changes to the adapter list
991 */
992 struct zfcp_adapter *
993 zfcp_adapter_enqueue(struct ccw_device *ccw_device)
994 {
995 int retval = 0;
996 struct zfcp_adapter *adapter;
997
998 /*
999 * Note: It is safe to release the list_lock, as any list changes
1000 * are protected by the config_sema, which must be held to get here
1001 */
1002
1003 /* try to allocate new adapter data structure (zeroed) */
1004 adapter = kzalloc(sizeof (struct zfcp_adapter), GFP_KERNEL);
1005 if (!adapter) {
1006 ZFCP_LOG_INFO("error: allocation of base adapter "
1007 "structure failed\n");
1008 goto out;
1009 }
1010
1011 ccw_device->handler = NULL;
1012
1013 /* save ccw_device pointer */
1014 adapter->ccw_device = ccw_device;
1015
1016 retval = zfcp_qdio_allocate_queues(adapter);
1017 if (retval)
1018 goto queues_alloc_failed;
1019
1020 retval = zfcp_qdio_allocate(adapter);
1021 if (retval)
1022 goto qdio_allocate_failed;
1023
1024 retval = zfcp_allocate_low_mem_buffers(adapter);
1025 if (retval) {
1026 ZFCP_LOG_INFO("error: pool allocation failed\n");
1027 goto failed_low_mem_buffers;
1028 }
1029
1030 /* initialise reference count stuff */
1031 atomic_set(&adapter->refcount, 0);
1032 init_waitqueue_head(&adapter->remove_wq);
1033
1034 /* initialise list of ports */
1035 INIT_LIST_HEAD(&adapter->port_list_head);
1036
1037 /* initialise list of ports to be removed */
1038 INIT_LIST_HEAD(&adapter->port_remove_lh);
1039
1040 /* initialize list of fsf requests */
1041 spin_lock_init(&adapter->req_list_lock);
1042 retval = zfcp_reqlist_alloc(adapter);
1043 if (retval) {
1044 ZFCP_LOG_INFO("request list initialization failed\n");
1045 goto failed_low_mem_buffers;
1046 }
1047
1048 /* initialize debug locks */
1049
1050 spin_lock_init(&adapter->erp_dbf_lock);
1051 spin_lock_init(&adapter->hba_dbf_lock);
1052 spin_lock_init(&adapter->san_dbf_lock);
1053 spin_lock_init(&adapter->scsi_dbf_lock);
1054
1055 /* initialize error recovery stuff */
1056
1057 rwlock_init(&adapter->erp_lock);
1058 sema_init(&adapter->erp_ready_sem, 0);
1059 INIT_LIST_HEAD(&adapter->erp_ready_head);
1060 INIT_LIST_HEAD(&adapter->erp_running_head);
1061
1062 /* initialize abort lock */
1063 rwlock_init(&adapter->abort_lock);
1064
1065 /* initialise some erp stuff */
1066 init_waitqueue_head(&adapter->erp_thread_wqh);
1067 init_waitqueue_head(&adapter->erp_done_wqh);
1068
1069 /* initialize lock of associated request queue */
1070 rwlock_init(&adapter->request_queue.queue_lock);
1071
1072 /* mark adapter unusable as long as sysfs registration is not complete */
1073 atomic_set_mask(ZFCP_STATUS_COMMON_REMOVE, &adapter->status);
1074
1075 adapter->ccw_device = ccw_device;
1076 dev_set_drvdata(&ccw_device->dev, adapter);
1077
1078 if (zfcp_sysfs_adapter_create_files(&ccw_device->dev))
1079 goto sysfs_failed;
1080
1081 adapter->generic_services.parent = &adapter->ccw_device->dev;
1082 adapter->generic_services.release = zfcp_dummy_release;
1083 snprintf(adapter->generic_services.bus_id, BUS_ID_SIZE,
1084 "generic_services");
1085
1086 if (device_register(&adapter->generic_services))
1087 goto generic_services_failed;
1088
1089 /* put allocated adapter at list tail */
1090 write_lock_irq(&zfcp_data.config_lock);
1091 atomic_clear_mask(ZFCP_STATUS_COMMON_REMOVE, &adapter->status);
1092 list_add_tail(&adapter->list, &zfcp_data.adapter_list_head);
1093 write_unlock_irq(&zfcp_data.config_lock);
1094
1095 zfcp_data.adapters++;
1096
1097 goto out;
1098
1099 generic_services_failed:
1100 zfcp_sysfs_adapter_remove_files(&adapter->ccw_device->dev);
1101 sysfs_failed:
1102 dev_set_drvdata(&ccw_device->dev, NULL);
1103 zfcp_reqlist_free(adapter);
1104 failed_low_mem_buffers:
1105 zfcp_free_low_mem_buffers(adapter);
1106 if (qdio_free(ccw_device) != 0)
1107 ZFCP_LOG_NORMAL("bug: qdio_free for adapter %s failed\n",
1108 zfcp_get_busid_by_adapter(adapter));
1109 qdio_allocate_failed:
1110 zfcp_qdio_free_queues(adapter);
1111 queues_alloc_failed:
1112 kfree(adapter);
1113 adapter = NULL;
1114 out:
1115 return adapter;
1116 }
1117
1118 /*
1119 * returns: 0 - struct zfcp_adapter data structure successfully removed
1120 * !0 - struct zfcp_adapter data structure could not be removed
1121 * (e.g. still used)
1122 * locks: adapter list write lock is assumed to be held by caller
1123 */
1124 void
1125 zfcp_adapter_dequeue(struct zfcp_adapter *adapter)
1126 {
1127 int retval = 0;
1128 unsigned long flags;
1129
1130 device_unregister(&adapter->generic_services);
1131 zfcp_sysfs_adapter_remove_files(&adapter->ccw_device->dev);
1132 dev_set_drvdata(&adapter->ccw_device->dev, NULL);
1133 /* sanity check: no pending FSF requests */
1134 spin_lock_irqsave(&adapter->req_list_lock, flags);
1135 retval = zfcp_reqlist_isempty(adapter);
1136 spin_unlock_irqrestore(&adapter->req_list_lock, flags);
1137 if (!retval) {
1138 ZFCP_LOG_NORMAL("bug: adapter %s (%p) still in use, "
1139 "%i requests outstanding\n",
1140 zfcp_get_busid_by_adapter(adapter), adapter,
1141 atomic_read(&adapter->reqs_active));
1142 retval = -EBUSY;
1143 goto out;
1144 }
1145
1146 /* remove specified adapter data structure from list */
1147 write_lock_irq(&zfcp_data.config_lock);
1148 list_del(&adapter->list);
1149 write_unlock_irq(&zfcp_data.config_lock);
1150
1151 /* decrease number of adapters in list */
1152 zfcp_data.adapters--;
1153
1154 ZFCP_LOG_TRACE("adapter %s (%p) removed from list, "
1155 "%i adapters still in list\n",
1156 zfcp_get_busid_by_adapter(adapter),
1157 adapter, zfcp_data.adapters);
1158
1159 retval = qdio_free(adapter->ccw_device);
1160 if (retval)
1161 ZFCP_LOG_NORMAL("bug: qdio_free for adapter %s failed\n",
1162 zfcp_get_busid_by_adapter(adapter));
1163
1164 zfcp_free_low_mem_buffers(adapter);
1165 /* free memory of adapter data structure and queues */
1166 zfcp_qdio_free_queues(adapter);
1167 zfcp_reqlist_free(adapter);
1168 kfree(adapter->fc_stats);
1169 kfree(adapter->stats_reset_data);
1170 ZFCP_LOG_TRACE("freeing adapter structure\n");
1171 kfree(adapter);
1172 out:
1173 return;
1174 }
1175
1176 /**
1177 * zfcp_port_enqueue - enqueue port to port list of adapter
1178 * @adapter: adapter where remote port is added
1179 * @wwpn: WWPN of the remote port to be enqueued
1180 * @status: initial status for the port
1181 * @d_id: destination id of the remote port to be enqueued
1182 * Return: pointer to enqueued port on success, NULL on error
1183 * Locks: config_sema must be held to serialize changes to the port list
1184 *
1185 * All port internal structures are set up and the sysfs entry is generated.
1186 * d_id is used to enqueue ports with a well known address like the Directory
1187 * Service for nameserver lookup.
1188 */
1189 struct zfcp_port *
1190 zfcp_port_enqueue(struct zfcp_adapter *adapter, wwn_t wwpn, u32 status,
1191 u32 d_id)
1192 {
1193 struct zfcp_port *port;
1194 int check_wwpn;
1195
1196 check_wwpn = !(status & ZFCP_STATUS_PORT_NO_WWPN);
1197 /*
1198 * check that there is no port with this WWPN already in list
1199 */
1200 if (check_wwpn) {
1201 read_lock_irq(&zfcp_data.config_lock);
1202 port = zfcp_get_port_by_wwpn(adapter, wwpn);
1203 read_unlock_irq(&zfcp_data.config_lock);
1204 if (port)
1205 return NULL;
1206 }
1207
1208 port = kzalloc(sizeof (struct zfcp_port), GFP_KERNEL);
1209 if (!port)
1210 return NULL;
1211
1212 /* initialise reference count stuff */
1213 atomic_set(&port->refcount, 0);
1214 init_waitqueue_head(&port->remove_wq);
1215
1216 INIT_LIST_HEAD(&port->unit_list_head);
1217 INIT_LIST_HEAD(&port->unit_remove_lh);
1218
1219 port->adapter = adapter;
1220
1221 if (check_wwpn)
1222 port->wwpn = wwpn;
1223
1224 atomic_set_mask(status, &port->status);
1225
1226 /* setup for sysfs registration */
1227 if (status & ZFCP_STATUS_PORT_WKA) {
1228 switch (d_id) {
1229 case ZFCP_DID_DIRECTORY_SERVICE:
1230 snprintf(port->sysfs_device.bus_id, BUS_ID_SIZE,
1231 "directory");
1232 break;
1233 case ZFCP_DID_MANAGEMENT_SERVICE:
1234 snprintf(port->sysfs_device.bus_id, BUS_ID_SIZE,
1235 "management");
1236 break;
1237 case ZFCP_DID_KEY_DISTRIBUTION_SERVICE:
1238 snprintf(port->sysfs_device.bus_id, BUS_ID_SIZE,
1239 "key_distribution");
1240 break;
1241 case ZFCP_DID_ALIAS_SERVICE:
1242 snprintf(port->sysfs_device.bus_id, BUS_ID_SIZE,
1243 "alias");
1244 break;
1245 case ZFCP_DID_TIME_SERVICE:
1246 snprintf(port->sysfs_device.bus_id, BUS_ID_SIZE,
1247 "time");
1248 break;
1249 default:
1250 kfree(port);
1251 return NULL;
1252 }
1253 port->d_id = d_id;
1254 port->sysfs_device.parent = &adapter->generic_services;
1255 } else {
1256 snprintf(port->sysfs_device.bus_id,
1257 BUS_ID_SIZE, "0x%016llx", wwpn);
1258 port->sysfs_device.parent = &adapter->ccw_device->dev;
1259 }
1260 port->sysfs_device.release = zfcp_sysfs_port_release;
1261 dev_set_drvdata(&port->sysfs_device, port);
1262
1263 /* mark port unusable as long as sysfs registration is not complete */
1264 atomic_set_mask(ZFCP_STATUS_COMMON_REMOVE, &port->status);
1265
1266 if (device_register(&port->sysfs_device)) {
1267 kfree(port);
1268 return NULL;
1269 }
1270
1271 if (zfcp_sysfs_port_create_files(&port->sysfs_device, status)) {
1272 device_unregister(&port->sysfs_device);
1273 return NULL;
1274 }
1275
1276 zfcp_port_get(port);
1277
1278 write_lock_irq(&zfcp_data.config_lock);
1279 list_add_tail(&port->list, &adapter->port_list_head);
1280 atomic_clear_mask(ZFCP_STATUS_COMMON_REMOVE, &port->status);
1281 atomic_set_mask(ZFCP_STATUS_COMMON_RUNNING, &port->status);
1282 if (d_id == ZFCP_DID_DIRECTORY_SERVICE)
1283 if (!adapter->nameserver_port)
1284 adapter->nameserver_port = port;
1285 adapter->ports++;
1286 write_unlock_irq(&zfcp_data.config_lock);
1287
1288 zfcp_adapter_get(adapter);
1289
1290 return port;
1291 }
1292
1293 void
1294 zfcp_port_dequeue(struct zfcp_port *port)
1295 {
1296 zfcp_port_wait(port);
1297 write_lock_irq(&zfcp_data.config_lock);
1298 list_del(&port->list);
1299 port->adapter->ports--;
1300 write_unlock_irq(&zfcp_data.config_lock);
1301 if (port->rport)
1302 fc_remote_port_delete(port->rport);
1303 port->rport = NULL;
1304 zfcp_adapter_put(port->adapter);
1305 zfcp_sysfs_port_remove_files(&port->sysfs_device,
1306 atomic_read(&port->status));
1307 device_unregister(&port->sysfs_device);
1308 }
1309
1310 /* Enqueues a nameserver port */
1311 int
1312 zfcp_nameserver_enqueue(struct zfcp_adapter *adapter)
1313 {
1314 struct zfcp_port *port;
1315
1316 port = zfcp_port_enqueue(adapter, 0, ZFCP_STATUS_PORT_WKA,
1317 ZFCP_DID_DIRECTORY_SERVICE);
1318 if (!port) {
1319 ZFCP_LOG_INFO("error: enqueue of nameserver port for "
1320 "adapter %s failed\n",
1321 zfcp_get_busid_by_adapter(adapter));
1322 return -ENXIO;
1323 }
1324 zfcp_port_put(port);
1325
1326 return 0;
1327 }
1328
1329 #undef ZFCP_LOG_AREA
1330
1331 /****************************************************************/
1332 /******* Fibre Channel Standard related Functions **************/
1333 /****************************************************************/
1334
1335 #define ZFCP_LOG_AREA ZFCP_LOG_AREA_FC
1336
1337 static void
1338 zfcp_fsf_incoming_els_rscn(struct zfcp_adapter *adapter,
1339 struct fsf_status_read_buffer *status_buffer)
1340 {
1341 struct fcp_rscn_head *fcp_rscn_head;
1342 struct fcp_rscn_element *fcp_rscn_element;
1343 struct zfcp_port *port;
1344 u16 i;
1345 u16 no_entries;
1346 u32 range_mask;
1347 unsigned long flags;
1348
1349 fcp_rscn_head = (struct fcp_rscn_head *) status_buffer->payload;
1350 fcp_rscn_element = (struct fcp_rscn_element *) status_buffer->payload;
1351
1352 /* see FC-FS */
1353 no_entries = (fcp_rscn_head->payload_len / 4);
1354
1355 for (i = 1; i < no_entries; i++) {
1356 /* skip head and start with 1st element */
1357 fcp_rscn_element++;
1358 switch (fcp_rscn_element->addr_format) {
1359 case ZFCP_PORT_ADDRESS:
1360 range_mask = ZFCP_PORTS_RANGE_PORT;
1361 break;
1362 case ZFCP_AREA_ADDRESS:
1363 range_mask = ZFCP_PORTS_RANGE_AREA;
1364 break;
1365 case ZFCP_DOMAIN_ADDRESS:
1366 range_mask = ZFCP_PORTS_RANGE_DOMAIN;
1367 break;
1368 case ZFCP_FABRIC_ADDRESS:
1369 range_mask = ZFCP_PORTS_RANGE_FABRIC;
1370 break;
1371 default:
1372 ZFCP_LOG_INFO("incoming RSCN with unknown "
1373 "address format\n");
1374 continue;
1375 }
1376 read_lock_irqsave(&zfcp_data.config_lock, flags);
1377 list_for_each_entry(port, &adapter->port_list_head, list) {
1378 if (atomic_test_mask
1379 (ZFCP_STATUS_PORT_WKA, &port->status))
1380 continue;
1381 /* Do we know this port? If not skip it. */
1382 if (!atomic_test_mask
1383 (ZFCP_STATUS_PORT_DID_DID, &port->status)) {
1384 ZFCP_LOG_INFO("incoming RSCN, trying to open "
1385 "port 0x%016Lx\n", port->wwpn);
1386 zfcp_erp_port_reopen(port,
1387 ZFCP_STATUS_COMMON_ERP_FAILED);
1388 continue;
1389 }
1390
1391 /*
1392 * FIXME: race: d_id might being invalidated
1393 * (...DID_DID reset)
1394 */
1395 if ((port->d_id & range_mask)
1396 == (fcp_rscn_element->nport_did & range_mask)) {
1397 ZFCP_LOG_TRACE("reopen did 0x%08x\n",
1398 fcp_rscn_element->nport_did);
1399 /*
1400 * Unfortunately, an RSCN does not specify the
1401 * type of change a target underwent. We assume
1402 * that it makes sense to reopen the link.
1403 * FIXME: Shall we try to find out more about
1404 * the target and link state before closing it?
1405 * How to accomplish this? (nameserver?)
1406 * Where would such code be put in?
1407 * (inside or outside erp)
1408 */
1409 ZFCP_LOG_INFO("incoming RSCN, trying to open "
1410 "port 0x%016Lx\n", port->wwpn);
1411 zfcp_test_link(port);
1412 }
1413 }
1414 read_unlock_irqrestore(&zfcp_data.config_lock, flags);
1415 }
1416 }
1417
1418 static void
1419 zfcp_fsf_incoming_els_plogi(struct zfcp_adapter *adapter,
1420 struct fsf_status_read_buffer *status_buffer)
1421 {
1422 struct fsf_plogi *els_plogi;
1423 struct zfcp_port *port;
1424 unsigned long flags;
1425
1426 els_plogi = (struct fsf_plogi *) status_buffer->payload;
1427 read_lock_irqsave(&zfcp_data.config_lock, flags);
1428 list_for_each_entry(port, &adapter->port_list_head, list) {
1429 if (port->wwpn == (*(wwn_t *) &els_plogi->serv_param.wwpn))
1430 break;
1431 }
1432 read_unlock_irqrestore(&zfcp_data.config_lock, flags);
1433
1434 if (!port || (port->wwpn != (*(wwn_t *) &els_plogi->serv_param.wwpn))) {
1435 ZFCP_LOG_DEBUG("ignored incoming PLOGI for nonexisting port "
1436 "with d_id 0x%06x on adapter %s\n",
1437 status_buffer->d_id,
1438 zfcp_get_busid_by_adapter(adapter));
1439 } else {
1440 zfcp_erp_port_forced_reopen(port, 0);
1441 }
1442 }
1443
1444 static void
1445 zfcp_fsf_incoming_els_logo(struct zfcp_adapter *adapter,
1446 struct fsf_status_read_buffer *status_buffer)
1447 {
1448 struct fcp_logo *els_logo = (struct fcp_logo *) status_buffer->payload;
1449 struct zfcp_port *port;
1450 unsigned long flags;
1451
1452 read_lock_irqsave(&zfcp_data.config_lock, flags);
1453 list_for_each_entry(port, &adapter->port_list_head, list) {
1454 if (port->wwpn == els_logo->nport_wwpn)
1455 break;
1456 }
1457 read_unlock_irqrestore(&zfcp_data.config_lock, flags);
1458
1459 if (!port || (port->wwpn != els_logo->nport_wwpn)) {
1460 ZFCP_LOG_DEBUG("ignored incoming LOGO for nonexisting port "
1461 "with d_id 0x%06x on adapter %s\n",
1462 status_buffer->d_id,
1463 zfcp_get_busid_by_adapter(adapter));
1464 } else {
1465 zfcp_erp_port_forced_reopen(port, 0);
1466 }
1467 }
1468
1469 static void
1470 zfcp_fsf_incoming_els_unknown(struct zfcp_adapter *adapter,
1471 struct fsf_status_read_buffer *status_buffer)
1472 {
1473 ZFCP_LOG_NORMAL("warning: unknown incoming ELS 0x%08x "
1474 "for adapter %s\n", *(u32 *) (status_buffer->payload),
1475 zfcp_get_busid_by_adapter(adapter));
1476
1477 }
1478
1479 void
1480 zfcp_fsf_incoming_els(struct zfcp_fsf_req *fsf_req)
1481 {
1482 struct fsf_status_read_buffer *status_buffer;
1483 u32 els_type;
1484 struct zfcp_adapter *adapter;
1485
1486 status_buffer = (struct fsf_status_read_buffer *) fsf_req->data;
1487 els_type = *(u32 *) (status_buffer->payload);
1488 adapter = fsf_req->adapter;
1489
1490 zfcp_san_dbf_event_incoming_els(fsf_req);
1491 if (els_type == LS_PLOGI)
1492 zfcp_fsf_incoming_els_plogi(adapter, status_buffer);
1493 else if (els_type == LS_LOGO)
1494 zfcp_fsf_incoming_els_logo(adapter, status_buffer);
1495 else if ((els_type & 0xffff0000) == LS_RSCN)
1496 /* we are only concerned with the command, not the length */
1497 zfcp_fsf_incoming_els_rscn(adapter, status_buffer);
1498 else
1499 zfcp_fsf_incoming_els_unknown(adapter, status_buffer);
1500 }
1501
1502
1503 /**
1504 * zfcp_gid_pn_buffers_alloc - allocate buffers for GID_PN nameserver request
1505 * @gid_pn: pointer to return pointer to struct zfcp_gid_pn_data
1506 * @pool: pointer to mempool_t if non-null memory pool is used for allocation
1507 */
1508 static int
1509 zfcp_gid_pn_buffers_alloc(struct zfcp_gid_pn_data **gid_pn, mempool_t *pool)
1510 {
1511 struct zfcp_gid_pn_data *data;
1512
1513 if (pool != NULL) {
1514 data = mempool_alloc(pool, GFP_ATOMIC);
1515 if (likely(data != NULL)) {
1516 data->ct.pool = pool;
1517 }
1518 } else {
1519 data = kmalloc(sizeof(struct zfcp_gid_pn_data), GFP_ATOMIC);
1520 }
1521
1522 if (NULL == data)
1523 return -ENOMEM;
1524
1525 memset(data, 0, sizeof(*data));
1526 data->ct.req = &data->req;
1527 data->ct.resp = &data->resp;
1528 data->ct.req_count = data->ct.resp_count = 1;
1529 zfcp_address_to_sg(&data->ct_iu_req, &data->req);
1530 zfcp_address_to_sg(&data->ct_iu_resp, &data->resp);
1531 data->req.length = sizeof(struct ct_iu_gid_pn_req);
1532 data->resp.length = sizeof(struct ct_iu_gid_pn_resp);
1533
1534 *gid_pn = data;
1535 return 0;
1536 }
1537
1538 /**
1539 * zfcp_gid_pn_buffers_free - free buffers for GID_PN nameserver request
1540 * @gid_pn: pointer to struct zfcp_gid_pn_data which has to be freed
1541 */
1542 static void
1543 zfcp_gid_pn_buffers_free(struct zfcp_gid_pn_data *gid_pn)
1544 {
1545 if ((gid_pn->ct.pool != 0))
1546 mempool_free(gid_pn, gid_pn->ct.pool);
1547 else
1548 kfree(gid_pn);
1549
1550 return;
1551 }
1552
1553 /**
1554 * zfcp_ns_gid_pn_request - initiate GID_PN nameserver request
1555 * @erp_action: pointer to zfcp_erp_action where GID_PN request is needed
1556 */
1557 int
1558 zfcp_ns_gid_pn_request(struct zfcp_erp_action *erp_action)
1559 {
1560 int ret;
1561 struct ct_iu_gid_pn_req *ct_iu_req;
1562 struct zfcp_gid_pn_data *gid_pn;
1563 struct zfcp_adapter *adapter = erp_action->adapter;
1564
1565 ret = zfcp_gid_pn_buffers_alloc(&gid_pn, adapter->pool.data_gid_pn);
1566 if (ret < 0) {
1567 ZFCP_LOG_INFO("error: buffer allocation for gid_pn nameserver "
1568 "request failed for adapter %s\n",
1569 zfcp_get_busid_by_adapter(adapter));
1570 goto out;
1571 }
1572
1573 /* setup nameserver request */
1574 ct_iu_req = zfcp_sg_to_address(gid_pn->ct.req);
1575 ct_iu_req->header.revision = ZFCP_CT_REVISION;
1576 ct_iu_req->header.gs_type = ZFCP_CT_DIRECTORY_SERVICE;
1577 ct_iu_req->header.gs_subtype = ZFCP_CT_NAME_SERVER;
1578 ct_iu_req->header.options = ZFCP_CT_SYNCHRONOUS;
1579 ct_iu_req->header.cmd_rsp_code = ZFCP_CT_GID_PN;
1580 ct_iu_req->header.max_res_size = ZFCP_CT_MAX_SIZE;
1581 ct_iu_req->wwpn = erp_action->port->wwpn;
1582
1583 /* setup parameters for send generic command */
1584 gid_pn->ct.port = adapter->nameserver_port;
1585 gid_pn->ct.handler = zfcp_ns_gid_pn_handler;
1586 gid_pn->ct.handler_data = (unsigned long) gid_pn;
1587 gid_pn->ct.timeout = ZFCP_NS_GID_PN_TIMEOUT;
1588 gid_pn->port = erp_action->port;
1589
1590 ret = zfcp_fsf_send_ct(&gid_pn->ct, adapter->pool.fsf_req_erp,
1591 erp_action);
1592 if (ret) {
1593 ZFCP_LOG_INFO("error: initiation of gid_pn nameserver request "
1594 "failed for adapter %s\n",
1595 zfcp_get_busid_by_adapter(adapter));
1596
1597 zfcp_gid_pn_buffers_free(gid_pn);
1598 }
1599
1600 out:
1601 return ret;
1602 }
1603
1604 /**
1605 * zfcp_ns_gid_pn_handler - handler for GID_PN nameserver request
1606 * @data: unsigned long, contains pointer to struct zfcp_gid_pn_data
1607 */
1608 static void zfcp_ns_gid_pn_handler(unsigned long data)
1609 {
1610 struct zfcp_port *port;
1611 struct zfcp_send_ct *ct;
1612 struct ct_iu_gid_pn_req *ct_iu_req;
1613 struct ct_iu_gid_pn_resp *ct_iu_resp;
1614 struct zfcp_gid_pn_data *gid_pn;
1615
1616
1617 gid_pn = (struct zfcp_gid_pn_data *) data;
1618 port = gid_pn->port;
1619 ct = &gid_pn->ct;
1620 ct_iu_req = zfcp_sg_to_address(ct->req);
1621 ct_iu_resp = zfcp_sg_to_address(ct->resp);
1622
1623 if (ct->status != 0)
1624 goto failed;
1625
1626 if (zfcp_check_ct_response(&ct_iu_resp->header)) {
1627 /* FIXME: do we need some specific erp entry points */
1628 atomic_set_mask(ZFCP_STATUS_PORT_INVALID_WWPN, &port->status);
1629 goto failed;
1630 }
1631 /* paranoia */
1632 if (ct_iu_req->wwpn != port->wwpn) {
1633 ZFCP_LOG_NORMAL("bug: wwpn 0x%016Lx returned by nameserver "
1634 "lookup does not match expected wwpn 0x%016Lx "
1635 "for adapter %s\n", ct_iu_req->wwpn, port->wwpn,
1636 zfcp_get_busid_by_port(port));
1637 goto mismatch;
1638 }
1639
1640 /* looks like a valid d_id */
1641 port->d_id = ct_iu_resp->d_id & ZFCP_DID_MASK;
1642 atomic_set_mask(ZFCP_STATUS_PORT_DID_DID, &port->status);
1643 ZFCP_LOG_DEBUG("adapter %s: wwpn=0x%016Lx ---> d_id=0x%06x\n",
1644 zfcp_get_busid_by_port(port), port->wwpn, port->d_id);
1645 goto out;
1646
1647 mismatch:
1648 ZFCP_LOG_DEBUG("CT IUs do not match:\n");
1649 ZFCP_HEX_DUMP(ZFCP_LOG_LEVEL_DEBUG, (char *) ct_iu_req,
1650 sizeof(struct ct_iu_gid_pn_req));
1651 ZFCP_HEX_DUMP(ZFCP_LOG_LEVEL_DEBUG, (char *) ct_iu_resp,
1652 sizeof(struct ct_iu_gid_pn_resp));
1653
1654 failed:
1655 ZFCP_LOG_NORMAL("warning: failed gid_pn nameserver request for wwpn "
1656 "0x%016Lx for adapter %s\n",
1657 port->wwpn, zfcp_get_busid_by_port(port));
1658 out:
1659 zfcp_gid_pn_buffers_free(gid_pn);
1660 return;
1661 }
1662
1663 /* reject CT_IU reason codes acc. to FC-GS-4 */
1664 static const struct zfcp_rc_entry zfcp_ct_rc[] = {
1665 {0x01, "invalid command code"},
1666 {0x02, "invalid version level"},
1667 {0x03, "logical error"},
1668 {0x04, "invalid CT_IU size"},
1669 {0x05, "logical busy"},
1670 {0x07, "protocol error"},
1671 {0x09, "unable to perform command request"},
1672 {0x0b, "command not supported"},
1673 {0x0d, "server not available"},
1674 {0x0e, "session could not be established"},
1675 {0xff, "vendor specific error"},
1676 {0, NULL},
1677 };
1678
1679 /* LS_RJT reason codes acc. to FC-FS */
1680 static const struct zfcp_rc_entry zfcp_ls_rjt_rc[] = {
1681 {0x01, "invalid LS_Command code"},
1682 {0x03, "logical error"},
1683 {0x05, "logical busy"},
1684 {0x07, "protocol error"},
1685 {0x09, "unable to perform command request"},
1686 {0x0b, "command not supported"},
1687 {0x0e, "command already in progress"},
1688 {0xff, "vendor specific error"},
1689 {0, NULL},
1690 };
1691
1692 /* reject reason codes according to FC-PH/FC-FS */
1693 static const struct zfcp_rc_entry zfcp_p_rjt_rc[] = {
1694 {0x01, "invalid D_ID"},
1695 {0x02, "invalid S_ID"},
1696 {0x03, "Nx_Port not available, temporary"},
1697 {0x04, "Nx_Port not available, permament"},
1698 {0x05, "class not supported"},
1699 {0x06, "delimiter usage error"},
1700 {0x07, "TYPE not supported"},
1701 {0x08, "invalid Link_Control"},
1702 {0x09, "invalid R_CTL field"},
1703 {0x0a, "invalid F_CTL field"},
1704 {0x0b, "invalid OX_ID"},
1705 {0x0c, "invalid RX_ID"},
1706 {0x0d, "invalid SEQ_ID"},
1707 {0x0e, "invalid DF_CTL"},
1708 {0x0f, "invalid SEQ_CNT"},
1709 {0x10, "invalid parameter field"},
1710 {0x11, "exchange error"},
1711 {0x12, "protocol error"},
1712 {0x13, "incorrect length"},
1713 {0x14, "unsupported ACK"},
1714 {0x15, "class of service not supported by entity at FFFFFE"},
1715 {0x16, "login required"},
1716 {0x17, "excessive sequences attempted"},
1717 {0x18, "unable to establish exchange"},
1718 {0x1a, "fabric path not available"},
1719 {0x1b, "invalid VC_ID (class 4)"},
1720 {0x1c, "invalid CS_CTL field"},
1721 {0x1d, "insufficient resources for VC (class 4)"},
1722 {0x1f, "invalid class of service"},
1723 {0x20, "preemption request rejected"},
1724 {0x21, "preemption not enabled"},
1725 {0x22, "multicast error"},
1726 {0x23, "multicast error terminate"},
1727 {0x24, "process login required"},
1728 {0xff, "vendor specific reject"},
1729 {0, NULL},
1730 };
1731
1732 /**
1733 * zfcp_rc_description - return description for given reaon code
1734 * @code: reason code
1735 * @rc_table: table of reason codes and descriptions
1736 */
1737 static const char *
1738 zfcp_rc_description(u8 code, const struct zfcp_rc_entry *rc_table)
1739 {
1740 const char *descr = "unknown reason code";
1741
1742 do {
1743 if (code == rc_table->code) {
1744 descr = rc_table->description;
1745 break;
1746 }
1747 rc_table++;
1748 } while (rc_table->code && rc_table->description);
1749
1750 return descr;
1751 }
1752
1753 /**
1754 * zfcp_check_ct_response - evaluate reason code for CT_IU
1755 * @rjt: response payload to an CT_IU request
1756 * Return: 0 for accept CT_IU, 1 for reject CT_IU or invlid response code
1757 */
1758 int
1759 zfcp_check_ct_response(struct ct_hdr *rjt)
1760 {
1761 if (rjt->cmd_rsp_code == ZFCP_CT_ACCEPT)
1762 return 0;
1763
1764 if (rjt->cmd_rsp_code != ZFCP_CT_REJECT) {
1765 ZFCP_LOG_NORMAL("error: invalid Generic Service command/"
1766 "response code (0x%04hx)\n",
1767 rjt->cmd_rsp_code);
1768 return 1;
1769 }
1770
1771 ZFCP_LOG_INFO("Generic Service command rejected\n");
1772 ZFCP_LOG_INFO("%s (0x%02x, 0x%02x, 0x%02x)\n",
1773 zfcp_rc_description(rjt->reason_code, zfcp_ct_rc),
1774 (u32) rjt->reason_code, (u32) rjt->reason_code_expl,
1775 (u32) rjt->vendor_unique);
1776
1777 return 1;
1778 }
1779
1780 /**
1781 * zfcp_print_els_rjt - print reject parameter and description for ELS reject
1782 * @rjt_par: reject parameter acc. to FC-PH/FC-FS
1783 * @rc_table: table of reason codes and descriptions
1784 */
1785 static void
1786 zfcp_print_els_rjt(struct zfcp_ls_rjt_par *rjt_par,
1787 const struct zfcp_rc_entry *rc_table)
1788 {
1789 ZFCP_LOG_INFO("%s (%02x %02x %02x %02x)\n",
1790 zfcp_rc_description(rjt_par->reason_code, rc_table),
1791 (u32) rjt_par->action, (u32) rjt_par->reason_code,
1792 (u32) rjt_par->reason_expl, (u32) rjt_par->vendor_unique);
1793 }
1794
1795 /**
1796 * zfcp_fsf_handle_els_rjt - evaluate status qualifier/reason code on ELS reject
1797 * @sq: status qualifier word
1798 * @rjt_par: reject parameter as described in FC-PH and FC-FS
1799 * Return: -EROMTEIO for LS_RJT, -EREMCHG for invalid D_ID, -EIO else
1800 */
1801 int
1802 zfcp_handle_els_rjt(u32 sq, struct zfcp_ls_rjt_par *rjt_par)
1803 {
1804 int ret = -EIO;
1805
1806 if (sq == FSF_IOSTAT_NPORT_RJT) {
1807 ZFCP_LOG_INFO("ELS rejected (P_RJT)\n");
1808 zfcp_print_els_rjt(rjt_par, zfcp_p_rjt_rc);
1809 /* invalid d_id */
1810 if (rjt_par->reason_code == 0x01)
1811 ret = -EREMCHG;
1812 } else if (sq == FSF_IOSTAT_FABRIC_RJT) {
1813 ZFCP_LOG_INFO("ELS rejected (F_RJT)\n");
1814 zfcp_print_els_rjt(rjt_par, zfcp_p_rjt_rc);
1815 /* invalid d_id */
1816 if (rjt_par->reason_code == 0x01)
1817 ret = -EREMCHG;
1818 } else if (sq == FSF_IOSTAT_LS_RJT) {
1819 ZFCP_LOG_INFO("ELS rejected (LS_RJT)\n");
1820 zfcp_print_els_rjt(rjt_par, zfcp_ls_rjt_rc);
1821 ret = -EREMOTEIO;
1822 } else
1823 ZFCP_LOG_INFO("unexpected SQ: 0x%02x\n", sq);
1824
1825 return ret;
1826 }
1827
1828 /**
1829 * zfcp_plogi_evaluate - evaluate PLOGI playload and copy important fields
1830 * into zfcp_port structure
1831 * @port: zfcp_port structure
1832 * @plogi: plogi payload
1833 */
1834 void
1835 zfcp_plogi_evaluate(struct zfcp_port *port, struct fsf_plogi *plogi)
1836 {
1837 port->maxframe_size = plogi->serv_param.common_serv_param[7] |
1838 ((plogi->serv_param.common_serv_param[6] & 0x0F) << 8);
1839 if (plogi->serv_param.class1_serv_param[0] & 0x80)
1840 port->supported_classes |= FC_COS_CLASS1;
1841 if (plogi->serv_param.class2_serv_param[0] & 0x80)
1842 port->supported_classes |= FC_COS_CLASS2;
1843 if (plogi->serv_param.class3_serv_param[0] & 0x80)
1844 port->supported_classes |= FC_COS_CLASS3;
1845 if (plogi->serv_param.class4_serv_param[0] & 0x80)
1846 port->supported_classes |= FC_COS_CLASS4;
1847 }
1848
1849 #undef ZFCP_LOG_AREA
This page took 0.112453 seconds and 6 git commands to generate.