2 * drivers/s390/cio/css.c
3 * driver for channel subsystem
5 * Copyright (C) 2002 IBM Deutschland Entwicklung GmbH,
7 * Author(s): Arnd Bergmann (arndb@de.ibm.com)
8 * Cornelia Huck (cornelia.huck@de.ibm.com)
10 #include <linux/module.h>
11 #include <linux/init.h>
12 #include <linux/device.h>
13 #include <linux/slab.h>
14 #include <linux/errno.h>
15 #include <linux/list.h>
19 #include "cio_debug.h"
26 int css_init_done
= 0;
27 static int need_reprobe
= 0;
28 static int max_ssid
= 0;
30 struct channel_subsystem
*css
[__MAX_CSSID
+ 1];
32 int css_characteristics_avail
= 0;
35 for_each_subchannel(int(*fn
)(struct subchannel_id
, void *), void *data
)
37 struct subchannel_id schid
;
40 init_subchannel_id(&schid
);
44 ret
= fn(schid
, data
);
47 } while (schid
.sch_no
++ < __MAX_SUBCHANNEL
);
49 } while (schid
.ssid
++ < max_ssid
);
53 static struct subchannel
*
54 css_alloc_subchannel(struct subchannel_id schid
)
56 struct subchannel
*sch
;
59 sch
= kmalloc (sizeof (*sch
), GFP_KERNEL
| GFP_DMA
);
61 return ERR_PTR(-ENOMEM
);
62 ret
= cio_validate_subchannel (sch
, schid
);
68 if (sch
->st
!= SUBCHANNEL_TYPE_IO
) {
69 /* For now we ignore all non-io subchannels. */
71 return ERR_PTR(-EINVAL
);
75 * Set intparm to subchannel address.
76 * This is fine even on 64bit since the subchannel is always located
79 sch
->schib
.pmcw
.intparm
= (__u32
)(unsigned long)sch
;
80 ret
= cio_modify(sch
);
89 css_free_subchannel(struct subchannel
*sch
)
92 /* Reset intparm to zeroes. */
93 sch
->schib
.pmcw
.intparm
= 0;
101 css_subchannel_release(struct device
*dev
)
103 struct subchannel
*sch
;
105 sch
= to_subchannel(dev
);
106 if (!cio_is_console(sch
->schid
)) {
112 int css_sch_device_register(struct subchannel
*sch
)
116 mutex_lock(&sch
->reg_mutex
);
117 ret
= device_register(&sch
->dev
);
118 mutex_unlock(&sch
->reg_mutex
);
122 void css_sch_device_unregister(struct subchannel
*sch
)
124 mutex_lock(&sch
->reg_mutex
);
125 device_unregister(&sch
->dev
);
126 mutex_unlock(&sch
->reg_mutex
);
129 static void ssd_from_pmcw(struct chsc_ssd_info
*ssd
, struct pmcw
*pmcw
)
134 memset(ssd
, 0, sizeof(struct chsc_ssd_info
));
135 ssd
->path_mask
= pmcw
->pim
;
136 for (i
= 0; i
< 8; i
++) {
138 if (pmcw
->pim
& mask
) {
139 chp_id_init(&ssd
->chpid
[i
]);
140 ssd
->chpid
[i
].id
= pmcw
->chpid
[i
];
145 static void ssd_register_chpids(struct chsc_ssd_info
*ssd
)
150 for (i
= 0; i
< 8; i
++) {
152 if (ssd
->path_mask
& mask
)
153 if (!chp_is_registered(ssd
->chpid
[i
]))
154 chp_new(ssd
->chpid
[i
]);
158 void css_update_ssd_info(struct subchannel
*sch
)
162 if (cio_is_console(sch
->schid
)) {
163 /* Console is initialized too early for functions requiring
164 * memory allocation. */
165 ssd_from_pmcw(&sch
->ssd_info
, &sch
->schib
.pmcw
);
167 ret
= chsc_get_ssd_info(sch
->schid
, &sch
->ssd_info
);
169 ssd_from_pmcw(&sch
->ssd_info
, &sch
->schib
.pmcw
);
170 ssd_register_chpids(&sch
->ssd_info
);
174 static int css_register_subchannel(struct subchannel
*sch
)
178 /* Initialize the subchannel structure */
179 sch
->dev
.parent
= &css
[0]->device
;
180 sch
->dev
.bus
= &css_bus_type
;
181 sch
->dev
.release
= &css_subchannel_release
;
182 sch
->dev
.groups
= subch_attr_groups
;
183 css_update_ssd_info(sch
);
184 /* make it known to the system */
185 ret
= css_sch_device_register(sch
);
187 printk (KERN_WARNING
"%s: could not register %s\n",
188 __func__
, sch
->dev
.bus_id
);
194 static int css_probe_device(struct subchannel_id schid
)
197 struct subchannel
*sch
;
199 sch
= css_alloc_subchannel(schid
);
202 ret
= css_register_subchannel(sch
);
204 css_free_subchannel(sch
);
209 check_subchannel(struct device
* dev
, void * data
)
211 struct subchannel
*sch
;
212 struct subchannel_id
*schid
= data
;
214 sch
= to_subchannel(dev
);
215 return schid_equal(&sch
->schid
, schid
);
219 get_subchannel_by_schid(struct subchannel_id schid
)
223 dev
= bus_find_device(&css_bus_type
, NULL
,
224 &schid
, check_subchannel
);
226 return dev
? to_subchannel(dev
) : NULL
;
229 static int css_get_subchannel_status(struct subchannel
*sch
)
233 if (stsch(sch
->schid
, &schib
) || !schib
.pmcw
.dnv
)
235 if (sch
->schib
.pmcw
.dnv
&& (schib
.pmcw
.dev
!= sch
->schib
.pmcw
.dev
))
236 return CIO_REVALIDATE
;
242 static int css_evaluate_known_subchannel(struct subchannel
*sch
, int slow
)
244 int event
, ret
, disc
;
246 enum { NONE
, UNREGISTER
, UNREGISTER_PROBE
, REPROBE
} action
;
248 spin_lock_irqsave(sch
->lock
, flags
);
249 disc
= device_is_disconnected(sch
);
251 /* Disconnected devices are evaluated directly only.*/
252 spin_unlock_irqrestore(sch
->lock
, flags
);
255 /* No interrupt after machine check - kill pending timers. */
256 device_kill_pending_timer(sch
);
257 if (!disc
&& !slow
) {
258 /* Non-disconnected devices are evaluated on the slow path. */
259 spin_unlock_irqrestore(sch
->lock
, flags
);
262 event
= css_get_subchannel_status(sch
);
263 CIO_MSG_EVENT(4, "Evaluating schid 0.%x.%04x, event %d, %s, %s path.\n",
264 sch
->schid
.ssid
, sch
->schid
.sch_no
, event
,
265 disc
? "disconnected" : "normal",
266 slow
? "slow" : "fast");
267 /* Analyze subchannel status. */
272 /* Check if paths have become available. */
278 /* Prevent unwanted effects when opening lock. */
279 cio_disable_subchannel(sch
);
280 device_set_disconnected(sch
);
281 /* Ask driver what to do with device. */
283 if (sch
->driver
&& sch
->driver
->notify
) {
284 spin_unlock_irqrestore(sch
->lock
, flags
);
285 ret
= sch
->driver
->notify(&sch
->dev
, event
);
286 spin_lock_irqsave(sch
->lock
, flags
);
292 /* Device will be removed, so no notify necessary. */
294 /* Reprobe because immediate unregister might block. */
297 action
= UNREGISTER_PROBE
;
301 /* Get device operational again. */
305 /* Perform action. */
309 case UNREGISTER_PROBE
:
310 /* Unregister device (will use subchannel lock). */
311 spin_unlock_irqrestore(sch
->lock
, flags
);
312 css_sch_device_unregister(sch
);
313 spin_lock_irqsave(sch
->lock
, flags
);
315 /* Reset intparm to zeroes. */
316 sch
->schib
.pmcw
.intparm
= 0;
320 device_trigger_reprobe(sch
);
325 spin_unlock_irqrestore(sch
->lock
, flags
);
326 /* Probe if necessary. */
327 if (action
== UNREGISTER_PROBE
)
328 ret
= css_probe_device(sch
->schid
);
333 static int css_evaluate_new_subchannel(struct subchannel_id schid
, int slow
)
338 /* Will be done on the slow path. */
341 if (stsch_err(schid
, &schib
) || !schib
.pmcw
.dnv
) {
342 /* Unusable - ignore. */
345 CIO_MSG_EVENT(4, "Evaluating schid 0.%x.%04x, event %d, unknown, "
346 "slow path.\n", schid
.ssid
, schid
.sch_no
, CIO_OPER
);
348 return css_probe_device(schid
);
351 static void css_evaluate_subchannel(struct subchannel_id schid
, int slow
)
353 struct subchannel
*sch
;
356 sch
= get_subchannel_by_schid(schid
);
358 ret
= css_evaluate_known_subchannel(sch
, slow
);
359 put_device(&sch
->dev
);
361 ret
= css_evaluate_new_subchannel(schid
, slow
);
363 css_schedule_eval(schid
);
366 static struct idset
*slow_subchannel_set
;
367 static spinlock_t slow_subchannel_lock
;
369 static int __init
slow_subchannel_init(void)
371 spin_lock_init(&slow_subchannel_lock
);
372 slow_subchannel_set
= idset_sch_new();
373 if (!slow_subchannel_set
) {
374 printk(KERN_WARNING
"cio: could not allocate slow subchannel "
381 subsys_initcall(slow_subchannel_init
);
383 static void css_slow_path_func(struct work_struct
*unused
)
385 struct subchannel_id schid
;
387 CIO_TRACE_EVENT(4, "slowpath");
388 spin_lock_irq(&slow_subchannel_lock
);
389 init_subchannel_id(&schid
);
390 while (idset_sch_get_first(slow_subchannel_set
, &schid
)) {
391 idset_sch_del(slow_subchannel_set
, schid
);
392 spin_unlock_irq(&slow_subchannel_lock
);
393 css_evaluate_subchannel(schid
, 1);
394 spin_lock_irq(&slow_subchannel_lock
);
396 spin_unlock_irq(&slow_subchannel_lock
);
399 static DECLARE_WORK(slow_path_work
, css_slow_path_func
);
400 struct workqueue_struct
*slow_path_wq
;
402 void css_schedule_eval(struct subchannel_id schid
)
406 spin_lock_irqsave(&slow_subchannel_lock
, flags
);
407 idset_sch_add(slow_subchannel_set
, schid
);
408 queue_work(slow_path_wq
, &slow_path_work
);
409 spin_unlock_irqrestore(&slow_subchannel_lock
, flags
);
412 void css_schedule_eval_all(void)
416 spin_lock_irqsave(&slow_subchannel_lock
, flags
);
417 idset_fill(slow_subchannel_set
);
418 queue_work(slow_path_wq
, &slow_path_work
);
419 spin_unlock_irqrestore(&slow_subchannel_lock
, flags
);
422 /* Reprobe subchannel if unregistered. */
423 static int reprobe_subchannel(struct subchannel_id schid
, void *data
)
425 struct subchannel
*sch
;
428 CIO_DEBUG(KERN_INFO
, 6, "cio: reprobe 0.%x.%04x\n",
429 schid
.ssid
, schid
.sch_no
);
433 sch
= get_subchannel_by_schid(schid
);
436 put_device(&sch
->dev
);
440 ret
= css_probe_device(schid
);
446 /* These should abort looping */
455 /* Work function used to reprobe all unregistered subchannels. */
456 static void reprobe_all(struct work_struct
*unused
)
460 CIO_MSG_EVENT(2, "reprobe start\n");
463 /* Make sure initial subchannel scan is done. */
464 wait_event(ccw_device_init_wq
,
465 atomic_read(&ccw_device_init_count
) == 0);
466 ret
= for_each_subchannel(reprobe_subchannel
, NULL
);
468 CIO_MSG_EVENT(2, "reprobe done (rc=%d, need_reprobe=%d)\n", ret
,
472 static DECLARE_WORK(css_reprobe_work
, reprobe_all
);
474 /* Schedule reprobing of all unregistered subchannels. */
475 void css_schedule_reprobe(void)
478 queue_work(ccw_device_work
, &css_reprobe_work
);
481 EXPORT_SYMBOL_GPL(css_schedule_reprobe
);
484 * Called from the machine check handler for subchannel report words.
486 void css_process_crw(int rsid1
, int rsid2
)
488 struct subchannel_id mchk_schid
;
490 CIO_CRW_EVENT(2, "source is subchannel %04X, subsystem id %x\n",
492 init_subchannel_id(&mchk_schid
);
493 mchk_schid
.sch_no
= rsid1
;
495 mchk_schid
.ssid
= (rsid2
>> 8) & 3;
498 * Since we are always presented with IPI in the CRW, we have to
499 * use stsch() to find out if the subchannel in question has come
502 css_evaluate_subchannel(mchk_schid
, 0);
506 __init_channel_subsystem(struct subchannel_id schid
, void *data
)
508 struct subchannel
*sch
;
511 if (cio_is_console(schid
))
512 sch
= cio_get_console_subchannel();
514 sch
= css_alloc_subchannel(schid
);
523 panic("Out of memory in init_channel_subsystem\n");
524 /* -ENXIO: no more subchannels. */
527 /* -EIO: this subchannel set not supported. */
535 * We register ALL valid subchannels in ioinfo, even those
536 * that have been present before init_channel_subsystem.
537 * These subchannels can't have been registered yet (kmalloc
538 * not working) so we do it now. This is true e.g. for the
539 * console subchannel.
541 css_register_subchannel(sch
);
546 css_generate_pgid(struct channel_subsystem
*css
, u32 tod_high
)
548 if (css_characteristics_avail
&& css_general_characteristics
.mcss
) {
549 css
->global_pgid
.pgid_high
.ext_cssid
.version
= 0x80;
550 css
->global_pgid
.pgid_high
.ext_cssid
.cssid
= css
->cssid
;
553 css
->global_pgid
.pgid_high
.cpu_addr
= hard_smp_processor_id();
555 css
->global_pgid
.pgid_high
.cpu_addr
= 0;
558 css
->global_pgid
.cpu_id
= ((cpuid_t
*) __LC_CPUID
)->ident
;
559 css
->global_pgid
.cpu_model
= ((cpuid_t
*) __LC_CPUID
)->machine
;
560 css
->global_pgid
.tod_high
= tod_high
;
565 channel_subsystem_release(struct device
*dev
)
567 struct channel_subsystem
*css
;
570 mutex_destroy(&css
->mutex
);
575 css_cm_enable_show(struct device
*dev
, struct device_attribute
*attr
,
578 struct channel_subsystem
*css
= to_css(dev
);
582 return sprintf(buf
, "%x\n", css
->cm_enabled
);
586 css_cm_enable_store(struct device
*dev
, struct device_attribute
*attr
,
587 const char *buf
, size_t count
)
589 struct channel_subsystem
*css
= to_css(dev
);
594 ret
= css
->cm_enabled
? chsc_secm(css
, 0) : 0;
597 ret
= css
->cm_enabled
? 0 : chsc_secm(css
, 1);
602 return ret
< 0 ? ret
: count
;
605 static DEVICE_ATTR(cm_enable
, 0644, css_cm_enable_show
, css_cm_enable_store
);
607 static int __init
setup_css(int nr
)
612 memset(css
[nr
], 0, sizeof(struct channel_subsystem
));
613 css
[nr
]->pseudo_subchannel
=
614 kzalloc(sizeof(*css
[nr
]->pseudo_subchannel
), GFP_KERNEL
);
615 if (!css
[nr
]->pseudo_subchannel
)
617 css
[nr
]->pseudo_subchannel
->dev
.parent
= &css
[nr
]->device
;
618 css
[nr
]->pseudo_subchannel
->dev
.release
= css_subchannel_release
;
619 sprintf(css
[nr
]->pseudo_subchannel
->dev
.bus_id
, "defunct");
620 ret
= cio_create_sch_lock(css
[nr
]->pseudo_subchannel
);
622 kfree(css
[nr
]->pseudo_subchannel
);
625 mutex_init(&css
[nr
]->mutex
);
628 sprintf(css
[nr
]->device
.bus_id
, "css%x", nr
);
629 css
[nr
]->device
.release
= channel_subsystem_release
;
630 tod_high
= (u32
) (get_clock() >> 32);
631 css_generate_pgid(css
[nr
], tod_high
);
636 * Now that the driver core is running, we can setup our channel subsystem.
637 * The struct subchannel's are created during probing (except for the
638 * static console subchannel).
641 init_channel_subsystem (void)
645 if (chsc_determine_css_characteristics() == 0)
646 css_characteristics_avail
= 1;
648 if ((ret
= bus_register(&css_bus_type
)))
651 /* Try to enable MSS. */
652 ret
= chsc_enable_facility(CHSC_SDA_OC_MSS
);
654 case 0: /* Success. */
655 max_ssid
= __MAX_SSID
;
662 /* Setup css structure. */
663 for (i
= 0; i
<= __MAX_CSSID
; i
++) {
664 css
[i
] = kmalloc(sizeof(struct channel_subsystem
), GFP_KERNEL
);
672 ret
= device_register(&css
[i
]->device
);
675 if (css_characteristics_avail
&&
676 css_chsc_characteristics
.secm
) {
677 ret
= device_create_file(&css
[i
]->device
,
678 &dev_attr_cm_enable
);
682 ret
= device_register(&css
[i
]->pseudo_subchannel
->dev
);
690 for_each_subchannel(__init_channel_subsystem
, NULL
);
693 device_remove_file(&css
[i
]->device
, &dev_attr_cm_enable
);
695 device_unregister(&css
[i
]->device
);
697 kfree(css
[i
]->pseudo_subchannel
->lock
);
698 kfree(css
[i
]->pseudo_subchannel
);
704 device_unregister(&css
[i
]->pseudo_subchannel
->dev
);
705 if (css_characteristics_avail
&& css_chsc_characteristics
.secm
)
706 device_remove_file(&css
[i
]->device
,
707 &dev_attr_cm_enable
);
708 device_unregister(&css
[i
]->device
);
711 bus_unregister(&css_bus_type
);
716 int sch_is_pseudo_sch(struct subchannel
*sch
)
718 return sch
== to_css(sch
->dev
.parent
)->pseudo_subchannel
;
722 * find a driver for a subchannel. They identify by the subchannel
723 * type with the exception that the console subchannel driver has its own
724 * subchannel type although the device is an i/o subchannel
727 css_bus_match (struct device
*dev
, struct device_driver
*drv
)
729 struct subchannel
*sch
= container_of (dev
, struct subchannel
, dev
);
730 struct css_driver
*driver
= container_of (drv
, struct css_driver
, drv
);
732 if (sch
->st
== driver
->subchannel_type
)
739 css_probe (struct device
*dev
)
741 struct subchannel
*sch
;
743 sch
= to_subchannel(dev
);
744 sch
->driver
= container_of (dev
->driver
, struct css_driver
, drv
);
745 return (sch
->driver
->probe
? sch
->driver
->probe(sch
) : 0);
749 css_remove (struct device
*dev
)
751 struct subchannel
*sch
;
753 sch
= to_subchannel(dev
);
754 return (sch
->driver
->remove
? sch
->driver
->remove(sch
) : 0);
758 css_shutdown (struct device
*dev
)
760 struct subchannel
*sch
;
762 sch
= to_subchannel(dev
);
763 if (sch
->driver
->shutdown
)
764 sch
->driver
->shutdown(sch
);
767 struct bus_type css_bus_type
= {
769 .match
= css_bus_match
,
771 .remove
= css_remove
,
772 .shutdown
= css_shutdown
,
775 subsys_initcall(init_channel_subsystem
);
777 MODULE_LICENSE("GPL");
778 EXPORT_SYMBOL(css_bus_type
);
779 EXPORT_SYMBOL_GPL(css_characteristics_avail
);