[PATCH] s390: introduce for_each_subchannel
[firefly-linux-kernel-4.4.55.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.120 $
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 static struct channel_path *chps[NR_CHPIDS];
28
29 static void *sei_page;
30
31 static int new_channel_path(int chpid);
32
33 static inline void
34 set_chp_logically_online(int chp, int onoff)
35 {
36         chps[chp]->state = onoff;
37 }
38
39 static int
40 get_chp_status(int chp)
41 {
42         return (chps[chp] ? chps[chp]->state : -ENODEV);
43 }
44
45 void
46 chsc_validate_chpids(struct subchannel *sch)
47 {
48         int mask, chp;
49
50         for (chp = 0; chp <= 7; chp++) {
51                 mask = 0x80 >> chp;
52                 if (!get_chp_status(sch->schib.pmcw.chpid[chp]))
53                         /* disable using this path */
54                         sch->opm &= ~mask;
55         }
56 }
57
58 void
59 chpid_is_actually_online(int chp)
60 {
61         int state;
62
63         state = get_chp_status(chp);
64         if (state < 0) {
65                 need_rescan = 1;
66                 queue_work(slow_path_wq, &slow_path_work);
67         } else
68                 WARN_ON(!state);
69 }
70
71 /* FIXME: this is _always_ called for every subchannel. shouldn't we
72  *        process more than one at a time? */
73 static int
74 chsc_get_sch_desc_irq(struct subchannel *sch, void *page)
75 {
76         int ccode, j;
77
78         struct {
79                 struct chsc_header request;
80                 u16 reserved1;
81                 u16 f_sch;        /* first subchannel */
82                 u16 reserved2;
83                 u16 l_sch;        /* last subchannel */
84                 u32 reserved3;
85                 struct chsc_header response;
86                 u32 reserved4;
87                 u8 sch_valid : 1;
88                 u8 dev_valid : 1;
89                 u8 st        : 3; /* subchannel type */
90                 u8 zeroes    : 3;
91                 u8  unit_addr;    /* unit address */
92                 u16 devno;        /* device number */
93                 u8 path_mask;
94                 u8 fla_valid_mask;
95                 u16 sch;          /* subchannel */
96                 u8 chpid[8];      /* chpids 0-7 */
97                 u16 fla[8];       /* full link addresses 0-7 */
98         } *ssd_area;
99
100         ssd_area = page;
101
102         ssd_area->request = (struct chsc_header) {
103                 .length = 0x0010,
104                 .code   = 0x0004,
105         };
106
107         ssd_area->f_sch = sch->schid.sch_no;
108         ssd_area->l_sch = sch->schid.sch_no;
109
110         ccode = chsc(ssd_area);
111         if (ccode > 0) {
112                 pr_debug("chsc returned with ccode = %d\n", ccode);
113                 return (ccode == 3) ? -ENODEV : -EBUSY;
114         }
115
116         switch (ssd_area->response.code) {
117         case 0x0001: /* everything ok */
118                 break;
119         case 0x0002:
120                 CIO_CRW_EVENT(2, "Invalid command!\n");
121                 return -EINVAL;
122         case 0x0003:
123                 CIO_CRW_EVENT(2, "Error in chsc request block!\n");
124                 return -EINVAL;
125         case 0x0004:
126                 CIO_CRW_EVENT(2, "Model does not provide ssd\n");
127                 return -EOPNOTSUPP;
128         default:
129                 CIO_CRW_EVENT(2, "Unknown CHSC response %d\n",
130                               ssd_area->response.code);
131                 return -EIO;
132         }
133
134         /*
135          * ssd_area->st stores the type of the detected
136          * subchannel, with the following definitions:
137          *
138          * 0: I/O subchannel:     All fields have meaning
139          * 1: CHSC subchannel:    Only sch_val, st and sch
140          *                        have meaning
141          * 2: Message subchannel: All fields except unit_addr
142          *                        have meaning
143          * 3: ADM subchannel:     Only sch_val, st and sch
144          *                        have meaning
145          *
146          * Other types are currently undefined.
147          */
148         if (ssd_area->st > 3) { /* uhm, that looks strange... */
149                 CIO_CRW_EVENT(0, "Strange subchannel type %d"
150                               " for sch %04x\n", ssd_area->st,
151                               sch->schid.sch_no);
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->schid.sch_no, 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->schid, &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         if (css_enqueue_subchannel_slow(sch->schid)) {
289                 css_clear_subchannel_slow_list();
290                 need_rescan = 1;
291         }
292         return 0;
293 }
294
295 static inline void
296 s390_set_chpid_offline( __u8 chpid)
297 {
298         char dbf_txt[15];
299
300         sprintf(dbf_txt, "chpr%x", chpid);
301         CIO_TRACE_EVENT(2, dbf_txt);
302
303         if (get_chp_status(chpid) <= 0)
304                 return;
305
306         bus_for_each_dev(&css_bus_type, NULL, &chpid,
307                          s390_subchannel_remove_chpid);
308
309         if (need_rescan || css_slow_subchannels_exist())
310                 queue_work(slow_path_wq, &slow_path_work);
311 }
312
313 struct res_acc_data {
314         struct channel_path *chp;
315         u32 fla_mask;
316         u16 fla;
317 };
318
319 static int
320 s390_process_res_acc_sch(struct res_acc_data *res_data, struct subchannel *sch)
321 {
322         int found;
323         int chp;
324         int ccode;
325         
326         found = 0;
327         for (chp = 0; chp <= 7; chp++)
328                 /*
329                  * check if chpid is in information updated by ssd
330                  */
331                 if (sch->ssd_info.valid &&
332                     sch->ssd_info.chpid[chp] == res_data->chp->id &&
333                     (sch->ssd_info.fla[chp] & res_data->fla_mask)
334                     == res_data->fla) {
335                         found = 1;
336                         break;
337                 }
338         
339         if (found == 0)
340                 return 0;
341
342         /*
343          * Do a stsch to update our subchannel structure with the
344          * new path information and eventually check for logically
345          * offline chpids.
346          */
347         ccode = stsch(sch->schid, &sch->schib);
348         if (ccode > 0)
349                 return 0;
350
351         return 0x80 >> chp;
352 }
353
354 static inline int
355 s390_process_res_acc_new_sch(struct subchannel_id schid)
356 {
357         struct schib schib;
358         int ret;
359         /*
360          * We don't know the device yet, but since a path
361          * may be available now to the device we'll have
362          * to do recognition again.
363          * Since we don't have any idea about which chpid
364          * that beast may be on we'll have to do a stsch
365          * on all devices, grr...
366          */
367         if (stsch(schid, &schib))
368                 /* We're through */
369                 return need_rescan ? -EAGAIN : -ENXIO;
370
371         /* Put it on the slow path. */
372         ret = css_enqueue_subchannel_slow(schid);
373         if (ret) {
374                 css_clear_subchannel_slow_list();
375                 need_rescan = 1;
376                 return -EAGAIN;
377         }
378         return 0;
379 }
380
381 static int
382 __s390_process_res_acc(struct subchannel_id schid, void *data)
383 {
384         int chp_mask, old_lpm;
385         struct res_acc_data *res_data;
386         struct subchannel *sch;
387
388         res_data = (struct res_acc_data *)data;
389         sch = get_subchannel_by_schid(schid);
390         if (!sch)
391                 /* Check if a subchannel is newly available. */
392                 return s390_process_res_acc_new_sch(schid);
393
394         spin_lock_irq(&sch->lock);
395
396         chp_mask = s390_process_res_acc_sch(res_data, sch);
397
398         if (chp_mask == 0) {
399                 spin_unlock_irq(&sch->lock);
400                 return 0;
401         }
402         old_lpm = sch->lpm;
403         sch->lpm = ((sch->schib.pmcw.pim &
404                      sch->schib.pmcw.pam &
405                      sch->schib.pmcw.pom)
406                     | chp_mask) & sch->opm;
407         if (!old_lpm && sch->lpm)
408                 device_trigger_reprobe(sch);
409         else if (sch->driver && sch->driver->verify)
410                 sch->driver->verify(&sch->dev);
411
412         spin_unlock_irq(&sch->lock);
413         put_device(&sch->dev);
414         return (res_data->fla_mask == 0xffff) ? -ENODEV : 0;
415 }
416
417
418 static int
419 s390_process_res_acc (struct res_acc_data *res_data)
420 {
421         int rc;
422         char dbf_txt[15];
423
424         sprintf(dbf_txt, "accpr%x", res_data->chp->id);
425         CIO_TRACE_EVENT( 2, dbf_txt);
426         if (res_data->fla != 0) {
427                 sprintf(dbf_txt, "fla%x", res_data->fla);
428                 CIO_TRACE_EVENT( 2, dbf_txt);
429         }
430
431         /*
432          * I/O resources may have become accessible.
433          * Scan through all subchannels that may be concerned and
434          * do a validation on those.
435          * The more information we have (info), the less scanning
436          * will we have to do.
437          */
438         rc = for_each_subchannel(__s390_process_res_acc, res_data);
439         if (css_slow_subchannels_exist())
440                 rc = -EAGAIN;
441         else if (rc != -EAGAIN)
442                 rc = 0;
443         return rc;
444 }
445
446 static int
447 __get_chpid_from_lir(void *data)
448 {
449         struct lir {
450                 u8  iq;
451                 u8  ic;
452                 u16 sci;
453                 /* incident-node descriptor */
454                 u32 indesc[28];
455                 /* attached-node descriptor */
456                 u32 andesc[28];
457                 /* incident-specific information */
458                 u32 isinfo[28];
459         } *lir;
460
461         lir = (struct lir*) data;
462         if (!(lir->iq&0x80))
463                 /* NULL link incident record */
464                 return -EINVAL;
465         if (!(lir->indesc[0]&0xc0000000))
466                 /* node descriptor not valid */
467                 return -EINVAL;
468         if (!(lir->indesc[0]&0x10000000))
469                 /* don't handle device-type nodes - FIXME */
470                 return -EINVAL;
471         /* Byte 3 contains the chpid. Could also be CTCA, but we don't care */
472
473         return (u16) (lir->indesc[0]&0x000000ff);
474 }
475
476 int
477 chsc_process_crw(void)
478 {
479         int chpid, ret;
480         struct res_acc_data res_data;
481         struct {
482                 struct chsc_header request;
483                 u32 reserved1;
484                 u32 reserved2;
485                 u32 reserved3;
486                 struct chsc_header response;
487                 u32 reserved4;
488                 u8  flags;
489                 u8  vf;         /* validity flags */
490                 u8  rs;         /* reporting source */
491                 u8  cc;         /* content code */
492                 u16 fla;        /* full link address */
493                 u16 rsid;       /* reporting source id */
494                 u32 reserved5;
495                 u32 reserved6;
496                 u32 ccdf[96];   /* content-code dependent field */
497                 /* ccdf has to be big enough for a link-incident record */
498         } *sei_area;
499
500         if (!sei_page)
501                 return 0;
502         /*
503          * build the chsc request block for store event information
504          * and do the call
505          * This function is only called by the machine check handler thread,
506          * so we don't need locking for the sei_page.
507          */
508         sei_area = sei_page;
509
510         CIO_TRACE_EVENT( 2, "prcss");
511         ret = 0;
512         do {
513                 int ccode, status;
514                 memset(sei_area, 0, sizeof(*sei_area));
515                 memset(&res_data, 0, sizeof(struct res_acc_data));
516                 sei_area->request = (struct chsc_header) {
517                         .length = 0x0010,
518                         .code   = 0x000e,
519                 };
520
521                 ccode = chsc(sei_area);
522                 if (ccode > 0)
523                         return 0;
524
525                 switch (sei_area->response.code) {
526                         /* for debug purposes, check for problems */
527                 case 0x0001:
528                         CIO_CRW_EVENT(4, "chsc_process_crw: event information "
529                                         "successfully stored\n");
530                         break; /* everything ok */
531                 case 0x0002:
532                         CIO_CRW_EVENT(2,
533                                       "chsc_process_crw: invalid command!\n");
534                         return 0;
535                 case 0x0003:
536                         CIO_CRW_EVENT(2, "chsc_process_crw: error in chsc "
537                                       "request block!\n");
538                         return 0;
539                 case 0x0005:
540                         CIO_CRW_EVENT(2, "chsc_process_crw: no event "
541                                       "information stored\n");
542                         return 0;
543                 default:
544                         CIO_CRW_EVENT(2, "chsc_process_crw: chsc response %d\n",
545                                       sei_area->response.code);
546                         return 0;
547                 }
548
549                 /* Check if we might have lost some information. */
550                 if (sei_area->flags & 0x40)
551                         CIO_CRW_EVENT(2, "chsc_process_crw: Event information "
552                                        "has been lost due to overflow!\n");
553
554                 if (sei_area->rs != 4) {
555                         CIO_CRW_EVENT(2, "chsc_process_crw: reporting source "
556                                       "(%04X) isn't a chpid!\n",
557                                       sei_area->rsid);
558                         continue;
559                 }
560
561                 /* which kind of information was stored? */
562                 switch (sei_area->cc) {
563                 case 1: /* link incident*/
564                         CIO_CRW_EVENT(4, "chsc_process_crw: "
565                                       "channel subsystem reports link incident,"
566                                       " reporting source is chpid %x\n",
567                                       sei_area->rsid);
568                         chpid = __get_chpid_from_lir(sei_area->ccdf);
569                         if (chpid < 0)
570                                 CIO_CRW_EVENT(4, "%s: Invalid LIR, skipping\n",
571                                               __FUNCTION__);
572                         else
573                                 s390_set_chpid_offline(chpid);
574                         break;
575                         
576                 case 2: /* i/o resource accessibiliy */
577                         CIO_CRW_EVENT(4, "chsc_process_crw: "
578                                       "channel subsystem reports some I/O "
579                                       "devices may have become accessible\n");
580                         pr_debug("Data received after sei: \n");
581                         pr_debug("Validity flags: %x\n", sei_area->vf);
582                         
583                         /* allocate a new channel path structure, if needed */
584                         status = get_chp_status(sei_area->rsid);
585                         if (status < 0)
586                                 new_channel_path(sei_area->rsid);
587                         else if (!status)
588                                 break;
589                         res_data.chp = chps[sei_area->rsid];
590                         pr_debug("chpid: %x", sei_area->rsid);
591                         if ((sei_area->vf & 0xc0) != 0) {
592                                 res_data.fla = sei_area->fla;
593                                 if ((sei_area->vf & 0xc0) == 0xc0) {
594                                         pr_debug(" full link addr: %x",
595                                                  sei_area->fla);
596                                         res_data.fla_mask = 0xffff;
597                                 } else {
598                                         pr_debug(" link addr: %x",
599                                                  sei_area->fla);
600                                         res_data.fla_mask = 0xff00;
601                                 }
602                         }
603                         ret = s390_process_res_acc(&res_data);
604                         pr_debug("\n\n");
605                         break;
606                         
607                 default: /* other stuff */
608                         CIO_CRW_EVENT(4, "chsc_process_crw: event %d\n",
609                                       sei_area->cc);
610                         break;
611                 }
612         } while (sei_area->flags & 0x80);
613         return ret;
614 }
615
616 static inline int
617 __chp_add_new_sch(struct subchannel_id schid)
618 {
619         struct schib schib;
620         int ret;
621
622         if (stsch(schid, &schib))
623                 /* We're through */
624                 return need_rescan ? -EAGAIN : -ENXIO;
625
626         /* Put it on the slow path. */
627         ret = css_enqueue_subchannel_slow(schid);
628         if (ret) {
629                 css_clear_subchannel_slow_list();
630                 need_rescan = 1;
631                 return -EAGAIN;
632         }
633         return 0;
634 }
635
636
637 static int
638 __chp_add(struct subchannel_id schid, void *data)
639 {
640         int i;
641         struct channel_path *chp;
642         struct subchannel *sch;
643
644         chp = (struct channel_path *)data;
645         sch = get_subchannel_by_schid(schid);
646         if (!sch)
647                 /* Check if the subchannel is now available. */
648                 return __chp_add_new_sch(schid);
649         spin_lock(&sch->lock);
650         for (i=0; i<8; i++)
651                 if (sch->schib.pmcw.chpid[i] == chp->id) {
652                         if (stsch(sch->schid, &sch->schib) != 0) {
653                                 /* Endgame. */
654                                 spin_unlock(&sch->lock);
655                                 return -ENXIO;
656                         }
657                         break;
658                 }
659         if (i==8) {
660                 spin_unlock(&sch->lock);
661                 return 0;
662         }
663         sch->lpm = ((sch->schib.pmcw.pim &
664                      sch->schib.pmcw.pam &
665                      sch->schib.pmcw.pom)
666                     | 0x80 >> i) & sch->opm;
667
668         if (sch->driver && sch->driver->verify)
669                 sch->driver->verify(&sch->dev);
670
671         spin_unlock(&sch->lock);
672         put_device(&sch->dev);
673         return 0;
674 }
675
676 static int
677 chp_add(int chpid)
678 {
679         int rc;
680         char dbf_txt[15];
681
682         if (!get_chp_status(chpid))
683                 return 0; /* no need to do the rest */
684         
685         sprintf(dbf_txt, "cadd%x", chpid);
686         CIO_TRACE_EVENT(2, dbf_txt);
687
688         rc = for_each_subchannel(__chp_add, chps[chpid]);
689         if (css_slow_subchannels_exist())
690                 rc = -EAGAIN;
691         if (rc != -EAGAIN)
692                 rc = 0;
693         return rc;
694 }
695
696 /* 
697  * Handling of crw machine checks with channel path source.
698  */
699 int
700 chp_process_crw(int chpid, int on)
701 {
702         if (on == 0) {
703                 /* Path has gone. We use the link incident routine.*/
704                 s390_set_chpid_offline(chpid);
705                 return 0; /* De-register is async anyway. */
706         }
707         /*
708          * Path has come. Allocate a new channel path structure,
709          * if needed.
710          */
711         if (get_chp_status(chpid) < 0)
712                 new_channel_path(chpid);
713         /* Avoid the extra overhead in process_rec_acc. */
714         return chp_add(chpid);
715 }
716
717 static inline int
718 __check_for_io_and_kill(struct subchannel *sch, int index)
719 {
720         int cc;
721
722         if (!device_is_online(sch))
723                 /* cio could be doing I/O. */
724                 return 0;
725         cc = stsch(sch->schid, &sch->schib);
726         if (cc)
727                 return 0;
728         if (sch->schib.scsw.actl && sch->schib.pmcw.lpum == (0x80 >> index)) {
729                 device_set_waiting(sch);
730                 return 1;
731         }
732         return 0;
733 }
734
735 static inline void
736 __s390_subchannel_vary_chpid(struct subchannel *sch, __u8 chpid, int on)
737 {
738         int chp, old_lpm;
739         unsigned long flags;
740
741         if (!sch->ssd_info.valid)
742                 return;
743         
744         spin_lock_irqsave(&sch->lock, flags);
745         old_lpm = sch->lpm;
746         for (chp = 0; chp < 8; chp++) {
747                 if (sch->ssd_info.chpid[chp] != chpid)
748                         continue;
749
750                 if (on) {
751                         sch->opm |= (0x80 >> chp);
752                         sch->lpm |= (0x80 >> chp);
753                         if (!old_lpm)
754                                 device_trigger_reprobe(sch);
755                         else if (sch->driver && sch->driver->verify)
756                                 sch->driver->verify(&sch->dev);
757                 } else {
758                         sch->opm &= ~(0x80 >> chp);
759                         sch->lpm &= ~(0x80 >> chp);
760                         /*
761                          * Give running I/O a grace period in which it
762                          * can successfully terminate, even using the
763                          * just varied off path. Then kill it.
764                          */
765                         if (!__check_for_io_and_kill(sch, chp) && !sch->lpm) {
766                                 if (css_enqueue_subchannel_slow(sch->schid)) {
767                                         css_clear_subchannel_slow_list();
768                                         need_rescan = 1;
769                                 }
770                         } else if (sch->driver && sch->driver->verify)
771                                 sch->driver->verify(&sch->dev);
772                 }
773                 break;
774         }
775         spin_unlock_irqrestore(&sch->lock, flags);
776 }
777
778 static int
779 s390_subchannel_vary_chpid_off(struct device *dev, void *data)
780 {
781         struct subchannel *sch;
782         __u8 *chpid;
783
784         sch = to_subchannel(dev);
785         chpid = data;
786
787         __s390_subchannel_vary_chpid(sch, *chpid, 0);
788         return 0;
789 }
790
791 static int
792 s390_subchannel_vary_chpid_on(struct device *dev, void *data)
793 {
794         struct subchannel *sch;
795         __u8 *chpid;
796
797         sch = to_subchannel(dev);
798         chpid = data;
799
800         __s390_subchannel_vary_chpid(sch, *chpid, 1);
801         return 0;
802 }
803
804 static int
805 __s390_vary_chpid_on(struct subchannel_id schid, void *data)
806 {
807         struct schib schib;
808         struct subchannel *sch;
809
810         sch = get_subchannel_by_schid(schid);
811         if (sch) {
812                 put_device(&sch->dev);
813                 return 0;
814         }
815         if (stsch(schid, &schib))
816                 /* We're through */
817                 return -ENXIO;
818         /* Put it on the slow path. */
819         if (css_enqueue_subchannel_slow(schid)) {
820                 css_clear_subchannel_slow_list();
821                 need_rescan = 1;
822                 return -EAGAIN;
823         }
824         return 0;
825 }
826
827 /*
828  * Function: s390_vary_chpid
829  * Varies the specified chpid online or offline
830  */
831 static int
832 s390_vary_chpid( __u8 chpid, int on)
833 {
834         char dbf_text[15];
835         int status;
836
837         sprintf(dbf_text, on?"varyon%x":"varyoff%x", chpid);
838         CIO_TRACE_EVENT( 2, dbf_text);
839
840         status = get_chp_status(chpid);
841         if (status < 0) {
842                 printk(KERN_ERR "Can't vary unknown chpid %02X\n", chpid);
843                 return -EINVAL;
844         }
845
846         if (!on && !status) {
847                 printk(KERN_ERR "chpid %x is already offline\n", chpid);
848                 return -EINVAL;
849         }
850
851         set_chp_logically_online(chpid, on);
852
853         /*
854          * Redo PathVerification on the devices the chpid connects to
855          */
856
857         bus_for_each_dev(&css_bus_type, NULL, &chpid, on ?
858                          s390_subchannel_vary_chpid_on :
859                          s390_subchannel_vary_chpid_off);
860         if (on)
861                 /* Scan for new devices on varied on path. */
862                 for_each_subchannel(__s390_vary_chpid_on, NULL);
863         if (need_rescan || css_slow_subchannels_exist())
864                 queue_work(slow_path_wq, &slow_path_work);
865         return 0;
866 }
867
868 /*
869  * Files for the channel path entries.
870  */
871 static ssize_t
872 chp_status_show(struct device *dev, struct device_attribute *attr, char *buf)
873 {
874         struct channel_path *chp = container_of(dev, struct channel_path, dev);
875
876         if (!chp)
877                 return 0;
878         return (get_chp_status(chp->id) ? sprintf(buf, "online\n") :
879                 sprintf(buf, "offline\n"));
880 }
881
882 static ssize_t
883 chp_status_write(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
884 {
885         struct channel_path *cp = container_of(dev, struct channel_path, dev);
886         char cmd[10];
887         int num_args;
888         int error;
889
890         num_args = sscanf(buf, "%5s", cmd);
891         if (!num_args)
892                 return count;
893
894         if (!strnicmp(cmd, "on", 2))
895                 error = s390_vary_chpid(cp->id, 1);
896         else if (!strnicmp(cmd, "off", 3))
897                 error = s390_vary_chpid(cp->id, 0);
898         else
899                 error = -EINVAL;
900
901         return error < 0 ? error : count;
902
903 }
904
905 static DEVICE_ATTR(status, 0644, chp_status_show, chp_status_write);
906
907 static ssize_t
908 chp_type_show(struct device *dev, struct device_attribute *attr, char *buf)
909 {
910         struct channel_path *chp = container_of(dev, struct channel_path, dev);
911
912         if (!chp)
913                 return 0;
914         return sprintf(buf, "%x\n", chp->desc.desc);
915 }
916
917 static DEVICE_ATTR(type, 0444, chp_type_show, NULL);
918
919 static struct attribute * chp_attrs[] = {
920         &dev_attr_status.attr,
921         &dev_attr_type.attr,
922         NULL,
923 };
924
925 static struct attribute_group chp_attr_group = {
926         .attrs = chp_attrs,
927 };
928
929 static void
930 chp_release(struct device *dev)
931 {
932         struct channel_path *cp;
933         
934         cp = container_of(dev, struct channel_path, dev);
935         kfree(cp);
936 }
937
938 static int
939 chsc_determine_channel_path_description(int chpid,
940                                         struct channel_path_desc *desc)
941 {
942         int ccode, ret;
943
944         struct {
945                 struct chsc_header request;
946                 u32 : 24;
947                 u32 first_chpid : 8;
948                 u32 : 24;
949                 u32 last_chpid : 8;
950                 u32 zeroes1;
951                 struct chsc_header response;
952                 u32 zeroes2;
953                 struct channel_path_desc desc;
954         } *scpd_area;
955
956         scpd_area = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
957         if (!scpd_area)
958                 return -ENOMEM;
959
960         scpd_area->request = (struct chsc_header) {
961                 .length = 0x0010,
962                 .code   = 0x0002,
963         };
964
965         scpd_area->first_chpid = chpid;
966         scpd_area->last_chpid = chpid;
967
968         ccode = chsc(scpd_area);
969         if (ccode > 0) {
970                 ret = (ccode == 3) ? -ENODEV : -EBUSY;
971                 goto out;
972         }
973
974         switch (scpd_area->response.code) {
975         case 0x0001: /* Success. */
976                 memcpy(desc, &scpd_area->desc,
977                        sizeof(struct channel_path_desc));
978                 ret = 0;
979                 break;
980         case 0x0003: /* Invalid block. */
981         case 0x0007: /* Invalid format. */
982         case 0x0008: /* Other invalid block. */
983                 CIO_CRW_EVENT(2, "Error in chsc request block!\n");
984                 ret = -EINVAL;
985                 break;
986         case 0x0004: /* Command not provided in model. */
987                 CIO_CRW_EVENT(2, "Model does not provide scpd\n");
988                 ret = -EOPNOTSUPP;
989                 break;
990         default:
991                 CIO_CRW_EVENT(2, "Unknown CHSC response %d\n",
992                               scpd_area->response.code);
993                 ret = -EIO;
994         }
995 out:
996         free_page((unsigned long)scpd_area);
997         return ret;
998 }
999
1000 /*
1001  * Entries for chpids on the system bus.
1002  * This replaces /proc/chpids.
1003  */
1004 static int
1005 new_channel_path(int chpid)
1006 {
1007         struct channel_path *chp;
1008         int ret;
1009
1010         chp = kmalloc(sizeof(struct channel_path), GFP_KERNEL);
1011         if (!chp)
1012                 return -ENOMEM;
1013         memset(chp, 0, sizeof(struct channel_path));
1014
1015         /* fill in status, etc. */
1016         chp->id = chpid;
1017         chp->state = 1;
1018         chp->dev = (struct device) {
1019                 .parent  = &css_bus_device,
1020                 .release = chp_release,
1021         };
1022         snprintf(chp->dev.bus_id, BUS_ID_SIZE, "chp0.%x", chpid);
1023
1024         /* Obtain channel path description and fill it in. */
1025         ret = chsc_determine_channel_path_description(chpid, &chp->desc);
1026         if (ret)
1027                 goto out_free;
1028
1029         /* make it known to the system */
1030         ret = device_register(&chp->dev);
1031         if (ret) {
1032                 printk(KERN_WARNING "%s: could not register %02x\n",
1033                        __func__, chpid);
1034                 goto out_free;
1035         }
1036         ret = sysfs_create_group(&chp->dev.kobj, &chp_attr_group);
1037         if (ret) {
1038                 device_unregister(&chp->dev);
1039                 goto out_free;
1040         } else
1041                 chps[chpid] = chp;
1042         return ret;
1043 out_free:
1044         kfree(chp);
1045         return ret;
1046 }
1047
1048 void *
1049 chsc_get_chp_desc(struct subchannel *sch, int chp_no)
1050 {
1051         struct channel_path *chp;
1052         struct channel_path_desc *desc;
1053
1054         chp = chps[sch->schib.pmcw.chpid[chp_no]];
1055         if (!chp)
1056                 return NULL;
1057         desc = kmalloc(sizeof(struct channel_path_desc), GFP_KERNEL);
1058         if (!desc)
1059                 return NULL;
1060         memcpy(desc, &chp->desc, sizeof(struct channel_path_desc));
1061         return desc;
1062 }
1063
1064
1065 static int __init
1066 chsc_alloc_sei_area(void)
1067 {
1068         sei_page = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
1069         if (!sei_page)
1070                 printk(KERN_WARNING"Can't allocate page for processing of " \
1071                        "chsc machine checks!\n");
1072         return (sei_page ? 0 : -ENOMEM);
1073 }
1074
1075 subsys_initcall(chsc_alloc_sei_area);
1076
1077 struct css_general_char css_general_characteristics;
1078 struct css_chsc_char css_chsc_characteristics;
1079
1080 int __init
1081 chsc_determine_css_characteristics(void)
1082 {
1083         int result;
1084         struct {
1085                 struct chsc_header request;
1086                 u32 reserved1;
1087                 u32 reserved2;
1088                 u32 reserved3;
1089                 struct chsc_header response;
1090                 u32 reserved4;
1091                 u32 general_char[510];
1092                 u32 chsc_char[518];
1093         } *scsc_area;
1094
1095         scsc_area = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
1096         if (!scsc_area) {
1097                 printk(KERN_WARNING"cio: Was not able to determine available" \
1098                        "CHSCs due to no memory.\n");
1099                 return -ENOMEM;
1100         }
1101
1102         scsc_area->request = (struct chsc_header) {
1103                 .length = 0x0010,
1104                 .code   = 0x0010,
1105         };
1106
1107         result = chsc(scsc_area);
1108         if (result) {
1109                 printk(KERN_WARNING"cio: Was not able to determine " \
1110                        "available CHSCs, cc=%i.\n", result);
1111                 result = -EIO;
1112                 goto exit;
1113         }
1114
1115         if (scsc_area->response.code != 1) {
1116                 printk(KERN_WARNING"cio: Was not able to determine " \
1117                        "available CHSCs.\n");
1118                 result = -EIO;
1119                 goto exit;
1120         }
1121         memcpy(&css_general_characteristics, scsc_area->general_char,
1122                sizeof(css_general_characteristics));
1123         memcpy(&css_chsc_characteristics, scsc_area->chsc_char,
1124                sizeof(css_chsc_characteristics));
1125 exit:
1126         free_page ((unsigned long) scsc_area);
1127         return result;
1128 }
1129
1130 EXPORT_SYMBOL_GPL(css_general_characteristics);
1131 EXPORT_SYMBOL_GPL(css_chsc_characteristics);