extmem.c 18.2 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5
/*
 * Author(s)......: Carsten Otte <cotte@de.ibm.com>
 * 		    Rob M van der Heij <rvdheij@nl.ibm.com>
 * 		    Steven Shultz <shultzss@us.ibm.com>
 * Bugreports.to..: <Linux390@de.ibm.com>
6
 * Copyright IBM Corp. 2002, 2004
L
Linus Torvalds 已提交
7 8
 */

9 10 11
#define KMSG_COMPONENT "extmem"
#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt

L
Linus Torvalds 已提交
12 13 14 15 16 17 18
#include <linux/kernel.h>
#include <linux/string.h>
#include <linux/spinlock.h>
#include <linux/list.h>
#include <linux/slab.h>
#include <linux/module.h>
#include <linux/bootmem.h>
19
#include <linux/ctype.h>
20
#include <linux/ioport.h>
21
#include <asm/diag.h>
L
Linus Torvalds 已提交
22
#include <asm/page.h>
H
Heiko Carstens 已提交
23
#include <asm/pgtable.h>
L
Linus Torvalds 已提交
24 25 26 27
#include <asm/ebcdic.h>
#include <asm/errno.h>
#include <asm/extmem.h>
#include <asm/cpcmd.h>
28
#include <asm/setup.h>
L
Linus Torvalds 已提交
29 30 31 32 33 34 35

#define DCSS_LOADSHR    0x00
#define DCSS_LOADNSR    0x04
#define DCSS_PURGESEG   0x08
#define DCSS_FINDSEG    0x0c
#define DCSS_LOADNOLY   0x10
#define DCSS_SEGEXT     0x18
36 37 38 39
#define DCSS_LOADSHRX	0x20
#define DCSS_LOADNSRX	0x24
#define DCSS_FINDSEGX	0x2c
#define DCSS_SEGEXTX	0x38
L
Linus Torvalds 已提交
40 41 42
#define DCSS_FINDSEGA   0x0c

struct qrange {
43 44
	unsigned long  start; /* last byte type */
	unsigned long  end;   /* last byte reserved */
L
Linus Torvalds 已提交
45 46 47
};

struct qout64 {
48 49 50 51 52 53 54 55 56 57 58 59 60 61
	unsigned long segstart;
	unsigned long segend;
	int segcnt;
	int segrcnt;
	struct qrange range[6];
};

struct qrange_old {
	unsigned int start; /* last byte type */
	unsigned int end;   /* last byte reserved */
};

/* output area format for the Diag x'64' old subcode x'18' */
struct qout64_old {
L
Linus Torvalds 已提交
62 63 64 65
	int segstart;
	int segend;
	int segcnt;
	int segrcnt;
66
	struct qrange_old range[6];
L
Linus Torvalds 已提交
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
};

struct qin64 {
	char qopcode;
	char rsrv1[3];
	char qrcode;
	char rsrv2[3];
	char qname[8];
	unsigned int qoutptr;
	short int qoutlen;
};

struct dcss_segment {
	struct list_head list;
	char dcss_name[8];
82
	char res_name[15];
L
Linus Torvalds 已提交
83 84 85 86 87 88 89
	unsigned long start_addr;
	unsigned long end;
	atomic_t ref_count;
	int do_nonshared;
	unsigned int vm_segtype;
	struct qrange range[6];
	int segcnt;
90
	struct resource *res;
L
Linus Torvalds 已提交
91 92
};

93
static DEFINE_MUTEX(dcss_lock);
94
static LIST_HEAD(dcss_list);
L
Linus Torvalds 已提交
95 96
static char *segtype_string[] = { "SW", "EW", "SR", "ER", "SN", "EN", "SC",
					"EW/EN-MIXED" };
97
static int loadshr_scode, loadnsr_scode;
98 99 100 101 102 103 104
static int segext_scode, purgeseg_scode;
static int scode_set;

