2 * drivers/s390/cio/css.c
3 * driver for channel subsystem
6 * Copyright (C) 2002 IBM Deutschland Entwicklung GmbH,
8 * Author(s): Arnd Bergmann (arndb@de.ibm.com)
9 * Cornelia Huck (cohuck@de.ibm.com)
11 #include <linux/module.h>
12 #include <linux/init.h>
13 #include <linux/device.h>
14 #include <linux/slab.h>
15 #include <linux/errno.h>
16 #include <linux/list.h>
20 #include "cio_debug.h"
25 int css_init_done
= 0;
26 static int max_ssid
= 0;
28 struct channel_subsystem
*css
[__MAX_CSSID
+ 1];
30 int css_characteristics_avail
= 0;
33 for_each_subchannel(int(*fn
)(struct subchannel_id
, void *), void *data
)
35 struct subchannel_id schid
;
38 init_subchannel_id(&schid
);
42 ret
= fn(schid
, data
);
45 } while (schid
.sch_no
++ < __MAX_SUBCHANNEL
);
47 } while (schid
.ssid
++ < max_ssid
);
51 static struct subchannel
*
52 css_alloc_subchannel(struct subchannel_id schid
)
54 struct subchannel
*sch
;
57 sch
= kmalloc (sizeof (*sch
), GFP_KERNEL
| GFP_DMA
);
59 return ERR_PTR(-ENOMEM
);
60 ret
= cio_validate_subchannel (sch
, schid
);
66 if (sch
->st
!= SUBCHANNEL_TYPE_IO
) {
67 /* For now we ignore all non-io subchannels. */
69 return ERR_PTR(-EINVAL
);
73 * Set intparm to subchannel address.
74 * This is fine even on 64bit since the subchannel is always located
77 sch
->schib
.pmcw
.intparm
= (__u32
)(unsigned long)sch
;
78 ret
= cio_modify(sch
);
87 css_free_subchannel(struct subchannel
*sch
)
90 /* Reset intparm to zeroes. */
91 sch
->schib
.pmcw
.intparm
= 0;
99 css_subchannel_release(struct device
*dev
)
101 struct subchannel
*sch
;
103 sch
= to_subchannel(dev
);
104 if (!cio_is_console(sch
->schid
))
108 extern int css_get_ssd_info(struct subchannel
*sch
);
111 css_register_subchannel(struct subchannel
*sch
)
115 /* Initialize the subchannel structure */
116 sch
->dev
.parent
= &css
[0]->device
;
117 sch
->dev
.bus
= &css_bus_type
;
118 sch
->dev
.release
= &css_subchannel_release
;
120 /* make it known to the system */
121 ret
= device_register(&sch
->dev
);
123 printk (KERN_WARNING
"%s: could not register %s\n",
124 __func__
, sch
->dev
.bus_id
);
126 css_get_ssd_info(sch
);
131 css_probe_device(struct subchannel_id schid
)
134 struct subchannel
*sch
;
136 sch
= css_alloc_subchannel(schid
);
139 ret
= css_register_subchannel(sch
);
141 css_free_subchannel(sch
);
146 check_subchannel(struct device
* dev
, void * data
)
148 struct subchannel
*sch
;
149 struct subchannel_id
*schid
= data
;
151 sch
= to_subchannel(dev
);
152 return schid_equal(&sch
->schid
, schid
);
156 get_subchannel_by_schid(struct subchannel_id schid
)
160 dev
= bus_find_device(&css_bus_type
, NULL
,
161 (void *)&schid
, check_subchannel
);
163 return dev
? to_subchannel(dev
) : NULL
;
168 css_get_subchannel_status(struct subchannel
*sch
, struct subchannel_id schid
)
173 cc
= stsch(schid
, &schib
);
178 if (sch
&& sch
->schib
.pmcw
.dnv
&&
179 (schib
.pmcw
.dev
!= sch
->schib
.pmcw
.dev
))
180 return CIO_REVALIDATE
;
181 if (sch
&& !sch
->lpm
)
187 css_evaluate_subchannel(struct subchannel_id schid
, int slow
)
189 int event
, ret
, disc
;
190 struct subchannel
*sch
;
193 sch
= get_subchannel_by_schid(schid
);
194 disc
= sch
? device_is_disconnected(sch
) : 0;
197 put_device(&sch
->dev
);
198 return 0; /* Already processed. */
201 * We've got a machine check, so running I/O won't get an interrupt.
202 * Kill any pending timers.
205 device_kill_pending_timer(sch
);
206 if (!disc
&& !slow
) {
208 put_device(&sch
->dev
);
209 return -EAGAIN
; /* Will be done on the slow path. */
211 event
= css_get_subchannel_status(sch
, schid
);
212 CIO_MSG_EVENT(4, "Evaluating schid 0.%x.%04x, event %d, %s, %s path.\n",
213 schid
.ssid
, schid
.sch_no
, event
,
214 sch
?(disc
?"disconnected":"normal"):"unknown",
220 /* Never used this subchannel. Ignore. */
224 if (disc
&& (event
== CIO_NO_PATH
)) {
226 * Uargh, hack again. Because we don't get a machine
227 * check on configure on, our path bookkeeping can
228 * be out of date here (it's fine while we only do
229 * logical varying or get chsc machine checks). We
230 * need to force reprobing or we might miss devices
231 * coming operational again. It won't do harm in real
232 * no path situations.
234 spin_lock_irqsave(&sch
->lock
, flags
);
235 device_trigger_reprobe(sch
);
236 spin_unlock_irqrestore(&sch
->lock
, flags
);
240 if (sch
->driver
&& sch
->driver
->notify
&&
241 sch
->driver
->notify(&sch
->dev
, event
)) {
242 cio_disable_subchannel(sch
);
243 device_set_disconnected(sch
);
248 * Unregister subchannel.
249 * The device will be killed automatically.
251 cio_disable_subchannel(sch
);
252 device_unregister(&sch
->dev
);
253 /* Reset intparm to zeroes. */
254 sch
->schib
.pmcw
.intparm
= 0;
256 put_device(&sch
->dev
);
261 * Revalidation machine check. Sick.
262 * We don't notify the driver since we have to throw the device
266 device_unregister(&sch
->dev
);
267 /* Reset intparm to zeroes. */
268 sch
->schib
.pmcw
.intparm
= 0;
270 put_device(&sch
->dev
);
271 ret
= css_probe_device(schid
);
274 * We can't immediately deregister the disconnected
275 * device since it might block.
277 spin_lock_irqsave(&sch
->lock
, flags
);
278 device_trigger_reprobe(sch
);
279 spin_unlock_irqrestore(&sch
->lock
, flags
);
285 spin_lock_irqsave(&sch
->lock
, flags
);
286 /* Get device operational again. */
287 device_trigger_reprobe(sch
);
288 spin_unlock_irqrestore(&sch
->lock
, flags
);
290 ret
= sch
? 0 : css_probe_device(schid
);
300 css_rescan_devices(struct subchannel_id schid
, void *data
)
302 return css_evaluate_subchannel(schid
, 1);
305 struct slow_subchannel
{
306 struct list_head slow_list
;
307 struct subchannel_id schid
;
310 static LIST_HEAD(slow_subchannels_head
);
311 static DEFINE_SPINLOCK(slow_subchannel_lock
);
314 css_trigger_slow_path(void)
316 CIO_TRACE_EVENT(4, "slowpath");
320 for_each_subchannel(css_rescan_devices
, NULL
);
324 spin_lock_irq(&slow_subchannel_lock
);
325 while (!list_empty(&slow_subchannels_head
)) {
326 struct slow_subchannel
*slow_sch
=
327 list_entry(slow_subchannels_head
.next
,
328 struct slow_subchannel
, slow_list
);
330 list_del_init(slow_subchannels_head
.next
);
331 spin_unlock_irq(&slow_subchannel_lock
);
332 css_evaluate_subchannel(slow_sch
->schid
, 1);
333 spin_lock_irq(&slow_subchannel_lock
);
336 spin_unlock_irq(&slow_subchannel_lock
);
339 typedef void (*workfunc
)(void *);
340 DECLARE_WORK(slow_path_work
, (workfunc
)css_trigger_slow_path
, NULL
);
341 struct workqueue_struct
*slow_path_wq
;
344 * Rescan for new devices. FIXME: This is slow.
345 * This function is called when we have lost CRWs due to overflows and we have
346 * to do subchannel housekeeping.
349 css_reiterate_subchannels(void)
351 css_clear_subchannel_slow_list();
356 * Called from the machine check handler for subchannel report words.
359 css_process_crw(int rsid1
, int rsid2
)
362 struct subchannel_id mchk_schid
;
364 CIO_CRW_EVENT(2, "source is subchannel %04X, subsystem id %x\n",
368 /* We need to iterate all subchannels anyway. */
371 init_subchannel_id(&mchk_schid
);
372 mchk_schid
.sch_no
= rsid1
;
374 mchk_schid
.ssid
= (rsid2
>> 8) & 3;
377 * Since we are always presented with IPI in the CRW, we have to
378 * use stsch() to find out if the subchannel in question has come
381 ret
= css_evaluate_subchannel(mchk_schid
, 0);
382 if (ret
== -EAGAIN
) {
383 if (css_enqueue_subchannel_slow(mchk_schid
)) {
384 css_clear_subchannel_slow_list();
392 __init_channel_subsystem(struct subchannel_id schid
, void *data
)
394 struct subchannel
*sch
;
397 if (cio_is_console(schid
))
398 sch
= cio_get_console_subchannel();
400 sch
= css_alloc_subchannel(schid
);
409 panic("Out of memory in init_channel_subsystem\n");
410 /* -ENXIO: no more subchannels. */
418 * We register ALL valid subchannels in ioinfo, even those
419 * that have been present before init_channel_subsystem.
420 * These subchannels can't have been registered yet (kmalloc
421 * not working) so we do it now. This is true e.g. for the
422 * console subchannel.
424 css_register_subchannel(sch
);
429 css_generate_pgid(struct channel_subsystem
*css
, u32 tod_high
)
431 if (css_characteristics_avail
&& css_general_characteristics
.mcss
) {
432 css
->global_pgid
.pgid_high
.ext_cssid
.version
= 0x80;
433 css
->global_pgid
.pgid_high
.ext_cssid
.cssid
= css
->cssid
;
436 css
->global_pgid
.pgid_high
.cpu_addr
= hard_smp_processor_id();
438 css
->global_pgid
.pgid_high
.cpu_addr
= 0;
441 css
->global_pgid
.cpu_id
= ((cpuid_t
*) __LC_CPUID
)->ident
;
442 css
->global_pgid
.cpu_model
= ((cpuid_t
*) __LC_CPUID
)->machine
;
443 css
->global_pgid
.tod_high
= tod_high
;
448 channel_subsystem_release(struct device
*dev
)
450 struct channel_subsystem
*css
;
456 static inline void __init
461 memset(css
[nr
], 0, sizeof(struct channel_subsystem
));
464 sprintf(css
[nr
]->device
.bus_id
, "css%x", nr
);
465 css
[nr
]->device
.release
= channel_subsystem_release
;
466 tod_high
= (u32
) (get_clock() >> 32);
467 css_generate_pgid(css
[nr
], tod_high
);
471 * Now that the driver core is running, we can setup our channel subsystem.
472 * The struct subchannel's are created during probing (except for the
473 * static console subchannel).
476 init_channel_subsystem (void)
480 if (chsc_determine_css_characteristics() == 0)
481 css_characteristics_avail
= 1;
483 if ((ret
= bus_register(&css_bus_type
)))
486 /* Try to enable MSS. */
487 ret
= chsc_enable_facility(CHSC_SDA_OC_MSS
);
489 case 0: /* Success. */
490 max_ssid
= __MAX_SSID
;
497 /* Setup css structure. */
498 for (i
= 0; i
<= __MAX_CSSID
; i
++) {
499 css
[i
] = kmalloc(sizeof(struct channel_subsystem
), GFP_KERNEL
);
505 ret
= device_register(&css
[i
]->device
);
513 for_each_subchannel(__init_channel_subsystem
, NULL
);
520 device_unregister(&css
[i
]->device
);
523 bus_unregister(&css_bus_type
);
529 * find a driver for a subchannel. They identify by the subchannel
530 * type with the exception that the console subchannel driver has its own
531 * subchannel type although the device is an i/o subchannel
534 css_bus_match (struct device
*dev
, struct device_driver
*drv
)
536 struct subchannel
*sch
= container_of (dev
, struct subchannel
, dev
);
537 struct css_driver
*driver
= container_of (drv
, struct css_driver
, drv
);
539 if (sch
->st
== driver
->subchannel_type
)
546 css_probe (struct device
*dev
)
548 struct subchannel
*sch
;
550 sch
= to_subchannel(dev
);
551 sch
->driver
= container_of (dev
->driver
, struct css_driver
, drv
);
552 return (sch
->driver
->probe
? sch
->driver
->probe(sch
) : 0);
556 css_remove (struct device
*dev
)
558 struct subchannel
*sch
;
560 sch
= to_subchannel(dev
);
561 return (sch
->driver
->remove
? sch
->driver
->remove(sch
) : 0);
565 css_shutdown (struct device
*dev
)
567 struct subchannel
*sch
;
569 sch
= to_subchannel(dev
);
570 if (sch
->driver
->shutdown
)
571 sch
->driver
->shutdown(sch
);
574 struct bus_type css_bus_type
= {
576 .match
= css_bus_match
,
578 .remove
= css_remove
,
579 .shutdown
= css_shutdown
,
582 subsys_initcall(init_channel_subsystem
);
585 css_enqueue_subchannel_slow(struct subchannel_id schid
)
587 struct slow_subchannel
*new_slow_sch
;
590 new_slow_sch
= kmalloc(sizeof(struct slow_subchannel
), GFP_ATOMIC
);
593 memset(new_slow_sch
, 0, sizeof(struct slow_subchannel
));
594 new_slow_sch
->schid
= schid
;
595 spin_lock_irqsave(&slow_subchannel_lock
, flags
);
596 list_add_tail(&new_slow_sch
->slow_list
, &slow_subchannels_head
);
597 spin_unlock_irqrestore(&slow_subchannel_lock
, flags
);
602 css_clear_subchannel_slow_list(void)
606 spin_lock_irqsave(&slow_subchannel_lock
, flags
);
607 while (!list_empty(&slow_subchannels_head
)) {
608 struct slow_subchannel
*slow_sch
=
609 list_entry(slow_subchannels_head
.next
,
610 struct slow_subchannel
, slow_list
);
612 list_del_init(slow_subchannels_head
.next
);
615 spin_unlock_irqrestore(&slow_subchannel_lock
, flags
);
621 css_slow_subchannels_exist(void)
623 return (!list_empty(&slow_subchannels_head
));
626 MODULE_LICENSE("GPL");
627 EXPORT_SYMBOL(css_bus_type
);
628 EXPORT_SYMBOL_GPL(css_characteristics_avail
);