blob: 41635b13ccb12e60ba484642967c8d02431d3260 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Andreas Herrmann4a9d2d82006-05-22 18:14:08 +02002 * This file is part of the zfcp device driver for
3 * FCP adapters for IBM System z9 and zSeries.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Andreas Herrmann4a9d2d82006-05-22 18:14:08 +02005 * (C) Copyright IBM Corp. 2002, 2006
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2, or (at your option)
10 * any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
Andreas Herrmann4a9d2d82006-05-22 18:14:08 +020022/*
23 * Driver authors:
24 * Martin Peschke (originator of the driver)
25 * Raimund Schroeder
26 * Aron Zeh
27 * Wolfgang Taphorn
28 * Stefan Bader
29 * Heiko Carstens (kernel 2.6 port of the driver)
30 * Andreas Herrmann
31 * Maxim Shchetynin
32 * Volker Sameske
33 * Ralph Wuerthner
34 */
35
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include "zfcp_ext.h"
37
38/* accumulated log level (module parameter) */
39static u32 loglevel = ZFCP_LOG_LEVEL_DEFAULTS;
40static char *device;
41/*********************** FUNCTION PROTOTYPES *********************************/
42
43/* written against the module interface */
44static int __init zfcp_module_init(void);
45
46/* FCP related */
47static void zfcp_ns_gid_pn_handler(unsigned long);
48
49/* miscellaneous */
Heiko Carstens4d284ca2007-02-05 21:18:53 +010050static int zfcp_sg_list_alloc(struct zfcp_sg_list *, size_t);
51static void zfcp_sg_list_free(struct zfcp_sg_list *);
52static int zfcp_sg_list_copy_from_user(struct zfcp_sg_list *,
53 void __user *, size_t);
54static int zfcp_sg_list_copy_to_user(void __user *,
55 struct zfcp_sg_list *, size_t);
Andreas Herrmannbd6ae2f2005-04-21 16:14:31 -040056static long zfcp_cfdc_dev_ioctl(struct file *, unsigned int, unsigned long);
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Arjan van de Vend54b1fd2007-02-12 00:55:34 -080063static const struct file_operations zfcp_cfdc_fops = {
e183b062005-04-02 13:57:17 -060064 .unlocked_ioctl = zfcp_cfdc_dev_ioctl,
65#ifdef CONFIG_COMPAT
66 .compat_ioctl = zfcp_cfdc_dev_ioctl
67#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070068};
69
70static struct miscdevice zfcp_cfdc_misc = {
71 .minor = ZFCP_CFDC_DEV_MINOR,
72 .name = ZFCP_CFDC_DEV_NAME,
73 .fops = &zfcp_cfdc_fops
74};
75
76/*********************** KERNEL/MODULE PARAMETERS ***************************/
77
78/* declare driver module init/cleanup functions */
79module_init(zfcp_module_init);
80
Andreas Herrmann4a9d2d82006-05-22 18:14:08 +020081MODULE_AUTHOR("IBM Deutschland Entwicklung GmbH - linux390@de.ibm.com");
Linus Torvalds1da177e2005-04-16 15:20:36 -070082MODULE_DESCRIPTION
Andreas Herrmann4a9d2d82006-05-22 18:14:08 +020083 ("FCP (SCSI over Fibre Channel) HBA driver for IBM System z9 and zSeries");
Linus Torvalds1da177e2005-04-16 15:20:36 -070084MODULE_LICENSE("GPL");
85
6f71d9b2005-04-10 23:04:28 -050086module_param(device, charp, 0400);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087MODULE_PARM_DESC(device, "specify initial device");
88
6f71d9b2005-04-10 23:04:28 -050089module_param(loglevel, uint, 0400);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090MODULE_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
Linus Torvalds1da177e2005-04-16 15:20:36 -070095/****************************************************************/
96/************** Functions without logging ***********************/
97/****************************************************************/
98
99void
100_zfcp_hex_dump(char *addr, int count)
101{
102 int i;
103 for (i = 0; i < count; i++) {
104 printk("%02x", addr[i]);
105 if ((i % 4) == 3)
106 printk(" ");
107 if ((i % 32) == 31)
108 printk("\n");
109 }
110 if (((i-1) % 32) != 31)
111 printk("\n");
112}
113
Volker Sameskefea9d6c2006-08-02 11:05:16 +0200114
115/****************************************************************/
116/****** Functions to handle the request ID hash table ********/
117/****************************************************************/
118
119#define ZFCP_LOG_AREA ZFCP_LOG_AREA_FSF
120
Heiko Carstensca2d02c2007-05-08 11:17:54 +0200121static int zfcp_reqlist_alloc(struct zfcp_adapter *adapter)
Volker Sameskefea9d6c2006-08-02 11:05:16 +0200122{
Heiko Carstensca2d02c2007-05-08 11:17:54 +0200123 int idx;
Volker Sameskefea9d6c2006-08-02 11:05:16 +0200124
125 adapter->req_list = kcalloc(REQUEST_LIST_SIZE, sizeof(struct list_head),
126 GFP_KERNEL);
Volker Sameskefea9d6c2006-08-02 11:05:16 +0200127 if (!adapter->req_list)
128 return -ENOMEM;
129
Heiko Carstensca2d02c2007-05-08 11:17:54 +0200130 for (idx = 0; idx < REQUEST_LIST_SIZE; idx++)
131 INIT_LIST_HEAD(&adapter->req_list[idx]);
Volker Sameskefea9d6c2006-08-02 11:05:16 +0200132 return 0;
133}
134
135static void zfcp_reqlist_free(struct zfcp_adapter *adapter)
136{
Volker Sameskefea9d6c2006-08-02 11:05:16 +0200137 kfree(adapter->req_list);
138}
139
Volker Sameskefea9d6c2006-08-02 11:05:16 +0200140int zfcp_reqlist_isempty(struct zfcp_adapter *adapter)
141{
Heiko Carstensca2d02c2007-05-08 11:17:54 +0200142 unsigned int idx;
Volker Sameskefea9d6c2006-08-02 11:05:16 +0200143
Heiko Carstensca2d02c2007-05-08 11:17:54 +0200144 for (idx = 0; idx < REQUEST_LIST_SIZE; idx++)
145 if (!list_empty(&adapter->req_list[idx]))
Volker Sameskefea9d6c2006-08-02 11:05:16 +0200146 return 0;
Volker Sameskefea9d6c2006-08-02 11:05:16 +0200147 return 1;
148}
149
150#undef ZFCP_LOG_AREA
151
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152/****************************************************************/
153/************** Uncategorised Functions *************************/
154/****************************************************************/
155
156#define ZFCP_LOG_AREA ZFCP_LOG_AREA_OTHER
157
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158/**
159 * zfcp_device_setup - setup function
160 * @str: pointer to parameter string
161 *
162 * Parse "device=..." parameter string.
163 */
164static int __init
Andreas Herrmanncd8a3832005-06-13 13:22:25 +0200165zfcp_device_setup(char *devstr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166{
Andreas Herrmanncd8a3832005-06-13 13:22:25 +0200167 char *tmp, *str;
168 size_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
Andreas Herrmanncd8a3832005-06-13 13:22:25 +0200170 if (!devstr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 return 0;
172
Andreas Herrmanncd8a3832005-06-13 13:22:25 +0200173 len = strlen(devstr) + 1;
Robert P. J. Day5cbded52006-12-13 00:35:56 -0800174 str = kmalloc(len, GFP_KERNEL);
Andreas Herrmanncd8a3832005-06-13 13:22:25 +0200175 if (!str)
176 goto err_out;
177 memcpy(str, devstr, len);
178
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 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;
Andreas Herrmanncd8a3832005-06-13 13:22:25 +0200195 kfree(str);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 return 1;
197
198 err_out:
199 ZFCP_LOG_NORMAL("Parse error for device parameter string %s\n", str);
Andreas Herrmanncd8a3832005-06-13 13:22:25 +0200200 kfree(str);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 return 0;
202}
203
204static void __init
205zfcp_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
Heiko Carstensdd52e0e2006-09-18 22:28:49 +0200240static 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253static int __init
254zfcp_module_init(void)
255{
Heiko Carstensdd52e0e2006-09-18 22:28:49 +0200256 int retval = -ENOMEM;
257 int size, align;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
Heiko Carstensdd52e0e2006-09-18 22:28:49 +0200259 size = sizeof(struct zfcp_fsf_req_qtcb);
260 align = calc_alignment(size);
261 zfcp_data.fsf_req_qtcb_cache =
Paul Mundt20c2df82007-07-20 10:11:58 +0900262 kmem_cache_create("zfcp_fsf", size, align, 0, NULL);
Heiko Carstensdd52e0e2006-09-18 22:28:49 +0200263 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 =
Paul Mundt20c2df82007-07-20 10:11:58 +0900269 kmem_cache_create("zfcp_sr", size, align, 0, NULL);
Heiko Carstensdd52e0e2006-09-18 22:28:49 +0200270 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 =
Paul Mundt20c2df82007-07-20 10:11:58 +0900276 kmem_cache_create("zfcp_gid", size, align, 0, NULL);
Heiko Carstensdd52e0e2006-09-18 22:28:49 +0200277 if (!zfcp_data.gid_pn_cache)
278 goto out_gid_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
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
Heiko Carstensdd52e0e2006-09-18 22:28:49 +0200288 zfcp_data.scsi_transport_template =
289 fc_attach_transport(&zfcp_transport_functions);
290 if (!zfcp_data.scsi_transport_template)
291 goto out_transport;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 retval = misc_register(&zfcp_cfdc_misc);
294 if (retval != 0) {
295 ZFCP_LOG_INFO("registration of misc device "
296 "zfcp_cfdc failed\n");
Heiko Carstensdd52e0e2006-09-18 22:28:49 +0200297 goto out_misc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 }
299
e183b062005-04-02 13:57:17 -0600300 ZFCP_LOG_TRACE("major/minor for zfcp_cfdc: %d/%d\n",
301 ZFCP_CFDC_DEV_MAJOR, zfcp_cfdc_misc.minor);
302
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 /* 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 /* 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);
Heiko Carstensdd52e0e2006-09-18 22:28:49 +0200323 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 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 */
e183b062005-04-02 13:57:17 -0600351static long
352zfcp_cfdc_dev_ioctl(struct file *file, unsigned int command,
353 unsigned long buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354{
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
Andreas Herrmannec4081c2006-05-22 18:17:30 +0200369 sg_list = kzalloc(sizeof(struct zfcp_sg_list), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 if (sg_list == NULL) {
371 retval = -ENOMEM;
372 goto out;
373 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
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)
Andreas Herrmann1db2c9c2005-06-13 13:20:35 +0200515 zfcp_fsf_req_free(fsf_req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
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
Jesper Juhl17fd6822005-11-07 01:01:30 -0800525 kfree(sense_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
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 */
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100542static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543zfcp_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++;
Andreas Herrmannec4081c2006-05-22 18:17:30 +0200555 sg_list->sg = kcalloc(sg_list->count, sizeof(struct scatterlist),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 GFP_KERNEL);
557 if (sg_list->sg == NULL) {
558 sg_list->count = 0;
559 retval = -ENOMEM;
560 goto out;
561 }
Jens Axboe73fc4f02007-10-23 09:17:53 +0200562 sg_init_table(sg_list->sg, sg_list->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
564 for (i = 0, sg = sg_list->sg; i < sg_list->count; i++, sg++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 address = (void *) get_zeroed_page(GFP_KERNEL);
566 if (address == NULL) {
567 sg_list->count = i;
568 zfcp_sg_list_free(sg_list);
569 retval = -ENOMEM;
570 goto out;
571 }
Jens Axboe642f149032007-10-24 11:20:47 +0200572 zfcp_address_to_sg(address, sg, min(size, PAGE_SIZE));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 size -= sg->length;
574 }
575
576 out:
577 return retval;
578}
579
580
581/**
582 * zfcp_sg_list_free - free memory of a scatter-gather list
583 * @sg_list: structure describing a scatter-gather list
584 *
585 * Memory for each element in the scatter-gather list is freed.
586 * Finally sg_list->sg is freed itself and sg_list->count is reset.
587 */
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100588static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589zfcp_sg_list_free(struct zfcp_sg_list *sg_list)
590{
591 struct scatterlist *sg;
592 unsigned int i;
593
594 BUG_ON(sg_list == NULL);
595
596 for (i = 0, sg = sg_list->sg; i < sg_list->count; i++, sg++)
597 free_page((unsigned long) zfcp_sg_to_address(sg));
598
599 sg_list->count = 0;
600 kfree(sg_list->sg);
601}
602
603/**
604 * zfcp_sg_size - determine size of a scatter-gather list
605 * @sg: array of (struct scatterlist)
606 * @sg_count: elements in array
607 * Return: size of entire scatter-gather list
608 */
Heiko Carstens763968e2007-05-10 15:45:46 +0200609static size_t zfcp_sg_size(struct scatterlist *sg, unsigned int sg_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610{
611 unsigned int i;
612 struct scatterlist *p;
613 size_t size;
614
615 size = 0;
616 for (i = 0, p = sg; i < sg_count; i++, p++) {
617 BUG_ON(p == NULL);
618 size += p->length;
619 }
620
621 return size;
622}
623
624
625/**
626 * zfcp_sg_list_copy_from_user -copy data from user space to scatter-gather list
627 * @sg_list: structure describing a scatter-gather list
628 * @user_buffer: pointer to buffer in user space
629 * @size: number of bytes to be copied
630 * Return: 0 on success, -EFAULT if copy_from_user fails.
631 */
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100632static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633zfcp_sg_list_copy_from_user(struct zfcp_sg_list *sg_list,
634 void __user *user_buffer,
635 size_t size)
636{
637 struct scatterlist *sg;
638 unsigned int length;
639 void *zfcp_buffer;
640 int retval = 0;
641
642 BUG_ON(sg_list == NULL);
643
644 if (zfcp_sg_size(sg_list->sg, sg_list->count) < size)
645 return -EFAULT;
646
647 for (sg = sg_list->sg; size > 0; sg++) {
648 length = min((unsigned int)size, sg->length);
649 zfcp_buffer = zfcp_sg_to_address(sg);
650 if (copy_from_user(zfcp_buffer, user_buffer, length)) {
651 retval = -EFAULT;
652 goto out;
653 }
654 user_buffer += length;
655 size -= length;
656 }
657
658 out:
659 return retval;
660}
661
662
663/**
664 * zfcp_sg_list_copy_to_user - copy data from scatter-gather list to user space
665 * @user_buffer: pointer to buffer in user space
666 * @sg_list: structure describing a scatter-gather list
667 * @size: number of bytes to be copied
668 * Return: 0 on success, -EFAULT if copy_to_user fails
669 */
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100670static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671zfcp_sg_list_copy_to_user(void __user *user_buffer,
672 struct zfcp_sg_list *sg_list,
673 size_t size)
674{
675 struct scatterlist *sg;
676 unsigned int length;
677 void *zfcp_buffer;
678 int retval = 0;
679
680 BUG_ON(sg_list == NULL);
681
682 if (zfcp_sg_size(sg_list->sg, sg_list->count) < size)
683 return -EFAULT;
684
685 for (sg = sg_list->sg; size > 0; sg++) {
686 length = min((unsigned int) size, sg->length);
687 zfcp_buffer = zfcp_sg_to_address(sg);
688 if (copy_to_user(user_buffer, zfcp_buffer, length)) {
689 retval = -EFAULT;
690 goto out;
691 }
692 user_buffer += length;
693 size -= length;
694 }
695
696 out:
697 return retval;
698}
699
700
701#undef ZFCP_LOG_AREA
702
703/****************************************************************/
704/****** Functions for configuration/set-up of structures ********/
705/****************************************************************/
706
707#define ZFCP_LOG_AREA ZFCP_LOG_AREA_CONFIG
708
709/**
710 * zfcp_get_unit_by_lun - find unit in unit list of port by FCP LUN
711 * @port: pointer to port to search for unit
712 * @fcp_lun: FCP LUN to search for
713 * Traverse list of all units of a port and return pointer to a unit
714 * with the given FCP LUN.
715 */
716struct zfcp_unit *
717zfcp_get_unit_by_lun(struct zfcp_port *port, fcp_lun_t fcp_lun)
718{
719 struct zfcp_unit *unit;
720 int found = 0;
721
722 list_for_each_entry(unit, &port->unit_list_head, list) {
723 if ((unit->fcp_lun == fcp_lun) &&
724 !atomic_test_mask(ZFCP_STATUS_COMMON_REMOVE, &unit->status))
725 {
726 found = 1;
727 break;
728 }
729 }
730 return found ? unit : NULL;
731}
732
733/**
734 * zfcp_get_port_by_wwpn - find port in port list of adapter by wwpn
735 * @adapter: pointer to adapter to search for port
736 * @wwpn: wwpn to search for
737 * Traverse list of all ports of an adapter and return pointer to a port
738 * with the given wwpn.
739 */
740struct zfcp_port *
741zfcp_get_port_by_wwpn(struct zfcp_adapter *adapter, wwn_t wwpn)
742{
743 struct zfcp_port *port;
744 int found = 0;
745
746 list_for_each_entry(port, &adapter->port_list_head, list) {
747 if ((port->wwpn == wwpn) &&
748 !(atomic_read(&port->status) &
749 (ZFCP_STATUS_PORT_NO_WWPN | ZFCP_STATUS_COMMON_REMOVE))) {
750 found = 1;
751 break;
752 }
753 }
754 return found ? port : NULL;
755}
756
757/**
758 * zfcp_get_port_by_did - find port in port list of adapter by d_id
759 * @adapter: pointer to adapter to search for port
760 * @d_id: d_id to search for
761 * Traverse list of all ports of an adapter and return pointer to a port
762 * with the given d_id.
763 */
764struct zfcp_port *
765zfcp_get_port_by_did(struct zfcp_adapter *adapter, u32 d_id)
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->d_id == d_id) &&
772 !atomic_test_mask(ZFCP_STATUS_COMMON_REMOVE, &port->status))
773 {
774 found = 1;
775 break;
776 }
777 }
778 return found ? port : NULL;
779}
780
781/**
782 * zfcp_get_adapter_by_busid - find adpater in adapter list by bus_id
783 * @bus_id: bus_id to search for
784 * Traverse list of all adapters and return pointer to an adapter
785 * with the given bus_id.
786 */
787struct zfcp_adapter *
788zfcp_get_adapter_by_busid(char *bus_id)
789{
790 struct zfcp_adapter *adapter;
791 int found = 0;
792
793 list_for_each_entry(adapter, &zfcp_data.adapter_list_head, list) {
794 if ((strncmp(bus_id, zfcp_get_busid_by_adapter(adapter),
795 BUS_ID_SIZE) == 0) &&
796 !atomic_test_mask(ZFCP_STATUS_COMMON_REMOVE,
797 &adapter->status)){
798 found = 1;
799 break;
800 }
801 }
802 return found ? adapter : NULL;
803}
804
805/**
806 * zfcp_unit_enqueue - enqueue unit to unit list of a port.
807 * @port: pointer to port where unit is added
808 * @fcp_lun: FCP LUN of unit to be enqueued
809 * Return: pointer to enqueued unit on success, NULL on error
810 * Locks: config_sema must be held to serialize changes to the unit list
811 *
812 * Sets up some unit internal structures and creates sysfs entry.
813 */
814struct zfcp_unit *
815zfcp_unit_enqueue(struct zfcp_port *port, fcp_lun_t fcp_lun)
816{
Christof Schmitt462b7852007-06-19 10:25:30 +0200817 struct zfcp_unit *unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
819 /*
820 * check that there is no unit with this FCP_LUN already in list
821 * and enqueue it.
822 * Note: Unlike for the adapter and the port, this is an error
823 */
824 read_lock_irq(&zfcp_data.config_lock);
825 unit = zfcp_get_unit_by_lun(port, fcp_lun);
826 read_unlock_irq(&zfcp_data.config_lock);
827 if (unit)
828 return NULL;
829
Andreas Herrmannec4081c2006-05-22 18:17:30 +0200830 unit = kzalloc(sizeof (struct zfcp_unit), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 if (!unit)
832 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
834 /* initialise reference count stuff */
835 atomic_set(&unit->refcount, 0);
836 init_waitqueue_head(&unit->remove_wq);
837
838 unit->port = port;
839 unit->fcp_lun = fcp_lun;
840
841 /* setup for sysfs registration */
842 snprintf(unit->sysfs_device.bus_id, BUS_ID_SIZE, "0x%016llx", fcp_lun);
843 unit->sysfs_device.parent = &port->sysfs_device;
844 unit->sysfs_device.release = zfcp_sysfs_unit_release;
845 dev_set_drvdata(&unit->sysfs_device, unit);
846
847 /* mark unit unusable as long as sysfs registration is not complete */
848 atomic_set_mask(ZFCP_STATUS_COMMON_REMOVE, &unit->status);
849
Christof Schmittc9615852008-05-06 11:00:05 +0200850 spin_lock_init(&unit->latencies.lock);
851 unit->latencies.write.channel.min = 0xFFFFFFFF;
852 unit->latencies.write.fabric.min = 0xFFFFFFFF;
853 unit->latencies.read.channel.min = 0xFFFFFFFF;
854 unit->latencies.read.fabric.min = 0xFFFFFFFF;
855 unit->latencies.cmd.channel.min = 0xFFFFFFFF;
856 unit->latencies.cmd.fabric.min = 0xFFFFFFFF;
857
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 if (device_register(&unit->sysfs_device)) {
859 kfree(unit);
860 return NULL;
861 }
862
863 if (zfcp_sysfs_unit_create_files(&unit->sysfs_device)) {
864 device_unregister(&unit->sysfs_device);
865 return NULL;
866 }
867
868 zfcp_unit_get(unit);
Christof Schmitt462b7852007-06-19 10:25:30 +0200869 unit->scsi_lun = scsilun_to_int((struct scsi_lun *)&unit->fcp_lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 write_lock_irq(&zfcp_data.config_lock);
Christof Schmitt462b7852007-06-19 10:25:30 +0200872 list_add_tail(&unit->list, &port->unit_list_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 atomic_clear_mask(ZFCP_STATUS_COMMON_REMOVE, &unit->status);
874 atomic_set_mask(ZFCP_STATUS_COMMON_RUNNING, &unit->status);
875 write_unlock_irq(&zfcp_data.config_lock);
876
877 port->units++;
878 zfcp_port_get(port);
879
880 return unit;
881}
882
883void
884zfcp_unit_dequeue(struct zfcp_unit *unit)
885{
886 zfcp_unit_wait(unit);
887 write_lock_irq(&zfcp_data.config_lock);
888 list_del(&unit->list);
889 write_unlock_irq(&zfcp_data.config_lock);
890 unit->port->units--;
891 zfcp_port_put(unit->port);
892 zfcp_sysfs_unit_remove_files(&unit->sysfs_device);
893 device_unregister(&unit->sysfs_device);
894}
895
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896/*
897 * Allocates a combined QTCB/fsf_req buffer for erp actions and fcp/SCSI
898 * commands.
Swen Schillig41fa2ad2007-09-07 09:15:31 +0200899 * It also genrates fcp-nameserver request/response buffer and unsolicited
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 * status read fsf_req buffers.
901 *
902 * locks: must only be called with zfcp_data.config_sema taken
903 */
904static int
905zfcp_allocate_low_mem_buffers(struct zfcp_adapter *adapter)
906{
907 adapter->pool.fsf_req_erp =
Heiko Carstensdd52e0e2006-09-18 22:28:49 +0200908 mempool_create_slab_pool(ZFCP_POOL_FSF_REQ_ERP_NR,
909 zfcp_data.fsf_req_qtcb_cache);
Matthew Dobson0eaae62a2006-03-26 01:37:47 -0800910 if (!adapter->pool.fsf_req_erp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 return -ENOMEM;
912
913 adapter->pool.fsf_req_scsi =
Heiko Carstensdd52e0e2006-09-18 22:28:49 +0200914 mempool_create_slab_pool(ZFCP_POOL_FSF_REQ_SCSI_NR,
915 zfcp_data.fsf_req_qtcb_cache);
Matthew Dobson0eaae62a2006-03-26 01:37:47 -0800916 if (!adapter->pool.fsf_req_scsi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 return -ENOMEM;
918
919 adapter->pool.fsf_req_abort =
Heiko Carstensdd52e0e2006-09-18 22:28:49 +0200920 mempool_create_slab_pool(ZFCP_POOL_FSF_REQ_ABORT_NR,
921 zfcp_data.fsf_req_qtcb_cache);
Matthew Dobson0eaae62a2006-03-26 01:37:47 -0800922 if (!adapter->pool.fsf_req_abort)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 return -ENOMEM;
924
925 adapter->pool.fsf_req_status_read =
Matthew Dobson0eaae62a2006-03-26 01:37:47 -0800926 mempool_create_kmalloc_pool(ZFCP_POOL_STATUS_READ_NR,
927 sizeof(struct zfcp_fsf_req));
928 if (!adapter->pool.fsf_req_status_read)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 return -ENOMEM;
930
931 adapter->pool.data_status_read =
Heiko Carstensdd52e0e2006-09-18 22:28:49 +0200932 mempool_create_slab_pool(ZFCP_POOL_STATUS_READ_NR,
933 zfcp_data.sr_buffer_cache);
Matthew Dobson0eaae62a2006-03-26 01:37:47 -0800934 if (!adapter->pool.data_status_read)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 return -ENOMEM;
936
937 adapter->pool.data_gid_pn =
Heiko Carstensdd52e0e2006-09-18 22:28:49 +0200938 mempool_create_slab_pool(ZFCP_POOL_DATA_GID_PN_NR,
939 zfcp_data.gid_pn_cache);
Matthew Dobson0eaae62a2006-03-26 01:37:47 -0800940 if (!adapter->pool.data_gid_pn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 return -ENOMEM;
942
943 return 0;
944}
945
946/**
947 * zfcp_free_low_mem_buffers - free memory pools of an adapter
948 * @adapter: pointer to zfcp_adapter for which memory pools should be freed
949 * locking: zfcp_data.config_sema must be held
950 */
951static void
952zfcp_free_low_mem_buffers(struct zfcp_adapter *adapter)
953{
954 if (adapter->pool.fsf_req_erp)
955 mempool_destroy(adapter->pool.fsf_req_erp);
956 if (adapter->pool.fsf_req_scsi)
957 mempool_destroy(adapter->pool.fsf_req_scsi);
958 if (adapter->pool.fsf_req_abort)
959 mempool_destroy(adapter->pool.fsf_req_abort);
960 if (adapter->pool.fsf_req_status_read)
961 mempool_destroy(adapter->pool.fsf_req_status_read);
962 if (adapter->pool.data_status_read)
963 mempool_destroy(adapter->pool.data_status_read);
964 if (adapter->pool.data_gid_pn)
965 mempool_destroy(adapter->pool.data_gid_pn);
966}
967
Heiko Carstens763968e2007-05-10 15:45:46 +0200968static void zfcp_dummy_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969{
970 return;
971}
972
Swen Schilligd26ab062008-05-19 12:17:37 +0200973int zfcp_status_read_refill(struct zfcp_adapter *adapter)
974{
975 while (atomic_read(&adapter->stat_miss) > 0)
976 if (zfcp_fsf_status_read(adapter, ZFCP_WAIT_FOR_SBAL))
977 break;
978 else
979 atomic_dec(&adapter->stat_miss);
980
981 if (ZFCP_STATUS_READS_RECOM <= atomic_read(&adapter->stat_miss)) {
982 zfcp_erp_adapter_reopen(adapter, 0, 103, NULL);
983 return 1;
984 }
985 return 0;
986}
987
988static void _zfcp_status_read_scheduler(struct work_struct *work)
989{
990 zfcp_status_read_refill(container_of(work, struct zfcp_adapter,
991 stat_work));
992}
993
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994/*
995 * Enqueues an adapter at the end of the adapter list in the driver data.
996 * All adapter internal structures are set up.
997 * Proc-fs entries are also created.
998 *
999 * returns: 0 if a new adapter was successfully enqueued
1000 * ZFCP_KNOWN if an adapter with this devno was already present
1001 * -ENOMEM if alloc failed
1002 * locks: config_sema must be held to serialise changes to the adapter list
1003 */
1004struct zfcp_adapter *
1005zfcp_adapter_enqueue(struct ccw_device *ccw_device)
1006{
1007 int retval = 0;
1008 struct zfcp_adapter *adapter;
1009
1010 /*
Swen Schillig41fa2ad2007-09-07 09:15:31 +02001011 * Note: It is safe to release the list_lock, as any list changes
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 * are protected by the config_sema, which must be held to get here
1013 */
1014
1015 /* try to allocate new adapter data structure (zeroed) */
Andreas Herrmannec4081c2006-05-22 18:17:30 +02001016 adapter = kzalloc(sizeof (struct zfcp_adapter), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 if (!adapter) {
1018 ZFCP_LOG_INFO("error: allocation of base adapter "
1019 "structure failed\n");
1020 goto out;
1021 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022
1023 ccw_device->handler = NULL;
1024
1025 /* save ccw_device pointer */
1026 adapter->ccw_device = ccw_device;
1027
1028 retval = zfcp_qdio_allocate_queues(adapter);
1029 if (retval)
1030 goto queues_alloc_failed;
1031
1032 retval = zfcp_qdio_allocate(adapter);
1033 if (retval)
1034 goto qdio_allocate_failed;
1035
1036 retval = zfcp_allocate_low_mem_buffers(adapter);
1037 if (retval) {
1038 ZFCP_LOG_INFO("error: pool allocation failed\n");
1039 goto failed_low_mem_buffers;
1040 }
1041
1042 /* initialise reference count stuff */
1043 atomic_set(&adapter->refcount, 0);
1044 init_waitqueue_head(&adapter->remove_wq);
1045
1046 /* initialise list of ports */
1047 INIT_LIST_HEAD(&adapter->port_list_head);
1048
1049 /* initialise list of ports to be removed */
1050 INIT_LIST_HEAD(&adapter->port_remove_lh);
1051
1052 /* initialize list of fsf requests */
Volker Sameskefea9d6c2006-08-02 11:05:16 +02001053 spin_lock_init(&adapter->req_list_lock);
Heiko Carstensca2d02c2007-05-08 11:17:54 +02001054 retval = zfcp_reqlist_alloc(adapter);
Volker Sameskefea9d6c2006-08-02 11:05:16 +02001055 if (retval) {
1056 ZFCP_LOG_INFO("request list initialization failed\n");
1057 goto failed_low_mem_buffers;
1058 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059
Heiko Carstensc48a29d2005-12-01 02:46:32 +01001060 /* initialize debug locks */
1061
Heiko Carstensc48a29d2005-12-01 02:46:32 +01001062 spin_lock_init(&adapter->hba_dbf_lock);
1063 spin_lock_init(&adapter->san_dbf_lock);
1064 spin_lock_init(&adapter->scsi_dbf_lock);
Martin Peschked79a83d2008-03-27 14:22:00 +01001065 spin_lock_init(&adapter->rec_dbf_lock);
Heiko Carstensc48a29d2005-12-01 02:46:32 +01001066
Christof Schmittff17a292007-08-28 09:31:41 +02001067 retval = zfcp_adapter_debug_register(adapter);
1068 if (retval)
1069 goto debug_register_failed;
1070
Heiko Carstensc48a29d2005-12-01 02:46:32 +01001071 /* initialize error recovery stuff */
1072
1073 rwlock_init(&adapter->erp_lock);
1074 sema_init(&adapter->erp_ready_sem, 0);
1075 INIT_LIST_HEAD(&adapter->erp_ready_head);
1076 INIT_LIST_HEAD(&adapter->erp_running_head);
1077
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 /* initialize abort lock */
1079 rwlock_init(&adapter->abort_lock);
1080
1081 /* initialise some erp stuff */
1082 init_waitqueue_head(&adapter->erp_thread_wqh);
1083 init_waitqueue_head(&adapter->erp_done_wqh);
1084
1085 /* initialize lock of associated request queue */
1086 rwlock_init(&adapter->request_queue.queue_lock);
Swen Schilligd26ab062008-05-19 12:17:37 +02001087 INIT_WORK(&adapter->stat_work, _zfcp_status_read_scheduler);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 /* mark adapter unusable as long as sysfs registration is not complete */
1090 atomic_set_mask(ZFCP_STATUS_COMMON_REMOVE, &adapter->status);
1091
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 dev_set_drvdata(&ccw_device->dev, adapter);
1093
1094 if (zfcp_sysfs_adapter_create_files(&ccw_device->dev))
1095 goto sysfs_failed;
1096
1097 adapter->generic_services.parent = &adapter->ccw_device->dev;
1098 adapter->generic_services.release = zfcp_dummy_release;
1099 snprintf(adapter->generic_services.bus_id, BUS_ID_SIZE,
1100 "generic_services");
1101
1102 if (device_register(&adapter->generic_services))
1103 goto generic_services_failed;
1104
1105 /* put allocated adapter at list tail */
1106 write_lock_irq(&zfcp_data.config_lock);
1107 atomic_clear_mask(ZFCP_STATUS_COMMON_REMOVE, &adapter->status);
1108 list_add_tail(&adapter->list, &zfcp_data.adapter_list_head);
1109 write_unlock_irq(&zfcp_data.config_lock);
1110
1111 zfcp_data.adapters++;
1112
1113 goto out;
1114
1115 generic_services_failed:
1116 zfcp_sysfs_adapter_remove_files(&adapter->ccw_device->dev);
1117 sysfs_failed:
Christof Schmittff17a292007-08-28 09:31:41 +02001118 zfcp_adapter_debug_unregister(adapter);
1119 debug_register_failed:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 dev_set_drvdata(&ccw_device->dev, NULL);
Heiko Carstensca2d02c2007-05-08 11:17:54 +02001121 zfcp_reqlist_free(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 failed_low_mem_buffers:
1123 zfcp_free_low_mem_buffers(adapter);
1124 if (qdio_free(ccw_device) != 0)
1125 ZFCP_LOG_NORMAL("bug: qdio_free for adapter %s failed\n",
1126 zfcp_get_busid_by_adapter(adapter));
1127 qdio_allocate_failed:
1128 zfcp_qdio_free_queues(adapter);
1129 queues_alloc_failed:
1130 kfree(adapter);
1131 adapter = NULL;
1132 out:
1133 return adapter;
1134}
1135
1136/*
1137 * returns: 0 - struct zfcp_adapter data structure successfully removed
1138 * !0 - struct zfcp_adapter data structure could not be removed
1139 * (e.g. still used)
1140 * locks: adapter list write lock is assumed to be held by caller
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 */
1142void
1143zfcp_adapter_dequeue(struct zfcp_adapter *adapter)
1144{
1145 int retval = 0;
1146 unsigned long flags;
1147
Swen Schilligd26ab062008-05-19 12:17:37 +02001148 cancel_work_sync(&adapter->stat_work);
Michael Loehr9f287452007-05-09 11:01:24 +02001149 zfcp_adapter_scsi_unregister(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 device_unregister(&adapter->generic_services);
1151 zfcp_sysfs_adapter_remove_files(&adapter->ccw_device->dev);
1152 dev_set_drvdata(&adapter->ccw_device->dev, NULL);
1153 /* sanity check: no pending FSF requests */
Volker Sameskefea9d6c2006-08-02 11:05:16 +02001154 spin_lock_irqsave(&adapter->req_list_lock, flags);
1155 retval = zfcp_reqlist_isempty(adapter);
1156 spin_unlock_irqrestore(&adapter->req_list_lock, flags);
1157 if (!retval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 ZFCP_LOG_NORMAL("bug: adapter %s (%p) still in use, "
1159 "%i requests outstanding\n",
1160 zfcp_get_busid_by_adapter(adapter), adapter,
Volker Sameskefea9d6c2006-08-02 11:05:16 +02001161 atomic_read(&adapter->reqs_active));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 retval = -EBUSY;
1163 goto out;
1164 }
1165
Christof Schmittff17a292007-08-28 09:31:41 +02001166 zfcp_adapter_debug_unregister(adapter);
1167
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 /* remove specified adapter data structure from list */
1169 write_lock_irq(&zfcp_data.config_lock);
1170 list_del(&adapter->list);
1171 write_unlock_irq(&zfcp_data.config_lock);
1172
1173 /* decrease number of adapters in list */
1174 zfcp_data.adapters--;
1175
1176 ZFCP_LOG_TRACE("adapter %s (%p) removed from list, "
1177 "%i adapters still in list\n",
1178 zfcp_get_busid_by_adapter(adapter),
1179 adapter, zfcp_data.adapters);
1180
1181 retval = qdio_free(adapter->ccw_device);
1182 if (retval)
1183 ZFCP_LOG_NORMAL("bug: qdio_free for adapter %s failed\n",
1184 zfcp_get_busid_by_adapter(adapter));
1185
1186 zfcp_free_low_mem_buffers(adapter);
1187 /* free memory of adapter data structure and queues */
1188 zfcp_qdio_free_queues(adapter);
Volker Sameskefea9d6c2006-08-02 11:05:16 +02001189 zfcp_reqlist_free(adapter);
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +01001190 kfree(adapter->fc_stats);
1191 kfree(adapter->stats_reset_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 ZFCP_LOG_TRACE("freeing adapter structure\n");
1193 kfree(adapter);
1194 out:
1195 return;
1196}
1197
1198/**
1199 * zfcp_port_enqueue - enqueue port to port list of adapter
1200 * @adapter: adapter where remote port is added
1201 * @wwpn: WWPN of the remote port to be enqueued
1202 * @status: initial status for the port
1203 * @d_id: destination id of the remote port to be enqueued
1204 * Return: pointer to enqueued port on success, NULL on error
1205 * Locks: config_sema must be held to serialize changes to the port list
1206 *
1207 * All port internal structures are set up and the sysfs entry is generated.
1208 * d_id is used to enqueue ports with a well known address like the Directory
1209 * Service for nameserver lookup.
1210 */
1211struct zfcp_port *
1212zfcp_port_enqueue(struct zfcp_adapter *adapter, wwn_t wwpn, u32 status,
1213 u32 d_id)
1214{
Andreas Herrmann3859f6a2005-08-27 11:07:54 -07001215 struct zfcp_port *port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 int check_wwpn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217
1218 check_wwpn = !(status & ZFCP_STATUS_PORT_NO_WWPN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 /*
1220 * check that there is no port with this WWPN already in list
1221 */
1222 if (check_wwpn) {
1223 read_lock_irq(&zfcp_data.config_lock);
1224 port = zfcp_get_port_by_wwpn(adapter, wwpn);
1225 read_unlock_irq(&zfcp_data.config_lock);
1226 if (port)
1227 return NULL;
1228 }
1229
Andreas Herrmannec4081c2006-05-22 18:17:30 +02001230 port = kzalloc(sizeof (struct zfcp_port), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 if (!port)
1232 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233
1234 /* initialise reference count stuff */
1235 atomic_set(&port->refcount, 0);
1236 init_waitqueue_head(&port->remove_wq);
1237
1238 INIT_LIST_HEAD(&port->unit_list_head);
1239 INIT_LIST_HEAD(&port->unit_remove_lh);
1240
1241 port->adapter = adapter;
1242
1243 if (check_wwpn)
1244 port->wwpn = wwpn;
1245
1246 atomic_set_mask(status, &port->status);
1247
1248 /* setup for sysfs registration */
1249 if (status & ZFCP_STATUS_PORT_WKA) {
1250 switch (d_id) {
1251 case ZFCP_DID_DIRECTORY_SERVICE:
1252 snprintf(port->sysfs_device.bus_id, BUS_ID_SIZE,
1253 "directory");
1254 break;
1255 case ZFCP_DID_MANAGEMENT_SERVICE:
1256 snprintf(port->sysfs_device.bus_id, BUS_ID_SIZE,
1257 "management");
1258 break;
1259 case ZFCP_DID_KEY_DISTRIBUTION_SERVICE:
1260 snprintf(port->sysfs_device.bus_id, BUS_ID_SIZE,
1261 "key_distribution");
1262 break;
1263 case ZFCP_DID_ALIAS_SERVICE:
1264 snprintf(port->sysfs_device.bus_id, BUS_ID_SIZE,
1265 "alias");
1266 break;
1267 case ZFCP_DID_TIME_SERVICE:
1268 snprintf(port->sysfs_device.bus_id, BUS_ID_SIZE,
1269 "time");
1270 break;
1271 default:
1272 kfree(port);
1273 return NULL;
1274 }
1275 port->d_id = d_id;
1276 port->sysfs_device.parent = &adapter->generic_services;
1277 } else {
1278 snprintf(port->sysfs_device.bus_id,
1279 BUS_ID_SIZE, "0x%016llx", wwpn);
Andreas Herrmann3859f6a2005-08-27 11:07:54 -07001280 port->sysfs_device.parent = &adapter->ccw_device->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 }
1282 port->sysfs_device.release = zfcp_sysfs_port_release;
1283 dev_set_drvdata(&port->sysfs_device, port);
1284
1285 /* mark port unusable as long as sysfs registration is not complete */
1286 atomic_set_mask(ZFCP_STATUS_COMMON_REMOVE, &port->status);
1287
1288 if (device_register(&port->sysfs_device)) {
1289 kfree(port);
1290 return NULL;
1291 }
1292
1293 if (zfcp_sysfs_port_create_files(&port->sysfs_device, status)) {
1294 device_unregister(&port->sysfs_device);
1295 return NULL;
1296 }
1297
1298 zfcp_port_get(port);
1299
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 write_lock_irq(&zfcp_data.config_lock);
Andreas Herrmann3859f6a2005-08-27 11:07:54 -07001301 list_add_tail(&port->list, &adapter->port_list_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 atomic_clear_mask(ZFCP_STATUS_COMMON_REMOVE, &port->status);
1303 atomic_set_mask(ZFCP_STATUS_COMMON_RUNNING, &port->status);
1304 if (d_id == ZFCP_DID_DIRECTORY_SERVICE)
1305 if (!adapter->nameserver_port)
1306 adapter->nameserver_port = port;
1307 adapter->ports++;
1308 write_unlock_irq(&zfcp_data.config_lock);
1309
1310 zfcp_adapter_get(adapter);
1311
1312 return port;
1313}
1314
1315void
1316zfcp_port_dequeue(struct zfcp_port *port)
1317{
1318 zfcp_port_wait(port);
1319 write_lock_irq(&zfcp_data.config_lock);
1320 list_del(&port->list);
1321 port->adapter->ports--;
1322 write_unlock_irq(&zfcp_data.config_lock);
Andreas Herrmann3859f6a2005-08-27 11:07:54 -07001323 if (port->rport)
Heiko Carstens20b17302005-08-28 13:22:37 -07001324 fc_remote_port_delete(port->rport);
1325 port->rport = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 zfcp_adapter_put(port->adapter);
1327 zfcp_sysfs_port_remove_files(&port->sysfs_device,
1328 atomic_read(&port->status));
1329 device_unregister(&port->sysfs_device);
1330}
1331
1332/* Enqueues a nameserver port */
1333int
1334zfcp_nameserver_enqueue(struct zfcp_adapter *adapter)
1335{
1336 struct zfcp_port *port;
1337
1338 port = zfcp_port_enqueue(adapter, 0, ZFCP_STATUS_PORT_WKA,
1339 ZFCP_DID_DIRECTORY_SERVICE);
1340 if (!port) {
1341 ZFCP_LOG_INFO("error: enqueue of nameserver port for "
1342 "adapter %s failed\n",
1343 zfcp_get_busid_by_adapter(adapter));
1344 return -ENXIO;
1345 }
1346 zfcp_port_put(port);
1347
1348 return 0;
1349}
1350
1351#undef ZFCP_LOG_AREA
1352
1353/****************************************************************/
1354/******* Fibre Channel Standard related Functions **************/
1355/****************************************************************/
1356
1357#define ZFCP_LOG_AREA ZFCP_LOG_AREA_FC
1358
Martin Peschke9467a9b2008-03-27 14:22:03 +01001359static void zfcp_fsf_incoming_els_rscn(struct zfcp_fsf_req *fsf_req)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360{
Martin Peschke9467a9b2008-03-27 14:22:03 +01001361 struct fsf_status_read_buffer *status_buffer = (void*)fsf_req->data;
1362 struct zfcp_adapter *adapter = fsf_req->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 struct fcp_rscn_head *fcp_rscn_head;
1364 struct fcp_rscn_element *fcp_rscn_element;
1365 struct zfcp_port *port;
1366 u16 i;
1367 u16 no_entries;
1368 u32 range_mask;
1369 unsigned long flags;
1370
1371 fcp_rscn_head = (struct fcp_rscn_head *) status_buffer->payload;
1372 fcp_rscn_element = (struct fcp_rscn_element *) status_buffer->payload;
1373
1374 /* see FC-FS */
1375 no_entries = (fcp_rscn_head->payload_len / 4);
1376
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 for (i = 1; i < no_entries; i++) {
1378 /* skip head and start with 1st element */
1379 fcp_rscn_element++;
1380 switch (fcp_rscn_element->addr_format) {
1381 case ZFCP_PORT_ADDRESS:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 range_mask = ZFCP_PORTS_RANGE_PORT;
1383 break;
1384 case ZFCP_AREA_ADDRESS:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 range_mask = ZFCP_PORTS_RANGE_AREA;
1386 break;
1387 case ZFCP_DOMAIN_ADDRESS:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 range_mask = ZFCP_PORTS_RANGE_DOMAIN;
1389 break;
1390 case ZFCP_FABRIC_ADDRESS:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 range_mask = ZFCP_PORTS_RANGE_FABRIC;
1392 break;
1393 default:
1394 ZFCP_LOG_INFO("incoming RSCN with unknown "
1395 "address format\n");
1396 continue;
1397 }
1398 read_lock_irqsave(&zfcp_data.config_lock, flags);
1399 list_for_each_entry(port, &adapter->port_list_head, list) {
1400 if (atomic_test_mask
1401 (ZFCP_STATUS_PORT_WKA, &port->status))
1402 continue;
1403 /* Do we know this port? If not skip it. */
1404 if (!atomic_test_mask
1405 (ZFCP_STATUS_PORT_DID_DID, &port->status)) {
1406 ZFCP_LOG_INFO("incoming RSCN, trying to open "
1407 "port 0x%016Lx\n", port->wwpn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 zfcp_erp_port_reopen(port,
Martin Peschke9467a9b2008-03-27 14:22:03 +01001409 ZFCP_STATUS_COMMON_ERP_FAILED,
Martin Peschke1f6f7122008-04-18 12:51:55 +02001410 82, fsf_req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 continue;
1412 }
1413
1414 /*
1415 * FIXME: race: d_id might being invalidated
1416 * (...DID_DID reset)
1417 */
1418 if ((port->d_id & range_mask)
1419 == (fcp_rscn_element->nport_did & range_mask)) {
1420 ZFCP_LOG_TRACE("reopen did 0x%08x\n",
1421 fcp_rscn_element->nport_did);
1422 /*
1423 * Unfortunately, an RSCN does not specify the
1424 * type of change a target underwent. We assume
1425 * that it makes sense to reopen the link.
1426 * FIXME: Shall we try to find out more about
1427 * the target and link state before closing it?
1428 * How to accomplish this? (nameserver?)
1429 * Where would such code be put in?
1430 * (inside or outside erp)
1431 */
1432 ZFCP_LOG_INFO("incoming RSCN, trying to open "
1433 "port 0x%016Lx\n", port->wwpn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 zfcp_test_link(port);
1435 }
1436 }
1437 read_unlock_irqrestore(&zfcp_data.config_lock, flags);
1438 }
1439}
1440
Martin Peschke9467a9b2008-03-27 14:22:03 +01001441static void zfcp_fsf_incoming_els_plogi(struct zfcp_fsf_req *fsf_req)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442{
Martin Peschke9467a9b2008-03-27 14:22:03 +01001443 struct fsf_status_read_buffer *status_buffer = (void*)fsf_req->data;
1444 struct zfcp_adapter *adapter = fsf_req->adapter;
Andreas Herrmann06506d02006-05-22 18:18:19 +02001445 struct fsf_plogi *els_plogi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 struct zfcp_port *port;
1447 unsigned long flags;
1448
Andreas Herrmann06506d02006-05-22 18:18:19 +02001449 els_plogi = (struct fsf_plogi *) status_buffer->payload;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 read_lock_irqsave(&zfcp_data.config_lock, flags);
1451 list_for_each_entry(port, &adapter->port_list_head, list) {
Andreas Herrmann06506d02006-05-22 18:18:19 +02001452 if (port->wwpn == (*(wwn_t *) &els_plogi->serv_param.wwpn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 break;
1454 }
1455 read_unlock_irqrestore(&zfcp_data.config_lock, flags);
1456
Andreas Herrmann06506d02006-05-22 18:18:19 +02001457 if (!port || (port->wwpn != (*(wwn_t *) &els_plogi->serv_param.wwpn))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 ZFCP_LOG_DEBUG("ignored incoming PLOGI for nonexisting port "
Christof Schmitt1d589ed2007-05-08 11:14:41 +02001459 "with d_id 0x%06x on adapter %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 status_buffer->d_id,
1461 zfcp_get_busid_by_adapter(adapter));
1462 } else {
Martin Peschke1f6f7122008-04-18 12:51:55 +02001463 zfcp_erp_port_forced_reopen(port, 0, 83, fsf_req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 }
1465}
1466
Martin Peschke9467a9b2008-03-27 14:22:03 +01001467static void zfcp_fsf_incoming_els_logo(struct zfcp_fsf_req *fsf_req)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468{
Martin Peschke9467a9b2008-03-27 14:22:03 +01001469 struct fsf_status_read_buffer *status_buffer = (void*)fsf_req->data;
1470 struct zfcp_adapter *adapter = fsf_req->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 struct fcp_logo *els_logo = (struct fcp_logo *) status_buffer->payload;
1472 struct zfcp_port *port;
1473 unsigned long flags;
1474
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 read_lock_irqsave(&zfcp_data.config_lock, flags);
1476 list_for_each_entry(port, &adapter->port_list_head, list) {
1477 if (port->wwpn == els_logo->nport_wwpn)
1478 break;
1479 }
1480 read_unlock_irqrestore(&zfcp_data.config_lock, flags);
1481
1482 if (!port || (port->wwpn != els_logo->nport_wwpn)) {
1483 ZFCP_LOG_DEBUG("ignored incoming LOGO for nonexisting port "
Christof Schmitt1d589ed2007-05-08 11:14:41 +02001484 "with d_id 0x%06x on adapter %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 status_buffer->d_id,
1486 zfcp_get_busid_by_adapter(adapter));
1487 } else {
Martin Peschke1f6f7122008-04-18 12:51:55 +02001488 zfcp_erp_port_forced_reopen(port, 0, 84, fsf_req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 }
1490}
1491
1492static void
1493zfcp_fsf_incoming_els_unknown(struct zfcp_adapter *adapter,
1494 struct fsf_status_read_buffer *status_buffer)
1495{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 ZFCP_LOG_NORMAL("warning: unknown incoming ELS 0x%08x "
1497 "for adapter %s\n", *(u32 *) (status_buffer->payload),
1498 zfcp_get_busid_by_adapter(adapter));
1499
1500}
1501
1502void
1503zfcp_fsf_incoming_els(struct zfcp_fsf_req *fsf_req)
1504{
1505 struct fsf_status_read_buffer *status_buffer;
1506 u32 els_type;
1507 struct zfcp_adapter *adapter;
1508
Andreas Herrmann059c97d2005-09-13 21:47:52 +02001509 status_buffer = (struct fsf_status_read_buffer *) fsf_req->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 els_type = *(u32 *) (status_buffer->payload);
1511 adapter = fsf_req->adapter;
1512
Maxim Shchetynin8a36e452005-09-13 21:50:38 +02001513 zfcp_san_dbf_event_incoming_els(fsf_req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 if (els_type == LS_PLOGI)
Martin Peschke9467a9b2008-03-27 14:22:03 +01001515 zfcp_fsf_incoming_els_plogi(fsf_req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 else if (els_type == LS_LOGO)
Martin Peschke9467a9b2008-03-27 14:22:03 +01001517 zfcp_fsf_incoming_els_logo(fsf_req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 else if ((els_type & 0xffff0000) == LS_RSCN)
1519 /* we are only concerned with the command, not the length */
Martin Peschke9467a9b2008-03-27 14:22:03 +01001520 zfcp_fsf_incoming_els_rscn(fsf_req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 else
1522 zfcp_fsf_incoming_els_unknown(adapter, status_buffer);
1523}
1524
1525
1526/**
1527 * zfcp_gid_pn_buffers_alloc - allocate buffers for GID_PN nameserver request
1528 * @gid_pn: pointer to return pointer to struct zfcp_gid_pn_data
1529 * @pool: pointer to mempool_t if non-null memory pool is used for allocation
1530 */
1531static int
1532zfcp_gid_pn_buffers_alloc(struct zfcp_gid_pn_data **gid_pn, mempool_t *pool)
1533{
1534 struct zfcp_gid_pn_data *data;
1535
1536 if (pool != NULL) {
1537 data = mempool_alloc(pool, GFP_ATOMIC);
1538 if (likely(data != NULL)) {
1539 data->ct.pool = pool;
1540 }
1541 } else {
Heiko Carstens47b87b72007-08-08 10:47:10 +02001542 data = kmem_cache_alloc(zfcp_data.gid_pn_cache, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 }
1544
1545 if (NULL == data)
1546 return -ENOMEM;
1547
1548 memset(data, 0, sizeof(*data));
Heiko Carstensde58d942007-10-24 08:45:35 +02001549 sg_init_table(&data->req , 1);
1550 sg_init_table(&data->resp , 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 data->ct.req = &data->req;
1552 data->ct.resp = &data->resp;
1553 data->ct.req_count = data->ct.resp_count = 1;
Jens Axboe642f149032007-10-24 11:20:47 +02001554 zfcp_address_to_sg(&data->ct_iu_req, &data->req, sizeof(struct ct_iu_gid_pn_req));
1555 zfcp_address_to_sg(&data->ct_iu_resp, &data->resp, sizeof(struct ct_iu_gid_pn_resp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556
1557 *gid_pn = data;
1558 return 0;
1559}
1560
1561/**
1562 * zfcp_gid_pn_buffers_free - free buffers for GID_PN nameserver request
1563 * @gid_pn: pointer to struct zfcp_gid_pn_data which has to be freed
1564 */
Heiko Carstensaa551da2007-07-18 10:55:10 +02001565static void zfcp_gid_pn_buffers_free(struct zfcp_gid_pn_data *gid_pn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566{
Heiko Carstensaa551da2007-07-18 10:55:10 +02001567 if (gid_pn->ct.pool)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 mempool_free(gid_pn, gid_pn->ct.pool);
1569 else
Heiko Carstens47b87b72007-08-08 10:47:10 +02001570 kmem_cache_free(zfcp_data.gid_pn_cache, gid_pn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571}
1572
1573/**
1574 * zfcp_ns_gid_pn_request - initiate GID_PN nameserver request
1575 * @erp_action: pointer to zfcp_erp_action where GID_PN request is needed
1576 */
1577int
1578zfcp_ns_gid_pn_request(struct zfcp_erp_action *erp_action)
1579{
1580 int ret;
1581 struct ct_iu_gid_pn_req *ct_iu_req;
1582 struct zfcp_gid_pn_data *gid_pn;
1583 struct zfcp_adapter *adapter = erp_action->adapter;
1584
1585 ret = zfcp_gid_pn_buffers_alloc(&gid_pn, adapter->pool.data_gid_pn);
1586 if (ret < 0) {
1587 ZFCP_LOG_INFO("error: buffer allocation for gid_pn nameserver "
1588 "request failed for adapter %s\n",
1589 zfcp_get_busid_by_adapter(adapter));
1590 goto out;
1591 }
1592
1593 /* setup nameserver request */
1594 ct_iu_req = zfcp_sg_to_address(gid_pn->ct.req);
1595 ct_iu_req->header.revision = ZFCP_CT_REVISION;
1596 ct_iu_req->header.gs_type = ZFCP_CT_DIRECTORY_SERVICE;
1597 ct_iu_req->header.gs_subtype = ZFCP_CT_NAME_SERVER;
1598 ct_iu_req->header.options = ZFCP_CT_SYNCHRONOUS;
1599 ct_iu_req->header.cmd_rsp_code = ZFCP_CT_GID_PN;
1600 ct_iu_req->header.max_res_size = ZFCP_CT_MAX_SIZE;
1601 ct_iu_req->wwpn = erp_action->port->wwpn;
1602
1603 /* setup parameters for send generic command */
1604 gid_pn->ct.port = adapter->nameserver_port;
1605 gid_pn->ct.handler = zfcp_ns_gid_pn_handler;
1606 gid_pn->ct.handler_data = (unsigned long) gid_pn;
1607 gid_pn->ct.timeout = ZFCP_NS_GID_PN_TIMEOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 gid_pn->port = erp_action->port;
1609
1610 ret = zfcp_fsf_send_ct(&gid_pn->ct, adapter->pool.fsf_req_erp,
1611 erp_action);
1612 if (ret) {
1613 ZFCP_LOG_INFO("error: initiation of gid_pn nameserver request "
1614 "failed for adapter %s\n",
1615 zfcp_get_busid_by_adapter(adapter));
1616
1617 zfcp_gid_pn_buffers_free(gid_pn);
1618 }
1619
1620 out:
1621 return ret;
1622}
1623
1624/**
1625 * zfcp_ns_gid_pn_handler - handler for GID_PN nameserver request
1626 * @data: unsigned long, contains pointer to struct zfcp_gid_pn_data
1627 */
1628static void zfcp_ns_gid_pn_handler(unsigned long data)
1629{
1630 struct zfcp_port *port;
1631 struct zfcp_send_ct *ct;
1632 struct ct_iu_gid_pn_req *ct_iu_req;
1633 struct ct_iu_gid_pn_resp *ct_iu_resp;
1634 struct zfcp_gid_pn_data *gid_pn;
1635
1636
1637 gid_pn = (struct zfcp_gid_pn_data *) data;
1638 port = gid_pn->port;
1639 ct = &gid_pn->ct;
1640 ct_iu_req = zfcp_sg_to_address(ct->req);
1641 ct_iu_resp = zfcp_sg_to_address(ct->resp);
1642
Andreas Herrmann66c86842005-06-13 13:13:45 +02001643 if (ct->status != 0)
1644 goto failed;
1645
1646 if (zfcp_check_ct_response(&ct_iu_resp->header)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 /* FIXME: do we need some specific erp entry points */
1648 atomic_set_mask(ZFCP_STATUS_PORT_INVALID_WWPN, &port->status);
1649 goto failed;
1650 }
1651 /* paranoia */
1652 if (ct_iu_req->wwpn != port->wwpn) {
1653 ZFCP_LOG_NORMAL("bug: wwpn 0x%016Lx returned by nameserver "
1654 "lookup does not match expected wwpn 0x%016Lx "
1655 "for adapter %s\n", ct_iu_req->wwpn, port->wwpn,
1656 zfcp_get_busid_by_port(port));
1657 goto mismatch;
1658 }
1659
1660 /* looks like a valid d_id */
1661 port->d_id = ct_iu_resp->d_id & ZFCP_DID_MASK;
1662 atomic_set_mask(ZFCP_STATUS_PORT_DID_DID, &port->status);
Christof Schmitt1d589ed2007-05-08 11:14:41 +02001663 ZFCP_LOG_DEBUG("adapter %s: wwpn=0x%016Lx ---> d_id=0x%06x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 zfcp_get_busid_by_port(port), port->wwpn, port->d_id);
1665 goto out;
1666
1667 mismatch:
1668 ZFCP_LOG_DEBUG("CT IUs do not match:\n");
1669 ZFCP_HEX_DUMP(ZFCP_LOG_LEVEL_DEBUG, (char *) ct_iu_req,
1670 sizeof(struct ct_iu_gid_pn_req));
1671 ZFCP_HEX_DUMP(ZFCP_LOG_LEVEL_DEBUG, (char *) ct_iu_resp,
1672 sizeof(struct ct_iu_gid_pn_resp));
1673
1674 failed:
1675 ZFCP_LOG_NORMAL("warning: failed gid_pn nameserver request for wwpn "
1676 "0x%016Lx for adapter %s\n",
1677 port->wwpn, zfcp_get_busid_by_port(port));
1678 out:
1679 zfcp_gid_pn_buffers_free(gid_pn);
1680 return;
1681}
1682
1683/* reject CT_IU reason codes acc. to FC-GS-4 */
1684static const struct zfcp_rc_entry zfcp_ct_rc[] = {
1685 {0x01, "invalid command code"},
1686 {0x02, "invalid version level"},
1687 {0x03, "logical error"},
1688 {0x04, "invalid CT_IU size"},
1689 {0x05, "logical busy"},
1690 {0x07, "protocol error"},
1691 {0x09, "unable to perform command request"},
1692 {0x0b, "command not supported"},
1693 {0x0d, "server not available"},
1694 {0x0e, "session could not be established"},
1695 {0xff, "vendor specific error"},
1696 {0, NULL},
1697};
1698
1699/* LS_RJT reason codes acc. to FC-FS */
1700static const struct zfcp_rc_entry zfcp_ls_rjt_rc[] = {
1701 {0x01, "invalid LS_Command code"},
1702 {0x03, "logical error"},
1703 {0x05, "logical busy"},
1704 {0x07, "protocol error"},
1705 {0x09, "unable to perform command request"},
1706 {0x0b, "command not supported"},
1707 {0x0e, "command already in progress"},
1708 {0xff, "vendor specific error"},
1709 {0, NULL},
1710};
1711
1712/* reject reason codes according to FC-PH/FC-FS */
1713static const struct zfcp_rc_entry zfcp_p_rjt_rc[] = {
1714 {0x01, "invalid D_ID"},
1715 {0x02, "invalid S_ID"},
1716 {0x03, "Nx_Port not available, temporary"},
1717 {0x04, "Nx_Port not available, permament"},
1718 {0x05, "class not supported"},
1719 {0x06, "delimiter usage error"},
1720 {0x07, "TYPE not supported"},
1721 {0x08, "invalid Link_Control"},
1722 {0x09, "invalid R_CTL field"},
1723 {0x0a, "invalid F_CTL field"},
1724 {0x0b, "invalid OX_ID"},
1725 {0x0c, "invalid RX_ID"},
1726 {0x0d, "invalid SEQ_ID"},
1727 {0x0e, "invalid DF_CTL"},
1728 {0x0f, "invalid SEQ_CNT"},
1729 {0x10, "invalid parameter field"},
1730 {0x11, "exchange error"},
1731 {0x12, "protocol error"},
1732 {0x13, "incorrect length"},
1733 {0x14, "unsupported ACK"},
1734 {0x15, "class of service not supported by entity at FFFFFE"},
1735 {0x16, "login required"},
1736 {0x17, "excessive sequences attempted"},
1737 {0x18, "unable to establish exchange"},
1738 {0x1a, "fabric path not available"},
1739 {0x1b, "invalid VC_ID (class 4)"},
1740 {0x1c, "invalid CS_CTL field"},
1741 {0x1d, "insufficient resources for VC (class 4)"},
1742 {0x1f, "invalid class of service"},
1743 {0x20, "preemption request rejected"},
1744 {0x21, "preemption not enabled"},
1745 {0x22, "multicast error"},
1746 {0x23, "multicast error terminate"},
1747 {0x24, "process login required"},
1748 {0xff, "vendor specific reject"},
1749 {0, NULL},
1750};
1751
1752/**
1753 * zfcp_rc_description - return description for given reaon code
1754 * @code: reason code
1755 * @rc_table: table of reason codes and descriptions
1756 */
Heiko Carstens4d284ca2007-02-05 21:18:53 +01001757static const char *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758zfcp_rc_description(u8 code, const struct zfcp_rc_entry *rc_table)
1759{
1760 const char *descr = "unknown reason code";
1761
1762 do {
1763 if (code == rc_table->code) {
1764 descr = rc_table->description;
1765 break;
1766 }
1767 rc_table++;
1768 } while (rc_table->code && rc_table->description);
1769
1770 return descr;
1771}
1772
1773/**
1774 * zfcp_check_ct_response - evaluate reason code for CT_IU
1775 * @rjt: response payload to an CT_IU request
1776 * Return: 0 for accept CT_IU, 1 for reject CT_IU or invlid response code
1777 */
1778int
1779zfcp_check_ct_response(struct ct_hdr *rjt)
1780{
1781 if (rjt->cmd_rsp_code == ZFCP_CT_ACCEPT)
1782 return 0;
1783
1784 if (rjt->cmd_rsp_code != ZFCP_CT_REJECT) {
1785 ZFCP_LOG_NORMAL("error: invalid Generic Service command/"
1786 "response code (0x%04hx)\n",
1787 rjt->cmd_rsp_code);
1788 return 1;
1789 }
1790
1791 ZFCP_LOG_INFO("Generic Service command rejected\n");
1792 ZFCP_LOG_INFO("%s (0x%02x, 0x%02x, 0x%02x)\n",
1793 zfcp_rc_description(rjt->reason_code, zfcp_ct_rc),
1794 (u32) rjt->reason_code, (u32) rjt->reason_code_expl,
1795 (u32) rjt->vendor_unique);
1796
1797 return 1;
1798}
1799
1800/**
1801 * zfcp_print_els_rjt - print reject parameter and description for ELS reject
1802 * @rjt_par: reject parameter acc. to FC-PH/FC-FS
1803 * @rc_table: table of reason codes and descriptions
1804 */
Heiko Carstens4d284ca2007-02-05 21:18:53 +01001805static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806zfcp_print_els_rjt(struct zfcp_ls_rjt_par *rjt_par,
1807 const struct zfcp_rc_entry *rc_table)
1808{
1809 ZFCP_LOG_INFO("%s (%02x %02x %02x %02x)\n",
1810 zfcp_rc_description(rjt_par->reason_code, rc_table),
1811 (u32) rjt_par->action, (u32) rjt_par->reason_code,
1812 (u32) rjt_par->reason_expl, (u32) rjt_par->vendor_unique);
1813}
1814
1815/**
1816 * zfcp_fsf_handle_els_rjt - evaluate status qualifier/reason code on ELS reject
1817 * @sq: status qualifier word
1818 * @rjt_par: reject parameter as described in FC-PH and FC-FS
1819 * Return: -EROMTEIO for LS_RJT, -EREMCHG for invalid D_ID, -EIO else
1820 */
1821int
1822zfcp_handle_els_rjt(u32 sq, struct zfcp_ls_rjt_par *rjt_par)
1823{
1824 int ret = -EIO;
1825
1826 if (sq == FSF_IOSTAT_NPORT_RJT) {
1827 ZFCP_LOG_INFO("ELS rejected (P_RJT)\n");
1828 zfcp_print_els_rjt(rjt_par, zfcp_p_rjt_rc);
1829 /* invalid d_id */
1830 if (rjt_par->reason_code == 0x01)
1831 ret = -EREMCHG;
1832 } else if (sq == FSF_IOSTAT_FABRIC_RJT) {
1833 ZFCP_LOG_INFO("ELS rejected (F_RJT)\n");
1834 zfcp_print_els_rjt(rjt_par, zfcp_p_rjt_rc);
1835 /* invalid d_id */
1836 if (rjt_par->reason_code == 0x01)
1837 ret = -EREMCHG;
1838 } else if (sq == FSF_IOSTAT_LS_RJT) {
1839 ZFCP_LOG_INFO("ELS rejected (LS_RJT)\n");
1840 zfcp_print_els_rjt(rjt_par, zfcp_ls_rjt_rc);
1841 ret = -EREMOTEIO;
1842 } else
1843 ZFCP_LOG_INFO("unexpected SQ: 0x%02x\n", sq);
1844
1845 return ret;
1846}
1847
Ralph Wuerthner75bfc282006-05-22 18:24:33 +02001848/**
1849 * zfcp_plogi_evaluate - evaluate PLOGI playload and copy important fields
1850 * into zfcp_port structure
1851 * @port: zfcp_port structure
1852 * @plogi: plogi payload
1853 */
1854void
1855zfcp_plogi_evaluate(struct zfcp_port *port, struct fsf_plogi *plogi)
1856{
1857 port->maxframe_size = plogi->serv_param.common_serv_param[7] |
1858 ((plogi->serv_param.common_serv_param[6] & 0x0F) << 8);
1859 if (plogi->serv_param.class1_serv_param[0] & 0x80)
1860 port->supported_classes |= FC_COS_CLASS1;
1861 if (plogi->serv_param.class2_serv_param[0] & 0x80)
1862 port->supported_classes |= FC_COS_CLASS2;
1863 if (plogi->serv_param.class3_serv_param[0] & 0x80)
1864 port->supported_classes |= FC_COS_CLASS3;
1865 if (plogi->serv_param.class4_serv_param[0] & 0x80)
1866 port->supported_classes |= FC_COS_CLASS4;
1867}
1868
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869#undef ZFCP_LOG_AREA