1 // SPDX-License-Identifier: GPL-2.0
3 * Driver for s390 chsc subchannels
5 * Copyright IBM Corp. 2008, 2011
7 * Author(s): Cornelia Huck <cornelia.huck@de.ibm.com>
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>
19 #include <asm/compat.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)(unsigned long)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);
96 if (dev_get_uevent_suppress(&sch->dev)) {
97 dev_set_uevent_suppress(&sch->dev, 0);
98 kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
104 static int chsc_subchannel_remove(struct subchannel *sch)
106 struct chsc_private *private;
108 cio_disable_subchannel(sch);
109 private = dev_get_drvdata(&sch->dev);
110 dev_set_drvdata(&sch->dev, NULL);
111 if (private->request) {
112 complete(&private->request->completion);
113 put_device(&sch->dev);
119 static void chsc_subchannel_shutdown(struct subchannel *sch)
121 cio_disable_subchannel(sch);
124 static int chsc_subchannel_prepare(struct subchannel *sch)
129 * Don't allow suspend while the subchannel is not idle
130 * since we don't have a way to clear the subchannel and
131 * cannot disable it with a request running.
133 cc = stsch(sch->schid, &schib);
134 if (!cc && scsw_stctl(&schib.scsw))
139 static int chsc_subchannel_freeze(struct subchannel *sch)
141 return cio_disable_subchannel(sch);
144 static int chsc_subchannel_restore(struct subchannel *sch)
146 return cio_enable_subchannel(sch, (u32)(unsigned long)sch);
149 static struct css_device_id chsc_subchannel_ids[] = {
150 { .match_flags = 0x1, .type =SUBCHANNEL_TYPE_CHSC, },
151 { /* end of list */ },
153 MODULE_DEVICE_TABLE(css, chsc_subchannel_ids);
155 static struct css_driver chsc_subchannel_driver = {
157 .owner = THIS_MODULE,
158 .name = "chsc_subchannel",
160 .subchannel_type = chsc_subchannel_ids,
161 .irq = chsc_subchannel_irq,
162 .probe = chsc_subchannel_probe,
163 .remove = chsc_subchannel_remove,
164 .shutdown = chsc_subchannel_shutdown,
165 .prepare = chsc_subchannel_prepare,
166 .freeze = chsc_subchannel_freeze,
167 .thaw = chsc_subchannel_restore,
168 .restore = chsc_subchannel_restore,
171 static int __init chsc_init_dbfs(void)
173 chsc_debug_msg_id = debug_register("chsc_msg", 8, 1, 4 * sizeof(long));
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 debug_unregister(chsc_debug_msg_id);
189 static void chsc_remove_dbfs(void)
191 debug_unregister(chsc_debug_log_id);
192 debug_unregister(chsc_debug_msg_id);
195 static int __init chsc_init_sch_driver(void)
197 return css_driver_register(&chsc_subchannel_driver);
200 static void chsc_cleanup_sch_driver(void)
202 css_driver_unregister(&chsc_subchannel_driver);
205 static DEFINE_SPINLOCK(chsc_lock);
207 static int chsc_subchannel_match_next_free(struct device *dev, void *data)
209 struct subchannel *sch = to_subchannel(dev);
211 return sch->schib.pmcw.ena && !scsw_fctl(&sch->schib.scsw);
214 static struct subchannel *chsc_get_next_subchannel(struct subchannel *sch)
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;
225 * chsc_async() - try to start a chsc request asynchronously
226 * @chsc_area: request to be started
227 * @request: request structure to associate
229 * Tries to start a chsc request on one of the existing chsc subchannels.
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
236 * interrupts disabled, chsc_lock held
238 static int chsc_async(struct chsc_async_area *chsc_area,
239 struct chsc_request *request)
242 struct chsc_private *private;
243 struct subchannel *sch = NULL;
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);
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 snprintf(dbf, sizeof(dbf), "cc:%d", cc);
267 sch->schib.scsw.cmd.fctl |= SCSW_FCTL_START_FUNC;
269 private->request = request;
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)
282 put_device(&sch->dev);
289 static void chsc_log_command(void *chsc_area)
293 snprintf(dbf, sizeof(dbf), "CHSC:%x", ((uint16_t *)chsc_area)[1]);
295 CHSC_LOG_HEX(0, chsc_area, 32);
298 static int chsc_examine_irb(struct chsc_request *request)
302 if (!(scsw_stctl(&request->irb.scsw) & SCSW_STCTL_STATUS_PEND))
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)
310 if (scsw_cstat(&request->irb.scsw) & SCHN_STAT_PROG_CHECK)
312 if (scsw_cstat(&request->irb.scsw) & SCHN_STAT_PROT_CHECK)
314 if (scsw_cstat(&request->irb.scsw) & SCHN_STAT_CHN_DATA_CHK)
316 if (scsw_cstat(&request->irb.scsw) & SCHN_STAT_CHN_CTRL_CHK)
321 static int chsc_ioctl_start(void __user *user_area)
323 struct chsc_request *request;
324 struct chsc_async_area *chsc_area;
328 if (!css_general_characteristics.dynio)
329 /* It makes no sense to try. */
331 chsc_area = (void *)get_zeroed_page(GFP_DMA | GFP_KERNEL);
334 request = kzalloc(sizeof(*request), GFP_KERNEL);
339 init_completion(&request->completion);
340 if (copy_from_user(chsc_area, user_area, PAGE_SIZE)) {
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);
352 /* copy area back to user */
354 if (copy_to_user(user_area, chsc_area, PAGE_SIZE))
357 snprintf(dbf, sizeof(dbf), "ret:%d", ret);
360 free_page((unsigned long)chsc_area);
364 static int chsc_ioctl_on_close_set(void __user *user_area)
369 mutex_lock(&on_close_mutex);
370 if (on_close_chsc_area) {
374 on_close_request = kzalloc(sizeof(*on_close_request), GFP_KERNEL);
375 if (!on_close_request) {
379 on_close_chsc_area = (void *)get_zeroed_page(GFP_DMA | GFP_KERNEL);
380 if (!on_close_chsc_area) {
382 goto out_free_request;
384 if (copy_from_user(on_close_chsc_area, user_area, PAGE_SIZE)) {
392 free_page((unsigned long)on_close_chsc_area);
393 on_close_chsc_area = NULL;
395 kfree(on_close_request);
396 on_close_request = NULL;
398 mutex_unlock(&on_close_mutex);
399 snprintf(dbf, sizeof(dbf), "ocsret:%d", ret);
404 static int chsc_ioctl_on_close_remove(void)
409 mutex_lock(&on_close_mutex);
410 if (!on_close_chsc_area) {
414 free_page((unsigned long)on_close_chsc_area);
415 on_close_chsc_area = NULL;
416 kfree(on_close_request);
417 on_close_request = NULL;
420 mutex_unlock(&on_close_mutex);
421 snprintf(dbf, sizeof(dbf), "ocrret:%d", ret);
426 static int chsc_ioctl_start_sync(void __user *user_area)
428 struct chsc_sync_area *chsc_area;
431 chsc_area = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
434 if (copy_from_user(chsc_area, user_area, PAGE_SIZE)) {
438 if (chsc_area->header.code & 0x4000) {
442 chsc_log_command(chsc_area);
443 ccode = chsc(chsc_area);
448 if (copy_to_user(user_area, chsc_area, PAGE_SIZE))
453 free_page((unsigned long)chsc_area);
457 static int chsc_ioctl_info_channel_path(void __user *user_cd)
459 struct chsc_chp_cd *cd;
462 struct chsc_header request;
473 struct chsc_header response;
474 u8 data[PAGE_SIZE - 20];
475 } __attribute__ ((packed)) *scpcd_area;
477 scpcd_area = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
480 cd = kzalloc(sizeof(*cd), GFP_KERNEL);
485 if (copy_from_user(cd, user_cd, sizeof(*cd))) {
489 scpcd_area->request.length = 0x0010;
490 scpcd_area->request.code = 0x0028;
491 scpcd_area->m = cd->m;
492 scpcd_area->fmt1 = cd->fmt;
493 scpcd_area->cssid = cd->chpid.cssid;
494 scpcd_area->first_chpid = cd->chpid.id;
495 scpcd_area->last_chpid = cd->chpid.id;
497 ccode = chsc(scpcd_area);
502 if (scpcd_area->response.code != 0x0001) {
504 CHSC_MSG(0, "scpcd: response code=%x\n",
505 scpcd_area->response.code);
508 memcpy(&cd->cpcb, &scpcd_area->response, scpcd_area->response.length);
509 if (copy_to_user(user_cd, cd, sizeof(*cd)))
515 free_page((unsigned long)scpcd_area);
519 static int chsc_ioctl_info_cu(void __user *user_cd)
521 struct chsc_cu_cd *cd;
524 struct chsc_header request;
535 struct chsc_header response;
536 u8 data[PAGE_SIZE - 20];
537 } __attribute__ ((packed)) *scucd_area;
539 scucd_area = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
542 cd = kzalloc(sizeof(*cd), GFP_KERNEL);
547 if (copy_from_user(cd, user_cd, sizeof(*cd))) {
551 scucd_area->request.length = 0x0010;
552 scucd_area->request.code = 0x0026;
553 scucd_area->m = cd->m;
554 scucd_area->fmt1 = cd->fmt;
555 scucd_area->cssid = cd->cssid;
556 scucd_area->first_cun = cd->cun;
557 scucd_area->last_cun = cd->cun;
559 ccode = chsc(scucd_area);
564 if (scucd_area->response.code != 0x0001) {
566 CHSC_MSG(0, "scucd: response code=%x\n",
567 scucd_area->response.code);
570 memcpy(&cd->cucb, &scucd_area->response, scucd_area->response.length);
571 if (copy_to_user(user_cd, cd, sizeof(*cd)))
577 free_page((unsigned long)scucd_area);
581 static int chsc_ioctl_info_sch_cu(void __user *user_cud)
583 struct chsc_sch_cud *cud;
586 struct chsc_header request;
598 struct chsc_header response;
599 u8 data[PAGE_SIZE - 20];
600 } __attribute__ ((packed)) *sscud_area;
602 sscud_area = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
605 cud = kzalloc(sizeof(*cud), GFP_KERNEL);
610 if (copy_from_user(cud, user_cud, sizeof(*cud))) {
614 sscud_area->request.length = 0x0010;
615 sscud_area->request.code = 0x0006;
616 sscud_area->m = cud->schid.m;
617 sscud_area->fmt1 = cud->fmt;
618 sscud_area->ssid = cud->schid.ssid;
619 sscud_area->first_sch = cud->schid.sch_no;
620 sscud_area->cssid = cud->schid.cssid;
621 sscud_area->last_sch = cud->schid.sch_no;
623 ccode = chsc(sscud_area);
628 if (sscud_area->response.code != 0x0001) {
630 CHSC_MSG(0, "sscud: response code=%x\n",
631 sscud_area->response.code);
634 memcpy(&cud->scub, &sscud_area->response, sscud_area->response.length);
635 if (copy_to_user(user_cud, cud, sizeof(*cud)))
641 free_page((unsigned long)sscud_area);
645 static int chsc_ioctl_conf_info(void __user *user_ci)
647 struct chsc_conf_info *ci;
650 struct chsc_header request;
660 struct chsc_header response;
661 u8 data[PAGE_SIZE - 20];
662 } __attribute__ ((packed)) *sci_area;
664 sci_area = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
667 ci = kzalloc(sizeof(*ci), GFP_KERNEL);
672 if (copy_from_user(ci, user_ci, sizeof(*ci))) {
676 sci_area->request.length = 0x0010;
677 sci_area->request.code = 0x0012;
678 sci_area->m = ci->id.m;
679 sci_area->fmt1 = ci->fmt;
680 sci_area->cssid = ci->id.cssid;
681 sci_area->ssid = ci->id.ssid;
683 ccode = chsc(sci_area);
688 if (sci_area->response.code != 0x0001) {
690 CHSC_MSG(0, "sci: response code=%x\n",
691 sci_area->response.code);
694 memcpy(&ci->scid, &sci_area->response, sci_area->response.length);
695 if (copy_to_user(user_ci, ci, sizeof(*ci)))
701 free_page((unsigned long)sci_area);
705 static int chsc_ioctl_conf_comp_list(void __user *user_ccl)
707 struct chsc_comp_list *ccl;
710 struct chsc_header request;
718 struct chsc_header response;
719 u8 data[PAGE_SIZE - 36];
720 } __attribute__ ((packed)) *sccl_area;
727 } __attribute__ ((packed)) *chpid_parm;
733 } __attribute__ ((packed)) *cssids_parm;
735 sccl_area = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
738 ccl = kzalloc(sizeof(*ccl), GFP_KERNEL);
743 if (copy_from_user(ccl, user_ccl, sizeof(*ccl))) {
747 sccl_area->request.length = 0x0020;
748 sccl_area->request.code = 0x0030;
749 sccl_area->fmt = ccl->req.fmt;
750 sccl_area->ctype = ccl->req.ctype;
751 switch (sccl_area->ctype) {
754 chpid_parm = (void *)&sccl_area->list_parm;
755 chpid_parm->m = ccl->req.chpid.m;
756 chpid_parm->cssid = ccl->req.chpid.chp.cssid;
757 chpid_parm->chpid = ccl->req.chpid.chp.id;
760 case CCL_CSS_IMG_CONF_CHAR:
761 cssids_parm = (void *)&sccl_area->list_parm;
762 cssids_parm->f_cssid = ccl->req.cssids.f_cssid;
763 cssids_parm->l_cssid = ccl->req.cssids.l_cssid;
766 ccode = chsc(sccl_area);
771 if (sccl_area->response.code != 0x0001) {
773 CHSC_MSG(0, "sccl: response code=%x\n",
774 sccl_area->response.code);
777 memcpy(&ccl->sccl, &sccl_area->response, sccl_area->response.length);
778 if (copy_to_user(user_ccl, ccl, sizeof(*ccl)))
784 free_page((unsigned long)sccl_area);
788 static int chsc_ioctl_chpd(void __user *user_chpd)
790 struct chsc_scpd *scpd_area;
791 struct chsc_cpd_info *chpd;
794 chpd = kzalloc(sizeof(*chpd), GFP_KERNEL);
795 scpd_area = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
796 if (!scpd_area || !chpd) {
800 if (copy_from_user(chpd, user_chpd, sizeof(*chpd))) {
804 ret = chsc_determine_channel_path_desc(chpd->chpid, chpd->fmt,
805 chpd->rfmt, chpd->c, chpd->m,
809 memcpy(&chpd->chpdb, &scpd_area->response, scpd_area->response.length);
810 if (copy_to_user(user_chpd, chpd, sizeof(*chpd)))
814 free_page((unsigned long)scpd_area);
818 static int chsc_ioctl_dcal(void __user *user_dcal)
820 struct chsc_dcal *dcal;
823 struct chsc_header request;
831 struct chsc_header response;
832 u8 data[PAGE_SIZE - 36];
833 } __attribute__ ((packed)) *sdcal_area;
835 sdcal_area = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
838 dcal = kzalloc(sizeof(*dcal), GFP_KERNEL);
843 if (copy_from_user(dcal, user_dcal, sizeof(*dcal))) {
847 sdcal_area->request.length = 0x0020;
848 sdcal_area->request.code = 0x0034;
849 sdcal_area->atype = dcal->req.atype;
850 sdcal_area->fmt = dcal->req.fmt;
851 memcpy(&sdcal_area->list_parm, &dcal->req.list_parm,
852 sizeof(sdcal_area->list_parm));
854 ccode = chsc(sdcal_area);
859 if (sdcal_area->response.code != 0x0001) {
861 CHSC_MSG(0, "sdcal: response code=%x\n",
862 sdcal_area->response.code);
865 memcpy(&dcal->sdcal, &sdcal_area->response,
866 sdcal_area->response.length);
867 if (copy_to_user(user_dcal, dcal, sizeof(*dcal)))
873 free_page((unsigned long)sdcal_area);
877 static long chsc_ioctl(struct file *filp, unsigned int cmd,
882 CHSC_MSG(2, "chsc_ioctl called, cmd=%x\n", cmd);
883 if (is_compat_task())
884 argp = compat_ptr(arg);
886 argp = (void __user *)arg;
889 return chsc_ioctl_start(argp);
890 case CHSC_START_SYNC:
891 return chsc_ioctl_start_sync(argp);
892 case CHSC_INFO_CHANNEL_PATH:
893 return chsc_ioctl_info_channel_path(argp);
895 return chsc_ioctl_info_cu(argp);
896 case CHSC_INFO_SCH_CU:
897 return chsc_ioctl_info_sch_cu(argp);
899 return chsc_ioctl_conf_info(argp);
901 return chsc_ioctl_conf_comp_list(argp);
903 return chsc_ioctl_chpd(argp);
905 return chsc_ioctl_dcal(argp);
906 case CHSC_ON_CLOSE_SET:
907 return chsc_ioctl_on_close_set(argp);
908 case CHSC_ON_CLOSE_REMOVE:
909 return chsc_ioctl_on_close_remove();
910 default: /* unknown ioctl number */
915 static atomic_t chsc_ready_for_use = ATOMIC_INIT(1);
917 static int chsc_open(struct inode *inode, struct file *file)
919 if (!atomic_dec_and_test(&chsc_ready_for_use)) {
920 atomic_inc(&chsc_ready_for_use);
923 return nonseekable_open(inode, file);
926 static int chsc_release(struct inode *inode, struct file *filp)
931 mutex_lock(&on_close_mutex);
932 if (!on_close_chsc_area)
934 init_completion(&on_close_request->completion);
935 CHSC_LOG(0, "on_close");
936 chsc_log_command(on_close_chsc_area);
937 spin_lock_irq(&chsc_lock);
938 ret = chsc_async(on_close_chsc_area, on_close_request);
939 spin_unlock_irq(&chsc_lock);
940 if (ret == -EINPROGRESS) {
941 wait_for_completion(&on_close_request->completion);
942 ret = chsc_examine_irb(on_close_request);
944 snprintf(dbf, sizeof(dbf), "relret:%d", ret);
946 free_page((unsigned long)on_close_chsc_area);
947 on_close_chsc_area = NULL;
948 kfree(on_close_request);
949 on_close_request = NULL;
951 mutex_unlock(&on_close_mutex);
952 atomic_inc(&chsc_ready_for_use);
956 static const struct file_operations chsc_fops = {
957 .owner = THIS_MODULE,
959 .release = chsc_release,
960 .unlocked_ioctl = chsc_ioctl,
961 .compat_ioctl = chsc_ioctl,
965 static struct miscdevice chsc_misc_device = {
966 .minor = MISC_DYNAMIC_MINOR,
971 static int __init chsc_misc_init(void)
973 return misc_register(&chsc_misc_device);
976 static void chsc_misc_cleanup(void)
978 misc_deregister(&chsc_misc_device);
981 static int __init chsc_sch_init(void)
985 ret = chsc_init_dbfs();
988 isc_register(CHSC_SCH_ISC);
989 ret = chsc_init_sch_driver();
992 ret = chsc_misc_init();
997 chsc_cleanup_sch_driver();
999 isc_unregister(CHSC_SCH_ISC);
1004 static void __exit chsc_sch_exit(void)
1006 chsc_misc_cleanup();
1007 chsc_cleanup_sch_driver();
1008 isc_unregister(CHSC_SCH_ISC);
1012 module_init(chsc_sch_init);
1013 module_exit(chsc_sch_exit);