linux/drivers/s390/cio/cmf.c
<<
>>
Prefs
   1/*
   2 * Linux on zSeries Channel Measurement Facility support
   3 *
   4 * Copyright IBM Corp. 2000, 2006
   5 *
   6 * Authors: Arnd Bergmann <arndb@de.ibm.com>
   7 *          Cornelia Huck <cornelia.huck@de.ibm.com>
   8 *
   9 * original idea from Natarajan Krishnaswami <nkrishna@us.ibm.com>
  10 *
  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.
  15 *
  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.
  20 *
  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.
  24 */
  25
  26#define KMSG_COMPONENT "cio"
  27#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
  28
  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() */
  37
  38#include <asm/ccwdev.h>
  39#include <asm/cio.h>
  40#include <asm/cmb.h>
  41#include <asm/div64.h>
  42
  43#include "cio.h"
  44#include "css.h"
  45#include "device.h"
  46#include "ioasm.h"
  47#include "chsc.h"
  48
  49/*
  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
  56 */
  57#define ARGSTRING "s390cmf"
  58
  59/* indices for READCMB */
  60enum 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,
  72};
  73
  74/**
  75 * enum cmb_format - types of supported measurement block formats
  76 *
  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
  84 */
  85enum cmb_format {
  86        CMF_BASIC,
  87        CMF_EXTENDED,
  88        CMF_AUTODETECT = -1,
  89};
  90
  91/*
  92 * format - actual format for all measurement blocks
  93 *
  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.
  97 */
  98static int format = CMF_AUTODETECT;
  99module_param(format, bint, 0444);
 100
 101/**
 102 * struct cmb_operations - functions to use depending on cmb_format
 103 *
 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.
 107 *
 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
 116 * @align:      align an allocated block so that the hardware can use it
 117 */
 118struct cmb_operations {
 119        int  (*alloc)  (struct ccw_device *);
 120        void (*free)   (struct ccw_device *);
 121        int  (*set)    (struct ccw_device *, u32);
 122        u64  (*read)   (struct ccw_device *, int);
 123        int  (*readall)(struct ccw_device *, struct cmbdata *);
 124        void (*reset)  (struct ccw_device *);
 125        void *(*align) (void *);
 126/* private: */
 127        struct attribute_group *attr_group;
 128};
 129static struct cmb_operations *cmbops;
 130
 131struct cmb_data {
 132        void *hw_block;   /* Pointer to block updated by hardware */
 133        void *last_block; /* Last changed block copied from hardware block */
 134        int size;         /* Size of hw_block and last_block */
 135        unsigned long long last_update;  /* when last_block was updated */
 136};
 137
 138/*
 139 * Our user interface is designed in terms of nanoseconds,
 140 * while the hardware measures total times in its own
 141 * unit.
 142 */
 143static inline u64 time_to_nsec(u32 value)
 144{
 145        return ((u64)value) * 128000ull;
 146}
 147
 148/*
 149 * Users are usually interested in average times,
 150 * not accumulated time.
 151 * This also helps us with atomicity problems
 152 * when reading sinlge values.
 153 */
 154static inline u64 time_to_avg_nsec(u32 value, u32 count)
 155{
 156        u64 ret;
 157
 158        /* no samples yet, avoid division by 0 */
 159        if (count == 0)
 160                return 0;
 161
 162        /* value comes in units of 128 µsec */
 163        ret = time_to_nsec(value);
 164        do_div(ret, count);
 165
 166        return ret;
 167}
 168
 169/*
 170 * Activate or deactivate the channel monitor. When area is NULL,
 171 * the monitor is deactivated. The channel monitor needs to
 172 * be active in order to measure subchannels, which also need
 173 * to be enabled.
 174 */
 175static inline void cmf_activate(void *area, unsigned int onoff)
 176{
 177        register void * __gpr2 asm("2");
 178        register long __gpr1 asm("1");
 179
 180        __gpr2 = area;
 181        __gpr1 = onoff ? 2 : 0;
 182        /* activate channel measurement */
 183        asm("schm" : : "d" (__gpr2), "d" (__gpr1) );
 184}
 185
 186static int set_schib(struct ccw_device *cdev, u32 mme, int mbfc,
 187                     unsigned long address)
 188{
 189        struct subchannel *sch;
 190
 191        sch = to_subchannel(cdev->dev.parent);
 192
 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;
 200
 201        return cio_commit_config(sch);
 202}
 203
 204struct set_schib_struct {
 205        u32 mme;
 206        int mbfc;
 207        unsigned long address;
 208        wait_queue_head_t wait;
 209        int ret;
 210        struct kref kref;
 211};
 212
 213static void cmf_set_schib_release(struct kref *kref)
 214{
 215        struct set_schib_struct *set_data;
 216
 217        set_data = container_of(kref, struct set_schib_struct, kref);
 218        kfree(set_data);
 219}
 220
 221#define CMF_PENDING 1
 222#define SET_SCHIB_TIMEOUT (10 * HZ)
 223
 224static int set_schib_wait(struct ccw_device *cdev, u32 mme,
 225                                int mbfc, unsigned long address)
 226{
 227        struct set_schib_struct *set_data;
 228        int ret;
 229
 230        spin_lock_irq(cdev->ccwlock);
 231        if (!cdev->private->cmb) {
 232                ret = -ENODEV;
 233                goto out;
 234        }
 235        set_data = kzalloc(sizeof(struct set_schib_struct), GFP_ATOMIC);
 236        if (!set_data) {
 237                ret = -ENOMEM;
 238                goto out;
 239        }
 240        init_waitqueue_head(&set_data->wait);
 241        kref_init(&set_data->kref);
 242        set_data->mme = mme;
 243        set_data->mbfc = mbfc;
 244        set_data->address = address;
 245
 246        ret = set_schib(cdev, mme, mbfc, address);
 247        if (ret != -EBUSY)
 248                goto out_put;
 249
 250        if (cdev->private->state != DEV_STATE_ONLINE) {
 251                /* if the device is not online, don't even try again */
 252                ret = -EBUSY;
 253                goto out_put;
 254        }
 255
 256        cdev->private->state = DEV_STATE_CMFCHANGE;
 257        set_data->ret = CMF_PENDING;
 258        cdev->private->cmb_wait = set_data;
 259        spin_unlock_irq(cdev->ccwlock);
 260
 261        ret = wait_event_interruptible_timeout(set_data->wait,
 262                                               set_data->ret != CMF_PENDING,
 263                                               SET_SCHIB_TIMEOUT);
 264        spin_lock_irq(cdev->ccwlock);
 265        if (ret <= 0) {
 266                if (set_data->ret == CMF_PENDING) {
 267                        set_data->ret = (ret == 0) ? -ETIME : ret;
 268                        if (cdev->private->state == DEV_STATE_CMFCHANGE)
 269                                cdev->private->state = DEV_STATE_ONLINE;
 270                }
 271        }
 272        cdev->private->cmb_wait = NULL;
 273        ret = set_data->ret;
 274out_put:
 275        kref_put(&set_data->kref, cmf_set_schib_release);
 276out:
 277        spin_unlock_irq(cdev->ccwlock);
 278        return ret;
 279}
 280
 281void retry_set_schib(struct ccw_device *cdev)
 282{
 283        struct set_schib_struct *set_data;
 284
 285        set_data = cdev->private->cmb_wait;
 286        if (!set_data) {
 287                WARN_ON(1);
 288                return;
 289        }
 290        kref_get(&set_data->kref);
 291        set_data->ret = set_schib(cdev, set_data->mme, set_data->mbfc,
 292                                  set_data->address);
 293        wake_up(&set_data->wait);
 294        kref_put(&set_data->kref, cmf_set_schib_release);
 295}
 296
 297static int cmf_copy_block(struct ccw_device *cdev)
 298{
 299        struct subchannel *sch;
 300        void *reference_buf;
 301        void *hw_block;
 302        struct cmb_data *cmb_data;
 303
 304        sch = to_subchannel(cdev->dev.parent);
 305
 306        if (cio_update_schib(sch))
 307                return -ENODEV;
 308
 309        if (scsw_fctl(&sch->schib.scsw) & SCSW_FCTL_START_FUNC) {
 310                /* Don't copy if a start function is in progress. */
 311                if ((!(scsw_actl(&sch->schib.scsw) & SCSW_ACTL_SUSPENDED)) &&
 312                    (scsw_actl(&sch->schib.scsw) &
 313                     (SCSW_ACTL_DEVACT | SCSW_ACTL_SCHACT)) &&
 314                    (!(scsw_stctl(&sch->schib.scsw) & SCSW_STCTL_SEC_STATUS)))
 315                        return -EBUSY;
 316        }
 317        cmb_data = cdev->private->cmb;
 318        hw_block = cmbops->align(cmb_data->hw_block);
 319        if (!memcmp(cmb_data->last_block, hw_block, cmb_data->size))
 320                /* No need to copy. */
 321                return 0;
 322        reference_buf = kzalloc(cmb_data->size, GFP_ATOMIC);
 323        if (!reference_buf)
 324                return -ENOMEM;
 325        /* Ensure consistency of block copied from hardware. */
 326        do {
 327                memcpy(cmb_data->last_block, hw_block, cmb_data->size);
 328                memcpy(reference_buf, hw_block, cmb_data->size);
 329        } while (memcmp(cmb_data->last_block, reference_buf, cmb_data->size));
 330        cmb_data->last_update = get_tod_clock();
 331        kfree(reference_buf);
 332        return 0;
 333}
 334
 335struct copy_block_struct {
 336        wait_queue_head_t wait;
 337        int ret;
 338        struct kref kref;
 339};
 340
 341static void cmf_copy_block_release(struct kref *kref)
 342{
 343        struct copy_block_struct *copy_block;
 344
 345        copy_block = container_of(kref, struct copy_block_struct, kref);
 346        kfree(copy_block);
 347}
 348
 349static int cmf_cmb_copy_wait(struct ccw_device *cdev)
 350{
 351        struct copy_block_struct *copy_block;
 352        int ret;
 353        unsigned long flags;
 354
 355        spin_lock_irqsave(cdev->ccwlock, flags);
 356        if (!cdev->private->cmb) {
 357                ret = -ENODEV;
 358                goto out;
 359        }
 360        copy_block = kzalloc(sizeof(struct copy_block_struct), GFP_ATOMIC);
 361        if (!copy_block) {
 362                ret = -ENOMEM;
 363                goto out;
 364        }
 365        init_waitqueue_head(&copy_block->wait);
 366        kref_init(&copy_block->kref);
 367
 368        ret = cmf_copy_block(cdev);
 369        if (ret != -EBUSY)
 370                goto out_put;
 371
 372        if (cdev->private->state != DEV_STATE_ONLINE) {
 373                ret = -EBUSY;
 374                goto out_put;
 375        }
 376
 377        cdev->private->state = DEV_STATE_CMFUPDATE;
 378        copy_block->ret = CMF_PENDING;
 379        cdev->private->cmb_wait = copy_block;
 380
 381        spin_unlock_irqrestore(cdev->ccwlock, flags);
 382        if (wait_event_interruptible(copy_block->wait,
 383                                     copy_block->ret != CMF_PENDING)) {
 384                spin_lock_irqsave(cdev->ccwlock, flags);
 385                if (copy_block->ret == CMF_PENDING) {
 386                        copy_block->ret = -ERESTARTSYS;
 387                        if (cdev->private->state == DEV_STATE_CMFUPDATE)
 388                                cdev->private->state = DEV_STATE_ONLINE;
 389                }
 390                spin_unlock_irqrestore(cdev->ccwlock, flags);
 391        }
 392        spin_lock_irqsave(cdev->ccwlock, flags);
 393        cdev->private->cmb_wait = NULL;
 394        ret = copy_block->ret;
 395out_put:
 396        kref_put(&copy_block->kref, cmf_copy_block_release);
 397out:
 398        spin_unlock_irqrestore(cdev->ccwlock, flags);
 399        return ret;
 400}
 401
 402void cmf_retry_copy_block(struct ccw_device *cdev)
 403{
 404        struct copy_block_struct *copy_block;
 405
 406        copy_block = cdev->private->cmb_wait;
 407        if (!copy_block) {
 408                WARN_ON(1);
 409                return;
 410        }
 411        kref_get(&copy_block->kref);
 412        copy_block->ret = cmf_copy_block(cdev);
 413        wake_up(&copy_block->wait);
 414        kref_put(&copy_block->kref, cmf_copy_block_release);
 415}
 416
 417static void cmf_generic_reset(struct ccw_device *cdev)
 418{
 419        struct cmb_data *cmb_data;
 420
 421        spin_lock_irq(cdev->ccwlock);
 422        cmb_data = cdev->private->cmb;
 423        if (cmb_data) {
 424                memset(cmb_data->last_block, 0, cmb_data->size);
 425                /*
 426                 * Need to reset hw block as well to make the hardware start
 427                 * from 0 again.
 428                 */
 429                memset(cmbops->align(cmb_data->hw_block), 0, cmb_data->size);
 430                cmb_data->last_update = 0;
 431        }
 432        cdev->private->cmb_start_time = get_tod_clock();
 433        spin_unlock_irq(cdev->ccwlock);
 434}
 435
 436/**
 437 * struct cmb_area - container for global cmb data
 438 *
 439 * @mem:        pointer to CMBs (only in basic measurement mode)
 440 * @list:       contains a linked list of all subchannels
 441 * @num_channels: number of channels to be measured
 442 * @lock:       protect concurrent access to @mem and @list
 443 */
 444struct cmb_area {
 445        struct cmb *mem;
 446        struct list_head list;
 447        int num_channels;
 448        spinlock_t lock;
 449};
 450
 451static struct cmb_area cmb_area = {
 452        .lock = __SPIN_LOCK_UNLOCKED(cmb_area.lock),
 453        .list = LIST_HEAD_INIT(cmb_area.list),
 454        .num_channels  = 1024,
 455};
 456
 457/* ****** old style CMB handling ********/
 458
 459/*
 460 * Basic channel measurement blocks are allocated in one contiguous
 461 * block of memory, which can not be moved as long as any channel
 462 * is active. Therefore, a maximum number of subchannels needs to
 463 * be defined somewhere. This is a module parameter, defaulting to
 464 * a reasonable value of 1024, or 32 kb of memory.
 465 * Current kernels don't allow kmalloc with more than 128kb, so the
 466 * maximum is 4096.
 467 */
 468
 469module_param_named(maxchannels, cmb_area.num_channels, uint, 0444);
 470
 471/**
 472 * struct cmb - basic channel measurement block
 473 * @ssch_rsch_count: number of ssch and rsch
 474 * @sample_count: number of samples
 475 * @device_connect_time: time of device connect
 476 * @function_pending_time: time of function pending
 477 * @device_disconnect_time: time of device disconnect
 478 * @control_unit_queuing_time: time of control unit queuing
 479 * @device_active_only_time: time of device active only
 480 * @reserved: unused in basic measurement mode
 481 *
 482 * The measurement block as used by the hardware. The fields are described
 483 * further in z/Architecture Principles of Operation, chapter 17.
 484 *
 485 * The cmb area made up from these blocks must be a contiguous array and may
 486 * not be reallocated or freed.
 487 * Only one cmb area can be present in the system.
 488 */
 489struct cmb {
 490        u16 ssch_rsch_count;
 491        u16 sample_count;
 492        u32 device_connect_time;
 493        u32 function_pending_time;
 494        u32 device_disconnect_time;
 495        u32 control_unit_queuing_time;
 496        u32 device_active_only_time;
 497        u32 reserved[2];
 498};
 499
 500/*
 501 * Insert a single device into the cmb_area list.
 502 * Called with cmb_area.lock held from alloc_cmb.
 503 */
 504static int alloc_cmb_single(struct ccw_device *cdev,
 505                            struct cmb_data *cmb_data)
 506{
 507        struct cmb *cmb;
 508        struct ccw_device_private *node;
 509        int ret;
 510
 511        spin_lock_irq(cdev->ccwlock);
 512        if (!list_empty(&cdev->private->cmb_list)) {
 513                ret = -EBUSY;
 514                goto out;
 515        }
 516
 517        /*
 518         * Find first unused cmb in cmb_area.mem.
 519         * This is a little tricky: cmb_area.list
 520         * remains sorted by ->cmb->hw_data pointers.
 521         */
 522        cmb = cmb_area.mem;
 523        list_for_each_entry(node, &cmb_area.list, cmb_list) {
 524                struct cmb_data *data;
 525                data = node->cmb;
 526                if ((struct cmb*)data->hw_block > cmb)
 527                        break;
 528                cmb++;
 529        }
 530        if (cmb - cmb_area.mem >= cmb_area.num_channels) {
 531                ret = -ENOMEM;
 532                goto out;
 533        }
 534
 535        /* insert new cmb */
 536        list_add_tail(&cdev->private->cmb_list, &node->cmb_list);
 537        cmb_data->hw_block = cmb;
 538        cdev->private->cmb = cmb_data;
 539        ret = 0;
 540out:
 541        spin_unlock_irq(cdev->ccwlock);
 542        return ret;
 543}
 544
 545static int alloc_cmb(struct ccw_device *cdev)
 546{
 547        int ret;
 548        struct cmb *mem;
 549        ssize_t size;
 550        struct cmb_data *cmb_data;
 551
 552        /* Allocate private cmb_data. */
 553        cmb_data = kzalloc(sizeof(struct cmb_data), GFP_KERNEL);
 554        if (!cmb_data)
 555                return -ENOMEM;
 556
 557        cmb_data->last_block = kzalloc(sizeof(struct cmb), GFP_KERNEL);
 558        if (!cmb_data->last_block) {
 559                kfree(cmb_data);
 560                return -ENOMEM;
 561        }
 562        cmb_data->size = sizeof(struct cmb);
 563        spin_lock(&cmb_area.lock);
 564
 565        if (!cmb_area.mem) {
 566                /* there is no user yet, so we need a new area */
 567                size = sizeof(struct cmb) * cmb_area.num_channels;
 568                WARN_ON(!list_empty(&cmb_area.list));
 569
 570                spin_unlock(&cmb_area.lock);
 571                mem = (void*)__get_free_pages(GFP_KERNEL | GFP_DMA,
 572                                 get_order(size));
 573                spin_lock(&cmb_area.lock);
 574
 575                if (cmb_area.mem) {
 576                        /* ok, another thread was faster */
 577                        free_pages((unsigned long)mem, get_order(size));
 578                } else if (!mem) {
 579                        /* no luck */
 580                        ret = -ENOMEM;
 581                        goto out;
 582                } else {
 583                        /* everything ok */
 584                        memset(mem, 0, size);
 585                        cmb_area.mem = mem;
 586                        cmf_activate(cmb_area.mem, 1);
 587                }
 588        }
 589
 590        /* do the actual allocation */
 591        ret = alloc_cmb_single(cdev, cmb_data);
 592out:
 593        spin_unlock(&cmb_area.lock);
 594        if (ret) {
 595                kfree(cmb_data->last_block);
 596                kfree(cmb_data);
 597        }
 598        return ret;
 599}
 600
 601static void free_cmb(struct ccw_device *cdev)
 602{
 603        struct ccw_device_private *priv;
 604        struct cmb_data *cmb_data;
 605
 606        spin_lock(&cmb_area.lock);
 607        spin_lock_irq(cdev->ccwlock);
 608
 609        priv = cdev->private;
 610
 611        if (list_empty(&priv->cmb_list)) {
 612                /* already freed */
 613                goto out;
 614        }
 615
 616        cmb_data = priv->cmb;
 617        priv->cmb = NULL;
 618        if (cmb_data)
 619                kfree(cmb_data->last_block);
 620        kfree(cmb_data);
 621        list_del_init(&priv->cmb_list);
 622
 623        if (list_empty(&cmb_area.list)) {
 624                ssize_t size;
 625                size = sizeof(struct cmb) * cmb_area.num_channels;
 626                cmf_activate(NULL, 0);
 627                free_pages((unsigned long)cmb_area.mem, get_order(size));
 628                cmb_area.mem = NULL;
 629        }
 630out:
 631        spin_unlock_irq(cdev->ccwlock);
 632        spin_unlock(&cmb_area.lock);
 633}
 634
 635static int set_cmb(struct ccw_device *cdev, u32 mme)
 636{
 637        u16 offset;
 638        struct cmb_data *cmb_data;
 639        unsigned long flags;
 640
 641        spin_lock_irqsave(cdev->ccwlock, flags);
 642        if (!cdev->private->cmb) {
 643                spin_unlock_irqrestore(cdev->ccwlock, flags);
 644                return -EINVAL;
 645        }
 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);
 649
 650        return set_schib_wait(cdev, mme, 0, offset);
 651}
 652
 653static u64 read_cmb(struct ccw_device *cdev, int index)
 654{
 655        struct cmb *cmb;
 656        u32 val;
 657        int ret;
 658        unsigned long flags;
 659
 660        ret = cmf_cmb_copy_wait(cdev);
 661        if (ret < 0)
 662                return 0;
 663
 664        spin_lock_irqsave(cdev->ccwlock, flags);
 665        if (!cdev->private->cmb) {
 666                ret = 0;
 667                goto out;
 668        }
 669        cmb = ((struct cmb_data *)cdev->private->cmb)->last_block;
 670
 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;
 696        }
 697        ret = time_to_avg_nsec(val, cmb->sample_count);
 698out:
 699        spin_unlock_irqrestore(cdev->ccwlock, flags);
 700        return ret;
 701}
 702
 703static int readall_cmb(struct ccw_device *cdev, struct cmbdata *data)
 704{
 705        struct cmb *cmb;
 706        struct cmb_data *cmb_data;
 707        u64 time;
 708        unsigned long flags;
 709        int ret;
 710
 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;
 719        }
 720        if (cmb_data->last_update == 0) {
 721                ret = -EAGAIN;
 722                goto out;
 723        }
 724        cmb = cmb_data->last_block;
 725        time = cmb_data->last_update - cdev->private->cmb_start_time;
 726
 727        memset(data, 0, sizeof(struct cmbdata));
 728
 729        /* we only know values before device_busy_time */
 730        data->size = offsetof(struct cmbdata, device_busy_time);
 731
 732        /* convert to nanoseconds */
 733        data->elapsed_time = (time * 1000) >> 12;
 734
 735        /* copy data to new structure */
 736        data->ssch_rsch_count = cmb->ssch_rsch_count;
 737        data->sample_count = cmb->sample_count;
 738
 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;
 749out:
 750        spin_unlock_irqrestore(cdev->ccwlock, flags);
 751        return ret;
 752}
 753
 754static void reset_cmb(struct ccw_device *cdev)
 755{
 756        cmf_generic_reset(cdev);
 757}
 758
 759static void * align_cmb(void *area)
 760{
 761        return area;
 762}
 763
 764static struct attribute_group cmf_attr_group;
 765
 766static struct cmb_operations cmbops_basic = {
 767        .alloc  = alloc_cmb,
 768        .free   = free_cmb,
 769        .set    = set_cmb,
 770        .read   = read_cmb,
 771        .readall    = readall_cmb,
 772        .reset      = reset_cmb,
 773        .align      = align_cmb,
 774        .attr_group = &cmf_attr_group,
 775};
 776
 777/* ******** extended cmb handling ********/
 778
 779/**
 780 * struct cmbe - extended channel measurement block
 781 * @ssch_rsch_count: number of ssch and rsch
 782 * @sample_count: number of samples
 783 * @device_connect_time: time of device connect
 784 * @function_pending_time: time of function pending
 785 * @device_disconnect_time: time of device disconnect
 786 * @control_unit_queuing_time: time of control unit queuing
 787 * @device_active_only_time: time of device active only
 788 * @device_busy_time: time of device busy
 789 * @initial_command_response_time: initial command response time
 790 * @reserved: unused
 791 *
 792 * The measurement block as used by the hardware. May be in any 64 bit physical
 793 * location.
 794 * The fields are described further in z/Architecture Principles of Operation,
 795 * third edition, chapter 17.
 796 */
 797struct cmbe {
 798        u32 ssch_rsch_count;
 799        u32 sample_count;
 800        u32 device_connect_time;
 801        u32 function_pending_time;
 802        u32 device_disconnect_time;
 803        u32 control_unit_queuing_time;
 804        u32 device_active_only_time;
 805        u32 device_busy_time;
 806        u32 initial_command_response_time;
 807        u32 reserved[7];
 808};
 809
 810/*
 811 * kmalloc only guarantees 8 byte alignment, but we need cmbe
 812 * pointers to be naturally aligned. Make sure to allocate
 813 * enough space for two cmbes.
 814 */
 815static inline struct cmbe *cmbe_align(struct cmbe *c)
 816{
 817        unsigned long addr;
 818        addr = ((unsigned long)c + sizeof (struct cmbe) - sizeof(long)) &
 819                                 ~(sizeof (struct cmbe) - sizeof(long));
 820        return (struct cmbe*)addr;
 821}
 822
 823static int alloc_cmbe(struct ccw_device *cdev)
 824{
 825        struct cmbe *cmbe;
 826        struct cmb_data *cmb_data;
 827        int ret;
 828
 829        cmbe = kzalloc (sizeof (*cmbe) * 2, GFP_KERNEL);
 830        if (!cmbe)
 831                return -ENOMEM;
 832        cmb_data = kzalloc(sizeof(struct cmb_data), GFP_KERNEL);
 833        if (!cmb_data) {
 834                ret = -ENOMEM;
 835                goto out_free;
 836        }
 837        cmb_data->last_block = kzalloc(sizeof(struct cmbe), GFP_KERNEL);
 838        if (!cmb_data->last_block) {
 839                ret = -ENOMEM;
 840                goto out_free;
 841        }
 842        cmb_data->size = sizeof(struct cmbe);
 843        spin_lock_irq(cdev->ccwlock);
 844        if (cdev->private->cmb) {
 845                spin_unlock_irq(cdev->ccwlock);
 846                ret = -EBUSY;
 847                goto out_free;
 848        }
 849        cmb_data->hw_block = cmbe;
 850        cdev->private->cmb = cmb_data;
 851        spin_unlock_irq(cdev->ccwlock);
 852
 853        /* activate global measurement if this is the first channel */
 854        spin_lock(&cmb_area.lock);
 855        if (list_empty(&cmb_area.list))
 856                cmf_activate(NULL, 1);
 857        list_add_tail(&cdev->private->cmb_list, &cmb_area.list);
 858        spin_unlock(&cmb_area.lock);
 859
 860        return 0;
 861out_free:
 862        if (cmb_data)
 863                kfree(cmb_data->last_block);
 864        kfree(cmb_data);
 865        kfree(cmbe);
 866        return ret;
 867}
 868
 869static void free_cmbe(struct ccw_device *cdev)
 870{
 871        struct cmb_data *cmb_data;
 872
 873        spin_lock_irq(cdev->ccwlock);
 874        cmb_data = cdev->private->cmb;
 875        cdev->private->cmb = NULL;
 876        if (cmb_data)
 877                kfree(cmb_data->last_block);
 878        kfree(cmb_data);
 879        spin_unlock_irq(cdev->ccwlock);
 880
 881        /* deactivate global measurement if this is the last channel */
 882        spin_lock(&cmb_area.lock);
 883        list_del_init(&cdev->private->cmb_list);
 884        if (list_empty(&cmb_area.list))
 885                cmf_activate(NULL, 0);
 886        spin_unlock(&cmb_area.lock);
 887}
 888
 889static int set_cmbe(struct ccw_device *cdev, u32 mme)
 890{
 891        unsigned long mba;
 892        struct cmb_data *cmb_data;
 893        unsigned long flags;
 894
 895        spin_lock_irqsave(cdev->ccwlock, flags);
 896        if (!cdev->private->cmb) {
 897                spin_unlock_irqrestore(cdev->ccwlock, flags);
 898                return -EINVAL;
 899        }
 900        cmb_data = cdev->private->cmb;
 901        mba = mme ? (unsigned long) cmbe_align(cmb_data->hw_block) : 0;
 902        spin_unlock_irqrestore(cdev->ccwlock, flags);
 903
 904        return set_schib_wait(cdev, mme, 1, mba);
 905}
 906
 907
 908static u64 read_cmbe(struct ccw_device *cdev, int index)
 909{
 910        struct cmbe *cmb;
 911        struct cmb_data *cmb_data;
 912        u32 val;
 913        int ret;
 914        unsigned long flags;
 915
 916        ret = cmf_cmb_copy_wait(cdev);
 917        if (ret < 0)
 918                return 0;
 919
 920        spin_lock_irqsave(cdev->ccwlock, flags);
 921        cmb_data = cdev->private->cmb;
 922        if (!cmb_data) {
 923                ret = 0;
 924                goto out;
 925        }
 926        cmb = cmb_data->last_block;
 927
 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;
 959        }
 960        ret = time_to_avg_nsec(val, cmb->sample_count);
 961out:
 962        spin_unlock_irqrestore(cdev->ccwlock, flags);
 963        return ret;
 964}
 965
 966static int readall_cmbe(struct ccw_device *cdev, struct cmbdata *data)
 967{
 968        struct cmbe *cmb;
 969        struct cmb_data *cmb_data;
 970        u64 time;
 971        unsigned long flags;
 972        int ret;
 973
 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;
 982        }
 983        if (cmb_data->last_update == 0) {
 984                ret = -EAGAIN;
 985                goto out;
 986        }
 987        time = cmb_data->last_update - cdev->private->cmb_start_time;
 988
 989        memset (data, 0, sizeof(struct cmbdata));
 990
 991        /* we only know values before device_busy_time */
 992        data->size = offsetof(struct cmbdata, device_busy_time);
 993
 994        /* conver to nanoseconds */
 995        data->elapsed_time = (time * 1000) >> 12;
 996
 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;