/* set correct Diag x'64' subcodes. */
static int
dcss_set_subcodes(void)
{
105
	char *name = kmalloc(8 * sizeof(char), GFP_KERNEL | GFP_DMA);
106 107 108 109 110 111 112 113 114 115
	unsigned long rx, ry;
	int rc;

	if (name == NULL)
		return -ENOMEM;

	rx = (unsigned long) name;
	ry = DCSS_FINDSEGX;

	strcpy(name, "dummy");
116
	diag_stat_inc(DIAG_STAT_X064);
117 118 119 120 121 122 123 124
	asm volatile(
		"	diag	%0,%1,0x64\n"
		"0:	ipm	%2\n"
		"	srl	%2,28\n"
		"	j	2f\n"
		"1:	la	%2,3\n"
		"2:\n"
		EX_TABLE(0b, 1b)
125
		: "+d" (rx), "+d" (ry), "=d" (rc) : : "cc", "memory");
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142

	kfree(name);
	/* Diag x'64' new subcodes are supported, set to new subcodes */
	if (rc != 3) {
		loadshr_scode = DCSS_LOADSHRX;
		loadnsr_scode = DCSS_LOADNSRX;
		purgeseg_scode = DCSS_PURGESEG;
		segext_scode = DCSS_SEGEXTX;
		return 0;
	}
	/* Diag x'64' new subcodes are not supported, set to old subcodes */
	loadshr_scode = DCSS_LOADNOLY;
	loadnsr_scode = DCSS_LOADNSR;
	purgeseg_scode = DCSS_PURGESEG;
	segext_scode = DCSS_SEGEXT;
	return 0;
}
L
Linus Torvalds 已提交
143 144 145 146 147

/*
 * Create the 8 bytes, ebcdic VM segment name from
 * an ascii name.
 */
148
static void
L
Linus Torvalds 已提交
149 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
dcss_mkname(char *name, char *dcss_name)
{
	int i;

	for (i = 0; i < 8; i++) {
		if (name[i] == '\0')
			break;
		dcss_name[i] = toupper(name[i]);
	};
	for (; i < 8; i++)
		dcss_name[i] = ' ';
	ASCEBC(dcss_name, 8);
}


/*
 * search all segments in dcss_list, and return the one
 * namend *name. If not found, return NULL.
 */
static struct dcss_segment *
segment_by_name (char *name)
{
	char dcss_name[9];
	struct list_head *l;
	struct dcss_segment *tmp, *retval = NULL;

175
	BUG_ON(!mutex_is_locked(&dcss_lock));
L
Linus Torvalds 已提交
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
	dcss_mkname (name, dcss_name);
	list_for_each (l, &dcss_list) {
		tmp = list_entry (l, struct dcss_segment, list);
		if (memcmp(tmp->dcss_name, dcss_name, 8) == 0) {
			retval = tmp;
			break;
		}
	}
	return retval;
}


/*
 * Perform a function on a dcss segment.
 */
static inline int
192
dcss_diag(int *func, void *parameter,
L
Linus Torvalds 已提交
193 194 195 196 197
           unsigned long *ret1, unsigned long *ret2)
{
	unsigned long rx, ry;
	int rc;

198 199 200 201 202 203
	if (scode_set == 0) {
		rc = dcss_set_subcodes();
		if (rc < 0)
			return rc;
		scode_set = 1;
	}
L
Linus Torvalds 已提交
204
	rx = (unsigned long) parameter;
205 206 207
	ry = (unsigned long) *func;

	/* 64-bit Diag x'64' new subcode, keep in 64-bit addressing mode */
208
	diag_stat_inc(DIAG_STAT_X064);
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
	if (*func > DCSS_SEGEXT)
		asm volatile(
			"	diag	%0,%1,0x64\n"
			"	ipm	%2\n"
			"	srl	%2,28\n"
			: "+d" (rx), "+d" (ry), "=d" (rc) : : "cc");
	/* 31-bit Diag x'64' old subcode, switch to 31-bit addressing mode */
	else
		asm volatile(
			"	sam31\n"
			"	diag	%0,%1,0x64\n"
			"	sam64\n"
			"	ipm	%2\n"
			"	srl	%2,28\n"
			: "+d" (rx), "+d" (ry), "=d" (rc) : : "cc");
L
Linus Torvalds 已提交
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
	*ret1 = rx;
	*ret2 = ry;
	return rc;
}

static inline int
dcss_diag_translate_rc (int vm_rc) {
	if (vm_rc == 44)
		return -ENOENT;
	return -EIO;
}


/* do a diag to get info about a segment.
 * fills start_address, end and vm_segtype fields
 */
