cmf.c 33.2 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3
/*
 * Linux on zSeries Channel Measurement Facility support
 *
4
 * Copyright IBM Corp. 2000, 2006
L
Linus Torvalds 已提交
5
 *
6 7
 * Authors: Arnd Bergmann <arndb@de.ibm.com>
 *	    Cornelia Huck <cornelia.huck@de.ibm.com>
L
Linus Torvalds 已提交
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 *
 * original idea from Natarajan Krishnaswami <nkrishna@us.ibm.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

26 27 28
#define KMSG_COMPONENT "cio"
#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt

L
Linus Torvalds 已提交
29 30 31 32 33 34
#include <linux/bootmem.h>
#include <linux/device.h>
#include <linux/init.h>
#include <linux/list.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
T
Tim Schmielau 已提交
35 36
#include <linux/slab.h>
#include <linux/timex.h>	/* get_clock() */
L
Linus Torvalds 已提交
37 38 39 40

#include <asm/ccwdev.h>
#include <asm/cio.h>
#include <asm/cmb.h>
T
Tim Schmielau 已提交
41
#include <asm/div64.h>
L
Linus Torvalds 已提交
42 43 44 45 46 47 48

#include "cio.h"
#include "css.h"
#include "device.h"
#include "ioasm.h"
#include "chsc.h"

49 50
/*
 * parameter to enable cmf during boot, possible uses are:
L
Linus Torvalds 已提交
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
 *  "s390cmf" -- enable cmf and allocate 2 MB of ram so measuring can be
 *               used on any subchannel
 *  "s390cmf=<num>" -- enable cmf and allocate enough memory to measure
 *                     <num> subchannel, where <num> is an integer
 *                     between 1 and 65535, default is 1024
 */
#define ARGSTRING "s390cmf"

/* indices for READCMB */
enum cmb_index {
 /* basic and exended format: */
	cmb_ssch_rsch_count,
	cmb_sample_count,
	cmb_device_connect_time,
	cmb_function_pending_time,
	cmb_device_disconnect_time,
	cmb_control_unit_queuing_time,
	cmb_device_active_only_time,
 /* extended format only: */
	cmb_device_busy_time,
	cmb_initial_command_response_time,
};

/**
 * enum cmb_format - types of supported measurement block formats
 *
 * @CMF_BASIC:      traditional channel measurement blocks supported
78
 *		    by all machines that we run on
L
Linus Torvalds 已提交
79
 * @CMF_EXTENDED:   improved format that was introduced with the z990
80 81 82 83 84
 *		    machine
 * @CMF_AUTODETECT: default: use extended format when running on a machine
 *		    supporting extended format, otherwise fall back to
 *		    basic format
 */
L
Linus Torvalds 已提交
85 86 87 88 89
enum cmb_format {
	CMF_BASIC,
	CMF_EXTENDED,
	CMF_AUTODETECT = -1,
};
90

91
/*
L
Linus Torvalds 已提交
92 93 94 95 96 97 98
 * format - actual format for all measurement blocks
 *
 * The format module parameter can be set to a value of 0 (zero)
 * or 1, indicating basic or extended format as described for
 * enum cmb_format.
 */
static int format = CMF_AUTODETECT;
99
module_param(format, bint, 0444);
L
Linus Torvalds 已提交
100 101 102 103

/**
 * struct cmb_operations - functions to use depending on cmb_format
 *
104 105 106
 * Most of these functions operate on a struct ccw_device. There is only
 * one instance of struct cmb_operations because the format of the measurement
 * data is guaranteed to be the same for every ccw_device.
L
Linus Torvalds 已提交
107 108 109 110 111
 *
 * @alloc:	allocate memory for a channel measurement block,
 *		either with the help of a special pool or with kmalloc
 * @free:	free memory allocated with @alloc
 * @set:	enable or disable measurement
112
 * @read:	read a measurement entry at an index
L
Linus Torvalds 已提交
113 114 115
 * @readall:	read a measurement block in a common format
 * @reset:	clear the data in the associated measurement block and
 *		reset its time stamp
116
 * @align:	align an allocated block so that the hardware can use it
L
Linus Torvalds 已提交
117 118
 */
struct cmb_operations {
119 120 121 122 123 124 125
	int  (*alloc)  (struct ccw_device *);
	void (*free)   (struct ccw_device *);
	int  (*set)    (struct ccw_device *, u32);
	u64  (*read)   (struct ccw_device *, int);
	int  (*readall)(struct ccw_device *, struct cmbdata *);
	void (*reset)  (struct ccw_device *);
	void *(*align) (void *);
126
/* private: */
L
Linus Torvalds 已提交
127 128 129 130
	struct attribute_group *attr_group;
};
static struct cmb_operations *cmbops;

131 132 133 134 135 136 137
struct cmb_data {
	void *hw_block;   /* Pointer to block updated by hardware */
	void *last_block; /* Last changed block copied from hardware block */
	int size;	  /* Size of hw_block and last_block */
	unsigned long long last_update;  /* when last_block was updated */
};

138 139
/*
 * Our user interface is designed in terms of nanoseconds,
L
Linus Torvalds 已提交
140
 * while the hardware measures total times in its own
141 142
 * unit.
 */
L
Linus Torvalds 已提交
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
static inline u64 time_to_nsec(u32 value)
{
	return ((u64)value) * 128000ull;
}

/*
 * Users are usually interested in average times,
 * not accumulated time.
 * This also helps us with atomicity problems
 * when reading sinlge values.
 */
static inline u64 time_to_avg_nsec(u32 value, u32 count)
{
	u64 ret;

	/* no samples yet, avoid division by 0 */
	if (count == 0)
		return 0;

162
	/* value comes in units of 128 µsec */
L
Linus Torvalds 已提交
163 164 165 166 167 168
	ret = time_to_nsec(value);
	do_div(ret, count);

	return ret;
}

169 170
/*
 * Activate or deactivate the channel monitor. When area is NULL,
L
Linus Torvalds 已提交
171 172
 * the monitor is deactivated. The channel monitor needs to
 * be active in order to measure subchannels, which also need
173 174 175
 * to be enabled.
 */
static inline void cmf_activate(void *area, unsigned int onoff)
L
Linus Torvalds 已提交
176 177 178 179 180 181 182 183 184 185
{
	register void * __gpr2 asm("2");
	register long __gpr1 asm("1");

	__gpr2 = area;
	__gpr1 = onoff ? 2 : 0;
	/* activate channel measurement */
	asm("schm" : : "d" (__gpr2), "d" (__gpr1) );
}

186 187
static int set_schib(struct ccw_device *cdev, u32 mme, int mbfc,
		     unsigned long address)
