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