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