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"
25 int css_init_done
= 0;
26 static int need_reprobe
= 0;
27 static int max_ssid
= 0;
29 struct channel_subsystem
*css
[__MAX_CSSID
+ 1];
31 int css_characteristics_avail
= 0;
34 for_each_subchannel(int(*fn
)(struct subchannel_id
, void *), void *data
)
36 struct subchannel_id schid
;
39 init_subchannel_id(&schid
);
43 ret
= fn(schid
, data
);
46 } while (schid
.sch_no
++ < __MAX_SUBCHANNEL
);
48 } while (schid
.ssid
++ < max_ssid
);
52 static struct subchannel
*
53 css_alloc_subchannel(struct subchannel_id schid
)
55 struct subchannel
*sch
;
58 sch
= kmalloc (sizeof (*sch
), GFP_KERNEL
| GFP_DMA
);
60 return ERR_PTR(-ENOMEM
);
61 ret
= cio_validate_subchannel (sch
, schid
);
67 if (sch
->st
!= SUBCHANNEL_TYPE_IO
) {
68 /* For now we ignore all non-io subchannels. */
70 return ERR_PTR(-EINVAL
);
74 * Set intparm to subchannel address.
75 * This is fine even on 64bit since the subchannel is always located
78 sch
->schib
.pmcw
.intparm
= (__u32
)(unsigned long)sch
;
79 ret
= cio_modify(sch
);
88 css_free_subchannel(struct subchannel
*sch
)
91 /* Reset intparm to zeroes. */
92 sch
->schib
.pmcw
.intparm
= 0;
100 css_subchannel_release(struct device
*dev
)
102 struct subchannel
*sch
;
104 sch
= to_subchannel(dev
);
105 if (!cio_is_console(sch
->schid
))
109 extern int css_get_ssd_info(struct subchannel
*sch
);
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
);
130 css_register_subchannel(struct subchannel
*sch
)
134 /* Initialize the subchannel structure */
135 sch
->dev
.parent
= &css
[0]->device
;
136 sch
->dev
.bus
= &css_bus_type
;
137 sch
->dev
.release
= &css_subchannel_release
;
139 /* make it known to the system */
140 ret
= css_sch_device_register(sch
);
142 printk (KERN_WARNING
"%s: could not register %s\n",
143 __func__
, sch
->dev
.bus_id
);
145 css_get_ssd_info(sch
);
150 css_probe_device(struct subchannel_id schid
)
153 struct subchannel
*sch
;
155 sch
= css_alloc_subchannel(schid
);
158 ret
= css_register_subchannel(sch
);
160 css_free_subchannel(sch
);
165 check_subchannel(struct device
* dev
, void * data
)
167 struct subchannel
*sch
;
168 struct subchannel_id
*schid
= data
;
170 sch
= to_subchannel(dev
);
171 return schid_equal(&sch
->schid
, schid
);
175 get_subchannel_by_schid(struct subchannel_id schid
)
179 dev
= bus_find_device(&css_bus_type
, NULL
,
180 (void *)&schid
, check_subchannel
);
182 return dev
? to_subchannel(dev
) : NULL
;
187 css_get_subchannel_status(struct subchannel
*sch
, struct subchannel_id schid
)
192 cc
= stsch(schid
, &schib
);
197 if (sch
&& sch
->schib
.pmcw
.dnv
&&
198 (schib
.pmcw
.dev
!= sch
->schib
.pmcw
.dev
))
199 return CIO_REVALIDATE
;
200 if (sch
&& !sch
->lpm
)
206 css_evaluate_subchannel(struct subchannel_id schid
, int slow
)
208 int event
, ret
, disc
;
209 struct subchannel
*sch
;
212 sch
= get_subchannel_by_schid(schid
);
213 disc
= sch
? device_is_disconnected(sch
) : 0;
216 put_device(&sch
->dev
);
217 return 0; /* Already processed. */
220 * We've got a machine check, so running I/O won't get an interrupt.
221 * Kill any pending timers.
224 device_kill_pending_timer(sch
);
225 if (!disc
&& !slow
) {
227 put_device(&sch
->dev
);
228 return -EAGAIN
; /* Will be done on the slow path. */
230 event
= css_get_subchannel_status(sch
, schid
);
231 CIO_MSG_EVENT(4, "Evaluating schid 0.%x.%04x, event %d, %s, %s path.\n",
232 schid
.ssid
, schid
.sch_no
, event
,
233 sch
?(disc
?"disconnected":"normal"):"unknown",
239 /* Never used this subchannel. Ignore. */
243 if (disc
&& (event
== CIO_NO_PATH
)) {
245 * Uargh, hack again. Because we don't get a machine
246 * check on configure on, our path bookkeeping can
247 * be out of date here (it's fine while we only do
248 * logical varying or get chsc machine checks). We
249 * need to force reprobing or we might miss devices
250 * coming operational again. It won't do harm in real
251 * no path situations.
253 spin_lock_irqsave(&sch
->lock
, flags
);
254 device_trigger_reprobe(sch
);
255 spin_unlock_irqrestore(&sch
->lock
, flags
);
259 if (sch
->driver
&& sch
->driver
->notify
&&
260 sch
->driver
->notify(&sch
->dev
, event
)) {
261 cio_disable_subchannel(sch
);
262 device_set_disconnected(sch
);
267 * Unregister subchannel.
268 * The device will be killed automatically.
270 cio_disable_subchannel(sch
);
271 css_sch_device_unregister(sch
);
272 /* Reset intparm to zeroes. */
273 sch
->schib
.pmcw
.intparm
= 0;
275 put_device(&sch
->dev
);
280 * Revalidation machine check. Sick.
281 * We don't notify the driver since we have to throw the device
285 css_sch_device_unregister(sch
);
286 /* Reset intparm to zeroes. */
287 sch
->schib
.pmcw
.intparm
= 0;
289 put_device(&sch
->dev
);
290 ret
= css_probe_device(schid
);
293 * We can't immediately deregister the disconnected
294 * device since it might block.
296 spin_lock_irqsave(&sch
->lock
, flags
);
297 device_trigger_reprobe(sch
);
298 spin_unlock_irqrestore(&sch
->lock
, flags
);
304 spin_lock_irqsave(&sch
->lock
, flags
);
305 /* Get device operational again. */
306 device_trigger_reprobe(sch
);
307 spin_unlock_irqrestore(&sch
->lock
, flags
);
309 ret
= sch
? 0 : css_probe_device(schid
);
319 css_rescan_devices(struct subchannel_id schid
, void *data
)
321 return css_evaluate_subchannel(schid
, 1);
324 struct slow_subchannel
{
325 struct list_head slow_list
;
326 struct subchannel_id schid
;
329 static LIST_HEAD(slow_subchannels_head
);
330 static DEFINE_SPINLOCK(slow_subchannel_lock
);
333 css_trigger_slow_path(void)
335 CIO_TRACE_EVENT(4, "slowpath");
339 for_each_subchannel(css_rescan_devices
, NULL
);
343 spin_lock_irq(&slow_subchannel_lock
);
344 while (!list_empty(&slow_subchannels_head
)) {
345 struct slow_subchannel
*slow_sch
=
346 list_entry(slow_subchannels_head
.next
,
347 struct slow_subchannel
, slow_list
);
349 list_del_init(slow_subchannels_head
.next
);
350 spin_unlock_irq(&slow_subchannel_lock
);
351 css_evaluate_subchannel(slow_sch
->schid
, 1);
352 spin_lock_irq(&slow_subchannel_lock
);
355 spin_unlock_irq(&slow_subchannel_lock
);
358 typedef void (*workfunc
)(void *);
359 DECLARE_WORK(slow_path_work
, (workfunc
)css_trigger_slow_path
, NULL
);
360 struct workqueue_struct
*slow_path_wq
;
362 /* Reprobe subchannel if unregistered. */
363 static int reprobe_subchannel(struct subchannel_id schid
, void *data
)
365 struct subchannel
*sch
;
368 CIO_DEBUG(KERN_INFO
, 6, "cio: reprobe 0.%x.%04x\n",
369 schid
.ssid
, schid
.sch_no
);
373 sch
= get_subchannel_by_schid(schid
);
376 put_device(&sch
->dev
);
380 ret
= css_probe_device(schid
);
386 /* These should abort looping */
395 /* Work function used to reprobe all unregistered subchannels. */
396 static void reprobe_all(void *data
)
400 CIO_MSG_EVENT(2, "reprobe start\n");
403 /* Make sure initial subchannel scan is done. */
404 wait_event(ccw_device_init_wq
,
405 atomic_read(&ccw_device_init_count
) == 0);
406 ret
= for_each_subchannel(reprobe_subchannel
, NULL
);
408 CIO_MSG_EVENT(2, "reprobe done (rc=%d, need_reprobe=%d)\n", ret
,
412 DECLARE_WORK(css_reprobe_work
, reprobe_all
, NULL
);
414 /* Schedule reprobing of all unregistered subchannels. */
415 void css_schedule_reprobe(void)
418 queue_work(ccw_device_work
, &css_reprobe_work
);
421 EXPORT_SYMBOL_GPL(css_schedule_reprobe
);
424 * Rescan for new devices. FIXME: This is slow.
425 * This function is called when we have lost CRWs due to overflows and we have
426 * to do subchannel housekeeping.
429 css_reiterate_subchannels(void)
431 css_clear_subchannel_slow_list();
436 * Called from the machine check handler for subchannel report words.
439 css_process_crw(int rsid1
, int rsid2
)
442 struct subchannel_id mchk_schid
;
444 CIO_CRW_EVENT(2, "source is subchannel %04X, subsystem id %x\n",
448 /* We need to iterate all subchannels anyway. */
451 init_subchannel_id(&mchk_schid
);
452 mchk_schid
.sch_no
= rsid1
;
454 mchk_schid
.ssid
= (rsid2
>> 8) & 3;
457 * Since we are always presented with IPI in the CRW, we have to
458 * use stsch() to find out if the subchannel in question has come
461 ret
= css_evaluate_subchannel(mchk_schid
, 0);
462 if (ret
== -EAGAIN
) {
463 if (css_enqueue_subchannel_slow(mchk_schid
)) {
464 css_clear_subchannel_slow_list();
472 __init_channel_subsystem(struct subchannel_id schid
, void *data
)
474 struct subchannel
*sch
;
477 if (cio_is_console(schid
))
478 sch
= cio_get_console_subchannel();
480 sch
= css_alloc_subchannel(schid
);
489 panic("Out of memory in init_channel_subsystem\n");
490 /* -ENXIO: no more subchannels. */
493 /* -EIO: this subchannel set not supported. */
501 * We register ALL valid subchannels in ioinfo, even those
502 * that have been present before init_channel_subsystem.
503 * These subchannels can't have been registered yet (kmalloc
504 * not working) so we do it now. This is true e.g. for the
505 * console subchannel.
507 css_register_subchannel(sch
);
512 css_generate_pgid(struct channel_subsystem
*css
, u32 tod_high
)
514 if (css_characteristics_avail
&& css_general_characteristics
.mcss
) {
515 css
->global_pgid
.pgid_high
.ext_cssid
.version
= 0x80;
516 css
->global_pgid
.pgid_high
.ext_cssid
.cssid
= css
->cssid
;
519 css
->global_pgid
.pgid_high
.cpu_addr
= hard_smp_processor_id();
521 css
->global_pgid
.pgid_high
.cpu_addr
= 0;
524 css
->global_pgid
.cpu_id
= ((cpuid_t
*) __LC_CPUID
)->ident
;
525 css
->global_pgid
.cpu_model
= ((cpuid_t
*) __LC_CPUID
)->machine
;
526 css
->global_pgid
.tod_high
= tod_high
;
531 channel_subsystem_release(struct device
*dev
)
533 struct channel_subsystem
*css
;
536 mutex_destroy(&css
->mutex
);
541 css_cm_enable_show(struct device
*dev
, struct device_attribute
*attr
,
544 struct channel_subsystem
*css
= to_css(dev
);
548 return sprintf(buf
, "%x\n", css
->cm_enabled
);
552 css_cm_enable_store(struct device
*dev
, struct device_attribute
*attr
,
553 const char *buf
, size_t count
)
555 struct channel_subsystem
*css
= to_css(dev
);
560 ret
= css
->cm_enabled
? chsc_secm(css
, 0) : 0;
563 ret
= css
->cm_enabled
? 0 : chsc_secm(css
, 1);
568 return ret
< 0 ? ret
: count
;
571 static DEVICE_ATTR(cm_enable
, 0644, css_cm_enable_show
, css_cm_enable_store
);
573 static inline void __init
578 memset(css
[nr
], 0, sizeof(struct channel_subsystem
));
579 mutex_init(&css
[nr
]->mutex
);
582 sprintf(css
[nr
]->device
.bus_id
, "css%x", nr
);
583 css
[nr
]->device
.release
= channel_subsystem_release
;
584 tod_high
= (u32
) (get_clock() >> 32);
585 css_generate_pgid(css
[nr
], tod_high
);
589 * Now that the driver core is running, we can setup our channel subsystem.
590 * The struct subchannel's are created during probing (except for the
591 * static console subchannel).
594 init_channel_subsystem (void)
598 if (chsc_determine_css_characteristics() == 0)
599 css_characteristics_avail
= 1;
601 if ((ret
= bus_register(&css_bus_type
)))
604 /* Try to enable MSS. */
605 ret
= chsc_enable_facility(CHSC_SDA_OC_MSS
);
607 case 0: /* Success. */
608 max_ssid
= __MAX_SSID
;
615 /* Setup css structure. */
616 for (i
= 0; i
<= __MAX_CSSID
; i
++) {
617 css
[i
] = kmalloc(sizeof(struct channel_subsystem
), GFP_KERNEL
);
623 ret
= device_register(&css
[i
]->device
);
626 if (css_characteristics_avail
&&
627 css_chsc_characteristics
.secm
) {
628 ret
= device_create_file(&css
[i
]->device
,
629 &dev_attr_cm_enable
);
638 for_each_subchannel(__init_channel_subsystem
, NULL
);
641 device_unregister(&css
[i
]->device
);
647 if (css_characteristics_avail
&& css_chsc_characteristics
.secm
)
648 device_remove_file(&css
[i
]->device
,
649 &dev_attr_cm_enable
);
650 device_unregister(&css
[i
]->device
);
653 bus_unregister(&css_bus_type
);
659 * find a driver for a subchannel. They identify by the subchannel
660 * type with the exception that the console subchannel driver has its own
661 * subchannel type although the device is an i/o subchannel
664 css_bus_match (struct device
*dev
, struct device_driver
*drv
)
666 struct subchannel
*sch
= container_of (dev
, struct subchannel
, dev
);
667 struct css_driver
*driver
= container_of (drv
, struct css_driver
, drv
);
669 if (sch
->st
== driver
->subchannel_type
)
676 css_probe (struct device
*dev
)
678 struct subchannel
*sch
;
680 sch
= to_subchannel(dev
);
681 sch
->driver
= container_of (dev
->driver
, struct css_driver
, drv
);
682 return (sch
->driver
->probe
? sch
->driver
->probe(sch
) : 0);
686 css_remove (struct device
*dev
)
688 struct subchannel
*sch
;
690 sch
= to_subchannel(dev
);
691 return (sch
->driver
->remove
? sch
->driver
->remove(sch
) : 0);
695 css_shutdown (struct device
*dev
)
697 struct subchannel
*sch
;
699 sch
= to_subchannel(dev
);
700 if (sch
->driver
->shutdown
)
701 sch
->driver
->shutdown(sch
);
704 struct bus_type css_bus_type
= {
706 .match
= css_bus_match
,
708 .remove
= css_remove
,
709 .shutdown
= css_shutdown
,
712 subsys_initcall(init_channel_subsystem
);
715 css_enqueue_subchannel_slow(struct subchannel_id schid
)
717 struct slow_subchannel
*new_slow_sch
;
720 new_slow_sch
= kzalloc(sizeof(struct slow_subchannel
), GFP_ATOMIC
);
723 new_slow_sch
->schid
= schid
;
724 spin_lock_irqsave(&slow_subchannel_lock
, flags
);
725 list_add_tail(&new_slow_sch
->slow_list
, &slow_subchannels_head
);
726 spin_unlock_irqrestore(&slow_subchannel_lock
, flags
);
731 css_clear_subchannel_slow_list(void)
735 spin_lock_irqsave(&slow_subchannel_lock
, flags
);
736 while (!list_empty(&slow_subchannels_head
)) {
737 struct slow_subchannel
*slow_sch
=
738 list_entry(slow_subchannels_head
.next
,
739 struct slow_subchannel
, slow_list
);
741 list_del_init(slow_subchannels_head
.next
);
744 spin_unlock_irqrestore(&slow_subchannel_lock
, flags
);
750 css_slow_subchannels_exist(void)
752 return (!list_empty(&slow_subchannels_head
));
755 MODULE_LICENSE("GPL");
756 EXPORT_SYMBOL(css_bus_type
);
757 EXPORT_SYMBOL_GPL(css_characteristics_avail
);