2 * Driver for s390 chsc subchannels
4 * Copyright IBM Corp. 2008
5 * Author(s): Cornelia Huck <cornelia.huck@de.ibm.com>
9 #include <linux/device.h>
10 #include <linux/module.h>
11 #include <linux/uaccess.h>
12 #include <linux/miscdevice.h>
19 #include "cio_debug.h"
24 static debug_info_t *chsc_debug_msg_id;
25 static debug_info_t *chsc_debug_log_id;
27 #define CHSC_MSG(imp, args...) do { \
28 debug_sprintf_event(chsc_debug_msg_id, imp , ##args); \
31 #define CHSC_LOG(imp, txt) do { \
32 debug_text_event(chsc_debug_log_id, imp , txt); \
35 static void CHSC_LOG_HEX(int level, void *data, int length)
38 debug_event(chsc_debug_log_id, level, data, length);
39 length -= chsc_debug_log_id->buf_size;
40 data += chsc_debug_log_id->buf_size;
44 MODULE_AUTHOR("IBM Corporation");
45 MODULE_DESCRIPTION("driver for s390 chsc subchannels");
46 MODULE_LICENSE("GPL");
48 static void chsc_subchannel_irq(struct subchannel *sch)
50 struct chsc_private *private = sch->private;
51 struct chsc_request *request = private->request;
52 struct irb *irb = (struct irb *)__LC_IRB;
55 CHSC_LOG_HEX(4, irb, sizeof(*irb));
56 /* Copy irb to provided request and set done. */
58 CHSC_MSG(0, "Interrupt on sch 0.%x.%04x with no request\n",
59 sch->schid.ssid, sch->schid.sch_no);
62 private->request = NULL;
63 memcpy(&request->irb, irb, sizeof(*irb));
64 cio_update_schib(sch);
65 complete(&request->completion);
66 put_device(&sch->dev);
69 static int chsc_subchannel_probe(struct subchannel *sch)
71 struct chsc_private *private;
74 CHSC_MSG(6, "Detected chsc subchannel 0.%x.%04x\n",
75 sch->schid.ssid, sch->schid.sch_no);
76 sch->isc = CHSC_SCH_ISC;
77 private = kzalloc(sizeof(*private), GFP_KERNEL);
80 ret = cio_enable_subchannel(sch, (u32)(unsigned long)sch);
82 CHSC_MSG(0, "Failed to enable 0.%x.%04x: %d\n",
83 sch->schid.ssid, sch->schid.sch_no, ret);
86 sch->private = private;
87 if (sch->dev.uevent_suppress) {
88 sch->dev.uevent_suppress = 0;
89 kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
95 static int chsc_subchannel_remove(struct subchannel *sch)
97 struct chsc_private *private;
99 cio_disable_subchannel(sch);
100 private = sch->private;
102 if (private->request) {
103 complete(&private->request->completion);
104 put_device(&sch->dev);
110 static void chsc_subchannel_shutdown(struct subchannel *sch)
112 cio_disable_subchannel(sch);
115 static struct css_device_id chsc_subchannel_ids[] = {
116 { .match_flags = 0x1, .type =SUBCHANNEL_TYPE_CHSC, },
117 { /* end of list */ },
119 MODULE_DEVICE_TABLE(css, chsc_subchannel_ids);
121 static struct css_driver chsc_subchannel_driver = {
122 .owner = THIS_MODULE,
123 .subchannel_type = chsc_subchannel_ids,
124 .irq = chsc_subchannel_irq,
125 .probe = chsc_subchannel_probe,
126 .remove = chsc_subchannel_remove,
127 .shutdown = chsc_subchannel_shutdown,
128 .name = "chsc_subchannel",
131 static int __init chsc_init_dbfs(void)
133 chsc_debug_msg_id = debug_register("chsc_msg", 16, 1,
135 if (!chsc_debug_msg_id)
137 debug_register_view(chsc_debug_msg_id, &debug_sprintf_view);
138 debug_set_level(chsc_debug_msg_id, 2);
139 chsc_debug_log_id = debug_register("chsc_log", 16, 1, 16);
140 if (!chsc_debug_log_id)
142 debug_register_view(chsc_debug_log_id, &debug_hex_ascii_view);
143 debug_set_level(chsc_debug_log_id, 2);
146 if (chsc_debug_msg_id)
147 debug_unregister(chsc_debug_msg_id);
151 static void chsc_remove_dbfs(void)
153 debug_unregister(chsc_debug_log_id);
154 debug_unregister(chsc_debug_msg_id);
157 static int __init chsc_init_sch_driver(void)
159 return css_driver_register(&chsc_subchannel_driver);
162 static void chsc_cleanup_sch_driver(void)
164 css_driver_unregister(&chsc_subchannel_driver);
167 static DEFINE_SPINLOCK(chsc_lock);
169 static int chsc_subchannel_match_next_free(struct device *dev, void *data)
171 struct subchannel *sch = to_subchannel(dev);
173 return sch->schib.pmcw.ena && !scsw_fctl(&sch->schib.scsw);
176 static struct subchannel *chsc_get_next_subchannel(struct subchannel *sch)
180 dev = driver_find_device(&chsc_subchannel_driver.drv,
181 sch ? &sch->dev : NULL, NULL,
182 chsc_subchannel_match_next_free);
183 return dev ? to_subchannel(dev) : NULL;
187 * chsc_async() - try to start a chsc request asynchronously
188 * @chsc_area: request to be started
189 * @request: request structure to associate
191 * Tries to start a chsc request on one of the existing chsc subchannels.
193 * %0 if the request was performed synchronously
194 * %-EINPROGRESS if the request was successfully started
195 * %-EBUSY if all chsc subchannels are busy
196 * %-ENODEV if no chsc subchannels are available
198 * interrupts disabled, chsc_lock held
200 static int chsc_async(struct chsc_async_area *chsc_area,
201 struct chsc_request *request)
204 struct chsc_private *private;
205 struct subchannel *sch = NULL;
209 chsc_area->header.key = PAGE_DEFAULT_KEY;
210 while ((sch = chsc_get_next_subchannel(sch))) {
211 spin_lock(sch->lock);
212 private = sch->private;
213 if (private->request) {
214 spin_unlock(sch->lock);
218 chsc_area->header.sid = sch->schid;
219 CHSC_LOG(2, "schid");
220 CHSC_LOG_HEX(2, &sch->schid, sizeof(sch->schid));
221 cc = chsc(chsc_area);
222 sprintf(dbf, "cc:%d", cc);
229 sch->schib.scsw.cmd.fctl |= SCSW_FCTL_START_FUNC;
231 private->request = request;
239 spin_unlock(sch->lock);
240 CHSC_MSG(2, "chsc on 0.%x.%04x returned cc=%d\n",
241 sch->schid.ssid, sch->schid.sch_no, cc);
242 if (ret == -EINPROGRESS)
244 put_device(&sch->dev);
251 static void chsc_log_command(struct chsc_async_area *chsc_area)
255 sprintf(dbf, "CHSC:%x", chsc_area->header.code);
257 CHSC_LOG_HEX(0, chsc_area, 32);
260 static int chsc_examine_irb(struct chsc_request *request)
264 if (!(scsw_stctl(&request->irb.scsw) & SCSW_STCTL_STATUS_PEND))
266 backed_up = scsw_cstat(&request->irb.scsw) & SCHN_STAT_CHAIN_CHECK;
267 request->irb.scsw.cmd.cstat &= ~SCHN_STAT_CHAIN_CHECK;
268 if (scsw_cstat(&request->irb.scsw) == 0)
272 if (scsw_cstat(&request->irb.scsw) & SCHN_STAT_PROG_CHECK)
274 if (scsw_cstat(&request->irb.scsw) & SCHN_STAT_PROT_CHECK)
276 if (scsw_cstat(&request->irb.scsw) & SCHN_STAT_CHN_DATA_CHK)
278 if (scsw_cstat(&request->irb.scsw) & SCHN_STAT_CHN_CTRL_CHK)
283 static int chsc_ioctl_start(void __user *user_area)
285 struct chsc_request *request;
286 struct chsc_async_area *chsc_area;
290 if (!css_general_characteristics.dynio)
291 /* It makes no sense to try. */
293 chsc_area = (void *)get_zeroed_page(GFP_DMA | GFP_KERNEL);
296 request = kzalloc(sizeof(*request), GFP_KERNEL);
301 init_completion(&request->completion);
302 if (copy_from_user(chsc_area, user_area, PAGE_SIZE)) {
306 chsc_log_command(chsc_area);
307 spin_lock_irq(&chsc_lock);
308 ret = chsc_async(chsc_area, request);
309 spin_unlock_irq(&chsc_lock);
310 if (ret == -EINPROGRESS) {
311 wait_for_completion(&request->completion);
312 ret = chsc_examine_irb(request);
314 /* copy area back to user */
316 if (copy_to_user(user_area, chsc_area, PAGE_SIZE))
319 sprintf(dbf, "ret:%d", ret);
322 free_page((unsigned long)chsc_area);
326 static int chsc_ioctl_info_channel_path(void __user *user_cd)
328 struct chsc_chp_cd *cd;
331 struct chsc_header request;
342 struct chsc_header response;
343 u8 data[PAGE_SIZE - 20];
344 } __attribute__ ((packed)) *scpcd_area;
346 scpcd_area = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
349 cd = kzalloc(sizeof(*cd), GFP_KERNEL);
354 if (copy_from_user(cd, user_cd, sizeof(*cd))) {
358 scpcd_area->request.length = 0x0010;
359 scpcd_area->request.code = 0x0028;
360 scpcd_area->m = cd->m;
361 scpcd_area->fmt1 = cd->fmt;
362 scpcd_area->cssid = cd->chpid.cssid;
363 scpcd_area->first_chpid = cd->chpid.id;
364 scpcd_area->last_chpid = cd->chpid.id;
366 ccode = chsc(scpcd_area);
371 if (scpcd_area->response.code != 0x0001) {
373 CHSC_MSG(0, "scpcd: response code=%x\n",
374 scpcd_area->response.code);
377 memcpy(&cd->cpcb, &scpcd_area->response, scpcd_area->response.length);
378 if (copy_to_user(user_cd, cd, sizeof(*cd)))
384 free_page((unsigned long)scpcd_area);
388 static int chsc_ioctl_info_cu(void __user *user_cd)
390 struct chsc_cu_cd *cd;
393 struct chsc_header request;
404 struct chsc_header response;
405 u8 data[PAGE_SIZE - 20];
406 } __attribute__ ((packed)) *scucd_area;
408 scucd_area = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
411 cd = kzalloc(sizeof(*cd), GFP_KERNEL);
416 if (copy_from_user(cd, user_cd, sizeof(*cd))) {
420 scucd_area->request.length = 0x0010;
421 scucd_area->request.code = 0x0028;
422 scucd_area->m = cd->m;
423 scucd_area->fmt1 = cd->fmt;
424 scucd_area->cssid = cd->cssid;
425 scucd_area->first_cun = cd->cun;
426 scucd_area->last_cun = cd->cun;
428 ccode = chsc(scucd_area);
433 if (scucd_area->response.code != 0x0001) {
435 CHSC_MSG(0, "scucd: response code=%x\n",
436 scucd_area->response.code);
439 memcpy(&cd->cucb, &scucd_area->response, scucd_area->response.length);
440 if (copy_to_user(user_cd, cd, sizeof(*cd)))
446 free_page((unsigned long)scucd_area);
450 static int chsc_ioctl_info_sch_cu(void __user *user_cud)
452 struct chsc_sch_cud *cud;
455 struct chsc_header request;
467 struct chsc_header response;
468 u8 data[PAGE_SIZE - 20];
469 } __attribute__ ((packed)) *sscud_area;
471 sscud_area = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
474 cud = kzalloc(sizeof(*cud), GFP_KERNEL);
479 if (copy_from_user(cud, user_cud, sizeof(*cud))) {
483 sscud_area->request.length = 0x0010;
484 sscud_area->request.code = 0x0006;
485 sscud_area->m = cud->schid.m;
486 sscud_area->fmt1 = cud->fmt;
487 sscud_area->ssid = cud->schid.ssid;
488 sscud_area->first_sch = cud->schid.sch_no;
489 sscud_area->cssid = cud->schid.cssid;
490 sscud_area->last_sch = cud->schid.sch_no;
492 ccode = chsc(sscud_area);
497 if (sscud_area->response.code != 0x0001) {
499 CHSC_MSG(0, "sscud: response code=%x\n",
500 sscud_area->response.code);
503 memcpy(&cud->scub, &sscud_area->response, sscud_area->response.length);
504 if (copy_to_user(user_cud, cud, sizeof(*cud)))
510 free_page((unsigned long)sscud_area);
514 static int chsc_ioctl_conf_info(void __user *user_ci)
516 struct chsc_conf_info *ci;
519 struct chsc_header request;
529 struct chsc_header response;
530 u8 data[PAGE_SIZE - 20];
531 } __attribute__ ((packed)) *sci_area;
533 sci_area = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
536 ci = kzalloc(sizeof(*ci), GFP_KERNEL);
541 if (copy_from_user(ci, user_ci, sizeof(*ci))) {
545 sci_area->request.length = 0x0010;
546 sci_area->request.code = 0x0012;
547 sci_area->m = ci->id.m;
548 sci_area->fmt1 = ci->fmt;
549 sci_area->cssid = ci->id.cssid;
550 sci_area->ssid = ci->id.ssid;
552 ccode = chsc(sci_area);
557 if (sci_area->response.code != 0x0001) {
559 CHSC_MSG(0, "sci: response code=%x\n",
560 sci_area->response.code);
563 memcpy(&ci->scid, &sci_area->response, sci_area->response.length);
564 if (copy_to_user(user_ci, ci, sizeof(*ci)))
570 free_page((unsigned long)sci_area);
574 static int chsc_ioctl_conf_comp_list(void __user *user_ccl)
576 struct chsc_comp_list *ccl;
579 struct chsc_header request;
587 struct chsc_header response;
588 u8 data[PAGE_SIZE - 36];
589 } __attribute__ ((packed)) *sccl_area;
596 } __attribute__ ((packed)) *chpid_parm;
602 } __attribute__ ((packed)) *cssids_parm;
604 sccl_area = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
607 ccl = kzalloc(sizeof(*ccl), GFP_KERNEL);
612 if (copy_from_user(ccl, user_ccl, sizeof(*ccl))) {
616 sccl_area->request.length = 0x0020;
617 sccl_area->request.code = 0x0030;
618 sccl_area->fmt = ccl->req.fmt;
619 sccl_area->ctype = ccl->req.ctype;
620 switch (sccl_area->ctype) {
623 chpid_parm = (void *)&sccl_area->list_parm;
624 chpid_parm->m = ccl->req.chpid.m;
625 chpid_parm->cssid = ccl->req.chpid.chp.cssid;
626 chpid_parm->chpid = ccl->req.chpid.chp.id;
629 case CCL_CSS_IMG_CONF_CHAR:
630 cssids_parm = (void *)&sccl_area->list_parm;
631 cssids_parm->f_cssid = ccl->req.cssids.f_cssid;
632 cssids_parm->l_cssid = ccl->req.cssids.l_cssid;
635 ccode = chsc(sccl_area);
640 if (sccl_area->response.code != 0x0001) {
642 CHSC_MSG(0, "sccl: response code=%x\n",
643 sccl_area->response.code);
646 memcpy(&ccl->sccl, &sccl_area->response, sccl_area->response.length);
647 if (copy_to_user(user_ccl, ccl, sizeof(*ccl)))
653 free_page((unsigned long)sccl_area);
657 static int chsc_ioctl_chpd(void __user *user_chpd)
659 struct chsc_cpd_info *chpd;
662 chpd = kzalloc(sizeof(*chpd), GFP_KERNEL);
665 if (copy_from_user(chpd, user_chpd, sizeof(*chpd))) {
669 ret = chsc_determine_channel_path_desc(chpd->chpid, chpd->fmt,
670 chpd->rfmt, chpd->c, chpd->m,
674 if (copy_to_user(user_chpd, chpd, sizeof(*chpd)))
681 static int chsc_ioctl_dcal(void __user *user_dcal)
683 struct chsc_dcal *dcal;
686 struct chsc_header request;
694 struct chsc_header response;
695 u8 data[PAGE_SIZE - 36];
696 } __attribute__ ((packed)) *sdcal_area;
698 sdcal_area = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
701 dcal = kzalloc(sizeof(*dcal), GFP_KERNEL);
706 if (copy_from_user(dcal, user_dcal, sizeof(*dcal))) {
710 sdcal_area->request.length = 0x0020;
711 sdcal_area->request.code = 0x0034;
712 sdcal_area->atype = dcal->req.atype;
713 sdcal_area->fmt = dcal->req.fmt;
714 memcpy(&sdcal_area->list_parm, &dcal->req.list_parm,
715 sizeof(sdcal_area->list_parm));
717 ccode = chsc(sdcal_area);
722 if (sdcal_area->response.code != 0x0001) {
724 CHSC_MSG(0, "sdcal: response code=%x\n",
725 sdcal_area->response.code);
728 memcpy(&dcal->sdcal, &sdcal_area->response,
729 sdcal_area->response.length);
730 if (copy_to_user(user_dcal, dcal, sizeof(*dcal)))
736 free_page((unsigned long)sdcal_area);
740 static long chsc_ioctl(struct file *filp, unsigned int cmd,
743 CHSC_MSG(2, "chsc_ioctl called, cmd=%x\n", cmd);
746 return chsc_ioctl_start((void __user *)arg);
747 case CHSC_INFO_CHANNEL_PATH:
748 return chsc_ioctl_info_channel_path((void __user *)arg);
750 return chsc_ioctl_info_cu((void __user *)arg);
751 case CHSC_INFO_SCH_CU:
752 return chsc_ioctl_info_sch_cu((void __user *)arg);
754 return chsc_ioctl_conf_info((void __user *)arg);
756 return chsc_ioctl_conf_comp_list((void __user *)arg);
758 return chsc_ioctl_chpd((void __user *)arg);
760 return chsc_ioctl_dcal((void __user *)arg);
761 default: /* unknown ioctl number */
766 static const struct file_operations chsc_fops = {
767 .owner = THIS_MODULE,
768 .unlocked_ioctl = chsc_ioctl,
769 .compat_ioctl = chsc_ioctl,
772 static struct miscdevice chsc_misc_device = {
773 .minor = MISC_DYNAMIC_MINOR,
778 static int __init chsc_misc_init(void)
780 return misc_register(&chsc_misc_device);
783 static void chsc_misc_cleanup(void)
785 misc_deregister(&chsc_misc_device);
788 static int __init chsc_sch_init(void)
792 ret = chsc_init_dbfs();
795 isc_register(CHSC_SCH_ISC);
796 ret = chsc_init_sch_driver();
799 ret = chsc_misc_init();
804 chsc_cleanup_sch_driver();
806 isc_unregister(CHSC_SCH_ISC);
811 static void __exit chsc_sch_exit(void)
814 chsc_cleanup_sch_driver();
815 isc_unregister(CHSC_SCH_ISC);
819 module_init(chsc_sch_init);
820 module_exit(chsc_sch_exit);