dasd_devmap.c 28.9 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * File...........: linux/drivers/s390/block/dasd_devmap.c
 * Author(s)......: Holger Smolinski <Holger.Smolinski@de.ibm.com>
 *		    Horst Hummel <Horst.Hummel@de.ibm.com>
 *		    Carsten Otte <Cotte@de.ibm.com>
 *		    Martin Schwidefsky <schwidefsky@de.ibm.com>
 * Bugreports.to..: <Linux390@de.ibm.com>
 * (C) IBM Corporation, IBM Deutschland Entwicklung GmbH, 1999-2001
 *
 * Device mapping and dasd= parameter parsing functions. All devmap
 * functions may not be called from interrupt context. In particular
 * dasd_get_device is a no-no from interrupt context.
 *
 */

S
Stefan Haberland 已提交
16 17
#define KMSG_COMPONENT "dasd"

L
Linus Torvalds 已提交
18 19
#include <linux/ctype.h>
#include <linux/init.h>
R
Rusty Russell 已提交
20
#include <linux/module.h>
L
Linus Torvalds 已提交
21 22 23

#include <asm/debug.h>
#include <asm/uaccess.h>
24
#include <asm/ipl.h>
L
Linus Torvalds 已提交
25 26 27

/* This is ugly... */
#define PRINTK_HEADER "dasd_devmap:"
28
#define DASD_BUS_ID_SIZE 20
L
Linus Torvalds 已提交
29 30 31

#include "dasd_int.h"

32
struct kmem_cache *dasd_page_cache;
33
EXPORT_SYMBOL_GPL(dasd_page_cache);
L
Linus Torvalds 已提交
34 35 36 37 38 39 40 41 42 43 44 45 46

/*
 * dasd_devmap_t is used to store the features and the relation
 * between device number and device index. To find a dasd_devmap_t
 * that corresponds to a device number of a device index each
 * dasd_devmap_t is added to two linked lists, one to search by
 * the device number and one to search by the device index. As
 * soon as big minor numbers are available the device index list
 * can be removed since the device number will then be identical
 * to the device index.
 */
struct dasd_devmap {
	struct list_head list;
47
	char bus_id[DASD_BUS_ID_SIZE];
L
Linus Torvalds 已提交
48 49 50
        unsigned int devindex;
        unsigned short features;
	struct dasd_device *device;
51
	struct dasd_uid uid;
L
Linus Torvalds 已提交
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
};

/*
 * Parameter parsing functions for dasd= parameter. The syntax is:
 *   <devno>		: (0x)?[0-9a-fA-F]+
 *   <busid>		: [0-0a-f]\.[0-9a-f]\.(0x)?[0-9a-fA-F]+
 *   <feature>		: ro
 *   <feature_list>	: \(<feature>(:<feature>)*\)
 *   <devno-range>	: <devno>(-<devno>)?<feature_list>?
 *   <busid-range>	: <busid>(-<busid>)?<feature_list>?
 *   <devices>		: <devno-range>|<busid-range>
 *   <dasd_module>	: dasd_diag_mod|dasd_eckd_mod|dasd_fba_mod
 *
 *   <dasd>		: autodetect|probeonly|<devices>(,<devices>)*
 */

int dasd_probeonly =  0;	/* is true, when probeonly mode is active */
int dasd_autodetect = 0;	/* is true, when autodetection is active */
70 71
int dasd_nopav = 0;		/* is true, when PAV is disabled */
EXPORT_SYMBOL_GPL(dasd_nopav);
72 73
int dasd_nofcx;			/* disable High Performance Ficon */
EXPORT_SYMBOL_GPL(dasd_nofcx);
L
Linus Torvalds 已提交
74 75 76 77 78 79 80

/*
 * char *dasd[] is intended to hold the ranges supplied by the dasd= statement
 * it is named 'dasd' to directly be filled by insmod with the comma separated
 * strings when running as a module.
 */
static char *dasd[256];
R
Rusty Russell 已提交
81 82
module_param_array(dasd, charp, NULL, 0);

L
Linus Torvalds 已提交
83
/*
84
 * Single spinlock to protect devmap and servermap structures and lists.
L
Linus Torvalds 已提交
85 86 87 88 89 90 91 92 93
 */
static DEFINE_SPINLOCK(dasd_devmap_lock);

/*
 * Hash lists for devmap structures.
 */
static struct list_head dasd_hashlists[256];
int dasd_max_devindex;

94
static struct dasd_devmap *dasd_add_busid(const char *, int);
L
Linus Torvalds 已提交
95 96

static inline int
97
dasd_hash_busid(const char *bus_id)
L
Linus Torvalds 已提交
98 99 100 101
{
	int hash, i;

	hash = 0;
102
	for (i = 0; (i < DASD_BUS_ID_SIZE) && *bus_id; i++, bus_id++)
L
Linus Torvalds 已提交
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
		hash += *bus_id;
	return hash & 0xff;
}

#ifndef MODULE
/*
 * The parameter parsing functions for builtin-drivers are called
 * before kmalloc works. Store the pointers to the parameters strings
 * into dasd[] for later processing.
 */
static int __init
dasd_call_setup(char *str)
{
	static int count = 0;

	if (count < 256)
		dasd[count++] = str;
	return 1;
}

__setup ("dasd=", dasd_call_setup);
#endif	/* #ifndef MODULE */

126 127
#define	DASD_IPLDEV	"ipldev"

L
Linus Torvalds 已提交
128 129 130
/*
 * Read a device busid/devno from a string.
 */
131
static int
S
Stefan Haberland 已提交
132

