Merge master.kernel.org:/pub/scm/linux/kernel/git/steve/gfs2-2.6-nmw
[sfrench/cifs-2.6.git] / arch / s390 / mm / extmem.c
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 #include <linux/kernel.h>
11 #include <linux/string.h>
12 #include <linux/spinlock.h>
13 #include <linux/list.h>
14 #include <linux/slab.h>
15 #include <linux/module.h>
16 #include <linux/bootmem.h>
17 #include <linux/ctype.h>
18 #include <asm/page.h>
19 #include <asm/ebcdic.h>
20 #include <asm/errno.h>
21 #include <asm/extmem.h>
22 #include <asm/cpcmd.h>
23 #include <asm/setup.h>
24
25 #define DCSS_DEBUG      /* Debug messages on/off */
26
27 #define DCSS_NAME "extmem"
28 #ifdef DCSS_DEBUG
29 #define PRINT_DEBUG(x...)       printk(KERN_DEBUG DCSS_NAME " debug:" x)
30 #else
31 #define PRINT_DEBUG(x...)   do {} while (0)
32 #endif
33 #define PRINT_INFO(x...)        printk(KERN_INFO DCSS_NAME " info:" x)
34 #define PRINT_WARN(x...)        printk(KERN_WARNING DCSS_NAME " warning:" x)
35 #define PRINT_ERR(x...)         printk(KERN_ERR DCSS_NAME " error:" x)
36
37
38 #define DCSS_LOADSHR    0x00
39 #define DCSS_LOADNSR    0x04
40 #define DCSS_PURGESEG   0x08
41 #define DCSS_FINDSEG    0x0c
42 #define DCSS_LOADNOLY   0x10
43 #define DCSS_SEGEXT     0x18
44 #define DCSS_FINDSEGA   0x0c
45
46 struct qrange {
47         unsigned int  start; // 3byte start address, 1 byte type
48         unsigned int  end;   // 3byte end address, 1 byte reserved
49 };
50
51 struct qout64 {
52         int segstart;
53         int segend;
54         int segcnt;
55         int segrcnt;
56         struct qrange range[6];
57 };
58
59 struct qin64 {
60         char qopcode;
61         char rsrv1[3];
62         char qrcode;
63         char rsrv2[3];
64         char qname[8];
65         unsigned int qoutptr;
66         short int qoutlen;
67 };
68
69 struct dcss_segment {
70         struct list_head list;
71         char dcss_name[8];
72         unsigned long start_addr;
73         unsigned long end;
74         atomic_t ref_count;
75         int do_nonshared;
76         unsigned int vm_segtype;
77         struct qrange range[6];
78         int segcnt;
79 };
80
81 static DEFINE_MUTEX(dcss_lock);
82 static struct list_head dcss_list = LIST_HEAD_INIT(dcss_list);
83 static char *segtype_string[] = { "SW", "EW", "SR", "ER", "SN", "EN", "SC",
84                                         "EW/EN-MIXED" };
85
86 /*
87  * Create the 8 bytes, ebcdic VM segment name from
88  * an ascii name.
89  */
90 static void inline
91 dcss_mkname(char *name, char *dcss_name)
92 {
93         int i;
94
95         for (i = 0; i < 8; i++) {
96                 if (name[i] == '\0')
97                         break;
98                 dcss_name[i] = toupper(name[i]);
99         };
100         for (; i < 8; i++)
101                 dcss_name[i] = ' ';
102         ASCEBC(dcss_name, 8);
103 }
104
105
106 /*
107  * search all segments in dcss_list, and return the one
108  * namend *name. If not found, return NULL.
109  */
110 static struct dcss_segment *
111 segment_by_name (char *name)
112 {
113         char dcss_name[9];
114         struct list_head *l;
115         struct dcss_segment *tmp, *retval = NULL;
116
117         BUG_ON(!mutex_is_locked(&dcss_lock));
118         dcss_mkname (name, dcss_name);
119         list_for_each (l, &dcss_list) {
120                 tmp = list_entry (l, struct dcss_segment, list);
121                 if (memcmp(tmp->dcss_name, dcss_name, 8) == 0) {
122                         retval = tmp;
123                         break;
124                 }
125         }
126         return retval;
127 }
128
129
130 /*
131  * Perform a function on a dcss segment.
132  */
133 static inline int
134 dcss_diag (__u8 func, void *parameter,
135            unsigned long *ret1, unsigned long *ret2)
136 {
137         unsigned long rx, ry;
138         int rc;
139
140         rx = (unsigned long) parameter;
141         ry = (unsigned long) func;
142         asm volatile(
143 #ifdef CONFIG_64BIT
144                 "       sam31\n"
145                 "       diag    %0,%1,0x64\n"
146                 "       sam64\n"
147 #else
148                 "       diag    %0,%1,0x64\n"
149 #endif
150                 "       ipm     %2\n"
151                 "       srl     %2,28\n"
152                 : "+d" (rx), "+d" (ry), "=d" (rc) : : "cc");
153         *ret1 = rx;
154         *ret2 = ry;
155         return rc;
156 }
157
158 static inline int
159 dcss_diag_translate_rc (int vm_rc) {
160         if (vm_rc == 44)
161                 return -ENOENT;
162         return -EIO;
163 }
164
165
166 /* do a diag to get info about a segment.
167  * fills start_address, end and vm_segtype fields
168  */
169 static int
170 query_segment_type (struct dcss_segment *seg)
171 {
172         struct qin64  *qin = kmalloc (sizeof(struct qin64), GFP_DMA);
173         struct qout64 *qout = kmalloc (sizeof(struct qout64), GFP_DMA);
174
175         int diag_cc, rc, i;
176         unsigned long dummy, vmrc;
177
178         if ((qin == NULL) || (qout == NULL)) {
179                 rc = -ENOMEM;
180                 goto out_free;
181         }
182
183         /* initialize diag input parameters */
184         qin->qopcode = DCSS_FINDSEGA;
185         qin->qoutptr = (unsigned long) qout;
186         qin->qoutlen = sizeof(struct qout64);
187         memcpy (qin->qname, seg->dcss_name, 8);
188
189         diag_cc = dcss_diag (DCSS_SEGEXT, qin, &dummy, &vmrc);
190
191         if (diag_cc > 1) {
192                 PRINT_WARN ("segment_type: diag returned error %ld\n", vmrc);
193                 rc = dcss_diag_translate_rc (vmrc);
194                 goto out_free;
195         }
196
197         if (qout->segcnt > 6) {
198                 rc = -ENOTSUPP;
199                 goto out_free;
200         }
201
202         if (qout->segcnt == 1) {
203                 seg->vm_segtype = qout->range[0].start & 0xff;
204         } else {
205                 /* multi-part segment. only one type supported here:
206                     - all parts are contiguous
207                     - all parts are either EW or EN type
208                     - maximum 6 parts allowed */
209                 unsigned long start = qout->segstart >> PAGE_SHIFT;
210                 for (i=0; i<qout->segcnt; i++) {
211                         if (((qout->range[i].start & 0xff) != SEG_TYPE_EW) &&
212                             ((qout->range[i].start & 0xff) != SEG_TYPE_EN)) {
213                                 rc = -ENOTSUPP;
214                                 goto out_free;
215                         }
216                         if (start != qout->range[i].start >> PAGE_SHIFT) {
217                                 rc = -ENOTSUPP;
218                                 goto out_free;
219                         }
220                         start = (qout->range[i].end >> PAGE_SHIFT) + 1;
221                 }
222                 seg->vm_segtype = SEG_TYPE_EWEN;
223         }
224
225         /* analyze diag output and update seg */
226         seg->start_addr = qout->segstart;
227         seg->end = qout->segend;
228
229         memcpy (seg->range, qout->range, 6*sizeof(struct qrange));
230         seg->segcnt = qout->segcnt;
231
232         rc = 0;
233
234  out_free:
235         kfree(qin);
236         kfree(qout);
237         return rc;
238 }
239
240 /*
241  * check if the given segment collides with guest storage.
242  * returns 1 if this is the case, 0 if no collision was found
243  */
244 static int
245 segment_overlaps_storage(struct dcss_segment *seg)
246 {
247         int i;
248
249         for (i = 0; i < MEMORY_CHUNKS && memory_chunk[i].size > 0; i++) {
250                 if (memory_chunk[i].type != CHUNK_READ_WRITE)
251                         continue;
252                 if ((memory_chunk[i].addr >> 20) > (seg->end >> 20))
253                         continue;
254                 if (((memory_chunk[i].addr + memory_chunk[i].size - 1) >> 20)
255                                 < (seg->start_addr >> 20))
256                         continue;
257                 return 1;
258         }
259         return 0;
260 }
261
262 /*
263  * check if segment collides with other segments that are currently loaded
264  * returns 1 if this is the case, 0 if no collision was found
265  */
266 static int
267 segment_overlaps_others (struct dcss_segment *seg)
268 {
269         struct list_head *l;
270         struct dcss_segment *tmp;
271
272         BUG_ON(!mutex_is_locked(&dcss_lock));
273         list_for_each(l, &dcss_list) {
274                 tmp = list_entry(l, struct dcss_segment, list);
275                 if ((tmp->start_addr >> 20) > (seg->end >> 20))
276                         continue;
277                 if ((tmp->end >> 20) < (seg->start_addr >> 20))
278                         continue;
279                 if (seg == tmp)
280                         continue;
281                 return 1;
282         }
283         return 0;
284 }
285
286 /*
287  * check if segment exceeds the kernel mapping range (detected or set via mem=)
288  * returns 1 if this is the case, 0 if segment fits into the range
289  */
290 static inline int
291 segment_exceeds_range (struct dcss_segment *seg)
292 {
293         int seg_last_pfn = (seg->end) >> PAGE_SHIFT;
294         if (seg_last_pfn > max_pfn)
295                 return 1;
296         return 0;
297 }
298
299 /*
300  * get info about a segment
301  * possible return values:
302  * -ENOSYS  : we are not running on VM
303  * -EIO     : could not perform query diagnose
304  * -ENOENT  : no such segment
305  * -ENOTSUPP: multi-part segment cannot be used with linux
306  * -ENOSPC  : segment cannot be used (overlaps with storage)
307  * -ENOMEM  : out of memory
308  * 0 .. 6   : type of segment as defined in include/asm-s390/extmem.h
309  */
310 int
311 segment_type (char* name)
312 {
313         int rc;
314         struct dcss_segment seg;
315
316         if (!MACHINE_IS_VM)
317                 return -ENOSYS;
318
319         dcss_mkname(name, seg.dcss_name);
320         rc = query_segment_type (&seg);
321         if (rc < 0)
322                 return rc;
323         return seg.vm_segtype;
324 }
325
326 /*
327  * real segment loading function, called from segment_load
328  */
329 static int
330 __segment_load (char *name, int do_nonshared, unsigned long *addr, unsigned long *end)
331 {
332         struct dcss_segment *seg = kmalloc(sizeof(struct dcss_segment),
333                         GFP_DMA);
334         int dcss_command, rc, diag_cc;
335
336         if (seg == NULL) {
337                 rc = -ENOMEM;
338                 goto out;
339         }
340         dcss_mkname (name, seg->dcss_name);
341         rc = query_segment_type (seg);
342         if (rc < 0)
343                 goto out_free;
344         if (segment_exceeds_range(seg)) {
345                 PRINT_WARN ("segment_load: not loading segment %s - exceeds"
346                                 " kernel mapping range\n",name);
347                 rc = -ERANGE;
348                 goto out_free;
349         }
350         if (segment_overlaps_storage(seg)) {
351                 PRINT_WARN ("segment_load: not loading segment %s - overlaps"
352                                 " storage\n",name);
353                 rc = -ENOSPC;
354                 goto out_free;
355         }
356         if (segment_overlaps_others(seg)) {
357                 PRINT_WARN ("segment_load: not loading segment %s - overlaps"
358                                 " other segments\n",name);
359                 rc = -EBUSY;
360                 goto out_free;
361         }
362         if (do_nonshared)
363                 dcss_command = DCSS_LOADNSR;
364         else
365                 dcss_command = DCSS_LOADNOLY;
366
367         diag_cc = dcss_diag(dcss_command, seg->dcss_name,
368                         &seg->start_addr, &seg->end);
369         if (diag_cc > 1) {
370                 PRINT_WARN ("segment_load: could not load segment %s - "
371                                 "diag returned error (%ld)\n",name,seg->end);
372                 rc = dcss_diag_translate_rc (seg->end);
373                 dcss_diag(DCSS_PURGESEG, seg->dcss_name,
374                                 &seg->start_addr, &seg->end);
375                 goto out_free;
376         }
377         seg->do_nonshared = do_nonshared;
378         atomic_set(&seg->ref_count, 1);
379         list_add(&seg->list, &dcss_list);
380         rc = seg->vm_segtype;
381         *addr = seg->start_addr;
382         *end  = seg->end;
383         if (do_nonshared)
384                 PRINT_INFO ("segment_load: loaded segment %s range %p .. %p "
385                                 "type %s in non-shared mode\n", name,
386                                 (void*)seg->start_addr, (void*)seg->end,
387                                 segtype_string[seg->vm_segtype]);
388         else
389                 PRINT_INFO ("segment_load: loaded segment %s range %p .. %p "
390                                 "type %s in shared mode\n", name,
391                                 (void*)seg->start_addr, (void*)seg->end,
392                                 segtype_string[seg->vm_segtype]);
393         goto out;
394  out_free:
395         kfree(seg);
396  out:
397         return rc;
398 }
399
400 /*
401  * this function loads a DCSS segment
402  * name         : name of the DCSS
403  * do_nonshared : 0 indicates that the dcss should be shared with other linux images
404  *                1 indicates that the dcss should be exclusive for this linux image
405  * addr         : will be filled with start address of the segment
406  * end          : will be filled with end address of the segment
407  * return values:
408  * -ENOSYS  : we are not running on VM
409  * -EIO     : could not perform query or load diagnose
410  * -ENOENT  : no such segment
411  * -ENOTSUPP: multi-part segment cannot be used with linux
412  * -ENOSPC  : segment cannot be used (overlaps with storage)
413  * -EBUSY   : segment can temporarily not be used (overlaps with dcss)
414  * -ERANGE  : segment cannot be used (exceeds kernel mapping range)
415  * -EPERM   : segment is currently loaded with incompatible permissions
416  * -ENOMEM  : out of memory
417  * 0 .. 6   : type of segment as defined in include/asm-s390/extmem.h
418  */
419 int
420 segment_load (char *name, int do_nonshared, unsigned long *addr,
421                 unsigned long *end)
422 {
423         struct dcss_segment *seg;
424         int rc;
425
426         if (!MACHINE_IS_VM)
427                 return -ENOSYS;
428
429         mutex_lock(&dcss_lock);
430         seg = segment_by_name (name);
431         if (seg == NULL)
432                 rc = __segment_load (name, do_nonshared, addr, end);
433         else {
434                 if (do_nonshared == seg->do_nonshared) {
435                         atomic_inc(&seg->ref_count);
436                         *addr = seg->start_addr;
437                         *end  = seg->end;
438                         rc    = seg->vm_segtype;
439                 } else {
440                         *addr = *end = 0;
441                         rc    = -EPERM;
442                 }
443         }
444         mutex_unlock(&dcss_lock);
445         return rc;
446 }
447
448 /*
449  * this function modifies the shared state of a DCSS segment. note that
450  * name         : name of the DCSS
451  * do_nonshared : 0 indicates that the dcss should be shared with other linux images
452  *                1 indicates that the dcss should be exclusive for this linux image
453  * return values:
454  * -EIO     : could not perform load diagnose (segment gone!)
455  * -ENOENT  : no such segment (segment gone!)
456  * -EAGAIN  : segment is in use by other exploiters, try later
457  * -EINVAL  : no segment with the given name is currently loaded - name invalid
458  * 0        : operation succeeded
459  */
460 int
461 segment_modify_shared (char *name, int do_nonshared)
462 {
463         struct dcss_segment *seg;
464         unsigned long dummy;
465         int dcss_command, rc, diag_cc;
466
467         mutex_lock(&dcss_lock);
468         seg = segment_by_name (name);
469         if (seg == NULL) {
470                 rc = -EINVAL;
471                 goto out_unlock;
472         }
473         if (do_nonshared == seg->do_nonshared) {
474                 PRINT_INFO ("segment_modify_shared: not reloading segment %s"
475                                 " - already in requested mode\n",name);
476                 rc = 0;
477                 goto out_unlock;
478         }
479         if (atomic_read (&seg->ref_count) != 1) {
480                 PRINT_WARN ("segment_modify_shared: not reloading segment %s - "
481                                 "segment is in use by other driver(s)\n",name);
482                 rc = -EAGAIN;
483                 goto out_unlock;
484         }
485         dcss_diag(DCSS_PURGESEG, seg->dcss_name,
486                   &dummy, &dummy);
487         if (do_nonshared)
488                 dcss_command = DCSS_LOADNSR;
489         else
490         dcss_command = DCSS_LOADNOLY;
491         diag_cc = dcss_diag(dcss_command, seg->dcss_name,
492                         &seg->start_addr, &seg->end);
493         if (diag_cc > 1) {
494                 PRINT_WARN ("segment_modify_shared: could not reload segment %s"
495                                 " - diag returned error (%ld)\n",name,seg->end);
496                 rc = dcss_diag_translate_rc (seg->end);
497                 goto out_del;
498         }
499         seg->do_nonshared = do_nonshared;
500         rc = 0;
501         goto out_unlock;
502  out_del:
503         list_del(&seg->list);
504         dcss_diag(DCSS_PURGESEG, seg->dcss_name,
505                   &dummy, &dummy);
506         kfree(seg);
507  out_unlock:
508         mutex_unlock(&dcss_lock);
509         return rc;
510 }
511
512 /*
513  * Decrease the use count of a DCSS segment and remove
514  * it from the address space if nobody is using it
515  * any longer.
516  */
517 void
518 segment_unload(char *name)
519 {
520         unsigned long dummy;
521         struct dcss_segment *seg;
522
523         if (!MACHINE_IS_VM)
524                 return;
525
526         mutex_lock(&dcss_lock);
527         seg = segment_by_name (name);
528         if (seg == NULL) {
529                 PRINT_ERR ("could not find segment %s in segment_unload, "
530                                 "please report to linux390@de.ibm.com\n",name);
531                 goto out_unlock;
532         }
533         if (atomic_dec_return(&seg->ref_count) == 0) {
534                 list_del(&seg->list);
535                 dcss_diag(DCSS_PURGESEG, seg->dcss_name,
536                           &dummy, &dummy);
537                 kfree(seg);
538         }
539 out_unlock:
540         mutex_unlock(&dcss_lock);
541 }
542
543 /*
544  * save segment content permanently
545  */
546 void
547 segment_save(char *name)
548 {
549         struct dcss_segment *seg;
550         int startpfn = 0;
551         int endpfn = 0;
552         char cmd1[160];
553         char cmd2[80];
554         int i, response;
555
556         if (!MACHINE_IS_VM)
557                 return;
558
559         mutex_lock(&dcss_lock);
560         seg = segment_by_name (name);
561
562         if (seg == NULL) {
563                 PRINT_ERR("could not find segment %s in segment_save, please "
564                           "report to linux390@de.ibm.com\n", name);
565                 goto out;
566         }
567
568         startpfn = seg->start_addr >> PAGE_SHIFT;
569         endpfn = (seg->end) >> PAGE_SHIFT;
570         sprintf(cmd1, "DEFSEG %s", name);
571         for (i=0; i<seg->segcnt; i++) {
572                 sprintf(cmd1+strlen(cmd1), " %X-%X %s",
573                         seg->range[i].start >> PAGE_SHIFT,
574                         seg->range[i].end >> PAGE_SHIFT,
575                         segtype_string[seg->range[i].start & 0xff]);
576         }
577         sprintf(cmd2, "SAVESEG %s", name);
578         response = 0;
579         cpcmd(cmd1, NULL, 0, &response);
580         if (response) {
581                 PRINT_ERR("segment_save: DEFSEG failed with response code %i\n",
582                           response);
583                 goto out;
584         }
585         cpcmd(cmd2, NULL, 0, &response);
586         if (response) {
587                 PRINT_ERR("segment_save: SAVESEG failed with response code %i\n",
588                           response);
589                 goto out;
590         }
591 out:
592         mutex_unlock(&dcss_lock);
593 }
594
595 EXPORT_SYMBOL(segment_load);
596 EXPORT_SYMBOL(segment_unload);
597 EXPORT_SYMBOL(segment_save);
598 EXPORT_SYMBOL(segment_type);
599 EXPORT_SYMBOL(segment_modify_shared);