Merge git://git.kernel.org/pub/scm/linux/kernel/git/sfrench/cifs-2.6
[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
19 #include "css.h"
20 #include "cio.h"
21 #include "cio_debug.h"
22 #include "ioasm.h"
23 #include "chsc.h"
24
25 static void *sei_page;
26
27 static int new_channel_path(int chpid);
28
29 static inline void
30 set_chp_logically_online(int chp, int onoff)
31 {
32         css[0]->chps[chp]->state = onoff;
33 }
34
35 static int
36 get_chp_status(int chp)
37 {
38         return (css[0]->chps[chp] ? css[0]->chps[chp]->state : -ENODEV);
39 }
40
41 void
42 chsc_validate_chpids(struct subchannel *sch)
43 {
44         int mask, chp;
45
46         for (chp = 0; chp <= 7; chp++) {
47                 mask = 0x80 >> chp;
48                 if (!get_chp_status(sch->schib.pmcw.chpid[chp]))
49                         /* disable using this path */
50                         sch->opm &= ~mask;
51         }
52 }
53
54 void
55 chpid_is_actually_online(int chp)
56 {
57         int state;
58
59         state = get_chp_status(chp);
60         if (state < 0) {
61                 need_rescan = 1;
62                 queue_work(slow_path_wq, &slow_path_work);
63         } else
64                 WARN_ON(!state);
65 }
66
67 /* FIXME: this is _always_ called for every subchannel. shouldn't we
68  *        process more than one at a time? */
69 static int
70 chsc_get_sch_desc_irq(struct subchannel *sch, void *page)
71 {
72         int ccode, j;
73
74         struct {
75                 struct chsc_header request;
76                 u16 reserved1a:10;
77                 u16 ssid:2;
78                 u16 reserved1b:4;
79                 u16 f_sch;        /* first subchannel */
80                 u16 reserved2;
81                 u16 l_sch;        /* last subchannel */
82                 u32 reserved3;
83                 struct chsc_header response;
84                 u32 reserved4;
85                 u8 sch_valid : 1;
86                 u8 dev_valid : 1;
87                 u8 st        : 3; /* subchannel type */
88                 u8 zeroes    : 3;
89                 u8  unit_addr;    /* unit address */
90                 u16 devno;        /* device number */
91                 u8 path_mask;
92                 u8 fla_valid_mask;
93                 u16 sch;          /* subchannel */
94                 u8 chpid[8];      /* chpids 0-7 */
95                 u16 fla[8];       /* full link addresses 0-7 */
96         } *ssd_area;
97
98         ssd_area = page;
99
100         ssd_area->request.length = 0x0010;
101         ssd_area->request.code = 0x0004;
102
103         ssd_area->ssid = sch->schid.ssid;
104         ssd_area->f_sch = sch->schid.sch_no;
105         ssd_area->l_sch = sch->schid.sch_no;
106
107         ccode = chsc(ssd_area);
108         if (ccode > 0) {
109                 pr_debug("chsc returned with ccode = %d\n", ccode);
110                 return (ccode == 3) ? -ENODEV : -EBUSY;
111         }
112
113         switch (ssd_area->response.code) {
114         case 0x0001: /* everything ok */
115                 break;
116         case 0x0002:
117                 CIO_CRW_EVENT(2, "Invalid command!\n");
118                 return -EINVAL;
119         case 0x0003:
120                 CIO_CRW_EVENT(2, "Error in chsc request block!\n");
121                 return -EINVAL;
122         case 0x0004:
123                 CIO_CRW_EVENT(2, "Model does not provide ssd\n");
124                 return -EOPNOTSUPP;
125         default:
126                 CIO_CRW_EVENT(2, "Unknown CHSC response %d\n",
127                               ssd_area->response.code);
128                 return -EIO;
129         }
130
131         /*
132          * ssd_area->st stores the type of the detected
133          * subchannel, with the following definitions:
134          *
135          * 0: I/O subchannel:     All fields have meaning
136          * 1: CHSC subchannel:    Only sch_val, st and sch
137          *                        have meaning
138          * 2: Message subchannel: All fields except unit_addr
139          *                        have meaning
140          * 3: ADM subchannel:     Only sch_val, st and sch
141          *                        have meaning
142          *
143          * Other types are currently undefined.
144          */
145         if (ssd_area->st > 3) { /* uhm, that looks strange... */
146                 CIO_CRW_EVENT(0, "Strange subchannel type %d"
147                               " for sch 0.%x.%04x\n", ssd_area->st,
148                               sch->schid.ssid, sch->schid.sch_no);
149                 /*
150                  * There may have been a new subchannel type defined in the
151                  * time since this code was written; since we don't know which
152                  * fields have meaning and what to do with it we just jump out
153                  */
154                 return 0;
155         } else {
156                 const char *type[4] = {"I/O", "chsc", "message", "ADM"};
157                 CIO_CRW_EVENT(6, "ssd: sch 0.%x.%04x is %s subchannel\n",
158                               sch->schid.ssid, sch->schid.sch_no,
159                               type[ssd_area->st]);
160
161                 sch->ssd_info.valid = 1;
162                 sch->ssd_info.type = ssd_area->st;
163         }
164
165         if (ssd_area->st == 0 || ssd_area->st == 2) {
166                 for (j = 0; j < 8; j++) {
167                         if (!((0x80 >> j) & ssd_area->path_mask &
168                               ssd_area->fla_valid_mask))
169                                 continue;
170                         sch->ssd_info.chpid[j] = ssd_area->chpid[j];
171                         sch->ssd_info.fla[j]   = ssd_area->fla[j];
172                 }
173         }
174         return 0;
175 }
176
177 int
178 css_get_ssd_info(struct subchannel *sch)
179 {
180         int ret;
181         void *page;
182
183         page = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
184         if (!page)
185                 return -ENOMEM;
186         spin_lock_irq(&sch->lock);
187         ret = chsc_get_sch_desc_irq(sch, page);
188         if (ret) {
189                 static int cio_chsc_err_msg;
190                 
191                 if (!cio_chsc_err_msg) {
192                         printk(KERN_ERR
193                                "chsc_get_sch_descriptions:"
194                                " Error %d while doing chsc; "
195                                "processing some machine checks may "
196                                "not work\n", ret);
197                         cio_chsc_err_msg = 1;
198                 }
199         }
200         spin_unlock_irq(&sch->lock);
201         free_page((unsigned long)page);
202         if (!ret) {
203                 int j, chpid, mask;
204                 /* Allocate channel path structures, if needed. */
205                 for (j = 0; j < 8; j++) {
206                         mask = 0x80 >> j;
207                         chpid = sch->ssd_info.chpid[j];
208                         if ((sch->schib.pmcw.pim & mask) &&
209                             (get_chp_status(chpid) < 0))
210                             new_channel_path(chpid);
211                 }
212         }
213         return ret;
214 }
215
216 static int
217 s390_subchannel_remove_chpid(struct device *dev, void *data)
218 {
219         int j;
220         int mask;
221         struct subchannel *sch;
222         struct channel_path *chpid;
223         struct schib schib;
224
225         sch = to_subchannel(dev);
226         chpid = data;
227         for (j = 0; j < 8; j++) {
228                 mask = 0x80 >> j;
229                 if ((sch->schib.pmcw.pim & mask) &&
230                     (sch->schib.pmcw.chpid[j] == chpid->id))
231                         break;
232         }
233         if (j >= 8)
234                 return 0;
235
236         spin_lock_irq(&sch->lock);
237
238         stsch(sch->schid, &schib);
239         if (!schib.pmcw.dnv)
240                 goto out_unreg;
241         memcpy(&sch->schib, &schib, sizeof(struct schib));
242         /* Check for single path devices. */
243         if (sch->schib.pmcw.pim == 0x80)
244                 goto out_unreg;
245
246         if ((sch->schib.scsw.actl & SCSW_ACTL_DEVACT) &&
247             (sch->schib.scsw.actl & SCSW_ACTL_SCHACT) &&
248             (sch->schib.pmcw.lpum == mask)) {
249                 int cc;
250
251                 cc = cio_clear(sch);
252                 if (cc == -ENODEV)
253                         goto out_unreg;
254                 /* Call handler. */
255                 if (sch->driver && sch->driver->termination)
256                         sch->driver->termination(&sch->dev);
257                 goto out_unlock;
258         }
259
260         /* trigger path verification. */
261         if (sch->driver && sch->driver->verify)
262                 sch->driver->verify(&sch->dev);
263         else if (sch->lpm == mask)
264                 goto out_unreg;
265 out_unlock:
266         spin_unlock_irq(&sch->lock);
267         return 0;
268 out_unreg:
269         spin_unlock_irq(&sch->lock);
270         sch->lpm = 0;
271         if (css_enqueue_subchannel_slow(sch->schid)) {
272                 css_clear_subchannel_slow_list();
273                 need_rescan = 1;
274         }
275         return 0;
276 }
277
278 static inline void
279 s390_set_chpid_offline( __u8 chpid)
280 {
281         char dbf_txt[15];
282         struct device *dev;
283
284         sprintf(dbf_txt, "chpr%x", chpid);
285         CIO_TRACE_EVENT(2, dbf_txt);
286
287         if (get_chp_status(chpid) <= 0)
288                 return;
289         dev = get_device(&css[0]->chps[chpid]->dev);
290         bus_for_each_dev(&css_bus_type, NULL, to_channelpath(dev),
291                          s390_subchannel_remove_chpid);
292
293         if (need_rescan || css_slow_subchannels_exist())
294                 queue_work(slow_path_wq, &slow_path_work);
295         put_device(dev);
296 }
297
298 struct res_acc_data {
299         struct channel_path *chp;
300         u32 fla_mask;
301         u16 fla;
302 };
303
304 static int
305 s390_process_res_acc_sch(struct res_acc_data *res_data, struct subchannel *sch)
306 {
307         int found;
308         int chp;
309         int ccode;
310         
311         found = 0;
312         for (chp = 0; chp <= 7; chp++)
313                 /*
314                  * check if chpid is in information updated by ssd
315                  */
316                 if (sch->ssd_info.valid &&
317                     sch->ssd_info.chpid[chp] == res_data->chp->id &&
318                     (sch->ssd_info.fla[chp] & res_data->fla_mask)
319                     == res_data->fla) {
320                         found = 1;
321                         break;
322                 }
323         
324         if (found == 0)
325                 return 0;
326
327         /*
328          * Do a stsch to update our subchannel structure with the
329          * new path information and eventually check for logically
330          * offline chpids.
331          */
332         ccode = stsch(sch->schid, &sch->schib);
333         if (ccode > 0)
334                 return 0;
335
336         return 0x80 >> chp;
337 }
338
339 static inline int
340 s390_process_res_acc_new_sch(struct subchannel_id schid)
341 {
342         struct schib schib;
343         int ret;
344         /*
345          * We don't know the device yet, but since a path
346          * may be available now to the device we'll have
347          * to do recognition again.
348          * Since we don't have any idea about which chpid
349          * that beast may be on we'll have to do a stsch
350          * on all devices, grr...
351          */
352         if (stsch_err(schid, &schib))
353                 /* We're through */
354                 return need_rescan ? -EAGAIN : -ENXIO;
355
356         /* Put it on the slow path. */
357         ret = css_enqueue_subchannel_slow(schid);
358         if (ret) {
359                 css_clear_subchannel_slow_list();
360                 need_rescan = 1;
361                 return -EAGAIN;
362         }
363         return 0;
364 }
365
366 static int
367 __s390_process_res_acc(struct subchannel_id schid, void *data)
368 {
369         int chp_mask, old_lpm;
370         struct res_acc_data *res_data;
371         struct subchannel *sch;
372
373         res_data = data;
374         sch = get_subchannel_by_schid(schid);
375         if (!sch)
376                 /* Check if a subchannel is newly available. */
377                 return s390_process_res_acc_new_sch(schid);
378
379         spin_lock_irq(&sch->lock);
380
381         chp_mask = s390_process_res_acc_sch(res_data, sch);
382
383         if (chp_mask == 0) {
384                 spin_unlock_irq(&sch->lock);
385                 put_device(&sch->dev);
386                 return 0;
387         }
388         old_lpm = sch->lpm;
389         sch->lpm = ((sch->schib.pmcw.pim &
390                      sch->schib.pmcw.pam &
391                      sch->schib.pmcw.pom)
392                     | chp_mask) & sch->opm;
393         if (!old_lpm && sch->lpm)
394                 device_trigger_reprobe(sch);
395         else if (sch->driver && sch->driver->verify)
396                 sch->driver->verify(&sch->dev);
397
398         spin_unlock_irq(&sch->lock);
399         put_device(&sch->dev);
400         return 0;
401 }
402
403
404 static int
405 s390_process_res_acc (struct res_acc_data *res_data)
406 {
407         int rc;
408         char dbf_txt[15];
409
410         sprintf(dbf_txt, "accpr%x", res_data->chp->id);
411         CIO_TRACE_EVENT( 2, dbf_txt);
412         if (res_data->fla != 0) {
413                 sprintf(dbf_txt, "fla%x", res_data->fla);
414                 CIO_TRACE_EVENT( 2, dbf_txt);
415         }
416
417         /*
418          * I/O resources may have become accessible.
419          * Scan through all subchannels that may be concerned and
420          * do a validation on those.
421          * The more information we have (info), the less scanning
422          * will we have to do.
423          */
424         rc = for_each_subchannel(__s390_process_res_acc, res_data);
425         if (css_slow_subchannels_exist())
426                 rc = -EAGAIN;
427         else if (rc != -EAGAIN)
428                 rc = 0;
429         return rc;
430 }
431
432 static int
433 __get_chpid_from_lir(void *data)
434 {
435         struct lir {
436                 u8  iq;
437                 u8  ic;
438                 u16 sci;
439                 /* incident-node descriptor */
440                 u32 indesc[28];
441                 /* attached-node descriptor */
442                 u32 andesc[28];
443                 /* incident-specific information */
444                 u32 isinfo[28];
445         } *lir;
446
447         lir = data;
448         if (!(lir->iq&0x80))
449                 /* NULL link incident record */
450                 return -EINVAL;
451         if (!(lir->indesc[0]&0xc0000000))
452                 /* node descriptor not valid */
453                 return -EINVAL;
454         if (!(lir->indesc[0]&0x10000000))
455                 /* don't handle device-type nodes - FIXME */
456                 return -EINVAL;
457         /* Byte 3 contains the chpid. Could also be CTCA, but we don't care */
458
459         return (u16) (lir->indesc[0]&0x000000ff);
460 }
461
462 int
463 chsc_process_crw(void)
464 {
465         int chpid, ret;
466         struct res_acc_data res_data;
467         struct {
468                 struct chsc_header request;
469                 u32 reserved1;
470                 u32 reserved2;
471                 u32 reserved3;
472                 struct chsc_header response;
473                 u32 reserved4;
474                 u8  flags;
475                 u8  vf;         /* validity flags */
476                 u8  rs;         /* reporting source */
477                 u8  cc;         /* content code */
478                 u16 fla;        /* full link address */
479                 u16 rsid;       /* reporting source id */
480                 u32 reserved5;
481                 u32 reserved6;
482                 u32 ccdf[96];   /* content-code dependent field */
483                 /* ccdf has to be big enough for a link-incident record */
484         } *sei_area;
485
486         if (!sei_page)
487                 return 0;
488         /*
489          * build the chsc request block for store event information
490          * and do the call
491          * This function is only called by the machine check handler thread,
492          * so we don't need locking for the sei_page.
493          */
494         sei_area = sei_page;
495
496         CIO_TRACE_EVENT( 2, "prcss");
497         ret = 0;
498         do {
499                 int ccode, status;
500                 struct device *dev;
501                 memset(sei_area, 0, sizeof(*sei_area));
502                 memset(&res_data, 0, sizeof(struct res_acc_data));
503                 sei_area->request.length = 0x0010;
504                 sei_area->request.code = 0x000e;
505
506                 ccode = chsc(sei_area);
507                 if (ccode > 0)
508                         return 0;
509
510                 switch (sei_area->response.code) {
511                         /* for debug purposes, check for problems */
512                 case 0x0001:
513                         CIO_CRW_EVENT(4, "chsc_process_crw: event information "
514                                         "successfully stored\n");
515                         break; /* everything ok */
516                 case 0x0002:
517                         CIO_CRW_EVENT(2,
518                                       "chsc_process_crw: invalid command!\n");
519                         return 0;
520                 case 0x0003:
521                         CIO_CRW_EVENT(2, "chsc_process_crw: error in chsc "
522                                       "request block!\n");
523                         return 0;
524                 case 0x0005:
525                         CIO_CRW_EVENT(2, "chsc_process_crw: no event "
526                                       "information stored\n");
527                         return 0;
528                 default:
529                         CIO_CRW_EVENT(2, "chsc_process_crw: chsc response %d\n",
530                                       sei_area->response.code);
531                         return 0;
532                 }
533
534                 /* Check if we might have lost some information. */
535                 if (sei_area->flags & 0x40)
536                         CIO_CRW_EVENT(2, "chsc_process_crw: Event information "
537                                        "has been lost due to overflow!\n");
538
539                 if (sei_area->rs != 4) {
540                         CIO_CRW_EVENT(2, "chsc_process_crw: reporting source "
541                                       "(%04X) isn't a chpid!\n",
542                                       sei_area->rsid);
543                         continue;
544                 }
545
546                 /* which kind of information was stored? */
547                 switch (sei_area->cc) {
548                 case 1: /* link incident*/
549                         CIO_CRW_EVENT(4, "chsc_process_crw: "
550                                       "channel subsystem reports link incident,"
551                                       " reporting source is chpid %x\n",
552                                       sei_area->rsid);
553                         chpid = __get_chpid_from_lir(sei_area->ccdf);
554                         if (chpid < 0)
555                                 CIO_CRW_EVENT(4, "%s: Invalid LIR, skipping\n",
556                                               __FUNCTION__);
557                         else
558                                 s390_set_chpid_offline(chpid);
559                         break;
560                         
561                 case 2: /* i/o resource accessibiliy */
562                         CIO_CRW_EVENT(4, "chsc_process_crw: "
563                                       "channel subsystem reports some I/O "
564                                       "devices may have become accessible\n");
565                         pr_debug("Data received after sei: \n");
566                         pr_debug("Validity flags: %x\n", sei_area->vf);
567                         
568                         /* allocate a new channel path structure, if needed */
569                         status = get_chp_status(sei_area->rsid);
570                         if (status < 0)
571                                 new_channel_path(sei_area->rsid);
572                         else if (!status)
573                                 break;
574                         dev = get_device(&css[0]->chps[sei_area->rsid]->dev);
575                         res_data.chp = to_channelpath(dev);
576                         pr_debug("chpid: %x", sei_area->rsid);
577                         if ((sei_area->vf & 0xc0) != 0) {
578                                 res_data.fla = sei_area->fla;
579                                 if ((sei_area->vf & 0xc0) == 0xc0) {
580                                         pr_debug(" full link addr: %x",
581                                                  sei_area->fla);
582                                         res_data.fla_mask = 0xffff;
583                                 } else {
584                                         pr_debug(" link addr: %x",
585                                                  sei_area->fla);
586                                         res_data.fla_mask = 0xff00;
587                                 }
588                         }
589                         ret = s390_process_res_acc(&res_data);
590                         pr_debug("\n\n");
591                         put_device(dev);
592                         break;
593                         
594                 default: /* other stuff */
595                         CIO_CRW_EVENT(4, "chsc_process_crw: event %d\n",
596                                       sei_area->cc);
597                         break;
598                 }
599         } while (sei_area->flags & 0x80);
600         return ret;
601 }
602
603 static inline int
604 __chp_add_new_sch(struct subchannel_id schid)
605 {
606         struct schib schib;
607         int ret;
608
609         if (stsch(schid, &schib))
610                 /* We're through */
611                 return need_rescan ? -EAGAIN : -ENXIO;
612
613         /* Put it on the slow path. */
614         ret = css_enqueue_subchannel_slow(schid);
615         if (ret) {
616                 css_clear_subchannel_slow_list();
617                 need_rescan = 1;
618                 return -EAGAIN;
619         }
620         return 0;
621 }
622
623
624 static int
625 __chp_add(struct subchannel_id schid, void *data)
626 {
627         int i, mask;
628         struct channel_path *chp;
629         struct subchannel *sch;
630
631         chp = data;
632         sch = get_subchannel_by_schid(schid);
633         if (!sch)
634                 /* Check if the subchannel is now available. */
635                 return __chp_add_new_sch(schid);
636         spin_lock_irq(&sch->lock);
637         for (i=0; i<8; i++) {
638                 mask = 0x80 >> i;
639                 if ((sch->schib.pmcw.pim & mask) &&
640                     (sch->schib.pmcw.chpid[i] == chp->id)) {
641                         if (stsch(sch->schid, &sch->schib) != 0) {
642                                 /* Endgame. */
643                                 spin_unlock_irq(&sch->lock);
644                                 return -ENXIO;
645                         }
646                         break;
647                 }
648         }
649         if (i==8) {
650                 spin_unlock_irq(&sch->lock);
651                 return 0;
652         }
653         sch->lpm = ((sch->schib.pmcw.pim &
654                      sch->schib.pmcw.pam &
655                      sch->schib.pmcw.pom)
656                     | mask) & sch->opm;
657
658         if (sch->driver && sch->driver->verify)
659                 sch->driver->verify(&sch->dev);
660
661         spin_unlock_irq(&sch->lock);
662         put_device(&sch->dev);
663         return 0;
664 }
665
666 static int
667 chp_add(int chpid)
668 {
669         int rc;
670         char dbf_txt[15];
671         struct device *dev;
672
673         if (!get_chp_status(chpid))
674                 return 0; /* no need to do the rest */
675         
676         sprintf(dbf_txt, "cadd%x", chpid);
677         CIO_TRACE_EVENT(2, dbf_txt);
678
679         dev = get_device(&css[0]->chps[chpid]->dev);
680         rc = for_each_subchannel(__chp_add, to_channelpath(dev));
681         if (css_slow_subchannels_exist())
682                 rc = -EAGAIN;
683         if (rc != -EAGAIN)
684                 rc = 0;
685         put_device(dev);
686         return rc;
687 }
688
689 /* 
690  * Handling of crw machine checks with channel path source.
691  */
692 int
693 chp_process_crw(int chpid, int on)
694 {
695         if (on == 0) {
696                 /* Path has gone. We use the link incident routine.*/
697                 s390_set_chpid_offline(chpid);
698                 return 0; /* De-register is async anyway. */
699         }
700         /*
701          * Path has come. Allocate a new channel path structure,
702          * if needed.
703          */
704         if (get_chp_status(chpid) < 0)
705                 new_channel_path(chpid);
706         /* Avoid the extra overhead in process_rec_acc. */
707         return chp_add(chpid);
708 }
709
710 static inline int check_for_io_on_path(struct subchannel *sch, int index)
711 {
712         int cc;
713
714         if (!device_is_online(sch))
715                 /* cio could be doing I/O. */
716                 return 0;
717         cc = stsch(sch->schid, &sch->schib);
718         if (cc)
719                 return 0;
720         if (sch->schib.scsw.actl && sch->schib.pmcw.lpum == (0x80 >> index))
721                 return 1;
722         return 0;
723 }
724
725 static inline void
726 __s390_subchannel_vary_chpid(struct subchannel *sch, __u8 chpid, int on)
727 {
728         int chp, old_lpm;
729         unsigned long flags;
730
731         if (!sch->ssd_info.valid)
732                 return;
733         
734         spin_lock_irqsave(&sch->lock, flags);
735         old_lpm = sch->lpm;
736         for (chp = 0; chp < 8; chp++) {
737                 if (sch->ssd_info.chpid[chp] != chpid)
738                         continue;
739
740                 if (on) {
741                         sch->opm |= (0x80 >> chp);
742                         sch->lpm |= (0x80 >> chp);
743                         if (!old_lpm)
744                                 device_trigger_reprobe(sch);
745                         else if (sch->driver && sch->driver->verify)
746                                 sch->driver->verify(&sch->dev);
747                 } else {
748                         sch->opm &= ~(0x80 >> chp);
749                         sch->lpm &= ~(0x80 >> chp);
750                         if (check_for_io_on_path(sch, chp))
751                                 /* Path verification is done after killing. */
752                                 device_kill_io(sch);
753                         else if (!sch->lpm) {
754                                 if (css_enqueue_subchannel_slow(sch->schid)) {
755                                         css_clear_subchannel_slow_list();
756                                         need_rescan = 1;
757                                 }
758                         } else if (sch->driver && sch->driver->verify)
759                                 sch->driver->verify(&sch->dev);
760                 }
761                 break;
762         }
763         spin_unlock_irqrestore(&sch->lock, flags);
764 }
765
766 static int
767 s390_subchannel_vary_chpid_off(struct device *dev, void *data)
768 {
769         struct subchannel *sch;
770         __u8 *chpid;
771
772         sch = to_subchannel(dev);
773         chpid = data;
774
775         __s390_subchannel_vary_chpid(sch, *chpid, 0);
776         return 0;
777 }
778
779 static int
780 s390_subchannel_vary_chpid_on(struct device *dev, void *data)
781 {
782         struct subchannel *sch;
783         __u8 *chpid;
784
785         sch = to_subchannel(dev);
786         chpid = data;
787
788         __s390_subchannel_vary_chpid(sch, *chpid, 1);
789         return 0;
790 }
791
792 static int
793 __s390_vary_chpid_on(struct subchannel_id schid, void *data)
794 {
795         struct schib schib;
796         struct subchannel *sch;
797
798         sch = get_subchannel_by_schid(schid);
799         if (sch) {
800                 put_device(&sch->dev);
801                 return 0;
802         }
803         if (stsch_err(schid, &schib))
804                 /* We're through */
805                 return -ENXIO;
806         /* Put it on the slow path. */
807         if (css_enqueue_subchannel_slow(schid)) {
808                 css_clear_subchannel_slow_list();
809                 need_rescan = 1;
810                 return -EAGAIN;
811         }
812         return 0;
813 }
814
815 /*
816  * Function: s390_vary_chpid
817  * Varies the specified chpid online or offline
818  */
819 static int
820 s390_vary_chpid( __u8 chpid, int on)
821 {
822         char dbf_text[15];
823         int status;
824
825         sprintf(dbf_text, on?"varyon%x":"varyoff%x", chpid);
826         CIO_TRACE_EVENT( 2, dbf_text);
827
828         status = get_chp_status(chpid);
829         if (status < 0) {
830                 printk(KERN_ERR "Can't vary unknown chpid %02X\n", chpid);
831                 return -EINVAL;
832         }
833
834         if (!on && !status) {
835                 printk(KERN_ERR "chpid %x is already offline\n", chpid);
836                 return -EINVAL;
837         }
838
839         set_chp_logically_online(chpid, on);
840
841         /*
842          * Redo PathVerification on the devices the chpid connects to
843          */
844
845         bus_for_each_dev(&css_bus_type, NULL, &chpid, on ?
846                          s390_subchannel_vary_chpid_on :
847                          s390_subchannel_vary_chpid_off);
848         if (on)
849                 /* Scan for new devices on varied on path. */
850                 for_each_subchannel(__s390_vary_chpid_on, NULL);
851         if (need_rescan || css_slow_subchannels_exist())
852                 queue_work(slow_path_wq, &slow_path_work);
853         return 0;
854 }
855
856 /*
857  * Channel measurement related functions
858  */
859 static ssize_t
860 chp_measurement_chars_read(struct kobject *kobj, char *buf, loff_t off,
861                            size_t count)
862 {
863         struct channel_path *chp;
864         unsigned int size;
865
866         chp = to_channelpath(container_of(kobj, struct device, kobj));
867         if (!chp->cmg_chars)
868                 return 0;
869
870         size = sizeof(struct cmg_chars);
871
872         if (off > size)
873                 return 0;
874         if (off + count > size)
875                 count = size - off;
876         memcpy(buf, chp->cmg_chars + off, count);
877         return count;
878 }
879
880 static struct bin_attribute chp_measurement_chars_attr = {
881         .attr = {
882                 .name = "measurement_chars",
883                 .mode = S_IRUSR,
884                 .owner = THIS_MODULE,
885         },
886         .size = sizeof(struct cmg_chars),
887         .read = chp_measurement_chars_read,
888 };
889
890 static void
891 chp_measurement_copy_block(struct cmg_entry *buf,
892                            struct channel_subsystem *css, int chpid)
893 {
894         void *area;
895         struct cmg_entry *entry, reference_buf;
896         int idx;
897
898         if (chpid < 128) {
899                 area = css->cub_addr1;
900                 idx = chpid;
901         } else {
902                 area = css->cub_addr2;
903                 idx = chpid - 128;
904         }
905         entry = area + (idx * sizeof(struct cmg_entry));
906         do {
907                 memcpy(buf, entry, sizeof(*entry));
908                 memcpy(&reference_buf, entry, sizeof(*entry));
909         } while (reference_buf.values[0] != buf->values[0]);
910 }
911
912 static ssize_t
913 chp_measurement_read(struct kobject *kobj, char *buf, loff_t off, size_t count)
914 {
915         struct channel_path *chp;
916         struct channel_subsystem *css;
917         unsigned int size;
918
919         chp = to_channelpath(container_of(kobj, struct device, kobj));
920         css = to_css(chp->dev.parent);
921
922         size = sizeof(struct cmg_entry);
923
924         /* Only allow single reads. */
925         if (off || count < size)
926                 return 0;
927         chp_measurement_copy_block((struct cmg_entry *)buf, css, chp->id);
928         count = size;
929         return count;
930 }
931
932 static struct bin_attribute chp_measurement_attr = {
933         .attr = {
934                 .name = "measurement",
935                 .mode = S_IRUSR,
936                 .owner = THIS_MODULE,
937         },
938         .size = sizeof(struct cmg_entry),
939         .read = chp_measurement_read,
940 };
941
942 static void
943 chsc_remove_chp_cmg_attr(struct channel_path *chp)
944 {
945         sysfs_remove_bin_file(&chp->dev.kobj, &chp_measurement_chars_attr);
946         sysfs_remove_bin_file(&chp->dev.kobj, &chp_measurement_attr);
947 }
948
949 static int
950 chsc_add_chp_cmg_attr(struct channel_path *chp)
951 {
952         int ret;
953
954         ret = sysfs_create_bin_file(&chp->dev.kobj,
955                                     &chp_measurement_chars_attr);
956         if (ret)
957                 return ret;
958         ret = sysfs_create_bin_file(&chp->dev.kobj, &chp_measurement_attr);
959         if (ret)
960                 sysfs_remove_bin_file(&chp->dev.kobj,
961                                       &chp_measurement_chars_attr);
962         return ret;
963 }
964
965 static void
966 chsc_remove_cmg_attr(struct channel_subsystem *css)
967 {
968         int i;
969
970         for (i = 0; i <= __MAX_CHPID; i++) {
971                 if (!css->chps[i])
972                         continue;
973                 chsc_remove_chp_cmg_attr(css->chps[i]);
974         }
975 }
976
977 static int
978 chsc_add_cmg_attr(struct channel_subsystem *css)
979 {
980         int i, ret;
981
982         ret = 0;
983         for (i = 0; i <= __MAX_CHPID; i++) {
984                 if (!css->chps[i])
985                         continue;
986                 ret = chsc_add_chp_cmg_attr(css->chps[i]);
987                 if (ret)
988                         goto cleanup;
989         }
990         return ret;
991 cleanup:
992         for (--i; i >= 0; i--) {
993                 if (!css->chps[i])
994                         continue;
995                 chsc_remove_chp_cmg_attr(css->chps[i]);
996         }
997         return ret;
998 }
999
1000
1001 static int
1002 __chsc_do_secm(struct channel_subsystem *css, int enable, void *page)
1003 {
1004         struct {
1005                 struct chsc_header request;
1006                 u32 operation_code : 2;
1007                 u32 : 30;
1008                 u32 key : 4;
1009                 u32 : 28;
1010                 u32 zeroes1;
1011                 u32 cub_addr1;
1012                 u32 zeroes2;
1013                 u32 cub_addr2;
1014                 u32 reserved[13];
1015                 struct chsc_header response;
1016                 u32 status : 8;
1017                 u32 : 4;
1018                 u32 fmt : 4;
1019                 u32 : 16;
1020         } *secm_area;
1021         int ret, ccode;
1022
1023         secm_area = page;
1024         secm_area->request.length = 0x0050;
1025         secm_area->request.code = 0x0016;
1026
1027         secm_area->key = PAGE_DEFAULT_KEY;
1028         secm_area->cub_addr1 = (u64)(unsigned long)css->cub_addr1;
1029         secm_area->cub_addr2 = (u64)(unsigned long)css->cub_addr2;
1030
1031         secm_area->operation_code = enable ? 0 : 1;
1032
1033         ccode = chsc(secm_area);
1034         if (ccode > 0)
1035                 return (ccode == 3) ? -ENODEV : -EBUSY;
1036
1037         switch (secm_area->response.code) {
1038         case 0x0001: /* Success. */
1039                 ret = 0;
1040                 break;
1041         case 0x0003: /* Invalid block. */
1042         case 0x0007: /* Invalid format. */
1043         case 0x0008: /* Other invalid block. */
1044                 CIO_CRW_EVENT(2, "Error in chsc request block!\n");
1045                 ret = -EINVAL;
1046                 break;
1047         case 0x0004: /* Command not provided in model. */
1048                 CIO_CRW_EVENT(2, "Model does not provide secm\n");
1049                 ret = -EOPNOTSUPP;
1050                 break;
1051         case 0x0102: /* cub adresses incorrect */
1052                 CIO_CRW_EVENT(2, "Invalid addresses in chsc request block\n");
1053                 ret = -EINVAL;
1054                 break;
1055         case 0x0103: /* key error */
1056                 CIO_CRW_EVENT(2, "Access key error in secm\n");
1057                 ret = -EINVAL;
1058                 break;
1059         case 0x0105: /* error while starting */
1060                 CIO_CRW_EVENT(2, "Error while starting channel measurement\n");
1061                 ret = -EIO;
1062                 break;
1063         default:
1064                 CIO_CRW_EVENT(2, "Unknown CHSC response %d\n",
1065                               secm_area->response.code);
1066                 ret = -EIO;
1067         }
1068         return ret;
1069 }
1070
1071 int
1072 chsc_secm(struct channel_subsystem *css, int enable)
1073 {
1074         void  *secm_area;
1075         int ret;
1076
1077         secm_area = (void *)get_zeroed_page(GFP_KERNEL |  GFP_DMA);
1078         if (!secm_area)
1079                 return -ENOMEM;
1080
1081         mutex_lock(&css->mutex);
1082         if (enable && !css->cm_enabled) {
1083                 css->cub_addr1 = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
1084                 css->cub_addr2 = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
1085                 if (!css->cub_addr1 || !css->cub_addr2) {
1086                         free_page((unsigned long)css->cub_addr1);
1087                         free_page((unsigned long)css->cub_addr2);
1088                         free_page((unsigned long)secm_area);
1089                         mutex_unlock(&css->mutex);
1090                         return -ENOMEM;
1091                 }
1092         }
1093         ret = __chsc_do_secm(css, enable, secm_area);
1094         if (!ret) {
1095                 css->cm_enabled = enable;
1096                 if (css->cm_enabled) {
1097                         ret = chsc_add_cmg_attr(css);
1098                         if (ret) {
1099                                 memset(secm_area, 0, PAGE_SIZE);
1100                                 __chsc_do_secm(css, 0, secm_area);
1101                                 css->cm_enabled = 0;
1102                         }
1103                 } else
1104                         chsc_remove_cmg_attr(css);
1105         }
1106         if (enable && !css->cm_enabled) {
1107                 free_page((unsigned long)css->cub_addr1);
1108                 free_page((unsigned long)css->cub_addr2);
1109         }
1110         mutex_unlock(&css->mutex);
1111         free_page((unsigned long)secm_area);
1112         return ret;
1113 }
1114
1115 /*
1116  * Files for the channel path entries.
1117  */
1118 static ssize_t
1119 chp_status_show(struct device *dev, struct device_attribute *attr, char *buf)
1120 {
1121         struct channel_path *chp = container_of(dev, struct channel_path, dev);
1122
1123         if (!chp)
1124                 return 0;
1125         return (get_chp_status(chp->id) ? sprintf(buf, "online\n") :
1126                 sprintf(buf, "offline\n"));
1127 }
1128
1129 static ssize_t
1130 chp_status_write(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
1131 {
1132         struct channel_path *cp = container_of(dev, struct channel_path, dev);
1133         char cmd[10];
1134         int num_args;
1135         int error;
1136
1137         num_args = sscanf(buf, "%5s", cmd);
1138         if (!num_args)
1139                 return count;
1140
1141         if (!strnicmp(cmd, "on", 2))
1142                 error = s390_vary_chpid(cp->id, 1);
1143         else if (!strnicmp(cmd, "off", 3))
1144                 error = s390_vary_chpid(cp->id, 0);
1145         else
1146                 error = -EINVAL;
1147
1148         return error < 0 ? error : count;
1149
1150 }
1151
1152 static DEVICE_ATTR(status, 0644, chp_status_show, chp_status_write);
1153
1154 static ssize_t
1155 chp_type_show(struct device *dev, struct device_attribute *attr, char *buf)
1156 {
1157         struct channel_path *chp = container_of(dev, struct channel_path, dev);
1158
1159         if (!chp)
1160                 return 0;
1161         return sprintf(buf, "%x\n", chp->desc.desc);
1162 }
1163
1164 static DEVICE_ATTR(type, 0444, chp_type_show, NULL);
1165
1166 static ssize_t
1167 chp_cmg_show(struct device *dev, struct device_attribute *attr, char *buf)
1168 {
1169         struct channel_path *chp = to_channelpath(dev);
1170
1171         if (!chp)
1172                 return 0;
1173         if (chp->cmg == -1) /* channel measurements not available */
1174                 return sprintf(buf, "unknown\n");
1175         return sprintf(buf, "%x\n", chp->cmg);
1176 }
1177
1178 static DEVICE_ATTR(cmg, 0444, chp_cmg_show, NULL);
1179
1180 static ssize_t
1181 chp_shared_show(struct device *dev, struct device_attribute *attr, char *buf)
1182 {
1183         struct channel_path *chp = to_channelpath(dev);
1184
1185         if (!chp)
1186                 return 0;
1187         if (chp->shared == -1) /* channel measurements not available */
1188                 return sprintf(buf, "unknown\n");
1189         return sprintf(buf, "%x\n", chp->shared);
1190 }
1191
1192 static DEVICE_ATTR(shared, 0444, chp_shared_show, NULL);
1193
1194 static struct attribute * chp_attrs[] = {
1195         &dev_attr_status.attr,
1196         &dev_attr_type.attr,
1197         &dev_attr_cmg.attr,
1198         &dev_attr_shared.attr,
1199         NULL,
1200 };
1201
1202 static struct attribute_group chp_attr_group = {
1203         .attrs = chp_attrs,
1204 };
1205
1206 static void
1207 chp_release(struct device *dev)
1208 {
1209         struct channel_path *cp;
1210         
1211         cp = container_of(dev, struct channel_path, dev);
1212         kfree(cp);
1213 }
1214
1215 static int
1216 chsc_determine_channel_path_description(int chpid,
1217                                         struct channel_path_desc *desc)
1218 {
1219         int ccode, ret;
1220
1221         struct {
1222                 struct chsc_header request;
1223                 u32 : 24;
1224                 u32 first_chpid : 8;
1225                 u32 : 24;
1226                 u32 last_chpid : 8;
1227                 u32 zeroes1;
1228                 struct chsc_header response;
1229                 u32 zeroes2;
1230                 struct channel_path_desc desc;
1231         } *scpd_area;
1232
1233         scpd_area = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
1234         if (!scpd_area)
1235                 return -ENOMEM;
1236
1237         scpd_area->request.length = 0x0010;
1238         scpd_area->request.code = 0x0002;
1239
1240         scpd_area->first_chpid = chpid;
1241         scpd_area->last_chpid = chpid;
1242
1243         ccode = chsc(scpd_area);
1244         if (ccode > 0) {
1245                 ret = (ccode == 3) ? -ENODEV : -EBUSY;
1246                 goto out;
1247         }
1248
1249         switch (scpd_area->response.code) {
1250         case 0x0001: /* Success. */
1251                 memcpy(desc, &scpd_area->desc,
1252                        sizeof(struct channel_path_desc));
1253                 ret = 0;
1254                 break;
1255         case 0x0003: /* Invalid block. */
1256         case 0x0007: /* Invalid format. */
1257         case 0x0008: /* Other invalid block. */
1258                 CIO_CRW_EVENT(2, "Error in chsc request block!\n");
1259                 ret = -EINVAL;
1260                 break;
1261         case 0x0004: /* Command not provided in model. */
1262                 CIO_CRW_EVENT(2, "Model does not provide scpd\n");
1263                 ret = -EOPNOTSUPP;
1264                 break;
1265         default:
1266                 CIO_CRW_EVENT(2, "Unknown CHSC response %d\n",
1267                               scpd_area->response.code);
1268                 ret = -EIO;
1269         }
1270 out:
1271         free_page((unsigned long)scpd_area);
1272         return ret;
1273 }
1274
1275 static void
1276 chsc_initialize_cmg_chars(struct channel_path *chp, u8 cmcv,
1277                           struct cmg_chars *chars)
1278 {
1279         switch (chp->cmg) {
1280         case 2:
1281         case 3:
1282                 chp->cmg_chars = kmalloc(sizeof(struct cmg_chars),
1283                                          GFP_KERNEL);
1284                 if (chp->cmg_chars) {
1285                         int i, mask;
1286                         struct cmg_chars *cmg_chars;
1287
1288                         cmg_chars = chp->cmg_chars;
1289                         for (i = 0; i < NR_MEASUREMENT_CHARS; i++) {
1290                                 mask = 0x80 >> (i + 3);
1291                                 if (cmcv & mask)
1292                                         cmg_chars->values[i] = chars->values[i];
1293                                 else
1294                                         cmg_chars->values[i] = 0;
1295                         }
1296                 }
1297                 break;
1298         default:
1299                 /* No cmg-dependent data. */
1300                 break;
1301         }
1302 }
1303
1304 static int
1305 chsc_get_channel_measurement_chars(struct channel_path *chp)
1306 {
1307         int ccode, ret;
1308
1309         struct {
1310                 struct chsc_header request;
1311                 u32 : 24;
1312                 u32 first_chpid : 8;
1313                 u32 : 24;
1314                 u32 last_chpid : 8;
1315                 u32 zeroes1;
1316                 struct chsc_header response;
1317                 u32 zeroes2;
1318                 u32 not_valid : 1;
1319                 u32 shared : 1;
1320                 u32 : 22;
1321                 u32 chpid : 8;
1322                 u32 cmcv : 5;
1323                 u32 : 11;
1324                 u32 cmgq : 8;
1325                 u32 cmg : 8;
1326                 u32 zeroes3;
1327                 u32 data[NR_MEASUREMENT_CHARS];
1328         } *scmc_area;
1329
1330         scmc_area = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
1331         if (!scmc_area)
1332                 return -ENOMEM;
1333
1334         scmc_area->request.length = 0x0010;
1335         scmc_area->request.code = 0x0022;
1336
1337         scmc_area->first_chpid = chp->id;
1338         scmc_area->last_chpid = chp->id;
1339
1340         ccode = chsc(scmc_area);
1341         if (ccode > 0) {
1342                 ret = (ccode == 3) ? -ENODEV : -EBUSY;
1343                 goto out;
1344         }
1345
1346         switch (scmc_area->response.code) {
1347         case 0x0001: /* Success. */
1348                 if (!scmc_area->not_valid) {
1349                         chp->cmg = scmc_area->cmg;
1350                         chp->shared = scmc_area->shared;
1351                         chsc_initialize_cmg_chars(chp, scmc_area->cmcv,
1352                                                   (struct cmg_chars *)
1353                                                   &scmc_area->data);
1354                 } else {
1355                         chp->cmg = -1;
1356                         chp->shared = -1;
1357                 }
1358                 ret = 0;
1359                 break;
1360         case 0x0003: /* Invalid block. */
1361         case 0x0007: /* Invalid format. */
1362         case 0x0008: /* Invalid bit combination. */
1363                 CIO_CRW_EVENT(2, "Error in chsc request block!\n");
1364                 ret = -EINVAL;
1365                 break;
1366         case 0x0004: /* Command not provided. */
1367                 CIO_CRW_EVENT(2, "Model does not provide scmc\n");
1368                 ret = -EOPNOTSUPP;
1369                 break;
1370         default:
1371                 CIO_CRW_EVENT(2, "Unknown CHSC response %d\n",
1372                               scmc_area->response.code);
1373                 ret = -EIO;
1374         }
1375 out:
1376         free_page((unsigned long)scmc_area);
1377         return ret;
1378 }
1379
1380 /*
1381  * Entries for chpids on the system bus.
1382  * This replaces /proc/chpids.
1383  */
1384 static int
1385 new_channel_path(int chpid)
1386 {
1387         struct channel_path *chp;
1388         int ret;
1389
1390         chp = kzalloc(sizeof(struct channel_path), GFP_KERNEL);
1391         if (!chp)
1392                 return -ENOMEM;
1393
1394         /* fill in status, etc. */
1395         chp->id = chpid;
1396         chp->state = 1;
1397         chp->dev.parent = &css[0]->device;
1398         chp->dev.release = chp_release;
1399         snprintf(chp->dev.bus_id, BUS_ID_SIZE, "chp0.%x", chpid);
1400
1401         /* Obtain channel path description and fill it in. */
1402         ret = chsc_determine_channel_path_description(chpid, &chp->desc);
1403         if (ret)
1404                 goto out_free;
1405         /* Get channel-measurement characteristics. */
1406         if (css_characteristics_avail && css_chsc_characteristics.scmc
1407             && css_chsc_characteristics.secm) {
1408                 ret = chsc_get_channel_measurement_chars(chp);
1409                 if (ret)
1410                         goto out_free;
1411         } else {
1412                 static int msg_done;
1413
1414                 if (!msg_done) {
1415                         printk(KERN_WARNING "cio: Channel measurements not "
1416                                "available, continuing.\n");
1417                         msg_done = 1;
1418                 }
1419                 chp->cmg = -1;
1420         }
1421
1422         /* make it known to the system */
1423         ret = device_register(&chp->dev);
1424         if (ret) {
1425                 printk(KERN_WARNING "%s: could not register %02x\n",
1426                        __func__, chpid);
1427                 goto out_free;
1428         }
1429         ret = sysfs_create_group(&chp->dev.kobj, &chp_attr_group);
1430         if (ret) {
1431                 device_unregister(&chp->dev);
1432                 goto out_free;
1433         }
1434         mutex_lock(&css[0]->mutex);
1435         if (css[0]->cm_enabled) {
1436                 ret = chsc_add_chp_cmg_attr(chp);
1437                 if (ret) {
1438                         sysfs_remove_group(&chp->dev.kobj, &chp_attr_group);
1439                         device_unregister(&chp->dev);
1440                         mutex_unlock(&css[0]->mutex);
1441                         goto out_free;
1442                 }
1443         }
1444         css[0]->chps[chpid] = chp;
1445         mutex_unlock(&css[0]->mutex);
1446         return ret;
1447 out_free:
1448         kfree(chp);
1449         return ret;
1450 }
1451
1452 void *
1453 chsc_get_chp_desc(struct subchannel *sch, int chp_no)
1454 {
1455         struct channel_path *chp;
1456         struct channel_path_desc *desc;
1457
1458         chp = css[0]->chps[sch->schib.pmcw.chpid[chp_no]];
1459         if (!chp)
1460                 return NULL;
1461         desc = kmalloc(sizeof(struct channel_path_desc), GFP_KERNEL);
1462         if (!desc)
1463                 return NULL;
1464         memcpy(desc, &chp->desc, sizeof(struct channel_path_desc));
1465         return desc;
1466 }
1467
1468 static int reset_channel_path(struct channel_path *chp)
1469 {
1470         int cc;
1471
1472         cc = rchp(chp->id);
1473         switch (cc) {
1474         case 0:
1475                 return 0;
1476         case 2:
1477                 return -EBUSY;
1478         default:
1479                 return -ENODEV;
1480         }
1481 }
1482
1483 static void reset_channel_paths_css(struct channel_subsystem *css)
1484 {
1485         int i;
1486
1487         for (i = 0; i <= __MAX_CHPID; i++) {
1488                 if (css->chps[i])
1489                         reset_channel_path(css->chps[i]);
1490         }
1491 }
1492
1493 void cio_reset_channel_paths(void)
1494 {
1495         int i;
1496
1497         for (i = 0; i <= __MAX_CSSID; i++) {
1498                 if (css[i] && css[i]->valid)
1499                         reset_channel_paths_css(css[i]);
1500         }
1501 }
1502
1503 static int __init
1504 chsc_alloc_sei_area(void)
1505 {
1506         sei_page = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
1507         if (!sei_page)
1508                 printk(KERN_WARNING"Can't allocate page for processing of " \
1509                        "chsc machine checks!\n");
1510         return (sei_page ? 0 : -ENOMEM);
1511 }
1512
1513 int __init
1514 chsc_enable_facility(int operation_code)
1515 {
1516         int ret;
1517         struct {
1518                 struct chsc_header request;
1519                 u8 reserved1:4;
1520                 u8 format:4;
1521                 u8 reserved2;
1522                 u16 operation_code;
1523                 u32 reserved3;
1524                 u32 reserved4;
1525                 u32 operation_data_area[252];
1526                 struct chsc_header response;
1527                 u32 reserved5:4;
1528                 u32 format2:4;
1529                 u32 reserved6:24;
1530         } *sda_area;
1531
1532         sda_area = (void *)get_zeroed_page(GFP_KERNEL|GFP_DMA);
1533         if (!sda_area)
1534                 return -ENOMEM;
1535         sda_area->request.length = 0x0400;
1536         sda_area->request.code = 0x0031;
1537         sda_area->operation_code = operation_code;
1538
1539         ret = chsc(sda_area);
1540         if (ret > 0) {
1541                 ret = (ret == 3) ? -ENODEV : -EBUSY;
1542                 goto out;
1543         }
1544         switch (sda_area->response.code) {
1545         case 0x0001: /* everything ok */
1546                 ret = 0;
1547                 break;
1548         case 0x0003: /* invalid request block */
1549         case 0x0007:
1550                 ret = -EINVAL;
1551                 break;
1552         case 0x0004: /* command not provided */
1553         case 0x0101: /* facility not provided */
1554                 ret = -EOPNOTSUPP;
1555                 break;
1556         default: /* something went wrong */
1557                 ret = -EIO;
1558         }
1559  out:
1560         free_page((unsigned long)sda_area);
1561         return ret;
1562 }
1563
1564 subsys_initcall(chsc_alloc_sei_area);
1565
1566 struct css_general_char css_general_characteristics;
1567 struct css_chsc_char css_chsc_characteristics;
1568
1569 int __init
1570 chsc_determine_css_characteristics(void)
1571 {
1572         int result;
1573         struct {
1574                 struct chsc_header request;
1575                 u32 reserved1;
1576                 u32 reserved2;
1577                 u32 reserved3;
1578                 struct chsc_header response;
1579                 u32 reserved4;
1580                 u32 general_char[510];
1581                 u32 chsc_char[518];
1582         } *scsc_area;
1583
1584         scsc_area = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
1585         if (!scsc_area) {
1586                 printk(KERN_WARNING"cio: Was not able to determine available" \
1587                        "CHSCs due to no memory.\n");
1588                 return -ENOMEM;
1589         }
1590
1591         scsc_area->request.length = 0x0010;
1592         scsc_area->request.code = 0x0010;
1593
1594         result = chsc(scsc_area);
1595         if (result) {
1596                 printk(KERN_WARNING"cio: Was not able to determine " \
1597                        "available CHSCs, cc=%i.\n", result);
1598                 result = -EIO;
1599                 goto exit;
1600         }
1601
1602         if (scsc_area->response.code != 1) {
1603                 printk(KERN_WARNING"cio: Was not able to determine " \
1604                        "available CHSCs.\n");
1605                 result = -EIO;
1606                 goto exit;
1607         }
1608         memcpy(&css_general_characteristics, scsc_area->general_char,
1609                sizeof(css_general_characteristics));
1610         memcpy(&css_chsc_characteristics, scsc_area->chsc_char,
1611                sizeof(css_chsc_characteristics));
1612 exit:
1613         free_page ((unsigned long) scsc_area);
1614         return result;
1615 }
1616
1617 EXPORT_SYMBOL_GPL(css_general_characteristics);
1618 EXPORT_SYMBOL_GPL(css_chsc_characteristics);