L
Linus Torvalds 已提交
133 134 135
dasd_busid(char **str, int *id0, int *id1, int *devno)
{
	int val, old_style;
136

137 138 139
	/* Interpret ipldev busid */
	if (strncmp(DASD_IPLDEV, *str, strlen(DASD_IPLDEV)) == 0) {
		if (ipl_info.type != IPL_TYPE_CCW) {
S
Stefan Haberland 已提交
140
			pr_err("The IPL device is not a CCW device\n");
141 142 143 144 145 146 147 148 149
			return -EINVAL;
		}
		*id0 = 0;
		*id1 = ipl_info.data.ccw.dev_id.ssid;
		*devno = ipl_info.data.ccw.dev_id.devno;
		*str += strlen(DASD_IPLDEV);

		return 0;
	}
L
Linus Torvalds 已提交
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
	/* check for leading '0x' */
	old_style = 0;
	if ((*str)[0] == '0' && (*str)[1] == 'x') {
		*str += 2;
		old_style = 1;
	}
	if (!isxdigit((*str)[0]))	/* We require at least one hex digit */
		return -EINVAL;
	val = simple_strtoul(*str, str, 16);
	if (old_style || (*str)[0] != '.') {
		*id0 = *id1 = 0;
		if (val < 0 || val > 0xffff)
			return -EINVAL;
		*devno = val;
		return 0;
	}
	/* New style x.y.z busid */
	if (val < 0 || val > 0xff)
		return -EINVAL;
	*id0 = val;
	(*str)++;
	if (!isxdigit((*str)[0]))	/* We require at least one hex digit */
		return -EINVAL;
	val = simple_strtoul(*str, str, 16);
	if (val < 0 || val > 0xff || (*str)++[0] != '.')
		return -EINVAL;
	*id1 = val;
	if (!isxdigit((*str)[0]))	/* We require at least one hex digit */
		return -EINVAL;
	val = simple_strtoul(*str, str, 16);
	if (val < 0 || val > 0xffff)
		return -EINVAL;
	*devno = val;
	return 0;
}

/*
 * Read colon separated list of dasd features. Currently there is
 * only one: "ro" for read-only devices. The default feature set
 * is empty (value 0).
 */
191
static int
L
Linus Torvalds 已提交
192 193 194 195 196 197 198 199 200 201 202 203 204
dasd_feature_list(char *str, char **endp)
{
	int features, len, rc;

	rc = 0;
	if (*str != '(') {
		*endp = str;
		return DASD_FEATURE_DEFAULT;
	}
	str++;
	features = 0;

	while (1) {
205
		for (len = 0;
L
Linus Torvalds 已提交
206 207 208 209 210
		     str[len] && str[len] != ':' && str[len] != ')'; len++);
		if (len == 2 && !strncmp(str, "ro", 2))
			features |= DASD_FEATURE_READONLY;
		else if (len == 4 && !strncmp(str, "diag", 4))
			features |= DASD_FEATURE_USEDIAG;
211 212
		else if (len == 6 && !strncmp(str, "erplog", 6))
			features |= DASD_FEATURE_ERPLOG;
213 214
		else if (len == 8 && !strncmp(str, "failfast", 8))
			features |= DASD_FEATURE_FAILFAST;
L
Linus Torvalds 已提交
215
		else {
S
Stefan Haberland 已提交
216 217
			pr_warning("%*s is not a supported device option\n",
				   len, str);
L
Linus Torvalds 已提交
218 219 220 221 222 223 224 225
			rc = -EINVAL;
		}
		str += len;
		if (*str != ':')
			break;
		str++;
	}
	if (*str != ')') {
S
Stefan Haberland 已提交
226 227
		pr_warning("A closing parenthesis ')' is missing in the "
			   "dasd= parameter\n");
L
Linus Torvalds 已提交
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
		rc = -EINVAL;
	} else
		str++;
	*endp = str;
	if (rc != 0)
		return rc;
	return features;
}

/*
 * Try to match the first element on the comma separated parse string
 * with one of the known keywords. If a keyword is found, take the approprate
 * action and return a pointer to the residual string. If the first element
 * could not be matched to any keyword then return an error code.
 */
static char *
dasd_parse_keyword( char *parsestring ) {

	char *nextcomma, *residual_str;
	int length;

	nextcomma = strchr(parsestring,',');
	if (nextcomma) {
		length = nextcomma - parsestring;
		residual_str = nextcomma + 1;
	} else {
		length = strlen(parsestring);
		residual_str = parsestring + length;
        }
257
	if (strncmp("autodetect", parsestring, length) == 0) {
L
Linus Torvalds 已提交
258
		dasd_autodetect = 1;
S
Stefan Haberland 已提交
259
		pr_info("The autodetection mode has been activated\n");
L
Linus Torvalds 已提交
260 261
                return residual_str;
        }
262
	if (strncmp("probeonly", parsestring, length) == 0) {
L
Linus Torvalds 已提交
263
		dasd_probeonly = 1;
S
Stefan Haberland 已提交
264
		pr_info("The probeonly mode has been activated\n");
L
Linus Torvalds 已提交
265 266
                return residual_str;
        }
267
	if (strncmp("nopav", parsestring, length) == 0) {
268
		if (MACHINE_IS_VM)
S
Stefan Haberland 已提交
269
			pr_info("'nopav' is not supported on z/VM\n");
270 271
		else {
			dasd_nopav = 1;
S
Stefan Haberland 已提交
272
			pr_info("PAV support has be deactivated\n");
273
		}
274 275
		return residual_str;
	}
276 277
	if (strncmp("nofcx", parsestring, length) == 0) {
		dasd_nofcx = 1;
S
Stefan Haberland 已提交
278 279
		pr_info("High Performance FICON support has been "
			"deactivated\n");
280 281
		return residual_str;
	}
282
	if (strncmp("fixedbuffers", parsestring, length) == 0) {
L
Linus Torvalds 已提交
283 284 285
		if (dasd_page_cache)
			return residual_str;
		dasd_page_cache =
286 287
			kmem_cache_create("dasd_page_cache", PAGE_SIZE,
					  PAGE_SIZE, SLAB_CACHE_DMA,
288
					  NULL);
L
Linus Torvalds 已提交
289
		if (!dasd_page_cache)
S
Stefan Haberland 已提交
290
			DBF_EVENT(DBF_WARNING, "%s", "Failed to create slab, "
L
Linus Torvalds 已提交
291 292
				"fixed buffer mode disabled.");
		else
S
Stefan Haberland 已提交
293
			DBF_EVENT(DBF_INFO, "%s",
L
Linus Torvalds 已提交
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
				 "turning on fixed buffer mode");
                return residual_str;
        }
	return ERR_PTR(-EINVAL);
}