L
Linus Torvalds 已提交
188 189 190 191 192
{
	struct subchannel *sch;

	sch = to_subchannel(cdev->dev.parent);

193 194 195 196 197 198 199
	sch->config.mme = mme;
	sch->config.mbfc = mbfc;
	/* address can be either a block address or a block index */
	if (mbfc)
		sch->config.mba = address;
	else
		sch->config.mbi = address;
L
Linus Torvalds 已提交
200

201
	return cio_commit_config(sch);
L
Linus Torvalds 已提交
202 203 204 205 206 207 208 209
}

struct set_schib_struct {
	u32 mme;
	int mbfc;
	unsigned long address;
	wait_queue_head_t wait;
	int ret;
210
	struct kref kref;
L
Linus Torvalds 已提交
211 212
};

213 214 215 216 217 218 219 220 221 222
static void cmf_set_schib_release(struct kref *kref)
{
	struct set_schib_struct *set_data;

	set_data = container_of(kref, struct set_schib_struct, kref);
	kfree(set_data);
}

#define CMF_PENDING 1

L
Linus Torvalds 已提交
223 224 225
static int set_schib_wait(struct ccw_device *cdev, u32 mme,
				int mbfc, unsigned long address)
{
226 227
	struct set_schib_struct *set_data;
	int ret;
L
Linus Torvalds 已提交
228 229

	spin_lock_irq(cdev->ccwlock);
230 231 232
	if (!cdev->private->cmb) {
		ret = -ENODEV;
		goto out;
L
Linus Torvalds 已提交
233
	}
234 235 236 237 238 239 240 241 242 243 244 245 246 247
	set_data = kzalloc(sizeof(struct set_schib_struct), GFP_ATOMIC);
	if (!set_data) {
		ret = -ENOMEM;
		goto out;
	}
	init_waitqueue_head(&set_data->wait);
	kref_init(&set_data->kref);
	set_data->mme = mme;
	set_data->mbfc = mbfc;
	set_data->address = address;

	ret = set_schib(cdev, mme, mbfc, address);
	if (ret != -EBUSY)
		goto out_put;
L
Linus Torvalds 已提交
248 249 250

	if (cdev->private->state != DEV_STATE_ONLINE) {
		/* if the device is not online, don't even try again */
251 252
		ret = -EBUSY;
		goto out_put;
L
Linus Torvalds 已提交
253
	}
254

L
Linus Torvalds 已提交
255
	cdev->private->state = DEV_STATE_CMFCHANGE;
256 257
	set_data->ret = CMF_PENDING;
	cdev->private->cmb_wait = set_data;
L
Linus Torvalds 已提交
258 259

	spin_unlock_irq(cdev->ccwlock);
260 261
	if (wait_event_interruptible(set_data->wait,
				     set_data->ret != CMF_PENDING)) {
L
Linus Torvalds 已提交
262
		spin_lock_irq(cdev->ccwlock);
263 264
		if (set_data->ret == CMF_PENDING) {
			set_data->ret = -ERESTARTSYS;
L
Linus Torvalds 已提交
265 266 267 268 269
			if (cdev->private->state == DEV_STATE_CMFCHANGE)
				cdev->private->state = DEV_STATE_ONLINE;
		}
		spin_unlock_irq(cdev->ccwlock);
	}
270 271 272 273 274 275
	spin_lock_irq(cdev->ccwlock);
	cdev->private->cmb_wait = NULL;
	ret = set_data->ret;
out_put:
	kref_put(&set_data->kref, cmf_set_schib_release);
out:
L
Linus Torvalds 已提交
276
	spin_unlock_irq(cdev->ccwlock);
277
	return ret;
L
Linus Torvalds 已提交
278 279 280 281
}

void retry_set_schib(struct ccw_device *cdev)
{
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
	struct set_schib_struct *set_data;

	set_data = cdev->private->cmb_wait;
	if (!set_data) {
		WARN_ON(1);
		return;
	}
	kref_get(&set_data->kref);
	set_data->ret = set_schib(cdev, set_data->mme, set_data->mbfc,
				  set_data->address);
	wake_up(&set_data->wait);
	kref_put(&set_data->kref, cmf_set_schib_release);
}

static int cmf_copy_block(struct ccw_device *cdev)
{
	struct subchannel *sch;
	void *reference_buf;
	void *hw_block;
	struct cmb_data *cmb_data;

	sch = to_subchannel(cdev->dev.parent);

305
	if (cio_update_schib(sch))
306 307
		return -ENODEV;

308
	if (scsw_fctl(&sch->schib.scsw) & SCSW_FCTL_START_FUNC) {
309
		/* Don't copy if a start function is in progress. */
310 311
		if ((!(scsw_actl(&sch->schib.scsw) & SCSW_ACTL_SUSPENDED)) &&
		    (scsw_actl(&sch->schib.scsw) &
312
		     (SCSW_ACTL_DEVACT | SCSW_ACTL_SCHACT)) &&
313
		    (!(scsw_stctl(&sch->schib.scsw) & SCSW_STCTL_SEC_STATUS)))
314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
			return -EBUSY;
	}
	cmb_data = cdev->private->cmb;
	hw_block = cmbops->align(cmb_data->hw_block);
	if (!memcmp(cmb_data->last_block, hw_block, cmb_data->size))
		/* No need to copy. */
		return 0;
	reference_buf = kzalloc(cmb_data->size, GFP_ATOMIC);
	if (!reference_buf)
		return -ENOMEM;
	/* Ensure consistency of block copied from hardware. */
	do {
		memcpy(cmb_data->last_block, hw_block, cmb_data->size);
		memcpy(reference_buf, hw_block, cmb_data->size);
	} while (memcmp(cmb_data->last_block, reference_buf, cmb_data->size));
	cmb_data->last_update = get_clock();
	kfree(reference_buf);
	return 0;
}

struct copy_block_struct {
	wait_queue_head_t wait;
	int ret;
	struct kref kref;
};

static void cmf_copy_block_release(struct kref *kref)
{
	struct copy_block_struct *copy_block;

	copy_block = container_of(kref, struct copy_block_struct, kref);
	kfree(copy_block);
}

