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