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