static int cmf_cmb_copy_wait(struct ccw_device *cdev)
{
	struct copy_block_struct *copy_block;
	int ret;
	unsigned long flags;

	spin_lock_irqsave(cdev->ccwlock, flags);
	if (!cdev->private->cmb) {
		ret = -ENODEV;
		goto out;
	}
	copy_block = kzalloc(sizeof(struct copy_block_struct), GFP_ATOMIC);
	if (!copy_block) {
		ret = -ENOMEM;
		goto out;
	}
	init_waitqueue_head(&copy_block->wait);
	kref_init(&copy_block->kref);

	ret = cmf_copy_block(cdev);
	if (ret != -EBUSY)
		goto out_put;

	if (cdev->private->state != DEV_STATE_ONLINE) {
		ret = -EBUSY;
		goto out_put;
	}

	cdev->private->state = DEV_STATE_CMFUPDATE;
	copy_block->ret = CMF_PENDING;
	cdev->private->cmb_wait = copy_block;

	spin_unlock_irqrestore(cdev->ccwlock, flags);
	if (wait_event_interruptible(copy_block->wait,
				     copy_block->ret != CMF_PENDING)) {
		spin_lock_irqsave(cdev->ccwlock, flags);
		if (copy_block->ret == CMF_PENDING) {
			copy_block->ret = -ERESTARTSYS;
			if (cdev->private->state == DEV_STATE_CMFUPDATE)
				cdev->private->state = DEV_STATE_ONLINE;
		}
		spin_unlock_irqrestore(cdev->ccwlock, flags);
	}
	spin_lock_irqsave(cdev->ccwlock, flags);
	cdev->private->cmb_wait = NULL;
	ret = copy_block->ret;
out_put:
	kref_put(&copy_block->kref, cmf_copy_block_release);
out:
	spin_unlock_irqrestore(cdev->ccwlock, flags);
	return ret;
}

void cmf_retry_copy_block(struct ccw_device *cdev)
{
	struct copy_block_struct *copy_block;
L
Linus Torvalds 已提交
404

405 406
	copy_block = cdev->private->cmb_wait;
	if (!copy_block) {
L
Linus Torvalds 已提交
407 408 409
		WARN_ON(1);
		return;
	}
410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
	kref_get(&copy_block->kref);
	copy_block->ret = cmf_copy_block(cdev);
	wake_up(&copy_block->wait);
	kref_put(&copy_block->kref, cmf_copy_block_release);
}

static void cmf_generic_reset(struct ccw_device *cdev)
{
	struct cmb_data *cmb_data;

	spin_lock_irq(cdev->ccwlock);
	cmb_data = cdev->private->cmb;
	if (cmb_data) {
		memset(cmb_data->last_block, 0, cmb_data->size);
		/*
		 * Need to reset hw block as well to make the hardware start
		 * from 0 again.
		 */
		memset(cmbops->align(cmb_data->hw_block), 0, cmb_data->size);
		cmb_data->last_update = 0;
	}
	cdev->private->cmb_start_time = get_clock();
	spin_unlock_irq(cdev->ccwlock);
L
Linus Torvalds 已提交
433 434 435 436 437 438 439
}

/**
 * struct cmb_area - container for global cmb data
 *
 * @mem:	pointer to CMBs (only in basic measurement mode)
 * @list:	contains a linked list of all subchannels
440
 * @num_channels: number of channels to be measured
L
Linus Torvalds 已提交
441 442 443 444 445 446 447 448 449 450
 * @lock:	protect concurrent access to @mem and @list
 */
struct cmb_area {
	struct cmb *mem;
	struct list_head list;
	int num_channels;
	spinlock_t lock;
};

static struct cmb_area cmb_area = {
451
	.lock = __SPIN_LOCK_UNLOCKED(cmb_area.lock),
L
Linus Torvalds 已提交
452 453 454 455 456 457
	.list = LIST_HEAD_INIT(cmb_area.list),
	.num_channels  = 1024,
};

/* ****** old style CMB handling ********/

458
/*
L
Linus Torvalds 已提交
459 460 461 462
 * Basic channel measurement blocks are allocated in one contiguous
 * block of memory, which can not be moved as long as any channel
 * is active. Therefore, a maximum number of subchannels needs to
 * be defined somewhere. This is a module parameter, defaulting to
463
 * a reasonable value of 1024, or 32 kb of memory.
L
Linus Torvalds 已提交
464
 * Current kernels don't allow kmalloc with more than 128kb, so the
465
 * maximum is 4096.
L
Linus Torvalds 已提交
466 467 468 469 470 471
 */

module_param_named(maxchannels, cmb_area.num_channels, uint, 0444);

/**
 * struct cmb - basic channel measurement block
472 473 474 475 476 477 478 479 480 481 482
 * @ssch_rsch_count: number of ssch and rsch
 * @sample_count: number of samples
 * @device_connect_time: time of device connect
 * @function_pending_time: time of function pending
 * @device_disconnect_time: time of device disconnect
 * @control_unit_queuing_time: time of control unit queuing
 * @device_active_only_time: time of device active only
 * @reserved: unused in basic measurement mode
 *
 * The measurement block as used by the hardware. The fields are described
 * further in z/Architecture Principles of Operation, chapter 17.
L
Linus Torvalds 已提交
483
 *
484 485
 * The cmb area made up from these blocks must be a contiguous array and may
 * not be reallocated or freed.
L
Linus Torvalds 已提交
486 487 488 489 490 491 492 493 494 495 496 497 498
 * Only one cmb area can be present in the system.
 */
struct cmb {
	u16 ssch_rsch_count;
	u16 sample_count;
	u32 device_connect_time;
	u32 function_pending_time;
	u32 device_disconnect_time;
	u32 control_unit_queuing_time;
	u32 device_active_only_time;
	u32 reserved[2];
};

499 500 501
/*
 * Insert a single device into the cmb_area list.
 * Called with cmb_area.lock held from alloc_cmb.
L
Linus Torvalds 已提交
502
 */
503 504
static int alloc_cmb_single(struct ccw_device *cdev,
			    struct cmb_data *cmb_data)
L
Linus Torvalds 已提交
505 506 507 508 509 510 511 512 513 514 515
{
	struct cmb *cmb;
	struct ccw_device_private *node;
	int ret;

	spin_lock_irq(cdev->ccwlock);
	if (!list_empty(&cdev->private->cmb_list)) {
		ret = -EBUSY;
		goto out;
	}

516 517 518 519 520
	/*
	 * Find first unused cmb in cmb_area.mem.
	 * This is a little tricky: cmb_area.list
	 * remains sorted by ->cmb->hw_data pointers.
	 */
L
Linus Torvalds 已提交
521 522
	cmb = cmb_area.mem;
	list_for_each_entry(node, &cmb_area.list, cmb_list) {
523 524 525
		struct cmb_data *data;
		data = node->cmb;
		if ((struct cmb*)data->hw_block > cmb)
L
Linus Torvalds 已提交
526 527 528 529 530 531 532 533 534 535
			break;
		cmb++;
	}
	if (cmb - cmb_area.mem >= cmb_area.num_channels) {
		ret = -ENOMEM;
		goto out;
	}

	/* insert new cmb */
	list_add_tail(&cdev->private->cmb_list, &node->cmb_list);
536 537
	cmb_data->hw_block = cmb;
	cdev->private->cmb = cmb_data;
L
Linus Torvalds 已提交
538 539 540 541 542 543
	ret = 0;
out:
	spin_unlock_irq(cdev->ccwlock);
	return ret;
}

