Merge branch 'for-linus' of git://git.alsa-project.org/alsa-kernel
[pandora-kernel.git] / drivers / s390 / cio / chsc.c
1 /*
2  *  drivers/s390/cio/chsc.c
3  *   S/390 common I/O routines -- channel subsystem call
4  *
5  *    Copyright IBM Corp. 1999,2008
6  *    Author(s): Ingo Adlung (adlung@de.ibm.com)
7  *               Cornelia Huck (cornelia.huck@de.ibm.com)
8  *               Arnd Bergmann (arndb@de.ibm.com)
9  */
10
11 #include <linux/module.h>
12 #include <linux/slab.h>
13 #include <linux/init.h>
14 #include <linux/device.h>
15
16 #include <asm/cio.h>
17 #include <asm/chpid.h>
18 #include <asm/chsc.h>
19
20 #include "../s390mach.h"
21 #include "css.h"
22 #include "cio.h"
23 #include "cio_debug.h"
24 #include "ioasm.h"
25 #include "chp.h"
26 #include "chsc.h"
27
28 static void *sei_page;
29
30 static int chsc_error_from_response(int response)
31 {
32         switch (response) {
33         case 0x0001:
34                 return 0;
35         case 0x0002:
36         case 0x0003:
37         case 0x0006:
38         case 0x0007:
39         case 0x0008:
40         case 0x000a:
41                 return -EINVAL;
42         case 0x0004:
43                 return -EOPNOTSUPP;
44         default:
45                 return -EIO;
46         }
47 }
48
49 struct chsc_ssd_area {
50         struct chsc_header request;
51         u16 :10;
52         u16 ssid:2;
53         u16 :4;
54         u16 f_sch;        /* first subchannel */
55         u16 :16;
56         u16 l_sch;        /* last subchannel */
57         u32 :32;
58         struct chsc_header response;
59         u32 :32;
60         u8 sch_valid : 1;
61         u8 dev_valid : 1;
62         u8 st        : 3; /* subchannel type */
63         u8 zeroes    : 3;
64         u8  unit_addr;    /* unit address */
65         u16 devno;        /* device number */
66         u8 path_mask;
67         u8 fla_valid_mask;
68         u16 sch;          /* subchannel */
69         u8 chpid[8];      /* chpids 0-7 */
70         u16 fla[8];       /* full link addresses 0-7 */
71 } __attribute__ ((packed));
72
73 int chsc_get_ssd_info(struct subchannel_id schid, struct chsc_ssd_info *ssd)
74 {
75         unsigned long page;
76         struct chsc_ssd_area *ssd_area;
77         int ccode;
78         int ret;
79         int i;
80         int mask;
81
82         page = get_zeroed_page(GFP_KERNEL | GFP_DMA);
83         if (!page)
84                 return -ENOMEM;
85         ssd_area = (struct chsc_ssd_area *) page;
86         ssd_area->request.length = 0x0010;
87         ssd_area->request.code = 0x0004;
88         ssd_area->ssid = schid.ssid;
89         ssd_area->f_sch = schid.sch_no;
90         ssd_area->l_sch = schid.sch_no;
91
92         ccode = chsc(ssd_area);
93         /* Check response. */
94         if (ccode > 0) {
95                 ret = (ccode == 3) ? -ENODEV : -EBUSY;
96                 goto out_free;
97         }
98         ret = chsc_error_from_response(ssd_area->response.code);
99         if (ret != 0) {
100                 CIO_MSG_EVENT(2, "chsc: ssd failed for 0.%x.%04x (rc=%04x)\n",
101                               schid.ssid, schid.sch_no,
102                               ssd_area->response.code);
103                 goto out_free;
104         }
105         if (!ssd_area->sch_valid) {
106                 ret = -ENODEV;
107                 goto out_free;
108         }
109         /* Copy data */
110         ret = 0;
111         memset(ssd, 0, sizeof(struct chsc_ssd_info));
112         if ((ssd_area->st != SUBCHANNEL_TYPE_IO) &&
113             (ssd_area->st != SUBCHANNEL_TYPE_MSG))
114                 goto out_free;
115         ssd->path_mask = ssd_area->path_mask;
116         ssd->fla_valid_mask = ssd_area->fla_valid_mask;
117         for (i = 0; i < 8; i++) {
118                 mask = 0x80 >> i;
119                 if (ssd_area->path_mask & mask) {
120                         chp_id_init(&ssd->chpid[i]);
121                         ssd->chpid[i].id = ssd_area->chpid[i];
122                 }
123                 if (ssd_area->fla_valid_mask & mask)
124                         ssd->fla[i] = ssd_area->fla[i];
125         }
126 out_free:
127         free_page(page);
128         return ret;
129 }
130
131 static int s390_subchannel_remove_chpid(struct subchannel *sch, void *data)
132 {
133         spin_lock_irq(sch->lock);
134         if (sch->driver && sch->driver->chp_event)
135                 if (sch->driver->chp_event(sch, data, CHP_OFFLINE) != 0)
136                         goto out_unreg;
137         spin_unlock_irq(sch->lock);
138         return 0;
139
140 out_unreg:
141         sch->lpm = 0;
142         spin_unlock_irq(sch->lock);
143         css_schedule_eval(sch->schid);
144         return 0;
145 }
146
147 void chsc_chp_offline(struct chp_id chpid)
148 {
149         char dbf_txt[15];
150         struct chp_link link;
151
152         sprintf(dbf_txt, "chpr%x.%02x", chpid.cssid, chpid.id);
153         CIO_TRACE_EVENT(2, dbf_txt);
154
155         if (chp_get_status(chpid) <= 0)
156                 return;
157         memset(&link, 0, sizeof(struct chp_link));
158         link.chpid = chpid;
159         /* Wait until previous actions have settled. */
160         css_wait_for_slow_path();
161         for_each_subchannel_staged(s390_subchannel_remove_chpid, NULL, &link);
162 }
163
164 static int s390_process_res_acc_new_sch(struct subchannel_id schid, void *data)
165 {
166         struct schib schib;
167         /*
168          * We don't know the device yet, but since a path
169          * may be available now to the device we'll have
170          * to do recognition again.
171          * Since we don't have any idea about which chpid
172          * that beast may be on we'll have to do a stsch
173          * on all devices, grr...
174          */
175         if (stsch_err(schid, &schib))
176                 /* We're through */
177                 return -ENXIO;
178
179         /* Put it on the slow path. */
180         css_schedule_eval(schid);
181         return 0;
182 }
183
184 static int __s390_process_res_acc(struct subchannel *sch, void *data)
185 {
186         spin_lock_irq(sch->lock);
187         if (sch->driver && sch->driver->chp_event)
188                 sch->driver->chp_event(sch, data, CHP_ONLINE);
189         spin_unlock_irq(sch->lock);
190
191         return 0;
192 }
193
194 static void s390_process_res_acc(struct chp_link *link)
195 {
196         char dbf_txt[15];
197
198         sprintf(dbf_txt, "accpr%x.%02x", link->chpid.cssid,
199                 link->chpid.id);
200         CIO_TRACE_EVENT( 2, dbf_txt);
201         if (link->fla != 0) {
202                 sprintf(dbf_txt, "fla%x", link->fla);
203                 CIO_TRACE_EVENT( 2, dbf_txt);
204         }
205         /* Wait until previous actions have settled. */
206         css_wait_for_slow_path();
207         /*
208          * I/O resources may have become accessible.
209          * Scan through all subchannels that may be concerned and
210          * do a validation on those.
211          * The more information we have (info), the less scanning
212          * will we have to do.
213          */
214         for_each_subchannel_staged(__s390_process_res_acc,
215                                    s390_process_res_acc_new_sch, link);
216 }
217
218 static int
219 __get_chpid_from_lir(void *data)
220 {
221         struct lir {
222                 u8  iq;
223                 u8  ic;
224                 u16 sci;
225                 /* incident-node descriptor */
226                 u32 indesc[28];
227                 /* attached-node descriptor */
228                 u32 andesc[28];
229                 /* incident-specific information */
230                 u32 isinfo[28];
231         } __attribute__ ((packed)) *lir;
232
233         lir = data;
234         if (!(lir->iq&0x80))
235                 /* NULL link incident record */
236                 return -EINVAL;
237         if (!(lir->indesc[0]&0xc0000000))
238                 /* node descriptor not valid */
239                 return -EINVAL;
240         if (!(lir->indesc[0]&0x10000000))
241                 /* don't handle device-type nodes - FIXME */
242                 return -EINVAL;
243         /* Byte 3 contains the chpid. Could also be CTCA, but we don't care */
244
245         return (u16) (lir->indesc[0]&0x000000ff);
246 }
247
248 struct chsc_sei_area {
249         struct chsc_header request;
250         u32 reserved1;
251         u32 reserved2;
252         u32 reserved3;
253         struct chsc_header response;
254         u32 reserved4;
255         u8  flags;
256         u8  vf;         /* validity flags */
257         u8  rs;         /* reporting source */
258         u8  cc;         /* content code */
259         u16 fla;        /* full link address */
260         u16 rsid;       /* reporting source id */
261         u32 reserved5;
262         u32 reserved6;
263         u8 ccdf[4096 - 16 - 24];        /* content-code dependent field */
264         /* ccdf has to be big enough for a link-incident record */
265 } __attribute__ ((packed));
266
267 static void chsc_process_sei_link_incident(struct chsc_sei_area *sei_area)
268 {
269         struct chp_id chpid;
270         int id;
271
272         CIO_CRW_EVENT(4, "chsc: link incident (rs=%02x, rs_id=%04x)\n",
273                       sei_area->rs, sei_area->rsid);
274         if (sei_area->rs != 4)
275                 return;
276         id = __get_chpid_from_lir(sei_area->ccdf);
277         if (id < 0)
278                 CIO_CRW_EVENT(4, "chsc: link incident - invalid LIR\n");
279         else {
280                 chp_id_init(&chpid);
281                 chpid.id = id;
282                 chsc_chp_offline(chpid);
283         }
284 }
285
286 static void chsc_process_sei_res_acc(struct chsc_sei_area *sei_area)
287 {
288         struct chp_link link;
289         struct chp_id chpid;
290         int status;
291
292         CIO_CRW_EVENT(4, "chsc: resource accessibility event (rs=%02x, "
293                       "rs_id=%04x)\n", sei_area->rs, sei_area->rsid);
294         if (sei_area->rs != 4)
295                 return;
296         chp_id_init(&chpid);
297         chpid.id = sei_area->rsid;
298         /* allocate a new channel path structure, if needed */
299         status = chp_get_status(chpid);
300         if (status < 0)
301                 chp_new(chpid);
302         else if (!status)
303                 return;
304         memset(&link, 0, sizeof(struct chp_link));
305         link.chpid = chpid;
306         if ((sei_area->vf & 0xc0) != 0) {
307                 link.fla = sei_area->fla;
308                 if ((sei_area->vf & 0xc0) == 0xc0)
309                         /* full link address */
310                         link.fla_mask = 0xffff;
311                 else
312                         /* link address */
313                         link.fla_mask = 0xff00;
314         }
315         s390_process_res_acc(&link);
316 }
317
318 struct chp_config_data {
319         u8 map[32];
320         u8 op;
321         u8 pc;
322 };
323
324 static void chsc_process_sei_chp_config(struct chsc_sei_area *sei_area)
325 {
326         struct chp_config_data *data;
327         struct chp_id chpid;
328         int num;
329
330         CIO_CRW_EVENT(4, "chsc: channel-path-configuration notification\n");
331         if (sei_area->rs != 0)
332                 return;
333         data = (struct chp_config_data *) &(sei_area->ccdf);
334         chp_id_init(&chpid);
335         for (num = 0; num <= __MAX_CHPID; num++) {
336                 if (!chp_test_bit(data->map, num))
337                         continue;
338                 chpid.id = num;
339                 printk(KERN_WARNING "cio: processing configure event %d for "
340                        "chpid %x.%02x\n", data->op, chpid.cssid, chpid.id);
341                 switch (data->op) {
342                 case 0:
343                         chp_cfg_schedule(chpid, 1);
344                         break;
345                 case 1:
346                         chp_cfg_schedule(chpid, 0);
347                         break;
348                 case 2:
349                         chp_cfg_cancel_deconfigure(chpid);
350                         break;
351                 }
352         }
353 }
354
355 static void chsc_process_sei(struct chsc_sei_area *sei_area)
356 {
357         /* Check if we might have lost some information. */
358         if (sei_area->flags & 0x40) {
359                 CIO_CRW_EVENT(2, "chsc: event overflow\n");
360                 css_schedule_eval_all();
361         }
362         /* which kind of information was stored? */
363         switch (sei_area->cc) {
364         case 1: /* link incident*/
365                 chsc_process_sei_link_incident(sei_area);
366                 break;
367         case 2: /* i/o resource accessibiliy */
368                 chsc_process_sei_res_acc(sei_area);
369                 break;
370         case 8: /* channel-path-configuration notification */
371                 chsc_process_sei_chp_config(sei_area);
372                 break;
373         default: /* other stuff */
374                 CIO_CRW_EVENT(4, "chsc: unhandled sei content code %d\n",
375                               sei_area->cc);
376                 break;
377         }
378 }
379
380 static void chsc_process_crw(struct crw *crw0, struct crw *crw1, int overflow)
381 {
382         struct chsc_sei_area *sei_area;
383
384         if (overflow) {
385                 css_schedule_eval_all();
386                 return;
387         }
388         CIO_CRW_EVENT(2, "CRW reports slct=%d, oflw=%d, "
389                       "chn=%d, rsc=%X, anc=%d, erc=%X, rsid=%X\n",
390                       crw0->slct, crw0->oflw, crw0->chn, crw0->rsc, crw0->anc,
391                       crw0->erc, crw0->rsid);
392         if (!sei_page)
393                 return;
394         /* Access to sei_page is serialized through machine check handler
395          * thread, so no need for locking. */
396         sei_area = sei_page;
397
398         CIO_TRACE_EVENT(2, "prcss");
399         do {
400                 memset(sei_area, 0, sizeof(*sei_area));
401                 sei_area->request.length = 0x0010;
402                 sei_area->request.code = 0x000e;
403                 if (chsc(sei_area))
404                         break;
405
406                 if (sei_area->response.code == 0x0001) {
407                         CIO_CRW_EVENT(4, "chsc: sei successful\n");
408                         chsc_process_sei(sei_area);
409                 } else {
410                         CIO_CRW_EVENT(2, "chsc: sei failed (rc=%04x)\n",
411                                       sei_area->response.code);
412                         break;
413                 }
414         } while (sei_area->flags & 0x80);
415 }
416
417 void chsc_chp_online(struct chp_id chpid)
418 {
419         char dbf_txt[15];
420         struct chp_link link;
421
422         sprintf(dbf_txt, "cadd%x.%02x", chpid.cssid, chpid.id);
423         CIO_TRACE_EVENT(2, dbf_txt);
424
425         if (chp_get_status(chpid) != 0) {
426                 memset(&link, 0, sizeof(struct chp_link));
427                 link.chpid = chpid;
428                 /* Wait until previous actions have settled. */
429                 css_wait_for_slow_path();
430                 for_each_subchannel_staged(__s390_process_res_acc, NULL,
431                                            &link);
432         }
433 }
434
435 static void __s390_subchannel_vary_chpid(struct subchannel *sch,
436                                          struct chp_id chpid, int on)
437 {
438         unsigned long flags;
439         struct chp_link link;
440
441         memset(&link, 0, sizeof(struct chp_link));
442         link.chpid = chpid;
443         spin_lock_irqsave(sch->lock, flags);
444         if (sch->driver && sch->driver->chp_event)
445                 sch->driver->chp_event(sch, &link,
446                                        on ? CHP_VARY_ON : CHP_VARY_OFF);
447         spin_unlock_irqrestore(sch->lock, flags);
448 }
449
450 static int s390_subchannel_vary_chpid_off(struct subchannel *sch, void *data)
451 {
452         struct chp_id *chpid = data;
453
454         __s390_subchannel_vary_chpid(sch, *chpid, 0);
455         return 0;
456 }
457
458 static int s390_subchannel_vary_chpid_on(struct subchannel *sch, void *data)
459 {
460         struct chp_id *chpid = data;
461
462         __s390_subchannel_vary_chpid(sch, *chpid, 1);
463         return 0;
464 }
465
466 static int
467 __s390_vary_chpid_on(struct subchannel_id schid, void *data)
468 {
469         struct schib schib;
470
471         if (stsch_err(schid, &schib))
472                 /* We're through */
473                 return -ENXIO;
474         /* Put it on the slow path. */
475         css_schedule_eval(schid);
476         return 0;
477 }
478
479 /**
480  * chsc_chp_vary - propagate channel-path vary operation to subchannels
481  * @chpid: channl-path ID
482  * @on: non-zero for vary online, zero for vary offline
483  */
484 int chsc_chp_vary(struct chp_id chpid, int on)
485 {
486         struct chp_link link;
487
488         memset(&link, 0, sizeof(struct chp_link));
489         link.chpid = chpid;
490         /* Wait until previous actions have settled. */
491         css_wait_for_slow_path();
492         /*
493          * Redo PathVerification on the devices the chpid connects to
494          */
495
496         if (on)
497                 for_each_subchannel_staged(s390_subchannel_vary_chpid_on,
498                                            __s390_vary_chpid_on, &link);
499         else
500                 for_each_subchannel_staged(s390_subchannel_vary_chpid_off,
501                                            NULL, &link);
502
503         return 0;
504 }
505
506 static void
507 chsc_remove_cmg_attr(struct channel_subsystem *css)
508 {
509         int i;
510
511         for (i = 0; i <= __MAX_CHPID; i++) {
512                 if (!css->chps[i])
513                         continue;
514                 chp_remove_cmg_attr(css->chps[i]);
515         }
516 }
517
518 static int
519 chsc_add_cmg_attr(struct channel_subsystem *css)
520 {
521         int i, ret;
522
523         ret = 0;
524         for (i = 0; i <= __MAX_CHPID; i++) {
525                 if (!css->chps[i])
526                         continue;
527                 ret = chp_add_cmg_attr(css->chps[i]);
528                 if (ret)
529                         goto cleanup;
530         }
531         return ret;
532 cleanup:
533         for (--i; i >= 0; i--) {
534                 if (!css->chps[i])
535                         continue;
536                 chp_remove_cmg_attr(css->chps[i]);
537         }
538         return ret;
539 }
540
541 static int
542 __chsc_do_secm(struct channel_subsystem *css, int enable, void *page)
543 {
544         struct {
545                 struct chsc_header request;
546                 u32 operation_code : 2;
547                 u32 : 30;
548                 u32 key : 4;
549                 u32 : 28;
550                 u32 zeroes1;
551                 u32 cub_addr1;
552                 u32 zeroes2;
553                 u32 cub_addr2;
554                 u32 reserved[13];
555                 struct chsc_header response;
556                 u32 status : 8;
557                 u32 : 4;
558                 u32 fmt : 4;
559                 u32 : 16;
560         } __attribute__ ((packed)) *secm_area;
561         int ret, ccode;
562
563         secm_area = page;
564         secm_area->request.length = 0x0050;
565         secm_area->request.code = 0x0016;
566
567         secm_area->key = PAGE_DEFAULT_KEY;
568         secm_area->cub_addr1 = (u64)(unsigned long)css->cub_addr1;
569         secm_area->cub_addr2 = (u64)(unsigned long)css->cub_addr2;
570
571         secm_area->operation_code = enable ? 0 : 1;
572
573         ccode = chsc(secm_area);
574         if (ccode > 0)
575                 return (ccode == 3) ? -ENODEV : -EBUSY;
576
577         switch (secm_area->response.code) {
578         case 0x0102:
579         case 0x0103:
580                 ret = -EINVAL;
581         default:
582                 ret = chsc_error_from_response(secm_area->response.code);
583         }
584         if (ret != 0)
585                 CIO_CRW_EVENT(2, "chsc: secm failed (rc=%04x)\n",
586                               secm_area->response.code);
587         return ret;
588 }
589
590 int
591 chsc_secm(struct channel_subsystem *css, int enable)
592 {
593         void  *secm_area;
594         int ret;
595
596         secm_area = (void *)get_zeroed_page(GFP_KERNEL |  GFP_DMA);
597         if (!secm_area)
598                 return -ENOMEM;
599
600         if (enable && !css->cm_enabled) {
601                 css->cub_addr1 = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
602                 css->cub_addr2 = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
603                 if (!css->cub_addr1 || !css->cub_addr2) {
604                         free_page((unsigned long)css->cub_addr1);
605                         free_page((unsigned long)css->cub_addr2);
606                         free_page((unsigned long)secm_area);
607                         return -ENOMEM;
608                 }
609         }
610         ret = __chsc_do_secm(css, enable, secm_area);
611         if (!ret) {
612                 css->cm_enabled = enable;
613                 if (css->cm_enabled) {
614                         ret = chsc_add_cmg_attr(css);
615                         if (ret) {
616                                 memset(secm_area, 0, PAGE_SIZE);
617                                 __chsc_do_secm(css, 0, secm_area);
618                                 css->cm_enabled = 0;
619                         }
620                 } else
621                         chsc_remove_cmg_attr(css);
622         }
623         if (!css->cm_enabled) {
624                 free_page((unsigned long)css->cub_addr1);
625                 free_page((unsigned long)css->cub_addr2);
626         }
627         free_page((unsigned long)secm_area);
628         return ret;
629 }
630
631 int chsc_determine_channel_path_desc(struct chp_id chpid, int fmt, int rfmt,
632                                      int c, int m,
633                                      struct chsc_response_struct *resp)
634 {
635         int ccode, ret;
636
637         struct {
638                 struct chsc_header request;
639                 u32 : 2;
640                 u32 m : 1;
641                 u32 c : 1;
642                 u32 fmt : 4;
643                 u32 cssid : 8;
644                 u32 : 4;
645                 u32 rfmt : 4;
646                 u32 first_chpid : 8;
647                 u32 : 24;
648                 u32 last_chpid : 8;
649                 u32 zeroes1;
650                 struct chsc_header response;
651                 u8 data[PAGE_SIZE - 20];
652         } __attribute__ ((packed)) *scpd_area;
653
654         if ((rfmt == 1) && !css_general_characteristics.fcs)
655                 return -EINVAL;
656         if ((rfmt == 2) && !css_general_characteristics.cib)
657                 return -EINVAL;
658         scpd_area = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
659         if (!scpd_area)
660                 return -ENOMEM;
661
662         scpd_area->request.length = 0x0010;
663         scpd_area->request.code = 0x0002;
664
665         scpd_area->cssid = chpid.cssid;
666         scpd_area->first_chpid = chpid.id;
667         scpd_area->last_chpid = chpid.id;
668         scpd_area->m = m;
669         scpd_area->c = c;
670         scpd_area->fmt = fmt;
671         scpd_area->rfmt = rfmt;
672
673         ccode = chsc(scpd_area);
674         if (ccode > 0) {
675                 ret = (ccode == 3) ? -ENODEV : -EBUSY;
676                 goto out;
677         }
678
679         ret = chsc_error_from_response(scpd_area->response.code);
680         if (ret == 0)
681                 /* Success. */
682                 memcpy(resp, &scpd_area->response, scpd_area->response.length);
683         else
684                 CIO_CRW_EVENT(2, "chsc: scpd failed (rc=%04x)\n",
685                               scpd_area->response.code);
686 out:
687         free_page((unsigned long)scpd_area);
688         return ret;
689 }
690 EXPORT_SYMBOL_GPL(chsc_determine_channel_path_desc);
691
692 int chsc_determine_base_channel_path_desc(struct chp_id chpid,
693                                           struct channel_path_desc *desc)
694 {
695         struct chsc_response_struct *chsc_resp;
696         int ret;
697
698         chsc_resp = kzalloc(sizeof(*chsc_resp), GFP_KERNEL);
699         if (!chsc_resp)
700                 return -ENOMEM;
701         ret = chsc_determine_channel_path_desc(chpid, 0, 0, 0, 0, chsc_resp);
702         if (ret)
703                 goto out_free;
704         memcpy(desc, &chsc_resp->data, chsc_resp->length);
705 out_free:
706         kfree(chsc_resp);
707         return ret;
708 }
709
710 static void
711 chsc_initialize_cmg_chars(struct channel_path *chp, u8 cmcv,
712                           struct cmg_chars *chars)
713 {
714         switch (chp->cmg) {
715         case 2:
716         case 3:
717                 chp->cmg_chars = kmalloc(sizeof(struct cmg_chars),
718                                          GFP_KERNEL);
719                 if (chp->cmg_chars) {
720                         int i, mask;
721                         struct cmg_chars *cmg_chars;
722
723                         cmg_chars = chp->cmg_chars;
724                         for (i = 0; i < NR_MEASUREMENT_CHARS; i++) {
725                                 mask = 0x80 >> (i + 3);
726                                 if (cmcv & mask)
727                                         cmg_chars->values[i] = chars->values[i];
728                                 else
729                                         cmg_chars->values[i] = 0;
730                         }
731                 }
732                 break;
733         default:
734                 /* No cmg-dependent data. */
735                 break;
736         }
737 }
738
739 int chsc_get_channel_measurement_chars(struct channel_path *chp)
740 {
741         int ccode, ret;
742
743         struct {
744                 struct chsc_header request;
745                 u32 : 24;
746                 u32 first_chpid : 8;
747                 u32 : 24;
748                 u32 last_chpid : 8;
749                 u32 zeroes1;
750                 struct chsc_header response;
751                 u32 zeroes2;
752                 u32 not_valid : 1;
753                 u32 shared : 1;
754                 u32 : 22;
755                 u32 chpid : 8;
756                 u32 cmcv : 5;
757                 u32 : 11;
758                 u32 cmgq : 8;
759                 u32 cmg : 8;
760                 u32 zeroes3;
761                 u32 data[NR_MEASUREMENT_CHARS];
762         } __attribute__ ((packed)) *scmc_area;
763
764         scmc_area = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
765         if (!scmc_area)
766                 return -ENOMEM;
767
768         scmc_area->request.length = 0x0010;
769         scmc_area->request.code = 0x0022;
770
771         scmc_area->first_chpid = chp->chpid.id;
772         scmc_area->last_chpid = chp->chpid.id;
773
774         ccode = chsc(scmc_area);
775         if (ccode > 0) {
776                 ret = (ccode == 3) ? -ENODEV : -EBUSY;
777                 goto out;
778         }
779
780         ret = chsc_error_from_response(scmc_area->response.code);
781         if (ret == 0) {
782                 /* Success. */
783                 if (!scmc_area->not_valid) {
784                         chp->cmg = scmc_area->cmg;
785                         chp->shared = scmc_area->shared;
786                         chsc_initialize_cmg_chars(chp, scmc_area->cmcv,
787                                                   (struct cmg_chars *)
788                                                   &scmc_area->data);
789                 } else {
790                         chp->cmg = -1;
791                         chp->shared = -1;
792                 }
793         } else {
794                 CIO_CRW_EVENT(2, "chsc: scmc failed (rc=%04x)\n",
795                               scmc_area->response.code);
796         }
797 out:
798         free_page((unsigned long)scmc_area);
799         return ret;
800 }
801
802 int __init chsc_alloc_sei_area(void)
803 {
804         int ret;
805
806         sei_page = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
807         if (!sei_page) {
808                 CIO_MSG_EVENT(0, "Can't allocate page for processing of "
809                               "chsc machine checks!\n");
810                 return -ENOMEM;
811         }
812         ret = s390_register_crw_handler(CRW_RSC_CSS, chsc_process_crw);
813         if (ret)
814                 kfree(sei_page);
815         return ret;
816 }
817
818 void __init chsc_free_sei_area(void)
819 {
820         s390_unregister_crw_handler(CRW_RSC_CSS);
821         kfree(sei_page);
822 }
823
824 int __init
825 chsc_enable_facility(int operation_code)
826 {
827         int ret;
828         struct {
829                 struct chsc_header request;
830                 u8 reserved1:4;
831                 u8 format:4;
832                 u8 reserved2;
833                 u16 operation_code;
834                 u32 reserved3;
835                 u32 reserved4;
836                 u32 operation_data_area[252];
837                 struct chsc_header response;
838                 u32 reserved5:4;
839                 u32 format2:4;
840                 u32 reserved6:24;
841         } __attribute__ ((packed)) *sda_area;
842
843         sda_area = (void *)get_zeroed_page(GFP_KERNEL|GFP_DMA);
844         if (!sda_area)
845                 return -ENOMEM;
846         sda_area->request.length = 0x0400;
847         sda_area->request.code = 0x0031;
848         sda_area->operation_code = operation_code;
849
850         ret = chsc(sda_area);
851         if (ret > 0) {
852                 ret = (ret == 3) ? -ENODEV : -EBUSY;
853                 goto out;
854         }
855
856         switch (sda_area->response.code) {
857         case 0x0101:
858                 ret = -EOPNOTSUPP;
859                 break;
860         default:
861                 ret = chsc_error_from_response(sda_area->response.code);
862         }
863         if (ret != 0)
864                 CIO_CRW_EVENT(2, "chsc: sda (oc=%x) failed (rc=%04x)\n",
865                               operation_code, sda_area->response.code);
866  out:
867         free_page((unsigned long)sda_area);
868         return ret;
869 }
870
871 struct css_general_char css_general_characteristics;
872 struct css_chsc_char css_chsc_characteristics;
873
874 int __init
875 chsc_determine_css_characteristics(void)
876 {
877         int result;
878         struct {
879                 struct chsc_header request;
880                 u32 reserved1;
881                 u32 reserved2;
882                 u32 reserved3;
883                 struct chsc_header response;
884                 u32 reserved4;
885                 u32 general_char[510];
886                 u32 chsc_char[518];
887         } __attribute__ ((packed)) *scsc_area;
888
889         scsc_area = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
890         if (!scsc_area)
891                 return -ENOMEM;
892
893         scsc_area->request.length = 0x0010;
894         scsc_area->request.code = 0x0010;
895
896         result = chsc(scsc_area);
897         if (result) {
898                 result = (result == 3) ? -ENODEV : -EBUSY;
899                 goto exit;
900         }
901
902         result = chsc_error_from_response(scsc_area->response.code);
903         if (result == 0) {
904                 memcpy(&css_general_characteristics, scsc_area->general_char,
905                        sizeof(css_general_characteristics));
906                 memcpy(&css_chsc_characteristics, scsc_area->chsc_char,
907                        sizeof(css_chsc_characteristics));
908         } else
909                 CIO_CRW_EVENT(2, "chsc: scsc failed (rc=%04x)\n",
910                               scsc_area->response.code);
911 exit:
912         free_page ((unsigned long) scsc_area);
913         return result;
914 }
915
916 EXPORT_SYMBOL_GPL(css_general_characteristics);
917 EXPORT_SYMBOL_GPL(css_chsc_characteristics);
918
919 int chsc_sstpc(void *page, unsigned int op, u16 ctrl)
920 {
921         struct {
922                 struct chsc_header request;
923                 unsigned int rsvd0;
924                 unsigned int op : 8;
925                 unsigned int rsvd1 : 8;
926                 unsigned int ctrl : 16;
927                 unsigned int rsvd2[5];
928                 struct chsc_header response;
929                 unsigned int rsvd3[7];
930         } __attribute__ ((packed)) *rr;
931         int rc;
932
933         memset(page, 0, PAGE_SIZE);
934         rr = page;
935         rr->request.length = 0x0020;
936         rr->request.code = 0x0033;
937         rr->op = op;
938         rr->ctrl = ctrl;
939         rc = chsc(rr);
940         if (rc)
941                 return -EIO;
942         rc = (rr->response.code == 0x0001) ? 0 : -EIO;
943         return rc;
944 }
945
946 int chsc_sstpi(void *page, void *result, size_t size)
947 {
948         struct {
949                 struct chsc_header request;
950                 unsigned int rsvd0[3];
951                 struct chsc_header response;
952                 char data[size];
953         } __attribute__ ((packed)) *rr;
954         int rc;
955
956         memset(page, 0, PAGE_SIZE);
957         rr = page;
958         rr->request.length = 0x0010;
959         rr->request.code = 0x0038;
960         rc = chsc(rr);
961         if (rc)
962                 return -EIO;
963         memcpy(result, &rr->data, size);
964         return (rr->response.code == 0x0001) ? 0 : -EIO;
965 }
966