[S390] cio: Kerneldoc comments for cmf.
[safe/jmp/linux-2.6] / drivers / s390 / cio / cmf.c
1 /*
2  * linux/drivers/s390/cio/cmf.c
3  *
4  * Linux on zSeries Channel Measurement Facility support
5  *
6  * Copyright 2000,2006 IBM Corporation
7  *
8  * Authors: Arnd Bergmann <arndb@de.ibm.com>
9  *          Cornelia Huck <cornelia.huck@de.ibm.com>
10  *
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>
34 #include <linux/slab.h>
35 #include <linux/timex.h>        /* get_clock() */
36
37 #include <asm/ccwdev.h>
38 #include <asm/cio.h>
39 #include <asm/cmb.h>
40 #include <asm/div64.h>
41
42 #include "cio.h"
43 #include "css.h"
44 #include "device.h"
45 #include "ioasm.h"
46 #include "chsc.h"
47
48 /*
49  * parameter to enable cmf during boot, possible uses are:
50  *  "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 */
59 enum cmb_index {
60  /* basic and exended format: */
61         cmb_ssch_rsch_count,
62         cmb_sample_count,
63         cmb_device_connect_time,
64         cmb_function_pending_time,
65         cmb_device_disconnect_time,
66         cmb_control_unit_queuing_time,
67         cmb_device_active_only_time,
68  /* extended format only: */
69         cmb_device_busy_time,
70         cmb_initial_command_response_time,
71 };
72
73 /**
74  * enum cmb_format - types of supported measurement block formats
75  *
76  * @CMF_BASIC:      traditional channel measurement blocks supported
77  *                  by all machines that we run on
78  * @CMF_EXTENDED:   improved format that was introduced with the z990
79  *                  machine
80  * @CMF_AUTODETECT: default: use extended format when running on a machine
81  *                  supporting extended format, otherwise fall back to
82  *                  basic format
83  */
84 enum cmb_format {
85         CMF_BASIC,
86         CMF_EXTENDED,
87         CMF_AUTODETECT = -1,
88 };
89
90 /*
91  * 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  */
97 static int format = CMF_AUTODETECT;
98 module_param(format, bool, 0444);
99
100 /**
101  * struct cmb_operations - functions to use depending on cmb_format
102  *
103  * 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.
106  *
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
111  * @read:       read a measurement entry at an index
112  * @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
115  * @align:      align an allocated block so that the hardware can use it
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         void *(*align) (void *);
125 /* private: */
126         struct attribute_group *attr_group;
127 };
128 static struct cmb_operations *cmbops;
129
130 struct 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
137 /*
138  * Our user interface is designed in terms of nanoseconds,
139  * while the hardware measures total times in its own
140  * unit.
141  */
142 static 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  */
153 static 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
161         /* value comes in units of 128 µsec */
162         ret = time_to_nsec(value);
163         do_div(ret, count);
164
165         return ret;
166 }
167
168 /*
169  * Activate or deactivate the channel monitor. When area is NULL,
170  * the monitor is deactivated. The channel monitor needs to
171  * be active in order to measure subchannels, which also need
172  * to be enabled.
173  */
174 static inline void cmf_activate(void *area, unsigned int onoff)
175 {
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
185 static int set_schib(struct ccw_device *cdev, u32 mme, int mbfc,
186                      unsigned long address)
187 {
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 */
198                 stsch(sch->schid, schib);
199                 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 */
208                 switch(ret = msch_err(sch->schid, schib)) {
209                         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                 }
222                 stsch(sch->schid, schib); /* restore the schib */
223
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
240 struct set_schib_struct {
241         u32 mme;
242         int mbfc;
243         unsigned long address;
244         wait_queue_head_t wait;
245         int ret;
246         struct kref kref;
247 };
248
249 static 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
259 static int set_schib_wait(struct ccw_device *cdev, u32 mme,
260                                 int mbfc, unsigned long address)
261 {
262         struct set_schib_struct *set_data;
263         int ret;
264
265         spin_lock_irq(cdev->ccwlock);
266         if (!cdev->private->cmb) {
267                 ret = -ENODEV;
268                 goto out;
269         }
270         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;
284
285         if (cdev->private->state != DEV_STATE_ONLINE) {
286                 /* if the device is not online, don't even try again */
287                 ret = -EBUSY;
288                 goto out_put;
289         }
290
291         cdev->private->state = DEV_STATE_CMFCHANGE;
292         set_data->ret = CMF_PENDING;
293         cdev->private->cmb_wait = set_data;
294
295         spin_unlock_irq(cdev->ccwlock);
296         if (wait_event_interruptible(set_data->wait,
297                                      set_data->ret != CMF_PENDING)) {
298                 spin_lock_irq(cdev->ccwlock);
299                 if (set_data->ret == CMF_PENDING) {
300                         set_data->ret = -ERESTARTSYS;
301                         if (cdev->private->state == DEV_STATE_CMFCHANGE)
302                                 cdev->private->state = DEV_STATE_ONLINE;
303                 }
304                 spin_unlock_irq(cdev->ccwlock);
305         }
306         spin_lock_irq(cdev->ccwlock);
307         cdev->private->cmb_wait = NULL;
308         ret = set_data->ret;
309 out_put:
310         kref_put(&set_data->kref, cmf_set_schib_release);
311 out:
312         spin_unlock_irq(cdev->ccwlock);
313         return ret;
314 }
315
316 void retry_set_schib(struct ccw_device *cdev)
317 {
318         struct set_schib_struct *set_data;
319
320         set_data = cdev->private->cmb_wait;
321         if (!set_data) {
322                 WARN_ON(1);
323                 return;
324         }
325         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
332 static 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
344         if (sch->schib.scsw.fctl & SCSW_FCTL_START_FUNC) {
345                 /* Don't copy if a start function is in progress. */
346                 if ((!sch->schib.scsw.actl & SCSW_ACTL_SUSPENDED) &&
347                     (sch->schib.scsw.actl &
348                      (SCSW_ACTL_DEVACT | SCSW_ACTL_SCHACT)) &&
349                     (!sch->schib.scsw.stctl & SCSW_STCTL_SEC_STATUS))
350                         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
370 struct copy_block_struct {
371         wait_queue_head_t wait;
372         int ret;
373         struct kref kref;
374 };
375
376 static 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
384 static 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;
430 out_put:
431         kref_put(&copy_block->kref, cmf_copy_block_release);
432 out:
433         spin_unlock_irqrestore(cdev->ccwlock, flags);
434         return ret;
435 }
436
437 void 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
452 static 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);
469 }
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
476  * @num_channels: number of channels to be measured
477  * @lock:       protect concurrent access to @mem and @list
478  */
479 struct cmb_area {
480         struct cmb *mem;
481         struct list_head list;
482         int num_channels;
483         spinlock_t lock;
484 };
485
486 static struct cmb_area cmb_area = {
487         .lock = __SPIN_LOCK_UNLOCKED(cmb_area.lock),
488         .list = LIST_HEAD_INIT(cmb_area.list),
489         .num_channels  = 1024,
490 };
491
492 \f
493 /* ****** old style CMB handling ********/
494
495 /*
496  * Basic channel measurement blocks are allocated in one contiguous
497  * block of memory, which can not be moved as long as any channel
498  * is active. Therefore, a maximum number of subchannels needs to
499  * be defined somewhere. This is a module parameter, defaulting to
500  * a resonable value of 1024, or 32 kb of memory.
501  * Current kernels don't allow kmalloc with more than 128kb, so the
502  * maximum is 4096.
503  */
504
505 module_param_named(maxchannels, cmb_area.num_channels, uint, 0444);
506
507 /**
508  * struct cmb - basic channel measurement block
509  * @ssch_rsch_count: number of ssch and rsch
510  * @sample_count: number of samples
511  * @device_connect_time: time of device connect
512  * @function_pending_time: time of function pending
513  * @device_disconnect_time: time of device disconnect
514  * @control_unit_queuing_time: time of control unit queuing
515  * @device_active_only_time: time of device active only
516  * @reserved: unused in basic measurement mode
517  *
518  * The measurement block as used by the hardware. The fields are described
519  * further in z/Architecture Principles of Operation, chapter 17.
520  *
521  * The cmb area made up from these blocks must be a contiguous array and may
522  * not be reallocated or freed.
523  * Only one cmb area can be present in the system.
524  */
525 struct cmb {
526         u16 ssch_rsch_count;
527         u16 sample_count;
528         u32 device_connect_time;
529         u32 function_pending_time;
530         u32 device_disconnect_time;
531         u32 control_unit_queuing_time;
532         u32 device_active_only_time;
533         u32 reserved[2];
534 };
535
536 /*
537  * Insert a single device into the cmb_area list.
538  * Called with cmb_area.lock held from alloc_cmb.
539  */
540 static int alloc_cmb_single(struct ccw_device *cdev,
541                             struct cmb_data *cmb_data)
542 {
543         struct cmb *cmb;
544         struct ccw_device_private *node;
545         int ret;
546
547         spin_lock_irq(cdev->ccwlock);
548         if (!list_empty(&cdev->private->cmb_list)) {
549                 ret = -EBUSY;
550                 goto out;
551         }
552
553         /*
554          * Find first unused cmb in cmb_area.mem.
555          * This is a little tricky: cmb_area.list
556          * remains sorted by ->cmb->hw_data pointers.
557          */
558         cmb = cmb_area.mem;
559         list_for_each_entry(node, &cmb_area.list, cmb_list) {
560                 struct cmb_data *data;
561                 data = node->cmb;
562                 if ((struct cmb*)data->hw_block > cmb)
563                         break;
564                 cmb++;
565         }
566         if (cmb - cmb_area.mem >= cmb_area.num_channels) {
567                 ret = -ENOMEM;
568                 goto out;
569         }
570
571         /* insert new cmb */
572         list_add_tail(&cdev->private->cmb_list, &node->cmb_list);
573         cmb_data->hw_block = cmb;
574         cdev->private->cmb = cmb_data;
575         ret = 0;
576 out:
577         spin_unlock_irq(cdev->ccwlock);
578         return ret;
579 }
580
581 static int alloc_cmb(struct ccw_device *cdev)
582 {
583         int ret;
584         struct cmb *mem;
585         ssize_t size;
586         struct cmb_data *cmb_data;
587
588         /* Allocate private cmb_data. */
589         cmb_data = kzalloc(sizeof(struct cmb_data), GFP_KERNEL);
590         if (!cmb_data)
591                 return -ENOMEM;
592
593         cmb_data->last_block = kzalloc(sizeof(struct cmb), GFP_KERNEL);
594         if (!cmb_data->last_block) {
595                 kfree(cmb_data);
596                 return -ENOMEM;
597         }
598         cmb_data->size = sizeof(struct cmb);
599         spin_lock(&cmb_area.lock);
600
601         if (!cmb_area.mem) {
602                 /* there is no user yet, so we need a new area */
603                 size = sizeof(struct cmb) * cmb_area.num_channels;
604                 WARN_ON(!list_empty(&cmb_area.list));
605
606                 spin_unlock(&cmb_area.lock);
607                 mem = (void*)__get_free_pages(GFP_KERNEL | GFP_DMA,
608                                  get_order(size));
609                 spin_lock(&cmb_area.lock);
610
611                 if (cmb_area.mem) {
612                         /* ok, another thread was faster */
613                         free_pages((unsigned long)mem, get_order(size));
614                 } else if (!mem) {
615                         /* no luck */
616                         printk(KERN_WARNING "cio: failed to allocate area "
617                                "for measuring %d subchannels\n",
618                                cmb_area.num_channels);
619                         ret = -ENOMEM;
620                         goto out;
621                 } else {
622                         /* everything ok */
623                         memset(mem, 0, size);
624                         cmb_area.mem = mem;
625                         cmf_activate(cmb_area.mem, 1);
626                 }
627         }
628
629         /* do the actual allocation */
630         ret = alloc_cmb_single(cdev, cmb_data);
631 out:
632         spin_unlock(&cmb_area.lock);
633         if (ret) {
634                 kfree(cmb_data->last_block);
635                 kfree(cmb_data);
636         }
637         return ret;
638 }
639
640 static void free_cmb(struct ccw_device *cdev)
641 {
642         struct ccw_device_private *priv;
643         struct cmb_data *cmb_data;
644
645         spin_lock(&cmb_area.lock);
646         spin_lock_irq(cdev->ccwlock);
647
648         priv = cdev->private;
649
650         if (list_empty(&priv->cmb_list)) {
651                 /* already freed */
652                 goto out;
653         }
654
655         cmb_data = priv->cmb;
656         priv->cmb = NULL;
657         if (cmb_data)
658                 kfree(cmb_data->last_block);
659         kfree(cmb_data);
660         list_del_init(&priv->cmb_list);
661
662         if (list_empty(&cmb_area.list)) {
663                 ssize_t size;
664                 size = sizeof(struct cmb) * cmb_area.num_channels;
665                 cmf_activate(NULL, 0);
666                 free_pages((unsigned long)cmb_area.mem, get_order(size));
667                 cmb_area.mem = NULL;
668         }
669 out:
670         spin_unlock_irq(cdev->ccwlock);
671         spin_unlock(&cmb_area.lock);
672 }
673
674 static int set_cmb(struct ccw_device *cdev, u32 mme)
675 {
676         u16 offset;
677         struct cmb_data *cmb_data;
678         unsigned long flags;
679
680         spin_lock_irqsave(cdev->ccwlock, flags);
681         if (!cdev->private->cmb) {
682                 spin_unlock_irqrestore(cdev->ccwlock, flags);
683                 return -EINVAL;
684         }
685         cmb_data = cdev->private->cmb;
686         offset = mme ? (struct cmb *)cmb_data->hw_block - cmb_area.mem : 0;
687         spin_unlock_irqrestore(cdev->ccwlock, flags);
688
689         return set_schib_wait(cdev, mme, 0, offset);
690 }
691
692 static u64 read_cmb(struct ccw_device *cdev, int index)
693 {
694         struct cmb *cmb;
695         u32 val;
696         int ret;
697         unsigned long flags;
698
699         ret = cmf_cmb_copy_wait(cdev);
700         if (ret < 0)
701                 return 0;
702
703         spin_lock_irqsave(cdev->ccwlock, flags);
704         if (!cdev->private->cmb) {
705                 ret = 0;
706                 goto out;
707         }
708         cmb = ((struct cmb_data *)cdev->private->cmb)->last_block;
709
710         switch (index) {
711         case cmb_ssch_rsch_count:
712                 ret = cmb->ssch_rsch_count;
713                 goto out;
714         case cmb_sample_count:
715                 ret = cmb->sample_count;
716                 goto out;
717         case cmb_device_connect_time:
718                 val = cmb->device_connect_time;
719                 break;
720         case cmb_function_pending_time:
721                 val = cmb->function_pending_time;
722                 break;
723         case cmb_device_disconnect_time:
724                 val = cmb->device_disconnect_time;
725                 break;
726         case cmb_control_unit_queuing_time:
727                 val = cmb->control_unit_queuing_time;
728                 break;
729         case cmb_device_active_only_time:
730                 val = cmb->device_active_only_time;
731                 break;
732         default:
733                 ret = 0;
734                 goto out;
735         }
736         ret = time_to_avg_nsec(val, cmb->sample_count);
737 out:
738         spin_unlock_irqrestore(cdev->ccwlock, flags);
739         return ret;
740 }
741
742 static int readall_cmb(struct ccw_device *cdev, struct cmbdata *data)
743 {
744         struct cmb *cmb;
745         struct cmb_data *cmb_data;
746         u64 time;
747         unsigned long flags;
748         int ret;
749
750         ret = cmf_cmb_copy_wait(cdev);
751         if (ret < 0)
752                 return ret;
753         spin_lock_irqsave(cdev->ccwlock, flags);
754         cmb_data = cdev->private->cmb;
755         if (!cmb_data) {
756                 ret = -ENODEV;
757                 goto out;
758         }
759         if (cmb_data->last_update == 0) {
760                 ret = -EAGAIN;
761                 goto out;
762         }
763         cmb = cmb_data->last_block;
764         time = cmb_data->last_update - cdev->private->cmb_start_time;
765
766         memset(data, 0, sizeof(struct cmbdata));
767
768         /* we only know values before device_busy_time */
769         data->size = offsetof(struct cmbdata, device_busy_time);
770
771         /* convert to nanoseconds */
772         data->elapsed_time = (time * 1000) >> 12;
773
774         /* copy data to new structure */
775         data->ssch_rsch_count = cmb->ssch_rsch_count;
776         data->sample_count = cmb->sample_count;
777
778         /* time fields are converted to nanoseconds while copying */
779         data->device_connect_time = time_to_nsec(cmb->device_connect_time);
780         data->function_pending_time = time_to_nsec(cmb->function_pending_time);
781         data->device_disconnect_time =
782                 time_to_nsec(cmb->device_disconnect_time);
783         data->control_unit_queuing_time
784                 = time_to_nsec(cmb->control_unit_queuing_time);
785         data->device_active_only_time
786                 = time_to_nsec(cmb->device_active_only_time);
787         ret = 0;
788 out:
789         spin_unlock_irqrestore(cdev->ccwlock, flags);
790         return ret;
791 }
792
793 static void reset_cmb(struct ccw_device *cdev)
794 {
795         cmf_generic_reset(cdev);
796 }
797
798 static void * align_cmb(void *area)
799 {
800         return area;
801 }
802
803 static struct attribute_group cmf_attr_group;
804
805 static struct cmb_operations cmbops_basic = {
806         .alloc  = alloc_cmb,
807         .free   = free_cmb,
808         .set    = set_cmb,
809         .read   = read_cmb,
810         .readall    = readall_cmb,
811         .reset      = reset_cmb,
812         .align      = align_cmb,
813         .attr_group = &cmf_attr_group,
814 };
815 \f
816 /* ******** extended cmb handling ********/
817
818 /**
819  * struct cmbe - extended channel measurement block
820  * @ssch_rsch_count: number of ssch and rsch
821  * @sample_count: number of samples
822  * @device_connect_time: time of device connect
823  * @function_pending_time: time of function pending
824  * @device_disconnect_time: time of device disconnect
825  * @control_unit_queuing_time: time of control unit queuing
826  * @device_active_only_time: time of device active only
827  * @device_busy_time: time of device busy
828  * @initial_command_response_time: initial command response time
829  * @reserved: unused
830  *
831  * The measurement block as used by the hardware. May be in any 64 bit physical
832  * location.
833  * The fields are described further in z/Architecture Principles of Operation,
834  * third edition, chapter 17.
835  */
836 struct cmbe {
837         u32 ssch_rsch_count;
838         u32 sample_count;
839         u32 device_connect_time;
840         u32 function_pending_time;
841         u32 device_disconnect_time;
842         u32 control_unit_queuing_time;
843         u32 device_active_only_time;
844         u32 device_busy_time;
845         u32 initial_command_response_time;
846         u32 reserved[7];
847 };
848
849 /*
850  * kmalloc only guarantees 8 byte alignment, but we need cmbe
851  * pointers to be naturally aligned. Make sure to allocate
852  * enough space for two cmbes.
853  */
854 static inline struct cmbe *cmbe_align(struct cmbe *c)
855 {
856         unsigned long addr;
857         addr = ((unsigned long)c + sizeof (struct cmbe) - sizeof(long)) &
858                                  ~(sizeof (struct cmbe) - sizeof(long));
859         return (struct cmbe*)addr;
860 }
861
862 static int alloc_cmbe(struct ccw_device *cdev)
863 {
864         struct cmbe *cmbe;
865         struct cmb_data *cmb_data;
866         int ret;
867
868         cmbe = kzalloc (sizeof (*cmbe) * 2, GFP_KERNEL);
869         if (!cmbe)
870                 return -ENOMEM;
871         cmb_data = kzalloc(sizeof(struct cmb_data), GFP_KERNEL);
872         if (!cmb_data) {
873                 ret = -ENOMEM;
874                 goto out_free;
875         }
876         cmb_data->last_block = kzalloc(sizeof(struct cmbe), GFP_KERNEL);
877         if (!cmb_data->last_block) {
878                 ret = -ENOMEM;
879                 goto out_free;
880         }
881         cmb_data->size = sizeof(struct cmbe);
882         spin_lock_irq(cdev->ccwlock);
883         if (cdev->private->cmb) {
884                 spin_unlock_irq(cdev->ccwlock);
885                 ret = -EBUSY;
886                 goto out_free;
887         }
888         cmb_data->hw_block = cmbe;
889         cdev->private->cmb = cmb_data;
890         spin_unlock_irq(cdev->ccwlock);
891
892         /* activate global measurement if this is the first channel */
893         spin_lock(&cmb_area.lock);
894         if (list_empty(&cmb_area.list))
895                 cmf_activate(NULL, 1);
896         list_add_tail(&cdev->private->cmb_list, &cmb_area.list);
897         spin_unlock(&cmb_area.lock);
898
899         return 0;
900 out_free:
901         if (cmb_data)
902                 kfree(cmb_data->last_block);
903         kfree(cmb_data);
904         kfree(cmbe);
905         return ret;
906 }
907
908 static void free_cmbe(struct ccw_device *cdev)
909 {
910         struct cmb_data *cmb_data;
911
912         spin_lock_irq(cdev->ccwlock);
913         cmb_data = cdev->private->cmb;
914         cdev->private->cmb = NULL;
915         if (cmb_data)
916                 kfree(cmb_data->last_block);
917         kfree(cmb_data);
918         spin_unlock_irq(cdev->ccwlock);
919
920         /* deactivate global measurement if this is the last channel */
921         spin_lock(&cmb_area.lock);
922         list_del_init(&cdev->private->cmb_list);
923         if (list_empty(&cmb_area.list))
924                 cmf_activate(NULL, 0);
925         spin_unlock(&cmb_area.lock);
926 }
927
928 static int set_cmbe(struct ccw_device *cdev, u32 mme)
929 {
930         unsigned long mba;
931         struct cmb_data *cmb_data;
932         unsigned long flags;
933
934         spin_lock_irqsave(cdev->ccwlock, flags);
935         if (!cdev->private->cmb) {
936                 spin_unlock_irqrestore(cdev->ccwlock, flags);
937                 return -EINVAL;
938         }
939         cmb_data = cdev->private->cmb;
940         mba = mme ? (unsigned long) cmbe_align(cmb_data->hw_block) : 0;
941         spin_unlock_irqrestore(cdev->ccwlock, flags);
942
943         return set_schib_wait(cdev, mme, 1, mba);
944 }
945
946
947 static u64 read_cmbe(struct ccw_device *cdev, int index)
948 {
949         struct cmbe *cmb;
950         struct cmb_data *cmb_data;
951         u32 val;
952         int ret;
953         unsigned long flags;
954
955         ret = cmf_cmb_copy_wait(cdev);
956         if (ret < 0)
957                 return 0;
958
959         spin_lock_irqsave(cdev->ccwlock, flags);
960         cmb_data = cdev->private->cmb;
961         if (!cmb_data) {
962                 ret = 0;
963                 goto out;
964         }
965         cmb = cmb_data->last_block;
966
967         switch (index) {
968         case cmb_ssch_rsch_count:
969                 ret = cmb->ssch_rsch_count;
970                 goto out;
971         case cmb_sample_count:
972                 ret = cmb->sample_count;
973                 goto out;
974         case cmb_device_connect_time:
975                 val = cmb->device_connect_time;
976                 break;
977         case cmb_function_pending_time:
978                 val = cmb->function_pending_time;
979                 break;
980         case cmb_device_disconnect_time:
981                 val = cmb->device_disconnect_time;
982                 break;
983         case cmb_control_unit_queuing_time:
984                 val = cmb->control_unit_queuing_time;
985                 break;
986         case cmb_device_active_only_time:
987                 val = cmb->device_active_only_time;
988                 break;
989         case cmb_device_busy_time:
990                 val = cmb->device_busy_time;
991                 break;
992         case cmb_initial_command_response_time:
993                 val = cmb->initial_command_response_time;
994                 break;
995         default:
996                 ret = 0;
997                 goto out;
998         }
999         ret = time_to_avg_nsec(val, cmb->sample_count);
1000 out:
1001         spin_unlock_irqrestore(cdev->ccwlock, flags);
1002         return ret;
1003 }
1004
1005 static int readall_cmbe(struct ccw_device *cdev, struct cmbdata *data)
1006 {
1007         struct cmbe *cmb;
1008         struct cmb_data *cmb_data;
1009         u64 time;
1010         unsigned long flags;
1011         int ret;
1012
1013         ret = cmf_cmb_copy_wait(cdev);
1014         if (ret < 0)
1015                 return ret;
1016         spin_lock_irqsave(cdev->ccwlock, flags);
1017         cmb_data = cdev->private->cmb;
1018         if (!cmb_data) {
1019                 ret = -ENODEV;
1020                 goto out;
1021         }
1022         if (cmb_data->last_update == 0) {
1023                 ret = -EAGAIN;
1024                 goto out;
1025         }
1026         time = cmb_data->last_update - cdev->private->cmb_start_time;
1027
1028         memset (data, 0, sizeof(struct cmbdata));
1029
1030         /* we only know values before device_busy_time */
1031         data->size = offsetof(struct cmbdata, device_busy_time);
1032
1033         /* conver to nanoseconds */
1034         data->elapsed_time = (time * 1000) >> 12;
1035
1036         cmb = cmb_data->last_block;
1037         /* copy data to new structure */
1038         data->ssch_rsch_count = cmb->ssch_rsch_count;
1039         data->sample_count = cmb->sample_count;
1040
1041         /* time fields are converted to nanoseconds while copying */
1042         data->device_connect_time = time_to_nsec(cmb->device_connect_time);
1043         data->function_pending_time = time_to_nsec(cmb->function_pending_time);
1044         data->device_disconnect_time =
1045                 time_to_nsec(cmb->device_disconnect_time);
1046         data->control_unit_queuing_time
1047                 = time_to_nsec(cmb->control_unit_queuing_time);
1048         data->device_active_only_time
1049                 = time_to_nsec(cmb->device_active_only_time);
1050         data->device_busy_time = time_to_nsec(cmb->device_busy_time);
1051         data->initial_command_response_time
1052                 = time_to_nsec(cmb->initial_command_response_time);
1053
1054         ret = 0;
1055 out:
1056         spin_unlock_irqrestore(cdev->ccwlock, flags);
1057         return ret;
1058 }
1059
1060 static void reset_cmbe(struct ccw_device *cdev)
1061 {
1062         cmf_generic_reset(cdev);
1063 }
1064
1065 static void * align_cmbe(void *area)
1066 {
1067         return cmbe_align(area);
1068 }
1069
1070 static struct attribute_group cmf_attr_group_ext;
1071
1072 static struct cmb_operations cmbops_extended = {
1073         .alloc      = alloc_cmbe,
1074         .free       = free_cmbe,
1075         .set        = set_cmbe,
1076         .read       = read_cmbe,
1077         .readall    = readall_cmbe,
1078         .reset      = reset_cmbe,
1079         .align      = align_cmbe,
1080         .attr_group = &cmf_attr_group_ext,
1081 };
1082 \f
1083
1084 static ssize_t cmb_show_attr(struct device *dev, char *buf, enum cmb_index idx)
1085 {
1086         return sprintf(buf, "%lld\n",
1087                 (unsigned long long) cmf_read(to_ccwdev(dev), idx));
1088 }
1089
1090 static ssize_t cmb_show_avg_sample_interval(struct device *dev,
1091                                             struct device_attribute *attr,
1092                                             char *buf)
1093 {
1094         struct ccw_device *cdev;
1095         long interval;
1096         unsigned long count;
1097         struct cmb_data *cmb_data;
1098
1099         cdev = to_ccwdev(dev);
1100         count = cmf_read(cdev, cmb_sample_count);
1101         spin_lock_irq(cdev->ccwlock);
1102         cmb_data = cdev->private->cmb;
1103         if (count) {
1104                 interval = cmb_data->last_update -
1105                         cdev->private->cmb_start_time;
1106                 interval = (interval * 1000) >> 12;
1107                 interval /= count;
1108         } else
1109                 interval = -1;
1110         spin_unlock_irq(cdev->ccwlock);
1111         return sprintf(buf, "%ld\n", interval);
1112 }
1113
1114 static ssize_t cmb_show_avg_utilization(struct device *dev,
1115                                         struct device_attribute *attr,
1116                                         char *buf)
1117 {
1118         struct cmbdata data;
1119         u64 utilization;
1120         unsigned long t, u;
1121         int ret;
1122
1123         ret = cmf_readall(to_ccwdev(dev), &data);
1124         if (ret == -EAGAIN || ret == -ENODEV)
1125                 /* No data (yet/currently) available to use for calculation. */
1126                 return sprintf(buf, "n/a\n");
1127         else if (ret)
1128                 return ret;
1129
1130         utilization = data.device_connect_time +
1131                       data.function_pending_time +
1132                       data.device_disconnect_time;
1133
1134         /* shift to avoid long long division */
1135         while (-1ul < (data.elapsed_time | utilization)) {
1136                 utilization >>= 8;
1137                 data.elapsed_time >>= 8;
1138         }
1139
1140         /* calculate value in 0.1 percent units */
1141         t = (unsigned long) data.elapsed_time / 1000;
1142         u = (unsigned long) utilization / t;
1143
1144         return sprintf(buf, "%02ld.%01ld%%\n", u/ 10, u - (u/ 10) * 10);
1145 }
1146
1147 #define cmf_attr(name) \
1148 static ssize_t show_##name(struct device *dev, \
1149                            struct device_attribute *attr, char *buf)    \
1150 { return cmb_show_attr((dev), buf, cmb_##name); } \
1151 static DEVICE_ATTR(name, 0444, show_##name, NULL);
1152
1153 #define cmf_attr_avg(name) \
1154 static ssize_t show_avg_##name(struct device *dev, \
1155                                struct device_attribute *attr, char *buf) \
1156 { return cmb_show_attr((dev), buf, cmb_##name); } \
1157 static DEVICE_ATTR(avg_##name, 0444, show_avg_##name, NULL);
1158
1159 cmf_attr(ssch_rsch_count);
1160 cmf_attr(sample_count);
1161 cmf_attr_avg(device_connect_time);
1162 cmf_attr_avg(function_pending_time);
1163 cmf_attr_avg(device_disconnect_time);
1164 cmf_attr_avg(control_unit_queuing_time);
1165 cmf_attr_avg(device_active_only_time);
1166 cmf_attr_avg(device_busy_time);
1167 cmf_attr_avg(initial_command_response_time);
1168
1169 static DEVICE_ATTR(avg_sample_interval, 0444, cmb_show_avg_sample_interval,
1170                    NULL);
1171 static DEVICE_ATTR(avg_utilization, 0444, cmb_show_avg_utilization, NULL);
1172
1173 static struct attribute *cmf_attributes[] = {
1174         &dev_attr_avg_sample_interval.attr,
1175         &dev_attr_avg_utilization.attr,
1176         &dev_attr_ssch_rsch_count.attr,
1177         &dev_attr_sample_count.attr,
1178         &dev_attr_avg_device_connect_time.attr,
1179         &dev_attr_avg_function_pending_time.attr,
1180         &dev_attr_avg_device_disconnect_time.attr,
1181         &dev_attr_avg_control_unit_queuing_time.attr,
1182         &dev_attr_avg_device_active_only_time.attr,
1183         NULL,
1184 };
1185
1186 static struct attribute_group cmf_attr_group = {
1187         .name  = "cmf",
1188         .attrs = cmf_attributes,
1189 };
1190
1191 static struct attribute *cmf_attributes_ext[] = {
1192         &dev_attr_avg_sample_interval.attr,
1193         &dev_attr_avg_utilization.attr,
1194         &dev_attr_ssch_rsch_count.attr,
1195         &dev_attr_sample_count.attr,
1196         &dev_attr_avg_device_connect_time.attr,
1197         &dev_attr_avg_function_pending_time.attr,
1198         &dev_attr_avg_device_disconnect_time.attr,
1199         &dev_attr_avg_control_unit_queuing_time.attr,
1200         &dev_attr_avg_device_active_only_time.attr,
1201         &dev_attr_avg_device_busy_time.attr,
1202         &dev_attr_avg_initial_command_response_time.attr,
1203         NULL,
1204 };
1205
1206 static struct attribute_group cmf_attr_group_ext = {
1207         .name  = "cmf",
1208         .attrs = cmf_attributes_ext,
1209 };
1210
1211 static ssize_t cmb_enable_show(struct device *dev,
1212                                struct device_attribute *attr,
1213                                char *buf)
1214 {
1215         return sprintf(buf, "%d\n", to_ccwdev(dev)->private->cmb ? 1 : 0);
1216 }
1217
1218 static ssize_t cmb_enable_store(struct device *dev,
1219                                 struct device_attribute *attr, const char *buf,
1220                                 size_t c)
1221 {
1222         struct ccw_device *cdev;
1223         int ret;
1224
1225         cdev = to_ccwdev(dev);
1226
1227         switch (buf[0]) {
1228         case '0':
1229                 ret = disable_cmf(cdev);
1230                 if (ret)
1231                         dev_info(&cdev->dev, "disable_cmf failed (%d)\n", ret);
1232                 break;
1233         case '1':
1234                 ret = enable_cmf(cdev);
1235                 if (ret && ret != -EBUSY)
1236                         dev_info(&cdev->dev, "enable_cmf failed (%d)\n", ret);
1237                 break;
1238         }
1239
1240         return c;
1241 }
1242
1243 DEVICE_ATTR(cmb_enable, 0644, cmb_enable_show, cmb_enable_store);
1244
1245 /**
1246  * enable_cmf() - switch on the channel measurement for a specific device
1247  *  @cdev:      The ccw device to be enabled
1248  *
1249  *  Returns %0 for success or a negative error value.
1250  *
1251  *  Context:
1252  *    non-atomic
1253  */
1254 int enable_cmf(struct ccw_device *cdev)
1255 {
1256         int ret;
1257
1258         ret = cmbops->alloc(cdev);
1259         cmbops->reset(cdev);
1260         if (ret)
1261                 return ret;
1262         ret = cmbops->set(cdev, 2);
1263         if (ret) {
1264                 cmbops->free(cdev);
1265                 return ret;
1266         }
1267         ret = sysfs_create_group(&cdev->dev.kobj, cmbops->attr_group);
1268         if (!ret)
1269                 return 0;
1270         cmbops->set(cdev, 0);  //FIXME: this can fail
1271         cmbops->free(cdev);
1272         return ret;
1273 }
1274
1275 /**
1276  * disable_cmf() - switch off the channel measurement for a specific device
1277  *  @cdev:      The ccw device to be disabled
1278  *
1279  *  Returns %0 for success or a negative error value.
1280  *
1281  *  Context:
1282  *    non-atomic
1283  */
1284 int disable_cmf(struct ccw_device *cdev)
1285 {
1286         int ret;
1287
1288         ret = cmbops->set(cdev, 0);
1289         if (ret)
1290                 return ret;
1291         cmbops->free(cdev);
1292         sysfs_remove_group(&cdev->dev.kobj, cmbops->attr_group);
1293         return ret;
1294 }
1295
1296 /**
1297  * cmf_read() - read one value from the current channel measurement block
1298  * @cdev:       the channel to be read
1299  * @index:      the index of the value to be read
1300  *
1301  * Returns the value read or %0 if the value cannot be read.
1302  *
1303  *  Context:
1304  *    any
1305  */
1306 u64 cmf_read(struct ccw_device *cdev, int index)
1307 {
1308         return cmbops->read(cdev, index);
1309 }
1310
1311 /**
1312  * cmf_readall() - read the current channel measurement block
1313  * @cdev:       the channel to be read
1314  * @data:       a pointer to a data block that will be filled
1315  *
1316  * Returns %0 on success, a negative error value otherwise.
1317  *
1318  *  Context:
1319  *    any
1320  */
1321 int cmf_readall(struct ccw_device *cdev, struct cmbdata *data)
1322 {
1323         return cmbops->readall(cdev, data);
1324 }
1325
1326 /* Reenable cmf when a disconnected device becomes available again. */
1327 int cmf_reenable(struct ccw_device *cdev)
1328 {
1329         cmbops->reset(cdev);
1330         return cmbops->set(cdev, 2);
1331 }
1332
1333 static int __init init_cmf(void)
1334 {
1335         char *format_string;
1336         char *detect_string = "parameter";
1337
1338         /*
1339          * If the user did not give a parameter, see if we are running on a
1340          * machine supporting extended measurement blocks, otherwise fall back
1341          * to basic mode.
1342          */
1343         if (format == CMF_AUTODETECT) {
1344                 if (!css_characteristics_avail ||
1345                     !css_general_characteristics.ext_mb) {
1346                         format = CMF_BASIC;
1347                 } else {
1348                         format = CMF_EXTENDED;
1349                 }
1350                 detect_string = "autodetected";
1351         } else {
1352                 detect_string = "parameter";
1353         }
1354
1355         switch (format) {
1356         case CMF_BASIC:
1357                 format_string = "basic";
1358                 cmbops = &cmbops_basic;
1359                 break;
1360         case CMF_EXTENDED:
1361                 format_string = "extended";
1362                 cmbops = &cmbops_extended;
1363                 break;
1364         default:
1365                 printk(KERN_ERR "cio: Invalid format %d for channel "
1366                         "measurement facility\n", format);
1367                 return 1;
1368         }
1369
1370         printk(KERN_INFO "cio: Channel measurement facility using %s "
1371                "format (%s)\n", format_string, detect_string);
1372         return 0;
1373 }
1374
1375 module_init(init_cmf);
1376
1377
1378 MODULE_AUTHOR("Arnd Bergmann <arndb@de.ibm.com>");
1379 MODULE_LICENSE("GPL");
1380 MODULE_DESCRIPTION("channel measurement facility base driver\n"
1381                    "Copyright 2003 IBM Corporation\n");
1382
1383 EXPORT_SYMBOL_GPL(enable_cmf);
1384 EXPORT_SYMBOL_GPL(disable_cmf);
1385 EXPORT_SYMBOL_GPL(cmf_read);
1386 EXPORT_SYMBOL_GPL(cmf_readall);