544
static int alloc_cmb(struct ccw_device *cdev)
L
Linus Torvalds 已提交
545 546 547 548
{
	int ret;
	struct cmb *mem;
	ssize_t size;
549 550 551 552 553 554
	struct cmb_data *cmb_data;

	/* Allocate private cmb_data. */
	cmb_data = kzalloc(sizeof(struct cmb_data), GFP_KERNEL);
	if (!cmb_data)
		return -ENOMEM;
L
Linus Torvalds 已提交
555

556 557 558 559 560 561
	cmb_data->last_block = kzalloc(sizeof(struct cmb), GFP_KERNEL);
	if (!cmb_data->last_block) {
		kfree(cmb_data);
		return -ENOMEM;
	}
	cmb_data->size = sizeof(struct cmb);
L
Linus Torvalds 已提交
562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589
	spin_lock(&cmb_area.lock);

	if (!cmb_area.mem) {
		/* there is no user yet, so we need a new area */
		size = sizeof(struct cmb) * cmb_area.num_channels;
		WARN_ON(!list_empty(&cmb_area.list));

		spin_unlock(&cmb_area.lock);
		mem = (void*)__get_free_pages(GFP_KERNEL | GFP_DMA,
				 get_order(size));
		spin_lock(&cmb_area.lock);

		if (cmb_area.mem) {
			/* ok, another thread was faster */
			free_pages((unsigned long)mem, get_order(size));
		} else if (!mem) {
			/* no luck */
			ret = -ENOMEM;
			goto out;
		} else {
			/* everything ok */
			memset(mem, 0, size);
			cmb_area.mem = mem;
			cmf_activate(cmb_area.mem, 1);
		}
	}

	/* do the actual allocation */
590
	ret = alloc_cmb_single(cdev, cmb_data);
L
Linus Torvalds 已提交
591 592
out:
	spin_unlock(&cmb_area.lock);
593 594 595 596
	if (ret) {
		kfree(cmb_data->last_block);
		kfree(cmb_data);
	}
L
Linus Torvalds 已提交
597 598 599
	return ret;
}

600
static void free_cmb(struct ccw_device *cdev)
L
Linus Torvalds 已提交
601 602
{
	struct ccw_device_private *priv;
603
	struct cmb_data *cmb_data;
L
Linus Torvalds 已提交
604 605 606 607

	spin_lock(&cmb_area.lock);
	spin_lock_irq(cdev->ccwlock);

608 609
	priv = cdev->private;

L
Linus Torvalds 已提交
610 611 612 613 614
	if (list_empty(&priv->cmb_list)) {
		/* already freed */
		goto out;
	}

615
	cmb_data = priv->cmb;
L
Linus Torvalds 已提交
616
	priv->cmb = NULL;
617 618 619
	if (cmb_data)
		kfree(cmb_data->last_block);
	kfree(cmb_data);
L
Linus Torvalds 已提交
620 621 622 623 624 625 626 627 628 629 630 631 632 633
	list_del_init(&priv->cmb_list);

	if (list_empty(&cmb_area.list)) {
		ssize_t size;
		size = sizeof(struct cmb) * cmb_area.num_channels;
		cmf_activate(NULL, 0);
		free_pages((unsigned long)cmb_area.mem, get_order(size));
		cmb_area.mem = NULL;
	}
out:
	spin_unlock_irq(cdev->ccwlock);
	spin_unlock(&cmb_area.lock);
}

634
static int set_cmb(struct ccw_device *cdev, u32 mme)
L
Linus Torvalds 已提交
635 636
{
	u16 offset;
637 638
	struct cmb_data *cmb_data;
	unsigned long flags;
L
Linus Torvalds 已提交
639

640 641 642
	spin_lock_irqsave(cdev->ccwlock, flags);
	if (!cdev->private->cmb) {
		spin_unlock_irqrestore(cdev->ccwlock, flags);
L
Linus Torvalds 已提交
643
		return -EINVAL;
644 645 646 647
	}
	cmb_data = cdev->private->cmb;
	offset = mme ? (struct cmb *)cmb_data->hw_block - cmb_area.mem : 0;
	spin_unlock_irqrestore(cdev->ccwlock, flags);
L
Linus Torvalds 已提交
648 649 650 651

	return set_schib_wait(cdev, mme, 0, offset);
}

652
static u64 read_cmb(struct ccw_device *cdev, int index)
L
Linus Torvalds 已提交
653
{
654
	struct cmb *cmb;
L
Linus Torvalds 已提交
655
	u32 val;
656 657 658 659 660 661
	int ret;
	unsigned long flags;

	ret = cmf_cmb_copy_wait(cdev);
	if (ret < 0)
		return 0;
L
Linus Torvalds 已提交
662 663 664

	spin_lock_irqsave(cdev->ccwlock, flags);
	if (!cdev->private->cmb) {
665 666
		ret = 0;
		goto out;
L
Linus Torvalds 已提交
667
	}
668
	cmb = ((struct cmb_data *)cdev->private->cmb)->last_block;
L
Linus Torvalds 已提交
669 670 671

	switch (index) {
	case cmb_ssch_rsch_count:
672 673
		ret = cmb->ssch_rsch_count;
		goto out;
L
Linus Torvalds 已提交
674
	case cmb_sample_count:
675 676
		ret = cmb->sample_count;
		goto out;
L
Linus Torvalds 已提交
677
	case cmb_device_connect_time:
678
		val = cmb->device_connect_time;
L
Linus Torvalds 已提交
679 680
		break;
	case cmb_function_pending_time:
681
		val = cmb->function_pending_time;
L
Linus Torvalds 已提交
682 683
		break;
	case cmb_device_disconnect_time:
684
		val = cmb->device_disconnect_time;
L
Linus Torvalds 已提交
685 686
		break;
	case cmb_control_unit_queuing_time:
687
		val = cmb->control_unit_queuing_time;
L
Linus Torvalds 已提交
688 689
		break;
	case cmb_device_active_only_time:
690
		val = cmb->device_active_only_time;
L
Linus Torvalds 已提交
691 692
		break;
	default:
693 694
		ret = 0;
		goto out;
L
Linus Torvalds 已提交
695
	}
696 697 698 699
	ret = time_to_avg_nsec(val, cmb->sample_count);
out:
	spin_unlock_irqrestore(cdev->ccwlock, flags);
	return ret;
L
Linus Torvalds 已提交
700 701
}

