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 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(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", 8, 1, 4 * sizeof(long));
173 if (!chsc_debug_msg_id)
175 debug_register_view(chsc_debug_msg_id, &debug_sprintf_view);
176 debug_set_level(chsc_debug_msg_id, 2);
177 chsc_debug_log_id = debug_register("chsc_log", 16, 1, 16);
178 if (!chsc_debug_log_id)
180 debug_register_view(chsc_debug_log_id, &debug_hex_ascii_view);
181 debug_set_level(chsc_debug_log_id, 2);
184 debug_unregister(chsc_debug_msg_id);
188 static void chsc_remove_dbfs(void)
190 debug_unregister(chsc_debug_log_id);
191 debug_unregister(chsc_debug_msg_id);
194 static int __init chsc_init_sch_driver(void)
196 return css_driver_register(&chsc_subchannel_driver);
199 static void chsc_cleanup_sch_driver(void)
201 css_driver_unregister(&chsc_subchannel_driver);
204 static DEFINE_SPINLOCK(chsc_lock);
206 static int chsc_subchannel_match_next_free(struct device *dev, const void *data)
208 struct subchannel *sch = to_subchannel(dev);
210 return sch->schib.pmcw.ena && !scsw_fctl(&sch->schib.scsw);
213 static struct subchannel *chsc_get_next_subchannel(struct subchannel *sch)
217 dev = driver_find_device(&chsc_subchannel_driver.drv,
218 sch ? &sch->dev : NULL, NULL,
219 chsc_subchannel_match_next_free);
220 return dev ? to_subchannel(dev) : NULL;
224 * chsc_async() - try to start a chsc request asynchronously
225 * @chsc_area: request to be started
226 * @request: request structure to associate
228 * Tries to start a chsc request on one of the existing chsc subchannels.
230 * %0 if the request was performed synchronously
231 * %-EINPROGRESS if the request was successfully started
232 * %-EBUSY if all chsc subchannels are busy
233 * %-ENODEV if no chsc subchannels are available
235 * interrupts disabled, chsc_lock held
237 static int chsc_async(struct chsc_async_area *chsc_area,
238 struct chsc_request *request)
241 struct chsc_private *private;
242 struct subchannel *sch = NULL;
246 chsc_area->header.key = PAGE_DEFAULT_KEY >> 4;
247 while ((sch = chsc_get_next_subchannel(sch))) {
248 spin_lock(sch->lock);
249 private = dev_get_drvdata(&sch->dev);
250 if (private->request) {
251 spin_unlock(sch->lock);
255 chsc_area->header.sid = sch->schid;
256 CHSC_LOG(2, "schid");
257 CHSC_LOG_HEX(2, &sch->schid, sizeof(sch->schid));
258 cc = chsc(chsc_area);
259 snprintf(dbf, sizeof(dbf), "cc:%d", cc);
266 sch->schib.scsw.cmd.fctl |= SCSW_FCTL_START_FUNC;
268 private->request = request;
276 spin_unlock(sch->lock);
277 CHSC_MSG(2, "chsc on 0.%x.%04x returned cc=%d\n",
278 sch->schid.ssid, sch->schid.sch_no, cc);
279 if (ret == -EINPROGRESS)
281 put_device(&sch->dev);
288 static void chsc_log_command(void *chsc_area)
292 snprintf(dbf, sizeof(dbf), "CHSC:%x", ((uint16_t *)chsc_area)[1]);
294 CHSC_LOG_HEX(0, chsc_area, 32);
297 static int chsc_examine_irb(struct chsc_request *request)
301 if (!(scsw_stctl(&request->irb.scsw) & SCSW_STCTL_STATUS_PEND))
303 backed_up = scsw_cstat(&request->irb.scsw) & SCHN_STAT_CHAIN_CHECK;
304 request->irb.scsw.cmd.cstat &= ~SCHN_STAT_CHAIN_CHECK;
305 if (scsw_cstat(&request->irb.scsw) == 0)
309 if (scsw_cstat(&request->irb.scsw) & SCHN_STAT_PROG_CHECK)
311 if (scsw_cstat(&request->irb.scsw) & SCHN_STAT_PROT_CHECK)
313 if (scsw_cstat(&request->irb.scsw) & SCHN_STAT_CHN_DATA_CHK)
315 if (scsw_cstat(&request->irb.scsw) & SCHN_STAT_CHN_CTRL_CHK)
320 static int chsc_ioctl_start(void __user *user_area)
322 struct chsc_request *request;
323 struct chsc_async_area *chsc_area;
327 if (!css_general_characteristics.dynio)
328 /* It makes no sense to try. */
330 chsc_area = (void *)get_zeroed_page(GFP_DMA | GFP_KERNEL);
333 request = kzalloc(sizeof(*request), GFP_KERNEL);
338 init_completion(&request->completion);
339 if (copy_from_user(chsc_area, user_area, PAGE_SIZE)) {
343 chsc_log_command(chsc_area);
344 spin_lock_irq(&chsc_lock);
345 ret = chsc_async(chsc_area, request);
346 spin_unlock_irq(&chsc_lock);
347 if (ret == -EINPROGRESS) {
348 wait_for_completion(&request->completion);
349 ret = chsc_examine_irb(request);
351 /* copy area back to user */
353 if (copy_to_user(user_area, chsc_area, PAGE_SIZE))
356 snprintf(dbf, sizeof(dbf), "ret:%d", ret);
359 free_page((unsigned long)chsc_area);
363 static int chsc_ioctl_on_close_set(void __user *user_area)
368 mutex_lock(&on_close_mutex);
369 if (on_close_chsc_area) {
373 on_close_request = kzalloc(sizeof(*on_close_request), GFP_KERNEL);
374 if (!on_close_request) {
378 on_close_chsc_area = (void *)get_zeroed_page(GFP_DMA | GFP_KERNEL);
379 if (!on_close_chsc_area) {
381 goto out_free_request;
383 if (copy_from_user(on_close_chsc_area, user_area, PAGE_SIZE)) {
391 free_page((unsigned long)on_close_chsc_area);
392 on_close_chsc_area = NULL;
394 kfree(on_close_request);
395 on_close_request = NULL;
397 mutex_unlock(&on_close_mutex);
398 snprintf(dbf, sizeof(dbf), "ocsret:%d", ret);
403 static int chsc_ioctl_on_close_remove(void)
408 mutex_lock(&on_close_mutex);
409 if (!on_close_chsc_area) {
413 free_page((unsigned long)on_close_chsc_area);
414 on_close_chsc_area = NULL;
415 kfree(on_close_request);
416 on_close_request = NULL;
419 mutex_unlock(&on_close_mutex);
420 snprintf(dbf, sizeof(dbf), "ocrret:%d", ret);
425 static int chsc_ioctl_start_sync(void __user *user_area)
427 struct chsc_sync_area *chsc_area;
430 chsc_area = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
433 if (copy_from_user(chsc_area, user_area, PAGE_SIZE)) {
437 if (chsc_area->header.code & 0x4000) {
441 chsc_log_command(chsc_area);
442 ccode = chsc(chsc_area);
447 if (copy_to_user(user_area, chsc_area, PAGE_SIZE))
452 free_page((unsigned long)chsc_area);
456 static int chsc_ioctl_info_channel_path(void __user *user_cd)
458 struct chsc_chp_cd *cd;
461 struct chsc_header request;
472 struct chsc_header response;
473 u8 data[PAGE_SIZE - 20];
474 } __attribute__ ((packed)) *scpcd_area;
476 scpcd_area = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
479 cd = kzalloc(sizeof(*cd), GFP_KERNEL);
484 if (copy_from_user(cd, user_cd, sizeof(*cd))) {
488 scpcd_area->request.length = 0x0010;
489 scpcd_area->request.code = 0x0028;
490 scpcd_area->m = cd->m;
491 scpcd_area->fmt1 = cd->fmt;
492 scpcd_area->cssid = cd->chpid.cssid;
493 scpcd_area->first_chpid = cd->chpid.id;
494 scpcd_area->last_chpid = cd->chpid.id;
496 ccode = chsc(scpcd_area);
501 if (scpcd_area->response.code != 0x0001) {
503 CHSC_MSG(0, "scpcd: response code=%x\n",
504 scpcd_area->response.code);
507 memcpy(&cd->cpcb, &scpcd_area->response, scpcd_area->response.length);
508 if (copy_to_user(user_cd, cd, sizeof(*cd)))
514 free_page((unsigned long)scpcd_area);
518 static int chsc_ioctl_info_cu(void __user *user_cd)
520 struct chsc_cu_cd *cd;
523 struct chsc_header request;
534 struct chsc_header response;
535 u8 data[PAGE_SIZE - 20];
536 } __attribute__ ((packed)) *scucd_area;
538 scucd_area = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
541 cd = kzalloc(sizeof(*cd), GFP_KERNEL);
546 if (copy_from_user(cd, user_cd, sizeof(*cd))) {
550 scucd_area->request.length = 0x0010;
551 scucd_area->request.code = 0x0026;
552 scucd_area->m = cd->m;
553 scucd_area->fmt1 = cd->fmt;
554 scucd_area->cssid = cd->cssid;
555 scucd_area->first_cun = cd->cun;
556 scucd_area->last_cun = cd->cun;
558 ccode = chsc(scucd_area);
563 if (scucd_area->response.code != 0x0001) {
565 CHSC_MSG(0, "scucd: response code=%x\n",
566 scucd_area->response.code);
569 memcpy(&cd->cucb, &scucd_area->response, scucd_area->response.length);
570 if (copy_to_user(user_cd, cd, sizeof(*cd)))
576 free_page((unsigned long)scucd_area);
580 static int chsc_ioctl_info_sch_cu(void __user *user_cud)
582 struct chsc_sch_cud *cud;
585 struct chsc_header request;
597 struct chsc_header response;
598 u8 data[PAGE_SIZE - 20];
599 } __attribute__ ((packed)) *sscud_area;
601 sscud_area = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
604 cud = kzalloc(sizeof(*cud), GFP_KERNEL);
609 if (copy_from_user(cud, user_cud, sizeof(*cud))) {
613 sscud_area->request.length = 0x0010;
614 sscud_area->request.code = 0x0006;
615 sscud_area->m = cud->schid.m;
616 sscud_area->fmt1 = cud->fmt;
617 sscud_area->ssid = cud->schid.ssid;
618 sscud_area->first_sch = cud->schid.sch_no;
619 sscud_area->cssid = cud->schid.cssid;
620 sscud_area->last_sch = cud->schid.sch_no;
622 ccode = chsc(sscud_area);
627 if (sscud_area->response.code != 0x0001) {
629 CHSC_MSG(0, "sscud: response code=%x\n",
630 sscud_area->response.code);
633 memcpy(&cud->scub, &sscud_area->response, sscud_area->response.length);
634 if (copy_to_user(user_cud, cud, sizeof(*cud)))
640 free_page((unsigned long)sscud_area);
644 static int chsc_ioctl_conf_info(void __user *user_ci)
646 struct chsc_conf_info *ci;
649 struct chsc_header request;
659 struct chsc_header response;
660 u8 data[PAGE_SIZE - 20];
661 } __attribute__ ((packed)) *sci_area;
663 sci_area = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
666 ci = kzalloc(sizeof(*ci), GFP_KERNEL);
671 if (copy_from_user(ci, user_ci, sizeof(*ci))) {
675 sci_area->request.length = 0x0010;
676 sci_area->request.code = 0x0012;
677 sci_area->m = ci->id.m;
678 sci_area->fmt1 = ci->fmt;
679 sci_area->cssid = ci->id.cssid;
680 sci_area->ssid = ci->id.ssid;
682 ccode = chsc(sci_area);
687 if (sci_area->response.code != 0x0001) {
689 CHSC_MSG(0, "sci: response code=%x\n",
690 sci_area->response.code);
693 memcpy(&ci->scid, &sci_area->response, sci_area->response.length);
694 if (copy_to_user(user_ci, ci, sizeof(*ci)))
700 free_page((unsigned long)sci_area);
704 static int chsc_ioctl_conf_comp_list(void __user *user_ccl)
706 struct chsc_comp_list *ccl;
709 struct chsc_header request;
717 struct chsc_header response;
718 u8 data[PAGE_SIZE - 36];
719 } __attribute__ ((packed)) *sccl_area;
726 } __attribute__ ((packed)) *chpid_parm;
732 } __attribute__ ((packed)) *cssids_parm;
734 sccl_area = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
737 ccl = kzalloc(sizeof(*ccl), GFP_KERNEL);
742 if (copy_from_user(ccl, user_ccl, sizeof(*ccl))) {
746 sccl_area->request.length = 0x0020;
747 sccl_area->request.code = 0x0030;
748 sccl_area->fmt = ccl->req.fmt;
749 sccl_area->ctype = ccl->req.ctype;
750 switch (sccl_area->ctype) {
753 chpid_parm = (void *)&sccl_area->list_parm;
754 chpid_parm->m = ccl->req.chpid.m;
755 chpid_parm->cssid = ccl->req.chpid.chp.cssid;
756 chpid_parm->chpid = ccl->req.chpid.chp.id;
759 case CCL_CSS_IMG_CONF_CHAR:
760 cssids_parm = (void *)&sccl_area->list_parm;
761 cssids_parm->f_cssid = ccl->req.cssids.f_cssid;
762 cssids_parm->l_cssid = ccl->req.cssids.l_cssid;
765 ccode = chsc(sccl_area);
770 if (sccl_area->response.code != 0x0001) {
772 CHSC_MSG(0, "sccl: response code=%x\n",
773 sccl_area->response.code);
776 memcpy(&ccl->sccl, &sccl_area->response, sccl_area->response.length);
777 if (copy_to_user(user_ccl, ccl, sizeof(*ccl)))
783 free_page((unsigned long)sccl_area);
787 static int chsc_ioctl_chpd(void __user *user_chpd)
789 struct chsc_scpd *scpd_area;
790 struct chsc_cpd_info *chpd;
793 chpd = kzalloc(sizeof(*chpd), GFP_KERNEL);
794 scpd_area = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
795 if (!scpd_area || !chpd) {
799 if (copy_from_user(chpd, user_chpd, sizeof(*chpd))) {
803 ret = chsc_determine_channel_path_desc(chpd->chpid, chpd->fmt,
804 chpd->rfmt, chpd->c, chpd->m,
808 memcpy(&chpd->chpdb, &scpd_area->response, scpd_area->response.length);
809 if (copy_to_user(user_chpd, chpd, sizeof(*chpd)))
813 free_page((unsigned long)scpd_area);
817 static int chsc_ioctl_dcal(void __user *user_dcal)
819 struct chsc_dcal *dcal;
822 struct chsc_header request;
830 struct chsc_header response;
831 u8 data[PAGE_SIZE - 36];
832 } __attribute__ ((packed)) *sdcal_area;
834 sdcal_area = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
837 dcal = kzalloc(sizeof(*dcal), GFP_KERNEL);
842 if (copy_from_user(dcal, user_dcal, sizeof(*dcal))) {
846 sdcal_area->request.length = 0x0020;
847 sdcal_area->request.code = 0x0034;
848 sdcal_area->atype = dcal->req.atype;
849 sdcal_area->fmt = dcal->req.fmt;
850 memcpy(&sdcal_area->list_parm, &dcal->req.list_parm,
851 sizeof(sdcal_area->list_parm));
853 ccode = chsc(sdcal_area);
858 if (sdcal_area->response.code != 0x0001) {
860 CHSC_MSG(0, "sdcal: response code=%x\n",
861 sdcal_area->response.code);
864 memcpy(&dcal->sdcal, &sdcal_area->response,
865 sdcal_area->response.length);
866 if (copy_to_user(user_dcal, dcal, sizeof(*dcal)))
872 free_page((unsigned long)sdcal_area);
876 static long chsc_ioctl(struct file *filp, unsigned int cmd,
881 CHSC_MSG(2, "chsc_ioctl called, cmd=%x\n", cmd);
882 if (is_compat_task())
883 argp = compat_ptr(arg);
885 argp = (void __user *)arg;
888 return chsc_ioctl_start(argp);
889 case CHSC_START_SYNC:
890 return chsc_ioctl_start_sync(argp);
891 case CHSC_INFO_CHANNEL_PATH:
892 return chsc_ioctl_info_channel_path(argp);
894 return chsc_ioctl_info_cu(argp);
895 case CHSC_INFO_SCH_CU:
896 return chsc_ioctl_info_sch_cu(argp);
898 return chsc_ioctl_conf_info(argp);
900 return chsc_ioctl_conf_comp_list(argp);
902 return chsc_ioctl_chpd(argp);
904 return chsc_ioctl_dcal(argp);
905 case CHSC_ON_CLOSE_SET:
906 return chsc_ioctl_on_close_set(argp);
907 case CHSC_ON_CLOSE_REMOVE:
908 return chsc_ioctl_on_close_remove();
909 default: /* unknown ioctl number */
914 static atomic_t chsc_ready_for_use = ATOMIC_INIT(1);
916 static int chsc_open(struct inode *inode, struct file *file)
918 if (!atomic_dec_and_test(&chsc_ready_for_use)) {
919 atomic_inc(&chsc_ready_for_use);
922 return nonseekable_open(inode, file);
925 static int chsc_release(struct inode *inode, struct file *filp)
930 mutex_lock(&on_close_mutex);
931 if (!on_close_chsc_area)
933 init_completion(&on_close_request->completion);
934 CHSC_LOG(0, "on_close");
935 chsc_log_command(on_close_chsc_area);
936 spin_lock_irq(&chsc_lock);
937 ret = chsc_async(on_close_chsc_area, on_close_request);
938 spin_unlock_irq(&chsc_lock);
939 if (ret == -EINPROGRESS) {
940 wait_for_completion(&on_close_request->completion);
941 ret = chsc_examine_irb(on_close_request);
943 snprintf(dbf, sizeof(dbf), "relret:%d", ret);
945 free_page((unsigned long)on_close_chsc_area);
946 on_close_chsc_area = NULL;
947 kfree(on_close_request);
948 on_close_request = NULL;
950 mutex_unlock(&on_close_mutex);
951 atomic_inc(&chsc_ready_for_use);
955 static const struct file_operations chsc_fops = {
956 .owner = THIS_MODULE,
958 .release = chsc_release,
959 .unlocked_ioctl = chsc_ioctl,
960 .compat_ioctl = chsc_ioctl,
964 static struct miscdevice chsc_misc_device = {
965 .minor = MISC_DYNAMIC_MINOR,
970 static int __init chsc_misc_init(void)
972 return misc_register(&chsc_misc_device);
975 static void chsc_misc_cleanup(void)
977 misc_deregister(&chsc_misc_device);
980 static int __init chsc_sch_init(void)
984 ret = chsc_init_dbfs();
987 isc_register(CHSC_SCH_ISC);
988 ret = chsc_init_sch_driver();
991 ret = chsc_misc_init();
996 chsc_cleanup_sch_driver();
998 isc_unregister(CHSC_SCH_ISC);
1003 static void __exit chsc_sch_exit(void)
1005 chsc_misc_cleanup();
1006 chsc_cleanup_sch_driver();
1007 isc_unregister(CHSC_SCH_ISC);
1011 module_init(chsc_sch_init);
1012 module_exit(chsc_sch_exit);