blob: a90b28c0be5707f64bdd6a058ac416272756265c [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
Cornelia Huckc0208712007-10-12 16:11:16 +020077 * by all machines that we run on
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 * @CMF_EXTENDED: improved format that was introduced with the z990
Cornelia Huckc0208712007-10-12 16:11:16 +020079 * machine
80 * @CMF_AUTODETECT: default: use extended format when running on a machine
81 * supporting extended format, otherwise fall back to
82 * basic format
83 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070084enum cmb_format {
85 CMF_BASIC,
86 CMF_EXTENDED,
87 CMF_AUTODETECT = -1,
88};
Cornelia Huckfc5019c2007-10-12 16:11:15 +020089
Cornelia Huckc0208712007-10-12 16:11:16 +020090/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 * format - actual format for all measurement blocks
92 *
93 * The format module parameter can be set to a value of 0 (zero)
94 * or 1, indicating basic or extended format as described for
95 * enum cmb_format.
96 */
97static int format = CMF_AUTODETECT;
98module_param(format, bool, 0444);
99
100/**
101 * struct cmb_operations - functions to use depending on cmb_format
102 *
Cornelia Huck94bb0632006-06-29 15:08:41 +0200103 * Most of these functions operate on a struct ccw_device. There is only
104 * one instance of struct cmb_operations because the format of the measurement
105 * data is guaranteed to be the same for every ccw_device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 *
107 * @alloc: allocate memory for a channel measurement block,
108 * either with the help of a special pool or with kmalloc
109 * @free: free memory allocated with @alloc
110 * @set: enable or disable measurement
Cornelia Huckc0208712007-10-12 16:11:16 +0200111 * @read: read a measurement entry at an index
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 * @readall: read a measurement block in a common format
113 * @reset: clear the data in the associated measurement block and
114 * reset its time stamp
Cornelia Huck94bb0632006-06-29 15:08:41 +0200115 * @align: align an allocated block so that the hardware can use it
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 */
117struct cmb_operations {
Cornelia Huckfc5019c2007-10-12 16:11:15 +0200118 int (*alloc) (struct ccw_device *);
119 void (*free) (struct ccw_device *);
120 int (*set) (struct ccw_device *, u32);
121 u64 (*read) (struct ccw_device *, int);
122 int (*readall)(struct ccw_device *, struct cmbdata *);
123 void (*reset) (struct ccw_device *);
124 void *(*align) (void *);
Cornelia Huckc0208712007-10-12 16:11:16 +0200125/* private: */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 struct attribute_group *attr_group;
127};
128static struct cmb_operations *cmbops;
129
Cornelia Huck94bb0632006-06-29 15:08:41 +0200130struct cmb_data {
131 void *hw_block; /* Pointer to block updated by hardware */
132 void *last_block; /* Last changed block copied from hardware block */
133 int size; /* Size of hw_block and last_block */
134 unsigned long long last_update; /* when last_block was updated */
135};
136
Cornelia Huckfc5019c2007-10-12 16:11:15 +0200137/*
138 * Our user interface is designed in terms of nanoseconds,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 * while the hardware measures total times in its own
Cornelia Huckfc5019c2007-10-12 16:11:15 +0200140 * unit.
141 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142static inline u64 time_to_nsec(u32 value)
143{
144 return ((u64)value) * 128000ull;
145}
146
147/*
148 * Users are usually interested in average times,
149 * not accumulated time.
150 * This also helps us with atomicity problems
151 * when reading sinlge values.
152 */
153static inline u64 time_to_avg_nsec(u32 value, u32 count)
154{
155 u64 ret;
156
157 /* no samples yet, avoid division by 0 */
158 if (count == 0)
159 return 0;
160
Jan Engelhardt96de0e22007-10-19 23:21:04 +0200161 /* value comes in units of 128 µsec */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 ret = time_to_nsec(value);
163 do_div(ret, count);
164
165 return ret;
166}
167
Cornelia Huckfc5019c2007-10-12 16:11:15 +0200168/*
169 * Activate or deactivate the channel monitor. When area is NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 * the monitor is deactivated. The channel monitor needs to
171 * be active in order to measure subchannels, which also need
Cornelia Huckfc5019c2007-10-12 16:11:15 +0200172 * to be enabled.
173 */
174static inline void cmf_activate(void *area, unsigned int onoff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175{
176 register void * __gpr2 asm("2");
177 register long __gpr1 asm("1");
178
179 __gpr2 = area;
180 __gpr1 = onoff ? 2 : 0;
181 /* activate channel measurement */
182 asm("schm" : : "d" (__gpr2), "d" (__gpr1) );
183}
184
Cornelia Huckfc5019c2007-10-12 16:11:15 +0200185static int set_schib(struct ccw_device *cdev, u32 mme, int mbfc,
186 unsigned long address)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187{
188 int ret;
189 int retry;
190 struct subchannel *sch;
191 struct schib *schib;
192
193 sch = to_subchannel(cdev->dev.parent);
194 schib = &sch->schib;
195 /* msch can silently fail, so do it again if necessary */
196 for (retry = 0; retry < 3; retry++) {
197 /* prepare schib */
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800198 stsch(sch->schid, schib);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 schib->pmcw.mme = mme;
200 schib->pmcw.mbfc = mbfc;
201 /* address can be either a block address or a block index */
202 if (mbfc)
203 schib->mba = address;
204 else
205 schib->pmcw.mbi = address;
206
207 /* try to submit it */
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800208 switch(ret = msch_err(sch->schid, schib)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 case 0:
210 break;
211 case 1:
212 case 2: /* in I/O or status pending */
213 ret = -EBUSY;
214 break;
215 case 3: /* subchannel is no longer valid */
216 ret = -ENODEV;
217 break;
218 default: /* msch caught an exception */
219 ret = -EINVAL;
220 break;
221 }
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800222 stsch(sch->schid, schib); /* restore the schib */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
224 if (ret)
225 break;
226
227 /* check if it worked */
228 if (schib->pmcw.mme == mme &&
229 schib->pmcw.mbfc == mbfc &&
230 (mbfc ? (schib->mba == address)
231 : (schib->pmcw.mbi == address)))
232 return 0;
233
234 ret = -EINVAL;
235 }
236
237 return ret;
238}
239
240struct set_schib_struct {
241 u32 mme;
242 int mbfc;
243 unsigned long address;
244 wait_queue_head_t wait;
245 int ret;
Cornelia Huck94bb0632006-06-29 15:08:41 +0200246 struct kref kref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247};
248
Cornelia Huck94bb0632006-06-29 15:08:41 +0200249static void cmf_set_schib_release(struct kref *kref)
250{
251 struct set_schib_struct *set_data;
252
253 set_data = container_of(kref, struct set_schib_struct, kref);
254 kfree(set_data);
255}
256
257#define CMF_PENDING 1
258
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259static int set_schib_wait(struct ccw_device *cdev, u32 mme,
260 int mbfc, unsigned long address)
261{
Cornelia Huck94bb0632006-06-29 15:08:41 +0200262 struct set_schib_struct *set_data;
263 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
265 spin_lock_irq(cdev->ccwlock);
Cornelia Huck94bb0632006-06-29 15:08:41 +0200266 if (!cdev->private->cmb) {
267 ret = -ENODEV;
268 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 }
Cornelia Huck94bb0632006-06-29 15:08:41 +0200270 set_data = kzalloc(sizeof(struct set_schib_struct), GFP_ATOMIC);
271 if (!set_data) {
272 ret = -ENOMEM;
273 goto out;
274 }
275 init_waitqueue_head(&set_data->wait);
276 kref_init(&set_data->kref);
277 set_data->mme = mme;
278 set_data->mbfc = mbfc;
279 set_data->address = address;
280
281 ret = set_schib(cdev, mme, mbfc, address);
282 if (ret != -EBUSY)
283 goto out_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
285 if (cdev->private->state != DEV_STATE_ONLINE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 /* if the device is not online, don't even try again */
Cornelia Huck94bb0632006-06-29 15:08:41 +0200287 ret = -EBUSY;
288 goto out_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 }
Cornelia Huck94bb0632006-06-29 15:08:41 +0200290
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 cdev->private->state = DEV_STATE_CMFCHANGE;
Cornelia Huck94bb0632006-06-29 15:08:41 +0200292 set_data->ret = CMF_PENDING;
293 cdev->private->cmb_wait = set_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
295 spin_unlock_irq(cdev->ccwlock);
Cornelia Huck94bb0632006-06-29 15:08:41 +0200296 if (wait_event_interruptible(set_data->wait,
297 set_data->ret != CMF_PENDING)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 spin_lock_irq(cdev->ccwlock);
Cornelia Huck94bb0632006-06-29 15:08:41 +0200299 if (set_data->ret == CMF_PENDING) {
300 set_data->ret = -ERESTARTSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 if (cdev->private->state == DEV_STATE_CMFCHANGE)
302 cdev->private->state = DEV_STATE_ONLINE;
303 }
304 spin_unlock_irq(cdev->ccwlock);
305 }
Cornelia Huck94bb0632006-06-29 15:08:41 +0200306 spin_lock_irq(cdev->ccwlock);
307 cdev->private->cmb_wait = NULL;
308 ret = set_data->ret;
309out_put:
310 kref_put(&set_data->kref, cmf_set_schib_release);
311out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 spin_unlock_irq(cdev->ccwlock);
Cornelia Huck94bb0632006-06-29 15:08:41 +0200313 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314}
315
316void retry_set_schib(struct ccw_device *cdev)
317{
Cornelia Huck94bb0632006-06-29 15:08:41 +0200318 struct set_schib_struct *set_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
Cornelia Huck94bb0632006-06-29 15:08:41 +0200320 set_data = cdev->private->cmb_wait;
321 if (!set_data) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 WARN_ON(1);
323 return;
324 }
Cornelia Huck94bb0632006-06-29 15:08:41 +0200325 kref_get(&set_data->kref);
326 set_data->ret = set_schib(cdev, set_data->mme, set_data->mbfc,
327 set_data->address);
328 wake_up(&set_data->wait);
329 kref_put(&set_data->kref, cmf_set_schib_release);
330}
331
332static int cmf_copy_block(struct ccw_device *cdev)
333{
334 struct subchannel *sch;
335 void *reference_buf;
336 void *hw_block;
337 struct cmb_data *cmb_data;
338
339 sch = to_subchannel(cdev->dev.parent);
340
341 if (stsch(sch->schid, &sch->schib))
342 return -ENODEV;
343
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200344 if (scsw_fctl(&sch->schib.scsw) & SCSW_FCTL_START_FUNC) {
Cornelia Huck94bb0632006-06-29 15:08:41 +0200345 /* Don't copy if a start function is in progress. */
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200346 if ((!(scsw_actl(&sch->schib.scsw) & SCSW_ACTL_SUSPENDED)) &&
347 (scsw_actl(&sch->schib.scsw) &
Cornelia Huck94bb0632006-06-29 15:08:41 +0200348 (SCSW_ACTL_DEVACT | SCSW_ACTL_SCHACT)) &&
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200349 (!(scsw_stctl(&sch->schib.scsw) & SCSW_STCTL_SEC_STATUS)))
Cornelia Huck94bb0632006-06-29 15:08:41 +0200350 return -EBUSY;
351 }
352 cmb_data = cdev->private->cmb;
353 hw_block = cmbops->align(cmb_data->hw_block);
354 if (!memcmp(cmb_data->last_block, hw_block, cmb_data->size))
355 /* No need to copy. */
356 return 0;
357 reference_buf = kzalloc(cmb_data->size, GFP_ATOMIC);
358 if (!reference_buf)
359 return -ENOMEM;
360 /* Ensure consistency of block copied from hardware. */
361 do {
362 memcpy(cmb_data->last_block, hw_block, cmb_data->size);
363 memcpy(reference_buf, hw_block, cmb_data->size);
364 } while (memcmp(cmb_data->last_block, reference_buf, cmb_data->size));
365 cmb_data->last_update = get_clock();
366 kfree(reference_buf);
367 return 0;
368}
369
370struct copy_block_struct {
371 wait_queue_head_t wait;
372 int ret;
373 struct kref kref;
374};
375
376static void cmf_copy_block_release(struct kref *kref)
377{
378 struct copy_block_struct *copy_block;
379
380 copy_block = container_of(kref, struct copy_block_struct, kref);
381 kfree(copy_block);
382}
383
384static int cmf_cmb_copy_wait(struct ccw_device *cdev)
385{
386 struct copy_block_struct *copy_block;
387 int ret;
388 unsigned long flags;
389
390 spin_lock_irqsave(cdev->ccwlock, flags);
391 if (!cdev->private->cmb) {
392 ret = -ENODEV;
393 goto out;
394 }
395 copy_block = kzalloc(sizeof(struct copy_block_struct), GFP_ATOMIC);
396 if (!copy_block) {
397 ret = -ENOMEM;
398 goto out;
399 }
400 init_waitqueue_head(&copy_block->wait);
401 kref_init(&copy_block->kref);
402
403 ret = cmf_copy_block(cdev);
404 if (ret != -EBUSY)
405 goto out_put;
406
407 if (cdev->private->state != DEV_STATE_ONLINE) {
408 ret = -EBUSY;
409 goto out_put;
410 }
411
412 cdev->private->state = DEV_STATE_CMFUPDATE;
413 copy_block->ret = CMF_PENDING;
414 cdev->private->cmb_wait = copy_block;
415
416 spin_unlock_irqrestore(cdev->ccwlock, flags);
417 if (wait_event_interruptible(copy_block->wait,
418 copy_block->ret != CMF_PENDING)) {
419 spin_lock_irqsave(cdev->ccwlock, flags);
420 if (copy_block->ret == CMF_PENDING) {
421 copy_block->ret = -ERESTARTSYS;
422 if (cdev->private->state == DEV_STATE_CMFUPDATE)
423 cdev->private->state = DEV_STATE_ONLINE;
424 }
425 spin_unlock_irqrestore(cdev->ccwlock, flags);
426 }
427 spin_lock_irqsave(cdev->ccwlock, flags);
428 cdev->private->cmb_wait = NULL;
429 ret = copy_block->ret;
430out_put:
431 kref_put(&copy_block->kref, cmf_copy_block_release);
432out:
433 spin_unlock_irqrestore(cdev->ccwlock, flags);
434 return ret;
435}
436
437void cmf_retry_copy_block(struct ccw_device *cdev)
438{
439 struct copy_block_struct *copy_block;
440
441 copy_block = cdev->private->cmb_wait;
442 if (!copy_block) {
443 WARN_ON(1);
444 return;
445 }
446 kref_get(&copy_block->kref);
447 copy_block->ret = cmf_copy_block(cdev);
448 wake_up(&copy_block->wait);
449 kref_put(&copy_block->kref, cmf_copy_block_release);
450}
451
452static void cmf_generic_reset(struct ccw_device *cdev)
453{
454 struct cmb_data *cmb_data;
455
456 spin_lock_irq(cdev->ccwlock);
457 cmb_data = cdev->private->cmb;
458 if (cmb_data) {
459 memset(cmb_data->last_block, 0, cmb_data->size);
460 /*
461 * Need to reset hw block as well to make the hardware start
462 * from 0 again.
463 */
464 memset(cmbops->align(cmb_data->hw_block), 0, cmb_data->size);
465 cmb_data->last_update = 0;
466 }
467 cdev->private->cmb_start_time = get_clock();
468 spin_unlock_irq(cdev->ccwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469}
470
471/**
472 * struct cmb_area - container for global cmb data
473 *
474 * @mem: pointer to CMBs (only in basic measurement mode)
475 * @list: contains a linked list of all subchannels
Cornelia Huckc0208712007-10-12 16:11:16 +0200476 * @num_channels: number of channels to be measured
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 * @lock: protect concurrent access to @mem and @list
478 */
479struct cmb_area {
480 struct cmb *mem;
481 struct list_head list;
482 int num_channels;
483 spinlock_t lock;
484};
485
486static struct cmb_area cmb_area = {
Milind Arun Choudharycb629a02007-04-27 16:02:01 +0200487 .lock = __SPIN_LOCK_UNLOCKED(cmb_area.lock),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 .list = LIST_HEAD_INIT(cmb_area.list),
489 .num_channels = 1024,
490};
491
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492/* ****** old style CMB handling ********/
493
Cornelia Huckfc5019c2007-10-12 16:11:15 +0200494/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 * Basic channel measurement blocks are allocated in one contiguous
496 * block of memory, which can not be moved as long as any channel
497 * is active. Therefore, a maximum number of subchannels needs to
498 * be defined somewhere. This is a module parameter, defaulting to
499 * a resonable value of 1024, or 32 kb of memory.
500 * Current kernels don't allow kmalloc with more than 128kb, so the
Cornelia Huckfc5019c2007-10-12 16:11:15 +0200501 * maximum is 4096.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 */
503
504module_param_named(maxchannels, cmb_area.num_channels, uint, 0444);
505
506/**
507 * struct cmb - basic channel measurement block
Cornelia Huckc0208712007-10-12 16:11:16 +0200508 * @ssch_rsch_count: number of ssch and rsch
509 * @sample_count: number of samples
510 * @device_connect_time: time of device connect
511 * @function_pending_time: time of function pending
512 * @device_disconnect_time: time of device disconnect
513 * @control_unit_queuing_time: time of control unit queuing
514 * @device_active_only_time: time of device active only
515 * @reserved: unused in basic measurement mode
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 *
Cornelia Huckc0208712007-10-12 16:11:16 +0200517 * The measurement block as used by the hardware. The fields are described
518 * further in z/Architecture Principles of Operation, chapter 17.
519 *
520 * The cmb area made up from these blocks must be a contiguous array and may
521 * not be reallocated or freed.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 * Only one cmb area can be present in the system.
523 */
524struct cmb {
525 u16 ssch_rsch_count;
526 u16 sample_count;
527 u32 device_connect_time;
528 u32 function_pending_time;
529 u32 device_disconnect_time;
530 u32 control_unit_queuing_time;
531 u32 device_active_only_time;
532 u32 reserved[2];
533};
534
Cornelia Huckfc5019c2007-10-12 16:11:15 +0200535/*
536 * Insert a single device into the cmb_area list.
537 * Called with cmb_area.lock held from alloc_cmb.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 */
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100539static int alloc_cmb_single(struct ccw_device *cdev,
540 struct cmb_data *cmb_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541{
542 struct cmb *cmb;
543 struct ccw_device_private *node;
544 int ret;
545
546 spin_lock_irq(cdev->ccwlock);
547 if (!list_empty(&cdev->private->cmb_list)) {
548 ret = -EBUSY;
549 goto out;
550 }
551
Cornelia Huckfc5019c2007-10-12 16:11:15 +0200552 /*
553 * Find first unused cmb in cmb_area.mem.
554 * This is a little tricky: cmb_area.list
555 * remains sorted by ->cmb->hw_data pointers.
556 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 cmb = cmb_area.mem;
558 list_for_each_entry(node, &cmb_area.list, cmb_list) {
Cornelia Huck94bb0632006-06-29 15:08:41 +0200559 struct cmb_data *data;
560 data = node->cmb;
561 if ((struct cmb*)data->hw_block > cmb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 break;
563 cmb++;
564 }
565 if (cmb - cmb_area.mem >= cmb_area.num_channels) {
566 ret = -ENOMEM;
567 goto out;
568 }
569
570 /* insert new cmb */
571 list_add_tail(&cdev->private->cmb_list, &node->cmb_list);
Cornelia Huck94bb0632006-06-29 15:08:41 +0200572 cmb_data->hw_block = cmb;
573 cdev->private->cmb = cmb_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 ret = 0;
575out:
576 spin_unlock_irq(cdev->ccwlock);
577 return ret;
578}
579
Cornelia Huckfc5019c2007-10-12 16:11:15 +0200580static int alloc_cmb(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581{
582 int ret;
583 struct cmb *mem;
584 ssize_t size;
Cornelia Huck94bb0632006-06-29 15:08:41 +0200585 struct cmb_data *cmb_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
Cornelia Huck94bb0632006-06-29 15:08:41 +0200587 /* Allocate private cmb_data. */
588 cmb_data = kzalloc(sizeof(struct cmb_data), GFP_KERNEL);
589 if (!cmb_data)
590 return -ENOMEM;
591
592 cmb_data->last_block = kzalloc(sizeof(struct cmb), GFP_KERNEL);
593 if (!cmb_data->last_block) {
594 kfree(cmb_data);
595 return -ENOMEM;
596 }
597 cmb_data->size = sizeof(struct cmb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 spin_lock(&cmb_area.lock);
599
600 if (!cmb_area.mem) {
601 /* there is no user yet, so we need a new area */
602 size = sizeof(struct cmb) * cmb_area.num_channels;
603 WARN_ON(!list_empty(&cmb_area.list));
604
605 spin_unlock(&cmb_area.lock);
606 mem = (void*)__get_free_pages(GFP_KERNEL | GFP_DMA,
607 get_order(size));
608 spin_lock(&cmb_area.lock);
609
610 if (cmb_area.mem) {
611 /* ok, another thread was faster */
612 free_pages((unsigned long)mem, get_order(size));
613 } else if (!mem) {
614 /* no luck */
615 ret = -ENOMEM;
616 goto out;
617 } else {
618 /* everything ok */
619 memset(mem, 0, size);
620 cmb_area.mem = mem;
621 cmf_activate(cmb_area.mem, 1);
622 }
623 }
624
625 /* do the actual allocation */
Cornelia Huck94bb0632006-06-29 15:08:41 +0200626 ret = alloc_cmb_single(cdev, cmb_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627out:
628 spin_unlock(&cmb_area.lock);
Cornelia Huck94bb0632006-06-29 15:08:41 +0200629 if (ret) {
630 kfree(cmb_data->last_block);
631 kfree(cmb_data);
632 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 return ret;
634}
635
Cornelia Huck94bb0632006-06-29 15:08:41 +0200636static void free_cmb(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637{
638 struct ccw_device_private *priv;
Cornelia Huck94bb0632006-06-29 15:08:41 +0200639 struct cmb_data *cmb_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
641 spin_lock(&cmb_area.lock);
642 spin_lock_irq(cdev->ccwlock);
643
Cornelia Huck94bb0632006-06-29 15:08:41 +0200644 priv = cdev->private;
645
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 if (list_empty(&priv->cmb_list)) {
647 /* already freed */
648 goto out;
649 }
650
Cornelia Huck94bb0632006-06-29 15:08:41 +0200651 cmb_data = priv->cmb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 priv->cmb = NULL;
Cornelia Huck94bb0632006-06-29 15:08:41 +0200653 if (cmb_data)
654 kfree(cmb_data->last_block);
655 kfree(cmb_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 list_del_init(&priv->cmb_list);
657
658 if (list_empty(&cmb_area.list)) {
659 ssize_t size;
660 size = sizeof(struct cmb) * cmb_area.num_channels;
661 cmf_activate(NULL, 0);
662 free_pages((unsigned long)cmb_area.mem, get_order(size));
663 cmb_area.mem = NULL;
664 }
665out:
666 spin_unlock_irq(cdev->ccwlock);
667 spin_unlock(&cmb_area.lock);
668}
669
Cornelia Huck94bb0632006-06-29 15:08:41 +0200670static int set_cmb(struct ccw_device *cdev, u32 mme)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671{
672 u16 offset;
Cornelia Huck94bb0632006-06-29 15:08:41 +0200673 struct cmb_data *cmb_data;
674 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675
Cornelia Huck94bb0632006-06-29 15:08:41 +0200676 spin_lock_irqsave(cdev->ccwlock, flags);
677 if (!cdev->private->cmb) {
678 spin_unlock_irqrestore(cdev->ccwlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 return -EINVAL;
Cornelia Huck94bb0632006-06-29 15:08:41 +0200680 }
681 cmb_data = cdev->private->cmb;
682 offset = mme ? (struct cmb *)cmb_data->hw_block - cmb_area.mem : 0;
683 spin_unlock_irqrestore(cdev->ccwlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
685 return set_schib_wait(cdev, mme, 0, offset);
686}
687
Cornelia Huckfc5019c2007-10-12 16:11:15 +0200688static u64 read_cmb(struct ccw_device *cdev, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689{
Cornelia Huck94bb0632006-06-29 15:08:41 +0200690 struct cmb *cmb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 u32 val;
Cornelia Huck94bb0632006-06-29 15:08:41 +0200692 int ret;
693 unsigned long flags;
694
695 ret = cmf_cmb_copy_wait(cdev);
696 if (ret < 0)
697 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
699 spin_lock_irqsave(cdev->ccwlock, flags);
700 if (!cdev->private->cmb) {
Cornelia Huck94bb0632006-06-29 15:08:41 +0200701 ret = 0;
702 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 }
Cornelia Huck94bb0632006-06-29 15:08:41 +0200704 cmb = ((struct cmb_data *)cdev->private->cmb)->last_block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
706 switch (index) {
707 case cmb_ssch_rsch_count:
Cornelia Huck94bb0632006-06-29 15:08:41 +0200708 ret = cmb->ssch_rsch_count;
709 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 case cmb_sample_count:
Cornelia Huck94bb0632006-06-29 15:08:41 +0200711 ret = cmb->sample_count;
712 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 case cmb_device_connect_time:
Cornelia Huck94bb0632006-06-29 15:08:41 +0200714 val = cmb->device_connect_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 break;
716 case cmb_function_pending_time:
Cornelia Huck94bb0632006-06-29 15:08:41 +0200717 val = cmb->function_pending_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 break;
719 case cmb_device_disconnect_time:
Cornelia Huck94bb0632006-06-29 15:08:41 +0200720 val = cmb->device_disconnect_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 break;
722 case cmb_control_unit_queuing_time:
Cornelia Huck94bb0632006-06-29 15:08:41 +0200723 val = cmb->control_unit_queuing_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 break;
725 case cmb_device_active_only_time:
Cornelia Huck94bb0632006-06-29 15:08:41 +0200726 val = cmb->device_active_only_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 break;
728 default:
Cornelia Huck94bb0632006-06-29 15:08:41 +0200729 ret = 0;
730 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 }
Cornelia Huck94bb0632006-06-29 15:08:41 +0200732 ret = time_to_avg_nsec(val, cmb->sample_count);
733out:
734 spin_unlock_irqrestore(cdev->ccwlock, flags);
735 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736}
737
Cornelia Huckfc5019c2007-10-12 16:11:15 +0200738static int readall_cmb(struct ccw_device *cdev, struct cmbdata *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739{
Cornelia Huck94bb0632006-06-29 15:08:41 +0200740 struct cmb *cmb;
741 struct cmb_data *cmb_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 u64 time;
Cornelia Huck94bb0632006-06-29 15:08:41 +0200743 unsigned long flags;
744 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
Cornelia Huck94bb0632006-06-29 15:08:41 +0200746 ret = cmf_cmb_copy_wait(cdev);
747 if (ret < 0)
748 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 spin_lock_irqsave(cdev->ccwlock, flags);
Cornelia Huck94bb0632006-06-29 15:08:41 +0200750 cmb_data = cdev->private->cmb;
751 if (!cmb_data) {
752 ret = -ENODEV;
753 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 }
Cornelia Huck94bb0632006-06-29 15:08:41 +0200755 if (cmb_data->last_update == 0) {
756 ret = -EAGAIN;
757 goto out;
758 }
759 cmb = cmb_data->last_block;
760 time = cmb_data->last_update - cdev->private->cmb_start_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
762 memset(data, 0, sizeof(struct cmbdata));
763
764 /* we only know values before device_busy_time */
765 data->size = offsetof(struct cmbdata, device_busy_time);
766
767 /* convert to nanoseconds */
768 data->elapsed_time = (time * 1000) >> 12;
769
770 /* copy data to new structure */
Cornelia Huck94bb0632006-06-29 15:08:41 +0200771 data->ssch_rsch_count = cmb->ssch_rsch_count;
772 data->sample_count = cmb->sample_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
774 /* time fields are converted to nanoseconds while copying */
Cornelia Huck94bb0632006-06-29 15:08:41 +0200775 data->device_connect_time = time_to_nsec(cmb->device_connect_time);
776 data->function_pending_time = time_to_nsec(cmb->function_pending_time);
777 data->device_disconnect_time =
778 time_to_nsec(cmb->device_disconnect_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 data->control_unit_queuing_time
Cornelia Huck94bb0632006-06-29 15:08:41 +0200780 = time_to_nsec(cmb->control_unit_queuing_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 data->device_active_only_time
Cornelia Huck94bb0632006-06-29 15:08:41 +0200782 = time_to_nsec(cmb->device_active_only_time);
783 ret = 0;
784out:
785 spin_unlock_irqrestore(cdev->ccwlock, flags);
786 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787}
788
Cornelia Huck94bb0632006-06-29 15:08:41 +0200789static void reset_cmb(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790{
Cornelia Huck94bb0632006-06-29 15:08:41 +0200791 cmf_generic_reset(cdev);
792}
793
794static void * align_cmb(void *area)
795{
796 return area;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797}
798
799static struct attribute_group cmf_attr_group;
800
801static struct cmb_operations cmbops_basic = {
802 .alloc = alloc_cmb,
803 .free = free_cmb,
804 .set = set_cmb,
805 .read = read_cmb,
806 .readall = readall_cmb,
807 .reset = reset_cmb,
Cornelia Huck94bb0632006-06-29 15:08:41 +0200808 .align = align_cmb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 .attr_group = &cmf_attr_group,
810};
Heiko Carstens364c8552007-10-12 16:11:35 +0200811
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812/* ******** extended cmb handling ********/
813
814/**
815 * struct cmbe - extended channel measurement block
Cornelia Huckc0208712007-10-12 16:11:16 +0200816 * @ssch_rsch_count: number of ssch and rsch
817 * @sample_count: number of samples
818 * @device_connect_time: time of device connect
819 * @function_pending_time: time of function pending
820 * @device_disconnect_time: time of device disconnect
821 * @control_unit_queuing_time: time of control unit queuing
822 * @device_active_only_time: time of device active only
823 * @device_busy_time: time of device busy
824 * @initial_command_response_time: initial command response time
825 * @reserved: unused
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 *
Cornelia Huckc0208712007-10-12 16:11:16 +0200827 * The measurement block as used by the hardware. May be in any 64 bit physical
828 * location.
829 * The fields are described further in z/Architecture Principles of Operation,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 * third edition, chapter 17.
831 */
832struct cmbe {
833 u32 ssch_rsch_count;
834 u32 sample_count;
835 u32 device_connect_time;
836 u32 function_pending_time;
837 u32 device_disconnect_time;
838 u32 control_unit_queuing_time;
839 u32 device_active_only_time;
840 u32 device_busy_time;
841 u32 initial_command_response_time;
842 u32 reserved[7];
843};
844
Cornelia Huckfc5019c2007-10-12 16:11:15 +0200845/*
846 * kmalloc only guarantees 8 byte alignment, but we need cmbe
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 * pointers to be naturally aligned. Make sure to allocate
Cornelia Huckfc5019c2007-10-12 16:11:15 +0200848 * enough space for two cmbes.
849 */
850static inline struct cmbe *cmbe_align(struct cmbe *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851{
852 unsigned long addr;
853 addr = ((unsigned long)c + sizeof (struct cmbe) - sizeof(long)) &
854 ~(sizeof (struct cmbe) - sizeof(long));
855 return (struct cmbe*)addr;
856}
857
Cornelia Huckfc5019c2007-10-12 16:11:15 +0200858static int alloc_cmbe(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859{
860 struct cmbe *cmbe;
Cornelia Huck94bb0632006-06-29 15:08:41 +0200861 struct cmb_data *cmb_data;
862 int ret;
863
864 cmbe = kzalloc (sizeof (*cmbe) * 2, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 if (!cmbe)
866 return -ENOMEM;
Cornelia Huck94bb0632006-06-29 15:08:41 +0200867 cmb_data = kzalloc(sizeof(struct cmb_data), GFP_KERNEL);
868 if (!cmb_data) {
869 ret = -ENOMEM;
870 goto out_free;
871 }
872 cmb_data->last_block = kzalloc(sizeof(struct cmbe), GFP_KERNEL);
873 if (!cmb_data->last_block) {
874 ret = -ENOMEM;
875 goto out_free;
876 }
877 cmb_data->size = sizeof(struct cmbe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 spin_lock_irq(cdev->ccwlock);
879 if (cdev->private->cmb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 spin_unlock_irq(cdev->ccwlock);
Cornelia Huck94bb0632006-06-29 15:08:41 +0200881 ret = -EBUSY;
882 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 }
Cornelia Huck94bb0632006-06-29 15:08:41 +0200884 cmb_data->hw_block = cmbe;
885 cdev->private->cmb = cmb_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 spin_unlock_irq(cdev->ccwlock);
887
888 /* activate global measurement if this is the first channel */
889 spin_lock(&cmb_area.lock);
890 if (list_empty(&cmb_area.list))
891 cmf_activate(NULL, 1);
892 list_add_tail(&cdev->private->cmb_list, &cmb_area.list);
893 spin_unlock(&cmb_area.lock);
894
895 return 0;
Cornelia Huck94bb0632006-06-29 15:08:41 +0200896out_free:
897 if (cmb_data)
898 kfree(cmb_data->last_block);
899 kfree(cmb_data);
900 kfree(cmbe);
901 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902}
903
Cornelia Huckfc5019c2007-10-12 16:11:15 +0200904static void free_cmbe(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905{
Cornelia Huck94bb0632006-06-29 15:08:41 +0200906 struct cmb_data *cmb_data;
907
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 spin_lock_irq(cdev->ccwlock);
Cornelia Huck94bb0632006-06-29 15:08:41 +0200909 cmb_data = cdev->private->cmb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 cdev->private->cmb = NULL;
Cornelia Huck94bb0632006-06-29 15:08:41 +0200911 if (cmb_data)
912 kfree(cmb_data->last_block);
913 kfree(cmb_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 spin_unlock_irq(cdev->ccwlock);
915
916 /* deactivate global measurement if this is the last channel */
917 spin_lock(&cmb_area.lock);
918 list_del_init(&cdev->private->cmb_list);
919 if (list_empty(&cmb_area.list))
920 cmf_activate(NULL, 0);
921 spin_unlock(&cmb_area.lock);
922}
923
Cornelia Huck94bb0632006-06-29 15:08:41 +0200924static int set_cmbe(struct ccw_device *cdev, u32 mme)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925{
926 unsigned long mba;
Cornelia Huck94bb0632006-06-29 15:08:41 +0200927 struct cmb_data *cmb_data;
928 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929
Cornelia Huck94bb0632006-06-29 15:08:41 +0200930 spin_lock_irqsave(cdev->ccwlock, flags);
931 if (!cdev->private->cmb) {
932 spin_unlock_irqrestore(cdev->ccwlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 return -EINVAL;
Cornelia Huck94bb0632006-06-29 15:08:41 +0200934 }
935 cmb_data = cdev->private->cmb;
936 mba = mme ? (unsigned long) cmbe_align(cmb_data->hw_block) : 0;
937 spin_unlock_irqrestore(cdev->ccwlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
939 return set_schib_wait(cdev, mme, 1, mba);
940}
941
942
Cornelia Huckfc5019c2007-10-12 16:11:15 +0200943static u64 read_cmbe(struct ccw_device *cdev, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944{
Cornelia Huck94bb0632006-06-29 15:08:41 +0200945 struct cmbe *cmb;
946 struct cmb_data *cmb_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 u32 val;
Cornelia Huck94bb0632006-06-29 15:08:41 +0200948 int ret;
949 unsigned long flags;
950
951 ret = cmf_cmb_copy_wait(cdev);
952 if (ret < 0)
953 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954
955 spin_lock_irqsave(cdev->ccwlock, flags);
Cornelia Huck94bb0632006-06-29 15:08:41 +0200956 cmb_data = cdev->private->cmb;
957 if (!cmb_data) {
958 ret = 0;
959 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 }
Cornelia Huck94bb0632006-06-29 15:08:41 +0200961 cmb = cmb_data->last_block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962
963 switch (index) {
964 case cmb_ssch_rsch_count:
Cornelia Huck94bb0632006-06-29 15:08:41 +0200965 ret = cmb->ssch_rsch_count;
966 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 case cmb_sample_count:
Cornelia Huck94bb0632006-06-29 15:08:41 +0200968 ret = cmb->sample_count;
969 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 case cmb_device_connect_time:
Cornelia Huck94bb0632006-06-29 15:08:41 +0200971 val = cmb->device_connect_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 break;
973 case cmb_function_pending_time:
Cornelia Huck94bb0632006-06-29 15:08:41 +0200974 val = cmb->function_pending_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 break;
976 case cmb_device_disconnect_time:
Cornelia Huck94bb0632006-06-29 15:08:41 +0200977 val = cmb->device_disconnect_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 break;
979 case cmb_control_unit_queuing_time:
Cornelia Huck94bb0632006-06-29 15:08:41 +0200980 val = cmb->control_unit_queuing_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 break;
982 case cmb_device_active_only_time:
Cornelia Huck94bb0632006-06-29 15:08:41 +0200983 val = cmb->device_active_only_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 break;
985 case cmb_device_busy_time:
Cornelia Huck94bb0632006-06-29 15:08:41 +0200986 val = cmb->device_busy_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 break;
988 case cmb_initial_command_response_time:
Cornelia Huck94bb0632006-06-29 15:08:41 +0200989 val = cmb->initial_command_response_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 break;
991 default:
Cornelia Huck94bb0632006-06-29 15:08:41 +0200992 ret = 0;
993 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 }
Cornelia Huck94bb0632006-06-29 15:08:41 +0200995 ret = time_to_avg_nsec(val, cmb->sample_count);
996out:
997 spin_unlock_irqrestore(cdev->ccwlock, flags);
998 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999}
1000
Cornelia Huckfc5019c2007-10-12 16:11:15 +02001001static int readall_cmbe(struct ccw_device *cdev, struct cmbdata *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002{
Cornelia Huck94bb0632006-06-29 15:08:41 +02001003 struct cmbe *cmb;
1004 struct cmb_data *cmb_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 u64 time;
Cornelia Huck94bb0632006-06-29 15:08:41 +02001006 unsigned long flags;
1007 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008
Cornelia Huck94bb0632006-06-29 15:08:41 +02001009 ret = cmf_cmb_copy_wait(cdev);
1010 if (ret < 0)
1011 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 spin_lock_irqsave(cdev->ccwlock, flags);
Cornelia Huck94bb0632006-06-29 15:08:41 +02001013 cmb_data = cdev->private->cmb;
1014 if (!cmb_data) {
1015 ret = -ENODEV;
1016 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 }
Cornelia Huck94bb0632006-06-29 15:08:41 +02001018 if (cmb_data->last_update == 0) {
1019 ret = -EAGAIN;
1020 goto out;
1021 }
1022 time = cmb_data->last_update - cdev->private->cmb_start_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023
1024 memset (data, 0, sizeof(struct cmbdata));
1025
1026 /* we only know values before device_busy_time */
1027 data->size = offsetof(struct cmbdata, device_busy_time);
1028
1029 /* conver to nanoseconds */
1030 data->elapsed_time = (time * 1000) >> 12;
1031
Cornelia Huck94bb0632006-06-29 15:08:41 +02001032 cmb = cmb_data->last_block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 /* copy data to new structure */
Cornelia Huck94bb0632006-06-29 15:08:41 +02001034 data->ssch_rsch_count = cmb->ssch_rsch_count;
1035 data->sample_count = cmb->sample_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036
1037 /* time fields are converted to nanoseconds while copying */
Cornelia Huck94bb0632006-06-29 15:08:41 +02001038 data->device_connect_time = time_to_nsec(cmb->device_connect_time);
1039 data->function_pending_time = time_to_nsec(cmb->function_pending_time);
1040 data->device_disconnect_time =
1041 time_to_nsec(cmb->device_disconnect_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 data->control_unit_queuing_time
Cornelia Huck94bb0632006-06-29 15:08:41 +02001043 = time_to_nsec(cmb->control_unit_queuing_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 data->device_active_only_time
Cornelia Huck94bb0632006-06-29 15:08:41 +02001045 = time_to_nsec(cmb->device_active_only_time);
1046 data->device_busy_time = time_to_nsec(cmb->device_busy_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 data->initial_command_response_time
Cornelia Huck94bb0632006-06-29 15:08:41 +02001048 = time_to_nsec(cmb->initial_command_response_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049
Cornelia Huck94bb0632006-06-29 15:08:41 +02001050 ret = 0;
1051out:
1052 spin_unlock_irqrestore(cdev->ccwlock, flags);
1053 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054}
1055
Cornelia Huck94bb0632006-06-29 15:08:41 +02001056static void reset_cmbe(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057{
Cornelia Huck94bb0632006-06-29 15:08:41 +02001058 cmf_generic_reset(cdev);
1059}
1060
1061static void * align_cmbe(void *area)
1062{
1063 return cmbe_align(area);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064}
1065
1066static struct attribute_group cmf_attr_group_ext;
1067
1068static struct cmb_operations cmbops_extended = {
1069 .alloc = alloc_cmbe,
1070 .free = free_cmbe,
1071 .set = set_cmbe,
1072 .read = read_cmbe,
1073 .readall = readall_cmbe,
1074 .reset = reset_cmbe,
Cornelia Huck94bb0632006-06-29 15:08:41 +02001075 .align = align_cmbe,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 .attr_group = &cmf_attr_group_ext,
1077};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078
Cornelia Huckfc5019c2007-10-12 16:11:15 +02001079static ssize_t cmb_show_attr(struct device *dev, char *buf, enum cmb_index idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080{
1081 return sprintf(buf, "%lld\n",
1082 (unsigned long long) cmf_read(to_ccwdev(dev), idx));
1083}
1084
Cornelia Huckfc5019c2007-10-12 16:11:15 +02001085static ssize_t cmb_show_avg_sample_interval(struct device *dev,
1086 struct device_attribute *attr,
1087 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088{
1089 struct ccw_device *cdev;
1090 long interval;
1091 unsigned long count;
Cornelia Huck94bb0632006-06-29 15:08:41 +02001092 struct cmb_data *cmb_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093
1094 cdev = to_ccwdev(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 count = cmf_read(cdev, cmb_sample_count);
Cornelia Huck94bb0632006-06-29 15:08:41 +02001096 spin_lock_irq(cdev->ccwlock);
1097 cmb_data = cdev->private->cmb;
1098 if (count) {
1099 interval = cmb_data->last_update -
1100 cdev->private->cmb_start_time;
Cornelia Huck13ffa922006-07-17 16:09:28 +02001101 interval = (interval * 1000) >> 12;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 interval /= count;
Cornelia Huck94bb0632006-06-29 15:08:41 +02001103 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 interval = -1;
Cornelia Huck94bb0632006-06-29 15:08:41 +02001105 spin_unlock_irq(cdev->ccwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 return sprintf(buf, "%ld\n", interval);
1107}
1108
Cornelia Huckfc5019c2007-10-12 16:11:15 +02001109static ssize_t cmb_show_avg_utilization(struct device *dev,
1110 struct device_attribute *attr,
1111 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112{
1113 struct cmbdata data;
1114 u64 utilization;
1115 unsigned long t, u;
1116 int ret;
1117
1118 ret = cmf_readall(to_ccwdev(dev), &data);
Cornelia Huck94bb0632006-06-29 15:08:41 +02001119 if (ret == -EAGAIN || ret == -ENODEV)
1120 /* No data (yet/currently) available to use for calculation. */
1121 return sprintf(buf, "n/a\n");
1122 else if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 return ret;
1124
1125 utilization = data.device_connect_time +
1126 data.function_pending_time +
1127 data.device_disconnect_time;
1128
1129 /* shift to avoid long long division */
1130 while (-1ul < (data.elapsed_time | utilization)) {
1131 utilization >>= 8;
1132 data.elapsed_time >>= 8;
1133 }
1134
1135 /* calculate value in 0.1 percent units */
1136 t = (unsigned long) data.elapsed_time / 1000;
1137 u = (unsigned long) utilization / t;
1138
1139 return sprintf(buf, "%02ld.%01ld%%\n", u/ 10, u - (u/ 10) * 10);
1140}
1141
1142#define cmf_attr(name) \
Cornelia Huckfc5019c2007-10-12 16:11:15 +02001143static ssize_t show_##name(struct device *dev, \
1144 struct device_attribute *attr, char *buf) \
1145{ return cmb_show_attr((dev), buf, cmb_##name); } \
1146static DEVICE_ATTR(name, 0444, show_##name, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147
1148#define cmf_attr_avg(name) \
Cornelia Huckfc5019c2007-10-12 16:11:15 +02001149static ssize_t show_avg_##name(struct device *dev, \
1150 struct device_attribute *attr, char *buf) \
1151{ return cmb_show_attr((dev), buf, cmb_##name); } \
1152static DEVICE_ATTR(avg_##name, 0444, show_avg_##name, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153
1154cmf_attr(ssch_rsch_count);
1155cmf_attr(sample_count);
1156cmf_attr_avg(device_connect_time);
1157cmf_attr_avg(function_pending_time);
1158cmf_attr_avg(device_disconnect_time);
1159cmf_attr_avg(control_unit_queuing_time);
1160cmf_attr_avg(device_active_only_time);
1161cmf_attr_avg(device_busy_time);
1162cmf_attr_avg(initial_command_response_time);
1163
Cornelia Huckfc5019c2007-10-12 16:11:15 +02001164static DEVICE_ATTR(avg_sample_interval, 0444, cmb_show_avg_sample_interval,
1165 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166static DEVICE_ATTR(avg_utilization, 0444, cmb_show_avg_utilization, NULL);
1167
1168static struct attribute *cmf_attributes[] = {
1169 &dev_attr_avg_sample_interval.attr,
1170 &dev_attr_avg_utilization.attr,
1171 &dev_attr_ssch_rsch_count.attr,
1172 &dev_attr_sample_count.attr,
1173 &dev_attr_avg_device_connect_time.attr,
1174 &dev_attr_avg_function_pending_time.attr,
1175 &dev_attr_avg_device_disconnect_time.attr,
1176 &dev_attr_avg_control_unit_queuing_time.attr,
1177 &dev_attr_avg_device_active_only_time.attr,
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001178 NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179};
1180
1181static struct attribute_group cmf_attr_group = {
1182 .name = "cmf",
1183 .attrs = cmf_attributes,
1184};
1185
1186static struct attribute *cmf_attributes_ext[] = {
1187 &dev_attr_avg_sample_interval.attr,
1188 &dev_attr_avg_utilization.attr,
1189 &dev_attr_ssch_rsch_count.attr,
1190 &dev_attr_sample_count.attr,
1191 &dev_attr_avg_device_connect_time.attr,
1192 &dev_attr_avg_function_pending_time.attr,
1193 &dev_attr_avg_device_disconnect_time.attr,
1194 &dev_attr_avg_control_unit_queuing_time.attr,
1195 &dev_attr_avg_device_active_only_time.attr,
1196 &dev_attr_avg_device_busy_time.attr,
1197 &dev_attr_avg_initial_command_response_time.attr,
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001198 NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199};
1200
1201static struct attribute_group cmf_attr_group_ext = {
1202 .name = "cmf",
1203 .attrs = cmf_attributes_ext,
1204};
1205
Cornelia Huckfc5019c2007-10-12 16:11:15 +02001206static ssize_t cmb_enable_show(struct device *dev,
1207 struct device_attribute *attr,
1208 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209{
1210 return sprintf(buf, "%d\n", to_ccwdev(dev)->private->cmb ? 1 : 0);
1211}
1212
Cornelia Huckfc5019c2007-10-12 16:11:15 +02001213static ssize_t cmb_enable_store(struct device *dev,
1214 struct device_attribute *attr, const char *buf,
1215 size_t c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216{
1217 struct ccw_device *cdev;
1218 int ret;
Cornelia Huck2f972202008-04-30 13:38:33 +02001219 unsigned long val;
1220
1221 ret = strict_strtoul(buf, 16, &val);
1222 if (ret)
1223 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224
1225 cdev = to_ccwdev(dev);
1226
Cornelia Huck2f972202008-04-30 13:38:33 +02001227 switch (val) {
1228 case 0:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 ret = disable_cmf(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 break;
Cornelia Huck2f972202008-04-30 13:38:33 +02001231 case 1:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 ret = enable_cmf(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 break;
1234 }
1235
1236 return c;
1237}
1238
1239DEVICE_ATTR(cmb_enable, 0644, cmb_enable_show, cmb_enable_store);
1240
Cornelia Huckc0208712007-10-12 16:11:16 +02001241/**
1242 * enable_cmf() - switch on the channel measurement for a specific device
1243 * @cdev: The ccw device to be enabled
1244 *
1245 * Returns %0 for success or a negative error value.
1246 *
1247 * Context:
1248 * non-atomic
1249 */
Cornelia Huckfc5019c2007-10-12 16:11:15 +02001250int enable_cmf(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251{
1252 int ret;
1253
1254 ret = cmbops->alloc(cdev);
1255 cmbops->reset(cdev);
1256 if (ret)
1257 return ret;
1258 ret = cmbops->set(cdev, 2);
1259 if (ret) {
1260 cmbops->free(cdev);
1261 return ret;
1262 }
1263 ret = sysfs_create_group(&cdev->dev.kobj, cmbops->attr_group);
1264 if (!ret)
1265 return 0;
1266 cmbops->set(cdev, 0); //FIXME: this can fail
1267 cmbops->free(cdev);
1268 return ret;
1269}
1270
Cornelia Huckc0208712007-10-12 16:11:16 +02001271/**
1272 * disable_cmf() - switch off the channel measurement for a specific device
1273 * @cdev: The ccw device to be disabled
1274 *
1275 * Returns %0 for success or a negative error value.
1276 *
1277 * Context:
1278 * non-atomic
1279 */
Cornelia Huckfc5019c2007-10-12 16:11:15 +02001280int disable_cmf(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281{
1282 int ret;
1283
1284 ret = cmbops->set(cdev, 0);
1285 if (ret)
1286 return ret;
1287 cmbops->free(cdev);
1288 sysfs_remove_group(&cdev->dev.kobj, cmbops->attr_group);
1289 return ret;
1290}
1291
Cornelia Huckc0208712007-10-12 16:11:16 +02001292/**
1293 * cmf_read() - read one value from the current channel measurement block
1294 * @cdev: the channel to be read
1295 * @index: the index of the value to be read
1296 *
1297 * Returns the value read or %0 if the value cannot be read.
1298 *
1299 * Context:
1300 * any
1301 */
Cornelia Huckfc5019c2007-10-12 16:11:15 +02001302u64 cmf_read(struct ccw_device *cdev, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303{
1304 return cmbops->read(cdev, index);
1305}
1306
Cornelia Huckc0208712007-10-12 16:11:16 +02001307/**
1308 * cmf_readall() - read the current channel measurement block
1309 * @cdev: the channel to be read
1310 * @data: a pointer to a data block that will be filled
1311 *
1312 * Returns %0 on success, a negative error value otherwise.
1313 *
1314 * Context:
1315 * any
1316 */
Cornelia Huckfc5019c2007-10-12 16:11:15 +02001317int cmf_readall(struct ccw_device *cdev, struct cmbdata *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318{
1319 return cmbops->readall(cdev, data);
1320}
1321
Cornelia Huck94bb0632006-06-29 15:08:41 +02001322/* Reenable cmf when a disconnected device becomes available again. */
1323int cmf_reenable(struct ccw_device *cdev)
1324{
1325 cmbops->reset(cdev);
1326 return cmbops->set(cdev, 2);
1327}
1328
Cornelia Huckfc5019c2007-10-12 16:11:15 +02001329static int __init init_cmf(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330{
1331 char *format_string;
1332 char *detect_string = "parameter";
1333
Cornelia Huckfc5019c2007-10-12 16:11:15 +02001334 /*
1335 * If the user did not give a parameter, see if we are running on a
1336 * machine supporting extended measurement blocks, otherwise fall back
1337 * to basic mode.
1338 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 if (format == CMF_AUTODETECT) {
Cornelia Huck75784c02008-07-14 09:58:57 +02001340 if (!css_general_characteristics.ext_mb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 format = CMF_BASIC;
1342 } else {
1343 format = CMF_EXTENDED;
1344 }
1345 detect_string = "autodetected";
1346 } else {
1347 detect_string = "parameter";
1348 }
1349
1350 switch (format) {
1351 case CMF_BASIC:
1352 format_string = "basic";
1353 cmbops = &cmbops_basic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 break;
1355 case CMF_EXTENDED:
Cornelia Huckfc5019c2007-10-12 16:11:15 +02001356 format_string = "extended";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 cmbops = &cmbops_extended;
1358 break;
1359 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 return 1;
1361 }
1362
Cornelia Hucke556bbb2007-07-27 12:29:19 +02001363 printk(KERN_INFO "cio: Channel measurement facility using %s "
1364 "format (%s)\n", format_string, detect_string);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 return 0;
1366}
1367
1368module_init(init_cmf);
1369
1370
1371MODULE_AUTHOR("Arnd Bergmann <arndb@de.ibm.com>");
1372MODULE_LICENSE("GPL");
1373MODULE_DESCRIPTION("channel measurement facility base driver\n"
1374 "Copyright 2003 IBM Corporation\n");
1375
1376EXPORT_SYMBOL_GPL(enable_cmf);
1377EXPORT_SYMBOL_GPL(disable_cmf);
1378EXPORT_SYMBOL_GPL(cmf_read);
1379EXPORT_SYMBOL_GPL(cmf_readall);