ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / s390 / cio / chsc.c
1 /*
2  *  drivers/s390/cio/chsc.c
3  *   S/390 common I/O routines -- channel subsystem call
4  *   $Revision: 1.110 $
5  *
6  *    Copyright (C) 1999-2002 IBM Deutschland Entwicklung GmbH,
7  *                            IBM Corporation
8  *    Author(s): Ingo Adlung (adlung@de.ibm.com)
9  *               Cornelia Huck (cohuck@de.ibm.com)
10  *               Arnd Bergmann (arndb@de.ibm.com)
11  */
12
13 #include <linux/module.h>
14 #include <linux/config.h>
15 #include <linux/slab.h>
16 #include <linux/init.h>
17 #include <linux/device.h>
18
19 #include <asm/cio.h>
20
21 #include "css.h"
22 #include "cio.h"
23 #include "cio_debug.h"
24 #include "ioasm.h"
25 #include "chsc.h"
26
27 #define CHPID_LONGS (256 / (8 * sizeof(long))) /* 256 chpids */
28 static struct channel_path *chps[NR_CHPIDS];
29
30 static void *sei_page;
31
32 static int new_channel_path(int chpid);
33
34 static inline void
35 set_chp_logically_online(int chp, int onoff)
36 {
37         chps[chp]->state = onoff;
38 }
39
40 static int
41 get_chp_status(int chp)
42 {
43         return (chps[chp] ? chps[chp]->state : -ENODEV);
44 }
45
46 void
47 chsc_validate_chpids(struct subchannel *sch)
48 {
49         int mask, chp;
50
51         for (chp = 0; chp <= 7; chp++) {
52                 mask = 0x80 >> chp;
53                 if (!get_chp_status(sch->schib.pmcw.chpid[chp]))
54                         /* disable using this path */
55                         sch->opm &= ~mask;
56         }
57 }
58
59 void
60 chpid_is_actually_online(int chp)
61 {
62         int state;
63
64         state = get_chp_status(chp);
65         if (state < 0)
66                 new_channel_path(chp);
67         else
68                 WARN_ON(!state);
69         /* FIXME: should notify other subchannels here */
70 }
71
72 /* FIXME: this is _always_ called for every subchannel. shouldn't we
73  *        process more than one at a time? */
74 static int
75 chsc_get_sch_desc_irq(struct subchannel *sch, void *page)
76 {
77         int ccode, j;
78
79         struct {
80                 struct chsc_header request;
81                 u16 reserved1;
82                 u16 f_sch;        /* first subchannel */
83                 u16 reserved2;
84                 u16 l_sch;        /* last subchannel */
85                 u32 reserved3;
86                 struct chsc_header response;
87                 u32 reserved4;
88                 u8 sch_valid : 1;
89                 u8 dev_valid : 1;
90                 u8 st        : 3; /* subchannel type */
91                 u8 zeroes    : 3;
92                 u8  unit_addr;    /* unit address */
93                 u16 devno;        /* device number */
94                 u8 path_mask;
95                 u8 fla_valid_mask;
96                 u16 sch;          /* subchannel */
97                 u8 chpid[8];      /* chpids 0-7 */
98                 u16 fla[8];       /* full link addresses 0-7 */
99         } *ssd_area;
100
101         ssd_area = page;
102
103         ssd_area->request = (struct chsc_header) {
104                 .length = 0x0010,
105                 .code   = 0x0004,
106         };
107
108         ssd_area->f_sch = sch->irq;
109         ssd_area->l_sch = sch->irq;
110
111         ccode = chsc(ssd_area);
112         if (ccode > 0) {
113                 pr_debug("chsc returned with ccode = %d\n", ccode);
114                 return (ccode == 3) ? -ENODEV : -EBUSY;
115         }
116
117         switch (ssd_area->response.code) {
118         case 0x0001: /* everything ok */
119                 break;
120         case 0x0002:
121                 CIO_CRW_EVENT(2, "Invalid command!\n");
122                 return -EINVAL;
123         case 0x0003:
124                 CIO_CRW_EVENT(2, "Error in chsc request block!\n");
125                 return -EINVAL;
126         case 0x0004:
127                 CIO_CRW_EVENT(2, "Model does not provide ssd\n");
128                 return -EOPNOTSUPP;
129         default:
130                 CIO_CRW_EVENT(2, "Unknown CHSC response %d\n",
131                               ssd_area->response.code);
132                 return -EIO;
133         }
134
135         /*
136          * ssd_area->st stores the type of the detected
137          * subchannel, with the following definitions:
138          *
139          * 0: I/O subchannel:     All fields have meaning
140          * 1: CHSC subchannel:    Only sch_val, st and sch
141          *                        have meaning
142          * 2: Message subchannel: All fields except unit_addr
143          *                        have meaning
144          * 3: ADM subchannel:     Only sch_val, st and sch
145          *                        have meaning
146          *
147          * Other types are currently undefined.
148          */
149         if (ssd_area->st > 3) { /* uhm, that looks strange... */
150                 CIO_CRW_EVENT(0, "Strange subchannel type %d"
151                               " for sch %04x\n", ssd_area->st, sch->irq);
152                 /*
153                  * There may have been a new subchannel type defined in the
154                  * time since this code was written; since we don't know which
155                  * fields have meaning and what to do with it we just jump out
156                  */
157                 return 0;
158         } else {
159                 const char *type[4] = {"I/O", "chsc", "message", "ADM"};
160                 CIO_CRW_EVENT(6, "ssd: sch %04x is %s subchannel\n",
161                               sch->irq, type[ssd_area->st]);
162
163                 sch->ssd_info.valid = 1;
164                 sch->ssd_info.type = ssd_area->st;
165         }
166
167         if (ssd_area->st == 0 || ssd_area->st == 2) {
168                 for (j = 0; j < 8; j++) {
169                         if (!((0x80 >> j) & ssd_area->path_mask &
170                               ssd_area->fla_valid_mask))
171                                 continue;
172                         sch->ssd_info.chpid[j] = ssd_area->chpid[j];
173                         sch->ssd_info.fla[j]   = ssd_area->fla[j];
174                 }
175         }
176         return 0;
177 }
178
179 int
180 css_get_ssd_info(struct subchannel *sch)
181 {
182         int ret;
183         void *page;
184
185         page = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
186         if (!page)
187                 return -ENOMEM;
188         spin_lock_irq(&sch->lock);
189         ret = chsc_get_sch_desc_irq(sch, page);
190         if (ret) {
191                 static int cio_chsc_err_msg;
192                 
193                 if (!cio_chsc_err_msg) {
194                         printk(KERN_ERR
195                                "chsc_get_sch_descriptions:"
196                                " Error %d while doing chsc; "
197                                "processing some machine checks may "
198                                "not work\n", ret);
199                         cio_chsc_err_msg = 1;
200                 }
201         }
202         spin_unlock_irq(&sch->lock);
203         free_page((unsigned long)page);
204         if (!ret) {
205                 int j, chpid;
206                 /* Allocate channel path structures, if needed. */
207                 for (j = 0; j < 8; j++) {
208                         chpid = sch->ssd_info.chpid[j];
209                         if (chpid && (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         __u8 *chpid;
223         struct schib schib;
224
225         sch = to_subchannel(dev);
226         chpid = data;
227         for (j = 0; j < 8; j++)
228                 if (sch->schib.pmcw.chpid[j] == *chpid)
229                         break;
230         if (j >= 8)
231                 return 0;
232
233         mask = 0x80 >> j;
234         spin_lock(&sch->lock);
235
236         stsch(sch->irq, &schib);
237         if (!schib.pmcw.dnv)
238                 goto out_unreg;
239         memcpy(&sch->schib, &schib, sizeof(struct schib));
240         /* Check for single path devices. */
241         if (sch->schib.pmcw.pim == 0x80)
242                 goto out_unreg;
243         if (sch->vpm == mask)
244                 goto out_unreg;
245
246         if ((sch->schib.scsw.actl & (SCSW_ACTL_CLEAR_PEND |
247                                      SCSW_ACTL_HALT_PEND |
248                                      SCSW_ACTL_START_PEND |
249                                      SCSW_ACTL_RESUME_PEND)) &&
250             (sch->schib.pmcw.lpum == mask)) {
251                 int cc = cio_cancel(sch);
252                 
253                 if (cc == -ENODEV)
254                         goto out_unreg;
255
256                 if (cc == -EINVAL) {
257                         cc = cio_clear(sch);
258                         if (cc == -ENODEV)
259                                 goto out_unreg;
260                         /* Call handler. */
261                         if (sch->driver && sch->driver->termination)
262                                 sch->driver->termination(&sch->dev);
263                         goto out_unlock;
264                 }
265         } else if ((sch->schib.scsw.actl & SCSW_ACTL_DEVACT) &&
266                    (sch->schib.scsw.actl & SCSW_ACTL_SCHACT) &&
267                    (sch->schib.pmcw.lpum == mask)) {
268                 int cc;
269
270                 cc = cio_clear(sch);
271                 if (cc == -ENODEV)
272                         goto out_unreg;
273                 /* Call handler. */
274                 if (sch->driver && sch->driver->termination)
275                         sch->driver->termination(&sch->dev);
276                 goto out_unlock;
277         }
278
279         /* trigger path verification. */
280         if (sch->driver && sch->driver->verify)
281                 sch->driver->verify(&sch->dev);
282 out_unlock:
283         spin_unlock(&sch->lock);
284         return 0;
285 out_unreg:
286         spin_unlock(&sch->lock);
287         sch->lpm = 0;
288         /* We can't block here. */
289         device_call_nopath_notify(sch);
290         return 0;
291 }
292
293 static inline void
294 s390_set_chpid_offline( __u8 chpid)
295 {
296         char dbf_txt[15];
297
298         sprintf(dbf_txt, "chpr%x", chpid);
299         CIO_TRACE_EVENT(2, dbf_txt);
300
301         if (get_chp_status(chpid) <= 0)
302                 return;
303
304         bus_for_each_dev(&css_bus_type, NULL, &chpid,
305                          s390_subchannel_remove_chpid);
306 }
307
308 static int
309 s390_process_res_acc_sch(u8 chpid, __u16 fla, u32 fla_mask,
310                          struct subchannel *sch)
311 {
312         int found;
313         int chp;
314         int ccode;
315         
316         found = 0;
317         for (chp = 0; chp <= 7; chp++)
318                 /*
319                  * check if chpid is in information updated by ssd
320                  */
321                 if (sch->ssd_info.valid &&
322                     sch->ssd_info.chpid[chp] == chpid &&
323                     (sch->ssd_info.fla[chp] & fla_mask) == fla) {
324                         found = 1;
325                         break;
326                 }
327         
328         if (found == 0)
329                 return 0;
330
331         /*
332          * Do a stsch to update our subchannel structure with the
333          * new path information and eventually check for logically
334          * offline chpids.
335          */
336         ccode = stsch(sch->irq, &sch->schib);
337         if (ccode > 0)
338                 return 0;
339
340         return 0x80 >> chp;
341 }
342
343 static int
344 s390_process_res_acc (u8 chpid, __u16 fla, u32 fla_mask)
345 {
346         struct subchannel *sch;
347         int irq, rc;
348         char dbf_txt[15];
349
350         sprintf(dbf_txt, "accpr%x", chpid);
351         CIO_TRACE_EVENT( 2, dbf_txt);
352         if (fla != 0) {
353                 sprintf(dbf_txt, "fla%x", fla);
354                 CIO_TRACE_EVENT( 2, dbf_txt);
355         }
356
357         /*
358          * I/O resources may have become accessible.
359          * Scan through all subchannels that may be concerned and
360          * do a validation on those.
361          * The more information we have (info), the less scanning
362          * will we have to do.
363          */
364
365         if (!get_chp_status(chpid))
366                 return 0; /* no need to do the rest */
367
368         rc = 0;
369         for (irq = 0; irq < __MAX_SUBCHANNELS; irq++) {
370                 int chp_mask, old_lpm;
371
372                 sch = get_subchannel_by_schid(irq);
373                 if (!sch) {
374                         struct schib schib;
375                         int ret;
376                         /*
377                          * We don't know the device yet, but since a path
378                          * may be available now to the device we'll have
379                          * to do recognition again.
380                          * Since we don't have any idea about which chpid
381                          * that beast may be on we'll have to do a stsch
382                          * on all devices, grr...
383                          */
384                         if (stsch(irq, &schib)) {
385                                 /* We're through */
386                                 if (need_rescan)
387                                         rc = -EAGAIN;
388                                 break;
389                         }
390                         if (need_rescan) {
391                                 rc = -EAGAIN;
392                                 continue;
393                         }
394                         /* Put it on the slow path. */
395                         ret = css_enqueue_subchannel_slow(irq);
396                         if (ret) {
397                                 css_clear_subchannel_slow_list();
398                                 need_rescan = 1;
399                         }
400                         rc = -EAGAIN;
401                         continue;
402                 }
403         
404                 spin_lock_irq(&sch->lock);
405
406                 chp_mask = s390_process_res_acc_sch(chpid, fla, fla_mask, sch);
407
408                 if (chp_mask == 0) {
409
410                         spin_unlock_irq(&sch->lock);
411
412                         if (fla_mask != 0)
413                                 break;
414                         else
415                                 continue;
416                 }
417                 old_lpm = sch->lpm;
418                 sch->lpm = ((sch->schib.pmcw.pim &
419                              sch->schib.pmcw.pam &
420                              sch->schib.pmcw.pom)
421                             | chp_mask) & sch->opm;
422                 spin_unlock_irq(&sch->lock);
423                 if (!old_lpm && sch->lpm)
424                         device_trigger_reprobe(sch);
425                 else if (sch->driver && sch->driver->verify)
426                         sch->driver->verify(&sch->dev);
427
428                 put_device(&sch->dev);
429                 if (fla_mask != 0)
430                         break;
431         }
432         return rc;
433 }
434
435 static int
436 __get_chpid_from_lir(void *data)
437 {
438         struct lir {
439                 u8  iq;
440                 u8  ic;
441                 u16 sci;
442                 /* incident-node descriptor */
443                 u32 indesc[28];
444                 /* attached-node descriptor */
445                 u32 andesc[28];
446                 /* incident-specific information */
447                 u32 isinfo[28];
448         } *lir;
449
450         lir = (struct lir*) data;
451         if (!(lir->iq&0x80))
452                 /* NULL link incident record */
453                 return -EINVAL;
454         if (!(lir->indesc[0]&0xc0000000))
455                 /* node descriptor not valid */
456                 return -EINVAL;
457         if (!(lir->indesc[0]&0x10000000))
458                 /* don't handle device-type nodes - FIXME */
459                 return -EINVAL;
460         /* Byte 3 contains the chpid. Could also be CTCA, but we don't care */
461
462         return (u16) (lir->indesc[0]&0x000000ff);
463 }
464
465 int
466 chsc_process_crw(void)
467 {
468         int chpid, ret;
469         struct {
470                 struct chsc_header request;
471                 u32 reserved1;
472                 u32 reserved2;
473                 u32 reserved3;
474                 struct chsc_header response;
475                 u32 reserved4;
476                 u8  flags;
477                 u8  vf;         /* validity flags */
478                 u8  rs;         /* reporting source */
479                 u8  cc;         /* content code */
480                 u16 fla;        /* full link address */
481                 u16 rsid;       /* reporting source id */
482                 u32 reserved5;
483                 u32 reserved6;
484                 u32 ccdf[96];   /* content-code dependent field */
485                 /* ccdf has to be big enough for a link-incident record */
486         } *sei_area;
487
488         if (!sei_page)
489                 return 0;
490         /*
491          * build the chsc request block for store event information
492          * and do the call
493          * This function is only called by the machine check handler thread,
494          * so we don't need locking for the sei_page.
495          */
496         sei_area = sei_page;
497
498         CIO_TRACE_EVENT( 2, "prcss");
499         ret = 0;
500         do {
501                 int ccode, status;
502                 memset(sei_area, 0, sizeof(*sei_area));
503
504                 sei_area->request = (struct chsc_header) {
505                         .length = 0x0010,
506                         .code   = 0x000e,
507                 };
508
509                 ccode = chsc(sei_area);
510                 if (ccode > 0)
511                         return 0;
512
513                 switch (sei_area->response.code) {
514                         /* for debug purposes, check for problems */
515                 case 0x0001:
516                         CIO_CRW_EVENT(4, "chsc_process_crw: event information "
517                                         "successfully stored\n");
518                         break; /* everything ok */
519                 case 0x0002:
520                         CIO_CRW_EVENT(2,
521                                       "chsc_process_crw: invalid command!\n");
522                         return 0;
523                 case 0x0003:
524                         CIO_CRW_EVENT(2, "chsc_process_crw: error in chsc "
525                                       "request block!\n");
526                         return 0;
527                 case 0x0005:
528                         CIO_CRW_EVENT(2, "chsc_process_crw: no event "
529                                       "information stored\n");
530                         return 0;
531                 default:
532                         CIO_CRW_EVENT(2, "chsc_process_crw: chsc response %d\n",
533                                       sei_area->response.code);
534                         return 0;
535                 }
536
537                 /* Check if we might have lost some information. */
538                 if (sei_area->flags & 0x40)
539                         CIO_CRW_EVENT(2, "chsc_process_crw: Event information "
540                                        "has been lost due to overflow!\n");
541
542                 if (sei_area->rs != 4) {
543                         CIO_CRW_EVENT(2, "chsc_process_crw: reporting source "
544                                       "(%04X) isn't a chpid!\n",
545                                       sei_area->rsid);
546                         continue;
547                 }
548
549                 /* which kind of information was stored? */
550                 switch (sei_area->cc) {
551                 case 1: /* link incident*/
552                         CIO_CRW_EVENT(4, "chsc_process_crw: "
553                                       "channel subsystem reports link incident,"
554                                       " reporting source is chpid %x\n",
555                                       sei_area->rsid);
556                         chpid = __get_chpid_from_lir(sei_area->ccdf);
557                         if (chpid < 0)
558                                 CIO_CRW_EVENT(4, "%s: Invalid LIR, skipping\n",
559                                               __FUNCTION__);
560                         else
561                                 s390_set_chpid_offline(chpid);
562                         break;
563                         
564                 case 2: /* i/o resource accessibiliy */
565                         CIO_CRW_EVENT(4, "chsc_process_crw: "
566                                       "channel subsystem reports some I/O "
567                                       "devices may have become accessible\n");
568                         pr_debug("Data received after sei: \n");
569                         pr_debug("Validity flags: %x\n", sei_area->vf);
570                         
571                         /* allocate a new channel path structure, if needed */
572                         status = get_chp_status(sei_area->rsid);
573                         if (status < 0)
574                                 new_channel_path(sei_area->rsid);
575                         else if (!status)
576                                 return 0;
577                         if ((sei_area->vf & 0x80) == 0) {
578                                 pr_debug("chpid: %x\n", sei_area->rsid);
579                                 ret = s390_process_res_acc(sei_area->rsid,
580                                                            0, 0);
581                         } else if ((sei_area->vf & 0xc0) == 0x80) {
582                                 pr_debug("chpid: %x link addr: %x\n",
583                                          sei_area->rsid, sei_area->fla);
584                                 ret = s390_process_res_acc(sei_area->rsid,
585                                                            sei_area->fla,
586                                                            0xff00);
587                         } else if ((sei_area->vf & 0xc0) == 0xc0) {
588                                 pr_debug("chpid: %x full link addr: %x\n",
589                                          sei_area->rsid, sei_area->fla);
590                                 ret = s390_process_res_acc(sei_area->rsid,
591                                                            sei_area->fla,
592                                                            0xffff);
593                         }
594                         pr_debug("\n");
595                         
596                         break;
597                         
598                 default: /* other stuff */
599                         CIO_CRW_EVENT(4, "chsc_process_crw: event %d\n",
600                                       sei_area->cc);
601                         break;
602                 }
603         } while (sei_area->flags & 0x80);
604         return ret;
605 }
606
607 static int
608 chp_add(int chpid)
609 {
610         struct subchannel *sch;
611         int irq, ret, rc;
612         char dbf_txt[15];
613
614         if (!get_chp_status(chpid))
615                 return 0; /* no need to do the rest */
616         
617         sprintf(dbf_txt, "cadd%x", chpid);
618         CIO_TRACE_EVENT(2, dbf_txt);
619
620         rc = 0;
621         for (irq = 0; irq < __MAX_SUBCHANNELS; irq++) {
622                 int i;
623
624                 sch = get_subchannel_by_schid(irq);
625                 if (!sch) {
626                         struct schib schib;
627
628                         if (stsch(irq, &schib)) {
629                                 /* We're through */
630                                 if (need_rescan)
631                                         rc = -EAGAIN;
632                                 break;
633                         }
634                         if (need_rescan) {
635                                 rc = -EAGAIN;
636                                 continue;
637                         }
638                         /* Put it on the slow path. */
639                         ret = css_enqueue_subchannel_slow(irq);
640                         if (ret) {
641                                 css_clear_subchannel_slow_list();
642                                 need_rescan = 1;
643                         }
644                         rc = -EAGAIN;
645                         continue;
646                 }
647         
648                 spin_lock(&sch->lock);
649                 for (i=0; i<8; i++)
650                         if (sch->schib.pmcw.chpid[i] == chpid) {
651                                 if (stsch(sch->irq, &sch->schib) != 0) {
652                                         /* Endgame. */
653                                         spin_unlock(&sch->lock);
654                                         return rc;
655                                 }
656                                 break;
657                         }
658                 if (i==8) {
659                         spin_unlock(&sch->lock);
660                         return rc;
661                 }
662                 sch->lpm = ((sch->schib.pmcw.pim &
663                              sch->schib.pmcw.pam &
664                              sch->schib.pmcw.pom)
665                             | 0x80 >> i) & sch->opm;
666
667                 if (sch->driver && sch->driver->verify)
668                         sch->driver->verify(&sch->dev);
669
670                 spin_unlock(&sch->lock);
671                 put_device(&sch->dev);
672         }
673         return rc;
674 }
675
676 /* 
677  * Handling of crw machine checks with channel path source.
678  */
679 int
680 chp_process_crw(int chpid, int on)
681 {
682         if (on == 0) {
683                 /* Path has gone. We use the link incident routine.*/
684                 s390_set_chpid_offline(chpid);
685                 return 0; /* De-register is async anyway. */
686         }
687         /*
688          * Path has come. Allocate a new channel path structure,
689          * if needed.
690          */
691         if (get_chp_status(chpid) < 0)
692                 new_channel_path(chpid);
693         /* Avoid the extra overhead in process_rec_acc. */
694         return chp_add(chpid);
695 }
696
697 static inline int
698 __check_for_io_and_kill(struct subchannel *sch, int index)
699 {
700         int cc;
701
702         cc = stsch(sch->irq, &sch->schib);
703         if (cc)
704                 return 0;
705         if (sch->schib.scsw.actl && sch->schib.pmcw.lpum == (0x80 >> index)) {
706                 device_set_waiting(sch);
707                 return 1;
708         }
709         return 0;
710 }
711
712 static inline void
713 __s390_subchannel_vary_chpid(struct subchannel *sch, __u8 chpid, int on)
714 {
715         int chp, old_lpm;
716
717         if (!sch->ssd_info.valid)
718                 return;
719         
720         old_lpm = sch->lpm;
721         for (chp = 0; chp < 8; chp++) {
722                 if (sch->ssd_info.chpid[chp] != chpid)
723                         continue;
724
725                 if (on) {
726                         sch->opm |= (0x80 >> chp);
727                         sch->lpm |= (0x80 >> chp);
728                         if (!old_lpm)
729                                 device_trigger_reprobe(sch);
730                         else if (sch->driver && sch->driver->verify)
731                                 sch->driver->verify(&sch->dev);
732                 } else {
733                         sch->opm &= ~(0x80 >> chp);
734                         sch->lpm &= ~(0x80 >> chp);
735                         /*
736                          * Give running I/O a grace period in which it
737                          * can successfully terminate, even using the
738                          * just varied off path. Then kill it.
739                          */
740                         if (!__check_for_io_and_kill(sch, chp) && !sch->lpm)
741                                 /* Get over with it now. */
742                                 device_call_nopath_notify(sch);
743                         else if (sch->driver && sch->driver->verify)
744                                 sch->driver->verify(&sch->dev);
745                 }
746                 break;
747         }
748 }
749
750 static int
751 s390_subchannel_vary_chpid_off(struct device *dev, void *data)
752 {
753         struct subchannel *sch;
754         __u8 *chpid;
755
756         sch = to_subchannel(dev);
757         chpid = data;
758
759         __s390_subchannel_vary_chpid(sch, *chpid, 0);
760         return 0;
761 }
762
763 static int
764 s390_subchannel_vary_chpid_on(struct device *dev, void *data)
765 {
766         struct subchannel *sch;
767         __u8 *chpid;
768
769         sch = to_subchannel(dev);
770         chpid = data;
771
772         __s390_subchannel_vary_chpid(sch, *chpid, 1);
773         return 0;
774 }
775
776 extern void css_trigger_slow_path(void);
777 typedef void (*workfunc)(void *);
778 static DECLARE_WORK(varyonoff_work, (workfunc)css_trigger_slow_path,
779                     NULL);
780
781 /*
782  * Function: s390_vary_chpid
783  * Varies the specified chpid online or offline
784  */
785 static int
786 s390_vary_chpid( __u8 chpid, int on)
787 {
788         char dbf_text[15];
789         int status, irq, ret;
790         struct subchannel *sch;
791
792         sprintf(dbf_text, on?"varyon%x":"varyoff%x", chpid);
793         CIO_TRACE_EVENT( 2, dbf_text);
794
795         status = get_chp_status(chpid);
796         if (status < 0) {
797                 printk(KERN_ERR "Can't vary unknown chpid %02X\n", chpid);
798                 return -EINVAL;
799         }
800
801         if (!on && !status) {
802                 printk(KERN_ERR "chpid %x is already offline\n", chpid);
803                 return -EINVAL;
804         }
805
806         set_chp_logically_online(chpid, on);
807
808         /*
809          * Redo PathVerification on the devices the chpid connects to
810          */
811
812         bus_for_each_dev(&css_bus_type, NULL, &chpid, on ?
813                          s390_subchannel_vary_chpid_on :
814                          s390_subchannel_vary_chpid_off);
815         if (!on)
816                 return 0;
817         /* Scan for new devices on varied on path. */
818         for (irq = 0; irq < __MAX_SUBCHANNELS; irq++) {
819                 struct schib schib;
820
821                 if (need_rescan)
822                         break;
823                 sch = get_subchannel_by_schid(irq);
824                 if (sch) {
825                         put_device(&sch->dev);
826                         continue;
827                 }
828                 if (stsch(irq, &schib))
829                         /* We're through */
830                         break;
831                 /* Put it on the slow path. */
832                 ret = css_enqueue_subchannel_slow(irq);
833                 if (ret) {
834                         css_clear_subchannel_slow_list();
835                         need_rescan = 1;
836                 }
837         }
838         if (need_rescan || css_slow_subchannels_exist())
839                 schedule_work(&varyonoff_work);
840         return 0;
841 }
842
843 /*
844  * Files for the channel path entries.
845  */
846 static ssize_t
847 chp_status_show(struct device *dev, char *buf)
848 {
849         struct channel_path *chp = container_of(dev, struct channel_path, dev);
850
851         if (!chp)
852                 return 0;
853         return (get_chp_status(chp->id) ? sprintf(buf, "online\n") :
854                 sprintf(buf, "offline\n"));
855 }
856
857 static ssize_t
858 chp_status_write(struct device *dev, const char *buf, size_t count)
859 {
860         struct channel_path *cp = container_of(dev, struct channel_path, dev);
861         char cmd[10];
862         int num_args;
863         int error;
864
865         num_args = sscanf(buf, "%5s", cmd);
866         if (!num_args)
867                 return count;
868
869         if (!strnicmp(cmd, "on", 2))
870                 error = s390_vary_chpid(cp->id, 1);
871         else if (!strnicmp(cmd, "off", 3))
872                 error = s390_vary_chpid(cp->id, 0);
873         else
874                 error = -EINVAL;
875
876         return error < 0 ? error : count;
877
878 }
879
880 static DEVICE_ATTR(status, 0644, chp_status_show, chp_status_write);
881
882
883 static void
884 chp_release(struct device *dev)
885 {
886         struct channel_path *cp;
887         
888         cp = container_of(dev, struct channel_path, dev);
889         kfree(cp);
890 }
891
892 /*
893  * Entries for chpids on the system bus.
894  * This replaces /proc/chpids.
895  */
896 static int
897 new_channel_path(int chpid)
898 {
899         struct channel_path *chp;
900         int ret;
901
902         chp = kmalloc(sizeof(struct channel_path), GFP_KERNEL);
903         if (!chp)
904                 return -ENOMEM;
905         memset(chp, 0, sizeof(struct channel_path));
906
907         chps[chpid] = chp;
908
909         /* fill in status, etc. */
910         chp->id = chpid;
911         chp->state = 1;
912         chp->dev = (struct device) {
913                 .parent  = &css_bus_device,
914                 .release = chp_release,
915         };
916         snprintf(chp->dev.bus_id, BUS_ID_SIZE, "chp0.%x", chpid);
917
918         /* make it known to the system */
919         ret = device_register(&chp->dev);
920         if (ret) {
921                 printk(KERN_WARNING "%s: could not register %02x\n",
922                        __func__, chpid);
923                 return ret;
924         }
925         ret = device_create_file(&chp->dev, &dev_attr_status);
926         if (ret)
927                 device_unregister(&chp->dev);
928
929         return ret;
930 }
931
932 static int __init
933 chsc_alloc_sei_area(void)
934 {
935         sei_page = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
936         if (!sei_page)
937                 printk(KERN_WARNING"Can't allocate page for processing of " \
938                        "chsc machine checks!\n");
939         return (sei_page ? 0 : -ENOMEM);
940 }
941
942 subsys_initcall(chsc_alloc_sei_area);