702
static int readall_cmb(struct ccw_device *cdev, struct cmbdata *data)
L
Linus Torvalds 已提交
703
{
704 705
	struct cmb *cmb;
	struct cmb_data *cmb_data;
L
Linus Torvalds 已提交
706
	u64 time;
707 708
	unsigned long flags;
	int ret;
L
Linus Torvalds 已提交
709

710 711 712
	ret = cmf_cmb_copy_wait(cdev);
	if (ret < 0)
		return ret;
L
Linus Torvalds 已提交
713
	spin_lock_irqsave(cdev->ccwlock, flags);
714 715 716 717
	cmb_data = cdev->private->cmb;
	if (!cmb_data) {
		ret = -ENODEV;
		goto out;
L
Linus Torvalds 已提交
718
	}
719 720 721 722 723 724
	if (cmb_data->last_update == 0) {
		ret = -EAGAIN;
		goto out;
	}
	cmb = cmb_data->last_block;
	time = cmb_data->last_update - cdev->private->cmb_start_time;
L
Linus Torvalds 已提交
725 726 727 728 729 730 731 732 733 734

	memset(data, 0, sizeof(struct cmbdata));

	/* we only know values before device_busy_time */
	data->size = offsetof(struct cmbdata, device_busy_time);

	/* convert to nanoseconds */
	data->elapsed_time = (time * 1000) >> 12;

	/* copy data to new structure */
735 736
	data->ssch_rsch_count = cmb->ssch_rsch_count;
	data->sample_count = cmb->sample_count;
L
Linus Torvalds 已提交
737 738

	/* time fields are converted to nanoseconds while copying */
739 740 741 742
	data->device_connect_time = time_to_nsec(cmb->device_connect_time);
	data->function_pending_time = time_to_nsec(cmb->function_pending_time);
	data->device_disconnect_time =
		time_to_nsec(cmb->device_disconnect_time);
L
Linus Torvalds 已提交
743
	data->control_unit_queuing_time
744
		= time_to_nsec(cmb->control_unit_queuing_time);
L
Linus Torvalds 已提交
745
	data->device_active_only_time
746 747 748 749 750 751
		= time_to_nsec(cmb->device_active_only_time);
	ret = 0;
out:
	spin_unlock_irqrestore(cdev->ccwlock, flags);
	return ret;
}
L
Linus Torvalds 已提交
752

753 754 755
static void reset_cmb(struct ccw_device *cdev)
{
	cmf_generic_reset(cdev);
L
Linus Torvalds 已提交
756 757
}

758
static void * align_cmb(void *area)
L
Linus Torvalds 已提交
759
{
760
	return area;
L
Linus Torvalds 已提交
761 762 763 764 765 766 767 768 769 770 771
}

static struct attribute_group cmf_attr_group;

static struct cmb_operations cmbops_basic = {
	.alloc	= alloc_cmb,
	.free	= free_cmb,
	.set	= set_cmb,
	.read	= read_cmb,
	.readall    = readall_cmb,
	.reset	    = reset_cmb,
772
	.align	    = align_cmb,
L
Linus Torvalds 已提交
773 774
	.attr_group = &cmf_attr_group,
};
775

L
Linus Torvalds 已提交
776 777 778 779
/* ******** extended cmb handling ********/

/**
 * struct cmbe - extended channel measurement block
780 781 782 783 784 785 786 787 788 789
 * @ssch_rsch_count: number of ssch and rsch
 * @sample_count: number of samples
 * @device_connect_time: time of device connect
 * @function_pending_time: time of function pending
 * @device_disconnect_time: time of device disconnect
 * @control_unit_queuing_time: time of control unit queuing
 * @device_active_only_time: time of device active only
 * @device_busy_time: time of device busy
 * @initial_command_response_time: initial command response time
 * @reserved: unused
L
Linus Torvalds 已提交
790
 *
791 792 793
 * The measurement block as used by the hardware. May be in any 64 bit physical
 * location.
 * The fields are described further in z/Architecture Principles of Operation,
L
Linus Torvalds 已提交
794 795 796 797 798 799 800 801 802 803 804 805 806 807 808
 * third edition, chapter 17.
 */
struct cmbe {
	u32 ssch_rsch_count;
	u32 sample_count;
	u32 device_connect_time;
	u32 function_pending_time;
	u32 device_disconnect_time;
	u32 control_unit_queuing_time;
	u32 device_active_only_time;
	u32 device_busy_time;
	u32 initial_command_response_time;
	u32 reserved[7];
};

809 810
/*
 * kmalloc only guarantees 8 byte alignment, but we need cmbe
L
Linus Torvalds 已提交
811
 * pointers to be naturally aligned. Make sure to allocate
812 813 814
 * enough space for two cmbes.
 */
static inline struct cmbe *cmbe_align(struct cmbe *c)
L
Linus Torvalds 已提交
815 816 817 818 819 820 821
{
	unsigned long addr;
	addr = ((unsigned long)c + sizeof (struct cmbe) - sizeof(long)) &
				 ~(sizeof (struct cmbe) - sizeof(long));
	return (struct cmbe*)addr;
}

822
static int alloc_cmbe(struct ccw_device *cdev)
L
Linus Torvalds 已提交
823 824
{
	struct cmbe *cmbe;
825 826 827 828
	struct cmb_data *cmb_data;
	int ret;

	cmbe = kzalloc (sizeof (*cmbe) * 2, GFP_KERNEL);
L
Linus Torvalds 已提交
829 830
	if (!cmbe)
		return -ENOMEM;
831 832 833 834 835 836 837 838 839 840 841
	cmb_data = kzalloc(sizeof(struct cmb_data), GFP_KERNEL);
	if (!cmb_data) {
		ret = -ENOMEM;
		goto out_free;
	}
	cmb_data->last_block = kzalloc(sizeof(struct cmbe), GFP_KERNEL);
	if (!cmb_data->last_block) {
		ret = -ENOMEM;
		goto out_free;
	}
	cmb_data->size = sizeof(struct cmbe);
L
Linus Torvalds 已提交
842 843 844
	spin_lock_irq(cdev->ccwlock);
	if (cdev->private->cmb) {
		spin_unlock_irq(cdev->ccwlock);
845 846
		ret = -EBUSY;
		goto out_free;
L
Linus Torvalds 已提交
847
	}
848 849
	cmb_data->hw_block = cmbe;
	cdev->private->cmb = cmb_data;
L
Linus Torvalds 已提交
850 851 852 853 854 855 856 857 858 859
	spin_unlock_irq(cdev->ccwlock);

	/* activate global measurement if this is the first channel */
	spin_lock(&cmb_area.lock);
	if (list_empty(&cmb_area.list))
		cmf_activate(NULL, 1);
	list_add_tail(&cdev->private->cmb_list, &cmb_area.list);
	spin_unlock(&cmb_area.lock);

	return 0;
860 861 862 863 864 865
out_free:
	if (cmb_data)
		kfree(cmb_data->last_block);
	kfree(cmb_data);
	kfree(cmbe);
	return ret;
L
Linus Torvalds 已提交
866 867
}

