2 * Driver for s390 chsc subchannels
4 * Copyright IBM Corp. 2008, 2011
6 * Author(s): Cornelia Huck <cornelia.huck@de.ibm.com>
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>
18 #include <asm/compat.h>
24 #include "cio_debug.h"
29 static debug_info_t *chsc_debug_msg_id;
30 static debug_info_t *chsc_debug_log_id;
32 #define CHSC_MSG(imp, args...) do { \
33 debug_sprintf_event(chsc_debug_msg_id, imp , ##args); \
36 #define CHSC_LOG(imp, txt) do { \
37 debug_text_event(chsc_debug_log_id, imp , txt); \
40 static void CHSC_LOG_HEX(int level, void *data, int length)
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;
49 MODULE_AUTHOR("IBM Corporation");
50 MODULE_DESCRIPTION("driver for s390 chsc subchannels");
51 MODULE_LICENSE("GPL");
53 static void chsc_subchannel_irq(struct subchannel *sch)
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;
60 CHSC_LOG_HEX(4, irb, sizeof(*irb));
61 inc_irq_stat(IRQIO_CSC);
63 /* Copy irb to provided request and set done. */
65 CHSC_MSG(0, "Interrupt on sch 0.%x.%04x with no request\n",
66 sch->schid.ssid, sch->schid.sch_no);
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);
76 static int chsc_subchannel_probe(struct subchannel *sch)
78 struct chsc_private *private;
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);
87 dev_set_drvdata(&sch->dev, private);
88 ret = cio_enable_subchannel(sch, (u32)(unsigned long)sch);
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);
95 if (dev_get_uevent_suppress(&sch->dev)) {
96 dev_set_uevent_suppress(&sch->dev, 0);
97 kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
103 static int chsc_subchannel_remove(struct subchannel *sch)
105 struct chsc_private *private;
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);
118 static void chsc_subchannel_shutdown(struct subchannel *sch)
120 cio_disable_subchannel(sch);
123 static int chsc_subchannel_prepare(struct subchannel *sch)
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.
132 cc = stsch_err(sch->schid, &schib);
133 if (!cc && scsw_stctl(&schib.scsw))
138 static int chsc_subchannel_freeze(struct subchannel *sch)
140 return cio_disable_subchannel(sch);
143 static int chsc_subchannel_restore(struct subchannel *sch)
145 return cio_enable_subchannel(sch, (u32)(unsigned long)sch);
148 static struct css_device_id chsc_subchannel_ids[] = {
149 { .match_flags = 0x1, .type =SUBCHANNEL_TYPE_CHSC, },
150 { /* end of list */ },
152 MODULE_DEVICE_TABLE(css, chsc_subchannel_ids);
154 static struct css_driver chsc_subchannel_driver = {
156 .owner = THIS_MODULE,
157 .name = "chsc_subchannel",
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,
170 static int __init chsc_init_dbfs(void)
172 chsc_debug_msg_id = debug_register("chsc_msg", 16, 1,
174 if (!chsc_debug_msg_id)
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)
181 debug_register_view(chsc_debug_log_id, &debug_hex_ascii_view);
182 debug_set_level(chsc_debug_log_id, 2);
185 if (chsc_debug_msg_id)
186 debug_unregister(chsc_debug_msg_id);
190 static void chsc_remove_dbfs(void)
192 debug_unregister(chsc_debug_log_id);
193 debug_unregister(chsc_debug_msg_id);
196 static int __init chsc_init_sch_driver(void)
198 return css_driver_register(&chsc_subchannel_driver);
201 static void chsc_cleanup_sch_driver(void)
203 css_driver_unregister(&chsc_subchannel_driver);
206 static DEFINE_SPINLOCK(chsc_lock);
208 static int chsc_subchannel_match_next_free(struct device *dev, void *data)
210 struct subchannel *sch = to_subchannel(dev);
212 return sch->schib.pmcw.ena && !scsw_fctl(&sch->schib.scsw);
215 static struct subchannel *chsc_get_next_subchannel(struct subchannel *sch)
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;
226 * chsc_async() - try to start a chsc request asynchronously
227 * @chsc_area: request to be started
228 * @request: request structure to associate
230 * Tries to start a chsc request on one of the existing chsc subchannels.
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
237 * interrupts disabled, chsc_lock held
239 static int chsc_async(struct chsc_async_area *chsc_area,
240 struct chsc_request *request)
243 struct chsc_private *private;
244 struct subchannel *sch = NULL;
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);
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);
268 sch->schib.scsw.cmd.fctl |= SCSW_FCTL_START_FUNC;
270 private->request = request;
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)
283 put_device(&sch->dev);
290 static void chsc_log_command(struct chsc_async_area *chsc_area)
294 sprintf(dbf, "CHSC:%x", chsc_area->header.code);
296 CHSC_LOG_HEX(0, chsc_area, 32);
299 static int chsc_examine_irb(struct chsc_request *request)
303 if (!(scsw_stctl(&request->irb.scsw) & SCSW_STCTL_STATUS_PEND))
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)
311 if (scsw_cstat(&request->irb.scsw) & SCHN_STAT_PROG_CHECK)
313 if (scsw_cstat(&request->irb.scsw) & SCHN_STAT_PROT_CHECK)
315 if (scsw_cstat(&request->irb.scsw) & SCHN_STAT_CHN_DATA_CHK)
317 if (scsw_cstat(&request->irb.scsw) & SCHN_STAT_CHN_CTRL_CHK)
322 static int chsc_ioctl_start(void __user *user_area)
324 struct chsc_request *request;
325 struct chsc_async_area *chsc_area;
329 if (!css_general_characteristics.dynio)
330 /* It makes no sense to try. */
332 chsc_area = (void *)get_zeroed_page(GFP_DMA | GFP_KERNEL);
335 request = kzalloc(sizeof(*request), GFP_KERNEL);
340 init_completion(&request->completion);
341 if (copy_from_user(chsc_area, user_area, PAGE_SIZE)) {
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);
353 /* copy area back to user */
355 if (copy_to_user(user_area, chsc_area, PAGE_SIZE))
358 sprintf(dbf, "ret:%d", ret);
361 free_page((unsigned long)chsc_area);
365 static int chsc_ioctl_info_channel_path(void __user *user_cd)
367 struct chsc_chp_cd *cd;
370 struct chsc_header request;
381 struct chsc_header response;
382 u8 data[PAGE_SIZE - 20];
383 } __attribute__ ((packed)) *scpcd_area;
385 scpcd_area = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
388 cd = kzalloc(sizeof(*cd), GFP_KERNEL);
393 if (copy_from_user(cd, user_cd, sizeof(*cd))) {
397 scpcd_area->request.length = 0x0010;
398 scpcd_area->request.code = 0x0028;
399 scpcd_area->m = cd->m;
400 scpcd_area->fmt1 = cd->fmt;
401 scpcd_area->cssid = cd->chpid.cssid;
402 scpcd_area->first_chpid = cd->chpid.id;
403 scpcd_area->last_chpid = cd->chpid.id;
405 ccode = chsc(scpcd_area);
410 if (scpcd_area->response.code != 0x0001) {
412 CHSC_MSG(0, "scpcd: response code=%x\n",
413 scpcd_area->response.code);
416 memcpy(&cd->cpcb, &scpcd_area->response, scpcd_area->response.length);
417 if (copy_to_user(user_cd, cd, sizeof(*cd)))
423 free_page((unsigned long)scpcd_area);
427 static int chsc_ioctl_info_cu(void __user *user_cd)
429 struct chsc_cu_cd *cd;
432 struct chsc_header request;
443 struct chsc_header response;
444 u8 data[PAGE_SIZE - 20];
445 } __attribute__ ((packed)) *scucd_area;
447 scucd_area = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
450 cd = kzalloc(sizeof(*cd), GFP_KERNEL);
455 if (copy_from_user(cd, user_cd, sizeof(*cd))) {
459 scucd_area->request.length = 0x0010;
460 scucd_area->request.code = 0x0028;
461 scucd_area->m = cd->m;
462 scucd_area->fmt1 = cd->fmt;
463 scucd_area->cssid = cd->cssid;
464 scucd_area->first_cun = cd->cun;
465 scucd_area->last_cun = cd->cun;
467 ccode = chsc(scucd_area);
472 if (scucd_area->response.code != 0x0001) {
474 CHSC_MSG(0, "scucd: response code=%x\n",
475 scucd_area->response.code);
478 memcpy(&cd->cucb, &scucd_area->response, scucd_area->response.length);
479 if (copy_to_user(user_cd, cd, sizeof(*cd)))
485 free_page((unsigned long)scucd_area);
489 static int chsc_ioctl_info_sch_cu(void __user *user_cud)
491 struct chsc_sch_cud *cud;
494 struct chsc_header request;
506 struct chsc_header response;
507 u8 data[PAGE_SIZE - 20];
508 } __attribute__ ((packed)) *sscud_area;
510 sscud_area = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
513 cud = kzalloc(sizeof(*cud), GFP_KERNEL);
518 if (copy_from_user(cud, user_cud, sizeof(*cud))) {
522 sscud_area->request.length = 0x0010;
523 sscud_area->request.code = 0x0006;
524 sscud_area->m = cud->schid.m;
525 sscud_area->fmt1 = cud->fmt;
526 sscud_area->ssid = cud->schid.ssid;
527 sscud_area->first_sch = cud->schid.sch_no;
528 sscud_area->cssid = cud->schid.cssid;
529 sscud_area->last_sch = cud->schid.sch_no;
531 ccode = chsc(sscud_area);
536 if (sscud_area->response.code != 0x0001) {
538 CHSC_MSG(0, "sscud: response code=%x\n",
539 sscud_area->response.code);
542 memcpy(&cud->scub, &sscud_area->response, sscud_area->response.length);
543 if (copy_to_user(user_cud, cud, sizeof(*cud)))
549 free_page((unsigned long)sscud_area);
553 static int chsc_ioctl_conf_info(void __user *user_ci)
555 struct chsc_conf_info *ci;
558 struct chsc_header request;
568 struct chsc_header response;
569 u8 data[PAGE_SIZE - 20];
570 } __attribute__ ((packed)) *sci_area;
572 sci_area = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
575 ci = kzalloc(sizeof(*ci), GFP_KERNEL);
580 if (copy_from_user(ci, user_ci, sizeof(*ci))) {
584 sci_area->request.length = 0x0010;
585 sci_area->request.code = 0x0012;
586 sci_area->m = ci->id.m;
587 sci_area->fmt1 = ci->fmt;
588 sci_area->cssid = ci->id.cssid;
589 sci_area->ssid = ci->id.ssid;
591 ccode = chsc(sci_area);
596 if (sci_area->response.code != 0x0001) {
598 CHSC_MSG(0, "sci: response code=%x\n",
599 sci_area->response.code);
602 memcpy(&ci->scid, &sci_area->response, sci_area->response.length);
603 if (copy_to_user(user_ci, ci, sizeof(*ci)))
609 free_page((unsigned long)sci_area);
613 static int chsc_ioctl_conf_comp_list(void __user *user_ccl)
615 struct chsc_comp_list *ccl;
618 struct chsc_header request;
626 struct chsc_header response;
627 u8 data[PAGE_SIZE - 36];
628 } __attribute__ ((packed)) *sccl_area;
635 } __attribute__ ((packed)) *chpid_parm;
641 } __attribute__ ((packed)) *cssids_parm;
643 sccl_area = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
646 ccl = kzalloc(sizeof(*ccl), GFP_KERNEL);
651 if (copy_from_user(ccl, user_ccl, sizeof(*ccl))) {
655 sccl_area->request.length = 0x0020;
656 sccl_area->request.code = 0x0030;
657 sccl_area->fmt = ccl->req.fmt;
658 sccl_area->ctype = ccl->req.ctype;
659 switch (sccl_area->ctype) {
662 chpid_parm = (void *)&sccl_area->list_parm;
663 chpid_parm->m = ccl->req.chpid.m;
664 chpid_parm->cssid = ccl->req.chpid.chp.cssid;
665 chpid_parm->chpid = ccl->req.chpid.chp.id;
668 case CCL_CSS_IMG_CONF_CHAR:
669 cssids_parm = (void *)&sccl_area->list_parm;
670 cssids_parm->f_cssid = ccl->req.cssids.f_cssid;
671 cssids_parm->l_cssid = ccl->req.cssids.l_cssid;
674 ccode = chsc(sccl_area);
679 if (sccl_area->response.code != 0x0001) {
681 CHSC_MSG(0, "sccl: response code=%x\n",
682 sccl_area->response.code);
685 memcpy(&ccl->sccl, &sccl_area->response, sccl_area->response.length);
686 if (copy_to_user(user_ccl, ccl, sizeof(*ccl)))
692 free_page((unsigned long)sccl_area);
696 static int chsc_ioctl_chpd(void __user *user_chpd)
698 struct chsc_scpd *scpd_area;
699 struct chsc_cpd_info *chpd;
702 chpd = kzalloc(sizeof(*chpd), GFP_KERNEL);
703 scpd_area = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
704 if (!scpd_area || !chpd) {
708 if (copy_from_user(chpd, user_chpd, sizeof(*chpd))) {
712 ret = chsc_determine_channel_path_desc(chpd->chpid, chpd->fmt,
713 chpd->rfmt, chpd->c, chpd->m,
717 memcpy(&chpd->chpdb, &scpd_area->response, scpd_area->response.length);
718 if (copy_to_user(user_chpd, chpd, sizeof(*chpd)))
722 free_page((unsigned long)scpd_area);
726 static int chsc_ioctl_dcal(void __user *user_dcal)
728 struct chsc_dcal *dcal;
731 struct chsc_header request;
739 struct chsc_header response;
740 u8 data[PAGE_SIZE - 36];
741 } __attribute__ ((packed)) *sdcal_area;
743 sdcal_area = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
746 dcal = kzalloc(sizeof(*dcal), GFP_KERNEL);
751 if (copy_from_user(dcal, user_dcal, sizeof(*dcal))) {
755 sdcal_area->request.length = 0x0020;
756 sdcal_area->request.code = 0x0034;
757 sdcal_area->atype = dcal->req.atype;
758 sdcal_area->fmt = dcal->req.fmt;
759 memcpy(&sdcal_area->list_parm, &dcal->req.list_parm,
760 sizeof(sdcal_area->list_parm));
762 ccode = chsc(sdcal_area);
767 if (sdcal_area->response.code != 0x0001) {
769 CHSC_MSG(0, "sdcal: response code=%x\n",
770 sdcal_area->response.code);
773 memcpy(&dcal->sdcal, &sdcal_area->response,
774 sdcal_area->response.length);
775 if (copy_to_user(user_dcal, dcal, sizeof(*dcal)))
781 free_page((unsigned long)sdcal_area);
785 static long chsc_ioctl(struct file *filp, unsigned int cmd,
790 CHSC_MSG(2, "chsc_ioctl called, cmd=%x\n", cmd);
791 if (is_compat_task())
792 argp = compat_ptr(arg);
794 argp = (void __user *)arg;
797 return chsc_ioctl_start(argp);
798 case CHSC_INFO_CHANNEL_PATH:
799 return chsc_ioctl_info_channel_path(argp);
801 return chsc_ioctl_info_cu(argp);
802 case CHSC_INFO_SCH_CU:
803 return chsc_ioctl_info_sch_cu(argp);
805 return chsc_ioctl_conf_info(argp);
807 return chsc_ioctl_conf_comp_list(argp);
809 return chsc_ioctl_chpd(argp);
811 return chsc_ioctl_dcal(argp);
812 default: /* unknown ioctl number */
817 static const struct file_operations chsc_fops = {
818 .owner = THIS_MODULE,
819 .open = nonseekable_open,
820 .unlocked_ioctl = chsc_ioctl,
821 .compat_ioctl = chsc_ioctl,
825 static struct miscdevice chsc_misc_device = {
826 .minor = MISC_DYNAMIC_MINOR,
831 static int __init chsc_misc_init(void)
833 return misc_register(&chsc_misc_device);
836 static void chsc_misc_cleanup(void)
838 misc_deregister(&chsc_misc_device);
841 static int __init chsc_sch_init(void)
845 ret = chsc_init_dbfs();
848 isc_register(CHSC_SCH_ISC);
849 ret = chsc_init_sch_driver();
852 ret = chsc_misc_init();
857 chsc_cleanup_sch_driver();
859 isc_unregister(CHSC_SCH_ISC);
864 static void __exit chsc_sch_exit(void)
867 chsc_cleanup_sch_driver();
868 isc_unregister(CHSC_SCH_ISC);
872 module_init(chsc_sch_init);
873 module_exit(chsc_sch_exit);