/*
 * Try to interprete the first element on the comma separated parse string
 * as a device number or a range of devices. If the interpretation is
 * successfull, create the matching dasd_devmap entries and return a pointer
 * to the residual string.
 * If interpretation fails or in case of an error, return an error code.
 */
static char *
dasd_parse_range( char *parsestring ) {

	struct dasd_devmap *devmap;
	int from, from_id0, from_id1;
	int to, to_id0, to_id1;
	int features, rc;
314
	char bus_id[DASD_BUS_ID_SIZE+1], *str;
L
Linus Torvalds 已提交
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330

	str = parsestring;
	rc = dasd_busid(&str, &from_id0, &from_id1, &from);
	if (rc == 0) {
		to = from;
		to_id0 = from_id0;
		to_id1 = from_id1;
		if (*str == '-') {
			str++;
			rc = dasd_busid(&str, &to_id0, &to_id1, &to);
		}
	}
	if (rc == 0 &&
	    (from_id0 != to_id0 || from_id1 != to_id1 || from > to))
		rc = -EINVAL;
	if (rc) {
S
Stefan Haberland 已提交
331
		pr_err("%s is not a valid device range\n", parsestring);
L
Linus Torvalds 已提交
332 333 334 335 336
		return ERR_PTR(rc);
	}
	features = dasd_feature_list(str, &str);
	if (features < 0)
		return ERR_PTR(-EINVAL);
337 338
	/* each device in dasd= parameter should be set initially online */
	features |= DASD_FEATURE_INITIAL_ONLINE;
L
Linus Torvalds 已提交
339 340 341 342 343 344 345 346 347 348 349
	while (from <= to) {
		sprintf(bus_id, "%01x.%01x.%04x",
			from_id0, from_id1, from++);
		devmap = dasd_add_busid(bus_id, features);
		if (IS_ERR(devmap))
			return (char *)devmap;
	}
	if (*str == ',')
		return str + 1;
	if (*str == '\0')
		return str;
S
Stefan Haberland 已提交
350 351
	pr_warning("The dasd= parameter value %s has an invalid ending\n",
		   str);
L
Linus Torvalds 已提交
352 353 354
	return ERR_PTR(-EINVAL);
}

355
static char *
L
Linus Torvalds 已提交
356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
dasd_parse_next_element( char *parsestring ) {
	char * residual_str;
	residual_str = dasd_parse_keyword(parsestring);
	if (!IS_ERR(residual_str))
		return residual_str;
	residual_str = dasd_parse_range(parsestring);
	return residual_str;
}

/*
 * Parse parameters stored in dasd[]
 * The 'dasd=...' parameter allows to specify a comma separated list of
 * keywords and device ranges. When the dasd driver is build into the kernel,
 * the complete list will be stored as one element of the dasd[] array.
 * When the dasd driver is build as a module, then the list is broken into
 * it's elements and each dasd[] entry contains one element.
 */
int
dasd_parse(void)
{
	int rc, i;
	char *parsestring;

	rc = 0;
	for (i = 0; i < 256; i++) {
		if (dasd[i] == NULL)
			break;
		parsestring = dasd[i];
		/* loop over the comma separated list in the parsestring */
		while (*parsestring) {
			parsestring = dasd_parse_next_element(parsestring);
			if(IS_ERR(parsestring)) {
				rc = PTR_ERR(parsestring);
				break;
			}
		}
		if (rc) {
			DBF_EVENT(DBF_ALERT, "%s", "invalid range found");
			break;
		}
	}
	return rc;
}

/*
 * Add a devmap for the device specified by busid. It is possible that
 * the devmap already exists (dasd= parameter). The order of the devices
 * added through this function will define the kdevs for the individual
404
 * devices.
L
Linus Torvalds 已提交
405 406
 */
static struct dasd_devmap *
407
dasd_add_busid(const char *bus_id, int features)
L
Linus Torvalds 已提交
408 409 410 411 412
{
	struct dasd_devmap *devmap, *new, *tmp;
	int hash;

	new = (struct dasd_devmap *)
413
		kzalloc(sizeof(struct dasd_devmap), GFP_KERNEL);
L
Linus Torvalds 已提交
414 415 416
	if (!new)
		return ERR_PTR(-ENOMEM);
	spin_lock(&dasd_devmap_lock);
H
Heiko Carstens 已提交
417
	devmap = NULL;
L
Linus Torvalds 已提交
418 419
	hash = dasd_hash_busid(bus_id);
	list_for_each_entry(tmp, &dasd_hashlists[hash], list)
420
		if (strncmp(tmp->bus_id, bus_id, DASD_BUS_ID_SIZE) == 0) {
L
Linus Torvalds 已提交
421 422 423 424 425 426
			devmap = tmp;
			break;
		}
	if (!devmap) {
		/* This bus_id is new. */
		new->devindex = dasd_max_devindex++;
427
		strncpy(new->bus_id, bus_id, DASD_BUS_ID_SIZE);
L
Linus Torvalds 已提交
428
		new->features = features;
H
Heiko Carstens 已提交
429
		new->device = NULL;
L
Linus Torvalds 已提交
430 431
		list_add(&new->list, &dasd_hashlists[hash]);
		devmap = new;
H
Heiko Carstens 已提交
432
		new = NULL;
L
Linus Torvalds 已提交
433 434
	}
	spin_unlock(&dasd_devmap_lock);
J
Jesper Juhl 已提交
435
	kfree(new);
L
Linus Torvalds 已提交
436 437 438 439 440 441 442
	return devmap;
}

