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>
15 #include <linux/module.h>
16 #include <linux/uaccess.h>
17 #include <linux/miscdevice.h>
18 #include <linux/kernel_stat.h>
25 #include "cio_debug.h"
30 static debug_info_t *chsc_debug_msg_id;
31 static debug_info_t *chsc_debug_log_id;
33 static struct chsc_request *on_close_request;
34 static struct chsc_async_area *on_close_chsc_area;
35 static DEFINE_MUTEX(on_close_mutex);
37 #define CHSC_MSG(imp, args...) do { \
38 debug_sprintf_event(chsc_debug_msg_id, imp , ##args); \
41 #define CHSC_LOG(imp, txt) do { \
42 debug_text_event(chsc_debug_log_id, imp , txt); \
45 static void CHSC_LOG_HEX(int level, void *data, int length)
47 debug_event(chsc_debug_log_id, level, data, length);
50 MODULE_AUTHOR("IBM Corporation");
51 MODULE_DESCRIPTION("driver for s390 chsc subchannels");
52 MODULE_LICENSE("GPL");
54 static void chsc_subchannel_irq(struct subchannel *sch)
56 struct chsc_private *private = dev_get_drvdata(&sch->dev);
57 struct chsc_request *request = private->request;
58 struct irb *irb = this_cpu_ptr(&cio_irb);
61 CHSC_LOG_HEX(4, irb, sizeof(*irb));
62 inc_irq_stat(IRQIO_CSC);
64 /* Copy irb to provided request and set done. */
66 CHSC_MSG(0, "Interrupt on sch 0.%x.%04x with no request\n",
67 sch->schid.ssid, sch->schid.sch_no);
70 private->request = NULL;
71 memcpy(&request->irb, irb, sizeof(*irb));
72 cio_update_schib(sch);
73 complete(&request->completion);
74 put_device(&sch->dev);
77 static int chsc_subchannel_probe(struct subchannel *sch)
79 struct chsc_private *private;
82 CHSC_MSG(6, "Detected chsc subchannel 0.%x.%04x\n",
83 sch->schid.ssid, sch->schid.sch_no);
84 sch->isc = CHSC_SCH_ISC;
85 private = kzalloc(sizeof(*private), GFP_KERNEL);
88 dev_set_drvdata(&sch->dev, private);
89 ret = cio_enable_subchannel(sch, (u32)virt_to_phys(sch));
91 CHSC_MSG(0, "Failed to enable 0.%x.%04x: %d\n",
92 sch->schid.ssid, sch->schid.sch_no, ret);
93 dev_set_drvdata(&sch->dev, NULL);
99 static void chsc_subchannel_remove(struct subchannel *sch)
101 struct chsc_private *private;
103 cio_disable_subchannel(sch);
104 private = dev_get_drvdata(&sch->dev);
105 dev_set_drvdata(&sch->dev, NULL);
106 if (private->request) {
107 complete(&private->request->completion);
108 put_device(&sch->dev);
113 static void chsc_subchannel_shutdown(struct subchannel *sch)
115 cio_disable_subchannel(sch);
118 static struct css_device_id chsc_subchannel_ids[] = {
119 { .match_flags = 0x1, .type =SUBCHANNEL_TYPE_CHSC, },
120 { /* end of list */ },
122 MODULE_DEVICE_TABLE(css, chsc_subchannel_ids);
124 static struct css_driver chsc_subchannel_driver = {
126 .owner = THIS_MODULE,
127 .name = "chsc_subchannel",
129 .subchannel_type = chsc_subchannel_ids,
130 .irq = chsc_subchannel_irq,
131 .probe = chsc_subchannel_probe,
132 .remove = chsc_subchannel_remove,
133 .shutdown = chsc_subchannel_shutdown,
136 static int __init chsc_init_dbfs(void)
138 chsc_debug_msg_id = debug_register("chsc_msg", 8, 1, 4 * sizeof(long));
139 if (!chsc_debug_msg_id)
141 debug_register_view(chsc_debug_msg_id, &debug_sprintf_view);
142 debug_set_level(chsc_debug_msg_id, 2);
143 chsc_debug_log_id = debug_register("chsc_log", 16, 1, 16);
144 if (!chsc_debug_log_id)
146 debug_register_view(chsc_debug_log_id, &debug_hex_ascii_view);
147 debug_set_level(chsc_debug_log_id, 2);
150 debug_unregister(chsc_debug_msg_id);
154 static void chsc_remove_dbfs(void)
156 debug_unregister(chsc_debug_log_id);
157 debug_unregister(chsc_debug_msg_id);
160 static int __init chsc_init_sch_driver(void)
162 return css_driver_register(&chsc_subchannel_driver);
165 static void chsc_cleanup_sch_driver(void)
167 css_driver_unregister(&chsc_subchannel_driver);
170 static DEFINE_SPINLOCK(chsc_lock);
172 static int chsc_subchannel_match_next_free(struct device *dev, const void *data)
174 struct subchannel *sch = to_subchannel(dev);
176 return sch->schib.pmcw.ena && !scsw_fctl(&sch->schib.scsw);
179 static struct subchannel *chsc_get_next_subchannel(struct subchannel *sch)
183 dev = driver_find_device(&chsc_subchannel_driver.drv,
184 sch ? &sch->dev : NULL, NULL,
185 chsc_subchannel_match_next_free);
186 return dev ? to_subchannel(dev) : NULL;
190 * chsc_async() - try to start a chsc request asynchronously
191 * @chsc_area: request to be started
192 * @request: request structure to associate
194 * Tries to start a chsc request on one of the existing chsc subchannels.
196 * %0 if the request was performed synchronously
197 * %-EINPROGRESS if the request was successfully started
198 * %-EBUSY if all chsc subchannels are busy
199 * %-ENODEV if no chsc subchannels are available
201 * interrupts disabled, chsc_lock held
203 static int chsc_async(struct chsc_async_area *chsc_area,
204 struct chsc_request *request)
207 struct chsc_private *private;
208 struct subchannel *sch = NULL;
212 chsc_area->header.key = PAGE_DEFAULT_KEY >> 4;
213 while ((sch = chsc_get_next_subchannel(sch))) {
214 spin_lock(sch->lock);
215 private = dev_get_drvdata(&sch->dev);
216 if (private->request) {
217 spin_unlock(sch->lock);
221 chsc_area->header.sid = sch->schid;
222 CHSC_LOG(2, "schid");
223 CHSC_LOG_HEX(2, &sch->schid, sizeof(sch->schid));
224 cc = chsc(chsc_area);
225 snprintf(dbf, sizeof(dbf), "cc:%d", cc);
232 sch->schib.scsw.cmd.fctl |= SCSW_FCTL_START_FUNC;
234 private->request = request;
242 spin_unlock(sch->lock);
243 CHSC_MSG(2, "chsc on 0.%x.%04x returned cc=%d\n",
244 sch->schid.ssid, sch->schid.sch_no, cc);
245 if (ret == -EINPROGRESS)
247 put_device(&sch->dev);
254 static void chsc_log_command(void *chsc_area)
258 snprintf(dbf, sizeof(dbf), "CHSC:%x", ((uint16_t *)chsc_area)[1]);
260 CHSC_LOG_HEX(0, chsc_area, 32);
263 static int chsc_examine_irb(struct chsc_request *request)
267 if (!(scsw_stctl(&request->irb.scsw) & SCSW_STCTL_STATUS_PEND))
269 backed_up = scsw_cstat(&request->irb.scsw) & SCHN_STAT_CHAIN_CHECK;
270 request->irb.scsw.cmd.cstat &= ~SCHN_STAT_CHAIN_CHECK;
271 if (scsw_cstat(&request->irb.scsw) == 0)
275 if (scsw_cstat(&request->irb.scsw) & SCHN_STAT_PROG_CHECK)
277 if (scsw_cstat(&request->irb.scsw) & SCHN_STAT_PROT_CHECK)
279 if (scsw_cstat(&request->irb.scsw) & SCHN_STAT_CHN_DATA_CHK)
281 if (scsw_cstat(&request->irb.scsw) & SCHN_STAT_CHN_CTRL_CHK)
286 static int chsc_ioctl_start(void __user *user_area)
288 struct chsc_request *request;
289 struct chsc_async_area *chsc_area;
293 if (!css_general_characteristics.dynio)
294 /* It makes no sense to try. */
296 chsc_area = (void *)get_zeroed_page(GFP_DMA | GFP_KERNEL);
299 request = kzalloc(sizeof(*request), GFP_KERNEL);
304 init_completion(&request->completion);
305 if (copy_from_user(chsc_area, user_area, PAGE_SIZE)) {
309 chsc_log_command(chsc_area);
310 spin_lock_irq(&chsc_lock);
311 ret = chsc_async(chsc_area, request);
312 spin_unlock_irq(&chsc_lock);
313 if (ret == -EINPROGRESS) {
314 wait_for_completion(&request->completion);
315 ret = chsc_examine_irb(request);
317 /* copy area back to user */
319 if (copy_to_user(user_area, chsc_area, PAGE_SIZE))
322 snprintf(dbf, sizeof(dbf), "ret:%d", ret);
325 free_page((unsigned long)chsc_area);
329 static int chsc_ioctl_on_close_set(void __user *user_area)
334 mutex_lock(&on_close_mutex);
335 if (on_close_chsc_area) {
339 on_close_request = kzalloc(sizeof(*on_close_request), GFP_KERNEL);
340 if (!on_close_request) {
344 on_close_chsc_area = (void *)get_zeroed_page(GFP_DMA | GFP_KERNEL);
345 if (!on_close_chsc_area) {
347 goto out_free_request;
349 if (copy_from_user(on_close_chsc_area, user_area, PAGE_SIZE)) {
357 free_page((unsigned long)on_close_chsc_area);
358 on_close_chsc_area = NULL;
360 kfree(on_close_request);
361 on_close_request = NULL;
363 mutex_unlock(&on_close_mutex);
364 snprintf(dbf, sizeof(dbf), "ocsret:%d", ret);
369 static int chsc_ioctl_on_close_remove(void)
374 mutex_lock(&on_close_mutex);
375 if (!on_close_chsc_area) {
379 free_page((unsigned long)on_close_chsc_area);
380 on_close_chsc_area = NULL;
381 kfree(on_close_request);
382 on_close_request = NULL;
385 mutex_unlock(&on_close_mutex);
386 snprintf(dbf, sizeof(dbf), "ocrret:%d", ret);
391 static int chsc_ioctl_start_sync(void __user *user_area)
393 struct chsc_sync_area *chsc_area;
396 chsc_area = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
399 if (copy_from_user(chsc_area, user_area, PAGE_SIZE)) {
403 if (chsc_area->header.code & 0x4000) {
407 chsc_log_command(chsc_area);
408 ccode = chsc(chsc_area);
413 if (copy_to_user(user_area, chsc_area, PAGE_SIZE))
418 free_page((unsigned long)chsc_area);
422 static int chsc_ioctl_info_channel_path(void __user *user_cd)
424 struct chsc_chp_cd *cd;
427 struct chsc_header request;
438 struct chsc_header response;
439 u8 data[PAGE_SIZE - 20];
440 } __attribute__ ((packed)) *scpcd_area;
442 scpcd_area = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
445 cd = kzalloc(sizeof(*cd), GFP_KERNEL);
450 if (copy_from_user(cd, user_cd, sizeof(*cd))) {
454 scpcd_area->request.length = 0x0010;
455 scpcd_area->request.code = 0x0028;
456 scpcd_area->m = cd->m;
457 scpcd_area->fmt1 = cd->fmt;
458 scpcd_area->cssid = cd->chpid.cssid;
459 scpcd_area->first_chpid = cd->chpid.id;
460 scpcd_area->last_chpid = cd->chpid.id;
462 ccode = chsc(scpcd_area);
467 if (scpcd_area->response.code != 0x0001) {
469 CHSC_MSG(0, "scpcd: response code=%x\n",
470 scpcd_area->response.code);
473 memcpy(&cd->cpcb, &scpcd_area->response, scpcd_area->response.length);
474 if (copy_to_user(user_cd, cd, sizeof(*cd)))
480 free_page((unsigned long)scpcd_area);
484 static int chsc_ioctl_info_cu(void __user *user_cd)
486 struct chsc_cu_cd *cd;
489 struct chsc_header request;
500 struct chsc_header response;
501 u8 data[PAGE_SIZE - 20];
502 } __attribute__ ((packed)) *scucd_area;
504 scucd_area = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
507 cd = kzalloc(sizeof(*cd), GFP_KERNEL);
512 if (copy_from_user(cd, user_cd, sizeof(*cd))) {
516 scucd_area->request.length = 0x0010;
517 scucd_area->request.code = 0x0026;
518 scucd_area->m = cd->m;
519 scucd_area->fmt1 = cd->fmt;
520 scucd_area->cssid = cd->cssid;
521 scucd_area->first_cun = cd->cun;
522 scucd_area->last_cun = cd->cun;
524 ccode = chsc(scucd_area);
529 if (scucd_area->response.code != 0x0001) {
531 CHSC_MSG(0, "scucd: response code=%x\n",
532 scucd_area->response.code);
535 memcpy(&cd->cucb, &scucd_area->response, scucd_area->response.length);
536 if (copy_to_user(user_cd, cd, sizeof(*cd)))
542 free_page((unsigned long)scucd_area);
546 static int chsc_ioctl_info_sch_cu(void __user *user_cud)
548 struct chsc_sch_cud *cud;
551 struct chsc_header request;
563 struct chsc_header response;
564 u8 data[PAGE_SIZE - 20];
565 } __attribute__ ((packed)) *sscud_area;
567 sscud_area = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
570 cud = kzalloc(sizeof(*cud), GFP_KERNEL);
575 if (copy_from_user(cud, user_cud, sizeof(*cud))) {
579 sscud_area->request.length = 0x0010;
580 sscud_area->request.code = 0x0006;
581 sscud_area->m = cud->schid.m;
582 sscud_area->fmt1 = cud->fmt;
583 sscud_area->ssid = cud->schid.ssid;
584 sscud_area->first_sch = cud->schid.sch_no;
585 sscud_area->cssid = cud->schid.cssid;
586 sscud_area->last_sch = cud->schid.sch_no;
588 ccode = chsc(sscud_area);
593 if (sscud_area->response.code != 0x0001) {
595 CHSC_MSG(0, "sscud: response code=%x\n",
596 sscud_area->response.code);
599 memcpy(&cud->scub, &sscud_area->response, sscud_area->response.length);
600 if (copy_to_user(user_cud, cud, sizeof(*cud)))
606 free_page((unsigned long)sscud_area);
610 static int chsc_ioctl_conf_info(void __user *user_ci)
612 struct chsc_conf_info *ci;
615 struct chsc_header request;
625 struct chsc_header response;
626 u8 data[PAGE_SIZE - 20];
627 } __attribute__ ((packed)) *sci_area;
629 sci_area = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
632 ci = kzalloc(sizeof(*ci), GFP_KERNEL);
637 if (copy_from_user(ci, user_ci, sizeof(*ci))) {
641 sci_area->request.length = 0x0010;
642 sci_area->request.code = 0x0012;
643 sci_area->m = ci->id.m;
644 sci_area->fmt1 = ci->fmt;
645 sci_area->cssid = ci->id.cssid;
646 sci_area->ssid = ci->id.ssid;
648 ccode = chsc(sci_area);
653 if (sci_area->response.code != 0x0001) {
655 CHSC_MSG(0, "sci: response code=%x\n",
656 sci_area->response.code);
659 memcpy(&ci->scid, &sci_area->response, sci_area->response.length);
660 if (copy_to_user(user_ci, ci, sizeof(*ci)))
666 free_page((unsigned long)sci_area);
670 static int chsc_ioctl_conf_comp_list(void __user *user_ccl)
672 struct chsc_comp_list *ccl;
675 struct chsc_header request;
683 struct chsc_header response;
684 u8 data[PAGE_SIZE - 36];
685 } __attribute__ ((packed)) *sccl_area;
692 } __attribute__ ((packed)) *chpid_parm;
698 } __attribute__ ((packed)) *cssids_parm;
700 sccl_area = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
703 ccl = kzalloc(sizeof(*ccl), GFP_KERNEL);
708 if (copy_from_user(ccl, user_ccl, sizeof(*ccl))) {
712 sccl_area->request.length = 0x0020;
713 sccl_area->request.code = 0x0030;
714 sccl_area->fmt = ccl->req.fmt;
715 sccl_area->ctype = ccl->req.ctype;
716 switch (sccl_area->ctype) {
719 chpid_parm = (void *)&sccl_area->list_parm;
720 chpid_parm->m = ccl->req.chpid.m;
721 chpid_parm->cssid = ccl->req.chpid.chp.cssid;
722 chpid_parm->chpid = ccl->req.chpid.chp.id;
725 case CCL_CSS_IMG_CONF_CHAR:
726 cssids_parm = (void *)&sccl_area->list_parm;
727 cssids_parm->f_cssid = ccl->req.cssids.f_cssid;
728 cssids_parm->l_cssid = ccl->req.cssids.l_cssid;
731 ccode = chsc(sccl_area);
736 if (sccl_area->response.code != 0x0001) {
738 CHSC_MSG(0, "sccl: response code=%x\n",
739 sccl_area->response.code);
742 memcpy(&ccl->sccl, &sccl_area->response, sccl_area->response.length);
743 if (copy_to_user(user_ccl, ccl, sizeof(*ccl)))
749 free_page((unsigned long)sccl_area);
753 static int chsc_ioctl_chpd(void __user *user_chpd)
755 struct chsc_scpd *scpd_area;
756 struct chsc_cpd_info *chpd;
759 chpd = kzalloc(sizeof(*chpd), GFP_KERNEL);
760 scpd_area = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
761 if (!scpd_area || !chpd) {
765 if (copy_from_user(chpd, user_chpd, sizeof(*chpd))) {
769 ret = chsc_determine_channel_path_desc(chpd->chpid, chpd->fmt,
770 chpd->rfmt, chpd->c, chpd->m,
774 memcpy(&chpd->chpdb, &scpd_area->response, scpd_area->response.length);
775 if (copy_to_user(user_chpd, chpd, sizeof(*chpd)))
779 free_page((unsigned long)scpd_area);
783 static int chsc_ioctl_dcal(void __user *user_dcal)
785 struct chsc_dcal *dcal;
788 struct chsc_header request;
796 struct chsc_header response;
797 u8 data[PAGE_SIZE - 36];
798 } __attribute__ ((packed)) *sdcal_area;
800 sdcal_area = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
803 dcal = kzalloc(sizeof(*dcal), GFP_KERNEL);
808 if (copy_from_user(dcal, user_dcal, sizeof(*dcal))) {
812 sdcal_area->request.length = 0x0020;
813 sdcal_area->request.code = 0x0034;
814 sdcal_area->atype = dcal->req.atype;
815 sdcal_area->fmt = dcal->req.fmt;
816 memcpy(&sdcal_area->list_parm, &dcal->req.list_parm,
817 sizeof(sdcal_area->list_parm));
819 ccode = chsc(sdcal_area);
824 if (sdcal_area->response.code != 0x0001) {
826 CHSC_MSG(0, "sdcal: response code=%x\n",
827 sdcal_area->response.code);
830 memcpy(&dcal->sdcal, &sdcal_area->response,
831 sdcal_area->response.length);
832 if (copy_to_user(user_dcal, dcal, sizeof(*dcal)))
838 free_page((unsigned long)sdcal_area);
842 static long chsc_ioctl(struct file *filp, unsigned int cmd,
847 CHSC_MSG(2, "chsc_ioctl called, cmd=%x\n", cmd);
848 if (is_compat_task())
849 argp = compat_ptr(arg);
851 argp = (void __user *)arg;
854 return chsc_ioctl_start(argp);
855 case CHSC_START_SYNC:
856 return chsc_ioctl_start_sync(argp);
857 case CHSC_INFO_CHANNEL_PATH:
858 return chsc_ioctl_info_channel_path(argp);
860 return chsc_ioctl_info_cu(argp);
861 case CHSC_INFO_SCH_CU:
862 return chsc_ioctl_info_sch_cu(argp);
864 return chsc_ioctl_conf_info(argp);
866 return chsc_ioctl_conf_comp_list(argp);
868 return chsc_ioctl_chpd(argp);
870 return chsc_ioctl_dcal(argp);
871 case CHSC_ON_CLOSE_SET:
872 return chsc_ioctl_on_close_set(argp);
873 case CHSC_ON_CLOSE_REMOVE:
874 return chsc_ioctl_on_close_remove();
875 default: /* unknown ioctl number */
880 static atomic_t chsc_ready_for_use = ATOMIC_INIT(1);
882 static int chsc_open(struct inode *inode, struct file *file)
884 if (!atomic_dec_and_test(&chsc_ready_for_use)) {
885 atomic_inc(&chsc_ready_for_use);
888 return nonseekable_open(inode, file);
891 static int chsc_release(struct inode *inode, struct file *filp)
896 mutex_lock(&on_close_mutex);
897 if (!on_close_chsc_area)
899 init_completion(&on_close_request->completion);
900 CHSC_LOG(0, "on_close");
901 chsc_log_command(on_close_chsc_area);
902 spin_lock_irq(&chsc_lock);
903 ret = chsc_async(on_close_chsc_area, on_close_request);
904 spin_unlock_irq(&chsc_lock);
905 if (ret == -EINPROGRESS) {
906 wait_for_completion(&on_close_request->completion);
907 ret = chsc_examine_irb(on_close_request);
909 snprintf(dbf, sizeof(dbf), "relret:%d", ret);
911 free_page((unsigned long)on_close_chsc_area);
912 on_close_chsc_area = NULL;
913 kfree(on_close_request);
914 on_close_request = NULL;
916 mutex_unlock(&on_close_mutex);
917 atomic_inc(&chsc_ready_for_use);
921 static const struct file_operations chsc_fops = {
922 .owner = THIS_MODULE,
924 .release = chsc_release,
925 .unlocked_ioctl = chsc_ioctl,
926 .compat_ioctl = chsc_ioctl,
930 static struct miscdevice chsc_misc_device = {
931 .minor = MISC_DYNAMIC_MINOR,
936 static int __init chsc_misc_init(void)
938 return misc_register(&chsc_misc_device);
941 static void chsc_misc_cleanup(void)
943 misc_deregister(&chsc_misc_device);
946 static int __init chsc_sch_init(void)
950 ret = chsc_init_dbfs();
953 isc_register(CHSC_SCH_ISC);
954 ret = chsc_init_sch_driver();
957 ret = chsc_misc_init();
962 chsc_cleanup_sch_driver();
964 isc_unregister(CHSC_SCH_ISC);
969 static void __exit chsc_sch_exit(void)
972 chsc_cleanup_sch_driver();
973 isc_unregister(CHSC_SCH_ISC);
977 module_init(chsc_sch_init);
978 module_exit(chsc_sch_exit);