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(void *chsc_area)
294 sprintf(dbf, "CHSC:%x", ((uint16_t *)chsc_area)[1]);
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_start_sync(void __user *user_area)
367 struct chsc_sync_area *chsc_area;
370 chsc_area = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
373 if (copy_from_user(chsc_area, user_area, PAGE_SIZE)) {
377 if (chsc_area->header.code & 0x4000) {
381 chsc_log_command(chsc_area);
382 ccode = chsc(chsc_area);
387 if (copy_to_user(user_area, chsc_area, PAGE_SIZE))
392 free_page((unsigned long)chsc_area);
396 static int chsc_ioctl_info_channel_path(void __user *user_cd)
398 struct chsc_chp_cd *cd;
401 struct chsc_header request;
412 struct chsc_header response;
413 u8 data[PAGE_SIZE - 20];
414 } __attribute__ ((packed)) *scpcd_area;
416 scpcd_area = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
419 cd = kzalloc(sizeof(*cd), GFP_KERNEL);
424 if (copy_from_user(cd, user_cd, sizeof(*cd))) {
428 scpcd_area->request.length = 0x0010;
429 scpcd_area->request.code = 0x0028;
430 scpcd_area->m = cd->m;
431 scpcd_area->fmt1 = cd->fmt;
432 scpcd_area->cssid = cd->chpid.cssid;
433 scpcd_area->first_chpid = cd->chpid.id;
434 scpcd_area->last_chpid = cd->chpid.id;
436 ccode = chsc(scpcd_area);
441 if (scpcd_area->response.code != 0x0001) {
443 CHSC_MSG(0, "scpcd: response code=%x\n",
444 scpcd_area->response.code);
447 memcpy(&cd->cpcb, &scpcd_area->response, scpcd_area->response.length);
448 if (copy_to_user(user_cd, cd, sizeof(*cd)))
454 free_page((unsigned long)scpcd_area);
458 static int chsc_ioctl_info_cu(void __user *user_cd)
460 struct chsc_cu_cd *cd;
463 struct chsc_header request;
474 struct chsc_header response;
475 u8 data[PAGE_SIZE - 20];
476 } __attribute__ ((packed)) *scucd_area;
478 scucd_area = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
481 cd = kzalloc(sizeof(*cd), GFP_KERNEL);
486 if (copy_from_user(cd, user_cd, sizeof(*cd))) {
490 scucd_area->request.length = 0x0010;
491 scucd_area->request.code = 0x0028;
492 scucd_area->m = cd->m;
493 scucd_area->fmt1 = cd->fmt;
494 scucd_area->cssid = cd->cssid;
495 scucd_area->first_cun = cd->cun;
496 scucd_area->last_cun = cd->cun;
498 ccode = chsc(scucd_area);
503 if (scucd_area->response.code != 0x0001) {
505 CHSC_MSG(0, "scucd: response code=%x\n",
506 scucd_area->response.code);
509 memcpy(&cd->cucb, &scucd_area->response, scucd_area->response.length);
510 if (copy_to_user(user_cd, cd, sizeof(*cd)))
516 free_page((unsigned long)scucd_area);
520 static int chsc_ioctl_info_sch_cu(void __user *user_cud)
522 struct chsc_sch_cud *cud;
525 struct chsc_header request;
537 struct chsc_header response;
538 u8 data[PAGE_SIZE - 20];
539 } __attribute__ ((packed)) *sscud_area;
541 sscud_area = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
544 cud = kzalloc(sizeof(*cud), GFP_KERNEL);
549 if (copy_from_user(cud, user_cud, sizeof(*cud))) {
553 sscud_area->request.length = 0x0010;
554 sscud_area->request.code = 0x0006;
555 sscud_area->m = cud->schid.m;
556 sscud_area->fmt1 = cud->fmt;
557 sscud_area->ssid = cud->schid.ssid;
558 sscud_area->first_sch = cud->schid.sch_no;
559 sscud_area->cssid = cud->schid.cssid;
560 sscud_area->last_sch = cud->schid.sch_no;
562 ccode = chsc(sscud_area);
567 if (sscud_area->response.code != 0x0001) {
569 CHSC_MSG(0, "sscud: response code=%x\n",
570 sscud_area->response.code);
573 memcpy(&cud->scub, &sscud_area->response, sscud_area->response.length);
574 if (copy_to_user(user_cud, cud, sizeof(*cud)))
580 free_page((unsigned long)sscud_area);
584 static int chsc_ioctl_conf_info(void __user *user_ci)
586 struct chsc_conf_info *ci;
589 struct chsc_header request;
599 struct chsc_header response;
600 u8 data[PAGE_SIZE - 20];
601 } __attribute__ ((packed)) *sci_area;
603 sci_area = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
606 ci = kzalloc(sizeof(*ci), GFP_KERNEL);
611 if (copy_from_user(ci, user_ci, sizeof(*ci))) {
615 sci_area->request.length = 0x0010;
616 sci_area->request.code = 0x0012;
617 sci_area->m = ci->id.m;
618 sci_area->fmt1 = ci->fmt;
619 sci_area->cssid = ci->id.cssid;
620 sci_area->ssid = ci->id.ssid;
622 ccode = chsc(sci_area);
627 if (sci_area->response.code != 0x0001) {
629 CHSC_MSG(0, "sci: response code=%x\n",
630 sci_area->response.code);
633 memcpy(&ci->scid, &sci_area->response, sci_area->response.length);
634 if (copy_to_user(user_ci, ci, sizeof(*ci)))
640 free_page((unsigned long)sci_area);
644 static int chsc_ioctl_conf_comp_list(void __user *user_ccl)
646 struct chsc_comp_list *ccl;
649 struct chsc_header request;
657 struct chsc_header response;
658 u8 data[PAGE_SIZE - 36];
659 } __attribute__ ((packed)) *sccl_area;
666 } __attribute__ ((packed)) *chpid_parm;
672 } __attribute__ ((packed)) *cssids_parm;
674 sccl_area = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
677 ccl = kzalloc(sizeof(*ccl), GFP_KERNEL);
682 if (copy_from_user(ccl, user_ccl, sizeof(*ccl))) {
686 sccl_area->request.length = 0x0020;
687 sccl_area->request.code = 0x0030;
688 sccl_area->fmt = ccl->req.fmt;
689 sccl_area->ctype = ccl->req.ctype;
690 switch (sccl_area->ctype) {
693 chpid_parm = (void *)&sccl_area->list_parm;
694 chpid_parm->m = ccl->req.chpid.m;
695 chpid_parm->cssid = ccl->req.chpid.chp.cssid;
696 chpid_parm->chpid = ccl->req.chpid.chp.id;
699 case CCL_CSS_IMG_CONF_CHAR:
700 cssids_parm = (void *)&sccl_area->list_parm;
701 cssids_parm->f_cssid = ccl->req.cssids.f_cssid;
702 cssids_parm->l_cssid = ccl->req.cssids.l_cssid;
705 ccode = chsc(sccl_area);
710 if (sccl_area->response.code != 0x0001) {
712 CHSC_MSG(0, "sccl: response code=%x\n",
713 sccl_area->response.code);
716 memcpy(&ccl->sccl, &sccl_area->response, sccl_area->response.length);
717 if (copy_to_user(user_ccl, ccl, sizeof(*ccl)))
723 free_page((unsigned long)sccl_area);
727 static int chsc_ioctl_chpd(void __user *user_chpd)
729 struct chsc_scpd *scpd_area;
730 struct chsc_cpd_info *chpd;
733 chpd = kzalloc(sizeof(*chpd), GFP_KERNEL);
734 scpd_area = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
735 if (!scpd_area || !chpd) {
739 if (copy_from_user(chpd, user_chpd, sizeof(*chpd))) {
743 ret = chsc_determine_channel_path_desc(chpd->chpid, chpd->fmt,
744 chpd->rfmt, chpd->c, chpd->m,
748 memcpy(&chpd->chpdb, &scpd_area->response, scpd_area->response.length);
749 if (copy_to_user(user_chpd, chpd, sizeof(*chpd)))
753 free_page((unsigned long)scpd_area);
757 static int chsc_ioctl_dcal(void __user *user_dcal)
759 struct chsc_dcal *dcal;
762 struct chsc_header request;
770 struct chsc_header response;
771 u8 data[PAGE_SIZE - 36];
772 } __attribute__ ((packed)) *sdcal_area;
774 sdcal_area = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
777 dcal = kzalloc(sizeof(*dcal), GFP_KERNEL);
782 if (copy_from_user(dcal, user_dcal, sizeof(*dcal))) {
786 sdcal_area->request.length = 0x0020;
787 sdcal_area->request.code = 0x0034;
788 sdcal_area->atype = dcal->req.atype;
789 sdcal_area->fmt = dcal->req.fmt;
790 memcpy(&sdcal_area->list_parm, &dcal->req.list_parm,
791 sizeof(sdcal_area->list_parm));
793 ccode = chsc(sdcal_area);
798 if (sdcal_area->response.code != 0x0001) {
800 CHSC_MSG(0, "sdcal: response code=%x\n",
801 sdcal_area->response.code);
804 memcpy(&dcal->sdcal, &sdcal_area->response,
805 sdcal_area->response.length);
806 if (copy_to_user(user_dcal, dcal, sizeof(*dcal)))
812 free_page((unsigned long)sdcal_area);
816 static long chsc_ioctl(struct file *filp, unsigned int cmd,
821 CHSC_MSG(2, "chsc_ioctl called, cmd=%x\n", cmd);
822 if (is_compat_task())
823 argp = compat_ptr(arg);
825 argp = (void __user *)arg;
828 return chsc_ioctl_start(argp);
829 case CHSC_START_SYNC:
830 return chsc_ioctl_start_sync(argp);
831 case CHSC_INFO_CHANNEL_PATH:
832 return chsc_ioctl_info_channel_path(argp);
834 return chsc_ioctl_info_cu(argp);
835 case CHSC_INFO_SCH_CU:
836 return chsc_ioctl_info_sch_cu(argp);
838 return chsc_ioctl_conf_info(argp);
840 return chsc_ioctl_conf_comp_list(argp);
842 return chsc_ioctl_chpd(argp);
844 return chsc_ioctl_dcal(argp);
845 default: /* unknown ioctl number */
850 static const struct file_operations chsc_fops = {
851 .owner = THIS_MODULE,
852 .open = nonseekable_open,
853 .unlocked_ioctl = chsc_ioctl,
854 .compat_ioctl = chsc_ioctl,
858 static struct miscdevice chsc_misc_device = {
859 .minor = MISC_DYNAMIC_MINOR,
864 static int __init chsc_misc_init(void)
866 return misc_register(&chsc_misc_device);
869 static void chsc_misc_cleanup(void)
871 misc_deregister(&chsc_misc_device);
874 static int __init chsc_sch_init(void)
878 ret = chsc_init_dbfs();
881 isc_register(CHSC_SCH_ISC);
882 ret = chsc_init_sch_driver();
885 ret = chsc_misc_init();
890 chsc_cleanup_sch_driver();
892 isc_unregister(CHSC_SCH_ISC);
897 static void __exit chsc_sch_exit(void)
900 chsc_cleanup_sch_driver();
901 isc_unregister(CHSC_SCH_ISC);
905 module_init(chsc_sch_init);
906 module_exit(chsc_sch_exit);