2 * drivers/s390/cio/chsc.c
3 * S/390 common I/O routines -- channel subsystem call
6 * Copyright (C) 1999-2002 IBM Deutschland Entwicklung GmbH,
8 * Author(s): Ingo Adlung (adlung@de.ibm.com)
9 * Cornelia Huck (cohuck@de.ibm.com)
10 * Arnd Bergmann (arndb@de.ibm.com)
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>
23 #include "cio_debug.h"
27 static struct channel_path *chps[NR_CHPIDS];
29 static void *sei_page;
31 static int new_channel_path(int chpid);
34 set_chp_logically_online(int chp, int onoff)
36 chps[chp]->state = onoff;
40 get_chp_status(int chp)
42 return (chps[chp] ? chps[chp]->state : -ENODEV);
46 chsc_validate_chpids(struct subchannel *sch)
50 for (chp = 0; chp <= 7; chp++) {
52 if (!get_chp_status(sch->schib.pmcw.chpid[chp]))
53 /* disable using this path */
59 chpid_is_actually_online(int chp)
63 state = get_chp_status(chp);
66 queue_work(slow_path_wq, &slow_path_work);
71 /* FIXME: this is _always_ called for every subchannel. shouldn't we
72 * process more than one at a time? */
74 chsc_get_sch_desc_irq(struct subchannel *sch, void *page)
79 struct chsc_header request;
81 u16 f_sch; /* first subchannel */
83 u16 l_sch; /* last subchannel */
85 struct chsc_header response;
89 u8 st : 3; /* subchannel type */
91 u8 unit_addr; /* unit address */
92 u16 devno; /* device number */
95 u16 sch; /* subchannel */
96 u8 chpid[8]; /* chpids 0-7 */
97 u16 fla[8]; /* full link addresses 0-7 */
102 ssd_area->request = (struct chsc_header) {
107 ssd_area->f_sch = sch->irq;
108 ssd_area->l_sch = sch->irq;
110 ccode = chsc(ssd_area);
112 pr_debug("chsc returned with ccode = %d\n", ccode);
113 return (ccode == 3) ? -ENODEV : -EBUSY;
116 switch (ssd_area->response.code) {
117 case 0x0001: /* everything ok */
120 CIO_CRW_EVENT(2, "Invalid command!\n");
123 CIO_CRW_EVENT(2, "Error in chsc request block!\n");
126 CIO_CRW_EVENT(2, "Model does not provide ssd\n");
129 CIO_CRW_EVENT(2, "Unknown CHSC response %d\n",
130 ssd_area->response.code);
135 * ssd_area->st stores the type of the detected
136 * subchannel, with the following definitions:
138 * 0: I/O subchannel: All fields have meaning
139 * 1: CHSC subchannel: Only sch_val, st and sch
141 * 2: Message subchannel: All fields except unit_addr
143 * 3: ADM subchannel: Only sch_val, st and sch
146 * Other types are currently undefined.
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, sch->irq);
152 * There may have been a new subchannel type defined in the
153 * time since this code was written; since we don't know which
154 * fields have meaning and what to do with it we just jump out
158 const char *type[4] = {"I/O", "chsc", "message", "ADM"};
159 CIO_CRW_EVENT(6, "ssd: sch %04x is %s subchannel\n",
160 sch->irq, type[ssd_area->st]);
162 sch->ssd_info.valid = 1;
163 sch->ssd_info.type = ssd_area->st;
166 if (ssd_area->st == 0 || ssd_area->st == 2) {
167 for (j = 0; j < 8; j++) {
168 if (!((0x80 >> j) & ssd_area->path_mask &
169 ssd_area->fla_valid_mask))
171 sch->ssd_info.chpid[j] = ssd_area->chpid[j];
172 sch->ssd_info.fla[j] = ssd_area->fla[j];
179 css_get_ssd_info(struct subchannel *sch)
184 page = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
187 spin_lock_irq(&sch->lock);
188 ret = chsc_get_sch_desc_irq(sch, page);
190 static int cio_chsc_err_msg;
192 if (!cio_chsc_err_msg) {
194 "chsc_get_sch_descriptions:"
195 " Error %d while doing chsc; "
196 "processing some machine checks may "
198 cio_chsc_err_msg = 1;
201 spin_unlock_irq(&sch->lock);
202 free_page((unsigned long)page);
205 /* Allocate channel path structures, if needed. */
206 for (j = 0; j < 8; j++) {
207 chpid = sch->ssd_info.chpid[j];
208 if (chpid && (get_chp_status(chpid) < 0))
209 new_channel_path(chpid);
216 s390_subchannel_remove_chpid(struct device *dev, void *data)
220 struct subchannel *sch;
224 sch = to_subchannel(dev);
226 for (j = 0; j < 8; j++)
227 if (sch->schib.pmcw.chpid[j] == *chpid)
233 spin_lock(&sch->lock);
235 stsch(sch->irq, &schib);
238 memcpy(&sch->schib, &schib, sizeof(struct schib));
239 /* Check for single path devices. */
240 if (sch->schib.pmcw.pim == 0x80)
242 if (sch->vpm == mask)
245 if ((sch->schib.scsw.actl & (SCSW_ACTL_CLEAR_PEND |
246 SCSW_ACTL_HALT_PEND |
247 SCSW_ACTL_START_PEND |
248 SCSW_ACTL_RESUME_PEND)) &&
249 (sch->schib.pmcw.lpum == mask)) {
250 int cc = cio_cancel(sch);
260 if (sch->driver && sch->driver->termination)
261 sch->driver->termination(&sch->dev);
264 } else if ((sch->schib.scsw.actl & SCSW_ACTL_DEVACT) &&
265 (sch->schib.scsw.actl & SCSW_ACTL_SCHACT) &&
266 (sch->schib.pmcw.lpum == mask)) {
273 if (sch->driver && sch->driver->termination)
274 sch->driver->termination(&sch->dev);
278 /* trigger path verification. */
279 if (sch->driver && sch->driver->verify)
280 sch->driver->verify(&sch->dev);
282 spin_unlock(&sch->lock);
285 spin_unlock(&sch->lock);
287 if (css_enqueue_subchannel_slow(sch->irq)) {
288 css_clear_subchannel_slow_list();
295 s390_set_chpid_offline( __u8 chpid)
299 sprintf(dbf_txt, "chpr%x", chpid);
300 CIO_TRACE_EVENT(2, dbf_txt);
302 if (get_chp_status(chpid) <= 0)
305 bus_for_each_dev(&css_bus_type, NULL, &chpid,
306 s390_subchannel_remove_chpid);
308 if (need_rescan || css_slow_subchannels_exist())
309 queue_work(slow_path_wq, &slow_path_work);
313 s390_process_res_acc_sch(u8 chpid, __u16 fla, u32 fla_mask,
314 struct subchannel *sch)
321 for (chp = 0; chp <= 7; chp++)
323 * check if chpid is in information updated by ssd
325 if (sch->ssd_info.valid &&
326 sch->ssd_info.chpid[chp] == chpid &&
327 (sch->ssd_info.fla[chp] & fla_mask) == fla) {
336 * Do a stsch to update our subchannel structure with the
337 * new path information and eventually check for logically
340 ccode = stsch(sch->irq, &sch->schib);
348 s390_process_res_acc (u8 chpid, __u16 fla, u32 fla_mask)
350 struct subchannel *sch;
354 sprintf(dbf_txt, "accpr%x", chpid);
355 CIO_TRACE_EVENT( 2, dbf_txt);
357 sprintf(dbf_txt, "fla%x", fla);
358 CIO_TRACE_EVENT( 2, dbf_txt);
362 * I/O resources may have become accessible.
363 * Scan through all subchannels that may be concerned and
364 * do a validation on those.
365 * The more information we have (info), the less scanning
366 * will we have to do.
369 if (!get_chp_status(chpid))
370 return 0; /* no need to do the rest */
373 for (irq = 0; irq < __MAX_SUBCHANNELS; irq++) {
374 int chp_mask, old_lpm;
376 sch = get_subchannel_by_schid(irq);
381 * We don't know the device yet, but since a path
382 * may be available now to the device we'll have
383 * to do recognition again.
384 * Since we don't have any idea about which chpid
385 * that beast may be on we'll have to do a stsch
386 * on all devices, grr...
388 if (stsch(irq, &schib)) {
398 /* Put it on the slow path. */
399 ret = css_enqueue_subchannel_slow(irq);
401 css_clear_subchannel_slow_list();
408 spin_lock_irq(&sch->lock);
410 chp_mask = s390_process_res_acc_sch(chpid, fla, fla_mask, sch);
414 spin_unlock_irq(&sch->lock);
422 sch->lpm = ((sch->schib.pmcw.pim &
423 sch->schib.pmcw.pam &
425 | chp_mask) & sch->opm;
426 spin_unlock_irq(&sch->lock);
427 if (!old_lpm && sch->lpm)
428 device_trigger_reprobe(sch);
429 else if (sch->driver && sch->driver->verify)
430 sch->driver->verify(&sch->dev);
432 put_device(&sch->dev);
440 __get_chpid_from_lir(void *data)
446 /* incident-node descriptor */
448 /* attached-node descriptor */
450 /* incident-specific information */
454 lir = (struct lir*) data;
456 /* NULL link incident record */
458 if (!(lir->indesc[0]&0xc0000000))
459 /* node descriptor not valid */
461 if (!(lir->indesc[0]&0x10000000))
462 /* don't handle device-type nodes - FIXME */
464 /* Byte 3 contains the chpid. Could also be CTCA, but we don't care */
466 return (u16) (lir->indesc[0]&0x000000ff);
470 chsc_process_crw(void)
474 struct chsc_header request;
478 struct chsc_header response;
481 u8 vf; /* validity flags */
482 u8 rs; /* reporting source */
483 u8 cc; /* content code */
484 u16 fla; /* full link address */
485 u16 rsid; /* reporting source id */
488 u32 ccdf[96]; /* content-code dependent field */
489 /* ccdf has to be big enough for a link-incident record */
495 * build the chsc request block for store event information
497 * This function is only called by the machine check handler thread,
498 * so we don't need locking for the sei_page.
502 CIO_TRACE_EVENT( 2, "prcss");
506 memset(sei_area, 0, sizeof(*sei_area));
508 sei_area->request = (struct chsc_header) {
513 ccode = chsc(sei_area);
517 switch (sei_area->response.code) {
518 /* for debug purposes, check for problems */
520 CIO_CRW_EVENT(4, "chsc_process_crw: event information "
521 "successfully stored\n");
522 break; /* everything ok */
525 "chsc_process_crw: invalid command!\n");
528 CIO_CRW_EVENT(2, "chsc_process_crw: error in chsc "
532 CIO_CRW_EVENT(2, "chsc_process_crw: no event "
533 "information stored\n");
536 CIO_CRW_EVENT(2, "chsc_process_crw: chsc response %d\n",
537 sei_area->response.code);
541 /* Check if we might have lost some information. */
542 if (sei_area->flags & 0x40)
543 CIO_CRW_EVENT(2, "chsc_process_crw: Event information "
544 "has been lost due to overflow!\n");
546 if (sei_area->rs != 4) {
547 CIO_CRW_EVENT(2, "chsc_process_crw: reporting source "
548 "(%04X) isn't a chpid!\n",
553 /* which kind of information was stored? */
554 switch (sei_area->cc) {
555 case 1: /* link incident*/
556 CIO_CRW_EVENT(4, "chsc_process_crw: "
557 "channel subsystem reports link incident,"
558 " reporting source is chpid %x\n",
560 chpid = __get_chpid_from_lir(sei_area->ccdf);
562 CIO_CRW_EVENT(4, "%s: Invalid LIR, skipping\n",
565 s390_set_chpid_offline(chpid);
568 case 2: /* i/o resource accessibiliy */
569 CIO_CRW_EVENT(4, "chsc_process_crw: "
570 "channel subsystem reports some I/O "
571 "devices may have become accessible\n");
572 pr_debug("Data received after sei: \n");
573 pr_debug("Validity flags: %x\n", sei_area->vf);
575 /* allocate a new channel path structure, if needed */
576 status = get_chp_status(sei_area->rsid);
578 new_channel_path(sei_area->rsid);
581 if ((sei_area->vf & 0x80) == 0) {
582 pr_debug("chpid: %x\n", sei_area->rsid);
583 ret = s390_process_res_acc(sei_area->rsid,
585 } else if ((sei_area->vf & 0xc0) == 0x80) {
586 pr_debug("chpid: %x link addr: %x\n",
587 sei_area->rsid, sei_area->fla);
588 ret = s390_process_res_acc(sei_area->rsid,
591 } else if ((sei_area->vf & 0xc0) == 0xc0) {
592 pr_debug("chpid: %x full link addr: %x\n",
593 sei_area->rsid, sei_area->fla);
594 ret = s390_process_res_acc(sei_area->rsid,
602 default: /* other stuff */
603 CIO_CRW_EVENT(4, "chsc_process_crw: event %d\n",
607 } while (sei_area->flags & 0x80);
614 struct subchannel *sch;
618 if (!get_chp_status(chpid))
619 return 0; /* no need to do the rest */
621 sprintf(dbf_txt, "cadd%x", chpid);
622 CIO_TRACE_EVENT(2, dbf_txt);
625 for (irq = 0; irq < __MAX_SUBCHANNELS; irq++) {
628 sch = get_subchannel_by_schid(irq);
632 if (stsch(irq, &schib)) {
642 /* Put it on the slow path. */
643 ret = css_enqueue_subchannel_slow(irq);
645 css_clear_subchannel_slow_list();
652 spin_lock(&sch->lock);
654 if (sch->schib.pmcw.chpid[i] == chpid) {
655 if (stsch(sch->irq, &sch->schib) != 0) {
657 spin_unlock(&sch->lock);
663 spin_unlock(&sch->lock);
666 sch->lpm = ((sch->schib.pmcw.pim &
667 sch->schib.pmcw.pam &
669 | 0x80 >> i) & sch->opm;
671 if (sch->driver && sch->driver->verify)
672 sch->driver->verify(&sch->dev);
674 spin_unlock(&sch->lock);
675 put_device(&sch->dev);
681 * Handling of crw machine checks with channel path source.
684 chp_process_crw(int chpid, int on)
687 /* Path has gone. We use the link incident routine.*/
688 s390_set_chpid_offline(chpid);
689 return 0; /* De-register is async anyway. */
692 * Path has come. Allocate a new channel path structure,
695 if (get_chp_status(chpid) < 0)
696 new_channel_path(chpid);
697 /* Avoid the extra overhead in process_rec_acc. */
698 return chp_add(chpid);
702 __check_for_io_and_kill(struct subchannel *sch, int index)
706 cc = stsch(sch->irq, &sch->schib);
709 if (sch->schib.scsw.actl && sch->schib.pmcw.lpum == (0x80 >> index)) {
710 device_set_waiting(sch);
717 __s390_subchannel_vary_chpid(struct subchannel *sch, __u8 chpid, int on)
721 if (!sch->ssd_info.valid)
725 for (chp = 0; chp < 8; chp++) {
726 if (sch->ssd_info.chpid[chp] != chpid)
730 sch->opm |= (0x80 >> chp);
731 sch->lpm |= (0x80 >> chp);
733 device_trigger_reprobe(sch);
734 else if (sch->driver && sch->driver->verify)
735 sch->driver->verify(&sch->dev);
737 sch->opm &= ~(0x80 >> chp);
738 sch->lpm &= ~(0x80 >> chp);
740 * Give running I/O a grace period in which it
741 * can successfully terminate, even using the
742 * just varied off path. Then kill it.
744 if (!__check_for_io_and_kill(sch, chp) && !sch->lpm) {
745 if (css_enqueue_subchannel_slow(sch->irq)) {
746 css_clear_subchannel_slow_list();
749 } else if (sch->driver && sch->driver->verify)
750 sch->driver->verify(&sch->dev);
757 s390_subchannel_vary_chpid_off(struct device *dev, void *data)
759 struct subchannel *sch;
762 sch = to_subchannel(dev);
765 __s390_subchannel_vary_chpid(sch, *chpid, 0);
770 s390_subchannel_vary_chpid_on(struct device *dev, void *data)
772 struct subchannel *sch;
775 sch = to_subchannel(dev);
778 __s390_subchannel_vary_chpid(sch, *chpid, 1);
783 * Function: s390_vary_chpid
784 * Varies the specified chpid online or offline
787 s390_vary_chpid( __u8 chpid, int on)
790 int status, irq, ret;
791 struct subchannel *sch;
793 sprintf(dbf_text, on?"varyon%x":"varyoff%x", chpid);
794 CIO_TRACE_EVENT( 2, dbf_text);
796 status = get_chp_status(chpid);
798 printk(KERN_ERR "Can't vary unknown chpid %02X\n", chpid);
802 if (!on && !status) {
803 printk(KERN_ERR "chpid %x is already offline\n", chpid);
807 set_chp_logically_online(chpid, on);
810 * Redo PathVerification on the devices the chpid connects to
813 bus_for_each_dev(&css_bus_type, NULL, &chpid, on ?
814 s390_subchannel_vary_chpid_on :
815 s390_subchannel_vary_chpid_off);
818 /* Scan for new devices on varied on path. */
819 for (irq = 0; irq < __MAX_SUBCHANNELS; irq++) {
824 sch = get_subchannel_by_schid(irq);
826 put_device(&sch->dev);
829 if (stsch(irq, &schib))
832 /* Put it on the slow path. */
833 ret = css_enqueue_subchannel_slow(irq);
835 css_clear_subchannel_slow_list();
840 if (need_rescan || css_slow_subchannels_exist())
841 queue_work(slow_path_wq, &slow_path_work);
846 * Files for the channel path entries.
849 chp_status_show(struct device *dev, char *buf)
851 struct channel_path *chp = container_of(dev, struct channel_path, dev);
855 return (get_chp_status(chp->id) ? sprintf(buf, "online\n") :
856 sprintf(buf, "offline\n"));
860 chp_status_write(struct device *dev, const char *buf, size_t count)
862 struct channel_path *cp = container_of(dev, struct channel_path, dev);
867 num_args = sscanf(buf, "%5s", cmd);
871 if (!strnicmp(cmd, "on", 2))
872 error = s390_vary_chpid(cp->id, 1);
873 else if (!strnicmp(cmd, "off", 3))
874 error = s390_vary_chpid(cp->id, 0);
878 return error < 0 ? error : count;
882 static DEVICE_ATTR(status, 0644, chp_status_show, chp_status_write);
886 chp_release(struct device *dev)
888 struct channel_path *cp;
890 cp = container_of(dev, struct channel_path, dev);
895 * Entries for chpids on the system bus.
896 * This replaces /proc/chpids.
899 new_channel_path(int chpid)
901 struct channel_path *chp;
904 chp = kmalloc(sizeof(struct channel_path), GFP_KERNEL);
907 memset(chp, 0, sizeof(struct channel_path));
911 /* fill in status, etc. */
914 chp->dev = (struct device) {
915 .parent = &css_bus_device,
916 .release = chp_release,
918 snprintf(chp->dev.bus_id, BUS_ID_SIZE, "chp0.%x", chpid);
920 /* make it known to the system */
921 ret = device_register(&chp->dev);
923 printk(KERN_WARNING "%s: could not register %02x\n",
927 ret = device_create_file(&chp->dev, &dev_attr_status);
929 device_unregister(&chp->dev);
935 chsc_alloc_sei_area(void)
937 sei_page = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
939 printk(KERN_WARNING"Can't allocate page for processing of " \
940 "chsc machine checks!\n");
941 return (sei_page ? 0 : -ENOMEM);
944 subsys_initcall(chsc_alloc_sei_area);
946 struct css_general_char css_general_characteristics;
947 struct css_chsc_char css_chsc_characteristics;
950 chsc_determine_css_characteristics(void)
954 struct chsc_header request;
958 struct chsc_header response;
960 u32 general_char[510];
964 scsc_area = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
966 printk(KERN_WARNING"cio: Was not able to determine available" \
967 "CHSCs due to no memory.\n");
971 scsc_area->request = (struct chsc_header) {
976 result = chsc(scsc_area);
978 printk(KERN_WARNING"cio: Was not able to determine " \
979 "available CHSCs, cc=%i.\n", result);
984 if (scsc_area->response.code != 1) {
985 printk(KERN_WARNING"cio: Was not able to determine " \
986 "available CHSCs.\n");
990 memcpy(&css_general_characteristics, scsc_area->general_char,
991 sizeof(css_general_characteristics));
992 memcpy(&css_chsc_characteristics, scsc_area->chsc_char,
993 sizeof(css_chsc_characteristics));
995 free_page ((unsigned long) scsc_area);
999 EXPORT_SYMBOL_GPL(css_general_characteristics);
1000 EXPORT_SYMBOL_GPL(css_chsc_characteristics);