ASoC: wm_adsp: Check return value from wm_adsp_buffer_init
[linux/fpc-iii.git] / drivers / s390 / cio / cmf.c
blob268aa23afa0166a3a509fb3d7b7e50d1f1f4515c
1 /*
2 * Linux on zSeries Channel Measurement Facility support
4 * Copyright IBM Corp. 2000, 2006
6 * Authors: Arnd Bergmann <arndb@de.ibm.com>
7 * Cornelia Huck <cornelia.huck@de.ibm.com>
9 * original idea from Natarajan Krishnaswami <nkrishna@us.ibm.com>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2, or (at your option)
14 * any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 #define KMSG_COMPONENT "cio"
27 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
29 #include <linux/bootmem.h>
30 #include <linux/device.h>
31 #include <linux/init.h>
32 #include <linux/list.h>
33 #include <linux/module.h>
34 #include <linux/moduleparam.h>
35 #include <linux/slab.h>
36 #include <linux/timex.h> /* get_tod_clock() */
38 #include <asm/ccwdev.h>
39 #include <asm/cio.h>
40 #include <asm/cmb.h>
41 #include <asm/div64.h>
43 #include "cio.h"
44 #include "css.h"
45 #include "device.h"
46 #include "ioasm.h"
47 #include "chsc.h"
50 * parameter to enable cmf during boot, possible uses are:
51 * "s390cmf" -- enable cmf and allocate 2 MB of ram so measuring can be
52 * used on any subchannel
53 * "s390cmf=<num>" -- enable cmf and allocate enough memory to measure
54 * <num> subchannel, where <num> is an integer
55 * between 1 and 65535, default is 1024
57 #define ARGSTRING "s390cmf"
59 /* indices for READCMB */
60 enum cmb_index {
61 /* basic and exended format: */
62 cmb_ssch_rsch_count,
63 cmb_sample_count,
64 cmb_device_connect_time,
65 cmb_function_pending_time,
66 cmb_device_disconnect_time,
67 cmb_control_unit_queuing_time,
68 cmb_device_active_only_time,
69 /* extended format only: */
70 cmb_device_busy_time,
71 cmb_initial_command_response_time,
74 /**
75 * enum cmb_format - types of supported measurement block formats
77 * @CMF_BASIC: traditional channel measurement blocks supported
78 * by all machines that we run on
79 * @CMF_EXTENDED: improved format that was introduced with the z990
80 * machine
81 * @CMF_AUTODETECT: default: use extended format when running on a machine
82 * supporting extended format, otherwise fall back to
83 * basic format
85 enum cmb_format {
86 CMF_BASIC,
87 CMF_EXTENDED,
88 CMF_AUTODETECT = -1,
92 * format - actual format for all measurement blocks
94 * The format module parameter can be set to a value of 0 (zero)
95 * or 1, indicating basic or extended format as described for
96 * enum cmb_format.
98 static int format = CMF_AUTODETECT;
99 module_param(format, bint, 0444);
102 * struct cmb_operations - functions to use depending on cmb_format
104 * Most of these functions operate on a struct ccw_device. There is only
105 * one instance of struct cmb_operations because the format of the measurement
106 * data is guaranteed to be the same for every ccw_device.
108 * @alloc: allocate memory for a channel measurement block,
109 * either with the help of a special pool or with kmalloc
110 * @free: free memory allocated with @alloc
111 * @set: enable or disable measurement
112 * @read: read a measurement entry at an index
113 * @readall: read a measurement block in a common format
114 * @reset: clear the data in the associated measurement block and
115 * reset its time stamp
117 struct cmb_operations {
118 int (*alloc) (struct ccw_device *);
119 void (*free) (struct ccw_device *);
120 int (*set) (struct ccw_device *, u32);
121 u64 (*read) (struct ccw_device *, int);
122 int (*readall)(struct ccw_device *, struct cmbdata *);
123 void (*reset) (struct ccw_device *);
124 /* private: */
125 struct attribute_group *attr_group;
127 static struct cmb_operations *cmbops;
129 struct cmb_data {
130 void *hw_block; /* Pointer to block updated by hardware */
131 void *last_block; /* Last changed block copied from hardware block */
132 int size; /* Size of hw_block and last_block */
133 unsigned long long last_update; /* when last_block was updated */
137 * Our user interface is designed in terms of nanoseconds,
138 * while the hardware measures total times in its own
139 * unit.
141 static inline u64 time_to_nsec(u32 value)
143 return ((u64)value) * 128000ull;
147 * Users are usually interested in average times,
148 * not accumulated time.
149 * This also helps us with atomicity problems
150 * when reading sinlge values.
152 static inline u64 time_to_avg_nsec(u32 value, u32 count)
154 u64 ret;
156 /* no samples yet, avoid division by 0 */
157 if (count == 0)
158 return 0;
160 /* value comes in units of 128 µsec */
161 ret = time_to_nsec(value);
162 do_div(ret, count);
164 return ret;
167 #define CMF_OFF 0
168 #define CMF_ON 2
171 * Activate or deactivate the channel monitor. When area is NULL,
172 * the monitor is deactivated. The channel monitor needs to
173 * be active in order to measure subchannels, which also need
174 * to be enabled.
176 static inline void cmf_activate(void *area, unsigned int onoff)
178 register void * __gpr2 asm("2");
179 register long __gpr1 asm("1");
181 __gpr2 = area;
182 __gpr1 = onoff;
183 /* activate channel measurement */
184 asm("schm" : : "d" (__gpr2), "d" (__gpr1) );
187 static int set_schib(struct ccw_device *cdev, u32 mme, int mbfc,
188 unsigned long address)
190 struct subchannel *sch = to_subchannel(cdev->dev.parent);
191 int ret;
193 sch->config.mme = mme;
194 sch->config.mbfc = mbfc;
195 /* address can be either a block address or a block index */
196 if (mbfc)
197 sch->config.mba = address;
198 else
199 sch->config.mbi = address;
201 ret = cio_commit_config(sch);
202 if (!mme && ret == -ENODEV) {
204 * The task was to disable measurement block updates but
205 * the subchannel is already gone. Report success.
207 ret = 0;
209 return ret;
212 struct set_schib_struct {
213 u32 mme;
214 int mbfc;
215 unsigned long address;
216 wait_queue_head_t wait;
217 int ret;
218 struct kref kref;
221 static void cmf_set_schib_release(struct kref *kref)
223 struct set_schib_struct *set_data;
225 set_data = container_of(kref, struct set_schib_struct, kref);
226 kfree(set_data);
229 #define CMF_PENDING 1
231 static int set_schib_wait(struct ccw_device *cdev, u32 mme,
232 int mbfc, unsigned long address)
234 struct set_schib_struct *set_data;
235 int ret;
237 spin_lock_irq(cdev->ccwlock);
238 if (!cdev->private->cmb) {
239 ret = -ENODEV;
240 goto out;
242 set_data = kzalloc(sizeof(struct set_schib_struct), GFP_ATOMIC);
243 if (!set_data) {
244 ret = -ENOMEM;
245 goto out;
247 init_waitqueue_head(&set_data->wait);
248 kref_init(&set_data->kref);
249 set_data->mme = mme;
250 set_data->mbfc = mbfc;
251 set_data->address = address;
253 ret = set_schib(cdev, mme, mbfc, address);
254 if (ret != -EBUSY)
255 goto out_put;
257 if (cdev->private->state != DEV_STATE_ONLINE) {
258 /* if the device is not online, don't even try again */
259 ret = -EBUSY;
260 goto out_put;
263 cdev->private->state = DEV_STATE_CMFCHANGE;
264 set_data->ret = CMF_PENDING;
265 cdev->private->cmb_wait = set_data;
267 spin_unlock_irq(cdev->ccwlock);
268 if (wait_event_interruptible(set_data->wait,
269 set_data->ret != CMF_PENDING)) {
270 spin_lock_irq(cdev->ccwlock);
271 if (set_data->ret == CMF_PENDING) {
272 set_data->ret = -ERESTARTSYS;
273 if (cdev->private->state == DEV_STATE_CMFCHANGE)
274 cdev->private->state = DEV_STATE_ONLINE;
276 spin_unlock_irq(cdev->ccwlock);
278 spin_lock_irq(cdev->ccwlock);
279 cdev->private->cmb_wait = NULL;
280 ret = set_data->ret;
281 out_put:
282 kref_put(&set_data->kref, cmf_set_schib_release);
283 out:
284 spin_unlock_irq(cdev->ccwlock);
285 return ret;
288 void retry_set_schib(struct ccw_device *cdev)
290 struct set_schib_struct *set_data;
292 set_data = cdev->private->cmb_wait;
293 if (!set_data) {
294 WARN_ON(1);
295 return;
297 kref_get(&set_data->kref);
298 set_data->ret = set_schib(cdev, set_data->mme, set_data->mbfc,
299 set_data->address);
300 wake_up(&set_data->wait);
301 kref_put(&set_data->kref, cmf_set_schib_release);
304 static int cmf_copy_block(struct ccw_device *cdev)
306 struct subchannel *sch;
307 void *reference_buf;
308 void *hw_block;
309 struct cmb_data *cmb_data;
311 sch = to_subchannel(cdev->dev.parent);
313 if (cio_update_schib(sch))
314 return -ENODEV;
316 if (scsw_fctl(&sch->schib.scsw) & SCSW_FCTL_START_FUNC) {
317 /* Don't copy if a start function is in progress. */
318 if ((!(scsw_actl(&sch->schib.scsw) & SCSW_ACTL_SUSPENDED)) &&
319 (scsw_actl(&sch->schib.scsw) &
320 (SCSW_ACTL_DEVACT | SCSW_ACTL_SCHACT)) &&
321 (!(scsw_stctl(&sch->schib.scsw) & SCSW_STCTL_SEC_STATUS)))
322 return -EBUSY;
324 cmb_data = cdev->private->cmb;
325 hw_block = cmb_data->hw_block;
326 if (!memcmp(cmb_data->last_block, hw_block, cmb_data->size))
327 /* No need to copy. */
328 return 0;
329 reference_buf = kzalloc(cmb_data->size, GFP_ATOMIC);
330 if (!reference_buf)
331 return -ENOMEM;
332 /* Ensure consistency of block copied from hardware. */
333 do {
334 memcpy(cmb_data->last_block, hw_block, cmb_data->size);
335 memcpy(reference_buf, hw_block, cmb_data->size);
336 } while (memcmp(cmb_data->last_block, reference_buf, cmb_data->size));
337 cmb_data->last_update = get_tod_clock();
338 kfree(reference_buf);
339 return 0;
342 struct copy_block_struct {
343 wait_queue_head_t wait;
344 int ret;
345 struct kref kref;
348 static void cmf_copy_block_release(struct kref *kref)
350 struct copy_block_struct *copy_block;
352 copy_block = container_of(kref, struct copy_block_struct, kref);
353 kfree(copy_block);
356 static int cmf_cmb_copy_wait(struct ccw_device *cdev)
358 struct copy_block_struct *copy_block;
359 int ret;
360 unsigned long flags;
362 spin_lock_irqsave(cdev->ccwlock, flags);
363 if (!cdev->private->cmb) {
364 ret = -ENODEV;
365 goto out;
367 copy_block = kzalloc(sizeof(struct copy_block_struct), GFP_ATOMIC);
368 if (!copy_block) {
369 ret = -ENOMEM;
370 goto out;
372 init_waitqueue_head(&copy_block->wait);
373 kref_init(&copy_block->kref);
375 ret = cmf_copy_block(cdev);
376 if (ret != -EBUSY)
377 goto out_put;
379 if (cdev->private->state != DEV_STATE_ONLINE) {
380 ret = -EBUSY;
381 goto out_put;
384 cdev->private->state = DEV_STATE_CMFUPDATE;
385 copy_block->ret = CMF_PENDING;
386 cdev->private->cmb_wait = copy_block;
388 spin_unlock_irqrestore(cdev->ccwlock, flags);
389 if (wait_event_interruptible(copy_block->wait,
390 copy_block->ret != CMF_PENDING)) {
391 spin_lock_irqsave(cdev->ccwlock, flags);
392 if (copy_block->ret == CMF_PENDING) {
393 copy_block->ret = -ERESTARTSYS;
394 if (cdev->private->state == DEV_STATE_CMFUPDATE)
395 cdev->private->state = DEV_STATE_ONLINE;
397 spin_unlock_irqrestore(cdev->ccwlock, flags);
399 spin_lock_irqsave(cdev->ccwlock, flags);
400 cdev->private->cmb_wait = NULL;
401 ret = copy_block->ret;
402 out_put:
403 kref_put(&copy_block->kref, cmf_copy_block_release);
404 out:
405 spin_unlock_irqrestore(cdev->ccwlock, flags);
406 return ret;
409 void cmf_retry_copy_block(struct ccw_device *cdev)
411 struct copy_block_struct *copy_block;
413 copy_block = cdev->private->cmb_wait;
414 if (!copy_block) {
415 WARN_ON(1);
416 return;
418 kref_get(&copy_block->kref);
419 copy_block->ret = cmf_copy_block(cdev);
420 wake_up(&copy_block->wait);
421 kref_put(&copy_block->kref, cmf_copy_block_release);
424 static void cmf_generic_reset(struct ccw_device *cdev)
426 struct cmb_data *cmb_data;
428 spin_lock_irq(cdev->ccwlock);
429 cmb_data = cdev->private->cmb;
430 if (cmb_data) {
431 memset(cmb_data->last_block, 0, cmb_data->size);
433 * Need to reset hw block as well to make the hardware start
434 * from 0 again.
436 memset(cmb_data->hw_block, 0, cmb_data->size);
437 cmb_data->last_update = 0;
439 cdev->private->cmb_start_time = get_tod_clock();
440 spin_unlock_irq(cdev->ccwlock);
444 * struct cmb_area - container for global cmb data
446 * @mem: pointer to CMBs (only in basic measurement mode)
447 * @list: contains a linked list of all subchannels
448 * @num_channels: number of channels to be measured
449 * @lock: protect concurrent access to @mem and @list
451 struct cmb_area {
452 struct cmb *mem;
453 struct list_head list;
454 int num_channels;
455 spinlock_t lock;
458 static struct cmb_area cmb_area = {
459 .lock = __SPIN_LOCK_UNLOCKED(cmb_area.lock),
460 .list = LIST_HEAD_INIT(cmb_area.list),
461 .num_channels = 1024,
464 /* ****** old style CMB handling ********/
467 * Basic channel measurement blocks are allocated in one contiguous
468 * block of memory, which can not be moved as long as any channel
469 * is active. Therefore, a maximum number of subchannels needs to
470 * be defined somewhere. This is a module parameter, defaulting to
471 * a reasonable value of 1024, or 32 kb of memory.
472 * Current kernels don't allow kmalloc with more than 128kb, so the
473 * maximum is 4096.
476 module_param_named(maxchannels, cmb_area.num_channels, uint, 0444);
479 * struct cmb - basic channel measurement block
480 * @ssch_rsch_count: number of ssch and rsch
481 * @sample_count: number of samples
482 * @device_connect_time: time of device connect
483 * @function_pending_time: time of function pending
484 * @device_disconnect_time: time of device disconnect
485 * @control_unit_queuing_time: time of control unit queuing
486 * @device_active_only_time: time of device active only
487 * @reserved: unused in basic measurement mode
489 * The measurement block as used by the hardware. The fields are described
490 * further in z/Architecture Principles of Operation, chapter 17.
492 * The cmb area made up from these blocks must be a contiguous array and may
493 * not be reallocated or freed.
494 * Only one cmb area can be present in the system.
496 struct cmb {
497 u16 ssch_rsch_count;
498 u16 sample_count;
499 u32 device_connect_time;
500 u32 function_pending_time;
501 u32 device_disconnect_time;
502 u32 control_unit_queuing_time;
503 u32 device_active_only_time;
504 u32 reserved[2];
508 * Insert a single device into the cmb_area list.
509 * Called with cmb_area.lock held from alloc_cmb.
511 static int alloc_cmb_single(struct ccw_device *cdev,
512 struct cmb_data *cmb_data)
514 struct cmb *cmb;
515 struct ccw_device_private *node;
516 int ret;
518 spin_lock_irq(cdev->ccwlock);
519 if (!list_empty(&cdev->private->cmb_list)) {
520 ret = -EBUSY;
521 goto out;
525 * Find first unused cmb in cmb_area.mem.
526 * This is a little tricky: cmb_area.list
527 * remains sorted by ->cmb->hw_data pointers.
529 cmb = cmb_area.mem;
530 list_for_each_entry(node, &cmb_area.list, cmb_list) {
531 struct cmb_data *data;
532 data = node->cmb;
533 if ((struct cmb*)data->hw_block > cmb)
534 break;
535 cmb++;
537 if (cmb - cmb_area.mem >= cmb_area.num_channels) {
538 ret = -ENOMEM;
539 goto out;
542 /* insert new cmb */
543 list_add_tail(&cdev->private->cmb_list, &node->cmb_list);
544 cmb_data->hw_block = cmb;
545 cdev->private->cmb = cmb_data;
546 ret = 0;
547 out:
548 spin_unlock_irq(cdev->ccwlock);
549 return ret;
552 static int alloc_cmb(struct ccw_device *cdev)
554 int ret;
555 struct cmb *mem;
556 ssize_t size;
557 struct cmb_data *cmb_data;
559 /* Allocate private cmb_data. */
560 cmb_data = kzalloc(sizeof(struct cmb_data), GFP_KERNEL);
561 if (!cmb_data)
562 return -ENOMEM;
564 cmb_data->last_block = kzalloc(sizeof(struct cmb), GFP_KERNEL);
565 if (!cmb_data->last_block) {
566 kfree(cmb_data);
567 return -ENOMEM;
569 cmb_data->size = sizeof(struct cmb);
570 spin_lock(&cmb_area.lock);
572 if (!cmb_area.mem) {
573 /* there is no user yet, so we need a new area */
574 size = sizeof(struct cmb) * cmb_area.num_channels;
575 WARN_ON(!list_empty(&cmb_area.list));
577 spin_unlock(&cmb_area.lock);
578 mem = (void*)__get_free_pages(GFP_KERNEL | GFP_DMA,
579 get_order(size));
580 spin_lock(&cmb_area.lock);
582 if (cmb_area.mem) {
583 /* ok, another thread was faster */
584 free_pages((unsigned long)mem, get_order(size));
585 } else if (!mem) {
586 /* no luck */
587 ret = -ENOMEM;
588 goto out;
589 } else {
590 /* everything ok */
591 memset(mem, 0, size);
592 cmb_area.mem = mem;
593 cmf_activate(cmb_area.mem, CMF_ON);
597 /* do the actual allocation */
598 ret = alloc_cmb_single(cdev, cmb_data);
599 out:
600 spin_unlock(&cmb_area.lock);
601 if (ret) {
602 kfree(cmb_data->last_block);
603 kfree(cmb_data);
605 return ret;
608 static void free_cmb(struct ccw_device *cdev)
610 struct ccw_device_private *priv;
611 struct cmb_data *cmb_data;
613 spin_lock(&cmb_area.lock);
614 spin_lock_irq(cdev->ccwlock);
616 priv = cdev->private;
617 cmb_data = priv->cmb;
618 priv->cmb = NULL;
619 if (cmb_data)
620 kfree(cmb_data->last_block);
621 kfree(cmb_data);
622 list_del_init(&priv->cmb_list);
624 if (list_empty(&cmb_area.list)) {
625 ssize_t size;
626 size = sizeof(struct cmb) * cmb_area.num_channels;
627 cmf_activate(NULL, CMF_OFF);
628 free_pages((unsigned long)cmb_area.mem, get_order(size));
629 cmb_area.mem = NULL;
631 spin_unlock_irq(cdev->ccwlock);
632 spin_unlock(&cmb_area.lock);
635 static int set_cmb(struct ccw_device *cdev, u32 mme)
637 u16 offset;
638 struct cmb_data *cmb_data;
639 unsigned long flags;
641 spin_lock_irqsave(cdev->ccwlock, flags);
642 if (!cdev->private->cmb) {
643 spin_unlock_irqrestore(cdev->ccwlock, flags);
644 return -EINVAL;
646 cmb_data = cdev->private->cmb;
647 offset = mme ? (struct cmb *)cmb_data->hw_block - cmb_area.mem : 0;
648 spin_unlock_irqrestore(cdev->ccwlock, flags);
650 return set_schib_wait(cdev, mme, 0, offset);
653 static u64 read_cmb(struct ccw_device *cdev, int index)
655 struct cmb *cmb;
656 u32 val;
657 int ret;
658 unsigned long flags;
660 ret = cmf_cmb_copy_wait(cdev);
661 if (ret < 0)
662 return 0;
664 spin_lock_irqsave(cdev->ccwlock, flags);
665 if (!cdev->private->cmb) {
666 ret = 0;
667 goto out;
669 cmb = ((struct cmb_data *)cdev->private->cmb)->last_block;
671 switch (index) {
672 case cmb_ssch_rsch_count:
673 ret = cmb->ssch_rsch_count;
674 goto out;
675 case cmb_sample_count:
676 ret = cmb->sample_count;
677 goto out;
678 case cmb_device_connect_time:
679 val = cmb->device_connect_time;
680 break;
681 case cmb_function_pending_time:
682 val = cmb->function_pending_time;
683 break;
684 case cmb_device_disconnect_time:
685 val = cmb->device_disconnect_time;
686 break;
687 case cmb_control_unit_queuing_time:
688 val = cmb->control_unit_queuing_time;
689 break;
690 case cmb_device_active_only_time:
691 val = cmb->device_active_only_time;
692 break;
693 default:
694 ret = 0;
695 goto out;
697 ret = time_to_avg_nsec(val, cmb->sample_count);
698 out:
699 spin_unlock_irqrestore(cdev->ccwlock, flags);
700 return ret;
703 static int readall_cmb(struct ccw_device *cdev, struct cmbdata *data)
705 struct cmb *cmb;
706 struct cmb_data *cmb_data;
707 u64 time;
708 unsigned long flags;
709 int ret;
711 ret = cmf_cmb_copy_wait(cdev);
712 if (ret < 0)
713 return ret;
714 spin_lock_irqsave(cdev->ccwlock, flags);
715 cmb_data = cdev->private->cmb;
716 if (!cmb_data) {
717 ret = -ENODEV;
718 goto out;
720 if (cmb_data->last_update == 0) {
721 ret = -EAGAIN;
722 goto out;
724 cmb = cmb_data->last_block;
725 time = cmb_data->last_update - cdev->private->cmb_start_time;
727 memset(data, 0, sizeof(struct cmbdata));
729 /* we only know values before device_busy_time */
730 data->size = offsetof(struct cmbdata, device_busy_time);
732 /* convert to nanoseconds */
733 data->elapsed_time = (time * 1000) >> 12;
735 /* copy data to new structure */
736 data->ssch_rsch_count = cmb->ssch_rsch_count;
737 data->sample_count = cmb->sample_count;
739 /* time fields are converted to nanoseconds while copying */
740 data->device_connect_time = time_to_nsec(cmb->device_connect_time);
741 data->function_pending_time = time_to_nsec(cmb->function_pending_time);
742 data->device_disconnect_time =
743 time_to_nsec(cmb->device_disconnect_time);
744 data->control_unit_queuing_time
745 = time_to_nsec(cmb->control_unit_queuing_time);
746 data->device_active_only_time
747 = time_to_nsec(cmb->device_active_only_time);
748 ret = 0;
749 out:
750 spin_unlock_irqrestore(cdev->ccwlock, flags);
751 return ret;
754 static void reset_cmb(struct ccw_device *cdev)
756 cmf_generic_reset(cdev);
759 static int cmf_enabled(struct ccw_device *cdev)
761 int enabled;
763 spin_lock_irq(cdev->ccwlock);
764 enabled = !!cdev->private->cmb;
765 spin_unlock_irq(cdev->ccwlock);
767 return enabled;
770 static struct attribute_group cmf_attr_group;
772 static struct cmb_operations cmbops_basic = {
773 .alloc = alloc_cmb,
774 .free = free_cmb,
775 .set = set_cmb,
776 .read = read_cmb,
777 .readall = readall_cmb,
778 .reset = reset_cmb,
779 .attr_group = &cmf_attr_group,
782 /* ******** extended cmb handling ********/
785 * struct cmbe - extended channel measurement block
786 * @ssch_rsch_count: number of ssch and rsch
787 * @sample_count: number of samples
788 * @device_connect_time: time of device connect
789 * @function_pending_time: time of function pending
790 * @device_disconnect_time: time of device disconnect
791 * @control_unit_queuing_time: time of control unit queuing
792 * @device_active_only_time: time of device active only
793 * @device_busy_time: time of device busy
794 * @initial_command_response_time: initial command response time
795 * @reserved: unused
797 * The measurement block as used by the hardware. May be in any 64 bit physical
798 * location.
799 * The fields are described further in z/Architecture Principles of Operation,
800 * third edition, chapter 17.
802 struct cmbe {
803 u32 ssch_rsch_count;
804 u32 sample_count;
805 u32 device_connect_time;
806 u32 function_pending_time;
807 u32 device_disconnect_time;
808 u32 control_unit_queuing_time;
809 u32 device_active_only_time;
810 u32 device_busy_time;
811 u32 initial_command_response_time;
812 u32 reserved[7];
813 } __packed __aligned(64);
815 static struct kmem_cache *cmbe_cache;
817 static int alloc_cmbe(struct ccw_device *cdev)
819 struct cmb_data *cmb_data;
820 struct cmbe *cmbe;
821 int ret = -ENOMEM;
823 cmbe = kmem_cache_zalloc(cmbe_cache, GFP_KERNEL);
824 if (!cmbe)
825 return ret;
827 cmb_data = kzalloc(sizeof(*cmb_data), GFP_KERNEL);
828 if (!cmb_data)
829 goto out_free;
831 cmb_data->last_block = kzalloc(sizeof(struct cmbe), GFP_KERNEL);
832 if (!cmb_data->last_block)
833 goto out_free;
835 cmb_data->size = sizeof(*cmbe);
836 cmb_data->hw_block = cmbe;
838 spin_lock(&cmb_area.lock);
839 spin_lock_irq(cdev->ccwlock);
840 if (cdev->private->cmb)
841 goto out_unlock;
843 cdev->private->cmb = cmb_data;
845 /* activate global measurement if this is the first channel */
846 if (list_empty(&cmb_area.list))
847 cmf_activate(NULL, CMF_ON);
848 list_add_tail(&cdev->private->cmb_list, &cmb_area.list);
850 spin_unlock_irq(cdev->ccwlock);
851 spin_unlock(&cmb_area.lock);
852 return 0;
854 out_unlock:
855 spin_unlock_irq(cdev->ccwlock);
856 spin_unlock(&cmb_area.lock);
857 ret = -EBUSY;
858 out_free:
859 if (cmb_data)
860 kfree(cmb_data->last_block);
861 kfree(cmb_data);
862 kmem_cache_free(cmbe_cache, cmbe);
864 return ret;
867 static void free_cmbe(struct ccw_device *cdev)
869 struct cmb_data *cmb_data;
871 spin_lock(&cmb_area.lock);
872 spin_lock_irq(cdev->ccwlock);
873 cmb_data = cdev->private->cmb;
874 cdev->private->cmb = NULL;
875 if (cmb_data) {
876 kfree(cmb_data->last_block);
877 kmem_cache_free(cmbe_cache, cmb_data->hw_block);
879 kfree(cmb_data);
881 /* deactivate global measurement if this is the last channel */
882 list_del_init(&cdev->private->cmb_list);
883 if (list_empty(&cmb_area.list))
884 cmf_activate(NULL, CMF_OFF);
885 spin_unlock_irq(cdev->ccwlock);
886 spin_unlock(&cmb_area.lock);
889 static int set_cmbe(struct ccw_device *cdev, u32 mme)
891 unsigned long mba;
892 struct cmb_data *cmb_data;
893 unsigned long flags;
895 spin_lock_irqsave(cdev->ccwlock, flags);
896 if (!cdev->private->cmb) {
897 spin_unlock_irqrestore(cdev->ccwlock, flags);
898 return -EINVAL;
900 cmb_data = cdev->private->cmb;
901 mba = mme ? (unsigned long) cmb_data->hw_block : 0;
902 spin_unlock_irqrestore(cdev->ccwlock, flags);
904 return set_schib_wait(cdev, mme, 1, mba);
908 static u64 read_cmbe(struct ccw_device *cdev, int index)
910 struct cmbe *cmb;
911 struct cmb_data *cmb_data;
912 u32 val;
913 int ret;
914 unsigned long flags;
916 ret = cmf_cmb_copy_wait(cdev);
917 if (ret < 0)
918 return 0;
920 spin_lock_irqsave(cdev->ccwlock, flags);
921 cmb_data = cdev->private->cmb;
922 if (!cmb_data) {
923 ret = 0;
924 goto out;
926 cmb = cmb_data->last_block;
928 switch (index) {
929 case cmb_ssch_rsch_count:
930 ret = cmb->ssch_rsch_count;
931 goto out;
932 case cmb_sample_count:
933 ret = cmb->sample_count;
934 goto out;
935 case cmb_device_connect_time:
936 val = cmb->device_connect_time;
937 break;
938 case cmb_function_pending_time:
939 val = cmb->function_pending_time;
940 break;
941 case cmb_device_disconnect_time:
942 val = cmb->device_disconnect_time;
943 break;
944 case cmb_control_unit_queuing_time:
945 val = cmb->control_unit_queuing_time;
946 break;
947 case cmb_device_active_only_time:
948 val = cmb->device_active_only_time;
949 break;
950 case cmb_device_busy_time:
951 val = cmb->device_busy_time;
952 break;
953 case cmb_initial_command_response_time:
954 val = cmb->initial_command_response_time;
955 break;
956 default:
957 ret = 0;
958 goto out;
960 ret = time_to_avg_nsec(val, cmb->sample_count);
961 out:
962 spin_unlock_irqrestore(cdev->ccwlock, flags);
963 return ret;
966 static int readall_cmbe(struct ccw_device *cdev, struct cmbdata *data)
968 struct cmbe *cmb;
969 struct cmb_data *cmb_data;
970 u64 time;
971 unsigned long flags;
972 int ret;
974 ret = cmf_cmb_copy_wait(cdev);
975 if (ret < 0)
976 return ret;
977 spin_lock_irqsave(cdev->ccwlock, flags);
978 cmb_data = cdev->private->cmb;
979 if (!cmb_data) {
980 ret = -ENODEV;
981 goto out;
983 if (cmb_data->last_update == 0) {
984 ret = -EAGAIN;
985 goto out;
987 time = cmb_data->last_update - cdev->private->cmb_start_time;
989 memset (data, 0, sizeof(struct cmbdata));
991 /* we only know values before device_busy_time */
992 data->size = offsetof(struct cmbdata, device_busy_time);
994 /* conver to nanoseconds */
995 data->elapsed_time = (time * 1000) >> 12;
997 cmb = cmb_data->last_block;
998 /* copy data to new structure */
999 data->ssch_rsch_count = cmb->ssch_rsch_count;
1000 data->sample_count = cmb->sample_count;
1002 /* time fields are converted to nanoseconds while copying */
1003 data->device_connect_time = time_to_nsec(cmb->device_connect_time);
1004 data->function_pending_time = time_to_nsec(cmb->function_pending_time);
1005 data->device_disconnect_time =
1006 time_to_nsec(cmb->device_disconnect_time);
1007 data->control_unit_queuing_time
1008 = time_to_nsec(cmb->control_unit_queuing_time);
1009 data->device_active_only_time
1010 = time_to_nsec(cmb->device_active_only_time);
1011 data->device_busy_time = time_to_nsec(cmb->device_busy_time);
1012 data->initial_command_response_time
1013 = time_to_nsec(cmb->initial_command_response_time);
1015 ret = 0;
1016 out:
1017 spin_unlock_irqrestore(cdev->ccwlock, flags);
1018 return ret;
1021 static void reset_cmbe(struct ccw_device *cdev)
1023 cmf_generic_reset(cdev);
1026 static struct attribute_group cmf_attr_group_ext;
1028 static struct cmb_operations cmbops_extended = {
1029 .alloc = alloc_cmbe,
1030 .free = free_cmbe,
1031 .set = set_cmbe,
1032 .read = read_cmbe,
1033 .readall = readall_cmbe,
1034 .reset = reset_cmbe,
1035 .attr_group = &cmf_attr_group_ext,
1038 static ssize_t cmb_show_attr(struct device *dev, char *buf, enum cmb_index idx)
1040 return sprintf(buf, "%lld\n",
1041 (unsigned long long) cmf_read(to_ccwdev(dev), idx));
1044 static ssize_t cmb_show_avg_sample_interval(struct device *dev,
1045 struct device_attribute *attr,
1046 char *buf)
1048 struct ccw_device *cdev;
1049 long interval;
1050 unsigned long count;
1051 struct cmb_data *cmb_data;
1053 cdev = to_ccwdev(dev);
1054 count = cmf_read(cdev, cmb_sample_count);
1055 spin_lock_irq(cdev->ccwlock);
1056 cmb_data = cdev->private->cmb;
1057 if (count) {
1058 interval = cmb_data->last_update -
1059 cdev->private->cmb_start_time;
1060 interval = (interval * 1000) >> 12;
1061 interval /= count;
1062 } else
1063 interval = -1;
1064 spin_unlock_irq(cdev->ccwlock);
1065 return sprintf(buf, "%ld\n", interval);
1068 static ssize_t cmb_show_avg_utilization(struct device *dev,
1069 struct device_attribute *attr,
1070 char *buf)
1072 struct cmbdata data;
1073 u64 utilization;
1074 unsigned long t, u;
1075 int ret;
1077 ret = cmf_readall(to_ccwdev(dev), &data);
1078 if (ret == -EAGAIN || ret == -ENODEV)
1079 /* No data (yet/currently) available to use for calculation. */
1080 return sprintf(buf, "n/a\n");
1081 else if (ret)
1082 return ret;
1084 utilization = data.device_connect_time +
1085 data.function_pending_time +
1086 data.device_disconnect_time;
1088 /* shift to avoid long long division */
1089 while (-1ul < (data.elapsed_time | utilization)) {
1090 utilization >>= 8;
1091 data.elapsed_time >>= 8;
1094 /* calculate value in 0.1 percent units */
1095 t = (unsigned long) data.elapsed_time / 1000;
1096 u = (unsigned long) utilization / t;
1098 return sprintf(buf, "%02ld.%01ld%%\n", u/ 10, u - (u/ 10) * 10);
1101 #define cmf_attr(name) \
1102 static ssize_t show_##name(struct device *dev, \
1103 struct device_attribute *attr, char *buf) \
1104 { return cmb_show_attr((dev), buf, cmb_##name); } \
1105 static DEVICE_ATTR(name, 0444, show_##name, NULL);
1107 #define cmf_attr_avg(name) \
1108 static ssize_t show_avg_##name(struct device *dev, \
1109 struct device_attribute *attr, char *buf) \
1110 { return cmb_show_attr((dev), buf, cmb_##name); } \
1111 static DEVICE_ATTR(avg_##name, 0444, show_avg_##name, NULL);
1113 cmf_attr(ssch_rsch_count);
1114 cmf_attr(sample_count);
1115 cmf_attr_avg(device_connect_time);
1116 cmf_attr_avg(function_pending_time);
1117 cmf_attr_avg(device_disconnect_time);
1118 cmf_attr_avg(control_unit_queuing_time);
1119 cmf_attr_avg(device_active_only_time);
1120 cmf_attr_avg(device_busy_time);
1121 cmf_attr_avg(initial_command_response_time);
1123 static DEVICE_ATTR(avg_sample_interval, 0444, cmb_show_avg_sample_interval,
1124 NULL);
1125 static DEVICE_ATTR(avg_utilization, 0444, cmb_show_avg_utilization, NULL);
1127 static struct attribute *cmf_attributes[] = {
1128 &dev_attr_avg_sample_interval.attr,
1129 &dev_attr_avg_utilization.attr,
1130 &dev_attr_ssch_rsch_count.attr,
1131 &dev_attr_sample_count.attr,
1132 &dev_attr_avg_device_connect_time.attr,
1133 &dev_attr_avg_function_pending_time.attr,
1134 &dev_attr_avg_device_disconnect_time.attr,
1135 &dev_attr_avg_control_unit_queuing_time.attr,
1136 &dev_attr_avg_device_active_only_time.attr,
1137 NULL,
1140 static struct attribute_group cmf_attr_group = {
1141 .name = "cmf",
1142 .attrs = cmf_attributes,
1145 static struct attribute *cmf_attributes_ext[] = {
1146 &dev_attr_avg_sample_interval.attr,
1147 &dev_attr_avg_utilization.attr,
1148 &dev_attr_ssch_rsch_count.attr,
1149 &dev_attr_sample_count.attr,
1150 &dev_attr_avg_device_connect_time.attr,
1151 &dev_attr_avg_function_pending_time.attr,
1152 &dev_attr_avg_device_disconnect_time.attr,
1153 &dev_attr_avg_control_unit_queuing_time.attr,
1154 &dev_attr_avg_device_active_only_time.attr,
1155 &dev_attr_avg_device_busy_time.attr,
1156 &dev_attr_avg_initial_command_response_time.attr,
1157 NULL,
1160 static struct attribute_group cmf_attr_group_ext = {
1161 .name = "cmf",
1162 .attrs = cmf_attributes_ext,
1165 static ssize_t cmb_enable_show(struct device *dev,
1166 struct device_attribute *attr,
1167 char *buf)
1169 struct ccw_device *cdev = to_ccwdev(dev);
1171 return sprintf(buf, "%d\n", cmf_enabled(cdev));
1174 static ssize_t cmb_enable_store(struct device *dev,
1175 struct device_attribute *attr, const char *buf,
1176 size_t c)
1178 struct ccw_device *cdev = to_ccwdev(dev);
1179 unsigned long val;
1180 int ret;
1182 ret = kstrtoul(buf, 16, &val);
1183 if (ret)
1184 return ret;
1186 switch (val) {
1187 case 0:
1188 ret = disable_cmf(cdev);
1189 break;
1190 case 1:
1191 ret = enable_cmf(cdev);
1192 break;
1193 default:
1194 ret = -EINVAL;
1197 return ret ? ret : c;
1199 DEVICE_ATTR_RW(cmb_enable);
1201 int ccw_set_cmf(struct ccw_device *cdev, int enable)
1203 return cmbops->set(cdev, enable ? 2 : 0);
1207 * enable_cmf() - switch on the channel measurement for a specific device
1208 * @cdev: The ccw device to be enabled
1210 * Returns %0 for success or a negative error value.
1211 * Note: If this is called on a device for which channel measurement is already
1212 * enabled a reset of the measurement data is triggered.
1213 * Context:
1214 * non-atomic
1216 int enable_cmf(struct ccw_device *cdev)
1218 int ret = 0;
1220 device_lock(&cdev->dev);
1221 if (cmf_enabled(cdev)) {
1222 cmbops->reset(cdev);
1223 goto out_unlock;
1225 get_device(&cdev->dev);
1226 ret = cmbops->alloc(cdev);
1227 if (ret)
1228 goto out;
1229 cmbops->reset(cdev);
1230 ret = sysfs_create_group(&cdev->dev.kobj, cmbops->attr_group);
1231 if (ret) {
1232 cmbops->free(cdev);
1233 goto out;
1235 ret = cmbops->set(cdev, 2);
1236 if (ret) {
1237 sysfs_remove_group(&cdev->dev.kobj, cmbops->attr_group);
1238 cmbops->free(cdev);
1240 out:
1241 if (ret)
1242 put_device(&cdev->dev);
1243 out_unlock:
1244 device_unlock(&cdev->dev);
1245 return ret;
1249 * __disable_cmf() - switch off the channel measurement for a specific device
1250 * @cdev: The ccw device to be disabled
1252 * Returns %0 for success or a negative error value.
1254 * Context:
1255 * non-atomic, device_lock() held.
1257 int __disable_cmf(struct ccw_device *cdev)
1259 int ret;
1261 ret = cmbops->set(cdev, 0);
1262 if (ret)
1263 return ret;
1265 sysfs_remove_group(&cdev->dev.kobj, cmbops->attr_group);
1266 cmbops->free(cdev);
1267 put_device(&cdev->dev);
1269 return ret;
1273 * disable_cmf() - switch off the channel measurement for a specific device
1274 * @cdev: The ccw device to be disabled
1276 * Returns %0 for success or a negative error value.
1278 * Context:
1279 * non-atomic
1281 int disable_cmf(struct ccw_device *cdev)
1283 int ret;
1285 device_lock(&cdev->dev);
1286 ret = __disable_cmf(cdev);
1287 device_unlock(&cdev->dev);
1289 return ret;
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.
1299 * Context:
1300 * any
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.
1314 * Context:
1315 * any
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);
1330 * cmf_reactivate() - reactivate measurement block updates
1332 * Use this during resume from hibernate.
1334 void cmf_reactivate(void)
1336 spin_lock(&cmb_area.lock);
1337 if (!list_empty(&cmb_area.list))
1338 cmf_activate(cmb_area.mem, CMF_ON);
1339 spin_unlock(&cmb_area.lock);
1342 static int __init init_cmbe(void)
1344 cmbe_cache = kmem_cache_create("cmbe_cache", sizeof(struct cmbe),
1345 __alignof__(struct cmbe), 0, NULL);
1347 return cmbe_cache ? 0 : -ENOMEM;
1350 static int __init init_cmf(void)
1352 char *format_string;
1353 char *detect_string;
1354 int ret;
1357 * If the user did not give a parameter, see if we are running on a
1358 * machine supporting extended measurement blocks, otherwise fall back
1359 * to basic mode.
1361 if (format == CMF_AUTODETECT) {
1362 if (!css_general_characteristics.ext_mb) {
1363 format = CMF_BASIC;
1364 } else {
1365 format = CMF_EXTENDED;
1367 detect_string = "autodetected";
1368 } else {
1369 detect_string = "parameter";
1372 switch (format) {
1373 case CMF_BASIC:
1374 format_string = "basic";
1375 cmbops = &cmbops_basic;
1376 break;
1377 case CMF_EXTENDED:
1378 format_string = "extended";
1379 cmbops = &cmbops_extended;
1381 ret = init_cmbe();
1382 if (ret)
1383 return ret;
1384 break;
1385 default:
1386 return -EINVAL;
1388 pr_info("Channel measurement facility initialized using format "
1389 "%s (mode %s)\n", format_string, detect_string);
1390 return 0;
1392 module_init(init_cmf);
1395 MODULE_AUTHOR("Arnd Bergmann <arndb@de.ibm.com>");
1396 MODULE_LICENSE("GPL");
1397 MODULE_DESCRIPTION("channel measurement facility base driver\n"
1398 "Copyright IBM Corp. 2003\n");
1400 EXPORT_SYMBOL_GPL(enable_cmf);
1401 EXPORT_SYMBOL_GPL(disable_cmf);
1402 EXPORT_SYMBOL_GPL(cmf_read);
1403 EXPORT_SYMBOL_GPL(cmf_readall);