/*
 * Find devmap for device with given bus_id.
 */
static struct dasd_devmap *
443
dasd_find_busid(const char *bus_id)
L
Linus Torvalds 已提交
444 445 446 447 448 449 450 451
{
	struct dasd_devmap *devmap, *tmp;
	int hash;

	spin_lock(&dasd_devmap_lock);
	devmap = ERR_PTR(-ENODEV);
	hash = dasd_hash_busid(bus_id);
	list_for_each_entry(tmp, &dasd_hashlists[hash], list) {
452
		if (strncmp(tmp->bus_id, bus_id, DASD_BUS_ID_SIZE) == 0) {
L
Linus Torvalds 已提交
453 454 455 456 457 458 459 460 461 462 463 464
			devmap = tmp;
			break;
		}
	}
	spin_unlock(&dasd_devmap_lock);
	return devmap;
}

/*
 * Check if busid has been added to the list of dasd ranges.
 */
int
465
dasd_busid_known(const char *bus_id)
L
Linus Torvalds 已提交
466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482
{
	return IS_ERR(dasd_find_busid(bus_id)) ? -ENOENT : 0;
}

/*
 * Forget all about the device numbers added so far.
 * This may only be called at module unload or system shutdown.
 */
static void
dasd_forget_ranges(void)
{
	struct dasd_devmap *devmap, *n;
	int i;

	spin_lock(&dasd_devmap_lock);
	for (i = 0; i < 256; i++) {
		list_for_each_entry_safe(devmap, n, &dasd_hashlists[i], list) {
483
			BUG_ON(devmap->device != NULL);
L
Linus Torvalds 已提交
484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501
			list_del(&devmap->list);
			kfree(devmap);
		}
	}
	spin_unlock(&dasd_devmap_lock);
}

/*
 * Find the device struct by its device index.
 */
struct dasd_device *
dasd_device_from_devindex(int devindex)
{
	struct dasd_devmap *devmap, *tmp;
	struct dasd_device *device;
	int i;

	spin_lock(&dasd_devmap_lock);
H
Heiko Carstens 已提交
502
	devmap = NULL;
L
Linus Torvalds 已提交
503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527
	for (i = 0; (i < 256) && !devmap; i++)
		list_for_each_entry(tmp, &dasd_hashlists[i], list)
			if (tmp->devindex == devindex) {
				/* Found the devmap for the device. */
				devmap = tmp;
				break;
			}
	if (devmap && devmap->device) {
		device = devmap->device;
		dasd_get_device(device);
	} else
		device = ERR_PTR(-ENODEV);
	spin_unlock(&dasd_devmap_lock);
	return device;
}

/*
 * Return devmap for cdev. If no devmap exists yet, create one and
 * connect it to the cdev.
 */
static struct dasd_devmap *
dasd_devmap_from_cdev(struct ccw_device *cdev)
{
	struct dasd_devmap *devmap;

528
	devmap = dasd_find_busid(dev_name(&cdev->dev));
L
Linus Torvalds 已提交
529
	if (IS_ERR(devmap))
530
		devmap = dasd_add_busid(dev_name(&cdev->dev),
L
Linus Torvalds 已提交
531 532 533 534 535 536 537 538 539 540 541 542
					DASD_FEATURE_DEFAULT);
	return devmap;
}

/*
 * Create a dasd device structure for cdev.
 */
struct dasd_device *
dasd_create_device(struct ccw_device *cdev)
{
	struct dasd_devmap *devmap;
	struct dasd_device *device;
543
	unsigned long flags;
L
Linus Torvalds 已提交
544 545 546 547 548 549 550 551 552
	int rc;

	devmap = dasd_devmap_from_cdev(cdev);
	if (IS_ERR(devmap))
		return (void *) devmap;

	device = dasd_alloc_device();
	if (IS_ERR(device))
		return device;
553
	atomic_set(&device->ref_count, 3);
L
Linus Torvalds 已提交
554 555 556 557 558

	spin_lock(&dasd_devmap_lock);
	if (!devmap->device) {
		devmap->device = device;
		device->devindex = devmap->devindex;
559
		device->features = devmap->features;
L
Linus Torvalds 已提交
560 561 562 563 564 565 566 567 568 569 570 571
		get_device(&cdev->dev);
		device->cdev = cdev;
		rc = 0;
	} else
		/* Someone else was faster. */
		rc = -EBUSY;
	spin_unlock(&dasd_devmap_lock);

	if (rc) {
		dasd_free_device(device);
		return ERR_PTR(rc);
	}
572 573

	spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
574
	dev_set_drvdata(&cdev->dev, device);
575 576
	spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);

L
Linus Torvalds 已提交
577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593
	return device;
}

/*
 * Wait queue for dasd_delete_device waits.
 */
static DECLARE_WAIT_QUEUE_HEAD(dasd_delete_wq);

/*
 * Remove a dasd device structure. The passed referenced
 * is destroyed.
 */