static int
query_segment_type (struct dcss_segment *seg)
{
	unsigned long dummy, vmrc;
244 245 246
	int diag_cc, rc, i;
	struct qout64 *qout;
	struct qin64 *qin;
L
Linus Torvalds 已提交
247

248 249
	qin = kmalloc(sizeof(*qin), GFP_KERNEL | GFP_DMA);
	qout = kmalloc(sizeof(*qout), GFP_KERNEL | GFP_DMA);
L
Linus Torvalds 已提交
250 251 252 253 254 255 256 257 258 259 260
	if ((qin == NULL) || (qout == NULL)) {
		rc = -ENOMEM;
		goto out_free;
	}

	/* initialize diag input parameters */
	qin->qopcode = DCSS_FINDSEGA;
	qin->qoutptr = (unsigned long) qout;
	qin->qoutlen = sizeof(struct qout64);
	memcpy (qin->qname, seg->dcss_name, 8);

261
	diag_cc = dcss_diag(&segext_scode, qin, &dummy, &vmrc);
L
Linus Torvalds 已提交
262

263 264 265 266
	if (diag_cc < 0) {
		rc = diag_cc;
		goto out_free;
	}
L
Linus Torvalds 已提交
267
	if (diag_cc > 1) {
268
		pr_warn("Querying a DCSS type failed with rc=%ld\n", vmrc);
L
Linus Torvalds 已提交
269 270 271 272
		rc = dcss_diag_translate_rc (vmrc);
		goto out_free;
	}

273 274 275 276
	/* Only old format of output area of Diagnose x'64' is supported,
	   copy data for the new format. */
	if (segext_scode == DCSS_SEGEXT) {
		struct qout64_old *qout_old;
277
		qout_old = kzalloc(sizeof(*qout_old), GFP_KERNEL | GFP_DMA);
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
		if (qout_old == NULL) {
			rc = -ENOMEM;
			goto out_free;
		}
		memcpy(qout_old, qout, sizeof(struct qout64_old));
		qout->segstart = (unsigned long) qout_old->segstart;
		qout->segend = (unsigned long) qout_old->segend;
		qout->segcnt = qout_old->segcnt;
		qout->segrcnt = qout_old->segrcnt;

		if (qout->segcnt > 6)
			qout->segrcnt = 6;
		for (i = 0; i < qout->segrcnt; i++) {
			qout->range[i].start =
				(unsigned long) qout_old->range[i].start;
			qout->range[i].end =
				(unsigned long) qout_old->range[i].end;
		}
		kfree(qout_old);
	}
L
Linus Torvalds 已提交
298
	if (qout->segcnt > 6) {
299
		rc = -EOPNOTSUPP;
L
Linus Torvalds 已提交
300 301 302 303 304 305 306 307 308 309 310 311 312 313
		goto out_free;
	}

	if (qout->segcnt == 1) {
		seg->vm_segtype = qout->range[0].start & 0xff;
	} else {
		/* multi-part segment. only one type supported here:
		    - all parts are contiguous
		    - all parts are either EW or EN type
		    - maximum 6 parts allowed */
		unsigned long start = qout->segstart >> PAGE_SHIFT;
		for (i=0; i<qout->segcnt; i++) {
			if (((qout->range[i].start & 0xff) != SEG_TYPE_EW) &&
			    ((qout->range[i].start & 0xff) != SEG_TYPE_EN)) {
314
				rc = -EOPNOTSUPP;
L
Linus Torvalds 已提交
315 316 317
				goto out_free;
			}
			if (start != qout->range[i].start >> PAGE_SHIFT) {
318
				rc = -EOPNOTSUPP;
L
Linus Torvalds 已提交
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
				goto out_free;
			}
			start = (qout->range[i].end >> PAGE_SHIFT) + 1;
		}
		seg->vm_segtype = SEG_TYPE_EWEN;
	}

	/* analyze diag output and update seg */
	seg->start_addr = qout->segstart;
	seg->end = qout->segend;

	memcpy (seg->range, qout->range, 6*sizeof(struct qrange));
	seg->segcnt = qout->segcnt;

	rc = 0;

 out_free:
J
Jesper Juhl 已提交
336 337
	kfree(qin);
	kfree(qout);
L
Linus Torvalds 已提交
338 339 340 341 342 343 344 345 346
	return rc;
}

