blob: ff1e442d4b099a1243b972644a0b474ac9c26d65 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Heiko Carstense018ba12006-02-01 03:06:31 -08002 * linux/drivers/s390/cio/cmf.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 * Linux on zSeries Channel Measurement Facility support
5 *
Cornelia Huck94bb0632006-06-29 15:08:41 +02006 * Copyright 2000,2006 IBM Corporation
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
Cornelia Huck94bb0632006-06-29 15:08:41 +02008 * Authors: Arnd Bergmann <arndb@de.ibm.com>
9 * Cornelia Huck <cornelia.huck@de.ibm.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 *
11 * original idea from Natarajan Krishnaswami <nkrishna@us.ibm.com>
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2, or (at your option)
16 * any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 */
27
28#include <linux/bootmem.h>
29#include <linux/device.h>
30#include <linux/init.h>
31#include <linux/list.h>
32#include <linux/module.h>
33#include <linux/moduleparam.h>
Tim Schmielau4e57b682005-10-30 15:03:48 -080034#include <linux/slab.h>
35#include <linux/timex.h> /* get_clock() */
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
37#include <asm/ccwdev.h>
38#include <asm/cio.h>
39#include <asm/cmb.h>
Tim Schmielau4e57b682005-10-30 15:03:48 -080040#include <asm/div64.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42#include "cio.h"
43#include "css.h"
44#include "device.h"
45#include "ioasm.h"
46#include "chsc.h"
47
Cornelia Huckfc5019c2007-10-12 16:11:15 +020048/*
49 * parameter to enable cmf during boot, possible uses are:
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 * "s390cmf" -- enable cmf and allocate 2 MB of ram so measuring can be
51 * used on any subchannel
52 * "s390cmf=<num>" -- enable cmf and allocate enough memory to measure
53 * <num> subchannel, where <num> is an integer
54 * between 1 and 65535, default is 1024
55 */
56#define ARGSTRING "s390cmf"
57
58/* indices for READCMB */
59enum cmb_index {
60 /* basic and exended format: */
61 cmb_ssch_rsch_count,
62 cmb_sample_count,
63 cmb_device_connect_time,
64 cmb_function_pending_time,
65 cmb_device_disconnect_time,
66 cmb_control_unit_queuing_time,
67 cmb_device_active_only_time,
68 /* extended format only: */
69 cmb_device_busy_time,
70 cmb_initial_command_response_time,
71};
72
73/**
74 * enum cmb_format - types of supported measurement block formats
75 *
76 * @CMF_BASIC: traditional channel measurement blocks supported
77 * by all machines that we run on
78 * @CMF_EXTENDED: improved format that was introduced with the z990
79 * machine
80 * @CMF_AUTODETECT: default: use extended format when running on a z990
81 * or later machine, otherwise fall back to basic format
82 **/
83enum cmb_format {
84 CMF_BASIC,
85 CMF_EXTENDED,
86 CMF_AUTODETECT = -1,
87};
Cornelia Huckfc5019c2007-10-12 16:11:15 +020088
Linus Torvalds1da177e2005-04-16 15:20:36 -070089/**
90 * format - actual format for all measurement blocks
91 *
92 * The format module parameter can be set to a value of 0 (zero)
93 * or 1, indicating basic or extended format as described for
94 * enum cmb_format.
95 */
96static int format = CMF_AUTODETECT;
97module_param(format, bool, 0444);
98
99/**
100 * struct cmb_operations - functions to use depending on cmb_format
101 *
Cornelia Huck94bb0632006-06-29 15:08:41 +0200102 * Most of these functions operate on a struct ccw_device. There is only
103 * one instance of struct cmb_operations because the format of the measurement
104 * data is guaranteed to be the same for every ccw_device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 *
106 * @alloc: allocate memory for a channel measurement block,
107 * either with the help of a special pool or with kmalloc
108 * @free: free memory allocated with @alloc
109 * @set: enable or disable measurement
110 * @readall: read a measurement block in a common format
111 * @reset: clear the data in the associated measurement block and
112 * reset its time stamp
Cornelia Huck94bb0632006-06-29 15:08:41 +0200113 * @align: align an allocated block so that the hardware can use it
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 */
115struct cmb_operations {
Cornelia Huckfc5019c2007-10-12 16:11:15 +0200116 int (*alloc) (struct ccw_device *);
117 void (*free) (struct ccw_device *);
118 int (*set) (struct ccw_device *, u32);
119 u64 (*read) (struct ccw_device *, int);
120 int (*readall)(struct ccw_device *, struct cmbdata *);
121 void (*reset) (struct ccw_device *);
122 void *(*align) (void *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
124 struct attribute_group *attr_group;
125};
126static struct cmb_operations *cmbops;
127
Cornelia Huck94bb0632006-06-29 15:08:41 +0200128struct cmb_data {
129 void *hw_block; /* Pointer to block updated by hardware */
130 void *last_block; /* Last changed block copied from hardware block */
131 int size; /* Size of hw_block and last_block */
132 unsigned long long last_update; /* when last_block was updated */
133};
134
Cornelia Huckfc5019c2007-10-12 16:11:15 +0200135/*
136 * Our user interface is designed in terms of nanoseconds,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 * while the hardware measures total times in its own
Cornelia Huckfc5019c2007-10-12 16:11:15 +0200138 * unit.
139 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140static inline u64 time_to_nsec(u32 value)
141{
142 return ((u64)value) * 128000ull;
143}
144
145/*
146 * Users are usually interested in average times,
147 * not accumulated time.
148 * This also helps us with atomicity problems
149 * when reading sinlge values.
150 */
151static inline u64 time_to_avg_nsec(u32 value, u32 count)
152{
153 u64 ret;
154
155 /* no samples yet, avoid division by 0 */
156 if (count == 0)
157 return 0;
158
159 /* value comes in units of 128 µsec */
160 ret = time_to_nsec(value);
161 do_div(ret, count);
162
163 return ret;
164}
165
Cornelia Huckfc5019c2007-10-12 16:11:15 +0200166/*
167 * Activate or deactivate the channel monitor. When area is NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 * the monitor is deactivated. The channel monitor needs to
169 * be active in order to measure subchannels, which also need
Cornelia Huckfc5019c2007-10-12 16:11:15 +0200170 * to be enabled.
171 */
172static inline void cmf_activate(void *area, unsigned int onoff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173{
174 register void * __gpr2 asm("2");
175 register long __gpr1 asm("1");
176
177 __gpr2 = area;
178 __gpr1 = onoff ? 2 : 0;
179 /* activate channel measurement */
180 asm("schm" : : "d" (__gpr2), "d" (__gpr1) );
181}
182
Cornelia Huckfc5019c2007-10-12 16:11:15 +0200183static int set_schib(struct ccw_device *cdev, u32 mme, int mbfc,
184 unsigned long address)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185{
186 int ret;
187 int retry;
188 struct subchannel *sch;
189 struct schib *schib;
190
191 sch = to_subchannel(cdev->dev.parent);
192 schib = &sch->schib;
193 /* msch can silently fail, so do it again if necessary */
194 for (retry = 0; retry < 3; retry++) {
195 /* prepare schib */
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800196 stsch(sch->schid, schib);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 schib->pmcw.mme = mme;
198 schib->pmcw.mbfc = mbfc;
199 /* address can be either a block address or a block index */
200 if (mbfc)
201 schib->mba = address;
202 else
203 schib->pmcw.mbi = address;
204
205 /* try to submit it */
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800206 switch(ret = msch_err(sch->schid, schib)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 case 0:
208 break;
209 case 1:
210 case 2: /* in I/O or status pending */
211 ret = -EBUSY;
212 break;
213 case 3: /* subchannel is no longer valid */
214 ret = -ENODEV;
215 break;
216 default: /* msch caught an exception */
217 ret = -EINVAL;
218 break;
219 }
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800220 stsch(sch->schid, schib); /* restore the schib */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
222 if (ret)
223 break;
224
225 /* check if it worked */
226 if (schib->pmcw.mme == mme &&
227 schib->pmcw.mbfc == mbfc &&
228 (mbfc ? (schib->mba == address)
229 : (schib->pmcw.mbi == address)))
230 return 0;
231
232 ret = -EINVAL;
233 }
234
235 return ret;
236}
237
238struct set_schib_struct {
239 u32 mme;
240 int mbfc;
241 unsigned long address;
242 wait_queue_head_t wait;
243 int ret;
Cornelia Huck94bb0632006-06-29 15:08:41 +0200244 struct kref kref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245};
246
Cornelia Huck94bb0632006-06-29 15:08:41 +0200247static void cmf_set_schib_release(struct kref *kref)
248{
249 struct set_schib_struct *set_data;
250
251 set_data = container_of(kref, struct set_schib_struct, kref);
252 kfree(set_data);
253}
254
255#define CMF_PENDING 1
256
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257static int set_schib_wait(struct ccw_device *cdev, u32 mme,
258 int mbfc, unsigned long address)
259{
Cornelia Huck94bb0632006-06-29 15:08:41 +0200260 struct set_schib_struct *set_data;
261 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
263 spin_lock_irq(cdev->ccwlock);
Cornelia Huck94bb0632006-06-29 15:08:41 +0200264 if (!cdev->private->cmb) {
265 ret = -ENODEV;
266 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 }
Cornelia Huck94bb0632006-06-29 15:08:41 +0200268 set_data = kzalloc(sizeof(struct set_schib_struct), GFP_ATOMIC);
269 if (!set_data) {
270 ret = -ENOMEM;
271 goto out;
272 }
273 init_waitqueue_head(&set_data->wait);
274 kref_init(&set_data->kref);
275 set_data->mme = mme;
276 set_data->mbfc = mbfc;
277 set_data->address = address;
278
279 ret = set_schib(cdev, mme, mbfc, address);
280 if (ret != -EBUSY)
281 goto out_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
283 if (cdev->private->state != DEV_STATE_ONLINE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 /* if the device is not online, don't even try again */
Cornelia Huck94bb0632006-06-29 15:08:41 +0200285 ret = -EBUSY;
286 goto out_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 }
Cornelia Huck94bb0632006-06-29 15:08:41 +0200288
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 cdev->private->state = DEV_STATE_CMFCHANGE;
Cornelia Huck94bb0632006-06-29 15:08:41 +0200290 set_data->ret = CMF_PENDING;
291 cdev->private->cmb_wait = set_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
293 spin_unlock_irq(cdev->ccwlock);
Cornelia Huck94bb0632006-06-29 15:08:41 +0200294 if (wait_event_interruptible(set_data->wait,
295 set_data->ret != CMF_PENDING)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 spin_lock_irq(cdev->ccwlock);
Cornelia Huck94bb0632006-06-29 15:08:41 +0200297 if (set_data->ret == CMF_PENDING) {
298 set_data->ret = -ERESTARTSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 if (cdev->private->state == DEV_STATE_CMFCHANGE)
300 cdev->private->state = DEV_STATE_ONLINE;
301 }
302 spin_unlock_irq(cdev->ccwlock);
303 }
Cornelia Huck94bb0632006-06-29 15:08:41 +0200304 spin_lock_irq(cdev->ccwlock);
305 cdev->private->cmb_wait = NULL;
306 ret = set_data->ret;
307out_put:
308 kref_put(&set_data->kref, cmf_set_schib_release);
309out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 spin_unlock_irq(cdev->ccwlock);
Cornelia Huck94bb0632006-06-29 15:08:41 +0200311 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312}
313
314void retry_set_schib(struct ccw_device *cdev)
315{
Cornelia Huck94bb0632006-06-29 15:08:41 +0200316 struct set_schib_struct *set_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
Cornelia Huck94bb0632006-06-29 15:08:41 +0200318 set_data = cdev->private->cmb_wait;
319 if (!set_data) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 WARN_ON(1);
321 return;
322 }
Cornelia Huck94bb0632006-06-29 15:08:41 +0200323 kref_get(&set_data->kref);
324 set_data->ret = set_schib(cdev, set_data->mme, set_data->mbfc,
325 set_data->address);
326 wake_up(&set_data->wait);
327 kref_put(&set_data->kref, cmf_set_schib_release);
328}
329
330static int cmf_copy_block(struct ccw_device *cdev)
331{
332 struct subchannel *sch;
333 void *reference_buf;
334 void *hw_block;
335 struct cmb_data *cmb_data;
336
337 sch = to_subchannel(cdev->dev.parent);
338
339 if (stsch(sch->schid, &sch->schib))
340 return -ENODEV;
341
342 if (sch->schib.scsw.fctl & SCSW_FCTL_START_FUNC) {
343 /* Don't copy if a start function is in progress. */
344 if ((!sch->schib.scsw.actl & SCSW_ACTL_SUSPENDED) &&
345 (sch->schib.scsw.actl &
346 (SCSW_ACTL_DEVACT | SCSW_ACTL_SCHACT)) &&
347 (!sch->schib.scsw.stctl & SCSW_STCTL_SEC_STATUS))
348 return -EBUSY;
349 }
350 cmb_data = cdev->private->cmb;
351 hw_block = cmbops->align(cmb_data->hw_block);
352 if (!memcmp(cmb_data->last_block, hw_block, cmb_data->size))
353 /* No need to copy. */
354 return 0;
355 reference_buf = kzalloc(cmb_data->size, GFP_ATOMIC);
356 if (!reference_buf)
357 return -ENOMEM;
358 /* Ensure consistency of block copied from hardware. */
359 do {
360 memcpy(cmb_data->last_block, hw_block, cmb_data->size);
361 memcpy(reference_buf, hw_block, cmb_data->size);
362 } while (memcmp(cmb_data->last_block, reference_buf, cmb_data->size));
363 cmb_data->last_update = get_clock();
364 kfree(reference_buf);
365 return 0;
366}
367
368struct copy_block_struct {
369 wait_queue_head_t wait;
370 int ret;
371 struct kref kref;
372};
373
374static void cmf_copy_block_release(struct kref *kref)
375{
376 struct copy_block_struct *copy_block;
377
378 copy_block = container_of(kref, struct copy_block_struct, kref);
379 kfree(copy_block);
380}
381
382static int cmf_cmb_copy_wait(struct ccw_device *cdev)
383{
384 struct copy_block_struct *copy_block;
385 int ret;
386 unsigned long flags;
387
388 spin_lock_irqsave(cdev->ccwlock, flags);
389 if (!cdev->private->cmb) {
390 ret = -ENODEV;
391 goto out;
392 }
393 copy_block = kzalloc(sizeof(struct copy_block_struct), GFP_ATOMIC);
394 if (!copy_block) {
395 ret = -ENOMEM;
396 goto out;
397 }
398 init_waitqueue_head(&copy_block->wait);
399 kref_init(&copy_block->kref);
400
401 ret = cmf_copy_block(cdev);
402 if (ret != -EBUSY)
403 goto out_put;
404
405 if (cdev->private->state != DEV_STATE_ONLINE) {
406 ret = -EBUSY;
407 goto out_put;
408 }
409
410 cdev->private->state = DEV_STATE_CMFUPDATE;
411 copy_block->ret = CMF_PENDING;
412 cdev->private->cmb_wait = copy_block;
413
414 spin_unlock_irqrestore(cdev->ccwlock, flags);
415 if (wait_event_interruptible(copy_block->wait,
416 copy_block->ret != CMF_PENDING)) {
417 spin_lock_irqsave(cdev->ccwlock, flags);
418 if (copy_block->ret == CMF_PENDING) {
419 copy_block->ret = -ERESTARTSYS;
420 if (cdev->private->state == DEV_STATE_CMFUPDATE)
421 cdev->private->state = DEV_STATE_ONLINE;
422 }
423 spin_unlock_irqrestore(cdev->ccwlock, flags);
424 }
425 spin_lock_irqsave(cdev->ccwlock, flags);
426 cdev->private->cmb_wait = NULL;
427 ret = copy_block->ret;
428out_put:
429 kref_put(&copy_block->kref, cmf_copy_block_release);
430out:
431 spin_unlock_irqrestore(cdev->ccwlock, flags);
432 return ret;
433}
434
435void cmf_retry_copy_block(struct ccw_device *cdev)
436{
437 struct copy_block_struct *copy_block;
438
439 copy_block = cdev->private->cmb_wait;
440 if (!copy_block) {
441 WARN_ON(1);
442 return;
443 }
444 kref_get(&copy_block->kref);
445 copy_block->ret = cmf_copy_block(cdev);
446 wake_up(&copy_block->wait);
447 kref_put(&copy_block->kref, cmf_copy_block_release);
448}
449
450static void cmf_generic_reset(struct ccw_device *cdev)
451{
452 struct cmb_data *cmb_data;
453
454 spin_lock_irq(cdev->ccwlock);
455 cmb_data = cdev->private->cmb;
456 if (cmb_data) {
457 memset(cmb_data->last_block, 0, cmb_data->size);
458 /*
459 * Need to reset hw block as well to make the hardware start
460 * from 0 again.
461 */
462 memset(cmbops->align(cmb_data->hw_block), 0, cmb_data->size);
463 cmb_data->last_update = 0;
464 }
465 cdev->private->cmb_start_time = get_clock();
466 spin_unlock_irq(cdev->ccwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467}
468
469/**
470 * struct cmb_area - container for global cmb data
471 *
472 * @mem: pointer to CMBs (only in basic measurement mode)
473 * @list: contains a linked list of all subchannels
474 * @lock: protect concurrent access to @mem and @list
475 */
476struct cmb_area {
477 struct cmb *mem;
478 struct list_head list;
479 int num_channels;
480 spinlock_t lock;
481};
482
483static struct cmb_area cmb_area = {
Milind Arun Choudharycb629a02007-04-27 16:02:01 +0200484 .lock = __SPIN_LOCK_UNLOCKED(cmb_area.lock),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 .list = LIST_HEAD_INIT(cmb_area.list),
486 .num_channels = 1024,
487};
488
489
490/* ****** old style CMB handling ********/
491
Cornelia Huckfc5019c2007-10-12 16:11:15 +0200492/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 * Basic channel measurement blocks are allocated in one contiguous
494 * block of memory, which can not be moved as long as any channel
495 * is active. Therefore, a maximum number of subchannels needs to
496 * be defined somewhere. This is a module parameter, defaulting to
497 * a resonable value of 1024, or 32 kb of memory.
498 * Current kernels don't allow kmalloc with more than 128kb, so the
Cornelia Huckfc5019c2007-10-12 16:11:15 +0200499 * maximum is 4096.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 */
501
502module_param_named(maxchannels, cmb_area.num_channels, uint, 0444);
503
504/**
505 * struct cmb - basic channel measurement block
506 *
507 * cmb as used by the hardware the fields are described in z/Architecture
508 * Principles of Operation, chapter 17.
509 * The area to be a contiguous array and may not be reallocated or freed.
510 * Only one cmb area can be present in the system.
511 */
512struct cmb {
513 u16 ssch_rsch_count;
514 u16 sample_count;
515 u32 device_connect_time;
516 u32 function_pending_time;
517 u32 device_disconnect_time;
518 u32 control_unit_queuing_time;
519 u32 device_active_only_time;
520 u32 reserved[2];
521};
522
Cornelia Huckfc5019c2007-10-12 16:11:15 +0200523/*
524 * Insert a single device into the cmb_area list.
525 * Called with cmb_area.lock held from alloc_cmb.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 */
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100527static int alloc_cmb_single(struct ccw_device *cdev,
528 struct cmb_data *cmb_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529{
530 struct cmb *cmb;
531 struct ccw_device_private *node;
532 int ret;
533
534 spin_lock_irq(cdev->ccwlock);
535 if (!list_empty(&cdev->private->cmb_list)) {
536 ret = -EBUSY;
537 goto out;
538 }
539
Cornelia Huckfc5019c2007-10-12 16:11:15 +0200540 /*
541 * Find first unused cmb in cmb_area.mem.
542 * This is a little tricky: cmb_area.list
543 * remains sorted by ->cmb->hw_data pointers.
544 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 cmb = cmb_area.mem;
546 list_for_each_entry(node, &cmb_area.list, cmb_list) {
Cornelia Huck94bb0632006-06-29 15:08:41 +0200547 struct cmb_data *data;
548 data = node->cmb;
549 if ((struct cmb*)data->hw_block > cmb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 break;
551 cmb++;
552 }
553 if (cmb - cmb_area.mem >= cmb_area.num_channels) {
554 ret = -ENOMEM;
555 goto out;
556 }
557
558 /* insert new cmb */
559 list_add_tail(&cdev->private->cmb_list, &node->cmb_list);
Cornelia Huck94bb0632006-06-29 15:08:41 +0200560 cmb_data->hw_block = cmb;
561 cdev->private->cmb = cmb_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 ret = 0;
563out:
564 spin_unlock_irq(cdev->ccwlock);
565 return ret;
566}
567
Cornelia Huckfc5019c2007-10-12 16:11:15 +0200568static int alloc_cmb(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569{
570 int ret;
571 struct cmb *mem;
572 ssize_t size;
Cornelia Huck94bb0632006-06-29 15:08:41 +0200573 struct cmb_data *cmb_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
Cornelia Huck94bb0632006-06-29 15:08:41 +0200575 /* Allocate private cmb_data. */
576 cmb_data = kzalloc(sizeof(struct cmb_data), GFP_KERNEL);
577 if (!cmb_data)
578 return -ENOMEM;
579
580 cmb_data->last_block = kzalloc(sizeof(struct cmb), GFP_KERNEL);
581 if (!cmb_data->last_block) {
582 kfree(cmb_data);
583 return -ENOMEM;
584 }
585 cmb_data->size = sizeof(struct cmb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 spin_lock(&cmb_area.lock);
587
588 if (!cmb_area.mem) {
589 /* there is no user yet, so we need a new area */
590 size = sizeof(struct cmb) * cmb_area.num_channels;
591 WARN_ON(!list_empty(&cmb_area.list));
592
593 spin_unlock(&cmb_area.lock);
594 mem = (void*)__get_free_pages(GFP_KERNEL | GFP_DMA,
595 get_order(size));
596 spin_lock(&cmb_area.lock);
597
598 if (cmb_area.mem) {
599 /* ok, another thread was faster */
600 free_pages((unsigned long)mem, get_order(size));
601 } else if (!mem) {
602 /* no luck */
Cornelia Huck23eb68c2007-08-22 13:51:37 +0200603 printk(KERN_WARNING "cio: failed to allocate area "
604 "for measuring %d subchannels\n",
605 cmb_area.num_channels);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 ret = -ENOMEM;
607 goto out;
608 } else {
609 /* everything ok */
610 memset(mem, 0, size);
611 cmb_area.mem = mem;
612 cmf_activate(cmb_area.mem, 1);
613 }
614 }
615
616 /* do the actual allocation */
Cornelia Huck94bb0632006-06-29 15:08:41 +0200617 ret = alloc_cmb_single(cdev, cmb_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618out:
619 spin_unlock(&cmb_area.lock);
Cornelia Huck94bb0632006-06-29 15:08:41 +0200620 if (ret) {
621 kfree(cmb_data->last_block);
622 kfree(cmb_data);
623 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 return ret;
625}
626
Cornelia Huck94bb0632006-06-29 15:08:41 +0200627static void free_cmb(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628{
629 struct ccw_device_private *priv;
Cornelia Huck94bb0632006-06-29 15:08:41 +0200630 struct cmb_data *cmb_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
632 spin_lock(&cmb_area.lock);
633 spin_lock_irq(cdev->ccwlock);
634
Cornelia Huck94bb0632006-06-29 15:08:41 +0200635 priv = cdev->private;
636
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 if (list_empty(&priv->cmb_list)) {
638 /* already freed */
639 goto out;
640 }
641
Cornelia Huck94bb0632006-06-29 15:08:41 +0200642 cmb_data = priv->cmb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 priv->cmb = NULL;
Cornelia Huck94bb0632006-06-29 15:08:41 +0200644 if (cmb_data)
645 kfree(cmb_data->last_block);
646 kfree(cmb_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 list_del_init(&priv->cmb_list);
648
649 if (list_empty(&cmb_area.list)) {
650 ssize_t size;
651 size = sizeof(struct cmb) * cmb_area.num_channels;
652 cmf_activate(NULL, 0);
653 free_pages((unsigned long)cmb_area.mem, get_order(size));
654 cmb_area.mem = NULL;
655 }
656out:
657 spin_unlock_irq(cdev->ccwlock);
658 spin_unlock(&cmb_area.lock);
659}
660
Cornelia Huck94bb0632006-06-29 15:08:41 +0200661static int set_cmb(struct ccw_device *cdev, u32 mme)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662{
663 u16 offset;
Cornelia Huck94bb0632006-06-29 15:08:41 +0200664 struct cmb_data *cmb_data;
665 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
Cornelia Huck94bb0632006-06-29 15:08:41 +0200667 spin_lock_irqsave(cdev->ccwlock, flags);
668 if (!cdev->private->cmb) {
669 spin_unlock_irqrestore(cdev->ccwlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 return -EINVAL;
Cornelia Huck94bb0632006-06-29 15:08:41 +0200671 }
672 cmb_data = cdev->private->cmb;
673 offset = mme ? (struct cmb *)cmb_data->hw_block - cmb_area.mem : 0;
674 spin_unlock_irqrestore(cdev->ccwlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675
676 return set_schib_wait(cdev, mme, 0, offset);
677}
678
Cornelia Huckfc5019c2007-10-12 16:11:15 +0200679static u64 read_cmb(struct ccw_device *cdev, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680{
Cornelia Huck94bb0632006-06-29 15:08:41 +0200681 struct cmb *cmb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 u32 val;
Cornelia Huck94bb0632006-06-29 15:08:41 +0200683 int ret;
684 unsigned long flags;
685
686 ret = cmf_cmb_copy_wait(cdev);
687 if (ret < 0)
688 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
690 spin_lock_irqsave(cdev->ccwlock, flags);
691 if (!cdev->private->cmb) {
Cornelia Huck94bb0632006-06-29 15:08:41 +0200692 ret = 0;
693 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 }
Cornelia Huck94bb0632006-06-29 15:08:41 +0200695 cmb = ((struct cmb_data *)cdev->private->cmb)->last_block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
697 switch (index) {
698 case cmb_ssch_rsch_count:
Cornelia Huck94bb0632006-06-29 15:08:41 +0200699 ret = cmb->ssch_rsch_count;
700 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 case cmb_sample_count:
Cornelia Huck94bb0632006-06-29 15:08:41 +0200702 ret = cmb->sample_count;
703 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 case cmb_device_connect_time:
Cornelia Huck94bb0632006-06-29 15:08:41 +0200705 val = cmb->device_connect_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 break;
707 case cmb_function_pending_time:
Cornelia Huck94bb0632006-06-29 15:08:41 +0200708 val = cmb->function_pending_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 break;
710 case cmb_device_disconnect_time:
Cornelia Huck94bb0632006-06-29 15:08:41 +0200711 val = cmb->device_disconnect_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 break;
713 case cmb_control_unit_queuing_time:
Cornelia Huck94bb0632006-06-29 15:08:41 +0200714 val = cmb->control_unit_queuing_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 break;
716 case cmb_device_active_only_time:
Cornelia Huck94bb0632006-06-29 15:08:41 +0200717 val = cmb->device_active_only_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 break;
719 default:
Cornelia Huck94bb0632006-06-29 15:08:41 +0200720 ret = 0;
721 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 }
Cornelia Huck94bb0632006-06-29 15:08:41 +0200723 ret = time_to_avg_nsec(val, cmb->sample_count);
724out:
725 spin_unlock_irqrestore(cdev->ccwlock, flags);
726 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727}
728
Cornelia Huckfc5019c2007-10-12 16:11:15 +0200729static int readall_cmb(struct ccw_device *cdev, struct cmbdata *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730{
Cornelia Huck94bb0632006-06-29 15:08:41 +0200731 struct cmb *cmb;
732 struct cmb_data *cmb_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 u64 time;
Cornelia Huck94bb0632006-06-29 15:08:41 +0200734 unsigned long flags;
735 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
Cornelia Huck94bb0632006-06-29 15:08:41 +0200737 ret = cmf_cmb_copy_wait(cdev);
738 if (ret < 0)
739 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 spin_lock_irqsave(cdev->ccwlock, flags);
Cornelia Huck94bb0632006-06-29 15:08:41 +0200741 cmb_data = cdev->private->cmb;
742 if (!cmb_data) {
743 ret = -ENODEV;
744 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 }
Cornelia Huck94bb0632006-06-29 15:08:41 +0200746 if (cmb_data->last_update == 0) {
747 ret = -EAGAIN;
748 goto out;
749 }
750 cmb = cmb_data->last_block;
751 time = cmb_data->last_update - cdev->private->cmb_start_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
753 memset(data, 0, sizeof(struct cmbdata));
754
755 /* we only know values before device_busy_time */
756 data->size = offsetof(struct cmbdata, device_busy_time);
757
758 /* convert to nanoseconds */
759 data->elapsed_time = (time * 1000) >> 12;
760
761 /* copy data to new structure */
Cornelia Huck94bb0632006-06-29 15:08:41 +0200762 data->ssch_rsch_count = cmb->ssch_rsch_count;
763 data->sample_count = cmb->sample_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
765 /* time fields are converted to nanoseconds while copying */
Cornelia Huck94bb0632006-06-29 15:08:41 +0200766 data->device_connect_time = time_to_nsec(cmb->device_connect_time);
767 data->function_pending_time = time_to_nsec(cmb->function_pending_time);
768 data->device_disconnect_time =
769 time_to_nsec(cmb->device_disconnect_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 data->control_unit_queuing_time
Cornelia Huck94bb0632006-06-29 15:08:41 +0200771 = time_to_nsec(cmb->control_unit_queuing_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 data->device_active_only_time
Cornelia Huck94bb0632006-06-29 15:08:41 +0200773 = time_to_nsec(cmb->device_active_only_time);
774 ret = 0;
775out:
776 spin_unlock_irqrestore(cdev->ccwlock, flags);
777 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778}
779
Cornelia Huck94bb0632006-06-29 15:08:41 +0200780static void reset_cmb(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781{
Cornelia Huck94bb0632006-06-29 15:08:41 +0200782 cmf_generic_reset(cdev);
783}
784
785static void * align_cmb(void *area)
786{
787 return area;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788}
789
790static struct attribute_group cmf_attr_group;
791
792static struct cmb_operations cmbops_basic = {
793 .alloc = alloc_cmb,
794 .free = free_cmb,
795 .set = set_cmb,
796 .read = read_cmb,
797 .readall = readall_cmb,
798 .reset = reset_cmb,
Cornelia Huck94bb0632006-06-29 15:08:41 +0200799 .align = align_cmb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 .attr_group = &cmf_attr_group,
801};
802
803/* ******** extended cmb handling ********/
804
805/**
806 * struct cmbe - extended channel measurement block
807 *
808 * cmb as used by the hardware, may be in any 64 bit physical location,
809 * the fields are described in z/Architecture Principles of Operation,
810 * third edition, chapter 17.
811 */
812struct cmbe {
813 u32 ssch_rsch_count;
814 u32 sample_count;
815 u32 device_connect_time;
816 u32 function_pending_time;
817 u32 device_disconnect_time;
818 u32 control_unit_queuing_time;
819 u32 device_active_only_time;
820 u32 device_busy_time;
821 u32 initial_command_response_time;
822 u32 reserved[7];
823};
824
Cornelia Huckfc5019c2007-10-12 16:11:15 +0200825/*
826 * kmalloc only guarantees 8 byte alignment, but we need cmbe
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 * pointers to be naturally aligned. Make sure to allocate
Cornelia Huckfc5019c2007-10-12 16:11:15 +0200828 * enough space for two cmbes.
829 */
830static inline struct cmbe *cmbe_align(struct cmbe *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831{
832 unsigned long addr;
833 addr = ((unsigned long)c + sizeof (struct cmbe) - sizeof(long)) &
834 ~(sizeof (struct cmbe) - sizeof(long));
835 return (struct cmbe*)addr;
836}
837
Cornelia Huckfc5019c2007-10-12 16:11:15 +0200838static int alloc_cmbe(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839{
840 struct cmbe *cmbe;
Cornelia Huck94bb0632006-06-29 15:08:41 +0200841 struct cmb_data *cmb_data;
842 int ret;
843
844 cmbe = kzalloc (sizeof (*cmbe) * 2, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 if (!cmbe)
846 return -ENOMEM;
Cornelia Huck94bb0632006-06-29 15:08:41 +0200847 cmb_data = kzalloc(sizeof(struct cmb_data), GFP_KERNEL);
848 if (!cmb_data) {
849 ret = -ENOMEM;
850 goto out_free;
851 }
852 cmb_data->last_block = kzalloc(sizeof(struct cmbe), GFP_KERNEL);
853 if (!cmb_data->last_block) {
854 ret = -ENOMEM;
855 goto out_free;
856 }
857 cmb_data->size = sizeof(struct cmbe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 spin_lock_irq(cdev->ccwlock);
859 if (cdev->private->cmb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 spin_unlock_irq(cdev->ccwlock);
Cornelia Huck94bb0632006-06-29 15:08:41 +0200861 ret = -EBUSY;
862 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 }
Cornelia Huck94bb0632006-06-29 15:08:41 +0200864 cmb_data->hw_block = cmbe;
865 cdev->private->cmb = cmb_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 spin_unlock_irq(cdev->ccwlock);
867
868 /* activate global measurement if this is the first channel */
869 spin_lock(&cmb_area.lock);
870 if (list_empty(&cmb_area.list))
871 cmf_activate(NULL, 1);
872 list_add_tail(&cdev->private->cmb_list, &cmb_area.list);
873 spin_unlock(&cmb_area.lock);
874
875 return 0;
Cornelia Huck94bb0632006-06-29 15:08:41 +0200876out_free:
877 if (cmb_data)
878 kfree(cmb_data->last_block);
879 kfree(cmb_data);
880 kfree(cmbe);
881 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882}
883
Cornelia Huckfc5019c2007-10-12 16:11:15 +0200884static void free_cmbe(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885{
Cornelia Huck94bb0632006-06-29 15:08:41 +0200886 struct cmb_data *cmb_data;
887
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 spin_lock_irq(cdev->ccwlock);
Cornelia Huck94bb0632006-06-29 15:08:41 +0200889 cmb_data = cdev->private->cmb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 cdev->private->cmb = NULL;
Cornelia Huck94bb0632006-06-29 15:08:41 +0200891 if (cmb_data)
892 kfree(cmb_data->last_block);
893 kfree(cmb_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 spin_unlock_irq(cdev->ccwlock);
895
896 /* deactivate global measurement if this is the last channel */
897 spin_lock(&cmb_area.lock);
898 list_del_init(&cdev->private->cmb_list);
899 if (list_empty(&cmb_area.list))
900 cmf_activate(NULL, 0);
901 spin_unlock(&cmb_area.lock);
902}
903
Cornelia Huck94bb0632006-06-29 15:08:41 +0200904static int set_cmbe(struct ccw_device *cdev, u32 mme)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905{
906 unsigned long mba;
Cornelia Huck94bb0632006-06-29 15:08:41 +0200907 struct cmb_data *cmb_data;
908 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909
Cornelia Huck94bb0632006-06-29 15:08:41 +0200910 spin_lock_irqsave(cdev->ccwlock, flags);
911 if (!cdev->private->cmb) {
912 spin_unlock_irqrestore(cdev->ccwlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 return -EINVAL;
Cornelia Huck94bb0632006-06-29 15:08:41 +0200914 }
915 cmb_data = cdev->private->cmb;
916 mba = mme ? (unsigned long) cmbe_align(cmb_data->hw_block) : 0;
917 spin_unlock_irqrestore(cdev->ccwlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918
919 return set_schib_wait(cdev, mme, 1, mba);
920}
921
922
Cornelia Huckfc5019c2007-10-12 16:11:15 +0200923static u64 read_cmbe(struct ccw_device *cdev, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924{
Cornelia Huck94bb0632006-06-29 15:08:41 +0200925 struct cmbe *cmb;
926 struct cmb_data *cmb_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 u32 val;
Cornelia Huck94bb0632006-06-29 15:08:41 +0200928 int ret;
929 unsigned long flags;
930
931 ret = cmf_cmb_copy_wait(cdev);
932 if (ret < 0)
933 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934
935 spin_lock_irqsave(cdev->ccwlock, flags);
Cornelia Huck94bb0632006-06-29 15:08:41 +0200936 cmb_data = cdev->private->cmb;
937 if (!cmb_data) {
938 ret = 0;
939 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 }
Cornelia Huck94bb0632006-06-29 15:08:41 +0200941 cmb = cmb_data->last_block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942
943 switch (index) {
944 case cmb_ssch_rsch_count:
Cornelia Huck94bb0632006-06-29 15:08:41 +0200945 ret = cmb->ssch_rsch_count;
946 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 case cmb_sample_count:
Cornelia Huck94bb0632006-06-29 15:08:41 +0200948 ret = cmb->sample_count;
949 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 case cmb_device_connect_time:
Cornelia Huck94bb0632006-06-29 15:08:41 +0200951 val = cmb->device_connect_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 break;
953 case cmb_function_pending_time:
Cornelia Huck94bb0632006-06-29 15:08:41 +0200954 val = cmb->function_pending_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 break;
956 case cmb_device_disconnect_time:
Cornelia Huck94bb0632006-06-29 15:08:41 +0200957 val = cmb->device_disconnect_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 break;
959 case cmb_control_unit_queuing_time:
Cornelia Huck94bb0632006-06-29 15:08:41 +0200960 val = cmb->control_unit_queuing_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 break;
962 case cmb_device_active_only_time:
Cornelia Huck94bb0632006-06-29 15:08:41 +0200963 val = cmb->device_active_only_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 break;
965 case cmb_device_busy_time:
Cornelia Huck94bb0632006-06-29 15:08:41 +0200966 val = cmb->device_busy_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 break;
968 case cmb_initial_command_response_time:
Cornelia Huck94bb0632006-06-29 15:08:41 +0200969 val = cmb->initial_command_response_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 break;
971 default:
Cornelia Huck94bb0632006-06-29 15:08:41 +0200972 ret = 0;
973 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 }
Cornelia Huck94bb0632006-06-29 15:08:41 +0200975 ret = time_to_avg_nsec(val, cmb->sample_count);
976out:
977 spin_unlock_irqrestore(cdev->ccwlock, flags);
978 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979}
980
Cornelia Huckfc5019c2007-10-12 16:11:15 +0200981static int readall_cmbe(struct ccw_device *cdev, struct cmbdata *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982{
Cornelia Huck94bb0632006-06-29 15:08:41 +0200983 struct cmbe *cmb;
984 struct cmb_data *cmb_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 u64 time;
Cornelia Huck94bb0632006-06-29 15:08:41 +0200986 unsigned long flags;
987 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988
Cornelia Huck94bb0632006-06-29 15:08:41 +0200989 ret = cmf_cmb_copy_wait(cdev);
990 if (ret < 0)
991 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 spin_lock_irqsave(cdev->ccwlock, flags);
Cornelia Huck94bb0632006-06-29 15:08:41 +0200993 cmb_data = cdev->private->cmb;
994 if (!cmb_data) {
995 ret = -ENODEV;
996 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 }
Cornelia Huck94bb0632006-06-29 15:08:41 +0200998 if (cmb_data->last_update == 0) {
999 ret = -EAGAIN;
1000 goto out;
1001 }
1002 time = cmb_data->last_update - cdev->private->cmb_start_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003
1004 memset (data, 0, sizeof(struct cmbdata));
1005
1006 /* we only know values before device_busy_time */
1007 data->size = offsetof(struct cmbdata, device_busy_time);
1008
1009 /* conver to nanoseconds */
1010 data->elapsed_time = (time * 1000) >> 12;
1011
Cornelia Huck94bb0632006-06-29 15:08:41 +02001012 cmb = cmb_data->last_block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 /* copy data to new structure */
Cornelia Huck94bb0632006-06-29 15:08:41 +02001014 data->ssch_rsch_count = cmb->ssch_rsch_count;
1015 data->sample_count = cmb->sample_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016
1017 /* time fields are converted to nanoseconds while copying */
Cornelia Huck94bb0632006-06-29 15:08:41 +02001018 data->device_connect_time = time_to_nsec(cmb->device_connect_time);
1019 data->function_pending_time = time_to_nsec(cmb->function_pending_time);
1020 data->device_disconnect_time =
1021 time_to_nsec(cmb->device_disconnect_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 data->control_unit_queuing_time
Cornelia Huck94bb0632006-06-29 15:08:41 +02001023 = time_to_nsec(cmb->control_unit_queuing_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 data->device_active_only_time
Cornelia Huck94bb0632006-06-29 15:08:41 +02001025 = time_to_nsec(cmb->device_active_only_time);
1026 data->device_busy_time = time_to_nsec(cmb->device_busy_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 data->initial_command_response_time
Cornelia Huck94bb0632006-06-29 15:08:41 +02001028 = time_to_nsec(cmb->initial_command_response_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029
Cornelia Huck94bb0632006-06-29 15:08:41 +02001030 ret = 0;
1031out:
1032 spin_unlock_irqrestore(cdev->ccwlock, flags);
1033 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034}
1035
Cornelia Huck94bb0632006-06-29 15:08:41 +02001036static void reset_cmbe(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037{
Cornelia Huck94bb0632006-06-29 15:08:41 +02001038 cmf_generic_reset(cdev);
1039}
1040
1041static void * align_cmbe(void *area)
1042{
1043 return cmbe_align(area);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044}
1045
1046static struct attribute_group cmf_attr_group_ext;
1047
1048static struct cmb_operations cmbops_extended = {
1049 .alloc = alloc_cmbe,
1050 .free = free_cmbe,
1051 .set = set_cmbe,
1052 .read = read_cmbe,
1053 .readall = readall_cmbe,
1054 .reset = reset_cmbe,
Cornelia Huck94bb0632006-06-29 15:08:41 +02001055 .align = align_cmbe,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 .attr_group = &cmf_attr_group_ext,
1057};
1058
1059
Cornelia Huckfc5019c2007-10-12 16:11:15 +02001060static ssize_t cmb_show_attr(struct device *dev, char *buf, enum cmb_index idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061{
1062 return sprintf(buf, "%lld\n",
1063 (unsigned long long) cmf_read(to_ccwdev(dev), idx));
1064}
1065
Cornelia Huckfc5019c2007-10-12 16:11:15 +02001066static ssize_t cmb_show_avg_sample_interval(struct device *dev,
1067 struct device_attribute *attr,
1068 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069{
1070 struct ccw_device *cdev;
1071 long interval;
1072 unsigned long count;
Cornelia Huck94bb0632006-06-29 15:08:41 +02001073 struct cmb_data *cmb_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074
1075 cdev = to_ccwdev(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 count = cmf_read(cdev, cmb_sample_count);
Cornelia Huck94bb0632006-06-29 15:08:41 +02001077 spin_lock_irq(cdev->ccwlock);
1078 cmb_data = cdev->private->cmb;
1079 if (count) {
1080 interval = cmb_data->last_update -
1081 cdev->private->cmb_start_time;
Cornelia Huck13ffa922006-07-17 16:09:28 +02001082 interval = (interval * 1000) >> 12;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 interval /= count;
Cornelia Huck94bb0632006-06-29 15:08:41 +02001084 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 interval = -1;
Cornelia Huck94bb0632006-06-29 15:08:41 +02001086 spin_unlock_irq(cdev->ccwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 return sprintf(buf, "%ld\n", interval);
1088}
1089
Cornelia Huckfc5019c2007-10-12 16:11:15 +02001090static ssize_t cmb_show_avg_utilization(struct device *dev,
1091 struct device_attribute *attr,
1092 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093{
1094 struct cmbdata data;
1095 u64 utilization;
1096 unsigned long t, u;
1097 int ret;
1098
1099 ret = cmf_readall(to_ccwdev(dev), &data);
Cornelia Huck94bb0632006-06-29 15:08:41 +02001100 if (ret == -EAGAIN || ret == -ENODEV)
1101 /* No data (yet/currently) available to use for calculation. */
1102 return sprintf(buf, "n/a\n");
1103 else if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 return ret;
1105
1106 utilization = data.device_connect_time +
1107 data.function_pending_time +
1108 data.device_disconnect_time;
1109
1110 /* shift to avoid long long division */
1111 while (-1ul < (data.elapsed_time | utilization)) {
1112 utilization >>= 8;
1113 data.elapsed_time >>= 8;
1114 }
1115
1116 /* calculate value in 0.1 percent units */
1117 t = (unsigned long) data.elapsed_time / 1000;
1118 u = (unsigned long) utilization / t;
1119
1120 return sprintf(buf, "%02ld.%01ld%%\n", u/ 10, u - (u/ 10) * 10);
1121}
1122
1123#define cmf_attr(name) \
Cornelia Huckfc5019c2007-10-12 16:11:15 +02001124static ssize_t show_##name(struct device *dev, \
1125 struct device_attribute *attr, char *buf) \
1126{ return cmb_show_attr((dev), buf, cmb_##name); } \
1127static DEVICE_ATTR(name, 0444, show_##name, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128
1129#define cmf_attr_avg(name) \
Cornelia Huckfc5019c2007-10-12 16:11:15 +02001130static ssize_t show_avg_##name(struct device *dev, \
1131 struct device_attribute *attr, char *buf) \
1132{ return cmb_show_attr((dev), buf, cmb_##name); } \
1133static DEVICE_ATTR(avg_##name, 0444, show_avg_##name, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134
1135cmf_attr(ssch_rsch_count);
1136cmf_attr(sample_count);
1137cmf_attr_avg(device_connect_time);
1138cmf_attr_avg(function_pending_time);
1139cmf_attr_avg(device_disconnect_time);
1140cmf_attr_avg(control_unit_queuing_time);
1141cmf_attr_avg(device_active_only_time);
1142cmf_attr_avg(device_busy_time);
1143cmf_attr_avg(initial_command_response_time);
1144
Cornelia Huckfc5019c2007-10-12 16:11:15 +02001145static DEVICE_ATTR(avg_sample_interval, 0444, cmb_show_avg_sample_interval,
1146 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147static DEVICE_ATTR(avg_utilization, 0444, cmb_show_avg_utilization, NULL);
1148
1149static struct attribute *cmf_attributes[] = {
1150 &dev_attr_avg_sample_interval.attr,
1151 &dev_attr_avg_utilization.attr,
1152 &dev_attr_ssch_rsch_count.attr,
1153 &dev_attr_sample_count.attr,
1154 &dev_attr_avg_device_connect_time.attr,
1155 &dev_attr_avg_function_pending_time.attr,
1156 &dev_attr_avg_device_disconnect_time.attr,
1157 &dev_attr_avg_control_unit_queuing_time.attr,
1158 &dev_attr_avg_device_active_only_time.attr,
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001159 NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160};
1161
1162static struct attribute_group cmf_attr_group = {
1163 .name = "cmf",
1164 .attrs = cmf_attributes,
1165};
1166
1167static struct attribute *cmf_attributes_ext[] = {
1168 &dev_attr_avg_sample_interval.attr,
1169 &dev_attr_avg_utilization.attr,
1170 &dev_attr_ssch_rsch_count.attr,
1171 &dev_attr_sample_count.attr,
1172 &dev_attr_avg_device_connect_time.attr,
1173 &dev_attr_avg_function_pending_time.attr,
1174 &dev_attr_avg_device_disconnect_time.attr,
1175 &dev_attr_avg_control_unit_queuing_time.attr,
1176 &dev_attr_avg_device_active_only_time.attr,
1177 &dev_attr_avg_device_busy_time.attr,
1178 &dev_attr_avg_initial_command_response_time.attr,
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001179 NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180};
1181
1182static struct attribute_group cmf_attr_group_ext = {
1183 .name = "cmf",
1184 .attrs = cmf_attributes_ext,
1185};
1186
Cornelia Huckfc5019c2007-10-12 16:11:15 +02001187static ssize_t cmb_enable_show(struct device *dev,
1188 struct device_attribute *attr,
1189 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190{
1191 return sprintf(buf, "%d\n", to_ccwdev(dev)->private->cmb ? 1 : 0);
1192}
1193
Cornelia Huckfc5019c2007-10-12 16:11:15 +02001194static ssize_t cmb_enable_store(struct device *dev,
1195 struct device_attribute *attr, const char *buf,
1196 size_t c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197{
1198 struct ccw_device *cdev;
1199 int ret;
1200
1201 cdev = to_ccwdev(dev);
1202
1203 switch (buf[0]) {
1204 case '0':
1205 ret = disable_cmf(cdev);
1206 if (ret)
Cornelia Hucke556bbb2007-07-27 12:29:19 +02001207 dev_info(&cdev->dev, "disable_cmf failed (%d)\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 break;
1209 case '1':
1210 ret = enable_cmf(cdev);
1211 if (ret && ret != -EBUSY)
Cornelia Hucke556bbb2007-07-27 12:29:19 +02001212 dev_info(&cdev->dev, "enable_cmf failed (%d)\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 break;
1214 }
1215
1216 return c;
1217}
1218
1219DEVICE_ATTR(cmb_enable, 0644, cmb_enable_show, cmb_enable_store);
1220
1221/* enable_cmf/disable_cmf: module interface for cmf (de)activation */
Cornelia Huckfc5019c2007-10-12 16:11:15 +02001222int enable_cmf(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223{
1224 int ret;
1225
1226 ret = cmbops->alloc(cdev);
1227 cmbops->reset(cdev);
1228 if (ret)
1229 return ret;
1230 ret = cmbops->set(cdev, 2);
1231 if (ret) {
1232 cmbops->free(cdev);
1233 return ret;
1234 }
1235 ret = sysfs_create_group(&cdev->dev.kobj, cmbops->attr_group);
1236 if (!ret)
1237 return 0;
1238 cmbops->set(cdev, 0); //FIXME: this can fail
1239 cmbops->free(cdev);
1240 return ret;
1241}
1242
Cornelia Huckfc5019c2007-10-12 16:11:15 +02001243int disable_cmf(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244{
1245 int ret;
1246
1247 ret = cmbops->set(cdev, 0);
1248 if (ret)
1249 return ret;
1250 cmbops->free(cdev);
1251 sysfs_remove_group(&cdev->dev.kobj, cmbops->attr_group);
1252 return ret;
1253}
1254
Cornelia Huckfc5019c2007-10-12 16:11:15 +02001255u64 cmf_read(struct ccw_device *cdev, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256{
1257 return cmbops->read(cdev, index);
1258}
1259
Cornelia Huckfc5019c2007-10-12 16:11:15 +02001260int cmf_readall(struct ccw_device *cdev, struct cmbdata *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261{
1262 return cmbops->readall(cdev, data);
1263}
1264
Cornelia Huck94bb0632006-06-29 15:08:41 +02001265/* Reenable cmf when a disconnected device becomes available again. */
1266int cmf_reenable(struct ccw_device *cdev)
1267{
1268 cmbops->reset(cdev);
1269 return cmbops->set(cdev, 2);
1270}
1271
Cornelia Huckfc5019c2007-10-12 16:11:15 +02001272static int __init init_cmf(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273{
1274 char *format_string;
1275 char *detect_string = "parameter";
1276
Cornelia Huckfc5019c2007-10-12 16:11:15 +02001277 /*
1278 * If the user did not give a parameter, see if we are running on a
1279 * machine supporting extended measurement blocks, otherwise fall back
1280 * to basic mode.
1281 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 if (format == CMF_AUTODETECT) {
1283 if (!css_characteristics_avail ||
1284 !css_general_characteristics.ext_mb) {
1285 format = CMF_BASIC;
1286 } else {
1287 format = CMF_EXTENDED;
1288 }
1289 detect_string = "autodetected";
1290 } else {
1291 detect_string = "parameter";
1292 }
1293
1294 switch (format) {
1295 case CMF_BASIC:
1296 format_string = "basic";
1297 cmbops = &cmbops_basic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 break;
1299 case CMF_EXTENDED:
Cornelia Huckfc5019c2007-10-12 16:11:15 +02001300 format_string = "extended";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 cmbops = &cmbops_extended;
1302 break;
1303 default:
Cornelia Hucke556bbb2007-07-27 12:29:19 +02001304 printk(KERN_ERR "cio: Invalid format %d for channel "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 "measurement facility\n", format);
1306 return 1;
1307 }
1308
Cornelia Hucke556bbb2007-07-27 12:29:19 +02001309 printk(KERN_INFO "cio: Channel measurement facility using %s "
1310 "format (%s)\n", format_string, detect_string);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 return 0;
1312}
1313
1314module_init(init_cmf);
1315
1316
1317MODULE_AUTHOR("Arnd Bergmann <arndb@de.ibm.com>");
1318MODULE_LICENSE("GPL");
1319MODULE_DESCRIPTION("channel measurement facility base driver\n"
1320 "Copyright 2003 IBM Corporation\n");
1321
1322EXPORT_SYMBOL_GPL(enable_cmf);
1323EXPORT_SYMBOL_GPL(disable_cmf);
1324EXPORT_SYMBOL_GPL(cmf_read);
1325EXPORT_SYMBOL_GPL(cmf_readall);