void
dasd_delete_device(struct dasd_device *device)
{
	struct ccw_device *cdev;
	struct dasd_devmap *devmap;
594
	unsigned long flags;
L
Linus Torvalds 已提交
595 596

	/* First remove device pointer from devmap. */
597
	devmap = dasd_find_busid(dev_name(&device->cdev->dev));
598
	BUG_ON(IS_ERR(devmap));
L
Linus Torvalds 已提交
599 600 601 602 603 604 605 606 607
	spin_lock(&dasd_devmap_lock);
	if (devmap->device != device) {
		spin_unlock(&dasd_devmap_lock);
		dasd_put_device(device);
		return;
	}
	devmap->device = NULL;
	spin_unlock(&dasd_devmap_lock);

608 609
	/* Disconnect dasd_device structure from ccw_device structure. */
	spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
610
	dev_set_drvdata(&device->cdev->dev, NULL);
611 612 613 614 615 616 617
	spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);

	/*
	 * Drop ref_count by 3, one for the devmap reference, one for
	 * the cdev reference and one for the passed reference.
	 */
	atomic_sub(3, &device->ref_count);
L
Linus Torvalds 已提交
618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642

	/* Wait for reference counter to drop to zero. */
	wait_event(dasd_delete_wq, atomic_read(&device->ref_count) == 0);

	/* Disconnect dasd_device structure from ccw_device structure. */
	cdev = device->cdev;
	device->cdev = NULL;

	/* Put ccw_device structure. */
	put_device(&cdev->dev);

	/* Now the device structure can be freed. */
	dasd_free_device(device);
}

/*
 * Reference counter dropped to zero. Wake up waiter
 * in dasd_delete_device.
 */
void
dasd_put_device_wake(struct dasd_device *device)
{
	wake_up(&dasd_delete_wq);
}

643 644 645 646 647 648 649 650
/*
 * Return dasd_device structure associated with cdev.
 * This function needs to be called with the ccw device
 * lock held. It can be used from interrupt context.
 */
struct dasd_device *
dasd_device_from_cdev_locked(struct ccw_device *cdev)
{
651
	struct dasd_device *device = dev_get_drvdata(&cdev->dev);
652 653 654 655 656 657 658

	if (!device)
		return ERR_PTR(-ENODEV);
	dasd_get_device(device);
	return device;
}

L
Linus Torvalds 已提交
659 660 661 662 663 664 665
/*
 * Return dasd_device structure associated with cdev.
 */
struct dasd_device *
dasd_device_from_cdev(struct ccw_device *cdev)
{
	struct dasd_device *device;
666
	unsigned long flags;
L
Linus Torvalds 已提交
667

668 669 670
	spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
	device = dasd_device_from_cdev_locked(cdev);
	spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
L
Linus Torvalds 已提交
671 672 673 674 675 676 677
	return device;
}

/*
 * SECTION: files in sysfs
 */

678 679 680 681 682 683 684 685 686
/*
 * failfast controls the behaviour, if no path is available
 */
static ssize_t dasd_ff_show(struct device *dev, struct device_attribute *attr,
			    char *buf)
{
	struct dasd_devmap *devmap;
	int ff_flag;

687
	devmap = dasd_find_busid(dev_name(dev));
688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722
	if (!IS_ERR(devmap))
		ff_flag = (devmap->features & DASD_FEATURE_FAILFAST) != 0;
	else
		ff_flag = (DASD_FEATURE_DEFAULT & DASD_FEATURE_FAILFAST) != 0;
	return snprintf(buf, PAGE_SIZE, ff_flag ? "1\n" : "0\n");
}

static ssize_t dasd_ff_store(struct device *dev, struct device_attribute *attr,
	      const char *buf, size_t count)
{
	struct dasd_devmap *devmap;
	int val;
	char *endp;

	devmap = dasd_devmap_from_cdev(to_ccwdev(dev));
	if (IS_ERR(devmap))
		return PTR_ERR(devmap);

	val = simple_strtoul(buf, &endp, 0);
	if (((endp + 1) < (buf + count)) || (val > 1))
		return -EINVAL;

	spin_lock(&dasd_devmap_lock);
	if (val)
		devmap->features |= DASD_FEATURE_FAILFAST;
	else
		devmap->features &= ~DASD_FEATURE_FAILFAST;
	if (devmap->device)
		devmap->device->features = devmap->features;
	spin_unlock(&dasd_devmap_lock);
	return count;
}

static DEVICE_ATTR(failfast, 0644, dasd_ff_show, dasd_ff_store);

L
Linus Torvalds 已提交
723 724 725 726
/*
 * readonly controls the readonly status of a dasd
 */
static ssize_t
727
dasd_ro_show(struct device *dev, struct device_attribute *attr, char *buf)
L
Linus Torvalds 已提交
728 729 730 731
{
	struct dasd_devmap *devmap;
	int ro_flag;

732
	devmap = dasd_find_busid(dev_name(dev));
L
Linus Torvalds 已提交
733 734 735 736 737 738 739 740
	if (!IS_ERR(devmap))
		ro_flag = (devmap->features & DASD_FEATURE_READONLY) != 0;
	else
		ro_flag = (DASD_FEATURE_DEFAULT & DASD_FEATURE_READONLY) != 0;
	return snprintf(buf, PAGE_SIZE, ro_flag ? "1\n" : "0\n");
}

static ssize_t
741 742
dasd_ro_store(struct device *dev, struct device_attribute *attr,
	      const char *buf, size_t count)