/*
 * get info about a segment
 * possible return values:
 * -ENOSYS  : we are not running on VM
 * -EIO     : could not perform query diagnose
 * -ENOENT  : no such segment
347
 * -EOPNOTSUPP: multi-part segment cannot be used with linux
L
Linus Torvalds 已提交
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
 * -ENOMEM  : out of memory
 * 0 .. 6   : type of segment as defined in include/asm-s390/extmem.h
 */
int
segment_type (char* name)
{
	int rc;
	struct dcss_segment seg;

	if (!MACHINE_IS_VM)
		return -ENOSYS;

	dcss_mkname(name, seg.dcss_name);
	rc = query_segment_type (&seg);
	if (rc < 0)
		return rc;
	return seg.vm_segtype;
}

367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
/*
 * check if segment collides with other segments that are currently loaded
 * returns 1 if this is the case, 0 if no collision was found
 */
static int
segment_overlaps_others (struct dcss_segment *seg)
{
	struct list_head *l;
	struct dcss_segment *tmp;

	BUG_ON(!mutex_is_locked(&dcss_lock));
	list_for_each(l, &dcss_list) {
		tmp = list_entry(l, struct dcss_segment, list);
		if ((tmp->start_addr >> 20) > (seg->end >> 20))
			continue;
		if ((tmp->end >> 20) < (seg->start_addr >> 20))
			continue;
		if (seg == tmp)
			continue;
		return 1;
	}
	return 0;
}

L
Linus Torvalds 已提交
391 392 393 394 395 396
/*
 * real segment loading function, called from segment_load
 */
static int
__segment_load (char *name, int do_nonshared, unsigned long *addr, unsigned long *end)
{
397
	unsigned long start_addr, end_addr, dummy;
398 399
	struct dcss_segment *seg;
	int rc, diag_cc;
L
Linus Torvalds 已提交
400

401
	start_addr = end_addr = 0;
402
	seg = kmalloc(sizeof(*seg), GFP_KERNEL | GFP_DMA);
L
Linus Torvalds 已提交
403 404 405 406 407 408 409 410
	if (seg == NULL) {
		rc = -ENOMEM;
		goto out;
	}
	dcss_mkname (name, seg->dcss_name);
	rc = query_segment_type (seg);
	if (rc < 0)
		goto out_free;
H
Heiko Carstens 已提交
411

412 413 414 415 416 417 418
	if (loadshr_scode == DCSS_LOADSHRX) {
		if (segment_overlaps_others(seg)) {
			rc = -EBUSY;
			goto out_free;
		}
	}

419
	rc = vmem_add_mapping(seg->start_addr, seg->end - seg->start_addr + 1);
H
Heiko Carstens 已提交
420

421
	if (rc)
L
Linus Torvalds 已提交
422
		goto out_free;
H
Heiko Carstens 已提交
423

424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446
	seg->res = kzalloc(sizeof(struct resource), GFP_KERNEL);
	if (seg->res == NULL) {
		rc = -ENOMEM;
		goto out_shared;
	}
	seg->res->flags = IORESOURCE_BUSY | IORESOURCE_MEM;
	seg->res->start = seg->start_addr;
	seg->res->end = seg->end;
	memcpy(&seg->res_name, seg->dcss_name, 8);
	EBCASC(seg->res_name, 8);
	seg->res_name[8] = '\0';
	strncat(seg->res_name, " (DCSS)", 7);
	seg->res->name = seg->res_name;
	rc = seg->vm_segtype;
	if (rc == SEG_TYPE_SC ||
	    ((rc == SEG_TYPE_SR || rc == SEG_TYPE_ER) && !do_nonshared))
		seg->res->flags |= IORESOURCE_READONLY;
	if (request_resource(&iomem_resource, seg->res)) {
		rc = -EBUSY;
		kfree(seg->res);
		goto out_shared;
	}

L
Linus Torvalds 已提交
447
	if (do_nonshared)
448 449
		diag_cc = dcss_diag(&loadnsr_scode, seg->dcss_name,
				&start_addr, &end_addr);
L
Linus Torvalds 已提交
450
	else
451 452 453 454 455 456 457 458
		diag_cc = dcss_diag(&loadshr_scode, seg->dcss_name,
				&start_addr, &end_addr);
	if (diag_cc < 0) {
		dcss_diag(&purgeseg_scode, seg->dcss_name,
				&dummy, &dummy);
		rc = diag_cc;
		goto out_resource;
	}
L
Linus Torvalds 已提交
459
	if (diag_cc > 1) {
460
		pr_warn("Loading DCSS %s failed with rc=%ld\n", name, end_addr);
461 462 463
		rc = dcss_diag_translate_rc(end_addr);
		dcss_diag(&purgeseg_scode, seg->dcss_name,
				&dummy, &dummy);
464
		goto out_resource;
L
Linus Torvalds 已提交
465
	}
466 467
	seg->start_addr = start_addr;
	seg->end = end_addr;
L
Linus Torvalds 已提交
468 469 470 471 472 473
	seg->do_nonshared = do_nonshared;
	atomic_set(&seg->ref_count, 1);
	list_add(&seg->list, &dcss_list);
	*addr = seg->start_addr;
	*end  = seg->end;
	if (do_nonshared)
474 475 476
		pr_info("DCSS %s of range %p to %p and type %s loaded as "
			"exclusive-writable\n", name, (void*) seg->start_addr,
			(void*) seg->end, segtype_string[seg->vm_segtype]);
477
	else {
478 479 480
		pr_info("DCSS %s of range %p to %p and type %s loaded in "
			"shared access mode\n", name, (void*) seg->start_addr,
			(void*) seg->end, segtype_string[seg->vm_segtype]);
481
	}
L
Linus Torvalds 已提交
482
	goto out;
483 484 485
 out_resource:
	release_resource(seg->res);
	kfree(seg->res);
H
Heiko Carstens 已提交
486
 out_shared:
487
	vmem_remove_mapping(seg->start_addr, seg->end - seg->start_addr + 1);
L
Linus Torvalds 已提交
488
 out_free:
J
Jesper Juhl 已提交
489
	kfree(seg);
L
Linus Torvalds 已提交
490 491 492 493 494 495 496 497 498 499 500 501 502 503 504
 out:
	return rc;
}

