2 * linux/drivers/s390/cio/cmf.c
4 * Linux on zSeries Channel Measurement Facility support
6 * Copyright 2000,2006 IBM Corporation
8 * Authors: Arnd Bergmann <arndb@de.ibm.com>
9 * Cornelia Huck <cornelia.huck@de.ibm.com>
11 * original idea from Natarajan Krishnaswami <nkrishna@us.ibm.com>
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2, or (at your option)
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 #define KMSG_COMPONENT "cio"
29 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
31 #include <linux/bootmem.h>
32 #include <linux/device.h>
33 #include <linux/init.h>
34 #include <linux/list.h>
35 #include <linux/module.h>
36 #include <linux/moduleparam.h>
37 #include <linux/slab.h>
38 #include <linux/timex.h> /* get_clock() */
40 #include <asm/ccwdev.h>
43 #include <asm/div64.h>
52 * parameter to enable cmf during boot, possible uses are:
53 * "s390cmf" -- enable cmf and allocate 2 MB of ram so measuring can be
54 * used on any subchannel
55 * "s390cmf=<num>" -- enable cmf and allocate enough memory to measure
56 * <num> subchannel, where <num> is an integer
57 * between 1 and 65535, default is 1024
59 #define ARGSTRING "s390cmf"
61 /* indices for READCMB */
63 /* basic and exended format: */
66 cmb_device_connect_time,
67 cmb_function_pending_time,
68 cmb_device_disconnect_time,
69 cmb_control_unit_queuing_time,
70 cmb_device_active_only_time,
71 /* extended format only: */
73 cmb_initial_command_response_time,
77 * enum cmb_format - types of supported measurement block formats
79 * @CMF_BASIC: traditional channel measurement blocks supported
80 * by all machines that we run on
81 * @CMF_EXTENDED: improved format that was introduced with the z990
83 * @CMF_AUTODETECT: default: use extended format when running on a machine
84 * supporting extended format, otherwise fall back to
94 * format - actual format for all measurement blocks
96 * The format module parameter can be set to a value of 0 (zero)
97 * or 1, indicating basic or extended format as described for
100 static int format = CMF_AUTODETECT;
101 module_param(format, bool, 0444);
104 * struct cmb_operations - functions to use depending on cmb_format
106 * Most of these functions operate on a struct ccw_device. There is only
107 * one instance of struct cmb_operations because the format of the measurement
108 * data is guaranteed to be the same for every ccw_device.
110 * @alloc: allocate memory for a channel measurement block,
111 * either with the help of a special pool or with kmalloc
112 * @free: free memory allocated with @alloc
113 * @set: enable or disable measurement
114 * @read: read a measurement entry at an index
115 * @readall: read a measurement block in a common format
116 * @reset: clear the data in the associated measurement block and
117 * reset its time stamp
118 * @align: align an allocated block so that the hardware can use it
120 struct cmb_operations {
121 int (*alloc) (struct ccw_device *);
122 void (*free) (struct ccw_device *);
123 int (*set) (struct ccw_device *, u32);
124 u64 (*read) (struct ccw_device *, int);
125 int (*readall)(struct ccw_device *, struct cmbdata *);
126 void (*reset) (struct ccw_device *);
127 void *(*align) (void *);
129 struct attribute_group *attr_group;
131 static struct cmb_operations *cmbops;
134 void *hw_block; /* Pointer to block updated by hardware */
135 void *last_block; /* Last changed block copied from hardware block */
136 int size; /* Size of hw_block and last_block */
137 unsigned long long last_update; /* when last_block was updated */
141 * Our user interface is designed in terms of nanoseconds,
142 * while the hardware measures total times in its own
145 static inline u64 time_to_nsec(u32 value)
147 return ((u64)value) * 128000ull;
151 * Users are usually interested in average times,
152 * not accumulated time.
153 * This also helps us with atomicity problems
154 * when reading sinlge values.
156 static inline u64 time_to_avg_nsec(u32 value, u32 count)
160 /* no samples yet, avoid division by 0 */
164 /* value comes in units of 128 µsec */
165 ret = time_to_nsec(value);
172 * Activate or deactivate the channel monitor. When area is NULL,
173 * the monitor is deactivated. The channel monitor needs to
174 * be active in order to measure subchannels, which also need
177 static inline void cmf_activate(void *area, unsigned int onoff)
179 register void * __gpr2 asm("2");
180 register long __gpr1 asm("1");
183 __gpr1 = onoff ? 2 : 0;
184 /* activate channel measurement */
185 asm("schm" : : "d" (__gpr2), "d" (__gpr1) );
188 static int set_schib(struct ccw_device *cdev, u32 mme, int mbfc,
189 unsigned long address)
191 struct subchannel *sch;
193 sch = to_subchannel(cdev->dev.parent);
195 sch->config.mme = mme;
196 sch->config.mbfc = mbfc;
197 /* address can be either a block address or a block index */
199 sch->config.mba = address;
201 sch->config.mbi = address;
203 return cio_commit_config(sch);
206 struct set_schib_struct {
209 unsigned long address;
210 wait_queue_head_t wait;
215 static void cmf_set_schib_release(struct kref *kref)
217 struct set_schib_struct *set_data;
219 set_data = container_of(kref, struct set_schib_struct, kref);
223 #define CMF_PENDING 1
225 static int set_schib_wait(struct ccw_device *cdev, u32 mme,
226 int mbfc, unsigned long address)
228 struct set_schib_struct *set_data;
231 spin_lock_irq(cdev->ccwlock);
232 if (!cdev->private->cmb) {
236 set_data = kzalloc(sizeof(struct set_schib_struct), GFP_ATOMIC);
241 init_waitqueue_head(&set_data->wait);
242 kref_init(&set_data->kref);
244 set_data->mbfc = mbfc;
245 set_data->address = address;
247 ret = set_schib(cdev, mme, mbfc, address);
251 if (cdev->private->state != DEV_STATE_ONLINE) {
252 /* if the device is not online, don't even try again */
257 cdev->private->state = DEV_STATE_CMFCHANGE;
258 set_data->ret = CMF_PENDING;
259 cdev->private->cmb_wait = set_data;
261 spin_unlock_irq(cdev->ccwlock);
262 if (wait_event_interruptible(set_data->wait,
263 set_data->ret != CMF_PENDING)) {
264 spin_lock_irq(cdev->ccwlock);
265 if (set_data->ret == CMF_PENDING) {
266 set_data->ret = -ERESTARTSYS;
267 if (cdev->private->state == DEV_STATE_CMFCHANGE)
268 cdev->private->state = DEV_STATE_ONLINE;
270 spin_unlock_irq(cdev->ccwlock);
272 spin_lock_irq(cdev->ccwlock);
273 cdev->private->cmb_wait = NULL;
276 kref_put(&set_data->kref, cmf_set_schib_release);
278 spin_unlock_irq(cdev->ccwlock);
282 void retry_set_schib(struct ccw_device *cdev)
284 struct set_schib_struct *set_data;
286 set_data = cdev->private->cmb_wait;
291 kref_get(&set_data->kref);
292 set_data->ret = set_schib(cdev, set_data->mme, set_data->mbfc,
294 wake_up(&set_data->wait);
295 kref_put(&set_data->kref, cmf_set_schib_release);
298 static int cmf_copy_block(struct ccw_device *cdev)
300 struct subchannel *sch;
303 struct cmb_data *cmb_data;
305 sch = to_subchannel(cdev->dev.parent);
307 if (cio_update_schib(sch))
310 if (scsw_fctl(&sch->schib.scsw) & SCSW_FCTL_START_FUNC) {
311 /* Don't copy if a start function is in progress. */
312 if ((!(scsw_actl(&sch->schib.scsw) & SCSW_ACTL_SUSPENDED)) &&
313 (scsw_actl(&sch->schib.scsw) &
314 (SCSW_ACTL_DEVACT | SCSW_ACTL_SCHACT)) &&
315 (!(scsw_stctl(&sch->schib.scsw) & SCSW_STCTL_SEC_STATUS)))
318 cmb_data = cdev->private->cmb;
319 hw_block = cmbops->align(cmb_data->hw_block);
320 if (!memcmp(cmb_data->last_block, hw_block, cmb_data->size))
321 /* No need to copy. */
323 reference_buf = kzalloc(cmb_data->size, GFP_ATOMIC);
326 /* Ensure consistency of block copied from hardware. */
328 memcpy(cmb_data->last_block, hw_block, cmb_data->size);
329 memcpy(reference_buf, hw_block, cmb_data->size);
330 } while (memcmp(cmb_data->last_block, reference_buf, cmb_data->size));
331 cmb_data->last_update = get_clock();
332 kfree(reference_buf);
336 struct copy_block_struct {
337 wait_queue_head_t wait;
342 static void cmf_copy_block_release(struct kref *kref)
344 struct copy_block_struct *copy_block;
346 copy_block = container_of(kref, struct copy_block_struct, kref);
350 static int cmf_cmb_copy_wait(struct ccw_device *cdev)
352 struct copy_block_struct *copy_block;
356 spin_lock_irqsave(cdev->ccwlock, flags);
357 if (!cdev->private->cmb) {
361 copy_block = kzalloc(sizeof(struct copy_block_struct), GFP_ATOMIC);
366 init_waitqueue_head(©_block->wait);
367 kref_init(©_block->kref);
369 ret = cmf_copy_block(cdev);
373 if (cdev->private->state != DEV_STATE_ONLINE) {
378 cdev->private->state = DEV_STATE_CMFUPDATE;
379 copy_block->ret = CMF_PENDING;
380 cdev->private->cmb_wait = copy_block;
382 spin_unlock_irqrestore(cdev->ccwlock, flags);
383 if (wait_event_interruptible(copy_block->wait,
384 copy_block->ret != CMF_PENDING)) {
385 spin_lock_irqsave(cdev->ccwlock, flags);
386 if (copy_block->ret == CMF_PENDING) {
387 copy_block->ret = -ERESTARTSYS;
388 if (cdev->private->state == DEV_STATE_CMFUPDATE)
389 cdev->private->state = DEV_STATE_ONLINE;
391 spin_unlock_irqrestore(cdev->ccwlock, flags);
393 spin_lock_irqsave(cdev->ccwlock, flags);
394 cdev->private->cmb_wait = NULL;
395 ret = copy_block->ret;
397 kref_put(©_block->kref, cmf_copy_block_release);
399 spin_unlock_irqrestore(cdev->ccwlock, flags);
403 void cmf_retry_copy_block(struct ccw_device *cdev)
405 struct copy_block_struct *copy_block;
407 copy_block = cdev->private->cmb_wait;
412 kref_get(©_block->kref);
413 copy_block->ret = cmf_copy_block(cdev);
414 wake_up(©_block->wait);
415 kref_put(©_block->kref, cmf_copy_block_release);
418 static void cmf_generic_reset(struct ccw_device *cdev)
420 struct cmb_data *cmb_data;
422 spin_lock_irq(cdev->ccwlock);
423 cmb_data = cdev->private->cmb;
425 memset(cmb_data->last_block, 0, cmb_data->size);
427 * Need to reset hw block as well to make the hardware start
430 memset(cmbops->align(cmb_data->hw_block), 0, cmb_data->size);
431 cmb_data->last_update = 0;
433 cdev->private->cmb_start_time = get_clock();
434 spin_unlock_irq(cdev->ccwlock);
438 * struct cmb_area - container for global cmb data
440 * @mem: pointer to CMBs (only in basic measurement mode)
441 * @list: contains a linked list of all subchannels
442 * @num_channels: number of channels to be measured
443 * @lock: protect concurrent access to @mem and @list
447 struct list_head list;
452 static struct cmb_area cmb_area = {
453 .lock = __SPIN_LOCK_UNLOCKED(cmb_area.lock),
454 .list = LIST_HEAD_INIT(cmb_area.list),
455 .num_channels = 1024,
458 /* ****** old style CMB handling ********/
461 * Basic channel measurement blocks are allocated in one contiguous
462 * block of memory, which can not be moved as long as any channel
463 * is active. Therefore, a maximum number of subchannels needs to
464 * be defined somewhere. This is a module parameter, defaulting to
465 * a resonable value of 1024, or 32 kb of memory.
466 * Current kernels don't allow kmalloc with more than 128kb, so the
470 module_param_named(maxchannels, cmb_area.num_channels, uint, 0444);
473 * struct cmb - basic channel measurement block
474 * @ssch_rsch_count: number of ssch and rsch
475 * @sample_count: number of samples
476 * @device_connect_time: time of device connect
477 * @function_pending_time: time of function pending
478 * @device_disconnect_time: time of device disconnect
479 * @control_unit_queuing_time: time of control unit queuing
480 * @device_active_only_time: time of device active only
481 * @reserved: unused in basic measurement mode
483 * The measurement block as used by the hardware. The fields are described
484 * further in z/Architecture Principles of Operation, chapter 17.
486 * The cmb area made up from these blocks must be a contiguous array and may
487 * not be reallocated or freed.
488 * Only one cmb area can be present in the system.
493 u32 device_connect_time;
494 u32 function_pending_time;
495 u32 device_disconnect_time;
496 u32 control_unit_queuing_time;
497 u32 device_active_only_time;
502 * Insert a single device into the cmb_area list.
503 * Called with cmb_area.lock held from alloc_cmb.
505 static int alloc_cmb_single(struct ccw_device *cdev,
506 struct cmb_data *cmb_data)
509 struct ccw_device_private *node;
512 spin_lock_irq(cdev->ccwlock);
513 if (!list_empty(&cdev->private->cmb_list)) {
519 * Find first unused cmb in cmb_area.mem.
520 * This is a little tricky: cmb_area.list
521 * remains sorted by ->cmb->hw_data pointers.
524 list_for_each_entry(node, &cmb_area.list, cmb_list) {
525 struct cmb_data *data;
527 if ((struct cmb*)data->hw_block > cmb)
531 if (cmb - cmb_area.mem >= cmb_area.num_channels) {
537 list_add_tail(&cdev->private->cmb_list, &node->cmb_list);
538 cmb_data->hw_block = cmb;
539 cdev->private->cmb = cmb_data;
542 spin_unlock_irq(cdev->ccwlock);
546 static int alloc_cmb(struct ccw_device *cdev)
551 struct cmb_data *cmb_data;
553 /* Allocate private cmb_data. */
554 cmb_data = kzalloc(sizeof(struct cmb_data), GFP_KERNEL);
558 cmb_data->last_block = kzalloc(sizeof(struct cmb), GFP_KERNEL);
559 if (!cmb_data->last_block) {
563 cmb_data->size = sizeof(struct cmb);
564 spin_lock(&cmb_area.lock);
567 /* there is no user yet, so we need a new area */
568 size = sizeof(struct cmb) * cmb_area.num_channels;
569 WARN_ON(!list_empty(&cmb_area.list));
571 spin_unlock(&cmb_area.lock);
572 mem = (void*)__get_free_pages(GFP_KERNEL | GFP_DMA,
574 spin_lock(&cmb_area.lock);
577 /* ok, another thread was faster */
578 free_pages((unsigned long)mem, get_order(size));
585 memset(mem, 0, size);
587 cmf_activate(cmb_area.mem, 1);
591 /* do the actual allocation */
592 ret = alloc_cmb_single(cdev, cmb_data);
594 spin_unlock(&cmb_area.lock);
596 kfree(cmb_data->last_block);
602 static void free_cmb(struct ccw_device *cdev)
604 struct ccw_device_private *priv;
605 struct cmb_data *cmb_data;
607 spin_lock(&cmb_area.lock);
608 spin_lock_irq(cdev->ccwlock);
610 priv = cdev->private;
612 if (list_empty(&priv->cmb_list)) {
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, 0);
628 free_pages((unsigned long)cmb_area.mem, get_order(size));
632 spin_unlock_irq(cdev->ccwlock);
633 spin_unlock(&cmb_area.lock);
636 static int set_cmb(struct ccw_device *cdev, u32 mme)
639 struct cmb_data *cmb_data;
642 spin_lock_irqsave(cdev->ccwlock, flags);
643 if (!cdev->private->cmb) {
644 spin_unlock_irqrestore(cdev->ccwlock, flags);
647 cmb_data = cdev->private->cmb;
648 offset = mme ? (struct cmb *)cmb_data->hw_block - cmb_area.mem : 0;
649 spin_unlock_irqrestore(cdev->ccwlock, flags);
651 return set_schib_wait(cdev, mme, 0, offset);
654 static u64 read_cmb(struct ccw_device *cdev, int index)
661 ret = cmf_cmb_copy_wait(cdev);
665 spin_lock_irqsave(cdev->ccwlock, flags);
666 if (!cdev->private->cmb) {
670 cmb = ((struct cmb_data *)cdev->private->cmb)->last_block;
673 case cmb_ssch_rsch_count:
674 ret = cmb->ssch_rsch_count;
676 case cmb_sample_count:
677 ret = cmb->sample_count;
679 case cmb_device_connect_time:
680 val = cmb->device_connect_time;
682 case cmb_function_pending_time:
683 val = cmb->function_pending_time;
685 case cmb_device_disconnect_time:
686 val = cmb->device_disconnect_time;
688 case cmb_control_unit_queuing_time:
689 val = cmb->control_unit_queuing_time;
691 case cmb_device_active_only_time:
692 val = cmb->device_active_only_time;
698 ret = time_to_avg_nsec(val, cmb->sample_count);
700 spin_unlock_irqrestore(cdev->ccwlock, flags);
704 static int readall_cmb(struct ccw_device *cdev, struct cmbdata *data)
707 struct cmb_data *cmb_data;
712 ret = cmf_cmb_copy_wait(cdev);
715 spin_lock_irqsave(cdev->ccwlock, flags);
716 cmb_data = cdev->private->cmb;
721 if (cmb_data->last_update == 0) {
725 cmb = cmb_data->last_block;
726 time = cmb_data->last_update - cdev->private->cmb_start_time;
728 memset(data, 0, sizeof(struct cmbdata));
730 /* we only know values before device_busy_time */
731 data->size = offsetof(struct cmbdata, device_busy_time);
733 /* convert to nanoseconds */
734 data->elapsed_time = (time * 1000) >> 12;
736 /* copy data to new structure */
737 data->ssch_rsch_count = cmb->ssch_rsch_count;
738 data->sample_count = cmb->sample_count;
740 /* time fields are converted to nanoseconds while copying */
741 data->device_connect_time = time_to_nsec(cmb->device_connect_time);
742 data->function_pending_time = time_to_nsec(cmb->function_pending_time);
743 data->device_disconnect_time =
744 time_to_nsec(cmb->device_disconnect_time);
745 data->control_unit_queuing_time
746 = time_to_nsec(cmb->control_unit_queuing_time);
747 data->device_active_only_time
748 = time_to_nsec(cmb->device_active_only_time);
751 spin_unlock_irqrestore(cdev->ccwlock, flags);
755 static void reset_cmb(struct ccw_device *cdev)
757 cmf_generic_reset(cdev);
760 static void * align_cmb(void *area)
765 static struct attribute_group cmf_attr_group;
767 static struct cmb_operations cmbops_basic = {
772 .readall = readall_cmb,
775 .attr_group = &cmf_attr_group,
778 /* ******** extended cmb handling ********/
781 * struct cmbe - extended channel measurement block
782 * @ssch_rsch_count: number of ssch and rsch
783 * @sample_count: number of samples
784 * @device_connect_time: time of device connect
785 * @function_pending_time: time of function pending
786 * @device_disconnect_time: time of device disconnect
787 * @control_unit_queuing_time: time of control unit queuing
788 * @device_active_only_time: time of device active only
789 * @device_busy_time: time of device busy
790 * @initial_command_response_time: initial command response time
793 * The measurement block as used by the hardware. May be in any 64 bit physical
795 * The fields are described further in z/Architecture Principles of Operation,
796 * third edition, chapter 17.
801 u32 device_connect_time;
802 u32 function_pending_time;
803 u32 device_disconnect_time;
804 u32 control_unit_queuing_time;
805 u32 device_active_only_time;
806 u32 device_busy_time;
807 u32 initial_command_response_time;
812 * kmalloc only guarantees 8 byte alignment, but we need cmbe
813 * pointers to be naturally aligned. Make sure to allocate
814 * enough space for two cmbes.
816 static inline struct cmbe *cmbe_align(struct cmbe *c)
819 addr = ((unsigned long)c + sizeof (struct cmbe) - sizeof(long)) &
820 ~(sizeof (struct cmbe) - sizeof(long));
821 return (struct cmbe*)addr;
824 static int alloc_cmbe(struct ccw_device *cdev)
827 struct cmb_data *cmb_data;
830 cmbe = kzalloc (sizeof (*cmbe) * 2, GFP_KERNEL);
833 cmb_data = kzalloc(sizeof(struct cmb_data), GFP_KERNEL);
838 cmb_data->last_block = kzalloc(sizeof(struct cmbe), GFP_KERNEL);
839 if (!cmb_data->last_block) {
843 cmb_data->size = sizeof(struct cmbe);
844 spin_lock_irq(cdev->ccwlock);
845 if (cdev->private->cmb) {
846 spin_unlock_irq(cdev->ccwlock);
850 cmb_data->hw_block = cmbe;
851 cdev->private->cmb = cmb_data;
852 spin_unlock_irq(cdev->ccwlock);
854 /* activate global measurement if this is the first channel */
855 spin_lock(&cmb_area.lock);
856 if (list_empty(&cmb_area.list))
857 cmf_activate(NULL, 1);
858 list_add_tail(&cdev->private->cmb_list, &cmb_area.list);
859 spin_unlock(&cmb_area.lock);
864 kfree(cmb_data->last_block);
870 static void free_cmbe(struct ccw_device *cdev)
872 struct cmb_data *cmb_data;
874 spin_lock_irq(cdev->ccwlock);
875 cmb_data = cdev->private->cmb;
876 cdev->private->cmb = NULL;
878 kfree(cmb_data->last_block);
880 spin_unlock_irq(cdev->ccwlock);
882 /* deactivate global measurement if this is the last channel */
883 spin_lock(&cmb_area.lock);
884 list_del_init(&cdev->private->cmb_list);
885 if (list_empty(&cmb_area.list))
886 cmf_activate(NULL, 0);
887 spin_unlock(&cmb_area.lock);
890 static int set_cmbe(struct ccw_device *cdev, u32 mme)
893 struct cmb_data *cmb_data;
896 spin_lock_irqsave(cdev->ccwlock, flags);
897 if (!cdev->private->cmb) {
898 spin_unlock_irqrestore(cdev->ccwlock, flags);
901 cmb_data = cdev->private->cmb;
902 mba = mme ? (unsigned long) cmbe_align(cmb_data->hw_block) : 0;
903 spin_unlock_irqrestore(cdev->ccwlock, flags);
905 return set_schib_wait(cdev, mme, 1, mba);
909 static u64 read_cmbe(struct ccw_device *cdev, int index)
912 struct cmb_data *cmb_data;
917 ret = cmf_cmb_copy_wait(cdev);
921 spin_lock_irqsave(cdev->ccwlock, flags);
922 cmb_data = cdev->private->cmb;
927 cmb = cmb_data->last_block;
930 case cmb_ssch_rsch_count:
931 ret = cmb->ssch_rsch_count;
933 case cmb_sample_count:
934 ret = cmb->sample_count;
936 case cmb_device_connect_time:
937 val = cmb->device_connect_time;
939 case cmb_function_pending_time:
940 val = cmb->function_pending_time;
942 case cmb_device_disconnect_time:
943 val = cmb->device_disconnect_time;
945 case cmb_control_unit_queuing_time:
946 val = cmb->control_unit_queuing_time;
948 case cmb_device_active_only_time:
949 val = cmb->device_active_only_time;
951 case cmb_device_busy_time:
952 val = cmb->device_busy_time;
954 case cmb_initial_command_response_time:
955 val = cmb->initial_command_response_time;
961 ret = time_to_avg_nsec(val, cmb->sample_count);
963 spin_unlock_irqrestore(cdev->ccwlock, flags);
967 static int readall_cmbe(struct ccw_device *cdev, struct cmbdata *data)
970 struct cmb_data *cmb_data;
975 ret = cmf_cmb_copy_wait(cdev);
978 spin_lock_irqsave(cdev->ccwlock, flags);
979 cmb_data = cdev->private->cmb;
984 if (cmb_data->last_update == 0) {
988 time = cmb_data->last_update - cdev->private->cmb_start_time;
990 memset (data, 0, sizeof(struct cmbdata));
992 /* we only know values before device_busy_time */
993 data->size = offsetof(struct cmbdata, device_busy_time);
995 /* conver to nanoseconds */
996 data->elapsed_time = (time * 1000) >> 12;
998 cmb = cmb_data->last_block;
999 /* copy data to new structure */
1000 data->ssch_rsch_count = cmb->ssch_rsch_count;
1001 data->sample_count = cmb->sample_count;
1003 /* time fields are converted to nanoseconds while copying */
1004 data->device_connect_time = time_to_nsec(cmb->device_connect_time);
1005 data->function_pending_time = time_to_nsec(cmb->function_pending_time);
1006 data->device_disconnect_time =
1007 time_to_nsec(cmb->device_disconnect_time);
1008 data->control_unit_queuing_time
1009 = time_to_nsec(cmb->control_unit_queuing_time);
1010 data->device_active_only_time
1011 = time_to_nsec(cmb->device_active_only_time);
1012 data->device_busy_time = time_to_nsec(cmb->device_busy_time);
1013 data->initial_command_response_time
1014 = time_to_nsec(cmb->initial_command_response_time);
1018 spin_unlock_irqrestore(cdev->ccwlock, flags);
1022 static void reset_cmbe(struct ccw_device *cdev)
1024 cmf_generic_reset(cdev);
1027 static void * align_cmbe(void *area)
1029 return cmbe_align(area);
1032 static struct attribute_group cmf_attr_group_ext;
1034 static struct cmb_operations cmbops_extended = {
1035 .alloc = alloc_cmbe,
1039 .readall = readall_cmbe,
1040 .reset = reset_cmbe,
1041 .align = align_cmbe,
1042 .attr_group = &cmf_attr_group_ext,
1045 static ssize_t cmb_show_attr(struct device *dev, char *buf, enum cmb_index idx)
1047 return sprintf(buf, "%lld\n",
1048 (unsigned long long) cmf_read(to_ccwdev(dev), idx));
1051 static ssize_t cmb_show_avg_sample_interval(struct device *dev,
1052 struct device_attribute *attr,
1055 struct ccw_device *cdev;
1057 unsigned long count;
1058 struct cmb_data *cmb_data;
1060 cdev = to_ccwdev(dev);
1061 count = cmf_read(cdev, cmb_sample_count);
1062 spin_lock_irq(cdev->ccwlock);
1063 cmb_data = cdev->private->cmb;
1065 interval = cmb_data->last_update -
1066 cdev->private->cmb_start_time;
1067 interval = (interval * 1000) >> 12;
1071 spin_unlock_irq(cdev->ccwlock);
1072 return sprintf(buf, "%ld\n", interval);
1075 static ssize_t cmb_show_avg_utilization(struct device *dev,
1076 struct device_attribute *attr,
1079 struct cmbdata data;
1084 ret = cmf_readall(to_ccwdev(dev), &data);
1085 if (ret == -EAGAIN || ret == -ENODEV)
1086 /* No data (yet/currently) available to use for calculation. */
1087 return sprintf(buf, "n/a\n");
1091 utilization = data.device_connect_time +
1092 data.function_pending_time +
1093 data.device_disconnect_time;
1095 /* shift to avoid long long division */
1096 while (-1ul < (data.elapsed_time | utilization)) {
1098 data.elapsed_time >>= 8;
1101 /* calculate value in 0.1 percent units */
1102 t = (unsigned long) data.elapsed_time / 1000;
1103 u = (unsigned long) utilization / t;
1105 return sprintf(buf, "%02ld.%01ld%%\n", u/ 10, u - (u/ 10) * 10);
1108 #define cmf_attr(name) \
1109 static ssize_t show_##name(struct device *dev, \
1110 struct device_attribute *attr, char *buf) \
1111 { return cmb_show_attr((dev), buf, cmb_##name); } \
1112 static DEVICE_ATTR(name, 0444, show_##name, NULL);
1114 #define cmf_attr_avg(name) \
1115 static ssize_t show_avg_##name(struct device *dev, \
1116 struct device_attribute *attr, char *buf) \
1117 { return cmb_show_attr((dev), buf, cmb_##name); } \
1118 static DEVICE_ATTR(avg_##name, 0444, show_avg_##name, NULL);
1120 cmf_attr(ssch_rsch_count);
1121 cmf_attr(sample_count);
1122 cmf_attr_avg(device_connect_time);
1123 cmf_attr_avg(function_pending_time);
1124 cmf_attr_avg(device_disconnect_time);
1125 cmf_attr_avg(control_unit_queuing_time);
1126 cmf_attr_avg(device_active_only_time);
1127 cmf_attr_avg(device_busy_time);
1128 cmf_attr_avg(initial_command_response_time);
1130 static DEVICE_ATTR(avg_sample_interval, 0444, cmb_show_avg_sample_interval,
1132 static DEVICE_ATTR(avg_utilization, 0444, cmb_show_avg_utilization, NULL);
1134 static struct attribute *cmf_attributes[] = {
1135 &dev_attr_avg_sample_interval.attr,
1136 &dev_attr_avg_utilization.attr,
1137 &dev_attr_ssch_rsch_count.attr,
1138 &dev_attr_sample_count.attr,
1139 &dev_attr_avg_device_connect_time.attr,
1140 &dev_attr_avg_function_pending_time.attr,
1141 &dev_attr_avg_device_disconnect_time.attr,
1142 &dev_attr_avg_control_unit_queuing_time.attr,
1143 &dev_attr_avg_device_active_only_time.attr,
1147 static struct attribute_group cmf_attr_group = {
1149 .attrs = cmf_attributes,
1152 static struct attribute *cmf_attributes_ext[] = {
1153 &dev_attr_avg_sample_interval.attr,
1154 &dev_attr_avg_utilization.attr,
1155 &dev_attr_ssch_rsch_count.attr,
1156 &dev_attr_sample_count.attr,
1157 &dev_attr_avg_device_connect_time.attr,
1158 &dev_attr_avg_function_pending_time.attr,
1159 &dev_attr_avg_device_disconnect_time.attr,
1160 &dev_attr_avg_control_unit_queuing_time.attr,
1161 &dev_attr_avg_device_active_only_time.attr,
1162 &dev_attr_avg_device_busy_time.attr,
1163 &dev_attr_avg_initial_command_response_time.attr,
1167 static struct attribute_group cmf_attr_group_ext = {
1169 .attrs = cmf_attributes_ext,
1172 static ssize_t cmb_enable_show(struct device *dev,
1173 struct device_attribute *attr,
1176 return sprintf(buf, "%d\n", to_ccwdev(dev)->private->cmb ? 1 : 0);
1179 static ssize_t cmb_enable_store(struct device *dev,
1180 struct device_attribute *attr, const char *buf,
1183 struct ccw_device *cdev;
1187 ret = strict_strtoul(buf, 16, &val);
1191 cdev = to_ccwdev(dev);
1195 ret = disable_cmf(cdev);
1198 ret = enable_cmf(cdev);
1205 DEVICE_ATTR(cmb_enable, 0644, cmb_enable_show, cmb_enable_store);
1208 * enable_cmf() - switch on the channel measurement for a specific device
1209 * @cdev: The ccw device to be enabled
1211 * Returns %0 for success or a negative error value.
1216 int enable_cmf(struct ccw_device *cdev)
1220 ret = cmbops->alloc(cdev);
1221 cmbops->reset(cdev);
1224 ret = cmbops->set(cdev, 2);
1229 ret = sysfs_create_group(&cdev->dev.kobj, cmbops->attr_group);
1232 cmbops->set(cdev, 0); //FIXME: this can fail
1238 * disable_cmf() - switch off the channel measurement for a specific device
1239 * @cdev: The ccw device to be disabled
1241 * Returns %0 for success or a negative error value.
1246 int disable_cmf(struct ccw_device *cdev)
1250 ret = cmbops->set(cdev, 0);
1254 sysfs_remove_group(&cdev->dev.kobj, cmbops->attr_group);
1259 * cmf_read() - read one value from the current channel measurement block
1260 * @cdev: the channel to be read
1261 * @index: the index of the value to be read
1263 * Returns the value read or %0 if the value cannot be read.
1268 u64 cmf_read(struct ccw_device *cdev, int index)
1270 return cmbops->read(cdev, index);
1274 * cmf_readall() - read the current channel measurement block
1275 * @cdev: the channel to be read
1276 * @data: a pointer to a data block that will be filled
1278 * Returns %0 on success, a negative error value otherwise.
1283 int cmf_readall(struct ccw_device *cdev, struct cmbdata *data)
1285 return cmbops->readall(cdev, data);
1288 /* Reenable cmf when a disconnected device becomes available again. */
1289 int cmf_reenable(struct ccw_device *cdev)
1291 cmbops->reset(cdev);
1292 return cmbops->set(cdev, 2);
1295 static int __init init_cmf(void)
1297 char *format_string;
1298 char *detect_string = "parameter";
1301 * If the user did not give a parameter, see if we are running on a
1302 * machine supporting extended measurement blocks, otherwise fall back
1305 if (format == CMF_AUTODETECT) {
1306 if (!css_general_characteristics.ext_mb) {
1309 format = CMF_EXTENDED;
1311 detect_string = "autodetected";
1313 detect_string = "parameter";
1318 format_string = "basic";
1319 cmbops = &cmbops_basic;
1322 format_string = "extended";
1323 cmbops = &cmbops_extended;
1328 pr_info("Channel measurement facility initialized using format "
1329 "%s (mode %s)\n", format_string, detect_string);
1333 module_init(init_cmf);
1336 MODULE_AUTHOR("Arnd Bergmann <arndb@de.ibm.com>");
1337 MODULE_LICENSE("GPL");
1338 MODULE_DESCRIPTION("channel measurement facility base driver\n"
1339 "Copyright 2003 IBM Corporation\n");
1341 EXPORT_SYMBOL_GPL(enable_cmf);
1342 EXPORT_SYMBOL_GPL(disable_cmf);
1343 EXPORT_SYMBOL_GPL(cmf_read);
1344 EXPORT_SYMBOL_GPL(cmf_readall);