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 machine
81 * supporting extended format, otherwise fall back to
91 * format - actual format for all measurement blocks
93 * The format module parameter can be set to a value of 0 (zero)
94 * or 1, indicating basic or extended format as described for
97 static int format = CMF_AUTODETECT;
98 module_param(format, bool, 0444);
101 * struct cmb_operations - functions to use depending on cmb_format
103 * Most of these functions operate on a struct ccw_device. There is only
104 * one instance of struct cmb_operations because the format of the measurement
105 * data is guaranteed to be the same for every ccw_device.
107 * @alloc: allocate memory for a channel measurement block,
108 * either with the help of a special pool or with kmalloc
109 * @free: free memory allocated with @alloc
110 * @set: enable or disable measurement
111 * @read: read a measurement entry at an index
112 * @readall: read a measurement block in a common format
113 * @reset: clear the data in the associated measurement block and
114 * reset its time stamp
115 * @align: align an allocated block so that the hardware can use it
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 *);
124 void *(*align) (void *);
126 struct attribute_group *attr_group;
128 static struct cmb_operations *cmbops;
131 void *hw_block; /* Pointer to block updated by hardware */
132 void *last_block; /* Last changed block copied from hardware block */
133 int size; /* Size of hw_block and last_block */
134 unsigned long long last_update; /* when last_block was updated */
138 * Our user interface is designed in terms of nanoseconds,
139 * while the hardware measures total times in its own
142 static inline u64 time_to_nsec(u32 value)
144 return ((u64)value) * 128000ull;
148 * Users are usually interested in average times,
149 * not accumulated time.
150 * This also helps us with atomicity problems
151 * when reading sinlge values.
153 static inline u64 time_to_avg_nsec(u32 value, u32 count)
157 /* no samples yet, avoid division by 0 */
161 /* value comes in units of 128 µsec */
162 ret = time_to_nsec(value);
169 * Activate or deactivate the channel monitor. When area is NULL,
170 * the monitor is deactivated. The channel monitor needs to
171 * be active in order to measure subchannels, which also need
174 static inline void cmf_activate(void *area, unsigned int onoff)
176 register void * __gpr2 asm("2");
177 register long __gpr1 asm("1");
180 __gpr1 = onoff ? 2 : 0;
181 /* activate channel measurement */
182 asm("schm" : : "d" (__gpr2), "d" (__gpr1) );
185 static int set_schib(struct ccw_device *cdev, u32 mme, int mbfc,
186 unsigned long address)
190 struct subchannel *sch;
193 sch = to_subchannel(cdev->dev.parent);
195 /* msch can silently fail, so do it again if necessary */
196 for (retry = 0; retry < 3; retry++) {
198 stsch(sch->schid, schib);
199 schib->pmcw.mme = mme;
200 schib->pmcw.mbfc = mbfc;
201 /* address can be either a block address or a block index */
203 schib->mba = address;
205 schib->pmcw.mbi = address;
207 /* try to submit it */
208 switch(ret = msch_err(sch->schid, schib)) {
212 case 2: /* in I/O or status pending */
215 case 3: /* subchannel is no longer valid */
218 default: /* msch caught an exception */
222 stsch(sch->schid, schib); /* restore the schib */
227 /* check if it worked */
228 if (schib->pmcw.mme == mme &&
229 schib->pmcw.mbfc == mbfc &&
230 (mbfc ? (schib->mba == address)
231 : (schib->pmcw.mbi == address)))
240 struct set_schib_struct {
243 unsigned long address;
244 wait_queue_head_t wait;
249 static void cmf_set_schib_release(struct kref *kref)
251 struct set_schib_struct *set_data;
253 set_data = container_of(kref, struct set_schib_struct, kref);
257 #define CMF_PENDING 1
259 static int set_schib_wait(struct ccw_device *cdev, u32 mme,
260 int mbfc, unsigned long address)
262 struct set_schib_struct *set_data;
265 spin_lock_irq(cdev->ccwlock);
266 if (!cdev->private->cmb) {
270 set_data = kzalloc(sizeof(struct set_schib_struct), GFP_ATOMIC);
275 init_waitqueue_head(&set_data->wait);
276 kref_init(&set_data->kref);
278 set_data->mbfc = mbfc;
279 set_data->address = address;
281 ret = set_schib(cdev, mme, mbfc, address);
285 if (cdev->private->state != DEV_STATE_ONLINE) {
286 /* if the device is not online, don't even try again */
291 cdev->private->state = DEV_STATE_CMFCHANGE;
292 set_data->ret = CMF_PENDING;
293 cdev->private->cmb_wait = set_data;
295 spin_unlock_irq(cdev->ccwlock);
296 if (wait_event_interruptible(set_data->wait,
297 set_data->ret != CMF_PENDING)) {
298 spin_lock_irq(cdev->ccwlock);
299 if (set_data->ret == CMF_PENDING) {
300 set_data->ret = -ERESTARTSYS;
301 if (cdev->private->state == DEV_STATE_CMFCHANGE)
302 cdev->private->state = DEV_STATE_ONLINE;
304 spin_unlock_irq(cdev->ccwlock);
306 spin_lock_irq(cdev->ccwlock);
307 cdev->private->cmb_wait = NULL;
310 kref_put(&set_data->kref, cmf_set_schib_release);
312 spin_unlock_irq(cdev->ccwlock);
316 void retry_set_schib(struct ccw_device *cdev)
318 struct set_schib_struct *set_data;
320 set_data = cdev->private->cmb_wait;
325 kref_get(&set_data->kref);
326 set_data->ret = set_schib(cdev, set_data->mme, set_data->mbfc,
328 wake_up(&set_data->wait);
329 kref_put(&set_data->kref, cmf_set_schib_release);
332 static int cmf_copy_block(struct ccw_device *cdev)
334 struct subchannel *sch;
337 struct cmb_data *cmb_data;
339 sch = to_subchannel(cdev->dev.parent);
341 if (stsch(sch->schid, &sch->schib))
344 if (scsw_fctl(&sch->schib.scsw) & SCSW_FCTL_START_FUNC) {
345 /* Don't copy if a start function is in progress. */
346 if ((!(scsw_actl(&sch->schib.scsw) & SCSW_ACTL_SUSPENDED)) &&
347 (scsw_actl(&sch->schib.scsw) &
348 (SCSW_ACTL_DEVACT | SCSW_ACTL_SCHACT)) &&
349 (!(scsw_stctl(&sch->schib.scsw) & SCSW_STCTL_SEC_STATUS)))
352 cmb_data = cdev->private->cmb;
353 hw_block = cmbops->align(cmb_data->hw_block);
354 if (!memcmp(cmb_data->last_block, hw_block, cmb_data->size))
355 /* No need to copy. */
357 reference_buf = kzalloc(cmb_data->size, GFP_ATOMIC);
360 /* Ensure consistency of block copied from hardware. */
362 memcpy(cmb_data->last_block, hw_block, cmb_data->size);
363 memcpy(reference_buf, hw_block, cmb_data->size);
364 } while (memcmp(cmb_data->last_block, reference_buf, cmb_data->size));
365 cmb_data->last_update = get_clock();
366 kfree(reference_buf);
370 struct copy_block_struct {
371 wait_queue_head_t wait;
376 static void cmf_copy_block_release(struct kref *kref)
378 struct copy_block_struct *copy_block;
380 copy_block = container_of(kref, struct copy_block_struct, kref);
384 static int cmf_cmb_copy_wait(struct ccw_device *cdev)
386 struct copy_block_struct *copy_block;
390 spin_lock_irqsave(cdev->ccwlock, flags);
391 if (!cdev->private->cmb) {
395 copy_block = kzalloc(sizeof(struct copy_block_struct), GFP_ATOMIC);
400 init_waitqueue_head(©_block->wait);
401 kref_init(©_block->kref);
403 ret = cmf_copy_block(cdev);
407 if (cdev->private->state != DEV_STATE_ONLINE) {
412 cdev->private->state = DEV_STATE_CMFUPDATE;
413 copy_block->ret = CMF_PENDING;
414 cdev->private->cmb_wait = copy_block;
416 spin_unlock_irqrestore(cdev->ccwlock, flags);
417 if (wait_event_interruptible(copy_block->wait,
418 copy_block->ret != CMF_PENDING)) {
419 spin_lock_irqsave(cdev->ccwlock, flags);
420 if (copy_block->ret == CMF_PENDING) {
421 copy_block->ret = -ERESTARTSYS;
422 if (cdev->private->state == DEV_STATE_CMFUPDATE)
423 cdev->private->state = DEV_STATE_ONLINE;
425 spin_unlock_irqrestore(cdev->ccwlock, flags);
427 spin_lock_irqsave(cdev->ccwlock, flags);
428 cdev->private->cmb_wait = NULL;
429 ret = copy_block->ret;
431 kref_put(©_block->kref, cmf_copy_block_release);
433 spin_unlock_irqrestore(cdev->ccwlock, flags);
437 void cmf_retry_copy_block(struct ccw_device *cdev)
439 struct copy_block_struct *copy_block;
441 copy_block = cdev->private->cmb_wait;
446 kref_get(©_block->kref);
447 copy_block->ret = cmf_copy_block(cdev);
448 wake_up(©_block->wait);
449 kref_put(©_block->kref, cmf_copy_block_release);
452 static void cmf_generic_reset(struct ccw_device *cdev)
454 struct cmb_data *cmb_data;
456 spin_lock_irq(cdev->ccwlock);
457 cmb_data = cdev->private->cmb;
459 memset(cmb_data->last_block, 0, cmb_data->size);
461 * Need to reset hw block as well to make the hardware start
464 memset(cmbops->align(cmb_data->hw_block), 0, cmb_data->size);
465 cmb_data->last_update = 0;
467 cdev->private->cmb_start_time = get_clock();
468 spin_unlock_irq(cdev->ccwlock);
472 * struct cmb_area - container for global cmb data
474 * @mem: pointer to CMBs (only in basic measurement mode)
475 * @list: contains a linked list of all subchannels
476 * @num_channels: number of channels to be measured
477 * @lock: protect concurrent access to @mem and @list
481 struct list_head list;
486 static struct cmb_area cmb_area = {
487 .lock = __SPIN_LOCK_UNLOCKED(cmb_area.lock),
488 .list = LIST_HEAD_INIT(cmb_area.list),
489 .num_channels = 1024,
492 /* ****** old style CMB handling ********/
495 * Basic channel measurement blocks are allocated in one contiguous
496 * block of memory, which can not be moved as long as any channel
497 * is active. Therefore, a maximum number of subchannels needs to
498 * be defined somewhere. This is a module parameter, defaulting to
499 * a resonable value of 1024, or 32 kb of memory.
500 * Current kernels don't allow kmalloc with more than 128kb, so the
504 module_param_named(maxchannels, cmb_area.num_channels, uint, 0444);
507 * struct cmb - basic channel measurement block
508 * @ssch_rsch_count: number of ssch and rsch
509 * @sample_count: number of samples
510 * @device_connect_time: time of device connect
511 * @function_pending_time: time of function pending
512 * @device_disconnect_time: time of device disconnect
513 * @control_unit_queuing_time: time of control unit queuing
514 * @device_active_only_time: time of device active only
515 * @reserved: unused in basic measurement mode
517 * The measurement block as used by the hardware. The fields are described
518 * further in z/Architecture Principles of Operation, chapter 17.
520 * The cmb area made up from these blocks must be a contiguous array and may
521 * not be reallocated or freed.
522 * Only one cmb area can be present in the system.
527 u32 device_connect_time;
528 u32 function_pending_time;
529 u32 device_disconnect_time;
530 u32 control_unit_queuing_time;
531 u32 device_active_only_time;
536 * Insert a single device into the cmb_area list.
537 * Called with cmb_area.lock held from alloc_cmb.
539 static int alloc_cmb_single(struct ccw_device *cdev,
540 struct cmb_data *cmb_data)
543 struct ccw_device_private *node;
546 spin_lock_irq(cdev->ccwlock);
547 if (!list_empty(&cdev->private->cmb_list)) {
553 * Find first unused cmb in cmb_area.mem.
554 * This is a little tricky: cmb_area.list
555 * remains sorted by ->cmb->hw_data pointers.
558 list_for_each_entry(node, &cmb_area.list, cmb_list) {
559 struct cmb_data *data;
561 if ((struct cmb*)data->hw_block > cmb)
565 if (cmb - cmb_area.mem >= cmb_area.num_channels) {
571 list_add_tail(&cdev->private->cmb_list, &node->cmb_list);
572 cmb_data->hw_block = cmb;
573 cdev->private->cmb = cmb_data;
576 spin_unlock_irq(cdev->ccwlock);
580 static int alloc_cmb(struct ccw_device *cdev)
585 struct cmb_data *cmb_data;
587 /* Allocate private cmb_data. */
588 cmb_data = kzalloc(sizeof(struct cmb_data), GFP_KERNEL);
592 cmb_data->last_block = kzalloc(sizeof(struct cmb), GFP_KERNEL);
593 if (!cmb_data->last_block) {
597 cmb_data->size = sizeof(struct cmb);
598 spin_lock(&cmb_area.lock);
601 /* there is no user yet, so we need a new area */
602 size = sizeof(struct cmb) * cmb_area.num_channels;
603 WARN_ON(!list_empty(&cmb_area.list));
605 spin_unlock(&cmb_area.lock);
606 mem = (void*)__get_free_pages(GFP_KERNEL | GFP_DMA,
608 spin_lock(&cmb_area.lock);
611 /* ok, another thread was faster */
612 free_pages((unsigned long)mem, get_order(size));
619 memset(mem, 0, size);
621 cmf_activate(cmb_area.mem, 1);
625 /* do the actual allocation */
626 ret = alloc_cmb_single(cdev, cmb_data);
628 spin_unlock(&cmb_area.lock);
630 kfree(cmb_data->last_block);
636 static void free_cmb(struct ccw_device *cdev)
638 struct ccw_device_private *priv;
639 struct cmb_data *cmb_data;
641 spin_lock(&cmb_area.lock);
642 spin_lock_irq(cdev->ccwlock);
644 priv = cdev->private;
646 if (list_empty(&priv->cmb_list)) {
651 cmb_data = priv->cmb;
654 kfree(cmb_data->last_block);
656 list_del_init(&priv->cmb_list);
658 if (list_empty(&cmb_area.list)) {
660 size = sizeof(struct cmb) * cmb_area.num_channels;
661 cmf_activate(NULL, 0);
662 free_pages((unsigned long)cmb_area.mem, get_order(size));
666 spin_unlock_irq(cdev->ccwlock);
667 spin_unlock(&cmb_area.lock);
670 static int set_cmb(struct ccw_device *cdev, u32 mme)
673 struct cmb_data *cmb_data;
676 spin_lock_irqsave(cdev->ccwlock, flags);
677 if (!cdev->private->cmb) {
678 spin_unlock_irqrestore(cdev->ccwlock, flags);
681 cmb_data = cdev->private->cmb;
682 offset = mme ? (struct cmb *)cmb_data->hw_block - cmb_area.mem : 0;
683 spin_unlock_irqrestore(cdev->ccwlock, flags);
685 return set_schib_wait(cdev, mme, 0, offset);
688 static u64 read_cmb(struct ccw_device *cdev, int index)
695 ret = cmf_cmb_copy_wait(cdev);
699 spin_lock_irqsave(cdev->ccwlock, flags);
700 if (!cdev->private->cmb) {
704 cmb = ((struct cmb_data *)cdev->private->cmb)->last_block;
707 case cmb_ssch_rsch_count:
708 ret = cmb->ssch_rsch_count;
710 case cmb_sample_count:
711 ret = cmb->sample_count;
713 case cmb_device_connect_time:
714 val = cmb->device_connect_time;
716 case cmb_function_pending_time:
717 val = cmb->function_pending_time;
719 case cmb_device_disconnect_time:
720 val = cmb->device_disconnect_time;
722 case cmb_control_unit_queuing_time:
723 val = cmb->control_unit_queuing_time;
725 case cmb_device_active_only_time:
726 val = cmb->device_active_only_time;
732 ret = time_to_avg_nsec(val, cmb->sample_count);
734 spin_unlock_irqrestore(cdev->ccwlock, flags);
738 static int readall_cmb(struct ccw_device *cdev, struct cmbdata *data)
741 struct cmb_data *cmb_data;
746 ret = cmf_cmb_copy_wait(cdev);
749 spin_lock_irqsave(cdev->ccwlock, flags);
750 cmb_data = cdev->private->cmb;
755 if (cmb_data->last_update == 0) {
759 cmb = cmb_data->last_block;
760 time = cmb_data->last_update - cdev->private->cmb_start_time;
762 memset(data, 0, sizeof(struct cmbdata));
764 /* we only know values before device_busy_time */
765 data->size = offsetof(struct cmbdata, device_busy_time);
767 /* convert to nanoseconds */
768 data->elapsed_time = (time * 1000) >> 12;
770 /* copy data to new structure */
771 data->ssch_rsch_count = cmb->ssch_rsch_count;
772 data->sample_count = cmb->sample_count;
774 /* time fields are converted to nanoseconds while copying */
775 data->device_connect_time = time_to_nsec(cmb->device_connect_time);
776 data->function_pending_time = time_to_nsec(cmb->function_pending_time);
777 data->device_disconnect_time =
778 time_to_nsec(cmb->device_disconnect_time);
779 data->control_unit_queuing_time
780 = time_to_nsec(cmb->control_unit_queuing_time);
781 data->device_active_only_time
782 = time_to_nsec(cmb->device_active_only_time);
785 spin_unlock_irqrestore(cdev->ccwlock, flags);
789 static void reset_cmb(struct ccw_device *cdev)
791 cmf_generic_reset(cdev);
794 static void * align_cmb(void *area)
799 static struct attribute_group cmf_attr_group;
801 static struct cmb_operations cmbops_basic = {
806 .readall = readall_cmb,
809 .attr_group = &cmf_attr_group,
812 /* ******** extended cmb handling ********/
815 * struct cmbe - extended channel measurement block
816 * @ssch_rsch_count: number of ssch and rsch
817 * @sample_count: number of samples
818 * @device_connect_time: time of device connect
819 * @function_pending_time: time of function pending
820 * @device_disconnect_time: time of device disconnect
821 * @control_unit_queuing_time: time of control unit queuing
822 * @device_active_only_time: time of device active only
823 * @device_busy_time: time of device busy
824 * @initial_command_response_time: initial command response time
827 * The measurement block as used by the hardware. May be in any 64 bit physical
829 * The fields are described further in z/Architecture Principles of Operation,
830 * third edition, chapter 17.
835 u32 device_connect_time;
836 u32 function_pending_time;
837 u32 device_disconnect_time;
838 u32 control_unit_queuing_time;
839 u32 device_active_only_time;
840 u32 device_busy_time;
841 u32 initial_command_response_time;
846 * kmalloc only guarantees 8 byte alignment, but we need cmbe
847 * pointers to be naturally aligned. Make sure to allocate
848 * enough space for two cmbes.
850 static inline struct cmbe *cmbe_align(struct cmbe *c)
853 addr = ((unsigned long)c + sizeof (struct cmbe) - sizeof(long)) &
854 ~(sizeof (struct cmbe) - sizeof(long));
855 return (struct cmbe*)addr;
858 static int alloc_cmbe(struct ccw_device *cdev)
861 struct cmb_data *cmb_data;
864 cmbe = kzalloc (sizeof (*cmbe) * 2, GFP_KERNEL);
867 cmb_data = kzalloc(sizeof(struct cmb_data), GFP_KERNEL);
872 cmb_data->last_block = kzalloc(sizeof(struct cmbe), GFP_KERNEL);
873 if (!cmb_data->last_block) {
877 cmb_data->size = sizeof(struct cmbe);
878 spin_lock_irq(cdev->ccwlock);
879 if (cdev->private->cmb) {
880 spin_unlock_irq(cdev->ccwlock);
884 cmb_data->hw_block = cmbe;
885 cdev->private->cmb = cmb_data;
886 spin_unlock_irq(cdev->ccwlock);
888 /* activate global measurement if this is the first channel */
889 spin_lock(&cmb_area.lock);
890 if (list_empty(&cmb_area.list))
891 cmf_activate(NULL, 1);
892 list_add_tail(&cdev->private->cmb_list, &cmb_area.list);
893 spin_unlock(&cmb_area.lock);
898 kfree(cmb_data->last_block);
904 static void free_cmbe(struct ccw_device *cdev)
906 struct cmb_data *cmb_data;
908 spin_lock_irq(cdev->ccwlock);
909 cmb_data = cdev->private->cmb;
910 cdev->private->cmb = NULL;
912 kfree(cmb_data->last_block);
914 spin_unlock_irq(cdev->ccwlock);
916 /* deactivate global measurement if this is the last channel */
917 spin_lock(&cmb_area.lock);
918 list_del_init(&cdev->private->cmb_list);
919 if (list_empty(&cmb_area.list))
920 cmf_activate(NULL, 0);
921 spin_unlock(&cmb_area.lock);
924 static int set_cmbe(struct ccw_device *cdev, u32 mme)
927 struct cmb_data *cmb_data;
930 spin_lock_irqsave(cdev->ccwlock, flags);
931 if (!cdev->private->cmb) {
932 spin_unlock_irqrestore(cdev->ccwlock, flags);
935 cmb_data = cdev->private->cmb;
936 mba = mme ? (unsigned long) cmbe_align(cmb_data->hw_block) : 0;
937 spin_unlock_irqrestore(cdev->ccwlock, flags);
939 return set_schib_wait(cdev, mme, 1, mba);
943 static u64 read_cmbe(struct ccw_device *cdev, int index)
946 struct cmb_data *cmb_data;
951 ret = cmf_cmb_copy_wait(cdev);
955 spin_lock_irqsave(cdev->ccwlock, flags);
956 cmb_data = cdev->private->cmb;
961 cmb = cmb_data->last_block;
964 case cmb_ssch_rsch_count:
965 ret = cmb->ssch_rsch_count;
967 case cmb_sample_count:
968 ret = cmb->sample_count;
970 case cmb_device_connect_time:
971 val = cmb->device_connect_time;
973 case cmb_function_pending_time:
974 val = cmb->function_pending_time;
976 case cmb_device_disconnect_time:
977 val = cmb->device_disconnect_time;
979 case cmb_control_unit_queuing_time:
980 val = cmb->control_unit_queuing_time;
982 case cmb_device_active_only_time:
983 val = cmb->device_active_only_time;
985 case cmb_device_busy_time:
986 val = cmb->device_busy_time;
988 case cmb_initial_command_response_time:
989 val = cmb->initial_command_response_time;
995 ret = time_to_avg_nsec(val, cmb->sample_count);
997 spin_unlock_irqrestore(cdev->ccwlock, flags);
1001 static int readall_cmbe(struct ccw_device *cdev, struct cmbdata *data)
1004 struct cmb_data *cmb_data;
1006 unsigned long flags;
1009 ret = cmf_cmb_copy_wait(cdev);
1012 spin_lock_irqsave(cdev->ccwlock, flags);
1013 cmb_data = cdev->private->cmb;
1018 if (cmb_data->last_update == 0) {
1022 time = cmb_data->last_update - cdev->private->cmb_start_time;
1024 memset (data, 0, sizeof(struct cmbdata));
1026 /* we only know values before device_busy_time */
1027 data->size = offsetof(struct cmbdata, device_busy_time);
1029 /* conver to nanoseconds */
1030 data->elapsed_time = (time * 1000) >> 12;
1032 cmb = cmb_data->last_block;
1033 /* copy data to new structure */
1034 data->ssch_rsch_count = cmb->ssch_rsch_count;
1035 data->sample_count = cmb->sample_count;
1037 /* time fields are converted to nanoseconds while copying */
1038 data->device_connect_time = time_to_nsec(cmb->device_connect_time);
1039 data->function_pending_time = time_to_nsec(cmb->function_pending_time);
1040 data->device_disconnect_time =
1041 time_to_nsec(cmb->device_disconnect_time);
1042 data->control_unit_queuing_time
1043 = time_to_nsec(cmb->control_unit_queuing_time);
1044 data->device_active_only_time
1045 = time_to_nsec(cmb->device_active_only_time);
1046 data->device_busy_time = time_to_nsec(cmb->device_busy_time);
1047 data->initial_command_response_time
1048 = time_to_nsec(cmb->initial_command_response_time);
1052 spin_unlock_irqrestore(cdev->ccwlock, flags);
1056 static void reset_cmbe(struct ccw_device *cdev)
1058 cmf_generic_reset(cdev);
1061 static void * align_cmbe(void *area)
1063 return cmbe_align(area);
1066 static struct attribute_group cmf_attr_group_ext;
1068 static struct cmb_operations cmbops_extended = {
1069 .alloc = alloc_cmbe,
1073 .readall = readall_cmbe,
1074 .reset = reset_cmbe,
1075 .align = align_cmbe,
1076 .attr_group = &cmf_attr_group_ext,
1079 static ssize_t cmb_show_attr(struct device *dev, char *buf, enum cmb_index idx)
1081 return sprintf(buf, "%lld\n",
1082 (unsigned long long) cmf_read(to_ccwdev(dev), idx));
1085 static ssize_t cmb_show_avg_sample_interval(struct device *dev,
1086 struct device_attribute *attr,
1089 struct ccw_device *cdev;
1091 unsigned long count;
1092 struct cmb_data *cmb_data;
1094 cdev = to_ccwdev(dev);
1095 count = cmf_read(cdev, cmb_sample_count);
1096 spin_lock_irq(cdev->ccwlock);
1097 cmb_data = cdev->private->cmb;
1099 interval = cmb_data->last_update -
1100 cdev->private->cmb_start_time;
1101 interval = (interval * 1000) >> 12;
1105 spin_unlock_irq(cdev->ccwlock);
1106 return sprintf(buf, "%ld\n", interval);
1109 static ssize_t cmb_show_avg_utilization(struct device *dev,
1110 struct device_attribute *attr,
1113 struct cmbdata data;
1118 ret = cmf_readall(to_ccwdev(dev), &data);
1119 if (ret == -EAGAIN || ret == -ENODEV)
1120 /* No data (yet/currently) available to use for calculation. */
1121 return sprintf(buf, "n/a\n");
1125 utilization = data.device_connect_time +
1126 data.function_pending_time +
1127 data.device_disconnect_time;
1129 /* shift to avoid long long division */
1130 while (-1ul < (data.elapsed_time | utilization)) {
1132 data.elapsed_time >>= 8;
1135 /* calculate value in 0.1 percent units */
1136 t = (unsigned long) data.elapsed_time / 1000;
1137 u = (unsigned long) utilization / t;
1139 return sprintf(buf, "%02ld.%01ld%%\n", u/ 10, u - (u/ 10) * 10);
1142 #define cmf_attr(name) \
1143 static ssize_t show_##name(struct device *dev, \
1144 struct device_attribute *attr, char *buf) \
1145 { return cmb_show_attr((dev), buf, cmb_##name); } \
1146 static DEVICE_ATTR(name, 0444, show_##name, NULL);
1148 #define cmf_attr_avg(name) \
1149 static ssize_t show_avg_##name(struct device *dev, \
1150 struct device_attribute *attr, char *buf) \
1151 { return cmb_show_attr((dev), buf, cmb_##name); } \
1152 static DEVICE_ATTR(avg_##name, 0444, show_avg_##name, NULL);
1154 cmf_attr(ssch_rsch_count);
1155 cmf_attr(sample_count);
1156 cmf_attr_avg(device_connect_time);
1157 cmf_attr_avg(function_pending_time);
1158 cmf_attr_avg(device_disconnect_time);
1159 cmf_attr_avg(control_unit_queuing_time);
1160 cmf_attr_avg(device_active_only_time);
1161 cmf_attr_avg(device_busy_time);
1162 cmf_attr_avg(initial_command_response_time);
1164 static DEVICE_ATTR(avg_sample_interval, 0444, cmb_show_avg_sample_interval,
1166 static DEVICE_ATTR(avg_utilization, 0444, cmb_show_avg_utilization, NULL);
1168 static struct attribute *cmf_attributes[] = {
1169 &dev_attr_avg_sample_interval.attr,
1170 &dev_attr_avg_utilization.attr,
1171 &dev_attr_ssch_rsch_count.attr,
1172 &dev_attr_sample_count.attr,
1173 &dev_attr_avg_device_connect_time.attr,
1174 &dev_attr_avg_function_pending_time.attr,
1175 &dev_attr_avg_device_disconnect_time.attr,
1176 &dev_attr_avg_control_unit_queuing_time.attr,
1177 &dev_attr_avg_device_active_only_time.attr,
1181 static struct attribute_group cmf_attr_group = {
1183 .attrs = cmf_attributes,
1186 static struct attribute *cmf_attributes_ext[] = {
1187 &dev_attr_avg_sample_interval.attr,
1188 &dev_attr_avg_utilization.attr,
1189 &dev_attr_ssch_rsch_count.attr,
1190 &dev_attr_sample_count.attr,
1191 &dev_attr_avg_device_connect_time.attr,
1192 &dev_attr_avg_function_pending_time.attr,
1193 &dev_attr_avg_device_disconnect_time.attr,
1194 &dev_attr_avg_control_unit_queuing_time.attr,
1195 &dev_attr_avg_device_active_only_time.attr,
1196 &dev_attr_avg_device_busy_time.attr,
1197 &dev_attr_avg_initial_command_response_time.attr,
1201 static struct attribute_group cmf_attr_group_ext = {
1203 .attrs = cmf_attributes_ext,
1206 static ssize_t cmb_enable_show(struct device *dev,
1207 struct device_attribute *attr,
1210 return sprintf(buf, "%d\n", to_ccwdev(dev)->private->cmb ? 1 : 0);
1213 static ssize_t cmb_enable_store(struct device *dev,
1214 struct device_attribute *attr, const char *buf,
1217 struct ccw_device *cdev;
1221 ret = strict_strtoul(buf, 16, &val);
1225 cdev = to_ccwdev(dev);
1229 ret = disable_cmf(cdev);
1232 ret = enable_cmf(cdev);
1239 DEVICE_ATTR(cmb_enable, 0644, cmb_enable_show, cmb_enable_store);
1242 * enable_cmf() - switch on the channel measurement for a specific device
1243 * @cdev: The ccw device to be enabled
1245 * Returns %0 for success or a negative error value.
1250 int enable_cmf(struct ccw_device *cdev)
1254 ret = cmbops->alloc(cdev);
1255 cmbops->reset(cdev);
1258 ret = cmbops->set(cdev, 2);
1263 ret = sysfs_create_group(&cdev->dev.kobj, cmbops->attr_group);
1266 cmbops->set(cdev, 0); //FIXME: this can fail
1272 * disable_cmf() - switch off the channel measurement for a specific device
1273 * @cdev: The ccw device to be disabled
1275 * Returns %0 for success or a negative error value.
1280 int disable_cmf(struct ccw_device *cdev)
1284 ret = cmbops->set(cdev, 0);
1288 sysfs_remove_group(&cdev->dev.kobj, cmbops->attr_group);
1293 * cmf_read() - read one value from the current channel measurement block
1294 * @cdev: the channel to be read
1295 * @index: the index of the value to be read
1297 * Returns the value read or %0 if the value cannot be read.
1302 u64 cmf_read(struct ccw_device *cdev, int index)
1304 return cmbops->read(cdev, index);
1308 * cmf_readall() - read the current channel measurement block
1309 * @cdev: the channel to be read
1310 * @data: a pointer to a data block that will be filled
1312 * Returns %0 on success, a negative error value otherwise.
1317 int cmf_readall(struct ccw_device *cdev, struct cmbdata *data)
1319 return cmbops->readall(cdev, data);
1322 /* Reenable cmf when a disconnected device becomes available again. */
1323 int cmf_reenable(struct ccw_device *cdev)
1325 cmbops->reset(cdev);
1326 return cmbops->set(cdev, 2);
1329 static int __init init_cmf(void)
1331 char *format_string;
1332 char *detect_string = "parameter";
1335 * If the user did not give a parameter, see if we are running on a
1336 * machine supporting extended measurement blocks, otherwise fall back
1339 if (format == CMF_AUTODETECT) {
1340 if (!css_general_characteristics.ext_mb) {
1343 format = CMF_EXTENDED;
1345 detect_string = "autodetected";
1347 detect_string = "parameter";
1352 format_string = "basic";
1353 cmbops = &cmbops_basic;
1356 format_string = "extended";
1357 cmbops = &cmbops_extended;
1363 printk(KERN_INFO "cio: Channel measurement facility using %s "
1364 "format (%s)\n", format_string, detect_string);
1368 module_init(init_cmf);
1371 MODULE_AUTHOR("Arnd Bergmann <arndb@de.ibm.com>");
1372 MODULE_LICENSE("GPL");
1373 MODULE_DESCRIPTION("channel measurement facility base driver\n"
1374 "Copyright 2003 IBM Corporation\n");
1376 EXPORT_SYMBOL_GPL(enable_cmf);
1377 EXPORT_SYMBOL_GPL(disable_cmf);
1378 EXPORT_SYMBOL_GPL(cmf_read);
1379 EXPORT_SYMBOL_GPL(cmf_readall);