/*
 * this function loads a DCSS segment
 * name         : name of the DCSS
 * do_nonshared : 0 indicates that the dcss should be shared with other linux images
 *                1 indicates that the dcss should be exclusive for this linux image
 * addr         : will be filled with start address of the segment
 * end          : will be filled with end address of the segment
 * return values:
 * -ENOSYS  : we are not running on VM
 * -EIO     : could not perform query or load diagnose
 * -ENOENT  : no such segment
505
 * -EOPNOTSUPP: multi-part segment cannot be used with linux
L
Linus Torvalds 已提交
506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522
 * -ENOSPC  : segment cannot be used (overlaps with storage)
 * -EBUSY   : segment can temporarily not be used (overlaps with dcss)
 * -ERANGE  : segment cannot be used (exceeds kernel mapping range)
 * -EPERM   : segment is currently loaded with incompatible permissions
 * -ENOMEM  : out of memory
 * 0 .. 6   : type of segment as defined in include/asm-s390/extmem.h
 */
int
segment_load (char *name, int do_nonshared, unsigned long *addr,
		unsigned long *end)
{
	struct dcss_segment *seg;
	int rc;

	if (!MACHINE_IS_VM)
		return -ENOSYS;

523
	mutex_lock(&dcss_lock);
L
Linus Torvalds 已提交
524 525 526 527 528 529 530 531 532 533 534 535 536 537
	seg = segment_by_name (name);
	if (seg == NULL)
		rc = __segment_load (name, do_nonshared, addr, end);
	else {
		if (do_nonshared == seg->do_nonshared) {
			atomic_inc(&seg->ref_count);
			*addr = seg->start_addr;
			*end  = seg->end;
			rc    = seg->vm_segtype;
		} else {
			*addr = *end = 0;
			rc    = -EPERM;
		}
	}
538
	mutex_unlock(&dcss_lock);
L
Linus Torvalds 已提交
539 540 541 542 543 544 545 546 547 548 549 550 551
	return rc;
}

/*
 * this function modifies the shared state of a DCSS segment. note that
 * name         : name of the DCSS
 * do_nonshared : 0 indicates that the dcss should be shared with other linux images
 *                1 indicates that the dcss should be exclusive for this linux image
 * return values:
 * -EIO     : could not perform load diagnose (segment gone!)
 * -ENOENT  : no such segment (segment gone!)
 * -EAGAIN  : segment is in use by other exploiters, try later
 * -EINVAL  : no segment with the given name is currently loaded - name invalid
552
 * -EBUSY   : segment can temporarily not be used (overlaps with dcss)
L
Linus Torvalds 已提交
553 554 555 556 557 558
 * 0	    : operation succeeded
 */
