Merge branch 'i2c-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jdelvar...
[pandora-kernel.git] / drivers / s390 / cio / chp.c
1 /*
2  *  drivers/s390/cio/chp.c
3  *
4  *    Copyright IBM Corp. 1999,2007
5  *    Author(s): Cornelia Huck (cornelia.huck@de.ibm.com)
6  *               Arnd Bergmann (arndb@de.ibm.com)
7  *               Peter Oberparleiter <peter.oberparleiter@de.ibm.com>
8  */
9
10 #include <linux/bug.h>
11 #include <linux/workqueue.h>
12 #include <linux/spinlock.h>
13 #include <linux/init.h>
14 #include <linux/jiffies.h>
15 #include <linux/wait.h>
16 #include <linux/mutex.h>
17 #include <linux/errno.h>
18 #include <linux/slab.h>
19 #include <asm/chpid.h>
20 #include <asm/sclp.h>
21 #include <asm/crw.h>
22
23 #include "cio.h"
24 #include "css.h"
25 #include "ioasm.h"
26 #include "cio_debug.h"
27 #include "chp.h"
28
29 #define to_channelpath(device) container_of(device, struct channel_path, dev)
30 #define CHP_INFO_UPDATE_INTERVAL        1*HZ
31
32 enum cfg_task_t {
33         cfg_none,
34         cfg_configure,
35         cfg_deconfigure
36 };
37
38 /* Map for pending configure tasks. */
39 static enum cfg_task_t chp_cfg_task[__MAX_CSSID + 1][__MAX_CHPID + 1];
40 static DEFINE_MUTEX(cfg_lock);
41 static int cfg_busy;
42
43 /* Map for channel-path status. */
44 static struct sclp_chp_info chp_info;
45 static DEFINE_MUTEX(info_lock);
46
47 /* Time after which channel-path status may be outdated. */
48 static unsigned long chp_info_expires;
49
50 /* Workqueue to perform pending configure tasks. */
51 static struct workqueue_struct *chp_wq;
52 static struct work_struct cfg_work;
53
54 /* Wait queue for configure completion events. */
55 static wait_queue_head_t cfg_wait_queue;
56
57 /* Return channel_path struct for given chpid. */
58 static inline struct channel_path *chpid_to_chp(struct chp_id chpid)
59 {
60         return channel_subsystems[chpid.cssid]->chps[chpid.id];
61 }
62
63 /* Set vary state for given chpid. */
64 static void set_chp_logically_online(struct chp_id chpid, int onoff)
65 {
66         chpid_to_chp(chpid)->state = onoff;
67 }
68
69 /* On success return 0 if channel-path is varied offline, 1 if it is varied
70  * online. Return -ENODEV if channel-path is not registered. */
71 int chp_get_status(struct chp_id chpid)
72 {
73         return (chpid_to_chp(chpid) ? chpid_to_chp(chpid)->state : -ENODEV);
74 }
75
76 /**
77  * chp_get_sch_opm - return opm for subchannel
78  * @sch: subchannel
79  *
80  * Calculate and return the operational path mask (opm) based on the chpids
81  * used by the subchannel and the status of the associated channel-paths.
82  */
83 u8 chp_get_sch_opm(struct subchannel *sch)
84 {
85         struct chp_id chpid;
86         int opm;
87         int i;
88
89         opm = 0;
90         chp_id_init(&chpid);
91         for (i = 0; i < 8; i++) {
92                 opm <<= 1;
93                 chpid.id = sch->schib.pmcw.chpid[i];
94                 if (chp_get_status(chpid) != 0)
95                         opm |= 1;
96         }
97         return opm;
98 }
99 EXPORT_SYMBOL_GPL(chp_get_sch_opm);
100
101 /**
102  * chp_is_registered - check if a channel-path is registered
103  * @chpid: channel-path ID
104  *
105  * Return non-zero if a channel-path with the given chpid is registered,
106  * zero otherwise.
107  */
108 int chp_is_registered(struct chp_id chpid)
109 {
110         return chpid_to_chp(chpid) != NULL;
111 }
112
113 /*
114  * Function: s390_vary_chpid
115  * Varies the specified chpid online or offline
116  */
117 static int s390_vary_chpid(struct chp_id chpid, int on)
118 {
119         char dbf_text[15];
120         int status;
121
122         sprintf(dbf_text, on?"varyon%x.%02x":"varyoff%x.%02x", chpid.cssid,
123                 chpid.id);
124         CIO_TRACE_EVENT(2, dbf_text);
125
126         status = chp_get_status(chpid);
127         if (!on && !status)
128                 return 0;
129
130         set_chp_logically_online(chpid, on);
131         chsc_chp_vary(chpid, on);
132         return 0;
133 }
134
135 /*
136  * Channel measurement related functions
137  */
138 static ssize_t chp_measurement_chars_read(struct file *filp,
139                                           struct kobject *kobj,
140                                           struct bin_attribute *bin_attr,
141                                           char *buf, loff_t off, size_t count)
142 {
143         struct channel_path *chp;
144         struct device *device;
145
146         device = container_of(kobj, struct device, kobj);
147         chp = to_channelpath(device);
148         if (!chp->cmg_chars)
149                 return 0;
150
151         return memory_read_from_buffer(buf, count, &off,
152                                 chp->cmg_chars, sizeof(struct cmg_chars));
153 }
154
155 static struct bin_attribute chp_measurement_chars_attr = {
156         .attr = {
157                 .name = "measurement_chars",
158                 .mode = S_IRUSR,
159         },
160         .size = sizeof(struct cmg_chars),
161         .read = chp_measurement_chars_read,
162 };
163
164 static void chp_measurement_copy_block(struct cmg_entry *buf,
165                                        struct channel_subsystem *css,
166                                        struct chp_id chpid)
167 {
168         void *area;
169         struct cmg_entry *entry, reference_buf;
170         int idx;
171
172         if (chpid.id < 128) {
173                 area = css->cub_addr1;
174                 idx = chpid.id;
175         } else {
176                 area = css->cub_addr2;
177                 idx = chpid.id - 128;
178         }
179         entry = area + (idx * sizeof(struct cmg_entry));
180         do {
181                 memcpy(buf, entry, sizeof(*entry));
182                 memcpy(&reference_buf, entry, sizeof(*entry));
183         } while (reference_buf.values[0] != buf->values[0]);
184 }
185
186 static ssize_t chp_measurement_read(struct file *filp, struct kobject *kobj,
187                                     struct bin_attribute *bin_attr,
188                                     char *buf, loff_t off, size_t count)
189 {
190         struct channel_path *chp;
191         struct channel_subsystem *css;
192         struct device *device;
193         unsigned int size;
194
195         device = container_of(kobj, struct device, kobj);
196         chp = to_channelpath(device);
197         css = to_css(chp->dev.parent);
198
199         size = sizeof(struct cmg_entry);
200
201         /* Only allow single reads. */
202         if (off || count < size)
203                 return 0;
204         chp_measurement_copy_block((struct cmg_entry *)buf, css, chp->chpid);
205         count = size;
206         return count;
207 }
208
209 static struct bin_attribute chp_measurement_attr = {
210         .attr = {
211                 .name = "measurement",
212                 .mode = S_IRUSR,
213         },
214         .size = sizeof(struct cmg_entry),
215         .read = chp_measurement_read,
216 };
217
218 void chp_remove_cmg_attr(struct channel_path *chp)
219 {
220         device_remove_bin_file(&chp->dev, &chp_measurement_chars_attr);
221         device_remove_bin_file(&chp->dev, &chp_measurement_attr);
222 }
223
224 int chp_add_cmg_attr(struct channel_path *chp)
225 {
226         int ret;
227
228         ret = device_create_bin_file(&chp->dev, &chp_measurement_chars_attr);
229         if (ret)
230                 return ret;
231         ret = device_create_bin_file(&chp->dev, &chp_measurement_attr);
232         if (ret)
233                 device_remove_bin_file(&chp->dev, &chp_measurement_chars_attr);
234         return ret;
235 }
236
237 /*
238  * Files for the channel path entries.
239  */
240 static ssize_t chp_status_show(struct device *dev,
241                                struct device_attribute *attr, char *buf)
242 {
243         struct channel_path *chp = to_channelpath(dev);
244
245         if (!chp)
246                 return 0;
247         return (chp_get_status(chp->chpid) ? sprintf(buf, "online\n") :
248                 sprintf(buf, "offline\n"));
249 }
250
251 static ssize_t chp_status_write(struct device *dev,
252                                 struct device_attribute *attr,
253                                 const char *buf, size_t count)
254 {
255         struct channel_path *cp = to_channelpath(dev);
256         char cmd[10];
257         int num_args;
258         int error;
259
260         num_args = sscanf(buf, "%5s", cmd);
261         if (!num_args)
262                 return count;
263
264         if (!strnicmp(cmd, "on", 2) || !strcmp(cmd, "1"))
265                 error = s390_vary_chpid(cp->chpid, 1);
266         else if (!strnicmp(cmd, "off", 3) || !strcmp(cmd, "0"))
267                 error = s390_vary_chpid(cp->chpid, 0);
268         else
269                 error = -EINVAL;
270
271         return error < 0 ? error : count;
272
273 }
274
275 static DEVICE_ATTR(status, 0644, chp_status_show, chp_status_write);
276
277 static ssize_t chp_configure_show(struct device *dev,
278                                   struct device_attribute *attr, char *buf)
279 {
280         struct channel_path *cp;
281         int status;
282
283         cp = to_channelpath(dev);
284         status = chp_info_get_status(cp->chpid);
285         if (status < 0)
286                 return status;
287
288         return snprintf(buf, PAGE_SIZE, "%d\n", status);
289 }
290
291 static int cfg_wait_idle(void);
292
293 static ssize_t chp_configure_write(struct device *dev,
294                                    struct device_attribute *attr,
295                                    const char *buf, size_t count)
296 {
297         struct channel_path *cp;
298         int val;
299         char delim;
300
301         if (sscanf(buf, "%d %c", &val, &delim) != 1)
302                 return -EINVAL;
303         if (val != 0 && val != 1)
304                 return -EINVAL;
305         cp = to_channelpath(dev);
306         chp_cfg_schedule(cp->chpid, val);
307         cfg_wait_idle();
308
309         return count;
310 }
311
312 static DEVICE_ATTR(configure, 0644, chp_configure_show, chp_configure_write);
313
314 static ssize_t chp_type_show(struct device *dev, struct device_attribute *attr,
315                              char *buf)
316 {
317         struct channel_path *chp = to_channelpath(dev);
318
319         if (!chp)
320                 return 0;
321         return sprintf(buf, "%x\n", chp->desc.desc);
322 }
323
324 static DEVICE_ATTR(type, 0444, chp_type_show, NULL);
325
326 static ssize_t chp_cmg_show(struct device *dev, struct device_attribute *attr,
327                             char *buf)
328 {
329         struct channel_path *chp = to_channelpath(dev);
330
331         if (!chp)
332                 return 0;
333         if (chp->cmg == -1) /* channel measurements not available */
334                 return sprintf(buf, "unknown\n");
335         return sprintf(buf, "%x\n", chp->cmg);
336 }
337
338 static DEVICE_ATTR(cmg, 0444, chp_cmg_show, NULL);
339
340 static ssize_t chp_shared_show(struct device *dev,
341                                struct device_attribute *attr, char *buf)
342 {
343         struct channel_path *chp = to_channelpath(dev);
344
345         if (!chp)
346                 return 0;
347         if (chp->shared == -1) /* channel measurements not available */
348                 return sprintf(buf, "unknown\n");
349         return sprintf(buf, "%x\n", chp->shared);
350 }
351
352 static DEVICE_ATTR(shared, 0444, chp_shared_show, NULL);
353
354 static struct attribute *chp_attrs[] = {
355         &dev_attr_status.attr,
356         &dev_attr_configure.attr,
357         &dev_attr_type.attr,
358         &dev_attr_cmg.attr,
359         &dev_attr_shared.attr,
360         NULL,
361 };
362
363 static struct attribute_group chp_attr_group = {
364         .attrs = chp_attrs,
365 };
366
367 static void chp_release(struct device *dev)
368 {
369         struct channel_path *cp;
370
371         cp = to_channelpath(dev);
372         kfree(cp);
373 }
374
375 /**
376  * chp_new - register a new channel-path
377  * @chpid - channel-path ID
378  *
379  * Create and register data structure representing new channel-path. Return
380  * zero on success, non-zero otherwise.
381  */
382 int chp_new(struct chp_id chpid)
383 {
384         struct channel_path *chp;
385         int ret;
386
387         if (chp_is_registered(chpid))
388                 return 0;
389         chp = kzalloc(sizeof(struct channel_path), GFP_KERNEL);
390         if (!chp)
391                 return -ENOMEM;
392
393         /* fill in status, etc. */
394         chp->chpid = chpid;
395         chp->state = 1;
396         chp->dev.parent = &channel_subsystems[chpid.cssid]->device;
397         chp->dev.release = chp_release;
398
399         /* Obtain channel path description and fill it in. */
400         ret = chsc_determine_base_channel_path_desc(chpid, &chp->desc);
401         if (ret)
402                 goto out_free;
403         if ((chp->desc.flags & 0x80) == 0) {
404                 ret = -ENODEV;
405                 goto out_free;
406         }
407         /* Get channel-measurement characteristics. */
408         if (css_chsc_characteristics.scmc && css_chsc_characteristics.secm) {
409                 ret = chsc_get_channel_measurement_chars(chp);
410                 if (ret)
411                         goto out_free;
412         } else {
413                 chp->cmg = -1;
414         }
415         dev_set_name(&chp->dev, "chp%x.%02x", chpid.cssid, chpid.id);
416
417         /* make it known to the system */
418         ret = device_register(&chp->dev);
419         if (ret) {
420                 CIO_MSG_EVENT(0, "Could not register chp%x.%02x: %d\n",
421                               chpid.cssid, chpid.id, ret);
422                 put_device(&chp->dev);
423                 goto out;
424         }
425         ret = sysfs_create_group(&chp->dev.kobj, &chp_attr_group);
426         if (ret) {
427                 device_unregister(&chp->dev);
428                 goto out;
429         }
430         mutex_lock(&channel_subsystems[chpid.cssid]->mutex);
431         if (channel_subsystems[chpid.cssid]->cm_enabled) {
432                 ret = chp_add_cmg_attr(chp);
433                 if (ret) {
434                         sysfs_remove_group(&chp->dev.kobj, &chp_attr_group);
435                         device_unregister(&chp->dev);
436                         mutex_unlock(&channel_subsystems[chpid.cssid]->mutex);
437                         goto out;
438                 }
439         }
440         channel_subsystems[chpid.cssid]->chps[chpid.id] = chp;
441         mutex_unlock(&channel_subsystems[chpid.cssid]->mutex);
442         goto out;
443 out_free:
444         kfree(chp);
445 out:
446         return ret;
447 }
448
449 /**
450  * chp_get_chp_desc - return newly allocated channel-path description
451  * @chpid: channel-path ID
452  *
453  * On success return a newly allocated copy of the channel-path description
454  * data associated with the given channel-path ID. Return %NULL on error.
455  */
456 void *chp_get_chp_desc(struct chp_id chpid)
457 {
458         struct channel_path *chp;
459         struct channel_path_desc *desc;
460
461         chp = chpid_to_chp(chpid);
462         if (!chp)
463                 return NULL;
464         desc = kmalloc(sizeof(struct channel_path_desc), GFP_KERNEL);
465         if (!desc)
466                 return NULL;
467         memcpy(desc, &chp->desc, sizeof(struct channel_path_desc));
468         return desc;
469 }
470
471 /**
472  * chp_process_crw - process channel-path status change
473  * @crw0: channel report-word to handler
474  * @crw1: second channel-report word (always NULL)
475  * @overflow: crw overflow indication
476  *
477  * Handle channel-report-words indicating that the status of a channel-path
478  * has changed.
479  */
480 static void chp_process_crw(struct crw *crw0, struct crw *crw1,
481                             int overflow)
482 {
483         struct chp_id chpid;
484
485         if (overflow) {
486                 css_schedule_eval_all();
487                 return;
488         }
489         CIO_CRW_EVENT(2, "CRW reports slct=%d, oflw=%d, "
490                       "chn=%d, rsc=%X, anc=%d, erc=%X, rsid=%X\n",
491                       crw0->slct, crw0->oflw, crw0->chn, crw0->rsc, crw0->anc,
492                       crw0->erc, crw0->rsid);
493         /*
494          * Check for solicited machine checks. These are
495          * created by reset channel path and need not be
496          * handled here.
497          */
498         if (crw0->slct) {
499                 CIO_CRW_EVENT(2, "solicited machine check for "
500                               "channel path %02X\n", crw0->rsid);
501                 return;
502         }
503         chp_id_init(&chpid);
504         chpid.id = crw0->rsid;
505         switch (crw0->erc) {
506         case CRW_ERC_IPARM: /* Path has come. */
507                 if (!chp_is_registered(chpid))
508                         chp_new(chpid);
509                 chsc_chp_online(chpid);
510                 break;
511         case CRW_ERC_PERRI: /* Path has gone. */
512         case CRW_ERC_PERRN:
513                 chsc_chp_offline(chpid);
514                 break;
515         default:
516                 CIO_CRW_EVENT(2, "Don't know how to handle erc=%x\n",
517                               crw0->erc);
518         }
519 }
520
521 int chp_ssd_get_mask(struct chsc_ssd_info *ssd, struct chp_link *link)
522 {
523         int i;
524         int mask;
525
526         for (i = 0; i < 8; i++) {
527                 mask = 0x80 >> i;
528                 if (!(ssd->path_mask & mask))
529                         continue;
530                 if (!chp_id_is_equal(&ssd->chpid[i], &link->chpid))
531                         continue;
532                 if ((ssd->fla_valid_mask & mask) &&
533                     ((ssd->fla[i] & link->fla_mask) != link->fla))
534                         continue;
535                 return mask;
536         }
537         return 0;
538 }
539 EXPORT_SYMBOL_GPL(chp_ssd_get_mask);
540
541 static inline int info_bit_num(struct chp_id id)
542 {
543         return id.id + id.cssid * (__MAX_CHPID + 1);
544 }
545
546 /* Force chp_info refresh on next call to info_validate(). */
547 static void info_expire(void)
548 {
549         mutex_lock(&info_lock);
550         chp_info_expires = jiffies - 1;
551         mutex_unlock(&info_lock);
552 }
553
554 /* Ensure that chp_info is up-to-date. */
555 static int info_update(void)
556 {
557         int rc;
558
559         mutex_lock(&info_lock);
560         rc = 0;
561         if (time_after(jiffies, chp_info_expires)) {
562                 /* Data is too old, update. */
563                 rc = sclp_chp_read_info(&chp_info);
564                 chp_info_expires = jiffies + CHP_INFO_UPDATE_INTERVAL ;
565         }
566         mutex_unlock(&info_lock);
567
568         return rc;
569 }
570
571 /**
572  * chp_info_get_status - retrieve configure status of a channel-path
573  * @chpid: channel-path ID
574  *
575  * On success, return 0 for standby, 1 for configured, 2 for reserved,
576  * 3 for not recognized. Return negative error code on error.
577  */
578 int chp_info_get_status(struct chp_id chpid)
579 {
580         int rc;
581         int bit;
582
583         rc = info_update();
584         if (rc)
585                 return rc;
586
587         bit = info_bit_num(chpid);
588         mutex_lock(&info_lock);
589         if (!chp_test_bit(chp_info.recognized, bit))
590                 rc = CHP_STATUS_NOT_RECOGNIZED;
591         else if (chp_test_bit(chp_info.configured, bit))
592                 rc = CHP_STATUS_CONFIGURED;
593         else if (chp_test_bit(chp_info.standby, bit))
594                 rc = CHP_STATUS_STANDBY;
595         else
596                 rc = CHP_STATUS_RESERVED;
597         mutex_unlock(&info_lock);
598
599         return rc;
600 }
601
602 /* Return configure task for chpid. */
603 static enum cfg_task_t cfg_get_task(struct chp_id chpid)
604 {
605         return chp_cfg_task[chpid.cssid][chpid.id];
606 }
607
608 /* Set configure task for chpid. */
609 static void cfg_set_task(struct chp_id chpid, enum cfg_task_t cfg)
610 {
611         chp_cfg_task[chpid.cssid][chpid.id] = cfg;
612 }
613
614 /* Perform one configure/deconfigure request. Reschedule work function until
615  * last request. */
616 static void cfg_func(struct work_struct *work)
617 {
618         struct chp_id chpid;
619         enum cfg_task_t t;
620         int rc;
621
622         mutex_lock(&cfg_lock);
623         t = cfg_none;
624         chp_id_for_each(&chpid) {
625                 t = cfg_get_task(chpid);
626                 if (t != cfg_none) {
627                         cfg_set_task(chpid, cfg_none);
628                         break;
629                 }
630         }
631         mutex_unlock(&cfg_lock);
632
633         switch (t) {
634         case cfg_configure:
635                 rc = sclp_chp_configure(chpid);
636                 if (rc)
637                         CIO_MSG_EVENT(2, "chp: sclp_chp_configure(%x.%02x)="
638                                       "%d\n", chpid.cssid, chpid.id, rc);
639                 else {
640                         info_expire();
641                         chsc_chp_online(chpid);
642                 }
643                 break;
644         case cfg_deconfigure:
645                 rc = sclp_chp_deconfigure(chpid);
646                 if (rc)
647                         CIO_MSG_EVENT(2, "chp: sclp_chp_deconfigure(%x.%02x)="
648                                       "%d\n", chpid.cssid, chpid.id, rc);
649                 else {
650                         info_expire();
651                         chsc_chp_offline(chpid);
652                 }
653                 break;
654         case cfg_none:
655                 /* Get updated information after last change. */
656                 info_update();
657                 mutex_lock(&cfg_lock);
658                 cfg_busy = 0;
659                 mutex_unlock(&cfg_lock);
660                 wake_up_interruptible(&cfg_wait_queue);
661                 return;
662         }
663         queue_work(chp_wq, &cfg_work);
664 }
665
666 /**
667  * chp_cfg_schedule - schedule chpid configuration request
668  * @chpid - channel-path ID
669  * @configure - Non-zero for configure, zero for deconfigure
670  *
671  * Schedule a channel-path configuration/deconfiguration request.
672  */
673 void chp_cfg_schedule(struct chp_id chpid, int configure)
674 {
675         CIO_MSG_EVENT(2, "chp_cfg_sched%x.%02x=%d\n", chpid.cssid, chpid.id,
676                       configure);
677         mutex_lock(&cfg_lock);
678         cfg_set_task(chpid, configure ? cfg_configure : cfg_deconfigure);
679         cfg_busy = 1;
680         mutex_unlock(&cfg_lock);
681         queue_work(chp_wq, &cfg_work);
682 }
683
684 /**
685  * chp_cfg_cancel_deconfigure - cancel chpid deconfiguration request
686  * @chpid - channel-path ID
687  *
688  * Cancel an active channel-path deconfiguration request if it has not yet
689  * been performed.
690  */
691 void chp_cfg_cancel_deconfigure(struct chp_id chpid)
692 {
693         CIO_MSG_EVENT(2, "chp_cfg_cancel:%x.%02x\n", chpid.cssid, chpid.id);
694         mutex_lock(&cfg_lock);
695         if (cfg_get_task(chpid) == cfg_deconfigure)
696                 cfg_set_task(chpid, cfg_none);
697         mutex_unlock(&cfg_lock);
698 }
699
700 static int cfg_wait_idle(void)
701 {
702         if (wait_event_interruptible(cfg_wait_queue, !cfg_busy))
703                 return -ERESTARTSYS;
704         return 0;
705 }
706
707 static int __init chp_init(void)
708 {
709         struct chp_id chpid;
710         int ret;
711
712         ret = crw_register_handler(CRW_RSC_CPATH, chp_process_crw);
713         if (ret)
714                 return ret;
715         chp_wq = create_singlethread_workqueue("cio_chp");
716         if (!chp_wq) {
717                 crw_unregister_handler(CRW_RSC_CPATH);
718                 return -ENOMEM;
719         }
720         INIT_WORK(&cfg_work, cfg_func);
721         init_waitqueue_head(&cfg_wait_queue);
722         if (info_update())
723                 return 0;
724         /* Register available channel-paths. */
725         chp_id_for_each(&chpid) {
726                 if (chp_info_get_status(chpid) != CHP_STATUS_NOT_RECOGNIZED)
727                         chp_new(chpid);
728         }
729
730         return 0;
731 }
732
733 subsys_initcall(chp_init);