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 #include <linux/bootmem.h>
29 #include <linux/device.h>
30 #include <linux/init.h>
31 #include <linux/list.h>
32 #include <linux/module.h>
33 #include <linux/moduleparam.h>
34 #include <linux/slab.h>
35 #include <linux/timex.h> /* get_clock() */
37 #include <asm/ccwdev.h>
40 #include <asm/div64.h>
49 * parameter to enable cmf during boot, possible uses are:
50 * "s390cmf" -- enable cmf and allocate 2 MB of ram so measuring can be
51 * used on any subchannel
52 * "s390cmf=<num>" -- enable cmf and allocate enough memory to measure
53 * <num> subchannel, where <num> is an integer
54 * between 1 and 65535, default is 1024
56 #define ARGSTRING "s390cmf"
58 /* indices for READCMB */
60 /* basic and exended format: */
63 cmb_device_connect_time,
64 cmb_function_pending_time,
65 cmb_device_disconnect_time,
66 cmb_control_unit_queuing_time,
67 cmb_device_active_only_time,
68 /* extended format only: */
70 cmb_initial_command_response_time,
74 * enum cmb_format - types of supported measurement block formats
76 * @CMF_BASIC: traditional channel measurement blocks supported
77 * by all machines that we run on
78 * @CMF_EXTENDED: improved format that was introduced with the z990
80 * @CMF_AUTODETECT: default: use extended format when running on a z990
81 * or later machine, otherwise fall back to basic format
90 * format - actual format for all measurement blocks
92 * The format module parameter can be set to a value of 0 (zero)
93 * or 1, indicating basic or extended format as described for
96 static int format = CMF_AUTODETECT;
97 module_param(format, bool, 0444);
100 * struct cmb_operations - functions to use depending on cmb_format
102 * Most of these functions operate on a struct ccw_device. There is only
103 * one instance of struct cmb_operations because the format of the measurement
104 * data is guaranteed to be the same for every ccw_device.
106 * @alloc: allocate memory for a channel measurement block,
107 * either with the help of a special pool or with kmalloc
108 * @free: free memory allocated with @alloc
109 * @set: enable or disable measurement
110 * @readall: read a measurement block in a common format
111 * @reset: clear the data in the associated measurement block and
112 * reset its time stamp
113 * @align: align an allocated block so that the hardware can use it
115 struct cmb_operations {
116 int (*alloc) (struct ccw_device *);
117 void (*free) (struct ccw_device *);
118 int (*set) (struct ccw_device *, u32);
119 u64 (*read) (struct ccw_device *, int);
120 int (*readall)(struct ccw_device *, struct cmbdata *);
121 void (*reset) (struct ccw_device *);
122 void *(*align) (void *);
124 struct attribute_group *attr_group;
126 static struct cmb_operations *cmbops;
129 void *hw_block; /* Pointer to block updated by hardware */
130 void *last_block; /* Last changed block copied from hardware block */
131 int size; /* Size of hw_block and last_block */
132 unsigned long long last_update; /* when last_block was updated */
136 * Our user interface is designed in terms of nanoseconds,
137 * while the hardware measures total times in its own
140 static inline u64 time_to_nsec(u32 value)
142 return ((u64)value) * 128000ull;
146 * Users are usually interested in average times,
147 * not accumulated time.
148 * This also helps us with atomicity problems
149 * when reading sinlge values.
151 static inline u64 time_to_avg_nsec(u32 value, u32 count)
155 /* no samples yet, avoid division by 0 */
159 /* value comes in units of 128 µsec */
160 ret = time_to_nsec(value);
167 * Activate or deactivate the channel monitor. When area is NULL,
168 * the monitor is deactivated. The channel monitor needs to
169 * be active in order to measure subchannels, which also need
172 static inline void cmf_activate(void *area, unsigned int onoff)
174 register void * __gpr2 asm("2");
175 register long __gpr1 asm("1");
178 __gpr1 = onoff ? 2 : 0;
179 /* activate channel measurement */
180 asm("schm" : : "d" (__gpr2), "d" (__gpr1) );
183 static int set_schib(struct ccw_device *cdev, u32 mme, int mbfc,
184 unsigned long address)
188 struct subchannel *sch;
191 sch = to_subchannel(cdev->dev.parent);
193 /* msch can silently fail, so do it again if necessary */
194 for (retry = 0; retry < 3; retry++) {
196 stsch(sch->schid, schib);
197 schib->pmcw.mme = mme;
198 schib->pmcw.mbfc = mbfc;
199 /* address can be either a block address or a block index */
201 schib->mba = address;
203 schib->pmcw.mbi = address;
205 /* try to submit it */
206 switch(ret = msch_err(sch->schid, schib)) {
210 case 2: /* in I/O or status pending */
213 case 3: /* subchannel is no longer valid */
216 default: /* msch caught an exception */
220 stsch(sch->schid, schib); /* restore the schib */
225 /* check if it worked */
226 if (schib->pmcw.mme == mme &&
227 schib->pmcw.mbfc == mbfc &&
228 (mbfc ? (schib->mba == address)
229 : (schib->pmcw.mbi == address)))
238 struct set_schib_struct {
241 unsigned long address;
242 wait_queue_head_t wait;
247 static void cmf_set_schib_release(struct kref *kref)
249 struct set_schib_struct *set_data;
251 set_data = container_of(kref, struct set_schib_struct, kref);
255 #define CMF_PENDING 1
257 static int set_schib_wait(struct ccw_device *cdev, u32 mme,
258 int mbfc, unsigned long address)
260 struct set_schib_struct *set_data;
263 spin_lock_irq(cdev->ccwlock);
264 if (!cdev->private->cmb) {
268 set_data = kzalloc(sizeof(struct set_schib_struct), GFP_ATOMIC);
273 init_waitqueue_head(&set_data->wait);
274 kref_init(&set_data->kref);
276 set_data->mbfc = mbfc;
277 set_data->address = address;
279 ret = set_schib(cdev, mme, mbfc, address);
283 if (cdev->private->state != DEV_STATE_ONLINE) {
284 /* if the device is not online, don't even try again */
289 cdev->private->state = DEV_STATE_CMFCHANGE;
290 set_data->ret = CMF_PENDING;
291 cdev->private->cmb_wait = set_data;
293 spin_unlock_irq(cdev->ccwlock);
294 if (wait_event_interruptible(set_data->wait,
295 set_data->ret != CMF_PENDING)) {
296 spin_lock_irq(cdev->ccwlock);
297 if (set_data->ret == CMF_PENDING) {
298 set_data->ret = -ERESTARTSYS;
299 if (cdev->private->state == DEV_STATE_CMFCHANGE)
300 cdev->private->state = DEV_STATE_ONLINE;
302 spin_unlock_irq(cdev->ccwlock);
304 spin_lock_irq(cdev->ccwlock);
305 cdev->private->cmb_wait = NULL;
308 kref_put(&set_data->kref, cmf_set_schib_release);
310 spin_unlock_irq(cdev->ccwlock);
314 void retry_set_schib(struct ccw_device *cdev)
316 struct set_schib_struct *set_data;
318 set_data = cdev->private->cmb_wait;
323 kref_get(&set_data->kref);
324 set_data->ret = set_schib(cdev, set_data->mme, set_data->mbfc,
326 wake_up(&set_data->wait);
327 kref_put(&set_data->kref, cmf_set_schib_release);
330 static int cmf_copy_block(struct ccw_device *cdev)
332 struct subchannel *sch;
335 struct cmb_data *cmb_data;
337 sch = to_subchannel(cdev->dev.parent);
339 if (stsch(sch->schid, &sch->schib))
342 if (sch->schib.scsw.fctl & SCSW_FCTL_START_FUNC) {
343 /* Don't copy if a start function is in progress. */
344 if ((!sch->schib.scsw.actl & SCSW_ACTL_SUSPENDED) &&
345 (sch->schib.scsw.actl &
346 (SCSW_ACTL_DEVACT | SCSW_ACTL_SCHACT)) &&
347 (!sch->schib.scsw.stctl & SCSW_STCTL_SEC_STATUS))
350 cmb_data = cdev->private->cmb;
351 hw_block = cmbops->align(cmb_data->hw_block);
352 if (!memcmp(cmb_data->last_block, hw_block, cmb_data->size))
353 /* No need to copy. */
355 reference_buf = kzalloc(cmb_data->size, GFP_ATOMIC);
358 /* Ensure consistency of block copied from hardware. */
360 memcpy(cmb_data->last_block, hw_block, cmb_data->size);
361 memcpy(reference_buf, hw_block, cmb_data->size);
362 } while (memcmp(cmb_data->last_block, reference_buf, cmb_data->size));
363 cmb_data->last_update = get_clock();
364 kfree(reference_buf);
368 struct copy_block_struct {
369 wait_queue_head_t wait;
374 static void cmf_copy_block_release(struct kref *kref)
376 struct copy_block_struct *copy_block;
378 copy_block = container_of(kref, struct copy_block_struct, kref);
382 static int cmf_cmb_copy_wait(struct ccw_device *cdev)
384 struct copy_block_struct *copy_block;
388 spin_lock_irqsave(cdev->ccwlock, flags);
389 if (!cdev->private->cmb) {
393 copy_block = kzalloc(sizeof(struct copy_block_struct), GFP_ATOMIC);
398 init_waitqueue_head(©_block->wait);
399 kref_init(©_block->kref);
401 ret = cmf_copy_block(cdev);
405 if (cdev->private->state != DEV_STATE_ONLINE) {
410 cdev->private->state = DEV_STATE_CMFUPDATE;
411 copy_block->ret = CMF_PENDING;
412 cdev->private->cmb_wait = copy_block;
414 spin_unlock_irqrestore(cdev->ccwlock, flags);
415 if (wait_event_interruptible(copy_block->wait,
416 copy_block->ret != CMF_PENDING)) {
417 spin_lock_irqsave(cdev->ccwlock, flags);
418 if (copy_block->ret == CMF_PENDING) {
419 copy_block->ret = -ERESTARTSYS;
420 if (cdev->private->state == DEV_STATE_CMFUPDATE)
421 cdev->private->state = DEV_STATE_ONLINE;
423 spin_unlock_irqrestore(cdev->ccwlock, flags);
425 spin_lock_irqsave(cdev->ccwlock, flags);
426 cdev->private->cmb_wait = NULL;
427 ret = copy_block->ret;
429 kref_put(©_block->kref, cmf_copy_block_release);
431 spin_unlock_irqrestore(cdev->ccwlock, flags);
435 void cmf_retry_copy_block(struct ccw_device *cdev)
437 struct copy_block_struct *copy_block;
439 copy_block = cdev->private->cmb_wait;
444 kref_get(©_block->kref);
445 copy_block->ret = cmf_copy_block(cdev);
446 wake_up(©_block->wait);
447 kref_put(©_block->kref, cmf_copy_block_release);
450 static void cmf_generic_reset(struct ccw_device *cdev)
452 struct cmb_data *cmb_data;
454 spin_lock_irq(cdev->ccwlock);
455 cmb_data = cdev->private->cmb;
457 memset(cmb_data->last_block, 0, cmb_data->size);
459 * Need to reset hw block as well to make the hardware start
462 memset(cmbops->align(cmb_data->hw_block), 0, cmb_data->size);
463 cmb_data->last_update = 0;
465 cdev->private->cmb_start_time = get_clock();
466 spin_unlock_irq(cdev->ccwlock);
470 * struct cmb_area - container for global cmb data
472 * @mem: pointer to CMBs (only in basic measurement mode)
473 * @list: contains a linked list of all subchannels
474 * @lock: protect concurrent access to @mem and @list
478 struct list_head list;
483 static struct cmb_area cmb_area = {
484 .lock = __SPIN_LOCK_UNLOCKED(cmb_area.lock),
485 .list = LIST_HEAD_INIT(cmb_area.list),
486 .num_channels = 1024,
490 /* ****** old style CMB handling ********/
493 * Basic channel measurement blocks are allocated in one contiguous
494 * block of memory, which can not be moved as long as any channel
495 * is active. Therefore, a maximum number of subchannels needs to
496 * be defined somewhere. This is a module parameter, defaulting to
497 * a resonable value of 1024, or 32 kb of memory.
498 * Current kernels don't allow kmalloc with more than 128kb, so the
502 module_param_named(maxchannels, cmb_area.num_channels, uint, 0444);
505 * struct cmb - basic channel measurement block
507 * cmb as used by the hardware the fields are described in z/Architecture
508 * Principles of Operation, chapter 17.
509 * The area to be a contiguous array and may not be reallocated or freed.
510 * Only one cmb area can be present in the system.
515 u32 device_connect_time;
516 u32 function_pending_time;
517 u32 device_disconnect_time;
518 u32 control_unit_queuing_time;
519 u32 device_active_only_time;
524 * Insert a single device into the cmb_area list.
525 * Called with cmb_area.lock held from alloc_cmb.
527 static int alloc_cmb_single(struct ccw_device *cdev,
528 struct cmb_data *cmb_data)
531 struct ccw_device_private *node;
534 spin_lock_irq(cdev->ccwlock);
535 if (!list_empty(&cdev->private->cmb_list)) {
541 * Find first unused cmb in cmb_area.mem.
542 * This is a little tricky: cmb_area.list
543 * remains sorted by ->cmb->hw_data pointers.
546 list_for_each_entry(node, &cmb_area.list, cmb_list) {
547 struct cmb_data *data;
549 if ((struct cmb*)data->hw_block > cmb)
553 if (cmb - cmb_area.mem >= cmb_area.num_channels) {
559 list_add_tail(&cdev->private->cmb_list, &node->cmb_list);
560 cmb_data->hw_block = cmb;
561 cdev->private->cmb = cmb_data;
564 spin_unlock_irq(cdev->ccwlock);
568 static int alloc_cmb(struct ccw_device *cdev)
573 struct cmb_data *cmb_data;
575 /* Allocate private cmb_data. */
576 cmb_data = kzalloc(sizeof(struct cmb_data), GFP_KERNEL);
580 cmb_data->last_block = kzalloc(sizeof(struct cmb), GFP_KERNEL);
581 if (!cmb_data->last_block) {
585 cmb_data->size = sizeof(struct cmb);
586 spin_lock(&cmb_area.lock);
589 /* there is no user yet, so we need a new area */
590 size = sizeof(struct cmb) * cmb_area.num_channels;
591 WARN_ON(!list_empty(&cmb_area.list));
593 spin_unlock(&cmb_area.lock);
594 mem = (void*)__get_free_pages(GFP_KERNEL | GFP_DMA,
596 spin_lock(&cmb_area.lock);
599 /* ok, another thread was faster */
600 free_pages((unsigned long)mem, get_order(size));
603 printk(KERN_WARNING "cio: failed to allocate area "
604 "for measuring %d subchannels\n",
605 cmb_area.num_channels);
610 memset(mem, 0, size);
612 cmf_activate(cmb_area.mem, 1);
616 /* do the actual allocation */
617 ret = alloc_cmb_single(cdev, cmb_data);
619 spin_unlock(&cmb_area.lock);
621 kfree(cmb_data->last_block);
627 static void free_cmb(struct ccw_device *cdev)
629 struct ccw_device_private *priv;
630 struct cmb_data *cmb_data;
632 spin_lock(&cmb_area.lock);
633 spin_lock_irq(cdev->ccwlock);
635 priv = cdev->private;
637 if (list_empty(&priv->cmb_list)) {
642 cmb_data = priv->cmb;
645 kfree(cmb_data->last_block);
647 list_del_init(&priv->cmb_list);
649 if (list_empty(&cmb_area.list)) {
651 size = sizeof(struct cmb) * cmb_area.num_channels;
652 cmf_activate(NULL, 0);
653 free_pages((unsigned long)cmb_area.mem, get_order(size));
657 spin_unlock_irq(cdev->ccwlock);
658 spin_unlock(&cmb_area.lock);
661 static int set_cmb(struct ccw_device *cdev, u32 mme)
664 struct cmb_data *cmb_data;
667 spin_lock_irqsave(cdev->ccwlock, flags);
668 if (!cdev->private->cmb) {
669 spin_unlock_irqrestore(cdev->ccwlock, flags);
672 cmb_data = cdev->private->cmb;
673 offset = mme ? (struct cmb *)cmb_data->hw_block - cmb_area.mem : 0;
674 spin_unlock_irqrestore(cdev->ccwlock, flags);
676 return set_schib_wait(cdev, mme, 0, offset);
679 static u64 read_cmb(struct ccw_device *cdev, int index)
686 ret = cmf_cmb_copy_wait(cdev);
690 spin_lock_irqsave(cdev->ccwlock, flags);
691 if (!cdev->private->cmb) {
695 cmb = ((struct cmb_data *)cdev->private->cmb)->last_block;
698 case cmb_ssch_rsch_count:
699 ret = cmb->ssch_rsch_count;
701 case cmb_sample_count:
702 ret = cmb->sample_count;
704 case cmb_device_connect_time:
705 val = cmb->device_connect_time;
707 case cmb_function_pending_time:
708 val = cmb->function_pending_time;
710 case cmb_device_disconnect_time:
711 val = cmb->device_disconnect_time;
713 case cmb_control_unit_queuing_time:
714 val = cmb->control_unit_queuing_time;
716 case cmb_device_active_only_time:
717 val = cmb->device_active_only_time;
723 ret = time_to_avg_nsec(val, cmb->sample_count);
725 spin_unlock_irqrestore(cdev->ccwlock, flags);
729 static int readall_cmb(struct ccw_device *cdev, struct cmbdata *data)
732 struct cmb_data *cmb_data;
737 ret = cmf_cmb_copy_wait(cdev);
740 spin_lock_irqsave(cdev->ccwlock, flags);
741 cmb_data = cdev->private->cmb;
746 if (cmb_data->last_update == 0) {
750 cmb = cmb_data->last_block;
751 time = cmb_data->last_update - cdev->private->cmb_start_time;
753 memset(data, 0, sizeof(struct cmbdata));
755 /* we only know values before device_busy_time */
756 data->size = offsetof(struct cmbdata, device_busy_time);
758 /* convert to nanoseconds */
759 data->elapsed_time = (time * 1000) >> 12;
761 /* copy data to new structure */
762 data->ssch_rsch_count = cmb->ssch_rsch_count;
763 data->sample_count = cmb->sample_count;
765 /* time fields are converted to nanoseconds while copying */
766 data->device_connect_time = time_to_nsec(cmb->device_connect_time);
767 data->function_pending_time = time_to_nsec(cmb->function_pending_time);
768 data->device_disconnect_time =
769 time_to_nsec(cmb->device_disconnect_time);
770 data->control_unit_queuing_time
771 = time_to_nsec(cmb->control_unit_queuing_time);
772 data->device_active_only_time
773 = time_to_nsec(cmb->device_active_only_time);
776 spin_unlock_irqrestore(cdev->ccwlock, flags);
780 static void reset_cmb(struct ccw_device *cdev)
782 cmf_generic_reset(cdev);
785 static void * align_cmb(void *area)
790 static struct attribute_group cmf_attr_group;
792 static struct cmb_operations cmbops_basic = {
797 .readall = readall_cmb,
800 .attr_group = &cmf_attr_group,
803 /* ******** extended cmb handling ********/
806 * struct cmbe - extended channel measurement block
808 * cmb as used by the hardware, may be in any 64 bit physical location,
809 * the fields are described in z/Architecture Principles of Operation,
810 * third edition, chapter 17.
815 u32 device_connect_time;
816 u32 function_pending_time;
817 u32 device_disconnect_time;
818 u32 control_unit_queuing_time;
819 u32 device_active_only_time;
820 u32 device_busy_time;
821 u32 initial_command_response_time;
826 * kmalloc only guarantees 8 byte alignment, but we need cmbe
827 * pointers to be naturally aligned. Make sure to allocate
828 * enough space for two cmbes.
830 static inline struct cmbe *cmbe_align(struct cmbe *c)
833 addr = ((unsigned long)c + sizeof (struct cmbe) - sizeof(long)) &
834 ~(sizeof (struct cmbe) - sizeof(long));
835 return (struct cmbe*)addr;
838 static int alloc_cmbe(struct ccw_device *cdev)
841 struct cmb_data *cmb_data;
844 cmbe = kzalloc (sizeof (*cmbe) * 2, GFP_KERNEL);
847 cmb_data = kzalloc(sizeof(struct cmb_data), GFP_KERNEL);
852 cmb_data->last_block = kzalloc(sizeof(struct cmbe), GFP_KERNEL);
853 if (!cmb_data->last_block) {
857 cmb_data->size = sizeof(struct cmbe);
858 spin_lock_irq(cdev->ccwlock);
859 if (cdev->private->cmb) {
860 spin_unlock_irq(cdev->ccwlock);
864 cmb_data->hw_block = cmbe;
865 cdev->private->cmb = cmb_data;
866 spin_unlock_irq(cdev->ccwlock);
868 /* activate global measurement if this is the first channel */
869 spin_lock(&cmb_area.lock);
870 if (list_empty(&cmb_area.list))
871 cmf_activate(NULL, 1);
872 list_add_tail(&cdev->private->cmb_list, &cmb_area.list);
873 spin_unlock(&cmb_area.lock);
878 kfree(cmb_data->last_block);
884 static void free_cmbe(struct ccw_device *cdev)
886 struct cmb_data *cmb_data;
888 spin_lock_irq(cdev->ccwlock);
889 cmb_data = cdev->private->cmb;
890 cdev->private->cmb = NULL;
892 kfree(cmb_data->last_block);
894 spin_unlock_irq(cdev->ccwlock);
896 /* deactivate global measurement if this is the last channel */
897 spin_lock(&cmb_area.lock);
898 list_del_init(&cdev->private->cmb_list);
899 if (list_empty(&cmb_area.list))
900 cmf_activate(NULL, 0);
901 spin_unlock(&cmb_area.lock);
904 static int set_cmbe(struct ccw_device *cdev, u32 mme)
907 struct cmb_data *cmb_data;
910 spin_lock_irqsave(cdev->ccwlock, flags);
911 if (!cdev->private->cmb) {
912 spin_unlock_irqrestore(cdev->ccwlock, flags);
915 cmb_data = cdev->private->cmb;
916 mba = mme ? (unsigned long) cmbe_align(cmb_data->hw_block) : 0;
917 spin_unlock_irqrestore(cdev->ccwlock, flags);
919 return set_schib_wait(cdev, mme, 1, mba);
923 static u64 read_cmbe(struct ccw_device *cdev, int index)
926 struct cmb_data *cmb_data;
931 ret = cmf_cmb_copy_wait(cdev);
935 spin_lock_irqsave(cdev->ccwlock, flags);
936 cmb_data = cdev->private->cmb;
941 cmb = cmb_data->last_block;
944 case cmb_ssch_rsch_count:
945 ret = cmb->ssch_rsch_count;
947 case cmb_sample_count:
948 ret = cmb->sample_count;
950 case cmb_device_connect_time:
951 val = cmb->device_connect_time;
953 case cmb_function_pending_time:
954 val = cmb->function_pending_time;
956 case cmb_device_disconnect_time:
957 val = cmb->device_disconnect_time;
959 case cmb_control_unit_queuing_time:
960 val = cmb->control_unit_queuing_time;
962 case cmb_device_active_only_time:
963 val = cmb->device_active_only_time;
965 case cmb_device_busy_time:
966 val = cmb->device_busy_time;
968 case cmb_initial_command_response_time:
969 val = cmb->initial_command_response_time;
975 ret = time_to_avg_nsec(val, cmb->sample_count);
977 spin_unlock_irqrestore(cdev->ccwlock, flags);
981 static int readall_cmbe(struct ccw_device *cdev, struct cmbdata *data)
984 struct cmb_data *cmb_data;
989 ret = cmf_cmb_copy_wait(cdev);
992 spin_lock_irqsave(cdev->ccwlock, flags);
993 cmb_data = cdev->private->cmb;
998 if (cmb_data->last_update == 0) {
1002 time = cmb_data->last_update - cdev->private->cmb_start_time;
1004 memset (data, 0, sizeof(struct cmbdata));
1006 /* we only know values before device_busy_time */
1007 data->size = offsetof(struct cmbdata, device_busy_time);
1009 /* conver to nanoseconds */
1010 data->elapsed_time = (time * 1000) >> 12;
1012 cmb = cmb_data->last_block;
1013 /* copy data to new structure */
1014 data->ssch_rsch_count = cmb->ssch_rsch_count;
1015 data->sample_count = cmb->sample_count;
1017 /* time fields are converted to nanoseconds while copying */
1018 data->device_connect_time = time_to_nsec(cmb->device_connect_time);
1019 data->function_pending_time = time_to_nsec(cmb->function_pending_time);
1020 data->device_disconnect_time =
1021 time_to_nsec(cmb->device_disconnect_time);
1022 data->control_unit_queuing_time
1023 = time_to_nsec(cmb->control_unit_queuing_time);
1024 data->device_active_only_time
1025 = time_to_nsec(cmb->device_active_only_time);
1026 data->device_busy_time = time_to_nsec(cmb->device_busy_time);
1027 data->initial_command_response_time
1028 = time_to_nsec(cmb->initial_command_response_time);
1032 spin_unlock_irqrestore(cdev->ccwlock, flags);
1036 static void reset_cmbe(struct ccw_device *cdev)
1038 cmf_generic_reset(cdev);
1041 static void * align_cmbe(void *area)
1043 return cmbe_align(area);
1046 static struct attribute_group cmf_attr_group_ext;
1048 static struct cmb_operations cmbops_extended = {
1049 .alloc = alloc_cmbe,
1053 .readall = readall_cmbe,
1054 .reset = reset_cmbe,
1055 .align = align_cmbe,
1056 .attr_group = &cmf_attr_group_ext,
1060 static ssize_t cmb_show_attr(struct device *dev, char *buf, enum cmb_index idx)
1062 return sprintf(buf, "%lld\n",
1063 (unsigned long long) cmf_read(to_ccwdev(dev), idx));
1066 static ssize_t cmb_show_avg_sample_interval(struct device *dev,
1067 struct device_attribute *attr,
1070 struct ccw_device *cdev;
1072 unsigned long count;
1073 struct cmb_data *cmb_data;
1075 cdev = to_ccwdev(dev);
1076 count = cmf_read(cdev, cmb_sample_count);
1077 spin_lock_irq(cdev->ccwlock);
1078 cmb_data = cdev->private->cmb;
1080 interval = cmb_data->last_update -
1081 cdev->private->cmb_start_time;
1082 interval = (interval * 1000) >> 12;
1086 spin_unlock_irq(cdev->ccwlock);
1087 return sprintf(buf, "%ld\n", interval);
1090 static ssize_t cmb_show_avg_utilization(struct device *dev,
1091 struct device_attribute *attr,
1094 struct cmbdata data;
1099 ret = cmf_readall(to_ccwdev(dev), &data);
1100 if (ret == -EAGAIN || ret == -ENODEV)
1101 /* No data (yet/currently) available to use for calculation. */
1102 return sprintf(buf, "n/a\n");
1106 utilization = data.device_connect_time +
1107 data.function_pending_time +
1108 data.device_disconnect_time;
1110 /* shift to avoid long long division */
1111 while (-1ul < (data.elapsed_time | utilization)) {
1113 data.elapsed_time >>= 8;
1116 /* calculate value in 0.1 percent units */
1117 t = (unsigned long) data.elapsed_time / 1000;
1118 u = (unsigned long) utilization / t;
1120 return sprintf(buf, "%02ld.%01ld%%\n", u/ 10, u - (u/ 10) * 10);
1123 #define cmf_attr(name) \
1124 static ssize_t show_##name(struct device *dev, \
1125 struct device_attribute *attr, char *buf) \
1126 { return cmb_show_attr((dev), buf, cmb_##name); } \
1127 static DEVICE_ATTR(name, 0444, show_##name, NULL);
1129 #define cmf_attr_avg(name) \
1130 static ssize_t show_avg_##name(struct device *dev, \
1131 struct device_attribute *attr, char *buf) \
1132 { return cmb_show_attr((dev), buf, cmb_##name); } \
1133 static DEVICE_ATTR(avg_##name, 0444, show_avg_##name, NULL);
1135 cmf_attr(ssch_rsch_count);
1136 cmf_attr(sample_count);
1137 cmf_attr_avg(device_connect_time);
1138 cmf_attr_avg(function_pending_time);
1139 cmf_attr_avg(device_disconnect_time);
1140 cmf_attr_avg(control_unit_queuing_time);
1141 cmf_attr_avg(device_active_only_time);
1142 cmf_attr_avg(device_busy_time);
1143 cmf_attr_avg(initial_command_response_time);
1145 static DEVICE_ATTR(avg_sample_interval, 0444, cmb_show_avg_sample_interval,
1147 static DEVICE_ATTR(avg_utilization, 0444, cmb_show_avg_utilization, NULL);
1149 static struct attribute *cmf_attributes[] = {
1150 &dev_attr_avg_sample_interval.attr,
1151 &dev_attr_avg_utilization.attr,
1152 &dev_attr_ssch_rsch_count.attr,
1153 &dev_attr_sample_count.attr,
1154 &dev_attr_avg_device_connect_time.attr,
1155 &dev_attr_avg_function_pending_time.attr,
1156 &dev_attr_avg_device_disconnect_time.attr,
1157 &dev_attr_avg_control_unit_queuing_time.attr,
1158 &dev_attr_avg_device_active_only_time.attr,
1162 static struct attribute_group cmf_attr_group = {
1164 .attrs = cmf_attributes,
1167 static struct attribute *cmf_attributes_ext[] = {
1168 &dev_attr_avg_sample_interval.attr,
1169 &dev_attr_avg_utilization.attr,
1170 &dev_attr_ssch_rsch_count.attr,
1171 &dev_attr_sample_count.attr,
1172 &dev_attr_avg_device_connect_time.attr,
1173 &dev_attr_avg_function_pending_time.attr,
1174 &dev_attr_avg_device_disconnect_time.attr,
1175 &dev_attr_avg_control_unit_queuing_time.attr,
1176 &dev_attr_avg_device_active_only_time.attr,
1177 &dev_attr_avg_device_busy_time.attr,
1178 &dev_attr_avg_initial_command_response_time.attr,
1182 static struct attribute_group cmf_attr_group_ext = {
1184 .attrs = cmf_attributes_ext,
1187 static ssize_t cmb_enable_show(struct device *dev,
1188 struct device_attribute *attr,
1191 return sprintf(buf, "%d\n", to_ccwdev(dev)->private->cmb ? 1 : 0);
1194 static ssize_t cmb_enable_store(struct device *dev,
1195 struct device_attribute *attr, const char *buf,
1198 struct ccw_device *cdev;
1201 cdev = to_ccwdev(dev);
1205 ret = disable_cmf(cdev);
1207 dev_info(&cdev->dev, "disable_cmf failed (%d)\n", ret);
1210 ret = enable_cmf(cdev);
1211 if (ret && ret != -EBUSY)
1212 dev_info(&cdev->dev, "enable_cmf failed (%d)\n", ret);
1219 DEVICE_ATTR(cmb_enable, 0644, cmb_enable_show, cmb_enable_store);
1221 /* enable_cmf/disable_cmf: module interface for cmf (de)activation */
1222 int enable_cmf(struct ccw_device *cdev)
1226 ret = cmbops->alloc(cdev);
1227 cmbops->reset(cdev);
1230 ret = cmbops->set(cdev, 2);
1235 ret = sysfs_create_group(&cdev->dev.kobj, cmbops->attr_group);
1238 cmbops->set(cdev, 0); //FIXME: this can fail
1243 int disable_cmf(struct ccw_device *cdev)
1247 ret = cmbops->set(cdev, 0);
1251 sysfs_remove_group(&cdev->dev.kobj, cmbops->attr_group);
1255 u64 cmf_read(struct ccw_device *cdev, int index)
1257 return cmbops->read(cdev, index);
1260 int cmf_readall(struct ccw_device *cdev, struct cmbdata *data)
1262 return cmbops->readall(cdev, data);
1265 /* Reenable cmf when a disconnected device becomes available again. */
1266 int cmf_reenable(struct ccw_device *cdev)
1268 cmbops->reset(cdev);
1269 return cmbops->set(cdev, 2);
1272 static int __init init_cmf(void)
1274 char *format_string;
1275 char *detect_string = "parameter";
1278 * If the user did not give a parameter, see if we are running on a
1279 * machine supporting extended measurement blocks, otherwise fall back
1282 if (format == CMF_AUTODETECT) {
1283 if (!css_characteristics_avail ||
1284 !css_general_characteristics.ext_mb) {
1287 format = CMF_EXTENDED;
1289 detect_string = "autodetected";
1291 detect_string = "parameter";
1296 format_string = "basic";
1297 cmbops = &cmbops_basic;
1300 format_string = "extended";
1301 cmbops = &cmbops_extended;
1304 printk(KERN_ERR "cio: Invalid format %d for channel "
1305 "measurement facility\n", format);
1309 printk(KERN_INFO "cio: Channel measurement facility using %s "
1310 "format (%s)\n", format_string, detect_string);
1314 module_init(init_cmf);
1317 MODULE_AUTHOR("Arnd Bergmann <arndb@de.ibm.com>");
1318 MODULE_LICENSE("GPL");
1319 MODULE_DESCRIPTION("channel measurement facility base driver\n"
1320 "Copyright 2003 IBM Corporation\n");
1322 EXPORT_SYMBOL_GPL(enable_cmf);
1323 EXPORT_SYMBOL_GPL(disable_cmf);
1324 EXPORT_SYMBOL_GPL(cmf_read);
1325 EXPORT_SYMBOL_GPL(cmf_readall);