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