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>
48 /* parameter to enable cmf during boot, possible uses are:
49 * "s390cmf" -- enable cmf and allocate 2 MB of ram so measuring can be
50 * used on any subchannel
51 * "s390cmf=<num>" -- enable cmf and allocate enough memory to measure
52 * <num> subchannel, where <num> is an integer
53 * between 1 and 65535, default is 1024
55 #define ARGSTRING "s390cmf"
57 /* indices for READCMB */
59 /* basic and exended format: */
62 cmb_device_connect_time,
63 cmb_function_pending_time,
64 cmb_device_disconnect_time,
65 cmb_control_unit_queuing_time,
66 cmb_device_active_only_time,
67 /* extended format only: */
69 cmb_initial_command_response_time,
73 * enum cmb_format - types of supported measurement block formats
75 * @CMF_BASIC: traditional channel measurement blocks supported
76 * by all machines that we run on
77 * @CMF_EXTENDED: improved format that was introduced with the z990
79 * @CMF_AUTODETECT: default: use extended format when running on a z990
80 * or later machine, otherwise fall back to basic format
88 * format - actual format for all measurement blocks
90 * The format module parameter can be set to a value of 0 (zero)
91 * or 1, indicating basic or extended format as described for
94 static int format = CMF_AUTODETECT;
95 module_param(format, bool, 0444);
98 * struct cmb_operations - functions to use depending on cmb_format
100 * Most of these functions operate on a struct ccw_device. There is only
101 * one instance of struct cmb_operations because the format of the measurement
102 * data is guaranteed to be the same for every ccw_device.
104 * @alloc: allocate memory for a channel measurement block,
105 * either with the help of a special pool or with kmalloc
106 * @free: free memory allocated with @alloc
107 * @set: enable or disable measurement
108 * @readall: read a measurement block in a common format
109 * @reset: clear the data in the associated measurement block and
110 * reset its time stamp
111 * @align: align an allocated block so that the hardware can use it
113 struct cmb_operations {
114 int (*alloc) (struct ccw_device*);
115 void(*free) (struct ccw_device*);
116 int (*set) (struct ccw_device*, u32);
117 u64 (*read) (struct ccw_device*, int);
118 int (*readall)(struct ccw_device*, struct cmbdata *);
119 void (*reset) (struct ccw_device*);
120 void * (*align) (void *);
122 struct attribute_group *attr_group;
124 static struct cmb_operations *cmbops;
127 void *hw_block; /* Pointer to block updated by hardware */
128 void *last_block; /* Last changed block copied from hardware block */
129 int size; /* Size of hw_block and last_block */
130 unsigned long long last_update; /* when last_block was updated */
133 /* our user interface is designed in terms of nanoseconds,
134 * while the hardware measures total times in its own
136 static inline u64 time_to_nsec(u32 value)
138 return ((u64)value) * 128000ull;
142 * Users are usually interested in average times,
143 * not accumulated time.
144 * This also helps us with atomicity problems
145 * when reading sinlge values.
147 static inline u64 time_to_avg_nsec(u32 value, u32 count)
151 /* no samples yet, avoid division by 0 */
155 /* value comes in units of 128 µsec */
156 ret = time_to_nsec(value);
162 /* activate or deactivate the channel monitor. When area is NULL,
163 * the monitor is deactivated. The channel monitor needs to
164 * be active in order to measure subchannels, which also need
167 cmf_activate(void *area, unsigned int onoff)
169 register void * __gpr2 asm("2");
170 register long __gpr1 asm("1");
173 __gpr1 = onoff ? 2 : 0;
174 /* activate channel measurement */
175 asm("schm" : : "d" (__gpr2), "d" (__gpr1) );
179 set_schib(struct ccw_device *cdev, u32 mme, int mbfc, unsigned long address)
183 struct subchannel *sch;
186 sch = to_subchannel(cdev->dev.parent);
188 /* msch can silently fail, so do it again if necessary */
189 for (retry = 0; retry < 3; retry++) {
191 stsch(sch->schid, schib);
192 schib->pmcw.mme = mme;
193 schib->pmcw.mbfc = mbfc;
194 /* address can be either a block address or a block index */
196 schib->mba = address;
198 schib->pmcw.mbi = address;
200 /* try to submit it */
201 switch(ret = msch_err(sch->schid, schib)) {
205 case 2: /* in I/O or status pending */
208 case 3: /* subchannel is no longer valid */
211 default: /* msch caught an exception */
215 stsch(sch->schid, schib); /* restore the schib */
220 /* check if it worked */
221 if (schib->pmcw.mme == mme &&
222 schib->pmcw.mbfc == mbfc &&
223 (mbfc ? (schib->mba == address)
224 : (schib->pmcw.mbi == address)))
233 struct set_schib_struct {
236 unsigned long address;
237 wait_queue_head_t wait;
242 static void cmf_set_schib_release(struct kref *kref)
244 struct set_schib_struct *set_data;
246 set_data = container_of(kref, struct set_schib_struct, kref);
250 #define CMF_PENDING 1
252 static int set_schib_wait(struct ccw_device *cdev, u32 mme,
253 int mbfc, unsigned long address)
255 struct set_schib_struct *set_data;
258 spin_lock_irq(cdev->ccwlock);
259 if (!cdev->private->cmb) {
263 set_data = kzalloc(sizeof(struct set_schib_struct), GFP_ATOMIC);
268 init_waitqueue_head(&set_data->wait);
269 kref_init(&set_data->kref);
271 set_data->mbfc = mbfc;
272 set_data->address = address;
274 ret = set_schib(cdev, mme, mbfc, address);
278 if (cdev->private->state != DEV_STATE_ONLINE) {
279 /* if the device is not online, don't even try again */
284 cdev->private->state = DEV_STATE_CMFCHANGE;
285 set_data->ret = CMF_PENDING;
286 cdev->private->cmb_wait = set_data;
288 spin_unlock_irq(cdev->ccwlock);
289 if (wait_event_interruptible(set_data->wait,
290 set_data->ret != CMF_PENDING)) {
291 spin_lock_irq(cdev->ccwlock);
292 if (set_data->ret == CMF_PENDING) {
293 set_data->ret = -ERESTARTSYS;
294 if (cdev->private->state == DEV_STATE_CMFCHANGE)
295 cdev->private->state = DEV_STATE_ONLINE;
297 spin_unlock_irq(cdev->ccwlock);
299 spin_lock_irq(cdev->ccwlock);
300 cdev->private->cmb_wait = NULL;
303 kref_put(&set_data->kref, cmf_set_schib_release);
305 spin_unlock_irq(cdev->ccwlock);
309 void retry_set_schib(struct ccw_device *cdev)
311 struct set_schib_struct *set_data;
313 set_data = cdev->private->cmb_wait;
318 kref_get(&set_data->kref);
319 set_data->ret = set_schib(cdev, set_data->mme, set_data->mbfc,
321 wake_up(&set_data->wait);
322 kref_put(&set_data->kref, cmf_set_schib_release);
325 static int cmf_copy_block(struct ccw_device *cdev)
327 struct subchannel *sch;
330 struct cmb_data *cmb_data;
332 sch = to_subchannel(cdev->dev.parent);
334 if (stsch(sch->schid, &sch->schib))
337 if (sch->schib.scsw.fctl & SCSW_FCTL_START_FUNC) {
338 /* Don't copy if a start function is in progress. */
339 if ((!sch->schib.scsw.actl & SCSW_ACTL_SUSPENDED) &&
340 (sch->schib.scsw.actl &
341 (SCSW_ACTL_DEVACT | SCSW_ACTL_SCHACT)) &&
342 (!sch->schib.scsw.stctl & SCSW_STCTL_SEC_STATUS))
345 cmb_data = cdev->private->cmb;
346 hw_block = cmbops->align(cmb_data->hw_block);
347 if (!memcmp(cmb_data->last_block, hw_block, cmb_data->size))
348 /* No need to copy. */
350 reference_buf = kzalloc(cmb_data->size, GFP_ATOMIC);
353 /* Ensure consistency of block copied from hardware. */
355 memcpy(cmb_data->last_block, hw_block, cmb_data->size);
356 memcpy(reference_buf, hw_block, cmb_data->size);
357 } while (memcmp(cmb_data->last_block, reference_buf, cmb_data->size));
358 cmb_data->last_update = get_clock();
359 kfree(reference_buf);
363 struct copy_block_struct {
364 wait_queue_head_t wait;
369 static void cmf_copy_block_release(struct kref *kref)
371 struct copy_block_struct *copy_block;
373 copy_block = container_of(kref, struct copy_block_struct, kref);
377 static int cmf_cmb_copy_wait(struct ccw_device *cdev)
379 struct copy_block_struct *copy_block;
383 spin_lock_irqsave(cdev->ccwlock, flags);
384 if (!cdev->private->cmb) {
388 copy_block = kzalloc(sizeof(struct copy_block_struct), GFP_ATOMIC);
393 init_waitqueue_head(©_block->wait);
394 kref_init(©_block->kref);
396 ret = cmf_copy_block(cdev);
400 if (cdev->private->state != DEV_STATE_ONLINE) {
405 cdev->private->state = DEV_STATE_CMFUPDATE;
406 copy_block->ret = CMF_PENDING;
407 cdev->private->cmb_wait = copy_block;
409 spin_unlock_irqrestore(cdev->ccwlock, flags);
410 if (wait_event_interruptible(copy_block->wait,
411 copy_block->ret != CMF_PENDING)) {
412 spin_lock_irqsave(cdev->ccwlock, flags);
413 if (copy_block->ret == CMF_PENDING) {
414 copy_block->ret = -ERESTARTSYS;
415 if (cdev->private->state == DEV_STATE_CMFUPDATE)
416 cdev->private->state = DEV_STATE_ONLINE;
418 spin_unlock_irqrestore(cdev->ccwlock, flags);
420 spin_lock_irqsave(cdev->ccwlock, flags);
421 cdev->private->cmb_wait = NULL;
422 ret = copy_block->ret;
424 kref_put(©_block->kref, cmf_copy_block_release);
426 spin_unlock_irqrestore(cdev->ccwlock, flags);
430 void cmf_retry_copy_block(struct ccw_device *cdev)
432 struct copy_block_struct *copy_block;
434 copy_block = cdev->private->cmb_wait;
439 kref_get(©_block->kref);
440 copy_block->ret = cmf_copy_block(cdev);
441 wake_up(©_block->wait);
442 kref_put(©_block->kref, cmf_copy_block_release);
445 static void cmf_generic_reset(struct ccw_device *cdev)
447 struct cmb_data *cmb_data;
449 spin_lock_irq(cdev->ccwlock);
450 cmb_data = cdev->private->cmb;
452 memset(cmb_data->last_block, 0, cmb_data->size);
454 * Need to reset hw block as well to make the hardware start
457 memset(cmbops->align(cmb_data->hw_block), 0, cmb_data->size);
458 cmb_data->last_update = 0;
460 cdev->private->cmb_start_time = get_clock();
461 spin_unlock_irq(cdev->ccwlock);
465 * struct cmb_area - container for global cmb data
467 * @mem: pointer to CMBs (only in basic measurement mode)
468 * @list: contains a linked list of all subchannels
469 * @lock: protect concurrent access to @mem and @list
473 struct list_head list;
478 static struct cmb_area cmb_area = {
479 .lock = __SPIN_LOCK_UNLOCKED(cmb_area.lock),
480 .list = LIST_HEAD_INIT(cmb_area.list),
481 .num_channels = 1024,
485 /* ****** old style CMB handling ********/
489 * Basic channel measurement blocks are allocated in one contiguous
490 * block of memory, which can not be moved as long as any channel
491 * is active. Therefore, a maximum number of subchannels needs to
492 * be defined somewhere. This is a module parameter, defaulting to
493 * a resonable value of 1024, or 32 kb of memory.
494 * Current kernels don't allow kmalloc with more than 128kb, so the
498 module_param_named(maxchannels, cmb_area.num_channels, uint, 0444);
501 * struct cmb - basic channel measurement block
503 * cmb as used by the hardware the fields are described in z/Architecture
504 * Principles of Operation, chapter 17.
505 * The area to be a contiguous array and may not be reallocated or freed.
506 * Only one cmb area can be present in the system.
511 u32 device_connect_time;
512 u32 function_pending_time;
513 u32 device_disconnect_time;
514 u32 control_unit_queuing_time;
515 u32 device_active_only_time;
519 /* insert a single device into the cmb_area list
520 * called with cmb_area.lock held from alloc_cmb
522 static int alloc_cmb_single(struct ccw_device *cdev,
523 struct cmb_data *cmb_data)
526 struct ccw_device_private *node;
529 spin_lock_irq(cdev->ccwlock);
530 if (!list_empty(&cdev->private->cmb_list)) {
535 /* find first unused cmb in cmb_area.mem.
536 * this is a little tricky: cmb_area.list
537 * remains sorted by ->cmb->hw_data pointers */
539 list_for_each_entry(node, &cmb_area.list, cmb_list) {
540 struct cmb_data *data;
542 if ((struct cmb*)data->hw_block > cmb)
546 if (cmb - cmb_area.mem >= cmb_area.num_channels) {
552 list_add_tail(&cdev->private->cmb_list, &node->cmb_list);
553 cmb_data->hw_block = cmb;
554 cdev->private->cmb = cmb_data;
557 spin_unlock_irq(cdev->ccwlock);
562 alloc_cmb (struct ccw_device *cdev)
567 struct cmb_data *cmb_data;
569 /* Allocate private cmb_data. */
570 cmb_data = kzalloc(sizeof(struct cmb_data), GFP_KERNEL);
574 cmb_data->last_block = kzalloc(sizeof(struct cmb), GFP_KERNEL);
575 if (!cmb_data->last_block) {
579 cmb_data->size = sizeof(struct cmb);
580 spin_lock(&cmb_area.lock);
583 /* there is no user yet, so we need a new area */
584 size = sizeof(struct cmb) * cmb_area.num_channels;
585 WARN_ON(!list_empty(&cmb_area.list));
587 spin_unlock(&cmb_area.lock);
588 mem = (void*)__get_free_pages(GFP_KERNEL | GFP_DMA,
590 spin_lock(&cmb_area.lock);
593 /* ok, another thread was faster */
594 free_pages((unsigned long)mem, get_order(size));
597 printk(KERN_WARNING "cio: failed to allocate area "
598 "for measuring %d subchannels\n",
599 cmb_area.num_channels);
604 memset(mem, 0, size);
606 cmf_activate(cmb_area.mem, 1);
610 /* do the actual allocation */
611 ret = alloc_cmb_single(cdev, cmb_data);
613 spin_unlock(&cmb_area.lock);
615 kfree(cmb_data->last_block);
621 static void free_cmb(struct ccw_device *cdev)
623 struct ccw_device_private *priv;
624 struct cmb_data *cmb_data;
626 spin_lock(&cmb_area.lock);
627 spin_lock_irq(cdev->ccwlock);
629 priv = cdev->private;
631 if (list_empty(&priv->cmb_list)) {
636 cmb_data = priv->cmb;
639 kfree(cmb_data->last_block);
641 list_del_init(&priv->cmb_list);
643 if (list_empty(&cmb_area.list)) {
645 size = sizeof(struct cmb) * cmb_area.num_channels;
646 cmf_activate(NULL, 0);
647 free_pages((unsigned long)cmb_area.mem, get_order(size));
651 spin_unlock_irq(cdev->ccwlock);
652 spin_unlock(&cmb_area.lock);
655 static int set_cmb(struct ccw_device *cdev, u32 mme)
658 struct cmb_data *cmb_data;
661 spin_lock_irqsave(cdev->ccwlock, flags);
662 if (!cdev->private->cmb) {
663 spin_unlock_irqrestore(cdev->ccwlock, flags);
666 cmb_data = cdev->private->cmb;
667 offset = mme ? (struct cmb *)cmb_data->hw_block - cmb_area.mem : 0;
668 spin_unlock_irqrestore(cdev->ccwlock, flags);
670 return set_schib_wait(cdev, mme, 0, offset);
673 static u64 read_cmb (struct ccw_device *cdev, int index)
680 ret = cmf_cmb_copy_wait(cdev);
684 spin_lock_irqsave(cdev->ccwlock, flags);
685 if (!cdev->private->cmb) {
689 cmb = ((struct cmb_data *)cdev->private->cmb)->last_block;
692 case cmb_ssch_rsch_count:
693 ret = cmb->ssch_rsch_count;
695 case cmb_sample_count:
696 ret = cmb->sample_count;
698 case cmb_device_connect_time:
699 val = cmb->device_connect_time;
701 case cmb_function_pending_time:
702 val = cmb->function_pending_time;
704 case cmb_device_disconnect_time:
705 val = cmb->device_disconnect_time;
707 case cmb_control_unit_queuing_time:
708 val = cmb->control_unit_queuing_time;
710 case cmb_device_active_only_time:
711 val = cmb->device_active_only_time;
717 ret = time_to_avg_nsec(val, cmb->sample_count);
719 spin_unlock_irqrestore(cdev->ccwlock, flags);
723 static int readall_cmb (struct ccw_device *cdev, struct cmbdata *data)
726 struct cmb_data *cmb_data;
731 ret = cmf_cmb_copy_wait(cdev);
734 spin_lock_irqsave(cdev->ccwlock, flags);
735 cmb_data = cdev->private->cmb;
740 if (cmb_data->last_update == 0) {
744 cmb = cmb_data->last_block;
745 time = cmb_data->last_update - cdev->private->cmb_start_time;
747 memset(data, 0, sizeof(struct cmbdata));
749 /* we only know values before device_busy_time */
750 data->size = offsetof(struct cmbdata, device_busy_time);
752 /* convert to nanoseconds */
753 data->elapsed_time = (time * 1000) >> 12;
755 /* copy data to new structure */
756 data->ssch_rsch_count = cmb->ssch_rsch_count;
757 data->sample_count = cmb->sample_count;
759 /* time fields are converted to nanoseconds while copying */
760 data->device_connect_time = time_to_nsec(cmb->device_connect_time);
761 data->function_pending_time = time_to_nsec(cmb->function_pending_time);
762 data->device_disconnect_time =
763 time_to_nsec(cmb->device_disconnect_time);
764 data->control_unit_queuing_time
765 = time_to_nsec(cmb->control_unit_queuing_time);
766 data->device_active_only_time
767 = time_to_nsec(cmb->device_active_only_time);
770 spin_unlock_irqrestore(cdev->ccwlock, flags);
774 static void reset_cmb(struct ccw_device *cdev)
776 cmf_generic_reset(cdev);
779 static void * align_cmb(void *area)
784 static struct attribute_group cmf_attr_group;
786 static struct cmb_operations cmbops_basic = {
791 .readall = readall_cmb,
794 .attr_group = &cmf_attr_group,
797 /* ******** extended cmb handling ********/
800 * struct cmbe - extended channel measurement block
802 * cmb as used by the hardware, may be in any 64 bit physical location,
803 * the fields are described in z/Architecture Principles of Operation,
804 * third edition, chapter 17.
809 u32 device_connect_time;
810 u32 function_pending_time;
811 u32 device_disconnect_time;
812 u32 control_unit_queuing_time;
813 u32 device_active_only_time;
814 u32 device_busy_time;
815 u32 initial_command_response_time;
819 /* kmalloc only guarantees 8 byte alignment, but we need cmbe
820 * pointers to be naturally aligned. Make sure to allocate
821 * enough space for two cmbes */
822 static inline struct cmbe* cmbe_align(struct cmbe *c)
825 addr = ((unsigned long)c + sizeof (struct cmbe) - sizeof(long)) &
826 ~(sizeof (struct cmbe) - sizeof(long));
827 return (struct cmbe*)addr;
830 static int alloc_cmbe (struct ccw_device *cdev)
833 struct cmb_data *cmb_data;
836 cmbe = kzalloc (sizeof (*cmbe) * 2, GFP_KERNEL);
839 cmb_data = kzalloc(sizeof(struct cmb_data), GFP_KERNEL);
844 cmb_data->last_block = kzalloc(sizeof(struct cmbe), GFP_KERNEL);
845 if (!cmb_data->last_block) {
849 cmb_data->size = sizeof(struct cmbe);
850 spin_lock_irq(cdev->ccwlock);
851 if (cdev->private->cmb) {
852 spin_unlock_irq(cdev->ccwlock);
856 cmb_data->hw_block = cmbe;
857 cdev->private->cmb = cmb_data;
858 spin_unlock_irq(cdev->ccwlock);
860 /* activate global measurement if this is the first channel */
861 spin_lock(&cmb_area.lock);
862 if (list_empty(&cmb_area.list))
863 cmf_activate(NULL, 1);
864 list_add_tail(&cdev->private->cmb_list, &cmb_area.list);
865 spin_unlock(&cmb_area.lock);
870 kfree(cmb_data->last_block);
876 static void free_cmbe (struct ccw_device *cdev)
878 struct cmb_data *cmb_data;
880 spin_lock_irq(cdev->ccwlock);
881 cmb_data = cdev->private->cmb;
882 cdev->private->cmb = NULL;
884 kfree(cmb_data->last_block);
886 spin_unlock_irq(cdev->ccwlock);
888 /* deactivate global measurement if this is the last channel */
889 spin_lock(&cmb_area.lock);
890 list_del_init(&cdev->private->cmb_list);
891 if (list_empty(&cmb_area.list))
892 cmf_activate(NULL, 0);
893 spin_unlock(&cmb_area.lock);
896 static int set_cmbe(struct ccw_device *cdev, u32 mme)
899 struct cmb_data *cmb_data;
902 spin_lock_irqsave(cdev->ccwlock, flags);
903 if (!cdev->private->cmb) {
904 spin_unlock_irqrestore(cdev->ccwlock, flags);
907 cmb_data = cdev->private->cmb;
908 mba = mme ? (unsigned long) cmbe_align(cmb_data->hw_block) : 0;
909 spin_unlock_irqrestore(cdev->ccwlock, flags);
911 return set_schib_wait(cdev, mme, 1, mba);
915 static u64 read_cmbe (struct ccw_device *cdev, int index)
918 struct cmb_data *cmb_data;
923 ret = cmf_cmb_copy_wait(cdev);
927 spin_lock_irqsave(cdev->ccwlock, flags);
928 cmb_data = cdev->private->cmb;
933 cmb = cmb_data->last_block;
936 case cmb_ssch_rsch_count:
937 ret = cmb->ssch_rsch_count;
939 case cmb_sample_count:
940 ret = cmb->sample_count;
942 case cmb_device_connect_time:
943 val = cmb->device_connect_time;
945 case cmb_function_pending_time:
946 val = cmb->function_pending_time;
948 case cmb_device_disconnect_time:
949 val = cmb->device_disconnect_time;
951 case cmb_control_unit_queuing_time:
952 val = cmb->control_unit_queuing_time;
954 case cmb_device_active_only_time:
955 val = cmb->device_active_only_time;
957 case cmb_device_busy_time:
958 val = cmb->device_busy_time;
960 case cmb_initial_command_response_time:
961 val = cmb->initial_command_response_time;
967 ret = time_to_avg_nsec(val, cmb->sample_count);
969 spin_unlock_irqrestore(cdev->ccwlock, flags);
973 static int readall_cmbe (struct ccw_device *cdev, struct cmbdata *data)
976 struct cmb_data *cmb_data;
981 ret = cmf_cmb_copy_wait(cdev);
984 spin_lock_irqsave(cdev->ccwlock, flags);
985 cmb_data = cdev->private->cmb;
990 if (cmb_data->last_update == 0) {
994 time = cmb_data->last_update - cdev->private->cmb_start_time;
996 memset (data, 0, sizeof(struct cmbdata));
998 /* we only know values before device_busy_time */
999 data->size = offsetof(struct cmbdata, device_busy_time);
1001 /* conver to nanoseconds */
1002 data->elapsed_time = (time * 1000) >> 12;
1004 cmb = cmb_data->last_block;
1005 /* copy data to new structure */
1006 data->ssch_rsch_count = cmb->ssch_rsch_count;
1007 data->sample_count = cmb->sample_count;
1009 /* time fields are converted to nanoseconds while copying */
1010 data->device_connect_time = time_to_nsec(cmb->device_connect_time);
1011 data->function_pending_time = time_to_nsec(cmb->function_pending_time);
1012 data->device_disconnect_time =
1013 time_to_nsec(cmb->device_disconnect_time);
1014 data->control_unit_queuing_time
1015 = time_to_nsec(cmb->control_unit_queuing_time);
1016 data->device_active_only_time
1017 = time_to_nsec(cmb->device_active_only_time);
1018 data->device_busy_time = time_to_nsec(cmb->device_busy_time);
1019 data->initial_command_response_time
1020 = time_to_nsec(cmb->initial_command_response_time);
1024 spin_unlock_irqrestore(cdev->ccwlock, flags);
1028 static void reset_cmbe(struct ccw_device *cdev)
1030 cmf_generic_reset(cdev);
1033 static void * align_cmbe(void *area)
1035 return cmbe_align(area);
1038 static struct attribute_group cmf_attr_group_ext;
1040 static struct cmb_operations cmbops_extended = {
1041 .alloc = alloc_cmbe,
1045 .readall = readall_cmbe,
1046 .reset = reset_cmbe,
1047 .align = align_cmbe,
1048 .attr_group = &cmf_attr_group_ext,
1053 cmb_show_attr(struct device *dev, char *buf, enum cmb_index idx)
1055 return sprintf(buf, "%lld\n",
1056 (unsigned long long) cmf_read(to_ccwdev(dev), idx));
1060 cmb_show_avg_sample_interval(struct device *dev, struct device_attribute *attr, char *buf)
1062 struct ccw_device *cdev;
1064 unsigned long count;
1065 struct cmb_data *cmb_data;
1067 cdev = to_ccwdev(dev);
1068 count = cmf_read(cdev, cmb_sample_count);
1069 spin_lock_irq(cdev->ccwlock);
1070 cmb_data = cdev->private->cmb;
1072 interval = cmb_data->last_update -
1073 cdev->private->cmb_start_time;
1074 interval = (interval * 1000) >> 12;
1078 spin_unlock_irq(cdev->ccwlock);
1079 return sprintf(buf, "%ld\n", interval);
1083 cmb_show_avg_utilization(struct device *dev, struct device_attribute *attr, char *buf)
1085 struct cmbdata data;
1090 ret = cmf_readall(to_ccwdev(dev), &data);
1091 if (ret == -EAGAIN || ret == -ENODEV)
1092 /* No data (yet/currently) available to use for calculation. */
1093 return sprintf(buf, "n/a\n");
1097 utilization = data.device_connect_time +
1098 data.function_pending_time +
1099 data.device_disconnect_time;
1101 /* shift to avoid long long division */
1102 while (-1ul < (data.elapsed_time | utilization)) {
1104 data.elapsed_time >>= 8;
1107 /* calculate value in 0.1 percent units */
1108 t = (unsigned long) data.elapsed_time / 1000;
1109 u = (unsigned long) utilization / t;
1111 return sprintf(buf, "%02ld.%01ld%%\n", u/ 10, u - (u/ 10) * 10);
1114 #define cmf_attr(name) \
1115 static ssize_t show_ ## name (struct device * dev, struct device_attribute *attr, char * buf) \
1116 { return cmb_show_attr((dev), buf, cmb_ ## name); } \
1117 static DEVICE_ATTR(name, 0444, show_ ## name, NULL);
1119 #define cmf_attr_avg(name) \
1120 static ssize_t show_avg_ ## name (struct device * dev, struct device_attribute *attr, char * buf) \
1121 { return cmb_show_attr((dev), buf, cmb_ ## name); } \
1122 static DEVICE_ATTR(avg_ ## name, 0444, show_avg_ ## name, NULL);
1124 cmf_attr(ssch_rsch_count);
1125 cmf_attr(sample_count);
1126 cmf_attr_avg(device_connect_time);
1127 cmf_attr_avg(function_pending_time);
1128 cmf_attr_avg(device_disconnect_time);
1129 cmf_attr_avg(control_unit_queuing_time);
1130 cmf_attr_avg(device_active_only_time);
1131 cmf_attr_avg(device_busy_time);
1132 cmf_attr_avg(initial_command_response_time);
1134 static DEVICE_ATTR(avg_sample_interval, 0444, cmb_show_avg_sample_interval, NULL);
1135 static DEVICE_ATTR(avg_utilization, 0444, cmb_show_avg_utilization, NULL);
1137 static struct attribute *cmf_attributes[] = {
1138 &dev_attr_avg_sample_interval.attr,
1139 &dev_attr_avg_utilization.attr,
1140 &dev_attr_ssch_rsch_count.attr,
1141 &dev_attr_sample_count.attr,
1142 &dev_attr_avg_device_connect_time.attr,
1143 &dev_attr_avg_function_pending_time.attr,
1144 &dev_attr_avg_device_disconnect_time.attr,
1145 &dev_attr_avg_control_unit_queuing_time.attr,
1146 &dev_attr_avg_device_active_only_time.attr,
1150 static struct attribute_group cmf_attr_group = {
1152 .attrs = cmf_attributes,
1155 static struct attribute *cmf_attributes_ext[] = {
1156 &dev_attr_avg_sample_interval.attr,
1157 &dev_attr_avg_utilization.attr,
1158 &dev_attr_ssch_rsch_count.attr,
1159 &dev_attr_sample_count.attr,
1160 &dev_attr_avg_device_connect_time.attr,
1161 &dev_attr_avg_function_pending_time.attr,
1162 &dev_attr_avg_device_disconnect_time.attr,
1163 &dev_attr_avg_control_unit_queuing_time.attr,
1164 &dev_attr_avg_device_active_only_time.attr,
1165 &dev_attr_avg_device_busy_time.attr,
1166 &dev_attr_avg_initial_command_response_time.attr,
1170 static struct attribute_group cmf_attr_group_ext = {
1172 .attrs = cmf_attributes_ext,
1175 static ssize_t cmb_enable_show(struct device *dev, struct device_attribute *attr, char *buf)
1177 return sprintf(buf, "%d\n", to_ccwdev(dev)->private->cmb ? 1 : 0);
1180 static ssize_t cmb_enable_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t c)
1182 struct ccw_device *cdev;
1185 cdev = to_ccwdev(dev);
1189 ret = disable_cmf(cdev);
1191 dev_info(&cdev->dev, "disable_cmf failed (%d)\n", ret);
1194 ret = enable_cmf(cdev);
1195 if (ret && ret != -EBUSY)
1196 dev_info(&cdev->dev, "enable_cmf failed (%d)\n", ret);
1203 DEVICE_ATTR(cmb_enable, 0644, cmb_enable_show, cmb_enable_store);
1205 /* enable_cmf/disable_cmf: module interface for cmf (de)activation */
1207 enable_cmf(struct ccw_device *cdev)
1211 ret = cmbops->alloc(cdev);
1212 cmbops->reset(cdev);
1215 ret = cmbops->set(cdev, 2);
1220 ret = sysfs_create_group(&cdev->dev.kobj, cmbops->attr_group);
1223 cmbops->set(cdev, 0); //FIXME: this can fail
1229 disable_cmf(struct ccw_device *cdev)
1233 ret = cmbops->set(cdev, 0);
1237 sysfs_remove_group(&cdev->dev.kobj, cmbops->attr_group);
1242 cmf_read(struct ccw_device *cdev, int index)
1244 return cmbops->read(cdev, index);
1248 cmf_readall(struct ccw_device *cdev, struct cmbdata *data)
1250 return cmbops->readall(cdev, data);
1253 /* Reenable cmf when a disconnected device becomes available again. */
1254 int cmf_reenable(struct ccw_device *cdev)
1256 cmbops->reset(cdev);
1257 return cmbops->set(cdev, 2);
1263 char *format_string;
1264 char *detect_string = "parameter";
1266 /* We cannot really autoprobe this. If the user did not give a parameter,
1267 see if we are running on z990 or up, otherwise fall back to basic mode. */
1269 if (format == CMF_AUTODETECT) {
1270 if (!css_characteristics_avail ||
1271 !css_general_characteristics.ext_mb) {
1274 format = CMF_EXTENDED;
1276 detect_string = "autodetected";
1278 detect_string = "parameter";
1283 format_string = "basic";
1284 cmbops = &cmbops_basic;
1287 format_string = "extended";
1288 cmbops = &cmbops_extended;
1291 printk(KERN_ERR "cio: Invalid format %d for channel "
1292 "measurement facility\n", format);
1296 printk(KERN_INFO "cio: Channel measurement facility using %s "
1297 "format (%s)\n", format_string, detect_string);
1301 module_init(init_cmf);
1304 MODULE_AUTHOR("Arnd Bergmann <arndb@de.ibm.com>");
1305 MODULE_LICENSE("GPL");
1306 MODULE_DESCRIPTION("channel measurement facility base driver\n"
1307 "Copyright 2003 IBM Corporation\n");
1309 EXPORT_SYMBOL_GPL(enable_cmf);
1310 EXPORT_SYMBOL_GPL(disable_cmf);
1311 EXPORT_SYMBOL_GPL(cmf_read);
1312 EXPORT_SYMBOL_GPL(cmf_readall);