1 // SPDX-License-Identifier: GPL-2.0
3 * Driver for s390 chsc subchannels
5 * Copyright IBM Corp. 2008, 2011
11 #include <linux/slab.h>
12 #include <linux/compat.h>
13 #include <linux/device.h>
14 #include <linux/module.h>
15 #include <linux/uaccess.h>
16 #include <linux/miscdevice.h>
17 #include <linux/kernel_stat.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 static struct chsc_request *on_close_request;
33 static struct chsc_async_area *on_close_chsc_area;
34 static DEFINE_MUTEX(on_close_mutex);
36 #define CHSC_MSG(imp, args...) do { \
37 debug_sprintf_event(chsc_debug_msg_id, imp , ##args); \
40 #define CHSC_LOG(imp, txt) do { \
41 debug_text_event(chsc_debug_log_id, imp , txt); \
44 static void CHSC_LOG_HEX(int level, void *data, int length)
46 debug_event(chsc_debug_log_id, level, data, length);
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 = this_cpu_ptr(&cio_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 struct css_device_id chsc_subchannel_ids[] = {
124 { .match_flags = 0x1, .type =SUBCHANNEL_TYPE_CHSC, },
125 { /* end of list */ },
127 MODULE_DEVICE_TABLE(css, chsc_subchannel_ids);
129 static struct css_driver chsc_subchannel_driver = {
131 .owner = THIS_MODULE,
132 .name = "chsc_subchannel",
134 .subchannel_type = chsc_subchannel_ids,
135 .irq = chsc_subchannel_irq,
136 .probe = chsc_subchannel_probe,
137 .remove = chsc_subchannel_remove,
138 .shutdown = chsc_subchannel_shutdown,
141 static int __init chsc_init_dbfs(void)
143 chsc_debug_msg_id = debug_register("chsc_msg", 8, 1, 4 * sizeof(long));
144 if (!chsc_debug_msg_id)
146 debug_register_view(chsc_debug_msg_id, &debug_sprintf_view);
147 debug_set_level(chsc_debug_msg_id, 2);
148 chsc_debug_log_id = debug_register("chsc_log", 16, 1, 16);
149 if (!chsc_debug_log_id)
151 debug_register_view(chsc_debug_log_id, &debug_hex_ascii_view);
152 debug_set_level(chsc_debug_log_id, 2);
155 debug_unregister(chsc_debug_msg_id);
159 static void chsc_remove_dbfs(void)
161 debug_unregister(chsc_debug_log_id);
162 debug_unregister(chsc_debug_msg_id);
165 static int __init chsc_init_sch_driver(void)
167 return css_driver_register(&chsc_subchannel_driver);
170 static void chsc_cleanup_sch_driver(void)
172 css_driver_unregister(&chsc_subchannel_driver);
175 static DEFINE_SPINLOCK(chsc_lock);
177 static int chsc_subchannel_match_next_free(struct device *dev, const void *data)
179 struct subchannel *sch = to_subchannel(dev);
181 return sch->schib.pmcw.ena && !scsw_fctl(&sch->schib.scsw);
184 static struct subchannel *chsc_get_next_subchannel(struct subchannel *sch)
188 dev = driver_find_device(&chsc_subchannel_driver.drv,
189 sch ? &sch->dev : NULL, NULL,
190 chsc_subchannel_match_next_free);
191 return dev ? to_subchannel(dev) : NULL;
195 * chsc_async() - try to start a chsc request asynchronously
196 * @chsc_area: request to be started
197 * @request: request structure to associate
199 * Tries to start a chsc request on one of the existing chsc subchannels.
201 * %0 if the request was performed synchronously
202 * %-EINPROGRESS if the request was successfully started
203 * %-EBUSY if all chsc subchannels are busy
204 * %-ENODEV if no chsc subchannels are available
206 * interrupts disabled, chsc_lock held
208 static int chsc_async(struct chsc_async_area *chsc_area,
209 struct chsc_request *request)
212 struct chsc_private *private;
213 struct subchannel *sch = NULL;
217 chsc_area->header.key = PAGE_DEFAULT_KEY >> 4;
218 while ((sch = chsc_get_next_subchannel(sch))) {
219 spin_lock(sch->lock);
220 private = dev_get_drvdata(&sch->dev);
221 if (private->request) {
222 spin_unlock(sch->lock);
226 chsc_area->header.sid = sch->schid;
227 CHSC_LOG(2, "schid");
228 CHSC_LOG_HEX(2, &sch->schid, sizeof(sch->schid));
229 cc = chsc(chsc_area);
230 snprintf(dbf, sizeof(dbf), "cc:%d", cc);
237 sch->schib.scsw.cmd.fctl |= SCSW_FCTL_START_FUNC;
239 private->request = request;
247 spin_unlock(sch->lock);
248 CHSC_MSG(2, "chsc on 0.%x.%04x returned cc=%d\n",
249 sch->schid.ssid, sch->schid.sch_no, cc);
250 if (ret == -EINPROGRESS)
252 put_device(&sch->dev);
259 static void chsc_log_command(void *chsc_area)
263 snprintf(dbf, sizeof(dbf), "CHSC:%x", ((uint16_t *)chsc_area)[1]);
265 CHSC_LOG_HEX(0, chsc_area, 32);
268 static int chsc_examine_irb(struct chsc_request *request)
272 if (!(scsw_stctl(&request->irb.scsw) & SCSW_STCTL_STATUS_PEND))
274 backed_up = scsw_cstat(&request->irb.scsw) & SCHN_STAT_CHAIN_CHECK;
275 request->irb.scsw.cmd.cstat &= ~SCHN_STAT_CHAIN_CHECK;
276 if (scsw_cstat(&request->irb.scsw) == 0)
280 if (scsw_cstat(&request->irb.scsw) & SCHN_STAT_PROG_CHECK)
282 if (scsw_cstat(&request->irb.scsw) & SCHN_STAT_PROT_CHECK)
284 if (scsw_cstat(&request->irb.scsw) & SCHN_STAT_CHN_DATA_CHK)
286 if (scsw_cstat(&request->irb.scsw) & SCHN_STAT_CHN_CTRL_CHK)
291 static int chsc_ioctl_start(void __user *user_area)
293 struct chsc_request *request;
294 struct chsc_async_area *chsc_area;
298 if (!css_general_characteristics.dynio)
299 /* It makes no sense to try. */
301 chsc_area = (void *)get_zeroed_page(GFP_DMA | GFP_KERNEL);
304 request = kzalloc(sizeof(*request), GFP_KERNEL);
309 init_completion(&request->completion);
310 if (copy_from_user(chsc_area, user_area, PAGE_SIZE)) {
314 chsc_log_command(chsc_area);
315 spin_lock_irq(&chsc_lock);
316 ret = chsc_async(chsc_area, request);
317 spin_unlock_irq(&chsc_lock);
318 if (ret == -EINPROGRESS) {
319 wait_for_completion(&request->completion);
320 ret = chsc_examine_irb(request);
322 /* copy area back to user */
324 if (copy_to_user(user_area, chsc_area, PAGE_SIZE))
327 snprintf(dbf, sizeof(dbf), "ret:%d", ret);
330 free_page((unsigned long)chsc_area);
334 static int chsc_ioctl_on_close_set(void __user *user_area)
339 mutex_lock(&on_close_mutex);
340 if (on_close_chsc_area) {
344 on_close_request = kzalloc(sizeof(*on_close_request), GFP_KERNEL);
345 if (!on_close_request) {
349 on_close_chsc_area = (void *)get_zeroed_page(GFP_DMA | GFP_KERNEL);
350 if (!on_close_chsc_area) {
352 goto out_free_request;
354 if (copy_from_user(on_close_chsc_area, user_area, PAGE_SIZE)) {
362 free_page((unsigned long)on_close_chsc_area);
363 on_close_chsc_area = NULL;
365 kfree(on_close_request);
366 on_close_request = NULL;
368 mutex_unlock(&on_close_mutex);
369 snprintf(dbf, sizeof(dbf), "ocsret:%d", ret);
374 static int chsc_ioctl_on_close_remove(void)
379 mutex_lock(&on_close_mutex);
380 if (!on_close_chsc_area) {
384 free_page((unsigned long)on_close_chsc_area);
385 on_close_chsc_area = NULL;
386 kfree(on_close_request);
387 on_close_request = NULL;
390 mutex_unlock(&on_close_mutex);
391 snprintf(dbf, sizeof(dbf), "ocrret:%d", ret);
396 static int chsc_ioctl_start_sync(void __user *user_area)
398 struct chsc_sync_area *chsc_area;
401 chsc_area = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
404 if (copy_from_user(chsc_area, user_area, PAGE_SIZE)) {
408 if (chsc_area->header.code & 0x4000) {
412 chsc_log_command(chsc_area);
413 ccode = chsc(chsc_area);
418 if (copy_to_user(user_area, chsc_area, PAGE_SIZE))
423 free_page((unsigned long)chsc_area);
427 static int chsc_ioctl_info_channel_path(void __user *user_cd)
429 struct chsc_chp_cd *cd;
432 struct chsc_header request;
443 struct chsc_header response;
444 u8 data[PAGE_SIZE - 20];
445 } __attribute__ ((packed)) *scpcd_area;
447 scpcd_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 scpcd_area->request.length = 0x0010;
460 scpcd_area->request.code = 0x0028;
461 scpcd_area->m = cd->m;
462 scpcd_area->fmt1 = cd->fmt;
463 scpcd_area->cssid = cd->chpid.cssid;
464 scpcd_area->first_chpid = cd->chpid.id;
465 scpcd_area->last_chpid = cd->chpid.id;
467 ccode = chsc(scpcd_area);
472 if (scpcd_area->response.code != 0x0001) {
474 CHSC_MSG(0, "scpcd: response code=%x\n",
475 scpcd_area->response.code);
478 memcpy(&cd->cpcb, &scpcd_area->response, scpcd_area->response.length);
479 if (copy_to_user(user_cd, cd, sizeof(*cd)))
485 free_page((unsigned long)scpcd_area);
489 static int chsc_ioctl_info_cu(void __user *user_cd)
491 struct chsc_cu_cd *cd;
494 struct chsc_header request;
505 struct chsc_header response;
506 u8 data[PAGE_SIZE - 20];
507 } __attribute__ ((packed)) *scucd_area;
509 scucd_area = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
512 cd = kzalloc(sizeof(*cd), GFP_KERNEL);
517 if (copy_from_user(cd, user_cd, sizeof(*cd))) {
521 scucd_area->request.length = 0x0010;
522 scucd_area->request.code = 0x0026;
523 scucd_area->m = cd->m;
524 scucd_area->fmt1 = cd->fmt;
525 scucd_area->cssid = cd->cssid;
526 scucd_area->first_cun = cd->cun;
527 scucd_area->last_cun = cd->cun;
529 ccode = chsc(scucd_area);
534 if (scucd_area->response.code != 0x0001) {
536 CHSC_MSG(0, "scucd: response code=%x\n",
537 scucd_area->response.code);
540 memcpy(&cd->cucb, &scucd_area->response, scucd_area->response.length);
541 if (copy_to_user(user_cd, cd, sizeof(*cd)))
547 free_page((unsigned long)scucd_area);
551 static int chsc_ioctl_info_sch_cu(void __user *user_cud)
553 struct chsc_sch_cud *cud;
556 struct chsc_header request;
568 struct chsc_header response;
569 u8 data[PAGE_SIZE - 20];
570 } __attribute__ ((packed)) *sscud_area;
572 sscud_area = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
575 cud = kzalloc(sizeof(*cud), GFP_KERNEL);
580 if (copy_from_user(cud, user_cud, sizeof(*cud))) {
584 sscud_area->request.length = 0x0010;
585 sscud_area->request.code = 0x0006;
586 sscud_area->m = cud->schid.m;
587 sscud_area->fmt1 = cud->fmt;
588 sscud_area->ssid = cud->schid.ssid;
589 sscud_area->first_sch = cud->schid.sch_no;
590 sscud_area->cssid = cud->schid.cssid;
591 sscud_area->last_sch = cud->schid.sch_no;
593 ccode = chsc(sscud_area);
598 if (sscud_area->response.code != 0x0001) {
600 CHSC_MSG(0, "sscud: response code=%x\n",
601 sscud_area->response.code);
604 memcpy(&cud->scub, &sscud_area->response, sscud_area->response.length);
605 if (copy_to_user(user_cud, cud, sizeof(*cud)))
611 free_page((unsigned long)sscud_area);
615 static int chsc_ioctl_conf_info(void __user *user_ci)
617 struct chsc_conf_info *ci;
620 struct chsc_header request;
630 struct chsc_header response;
631 u8 data[PAGE_SIZE - 20];
632 } __attribute__ ((packed)) *sci_area;
634 sci_area = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
637 ci = kzalloc(sizeof(*ci), GFP_KERNEL);
642 if (copy_from_user(ci, user_ci, sizeof(*ci))) {
646 sci_area->request.length = 0x0010;
647 sci_area->request.code = 0x0012;
648 sci_area->m = ci->id.m;
649 sci_area->fmt1 = ci->fmt;
650 sci_area->cssid = ci->id.cssid;
651 sci_area->ssid = ci->id.ssid;
653 ccode = chsc(sci_area);
658 if (sci_area->response.code != 0x0001) {
660 CHSC_MSG(0, "sci: response code=%x\n",
661 sci_area->response.code);
664 memcpy(&ci->scid, &sci_area->response, sci_area->response.length);
665 if (copy_to_user(user_ci, ci, sizeof(*ci)))
671 free_page((unsigned long)sci_area);
675 static int chsc_ioctl_conf_comp_list(void __user *user_ccl)
677 struct chsc_comp_list *ccl;
680 struct chsc_header request;
688 struct chsc_header response;
689 u8 data[PAGE_SIZE - 36];
690 } __attribute__ ((packed)) *sccl_area;
697 } __attribute__ ((packed)) *chpid_parm;
703 } __attribute__ ((packed)) *cssids_parm;
705 sccl_area = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
708 ccl = kzalloc(sizeof(*ccl), GFP_KERNEL);
713 if (copy_from_user(ccl, user_ccl, sizeof(*ccl))) {
717 sccl_area->request.length = 0x0020;
718 sccl_area->request.code = 0x0030;
719 sccl_area->fmt = ccl->req.fmt;
720 sccl_area->ctype = ccl->req.ctype;
721 switch (sccl_area->ctype) {
724 chpid_parm = (void *)&sccl_area->list_parm;
725 chpid_parm->m = ccl->req.chpid.m;
726 chpid_parm->cssid = ccl->req.chpid.chp.cssid;
727 chpid_parm->chpid = ccl->req.chpid.chp.id;
730 case CCL_CSS_IMG_CONF_CHAR:
731 cssids_parm = (void *)&sccl_area->list_parm;
732 cssids_parm->f_cssid = ccl->req.cssids.f_cssid;
733 cssids_parm->l_cssid = ccl->req.cssids.l_cssid;
736 ccode = chsc(sccl_area);
741 if (sccl_area->response.code != 0x0001) {
743 CHSC_MSG(0, "sccl: response code=%x\n",
744 sccl_area->response.code);
747 memcpy(&ccl->sccl, &sccl_area->response, sccl_area->response.length);
748 if (copy_to_user(user_ccl, ccl, sizeof(*ccl)))
754 free_page((unsigned long)sccl_area);
758 static int chsc_ioctl_chpd(void __user *user_chpd)
760 struct chsc_scpd *scpd_area;
761 struct chsc_cpd_info *chpd;
764 chpd = kzalloc(sizeof(*chpd), GFP_KERNEL);
765 scpd_area = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
766 if (!scpd_area || !chpd) {
770 if (copy_from_user(chpd, user_chpd, sizeof(*chpd))) {
774 ret = chsc_determine_channel_path_desc(chpd->chpid, chpd->fmt,
775 chpd->rfmt, chpd->c, chpd->m,
779 memcpy(&chpd->chpdb, &scpd_area->response, scpd_area->response.length);
780 if (copy_to_user(user_chpd, chpd, sizeof(*chpd)))
784 free_page((unsigned long)scpd_area);
788 static int chsc_ioctl_dcal(void __user *user_dcal)
790 struct chsc_dcal *dcal;
793 struct chsc_header request;
801 struct chsc_header response;
802 u8 data[PAGE_SIZE - 36];
803 } __attribute__ ((packed)) *sdcal_area;
805 sdcal_area = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
808 dcal = kzalloc(sizeof(*dcal), GFP_KERNEL);
813 if (copy_from_user(dcal, user_dcal, sizeof(*dcal))) {
817 sdcal_area->request.length = 0x0020;
818 sdcal_area->request.code = 0x0034;
819 sdcal_area->atype = dcal->req.atype;
820 sdcal_area->fmt = dcal->req.fmt;
821 memcpy(&sdcal_area->list_parm, &dcal->req.list_parm,
822 sizeof(sdcal_area->list_parm));
824 ccode = chsc(sdcal_area);
829 if (sdcal_area->response.code != 0x0001) {
831 CHSC_MSG(0, "sdcal: response code=%x\n",
832 sdcal_area->response.code);
835 memcpy(&dcal->sdcal, &sdcal_area->response,
836 sdcal_area->response.length);
837 if (copy_to_user(user_dcal, dcal, sizeof(*dcal)))
843 free_page((unsigned long)sdcal_area);
847 static long chsc_ioctl(struct file *filp, unsigned int cmd,
852 CHSC_MSG(2, "chsc_ioctl called, cmd=%x\n", cmd);
853 if (is_compat_task())
854 argp = compat_ptr(arg);
856 argp = (void __user *)arg;
859 return chsc_ioctl_start(argp);
860 case CHSC_START_SYNC:
861 return chsc_ioctl_start_sync(argp);
862 case CHSC_INFO_CHANNEL_PATH:
863 return chsc_ioctl_info_channel_path(argp);
865 return chsc_ioctl_info_cu(argp);
866 case CHSC_INFO_SCH_CU:
867 return chsc_ioctl_info_sch_cu(argp);
869 return chsc_ioctl_conf_info(argp);
871 return chsc_ioctl_conf_comp_list(argp);
873 return chsc_ioctl_chpd(argp);
875 return chsc_ioctl_dcal(argp);
876 case CHSC_ON_CLOSE_SET:
877 return chsc_ioctl_on_close_set(argp);
878 case CHSC_ON_CLOSE_REMOVE:
879 return chsc_ioctl_on_close_remove();
880 default: /* unknown ioctl number */
885 static atomic_t chsc_ready_for_use = ATOMIC_INIT(1);
887 static int chsc_open(struct inode *inode, struct file *file)
889 if (!atomic_dec_and_test(&chsc_ready_for_use)) {
890 atomic_inc(&chsc_ready_for_use);
893 return nonseekable_open(inode, file);
896 static int chsc_release(struct inode *inode, struct file *filp)
901 mutex_lock(&on_close_mutex);
902 if (!on_close_chsc_area)
904 init_completion(&on_close_request->completion);
905 CHSC_LOG(0, "on_close");
906 chsc_log_command(on_close_chsc_area);
907 spin_lock_irq(&chsc_lock);
908 ret = chsc_async(on_close_chsc_area, on_close_request);
909 spin_unlock_irq(&chsc_lock);
910 if (ret == -EINPROGRESS) {
911 wait_for_completion(&on_close_request->completion);
912 ret = chsc_examine_irb(on_close_request);
914 snprintf(dbf, sizeof(dbf), "relret:%d", ret);
916 free_page((unsigned long)on_close_chsc_area);
917 on_close_chsc_area = NULL;
918 kfree(on_close_request);
919 on_close_request = NULL;
921 mutex_unlock(&on_close_mutex);
922 atomic_inc(&chsc_ready_for_use);
926 static const struct file_operations chsc_fops = {
927 .owner = THIS_MODULE,
929 .release = chsc_release,
930 .unlocked_ioctl = chsc_ioctl,
931 .compat_ioctl = chsc_ioctl,
935 static struct miscdevice chsc_misc_device = {
936 .minor = MISC_DYNAMIC_MINOR,
941 static int __init chsc_misc_init(void)
943 return misc_register(&chsc_misc_device);
946 static void chsc_misc_cleanup(void)
948 misc_deregister(&chsc_misc_device);
951 static int __init chsc_sch_init(void)
955 ret = chsc_init_dbfs();
958 isc_register(CHSC_SCH_ISC);
959 ret = chsc_init_sch_driver();
962 ret = chsc_misc_init();
967 chsc_cleanup_sch_driver();
969 isc_unregister(CHSC_SCH_ISC);
974 static void __exit chsc_sch_exit(void)
977 chsc_cleanup_sch_driver();
978 isc_unregister(CHSC_SCH_ISC);
982 module_init(chsc_sch_init);
983 module_exit(chsc_sch_exit);