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