2 * Linux on zSeries Channel Measurement Facility support
4 * Copyright IBM Corp. 2000, 2006
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2, or (at your option)
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 #define KMSG_COMPONENT "cio"
27 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
29 #include <linux/bootmem.h>
30 #include <linux/device.h>
31 #include <linux/init.h>
32 #include <linux/list.h>
33 #include <linux/export.h>
34 #include <linux/moduleparam.h>
35 #include <linux/slab.h>
36 #include <linux/timex.h> /* get_tod_clock() */
38 #include <asm/ccwdev.h>
41 #include <asm/div64.h>
50 * parameter to enable cmf during boot, possible uses are:
51 * "s390cmf" -- enable cmf and allocate 2 MB of ram so measuring can be
52 * used on any subchannel
53 * "s390cmf=<num>" -- enable cmf and allocate enough memory to measure
54 * <num> subchannel, where <num> is an integer
55 * between 1 and 65535, default is 1024
57 #define ARGSTRING "s390cmf"
59 /* indices for READCMB */
61 /* basic and exended format: */
64 cmb_device_connect_time,
65 cmb_function_pending_time,
66 cmb_device_disconnect_time,
67 cmb_control_unit_queuing_time,
68 cmb_device_active_only_time,
69 /* extended format only: */
71 cmb_initial_command_response_time,
75 * enum cmb_format - types of supported measurement block formats
77 * @CMF_BASIC: traditional channel measurement blocks supported
78 * by all machines that we run on
79 * @CMF_EXTENDED: improved format that was introduced with the z990
81 * @CMF_AUTODETECT: default: use extended format when running on a machine
82 * supporting extended format, otherwise fall back to
92 * format - actual format for all measurement blocks
94 * The format module parameter can be set to a value of 0 (zero)
95 * or 1, indicating basic or extended format as described for
98 static int format = CMF_AUTODETECT;
99 module_param(format, bint, 0444);
102 * struct cmb_operations - functions to use depending on cmb_format
104 * Most of these functions operate on a struct ccw_device. There is only
105 * one instance of struct cmb_operations because the format of the measurement
106 * data is guaranteed to be the same for every ccw_device.
108 * @alloc: allocate memory for a channel measurement block,
109 * either with the help of a special pool or with kmalloc
110 * @free: free memory allocated with @alloc
111 * @set: enable or disable measurement
112 * @read: read a measurement entry at an index
113 * @readall: read a measurement block in a common format
114 * @reset: clear the data in the associated measurement block and
115 * reset its time stamp
117 struct cmb_operations {
118 int (*alloc) (struct ccw_device *);
119 void (*free) (struct ccw_device *);
120 int (*set) (struct ccw_device *, u32);
121 u64 (*read) (struct ccw_device *, int);
122 int (*readall)(struct ccw_device *, struct cmbdata *);
123 void (*reset) (struct ccw_device *);
125 struct attribute_group *attr_group;
127 static struct cmb_operations *cmbops;
130 void *hw_block; /* Pointer to block updated by hardware */
131 void *last_block; /* Last changed block copied from hardware block */
132 int size; /* Size of hw_block and last_block */
133 unsigned long long last_update; /* when last_block was updated */
137 * Our user interface is designed in terms of nanoseconds,
138 * while the hardware measures total times in its own
141 static inline u64 time_to_nsec(u32 value)
143 return ((u64)value) * 128000ull;
147 * Users are usually interested in average times,
148 * not accumulated time.
149 * This also helps us with atomicity problems
150 * when reading sinlge values.
152 static inline u64 time_to_avg_nsec(u32 value, u32 count)
156 /* no samples yet, avoid division by 0 */
160 /* value comes in units of 128 µsec */
161 ret = time_to_nsec(value);
171 * Activate or deactivate the channel monitor. When area is NULL,
172 * the monitor is deactivated. The channel monitor needs to
173 * be active in order to measure subchannels, which also need
176 static inline void cmf_activate(void *area, unsigned int onoff)
178 register void * __gpr2 asm("2");
179 register long __gpr1 asm("1");
183 /* activate channel measurement */
184 asm("schm" : : "d" (__gpr2), "d" (__gpr1) );
187 static int set_schib(struct ccw_device *cdev, u32 mme, int mbfc,
188 unsigned long address)
190 struct subchannel *sch = to_subchannel(cdev->dev.parent);
193 sch->config.mme = mme;
194 sch->config.mbfc = mbfc;
195 /* address can be either a block address or a block index */
197 sch->config.mba = address;
199 sch->config.mbi = address;
201 ret = cio_commit_config(sch);
202 if (!mme && ret == -ENODEV) {
204 * The task was to disable measurement block updates but
205 * the subchannel is already gone. Report success.
212 struct set_schib_struct {
215 unsigned long address;
216 wait_queue_head_t wait;
221 static void cmf_set_schib_release(struct kref *kref)
223 struct set_schib_struct *set_data;
225 set_data = container_of(kref, struct set_schib_struct, kref);
229 #define CMF_PENDING 1
231 static int set_schib_wait(struct ccw_device *cdev, u32 mme,
232 int mbfc, unsigned long address)
234 struct set_schib_struct *set_data;
237 spin_lock_irq(cdev->ccwlock);
238 if (!cdev->private->cmb) {
242 set_data = kzalloc(sizeof(struct set_schib_struct), GFP_ATOMIC);
247 init_waitqueue_head(&set_data->wait);
248 kref_init(&set_data->kref);
250 set_data->mbfc = mbfc;
251 set_data->address = address;
253 ret = set_schib(cdev, mme, mbfc, address);
257 if (cdev->private->state != DEV_STATE_ONLINE) {
258 /* if the device is not online, don't even try again */
263 cdev->private->state = DEV_STATE_CMFCHANGE;
264 set_data->ret = CMF_PENDING;
265 cdev->private->cmb_wait = set_data;
267 spin_unlock_irq(cdev->ccwlock);
268 if (wait_event_interruptible(set_data->wait,
269 set_data->ret != CMF_PENDING)) {
270 spin_lock_irq(cdev->ccwlock);
271 if (set_data->ret == CMF_PENDING) {
272 set_data->ret = -ERESTARTSYS;
273 if (cdev->private->state == DEV_STATE_CMFCHANGE)
274 cdev->private->state = DEV_STATE_ONLINE;
276 spin_unlock_irq(cdev->ccwlock);
278 spin_lock_irq(cdev->ccwlock);
279 cdev->private->cmb_wait = NULL;
282 kref_put(&set_data->kref, cmf_set_schib_release);
284 spin_unlock_irq(cdev->ccwlock);
288 void retry_set_schib(struct ccw_device *cdev)
290 struct set_schib_struct *set_data;
292 set_data = cdev->private->cmb_wait;
297 kref_get(&set_data->kref);
298 set_data->ret = set_schib(cdev, set_data->mme, set_data->mbfc,
300 wake_up(&set_data->wait);
301 kref_put(&set_data->kref, cmf_set_schib_release);
304 static int cmf_copy_block(struct ccw_device *cdev)
306 struct subchannel *sch;
309 struct cmb_data *cmb_data;
311 sch = to_subchannel(cdev->dev.parent);
313 if (cio_update_schib(sch))
316 if (scsw_fctl(&sch->schib.scsw) & SCSW_FCTL_START_FUNC) {
317 /* Don't copy if a start function is in progress. */
318 if ((!(scsw_actl(&sch->schib.scsw) & SCSW_ACTL_SUSPENDED)) &&
319 (scsw_actl(&sch->schib.scsw) &
320 (SCSW_ACTL_DEVACT | SCSW_ACTL_SCHACT)) &&
321 (!(scsw_stctl(&sch->schib.scsw) & SCSW_STCTL_SEC_STATUS)))
324 cmb_data = cdev->private->cmb;
325 hw_block = cmb_data->hw_block;
326 if (!memcmp(cmb_data->last_block, hw_block, cmb_data->size))
327 /* No need to copy. */
329 reference_buf = kzalloc(cmb_data->size, GFP_ATOMIC);
332 /* Ensure consistency of block copied from hardware. */
334 memcpy(cmb_data->last_block, hw_block, cmb_data->size);
335 memcpy(reference_buf, hw_block, cmb_data->size);
336 } while (memcmp(cmb_data->last_block, reference_buf, cmb_data->size));
337 cmb_data->last_update = get_tod_clock();
338 kfree(reference_buf);
342 struct copy_block_struct {
343 wait_queue_head_t wait;
348 static void cmf_copy_block_release(struct kref *kref)
350 struct copy_block_struct *copy_block;
352 copy_block = container_of(kref, struct copy_block_struct, kref);
356 static int cmf_cmb_copy_wait(struct ccw_device *cdev)
358 struct copy_block_struct *copy_block;
362 spin_lock_irqsave(cdev->ccwlock, flags);
363 if (!cdev->private->cmb) {
367 copy_block = kzalloc(sizeof(struct copy_block_struct), GFP_ATOMIC);
372 init_waitqueue_head(©_block->wait);
373 kref_init(©_block->kref);
375 ret = cmf_copy_block(cdev);
379 if (cdev->private->state != DEV_STATE_ONLINE) {
384 cdev->private->state = DEV_STATE_CMFUPDATE;
385 copy_block->ret = CMF_PENDING;
386 cdev->private->cmb_wait = copy_block;
388 spin_unlock_irqrestore(cdev->ccwlock, flags);
389 if (wait_event_interruptible(copy_block->wait,
390 copy_block->ret != CMF_PENDING)) {
391 spin_lock_irqsave(cdev->ccwlock, flags);
392 if (copy_block->ret == CMF_PENDING) {
393 copy_block->ret = -ERESTARTSYS;
394 if (cdev->private->state == DEV_STATE_CMFUPDATE)
395 cdev->private->state = DEV_STATE_ONLINE;
397 spin_unlock_irqrestore(cdev->ccwlock, flags);
399 spin_lock_irqsave(cdev->ccwlock, flags);
400 cdev->private->cmb_wait = NULL;
401 ret = copy_block->ret;
403 kref_put(©_block->kref, cmf_copy_block_release);
405 spin_unlock_irqrestore(cdev->ccwlock, flags);
409 void cmf_retry_copy_block(struct ccw_device *cdev)
411 struct copy_block_struct *copy_block;
413 copy_block = cdev->private->cmb_wait;
418 kref_get(©_block->kref);
419 copy_block->ret = cmf_copy_block(cdev);
420 wake_up(©_block->wait);
421 kref_put(©_block->kref, cmf_copy_block_release);
424 static void cmf_generic_reset(struct ccw_device *cdev)
426 struct cmb_data *cmb_data;
428 spin_lock_irq(cdev->ccwlock);
429 cmb_data = cdev->private->cmb;
431 memset(cmb_data->last_block, 0, cmb_data->size);
433 * Need to reset hw block as well to make the hardware start
436 memset(cmb_data->hw_block, 0, cmb_data->size);
437 cmb_data->last_update = 0;
439 cdev->private->cmb_start_time = get_tod_clock();
440 spin_unlock_irq(cdev->ccwlock);
444 * struct cmb_area - container for global cmb data
446 * @mem: pointer to CMBs (only in basic measurement mode)
447 * @list: contains a linked list of all subchannels
448 * @num_channels: number of channels to be measured
449 * @lock: protect concurrent access to @mem and @list
453 struct list_head list;
458 static struct cmb_area cmb_area = {
459 .lock = __SPIN_LOCK_UNLOCKED(cmb_area.lock),
460 .list = LIST_HEAD_INIT(cmb_area.list),
461 .num_channels = 1024,
464 /* ****** old style CMB handling ********/
467 * Basic channel measurement blocks are allocated in one contiguous
468 * block of memory, which can not be moved as long as any channel
469 * is active. Therefore, a maximum number of subchannels needs to
470 * be defined somewhere. This is a module parameter, defaulting to
471 * a reasonable value of 1024, or 32 kb of memory.
472 * Current kernels don't allow kmalloc with more than 128kb, so the
476 module_param_named(maxchannels, cmb_area.num_channels, uint, 0444);
479 * struct cmb - basic channel measurement block
480 * @ssch_rsch_count: number of ssch and rsch
481 * @sample_count: number of samples
482 * @device_connect_time: time of device connect
483 * @function_pending_time: time of function pending
484 * @device_disconnect_time: time of device disconnect
485 * @control_unit_queuing_time: time of control unit queuing
486 * @device_active_only_time: time of device active only
487 * @reserved: unused in basic measurement mode
489 * The measurement block as used by the hardware. The fields are described
490 * further in z/Architecture Principles of Operation, chapter 17.
492 * The cmb area made up from these blocks must be a contiguous array and may
493 * not be reallocated or freed.
494 * Only one cmb area can be present in the system.
499 u32 device_connect_time;
500 u32 function_pending_time;
501 u32 device_disconnect_time;
502 u32 control_unit_queuing_time;
503 u32 device_active_only_time;
508 * Insert a single device into the cmb_area list.
509 * Called with cmb_area.lock held from alloc_cmb.
511 static int alloc_cmb_single(struct ccw_device *cdev,
512 struct cmb_data *cmb_data)
515 struct ccw_device_private *node;
518 spin_lock_irq(cdev->ccwlock);
519 if (!list_empty(&cdev->private->cmb_list)) {
525 * Find first unused cmb in cmb_area.mem.
526 * This is a little tricky: cmb_area.list
527 * remains sorted by ->cmb->hw_data pointers.
530 list_for_each_entry(node, &cmb_area.list, cmb_list) {
531 struct cmb_data *data;
533 if ((struct cmb*)data->hw_block > cmb)
537 if (cmb - cmb_area.mem >= cmb_area.num_channels) {
543 list_add_tail(&cdev->private->cmb_list, &node->cmb_list);
544 cmb_data->hw_block = cmb;
545 cdev->private->cmb = cmb_data;
548 spin_unlock_irq(cdev->ccwlock);
552 static int alloc_cmb(struct ccw_device *cdev)
557 struct cmb_data *cmb_data;
559 /* Allocate private cmb_data. */
560 cmb_data = kzalloc(sizeof(struct cmb_data), GFP_KERNEL);
564 cmb_data->last_block = kzalloc(sizeof(struct cmb), GFP_KERNEL);
565 if (!cmb_data->last_block) {
569 cmb_data->size = sizeof(struct cmb);
570 spin_lock(&cmb_area.lock);
573 /* there is no user yet, so we need a new area */
574 size = sizeof(struct cmb) * cmb_area.num_channels;
575 WARN_ON(!list_empty(&cmb_area.list));
577 spin_unlock(&cmb_area.lock);
578 mem = (void*)__get_free_pages(GFP_KERNEL | GFP_DMA,
580 spin_lock(&cmb_area.lock);
583 /* ok, another thread was faster */
584 free_pages((unsigned long)mem, get_order(size));
591 memset(mem, 0, size);
593 cmf_activate(cmb_area.mem, CMF_ON);
597 /* do the actual allocation */
598 ret = alloc_cmb_single(cdev, cmb_data);
600 spin_unlock(&cmb_area.lock);
602 kfree(cmb_data->last_block);
608 static void free_cmb(struct ccw_device *cdev)
610 struct ccw_device_private *priv;
611 struct cmb_data *cmb_data;
613 spin_lock(&cmb_area.lock);
614 spin_lock_irq(cdev->ccwlock);
616 priv = cdev->private;
617 cmb_data = priv->cmb;
620 kfree(cmb_data->last_block);
622 list_del_init(&priv->cmb_list);
624 if (list_empty(&cmb_area.list)) {
626 size = sizeof(struct cmb) * cmb_area.num_channels;
627 cmf_activate(NULL, CMF_OFF);
628 free_pages((unsigned long)cmb_area.mem, get_order(size));
631 spin_unlock_irq(cdev->ccwlock);
632 spin_unlock(&cmb_area.lock);
635 static int set_cmb(struct ccw_device *cdev, u32 mme)
638 struct cmb_data *cmb_data;
641 spin_lock_irqsave(cdev->ccwlock, flags);
642 if (!cdev->private->cmb) {
643 spin_unlock_irqrestore(cdev->ccwlock, flags);
646 cmb_data = cdev->private->cmb;
647 offset = mme ? (struct cmb *)cmb_data->hw_block - cmb_area.mem : 0;
648 spin_unlock_irqrestore(cdev->ccwlock, flags);
650 return set_schib_wait(cdev, mme, 0, offset);
653 static u64 read_cmb(struct ccw_device *cdev, int index)
660 ret = cmf_cmb_copy_wait(cdev);
664 spin_lock_irqsave(cdev->ccwlock, flags);
665 if (!cdev->private->cmb) {
669 cmb = ((struct cmb_data *)cdev->private->cmb)->last_block;
672 case cmb_ssch_rsch_count:
673 ret = cmb->ssch_rsch_count;
675 case cmb_sample_count:
676 ret = cmb->sample_count;
678 case cmb_device_connect_time:
679 val = cmb->device_connect_time;
681 case cmb_function_pending_time:
682 val = cmb->function_pending_time;
684 case cmb_device_disconnect_time:
685 val = cmb->device_disconnect_time;
687 case cmb_control_unit_queuing_time:
688 val = cmb->control_unit_queuing_time;
690 case cmb_device_active_only_time:
691 val = cmb->device_active_only_time;
697 ret = time_to_avg_nsec(val, cmb->sample_count);
699 spin_unlock_irqrestore(cdev->ccwlock, flags);
703 static int readall_cmb(struct ccw_device *cdev, struct cmbdata *data)
706 struct cmb_data *cmb_data;
711 ret = cmf_cmb_copy_wait(cdev);
714 spin_lock_irqsave(cdev->ccwlock, flags);
715 cmb_data = cdev->private->cmb;
720 if (cmb_data->last_update == 0) {
724 cmb = cmb_data->last_block;
725 time = cmb_data->last_update - cdev->private->cmb_start_time;
727 memset(data, 0, sizeof(struct cmbdata));
729 /* we only know values before device_busy_time */
730 data->size = offsetof(struct cmbdata, device_busy_time);
732 /* convert to nanoseconds */
733 data->elapsed_time = (time * 1000) >> 12;
735 /* copy data to new structure */
736 data->ssch_rsch_count = cmb->ssch_rsch_count;
737 data->sample_count = cmb->sample_count;
739 /* time fields are converted to nanoseconds while copying */
740 data->device_connect_time = time_to_nsec(cmb->device_connect_time);
741 data->function_pending_time = time_to_nsec(cmb->function_pending_time);
742 data->device_disconnect_time =
743 time_to_nsec(cmb->device_disconnect_time);
744 data->control_unit_queuing_time
745 = time_to_nsec(cmb->control_unit_queuing_time);
746 data->device_active_only_time
747 = time_to_nsec(cmb->device_active_only_time);
750 spin_unlock_irqrestore(cdev->ccwlock, flags);
754 static void reset_cmb(struct ccw_device *cdev)
756 cmf_generic_reset(cdev);
759 static int cmf_enabled(struct ccw_device *cdev)
763 spin_lock_irq(cdev->ccwlock);
764 enabled = !!cdev->private->cmb;
765 spin_unlock_irq(cdev->ccwlock);
770 static struct attribute_group cmf_attr_group;
772 static struct cmb_operations cmbops_basic = {
777 .readall = readall_cmb,
779 .attr_group = &cmf_attr_group,
782 /* ******** extended cmb handling ********/
785 * struct cmbe - extended channel measurement block
786 * @ssch_rsch_count: number of ssch and rsch
787 * @sample_count: number of samples
788 * @device_connect_time: time of device connect
789 * @function_pending_time: time of function pending
790 * @device_disconnect_time: time of device disconnect
791 * @control_unit_queuing_time: time of control unit queuing
792 * @device_active_only_time: time of device active only
793 * @device_busy_time: time of device busy
794 * @initial_command_response_time: initial command response time
797 * The measurement block as used by the hardware. May be in any 64 bit physical
799 * The fields are described further in z/Architecture Principles of Operation,
800 * third edition, chapter 17.
805 u32 device_connect_time;
806 u32 function_pending_time;
807 u32 device_disconnect_time;
808 u32 control_unit_queuing_time;
809 u32 device_active_only_time;
810 u32 device_busy_time;
811 u32 initial_command_response_time;
813 } __packed __aligned(64);
815 static struct kmem_cache *cmbe_cache;
817 static int alloc_cmbe(struct ccw_device *cdev)
819 struct cmb_data *cmb_data;
823 cmbe = kmem_cache_zalloc(cmbe_cache, GFP_KERNEL);
827 cmb_data = kzalloc(sizeof(*cmb_data), GFP_KERNEL);
831 cmb_data->last_block = kzalloc(sizeof(struct cmbe), GFP_KERNEL);
832 if (!cmb_data->last_block)
835 cmb_data->size = sizeof(*cmbe);
836 cmb_data->hw_block = cmbe;
838 spin_lock(&cmb_area.lock);
839 spin_lock_irq(cdev->ccwlock);
840 if (cdev->private->cmb)
843 cdev->private->cmb = cmb_data;
845 /* activate global measurement if this is the first channel */
846 if (list_empty(&cmb_area.list))
847 cmf_activate(NULL, CMF_ON);
848 list_add_tail(&cdev->private->cmb_list, &cmb_area.list);
850 spin_unlock_irq(cdev->ccwlock);
851 spin_unlock(&cmb_area.lock);
855 spin_unlock_irq(cdev->ccwlock);
856 spin_unlock(&cmb_area.lock);
860 kfree(cmb_data->last_block);
862 kmem_cache_free(cmbe_cache, cmbe);
867 static void free_cmbe(struct ccw_device *cdev)
869 struct cmb_data *cmb_data;
871 spin_lock(&cmb_area.lock);
872 spin_lock_irq(cdev->ccwlock);
873 cmb_data = cdev->private->cmb;
874 cdev->private->cmb = NULL;
876 kfree(cmb_data->last_block);
877 kmem_cache_free(cmbe_cache, cmb_data->hw_block);
881 /* deactivate global measurement if this is the last channel */
882 list_del_init(&cdev->private->cmb_list);
883 if (list_empty(&cmb_area.list))
884 cmf_activate(NULL, CMF_OFF);
885 spin_unlock_irq(cdev->ccwlock);
886 spin_unlock(&cmb_area.lock);
889 static int set_cmbe(struct ccw_device *cdev, u32 mme)
892 struct cmb_data *cmb_data;
895 spin_lock_irqsave(cdev->ccwlock, flags);
896 if (!cdev->private->cmb) {
897 spin_unlock_irqrestore(cdev->ccwlock, flags);
900 cmb_data = cdev->private->cmb;
901 mba = mme ? (unsigned long) cmb_data->hw_block : 0;
902 spin_unlock_irqrestore(cdev->ccwlock, flags);
904 return set_schib_wait(cdev, mme, 1, mba);
908 static u64 read_cmbe(struct ccw_device *cdev, int index)
911 struct cmb_data *cmb_data;
916 ret = cmf_cmb_copy_wait(cdev);
920 spin_lock_irqsave(cdev->ccwlock, flags);
921 cmb_data = cdev->private->cmb;
926 cmb = cmb_data->last_block;
929 case cmb_ssch_rsch_count:
930 ret = cmb->ssch_rsch_count;
932 case cmb_sample_count:
933 ret = cmb->sample_count;
935 case cmb_device_connect_time:
936 val = cmb->device_connect_time;
938 case cmb_function_pending_time:
939 val = cmb->function_pending_time;
941 case cmb_device_disconnect_time:
942 val = cmb->device_disconnect_time;
944 case cmb_control_unit_queuing_time:
945 val = cmb->control_unit_queuing_time;
947 case cmb_device_active_only_time:
948 val = cmb->device_active_only_time;
950 case cmb_device_busy_time:
951 val = cmb->device_busy_time;
953 case cmb_initial_command_response_time:
954 val = cmb->initial_command_response_time;
960 ret = time_to_avg_nsec(val, cmb->sample_count);
962 spin_unlock_irqrestore(cdev->ccwlock, flags);
966 static int readall_cmbe(struct ccw_device *cdev, struct cmbdata *data)
969 struct cmb_data *cmb_data;
974 ret = cmf_cmb_copy_wait(cdev);
977 spin_lock_irqsave(cdev->ccwlock, flags);
978 cmb_data = cdev->private->cmb;
983 if (cmb_data->last_update == 0) {
987 time = cmb_data->last_update - cdev->private->cmb_start_time;
989 memset (data, 0, sizeof(struct cmbdata));
991 /* we only know values before device_busy_time */
992 data->size = offsetof(struct cmbdata, device_busy_time);
994 /* conver to nanoseconds */
995 data->elapsed_time = (time * 1000) >> 12;
997 cmb = cmb_data->last_block;
998 /* copy data to new structure */
999 data->ssch_rsch_count = cmb->ssch_rsch_count;
1000 data->sample_count = cmb->sample_count;
1002 /* time fields are converted to nanoseconds while copying */
1003 data->device_connect_time = time_to_nsec(cmb->device_connect_time);
1004 data->function_pending_time = time_to_nsec(cmb->function_pending_time);
1005 data->device_disconnect_time =
1006 time_to_nsec(cmb->device_disconnect_time);
1007 data->control_unit_queuing_time
1008 = time_to_nsec(cmb->control_unit_queuing_time);
1009 data->device_active_only_time
1010 = time_to_nsec(cmb->device_active_only_time);
1011 data->device_busy_time = time_to_nsec(cmb->device_busy_time);
1012 data->initial_command_response_time
1013 = time_to_nsec(cmb->initial_command_response_time);
1017 spin_unlock_irqrestore(cdev->ccwlock, flags);
1021 static void reset_cmbe(struct ccw_device *cdev)
1023 cmf_generic_reset(cdev);
1026 static struct attribute_group cmf_attr_group_ext;
1028 static struct cmb_operations cmbops_extended = {
1029 .alloc = alloc_cmbe,
1033 .readall = readall_cmbe,
1034 .reset = reset_cmbe,
1035 .attr_group = &cmf_attr_group_ext,
1038 static ssize_t cmb_show_attr(struct device *dev, char *buf, enum cmb_index idx)
1040 return sprintf(buf, "%lld\n",
1041 (unsigned long long) cmf_read(to_ccwdev(dev), idx));
1044 static ssize_t cmb_show_avg_sample_interval(struct device *dev,
1045 struct device_attribute *attr,
1048 struct ccw_device *cdev;
1050 unsigned long count;
1051 struct cmb_data *cmb_data;
1053 cdev = to_ccwdev(dev);
1054 count = cmf_read(cdev, cmb_sample_count);
1055 spin_lock_irq(cdev->ccwlock);
1056 cmb_data = cdev->private->cmb;
1058 interval = cmb_data->last_update -
1059 cdev->private->cmb_start_time;
1060 interval = (interval * 1000) >> 12;
1064 spin_unlock_irq(cdev->ccwlock);
1065 return sprintf(buf, "%ld\n", interval);
1068 static ssize_t cmb_show_avg_utilization(struct device *dev,
1069 struct device_attribute *attr,
1072 struct cmbdata data;
1077 ret = cmf_readall(to_ccwdev(dev), &data);
1078 if (ret == -EAGAIN || ret == -ENODEV)
1079 /* No data (yet/currently) available to use for calculation. */
1080 return sprintf(buf, "n/a\n");
1084 utilization = data.device_connect_time +
1085 data.function_pending_time +
1086 data.device_disconnect_time;
1088 /* calculate value in 0.1 percent units */
1089 t = data.elapsed_time / 1000;
1090 u = utilization / t;
1092 return sprintf(buf, "%02ld.%01ld%%\n", u/ 10, u - (u/ 10) * 10);
1095 #define cmf_attr(name) \
1096 static ssize_t show_##name(struct device *dev, \
1097 struct device_attribute *attr, char *buf) \
1098 { return cmb_show_attr((dev), buf, cmb_##name); } \
1099 static DEVICE_ATTR(name, 0444, show_##name, NULL);
1101 #define cmf_attr_avg(name) \
1102 static ssize_t show_avg_##name(struct device *dev, \
1103 struct device_attribute *attr, char *buf) \
1104 { return cmb_show_attr((dev), buf, cmb_##name); } \
1105 static DEVICE_ATTR(avg_##name, 0444, show_avg_##name, NULL);
1107 cmf_attr(ssch_rsch_count);
1108 cmf_attr(sample_count);
1109 cmf_attr_avg(device_connect_time);
1110 cmf_attr_avg(function_pending_time);
1111 cmf_attr_avg(device_disconnect_time);
1112 cmf_attr_avg(control_unit_queuing_time);
1113 cmf_attr_avg(device_active_only_time);
1114 cmf_attr_avg(device_busy_time);
1115 cmf_attr_avg(initial_command_response_time);
1117 static DEVICE_ATTR(avg_sample_interval, 0444, cmb_show_avg_sample_interval,
1119 static DEVICE_ATTR(avg_utilization, 0444, cmb_show_avg_utilization, NULL);
1121 static struct attribute *cmf_attributes[] = {
1122 &dev_attr_avg_sample_interval.attr,
1123 &dev_attr_avg_utilization.attr,
1124 &dev_attr_ssch_rsch_count.attr,
1125 &dev_attr_sample_count.attr,
1126 &dev_attr_avg_device_connect_time.attr,
1127 &dev_attr_avg_function_pending_time.attr,
1128 &dev_attr_avg_device_disconnect_time.attr,
1129 &dev_attr_avg_control_unit_queuing_time.attr,
1130 &dev_attr_avg_device_active_only_time.attr,
1134 static struct attribute_group cmf_attr_group = {
1136 .attrs = cmf_attributes,
1139 static struct attribute *cmf_attributes_ext[] = {
1140 &dev_attr_avg_sample_interval.attr,
1141 &dev_attr_avg_utilization.attr,
1142 &dev_attr_ssch_rsch_count.attr,
1143 &dev_attr_sample_count.attr,
1144 &dev_attr_avg_device_connect_time.attr,
1145 &dev_attr_avg_function_pending_time.attr,
1146 &dev_attr_avg_device_disconnect_time.attr,
1147 &dev_attr_avg_control_unit_queuing_time.attr,
1148 &dev_attr_avg_device_active_only_time.attr,
1149 &dev_attr_avg_device_busy_time.attr,
1150 &dev_attr_avg_initial_command_response_time.attr,
1154 static struct attribute_group cmf_attr_group_ext = {
1156 .attrs = cmf_attributes_ext,
1159 static ssize_t cmb_enable_show(struct device *dev,
1160 struct device_attribute *attr,
1163 struct ccw_device *cdev = to_ccwdev(dev);
1165 return sprintf(buf, "%d\n", cmf_enabled(cdev));
1168 static ssize_t cmb_enable_store(struct device *dev,
1169 struct device_attribute *attr, const char *buf,
1172 struct ccw_device *cdev = to_ccwdev(dev);
1176 ret = kstrtoul(buf, 16, &val);
1182 ret = disable_cmf(cdev);
1185 ret = enable_cmf(cdev);
1191 return ret ? ret : c;
1193 DEVICE_ATTR_RW(cmb_enable);
1195 int ccw_set_cmf(struct ccw_device *cdev, int enable)
1197 return cmbops->set(cdev, enable ? 2 : 0);
1201 * enable_cmf() - switch on the channel measurement for a specific device
1202 * @cdev: The ccw device to be enabled
1204 * Returns %0 for success or a negative error value.
1205 * Note: If this is called on a device for which channel measurement is already
1206 * enabled a reset of the measurement data is triggered.
1210 int enable_cmf(struct ccw_device *cdev)
1214 device_lock(&cdev->dev);
1215 if (cmf_enabled(cdev)) {
1216 cmbops->reset(cdev);
1219 get_device(&cdev->dev);
1220 ret = cmbops->alloc(cdev);
1223 cmbops->reset(cdev);
1224 ret = sysfs_create_group(&cdev->dev.kobj, cmbops->attr_group);
1229 ret = cmbops->set(cdev, 2);
1231 sysfs_remove_group(&cdev->dev.kobj, cmbops->attr_group);
1236 put_device(&cdev->dev);
1238 device_unlock(&cdev->dev);
1243 * __disable_cmf() - switch off the channel measurement for a specific device
1244 * @cdev: The ccw device to be disabled
1246 * Returns %0 for success or a negative error value.
1249 * non-atomic, device_lock() held.
1251 int __disable_cmf(struct ccw_device *cdev)
1255 ret = cmbops->set(cdev, 0);
1259 sysfs_remove_group(&cdev->dev.kobj, cmbops->attr_group);
1261 put_device(&cdev->dev);
1267 * disable_cmf() - switch off the channel measurement for a specific device
1268 * @cdev: The ccw device to be disabled
1270 * Returns %0 for success or a negative error value.
1275 int disable_cmf(struct ccw_device *cdev)
1279 device_lock(&cdev->dev);
1280 ret = __disable_cmf(cdev);
1281 device_unlock(&cdev->dev);
1287 * cmf_read() - read one value from the current channel measurement block
1288 * @cdev: the channel to be read
1289 * @index: the index of the value to be read
1291 * Returns the value read or %0 if the value cannot be read.
1296 u64 cmf_read(struct ccw_device *cdev, int index)
1298 return cmbops->read(cdev, index);
1302 * cmf_readall() - read the current channel measurement block
1303 * @cdev: the channel to be read
1304 * @data: a pointer to a data block that will be filled
1306 * Returns %0 on success, a negative error value otherwise.
1311 int cmf_readall(struct ccw_device *cdev, struct cmbdata *data)
1313 return cmbops->readall(cdev, data);
1316 /* Reenable cmf when a disconnected device becomes available again. */
1317 int cmf_reenable(struct ccw_device *cdev)
1319 cmbops->reset(cdev);
1320 return cmbops->set(cdev, 2);
1324 * cmf_reactivate() - reactivate measurement block updates
1326 * Use this during resume from hibernate.
1328 void cmf_reactivate(void)
1330 spin_lock(&cmb_area.lock);
1331 if (!list_empty(&cmb_area.list))
1332 cmf_activate(cmb_area.mem, CMF_ON);
1333 spin_unlock(&cmb_area.lock);
1336 static int __init init_cmbe(void)
1338 cmbe_cache = kmem_cache_create("cmbe_cache", sizeof(struct cmbe),
1339 __alignof__(struct cmbe), 0, NULL);
1341 return cmbe_cache ? 0 : -ENOMEM;
1344 static int __init init_cmf(void)
1346 char *format_string;
1347 char *detect_string;
1351 * If the user did not give a parameter, see if we are running on a
1352 * machine supporting extended measurement blocks, otherwise fall back
1355 if (format == CMF_AUTODETECT) {
1356 if (!css_general_characteristics.ext_mb) {
1359 format = CMF_EXTENDED;
1361 detect_string = "autodetected";
1363 detect_string = "parameter";
1368 format_string = "basic";
1369 cmbops = &cmbops_basic;
1372 format_string = "extended";
1373 cmbops = &cmbops_extended;
1382 pr_info("Channel measurement facility initialized using format "
1383 "%s (mode %s)\n", format_string, detect_string);
1386 device_initcall(init_cmf);
1388 EXPORT_SYMBOL_GPL(enable_cmf);
1389 EXPORT_SYMBOL_GPL(disable_cmf);
1390 EXPORT_SYMBOL_GPL(cmf_read);
1391 EXPORT_SYMBOL_GPL(cmf_readall);