L
Linus Torvalds 已提交
743 744
{
	struct dasd_devmap *devmap;
745 746
	int val;
	char *endp;
L
Linus Torvalds 已提交
747 748 749 750

	devmap = dasd_devmap_from_cdev(to_ccwdev(dev));
	if (IS_ERR(devmap))
		return PTR_ERR(devmap);
751 752 753 754 755

	val = simple_strtoul(buf, &endp, 0);
	if (((endp + 1) < (buf + count)) || (val > 1))
		return -EINVAL;

L
Linus Torvalds 已提交
756
	spin_lock(&dasd_devmap_lock);
757
	if (val)
L
Linus Torvalds 已提交
758 759 760
		devmap->features |= DASD_FEATURE_READONLY;
	else
		devmap->features &= ~DASD_FEATURE_READONLY;
761 762
	if (devmap->device)
		devmap->device->features = devmap->features;
763 764 765
	if (devmap->device && devmap->device->block
	    && devmap->device->block->gdp)
		set_disk_ro(devmap->device->block->gdp, val);
L
Linus Torvalds 已提交
766 767 768 769 770
	spin_unlock(&dasd_devmap_lock);
	return count;
}

static DEVICE_ATTR(readonly, 0644, dasd_ro_show, dasd_ro_store);
771 772 773 774 775 776 777 778 779 780
/*
 * erplog controls the logging of ERP related data
 * (e.g. failing channel programs).
 */
static ssize_t
dasd_erplog_show(struct device *dev, struct device_attribute *attr, char *buf)
{
	struct dasd_devmap *devmap;
	int erplog;

781
	devmap = dasd_find_busid(dev_name(dev));
782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816
	if (!IS_ERR(devmap))
		erplog = (devmap->features & DASD_FEATURE_ERPLOG) != 0;
	else
		erplog = (DASD_FEATURE_DEFAULT & DASD_FEATURE_ERPLOG) != 0;
	return snprintf(buf, PAGE_SIZE, erplog ? "1\n" : "0\n");
}

static ssize_t
dasd_erplog_store(struct device *dev, struct device_attribute *attr,
	      const char *buf, size_t count)
{
	struct dasd_devmap *devmap;
	int val;
	char *endp;

	devmap = dasd_devmap_from_cdev(to_ccwdev(dev));
	if (IS_ERR(devmap))
		return PTR_ERR(devmap);

	val = simple_strtoul(buf, &endp, 0);
	if (((endp + 1) < (buf + count)) || (val > 1))
		return -EINVAL;

	spin_lock(&dasd_devmap_lock);
	if (val)
		devmap->features |= DASD_FEATURE_ERPLOG;
	else
		devmap->features &= ~DASD_FEATURE_ERPLOG;
	if (devmap->device)
		devmap->device->features = devmap->features;
	spin_unlock(&dasd_devmap_lock);
	return count;
}

static DEVICE_ATTR(erplog, 0644, dasd_erplog_show, dasd_erplog_store);
L
Linus Torvalds 已提交
817 818 819 820 821

/*
 * use_diag controls whether the driver should use diag rather than ssch
 * to talk to the device
 */
822
static ssize_t
823
dasd_use_diag_show(struct device *dev, struct device_attribute *attr, char *buf)
L
Linus Torvalds 已提交
824 825 826 827
{
	struct dasd_devmap *devmap;
	int use_diag;

828
	devmap = dasd_find_busid(dev_name(dev));
L
Linus Torvalds 已提交
829 830 831 832 833 834 835 836
	if (!IS_ERR(devmap))
		use_diag = (devmap->features & DASD_FEATURE_USEDIAG) != 0;
	else
		use_diag = (DASD_FEATURE_DEFAULT & DASD_FEATURE_USEDIAG) != 0;
	return sprintf(buf, use_diag ? "1\n" : "0\n");
}

static ssize_t
837 838
dasd_use_diag_store(struct device *dev, struct device_attribute *attr,
		    const char *buf, size_t count)
L
Linus Torvalds 已提交
839 840 841
{
	struct dasd_devmap *devmap;
	ssize_t rc;
842 843
	int val;
	char *endp;
L
Linus Torvalds 已提交
844 845 846 847

	devmap = dasd_devmap_from_cdev(to_ccwdev(dev));
	if (IS_ERR(devmap))
		return PTR_ERR(devmap);
848 849 850 851 852

	val = simple_strtoul(buf, &endp, 0);
	if (((endp + 1) < (buf + count)) || (val > 1))
		return -EINVAL;

L
Linus Torvalds 已提交
853 854 855 856
	spin_lock(&dasd_devmap_lock);
	/* Changing diag discipline flag is only allowed in offline state. */
	rc = count;
	if (!devmap->device) {
857
		if (val)
L
Linus Torvalds 已提交
858 859 860 861 862 863 864 865 866
			devmap->features |= DASD_FEATURE_USEDIAG;
		else
			devmap->features &= ~DASD_FEATURE_USEDIAG;
	} else
		rc = -EPERM;
	spin_unlock(&dasd_devmap_lock);
	return rc;
}

867
static DEVICE_ATTR(use_diag, 0644, dasd_use_diag_show, dasd_use_diag_store);
L
Linus Torvalds 已提交
868 869

static ssize_t
870 871
dasd_discipline_show(struct device *dev, struct device_attribute *attr,
		     char *buf)
L
Linus Torvalds 已提交
872
{
873 874
	struct dasd_device *device;
	ssize_t len;
L
Linus Torvalds 已提交
875

876 877 878 879 880 881 882 883
	device = dasd_device_from_cdev(to_ccwdev(dev));
	if (!IS_ERR(device) && device->discipline) {
		len = snprintf(buf, PAGE_SIZE, "%s\n",
			       device->discipline->name);
		dasd_put_device(device);
	} else
		len = snprintf(buf, PAGE_SIZE, "none\n");
	return len;
L
Linus Torvalds 已提交
884 885 886 887
}

static DEVICE_ATTR(discipline, 0444, dasd_discipline_show, NULL);

