1 // SPDX-License-Identifier: GPL-2.0
3 * driver for channel subsystem
5 * Copyright IBM Corp. 2002, 2010
7 * Author(s): Arnd Bergmann (arndb@de.ibm.com)
8 * Cornelia Huck (cornelia.huck@de.ibm.com)
11 #define KMSG_COMPONENT "cio"
12 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
14 #include <linux/export.h>
15 #include <linux/init.h>
16 #include <linux/device.h>
17 #include <linux/slab.h>
18 #include <linux/errno.h>
19 #include <linux/list.h>
20 #include <linux/reboot.h>
21 #include <linux/suspend.h>
22 #include <linux/proc_fs.h>
23 #include <linux/genalloc.h>
24 #include <linux/dma-mapping.h>
30 #include "blacklist.h"
31 #include "cio_debug.h"
38 int css_init_done
= 0;
42 struct channel_subsystem
*channel_subsystems
[MAX_CSS_IDX
+ 1];
43 static struct bus_type css_bus_type
;
46 for_each_subchannel(int(*fn
)(struct subchannel_id
, void *), void *data
)
48 struct subchannel_id schid
;
51 init_subchannel_id(&schid
);
54 ret
= fn(schid
, data
);
57 } while (schid
.sch_no
++ < __MAX_SUBCHANNEL
);
59 } while (schid
.ssid
++ < max_ssid
);
66 int (*fn_known_sch
)(struct subchannel
*, void *);
67 int (*fn_unknown_sch
)(struct subchannel_id
, void *);
70 static int call_fn_known_sch(struct device
*dev
, void *data
)
72 struct subchannel
*sch
= to_subchannel(dev
);
73 struct cb_data
*cb
= data
;
77 idset_sch_del(cb
->set
, sch
->schid
);
79 rc
= cb
->fn_known_sch(sch
, cb
->data
);
83 static int call_fn_unknown_sch(struct subchannel_id schid
, void *data
)
85 struct cb_data
*cb
= data
;
88 if (idset_sch_contains(cb
->set
, schid
))
89 rc
= cb
->fn_unknown_sch(schid
, cb
->data
);
93 static int call_fn_all_sch(struct subchannel_id schid
, void *data
)
95 struct cb_data
*cb
= data
;
96 struct subchannel
*sch
;
99 sch
= get_subchannel_by_schid(schid
);
101 if (cb
->fn_known_sch
)
102 rc
= cb
->fn_known_sch(sch
, cb
->data
);
103 put_device(&sch
->dev
);
105 if (cb
->fn_unknown_sch
)
106 rc
= cb
->fn_unknown_sch(schid
, cb
->data
);
112 int for_each_subchannel_staged(int (*fn_known
)(struct subchannel
*, void *),
113 int (*fn_unknown
)(struct subchannel_id
,
120 cb
.fn_known_sch
= fn_known
;
121 cb
.fn_unknown_sch
= fn_unknown
;
123 if (fn_known
&& !fn_unknown
) {
124 /* Skip idset allocation in case of known-only loop. */
126 return bus_for_each_dev(&css_bus_type
, NULL
, &cb
,
130 cb
.set
= idset_sch_new();
132 /* fall back to brute force scanning in case of oom */
133 return for_each_subchannel(call_fn_all_sch
, &cb
);
137 /* Process registered subchannels. */
138 rc
= bus_for_each_dev(&css_bus_type
, NULL
, &cb
, call_fn_known_sch
);
141 /* Process unregistered subchannels. */
143 rc
= for_each_subchannel(call_fn_unknown_sch
, &cb
);
150 static void css_sch_todo(struct work_struct
*work
);
152 static int css_sch_create_locks(struct subchannel
*sch
)
154 sch
->lock
= kmalloc(sizeof(*sch
->lock
), GFP_KERNEL
);
158 spin_lock_init(sch
->lock
);
159 mutex_init(&sch
->reg_mutex
);
164 static void css_subchannel_release(struct device
*dev
)
166 struct subchannel
*sch
= to_subchannel(dev
);
168 sch
->config
.intparm
= 0;
169 cio_commit_config(sch
);
170 kfree(sch
->driver_override
);
175 static int css_validate_subchannel(struct subchannel_id schid
,
180 switch (schib
->pmcw
.st
) {
181 case SUBCHANNEL_TYPE_IO
:
182 case SUBCHANNEL_TYPE_MSG
:
183 if (!css_sch_is_valid(schib
))
185 else if (is_blacklisted(schid
.ssid
, schib
->pmcw
.dev
)) {
186 CIO_MSG_EVENT(6, "Blacklisted device detected "
187 "at devno %04X, subchannel set %x\n",
188 schib
->pmcw
.dev
, schid
.ssid
);
199 CIO_MSG_EVENT(4, "Subchannel 0.%x.%04x reports subchannel type %04X\n",
200 schid
.ssid
, schid
.sch_no
, schib
->pmcw
.st
);
205 struct subchannel
*css_alloc_subchannel(struct subchannel_id schid
,
208 struct subchannel
*sch
;
211 ret
= css_validate_subchannel(schid
, schib
);
215 sch
= kzalloc(sizeof(*sch
), GFP_KERNEL
| GFP_DMA
);
217 return ERR_PTR(-ENOMEM
);
221 sch
->st
= schib
->pmcw
.st
;
223 ret
= css_sch_create_locks(sch
);
227 INIT_WORK(&sch
->todo_work
, css_sch_todo
);
228 sch
->dev
.release
= &css_subchannel_release
;
229 device_initialize(&sch
->dev
);
231 * The physical addresses of some the dma structures that can
232 * belong to a subchannel need to fit 31 bit width (e.g. ccw).
234 sch
->dev
.coherent_dma_mask
= DMA_BIT_MASK(31);
235 sch
->dev
.dma_mask
= &sch
->dev
.coherent_dma_mask
;
243 static int css_sch_device_register(struct subchannel
*sch
)
247 mutex_lock(&sch
->reg_mutex
);
248 dev_set_name(&sch
->dev
, "0.%x.%04x", sch
->schid
.ssid
,
250 ret
= device_add(&sch
->dev
);
251 mutex_unlock(&sch
->reg_mutex
);
256 * css_sch_device_unregister - unregister a subchannel
257 * @sch: subchannel to be unregistered
259 void css_sch_device_unregister(struct subchannel
*sch
)
261 mutex_lock(&sch
->reg_mutex
);
262 if (device_is_registered(&sch
->dev
))
263 device_unregister(&sch
->dev
);
264 mutex_unlock(&sch
->reg_mutex
);
266 EXPORT_SYMBOL_GPL(css_sch_device_unregister
);
268 static void ssd_from_pmcw(struct chsc_ssd_info
*ssd
, struct pmcw
*pmcw
)
273 memset(ssd
, 0, sizeof(struct chsc_ssd_info
));
274 ssd
->path_mask
= pmcw
->pim
;
275 for (i
= 0; i
< 8; i
++) {
277 if (pmcw
->pim
& mask
) {
278 chp_id_init(&ssd
->chpid
[i
]);
279 ssd
->chpid
[i
].id
= pmcw
->chpid
[i
];
284 static void ssd_register_chpids(struct chsc_ssd_info
*ssd
)
289 for (i
= 0; i
< 8; i
++) {
291 if (ssd
->path_mask
& mask
)
292 chp_new(ssd
->chpid
[i
]);
296 void css_update_ssd_info(struct subchannel
*sch
)
300 ret
= chsc_get_ssd_info(sch
->schid
, &sch
->ssd_info
);
302 ssd_from_pmcw(&sch
->ssd_info
, &sch
->schib
.pmcw
);
304 ssd_register_chpids(&sch
->ssd_info
);
307 static ssize_t
type_show(struct device
*dev
, struct device_attribute
*attr
,
310 struct subchannel
*sch
= to_subchannel(dev
);
312 return sprintf(buf
, "%01x\n", sch
->st
);
315 static DEVICE_ATTR_RO(type
);
317 static ssize_t
modalias_show(struct device
*dev
, struct device_attribute
*attr
,
320 struct subchannel
*sch
= to_subchannel(dev
);
322 return sprintf(buf
, "css:t%01X\n", sch
->st
);
325 static DEVICE_ATTR_RO(modalias
);
327 static ssize_t
driver_override_store(struct device
*dev
,
328 struct device_attribute
*attr
,
329 const char *buf
, size_t count
)
331 struct subchannel
*sch
= to_subchannel(dev
);
332 char *driver_override
, *old
, *cp
;
334 /* We need to keep extra room for a newline */
335 if (count
>= (PAGE_SIZE
- 1))
338 driver_override
= kstrndup(buf
, count
, GFP_KERNEL
);
339 if (!driver_override
)
342 cp
= strchr(driver_override
, '\n');
347 old
= sch
->driver_override
;
348 if (strlen(driver_override
)) {
349 sch
->driver_override
= driver_override
;
351 kfree(driver_override
);
352 sch
->driver_override
= NULL
;
361 static ssize_t
driver_override_show(struct device
*dev
,
362 struct device_attribute
*attr
, char *buf
)
364 struct subchannel
*sch
= to_subchannel(dev
);
368 len
= snprintf(buf
, PAGE_SIZE
, "%s\n", sch
->driver_override
);
372 static DEVICE_ATTR_RW(driver_override
);
374 static struct attribute
*subch_attrs
[] = {
376 &dev_attr_modalias
.attr
,
377 &dev_attr_driver_override
.attr
,
381 static struct attribute_group subch_attr_group
= {
382 .attrs
= subch_attrs
,
385 static const struct attribute_group
*default_subch_attr_groups
[] = {
390 static ssize_t
chpids_show(struct device
*dev
,
391 struct device_attribute
*attr
,
394 struct subchannel
*sch
= to_subchannel(dev
);
395 struct chsc_ssd_info
*ssd
= &sch
->ssd_info
;
400 for (chp
= 0; chp
< 8; chp
++) {
402 if (ssd
->path_mask
& mask
)
403 ret
+= sprintf(buf
+ ret
, "%02x ", ssd
->chpid
[chp
].id
);
405 ret
+= sprintf(buf
+ ret
, "00 ");
407 ret
+= sprintf(buf
+ ret
, "\n");
410 static DEVICE_ATTR_RO(chpids
);
412 static ssize_t
pimpampom_show(struct device
*dev
,
413 struct device_attribute
*attr
,
416 struct subchannel
*sch
= to_subchannel(dev
);
417 struct pmcw
*pmcw
= &sch
->schib
.pmcw
;
419 return sprintf(buf
, "%02x %02x %02x\n",
420 pmcw
->pim
, pmcw
->pam
, pmcw
->pom
);
422 static DEVICE_ATTR_RO(pimpampom
);
424 static struct attribute
*io_subchannel_type_attrs
[] = {
425 &dev_attr_chpids
.attr
,
426 &dev_attr_pimpampom
.attr
,
429 ATTRIBUTE_GROUPS(io_subchannel_type
);
431 static const struct device_type io_subchannel_type
= {
432 .groups
= io_subchannel_type_groups
,
435 int css_register_subchannel(struct subchannel
*sch
)
439 /* Initialize the subchannel structure */
440 sch
->dev
.parent
= &channel_subsystems
[0]->device
;
441 sch
->dev
.bus
= &css_bus_type
;
442 sch
->dev
.groups
= default_subch_attr_groups
;
444 if (sch
->st
== SUBCHANNEL_TYPE_IO
)
445 sch
->dev
.type
= &io_subchannel_type
;
448 * We don't want to generate uevents for I/O subchannels that don't
449 * have a working ccw device behind them since they will be
450 * unregistered before they can be used anyway, so we delay the add
451 * uevent until after device recognition was successful.
452 * Note that we suppress the uevent for all subchannel types;
453 * the subchannel driver can decide itself when it wants to inform
454 * userspace of its existence.
456 dev_set_uevent_suppress(&sch
->dev
, 1);
457 css_update_ssd_info(sch
);
458 /* make it known to the system */
459 ret
= css_sch_device_register(sch
);
461 CIO_MSG_EVENT(0, "Could not register sch 0.%x.%04x: %d\n",
462 sch
->schid
.ssid
, sch
->schid
.sch_no
, ret
);
467 * No driver matched. Generate the uevent now so that
468 * a fitting driver module may be loaded based on the
471 dev_set_uevent_suppress(&sch
->dev
, 0);
472 kobject_uevent(&sch
->dev
.kobj
, KOBJ_ADD
);
477 static int css_probe_device(struct subchannel_id schid
, struct schib
*schib
)
479 struct subchannel
*sch
;
482 sch
= css_alloc_subchannel(schid
, schib
);
486 ret
= css_register_subchannel(sch
);
488 put_device(&sch
->dev
);
494 check_subchannel(struct device
*dev
, const void *data
)
496 struct subchannel
*sch
;
497 struct subchannel_id
*schid
= (void *)data
;
499 sch
= to_subchannel(dev
);
500 return schid_equal(&sch
->schid
, schid
);
504 get_subchannel_by_schid(struct subchannel_id schid
)
508 dev
= bus_find_device(&css_bus_type
, NULL
,
509 &schid
, check_subchannel
);
511 return dev
? to_subchannel(dev
) : NULL
;
515 * css_sch_is_valid() - check if a subchannel is valid
516 * @schib: subchannel information block for the subchannel
518 int css_sch_is_valid(struct schib
*schib
)
520 if ((schib
->pmcw
.st
== SUBCHANNEL_TYPE_IO
) && !schib
->pmcw
.dnv
)
522 if ((schib
->pmcw
.st
== SUBCHANNEL_TYPE_MSG
) && !schib
->pmcw
.w
)
526 EXPORT_SYMBOL_GPL(css_sch_is_valid
);
528 static int css_evaluate_new_subchannel(struct subchannel_id schid
, int slow
)
534 /* Will be done on the slow path. */
538 * The first subchannel that is not-operational (ccode==3)
539 * indicates that there aren't any more devices available.
540 * If stsch gets an exception, it means the current subchannel set
543 ccode
= stsch(schid
, &schib
);
545 return (ccode
== 3) ? -ENXIO
: ccode
;
547 return css_probe_device(schid
, &schib
);
550 static int css_evaluate_known_subchannel(struct subchannel
*sch
, int slow
)
555 if (sch
->driver
->sch_event
)
556 ret
= sch
->driver
->sch_event(sch
, slow
);
559 "Got subchannel machine check but "
560 "no sch_event handler provided.\n");
562 if (ret
!= 0 && ret
!= -EAGAIN
) {
563 CIO_MSG_EVENT(2, "eval: sch 0.%x.%04x, rc=%d\n",
564 sch
->schid
.ssid
, sch
->schid
.sch_no
, ret
);
569 static void css_evaluate_subchannel(struct subchannel_id schid
, int slow
)
571 struct subchannel
*sch
;
574 sch
= get_subchannel_by_schid(schid
);
576 ret
= css_evaluate_known_subchannel(sch
, slow
);
577 put_device(&sch
->dev
);
579 ret
= css_evaluate_new_subchannel(schid
, slow
);
581 css_schedule_eval(schid
);
585 * css_sched_sch_todo - schedule a subchannel operation
589 * Schedule the operation identified by @todo to be performed on the slow path
590 * workqueue. Do nothing if another operation with higher priority is already
591 * scheduled. Needs to be called with subchannel lock held.
593 void css_sched_sch_todo(struct subchannel
*sch
, enum sch_todo todo
)
595 CIO_MSG_EVENT(4, "sch_todo: sched sch=0.%x.%04x todo=%d\n",
596 sch
->schid
.ssid
, sch
->schid
.sch_no
, todo
);
597 if (sch
->todo
>= todo
)
599 /* Get workqueue ref. */
600 if (!get_device(&sch
->dev
))
603 if (!queue_work(cio_work_q
, &sch
->todo_work
)) {
604 /* Already queued, release workqueue ref. */
605 put_device(&sch
->dev
);
608 EXPORT_SYMBOL_GPL(css_sched_sch_todo
);
610 static void css_sch_todo(struct work_struct
*work
)
612 struct subchannel
*sch
;
616 sch
= container_of(work
, struct subchannel
, todo_work
);
618 spin_lock_irq(sch
->lock
);
620 CIO_MSG_EVENT(4, "sch_todo: sch=0.%x.%04x, todo=%d\n", sch
->schid
.ssid
,
621 sch
->schid
.sch_no
, todo
);
622 sch
->todo
= SCH_TODO_NOTHING
;
623 spin_unlock_irq(sch
->lock
);
626 case SCH_TODO_NOTHING
:
629 ret
= css_evaluate_known_subchannel(sch
, 1);
630 if (ret
== -EAGAIN
) {
631 spin_lock_irq(sch
->lock
);
632 css_sched_sch_todo(sch
, todo
);
633 spin_unlock_irq(sch
->lock
);
637 css_sch_device_unregister(sch
);
640 /* Release workqueue ref. */
641 put_device(&sch
->dev
);
644 static struct idset
*slow_subchannel_set
;
645 static spinlock_t slow_subchannel_lock
;
646 static wait_queue_head_t css_eval_wq
;
647 static atomic_t css_eval_scheduled
;
649 static int __init
slow_subchannel_init(void)
651 spin_lock_init(&slow_subchannel_lock
);
652 atomic_set(&css_eval_scheduled
, 0);
653 init_waitqueue_head(&css_eval_wq
);
654 slow_subchannel_set
= idset_sch_new();
655 if (!slow_subchannel_set
) {
656 CIO_MSG_EVENT(0, "could not allocate slow subchannel set\n");
662 static int slow_eval_known_fn(struct subchannel
*sch
, void *data
)
667 spin_lock_irq(&slow_subchannel_lock
);
668 eval
= idset_sch_contains(slow_subchannel_set
, sch
->schid
);
669 idset_sch_del(slow_subchannel_set
, sch
->schid
);
670 spin_unlock_irq(&slow_subchannel_lock
);
672 rc
= css_evaluate_known_subchannel(sch
, 1);
674 css_schedule_eval(sch
->schid
);
679 static int slow_eval_unknown_fn(struct subchannel_id schid
, void *data
)
684 spin_lock_irq(&slow_subchannel_lock
);
685 eval
= idset_sch_contains(slow_subchannel_set
, schid
);
686 idset_sch_del(slow_subchannel_set
, schid
);
687 spin_unlock_irq(&slow_subchannel_lock
);
689 rc
= css_evaluate_new_subchannel(schid
, 1);
692 css_schedule_eval(schid
);
698 /* These should abort looping */
699 spin_lock_irq(&slow_subchannel_lock
);
700 idset_sch_del_subseq(slow_subchannel_set
, schid
);
701 spin_unlock_irq(&slow_subchannel_lock
);
706 /* Allow scheduling here since the containing loop might
713 static void css_slow_path_func(struct work_struct
*unused
)
717 CIO_TRACE_EVENT(4, "slowpath");
718 for_each_subchannel_staged(slow_eval_known_fn
, slow_eval_unknown_fn
,
720 spin_lock_irqsave(&slow_subchannel_lock
, flags
);
721 if (idset_is_empty(slow_subchannel_set
)) {
722 atomic_set(&css_eval_scheduled
, 0);
723 wake_up(&css_eval_wq
);
725 spin_unlock_irqrestore(&slow_subchannel_lock
, flags
);
728 static DECLARE_DELAYED_WORK(slow_path_work
, css_slow_path_func
);
729 struct workqueue_struct
*cio_work_q
;
731 void css_schedule_eval(struct subchannel_id schid
)
735 spin_lock_irqsave(&slow_subchannel_lock
, flags
);
736 idset_sch_add(slow_subchannel_set
, schid
);
737 atomic_set(&css_eval_scheduled
, 1);
738 queue_delayed_work(cio_work_q
, &slow_path_work
, 0);
739 spin_unlock_irqrestore(&slow_subchannel_lock
, flags
);
742 void css_schedule_eval_all(void)
746 spin_lock_irqsave(&slow_subchannel_lock
, flags
);
747 idset_fill(slow_subchannel_set
);
748 atomic_set(&css_eval_scheduled
, 1);
749 queue_delayed_work(cio_work_q
, &slow_path_work
, 0);
750 spin_unlock_irqrestore(&slow_subchannel_lock
, flags
);
753 static int __unset_registered(struct device
*dev
, void *data
)
755 struct idset
*set
= data
;
756 struct subchannel
*sch
= to_subchannel(dev
);
758 idset_sch_del(set
, sch
->schid
);
762 void css_schedule_eval_all_unreg(unsigned long delay
)
765 struct idset
*unreg_set
;
767 /* Find unregistered subchannels. */
768 unreg_set
= idset_sch_new();
771 css_schedule_eval_all();
774 idset_fill(unreg_set
);
775 bus_for_each_dev(&css_bus_type
, NULL
, unreg_set
, __unset_registered
);
776 /* Apply to slow_subchannel_set. */
777 spin_lock_irqsave(&slow_subchannel_lock
, flags
);
778 idset_add_set(slow_subchannel_set
, unreg_set
);
779 atomic_set(&css_eval_scheduled
, 1);
780 queue_delayed_work(cio_work_q
, &slow_path_work
, delay
);
781 spin_unlock_irqrestore(&slow_subchannel_lock
, flags
);
782 idset_free(unreg_set
);
785 void css_wait_for_slow_path(void)
787 flush_workqueue(cio_work_q
);
790 /* Schedule reprobing of all unregistered subchannels. */
791 void css_schedule_reprobe(void)
793 /* Schedule with a delay to allow merging of subsequent calls. */
794 css_schedule_eval_all_unreg(1 * HZ
);
796 EXPORT_SYMBOL_GPL(css_schedule_reprobe
);
799 * Called from the machine check handler for subchannel report words.
801 static void css_process_crw(struct crw
*crw0
, struct crw
*crw1
, int overflow
)
803 struct subchannel_id mchk_schid
;
804 struct subchannel
*sch
;
807 css_schedule_eval_all();
810 CIO_CRW_EVENT(2, "CRW0 reports slct=%d, oflw=%d, "
811 "chn=%d, rsc=%X, anc=%d, erc=%X, rsid=%X\n",
812 crw0
->slct
, crw0
->oflw
, crw0
->chn
, crw0
->rsc
, crw0
->anc
,
813 crw0
->erc
, crw0
->rsid
);
815 CIO_CRW_EVENT(2, "CRW1 reports slct=%d, oflw=%d, "
816 "chn=%d, rsc=%X, anc=%d, erc=%X, rsid=%X\n",
817 crw1
->slct
, crw1
->oflw
, crw1
->chn
, crw1
->rsc
,
818 crw1
->anc
, crw1
->erc
, crw1
->rsid
);
819 init_subchannel_id(&mchk_schid
);
820 mchk_schid
.sch_no
= crw0
->rsid
;
822 mchk_schid
.ssid
= (crw1
->rsid
>> 4) & 3;
824 if (crw0
->erc
== CRW_ERC_PMOD
) {
825 sch
= get_subchannel_by_schid(mchk_schid
);
827 css_update_ssd_info(sch
);
828 put_device(&sch
->dev
);
832 * Since we are always presented with IPI in the CRW, we have to
833 * use stsch() to find out if the subchannel in question has come
836 css_evaluate_subchannel(mchk_schid
, 0);
840 css_generate_pgid(struct channel_subsystem
*css
, u32 tod_high
)
844 if (css_general_characteristics
.mcss
) {
845 css
->global_pgid
.pgid_high
.ext_cssid
.version
= 0x80;
846 css
->global_pgid
.pgid_high
.ext_cssid
.cssid
=
847 (css
->cssid
< 0) ? 0 : css
->cssid
;
849 css
->global_pgid
.pgid_high
.cpu_addr
= stap();
852 css
->global_pgid
.cpu_id
= cpu_id
.ident
;
853 css
->global_pgid
.cpu_model
= cpu_id
.machine
;
854 css
->global_pgid
.tod_high
= tod_high
;
857 static void channel_subsystem_release(struct device
*dev
)
859 struct channel_subsystem
*css
= to_css(dev
);
861 mutex_destroy(&css
->mutex
);
865 static ssize_t
real_cssid_show(struct device
*dev
, struct device_attribute
*a
,
868 struct channel_subsystem
*css
= to_css(dev
);
873 return sprintf(buf
, "%x\n", css
->cssid
);
875 static DEVICE_ATTR_RO(real_cssid
);
877 static ssize_t
cm_enable_show(struct device
*dev
, struct device_attribute
*a
,
880 struct channel_subsystem
*css
= to_css(dev
);
883 mutex_lock(&css
->mutex
);
884 ret
= sprintf(buf
, "%x\n", css
->cm_enabled
);
885 mutex_unlock(&css
->mutex
);
889 static ssize_t
cm_enable_store(struct device
*dev
, struct device_attribute
*a
,
890 const char *buf
, size_t count
)
892 struct channel_subsystem
*css
= to_css(dev
);
896 ret
= kstrtoul(buf
, 16, &val
);
899 mutex_lock(&css
->mutex
);
902 ret
= css
->cm_enabled
? chsc_secm(css
, 0) : 0;
905 ret
= css
->cm_enabled
? 0 : chsc_secm(css
, 1);
910 mutex_unlock(&css
->mutex
);
911 return ret
< 0 ? ret
: count
;
913 static DEVICE_ATTR_RW(cm_enable
);
915 static umode_t
cm_enable_mode(struct kobject
*kobj
, struct attribute
*attr
,
918 return css_chsc_characteristics
.secm
? attr
->mode
: 0;
921 static struct attribute
*cssdev_attrs
[] = {
922 &dev_attr_real_cssid
.attr
,
926 static struct attribute_group cssdev_attr_group
= {
927 .attrs
= cssdev_attrs
,
930 static struct attribute
*cssdev_cm_attrs
[] = {
931 &dev_attr_cm_enable
.attr
,
935 static struct attribute_group cssdev_cm_attr_group
= {
936 .attrs
= cssdev_cm_attrs
,
937 .is_visible
= cm_enable_mode
,
940 static const struct attribute_group
*cssdev_attr_groups
[] = {
942 &cssdev_cm_attr_group
,
946 static int __init
setup_css(int nr
)
948 struct channel_subsystem
*css
;
951 css
= kzalloc(sizeof(*css
), GFP_KERNEL
);
955 channel_subsystems
[nr
] = css
;
956 dev_set_name(&css
->device
, "css%x", nr
);
957 css
->device
.groups
= cssdev_attr_groups
;
958 css
->device
.release
= channel_subsystem_release
;
960 * We currently allocate notifier bits with this (using
961 * css->device as the device argument with the DMA API)
962 * and are fine with 64 bit addresses.
964 css
->device
.coherent_dma_mask
= DMA_BIT_MASK(64);
965 css
->device
.dma_mask
= &css
->device
.coherent_dma_mask
;
967 mutex_init(&css
->mutex
);
968 css
->cssid
= chsc_get_cssid(nr
);
969 css_generate_pgid(css
, (u32
) (get_tod_clock() >> 32));
971 ret
= device_register(&css
->device
);
973 put_device(&css
->device
);
977 css
->pseudo_subchannel
= kzalloc(sizeof(*css
->pseudo_subchannel
),
979 if (!css
->pseudo_subchannel
) {
980 device_unregister(&css
->device
);
985 css
->pseudo_subchannel
->dev
.parent
= &css
->device
;
986 css
->pseudo_subchannel
->dev
.release
= css_subchannel_release
;
987 mutex_init(&css
->pseudo_subchannel
->reg_mutex
);
988 ret
= css_sch_create_locks(css
->pseudo_subchannel
);
990 kfree(css
->pseudo_subchannel
);
991 device_unregister(&css
->device
);
995 dev_set_name(&css
->pseudo_subchannel
->dev
, "defunct");
996 ret
= device_register(&css
->pseudo_subchannel
->dev
);
998 put_device(&css
->pseudo_subchannel
->dev
);
999 device_unregister(&css
->device
);
1005 channel_subsystems
[nr
] = NULL
;
1009 static int css_reboot_event(struct notifier_block
*this,
1010 unsigned long event
,
1013 struct channel_subsystem
*css
;
1018 mutex_lock(&css
->mutex
);
1019 if (css
->cm_enabled
)
1020 if (chsc_secm(css
, 0))
1022 mutex_unlock(&css
->mutex
);
1028 static struct notifier_block css_reboot_notifier
= {
1029 .notifier_call
= css_reboot_event
,
1033 * Since the css devices are neither on a bus nor have a class
1034 * nor have a special device type, we cannot stop/restart channel
1035 * path measurements via the normal suspend/resume callbacks, but have
1038 static int css_power_event(struct notifier_block
*this, unsigned long event
,
1041 struct channel_subsystem
*css
;
1045 case PM_HIBERNATION_PREPARE
:
1046 case PM_SUSPEND_PREPARE
:
1049 mutex_lock(&css
->mutex
);
1050 if (!css
->cm_enabled
) {
1051 mutex_unlock(&css
->mutex
);
1054 ret
= __chsc_do_secm(css
, 0);
1055 ret
= notifier_from_errno(ret
);
1056 mutex_unlock(&css
->mutex
);
1059 case PM_POST_HIBERNATION
:
1060 case PM_POST_SUSPEND
:
1063 mutex_lock(&css
->mutex
);
1064 if (!css
->cm_enabled
) {
1065 mutex_unlock(&css
->mutex
);
1068 ret
= __chsc_do_secm(css
, 1);
1069 ret
= notifier_from_errno(ret
);
1070 mutex_unlock(&css
->mutex
);
1072 /* search for subchannels, which appeared during hibernation */
1073 css_schedule_reprobe();
1081 static struct notifier_block css_power_notifier
= {
1082 .notifier_call
= css_power_event
,
1085 #define CIO_DMA_GFP (GFP_KERNEL | __GFP_ZERO)
1086 static struct gen_pool
*cio_dma_pool
;
1088 /* Currently cio supports only a single css */
1089 struct device
*cio_get_dma_css_dev(void)
1091 return &channel_subsystems
[0]->device
;
1094 struct gen_pool
*cio_gp_dma_create(struct device
*dma_dev
, int nr_pages
)
1096 struct gen_pool
*gp_dma
;
1098 dma_addr_t dma_addr
;
1101 gp_dma
= gen_pool_create(3, -1);
1104 for (i
= 0; i
< nr_pages
; ++i
) {
1105 cpu_addr
= dma_alloc_coherent(dma_dev
, PAGE_SIZE
, &dma_addr
,
1109 gen_pool_add_virt(gp_dma
, (unsigned long) cpu_addr
,
1110 dma_addr
, PAGE_SIZE
, -1);
1115 static void __gp_dma_free_dma(struct gen_pool
*pool
,
1116 struct gen_pool_chunk
*chunk
, void *data
)
1118 size_t chunk_size
= chunk
->end_addr
- chunk
->start_addr
+ 1;
1120 dma_free_coherent((struct device
*) data
, chunk_size
,
1121 (void *) chunk
->start_addr
,
1122 (dma_addr_t
) chunk
->phys_addr
);
1125 void cio_gp_dma_destroy(struct gen_pool
*gp_dma
, struct device
*dma_dev
)
1129 /* this is quite ugly but no better idea */
1130 gen_pool_for_each_chunk(gp_dma
, __gp_dma_free_dma
, dma_dev
);
1131 gen_pool_destroy(gp_dma
);
1134 static int cio_dma_pool_init(void)
1136 /* No need to free up the resources: compiled in */
1137 cio_dma_pool
= cio_gp_dma_create(cio_get_dma_css_dev(), 1);
1143 void *cio_gp_dma_zalloc(struct gen_pool
*gp_dma
, struct device
*dma_dev
,
1146 dma_addr_t dma_addr
;
1152 addr
= gen_pool_alloc(gp_dma
, size
);
1154 chunk_size
= round_up(size
, PAGE_SIZE
);
1155 addr
= (unsigned long) dma_alloc_coherent(dma_dev
,
1156 chunk_size
, &dma_addr
, CIO_DMA_GFP
);
1159 gen_pool_add_virt(gp_dma
, addr
, dma_addr
, chunk_size
, -1);
1160 addr
= gen_pool_alloc(gp_dma
, size
);
1162 return (void *) addr
;
1165 void cio_gp_dma_free(struct gen_pool
*gp_dma
, void *cpu_addr
, size_t size
)
1169 memset(cpu_addr
, 0, size
);
1170 gen_pool_free(gp_dma
, (unsigned long) cpu_addr
, size
);
1174 * Allocate dma memory from the css global pool. Intended for memory not
1175 * specific to any single device within the css. The allocated memory
1176 * is not guaranteed to be 31-bit addressable.
1178 * Caution: Not suitable for early stuff like console.
1180 void *cio_dma_zalloc(size_t size
)
1182 return cio_gp_dma_zalloc(cio_dma_pool
, cio_get_dma_css_dev(), size
);
1185 void cio_dma_free(void *cpu_addr
, size_t size
)
1187 cio_gp_dma_free(cio_dma_pool
, cpu_addr
, size
);
1191 * Now that the driver core is running, we can setup our channel subsystem.
1192 * The struct subchannel's are created during probing.
1194 static int __init
css_bus_init(void)
1202 chsc_determine_css_characteristics();
1203 /* Try to enable MSS. */
1204 ret
= chsc_enable_facility(CHSC_SDA_OC_MSS
);
1208 max_ssid
= __MAX_SSID
;
1210 ret
= slow_subchannel_init();
1214 ret
= crw_register_handler(CRW_RSC_SCH
, css_process_crw
);
1218 if ((ret
= bus_register(&css_bus_type
)))
1221 /* Setup css structure. */
1222 for (i
= 0; i
<= MAX_CSS_IDX
; i
++) {
1225 goto out_unregister
;
1227 ret
= register_reboot_notifier(&css_reboot_notifier
);
1229 goto out_unregister
;
1230 ret
= register_pm_notifier(&css_power_notifier
);
1232 goto out_unregister_rn
;
1233 ret
= cio_dma_pool_init();
1235 goto out_unregister_pmn
;
1239 /* Enable default isc for I/O subchannels. */
1240 isc_register(IO_SCH_ISC
);
1244 unregister_pm_notifier(&css_power_notifier
);
1246 unregister_reboot_notifier(&css_reboot_notifier
);
1249 struct channel_subsystem
*css
= channel_subsystems
[i
];
1250 device_unregister(&css
->pseudo_subchannel
->dev
);
1251 device_unregister(&css
->device
);
1253 bus_unregister(&css_bus_type
);
1255 crw_unregister_handler(CRW_RSC_SCH
);
1256 idset_free(slow_subchannel_set
);
1257 chsc_init_cleanup();
1258 pr_alert("The CSS device driver initialization failed with "
1263 static void __init
css_bus_cleanup(void)
1265 struct channel_subsystem
*css
;
1268 device_unregister(&css
->pseudo_subchannel
->dev
);
1269 device_unregister(&css
->device
);
1271 bus_unregister(&css_bus_type
);
1272 crw_unregister_handler(CRW_RSC_SCH
);
1273 idset_free(slow_subchannel_set
);
1274 chsc_init_cleanup();
1275 isc_unregister(IO_SCH_ISC
);
1278 static int __init
channel_subsystem_init(void)
1282 ret
= css_bus_init();
1285 cio_work_q
= create_singlethread_workqueue("cio");
1290 ret
= io_subchannel_init();
1294 /* Register subchannels which are already in use. */
1295 cio_register_early_subchannels();
1296 /* Start initial subchannel evaluation. */
1297 css_schedule_eval_all();
1301 destroy_workqueue(cio_work_q
);
1306 subsys_initcall(channel_subsystem_init
);
1308 static int css_settle(struct device_driver
*drv
, void *unused
)
1310 struct css_driver
*cssdrv
= to_cssdriver(drv
);
1313 return cssdrv
->settle();
1317 int css_complete_work(void)
1321 /* Wait for the evaluation of subchannels to finish. */
1322 ret
= wait_event_interruptible(css_eval_wq
,
1323 atomic_read(&css_eval_scheduled
) == 0);
1326 flush_workqueue(cio_work_q
);
1327 /* Wait for the subchannel type specific initialization to finish */
1328 return bus_for_each_drv(&css_bus_type
, NULL
, NULL
, css_settle
);
1333 * Wait for the initialization of devices to finish, to make sure we are
1334 * done with our setup if the search for the root device starts.
1336 static int __init
channel_subsystem_init_sync(void)
1338 css_complete_work();
1341 subsys_initcall_sync(channel_subsystem_init_sync
);
1343 void channel_subsystem_reinit(void)
1345 struct channel_path
*chp
;
1346 struct chp_id chpid
;
1348 chsc_enable_facility(CHSC_SDA_OC_MSS
);
1349 chp_id_for_each(&chpid
) {
1350 chp
= chpid_to_chp(chpid
);
1352 chp_update_desc(chp
);
1357 #ifdef CONFIG_PROC_FS
1358 static ssize_t
cio_settle_write(struct file
*file
, const char __user
*buf
,
1359 size_t count
, loff_t
*ppos
)
1363 /* Handle pending CRW's. */
1364 crw_wait_for_channel_report();
1365 ret
= css_complete_work();
1367 return ret
? ret
: count
;
1370 static const struct file_operations cio_settle_proc_fops
= {
1371 .open
= nonseekable_open
,
1372 .write
= cio_settle_write
,
1373 .llseek
= no_llseek
,
1376 static int __init
cio_settle_init(void)
1378 struct proc_dir_entry
*entry
;
1380 entry
= proc_create("cio_settle", S_IWUSR
, NULL
,
1381 &cio_settle_proc_fops
);
1386 device_initcall(cio_settle_init
);
1387 #endif /*CONFIG_PROC_FS*/
1389 int sch_is_pseudo_sch(struct subchannel
*sch
)
1391 return sch
== to_css(sch
->dev
.parent
)->pseudo_subchannel
;
1394 static int css_bus_match(struct device
*dev
, struct device_driver
*drv
)
1396 struct subchannel
*sch
= to_subchannel(dev
);
1397 struct css_driver
*driver
= to_cssdriver(drv
);
1398 struct css_device_id
*id
;
1400 /* When driver_override is set, only bind to the matching driver */
1401 if (sch
->driver_override
&& strcmp(sch
->driver_override
, drv
->name
))
1404 for (id
= driver
->subchannel_type
; id
->match_flags
; id
++) {
1405 if (sch
->st
== id
->type
)
1412 static int css_probe(struct device
*dev
)
1414 struct subchannel
*sch
;
1417 sch
= to_subchannel(dev
);
1418 sch
->driver
= to_cssdriver(dev
->driver
);
1419 ret
= sch
->driver
->probe
? sch
->driver
->probe(sch
) : 0;
1425 static int css_remove(struct device
*dev
)
1427 struct subchannel
*sch
;
1430 sch
= to_subchannel(dev
);
1431 ret
= sch
->driver
->remove
? sch
->driver
->remove(sch
) : 0;
1436 static void css_shutdown(struct device
*dev
)
1438 struct subchannel
*sch
;
1440 sch
= to_subchannel(dev
);
1441 if (sch
->driver
&& sch
->driver
->shutdown
)
1442 sch
->driver
->shutdown(sch
);
1445 static int css_uevent(struct device
*dev
, struct kobj_uevent_env
*env
)
1447 struct subchannel
*sch
= to_subchannel(dev
);
1450 ret
= add_uevent_var(env
, "ST=%01X", sch
->st
);
1453 ret
= add_uevent_var(env
, "MODALIAS=css:t%01X", sch
->st
);
1457 static int css_pm_prepare(struct device
*dev
)
1459 struct subchannel
*sch
= to_subchannel(dev
);
1460 struct css_driver
*drv
;
1462 if (mutex_is_locked(&sch
->reg_mutex
))
1464 if (!sch
->dev
.driver
)
1466 drv
= to_cssdriver(sch
->dev
.driver
);
1467 /* Notify drivers that they may not register children. */
1468 return drv
->prepare
? drv
->prepare(sch
) : 0;
1471 static void css_pm_complete(struct device
*dev
)
1473 struct subchannel
*sch
= to_subchannel(dev
);
1474 struct css_driver
*drv
;
1476 if (!sch
->dev
.driver
)
1478 drv
= to_cssdriver(sch
->dev
.driver
);
1483 static int css_pm_freeze(struct device
*dev
)
1485 struct subchannel
*sch
= to_subchannel(dev
);
1486 struct css_driver
*drv
;
1488 if (!sch
->dev
.driver
)
1490 drv
= to_cssdriver(sch
->dev
.driver
);
1491 return drv
->freeze
? drv
->freeze(sch
) : 0;
1494 static int css_pm_thaw(struct device
*dev
)
1496 struct subchannel
*sch
= to_subchannel(dev
);
1497 struct css_driver
*drv
;
1499 if (!sch
->dev
.driver
)
1501 drv
= to_cssdriver(sch
->dev
.driver
);
1502 return drv
->thaw
? drv
->thaw(sch
) : 0;
1505 static int css_pm_restore(struct device
*dev
)
1507 struct subchannel
*sch
= to_subchannel(dev
);
1508 struct css_driver
*drv
;
1510 css_update_ssd_info(sch
);
1511 if (!sch
->dev
.driver
)
1513 drv
= to_cssdriver(sch
->dev
.driver
);
1514 return drv
->restore
? drv
->restore(sch
) : 0;
1517 static const struct dev_pm_ops css_pm_ops
= {
1518 .prepare
= css_pm_prepare
,
1519 .complete
= css_pm_complete
,
1520 .freeze
= css_pm_freeze
,
1521 .thaw
= css_pm_thaw
,
1522 .restore
= css_pm_restore
,
1525 static struct bus_type css_bus_type
= {
1527 .match
= css_bus_match
,
1529 .remove
= css_remove
,
1530 .shutdown
= css_shutdown
,
1531 .uevent
= css_uevent
,
1536 * css_driver_register - register a css driver
1537 * @cdrv: css driver to register
1539 * This is mainly a wrapper around driver_register that sets name
1540 * and bus_type in the embedded struct device_driver correctly.
1542 int css_driver_register(struct css_driver
*cdrv
)
1544 cdrv
->drv
.bus
= &css_bus_type
;
1545 return driver_register(&cdrv
->drv
);
1547 EXPORT_SYMBOL_GPL(css_driver_register
);
1550 * css_driver_unregister - unregister a css driver
1551 * @cdrv: css driver to unregister
1553 * This is a wrapper around driver_unregister.
1555 void css_driver_unregister(struct css_driver
*cdrv
)
1557 driver_unregister(&cdrv
->drv
);
1559 EXPORT_SYMBOL_GPL(css_driver_unregister
);