int
segment_modify_shared (char *name, int do_nonshared)
{
	struct dcss_segment *seg;
559 560
	unsigned long start_addr, end_addr, dummy;
	int rc, diag_cc;
L
Linus Torvalds 已提交
561

562
	start_addr = end_addr = 0;
563
	mutex_lock(&dcss_lock);
L
Linus Torvalds 已提交
564 565 566 567 568 569
	seg = segment_by_name (name);
	if (seg == NULL) {
		rc = -EINVAL;
		goto out_unlock;
	}
	if (do_nonshared == seg->do_nonshared) {
570 571
		pr_info("DCSS %s is already in the requested access "
			"mode\n", name);
L
Linus Torvalds 已提交
572 573 574 575
		rc = 0;
		goto out_unlock;
	}
	if (atomic_read (&seg->ref_count) != 1) {
576
		pr_warn("DCSS %s is in use and cannot be reloaded\n", name);
L
Linus Torvalds 已提交
577 578 579
		rc = -EAGAIN;
		goto out_unlock;
	}
580
	release_resource(seg->res);
581
	if (do_nonshared)
582
		seg->res->flags &= ~IORESOURCE_READONLY;
583
	else
584 585 586
		if (seg->vm_segtype == SEG_TYPE_SR ||
		    seg->vm_segtype == SEG_TYPE_ER)
			seg->res->flags |= IORESOURCE_READONLY;
587

588
	if (request_resource(&iomem_resource, seg->res)) {
589 590
		pr_warn("DCSS %s overlaps with used memory resources and cannot be reloaded\n",
			name);
591 592
		rc = -EBUSY;
		kfree(seg->res);
593 594 595 596 597 598 599 600 601 602 603 604 605
		goto out_del_mem;
	}

	dcss_diag(&purgeseg_scode, seg->dcss_name, &dummy, &dummy);
	if (do_nonshared)
		diag_cc = dcss_diag(&loadnsr_scode, seg->dcss_name,
				&start_addr, &end_addr);
	else
		diag_cc = dcss_diag(&loadshr_scode, seg->dcss_name,
				&start_addr, &end_addr);
	if (diag_cc < 0) {
		rc = diag_cc;
		goto out_del_res;
606
	}
L
Linus Torvalds 已提交
607
	if (diag_cc > 1) {
608 609
		pr_warn("Reloading DCSS %s failed with rc=%ld\n",
			name, end_addr);
610 611
		rc = dcss_diag_translate_rc(end_addr);
		goto out_del_res;
L
Linus Torvalds 已提交
612
	}
613 614
	seg->start_addr = start_addr;
	seg->end = end_addr;
L
Linus Torvalds 已提交
615 616 617
	seg->do_nonshared = do_nonshared;
	rc = 0;
	goto out_unlock;
618 619 620 621
 out_del_res:
	release_resource(seg->res);
	kfree(seg->res);
 out_del_mem:
622
	vmem_remove_mapping(seg->start_addr, seg->end - seg->start_addr + 1);
L
Linus Torvalds 已提交
623
	list_del(&seg->list);
624
	dcss_diag(&purgeseg_scode, seg->dcss_name, &dummy, &dummy);
J
Jesper Juhl 已提交
625
	kfree(seg);
L
Linus Torvalds 已提交
626
 out_unlock:
627
	mutex_unlock(&dcss_lock);
L
Linus Torvalds 已提交
628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644
	return rc;
}

/*
 * Decrease the use count of a DCSS segment and remove
 * it from the address space if nobody is using it
 * any longer.
 */
void
segment_unload(char *name)
{
	unsigned long dummy;
	struct dcss_segment *seg;

	if (!MACHINE_IS_VM)
		return;

645
	mutex_lock(&dcss_lock);
L
Linus Torvalds 已提交
646 647
	seg = segment_by_name (name);
	if (seg == NULL) {
648
		pr_err("Unloading unknown DCSS %s failed\n", name);
L
Linus Torvalds 已提交
649 650
		goto out_unlock;
	}
H
Heiko Carstens 已提交
651 652
	if (atomic_dec_return(&seg->ref_count) != 0)
		goto out_unlock;
653 654
	release_resource(seg->res);
	kfree(seg->res);
655
	vmem_remove_mapping(seg->start_addr, seg->end - seg->start_addr + 1);
H
Heiko Carstens 已提交
656
	list_del(&seg->list);
657
	dcss_diag(&purgeseg_scode, seg->dcss_name, &dummy, &dummy);
H
Heiko Carstens 已提交
658
	kfree(seg);
L
Linus Torvalds 已提交
659
out_unlock:
660
	mutex_unlock(&dcss_lock);
L
Linus Torvalds 已提交
661 662 663 664 665 666 667 668 669 670 671
}

