2 * Driver for s390 chsc subchannels
4 * Copyright IBM Corp. 2008, 2009
6 * Author(s): Cornelia Huck <cornelia.huck@de.ibm.com>
10 #include <linux/slab.h>
11 #include <linux/device.h>
12 #include <linux/module.h>
13 #include <linux/uaccess.h>
14 #include <linux/miscdevice.h>
16 #include <asm/compat.h>
22 #include "cio_debug.h"
27 static debug_info_t *chsc_debug_msg_id;
28 static debug_info_t *chsc_debug_log_id;
30 #define CHSC_MSG(imp, args...) do { \
31 debug_sprintf_event(chsc_debug_msg_id, imp , ##args); \
34 #define CHSC_LOG(imp, txt) do { \
35 debug_text_event(chsc_debug_log_id, imp , txt); \
38 static void CHSC_LOG_HEX(int level, void *data, int length)
41 debug_event(chsc_debug_log_id, level, data, length);
42 length -= chsc_debug_log_id->buf_size;
43 data += chsc_debug_log_id->buf_size;
47 MODULE_AUTHOR("IBM Corporation");
48 MODULE_DESCRIPTION("driver for s390 chsc subchannels");
49 MODULE_LICENSE("GPL");
51 static void chsc_subchannel_irq(struct subchannel *sch)
53 struct chsc_private *private = dev_get_drvdata(&sch->dev);
54 struct chsc_request *request = private->request;
55 struct irb *irb = (struct irb *)&S390_lowcore.irb;
58 CHSC_LOG_HEX(4, irb, sizeof(*irb));
59 /* Copy irb to provided request and set done. */
61 CHSC_MSG(0, "Interrupt on sch 0.%x.%04x with no request\n",
62 sch->schid.ssid, sch->schid.sch_no);
65 private->request = NULL;
66 memcpy(&request->irb, irb, sizeof(*irb));
67 cio_update_schib(sch);
68 complete(&request->completion);
69 put_device(&sch->dev);
72 static int chsc_subchannel_probe(struct subchannel *sch)
74 struct chsc_private *private;
77 CHSC_MSG(6, "Detected chsc subchannel 0.%x.%04x\n",
78 sch->schid.ssid, sch->schid.sch_no);
79 sch->isc = CHSC_SCH_ISC;
80 private = kzalloc(sizeof(*private), GFP_KERNEL);
83 dev_set_drvdata(&sch->dev, private);
84 ret = cio_enable_subchannel(sch, (u32)(unsigned long)sch);
86 CHSC_MSG(0, "Failed to enable 0.%x.%04x: %d\n",
87 sch->schid.ssid, sch->schid.sch_no, ret);
88 dev_set_drvdata(&sch->dev, NULL);
91 if (dev_get_uevent_suppress(&sch->dev)) {
92 dev_set_uevent_suppress(&sch->dev, 0);
93 kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
99 static int 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);
114 static void chsc_subchannel_shutdown(struct subchannel *sch)
116 cio_disable_subchannel(sch);
119 static int chsc_subchannel_prepare(struct subchannel *sch)
124 * Don't allow suspend while the subchannel is not idle
125 * since we don't have a way to clear the subchannel and
126 * cannot disable it with a request running.
128 cc = stsch_err(sch->schid, &schib);
129 if (!cc && scsw_stctl(&schib.scsw))
134 static int chsc_subchannel_freeze(struct subchannel *sch)
136 return cio_disable_subchannel(sch);
139 static int chsc_subchannel_restore(struct subchannel *sch)
141 return cio_enable_subchannel(sch, (u32)(unsigned long)sch);
144 static struct css_device_id chsc_subchannel_ids[] = {
145 { .match_flags = 0x1, .type =SUBCHANNEL_TYPE_CHSC, },
146 { /* end of list */ },
148 MODULE_DEVICE_TABLE(css, chsc_subchannel_ids);
150 static struct css_driver chsc_subchannel_driver = {
152 .owner = THIS_MODULE,
153 .name = "chsc_subchannel",
155 .subchannel_type = chsc_subchannel_ids,
156 .irq = chsc_subchannel_irq,
157 .probe = chsc_subchannel_probe,
158 .remove = chsc_subchannel_remove,
159 .shutdown = chsc_subchannel_shutdown,
160 .prepare = chsc_subchannel_prepare,
161 .freeze = chsc_subchannel_freeze,
162 .thaw = chsc_subchannel_restore,
163 .restore = chsc_subchannel_restore,
166 static int __init chsc_init_dbfs(void)
168 chsc_debug_msg_id = debug_register("chsc_msg", 16, 1,
170 if (!chsc_debug_msg_id)
172 debug_register_view(chsc_debug_msg_id, &debug_sprintf_view);
173 debug_set_level(chsc_debug_msg_id, 2);
174 chsc_debug_log_id = debug_register("chsc_log", 16, 1, 16);
175 if (!chsc_debug_log_id)
177 debug_register_view(chsc_debug_log_id, &debug_hex_ascii_view);
178 debug_set_level(chsc_debug_log_id, 2);
181 if (chsc_debug_msg_id)
182 debug_unregister(chsc_debug_msg_id);
186 static void chsc_remove_dbfs(void)
188 debug_unregister(chsc_debug_log_id);
189 debug_unregister(chsc_debug_msg_id);
192 static int __init chsc_init_sch_driver(void)
194 return css_driver_register(&chsc_subchannel_driver);
197 static void chsc_cleanup_sch_driver(void)
199 css_driver_unregister(&chsc_subchannel_driver);
202 static DEFINE_SPINLOCK(chsc_lock);
204 static int chsc_subchannel_match_next_free(struct device *dev, void *data)
206 struct subchannel *sch = to_subchannel(dev);
208 return sch->schib.pmcw.ena && !scsw_fctl(&sch->schib.scsw);
211 static struct subchannel *chsc_get_next_subchannel(struct subchannel *sch)
215 dev = driver_find_device(&chsc_subchannel_driver.drv,
216 sch ? &sch->dev : NULL, NULL,
217 chsc_subchannel_match_next_free);
218 return dev ? to_subchannel(dev) : NULL;
222 * chsc_async() - try to start a chsc request asynchronously
223 * @chsc_area: request to be started
224 * @request: request structure to associate
226 * Tries to start a chsc request on one of the existing chsc subchannels.
228 * %0 if the request was performed synchronously
229 * %-EINPROGRESS if the request was successfully started
230 * %-EBUSY if all chsc subchannels are busy
231 * %-ENODEV if no chsc subchannels are available
233 * interrupts disabled, chsc_lock held
235 static int chsc_async(struct chsc_async_area *chsc_area,
236 struct chsc_request *request)
239 struct chsc_private *private;
240 struct subchannel *sch = NULL;
244 chsc_area->header.key = PAGE_DEFAULT_KEY >> 4;
245 while ((sch = chsc_get_next_subchannel(sch))) {
246 spin_lock(sch->lock);
247 private = dev_get_drvdata(&sch->dev);
248 if (private->request) {
249 spin_unlock(sch->lock);
253 chsc_area->header.sid = sch->schid;
254 CHSC_LOG(2, "schid");
255 CHSC_LOG_HEX(2, &sch->schid, sizeof(sch->schid));
256 cc = chsc(chsc_area);
257 sprintf(dbf, "cc:%d", cc);
264 sch->schib.scsw.cmd.fctl |= SCSW_FCTL_START_FUNC;
266 private->request = request;
274 spin_unlock(sch->lock);
275 CHSC_MSG(2, "chsc on 0.%x.%04x returned cc=%d\n",
276 sch->schid.ssid, sch->schid.sch_no, cc);
277 if (ret == -EINPROGRESS)
279 put_device(&sch->dev);
286 static void chsc_log_command(struct chsc_async_area *chsc_area)
290 sprintf(dbf, "CHSC:%x", chsc_area->header.code);
292 CHSC_LOG_HEX(0, chsc_area, 32);
295 static int chsc_examine_irb(struct chsc_request *request)
299 if (!(scsw_stctl(&request->irb.scsw) & SCSW_STCTL_STATUS_PEND))
301 backed_up = scsw_cstat(&request->irb.scsw) & SCHN_STAT_CHAIN_CHECK;
302 request->irb.scsw.cmd.cstat &= ~SCHN_STAT_CHAIN_CHECK;
303 if (scsw_cstat(&request->irb.scsw) == 0)
307 if (scsw_cstat(&request->irb.scsw) & SCHN_STAT_PROG_CHECK)
309 if (scsw_cstat(&request->irb.scsw) & SCHN_STAT_PROT_CHECK)
311 if (scsw_cstat(&request->irb.scsw) & SCHN_STAT_CHN_DATA_CHK)
313 if (scsw_cstat(&request->irb.scsw) & SCHN_STAT_CHN_CTRL_CHK)
318 static int chsc_ioctl_start(void __user *user_area)
320 struct chsc_request *request;
321 struct chsc_async_area *chsc_area;
325 if (!css_general_characteristics.dynio)
326 /* It makes no sense to try. */
328 chsc_area = (void *)get_zeroed_page(GFP_DMA | GFP_KERNEL);
331 request = kzalloc(sizeof(*request), GFP_KERNEL);
336 init_completion(&request->completion);
337 if (copy_from_user(chsc_area, user_area, PAGE_SIZE)) {
341 chsc_log_command(chsc_area);
342 spin_lock_irq(&chsc_lock);
343 ret = chsc_async(chsc_area, request);
344 spin_unlock_irq(&chsc_lock);
345 if (ret == -EINPROGRESS) {
346 wait_for_completion(&request->completion);
347 ret = chsc_examine_irb(request);
349 /* copy area back to user */
351 if (copy_to_user(user_area, chsc_area, PAGE_SIZE))
354 sprintf(dbf, "ret:%d", ret);
357 free_page((unsigned long)chsc_area);
361 static int chsc_ioctl_info_channel_path(void __user *user_cd)
363 struct chsc_chp_cd *cd;
366 struct chsc_header request;
377 struct chsc_header response;
378 u8 data[PAGE_SIZE - 20];
379 } __attribute__ ((packed)) *scpcd_area;
381 scpcd_area = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
384 cd = kzalloc(sizeof(*cd), GFP_KERNEL);
389 if (copy_from_user(cd, user_cd, sizeof(*cd))) {
393 scpcd_area->request.length = 0x0010;
394 scpcd_area->request.code = 0x0028;
395 scpcd_area->m = cd->m;
396 scpcd_area->fmt1 = cd->fmt;
397 scpcd_area->cssid = cd->chpid.cssid;
398 scpcd_area->first_chpid = cd->chpid.id;
399 scpcd_area->last_chpid = cd->chpid.id;
401 ccode = chsc(scpcd_area);
406 if (scpcd_area->response.code != 0x0001) {
408 CHSC_MSG(0, "scpcd: response code=%x\n",
409 scpcd_area->response.code);
412 memcpy(&cd->cpcb, &scpcd_area->response, scpcd_area->response.length);
413 if (copy_to_user(user_cd, cd, sizeof(*cd)))
419 free_page((unsigned long)scpcd_area);
423 static int chsc_ioctl_info_cu(void __user *user_cd)
425 struct chsc_cu_cd *cd;
428 struct chsc_header request;
439 struct chsc_header response;
440 u8 data[PAGE_SIZE - 20];
441 } __attribute__ ((packed)) *scucd_area;
443 scucd_area = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
446 cd = kzalloc(sizeof(*cd), GFP_KERNEL);
451 if (copy_from_user(cd, user_cd, sizeof(*cd))) {
455 scucd_area->request.length = 0x0010;
456 scucd_area->request.code = 0x0028;
457 scucd_area->m = cd->m;
458 scucd_area->fmt1 = cd->fmt;
459 scucd_area->cssid = cd->cssid;
460 scucd_area->first_cun = cd->cun;
461 scucd_area->last_cun = cd->cun;
463 ccode = chsc(scucd_area);
468 if (scucd_area->response.code != 0x0001) {
470 CHSC_MSG(0, "scucd: response code=%x\n",
471 scucd_area->response.code);
474 memcpy(&cd->cucb, &scucd_area->response, scucd_area->response.length);
475 if (copy_to_user(user_cd, cd, sizeof(*cd)))
481 free_page((unsigned long)scucd_area);
485 static int chsc_ioctl_info_sch_cu(void __user *user_cud)
487 struct chsc_sch_cud *cud;
490 struct chsc_header request;
502 struct chsc_header response;
503 u8 data[PAGE_SIZE - 20];
504 } __attribute__ ((packed)) *sscud_area;
506 sscud_area = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
509 cud = kzalloc(sizeof(*cud), GFP_KERNEL);
514 if (copy_from_user(cud, user_cud, sizeof(*cud))) {
518 sscud_area->request.length = 0x0010;
519 sscud_area->request.code = 0x0006;
520 sscud_area->m = cud->schid.m;
521 sscud_area->fmt1 = cud->fmt;
522 sscud_area->ssid = cud->schid.ssid;
523 sscud_area->first_sch = cud->schid.sch_no;
524 sscud_area->cssid = cud->schid.cssid;
525 sscud_area->last_sch = cud->schid.sch_no;
527 ccode = chsc(sscud_area);
532 if (sscud_area->response.code != 0x0001) {
534 CHSC_MSG(0, "sscud: response code=%x\n",
535 sscud_area->response.code);
538 memcpy(&cud->scub, &sscud_area->response, sscud_area->response.length);
539 if (copy_to_user(user_cud, cud, sizeof(*cud)))
545 free_page((unsigned long)sscud_area);
549 static int chsc_ioctl_conf_info(void __user *user_ci)
551 struct chsc_conf_info *ci;
554 struct chsc_header request;
564 struct chsc_header response;
565 u8 data[PAGE_SIZE - 20];
566 } __attribute__ ((packed)) *sci_area;
568 sci_area = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
571 ci = kzalloc(sizeof(*ci), GFP_KERNEL);
576 if (copy_from_user(ci, user_ci, sizeof(*ci))) {
580 sci_area->request.length = 0x0010;
581 sci_area->request.code = 0x0012;
582 sci_area->m = ci->id.m;
583 sci_area->fmt1 = ci->fmt;
584 sci_area->cssid = ci->id.cssid;
585 sci_area->ssid = ci->id.ssid;
587 ccode = chsc(sci_area);
592 if (sci_area->response.code != 0x0001) {
594 CHSC_MSG(0, "sci: response code=%x\n",
595 sci_area->response.code);
598 memcpy(&ci->scid, &sci_area->response, sci_area->response.length);
599 if (copy_to_user(user_ci, ci, sizeof(*ci)))
605 free_page((unsigned long)sci_area);
609 static int chsc_ioctl_conf_comp_list(void __user *user_ccl)
611 struct chsc_comp_list *ccl;
614 struct chsc_header request;
622 struct chsc_header response;
623 u8 data[PAGE_SIZE - 36];
624 } __attribute__ ((packed)) *sccl_area;
631 } __attribute__ ((packed)) *chpid_parm;
637 } __attribute__ ((packed)) *cssids_parm;
639 sccl_area = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
642 ccl = kzalloc(sizeof(*ccl), GFP_KERNEL);
647 if (copy_from_user(ccl, user_ccl, sizeof(*ccl))) {
651 sccl_area->request.length = 0x0020;
652 sccl_area->request.code = 0x0030;
653 sccl_area->fmt = ccl->req.fmt;
654 sccl_area->ctype = ccl->req.ctype;
655 switch (sccl_area->ctype) {
658 chpid_parm = (void *)&sccl_area->list_parm;
659 chpid_parm->m = ccl->req.chpid.m;
660 chpid_parm->cssid = ccl->req.chpid.chp.cssid;
661 chpid_parm->chpid = ccl->req.chpid.chp.id;
664 case CCL_CSS_IMG_CONF_CHAR:
665 cssids_parm = (void *)&sccl_area->list_parm;
666 cssids_parm->f_cssid = ccl->req.cssids.f_cssid;
667 cssids_parm->l_cssid = ccl->req.cssids.l_cssid;
670 ccode = chsc(sccl_area);
675 if (sccl_area->response.code != 0x0001) {
677 CHSC_MSG(0, "sccl: response code=%x\n",
678 sccl_area->response.code);
681 memcpy(&ccl->sccl, &sccl_area->response, sccl_area->response.length);
682 if (copy_to_user(user_ccl, ccl, sizeof(*ccl)))
688 free_page((unsigned long)sccl_area);
692 static int chsc_ioctl_chpd(void __user *user_chpd)
694 struct chsc_scpd *scpd_area;
695 struct chsc_cpd_info *chpd;
698 chpd = kzalloc(sizeof(*chpd), GFP_KERNEL);
699 scpd_area = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
700 if (!scpd_area || !chpd) {
704 if (copy_from_user(chpd, user_chpd, sizeof(*chpd))) {
708 ret = chsc_determine_channel_path_desc(chpd->chpid, chpd->fmt,
709 chpd->rfmt, chpd->c, chpd->m,
713 memcpy(&chpd->chpdb, &scpd_area->response, scpd_area->response.length);
714 if (copy_to_user(user_chpd, chpd, sizeof(*chpd)))
718 free_page((unsigned long)scpd_area);
722 static int chsc_ioctl_dcal(void __user *user_dcal)
724 struct chsc_dcal *dcal;
727 struct chsc_header request;
735 struct chsc_header response;
736 u8 data[PAGE_SIZE - 36];
737 } __attribute__ ((packed)) *sdcal_area;
739 sdcal_area = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
742 dcal = kzalloc(sizeof(*dcal), GFP_KERNEL);
747 if (copy_from_user(dcal, user_dcal, sizeof(*dcal))) {
751 sdcal_area->request.length = 0x0020;
752 sdcal_area->request.code = 0x0034;
753 sdcal_area->atype = dcal->req.atype;
754 sdcal_area->fmt = dcal->req.fmt;
755 memcpy(&sdcal_area->list_parm, &dcal->req.list_parm,
756 sizeof(sdcal_area->list_parm));
758 ccode = chsc(sdcal_area);
763 if (sdcal_area->response.code != 0x0001) {
765 CHSC_MSG(0, "sdcal: response code=%x\n",
766 sdcal_area->response.code);
769 memcpy(&dcal->sdcal, &sdcal_area->response,
770 sdcal_area->response.length);
771 if (copy_to_user(user_dcal, dcal, sizeof(*dcal)))
777 free_page((unsigned long)sdcal_area);
781 static long chsc_ioctl(struct file *filp, unsigned int cmd,
786 CHSC_MSG(2, "chsc_ioctl called, cmd=%x\n", cmd);
787 if (is_compat_task())
788 argp = compat_ptr(arg);
790 argp = (void __user *)arg;
793 return chsc_ioctl_start(argp);
794 case CHSC_INFO_CHANNEL_PATH:
795 return chsc_ioctl_info_channel_path(argp);
797 return chsc_ioctl_info_cu(argp);
798 case CHSC_INFO_SCH_CU:
799 return chsc_ioctl_info_sch_cu(argp);
801 return chsc_ioctl_conf_info(argp);
803 return chsc_ioctl_conf_comp_list(argp);
805 return chsc_ioctl_chpd(argp);
807 return chsc_ioctl_dcal(argp);
808 default: /* unknown ioctl number */
813 static const struct file_operations chsc_fops = {
814 .owner = THIS_MODULE,
815 .open = nonseekable_open,
816 .unlocked_ioctl = chsc_ioctl,
817 .compat_ioctl = chsc_ioctl,
821 static struct miscdevice chsc_misc_device = {
822 .minor = MISC_DYNAMIC_MINOR,
827 static int __init chsc_misc_init(void)
829 return misc_register(&chsc_misc_device);
832 static void chsc_misc_cleanup(void)
834 misc_deregister(&chsc_misc_device);
837 static int __init chsc_sch_init(void)
841 ret = chsc_init_dbfs();
844 isc_register(CHSC_SCH_ISC);
845 ret = chsc_init_sch_driver();
848 ret = chsc_misc_init();
853 chsc_cleanup_sch_driver();
855 isc_unregister(CHSC_SCH_ISC);
860 static void __exit chsc_sch_exit(void)
863 chsc_cleanup_sch_driver();
864 isc_unregister(CHSC_SCH_ISC);
868 module_init(chsc_sch_init);
869 module_exit(chsc_sch_exit);