Linux Audio

Check our new training course

Loading...
v3.5.6
 
  1/*
  2 * File...........: arch/s390/mm/extmem.c
  3 * Author(s)......: Carsten Otte <cotte@de.ibm.com>
  4 * 		    Rob M van der Heij <rvdheij@nl.ibm.com>
  5 * 		    Steven Shultz <shultzss@us.ibm.com>
  6 * Bugreports.to..: <Linux390@de.ibm.com>
  7 * (C) IBM Corporation 2002-2004
  8 */
  9
 10#define KMSG_COMPONENT "extmem"
 11#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
 12
 13#include <linux/kernel.h>
 14#include <linux/string.h>
 15#include <linux/spinlock.h>
 16#include <linux/list.h>
 17#include <linux/slab.h>
 18#include <linux/module.h>
 19#include <linux/bootmem.h>
 20#include <linux/ctype.h>
 21#include <linux/ioport.h>
 
 22#include <asm/page.h>
 23#include <asm/pgtable.h>
 24#include <asm/ebcdic.h>
 25#include <asm/errno.h>
 26#include <asm/extmem.h>
 27#include <asm/cpcmd.h>
 28#include <asm/setup.h>
 29
 30#define DCSS_LOADSHR    0x00
 31#define DCSS_LOADNSR    0x04
 32#define DCSS_PURGESEG   0x08
 33#define DCSS_FINDSEG    0x0c
 34#define DCSS_LOADNOLY   0x10
 35#define DCSS_SEGEXT     0x18
 36#define DCSS_LOADSHRX	0x20
 37#define DCSS_LOADNSRX	0x24
 38#define DCSS_FINDSEGX	0x2c
 39#define DCSS_SEGEXTX	0x38
 40#define DCSS_FINDSEGA   0x0c
 41
 42struct qrange {
 43	unsigned long  start; /* last byte type */
 44	unsigned long  end;   /* last byte reserved */
 45};
 46
 47struct qout64 {
 48	unsigned long segstart;
 49	unsigned long segend;
 50	int segcnt;
 51	int segrcnt;
 52	struct qrange range[6];
 53};
 54
 55#ifdef CONFIG_64BIT
 56struct qrange_old {
 57	unsigned int start; /* last byte type */
 58	unsigned int end;   /* last byte reserved */
 59};
 60
 61/* output area format for the Diag x'64' old subcode x'18' */
 62struct qout64_old {
 63	int segstart;
 64	int segend;
 65	int segcnt;
 66	int segrcnt;
 67	struct qrange_old range[6];
 68};
 69#endif
 70
 71struct qin64 {
 72	char qopcode;
 73	char rsrv1[3];
 74	char qrcode;
 75	char rsrv2[3];
 76	char qname[8];
 77	unsigned int qoutptr;
 78	short int qoutlen;
 79};
 80
 81struct dcss_segment {
 82	struct list_head list;
 83	char dcss_name[8];
 84	char res_name[15];
 85	unsigned long start_addr;
 86	unsigned long end;
 87	atomic_t ref_count;
 88	int do_nonshared;
 89	unsigned int vm_segtype;
 90	struct qrange range[6];
 91	int segcnt;
 92	struct resource *res;
 93};
 94
 95static DEFINE_MUTEX(dcss_lock);
 96static LIST_HEAD(dcss_list);
 97static char *segtype_string[] = { "SW", "EW", "SR", "ER", "SN", "EN", "SC",
 98					"EW/EN-MIXED" };
 99static int loadshr_scode, loadnsr_scode, findseg_scode;
