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