888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927
static ssize_t
dasd_device_status_show(struct device *dev, struct device_attribute *attr,
		     char *buf)
{
	struct dasd_device *device;
	ssize_t len;

	device = dasd_device_from_cdev(to_ccwdev(dev));
	if (!IS_ERR(device)) {
		switch (device->state) {
		case DASD_STATE_NEW:
			len = snprintf(buf, PAGE_SIZE, "new\n");
			break;
		case DASD_STATE_KNOWN:
			len = snprintf(buf, PAGE_SIZE, "detected\n");
			break;
		case DASD_STATE_BASIC:
			len = snprintf(buf, PAGE_SIZE, "basic\n");
			break;
		case DASD_STATE_UNFMT:
			len = snprintf(buf, PAGE_SIZE, "unformatted\n");
			break;
		case DASD_STATE_READY:
			len = snprintf(buf, PAGE_SIZE, "ready\n");
			break;
		case DASD_STATE_ONLINE:
			len = snprintf(buf, PAGE_SIZE, "online\n");
			break;
		default:
			len = snprintf(buf, PAGE_SIZE, "no stat\n");
			break;
		}
		dasd_put_device(device);
	} else
		len = snprintf(buf, PAGE_SIZE, "unknown\n");
	return len;
}

static DEVICE_ATTR(status, 0444, dasd_device_status_show, NULL);

928 929 930 931 932 933
static ssize_t
dasd_alias_show(struct device *dev, struct device_attribute *attr, char *buf)
{
	struct dasd_devmap *devmap;
	int alias;

934
	devmap = dasd_find_busid(dev_name(dev));
935
	spin_lock(&dasd_devmap_lock);
936 937 938 939 940 941 942
	if (IS_ERR(devmap) || strlen(devmap->uid.vendor) == 0) {
		spin_unlock(&dasd_devmap_lock);
		return sprintf(buf, "0\n");
	}
	if (devmap->uid.type == UA_BASE_PAV_ALIAS ||
	    devmap->uid.type == UA_HYPER_PAV_ALIAS)
		alias = 1;
943 944 945 946 947 948 949 950 951 952 953 954 955 956
	else
		alias = 0;
	spin_unlock(&dasd_devmap_lock);
	return sprintf(buf, alias ? "1\n" : "0\n");
}

static DEVICE_ATTR(alias, 0444, dasd_alias_show, NULL);

static ssize_t
dasd_vendor_show(struct device *dev, struct device_attribute *attr, char *buf)
{
	struct dasd_devmap *devmap;
	char *vendor;

957
	devmap = dasd_find_busid(dev_name(dev));
958 959 960 961 962 963 964 965 966 967 968 969 970
	spin_lock(&dasd_devmap_lock);
	if (!IS_ERR(devmap) && strlen(devmap->uid.vendor) > 0)
		vendor = devmap->uid.vendor;
	else
		vendor = "";
	spin_unlock(&dasd_devmap_lock);

	return snprintf(buf, PAGE_SIZE, "%s\n", vendor);
}

static DEVICE_ATTR(vendor, 0444, dasd_vendor_show, NULL);

#define UID_STRLEN ( /* vendor */ 3 + 1 + /* serial    */ 14 + 1 +\
971 972
		     /* SSID   */ 4 + 1 + /* unit addr */ 2 + 1 +\
		     /* vduit */ 32 + 1)
973 974 975 976 977

static ssize_t
dasd_uid_show(struct device *dev, struct device_attribute *attr, char *buf)
{
	struct dasd_devmap *devmap;
978 979 980
	char uid_string[UID_STRLEN];
	char ua_string[3];
	struct dasd_uid *uid;
981

982
	devmap = dasd_find_busid(dev_name(dev));
983
	spin_lock(&dasd_devmap_lock);
984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003
	if (IS_ERR(devmap) || strlen(devmap->uid.vendor) == 0) {
		spin_unlock(&dasd_devmap_lock);
		return sprintf(buf, "\n");
	}
	uid = &devmap->uid;
	switch (uid->type) {
	case UA_BASE_DEVICE:
		sprintf(ua_string, "%02x", uid->real_unit_addr);
		break;
	case UA_BASE_PAV_ALIAS:
		sprintf(ua_string, "%02x", uid->base_unit_addr);
		break;
	case UA_HYPER_PAV_ALIAS:
		sprintf(ua_string, "xx");
		break;
	default:
		/* should not happen, treat like base device */
		sprintf(ua_string, "%02x", uid->real_unit_addr);
		break;
	}
1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014
	if (strlen(uid->vduit) > 0)
		snprintf(uid_string, sizeof(uid_string),
			 "%s.%s.%04x.%s.%s",
			 uid->vendor, uid->serial,
			 uid->ssid, ua_string,
			 uid->vduit);
	else
		snprintf(uid_string, sizeof(uid_string),
			 "%s.%s.%04x.%s",
			 uid->vendor, uid->serial,
			 uid->ssid, ua_string);
1015
	spin_unlock(&dasd_devmap_lock);
1016
	return snprintf(buf, PAGE_SIZE, "%s\n", uid_string);
1017 1018 1019 1020
}

static DEVICE_ATTR(uid, 0444, dasd_uid_show, NULL);

1021 1022 1023 1024 1025 1026 1027 1028 1029
/*
 * extended error-reporting
 */
static ssize_t
dasd_eer_show(struct device *dev, struct device_attribute *attr, char *buf)
{
	struct dasd_devmap *devmap;
	int eer_flag;

1030
	devmap = dasd_find_busid(dev_name(dev));
1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042
	if (!IS_ERR(devmap) && devmap->device)
		eer_flag = dasd_eer_enabled(devmap->device);
	else
		eer_flag = 0;
	return snprintf(buf, PAGE_SIZE, eer_flag ? "1\n" : "0\n");
}