868
static void free_cmbe(struct ccw_device *cdev)
L
Linus Torvalds 已提交
869
{
870 871
	struct cmb_data *cmb_data;

L
Linus Torvalds 已提交
872
	spin_lock_irq(cdev->ccwlock);
873
	cmb_data = cdev->private->cmb;
L
Linus Torvalds 已提交
874
	cdev->private->cmb = NULL;
875 876 877
	if (cmb_data)
		kfree(cmb_data->last_block);
	kfree(cmb_data);
L
Linus Torvalds 已提交
878 879 880 881 882 883 884 885 886 887
	spin_unlock_irq(cdev->ccwlock);

	/* deactivate global measurement if this is the last channel */
	spin_lock(&cmb_area.lock);
	list_del_init(&cdev->private->cmb_list);
	if (list_empty(&cmb_area.list))
		cmf_activate(NULL, 0);
	spin_unlock(&cmb_area.lock);
}

888
static int set_cmbe(struct ccw_device *cdev, u32 mme)
L
Linus Torvalds 已提交
889 890
{
	unsigned long mba;
891 892
	struct cmb_data *cmb_data;
	unsigned long flags;
L
Linus Torvalds 已提交
893

894 895 896
	spin_lock_irqsave(cdev->ccwlock, flags);
	if (!cdev->private->cmb) {
		spin_unlock_irqrestore(cdev->ccwlock, flags);
L
Linus Torvalds 已提交
897
		return -EINVAL;
898 899 900 901
	}
	cmb_data = cdev->private->cmb;
	mba = mme ? (unsigned long) cmbe_align(cmb_data->hw_block) : 0;
	spin_unlock_irqrestore(cdev->ccwlock, flags);
L
Linus Torvalds 已提交
902 903 904 905 906

	return set_schib_wait(cdev, mme, 1, mba);
}


907
static u64 read_cmbe(struct ccw_device *cdev, int index)
L
Linus Torvalds 已提交
908
{
909 910
	struct cmbe *cmb;
	struct cmb_data *cmb_data;
L
Linus Torvalds 已提交
911
	u32 val;
912 913
	int ret;
	unsigned long flags;
L
Linus Torvalds 已提交
914

915 916
	ret = cmf_cmb_copy_wait(cdev);
	if (ret < 0)
L
Linus Torvalds 已提交
917 918
		return 0;

919 920 921 922 923 924 925
	spin_lock_irqsave(cdev->ccwlock, flags);
	cmb_data = cdev->private->cmb;
	if (!cmb_data) {
		ret = 0;
		goto out;
	}
	cmb = cmb_data->last_block;
L
Linus Torvalds 已提交
926 927 928

	switch (index) {
	case cmb_ssch_rsch_count:
929 930
		ret = cmb->ssch_rsch_count;
		goto out;
L
Linus Torvalds 已提交
931
	case cmb_sample_count:
932 933
		ret = cmb->sample_count;
		goto out;
L
Linus Torvalds 已提交
934
	case cmb_device_connect_time:
935
		val = cmb->device_connect_time;
L
Linus Torvalds 已提交
936 937
		break;
	case cmb_function_pending_time:
938
		val = cmb->function_pending_time;
L
Linus Torvalds 已提交
939 940
		break;
	case cmb_device_disconnect_time:
941
		val = cmb->device_disconnect_time;
L
Linus Torvalds 已提交
942 943
		break;
	case cmb_control_unit_queuing_time:
944
		val = cmb->control_unit_queuing_time;
L
Linus Torvalds 已提交
945 946
		break;
	case cmb_device_active_only_time:
947
		val = cmb->device_active_only_time;
L
Linus Torvalds 已提交
948 949
		break;
	case cmb_device_busy_time:
950
		val = cmb->device_busy_time;
L
Linus Torvalds 已提交
951 952
		break;
	case cmb_initial_command_response_time:
953
		val = cmb->initial_command_response_time;
L
Linus Torvalds 已提交
954 955
		break;
	default:
956 957
		ret = 0;
		goto out;
L
Linus Torvalds 已提交
958
	}
959 960 961 962
	ret = time_to_avg_nsec(val, cmb->sample_count);
out:
	spin_unlock_irqrestore(cdev->ccwlock, flags);
	return ret;
L
Linus Torvalds 已提交
963 964
}

965
static int readall_cmbe(struct ccw_device *cdev, struct cmbdata *data)
L
Linus Torvalds 已提交
966
{
967 968
	struct cmbe *cmb;
	struct cmb_data *cmb_data;
L
Linus Torvalds 已提交
969
	u64 time;
970 971
	unsigned long flags;
	int ret;
L
Linus Torvalds 已提交
972

973 974 975
	ret = cmf_cmb_copy_wait(cdev);
	if (ret < 0)
		return ret;
L
Linus Torvalds 已提交
976
	spin_lock_irqsave(cdev->ccwlock, flags);
977 978 979 980
	cmb_data = cdev->private->cmb;
	if (!cmb_data) {
		ret = -ENODEV;
		goto out;
L
Linus Torvalds 已提交
981
	}
982 983 984 985 986
	if (cmb_data->last_update == 0) {
		ret = -EAGAIN;
		goto out;
	}
	time = cmb_data->last_update - cdev->private->cmb_start_time;
L
Linus Torvalds 已提交
987 988 989 990 991 992 993 994 995

	memset (data, 0, sizeof(struct cmbdata));

	/* we only know values before device_busy_time */
	data->size = offsetof(struct cmbdata, device_busy_time);

	/* conver to nanoseconds */
	data->elapsed_time = (time * 1000) >> 12;

996
	cmb = cmb_data->last_block;
L
Linus Torvalds 已提交
997
	/* copy data to new structure */
998 999
	data->ssch_rsch_count = cmb->ssch_rsch_count;
	data->sample_count = cmb->sample_count;
L
Linus Torvalds 已提交
1000 1001

	/* time fields are converted to nanoseconds while copying */
1002 1003 1004 1005
	data->device_connect_time = time_to_nsec(cmb->device_connect_time);
	data->function_pending_time = time_to_nsec(cmb->function_pending_time);
	data->device_disconnect_time =
		time_to_nsec(cmb->device_disconnect_time);
L
Linus Torvalds 已提交
1006
	data->control_unit_queuing_time
1007
		= time_to_nsec(cmb->control_unit_queuing_time);
L
Linus Torvalds 已提交
1008
	data->device_active_only_time
1009 1010
		= time_to_nsec(cmb->device_active_only_time);
	data->device_busy_time = time_to_nsec(cmb->device_busy_time);
L
Linus Torvalds 已提交
1011
	data->initial_command_response_time
1012
		= time_to_nsec(cmb->initial_command_response_time);
L
Linus Torvalds 已提交
1013

1014 1015 1016 1017
	ret = 0;
out:
	spin_unlock_irqrestore(cdev->ccwlock, flags);
	return ret;
L
Linus Torvalds 已提交
1018 1019
}

