0c87b0fc7714b42f3dedec1e1adc1035c2f96877
[linux-2.6.git] / drivers / s390 / cio / chsc_sch.c
1 /*
2  * Driver for s390 chsc subchannels
3  *
4  * Copyright IBM Corp. 2008, 2011
5  *
6  * Author(s): Cornelia Huck <cornelia.huck@de.ibm.com>
7  *
8  */
9
10 #include <linux/slab.h>
11 #include <linux/device.h>
12 #include <linux/module.h>
13 #include <linux/uaccess.h>
14 #include <linux/miscdevice.h>
15 #include <linux/kernel_stat.h>
16
17 #include <asm/compat.h>
18 #include <asm/cio.h>
19 #include <asm/chsc.h>
20 #include <asm/isc.h>
21
22 #include "cio.h"
23 #include "cio_debug.h"
24 #include "css.h"
25 #include "chsc_sch.h"
26 #include "ioasm.h"
27
28 static debug_info_t *chsc_debug_msg_id;
29 static debug_info_t *chsc_debug_log_id;
30
31 #define CHSC_MSG(imp, args...) do {                                     \
32                 debug_sprintf_event(chsc_debug_msg_id, imp , ##args);   \
33         } while (0)
34
35 #define CHSC_LOG(imp, txt) do {                                 \
36                 debug_text_event(chsc_debug_log_id, imp , txt); \
37         } while (0)
38
39 static void CHSC_LOG_HEX(int level, void *data, int length)
40 {
41         while (length > 0) {
42                 debug_event(chsc_debug_log_id, level, data, length);
43                 length -= chsc_debug_log_id->buf_size;
44                 data += chsc_debug_log_id->buf_size;
45         }
46 }
47
48 MODULE_AUTHOR("IBM Corporation");
49 MODULE_DESCRIPTION("driver for s390 chsc subchannels");
50 MODULE_LICENSE("GPL");
51
52 static void chsc_subchannel_irq(struct subchannel *sch)
53 {
54         struct chsc_private *private = dev_get_drvdata(&sch->dev);
55         struct chsc_request *request = private->request;
56         struct irb *irb = (struct irb *)&S390_lowcore.irb;
57
58         CHSC_LOG(4, "irb");
59         CHSC_LOG_HEX(4, irb, sizeof(*irb));
60         kstat_cpu(smp_processor_id()).irqs[IOINT_CSC]++;
61
62         /* Copy irb to provided request and set done. */
63         if (!request) {
64                 CHSC_MSG(0, "Interrupt on sch 0.%x.%04x with no request\n",
65                          sch->schid.ssid, sch->schid.sch_no);
66                 return;
67         }
68         private->request = NULL;
69         memcpy(&request->irb, irb, sizeof(*irb));
70         cio_update_schib(sch);
71         complete(&request->completion);
72         put_device(&sch->dev);
73 }
74
75 static int chsc_subchannel_probe(struct subchannel *sch)
76 {
77         struct chsc_private *private;
78         int ret;
79
80         CHSC_MSG(6, "Detected chsc subchannel 0.%x.%04x\n",
81                  sch->schid.ssid, sch->schid.sch_no);
82         sch->isc = CHSC_SCH_ISC;
83         private = kzalloc(sizeof(*private), GFP_KERNEL);
84         if (!private)
85                 return -ENOMEM;
86         dev_set_drvdata(&sch->dev, private);
87         ret = cio_enable_subchannel(sch, (u32)(unsigned long)sch);
88         if (ret) {
89                 CHSC_MSG(0, "Failed to enable 0.%x.%04x: %d\n",
90                          sch->schid.ssid, sch->schid.sch_no, ret);
91                 dev_set_drvdata(&sch->dev, NULL);
92                 kfree(private);
93         } else {
94                 if (dev_get_uevent_suppress(&sch->dev)) {
95                         dev_set_uevent_suppress(&sch->dev, 0);
96                         kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
97                 }
98         }
99         return ret;
100 }
101
102 static int chsc_subchannel_remove(struct subchannel *sch)
103 {
104         struct chsc_private *private;
105
106         cio_disable_subchannel(sch);
107         private = dev_get_drvdata(&sch->dev);
108         dev_set_drvdata(&sch->dev, NULL);
109         if (private->request) {
110                 complete(&private->request->completion);
111                 put_device(&sch->dev);
112         }
113         kfree(private);
114         return 0;
115 }
116
117 static void chsc_subchannel_shutdown(struct subchannel *sch)
118 {
119         cio_disable_subchannel(sch);
120 }
121
122 static int chsc_subchannel_prepare(struct subchannel *sch)
123 {
124         int cc;
125         struct schib schib;
126         /*
127          * Don't allow suspend while the subchannel is not idle
128          * since we don't have a way to clear the subchannel and
129          * cannot disable it with a request running.
130          */
131         cc = stsch_err(sch->schid, &schib);
132         if (!cc && scsw_stctl(&schib.scsw))
133                 return -EAGAIN;
134         return 0;
135 }
136
137 static int chsc_subchannel_freeze(struct subchannel *sch)
138 {
139         return cio_disable_subchannel(sch);
140 }
141
142 static int chsc_subchannel_restore(struct subchannel *sch)
143 {
144         return cio_enable_subchannel(sch, (u32)(unsigned long)sch);
145 }
146
147 static struct css_device_id chsc_subchannel_ids[] = {
148         { .match_flags = 0x1, .type =SUBCHANNEL_TYPE_CHSC, },
149         { /* end of list */ },
150 };
151 MODULE_DEVICE_TABLE(css, chsc_subchannel_ids);
152
153 static struct css_driver chsc_subchannel_driver = {
154         .drv = {
155                 .owner = THIS_MODULE,
156                 .name = "chsc_subchannel",
157         },
158         .subchannel_type = chsc_subchannel_ids,
159         .irq = chsc_subchannel_irq,
160         .probe = chsc_subchannel_probe,
161         .remove = chsc_subchannel_remove,
162         .shutdown = chsc_subchannel_shutdown,
163         .prepare = chsc_subchannel_prepare,
164         .freeze = chsc_subchannel_freeze,
165         .thaw = chsc_subchannel_restore,
166         .restore = chsc_subchannel_restore,
167 };
168
169 static int __init chsc_init_dbfs(void)
170 {
171         chsc_debug_msg_id = debug_register("chsc_msg", 16, 1,
172                                            16 * sizeof(long));
173         if (!chsc_debug_msg_id)
174                 goto out;
175         debug_register_view(chsc_debug_msg_id, &debug_sprintf_view);
176         debug_set_level(chsc_debug_msg_id, 2);
177         chsc_debug_log_id = debug_register("chsc_log", 16, 1, 16);
178         if (!chsc_debug_log_id)
179                 goto out;
180         debug_register_view(chsc_debug_log_id, &debug_hex_ascii_view);
181         debug_set_level(chsc_debug_log_id, 2);
182         return 0;
183 out:
184         if (chsc_debug_msg_id)
185                 debug_unregister(chsc_debug_msg_id);
186         return -ENOMEM;
187 }
188
189 static void chsc_remove_dbfs(void)
190 {
191         debug_unregister(chsc_debug_log_id);
192         debug_unregister(chsc_debug_msg_id);
193 }
194
195 static int __init chsc_init_sch_driver(void)
196 {
197         return css_driver_register(&chsc_subchannel_driver);
198 }
199
200 static void chsc_cleanup_sch_driver(void)
201 {
202         css_driver_unregister(&chsc_subchannel_driver);
203 }
204
205 static DEFINE_SPINLOCK(chsc_lock);
206
207 static int chsc_subchannel_match_next_free(struct device *dev, void *data)
208 {
209         struct subchannel *sch = to_subchannel(dev);
210
211         return sch->schib.pmcw.ena && !scsw_fctl(&sch->schib.scsw);
212 }
213
214 static struct subchannel *chsc_get_next_subchannel(struct subchannel *sch)
215 {
216         struct device *dev;
217
218         dev = driver_find_device(&chsc_subchannel_driver.drv,
219                                  sch ? &sch->dev : NULL, NULL,
220                                  chsc_subchannel_match_next_free);
221         return dev ? to_subchannel(dev) : NULL;
222 }
223
224 /**
225  * chsc_async() - try to start a chsc request asynchronously
226  * @chsc_area: request to be started
227  * @request: request structure to associate
228  *
229  * Tries to start a chsc request on one of the existing chsc subchannels.
230  * Returns:
231  *  %0 if the request was performed synchronously
232  *  %-EINPROGRESS if the request was successfully started
233  *  %-EBUSY if all chsc subchannels are busy
234  *  %-ENODEV if no chsc subchannels are available
235  * Context:
236  *  interrupts disabled, chsc_lock held
237  */
238 static int chsc_async(struct chsc_async_area *chsc_area,
239                       struct chsc_request *request)
240 {
241         int cc;
242         struct chsc_private *private;
243         struct subchannel *sch = NULL;
244         int ret = -ENODEV;
245         char dbf[10];
246
247         chsc_area->header.key = PAGE_DEFAULT_KEY >> 4;
248         while ((sch = chsc_get_next_subchannel(sch))) {
249                 spin_lock(sch->lock);
250                 private = dev_get_drvdata(&sch->dev);
251                 if (private->request) {
252                         spin_unlock(sch->lock);
253                         ret = -EBUSY;
254                         continue;
255                 }
256                 chsc_area->header.sid = sch->schid;
257                 CHSC_LOG(2, "schid");
258                 CHSC_LOG_HEX(2, &sch->schid, sizeof(sch->schid));
259                 cc = chsc(chsc_area);
260                 sprintf(dbf, "cc:%d", cc);
261                 CHSC_LOG(2, dbf);
262                 switch (cc) {
263                 case 0:
264                         ret = 0;
265                         break;
266                 case 1:
267                         sch->schib.scsw.cmd.fctl |= SCSW_FCTL_START_FUNC;
268                         ret = -EINPROGRESS;
269                         private->request = request;
270                         break;
271                 case 2:
272                         ret = -EBUSY;
273                         break;
274                 default:
275                         ret = -ENODEV;
276                 }
277                 spin_unlock(sch->lock);
278                 CHSC_MSG(2, "chsc on 0.%x.%04x returned cc=%d\n",
279                          sch->schid.ssid, sch->schid.sch_no, cc);
280                 if (ret == -EINPROGRESS)
281                         return -EINPROGRESS;
282                 put_device(&sch->dev);
283                 if (ret == 0)
284                         return 0;
285         }
286         return ret;
287 }
288
289 static void chsc_log_command(struct chsc_async_area *chsc_area)
290 {
291         char dbf[10];
292
293         sprintf(dbf, "CHSC:%x", chsc_area->header.code);
294         CHSC_LOG(0, dbf);
295         CHSC_LOG_HEX(0, chsc_area, 32);
296 }
297
298 static int chsc_examine_irb(struct chsc_request *request)
299 {
300         int backed_up;
301
302         if (!(scsw_stctl(&request->irb.scsw) & SCSW_STCTL_STATUS_PEND))
303                 return -EIO;
304         backed_up = scsw_cstat(&request->irb.scsw) & SCHN_STAT_CHAIN_CHECK;
305         request->irb.scsw.cmd.cstat &= ~SCHN_STAT_CHAIN_CHECK;
306         if (scsw_cstat(&request->irb.scsw) == 0)
307                 return 0;
308         if (!backed_up)
309                 return 0;
310         if (scsw_cstat(&request->irb.scsw) & SCHN_STAT_PROG_CHECK)
311                 return -EIO;
312         if (scsw_cstat(&request->irb.scsw) & SCHN_STAT_PROT_CHECK)
313                 return -EPERM;
314         if (scsw_cstat(&request->irb.scsw) & SCHN_STAT_CHN_DATA_CHK)
315                 return -EAGAIN;
316         if (scsw_cstat(&request->irb.scsw) & SCHN_STAT_CHN_CTRL_CHK)
317                 return -EAGAIN;
318         return -EIO;
319 }
320
321 static int chsc_ioctl_start(void __user *user_area)
322 {
323         struct chsc_request *request;
324         struct chsc_async_area *chsc_area;
325         int ret;
326         char dbf[10];
327
328         if (!css_general_characteristics.dynio)
329                 /* It makes no sense to try. */
330                 return -EOPNOTSUPP;
331         chsc_area = (void *)get_zeroed_page(GFP_DMA | GFP_KERNEL);
332         if (!chsc_area)
333                 return -ENOMEM;
334         request = kzalloc(sizeof(*request), GFP_KERNEL);
335         if (!request) {
336                 ret = -ENOMEM;
337                 goto out_free;
338         }
339         init_completion(&request->completion);
340         if (copy_from_user(chsc_area, user_area, PAGE_SIZE)) {
341                 ret = -EFAULT;
342                 goto out_free;
343         }
344         chsc_log_command(chsc_area);
345         spin_lock_irq(&chsc_lock);
346         ret = chsc_async(chsc_area, request);
347         spin_unlock_irq(&chsc_lock);
348         if (ret == -EINPROGRESS) {
349                 wait_for_completion(&request->completion);
350                 ret = chsc_examine_irb(request);
351         }
352         /* copy area back to user */
353         if (!ret)
354                 if (copy_to_user(user_area, chsc_area, PAGE_SIZE))
355                         ret = -EFAULT;
356 out_free:
357         sprintf(dbf, "ret:%d", ret);
358         CHSC_LOG(0, dbf);
359         kfree(request);
360         free_page((unsigned long)chsc_area);
361         return ret;
362 }
363
364 static int chsc_ioctl_info_channel_path(void __user *user_cd)
365 {
366         struct chsc_chp_cd *cd;
367         int ret, ccode;
368         struct {
369                 struct chsc_header request;
370                 u32 : 2;
371                 u32 m : 1;
372                 u32 : 1;
373                 u32 fmt1 : 4;
374                 u32 cssid : 8;
375                 u32 : 8;
376                 u32 first_chpid : 8;
377                 u32 : 24;
378                 u32 last_chpid : 8;
379                 u32 : 32;
380                 struct chsc_header response;
381                 u8 data[PAGE_SIZE - 20];
382         } __attribute__ ((packed)) *scpcd_area;
383
384         scpcd_area = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
385         if (!scpcd_area)
386                 return -ENOMEM;
387         cd = kzalloc(sizeof(*cd), GFP_KERNEL);
388         if (!cd) {
389                 ret = -ENOMEM;
390                 goto out_free;
391         }
392         if (copy_from_user(cd, user_cd, sizeof(*cd))) {
393                 ret = -EFAULT;
394                 goto out_free;
395         }
396         scpcd_area->request.length = 0x0010;
397         scpcd_area->request.code = 0x0028;
398         scpcd_area->m = cd->m;
399         scpcd_area->fmt1 = cd->fmt;
400         scpcd_area->cssid = cd->chpid.cssid;
401         scpcd_area->first_chpid = cd->chpid.id;
402         scpcd_area->last_chpid = cd->chpid.id;
403
404         ccode = chsc(scpcd_area);
405         if (ccode != 0) {
406                 ret = -EIO;
407                 goto out_free;
408         }
409         if (scpcd_area->response.code != 0x0001) {
410                 ret = -EIO;
411                 CHSC_MSG(0, "scpcd: response code=%x\n",
412                          scpcd_area->response.code);
413                 goto out_free;
414         }
415         memcpy(&cd->cpcb, &scpcd_area->response, scpcd_area->response.length);
416         if (copy_to_user(user_cd, cd, sizeof(*cd)))
417                 ret = -EFAULT;
418         else
419                 ret = 0;
420 out_free:
421         kfree(cd);
422         free_page((unsigned long)scpcd_area);
423         return ret;
424 }
425
426 static int chsc_ioctl_info_cu(void __user *user_cd)
427 {
428         struct chsc_cu_cd *cd;
429         int ret, ccode;
430         struct {
431                 struct chsc_header request;
432                 u32 : 2;
433                 u32 m : 1;
434                 u32 : 1;
435                 u32 fmt1 : 4;
436                 u32 cssid : 8;
437                 u32 : 8;
438                 u32 first_cun : 8;
439                 u32 : 24;
440                 u32 last_cun : 8;
441                 u32 : 32;
442                 struct chsc_header response;
443                 u8 data[PAGE_SIZE - 20];
444         } __attribute__ ((packed)) *scucd_area;
445
446         scucd_area = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
447         if (!scucd_area)
448                 return -ENOMEM;
449         cd = kzalloc(sizeof(*cd), GFP_KERNEL);
450         if (!cd) {
451                 ret = -ENOMEM;
452                 goto out_free;
453         }
454         if (copy_from_user(cd, user_cd, sizeof(*cd))) {
455                 ret = -EFAULT;
456                 goto out_free;
457         }
458         scucd_area->request.length = 0x0010;
459         scucd_area->request.code = 0x0028;
460         scucd_area->m = cd->m;
461         scucd_area->fmt1 = cd->fmt;
462         scucd_area->cssid = cd->cssid;
463         scucd_area->first_cun = cd->cun;
464         scucd_area->last_cun = cd->cun;
465
466         ccode = chsc(scucd_area);
467         if (ccode != 0) {
468                 ret = -EIO;
469                 goto out_free;
470         }
471         if (scucd_area->response.code != 0x0001) {
472                 ret = -EIO;
473                 CHSC_MSG(0, "scucd: response code=%x\n",
474                          scucd_area->response.code);
475                 goto out_free;
476         }
477         memcpy(&cd->cucb, &scucd_area->response, scucd_area->response.length);
478         if (copy_to_user(user_cd, cd, sizeof(*cd)))
479                 ret = -EFAULT;
480         else
481                 ret = 0;
482 out_free:
483         kfree(cd);
484         free_page((unsigned long)scucd_area);
485         return ret;
486 }
487
488 static int chsc_ioctl_info_sch_cu(void __user *user_cud)
489 {
490         struct chsc_sch_cud *cud;
491         int ret, ccode;
492         struct {
493                 struct chsc_header request;
494                 u32 : 2;
495                 u32 m : 1;
496                 u32 : 5;
497                 u32 fmt1 : 4;
498                 u32 : 2;
499                 u32 ssid : 2;
500                 u32 first_sch : 16;
501                 u32 : 8;
502                 u32 cssid : 8;
503                 u32 last_sch : 16;
504                 u32 : 32;
505                 struct chsc_header response;
506                 u8 data[PAGE_SIZE - 20];
507         } __attribute__ ((packed)) *sscud_area;
508
509         sscud_area = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
510         if (!sscud_area)
511                 return -ENOMEM;
512         cud = kzalloc(sizeof(*cud), GFP_KERNEL);
513         if (!cud) {
514                 ret = -ENOMEM;
515                 goto out_free;
516         }
517         if (copy_from_user(cud, user_cud, sizeof(*cud))) {
518                 ret = -EFAULT;
519                 goto out_free;
520         }
521         sscud_area->request.length = 0x0010;
522         sscud_area->request.code = 0x0006;
523         sscud_area->m = cud->schid.m;
524         sscud_area->fmt1 = cud->fmt;
525         sscud_area->ssid = cud->schid.ssid;
526         sscud_area->first_sch = cud->schid.sch_no;
527         sscud_area->cssid = cud->schid.cssid;
528         sscud_area->last_sch = cud->schid.sch_no;
529
530         ccode = chsc(sscud_area);
531         if (ccode != 0) {
532                 ret = -EIO;
533                 goto out_free;
534         }
535         if (sscud_area->response.code != 0x0001) {
536                 ret = -EIO;
537                 CHSC_MSG(0, "sscud: response code=%x\n",
538                          sscud_area->response.code);
539                 goto out_free;
540         }
541         memcpy(&cud->scub, &sscud_area->response, sscud_area->response.length);
542         if (copy_to_user(user_cud, cud, sizeof(*cud)))
543                 ret = -EFAULT;
544         else
545                 ret = 0;
546 out_free:
547         kfree(cud);
548         free_page((unsigned long)sscud_area);
549         return ret;
550 }
551
552 static int chsc_ioctl_conf_info(void __user *user_ci)
553 {
554         struct chsc_conf_info *ci;
555         int ret, ccode;
556         struct {
557                 struct chsc_header request;
558                 u32 : 2;
559                 u32 m : 1;
560                 u32 : 1;
561                 u32 fmt1 : 4;
562                 u32 cssid : 8;
563                 u32 : 6;
564                 u32 ssid : 2;
565                 u32 : 8;
566                 u64 : 64;
567                 struct chsc_header response;
568                 u8 data[PAGE_SIZE - 20];
569         } __attribute__ ((packed)) *sci_area;
570
571         sci_area = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
572         if (!sci_area)
573                 return -ENOMEM;
574         ci = kzalloc(sizeof(*ci), GFP_KERNEL);
575         if (!ci) {
576                 ret = -ENOMEM;
577                 goto out_free;
578         }
579         if (copy_from_user(ci, user_ci, sizeof(*ci))) {
580                 ret = -EFAULT;
581                 goto out_free;
582         }
583         sci_area->request.length = 0x0010;
584         sci_area->request.code = 0x0012;
585         sci_area->m = ci->id.m;
586         sci_area->fmt1 = ci->fmt;
587         sci_area->cssid = ci->id.cssid;
588         sci_area->ssid = ci->id.ssid;
589
590         ccode = chsc(sci_area);
591         if (ccode != 0) {
592                 ret = -EIO;
593                 goto out_free;
594         }
595         if (sci_area->response.code != 0x0001) {
596                 ret = -EIO;
597                 CHSC_MSG(0, "sci: response code=%x\n",
598                          sci_area->response.code);
599                 goto out_free;
600         }
601         memcpy(&ci->scid, &sci_area->response, sci_area->response.length);
602         if (copy_to_user(user_ci, ci, sizeof(*ci)))
603                 ret = -EFAULT;
604         else
605                 ret = 0;
606 out_free:
607         kfree(ci);
608         free_page((unsigned long)sci_area);
609         return ret;
610 }
611
612 static int chsc_ioctl_conf_comp_list(void __user *user_ccl)
613 {
614         struct chsc_comp_list *ccl;
615         int ret, ccode;
616         struct {
617                 struct chsc_header request;
618                 u32 ctype : 8;
619                 u32 : 4;
620                 u32 fmt : 4;
621                 u32 : 16;
622                 u64 : 64;
623                 u32 list_parm[2];
624                 u64 : 64;
625                 struct chsc_header response;
626                 u8 data[PAGE_SIZE - 36];
627         } __attribute__ ((packed)) *sccl_area;
628         struct {
629                 u32 m : 1;
630                 u32 : 31;
631                 u32 cssid : 8;
632                 u32 : 16;
633                 u32 chpid : 8;
634         } __attribute__ ((packed)) *chpid_parm;
635         struct {
636                 u32 f_cssid : 8;
637                 u32 l_cssid : 8;
638                 u32 : 16;
639                 u32 res;
640         } __attribute__ ((packed)) *cssids_parm;
641
642         sccl_area = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
643         if (!sccl_area)
644                 return -ENOMEM;
645         ccl = kzalloc(sizeof(*ccl), GFP_KERNEL);
646         if (!ccl) {
647                 ret = -ENOMEM;
648                 goto out_free;
649         }
650         if (copy_from_user(ccl, user_ccl, sizeof(*ccl))) {
651                 ret = -EFAULT;
652                 goto out_free;
653         }
654         sccl_area->request.length = 0x0020;
655         sccl_area->request.code = 0x0030;
656         sccl_area->fmt = ccl->req.fmt;
657         sccl_area->ctype = ccl->req.ctype;
658         switch (sccl_area->ctype) {
659         case CCL_CU_ON_CHP:
660         case CCL_IOP_CHP:
661                 chpid_parm = (void *)&sccl_area->list_parm;
662                 chpid_parm->m = ccl->req.chpid.m;
663                 chpid_parm->cssid = ccl->req.chpid.chp.cssid;
664                 chpid_parm->chpid = ccl->req.chpid.chp.id;
665                 break;
666         case CCL_CSS_IMG:
667         case CCL_CSS_IMG_CONF_CHAR:
668                 cssids_parm = (void *)&sccl_area->list_parm;
669                 cssids_parm->f_cssid = ccl->req.cssids.f_cssid;
670                 cssids_parm->l_cssid = ccl->req.cssids.l_cssid;
671                 break;
672         }
673         ccode = chsc(sccl_area);
674         if (ccode != 0) {
675                 ret = -EIO;
676                 goto out_free;
677         }
678         if (sccl_area->response.code != 0x0001) {
679                 ret = -EIO;
680                 CHSC_MSG(0, "sccl: response code=%x\n",
681                          sccl_area->response.code);
682                 goto out_free;
683         }
684         memcpy(&ccl->sccl, &sccl_area->response, sccl_area->response.length);
685         if (copy_to_user(user_ccl, ccl, sizeof(*ccl)))
686                 ret = -EFAULT;
687         else
688                 ret = 0;
689 out_free:
690         kfree(ccl);
691         free_page((unsigned long)sccl_area);
692         return ret;
693 }
694
695 static int chsc_ioctl_chpd(void __user *user_chpd)
696 {
697         struct chsc_scpd *scpd_area;
698         struct chsc_cpd_info *chpd;
699         int ret;
700
701         chpd = kzalloc(sizeof(*chpd), GFP_KERNEL);
702         scpd_area = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
703         if (!scpd_area || !chpd) {
704                 ret = -ENOMEM;
705                 goto out_free;
706         }
707         if (copy_from_user(chpd, user_chpd, sizeof(*chpd))) {
708                 ret = -EFAULT;
709                 goto out_free;
710         }
711         ret = chsc_determine_channel_path_desc(chpd->chpid, chpd->fmt,
712                                                chpd->rfmt, chpd->c, chpd->m,
713                                                scpd_area);
714         if (ret)
715                 goto out_free;
716         memcpy(&chpd->chpdb, &scpd_area->response, scpd_area->response.length);
717         if (copy_to_user(user_chpd, chpd, sizeof(*chpd)))
718                 ret = -EFAULT;
719 out_free:
720         kfree(chpd);
721         free_page((unsigned long)scpd_area);
722         return ret;
723 }
724
725 static int chsc_ioctl_dcal(void __user *user_dcal)
726 {
727         struct chsc_dcal *dcal;
728         int ret, ccode;
729         struct {
730                 struct chsc_header request;
731                 u32 atype : 8;
732                 u32 : 4;
733                 u32 fmt : 4;
734                 u32 : 16;
735                 u32 res0[2];
736                 u32 list_parm[2];
737                 u32 res1[2];
738                 struct chsc_header response;
739                 u8 data[PAGE_SIZE - 36];
740         } __attribute__ ((packed)) *sdcal_area;
741
742         sdcal_area = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
743         if (!sdcal_area)
744                 return -ENOMEM;
745         dcal = kzalloc(sizeof(*dcal), GFP_KERNEL);
746         if (!dcal) {
747                 ret = -ENOMEM;
748                 goto out_free;
749         }
750         if (copy_from_user(dcal, user_dcal, sizeof(*dcal))) {
751                 ret = -EFAULT;
752                 goto out_free;
753         }
754         sdcal_area->request.length = 0x0020;
755         sdcal_area->request.code = 0x0034;
756         sdcal_area->atype = dcal->req.atype;
757         sdcal_area->fmt = dcal->req.fmt;
758         memcpy(&sdcal_area->list_parm, &dcal->req.list_parm,
759                sizeof(sdcal_area->list_parm));
760
761         ccode = chsc(sdcal_area);
762         if (ccode != 0) {
763                 ret = -EIO;
764                 goto out_free;
765         }
766         if (sdcal_area->response.code != 0x0001) {
767                 ret = -EIO;
768                 CHSC_MSG(0, "sdcal: response code=%x\n",
769                          sdcal_area->response.code);
770                 goto out_free;
771         }
772         memcpy(&dcal->sdcal, &sdcal_area->response,
773                sdcal_area->response.length);
774         if (copy_to_user(user_dcal, dcal, sizeof(*dcal)))
775                 ret = -EFAULT;
776         else
777                 ret = 0;
778 out_free:
779         kfree(dcal);
780         free_page((unsigned long)sdcal_area);
781         return ret;
782 }
783
784 static long chsc_ioctl(struct file *filp, unsigned int cmd,
785                        unsigned long arg)
786 {
787         void __user *argp;
788
789         CHSC_MSG(2, "chsc_ioctl called, cmd=%x\n", cmd);
790         if (is_compat_task())
791                 argp = compat_ptr(arg);
792         else
793                 argp = (void __user *)arg;
794         switch (cmd) {
795         case CHSC_START:
796                 return chsc_ioctl_start(argp);
797         case CHSC_INFO_CHANNEL_PATH:
798                 return chsc_ioctl_info_channel_path(argp);
799         case CHSC_INFO_CU:
800                 return chsc_ioctl_info_cu(argp);
801         case CHSC_INFO_SCH_CU:
802                 return chsc_ioctl_info_sch_cu(argp);
803         case CHSC_INFO_CI:
804                 return chsc_ioctl_conf_info(argp);
805         case CHSC_INFO_CCL:
806                 return chsc_ioctl_conf_comp_list(argp);
807         case CHSC_INFO_CPD:
808                 return chsc_ioctl_chpd(argp);
809         case CHSC_INFO_DCAL:
810                 return chsc_ioctl_dcal(argp);
811         default: /* unknown ioctl number */
812                 return -ENOIOCTLCMD;
813         }
814 }
815
816 static const struct file_operations chsc_fops = {
817         .owner = THIS_MODULE,
818         .open = nonseekable_open,
819         .unlocked_ioctl = chsc_ioctl,
820         .compat_ioctl = chsc_ioctl,
821         .llseek = no_llseek,
822 };
823
824 static struct miscdevice chsc_misc_device = {
825         .minor = MISC_DYNAMIC_MINOR,
826         .name = "chsc",
827         .fops = &chsc_fops,
828 };
829
830 static int __init chsc_misc_init(void)
831 {
832         return misc_register(&chsc_misc_device);
833 }
834
835 static void chsc_misc_cleanup(void)
836 {
837         misc_deregister(&chsc_misc_device);
838 }
839
840 static int __init chsc_sch_init(void)
841 {
842         int ret;
843
844         ret = chsc_init_dbfs();
845         if (ret)
846                 return ret;
847         isc_register(CHSC_SCH_ISC);
848         ret = chsc_init_sch_driver();
849         if (ret)
850                 goto out_dbf;
851         ret = chsc_misc_init();
852         if (ret)
853                 goto out_driver;
854         return ret;
855 out_driver:
856         chsc_cleanup_sch_driver();
857 out_dbf:
858         isc_unregister(CHSC_SCH_ISC);
859         chsc_remove_dbfs();
860         return ret;
861 }
862
863 static void __exit chsc_sch_exit(void)
864 {
865         chsc_misc_cleanup();
866         chsc_cleanup_sch_driver();
867         isc_unregister(CHSC_SCH_ISC);
868         chsc_remove_dbfs();
869 }
870
871 module_init(chsc_sch_init);
872 module_exit(chsc_sch_exit);