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