1 // SPDX-License-Identifier: GPL-2.0+
3 * Linux on zSeries Channel Measurement Facility support
5 * Copyright IBM Corp. 2000, 2006
13 #define KMSG_COMPONENT "cio"
14 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
16 #include <linux/memblock.h>
17 #include <linux/device.h>
18 #include <linux/init.h>
19 #include <linux/list.h>
20 #include <linux/export.h>
21 #include <linux/moduleparam.h>
22 #include <linux/slab.h>
23 #include <linux/timex.h> /* get_tod_clock() */
25 #include <asm/ccwdev.h>
28 #include <asm/div64.h>
37 * parameter to enable cmf during boot, possible uses are:
38 * "s390cmf" -- enable cmf and allocate 2 MB of ram so measuring can be
39 * used on any subchannel
40 * "s390cmf=<num>" -- enable cmf and allocate enough memory to measure
41 * <num> subchannel, where <num> is an integer
42 * between 1 and 65535, default is 1024
44 #define ARGSTRING "s390cmf"
46 /* indices for READCMB */
49 /* basic and exended format: */
50 cmb_ssch_rsch_count = 0,
52 cmb_device_connect_time,
53 cmb_function_pending_time,
54 cmb_device_disconnect_time,
55 cmb_control_unit_queuing_time,
56 cmb_device_active_only_time,
57 /* extended format only: */
59 cmb_initial_command_response_time,
63 * enum cmb_format - types of supported measurement block formats
65 * @CMF_BASIC: traditional channel measurement blocks supported
66 * by all machines that we run on
67 * @CMF_EXTENDED: improved format that was introduced with the z990
69 * @CMF_AUTODETECT: default: use extended format when running on a machine
70 * supporting extended format, otherwise fall back to
80 * format - actual format for all measurement blocks
82 * The format module parameter can be set to a value of 0 (zero)
83 * or 1, indicating basic or extended format as described for
86 static int format = CMF_AUTODETECT;
87 module_param(format, bint, 0444);
90 * struct cmb_operations - functions to use depending on cmb_format
92 * Most of these functions operate on a struct ccw_device. There is only
93 * one instance of struct cmb_operations because the format of the measurement
94 * data is guaranteed to be the same for every ccw_device.
96 * @alloc: allocate memory for a channel measurement block,
97 * either with the help of a special pool or with kmalloc
98 * @free: free memory allocated with @alloc
99 * @set: enable or disable measurement
100 * @read: read a measurement entry at an index
101 * @readall: read a measurement block in a common format
102 * @reset: clear the data in the associated measurement block and
103 * reset its time stamp
105 struct cmb_operations {
106 int (*alloc) (struct ccw_device *);
107 void (*free) (struct ccw_device *);
108 int (*set) (struct ccw_device *, u32);
109 u64 (*read) (struct ccw_device *, int);
110 int (*readall)(struct ccw_device *, struct cmbdata *);
111 void (*reset) (struct ccw_device *);
113 struct attribute_group *attr_group;
115 static struct cmb_operations *cmbops;
118 void *hw_block; /* Pointer to block updated by hardware */
119 void *last_block; /* Last changed block copied from hardware block */
120 int size; /* Size of hw_block and last_block */
121 unsigned long long last_update; /* when last_block was updated */
125 * Our user interface is designed in terms of nanoseconds,
126 * while the hardware measures total times in its own
129 static inline u64 time_to_nsec(u32 value)
131 return ((u64)value) * 128000ull;
135 * Users are usually interested in average times,
136 * not accumulated time.
137 * This also helps us with atomicity problems
138 * when reading sinlge values.
140 static inline u64 time_to_avg_nsec(u32 value, u32 count)
144 /* no samples yet, avoid division by 0 */
148 /* value comes in units of 128 µsec */
149 ret = time_to_nsec(value);
159 * Activate or deactivate the channel monitor. When area is NULL,
160 * the monitor is deactivated. The channel monitor needs to
161 * be active in order to measure subchannels, which also need
164 static inline void cmf_activate(void *area, unsigned int onoff)
166 register void * __gpr2 asm("2");
167 register long __gpr1 asm("1");
171 /* activate channel measurement */
172 asm("schm" : : "d" (__gpr2), "d" (__gpr1) );
175 static int set_schib(struct ccw_device *cdev, u32 mme, int mbfc,
176 unsigned long address)
178 struct subchannel *sch = to_subchannel(cdev->dev.parent);
181 sch->config.mme = mme;
182 sch->config.mbfc = mbfc;
183 /* address can be either a block address or a block index */
185 sch->config.mba = address;
187 sch->config.mbi = address;
189 ret = cio_commit_config(sch);
190 if (!mme && ret == -ENODEV) {
192 * The task was to disable measurement block updates but
193 * the subchannel is already gone. Report success.
200 struct set_schib_struct {
203 unsigned long address;
204 wait_queue_head_t wait;
208 #define CMF_PENDING 1
209 #define SET_SCHIB_TIMEOUT (10 * HZ)
211 static int set_schib_wait(struct ccw_device *cdev, u32 mme,
212 int mbfc, unsigned long address)
214 struct set_schib_struct set_data;
217 spin_lock_irq(cdev->ccwlock);
218 if (!cdev->private->cmb)
221 ret = set_schib(cdev, mme, mbfc, address);
225 /* if the device is not online, don't even try again */
226 if (cdev->private->state != DEV_STATE_ONLINE)
229 init_waitqueue_head(&set_data.wait);
231 set_data.mbfc = mbfc;
232 set_data.address = address;
233 set_data.ret = CMF_PENDING;
235 cdev->private->state = DEV_STATE_CMFCHANGE;
236 cdev->private->cmb_wait = &set_data;
237 spin_unlock_irq(cdev->ccwlock);
239 ret = wait_event_interruptible_timeout(set_data.wait,
240 set_data.ret != CMF_PENDING,
242 spin_lock_irq(cdev->ccwlock);
244 if (set_data.ret == CMF_PENDING) {
245 set_data.ret = (ret == 0) ? -ETIME : ret;
246 if (cdev->private->state == DEV_STATE_CMFCHANGE)
247 cdev->private->state = DEV_STATE_ONLINE;
250 cdev->private->cmb_wait = NULL;
253 spin_unlock_irq(cdev->ccwlock);
257 void retry_set_schib(struct ccw_device *cdev)
259 struct set_schib_struct *set_data = cdev->private->cmb_wait;
264 set_data->ret = set_schib(cdev, set_data->mme, set_data->mbfc,
266 wake_up(&set_data->wait);
269 static int cmf_copy_block(struct ccw_device *cdev)
271 struct subchannel *sch = to_subchannel(cdev->dev.parent);
272 struct cmb_data *cmb_data;
275 if (cio_update_schib(sch))
278 if (scsw_fctl(&sch->schib.scsw) & SCSW_FCTL_START_FUNC) {
279 /* Don't copy if a start function is in progress. */
280 if ((!(scsw_actl(&sch->schib.scsw) & SCSW_ACTL_SUSPENDED)) &&
281 (scsw_actl(&sch->schib.scsw) &
282 (SCSW_ACTL_DEVACT | SCSW_ACTL_SCHACT)) &&
283 (!(scsw_stctl(&sch->schib.scsw) & SCSW_STCTL_SEC_STATUS)))
286 cmb_data = cdev->private->cmb;
287 hw_block = cmb_data->hw_block;
288 memcpy(cmb_data->last_block, hw_block, cmb_data->size);
289 cmb_data->last_update = get_tod_clock();
293 struct copy_block_struct {
294 wait_queue_head_t wait;
298 static int cmf_cmb_copy_wait(struct ccw_device *cdev)
300 struct copy_block_struct copy_block;
303 spin_lock_irq(cdev->ccwlock);
304 if (!cdev->private->cmb)
307 ret = cmf_copy_block(cdev);
311 if (cdev->private->state != DEV_STATE_ONLINE)
314 init_waitqueue_head(©_block.wait);
315 copy_block.ret = CMF_PENDING;
317 cdev->private->state = DEV_STATE_CMFUPDATE;
318 cdev->private->cmb_wait = ©_block;
319 spin_unlock_irq(cdev->ccwlock);
321 ret = wait_event_interruptible(copy_block.wait,
322 copy_block.ret != CMF_PENDING);
323 spin_lock_irq(cdev->ccwlock);
325 if (copy_block.ret == CMF_PENDING) {
326 copy_block.ret = -ERESTARTSYS;
327 if (cdev->private->state == DEV_STATE_CMFUPDATE)
328 cdev->private->state = DEV_STATE_ONLINE;
331 cdev->private->cmb_wait = NULL;
332 ret = copy_block.ret;
334 spin_unlock_irq(cdev->ccwlock);
338 void cmf_retry_copy_block(struct ccw_device *cdev)
340 struct copy_block_struct *copy_block = cdev->private->cmb_wait;
345 copy_block->ret = cmf_copy_block(cdev);
346 wake_up(©_block->wait);
349 static void cmf_generic_reset(struct ccw_device *cdev)
351 struct cmb_data *cmb_data;
353 spin_lock_irq(cdev->ccwlock);
354 cmb_data = cdev->private->cmb;
356 memset(cmb_data->last_block, 0, cmb_data->size);
358 * Need to reset hw block as well to make the hardware start
361 memset(cmb_data->hw_block, 0, cmb_data->size);
362 cmb_data->last_update = 0;
364 cdev->private->cmb_start_time = get_tod_clock();
365 spin_unlock_irq(cdev->ccwlock);
369 * struct cmb_area - container for global cmb data
371 * @mem: pointer to CMBs (only in basic measurement mode)
372 * @list: contains a linked list of all subchannels
373 * @num_channels: number of channels to be measured
374 * @lock: protect concurrent access to @mem and @list
378 struct list_head list;
383 static struct cmb_area cmb_area = {
384 .lock = __SPIN_LOCK_UNLOCKED(cmb_area.lock),
385 .list = LIST_HEAD_INIT(cmb_area.list),
386 .num_channels = 1024,
389 /* ****** old style CMB handling ********/
392 * Basic channel measurement blocks are allocated in one contiguous
393 * block of memory, which can not be moved as long as any channel
394 * is active. Therefore, a maximum number of subchannels needs to
395 * be defined somewhere. This is a module parameter, defaulting to
396 * a reasonable value of 1024, or 32 kb of memory.
397 * Current kernels don't allow kmalloc with more than 128kb, so the
401 module_param_named(maxchannels, cmb_area.num_channels, uint, 0444);
404 * struct cmb - basic channel measurement block
405 * @ssch_rsch_count: number of ssch and rsch
406 * @sample_count: number of samples
407 * @device_connect_time: time of device connect
408 * @function_pending_time: time of function pending
409 * @device_disconnect_time: time of device disconnect
410 * @control_unit_queuing_time: time of control unit queuing
411 * @device_active_only_time: time of device active only
412 * @reserved: unused in basic measurement mode
414 * The measurement block as used by the hardware. The fields are described
415 * further in z/Architecture Principles of Operation, chapter 17.
417 * The cmb area made up from these blocks must be a contiguous array and may
418 * not be reallocated or freed.
419 * Only one cmb area can be present in the system.
424 u32 device_connect_time;
425 u32 function_pending_time;
426 u32 device_disconnect_time;
427 u32 control_unit_queuing_time;
428 u32 device_active_only_time;
433 * Insert a single device into the cmb_area list.
434 * Called with cmb_area.lock held from alloc_cmb.
436 static int alloc_cmb_single(struct ccw_device *cdev,
437 struct cmb_data *cmb_data)
440 struct ccw_device_private *node;
443 spin_lock_irq(cdev->ccwlock);
444 if (!list_empty(&cdev->private->cmb_list)) {
450 * Find first unused cmb in cmb_area.mem.
451 * This is a little tricky: cmb_area.list
452 * remains sorted by ->cmb->hw_data pointers.
455 list_for_each_entry(node, &cmb_area.list, cmb_list) {
456 struct cmb_data *data;
458 if ((struct cmb*)data->hw_block > cmb)
462 if (cmb - cmb_area.mem >= cmb_area.num_channels) {
468 list_add_tail(&cdev->private->cmb_list, &node->cmb_list);
469 cmb_data->hw_block = cmb;
470 cdev->private->cmb = cmb_data;
473 spin_unlock_irq(cdev->ccwlock);
477 static int alloc_cmb(struct ccw_device *cdev)
482 struct cmb_data *cmb_data;
484 /* Allocate private cmb_data. */
485 cmb_data = kzalloc(sizeof(struct cmb_data), GFP_KERNEL);
489 cmb_data->last_block = kzalloc(sizeof(struct cmb), GFP_KERNEL);
490 if (!cmb_data->last_block) {
494 cmb_data->size = sizeof(struct cmb);
495 spin_lock(&cmb_area.lock);
498 /* there is no user yet, so we need a new area */
499 size = sizeof(struct cmb) * cmb_area.num_channels;
500 WARN_ON(!list_empty(&cmb_area.list));
502 spin_unlock(&cmb_area.lock);
503 mem = (void*)__get_free_pages(GFP_KERNEL | GFP_DMA,
505 spin_lock(&cmb_area.lock);
508 /* ok, another thread was faster */
509 free_pages((unsigned long)mem, get_order(size));
516 memset(mem, 0, size);
518 cmf_activate(cmb_area.mem, CMF_ON);
522 /* do the actual allocation */
523 ret = alloc_cmb_single(cdev, cmb_data);
525 spin_unlock(&cmb_area.lock);
527 kfree(cmb_data->last_block);
533 static void free_cmb(struct ccw_device *cdev)
535 struct ccw_device_private *priv;
536 struct cmb_data *cmb_data;
538 spin_lock(&cmb_area.lock);
539 spin_lock_irq(cdev->ccwlock);
541 priv = cdev->private;
542 cmb_data = priv->cmb;
545 kfree(cmb_data->last_block);
547 list_del_init(&priv->cmb_list);
549 if (list_empty(&cmb_area.list)) {
551 size = sizeof(struct cmb) * cmb_area.num_channels;
552 cmf_activate(NULL, CMF_OFF);
553 free_pages((unsigned long)cmb_area.mem, get_order(size));
556 spin_unlock_irq(cdev->ccwlock);
557 spin_unlock(&cmb_area.lock);
560 static int set_cmb(struct ccw_device *cdev, u32 mme)
563 struct cmb_data *cmb_data;
566 spin_lock_irqsave(cdev->ccwlock, flags);
567 if (!cdev->private->cmb) {
568 spin_unlock_irqrestore(cdev->ccwlock, flags);
571 cmb_data = cdev->private->cmb;
572 offset = mme ? (struct cmb *)cmb_data->hw_block - cmb_area.mem : 0;
573 spin_unlock_irqrestore(cdev->ccwlock, flags);
575 return set_schib_wait(cdev, mme, 0, offset);
578 /* calculate utilization in 0.1 percent units */
579 static u64 __cmb_utilization(u64 device_connect_time, u64 function_pending_time,
580 u64 device_disconnect_time, u64 start_time)
582 u64 utilization, elapsed_time;
584 utilization = time_to_nsec(device_connect_time +
585 function_pending_time +
586 device_disconnect_time);
588 elapsed_time = get_tod_clock() - start_time;
589 elapsed_time = tod_to_ns(elapsed_time);
590 elapsed_time /= 1000;
592 return elapsed_time ? (utilization / elapsed_time) : 0;
595 static u64 read_cmb(struct ccw_device *cdev, int index)
597 struct cmb_data *cmb_data;
603 spin_lock_irqsave(cdev->ccwlock, flags);
604 cmb_data = cdev->private->cmb;
608 cmb = cmb_data->hw_block;
610 case avg_utilization:
611 ret = __cmb_utilization(cmb->device_connect_time,
612 cmb->function_pending_time,
613 cmb->device_disconnect_time,
614 cdev->private->cmb_start_time);
616 case cmb_ssch_rsch_count:
617 ret = cmb->ssch_rsch_count;
619 case cmb_sample_count:
620 ret = cmb->sample_count;
622 case cmb_device_connect_time:
623 val = cmb->device_connect_time;
625 case cmb_function_pending_time:
626 val = cmb->function_pending_time;
628 case cmb_device_disconnect_time:
629 val = cmb->device_disconnect_time;
631 case cmb_control_unit_queuing_time:
632 val = cmb->control_unit_queuing_time;
634 case cmb_device_active_only_time:
635 val = cmb->device_active_only_time;
640 ret = time_to_avg_nsec(val, cmb->sample_count);
642 spin_unlock_irqrestore(cdev->ccwlock, flags);
646 static int readall_cmb(struct ccw_device *cdev, struct cmbdata *data)
649 struct cmb_data *cmb_data;
654 ret = cmf_cmb_copy_wait(cdev);
657 spin_lock_irqsave(cdev->ccwlock, flags);
658 cmb_data = cdev->private->cmb;
663 if (cmb_data->last_update == 0) {
667 cmb = cmb_data->last_block;
668 time = cmb_data->last_update - cdev->private->cmb_start_time;
670 memset(data, 0, sizeof(struct cmbdata));
672 /* we only know values before device_busy_time */
673 data->size = offsetof(struct cmbdata, device_busy_time);
675 data->elapsed_time = tod_to_ns(time);
677 /* copy data to new structure */
678 data->ssch_rsch_count = cmb->ssch_rsch_count;
679 data->sample_count = cmb->sample_count;
681 /* time fields are converted to nanoseconds while copying */
682 data->device_connect_time = time_to_nsec(cmb->device_connect_time);
683 data->function_pending_time = time_to_nsec(cmb->function_pending_time);
684 data->device_disconnect_time =
685 time_to_nsec(cmb->device_disconnect_time);
686 data->control_unit_queuing_time
687 = time_to_nsec(cmb->control_unit_queuing_time);
688 data->device_active_only_time
689 = time_to_nsec(cmb->device_active_only_time);
692 spin_unlock_irqrestore(cdev->ccwlock, flags);
696 static void reset_cmb(struct ccw_device *cdev)
698 cmf_generic_reset(cdev);
701 static int cmf_enabled(struct ccw_device *cdev)
705 spin_lock_irq(cdev->ccwlock);
706 enabled = !!cdev->private->cmb;
707 spin_unlock_irq(cdev->ccwlock);
712 static struct attribute_group cmf_attr_group;
714 static struct cmb_operations cmbops_basic = {
719 .readall = readall_cmb,
721 .attr_group = &cmf_attr_group,
724 /* ******** extended cmb handling ********/
727 * struct cmbe - extended channel measurement block
728 * @ssch_rsch_count: number of ssch and rsch
729 * @sample_count: number of samples
730 * @device_connect_time: time of device connect
731 * @function_pending_time: time of function pending
732 * @device_disconnect_time: time of device disconnect
733 * @control_unit_queuing_time: time of control unit queuing
734 * @device_active_only_time: time of device active only
735 * @device_busy_time: time of device busy
736 * @initial_command_response_time: initial command response time
739 * The measurement block as used by the hardware. May be in any 64 bit physical
741 * The fields are described further in z/Architecture Principles of Operation,
742 * third edition, chapter 17.
747 u32 device_connect_time;
748 u32 function_pending_time;
749 u32 device_disconnect_time;
750 u32 control_unit_queuing_time;
751 u32 device_active_only_time;
752 u32 device_busy_time;
753 u32 initial_command_response_time;
755 } __packed __aligned(64);
757 static struct kmem_cache *cmbe_cache;
759 static int alloc_cmbe(struct ccw_device *cdev)
761 struct cmb_data *cmb_data;
765 cmbe = kmem_cache_zalloc(cmbe_cache, GFP_KERNEL);
769 cmb_data = kzalloc(sizeof(*cmb_data), GFP_KERNEL);
773 cmb_data->last_block = kzalloc(sizeof(struct cmbe), GFP_KERNEL);
774 if (!cmb_data->last_block)
777 cmb_data->size = sizeof(*cmbe);
778 cmb_data->hw_block = cmbe;
780 spin_lock(&cmb_area.lock);
781 spin_lock_irq(cdev->ccwlock);
782 if (cdev->private->cmb)
785 cdev->private->cmb = cmb_data;
787 /* activate global measurement if this is the first channel */
788 if (list_empty(&cmb_area.list))
789 cmf_activate(NULL, CMF_ON);
790 list_add_tail(&cdev->private->cmb_list, &cmb_area.list);
792 spin_unlock_irq(cdev->ccwlock);
793 spin_unlock(&cmb_area.lock);
797 spin_unlock_irq(cdev->ccwlock);
798 spin_unlock(&cmb_area.lock);
802 kfree(cmb_data->last_block);
804 kmem_cache_free(cmbe_cache, cmbe);
809 static void free_cmbe(struct ccw_device *cdev)
811 struct cmb_data *cmb_data;
813 spin_lock(&cmb_area.lock);
814 spin_lock_irq(cdev->ccwlock);
815 cmb_data = cdev->private->cmb;
816 cdev->private->cmb = NULL;
818 kfree(cmb_data->last_block);
819 kmem_cache_free(cmbe_cache, cmb_data->hw_block);
823 /* deactivate global measurement if this is the last channel */
824 list_del_init(&cdev->private->cmb_list);
825 if (list_empty(&cmb_area.list))
826 cmf_activate(NULL, CMF_OFF);
827 spin_unlock_irq(cdev->ccwlock);
828 spin_unlock(&cmb_area.lock);
831 static int set_cmbe(struct ccw_device *cdev, u32 mme)
834 struct cmb_data *cmb_data;
837 spin_lock_irqsave(cdev->ccwlock, flags);
838 if (!cdev->private->cmb) {
839 spin_unlock_irqrestore(cdev->ccwlock, flags);
842 cmb_data = cdev->private->cmb;
843 mba = mme ? (unsigned long) cmb_data->hw_block : 0;
844 spin_unlock_irqrestore(cdev->ccwlock, flags);
846 return set_schib_wait(cdev, mme, 1, mba);
849 static u64 read_cmbe(struct ccw_device *cdev, int index)
851 struct cmb_data *cmb_data;
857 spin_lock_irqsave(cdev->ccwlock, flags);
858 cmb_data = cdev->private->cmb;
862 cmb = cmb_data->hw_block;
864 case avg_utilization:
865 ret = __cmb_utilization(cmb->device_connect_time,
866 cmb->function_pending_time,
867 cmb->device_disconnect_time,
868 cdev->private->cmb_start_time);
870 case cmb_ssch_rsch_count:
871 ret = cmb->ssch_rsch_count;
873 case cmb_sample_count:
874 ret = cmb->sample_count;
876 case cmb_device_connect_time:
877 val = cmb->device_connect_time;
879 case cmb_function_pending_time:
880 val = cmb->function_pending_time;
882 case cmb_device_disconnect_time:
883 val = cmb->device_disconnect_time;
885 case cmb_control_unit_queuing_time:
886 val = cmb->control_unit_queuing_time;
888 case cmb_device_active_only_time:
889 val = cmb->device_active_only_time;
891 case cmb_device_busy_time:
892 val = cmb->device_busy_time;
894 case cmb_initial_command_response_time:
895 val = cmb->initial_command_response_time;
900 ret = time_to_avg_nsec(val, cmb->sample_count);
902 spin_unlock_irqrestore(cdev->ccwlock, flags);
906 static int readall_cmbe(struct ccw_device *cdev, struct cmbdata *data)
909 struct cmb_data *cmb_data;
914 ret = cmf_cmb_copy_wait(cdev);
917 spin_lock_irqsave(cdev->ccwlock, flags);
918 cmb_data = cdev->private->cmb;
923 if (cmb_data->last_update == 0) {
927 time = cmb_data->last_update - cdev->private->cmb_start_time;
929 memset (data, 0, sizeof(struct cmbdata));
931 /* we only know values before device_busy_time */
932 data->size = offsetof(struct cmbdata, device_busy_time);
934 data->elapsed_time = tod_to_ns(time);
936 cmb = cmb_data->last_block;
937 /* copy data to new structure */
938 data->ssch_rsch_count = cmb->ssch_rsch_count;
939 data->sample_count = cmb->sample_count;
941 /* time fields are converted to nanoseconds while copying */
942 data->device_connect_time = time_to_nsec(cmb->device_connect_time);
943 data->function_pending_time = time_to_nsec(cmb->function_pending_time);
944 data->device_disconnect_time =
945 time_to_nsec(cmb->device_disconnect_time);
946 data->control_unit_queuing_time
947 = time_to_nsec(cmb->control_unit_queuing_time);
948 data->device_active_only_time
949 = time_to_nsec(cmb->device_active_only_time);
950 data->device_busy_time = time_to_nsec(cmb->device_busy_time);
951 data->initial_command_response_time
952 = time_to_nsec(cmb->initial_command_response_time);
956 spin_unlock_irqrestore(cdev->ccwlock, flags);
960 static void reset_cmbe(struct ccw_device *cdev)
962 cmf_generic_reset(cdev);
965 static struct attribute_group cmf_attr_group_ext;
967 static struct cmb_operations cmbops_extended = {
972 .readall = readall_cmbe,
974 .attr_group = &cmf_attr_group_ext,
977 static ssize_t cmb_show_attr(struct device *dev, char *buf, enum cmb_index idx)
979 return sprintf(buf, "%lld\n",
980 (unsigned long long) cmf_read(to_ccwdev(dev), idx));
983 static ssize_t cmb_show_avg_sample_interval(struct device *dev,
984 struct device_attribute *attr,
987 struct ccw_device *cdev = to_ccwdev(dev);
991 count = cmf_read(cdev, cmb_sample_count);
992 spin_lock_irq(cdev->ccwlock);
994 interval = get_tod_clock() - cdev->private->cmb_start_time;
995 interval = tod_to_ns(interval);
999 spin_unlock_irq(cdev->ccwlock);
1000 return sprintf(buf, "%ld\n", interval);
1003 static ssize_t cmb_show_avg_utilization(struct device *dev,
1004 struct device_attribute *attr,
1007 unsigned long u = cmf_read(to_ccwdev(dev), avg_utilization);
1009 return sprintf(buf, "%02lu.%01lu%%\n", u / 10, u % 10);
1012 #define cmf_attr(name) \
1013 static ssize_t show_##name(struct device *dev, \
1014 struct device_attribute *attr, char *buf) \
1015 { return cmb_show_attr((dev), buf, cmb_##name); } \
1016 static DEVICE_ATTR(name, 0444, show_##name, NULL);
1018 #define cmf_attr_avg(name) \
1019 static ssize_t show_avg_##name(struct device *dev, \
1020 struct device_attribute *attr, char *buf) \
1021 { return cmb_show_attr((dev), buf, cmb_##name); } \
1022 static DEVICE_ATTR(avg_##name, 0444, show_avg_##name, NULL);
1024 cmf_attr(ssch_rsch_count);
1025 cmf_attr(sample_count);
1026 cmf_attr_avg(device_connect_time);
1027 cmf_attr_avg(function_pending_time);
1028 cmf_attr_avg(device_disconnect_time);
1029 cmf_attr_avg(control_unit_queuing_time);
1030 cmf_attr_avg(device_active_only_time);
1031 cmf_attr_avg(device_busy_time);
1032 cmf_attr_avg(initial_command_response_time);
1034 static DEVICE_ATTR(avg_sample_interval, 0444, cmb_show_avg_sample_interval,
1036 static DEVICE_ATTR(avg_utilization, 0444, cmb_show_avg_utilization, NULL);
1038 static struct attribute *cmf_attributes[] = {
1039 &dev_attr_avg_sample_interval.attr,
1040 &dev_attr_avg_utilization.attr,
1041 &dev_attr_ssch_rsch_count.attr,
1042 &dev_attr_sample_count.attr,
1043 &dev_attr_avg_device_connect_time.attr,
1044 &dev_attr_avg_function_pending_time.attr,
1045 &dev_attr_avg_device_disconnect_time.attr,
1046 &dev_attr_avg_control_unit_queuing_time.attr,
1047 &dev_attr_avg_device_active_only_time.attr,
1051 static struct attribute_group cmf_attr_group = {
1053 .attrs = cmf_attributes,
1056 static struct attribute *cmf_attributes_ext[] = {
1057 &dev_attr_avg_sample_interval.attr,
1058 &dev_attr_avg_utilization.attr,
1059 &dev_attr_ssch_rsch_count.attr,
1060 &dev_attr_sample_count.attr,
1061 &dev_attr_avg_device_connect_time.attr,
1062 &dev_attr_avg_function_pending_time.attr,
1063 &dev_attr_avg_device_disconnect_time.attr,
1064 &dev_attr_avg_control_unit_queuing_time.attr,
1065 &dev_attr_avg_device_active_only_time.attr,
1066 &dev_attr_avg_device_busy_time.attr,
1067 &dev_attr_avg_initial_command_response_time.attr,
1071 static struct attribute_group cmf_attr_group_ext = {
1073 .attrs = cmf_attributes_ext,
1076 static ssize_t cmb_enable_show(struct device *dev,
1077 struct device_attribute *attr,
1080 struct ccw_device *cdev = to_ccwdev(dev);
1082 return sprintf(buf, "%d\n", cmf_enabled(cdev));
1085 static ssize_t cmb_enable_store(struct device *dev,
1086 struct device_attribute *attr, const char *buf,
1089 struct ccw_device *cdev = to_ccwdev(dev);
1093 ret = kstrtoul(buf, 16, &val);
1099 ret = disable_cmf(cdev);
1102 ret = enable_cmf(cdev);
1108 return ret ? ret : c;
1110 DEVICE_ATTR_RW(cmb_enable);
1112 int ccw_set_cmf(struct ccw_device *cdev, int enable)
1114 return cmbops->set(cdev, enable ? 2 : 0);
1118 * enable_cmf() - switch on the channel measurement for a specific device
1119 * @cdev: The ccw device to be enabled
1121 * Enable channel measurements for @cdev. If this is called on a device
1122 * for which channel measurement is already enabled a reset of the
1123 * measurement data is triggered.
1124 * Returns: %0 for success or a negative error value.
1128 int enable_cmf(struct ccw_device *cdev)
1132 device_lock(&cdev->dev);
1133 if (cmf_enabled(cdev)) {
1134 cmbops->reset(cdev);
1137 get_device(&cdev->dev);
1138 ret = cmbops->alloc(cdev);
1141 cmbops->reset(cdev);
1142 ret = sysfs_create_group(&cdev->dev.kobj, cmbops->attr_group);
1147 ret = cmbops->set(cdev, 2);
1149 sysfs_remove_group(&cdev->dev.kobj, cmbops->attr_group);
1154 put_device(&cdev->dev);
1156 device_unlock(&cdev->dev);
1161 * __disable_cmf() - switch off the channel measurement for a specific device
1162 * @cdev: The ccw device to be disabled
1164 * Returns: %0 for success or a negative error value.
1167 * non-atomic, device_lock() held.
1169 int __disable_cmf(struct ccw_device *cdev)
1173 ret = cmbops->set(cdev, 0);
1177 sysfs_remove_group(&cdev->dev.kobj, cmbops->attr_group);
1179 put_device(&cdev->dev);
1185 * disable_cmf() - switch off the channel measurement for a specific device
1186 * @cdev: The ccw device to be disabled
1188 * Returns: %0 for success or a negative error value.
1193 int disable_cmf(struct ccw_device *cdev)
1197 device_lock(&cdev->dev);
1198 ret = __disable_cmf(cdev);
1199 device_unlock(&cdev->dev);
1205 * cmf_read() - read one value from the current channel measurement block
1206 * @cdev: the channel to be read
1207 * @index: the index of the value to be read
1209 * Returns: The value read or %0 if the value cannot be read.
1214 u64 cmf_read(struct ccw_device *cdev, int index)
1216 return cmbops->read(cdev, index);
1220 * cmf_readall() - read the current channel measurement block
1221 * @cdev: the channel to be read
1222 * @data: a pointer to a data block that will be filled
1224 * Returns: %0 on success, a negative error value otherwise.
1229 int cmf_readall(struct ccw_device *cdev, struct cmbdata *data)
1231 return cmbops->readall(cdev, data);
1234 /* Reenable cmf when a disconnected device becomes available again. */
1235 int cmf_reenable(struct ccw_device *cdev)
1237 cmbops->reset(cdev);
1238 return cmbops->set(cdev, 2);
1242 * cmf_reactivate() - reactivate measurement block updates
1244 * Use this during resume from hibernate.
1246 void cmf_reactivate(void)
1248 spin_lock(&cmb_area.lock);
1249 if (!list_empty(&cmb_area.list))
1250 cmf_activate(cmb_area.mem, CMF_ON);
1251 spin_unlock(&cmb_area.lock);
1254 static int __init init_cmbe(void)
1256 cmbe_cache = kmem_cache_create("cmbe_cache", sizeof(struct cmbe),
1257 __alignof__(struct cmbe), 0, NULL);
1259 return cmbe_cache ? 0 : -ENOMEM;
1262 static int __init init_cmf(void)
1264 char *format_string;
1265 char *detect_string;
1269 * If the user did not give a parameter, see if we are running on a
1270 * machine supporting extended measurement blocks, otherwise fall back
1273 if (format == CMF_AUTODETECT) {
1274 if (!css_general_characteristics.ext_mb) {
1277 format = CMF_EXTENDED;
1279 detect_string = "autodetected";
1281 detect_string = "parameter";
1286 format_string = "basic";
1287 cmbops = &cmbops_basic;
1290 format_string = "extended";
1291 cmbops = &cmbops_extended;
1300 pr_info("Channel measurement facility initialized using format "
1301 "%s (mode %s)\n", format_string, detect_string);
1304 device_initcall(init_cmf);
1306 EXPORT_SYMBOL_GPL(enable_cmf);
1307 EXPORT_SYMBOL_GPL(disable_cmf);
1308 EXPORT_SYMBOL_GPL(cmf_read);
1309 EXPORT_SYMBOL_GPL(cmf_readall);