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 = {
151 .owner = THIS_MODULE,
152 .subchannel_type = chsc_subchannel_ids,
153 .irq = chsc_subchannel_irq,
154 .probe = chsc_subchannel_probe,
155 .remove = chsc_subchannel_remove,
156 .shutdown = chsc_subchannel_shutdown,
157 .prepare = chsc_subchannel_prepare,
158 .freeze = chsc_subchannel_freeze,
159 .thaw = chsc_subchannel_restore,
160 .restore = chsc_subchannel_restore,
161 .name = "chsc_subchannel",
164 static int __init chsc_init_dbfs(void)
166 chsc_debug_msg_id = debug_register("chsc_msg", 16, 1,
168 if (!chsc_debug_msg_id)
170 debug_register_view(chsc_debug_msg_id, &debug_sprintf_view);
171 debug_set_level(chsc_debug_msg_id, 2);
172 chsc_debug_log_id = debug_register("chsc_log", 16, 1, 16);
173 if (!chsc_debug_log_id)
175 debug_register_view(chsc_debug_log_id, &debug_hex_ascii_view);
176 debug_set_level(chsc_debug_log_id, 2);
179 if (chsc_debug_msg_id)
180 debug_unregister(chsc_debug_msg_id);
184 static void chsc_remove_dbfs(void)
186 debug_unregister(chsc_debug_log_id);
187 debug_unregister(chsc_debug_msg_id);
190 static int __init chsc_init_sch_driver(void)
192 return css_driver_register(&chsc_subchannel_driver);
195 static void chsc_cleanup_sch_driver(void)
197 css_driver_unregister(&chsc_subchannel_driver);
200 static DEFINE_SPINLOCK(chsc_lock);
202 static int chsc_subchannel_match_next_free(struct device *dev, void *data)
204 struct subchannel *sch = to_subchannel(dev);
206 return sch->schib.pmcw.ena && !scsw_fctl(&sch->schib.scsw);
209 static struct subchannel *chsc_get_next_subchannel(struct subchannel *sch)
213 dev = driver_find_device(&chsc_subchannel_driver.drv,
214 sch ? &sch->dev : NULL, NULL,
215 chsc_subchannel_match_next_free);
216 return dev ? to_subchannel(dev) : NULL;
220 * chsc_async() - try to start a chsc request asynchronously
221 * @chsc_area: request to be started
222 * @request: request structure to associate
224 * Tries to start a chsc request on one of the existing chsc subchannels.
226 * %0 if the request was performed synchronously
227 * %-EINPROGRESS if the request was successfully started
228 * %-EBUSY if all chsc subchannels are busy
229 * %-ENODEV if no chsc subchannels are available
231 * interrupts disabled, chsc_lock held
233 static int chsc_async(struct chsc_async_area *chsc_area,
234 struct chsc_request *request)
237 struct chsc_private *private;
238 struct subchannel *sch = NULL;
242 chsc_area->header.key = PAGE_DEFAULT_KEY >> 4;
243 while ((sch = chsc_get_next_subchannel(sch))) {
244 spin_lock(sch->lock);
245 private = dev_get_drvdata(&sch->dev);
246 if (private->request) {
247 spin_unlock(sch->lock);
251 chsc_area->header.sid = sch->schid;
252 CHSC_LOG(2, "schid");
253 CHSC_LOG_HEX(2, &sch->schid, sizeof(sch->schid));
254 cc = chsc(chsc_area);
255 sprintf(dbf, "cc:%d", cc);
262 sch->schib.scsw.cmd.fctl |= SCSW_FCTL_START_FUNC;
264 private->request = request;
272 spin_unlock(sch->lock);
273 CHSC_MSG(2, "chsc on 0.%x.%04x returned cc=%d\n",
274 sch->schid.ssid, sch->schid.sch_no, cc);
275 if (ret == -EINPROGRESS)
277 put_device(&sch->dev);
284 static void chsc_log_command(struct chsc_async_area *chsc_area)
288 sprintf(dbf, "CHSC:%x", chsc_area->header.code);
290 CHSC_LOG_HEX(0, chsc_area, 32);
293 static int chsc_examine_irb(struct chsc_request *request)
297 if (!(scsw_stctl(&request->irb.scsw) & SCSW_STCTL_STATUS_PEND))
299 backed_up = scsw_cstat(&request->irb.scsw) & SCHN_STAT_CHAIN_CHECK;
300 request->irb.scsw.cmd.cstat &= ~SCHN_STAT_CHAIN_CHECK;
301 if (scsw_cstat(&request->irb.scsw) == 0)
305 if (scsw_cstat(&request->irb.scsw) & SCHN_STAT_PROG_CHECK)
307 if (scsw_cstat(&request->irb.scsw) & SCHN_STAT_PROT_CHECK)
309 if (scsw_cstat(&request->irb.scsw) & SCHN_STAT_CHN_DATA_CHK)
311 if (scsw_cstat(&request->irb.scsw) & SCHN_STAT_CHN_CTRL_CHK)
316 static int chsc_ioctl_start(void __user *user_area)
318 struct chsc_request *request;
319 struct chsc_async_area *chsc_area;
323 if (!css_general_characteristics.dynio)
324 /* It makes no sense to try. */
326 chsc_area = (void *)get_zeroed_page(GFP_DMA | GFP_KERNEL);
329 request = kzalloc(sizeof(*request), GFP_KERNEL);
334 init_completion(&request->completion);
335 if (copy_from_user(chsc_area, user_area, PAGE_SIZE)) {
339 chsc_log_command(chsc_area);
340 spin_lock_irq(&chsc_lock);
341 ret = chsc_async(chsc_area, request);
342 spin_unlock_irq(&chsc_lock);
343 if (ret == -EINPROGRESS) {
344 wait_for_completion(&request->completion);
345 ret = chsc_examine_irb(request);
347 /* copy area back to user */
349 if (copy_to_user(user_area, chsc_area, PAGE_SIZE))
352 sprintf(dbf, "ret:%d", ret);
355 free_page((unsigned long)chsc_area);
359 static int chsc_ioctl_info_channel_path(void __user *user_cd)
361 struct chsc_chp_cd *cd;
364 struct chsc_header request;
375 struct chsc_header response;
376 u8 data[PAGE_SIZE - 20];
377 } __attribute__ ((packed)) *scpcd_area;
379 scpcd_area = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
382 cd = kzalloc(sizeof(*cd), GFP_KERNEL);
387 if (copy_from_user(cd, user_cd, sizeof(*cd))) {
391 scpcd_area->request.length = 0x0010;
392 scpcd_area->request.code = 0x0028;
393 scpcd_area->m = cd->m;
394 scpcd_area->fmt1 = cd->fmt;
395 scpcd_area->cssid = cd->chpid.cssid;
396 scpcd_area->first_chpid = cd->chpid.id;
397 scpcd_area->last_chpid = cd->chpid.id;
399 ccode = chsc(scpcd_area);
404 if (scpcd_area->response.code != 0x0001) {
406 CHSC_MSG(0, "scpcd: response code=%x\n",
407 scpcd_area->response.code);
410 memcpy(&cd->cpcb, &scpcd_area->response, scpcd_area->response.length);
411 if (copy_to_user(user_cd, cd, sizeof(*cd)))
417 free_page((unsigned long)scpcd_area);
421 static int chsc_ioctl_info_cu(void __user *user_cd)
423 struct chsc_cu_cd *cd;
426 struct chsc_header request;
437 struct chsc_header response;
438 u8 data[PAGE_SIZE - 20];
439 } __attribute__ ((packed)) *scucd_area;
441 scucd_area = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
444 cd = kzalloc(sizeof(*cd), GFP_KERNEL);
449 if (copy_from_user(cd, user_cd, sizeof(*cd))) {
453 scucd_area->request.length = 0x0010;
454 scucd_area->request.code = 0x0028;
455 scucd_area->m = cd->m;
456 scucd_area->fmt1 = cd->fmt;
457 scucd_area->cssid = cd->cssid;
458 scucd_area->first_cun = cd->cun;
459 scucd_area->last_cun = cd->cun;
461 ccode = chsc(scucd_area);
466 if (scucd_area->response.code != 0x0001) {
468 CHSC_MSG(0, "scucd: response code=%x\n",
469 scucd_area->response.code);
472 memcpy(&cd->cucb, &scucd_area->response, scucd_area->response.length);
473 if (copy_to_user(user_cd, cd, sizeof(*cd)))
479 free_page((unsigned long)scucd_area);
483 static int chsc_ioctl_info_sch_cu(void __user *user_cud)
485 struct chsc_sch_cud *cud;
488 struct chsc_header request;
500 struct chsc_header response;
501 u8 data[PAGE_SIZE - 20];
502 } __attribute__ ((packed)) *sscud_area;
504 sscud_area = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
507 cud = kzalloc(sizeof(*cud), GFP_KERNEL);
512 if (copy_from_user(cud, user_cud, sizeof(*cud))) {
516 sscud_area->request.length = 0x0010;
517 sscud_area->request.code = 0x0006;
518 sscud_area->m = cud->schid.m;
519 sscud_area->fmt1 = cud->fmt;
520 sscud_area->ssid = cud->schid.ssid;
521 sscud_area->first_sch = cud->schid.sch_no;
522 sscud_area->cssid = cud->schid.cssid;
523 sscud_area->last_sch = cud->schid.sch_no;
525 ccode = chsc(sscud_area);
530 if (sscud_area->response.code != 0x0001) {
532 CHSC_MSG(0, "sscud: response code=%x\n",
533 sscud_area->response.code);
536 memcpy(&cud->scub, &sscud_area->response, sscud_area->response.length);
537 if (copy_to_user(user_cud, cud, sizeof(*cud)))
543 free_page((unsigned long)sscud_area);
547 static int chsc_ioctl_conf_info(void __user *user_ci)
549 struct chsc_conf_info *ci;
552 struct chsc_header request;
562 struct chsc_header response;
563 u8 data[PAGE_SIZE - 20];
564 } __attribute__ ((packed)) *sci_area;
566 sci_area = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
569 ci = kzalloc(sizeof(*ci), GFP_KERNEL);
574 if (copy_from_user(ci, user_ci, sizeof(*ci))) {
578 sci_area->request.length = 0x0010;
579 sci_area->request.code = 0x0012;
580 sci_area->m = ci->id.m;
581 sci_area->fmt1 = ci->fmt;
582 sci_area->cssid = ci->id.cssid;
583 sci_area->ssid = ci->id.ssid;
585 ccode = chsc(sci_area);
590 if (sci_area->response.code != 0x0001) {
592 CHSC_MSG(0, "sci: response code=%x\n",
593 sci_area->response.code);
596 memcpy(&ci->scid, &sci_area->response, sci_area->response.length);
597 if (copy_to_user(user_ci, ci, sizeof(*ci)))
603 free_page((unsigned long)sci_area);
607 static int chsc_ioctl_conf_comp_list(void __user *user_ccl)
609 struct chsc_comp_list *ccl;
612 struct chsc_header request;
620 struct chsc_header response;
621 u8 data[PAGE_SIZE - 36];
622 } __attribute__ ((packed)) *sccl_area;
629 } __attribute__ ((packed)) *chpid_parm;
635 } __attribute__ ((packed)) *cssids_parm;
637 sccl_area = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
640 ccl = kzalloc(sizeof(*ccl), GFP_KERNEL);
645 if (copy_from_user(ccl, user_ccl, sizeof(*ccl))) {
649 sccl_area->request.length = 0x0020;
650 sccl_area->request.code = 0x0030;
651 sccl_area->fmt = ccl->req.fmt;
652 sccl_area->ctype = ccl->req.ctype;
653 switch (sccl_area->ctype) {
656 chpid_parm = (void *)&sccl_area->list_parm;
657 chpid_parm->m = ccl->req.chpid.m;
658 chpid_parm->cssid = ccl->req.chpid.chp.cssid;
659 chpid_parm->chpid = ccl->req.chpid.chp.id;
662 case CCL_CSS_IMG_CONF_CHAR:
663 cssids_parm = (void *)&sccl_area->list_parm;
664 cssids_parm->f_cssid = ccl->req.cssids.f_cssid;
665 cssids_parm->l_cssid = ccl->req.cssids.l_cssid;
668 ccode = chsc(sccl_area);
673 if (sccl_area->response.code != 0x0001) {
675 CHSC_MSG(0, "sccl: response code=%x\n",
676 sccl_area->response.code);
679 memcpy(&ccl->sccl, &sccl_area->response, sccl_area->response.length);
680 if (copy_to_user(user_ccl, ccl, sizeof(*ccl)))
686 free_page((unsigned long)sccl_area);
690 static int chsc_ioctl_chpd(void __user *user_chpd)
692 struct chsc_scpd *scpd_area;
693 struct chsc_cpd_info *chpd;
696 chpd = kzalloc(sizeof(*chpd), GFP_KERNEL);
697 scpd_area = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
698 if (!scpd_area || !chpd) {
702 if (copy_from_user(chpd, user_chpd, sizeof(*chpd))) {
706 ret = chsc_determine_channel_path_desc(chpd->chpid, chpd->fmt,
707 chpd->rfmt, chpd->c, chpd->m,
711 memcpy(&chpd->chpdb, &scpd_area->response, scpd_area->response.length);
712 if (copy_to_user(user_chpd, chpd, sizeof(*chpd)))
716 free_page((unsigned long)scpd_area);
720 static int chsc_ioctl_dcal(void __user *user_dcal)
722 struct chsc_dcal *dcal;
725 struct chsc_header request;
733 struct chsc_header response;
734 u8 data[PAGE_SIZE - 36];
735 } __attribute__ ((packed)) *sdcal_area;
737 sdcal_area = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
740 dcal = kzalloc(sizeof(*dcal), GFP_KERNEL);
745 if (copy_from_user(dcal, user_dcal, sizeof(*dcal))) {
749 sdcal_area->request.length = 0x0020;
750 sdcal_area->request.code = 0x0034;
751 sdcal_area->atype = dcal->req.atype;
752 sdcal_area->fmt = dcal->req.fmt;
753 memcpy(&sdcal_area->list_parm, &dcal->req.list_parm,
754 sizeof(sdcal_area->list_parm));
756 ccode = chsc(sdcal_area);
761 if (sdcal_area->response.code != 0x0001) {
763 CHSC_MSG(0, "sdcal: response code=%x\n",
764 sdcal_area->response.code);
767 memcpy(&dcal->sdcal, &sdcal_area->response,
768 sdcal_area->response.length);
769 if (copy_to_user(user_dcal, dcal, sizeof(*dcal)))
775 free_page((unsigned long)sdcal_area);
779 static long chsc_ioctl(struct file *filp, unsigned int cmd,
784 CHSC_MSG(2, "chsc_ioctl called, cmd=%x\n", cmd);
785 if (is_compat_task())
786 argp = compat_ptr(arg);
788 argp = (void __user *)arg;
791 return chsc_ioctl_start(argp);
792 case CHSC_INFO_CHANNEL_PATH:
793 return chsc_ioctl_info_channel_path(argp);
795 return chsc_ioctl_info_cu(argp);
796 case CHSC_INFO_SCH_CU:
797 return chsc_ioctl_info_sch_cu(argp);
799 return chsc_ioctl_conf_info(argp);
801 return chsc_ioctl_conf_comp_list(argp);
803 return chsc_ioctl_chpd(argp);
805 return chsc_ioctl_dcal(argp);
806 default: /* unknown ioctl number */
811 static const struct file_operations chsc_fops = {
812 .owner = THIS_MODULE,
813 .open = nonseekable_open,
814 .unlocked_ioctl = chsc_ioctl,
815 .compat_ioctl = chsc_ioctl,
819 static struct miscdevice chsc_misc_device = {
820 .minor = MISC_DYNAMIC_MINOR,
825 static int __init chsc_misc_init(void)
827 return misc_register(&chsc_misc_device);
830 static void chsc_misc_cleanup(void)
832 misc_deregister(&chsc_misc_device);
835 static int __init chsc_sch_init(void)
839 ret = chsc_init_dbfs();
842 isc_register(CHSC_SCH_ISC);
843 ret = chsc_init_sch_driver();
846 ret = chsc_misc_init();
851 chsc_cleanup_sch_driver();
853 isc_unregister(CHSC_SCH_ISC);
858 static void __exit chsc_sch_exit(void)
861 chsc_cleanup_sch_driver();
862 isc_unregister(CHSC_SCH_ISC);
866 module_init(chsc_sch_init);
867 module_exit(chsc_sch_exit);