/*
 * save segment content permanently
 */
void
segment_save(char *name)
{
	struct dcss_segment *seg;
	char cmd1[160];
	char cmd2[80];
672
	int i, response;
L
Linus Torvalds 已提交
673 674 675 676

	if (!MACHINE_IS_VM)
		return;

677
	mutex_lock(&dcss_lock);
L
Linus Torvalds 已提交
678 679 680
	seg = segment_by_name (name);

	if (seg == NULL) {
681
		pr_err("Saving unknown DCSS %s failed\n", name);
682
		goto out;
L
Linus Torvalds 已提交
683 684 685 686
	}

	sprintf(cmd1, "DEFSEG %s", name);
	for (i=0; i<seg->segcnt; i++) {
687
		sprintf(cmd1+strlen(cmd1), " %lX-%lX %s",
L
Linus Torvalds 已提交
688 689 690 691 692
			seg->range[i].start >> PAGE_SHIFT,
			seg->range[i].end >> PAGE_SHIFT,
			segtype_string[seg->range[i].start & 0xff]);
	}
	sprintf(cmd2, "SAVESEG %s", name);
693 694 695
	response = 0;
	cpcmd(cmd1, NULL, 0, &response);
	if (response) {
696 697
		pr_err("Saving a DCSS failed with DEFSEG response code "
		       "%i\n", response);
698 699 700 701
		goto out;
	}
	cpcmd(cmd2, NULL, 0, &response);
	if (response) {
702 703
		pr_err("Saving a DCSS failed with SAVESEG response code "
		       "%i\n", response);
704 705 706
		goto out;
	}
out:
707
	mutex_unlock(&dcss_lock);
L
Linus Torvalds 已提交
708 709
}

710 711 712 713 714 715 716 717
/*
 * print appropriate error message for segment_load()/segment_type()
 * return code
 */
void segment_warning(int rc, char *seg_name)
{
	switch (rc) {
	case -ENOENT:
718
		pr_err("DCSS %s cannot be loaded or queried\n", seg_name);
719 720
		break;
	case -ENOSYS:
721 722
		pr_err("DCSS %s cannot be loaded or queried without "
		       "z/VM\n", seg_name);
723 724
		break;
	case -EIO:
725 726
		pr_err("Loading or querying DCSS %s resulted in a "
		       "hardware error\n", seg_name);
727
		break;
728
	case -EOPNOTSUPP:
729 730
		pr_err("DCSS %s has multiple page ranges and cannot be "
		       "loaded or queried\n", seg_name);
731 732
		break;
	case -ENOSPC:
733 734
		pr_err("DCSS %s overlaps with used storage and cannot "
		       "be loaded\n", seg_name);
735 736
		break;
	case -EBUSY:
737 738
		pr_err("%s needs used memory resources and cannot be "
		       "loaded or queried\n", seg_name);
739 740
		break;
	case -EPERM:
741 742
		pr_err("DCSS %s is already loaded in a different access "
		       "mode\n", seg_name);
743 744
		break;
	case -ENOMEM:
745 746
		pr_err("There is not enough memory to load or query "
		       "DCSS %s\n", seg_name);
747 748
		break;
	case -ERANGE:
749 750
		pr_err("DCSS %s exceeds the kernel mapping range (%lu) "
		       "and cannot be loaded\n", seg_name, VMEM_MAX_PHYS);
751 752 753 754 755 756
		break;
	default:
		break;
	}
}

L
Linus Torvalds 已提交
757 758 759 760 761
EXPORT_SYMBOL(segment_load);
EXPORT_SYMBOL(segment_unload);
EXPORT_SYMBOL(segment_save);
EXPORT_SYMBOL(segment_type);
EXPORT_SYMBOL(segment_modify_shared);
762
EXPORT_SYMBOL(segment_warning);