100static int segext_scode, purgeseg_scode;
101static int scode_set;
102
103/* set correct Diag x'64' subcodes. */
104static int
105dcss_set_subcodes(void)
106{
107#ifdef CONFIG_64BIT
108	char *name = kmalloc(8 * sizeof(char), GFP_KERNEL | GFP_DMA);
109	unsigned long rx, ry;
110	int rc;
111
112	if (name == NULL)
113		return -ENOMEM;
114
115	rx = (unsigned long) name;
116	ry = DCSS_FINDSEGX;
117
118	strcpy(name, "dummy");
 
119	asm volatile(
120		"	diag	%0,%1,0x64\n"
121		"0:	ipm	%2\n"
122		"	srl	%2,28\n"
123		"	j	2f\n"
124		"1:	la	%2,3\n"
125		"2:\n"
126		EX_TABLE(0b, 1b)
127		: "+d" (rx), "+d" (ry), "=d" (rc) : : "cc");
128
129	kfree(name);
130	/* Diag x'64' new subcodes are supported, set to new subcodes */
131	if (rc != 3) {
132		loadshr_scode = DCSS_LOADSHRX;
133		loadnsr_scode = DCSS_LOADNSRX;
134		purgeseg_scode = DCSS_PURGESEG;
135		findseg_scode = DCSS_FINDSEGX;
136		segext_scode = DCSS_SEGEXTX;
137		return 0;
138	}
139#endif
140	/* Diag x'64' new subcodes are not supported, set to old subcodes */
141	loadshr_scode = DCSS_LOADNOLY;
142	loadnsr_scode = DCSS_LOADNSR;
143	purgeseg_scode = DCSS_PURGESEG;
144	findseg_scode = DCSS_FINDSEG;
145	segext_scode = DCSS_SEGEXT;
146	return 0;
147}
148
149/*
150 * Create the 8 bytes, ebcdic VM segment name from
151 * an ascii name.
152 */
153static void
154dcss_mkname(char *name, char *dcss_name)
155{
156	int i;
157
158	for (i = 0; i < 8; i++) {
159		if (name[i] == '\0')
160			break;
161		dcss_name[i] = toupper(name[i]);
162	};
163	for (; i < 8; i++)
164		dcss_name[i] = ' ';
165	ASCEBC(dcss_name, 8);
166}
167
168
169/*
170 * search all segments in dcss_list, and return the one
171 * namend *name. If not found, return NULL.
172 */
173static struct dcss_segment *
174segment_by_name (char *name)
175{
176	char dcss_name[9];
177	struct list_head *l;
178	struct dcss_segment *tmp, *retval = NULL;
179
180	BUG_ON(!mutex_is_locked(&dcss_lock));
181	dcss_mkname (name, dcss_name);
182	list_for_each (l, &dcss_list) {
183		tmp = list_entry (l, struct dcss_segment, list);
184		if (memcmp(tmp->dcss_name, dcss_name, 8) == 0) {
185			retval = tmp;
186			break;
187		}
188	}
189	return retval;
190}
191
192
193/*
194 * Perform a function on a dcss segment.
195 */
196static inline int
197dcss_diag(int *func, void *parameter,
198           unsigned long *ret1, unsigned long *ret2)
199{
200	unsigned long rx, ry;
201	int rc;
202
203	if (scode_set == 0) {
204		rc = dcss_set_subcodes();
205		if (rc < 0)
206			return rc;
207		scode_set = 1;
208	}
209	rx = (unsigned long) parameter;
210	ry = (unsigned long) *func;
211
212#ifdef CONFIG_64BIT
213	/* 64-bit Diag x'64' new subcode, keep in 64-bit addressing mode */
 
214	if (*func > DCSS_SEGEXT)
215		asm volatile(
216			"	diag	%0,%1,0x64\n"
217			"	ipm	%2\n"
218			"	srl	%2,28\n"
219			: "+d" (rx), "+d" (ry), "=d" (rc) : : "cc");
220	/* 31-bit Diag x'64' old subcode, switch to 31-bit addressing mode */
221	else
222		asm volatile(
223			"	sam31\n"
224			"	diag	%0,%1,0x64\n"
225			"	sam64\n"
226			"	ipm	%2\n"
227			"	srl	%2,28\n"
228			: "+d" (rx), "+d" (ry), "=d" (rc) : : "cc");
229#else
230	asm volatile(
231		"	diag	%0,%1,0x64\n"
232		"	ipm	%2\n"
233		"	srl	%2,28\n"
234		: "+d" (rx), "+d" (ry), "=d" (rc) : : "cc");
235#endif
236	*ret1 = rx;
237	*ret2 = ry;
238	return rc;
239}
240
241static inline int
242dcss_diag_translate_rc (int vm_rc) {
243	if (vm_rc == 44)
244		return -ENOENT;
245	return -EIO;
246}
247
248
249/* do a diag to get info about a segment.
250 * fills start_address, end and vm_segtype fields
251 */
252static int
253query_segment_type (struct dcss_segment *seg)
254{
255	unsigned long dummy, vmrc;
256	int diag_cc, rc, i;
257	struct qout64 *qout;
258	struct qin64 *qin;
259
260	qin = kmalloc(sizeof(*qin), GFP_KERNEL | GFP_DMA);
261	qout = kmalloc(sizeof(*qout), GFP_KERNEL | GFP_DMA);
262	if ((qin == NULL) || (qout == NULL)) {
263		rc = -ENOMEM;
264		goto out_free;
265	}
266
267	/* initialize diag input parameters */
268	qin->qopcode = DCSS_FINDSEGA;
269	qin->qoutptr = (unsigned long) qout;
270	qin->qoutlen = sizeof(struct qout64);
271	memcpy (qin->qname, seg->dcss_name, 8);
272
273	diag_cc = dcss_diag(&segext_scode, qin, &dummy, &vmrc);
274
275	if (diag_cc < 0) {
276		rc = diag_cc;
277		goto out_free;
278	}
279	if (diag_cc > 1) {
280		pr_warning("Querying a DCSS type failed with rc=%ld\n", vmrc);
281		rc = dcss_diag_translate_rc (vmrc);
282		goto out_free;
283	}
284
285#ifdef CONFIG_64BIT
286	/* Only old format of output area of Diagnose x'64' is supported,
287	   copy data for the new format. */
288	if (segext_scode == DCSS_SEGEXT) {
289		struct qout64_old *qout_old;
290		qout_old = kzalloc(sizeof(*qout_old), GFP_KERNEL | GFP_DMA);
291		if (qout_old == NULL) {
292			rc = -ENOMEM;
293			goto out_free;
294		}
295		memcpy(qout_old, qout, sizeof(struct qout64_old));
296		qout->segstart = (unsigned long) qout_old->segstart;
297		qout->segend = (unsigned long) qout_old->segend;
298		qout->segcnt = qout_old->segcnt;
299		qout->segrcnt = qout_old->segrcnt;
300
301		if (qout->segcnt > 6)
302			qout->segrcnt = 6;
303		for (i = 0; i < qout->segrcnt; i++) {
304			qout->range[i].start =
305				(unsigned long) qout_old->range[i].start;
306			qout->range[i].end =
307				(unsigned long) qout_old->range[i].end;
308		}
309		kfree(qout_old);
310	}
311#endif
312	if (qout->segcnt > 6) {
313		rc = -EOPNOTSUPP;
314		goto out_free;
315	}
316
317	if (qout->segcnt == 1) {
318		seg->vm_segtype = qout->range[0].start & 0xff;
319	} else {
320		/* multi-part segment. only one type supported here:
321		    - all parts are contiguous
322		    - all parts are either EW or EN type
323		    - maximum 6 parts allowed */
324		unsigned long start = qout->segstart >> PAGE_SHIFT;
325		for (i=0; i<qout->segcnt; i++) {
326			if (((qout->range[i].start & 0xff) != SEG_TYPE_EW) &&
327			    ((qout->range[i].start & 0xff) != SEG_TYPE_EN)) {
328				rc = -EOPNOTSUPP;
329				goto out_free;
330			}
331			if (start != qout->range[i].start >> PAGE_SHIFT) {
332				rc = -EOPNOTSUPP;
333				goto out_free;
334			}
335			start = (qout->range[i].end >> PAGE_SHIFT) + 1;
336		}
337		seg->vm_segtype = SEG_TYPE_EWEN;
338	}
339
340	/* analyze diag output and update seg */
341	seg->start_addr = qout->segstart;
342	seg->end = qout->segend;
343
344	memcpy (seg->range, qout->range, 6*sizeof(struct qrange));
345	seg->segcnt = qout->segcnt;
346
347	rc = 0;
348
349 out_free:
350	kfree(qin);
351	kfree(qout);
352	return rc;
353}
354
355/*
356 * get info about a segment
357 * possible return values:
358 * -ENOSYS  : we are not running on VM
359 * -EIO     : could not perform query diagnose
360 * -ENOENT  : no such segment
361 * -EOPNOTSUPP: multi-part segment cannot be used with linux
362 * -ENOMEM  : out of memory
363 * 0 .. 6   : type of segment as defined in include/asm-s390/extmem.h
364 */
365int
366segment_type (char* name)
367{
368	int rc;
369	struct dcss_segment seg;
370
371	if (!MACHINE_IS_VM)
372		return -ENOSYS;
373
374	dcss_mkname(name, seg.dcss_name);
375	rc = query_segment_type (&seg);
376	if (rc < 0)
377		return rc;
378	return seg.vm_segtype;
379}
380
381/*
382 * check if segment collides with other segments that are currently loaded
383 * returns 1 if this is the case, 0 if no collision was found
384 */
385static int
386segment_overlaps_others (struct dcss_segment *seg)
387{
388	struct list_head *l;
389	struct dcss_segment *tmp;
390
391	BUG_ON(!mutex_is_locked(&dcss_lock));
392	list_for_each(l, &dcss_list) {
393		tmp = list_entry(l, struct dcss_segment, list);
394		if ((tmp->start_addr >> 20) > (seg->end >> 20))
395			continue;
396		if ((tmp->end >> 20) < (seg->start_addr >> 20))
397			continue;
398		if (seg == tmp)
399			continue;
400		return 1;
401	}
402	return 0;
403}
404
405/*
406 * real segment loading function, called from segment_load
407 */
408static int
409__segment_load (char *name, int do_nonshared, unsigned long *addr, unsigned long *end)
410{
411	unsigned long start_addr, end_addr, dummy;
412	struct dcss_segment *seg;
413	int rc, diag_cc;
414
415	start_addr = end_addr = 0;
416	seg = kmalloc(sizeof(*seg), GFP_KERNEL | GFP_DMA);
417	if (seg == NULL) {
418		rc = -ENOMEM;
419		goto out;
420	}
421	dcss_mkname (name, seg->dcss_name);
422	rc = query_segment_type (seg);
423	if (rc < 0)
424		goto out_free;
425
426	if (loadshr_scode == DCSS_LOADSHRX) {
427		if (segment_overlaps_others(seg)) {
428			rc = -EBUSY;
429			goto out_free;
430		}
431	}
432
433	rc = vmem_add_mapping(seg->start_addr, seg->end - seg->start_addr + 1);
434
435	if (rc)
436		goto out_free;
437
438	seg->res = kzalloc(sizeof(struct resource), GFP_KERNEL);
439	if (seg->res == NULL) {
440		rc = -ENOMEM;
441		goto out_shared;
442	}
443	seg->res->flags = IORESOURCE_BUSY | IORESOURCE_MEM;
444	seg->res->start = seg->start_addr;
445	seg->res->end = seg->end;
446	memcpy(&seg->res_name, seg->dcss_name, 8);
447	EBCASC(seg->res_name, 8);
448	seg->res_name[8] = '\0';
449	strncat(seg->res_name, " (DCSS)", 7);
450	seg->res->name = seg->res_name;
451	rc = seg->vm_segtype;
452	if (rc == SEG_TYPE_SC ||
453	    ((rc == SEG_TYPE_SR || rc == SEG_TYPE_ER) && !do_nonshared))
454		seg->res->flags |= IORESOURCE_READONLY;
455	if (request_resource(&iomem_resource, seg->res)) {
456		rc = -EBUSY;
457		kfree(seg->res);
458		goto out_shared;
459	}
460
461	if (do_nonshared)
462		diag_cc = dcss_diag(&loadnsr_scode, seg->dcss_name,
463				&start_addr, &end_addr);
464	else
465		diag_cc = dcss_diag(&loadshr_scode, seg->dcss_name,
466				&start_addr, &end_addr);
467	if (diag_cc < 0) {
468		dcss_diag(&purgeseg_scode, seg->dcss_name,
469				&dummy, &dummy);
470		rc = diag_cc;
471		goto out_resource;
472	}
473	if (diag_cc > 1) {
474		pr_warning("Loading DCSS %s failed with rc=%ld\n", name,
475			   end_addr);
476		rc = dcss_diag_translate_rc(end_addr);
477		dcss_diag(&purgeseg_scode, seg->dcss_name,
478				&dummy, &dummy);
479		goto out_resource;
480	}
481	seg->start_addr = start_addr;
482	seg->end = end_addr;
483	seg->do_nonshared = do_nonshared;
484	atomic_set(&seg->ref_count, 1);
485	list_add(&seg->list, &dcss_list);
486	*addr = seg->start_addr;
487	*end  = seg->end;
488	if (do_nonshared)
489		pr_info("DCSS %s of range %p to %p and type %s loaded as "
490			"exclusive-writable\n", name, (void*) seg->start_addr,
491			(void*) seg->end, segtype_string[seg->vm_segtype]);
492	else {
493		pr_info("DCSS %s of range %p to %p and type %s loaded in "
494			"shared access mode\n", name, (void*) seg->start_addr,
495			(void*) seg->end, segtype_string[seg->vm_segtype]);
496	}
497	goto out;
498 out_resource:
499	release_resource(seg->res);
500	kfree(seg->res);
501 out_shared:
502	vmem_remove_mapping(seg->start_addr, seg->end - seg->start_addr + 1);
503 out_free:
504	kfree(seg);
505 out:
506	return rc;
507}
508
509/*
510 * this function loads a DCSS segment
511 * name         : name of the DCSS
512 * do_nonshared : 0 indicates that the dcss should be shared with other linux images
513 *                1 indicates that the dcss should be exclusive for this linux image
514 * addr         : will be filled with start address of the segment
515 * end          : will be filled with end address of the segment
516 * return values:
517 * -ENOSYS  : we are not running on VM
518 * -EIO     : could not perform query or load diagnose
519 * -ENOENT  : no such segment
520 * -EOPNOTSUPP: multi-part segment cannot be used with linux
521 * -ENOSPC  : segment cannot be used (overlaps with storage)
522 * -EBUSY   : segment can temporarily not be used (overlaps with dcss)
523 * -ERANGE  : segment cannot be used (exceeds kernel mapping range)
524 * -EPERM   : segment is currently loaded with incompatible permissions
525 * -ENOMEM  : out of memory
526 * 0 .. 6   : type of segment as defined in include/asm-s390/extmem.h
527 */
528int
529segment_load (char *name, int do_nonshared, unsigned long *addr,
530		unsigned long *end)
531{
532	struct dcss_segment *seg;
533	int rc;
534
535	if (!MACHINE_IS_VM)
536		return -ENOSYS;
537
538	mutex_lock(&dcss_lock);
539	seg = segment_by_name (name);
540	if (seg == NULL)
541		rc = __segment_load (name, do_nonshared, addr, end);
542	else {
543		if (do_nonshared == seg->do_nonshared) {
544			atomic_inc(&seg->ref_count);
545			*addr = seg->start_addr;
546			*end  = seg->end;
547			rc    = seg->vm_segtype;
548		} else {
549			*addr = *end = 0;
550			rc    = -EPERM;
551		}
552	}
553	mutex_unlock(&dcss_lock);
554	return rc;
555}
556
557/*
558 * this function modifies the shared state of a DCSS segment. note that
559 * name         : name of the DCSS
560 * do_nonshared : 0 indicates that the dcss should be shared with other linux images
561 *                1 indicates that the dcss should be exclusive for this linux image
562 * return values:
563 * -EIO     : could not perform load diagnose (segment gone!)
564 * -ENOENT  : no such segment (segment gone!)
565 * -EAGAIN  : segment is in use by other exploiters, try later
566 * -EINVAL  : no segment with the given name is currently loaded - name invalid
567 * -EBUSY   : segment can temporarily not be used (overlaps with dcss)
568 * 0	    : operation succeeded
569 */
570int
571segment_modify_shared (char *name, int do_nonshared)
572{
573	struct dcss_segment *seg;
574	unsigned long start_addr, end_addr, dummy;
575	int rc, diag_cc;
576
577	start_addr = end_addr = 0;
578	mutex_lock(&dcss_lock);
579	seg = segment_by_name (name);
580	if (seg == NULL) {
581		rc = -EINVAL;
582		goto out_unlock;
583	}
584	if (do_nonshared == seg->do_nonshared) {
585		pr_info("DCSS %s is already in the requested access "
586			"mode\n", name);
587		rc = 0;
588		goto out_unlock;
589	}
590	if (atomic_read (&seg->ref_count) != 1) {
591		pr_warning("DCSS %s is in use and cannot be reloaded\n",
592			   name);
593		rc = -EAGAIN;
594		goto out_unlock;
595	}
596	release_resource(seg->res);
597	if (do_nonshared)
598		seg->res->flags &= ~IORESOURCE_READONLY;
599	else
600		if (seg->vm_segtype == SEG_TYPE_SR ||
601		    seg->vm_segtype == SEG_TYPE_ER)
602			seg->res->flags |= IORESOURCE_READONLY;
603
604	if (request_resource(&iomem_resource, seg->res)) {
605		pr_warning("DCSS %s overlaps with used memory resources "
606			   "and cannot be reloaded\n", name);
607		rc = -EBUSY;
608		kfree(seg->res);
609		goto out_del_mem;
610	}
611
612	dcss_diag(&purgeseg_scode, seg->dcss_name, &dummy, &dummy);
613	if (do_nonshared)
614		diag_cc = dcss_diag(&loadnsr_scode, seg->dcss_name,
615				&start_addr, &end_addr);
616	else
617		diag_cc = dcss_diag(&loadshr_scode, seg->dcss_name,
618				&start_addr, &end_addr);
619	if (diag_cc < 0) {
620		rc = diag_cc;
621		goto out_del_res;
622	}
623	if (diag_cc > 1) {
624		pr_warning("Reloading DCSS %s failed with rc=%ld\n", name,
625			   end_addr);
626		rc = dcss_diag_translate_rc(end_addr);
627		goto out_del_res;
628	}
629	seg->start_addr = start_addr;
630	seg->end = end_addr;
631	seg->do_nonshared = do_nonshared;
632	rc = 0;
633	goto out_unlock;
634 out_del_res:
635	release_resource(seg->res);
636	kfree(seg->res);
637 out_del_mem:
638	vmem_remove_mapping(seg->start_addr, seg->end - seg->start_addr + 1);
639	list_del(&seg->list);
640	dcss_diag(&purgeseg_scode, seg->dcss_name, &dummy, &dummy);
641	kfree(seg);
642 out_unlock:
643	mutex_unlock(&dcss_lock);
644	return rc;
645}
646
647/*
648 * Decrease the use count of a DCSS segment and remove
649 * it from the address space if nobody is using it
650 * any longer.
651 */
652void
653segment_unload(char *name)
654{
655	unsigned long dummy;
656	struct dcss_segment *seg;
657
658	if (!MACHINE_IS_VM)
659		return;
660
661	mutex_lock(&dcss_lock);
662	seg = segment_by_name (name);
663	if (seg == NULL) {
664		pr_err("Unloading unknown DCSS %s failed\n", name);
665		goto out_unlock;
666	}
667	if (atomic_dec_return(&seg->ref_count) != 0)
668		goto out_unlock;
669	release_resource(seg->res);
670	kfree(seg->res);
671	vmem_remove_mapping(seg->start_addr, seg->end - seg->start_addr + 1);
672	list_del(&seg->list);
673	dcss_diag(&purgeseg_scode, seg->dcss_name, &dummy, &dummy);
674	kfree(seg);
675out_unlock:
676	mutex_unlock(&dcss_lock);
677}
678
679/*
680 * save segment content permanently
681 */
682void
683segment_save(char *name)
684{
685	struct dcss_segment *seg;
686	char cmd1[160];
687	char cmd2[80];
688	int i, response;
689
690	if (!MACHINE_IS_VM)
691		return;
692
693	mutex_lock(&dcss_lock);
694	seg = segment_by_name (name);
695
696	if (seg == NULL) {
697		pr_err("Saving unknown DCSS %s failed\n", name);
698		goto out;
699	}
700
701	sprintf(cmd1, "DEFSEG %s", name);
702	for (i=0; i<seg->segcnt; i++) {
703		sprintf(cmd1+strlen(cmd1), " %lX-%lX %s",
704			seg->range[i].start >> PAGE_SHIFT,
705			seg->range[i].end >> PAGE_SHIFT,
706			segtype_string[seg->range[i].start & 0xff]);
707	}
708	sprintf(cmd2, "SAVESEG %s", name);
709	response = 0;
710	cpcmd(cmd1, NULL, 0, &response);
711	if (response) {
712		pr_err("Saving a DCSS failed with DEFSEG response code "
713		       "%i\n", response);
714		goto out;
715	}
716	cpcmd(cmd2, NULL, 0, &response);
717	if (response) {
718		pr_err("Saving a DCSS failed with SAVESEG response code "
719		       "%i\n", response);
720		goto out;
721	}
722out:
723	mutex_unlock(&dcss_lock);
724}
725
726/*
727 * print appropriate error message for segment_load()/segment_type()
728 * return code
729 */
730void segment_warning(int rc, char *seg_name)
731{
732	switch (rc) {
733	case -ENOENT:
734		pr_err("DCSS %s cannot be loaded or queried\n", seg_name);
735		break;
736	case -ENOSYS:
737		pr_err("DCSS %s cannot be loaded or queried without "
738		       "z/VM\n", seg_name);
739		break;
740	case -EIO:
741		pr_err("Loading or querying DCSS %s resulted in a "
742		       "hardware error\n", seg_name);
743		break;
744	case -EOPNOTSUPP:
745		pr_err("DCSS %s has multiple page ranges and cannot be "
746		       "loaded or queried\n", seg_name);
747		break;
748	case -ENOSPC:
749		pr_err("DCSS %s overlaps with used storage and cannot "
750		       "be loaded\n", seg_name);
751		break;
752	case -EBUSY:
753		pr_err("%s needs used memory resources and cannot be "
754		       "loaded or queried\n", seg_name);
755		break;
756	case -EPERM:
757		pr_err("DCSS %s is already loaded in a different access "
758		       "mode\n", seg_name);
759		break;
760	case -ENOMEM:
761		pr_err("There is not enough memory to load or query "
762		       "DCSS %s\n", seg_name);
763		break;
764	case -ERANGE:
765		pr_err("DCSS %s exceeds the kernel mapping range (%lu) "
766		       "and cannot be loaded\n", seg_name, VMEM_MAX_PHYS);
767		break;
768	default:
769		break;
770	}
771}
772
773EXPORT_SYMBOL(segment_load);
774EXPORT_SYMBOL(segment_unload);
775EXPORT_SYMBOL(segment_save);
776EXPORT_SYMBOL(segment_type);
777EXPORT_SYMBOL(segment_modify_shared);
778EXPORT_SYMBOL(segment_warning);
v4.17
  1// SPDX-License-Identifier: GPL-2.0
  2/*
 
  3 * Author(s)......: Carsten Otte <cotte@de.ibm.com>
  4 * 		    Rob M van der Heij <rvdheij@nl.ibm.com>
  5 * 		    Steven Shultz <shultzss@us.ibm.com>
  6 * Bugreports.to..: <Linux390@de.ibm.com>
  7 * Copyright IBM Corp. 2002, 2004
  8 */
  9
 10#define KMSG_COMPONENT "extmem"
 11#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
 12
 13#include <linux/kernel.h>
 14#include <linux/string.h>
 15#include <linux/spinlock.h>
 16#include <linux/list.h>
 17#include <linux/slab.h>
 18#include <linux/export.h>
 19#include <linux/bootmem.h>
 20#include <linux/ctype.h>
 21#include <linux/ioport.h>
 22#include <asm/diag.h>
 23#include <asm/page.h>
 24#include <asm/pgtable.h>
 25#include <asm/ebcdic.h>
 26#include <asm/errno.h>
 27#include <asm/extmem.h>
 28#include <asm/cpcmd.h>
 29#include <asm/setup.h>
 30
 31#define DCSS_LOADSHR    0x00
 32#define DCSS_LOADNSR    0x04
 33#define DCSS_PURGESEG   0x08
 34#define DCSS_FINDSEG    0x0c
 35#define DCSS_LOADNOLY   0x10
 36#define DCSS_SEGEXT     0x18
 37#define DCSS_LOADSHRX	0x20
 38#define DCSS_LOADNSRX	0x24
 39#define DCSS_FINDSEGX	0x2c
 40#define DCSS_SEGEXTX	0x38
 41#define DCSS_FINDSEGA   0x0c
 42
 43struct qrange {
 44	unsigned long  start; /* last byte type */
 45	unsigned long  end;   /* last byte reserved */
 46};
 47
 48struct qout64 {
 49	unsigned long segstart;
 50	unsigned long segend;
 51	int segcnt;
 52	int segrcnt;
 53	struct qrange range[6];
 54};
 55
 
 56struct qrange_old {
 57	unsigned int start; /* last byte type */
 58	unsigned int end;   /* last byte reserved */
 59};
 60
 61/* output area format for the Diag x'64' old subcode x'18' */
 62struct qout64_old {
 63	int segstart;
 64	int segend;
 65	int segcnt;
 66	int segrcnt;
 67	struct qrange_old range[6];
 68};
 
 69
 70struct qin64 {
 71	char qopcode;
 72	char rsrv1[3];
 73	char qrcode;
 74	char rsrv2[3];
 75	char qname[8];
 76	unsigned int qoutptr;
 77	short int qoutlen;
 78};
 79
 80struct dcss_segment {
 81	struct list_head list;
 82	char dcss_name[8];
 83	char res_name[15];
 84	unsigned long start_addr;
 85	unsigned long end;
 86	atomic_t ref_count;
 87	int do_nonshared;
 88	unsigned int vm_segtype;
 89	struct qrange range[6];
 90	int segcnt;
 91	struct resource *res;
 92};
 93
 94static DEFINE_MUTEX(dcss_lock);
 95static LIST_HEAD(dcss_list);
 96static char *segtype_string[] = { "SW", "EW", "SR", "ER", "SN", "EN", "SC",
 97					"EW/EN-MIXED" };
 98static int loadshr_scode, loadnsr_scode;
 99static int segext_scode, purgeseg_scode;
