blob: 9eb8827e19e2c69136d97151412d67b92dc16419 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070046/* miscellaneous */
Heiko Carstens4d284ca2007-02-05 21:18:53 +010047static int zfcp_sg_list_alloc(struct zfcp_sg_list *, size_t);
48static void zfcp_sg_list_free(struct zfcp_sg_list *);
49static int zfcp_sg_list_copy_from_user(struct zfcp_sg_list *,
50 void __user *, size_t);
51static int zfcp_sg_list_copy_to_user(void __user *,
52 struct zfcp_sg_list *, size_t);
Andreas Herrmannbd6ae2f2005-04-21 16:14:31 -040053static long zfcp_cfdc_dev_ioctl(struct file *, unsigned int, unsigned long);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
55#define ZFCP_CFDC_IOC_MAGIC 0xDD
56#define ZFCP_CFDC_IOC \
57 _IOWR(ZFCP_CFDC_IOC_MAGIC, 0, struct zfcp_cfdc_sense_data)
58
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Arjan van de Vend54b1fd2007-02-12 00:55:34 -080060static const struct file_operations zfcp_cfdc_fops = {
e183b06b2005-04-02 13:57:17 -060061 .unlocked_ioctl = zfcp_cfdc_dev_ioctl,
62#ifdef CONFIG_COMPAT
63 .compat_ioctl = zfcp_cfdc_dev_ioctl
64#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070065};
66
67static struct miscdevice zfcp_cfdc_misc = {
68 .minor = ZFCP_CFDC_DEV_MINOR,
69 .name = ZFCP_CFDC_DEV_NAME,
70 .fops = &zfcp_cfdc_fops
71};
72
73/*********************** KERNEL/MODULE PARAMETERS ***************************/
74
75/* declare driver module init/cleanup functions */
76module_init(zfcp_module_init);
77
Andreas Herrmann4a9d2d82006-05-22 18:14:08 +020078MODULE_AUTHOR("IBM Deutschland Entwicklung GmbH - linux390@de.ibm.com");
Linus Torvalds1da177e2005-04-16 15:20:36 -070079MODULE_DESCRIPTION
Andreas Herrmann4a9d2d82006-05-22 18:14:08 +020080 ("FCP (SCSI over Fibre Channel) HBA driver for IBM System z9 and zSeries");
Linus Torvalds1da177e2005-04-16 15:20:36 -070081MODULE_LICENSE("GPL");
82
6f71d9b2005-04-10 23:04:28 -050083module_param(device, charp, 0400);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084MODULE_PARM_DESC(device, "specify initial device");
85
6f71d9b2005-04-10 23:04:28 -050086module_param(loglevel, uint, 0400);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087MODULE_PARM_DESC(loglevel,
88 "log levels, 8 nibbles: "
89 "FC ERP QDIO CIO Config FSF SCSI Other, "
90 "levels: 0=none 1=normal 2=devel 3=trace");
91
Linus Torvalds1da177e2005-04-16 15:20:36 -070092/****************************************************************/
93/************** Functions without logging ***********************/
94/****************************************************************/
95
96void
97_zfcp_hex_dump(char *addr, int count)
98{
99 int i;
100 for (i = 0; i < count; i++) {
101 printk("%02x", addr[i]);
102 if ((i % 4) == 3)
103 printk(" ");
104 if ((i % 32) == 31)
105 printk("\n");
106 }
107 if (((i-1) % 32) != 31)
108 printk("\n");
109}
110
Volker Sameskefea9d6c2006-08-02 11:05:16 +0200111
112/****************************************************************/
113/****** Functions to handle the request ID hash table ********/
114/****************************************************************/
115
116#define ZFCP_LOG_AREA ZFCP_LOG_AREA_FSF
117
Heiko Carstensca2d02c2007-05-08 11:17:54 +0200118static int zfcp_reqlist_alloc(struct zfcp_adapter *adapter)
Volker Sameskefea9d6c2006-08-02 11:05:16 +0200119{
Heiko Carstensca2d02c2007-05-08 11:17:54 +0200120 int idx;
Volker Sameskefea9d6c2006-08-02 11:05:16 +0200121
122 adapter->req_list = kcalloc(REQUEST_LIST_SIZE, sizeof(struct list_head),
123 GFP_KERNEL);
Volker Sameskefea9d6c2006-08-02 11:05:16 +0200124 if (!adapter->req_list)
125 return -ENOMEM;
126
Heiko Carstensca2d02c2007-05-08 11:17:54 +0200127 for (idx = 0; idx < REQUEST_LIST_SIZE; idx++)
128 INIT_LIST_HEAD(&adapter->req_list[idx]);
Volker Sameskefea9d6c2006-08-02 11:05:16 +0200129 return 0;
130}
131
132static void zfcp_reqlist_free(struct zfcp_adapter *adapter)
133{
Volker Sameskefea9d6c2006-08-02 11:05:16 +0200134 kfree(adapter->req_list);
135}
136
Volker Sameskefea9d6c2006-08-02 11:05:16 +0200137int zfcp_reqlist_isempty(struct zfcp_adapter *adapter)
138{
Heiko Carstensca2d02c2007-05-08 11:17:54 +0200139 unsigned int idx;
Volker Sameskefea9d6c2006-08-02 11:05:16 +0200140
Heiko Carstensca2d02c2007-05-08 11:17:54 +0200141 for (idx = 0; idx < REQUEST_LIST_SIZE; idx++)
142 if (!list_empty(&adapter->req_list[idx]))
Volker Sameskefea9d6c2006-08-02 11:05:16 +0200143 return 0;
Volker Sameskefea9d6c2006-08-02 11:05:16 +0200144 return 1;
145}
146
147#undef ZFCP_LOG_AREA
148
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149/****************************************************************/
150/************** Uncategorised Functions *************************/
151/****************************************************************/
152
153#define ZFCP_LOG_AREA ZFCP_LOG_AREA_OTHER
154
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155/**
156 * zfcp_device_setup - setup function
157 * @str: pointer to parameter string
158 *
159 * Parse "device=..." parameter string.
160 */
161static int __init
Andreas Herrmanncd8a3832005-06-13 13:22:25 +0200162zfcp_device_setup(char *devstr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163{
Andreas Herrmanncd8a3832005-06-13 13:22:25 +0200164 char *tmp, *str;
165 size_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
Andreas Herrmanncd8a3832005-06-13 13:22:25 +0200167 if (!devstr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 return 0;
169
Andreas Herrmanncd8a3832005-06-13 13:22:25 +0200170 len = strlen(devstr) + 1;
Robert P. J. Day5cbded52006-12-13 00:35:56 -0800171 str = kmalloc(len, GFP_KERNEL);
Andreas Herrmanncd8a3832005-06-13 13:22:25 +0200172 if (!str)
173 goto err_out;
174 memcpy(str, devstr, len);
175
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 tmp = strchr(str, ',');
177 if (!tmp)
178 goto err_out;
179 *tmp++ = '\0';
180 strncpy(zfcp_data.init_busid, str, BUS_ID_SIZE);
181 zfcp_data.init_busid[BUS_ID_SIZE-1] = '\0';
182
183 zfcp_data.init_wwpn = simple_strtoull(tmp, &tmp, 0);
184 if (*tmp++ != ',')
185 goto err_out;
186 if (*tmp == '\0')
187 goto err_out;
188
189 zfcp_data.init_fcp_lun = simple_strtoull(tmp, &tmp, 0);
190 if (*tmp != '\0')
191 goto err_out;
Andreas Herrmanncd8a3832005-06-13 13:22:25 +0200192 kfree(str);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 return 1;
194
195 err_out:
196 ZFCP_LOG_NORMAL("Parse error for device parameter string %s\n", str);
Andreas Herrmanncd8a3832005-06-13 13:22:25 +0200197 kfree(str);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 return 0;
199}
200
201static void __init
202zfcp_init_device_configure(void)
203{
204 struct zfcp_adapter *adapter;
205 struct zfcp_port *port;
206 struct zfcp_unit *unit;
207
208 down(&zfcp_data.config_sema);
209 read_lock_irq(&zfcp_data.config_lock);
210 adapter = zfcp_get_adapter_by_busid(zfcp_data.init_busid);
211 if (adapter)
212 zfcp_adapter_get(adapter);
213 read_unlock_irq(&zfcp_data.config_lock);
214
215 if (adapter == NULL)
216 goto out_adapter;
217 port = zfcp_port_enqueue(adapter, zfcp_data.init_wwpn, 0, 0);
218 if (!port)
219 goto out_port;
220 unit = zfcp_unit_enqueue(port, zfcp_data.init_fcp_lun);
221 if (!unit)
222 goto out_unit;
223 up(&zfcp_data.config_sema);
224 ccw_device_set_online(adapter->ccw_device);
225 zfcp_erp_wait(adapter);
226 down(&zfcp_data.config_sema);
227 zfcp_unit_put(unit);
228 out_unit:
229 zfcp_port_put(port);
230 out_port:
231 zfcp_adapter_put(adapter);
232 out_adapter:
233 up(&zfcp_data.config_sema);
234 return;
235}
236
Heiko Carstensdd52e0e2006-09-18 22:28:49 +0200237static int calc_alignment(int size)
238{
239 int align = 1;
240
241 if (!size)
242 return 0;
243
244 while ((size - align) > 0)
245 align <<= 1;
246
247 return align;
248}
249
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250static int __init
251zfcp_module_init(void)
252{
Heiko Carstensdd52e0e2006-09-18 22:28:49 +0200253 int retval = -ENOMEM;
254 int size, align;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
Heiko Carstensdd52e0e2006-09-18 22:28:49 +0200256 size = sizeof(struct zfcp_fsf_req_qtcb);
257 align = calc_alignment(size);
258 zfcp_data.fsf_req_qtcb_cache =
Paul Mundt20c2df82007-07-20 10:11:58 +0900259 kmem_cache_create("zfcp_fsf", size, align, 0, NULL);
Heiko Carstensdd52e0e2006-09-18 22:28:49 +0200260 if (!zfcp_data.fsf_req_qtcb_cache)
261 goto out;
262
263 size = sizeof(struct fsf_status_read_buffer);
264 align = calc_alignment(size);
265 zfcp_data.sr_buffer_cache =
Paul Mundt20c2df82007-07-20 10:11:58 +0900266 kmem_cache_create("zfcp_sr", size, align, 0, NULL);
Heiko Carstensdd52e0e2006-09-18 22:28:49 +0200267 if (!zfcp_data.sr_buffer_cache)
268 goto out_sr_cache;
269
270 size = sizeof(struct zfcp_gid_pn_data);
271 align = calc_alignment(size);
272 zfcp_data.gid_pn_cache =
Paul Mundt20c2df82007-07-20 10:11:58 +0900273 kmem_cache_create("zfcp_gid", size, align, 0, NULL);
Heiko Carstensdd52e0e2006-09-18 22:28:49 +0200274 if (!zfcp_data.gid_pn_cache)
275 goto out_gid_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
277 atomic_set(&zfcp_data.loglevel, loglevel);
278
279 /* initialize adapter list */
280 INIT_LIST_HEAD(&zfcp_data.adapter_list_head);
281
282 /* initialize adapters to be removed list head */
283 INIT_LIST_HEAD(&zfcp_data.adapter_remove_lh);
284
Heiko Carstensdd52e0e2006-09-18 22:28:49 +0200285 zfcp_data.scsi_transport_template =
286 fc_attach_transport(&zfcp_transport_functions);
287 if (!zfcp_data.scsi_transport_template)
288 goto out_transport;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 retval = misc_register(&zfcp_cfdc_misc);
291 if (retval != 0) {
292 ZFCP_LOG_INFO("registration of misc device "
293 "zfcp_cfdc failed\n");
Heiko Carstensdd52e0e2006-09-18 22:28:49 +0200294 goto out_misc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 }
296
e183b06b2005-04-02 13:57:17 -0600297 ZFCP_LOG_TRACE("major/minor for zfcp_cfdc: %d/%d\n",
298 ZFCP_CFDC_DEV_MAJOR, zfcp_cfdc_misc.minor);
299
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 /* Initialise proc semaphores */
301 sema_init(&zfcp_data.config_sema, 1);
302
303 /* initialise configuration rw lock */
304 rwlock_init(&zfcp_data.config_lock);
305
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 /* setup dynamic I/O */
307 retval = zfcp_ccw_register();
308 if (retval) {
309 ZFCP_LOG_NORMAL("registration with common I/O layer failed\n");
310 goto out_ccw_register;
311 }
312
313 if (zfcp_device_setup(device))
314 zfcp_init_device_configure();
315
316 goto out;
317
318 out_ccw_register:
319 misc_deregister(&zfcp_cfdc_misc);
Heiko Carstensdd52e0e2006-09-18 22:28:49 +0200320 out_misc:
321 fc_release_transport(zfcp_data.scsi_transport_template);
322 out_transport:
323 kmem_cache_destroy(zfcp_data.gid_pn_cache);
324 out_gid_cache:
325 kmem_cache_destroy(zfcp_data.sr_buffer_cache);
326 out_sr_cache:
327 kmem_cache_destroy(zfcp_data.fsf_req_qtcb_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 out:
329 return retval;
330}
331
332/*
333 * function: zfcp_cfdc_dev_ioctl
334 *
335 * purpose: Handle control file upload/download transaction via IOCTL
336 * interface
337 *
338 * returns: 0 - Operation completed successfuly
339 * -ENOTTY - Unknown IOCTL command
340 * -EINVAL - Invalid sense data record
341 * -ENXIO - The FCP adapter is not available
342 * -EOPNOTSUPP - The FCP adapter does not have CFDC support
343 * -ENOMEM - Insufficient memory
344 * -EFAULT - User space memory I/O operation fault
345 * -EPERM - Cannot create or queue FSF request or create SBALs
346 * -ERESTARTSYS- Received signal (is mapped to EAGAIN by VFS)
347 */
e183b06b2005-04-02 13:57:17 -0600348static long
349zfcp_cfdc_dev_ioctl(struct file *file, unsigned int command,
350 unsigned long buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351{
352 struct zfcp_cfdc_sense_data *sense_data, __user *sense_data_user;
353 struct zfcp_adapter *adapter = NULL;
354 struct zfcp_fsf_req *fsf_req = NULL;
355 struct zfcp_sg_list *sg_list = NULL;
356 u32 fsf_command, option;
357 char *bus_id = NULL;
358 int retval = 0;
359
360 sense_data = kmalloc(sizeof(struct zfcp_cfdc_sense_data), GFP_KERNEL);
361 if (sense_data == NULL) {
362 retval = -ENOMEM;
363 goto out;
364 }
365
Andreas Herrmannec4081c2006-05-22 18:17:30 +0200366 sg_list = kzalloc(sizeof(struct zfcp_sg_list), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 if (sg_list == NULL) {
368 retval = -ENOMEM;
369 goto out;
370 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
372 if (command != ZFCP_CFDC_IOC) {
373 ZFCP_LOG_INFO("IOC request code 0x%x invalid\n", command);
374 retval = -ENOTTY;
375 goto out;
376 }
377
378 if ((sense_data_user = (void __user *) buffer) == NULL) {
379 ZFCP_LOG_INFO("sense data record is required\n");
380 retval = -EINVAL;
381 goto out;
382 }
383
384 retval = copy_from_user(sense_data, sense_data_user,
385 sizeof(struct zfcp_cfdc_sense_data));
386 if (retval) {
387 retval = -EFAULT;
388 goto out;
389 }
390
391 if (sense_data->signature != ZFCP_CFDC_SIGNATURE) {
392 ZFCP_LOG_INFO("invalid sense data request signature 0x%08x\n",
393 ZFCP_CFDC_SIGNATURE);
394 retval = -EINVAL;
395 goto out;
396 }
397
398 switch (sense_data->command) {
399
400 case ZFCP_CFDC_CMND_DOWNLOAD_NORMAL:
401 fsf_command = FSF_QTCB_DOWNLOAD_CONTROL_FILE;
402 option = FSF_CFDC_OPTION_NORMAL_MODE;
403 break;
404
405 case ZFCP_CFDC_CMND_DOWNLOAD_FORCE:
406 fsf_command = FSF_QTCB_DOWNLOAD_CONTROL_FILE;
407 option = FSF_CFDC_OPTION_FORCE;
408 break;
409
410 case ZFCP_CFDC_CMND_FULL_ACCESS:
411 fsf_command = FSF_QTCB_DOWNLOAD_CONTROL_FILE;
412 option = FSF_CFDC_OPTION_FULL_ACCESS;
413 break;
414
415 case ZFCP_CFDC_CMND_RESTRICTED_ACCESS:
416 fsf_command = FSF_QTCB_DOWNLOAD_CONTROL_FILE;
417 option = FSF_CFDC_OPTION_RESTRICTED_ACCESS;
418 break;
419
420 case ZFCP_CFDC_CMND_UPLOAD:
421 fsf_command = FSF_QTCB_UPLOAD_CONTROL_FILE;
422 option = 0;
423 break;
424
425 default:
426 ZFCP_LOG_INFO("invalid command code 0x%08x\n",
427 sense_data->command);
428 retval = -EINVAL;
429 goto out;
430 }
431
432 bus_id = kmalloc(BUS_ID_SIZE, GFP_KERNEL);
433 if (bus_id == NULL) {
434 retval = -ENOMEM;
435 goto out;
436 }
437 snprintf(bus_id, BUS_ID_SIZE, "%d.%d.%04x",
438 (sense_data->devno >> 24),
439 (sense_data->devno >> 16) & 0xFF,
440 (sense_data->devno & 0xFFFF));
441
442 read_lock_irq(&zfcp_data.config_lock);
443 adapter = zfcp_get_adapter_by_busid(bus_id);
444 if (adapter)
445 zfcp_adapter_get(adapter);
446 read_unlock_irq(&zfcp_data.config_lock);
447
448 kfree(bus_id);
449
450 if (adapter == NULL) {
451 ZFCP_LOG_INFO("invalid adapter\n");
452 retval = -ENXIO;
453 goto out;
454 }
455
456 if (sense_data->command & ZFCP_CFDC_WITH_CONTROL_FILE) {
457 retval = zfcp_sg_list_alloc(sg_list,
458 ZFCP_CFDC_MAX_CONTROL_FILE_SIZE);
459 if (retval) {
460 retval = -ENOMEM;
461 goto out;
462 }
463 }
464
465 if ((sense_data->command & ZFCP_CFDC_DOWNLOAD) &&
466 (sense_data->command & ZFCP_CFDC_WITH_CONTROL_FILE)) {
467 retval = zfcp_sg_list_copy_from_user(
468 sg_list, &sense_data_user->control_file,
469 ZFCP_CFDC_MAX_CONTROL_FILE_SIZE);
470 if (retval) {
471 retval = -EFAULT;
472 goto out;
473 }
474 }
475
476 retval = zfcp_fsf_control_file(adapter, &fsf_req, fsf_command,
477 option, sg_list);
478 if (retval)
479 goto out;
480
481 if ((fsf_req->qtcb->prefix.prot_status != FSF_PROT_GOOD) &&
482 (fsf_req->qtcb->prefix.prot_status != FSF_PROT_FSF_STATUS_PRESENTED)) {
483 retval = -ENXIO;
484 goto out;
485 }
486
487 sense_data->fsf_status = fsf_req->qtcb->header.fsf_status;
488 memcpy(&sense_data->fsf_status_qual,
489 &fsf_req->qtcb->header.fsf_status_qual,
490 sizeof(union fsf_status_qual));
491 memcpy(&sense_data->payloads, &fsf_req->qtcb->bottom.support.els, 256);
492
493 retval = copy_to_user(sense_data_user, sense_data,
494 sizeof(struct zfcp_cfdc_sense_data));
495 if (retval) {
496 retval = -EFAULT;
497 goto out;
498 }
499
500 if (sense_data->command & ZFCP_CFDC_UPLOAD) {
501 retval = zfcp_sg_list_copy_to_user(
502 &sense_data_user->control_file, sg_list,
503 ZFCP_CFDC_MAX_CONTROL_FILE_SIZE);
504 if (retval) {
505 retval = -EFAULT;
506 goto out;
507 }
508 }
509
510 out:
511 if (fsf_req != NULL)
Andreas Herrmann1db2c9c2005-06-13 13:20:35 +0200512 zfcp_fsf_req_free(fsf_req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
514 if ((adapter != NULL) && (retval != -ENXIO))
515 zfcp_adapter_put(adapter);
516
517 if (sg_list != NULL) {
518 zfcp_sg_list_free(sg_list);
519 kfree(sg_list);
520 }
521
Jesper Juhl17fd6822005-11-07 01:01:30 -0800522 kfree(sense_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
524 return retval;
525}
526
527
528/**
529 * zfcp_sg_list_alloc - create a scatter-gather list of the specified size
530 * @sg_list: structure describing a scatter gather list
531 * @size: size of scatter-gather list
532 * Return: 0 on success, else -ENOMEM
533 *
534 * In sg_list->sg a pointer to the created scatter-gather list is returned,
535 * or NULL if we run out of memory. sg_list->count specifies the number of
536 * elements of the scatter-gather list. The maximum size of a single element
537 * in the scatter-gather list is PAGE_SIZE.
538 */
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100539static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540zfcp_sg_list_alloc(struct zfcp_sg_list *sg_list, size_t size)
541{
542 struct scatterlist *sg;
543 unsigned int i;
544 int retval = 0;
545 void *address;
546
547 BUG_ON(sg_list == NULL);
548
549 sg_list->count = size >> PAGE_SHIFT;
550 if (size & ~PAGE_MASK)
551 sg_list->count++;
Andreas Herrmannec4081c2006-05-22 18:17:30 +0200552 sg_list->sg = kcalloc(sg_list->count, sizeof(struct scatterlist),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 GFP_KERNEL);
554 if (sg_list->sg == NULL) {
555 sg_list->count = 0;
556 retval = -ENOMEM;
557 goto out;
558 }
Jens Axboe73fc4f02007-10-23 09:17:53 +0200559 sg_init_table(sg_list->sg, sg_list->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
561 for (i = 0, sg = sg_list->sg; i < sg_list->count; i++, sg++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 address = (void *) get_zeroed_page(GFP_KERNEL);
563 if (address == NULL) {
564 sg_list->count = i;
565 zfcp_sg_list_free(sg_list);
566 retval = -ENOMEM;
567 goto out;
568 }
Jens Axboe642f1492007-10-24 11:20:47 +0200569 zfcp_address_to_sg(address, sg, min(size, PAGE_SIZE));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 size -= sg->length;
571 }
572
573 out:
574 return retval;
575}
576
577
578/**
579 * zfcp_sg_list_free - free memory of a scatter-gather list
580 * @sg_list: structure describing a scatter-gather list
581 *
582 * Memory for each element in the scatter-gather list is freed.
583 * Finally sg_list->sg is freed itself and sg_list->count is reset.
584 */
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100585static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586zfcp_sg_list_free(struct zfcp_sg_list *sg_list)
587{
588 struct scatterlist *sg;
589 unsigned int i;
590
591 BUG_ON(sg_list == NULL);
592
593 for (i = 0, sg = sg_list->sg; i < sg_list->count; i++, sg++)
594 free_page((unsigned long) zfcp_sg_to_address(sg));
595
596 sg_list->count = 0;
597 kfree(sg_list->sg);
598}
599
600/**
601 * zfcp_sg_size - determine size of a scatter-gather list
602 * @sg: array of (struct scatterlist)
603 * @sg_count: elements in array
604 * Return: size of entire scatter-gather list
605 */
Heiko Carstens763968e2007-05-10 15:45:46 +0200606static size_t zfcp_sg_size(struct scatterlist *sg, unsigned int sg_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607{
608 unsigned int i;
609 struct scatterlist *p;
610 size_t size;
611
612 size = 0;
613 for (i = 0, p = sg; i < sg_count; i++, p++) {
614 BUG_ON(p == NULL);
615 size += p->length;
616 }
617
618 return size;
619}
620
621
622/**
623 * zfcp_sg_list_copy_from_user -copy data from user space to scatter-gather list
624 * @sg_list: structure describing a scatter-gather list
625 * @user_buffer: pointer to buffer in user space
626 * @size: number of bytes to be copied
627 * Return: 0 on success, -EFAULT if copy_from_user fails.
628 */
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100629static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630zfcp_sg_list_copy_from_user(struct zfcp_sg_list *sg_list,
631 void __user *user_buffer,
632 size_t size)
633{
634 struct scatterlist *sg;
635 unsigned int length;
636 void *zfcp_buffer;
637 int retval = 0;
638
639 BUG_ON(sg_list == NULL);
640
641 if (zfcp_sg_size(sg_list->sg, sg_list->count) < size)
642 return -EFAULT;
643
644 for (sg = sg_list->sg; size > 0; sg++) {
645 length = min((unsigned int)size, sg->length);
646 zfcp_buffer = zfcp_sg_to_address(sg);
647 if (copy_from_user(zfcp_buffer, user_buffer, length)) {
648 retval = -EFAULT;
649 goto out;
650 }
651 user_buffer += length;
652 size -= length;
653 }
654
655 out:
656 return retval;
657}
658
659
660/**
661 * zfcp_sg_list_copy_to_user - copy data from scatter-gather list to user space
662 * @user_buffer: pointer to buffer in user space
663 * @sg_list: structure describing a scatter-gather list
664 * @size: number of bytes to be copied
665 * Return: 0 on success, -EFAULT if copy_to_user fails
666 */
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100667static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668zfcp_sg_list_copy_to_user(void __user *user_buffer,
669 struct zfcp_sg_list *sg_list,
670 size_t size)
671{
672 struct scatterlist *sg;
673 unsigned int length;
674 void *zfcp_buffer;
675 int retval = 0;
676
677 BUG_ON(sg_list == NULL);
678
679 if (zfcp_sg_size(sg_list->sg, sg_list->count) < size)
680 return -EFAULT;
681
682 for (sg = sg_list->sg; size > 0; sg++) {
683 length = min((unsigned int) size, sg->length);
684 zfcp_buffer = zfcp_sg_to_address(sg);
685 if (copy_to_user(user_buffer, zfcp_buffer, length)) {
686 retval = -EFAULT;
687 goto out;
688 }
689 user_buffer += length;
690 size -= length;
691 }
692
693 out:
694 return retval;
695}
696
697
698#undef ZFCP_LOG_AREA
699
700/****************************************************************/
701/****** Functions for configuration/set-up of structures ********/
702/****************************************************************/
703
704#define ZFCP_LOG_AREA ZFCP_LOG_AREA_CONFIG
705
706/**
707 * zfcp_get_unit_by_lun - find unit in unit list of port by FCP LUN
708 * @port: pointer to port to search for unit
709 * @fcp_lun: FCP LUN to search for
710 * Traverse list of all units of a port and return pointer to a unit
711 * with the given FCP LUN.
712 */
713struct zfcp_unit *
714zfcp_get_unit_by_lun(struct zfcp_port *port, fcp_lun_t fcp_lun)
715{
716 struct zfcp_unit *unit;
717 int found = 0;
718
719 list_for_each_entry(unit, &port->unit_list_head, list) {
720 if ((unit->fcp_lun == fcp_lun) &&
721 !atomic_test_mask(ZFCP_STATUS_COMMON_REMOVE, &unit->status))
722 {
723 found = 1;
724 break;
725 }
726 }
727 return found ? unit : NULL;
728}
729
730/**
731 * zfcp_get_port_by_wwpn - find port in port list of adapter by wwpn
732 * @adapter: pointer to adapter to search for port
733 * @wwpn: wwpn to search for
734 * Traverse list of all ports of an adapter and return pointer to a port
735 * with the given wwpn.
736 */
737struct zfcp_port *
738zfcp_get_port_by_wwpn(struct zfcp_adapter *adapter, wwn_t wwpn)
739{
740 struct zfcp_port *port;
741 int found = 0;
742
743 list_for_each_entry(port, &adapter->port_list_head, list) {
744 if ((port->wwpn == wwpn) &&
745 !(atomic_read(&port->status) &
746 (ZFCP_STATUS_PORT_NO_WWPN | ZFCP_STATUS_COMMON_REMOVE))) {
747 found = 1;
748 break;
749 }
750 }
751 return found ? port : NULL;
752}
753
754/**
755 * zfcp_get_port_by_did - find port in port list of adapter by d_id
756 * @adapter: pointer to adapter to search for port
757 * @d_id: d_id to search for
758 * Traverse list of all ports of an adapter and return pointer to a port
759 * with the given d_id.
760 */
761struct zfcp_port *
762zfcp_get_port_by_did(struct zfcp_adapter *adapter, u32 d_id)
763{
764 struct zfcp_port *port;
765 int found = 0;
766
767 list_for_each_entry(port, &adapter->port_list_head, list) {
768 if ((port->d_id == d_id) &&
769 !atomic_test_mask(ZFCP_STATUS_COMMON_REMOVE, &port->status))
770 {
771 found = 1;
772 break;
773 }
774 }
775 return found ? port : NULL;
776}
777
778/**
779 * zfcp_get_adapter_by_busid - find adpater in adapter list by bus_id
780 * @bus_id: bus_id to search for
781 * Traverse list of all adapters and return pointer to an adapter
782 * with the given bus_id.
783 */
784struct zfcp_adapter *
785zfcp_get_adapter_by_busid(char *bus_id)
786{
787 struct zfcp_adapter *adapter;
788 int found = 0;
789
790 list_for_each_entry(adapter, &zfcp_data.adapter_list_head, list) {
791 if ((strncmp(bus_id, zfcp_get_busid_by_adapter(adapter),
792 BUS_ID_SIZE) == 0) &&
793 !atomic_test_mask(ZFCP_STATUS_COMMON_REMOVE,
794 &adapter->status)){
795 found = 1;
796 break;
797 }
798 }
799 return found ? adapter : NULL;
800}
801
802/**
803 * zfcp_unit_enqueue - enqueue unit to unit list of a port.
804 * @port: pointer to port where unit is added
805 * @fcp_lun: FCP LUN of unit to be enqueued
806 * Return: pointer to enqueued unit on success, NULL on error
807 * Locks: config_sema must be held to serialize changes to the unit list
808 *
809 * Sets up some unit internal structures and creates sysfs entry.
810 */
811struct zfcp_unit *
812zfcp_unit_enqueue(struct zfcp_port *port, fcp_lun_t fcp_lun)
813{
Christof Schmitt462b7852007-06-19 10:25:30 +0200814 struct zfcp_unit *unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
816 /*
817 * check that there is no unit with this FCP_LUN already in list
818 * and enqueue it.
819 * Note: Unlike for the adapter and the port, this is an error
820 */
821 read_lock_irq(&zfcp_data.config_lock);
822 unit = zfcp_get_unit_by_lun(port, fcp_lun);
823 read_unlock_irq(&zfcp_data.config_lock);
824 if (unit)
825 return NULL;
826
Andreas Herrmannec4081c2006-05-22 18:17:30 +0200827 unit = kzalloc(sizeof (struct zfcp_unit), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 if (!unit)
829 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830
831 /* initialise reference count stuff */
832 atomic_set(&unit->refcount, 0);
833 init_waitqueue_head(&unit->remove_wq);
834
835 unit->port = port;
836 unit->fcp_lun = fcp_lun;
837
838 /* setup for sysfs registration */
839 snprintf(unit->sysfs_device.bus_id, BUS_ID_SIZE, "0x%016llx", fcp_lun);
840 unit->sysfs_device.parent = &port->sysfs_device;
841 unit->sysfs_device.release = zfcp_sysfs_unit_release;
842 dev_set_drvdata(&unit->sysfs_device, unit);
843
844 /* mark unit unusable as long as sysfs registration is not complete */
845 atomic_set_mask(ZFCP_STATUS_COMMON_REMOVE, &unit->status);
846
Christof Schmittc9615852008-05-06 11:00:05 +0200847 spin_lock_init(&unit->latencies.lock);
848 unit->latencies.write.channel.min = 0xFFFFFFFF;
849 unit->latencies.write.fabric.min = 0xFFFFFFFF;
850 unit->latencies.read.channel.min = 0xFFFFFFFF;
851 unit->latencies.read.fabric.min = 0xFFFFFFFF;
852 unit->latencies.cmd.channel.min = 0xFFFFFFFF;
853 unit->latencies.cmd.fabric.min = 0xFFFFFFFF;
854
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 if (device_register(&unit->sysfs_device)) {
856 kfree(unit);
857 return NULL;
858 }
859
860 if (zfcp_sysfs_unit_create_files(&unit->sysfs_device)) {
861 device_unregister(&unit->sysfs_device);
862 return NULL;
863 }
864
865 zfcp_unit_get(unit);
Christof Schmitt462b7852007-06-19 10:25:30 +0200866 unit->scsi_lun = scsilun_to_int((struct scsi_lun *)&unit->fcp_lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 write_lock_irq(&zfcp_data.config_lock);
Christof Schmitt462b7852007-06-19 10:25:30 +0200869 list_add_tail(&unit->list, &port->unit_list_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 atomic_clear_mask(ZFCP_STATUS_COMMON_REMOVE, &unit->status);
871 atomic_set_mask(ZFCP_STATUS_COMMON_RUNNING, &unit->status);
872 write_unlock_irq(&zfcp_data.config_lock);
873
874 port->units++;
875 zfcp_port_get(port);
876
877 return unit;
878}
879
880void
881zfcp_unit_dequeue(struct zfcp_unit *unit)
882{
883 zfcp_unit_wait(unit);
884 write_lock_irq(&zfcp_data.config_lock);
885 list_del(&unit->list);
886 write_unlock_irq(&zfcp_data.config_lock);
887 unit->port->units--;
888 zfcp_port_put(unit->port);
889 zfcp_sysfs_unit_remove_files(&unit->sysfs_device);
890 device_unregister(&unit->sysfs_device);
891}
892
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893/*
894 * Allocates a combined QTCB/fsf_req buffer for erp actions and fcp/SCSI
895 * commands.
Swen Schillig41fa2ad2007-09-07 09:15:31 +0200896 * It also genrates fcp-nameserver request/response buffer and unsolicited
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 * status read fsf_req buffers.
898 *
899 * locks: must only be called with zfcp_data.config_sema taken
900 */
901static int
902zfcp_allocate_low_mem_buffers(struct zfcp_adapter *adapter)
903{
904 adapter->pool.fsf_req_erp =
Heiko Carstensdd52e0e2006-09-18 22:28:49 +0200905 mempool_create_slab_pool(ZFCP_POOL_FSF_REQ_ERP_NR,
906 zfcp_data.fsf_req_qtcb_cache);
Matthew Dobson0eaae62a2006-03-26 01:37:47 -0800907 if (!adapter->pool.fsf_req_erp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 return -ENOMEM;
909
910 adapter->pool.fsf_req_scsi =
Heiko Carstensdd52e0e2006-09-18 22:28:49 +0200911 mempool_create_slab_pool(ZFCP_POOL_FSF_REQ_SCSI_NR,
912 zfcp_data.fsf_req_qtcb_cache);
Matthew Dobson0eaae62a2006-03-26 01:37:47 -0800913 if (!adapter->pool.fsf_req_scsi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 return -ENOMEM;
915
916 adapter->pool.fsf_req_abort =
Heiko Carstensdd52e0e2006-09-18 22:28:49 +0200917 mempool_create_slab_pool(ZFCP_POOL_FSF_REQ_ABORT_NR,
918 zfcp_data.fsf_req_qtcb_cache);
Matthew Dobson0eaae62a2006-03-26 01:37:47 -0800919 if (!adapter->pool.fsf_req_abort)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 return -ENOMEM;
921
922 adapter->pool.fsf_req_status_read =
Matthew Dobson0eaae62a2006-03-26 01:37:47 -0800923 mempool_create_kmalloc_pool(ZFCP_POOL_STATUS_READ_NR,
924 sizeof(struct zfcp_fsf_req));
925 if (!adapter->pool.fsf_req_status_read)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 return -ENOMEM;
927
928 adapter->pool.data_status_read =
Heiko Carstensdd52e0e2006-09-18 22:28:49 +0200929 mempool_create_slab_pool(ZFCP_POOL_STATUS_READ_NR,
930 zfcp_data.sr_buffer_cache);
Matthew Dobson0eaae62a2006-03-26 01:37:47 -0800931 if (!adapter->pool.data_status_read)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 return -ENOMEM;
933
934 adapter->pool.data_gid_pn =
Heiko Carstensdd52e0e2006-09-18 22:28:49 +0200935 mempool_create_slab_pool(ZFCP_POOL_DATA_GID_PN_NR,
936 zfcp_data.gid_pn_cache);
Matthew Dobson0eaae62a2006-03-26 01:37:47 -0800937 if (!adapter->pool.data_gid_pn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 return -ENOMEM;
939
940 return 0;
941}
942
943/**
944 * zfcp_free_low_mem_buffers - free memory pools of an adapter
945 * @adapter: pointer to zfcp_adapter for which memory pools should be freed
946 * locking: zfcp_data.config_sema must be held
947 */
948static void
949zfcp_free_low_mem_buffers(struct zfcp_adapter *adapter)
950{
951 if (adapter->pool.fsf_req_erp)
952 mempool_destroy(adapter->pool.fsf_req_erp);
953 if (adapter->pool.fsf_req_scsi)
954 mempool_destroy(adapter->pool.fsf_req_scsi);
955 if (adapter->pool.fsf_req_abort)
956 mempool_destroy(adapter->pool.fsf_req_abort);
957 if (adapter->pool.fsf_req_status_read)
958 mempool_destroy(adapter->pool.fsf_req_status_read);
959 if (adapter->pool.data_status_read)
960 mempool_destroy(adapter->pool.data_status_read);
961 if (adapter->pool.data_gid_pn)
962 mempool_destroy(adapter->pool.data_gid_pn);
963}
964
Heiko Carstens763968e2007-05-10 15:45:46 +0200965static void zfcp_dummy_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966{
967 return;
968}
969
Swen Schilligd26ab062008-05-19 12:17:37 +0200970int zfcp_status_read_refill(struct zfcp_adapter *adapter)
971{
972 while (atomic_read(&adapter->stat_miss) > 0)
973 if (zfcp_fsf_status_read(adapter, ZFCP_WAIT_FOR_SBAL))
974 break;
975 else
976 atomic_dec(&adapter->stat_miss);
977
978 if (ZFCP_STATUS_READS_RECOM <= atomic_read(&adapter->stat_miss)) {
979 zfcp_erp_adapter_reopen(adapter, 0, 103, NULL);
980 return 1;
981 }
982 return 0;
983}
984
985static void _zfcp_status_read_scheduler(struct work_struct *work)
986{
987 zfcp_status_read_refill(container_of(work, struct zfcp_adapter,
988 stat_work));
989}
990
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991/*
992 * Enqueues an adapter at the end of the adapter list in the driver data.
993 * All adapter internal structures are set up.
994 * Proc-fs entries are also created.
995 *
996 * returns: 0 if a new adapter was successfully enqueued
997 * ZFCP_KNOWN if an adapter with this devno was already present
998 * -ENOMEM if alloc failed
999 * locks: config_sema must be held to serialise changes to the adapter list
1000 */
1001struct zfcp_adapter *
1002zfcp_adapter_enqueue(struct ccw_device *ccw_device)
1003{
1004 int retval = 0;
1005 struct zfcp_adapter *adapter;
1006
1007 /*
Swen Schillig41fa2ad2007-09-07 09:15:31 +02001008 * Note: It is safe to release the list_lock, as any list changes
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 * are protected by the config_sema, which must be held to get here
1010 */
1011
1012 /* try to allocate new adapter data structure (zeroed) */
Andreas Herrmannec4081c2006-05-22 18:17:30 +02001013 adapter = kzalloc(sizeof (struct zfcp_adapter), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 if (!adapter) {
1015 ZFCP_LOG_INFO("error: allocation of base adapter "
1016 "structure failed\n");
1017 goto out;
1018 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019
1020 ccw_device->handler = NULL;
1021
1022 /* save ccw_device pointer */
1023 adapter->ccw_device = ccw_device;
1024
1025 retval = zfcp_qdio_allocate_queues(adapter);
1026 if (retval)
1027 goto queues_alloc_failed;
1028
1029 retval = zfcp_qdio_allocate(adapter);
1030 if (retval)
1031 goto qdio_allocate_failed;
1032
1033 retval = zfcp_allocate_low_mem_buffers(adapter);
1034 if (retval) {
1035 ZFCP_LOG_INFO("error: pool allocation failed\n");
1036 goto failed_low_mem_buffers;
1037 }
1038
1039 /* initialise reference count stuff */
1040 atomic_set(&adapter->refcount, 0);
1041 init_waitqueue_head(&adapter->remove_wq);
1042
1043 /* initialise list of ports */
1044 INIT_LIST_HEAD(&adapter->port_list_head);
1045
1046 /* initialise list of ports to be removed */
1047 INIT_LIST_HEAD(&adapter->port_remove_lh);
1048
1049 /* initialize list of fsf requests */
Volker Sameskefea9d6c2006-08-02 11:05:16 +02001050 spin_lock_init(&adapter->req_list_lock);
Heiko Carstensca2d02c2007-05-08 11:17:54 +02001051 retval = zfcp_reqlist_alloc(adapter);
Volker Sameskefea9d6c2006-08-02 11:05:16 +02001052 if (retval) {
1053 ZFCP_LOG_INFO("request list initialization failed\n");
1054 goto failed_low_mem_buffers;
1055 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056
Heiko Carstensc48a29d2005-12-01 02:46:32 +01001057 /* initialize debug locks */
1058
Heiko Carstensc48a29d2005-12-01 02:46:32 +01001059 spin_lock_init(&adapter->hba_dbf_lock);
1060 spin_lock_init(&adapter->san_dbf_lock);
1061 spin_lock_init(&adapter->scsi_dbf_lock);
Martin Peschked79a83d2008-03-27 14:22:00 +01001062 spin_lock_init(&adapter->rec_dbf_lock);
Heiko Carstensc48a29d2005-12-01 02:46:32 +01001063
Christof Schmittff17a292007-08-28 09:31:41 +02001064 retval = zfcp_adapter_debug_register(adapter);
1065 if (retval)
1066 goto debug_register_failed;
1067
Heiko Carstensc48a29d2005-12-01 02:46:32 +01001068 /* initialize error recovery stuff */
1069
1070 rwlock_init(&adapter->erp_lock);
1071 sema_init(&adapter->erp_ready_sem, 0);
1072 INIT_LIST_HEAD(&adapter->erp_ready_head);
1073 INIT_LIST_HEAD(&adapter->erp_running_head);
1074
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 /* initialize abort lock */
1076 rwlock_init(&adapter->abort_lock);
1077
1078 /* initialise some erp stuff */
1079 init_waitqueue_head(&adapter->erp_thread_wqh);
1080 init_waitqueue_head(&adapter->erp_done_wqh);
1081
1082 /* initialize lock of associated request queue */
1083 rwlock_init(&adapter->request_queue.queue_lock);
Swen Schilligd26ab062008-05-19 12:17:37 +02001084 INIT_WORK(&adapter->stat_work, _zfcp_status_read_scheduler);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 /* mark adapter unusable as long as sysfs registration is not complete */
1087 atomic_set_mask(ZFCP_STATUS_COMMON_REMOVE, &adapter->status);
1088
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 dev_set_drvdata(&ccw_device->dev, adapter);
1090
1091 if (zfcp_sysfs_adapter_create_files(&ccw_device->dev))
1092 goto sysfs_failed;
1093
1094 adapter->generic_services.parent = &adapter->ccw_device->dev;
1095 adapter->generic_services.release = zfcp_dummy_release;
1096 snprintf(adapter->generic_services.bus_id, BUS_ID_SIZE,
1097 "generic_services");
1098
1099 if (device_register(&adapter->generic_services))
1100 goto generic_services_failed;
1101
1102 /* put allocated adapter at list tail */
1103 write_lock_irq(&zfcp_data.config_lock);
1104 atomic_clear_mask(ZFCP_STATUS_COMMON_REMOVE, &adapter->status);
1105 list_add_tail(&adapter->list, &zfcp_data.adapter_list_head);
1106 write_unlock_irq(&zfcp_data.config_lock);
1107
1108 zfcp_data.adapters++;
1109
1110 goto out;
1111
1112 generic_services_failed:
1113 zfcp_sysfs_adapter_remove_files(&adapter->ccw_device->dev);
1114 sysfs_failed:
Christof Schmittff17a292007-08-28 09:31:41 +02001115 zfcp_adapter_debug_unregister(adapter);
1116 debug_register_failed:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 dev_set_drvdata(&ccw_device->dev, NULL);
Heiko Carstensca2d02c2007-05-08 11:17:54 +02001118 zfcp_reqlist_free(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 failed_low_mem_buffers:
1120 zfcp_free_low_mem_buffers(adapter);
1121 if (qdio_free(ccw_device) != 0)
1122 ZFCP_LOG_NORMAL("bug: qdio_free for adapter %s failed\n",
1123 zfcp_get_busid_by_adapter(adapter));
1124 qdio_allocate_failed:
1125 zfcp_qdio_free_queues(adapter);
1126 queues_alloc_failed:
1127 kfree(adapter);
1128 adapter = NULL;
1129 out:
1130 return adapter;
1131}
1132
1133/*
1134 * returns: 0 - struct zfcp_adapter data structure successfully removed
1135 * !0 - struct zfcp_adapter data structure could not be removed
1136 * (e.g. still used)
1137 * locks: adapter list write lock is assumed to be held by caller
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 */
1139void
1140zfcp_adapter_dequeue(struct zfcp_adapter *adapter)
1141{
1142 int retval = 0;
1143 unsigned long flags;
1144
Swen Schilligd26ab062008-05-19 12:17:37 +02001145 cancel_work_sync(&adapter->stat_work);
Michael Loehr9f287452007-05-09 11:01:24 +02001146 zfcp_adapter_scsi_unregister(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 device_unregister(&adapter->generic_services);
1148 zfcp_sysfs_adapter_remove_files(&adapter->ccw_device->dev);
1149 dev_set_drvdata(&adapter->ccw_device->dev, NULL);
1150 /* sanity check: no pending FSF requests */
Volker Sameskefea9d6c2006-08-02 11:05:16 +02001151 spin_lock_irqsave(&adapter->req_list_lock, flags);
1152 retval = zfcp_reqlist_isempty(adapter);
1153 spin_unlock_irqrestore(&adapter->req_list_lock, flags);
1154 if (!retval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 ZFCP_LOG_NORMAL("bug: adapter %s (%p) still in use, "
1156 "%i requests outstanding\n",
1157 zfcp_get_busid_by_adapter(adapter), adapter,
Volker Sameskefea9d6c2006-08-02 11:05:16 +02001158 atomic_read(&adapter->reqs_active));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 retval = -EBUSY;
1160 goto out;
1161 }
1162
Christof Schmittff17a292007-08-28 09:31:41 +02001163 zfcp_adapter_debug_unregister(adapter);
1164
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 /* remove specified adapter data structure from list */
1166 write_lock_irq(&zfcp_data.config_lock);
1167 list_del(&adapter->list);
1168 write_unlock_irq(&zfcp_data.config_lock);
1169
1170 /* decrease number of adapters in list */
1171 zfcp_data.adapters--;
1172
1173 ZFCP_LOG_TRACE("adapter %s (%p) removed from list, "
1174 "%i adapters still in list\n",
1175 zfcp_get_busid_by_adapter(adapter),
1176 adapter, zfcp_data.adapters);
1177
1178 retval = qdio_free(adapter->ccw_device);
1179 if (retval)
1180 ZFCP_LOG_NORMAL("bug: qdio_free for adapter %s failed\n",
1181 zfcp_get_busid_by_adapter(adapter));
1182
1183 zfcp_free_low_mem_buffers(adapter);
1184 /* free memory of adapter data structure and queues */
1185 zfcp_qdio_free_queues(adapter);
Volker Sameskefea9d6c2006-08-02 11:05:16 +02001186 zfcp_reqlist_free(adapter);
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +01001187 kfree(adapter->fc_stats);
1188 kfree(adapter->stats_reset_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 ZFCP_LOG_TRACE("freeing adapter structure\n");
1190 kfree(adapter);
1191 out:
1192 return;
1193}
1194
1195/**
1196 * zfcp_port_enqueue - enqueue port to port list of adapter
1197 * @adapter: adapter where remote port is added
1198 * @wwpn: WWPN of the remote port to be enqueued
1199 * @status: initial status for the port
1200 * @d_id: destination id of the remote port to be enqueued
1201 * Return: pointer to enqueued port on success, NULL on error
1202 * Locks: config_sema must be held to serialize changes to the port list
1203 *
1204 * All port internal structures are set up and the sysfs entry is generated.
1205 * d_id is used to enqueue ports with a well known address like the Directory
1206 * Service for nameserver lookup.
1207 */
1208struct zfcp_port *
1209zfcp_port_enqueue(struct zfcp_adapter *adapter, wwn_t wwpn, u32 status,
1210 u32 d_id)
1211{
Andreas Herrmann3859f6a2005-08-27 11:07:54 -07001212 struct zfcp_port *port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 int check_wwpn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
1215 check_wwpn = !(status & ZFCP_STATUS_PORT_NO_WWPN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 /*
1217 * check that there is no port with this WWPN already in list
1218 */
1219 if (check_wwpn) {
1220 read_lock_irq(&zfcp_data.config_lock);
1221 port = zfcp_get_port_by_wwpn(adapter, wwpn);
1222 read_unlock_irq(&zfcp_data.config_lock);
1223 if (port)
1224 return NULL;
1225 }
1226
Andreas Herrmannec4081c2006-05-22 18:17:30 +02001227 port = kzalloc(sizeof (struct zfcp_port), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 if (!port)
1229 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230
1231 /* initialise reference count stuff */
1232 atomic_set(&port->refcount, 0);
1233 init_waitqueue_head(&port->remove_wq);
1234
1235 INIT_LIST_HEAD(&port->unit_list_head);
1236 INIT_LIST_HEAD(&port->unit_remove_lh);
1237
1238 port->adapter = adapter;
1239
1240 if (check_wwpn)
1241 port->wwpn = wwpn;
1242
1243 atomic_set_mask(status, &port->status);
1244
1245 /* setup for sysfs registration */
1246 if (status & ZFCP_STATUS_PORT_WKA) {
1247 switch (d_id) {
1248 case ZFCP_DID_DIRECTORY_SERVICE:
1249 snprintf(port->sysfs_device.bus_id, BUS_ID_SIZE,
1250 "directory");
1251 break;
1252 case ZFCP_DID_MANAGEMENT_SERVICE:
1253 snprintf(port->sysfs_device.bus_id, BUS_ID_SIZE,
1254 "management");
1255 break;
1256 case ZFCP_DID_KEY_DISTRIBUTION_SERVICE:
1257 snprintf(port->sysfs_device.bus_id, BUS_ID_SIZE,
1258 "key_distribution");
1259 break;
1260 case ZFCP_DID_ALIAS_SERVICE:
1261 snprintf(port->sysfs_device.bus_id, BUS_ID_SIZE,
1262 "alias");
1263 break;
1264 case ZFCP_DID_TIME_SERVICE:
1265 snprintf(port->sysfs_device.bus_id, BUS_ID_SIZE,
1266 "time");
1267 break;
1268 default:
1269 kfree(port);
1270 return NULL;
1271 }
1272 port->d_id = d_id;
1273 port->sysfs_device.parent = &adapter->generic_services;
1274 } else {
1275 snprintf(port->sysfs_device.bus_id,
1276 BUS_ID_SIZE, "0x%016llx", wwpn);
Andreas Herrmann3859f6a2005-08-27 11:07:54 -07001277 port->sysfs_device.parent = &adapter->ccw_device->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 }
1279 port->sysfs_device.release = zfcp_sysfs_port_release;
1280 dev_set_drvdata(&port->sysfs_device, port);
1281
1282 /* mark port unusable as long as sysfs registration is not complete */
1283 atomic_set_mask(ZFCP_STATUS_COMMON_REMOVE, &port->status);
1284
1285 if (device_register(&port->sysfs_device)) {
1286 kfree(port);
1287 return NULL;
1288 }
1289
1290 if (zfcp_sysfs_port_create_files(&port->sysfs_device, status)) {
1291 device_unregister(&port->sysfs_device);
1292 return NULL;
1293 }
1294
1295 zfcp_port_get(port);
1296
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 write_lock_irq(&zfcp_data.config_lock);
Andreas Herrmann3859f6a2005-08-27 11:07:54 -07001298 list_add_tail(&port->list, &adapter->port_list_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 atomic_clear_mask(ZFCP_STATUS_COMMON_REMOVE, &port->status);
1300 atomic_set_mask(ZFCP_STATUS_COMMON_RUNNING, &port->status);
1301 if (d_id == ZFCP_DID_DIRECTORY_SERVICE)
1302 if (!adapter->nameserver_port)
1303 adapter->nameserver_port = port;
1304 adapter->ports++;
1305 write_unlock_irq(&zfcp_data.config_lock);
1306
1307 zfcp_adapter_get(adapter);
1308
1309 return port;
1310}
1311
1312void
1313zfcp_port_dequeue(struct zfcp_port *port)
1314{
1315 zfcp_port_wait(port);
1316 write_lock_irq(&zfcp_data.config_lock);
1317 list_del(&port->list);
1318 port->adapter->ports--;
1319 write_unlock_irq(&zfcp_data.config_lock);
Andreas Herrmann3859f6a2005-08-27 11:07:54 -07001320 if (port->rport)
Heiko Carstens20b17302005-08-28 13:22:37 -07001321 fc_remote_port_delete(port->rport);
1322 port->rport = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 zfcp_adapter_put(port->adapter);
1324 zfcp_sysfs_port_remove_files(&port->sysfs_device,
1325 atomic_read(&port->status));
1326 device_unregister(&port->sysfs_device);
1327}
1328
1329/* Enqueues a nameserver port */
1330int
1331zfcp_nameserver_enqueue(struct zfcp_adapter *adapter)
1332{
1333 struct zfcp_port *port;
1334
1335 port = zfcp_port_enqueue(adapter, 0, ZFCP_STATUS_PORT_WKA,
1336 ZFCP_DID_DIRECTORY_SERVICE);
1337 if (!port) {
1338 ZFCP_LOG_INFO("error: enqueue of nameserver port for "
1339 "adapter %s failed\n",
1340 zfcp_get_busid_by_adapter(adapter));
1341 return -ENXIO;
1342 }
1343 zfcp_port_put(port);
1344
1345 return 0;
1346}
1347
1348#undef ZFCP_LOG_AREA