1001
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);
1014
1015        ret = 0;
1016out:
1017        spin_unlock_irqrestore(cdev->ccwlock, flags);
1018        return ret;
1019}
1020
1021static void reset_cmbe(struct ccw_device *cdev)
1022{
1023        cmf_generic_reset(cdev);
1024}
1025
1026static void * align_cmbe(void *area)
1027{
1028        return cmbe_align(area);
1029}
1030
1031static struct attribute_group cmf_attr_group_ext;
1032
1033static struct cmb_operations cmbops_extended = {
1034        .alloc      = alloc_cmbe,
1035        .free       = free_cmbe,
1036        .set        = set_cmbe,
1037        .read       = read_cmbe,
1038        .readall    = readall_cmbe,
1039        .reset      = reset_cmbe,
1040        .align      = align_cmbe,
1041        .attr_group = &cmf_attr_group_ext,
1042};
1043
1044static ssize_t cmb_show_attr(struct device *dev, char *buf, enum cmb_index idx)
1045{
1046        return sprintf(buf, "%lld\n",
1047                (unsigned long long) cmf_read(to_ccwdev(dev), idx));
1048}
1049
1050static ssize_t cmb_show_avg_sample_interval(struct device *dev,
1051                                            struct device_attribute *attr,
1052                                            char *buf)
1053{
1054        struct ccw_device *cdev;
1055        long interval;
1056        unsigned long count;
1057        struct cmb_data *cmb_data;
1058
1059        cdev = to_ccwdev(dev);
1060        count = cmf_read(cdev, cmb_sample_count);
1061        spin_lock_irq(cdev->ccwlock);
1062        cmb_data = cdev->private->cmb;
1063        if (count) {
1064                interval = cmb_data->last_update -
1065                        cdev->private->cmb_start_time;
1066                interval = (interval * 1000) >> 12;
1067                interval /= count;
1068        } else
1069                interval = -1;
1070        spin_unlock_irq(cdev->ccwlock);
1071        return sprintf(buf, "%ld\n", interval);
1072}
1073
1074static ssize_t cmb_show_avg_utilization(struct device *dev,
1075                                        struct device_attribute *attr,
1076                                        char *buf)
1077{
1078        struct cmbdata data;
1079        u64 utilization;
1080        unsigned long t, u;
1081        int ret;
1082
1083        ret = cmf_readall(to_ccwdev(dev), &data);
1084        if (ret == -EAGAIN || ret == -ENODEV)
1085                /* No data (yet/currently) available to use for calculation. */
1086                return sprintf(buf, "n/a\n");
1087        else if (ret)
1088                return ret;
1089
1090        utilization = data.device_connect_time +
1091                      data.function_pending_time +
1092                      data.device_disconnect_time;
1093
1094        /* shift to avoid long long division */
1095        while (-1ul < (data.elapsed_time | utilization)) {
1096                utilization >>= 8;
1097                data.elapsed_time >>= 8;
1098        }
1099
1100        /* calculate value in 0.1 percent units */
1101        t = (unsigned long) data.elapsed_time / 1000;
1102        u = (unsigned long) utilization / t;
1103
1104        return sprintf(buf, "%02ld.%01ld%%\n", u/ 10, u - (u/ 10) * 10);
1105}
1106
1107#define cmf_attr(name) \
1108static ssize_t show_##name(struct device *dev, \
1109                           struct device_attribute *attr, char *buf)    \
1110{ return cmb_show_attr((dev), buf, cmb_##name); } \
1111static DEVICE_ATTR(name, 0444, show_##name, NULL);
1112
1113#define cmf_attr_avg(name) \
1114static ssize_t show_avg_##name(struct device *dev, \
1115                               struct device_attribute *attr, char *buf) \
1116{ return cmb_show_attr((dev), buf, cmb_##name); } \
1117static DEVICE_ATTR(avg_##name, 0444, show_avg_##name, NULL);
1118
1119cmf_attr(ssch_rsch_count);
1120cmf_attr(sample_count);
1121cmf_attr_avg(device_connect_time);
1122cmf_attr_avg(function_pending_time);
1123cmf_attr_avg(device_disconnect_time);
1124cmf_attr_avg(control_unit_queuing_time);
1125cmf_attr_avg(device_active_only_time);
1126cmf_attr_avg(device_busy_time);
1127cmf_attr_avg(initial_command_response_time);
1128
1129static DEVICE_ATTR(avg_sample_interval, 0444, cmb_show_avg_sample_interval,
1130                   NULL);
1131static DEVICE_ATTR(avg_utilization, 0444, cmb_show_avg_utilization, NULL);
1132
1133static struct attribute *cmf_attributes[] = {
1134        &dev_attr_avg_sample_interval.attr,
1135        &dev_attr_avg_utilization.attr,
1136        &dev_attr_ssch_rsch_count.attr,
1137        &dev_attr_sample_count.attr,
1138        &dev_attr_avg_device_connect_time.attr,
1139        &dev_attr_avg_function_pending_time.attr,
1140        &dev_attr_avg_device_disconnect_time.attr,
1141        &dev_attr_avg_control_unit_queuing_time.attr,
1142        &dev_attr_avg_device_active_only_time.attr,
1143        NULL,
1144};
1145
1146static struct attribute_group cmf_attr_group = {
1147        .name  = "cmf",
1148        .attrs = cmf_attributes,
1149};
1150
1151static struct attribute *cmf_attributes_ext[] = {
1152        &dev_attr_avg_sample_interval.attr,
1153        &dev_attr_avg_utilization.attr,
1154        &dev_attr_ssch_rsch_count.attr,
1155        &dev_attr_sample_count.attr,
1156        &dev_attr_avg_device_connect_time.attr,
1157        &dev_attr_avg_function_pending_time.attr,
1158        &dev_attr_avg_device_disconnect_time.attr,
1159        &dev_attr_avg_control_unit_queuing_time.attr,
1160        &dev_attr_avg_device_active_only_time.attr,
1161        &dev_attr_avg_device_busy_time.attr,
1162        &dev_attr_avg_initial_command_response_time.attr,
1163        NULL,
1164};
1165
1166static struct attribute_group cmf_attr_group_ext = {
1167        .name  = "cmf",
1168        .attrs = cmf_attributes_ext,
1169};
1170
1171static ssize_t cmb_enable_show(struct device *dev,
1172                               struct device_attribute *attr,
1173                               char *buf)
1174{
1175        return sprintf(buf, "%d\n", to_ccwdev(dev)->private->cmb ? 1 : 0);
1176}
1177
1178static ssize_t cmb_enable_store(struct device *dev,
1179                                struct device_attribute *attr, const char *buf,
1180                                size_t c)
1181{
1182        struct ccw_device *cdev;
1183        int ret;
1184        unsigned long val;
1185
1186        ret = strict_strtoul(buf, 16, &val);
1187        if (ret)
1188                return ret;
1189
1190        cdev = to_ccwdev(dev);
1191
1192        switch (val) {
1193        case 0:
1194                ret = disable_cmf(cdev);
1195                break;
1196        case 1:
1197                ret = enable_cmf(cdev);
1198                break;
1199        }
1200
1201        return c;
1202}
1203
1204DEVICE_ATTR(cmb_enable, 0644, cmb_enable_show, cmb_enable_store);
1205
1206int ccw_set_cmf(struct ccw_device *cdev, int enable)
1207{
1208        return cmbops->set(cdev, enable ? 2 : 0);
1209}
1210
1211/**
1212 * enable_cmf() - switch on the channel measurement for a specific device
1213 *  @cdev:      The ccw device to be enabled
1214 *
1215 *  Returns %0 for success or a negative error value.
1216 *
1217 *  Context:
1218 *    non-atomic
1219 */
1220int enable_cmf(struct ccw_device *cdev)
1221{
1222        int ret;
1223
1224        ret = cmbops->alloc(cdev);
1225        cmbops->reset(cdev);
1226        if (ret)
1227                return ret;
1228        ret = cmbops->set(cdev, 2);
1229        if (ret) {
1230                cmbops->free(cdev);
1231                return ret;
1232        }
1233        ret = sysfs_create_group(&cdev->dev.kobj, cmbops->attr_group);
1234        if (!ret)
1235                return 0;
1236        cmbops->set(cdev, 0);  //FIXME: this can fail
1237        cmbops->free(cdev);
1238        return ret;
1239}
1240
1241/**
1242 * disable_cmf() - switch off the channel measurement for a specific device
1243 *  @cdev:      The ccw device to be disabled
1244 *
1245 *  Returns %0 for success or a negative error value.
1246 *
1247 *  Context:
1248 *    non-atomic
1249 */
1250int disable_cmf(struct ccw_device *cdev)
1251{
1252        int ret;
1253
1254        ret = cmbops->set(cdev, 0);
1255        if (ret)
1256                return ret;
1257        cmbops->free(cdev);
1258        sysfs_remove_group(&cdev->dev.kobj, cmbops->attr_group);
1259        return ret;
1260}
1261
1262/**
1263 * cmf_read() - read one value from the current channel measurement block
1264 * @cdev:       the channel to be read
1265 * @index:      the index of the value to be read
1266 *
1267 * Returns the value read or %0 if the value cannot be read.
1268 *
1269 *  Context:
1270 *    any
1271 */
1272u64 cmf_read(struct ccw_device *cdev, int index)
1273{
1274        return cmbops->read(cdev, index);
1275}
1276
1277/**
1278 * cmf_readall() - read the current channel measurement block
1279 * @cdev:       the channel to be read
1280 * @data:       a pointer to a data block that will be filled
1281 *
1282 * Returns %0 on success, a negative error value otherwise.
1283 *
1284 *  Context:
1285 *    any
1286 */
1287int cmf_readall(struct ccw_device *cdev, struct cmbdata *data)
1288{
1289        return cmbops->readall(cdev, data);
1290}
1291
1292/* Reenable cmf when a disconnected device becomes available again. */
1293int cmf_reenable(struct ccw_device *cdev)
1294{
1295        cmbops->reset(cdev);
1296        return cmbops->set(cdev, 2);
1297}
1298
1299static int __init init_cmf(void)
1300{
1301        char *format_string;
1302        char *detect_string = "parameter";
1303
1304        /*
1305         * If the user did not give a parameter, see if we are running on a
1306         * machine supporting extended measurement blocks, otherwise fall back
1307         * to basic mode.
1308         */
1309        if (format == CMF_AUTODETECT) {
1310                if (!css_general_characteristics.ext_mb) {
1311                        format = CMF_BASIC;
1312                } else {
1313                        format = CMF_EXTENDED;
1314                }
1315                detect_string = "autodetected";
1316        } else {
1317                detect_string = "parameter";
1318        }
1319
1320        switch (format) {
1321        case CMF_BASIC:
1322                format_string = "basic";
1323                cmbops = &cmbops_basic;
1324                break;
1325        case CMF_EXTENDED:
1326                format_string = "extended";
1327                cmbops = &cmbops_extended;
1328                break;
1329        default:
1330                return 1;
1331        }
1332        pr_info("Channel measurement facility initialized using format "
1333                "%s (mode %s)\n", format_string, detect_string);
1334        return 0;
1335}
1336
1337module_init(init_cmf);
1338
1339
1340MODULE_AUTHOR("Arnd Bergmann <arndb@de.ibm.com>");
1341MODULE_LICENSE("GPL");
1342MODULE_DESCRIPTION("channel measurement facility base driver\n"
1343                   "Copyright IBM Corp. 2003\n");
1344
1345EXPORT_SYMBOL_GPL(enable_cmf);
1346EXPORT_SYMBOL_GPL(disable_cmf);
1347EXPORT_SYMBOL_GPL(cmf_read);
1348EXPORT_SYMBOL_GPL(cmf_readall);
1349