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