1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright IBM Corp. 1999, 2010
4 * Author(s): Cornelia Huck (cornelia.huck@de.ibm.com)
5 * Arnd Bergmann (arndb@de.ibm.com)
6 * Peter Oberparleiter <peter.oberparleiter@de.ibm.com>
10 #include <linux/workqueue.h>
11 #include <linux/spinlock.h>
12 #include <linux/export.h>
13 #include <linux/sched.h>
14 #include <linux/init.h>
15 #include <linux/jiffies.h>
16 #include <linux/wait.h>
17 #include <linux/mutex.h>
18 #include <linux/errno.h>
19 #include <linux/slab.h>
20 #include <asm/chpid.h>
27 #include "cio_debug.h"
30 #define to_channelpath(device) container_of(device, struct channel_path, dev)
31 #define CHP_INFO_UPDATE_INTERVAL 1*HZ
39 /* Map for pending configure tasks. */
40 static enum cfg_task_t chp_cfg_task
[__MAX_CSSID
+ 1][__MAX_CHPID
+ 1];
41 static DEFINE_SPINLOCK(cfg_lock
);
43 /* Map for channel-path status. */
44 static struct sclp_chp_info chp_info
;
45 static DEFINE_MUTEX(info_lock
);
47 /* Time after which channel-path status may be outdated. */
48 static unsigned long chp_info_expires
;
50 static struct work_struct cfg_work
;
52 /* Wait queue for configure completion events. */
53 static DECLARE_WAIT_QUEUE_HEAD(cfg_wait_queue
);
55 /* Set vary state for given chpid. */
56 static void set_chp_logically_online(struct chp_id chpid
, int onoff
)
58 chpid_to_chp(chpid
)->state
= onoff
;
61 /* On success return 0 if channel-path is varied offline, 1 if it is varied
62 * online. Return -ENODEV if channel-path is not registered. */
63 int chp_get_status(struct chp_id chpid
)
65 return (chpid_to_chp(chpid
) ? chpid_to_chp(chpid
)->state
: -ENODEV
);
69 * chp_get_sch_opm - return opm for subchannel
72 * Calculate and return the operational path mask (opm) based on the chpids
73 * used by the subchannel and the status of the associated channel-paths.
75 u8
chp_get_sch_opm(struct subchannel
*sch
)
83 for (i
= 0; i
< 8; i
++) {
85 chpid
.id
= sch
->schib
.pmcw
.chpid
[i
];
86 if (chp_get_status(chpid
) != 0)
91 EXPORT_SYMBOL_GPL(chp_get_sch_opm
);
94 * chp_is_registered - check if a channel-path is registered
95 * @chpid: channel-path ID
97 * Return non-zero if a channel-path with the given chpid is registered,
100 int chp_is_registered(struct chp_id chpid
)
102 return chpid_to_chp(chpid
) != NULL
;
106 * Function: s390_vary_chpid
107 * Varies the specified chpid online or offline
109 static int s390_vary_chpid(struct chp_id chpid
, int on
)
114 sprintf(dbf_text
, on
?"varyon%x.%02x":"varyoff%x.%02x", chpid
.cssid
,
116 CIO_TRACE_EVENT(2, dbf_text
);
118 status
= chp_get_status(chpid
);
122 set_chp_logically_online(chpid
, on
);
123 chsc_chp_vary(chpid
, on
);
128 * Channel measurement related functions
130 static ssize_t
measurement_chars_read(struct file
*filp
, struct kobject
*kobj
,
131 struct bin_attribute
*bin_attr
,
132 char *buf
, loff_t off
, size_t count
)
134 struct channel_path
*chp
;
135 struct device
*device
;
137 device
= kobj_to_dev(kobj
);
138 chp
= to_channelpath(device
);
142 return memory_read_from_buffer(buf
, count
, &off
, &chp
->cmg_chars
,
143 sizeof(chp
->cmg_chars
));
145 static BIN_ATTR_ADMIN_RO(measurement_chars
, sizeof(struct cmg_chars
));
147 static ssize_t
measurement_chars_full_read(struct file
*filp
,
148 struct kobject
*kobj
,
149 struct bin_attribute
*bin_attr
,
150 char *buf
, loff_t off
, size_t count
)
152 struct channel_path
*chp
= to_channelpath(kobj_to_dev(kobj
));
154 return memory_read_from_buffer(buf
, count
, &off
, &chp
->cmcb
,
157 static BIN_ATTR_ADMIN_RO(measurement_chars_full
, sizeof(struct cmg_cmcb
));
159 static ssize_t
chp_measurement_copy_block(void *buf
, loff_t off
, size_t count
,
160 struct kobject
*kobj
, bool extended
)
162 struct channel_path
*chp
;
163 struct channel_subsystem
*css
;
164 struct device
*device
;
169 device
= kobj_to_dev(kobj
);
170 chp
= to_channelpath(device
);
171 css
= to_css(chp
->dev
.parent
);
175 /* Check if extended measurement data is available. */
179 size
= sizeof(struct cmg_ext_entry
);
180 area
= css
->ecub
[id
/ CSS_ECUES_PER_PAGE
];
181 idx
= id
% CSS_ECUES_PER_PAGE
;
183 size
= sizeof(struct cmg_entry
);
184 area
= css
->cub
[id
/ CSS_CUES_PER_PAGE
];
185 idx
= id
% CSS_CUES_PER_PAGE
;
187 entry
= area
+ (idx
* size
);
189 /* Only allow single reads. */
190 if (off
|| count
< size
)
193 memcpy(buf
, entry
, size
);
198 static ssize_t
measurement_read(struct file
*filp
, struct kobject
*kobj
,
199 struct bin_attribute
*bin_attr
,
200 char *buf
, loff_t off
, size_t count
)
202 return chp_measurement_copy_block(buf
, off
, count
, kobj
, false);
204 static BIN_ATTR_ADMIN_RO(measurement
, sizeof(struct cmg_entry
));
206 static ssize_t
ext_measurement_read(struct file
*filp
, struct kobject
*kobj
,
207 struct bin_attribute
*bin_attr
,
208 char *buf
, loff_t off
, size_t count
)
210 return chp_measurement_copy_block(buf
, off
, count
, kobj
, true);
212 static BIN_ATTR_ADMIN_RO(ext_measurement
, sizeof(struct cmg_ext_entry
));
214 static struct bin_attribute
*measurement_attrs
[] = {
215 &bin_attr_measurement_chars
,
216 &bin_attr_measurement_chars_full
,
217 &bin_attr_measurement
,
218 &bin_attr_ext_measurement
,
221 BIN_ATTRIBUTE_GROUPS(measurement
);
223 void chp_remove_cmg_attr(struct channel_path
*chp
)
225 device_remove_groups(&chp
->dev
, measurement_groups
);
228 int chp_add_cmg_attr(struct channel_path
*chp
)
230 return device_add_groups(&chp
->dev
, measurement_groups
);
234 * Files for the channel path entries.
236 static ssize_t
chp_status_show(struct device
*dev
,
237 struct device_attribute
*attr
, char *buf
)
239 struct channel_path
*chp
= to_channelpath(dev
);
242 mutex_lock(&chp
->lock
);
244 mutex_unlock(&chp
->lock
);
246 return status
? sysfs_emit(buf
, "online\n") : sysfs_emit(buf
, "offline\n");
249 static ssize_t
chp_status_write(struct device
*dev
,
250 struct device_attribute
*attr
,
251 const char *buf
, size_t count
)
253 struct channel_path
*cp
= to_channelpath(dev
);
258 num_args
= sscanf(buf
, "%5s", cmd
);
262 /* Wait until previous actions have settled. */
263 css_wait_for_slow_path();
265 if (!strncasecmp(cmd
, "on", 2) || !strcmp(cmd
, "1")) {
266 mutex_lock(&cp
->lock
);
267 error
= s390_vary_chpid(cp
->chpid
, 1);
268 mutex_unlock(&cp
->lock
);
269 } else if (!strncasecmp(cmd
, "off", 3) || !strcmp(cmd
, "0")) {
270 mutex_lock(&cp
->lock
);
271 error
= s390_vary_chpid(cp
->chpid
, 0);
272 mutex_unlock(&cp
->lock
);
276 return error
< 0 ? error
: count
;
279 static DEVICE_ATTR(status
, 0644, chp_status_show
, chp_status_write
);
281 static ssize_t
chp_configure_show(struct device
*dev
,
282 struct device_attribute
*attr
, char *buf
)
284 struct channel_path
*cp
;
287 cp
= to_channelpath(dev
);
288 status
= chp_info_get_status(cp
->chpid
);
292 return sysfs_emit(buf
, "%d\n", status
);
295 static int cfg_wait_idle(void);
297 static ssize_t
chp_configure_write(struct device
*dev
,
298 struct device_attribute
*attr
,
299 const char *buf
, size_t count
)
301 struct channel_path
*cp
;
305 if (sscanf(buf
, "%d %c", &val
, &delim
) != 1)
307 if (val
!= 0 && val
!= 1)
309 cp
= to_channelpath(dev
);
310 chp_cfg_schedule(cp
->chpid
, val
);
316 static DEVICE_ATTR(configure
, 0644, chp_configure_show
, chp_configure_write
);
318 static ssize_t
chp_type_show(struct device
*dev
, struct device_attribute
*attr
,
321 struct channel_path
*chp
= to_channelpath(dev
);
324 mutex_lock(&chp
->lock
);
325 type
= chp
->desc
.desc
;
326 mutex_unlock(&chp
->lock
);
327 return sysfs_emit(buf
, "%x\n", type
);
330 static DEVICE_ATTR(type
, 0444, chp_type_show
, NULL
);
332 static ssize_t
chp_cmg_show(struct device
*dev
, struct device_attribute
*attr
,
335 struct channel_path
*chp
= to_channelpath(dev
);
339 if (chp
->cmg
== -1) /* channel measurements not available */
340 return sysfs_emit(buf
, "unknown\n");
341 return sysfs_emit(buf
, "%d\n", chp
->cmg
);
344 static DEVICE_ATTR(cmg
, 0444, chp_cmg_show
, NULL
);
346 static ssize_t
chp_shared_show(struct device
*dev
,
347 struct device_attribute
*attr
, char *buf
)
349 struct channel_path
*chp
= to_channelpath(dev
);
353 if (chp
->shared
== -1) /* channel measurements not available */
354 return sysfs_emit(buf
, "unknown\n");
355 return sysfs_emit(buf
, "%x\n", chp
->shared
);
358 static DEVICE_ATTR(shared
, 0444, chp_shared_show
, NULL
);
360 static ssize_t
chp_chid_show(struct device
*dev
, struct device_attribute
*attr
,
363 struct channel_path
*chp
= to_channelpath(dev
);
366 mutex_lock(&chp
->lock
);
367 if (chp
->desc_fmt1
.flags
& 0x10)
368 rc
= sysfs_emit(buf
, "%04x\n", chp
->desc_fmt1
.chid
);
371 mutex_unlock(&chp
->lock
);
375 static DEVICE_ATTR(chid
, 0444, chp_chid_show
, NULL
);
377 static ssize_t
chp_chid_external_show(struct device
*dev
,
378 struct device_attribute
*attr
, char *buf
)
380 struct channel_path
*chp
= to_channelpath(dev
);
383 mutex_lock(&chp
->lock
);
384 if (chp
->desc_fmt1
.flags
& 0x10)
385 rc
= sysfs_emit(buf
, "%x\n", chp
->desc_fmt1
.flags
& 0x8 ? 1 : 0);
388 mutex_unlock(&chp
->lock
);
392 static DEVICE_ATTR(chid_external
, 0444, chp_chid_external_show
, NULL
);
394 static ssize_t
chp_esc_show(struct device
*dev
,
395 struct device_attribute
*attr
, char *buf
)
397 struct channel_path
*chp
= to_channelpath(dev
);
400 mutex_lock(&chp
->lock
);
401 rc
= sysfs_emit(buf
, "%x\n", chp
->desc_fmt1
.esc
);
402 mutex_unlock(&chp
->lock
);
406 static DEVICE_ATTR(esc
, 0444, chp_esc_show
, NULL
);
408 static char apply_max_suffix(unsigned long *value
, unsigned long base
)
410 static char suffixes
[] = { 0, 'K', 'M', 'G', 'T' };
413 for (i
= 0; i
< ARRAY_SIZE(suffixes
) - 1; i
++) {
414 if (*value
< base
|| *value
% base
!= 0)
422 static ssize_t
speed_bps_show(struct device
*dev
,
423 struct device_attribute
*attr
, char *buf
)
425 struct channel_path
*chp
= to_channelpath(dev
);
426 unsigned long speed
= chp
->speed
;
429 suffix
= apply_max_suffix(&speed
, 1000);
431 return suffix
? sysfs_emit(buf
, "%lu%c\n", speed
, suffix
) :
432 sysfs_emit(buf
, "%lu\n", speed
);
435 static DEVICE_ATTR_RO(speed_bps
);
437 static ssize_t
util_string_read(struct file
*filp
, struct kobject
*kobj
,
438 struct bin_attribute
*attr
, char *buf
,
439 loff_t off
, size_t count
)
441 struct channel_path
*chp
= to_channelpath(kobj_to_dev(kobj
));
444 mutex_lock(&chp
->lock
);
445 rc
= memory_read_from_buffer(buf
, count
, &off
, chp
->desc_fmt3
.util_str
,
446 sizeof(chp
->desc_fmt3
.util_str
));
447 mutex_unlock(&chp
->lock
);
451 static BIN_ATTR_RO(util_string
,
452 sizeof(((struct channel_path_desc_fmt3
*)0)->util_str
));
454 static struct bin_attribute
*chp_bin_attrs
[] = {
455 &bin_attr_util_string
,
459 static struct attribute
*chp_attrs
[] = {
460 &dev_attr_status
.attr
,
461 &dev_attr_configure
.attr
,
464 &dev_attr_shared
.attr
,
466 &dev_attr_chid_external
.attr
,
468 &dev_attr_speed_bps
.attr
,
471 static struct attribute_group chp_attr_group
= {
473 .bin_attrs
= chp_bin_attrs
,
475 static const struct attribute_group
*chp_attr_groups
[] = {
480 static void chp_release(struct device
*dev
)
482 struct channel_path
*cp
;
484 cp
= to_channelpath(dev
);
489 * chp_update_desc - update channel-path description
492 * Update the channel-path description of the specified channel-path
493 * including channel measurement related information.
494 * Return zero on success, non-zero otherwise.
496 int chp_update_desc(struct channel_path
*chp
)
500 rc
= chsc_determine_fmt0_channel_path_desc(chp
->chpid
, &chp
->desc
);
505 * Fetching the following data is optional. Not all machines or
506 * hypervisors implement the required chsc commands.
508 chsc_determine_fmt1_channel_path_desc(chp
->chpid
, &chp
->desc_fmt1
);
509 chsc_determine_fmt3_channel_path_desc(chp
->chpid
, &chp
->desc_fmt3
);
510 chsc_get_channel_measurement_chars(chp
);
516 * chp_new - register a new channel-path
517 * @chpid: channel-path ID
519 * Create and register data structure representing new channel-path. Return
520 * zero on success, non-zero otherwise.
522 int chp_new(struct chp_id chpid
)
524 struct channel_subsystem
*css
= css_by_id(chpid
.cssid
);
525 struct channel_path
*chp
;
528 mutex_lock(&css
->mutex
);
529 if (chp_is_registered(chpid
))
532 chp
= kzalloc(sizeof(struct channel_path
), GFP_KERNEL
);
537 /* fill in status, etc. */
540 chp
->dev
.parent
= &css
->device
;
541 chp
->dev
.groups
= chp_attr_groups
;
542 chp
->dev
.release
= chp_release
;
543 mutex_init(&chp
->lock
);
545 /* Obtain channel path description and fill it in. */
546 ret
= chp_update_desc(chp
);
549 if ((chp
->desc
.flags
& 0x80) == 0) {
553 dev_set_name(&chp
->dev
, "chp%x.%02x", chpid
.cssid
, chpid
.id
);
555 /* make it known to the system */
556 ret
= device_register(&chp
->dev
);
558 CIO_MSG_EVENT(0, "Could not register chp%x.%02x: %d\n",
559 chpid
.cssid
, chpid
.id
, ret
);
560 put_device(&chp
->dev
);
564 if (css
->cm_enabled
) {
565 ret
= chp_add_cmg_attr(chp
);
567 device_unregister(&chp
->dev
);
571 css
->chps
[chpid
.id
] = chp
;
576 mutex_unlock(&css
->mutex
);
581 * chp_get_chp_desc - return newly allocated channel-path description
582 * @chpid: channel-path ID
584 * On success return a newly allocated copy of the channel-path description
585 * data associated with the given channel-path ID. Return %NULL on error.
587 struct channel_path_desc_fmt0
*chp_get_chp_desc(struct chp_id chpid
)
589 struct channel_path
*chp
;
590 struct channel_path_desc_fmt0
*desc
;
592 chp
= chpid_to_chp(chpid
);
595 desc
= kmalloc(sizeof(*desc
), GFP_KERNEL
);
599 mutex_lock(&chp
->lock
);
600 memcpy(desc
, &chp
->desc
, sizeof(*desc
));
601 mutex_unlock(&chp
->lock
);
606 * chp_process_crw - process channel-path status change
607 * @crw0: channel report-word to handler
608 * @crw1: second channel-report word (always NULL)
609 * @overflow: crw overflow indication
611 * Handle channel-report-words indicating that the status of a channel-path
614 static void chp_process_crw(struct crw
*crw0
, struct crw
*crw1
,
620 css_schedule_eval_all();
623 CIO_CRW_EVENT(2, "CRW reports slct=%d, oflw=%d, "
624 "chn=%d, rsc=%X, anc=%d, erc=%X, rsid=%X\n",
625 crw0
->slct
, crw0
->oflw
, crw0
->chn
, crw0
->rsc
, crw0
->anc
,
626 crw0
->erc
, crw0
->rsid
);
628 * Check for solicited machine checks. These are
629 * created by reset channel path and need not be
633 CIO_CRW_EVENT(2, "solicited machine check for "
634 "channel path %02X\n", crw0
->rsid
);
638 chpid
.id
= crw0
->rsid
;
640 case CRW_ERC_IPARM
: /* Path has come. */
643 chsc_chp_online(chpid
);
645 case CRW_ERC_PERRI
: /* Path has gone. */
647 chsc_chp_offline(chpid
);
650 CIO_CRW_EVENT(2, "Don't know how to handle erc=%x\n",
655 int chp_ssd_get_mask(struct chsc_ssd_info
*ssd
, struct chp_link
*link
)
660 for (i
= 0; i
< 8; i
++) {
662 if (!(ssd
->path_mask
& mask
))
664 if (!chp_id_is_equal(&ssd
->chpid
[i
], &link
->chpid
))
666 if ((ssd
->fla_valid_mask
& mask
) &&
667 ((ssd
->fla
[i
] & link
->fla_mask
) != link
->fla
))
673 EXPORT_SYMBOL_GPL(chp_ssd_get_mask
);
675 static inline int info_bit_num(struct chp_id id
)
677 return id
.id
+ id
.cssid
* (__MAX_CHPID
+ 1);
680 /* Force chp_info refresh on next call to info_validate(). */
681 static void info_expire(void)
683 mutex_lock(&info_lock
);
684 chp_info_expires
= jiffies
- 1;
685 mutex_unlock(&info_lock
);
688 /* Ensure that chp_info is up-to-date. */
689 static int info_update(void)
693 mutex_lock(&info_lock
);
695 if (time_after(jiffies
, chp_info_expires
)) {
696 /* Data is too old, update. */
697 rc
= sclp_chp_read_info(&chp_info
);
698 chp_info_expires
= jiffies
+ CHP_INFO_UPDATE_INTERVAL
;
700 mutex_unlock(&info_lock
);
706 * chp_info_get_status - retrieve configure status of a channel-path
707 * @chpid: channel-path ID
709 * On success, return 0 for standby, 1 for configured, 2 for reserved,
710 * 3 for not recognized. Return negative error code on error.
712 int chp_info_get_status(struct chp_id chpid
)
721 bit
= info_bit_num(chpid
);
722 mutex_lock(&info_lock
);
723 if (!chp_test_bit(chp_info
.recognized
, bit
))
724 rc
= CHP_STATUS_NOT_RECOGNIZED
;
725 else if (chp_test_bit(chp_info
.configured
, bit
))
726 rc
= CHP_STATUS_CONFIGURED
;
727 else if (chp_test_bit(chp_info
.standby
, bit
))
728 rc
= CHP_STATUS_STANDBY
;
730 rc
= CHP_STATUS_RESERVED
;
731 mutex_unlock(&info_lock
);
736 /* Return configure task for chpid. */
737 static enum cfg_task_t
cfg_get_task(struct chp_id chpid
)
739 return chp_cfg_task
[chpid
.cssid
][chpid
.id
];
742 /* Set configure task for chpid. */
743 static void cfg_set_task(struct chp_id chpid
, enum cfg_task_t cfg
)
745 chp_cfg_task
[chpid
.cssid
][chpid
.id
] = cfg
;
748 /* Fetch the first configure task. Set chpid accordingly. */
749 static enum cfg_task_t
chp_cfg_fetch_task(struct chp_id
*chpid
)
751 enum cfg_task_t t
= cfg_none
;
753 chp_id_for_each(chpid
) {
754 t
= cfg_get_task(*chpid
);
762 /* Perform one configure/deconfigure request. Reschedule work function until
764 static void cfg_func(struct work_struct
*work
)
770 spin_lock(&cfg_lock
);
771 t
= chp_cfg_fetch_task(&chpid
);
772 spin_unlock(&cfg_lock
);
776 rc
= sclp_chp_configure(chpid
);
778 CIO_MSG_EVENT(2, "chp: sclp_chp_configure(%x.%02x)="
779 "%d\n", chpid
.cssid
, chpid
.id
, rc
);
782 chsc_chp_online(chpid
);
785 case cfg_deconfigure
:
786 rc
= sclp_chp_deconfigure(chpid
);
788 CIO_MSG_EVENT(2, "chp: sclp_chp_deconfigure(%x.%02x)="
789 "%d\n", chpid
.cssid
, chpid
.id
, rc
);
792 chsc_chp_offline(chpid
);
796 /* Get updated information after last change. */
798 wake_up_interruptible(&cfg_wait_queue
);
801 spin_lock(&cfg_lock
);
802 if (t
== cfg_get_task(chpid
))
803 cfg_set_task(chpid
, cfg_none
);
804 spin_unlock(&cfg_lock
);
805 schedule_work(&cfg_work
);
809 * chp_cfg_schedule - schedule chpid configuration request
810 * @chpid: channel-path ID
811 * @configure: Non-zero for configure, zero for deconfigure
813 * Schedule a channel-path configuration/deconfiguration request.
815 void chp_cfg_schedule(struct chp_id chpid
, int configure
)
817 CIO_MSG_EVENT(2, "chp_cfg_sched%x.%02x=%d\n", chpid
.cssid
, chpid
.id
,
819 spin_lock(&cfg_lock
);
820 cfg_set_task(chpid
, configure
? cfg_configure
: cfg_deconfigure
);
821 spin_unlock(&cfg_lock
);
822 schedule_work(&cfg_work
);
826 * chp_cfg_cancel_deconfigure - cancel chpid deconfiguration request
827 * @chpid: channel-path ID
829 * Cancel an active channel-path deconfiguration request if it has not yet
832 void chp_cfg_cancel_deconfigure(struct chp_id chpid
)
834 CIO_MSG_EVENT(2, "chp_cfg_cancel:%x.%02x\n", chpid
.cssid
, chpid
.id
);
835 spin_lock(&cfg_lock
);
836 if (cfg_get_task(chpid
) == cfg_deconfigure
)
837 cfg_set_task(chpid
, cfg_none
);
838 spin_unlock(&cfg_lock
);
841 static bool cfg_idle(void)
846 spin_lock(&cfg_lock
);
847 t
= chp_cfg_fetch_task(&chpid
);
848 spin_unlock(&cfg_lock
);
850 return t
== cfg_none
;
853 static int cfg_wait_idle(void)
855 if (wait_event_interruptible(cfg_wait_queue
, cfg_idle()))
860 static int __init
chp_init(void)
865 ret
= crw_register_handler(CRW_RSC_CPATH
, chp_process_crw
);
868 INIT_WORK(&cfg_work
, cfg_func
);
871 /* Register available channel-paths. */
872 chp_id_for_each(&chpid
) {
873 state
= chp_info_get_status(chpid
);
874 if (state
== CHP_STATUS_CONFIGURED
||
875 state
== CHP_STATUS_STANDBY
)
882 subsys_initcall(chp_init
);