static ssize_t
dasd_eer_store(struct device *dev, struct device_attribute *attr,
	       const char *buf, size_t count)
{
	struct dasd_devmap *devmap;
1043 1044
	int val, rc;
	char *endp;
1045 1046 1047 1048 1049

	devmap = dasd_devmap_from_cdev(to_ccwdev(dev));
	if (IS_ERR(devmap))
		return PTR_ERR(devmap);
	if (!devmap->device)
1050 1051 1052 1053 1054 1055
		return -ENODEV;

	val = simple_strtoul(buf, &endp, 0);
	if (((endp + 1) < (buf + count)) || (val > 1))
		return -EINVAL;

1056
	if (val) {
1057
		rc = dasd_eer_enable(devmap->device);
1058 1059 1060
		if (rc)
			return rc;
	} else
1061
		dasd_eer_disable(devmap->device);
1062
	return count;
1063 1064 1065 1066
}

static DEVICE_ATTR(eer_enabled, 0644, dasd_eer_show, dasd_eer_store);

L
Linus Torvalds 已提交
1067 1068 1069
static struct attribute * dasd_attrs[] = {
	&dev_attr_readonly.attr,
	&dev_attr_discipline.attr,
1070
	&dev_attr_status.attr,
1071 1072 1073
	&dev_attr_alias.attr,
	&dev_attr_vendor.attr,
	&dev_attr_uid.attr,
L
Linus Torvalds 已提交
1074
	&dev_attr_use_diag.attr,
1075
	&dev_attr_eer_enabled.attr,
1076
	&dev_attr_erplog.attr,
1077
	&dev_attr_failfast.attr,
L
Linus Torvalds 已提交
1078 1079 1080 1081 1082 1083 1084
	NULL,
};

static struct attribute_group dasd_attr_group = {
	.attrs = dasd_attrs,
};

1085 1086 1087 1088 1089 1090 1091 1092
/*
 * Return copy of the device unique identifier.
 */
int
dasd_get_uid(struct ccw_device *cdev, struct dasd_uid *uid)
{
	struct dasd_devmap *devmap;

1093
	devmap = dasd_find_busid(dev_name(&cdev->dev));
1094 1095 1096 1097 1098 1099 1100
	if (IS_ERR(devmap))
		return PTR_ERR(devmap);
	spin_lock(&dasd_devmap_lock);
	*uid = devmap->uid;
	spin_unlock(&dasd_devmap_lock);
	return 0;
}
1101
EXPORT_SYMBOL_GPL(dasd_get_uid);
1102 1103 1104

/*
 * Register the given device unique identifier into devmap struct.
1105 1106 1107
 * In addition check if the related storage server subsystem ID is already
 * contained in the dasd_server_ssid_list. If subsystem ID is not contained,
 * create new entry.
1108 1109 1110
 * Return 0 if server was already in serverlist,
 *	  1 if the server was added successful
 *	 <0 in case of error.
1111 1112 1113 1114 1115 1116
 */
int
dasd_set_uid(struct ccw_device *cdev, struct dasd_uid *uid)
{
	struct dasd_devmap *devmap;

1117
	devmap = dasd_find_busid(dev_name(&cdev->dev));
1118 1119
	if (IS_ERR(devmap))
		return PTR_ERR(devmap);
1120

1121 1122 1123
	spin_lock(&dasd_devmap_lock);
	devmap->uid = *uid;
	spin_unlock(&dasd_devmap_lock);
1124

1125
	return 0;
1126
}
1127
EXPORT_SYMBOL_GPL(dasd_set_uid);
1128

1129 1130 1131 1132 1133 1134 1135 1136
/*
 * Return value of the specified feature.
 */
int
dasd_get_feature(struct ccw_device *cdev, int feature)
{
	struct dasd_devmap *devmap;

1137
	devmap = dasd_find_busid(dev_name(&cdev->dev));
1138
	if (IS_ERR(devmap))
1139
		return PTR_ERR(devmap);
1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152

	return ((devmap->features & feature) != 0);
}

/*
 * Set / reset given feature.
 * Flag indicates wether to set (!=0) or the reset (=0) the feature.
 */
int
dasd_set_feature(struct ccw_device *cdev, int feature, int flag)
{
	struct dasd_devmap *devmap;

1153
	devmap = dasd_find_busid(dev_name(&cdev->dev));
1154
	if (IS_ERR(devmap))
1155
		return PTR_ERR(devmap);
1156 1157 1158 1159 1160 1161

	spin_lock(&dasd_devmap_lock);
	if (flag)
		devmap->features |= feature;
	else
		devmap->features &= ~feature;
1162 1163
	if (devmap->device)
		devmap->device->features = devmap->features;
1164 1165 1166 1167 1168
	spin_unlock(&dasd_devmap_lock);
	return 0;
}


L
Linus Torvalds 已提交
1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190
int
dasd_add_sysfs_files(struct ccw_device *cdev)
{
	return sysfs_create_group(&cdev->dev.kobj, &dasd_attr_group);
}

void
dasd_remove_sysfs_files(struct ccw_device *cdev)
{
	sysfs_remove_group(&cdev->dev.kobj, &dasd_attr_group);
}


int
dasd_devmap_init(void)
{
	int i;

	/* Initialize devmap structures. */
	dasd_max_devindex = 0;
	for (i = 0; i < 256; i++)
		INIT_LIST_HEAD(&dasd_hashlists[i]);
1191
	return 0;
L
Linus Torvalds 已提交
1192 1193 1194 1195 1196 1197 1198
}

void
dasd_devmap_exit(void)
{
	dasd_forget_ranges();
}