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>
16 #include <linux/reboot.h>
20 #include "cio_debug.h"
27 int css_init_done
= 0;
28 static int need_reprobe
= 0;
29 static int max_ssid
= 0;
31 struct channel_subsystem
*channel_subsystems
[__MAX_CSSID
+ 1];
33 int css_characteristics_avail
= 0;
36 for_each_subchannel(int(*fn
)(struct subchannel_id
, void *), void *data
)
38 struct subchannel_id schid
;
41 init_subchannel_id(&schid
);
45 ret
= fn(schid
, data
);
48 } while (schid
.sch_no
++ < __MAX_SUBCHANNEL
);
50 } while (schid
.ssid
++ < max_ssid
);
54 static struct subchannel
*
55 css_alloc_subchannel(struct subchannel_id schid
)
57 struct subchannel
*sch
;
60 sch
= kmalloc (sizeof (*sch
), GFP_KERNEL
| GFP_DMA
);
62 return ERR_PTR(-ENOMEM
);
63 ret
= cio_validate_subchannel (sch
, schid
);
69 if (sch
->st
!= SUBCHANNEL_TYPE_IO
) {
70 /* For now we ignore all non-io subchannels. */
72 return ERR_PTR(-EINVAL
);
76 * Set intparm to subchannel address.
77 * This is fine even on 64bit since the subchannel is always located
80 sch
->schib
.pmcw
.intparm
= (__u32
)(unsigned long)sch
;
81 ret
= cio_modify(sch
);
91 css_free_subchannel(struct subchannel
*sch
)
94 /* Reset intparm to zeroes. */
95 sch
->schib
.pmcw
.intparm
= 0;
103 css_subchannel_release(struct device
*dev
)
105 struct subchannel
*sch
;
107 sch
= to_subchannel(dev
);
108 if (!cio_is_console(sch
->schid
)) {
114 static int css_sch_device_register(struct subchannel
*sch
)
118 mutex_lock(&sch
->reg_mutex
);
119 ret
= device_register(&sch
->dev
);
120 mutex_unlock(&sch
->reg_mutex
);
124 void css_sch_device_unregister(struct subchannel
*sch
)
126 mutex_lock(&sch
->reg_mutex
);
127 device_unregister(&sch
->dev
);
128 mutex_unlock(&sch
->reg_mutex
);
131 static void ssd_from_pmcw(struct chsc_ssd_info
*ssd
, struct pmcw
*pmcw
)
136 memset(ssd
, 0, sizeof(struct chsc_ssd_info
));
137 ssd
->path_mask
= pmcw
->pim
;
138 for (i
= 0; i
< 8; i
++) {
140 if (pmcw
->pim
& mask
) {
141 chp_id_init(&ssd
->chpid
[i
]);
142 ssd
->chpid
[i
].id
= pmcw
->chpid
[i
];
147 static void ssd_register_chpids(struct chsc_ssd_info
*ssd
)
152 for (i
= 0; i
< 8; i
++) {
154 if (ssd
->path_mask
& mask
)
155 if (!chp_is_registered(ssd
->chpid
[i
]))
156 chp_new(ssd
->chpid
[i
]);
160 void css_update_ssd_info(struct subchannel
*sch
)
164 if (cio_is_console(sch
->schid
)) {
165 /* Console is initialized too early for functions requiring
166 * memory allocation. */
167 ssd_from_pmcw(&sch
->ssd_info
, &sch
->schib
.pmcw
);
169 ret
= chsc_get_ssd_info(sch
->schid
, &sch
->ssd_info
);
171 ssd_from_pmcw(&sch
->ssd_info
, &sch
->schib
.pmcw
);
172 ssd_register_chpids(&sch
->ssd_info
);
176 static int css_register_subchannel(struct subchannel
*sch
)
180 /* Initialize the subchannel structure */
181 sch
->dev
.parent
= &channel_subsystems
[0]->device
;
182 sch
->dev
.bus
= &css_bus_type
;
183 sch
->dev
.release
= &css_subchannel_release
;
184 sch
->dev
.groups
= subch_attr_groups
;
186 * We don't want to generate uevents for I/O subchannels that don't
187 * have a working ccw device behind them since they will be
188 * unregistered before they can be used anyway, so we delay the add
189 * uevent until after device recognition was successful.
191 if (!cio_is_console(sch
->schid
))
192 /* Console is special, no need to suppress. */
193 sch
->dev
.uevent_suppress
= 1;
194 css_update_ssd_info(sch
);
195 /* make it known to the system */
196 ret
= css_sch_device_register(sch
);
198 CIO_MSG_EVENT(0, "Could not register sch 0.%x.%04x: %d\n",
199 sch
->schid
.ssid
, sch
->schid
.sch_no
, ret
);
205 static int css_probe_device(struct subchannel_id schid
)
208 struct subchannel
*sch
;
210 sch
= css_alloc_subchannel(schid
);
213 ret
= css_register_subchannel(sch
);
215 css_free_subchannel(sch
);
220 check_subchannel(struct device
* dev
, void * data
)
222 struct subchannel
*sch
;
223 struct subchannel_id
*schid
= data
;
225 sch
= to_subchannel(dev
);
226 return schid_equal(&sch
->schid
, schid
);
230 get_subchannel_by_schid(struct subchannel_id schid
)
234 dev
= bus_find_device(&css_bus_type
, NULL
,
235 &schid
, check_subchannel
);
237 return dev
? to_subchannel(dev
) : NULL
;
240 static int css_get_subchannel_status(struct subchannel
*sch
)
244 if (stsch(sch
->schid
, &schib
) || !schib
.pmcw
.dnv
)
246 if (sch
->schib
.pmcw
.dnv
&& (schib
.pmcw
.dev
!= sch
->schib
.pmcw
.dev
))
247 return CIO_REVALIDATE
;
253 static int css_evaluate_known_subchannel(struct subchannel
*sch
, int slow
)
255 int event
, ret
, disc
;
257 enum { NONE
, UNREGISTER
, UNREGISTER_PROBE
, REPROBE
} action
;
259 spin_lock_irqsave(sch
->lock
, flags
);
260 disc
= device_is_disconnected(sch
);
262 /* Disconnected devices are evaluated directly only.*/
263 spin_unlock_irqrestore(sch
->lock
, flags
);
266 /* No interrupt after machine check - kill pending timers. */
267 device_kill_pending_timer(sch
);
268 if (!disc
&& !slow
) {
269 /* Non-disconnected devices are evaluated on the slow path. */
270 spin_unlock_irqrestore(sch
->lock
, flags
);
273 event
= css_get_subchannel_status(sch
);
274 CIO_MSG_EVENT(4, "Evaluating schid 0.%x.%04x, event %d, %s, %s path.\n",
275 sch
->schid
.ssid
, sch
->schid
.sch_no
, event
,
276 disc
? "disconnected" : "normal",
277 slow
? "slow" : "fast");
278 /* Analyze subchannel status. */
283 /* Check if paths have become available. */
289 /* Prevent unwanted effects when opening lock. */
290 cio_disable_subchannel(sch
);
291 device_set_disconnected(sch
);
292 /* Ask driver what to do with device. */
294 if (sch
->driver
&& sch
->driver
->notify
) {
295 spin_unlock_irqrestore(sch
->lock
, flags
);
296 ret
= sch
->driver
->notify(&sch
->dev
, event
);
297 spin_lock_irqsave(sch
->lock
, flags
);
303 /* Device will be removed, so no notify necessary. */
305 /* Reprobe because immediate unregister might block. */
308 action
= UNREGISTER_PROBE
;
312 /* Get device operational again. */
316 /* Perform action. */
320 case UNREGISTER_PROBE
:
321 /* Unregister device (will use subchannel lock). */
322 spin_unlock_irqrestore(sch
->lock
, flags
);
323 css_sch_device_unregister(sch
);
324 spin_lock_irqsave(sch
->lock
, flags
);
326 /* Reset intparm to zeroes. */
327 sch
->schib
.pmcw
.intparm
= 0;
331 device_trigger_reprobe(sch
);
336 spin_unlock_irqrestore(sch
->lock
, flags
);
337 /* Probe if necessary. */
338 if (action
== UNREGISTER_PROBE
)
339 ret
= css_probe_device(sch
->schid
);
344 static int css_evaluate_new_subchannel(struct subchannel_id schid
, int slow
)
349 /* Will be done on the slow path. */
352 if (stsch_err(schid
, &schib
) || !schib
.pmcw
.dnv
) {
353 /* Unusable - ignore. */
356 CIO_MSG_EVENT(4, "Evaluating schid 0.%x.%04x, event %d, unknown, "
357 "slow path.\n", schid
.ssid
, schid
.sch_no
, CIO_OPER
);
359 return css_probe_device(schid
);
362 static void css_evaluate_subchannel(struct subchannel_id schid
, int slow
)
364 struct subchannel
*sch
;
367 sch
= get_subchannel_by_schid(schid
);
369 ret
= css_evaluate_known_subchannel(sch
, slow
);
370 put_device(&sch
->dev
);
372 ret
= css_evaluate_new_subchannel(schid
, slow
);
374 css_schedule_eval(schid
);
377 static struct idset
*slow_subchannel_set
;
378 static spinlock_t slow_subchannel_lock
;
380 static int __init
slow_subchannel_init(void)
382 spin_lock_init(&slow_subchannel_lock
);
383 slow_subchannel_set
= idset_sch_new();
384 if (!slow_subchannel_set
) {
385 CIO_MSG_EVENT(0, "could not allocate slow subchannel set\n");
391 static void css_slow_path_func(struct work_struct
*unused
)
393 struct subchannel_id schid
;
395 CIO_TRACE_EVENT(4, "slowpath");
396 spin_lock_irq(&slow_subchannel_lock
);
397 init_subchannel_id(&schid
);
398 while (idset_sch_get_first(slow_subchannel_set
, &schid
)) {
399 idset_sch_del(slow_subchannel_set
, schid
);
400 spin_unlock_irq(&slow_subchannel_lock
);
401 css_evaluate_subchannel(schid
, 1);
402 spin_lock_irq(&slow_subchannel_lock
);
404 spin_unlock_irq(&slow_subchannel_lock
);
407 static DECLARE_WORK(slow_path_work
, css_slow_path_func
);
408 struct workqueue_struct
*slow_path_wq
;
410 void css_schedule_eval(struct subchannel_id schid
)
414 spin_lock_irqsave(&slow_subchannel_lock
, flags
);
415 idset_sch_add(slow_subchannel_set
, schid
);
416 queue_work(slow_path_wq
, &slow_path_work
);
417 spin_unlock_irqrestore(&slow_subchannel_lock
, flags
);
420 void css_schedule_eval_all(void)
424 spin_lock_irqsave(&slow_subchannel_lock
, flags
);
425 idset_fill(slow_subchannel_set
);
426 queue_work(slow_path_wq
, &slow_path_work
);
427 spin_unlock_irqrestore(&slow_subchannel_lock
, flags
);
430 /* Reprobe subchannel if unregistered. */
431 static int reprobe_subchannel(struct subchannel_id schid
, void *data
)
433 struct subchannel
*sch
;
436 CIO_MSG_EVENT(6, "cio: reprobe 0.%x.%04x\n",
437 schid
.ssid
, schid
.sch_no
);
441 sch
= get_subchannel_by_schid(schid
);
444 put_device(&sch
->dev
);
448 ret
= css_probe_device(schid
);
455 /* These should abort looping */
464 /* Work function used to reprobe all unregistered subchannels. */
465 static void reprobe_all(struct work_struct
*unused
)
469 CIO_MSG_EVENT(2, "reprobe start\n");
472 /* Make sure initial subchannel scan is done. */
473 wait_event(ccw_device_init_wq
,
474 atomic_read(&ccw_device_init_count
) == 0);
475 ret
= for_each_subchannel(reprobe_subchannel
, NULL
);
477 CIO_MSG_EVENT(2, "reprobe done (rc=%d, need_reprobe=%d)\n", ret
,
481 static DECLARE_WORK(css_reprobe_work
, reprobe_all
);
483 /* Schedule reprobing of all unregistered subchannels. */
484 void css_schedule_reprobe(void)
487 queue_work(slow_path_wq
, &css_reprobe_work
);
490 EXPORT_SYMBOL_GPL(css_schedule_reprobe
);
493 * Called from the machine check handler for subchannel report words.
495 void css_process_crw(int rsid1
, int rsid2
)
497 struct subchannel_id mchk_schid
;
499 CIO_CRW_EVENT(2, "source is subchannel %04X, subsystem id %x\n",
501 init_subchannel_id(&mchk_schid
);
502 mchk_schid
.sch_no
= rsid1
;
504 mchk_schid
.ssid
= (rsid2
>> 8) & 3;
507 * Since we are always presented with IPI in the CRW, we have to
508 * use stsch() to find out if the subchannel in question has come
511 css_evaluate_subchannel(mchk_schid
, 0);
515 __init_channel_subsystem(struct subchannel_id schid
, void *data
)
517 struct subchannel
*sch
;
520 if (cio_is_console(schid
))
521 sch
= cio_get_console_subchannel();
523 sch
= css_alloc_subchannel(schid
);
532 panic("Out of memory in init_channel_subsystem\n");
533 /* -ENXIO: no more subchannels. */
536 /* -EIO: this subchannel set not supported. */
544 * We register ALL valid subchannels in ioinfo, even those
545 * that have been present before init_channel_subsystem.
546 * These subchannels can't have been registered yet (kmalloc
547 * not working) so we do it now. This is true e.g. for the
548 * console subchannel.
550 css_register_subchannel(sch
);
555 css_generate_pgid(struct channel_subsystem
*css
, u32 tod_high
)
557 if (css_characteristics_avail
&& css_general_characteristics
.mcss
) {
558 css
->global_pgid
.pgid_high
.ext_cssid
.version
= 0x80;
559 css
->global_pgid
.pgid_high
.ext_cssid
.cssid
= css
->cssid
;
562 css
->global_pgid
.pgid_high
.cpu_addr
= hard_smp_processor_id();
564 css
->global_pgid
.pgid_high
.cpu_addr
= 0;
567 css
->global_pgid
.cpu_id
= ((cpuid_t
*) __LC_CPUID
)->ident
;
568 css
->global_pgid
.cpu_model
= ((cpuid_t
*) __LC_CPUID
)->machine
;
569 css
->global_pgid
.tod_high
= tod_high
;
574 channel_subsystem_release(struct device
*dev
)
576 struct channel_subsystem
*css
;
579 mutex_destroy(&css
->mutex
);
584 css_cm_enable_show(struct device
*dev
, struct device_attribute
*attr
,
587 struct channel_subsystem
*css
= to_css(dev
);
591 return sprintf(buf
, "%x\n", css
->cm_enabled
);
595 css_cm_enable_store(struct device
*dev
, struct device_attribute
*attr
,
596 const char *buf
, size_t count
)
598 struct channel_subsystem
*css
= to_css(dev
);
603 ret
= css
->cm_enabled
? chsc_secm(css
, 0) : 0;
606 ret
= css
->cm_enabled
? 0 : chsc_secm(css
, 1);
611 return ret
< 0 ? ret
: count
;
614 static DEVICE_ATTR(cm_enable
, 0644, css_cm_enable_show
, css_cm_enable_store
);
616 static int __init
setup_css(int nr
)
620 struct channel_subsystem
*css
;
622 css
= channel_subsystems
[nr
];
623 memset(css
, 0, sizeof(struct channel_subsystem
));
624 css
->pseudo_subchannel
=
625 kzalloc(sizeof(*css
->pseudo_subchannel
), GFP_KERNEL
);
626 if (!css
->pseudo_subchannel
)
628 css
->pseudo_subchannel
->dev
.parent
= &css
->device
;
629 css
->pseudo_subchannel
->dev
.release
= css_subchannel_release
;
630 sprintf(css
->pseudo_subchannel
->dev
.bus_id
, "defunct");
631 ret
= cio_create_sch_lock(css
->pseudo_subchannel
);
633 kfree(css
->pseudo_subchannel
);
636 mutex_init(&css
->mutex
);
639 sprintf(css
->device
.bus_id
, "css%x", nr
);
640 css
->device
.release
= channel_subsystem_release
;
641 tod_high
= (u32
) (get_clock() >> 32);
642 css_generate_pgid(css
, tod_high
);
646 static int css_reboot_event(struct notifier_block
*this,
653 for (i
= 0; i
<= __MAX_CSSID
; i
++) {
654 struct channel_subsystem
*css
;
656 css
= channel_subsystems
[i
];
658 if (chsc_secm(css
, 0))
665 static struct notifier_block css_reboot_notifier
= {
666 .notifier_call
= css_reboot_event
,
670 * Now that the driver core is running, we can setup our channel subsystem.
671 * The struct subchannel's are created during probing (except for the
672 * static console subchannel).
675 init_channel_subsystem (void)
679 ret
= chsc_determine_css_characteristics();
681 goto out
; /* No need to continue. */
683 css_characteristics_avail
= 1;
685 ret
= chsc_alloc_sei_area();
689 ret
= slow_subchannel_init();
693 if ((ret
= bus_register(&css_bus_type
)))
696 /* Try to enable MSS. */
697 ret
= chsc_enable_facility(CHSC_SDA_OC_MSS
);
699 case 0: /* Success. */
700 max_ssid
= __MAX_SSID
;
707 /* Setup css structure. */
708 for (i
= 0; i
<= __MAX_CSSID
; i
++) {
709 struct channel_subsystem
*css
;
711 css
= kmalloc(sizeof(struct channel_subsystem
), GFP_KERNEL
);
716 channel_subsystems
[i
] = css
;
720 ret
= device_register(&css
->device
);
723 if (css_characteristics_avail
&&
724 css_chsc_characteristics
.secm
) {
725 ret
= device_create_file(&css
->device
,
726 &dev_attr_cm_enable
);
730 ret
= device_register(&css
->pseudo_subchannel
->dev
);
734 ret
= register_reboot_notifier(&css_reboot_notifier
);
741 for_each_subchannel(__init_channel_subsystem
, NULL
);
744 device_unregister(&channel_subsystems
[i
]->pseudo_subchannel
->dev
);
746 device_remove_file(&channel_subsystems
[i
]->device
,
747 &dev_attr_cm_enable
);
749 device_unregister(&channel_subsystems
[i
]->device
);
751 kfree(channel_subsystems
[i
]->pseudo_subchannel
->lock
);
752 kfree(channel_subsystems
[i
]->pseudo_subchannel
);
754 kfree(channel_subsystems
[i
]);
757 struct channel_subsystem
*css
;
760 css
= channel_subsystems
[i
];
761 device_unregister(&css
->pseudo_subchannel
->dev
);
762 if (css_characteristics_avail
&& css_chsc_characteristics
.secm
)
763 device_remove_file(&css
->device
,
764 &dev_attr_cm_enable
);
765 device_unregister(&css
->device
);
768 bus_unregister(&css_bus_type
);
770 chsc_free_sei_area();
771 kfree(slow_subchannel_set
);
772 printk(KERN_WARNING
"cio: failed to initialize css driver (%d)!\n",
777 int sch_is_pseudo_sch(struct subchannel
*sch
)
779 return sch
== to_css(sch
->dev
.parent
)->pseudo_subchannel
;
783 * find a driver for a subchannel. They identify by the subchannel
784 * type with the exception that the console subchannel driver has its own
785 * subchannel type although the device is an i/o subchannel
788 css_bus_match (struct device
*dev
, struct device_driver
*drv
)
790 struct subchannel
*sch
= container_of (dev
, struct subchannel
, dev
);
791 struct css_driver
*driver
= container_of (drv
, struct css_driver
, drv
);
793 if (sch
->st
== driver
->subchannel_type
)
800 css_probe (struct device
*dev
)
802 struct subchannel
*sch
;
804 sch
= to_subchannel(dev
);
805 sch
->driver
= container_of (dev
->driver
, struct css_driver
, drv
);
806 return (sch
->driver
->probe
? sch
->driver
->probe(sch
) : 0);
810 css_remove (struct device
*dev
)
812 struct subchannel
*sch
;
814 sch
= to_subchannel(dev
);
815 return (sch
->driver
->remove
? sch
->driver
->remove(sch
) : 0);
819 css_shutdown (struct device
*dev
)
821 struct subchannel
*sch
;
823 sch
= to_subchannel(dev
);
824 if (sch
->driver
->shutdown
)
825 sch
->driver
->shutdown(sch
);
828 struct bus_type css_bus_type
= {
830 .match
= css_bus_match
,
832 .remove
= css_remove
,
833 .shutdown
= css_shutdown
,
836 subsys_initcall(init_channel_subsystem
);
838 MODULE_LICENSE("GPL");
839 EXPORT_SYMBOL(css_bus_type
);
840 EXPORT_SYMBOL_GPL(css_characteristics_avail
);