1020
static void reset_cmbe(struct ccw_device *cdev)
L
Linus Torvalds 已提交
1021
{
1022 1023 1024 1025 1026 1027
	cmf_generic_reset(cdev);
}

static void * align_cmbe(void *area)
{
	return cmbe_align(area);
L
Linus Torvalds 已提交
1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038
}

static struct attribute_group cmf_attr_group_ext;

static struct cmb_operations cmbops_extended = {
	.alloc	    = alloc_cmbe,
	.free	    = free_cmbe,
	.set	    = set_cmbe,
	.read	    = read_cmbe,
	.readall    = readall_cmbe,
	.reset	    = reset_cmbe,
1039
	.align	    = align_cmbe,
L
Linus Torvalds 已提交
1040 1041 1042
	.attr_group = &cmf_attr_group_ext,
};

1043
static ssize_t cmb_show_attr(struct device *dev, char *buf, enum cmb_index idx)
L
Linus Torvalds 已提交
1044 1045 1046 1047 1048
{
	return sprintf(buf, "%lld\n",
		(unsigned long long) cmf_read(to_ccwdev(dev), idx));
}

1049 1050 1051
static ssize_t cmb_show_avg_sample_interval(struct device *dev,
					    struct device_attribute *attr,
					    char *buf)
L
Linus Torvalds 已提交
1052 1053 1054 1055
{
	struct ccw_device *cdev;
	long interval;
	unsigned long count;
1056
	struct cmb_data *cmb_data;
L
Linus Torvalds 已提交
1057 1058 1059

	cdev = to_ccwdev(dev);
	count = cmf_read(cdev, cmb_sample_count);
1060 1061 1062 1063 1064
	spin_lock_irq(cdev->ccwlock);
	cmb_data = cdev->private->cmb;
	if (count) {
		interval = cmb_data->last_update -
			cdev->private->cmb_start_time;
1065
		interval = (interval * 1000) >> 12;
L
Linus Torvalds 已提交
1066
		interval /= count;
1067
	} else
L
Linus Torvalds 已提交
1068
		interval = -1;
1069
	spin_unlock_irq(cdev->ccwlock);
L
Linus Torvalds 已提交
1070 1071 1072
	return sprintf(buf, "%ld\n", interval);
}

1073 1074 1075
static ssize_t cmb_show_avg_utilization(struct device *dev,
					struct device_attribute *attr,
					char *buf)
L
Linus Torvalds 已提交
1076 1077 1078 1079 1080 1081 1082
{
	struct cmbdata data;
	u64 utilization;
	unsigned long t, u;
	int ret;

	ret = cmf_readall(to_ccwdev(dev), &data);
1083 1084 1085 1086
	if (ret == -EAGAIN || ret == -ENODEV)
		/* No data (yet/currently) available to use for calculation. */
		return sprintf(buf, "n/a\n");
	else if (ret)
L
Linus Torvalds 已提交
1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106
		return ret;

	utilization = data.device_connect_time +
		      data.function_pending_time +
		      data.device_disconnect_time;

	/* shift to avoid long long division */
	while (-1ul < (data.elapsed_time | utilization)) {
		utilization >>= 8;
		data.elapsed_time >>= 8;
	}

	/* calculate value in 0.1 percent units */
	t = (unsigned long) data.elapsed_time / 1000;
	u = (unsigned long) utilization / t;

	return sprintf(buf, "%02ld.%01ld%%\n", u/ 10, u - (u/ 10) * 10);
}