100static int scode_set;
101
102/* set correct Diag x'64' subcodes. */
103static int
104dcss_set_subcodes(void)
105{
 
106	char *name = kmalloc(8 * sizeof(char), GFP_KERNEL | GFP_DMA);
107	unsigned long rx, ry;
108	int rc;
109
110	if (name == NULL)
111		return -ENOMEM;
112
113	rx = (unsigned long) name;
114	ry = DCSS_FINDSEGX;
115
116	strcpy(name, "dummy");
117	diag_stat_inc(DIAG_STAT_X064);
118	asm volatile(
119		"	diag	%0,%1,0x64\n"
120		"0:	ipm	%2\n"
121		"	srl	%2,28\n"
122		"	j	2f\n"
123		"1:	la	%2,3\n"
124		"2:\n"
125		EX_TABLE(0b, 1b)
126		: "+d" (rx), "+d" (ry), "=d" (rc) : : "cc", "memory");
127
128	kfree(name);
129	/* Diag x'64' new subcodes are supported, set to new subcodes */
130	if (rc != 3) {
131		loadshr_scode = DCSS_LOADSHRX;
132		loadnsr_scode = DCSS_LOADNSRX;
133		purgeseg_scode = DCSS_PURGESEG;
 
134		segext_scode = DCSS_SEGEXTX;
135		return 0;
136	}
 
137	/* Diag x'64' new subcodes are not supported, set to old subcodes */
138	loadshr_scode = DCSS_LOADNOLY;
139	loadnsr_scode = DCSS_LOADNSR;
140	purgeseg_scode = DCSS_PURGESEG;
 
141	segext_scode = DCSS_SEGEXT;
142	return 0;
143}
144
145/*
146 * Create the 8 bytes, ebcdic VM segment name from
147 * an ascii name.
148 */
149static void
150dcss_mkname(char *name, char *dcss_name)
151{
152	int i;
153
154	for (i = 0; i < 8; i++) {
155		if (name[i] == '\0')
156			break;
157		dcss_name[i] = toupper(name[i]);
158	}
159	for (; i < 8; i++)
160		dcss_name[i] = ' ';
161	ASCEBC(dcss_name, 8);
162}
163
164
165/*
166 * search all segments in dcss_list, and return the one
167 * namend *name. If not found, return NULL.
168 */
169static struct dcss_segment *
170segment_by_name (char *name)
171{
172	char dcss_name[9];
173	struct list_head *l;
174	struct dcss_segment *tmp, *retval = NULL;
175
176	BUG_ON(!mutex_is_locked(&dcss_lock));
177	dcss_mkname (name, dcss_name);
178	list_for_each (l, &dcss_list) {
179		tmp = list_entry (l, struct dcss_segment, list);
180		if (memcmp(tmp->dcss_name, dcss_name, 8) == 0) {
181			retval = tmp;
182			break;
183		}
184	}
185	return retval;
186}
187
188
189/*
190 * Perform a function on a dcss segment.
191 */
192static inline int
193dcss_diag(int *func, void *parameter,
194           unsigned long *ret1, unsigned long *ret2)
195{
196	unsigned long rx, ry;
197	int rc;
198
199	if (scode_set == 0) {
200		rc = dcss_set_subcodes();
201		if (rc < 0)
202			return rc;
203		scode_set = 1;
204	}
205	rx = (unsigned long) parameter;
206	ry = (unsigned long) *func;
207
 
208	/* 64-bit Diag x'64' new subcode, keep in 64-bit addressing mode */
209	diag_stat_inc(DIAG_STAT_X064);
210	if (*func > DCSS_SEGEXT)
211		asm volatile(
212			"	diag	%0,%1,0x64\n"
213			"	ipm	%2\n"
214			"	srl	%2,28\n"
215			: "+d" (rx), "+d" (ry), "=d" (rc) : : "cc");
216	/* 31-bit Diag x'64' old subcode, switch to 31-bit addressing mode */
217	else
218		asm volatile(
219			"	sam31\n"
220			"	diag	%0,%1,0x64\n"
221			"	sam64\n"
222			"	ipm	%2\n"
223			"	srl	%2,28\n"
224			: "+d" (rx), "+d" (ry), "=d" (rc) : : "cc");
 
 
 
 
 
 
 
225	*ret1 = rx;
226	*ret2 = ry;
227	return rc;
228}
229
230static inline int
231dcss_diag_translate_rc (int vm_rc) {
232	if (vm_rc == 44)
233		return -ENOENT;
234	return -EIO;
235}
236
237
238/* do a diag to get info about a segment.
239 * fills start_address, end and vm_segtype fields
240 */
241static int
242query_segment_type (struct dcss_segment *seg)
243{
244	unsigned long dummy, vmrc;
245	int diag_cc, rc, i;
246	struct qout64 *qout;
247	struct qin64 *qin;
248
249	qin = kmalloc(sizeof(*qin), GFP_KERNEL | GFP_DMA);
250	qout = kmalloc(sizeof(*qout), GFP_KERNEL | GFP_DMA);
251	if ((qin == NULL) || (qout == NULL)) {
252		rc = -ENOMEM;
253		goto out_free;
254	}
255
256	/* initialize diag input parameters */
257	qin->qopcode = DCSS_FINDSEGA;
258	qin->qoutptr = (unsigned long) qout;
259	qin->qoutlen = sizeof(struct qout64);
260	memcpy (qin->qname, seg->dcss_name, 8);
261
262	diag_cc = dcss_diag(&segext_scode, qin, &dummy, &vmrc);
263
264	if (diag_cc < 0) {
265		rc = diag_cc;
266		goto out_free;
267	}
268	if (diag_cc > 1) {
269		pr_warn("Querying a DCSS type failed with rc=%ld\n", vmrc);
270		rc = dcss_diag_translate_rc (vmrc);
271		goto out_free;
272	}
273
 
274	/* Only old format of output area of Diagnose x'64' is supported,
275	   copy data for the new format. */
276	if (segext_scode == DCSS_SEGEXT) {
277		struct qout64_old *qout_old;
278		qout_old = kzalloc(sizeof(*qout_old), GFP_KERNEL | GFP_DMA);
279		if (qout_old == NULL) {
280			rc = -ENOMEM;
281			goto out_free;
282		}
283		memcpy(qout_old, qout, sizeof(struct qout64_old));
284		qout->segstart = (unsigned long) qout_old->segstart;
285		qout->segend = (unsigned long) qout_old->segend;
286		qout->segcnt = qout_old->segcnt;
287		qout->segrcnt = qout_old->segrcnt;
288
289		if (qout->segcnt > 6)
290			qout->segrcnt = 6;
291		for (i = 0; i < qout->segrcnt; i++) {
292			qout->range[i].start =
293				(unsigned long) qout_old->range[i].start;
294			qout->range[i].end =
295				(unsigned long) qout_old->range[i].end;
296		}
297		kfree(qout_old);
298	}
 
299	if (qout->segcnt > 6) {
300		rc = -EOPNOTSUPP;
301		goto out_free;
302	}
303
304	if (qout->segcnt == 1) {
305		seg->vm_segtype = qout->range[0].start & 0xff;
306	} else {
307		/* multi-part segment. only one type supported here:
308		    - all parts are contiguous
309		    - all parts are either EW or EN type
310		    - maximum 6 parts allowed */
311		unsigned long start = qout->segstart >> PAGE_SHIFT;
312		for (i=0; i<qout->segcnt; i++) {
313			if (((qout->range[i].start & 0xff) != SEG_TYPE_EW) &&
314			    ((qout->range[i].start & 0xff) != SEG_TYPE_EN)) {
315				rc = -EOPNOTSUPP;
316				goto out_free;
317			}
318			if (start != qout->range[i].start >> PAGE_SHIFT) {
319				rc = -EOPNOTSUPP;
320				goto out_free;
321			}
322			start = (qout->range[i].end >> PAGE_SHIFT) + 1;
323		}
324		seg->vm_segtype = SEG_TYPE_EWEN;
325	}
326
327	/* analyze diag output and update seg */
328	seg->start_addr = qout->segstart;
329	seg->end = qout->segend;
330
331	memcpy (seg->range, qout->range, 6*sizeof(struct qrange));
332	seg->segcnt = qout->segcnt;
333
334	rc = 0;
335
336 out_free:
337	kfree(qin);
338	kfree(qout);
339	return rc;
340}
341
342/*
343 * get info about a segment
344 * possible return values:
345 * -ENOSYS  : we are not running on VM
346 * -EIO     : could not perform query diagnose
347 * -ENOENT  : no such segment
348 * -EOPNOTSUPP: multi-part segment cannot be used with linux
349 * -ENOMEM  : out of memory
350 * 0 .. 6   : type of segment as defined in include/asm-s390/extmem.h
351 */
352int
353segment_type (char* name)
354{
355	int rc;
356	struct dcss_segment seg;
357
358	if (!MACHINE_IS_VM)
359		return -ENOSYS;
360
361	dcss_mkname(name, seg.dcss_name);
362	rc = query_segment_type (&seg);
363	if (rc < 0)
364		return rc;
365	return seg.vm_segtype;
366}
367
368/*
369 * check if segment collides with other segments that are currently loaded
370 * returns 1 if this is the case, 0 if no collision was found
371 */
372static int
373segment_overlaps_others (struct dcss_segment *seg)
374{
375	struct list_head *l;
376	struct dcss_segment *tmp;
377
378	BUG_ON(!mutex_is_locked(&dcss_lock));
379	list_for_each(l, &dcss_list) {
380		tmp = list_entry(l, struct dcss_segment, list);
381		if ((tmp->start_addr >> 20) > (seg->end >> 20))
382			continue;
383		if ((tmp->end >> 20) < (seg->start_addr >> 20))
384			continue;
385		if (seg == tmp)
386			continue;
387		return 1;
388	}
389	return 0;
390}
391
392/*
393 * real segment loading function, called from segment_load
394 */
395static int
396__segment_load (char *name, int do_nonshared, unsigned long *addr, unsigned long *end)
397{
398	unsigned long start_addr, end_addr, dummy;
399	struct dcss_segment *seg;
400	int rc, diag_cc;
401
402	start_addr = end_addr = 0;
403	seg = kmalloc(sizeof(*seg), GFP_KERNEL | GFP_DMA);
404	if (seg == NULL) {
405		rc = -ENOMEM;
406		goto out;
407	}
408	dcss_mkname (name, seg->dcss_name);
409	rc = query_segment_type (seg);
410	if (rc < 0)
411		goto out_free;
412
413	if (loadshr_scode == DCSS_LOADSHRX) {
414		if (segment_overlaps_others(seg)) {
415			rc = -EBUSY;
416			goto out_free;
417		}
418	}
419
420	rc = vmem_add_mapping(seg->start_addr, seg->end - seg->start_addr + 1);
421
422	if (rc)
423		goto out_free;
424
425	seg->res = kzalloc(sizeof(struct resource), GFP_KERNEL);
426	if (seg->res == NULL) {
427		rc = -ENOMEM;
428		goto out_shared;
429	}
430	seg->res->flags = IORESOURCE_BUSY | IORESOURCE_MEM;
431	seg->res->start = seg->start_addr;
432	seg->res->end = seg->end;
433	memcpy(&seg->res_name, seg->dcss_name, 8);
434	EBCASC(seg->res_name, 8);
435	seg->res_name[8] = '\0';
436	strncat(seg->res_name, " (DCSS)", 7);
437	seg->res->name = seg->res_name;
438	rc = seg->vm_segtype;
439	if (rc == SEG_TYPE_SC ||
440	    ((rc == SEG_TYPE_SR || rc == SEG_TYPE_ER) && !do_nonshared))
441		seg->res->flags |= IORESOURCE_READONLY;
442	if (request_resource(&iomem_resource, seg->res)) {
443		rc = -EBUSY;
444		kfree(seg->res);
445		goto out_shared;
446	}
447
448	if (do_nonshared)
449		diag_cc = dcss_diag(&loadnsr_scode, seg->dcss_name,
450				&start_addr, &end_addr);
451	else
452		diag_cc = dcss_diag(&loadshr_scode, seg->dcss_name,
453				&start_addr, &end_addr);
454	if (diag_cc < 0) {
455		dcss_diag(&purgeseg_scode, seg->dcss_name,
456				&dummy, &dummy);
457		rc = diag_cc;
458		goto out_resource;
459	}
460	if (diag_cc > 1) {
461		pr_warn("Loading DCSS %s failed with rc=%ld\n", name, end_addr);
 
462		rc = dcss_diag_translate_rc(end_addr);
463		dcss_diag(&purgeseg_scode, seg->dcss_name,
464				&dummy, &dummy);
465		goto out_resource;
466	}
467	seg->start_addr = start_addr;
468	seg->end = end_addr;
469	seg->do_nonshared = do_nonshared;
470	atomic_set(&seg->ref_count, 1);
471	list_add(&seg->list, &dcss_list);
472	*addr = seg->start_addr;
473	*end  = seg->end;
474	if (do_nonshared)
475		pr_info("DCSS %s of range %p to %p and type %s loaded as "
476			"exclusive-writable\n", name, (void*) seg->start_addr,
477			(void*) seg->end, segtype_string[seg->vm_segtype]);
478	else {
479		pr_info("DCSS %s of range %p to %p and type %s loaded in "
480			"shared access mode\n", name, (void*) seg->start_addr,
481			(void*) seg->end, segtype_string[seg->vm_segtype]);
482	}
483	goto out;
484 out_resource:
485	release_resource(seg->res);
486	kfree(seg->res);
487 out_shared:
488	vmem_remove_mapping(seg->start_addr, seg->end - seg->start_addr + 1);
489 out_free:
490	kfree(seg);
491 out:
492	return rc;
493}
494
495/*
496 * this function loads a DCSS segment
497 * name         : name of the DCSS
498 * do_nonshared : 0 indicates that the dcss should be shared with other linux images
499 *                1 indicates that the dcss should be exclusive for this linux image
500 * addr         : will be filled with start address of the segment
501 * end          : will be filled with end address of the segment
502 * return values:
503 * -ENOSYS  : we are not running on VM
504 * -EIO     : could not perform query or load diagnose
505 * -ENOENT  : no such segment
506 * -EOPNOTSUPP: multi-part segment cannot be used with linux
507 * -ENOSPC  : segment cannot be used (overlaps with storage)
508 * -EBUSY   : segment can temporarily not be used (overlaps with dcss)
509 * -ERANGE  : segment cannot be used (exceeds kernel mapping range)
510 * -EPERM   : segment is currently loaded with incompatible permissions
511 * -ENOMEM  : out of memory
512 * 0 .. 6   : type of segment as defined in include/asm-s390/extmem.h
513 */
514int
515segment_load (char *name, int do_nonshared, unsigned long *addr,
516		unsigned long *end)
517{
518	struct dcss_segment *seg;
519	int rc;
520
521	if (!MACHINE_IS_VM)
522		return -ENOSYS;
523
524	mutex_lock(&dcss_lock);
525	seg = segment_by_name (name);
526	if (seg == NULL)
527		rc = __segment_load (name, do_nonshared, addr, end);
528	else {
529		if (do_nonshared == seg->do_nonshared) {
530			atomic_inc(&seg->ref_count);
531			*addr = seg->start_addr;
532			*end  = seg->end;
533			rc    = seg->vm_segtype;
534		} else {
535			*addr = *end = 0;
536			rc    = -EPERM;
537		}
538	}
539	mutex_unlock(&dcss_lock);
540	return rc;
541}
542
543/*
544 * this function modifies the shared state of a DCSS segment. note that
545 * name         : name of the DCSS
546 * do_nonshared : 0 indicates that the dcss should be shared with other linux images
547 *                1 indicates that the dcss should be exclusive for this linux image
548 * return values:
549 * -EIO     : could not perform load diagnose (segment gone!)
550 * -ENOENT  : no such segment (segment gone!)
551 * -EAGAIN  : segment is in use by other exploiters, try later
552 * -EINVAL  : no segment with the given name is currently loaded - name invalid
553 * -EBUSY   : segment can temporarily not be used (overlaps with dcss)
554 * 0	    : operation succeeded
555 */
556int
557segment_modify_shared (char *name, int do_nonshared)
558{
559	struct dcss_segment *seg;
560	unsigned long start_addr, end_addr, dummy;
561	int rc, diag_cc;
562
563	start_addr = end_addr = 0;
564	mutex_lock(&dcss_lock);
565	seg = segment_by_name (name);
566	if (seg == NULL) {
567		rc = -EINVAL;
568		goto out_unlock;
569	}
570	if (do_nonshared == seg->do_nonshared) {
571		pr_info("DCSS %s is already in the requested access "
572			"mode\n", name);
573		rc = 0;
574		goto out_unlock;
575	}
576	if (atomic_read (&seg->ref_count) != 1) {
577		pr_warn("DCSS %s is in use and cannot be reloaded\n", name);
 
578		rc = -EAGAIN;
579		goto out_unlock;
580	}
581	release_resource(seg->res);
582	if (do_nonshared)
583		seg->res->flags &= ~IORESOURCE_READONLY;
584	else
585		if (seg->vm_segtype == SEG_TYPE_SR ||
586		    seg->vm_segtype == SEG_TYPE_ER)
587			seg->res->flags |= IORESOURCE_READONLY;
588
589	if (request_resource(&iomem_resource, seg->res)) {
590		pr_warn("DCSS %s overlaps with used memory resources and cannot be reloaded\n",
591			name);
592		rc = -EBUSY;
593		kfree(seg->res);
594		goto out_del_mem;
595	}
596
597	dcss_diag(&purgeseg_scode, seg->dcss_name, &dummy, &dummy);
598	if (do_nonshared)
599		diag_cc = dcss_diag(&loadnsr_scode, seg->dcss_name,
600				&start_addr, &end_addr);
601	else
602		diag_cc = dcss_diag(&loadshr_scode, seg->dcss_name,
603				&start_addr, &end_addr);
604	if (diag_cc < 0) {
605		rc = diag_cc;
606		goto out_del_res;
607	}
608	if (diag_cc > 1) {
609		pr_warn("Reloading DCSS %s failed with rc=%ld\n",
610			name, end_addr);
611		rc = dcss_diag_translate_rc(end_addr);
612		goto out_del_res;
613	}
614	seg->start_addr = start_addr;
615	seg->end = end_addr;
616	seg->do_nonshared = do_nonshared;
617	rc = 0;
618	goto out_unlock;
619 out_del_res:
620	release_resource(seg->res);
621	kfree(seg->res);
622 out_del_mem:
623	vmem_remove_mapping(seg->start_addr, seg->end - seg->start_addr + 1);
624	list_del(&seg->list);
625	dcss_diag(&purgeseg_scode, seg->dcss_name, &dummy, &dummy);
626	kfree(seg);
627 out_unlock:
628	mutex_unlock(&dcss_lock);
629	return rc;
630}
631
632/*
633 * Decrease the use count of a DCSS segment and remove
634 * it from the address space if nobody is using it
635 * any longer.
636 */
637void
638segment_unload(char *name)
639{
640	unsigned long dummy;
641	struct dcss_segment *seg;
642
643	if (!MACHINE_IS_VM)
644		return;
645
646	mutex_lock(&dcss_lock);
647	seg = segment_by_name (name);
648	if (seg == NULL) {
649		pr_err("Unloading unknown DCSS %s failed\n", name);
650		goto out_unlock;
651	}
652	if (atomic_dec_return(&seg->ref_count) != 0)
653		goto out_unlock;
654	release_resource(seg->res);
655	kfree(seg->res);
656	vmem_remove_mapping(seg->start_addr, seg->end - seg->start_addr + 1);
657	list_del(&seg->list);
658	dcss_diag(&purgeseg_scode, seg->dcss_name, &dummy, &dummy);
659	kfree(seg);
660out_unlock:
661	mutex_unlock(&dcss_lock);
662}
663
664/*
665 * save segment content permanently
666 */
667void
668segment_save(char *name)
669{
670	struct dcss_segment *seg;
671	char cmd1[160];
672	char cmd2[80];
673	int i, response;
674
675	if (!MACHINE_IS_VM)
676		return;
677
678	mutex_lock(&dcss_lock);
679	seg = segment_by_name (name);
680
681	if (seg == NULL) {
682		pr_err("Saving unknown DCSS %s failed\n", name);
683		goto out;
684	}
685
686	sprintf(cmd1, "DEFSEG %s", name);
687	for (i=0; i<seg->segcnt; i++) {
688		sprintf(cmd1+strlen(cmd1), " %lX-%lX %s",
689			seg->range[i].start >> PAGE_SHIFT,
690			seg->range[i].end >> PAGE_SHIFT,
691			segtype_string[seg->range[i].start & 0xff]);
692	}
693	sprintf(cmd2, "SAVESEG %s", name);
694	response = 0;
695	cpcmd(cmd1, NULL, 0, &response);
696	if (response) {
697		pr_err("Saving a DCSS failed with DEFSEG response code "
698		       "%i\n", response);
699		goto out;
700	}
701	cpcmd(cmd2, NULL, 0, &response);
702	if (response) {
703		pr_err("Saving a DCSS failed with SAVESEG response code "
704		       "%i\n", response);
705		goto out;
706	}
707out:
708	mutex_unlock(&dcss_lock);
709}
710
711/*
712 * print appropriate error message for segment_load()/segment_type()
713 * return code
714 */
715void segment_warning(int rc, char *seg_name)
716{
717	switch (rc) {
718	case -ENOENT:
719		pr_err("DCSS %s cannot be loaded or queried\n", seg_name);
720		break;
721	case -ENOSYS:
722		pr_err("DCSS %s cannot be loaded or queried without "
723		       "z/VM\n", seg_name);
724		break;
725	case -EIO:
726		pr_err("Loading or querying DCSS %s resulted in a "
727		       "hardware error\n", seg_name);
728		break;
729	case -EOPNOTSUPP:
730		pr_err("DCSS %s has multiple page ranges and cannot be "
731		       "loaded or queried\n", seg_name);
732		break;
733	case -ENOSPC:
734		pr_err("DCSS %s overlaps with used storage and cannot "
735		       "be loaded\n", seg_name);
736		break;
737	case -EBUSY:
738		pr_err("%s needs used memory resources and cannot be "
739		       "loaded or queried\n", seg_name);
740		break;
741	case -EPERM:
742		pr_err("DCSS %s is already loaded in a different access "
743		       "mode\n", seg_name);
744		break;
745	case -ENOMEM:
746		pr_err("There is not enough memory to load or query "
747		       "DCSS %s\n", seg_name);
748		break;
749	case -ERANGE:
750		pr_err("DCSS %s exceeds the kernel mapping range (%lu) "
751		       "and cannot be loaded\n", seg_name, VMEM_MAX_PHYS);
752		break;
753	default:
754		break;
755	}
756}
757
758EXPORT_SYMBOL(segment_load);
759EXPORT_SYMBOL(segment_unload);
760EXPORT_SYMBOL(segment_save);
761EXPORT_SYMBOL(segment_type);
762EXPORT_SYMBOL(segment_modify_shared);
763EXPORT_SYMBOL(segment_warning);