#define cmf_attr(name) \
1107 1108 1109 1110
static ssize_t show_##name(struct device *dev, \
			   struct device_attribute *attr, char *buf)	\
{ return cmb_show_attr((dev), buf, cmb_##name); } \
static DEVICE_ATTR(name, 0444, show_##name, NULL);
L
Linus Torvalds 已提交
1111 1112

#define cmf_attr_avg(name) \
1113 1114 1115 1116
static ssize_t show_avg_##name(struct device *dev, \
			       struct device_attribute *attr, char *buf) \
{ return cmb_show_attr((dev), buf, cmb_##name); } \
static DEVICE_ATTR(avg_##name, 0444, show_avg_##name, NULL);
L
Linus Torvalds 已提交
1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127

cmf_attr(ssch_rsch_count);
cmf_attr(sample_count);
cmf_attr_avg(device_connect_time);
cmf_attr_avg(function_pending_time);
cmf_attr_avg(device_disconnect_time);
cmf_attr_avg(control_unit_queuing_time);
cmf_attr_avg(device_active_only_time);
cmf_attr_avg(device_busy_time);
cmf_attr_avg(initial_command_response_time);

1128 1129
static DEVICE_ATTR(avg_sample_interval, 0444, cmb_show_avg_sample_interval,
		   NULL);
L
Linus Torvalds 已提交
1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141
static DEVICE_ATTR(avg_utilization, 0444, cmb_show_avg_utilization, NULL);

static struct attribute *cmf_attributes[] = {
	&dev_attr_avg_sample_interval.attr,
	&dev_attr_avg_utilization.attr,
	&dev_attr_ssch_rsch_count.attr,
	&dev_attr_sample_count.attr,
	&dev_attr_avg_device_connect_time.attr,
	&dev_attr_avg_function_pending_time.attr,
	&dev_attr_avg_device_disconnect_time.attr,
	&dev_attr_avg_control_unit_queuing_time.attr,
	&dev_attr_avg_device_active_only_time.attr,
H
Heiko Carstens 已提交
1142
	NULL,
L
Linus Torvalds 已提交
1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161
};

static struct attribute_group cmf_attr_group = {
	.name  = "cmf",
	.attrs = cmf_attributes,
};

static struct attribute *cmf_attributes_ext[] = {
	&dev_attr_avg_sample_interval.attr,
	&dev_attr_avg_utilization.attr,
	&dev_attr_ssch_rsch_count.attr,
	&dev_attr_sample_count.attr,
	&dev_attr_avg_device_connect_time.attr,
	&dev_attr_avg_function_pending_time.attr,
	&dev_attr_avg_device_disconnect_time.attr,
	&dev_attr_avg_control_unit_queuing_time.attr,
	&dev_attr_avg_device_active_only_time.attr,
	&dev_attr_avg_device_busy_time.attr,
	&dev_attr_avg_initial_command_response_time.attr,
H
Heiko Carstens 已提交
1162
	NULL,
L
Linus Torvalds 已提交
1163 1164 1165 1166 1167 1168 1169
};

static struct attribute_group cmf_attr_group_ext = {
	.name  = "cmf",
	.attrs = cmf_attributes_ext,
};

1170 1171 1172
static ssize_t cmb_enable_show(struct device *dev,
			       struct device_attribute *attr,
			       char *buf)
L
Linus Torvalds 已提交
1173 1174 1175 1176
{
	return sprintf(buf, "%d\n", to_ccwdev(dev)->private->cmb ? 1 : 0);
}

1177 1178 1179
static ssize_t cmb_enable_store(struct device *dev,
				struct device_attribute *attr, const char *buf,
				size_t c)
L
Linus Torvalds 已提交
1180 1181 1182
{
	struct ccw_device *cdev;
	int ret;
1183 1184 1185 1186 1187
	unsigned long val;

	ret = strict_strtoul(buf, 16, &val);
	if (ret)
		return ret;
L
Linus Torvalds 已提交
1188 1189 1190

	cdev = to_ccwdev(dev);

1191 1192
	switch (val) {
	case 0:
L
Linus Torvalds 已提交
1193 1194
		ret = disable_cmf(cdev);
		break;
1195
	case 1:
L
Linus Torvalds 已提交
1196 1197 1198 1199 1200 1201 1202 1203 1204
		ret = enable_cmf(cdev);
		break;
	}

	return c;
}

DEVICE_ATTR(cmb_enable, 0644, cmb_enable_show, cmb_enable_store);

1205 1206 1207 1208 1209
int ccw_set_cmf(struct ccw_device *cdev, int enable)
{
	return cmbops->set(cdev, enable ? 2 : 0);
}

1210 1211 1212 1213 1214 1215 1216 1217 1218
/**
 * enable_cmf() - switch on the channel measurement for a specific device
 *  @cdev:	The ccw device to be enabled
 *
 *  Returns %0 for success or a negative error value.
 *
 *  Context:
 *    non-atomic
 */
1219
int enable_cmf(struct ccw_device *cdev)
L
Linus Torvalds 已提交
1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239
{
	int ret;

	ret = cmbops->alloc(cdev);
	cmbops->reset(cdev);
	if (ret)
		return ret;
	ret = cmbops->set(cdev, 2);
	if (ret) {
		cmbops->free(cdev);
		return ret;
	}
	ret = sysfs_create_group(&cdev->dev.kobj, cmbops->attr_group);
	if (!ret)
		return 0;
	cmbops->set(cdev, 0);  //FIXME: this can fail
	cmbops->free(cdev);
	return ret;
}

1240 1241 1242 1243 1244 1245 1246 1247 1248
/**
 * disable_cmf() - switch off the channel measurement for a specific device
 *  @cdev:	The ccw device to be disabled
 *
 *  Returns %0 for success or a negative error value.
 *
 *  Context:
 *    non-atomic
 */
1249
int disable_cmf(struct ccw_device *cdev)
L
Linus Torvalds 已提交
1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260
{
	int ret;

	ret = cmbops->set(cdev, 0);
	if (ret)
		return ret;
	cmbops->free(cdev);
	sysfs_remove_group(&cdev->dev.kobj, cmbops->attr_group);
	return ret;
}

1261 1262 1263 1264 1265 1266 1267 1268 1269 1270
/**
 * cmf_read() - read one value from the current channel measurement block
 * @cdev:	the channel to be read
 * @index:	the index of the value to be read
 *
 * Returns the value read or %0 if the value cannot be read.
 *
 *  Context:
 *    any
 */
1271
u64 cmf_read(struct ccw_device *cdev, int index)
L
Linus Torvalds 已提交
1272 1273 1274 1275
{
	return cmbops->read(cdev, index);
}

1276 1277 1278 1279 1280 1281 1282 1283 1284 1285
/**
 * cmf_readall() - read the current channel measurement block
 * @cdev:	the channel to be read
 * @data:	a pointer to a data block that will be filled
 *
 * Returns %0 on success, a negative error value otherwise.
 *
 *  Context:
 *    any
 */
1286
int cmf_readall(struct ccw_device *cdev, struct cmbdata *data)
L
Linus Torvalds 已提交
1287 1288 1289 1290
{
	return cmbops->readall(cdev, data);
}

1291 1292 1293 1294 1295 1296 1297
/* Reenable cmf when a disconnected device becomes available again. */
int cmf_reenable(struct ccw_device *cdev)
{
	cmbops->reset(cdev);
	return cmbops->set(cdev, 2);
}

1298
static int __init init_cmf(void)
L
Linus Torvalds 已提交
1299 1300 1301 1302
{
	char *format_string;
	char *detect_string = "parameter";

1303 1304 1305 1306 1307
	/*
	 * If the user did not give a parameter, see if we are running on a
	 * machine supporting extended measurement blocks, otherwise fall back
	 * to basic mode.
	 */
L
Linus Torvalds 已提交
1308
	if (format == CMF_AUTODETECT) {
1309
		if (!css_general_characteristics.ext_mb) {
L
Linus Torvalds 已提交
1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324
			format = CMF_BASIC;
		} else {
			format = CMF_EXTENDED;
		}
		detect_string = "autodetected";
	} else {
		detect_string = "parameter";
	}

	switch (format) {
	case CMF_BASIC:
		format_string = "basic";
		cmbops = &cmbops_basic;
		break;
	case CMF_EXTENDED:
1325
		format_string = "extended";
L
Linus Torvalds 已提交
1326 1327 1328 1329 1330
		cmbops = &cmbops_extended;
		break;
	default:
		return 1;
	}
1331 1332
	pr_info("Channel measurement facility initialized using format "
		"%s (mode %s)\n", format_string, detect_string);
L
Linus Torvalds 已提交
1333 1334 1335 1336 1337 1338 1339 1340 1341
	return 0;
}

module_init(init_cmf);


MODULE_AUTHOR("Arnd Bergmann <arndb@de.ibm.com>");
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("channel measurement facility base driver\n"
1342
		   "Copyright IBM Corp. 2003\n");
L
Linus Torvalds 已提交
1343 1344 1345 1346 1347

EXPORT_SYMBOL_GPL(enable_cmf);
EXPORT_SYMBOL_GPL(disable_cmf);
EXPORT_SYMBOL_GPL(cmf_read);
EXPORT_SYMBOL_GPL(cmf_readall);