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/device.h>
11 #include <linux/module.h>
12 #include <linux/uaccess.h>
13 #include <linux/miscdevice.h>
15 #include <asm/compat.h>
21 #include "cio_debug.h"
26 static debug_info_t *chsc_debug_msg_id;
27 static debug_info_t *chsc_debug_log_id;
29 #define CHSC_MSG(imp, args...) do { \
30 debug_sprintf_event(chsc_debug_msg_id, imp , ##args); \
33 #define CHSC_LOG(imp, txt) do { \
34 debug_text_event(chsc_debug_log_id, imp , txt); \
37 static void CHSC_LOG_HEX(int level, void *data, int length)
40 debug_event(chsc_debug_log_id, level, data, length);
41 length -= chsc_debug_log_id->buf_size;
42 data += chsc_debug_log_id->buf_size;
46 MODULE_AUTHOR("IBM Corporation");
47 MODULE_DESCRIPTION("driver for s390 chsc subchannels");
48 MODULE_LICENSE("GPL");
50 static void chsc_subchannel_irq(struct subchannel *sch)
52 struct chsc_private *private = sch->private;
53 struct chsc_request *request = private->request;
54 struct irb *irb = (struct irb *)__LC_IRB;
57 CHSC_LOG_HEX(4, irb, sizeof(*irb));
58 /* Copy irb to provided request and set done. */
60 CHSC_MSG(0, "Interrupt on sch 0.%x.%04x with no request\n",
61 sch->schid.ssid, sch->schid.sch_no);
64 private->request = NULL;
65 memcpy(&request->irb, irb, sizeof(*irb));
66 cio_update_schib(sch);
67 complete(&request->completion);
68 put_device(&sch->dev);
71 static int chsc_subchannel_probe(struct subchannel *sch)
73 struct chsc_private *private;
76 CHSC_MSG(6, "Detected chsc subchannel 0.%x.%04x\n",
77 sch->schid.ssid, sch->schid.sch_no);
78 sch->isc = CHSC_SCH_ISC;
79 private = kzalloc(sizeof(*private), GFP_KERNEL);
82 ret = cio_enable_subchannel(sch, (u32)(unsigned long)sch);
84 CHSC_MSG(0, "Failed to enable 0.%x.%04x: %d\n",
85 sch->schid.ssid, sch->schid.sch_no, ret);
88 sch->private = private;
89 if (dev_get_uevent_suppress(&sch->dev)) {
90 dev_set_uevent_suppress(&sch->dev, 0);
91 kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
97 static int chsc_subchannel_remove(struct subchannel *sch)
99 struct chsc_private *private;
101 cio_disable_subchannel(sch);
102 private = sch->private;
104 if (private->request) {
105 complete(&private->request->completion);
106 put_device(&sch->dev);
112 static void chsc_subchannel_shutdown(struct subchannel *sch)
114 cio_disable_subchannel(sch);
117 static int chsc_subchannel_prepare(struct subchannel *sch)
122 * Don't allow suspend while the subchannel is not idle
123 * since we don't have a way to clear the subchannel and
124 * cannot disable it with a request running.
126 cc = stsch(sch->schid, &schib);
127 if (!cc && scsw_stctl(&schib.scsw))
132 static int chsc_subchannel_freeze(struct subchannel *sch)
134 return cio_disable_subchannel(sch);
137 static int chsc_subchannel_restore(struct subchannel *sch)
139 return cio_enable_subchannel(sch, (u32)(unsigned long)sch);
142 static struct css_device_id chsc_subchannel_ids[] = {
143 { .match_flags = 0x1, .type =SUBCHANNEL_TYPE_CHSC, },
144 { /* end of list */ },
146 MODULE_DEVICE_TABLE(css, chsc_subchannel_ids);
148 static struct css_driver chsc_subchannel_driver = {
149 .owner = THIS_MODULE,
150 .subchannel_type = chsc_subchannel_ids,
151 .irq = chsc_subchannel_irq,
152 .probe = chsc_subchannel_probe,
153 .remove = chsc_subchannel_remove,
154 .shutdown = chsc_subchannel_shutdown,
155 .prepare = chsc_subchannel_prepare,
156 .freeze = chsc_subchannel_freeze,
157 .thaw = chsc_subchannel_restore,
158 .restore = chsc_subchannel_restore,
159 .name = "chsc_subchannel",
162 static int __init chsc_init_dbfs(void)
164 chsc_debug_msg_id = debug_register("chsc_msg", 16, 1,
166 if (!chsc_debug_msg_id)
168 debug_register_view(chsc_debug_msg_id, &debug_sprintf_view);
169 debug_set_level(chsc_debug_msg_id, 2);
170 chsc_debug_log_id = debug_register("chsc_log", 16, 1, 16);
171 if (!chsc_debug_log_id)
173 debug_register_view(chsc_debug_log_id, &debug_hex_ascii_view);
174 debug_set_level(chsc_debug_log_id, 2);
177 if (chsc_debug_msg_id)
178 debug_unregister(chsc_debug_msg_id);
182 static void chsc_remove_dbfs(void)
184 debug_unregister(chsc_debug_log_id);
185 debug_unregister(chsc_debug_msg_id);
188 static int __init chsc_init_sch_driver(void)
190 return css_driver_register(&chsc_subchannel_driver);
193 static void chsc_cleanup_sch_driver(void)
195 css_driver_unregister(&chsc_subchannel_driver);
198 static DEFINE_SPINLOCK(chsc_lock);
200 static int chsc_subchannel_match_next_free(struct device *dev, void *data)
202 struct subchannel *sch = to_subchannel(dev);
204 return sch->schib.pmcw.ena && !scsw_fctl(&sch->schib.scsw);
207 static struct subchannel *chsc_get_next_subchannel(struct subchannel *sch)
211 dev = driver_find_device(&chsc_subchannel_driver.drv,
212 sch ? &sch->dev : NULL, NULL,
213 chsc_subchannel_match_next_free);
214 return dev ? to_subchannel(dev) : NULL;
218 * chsc_async() - try to start a chsc request asynchronously
219 * @chsc_area: request to be started
220 * @request: request structure to associate
222 * Tries to start a chsc request on one of the existing chsc subchannels.
224 * %0 if the request was performed synchronously
225 * %-EINPROGRESS if the request was successfully started
226 * %-EBUSY if all chsc subchannels are busy
227 * %-ENODEV if no chsc subchannels are available
229 * interrupts disabled, chsc_lock held
231 static int chsc_async(struct chsc_async_area *chsc_area,
232 struct chsc_request *request)
235 struct chsc_private *private;
236 struct subchannel *sch = NULL;
240 chsc_area->header.key = PAGE_DEFAULT_KEY;
241 while ((sch = chsc_get_next_subchannel(sch))) {
242 spin_lock(sch->lock);
243 private = sch->private;
244 if (private->request) {
245 spin_unlock(sch->lock);
249 chsc_area->header.sid = sch->schid;
250 CHSC_LOG(2, "schid");
251 CHSC_LOG_HEX(2, &sch->schid, sizeof(sch->schid));
252 cc = chsc(chsc_area);
253 sprintf(dbf, "cc:%d", cc);
260 sch->schib.scsw.cmd.fctl |= SCSW_FCTL_START_FUNC;
262 private->request = request;
270 spin_unlock(sch->lock);
271 CHSC_MSG(2, "chsc on 0.%x.%04x returned cc=%d\n",
272 sch->schid.ssid, sch->schid.sch_no, cc);
273 if (ret == -EINPROGRESS)
275 put_device(&sch->dev);
282 static void chsc_log_command(struct chsc_async_area *chsc_area)
286 sprintf(dbf, "CHSC:%x", chsc_area->header.code);
288 CHSC_LOG_HEX(0, chsc_area, 32);
291 static int chsc_examine_irb(struct chsc_request *request)
295 if (!(scsw_stctl(&request->irb.scsw) & SCSW_STCTL_STATUS_PEND))
297 backed_up = scsw_cstat(&request->irb.scsw) & SCHN_STAT_CHAIN_CHECK;
298 request->irb.scsw.cmd.cstat &= ~SCHN_STAT_CHAIN_CHECK;
299 if (scsw_cstat(&request->irb.scsw) == 0)
303 if (scsw_cstat(&request->irb.scsw) & SCHN_STAT_PROG_CHECK)
305 if (scsw_cstat(&request->irb.scsw) & SCHN_STAT_PROT_CHECK)
307 if (scsw_cstat(&request->irb.scsw) & SCHN_STAT_CHN_DATA_CHK)
309 if (scsw_cstat(&request->irb.scsw) & SCHN_STAT_CHN_CTRL_CHK)
314 static int chsc_ioctl_start(void __user *user_area)
316 struct chsc_request *request;
317 struct chsc_async_area *chsc_area;
321 if (!css_general_characteristics.dynio)
322 /* It makes no sense to try. */
324 chsc_area = (void *)get_zeroed_page(GFP_DMA | GFP_KERNEL);
327 request = kzalloc(sizeof(*request), GFP_KERNEL);
332 init_completion(&request->completion);
333 if (copy_from_user(chsc_area, user_area, PAGE_SIZE)) {
337 chsc_log_command(chsc_area);
338 spin_lock_irq(&chsc_lock);
339 ret = chsc_async(chsc_area, request);
340 spin_unlock_irq(&chsc_lock);
341 if (ret == -EINPROGRESS) {
342 wait_for_completion(&request->completion);
343 ret = chsc_examine_irb(request);
345 /* copy area back to user */
347 if (copy_to_user(user_area, chsc_area, PAGE_SIZE))
350 sprintf(dbf, "ret:%d", ret);
353 free_page((unsigned long)chsc_area);
357 static int chsc_ioctl_info_channel_path(void __user *user_cd)
359 struct chsc_chp_cd *cd;
362 struct chsc_header request;
373 struct chsc_header response;
374 u8 data[PAGE_SIZE - 20];
375 } __attribute__ ((packed)) *scpcd_area;
377 scpcd_area = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
380 cd = kzalloc(sizeof(*cd), GFP_KERNEL);
385 if (copy_from_user(cd, user_cd, sizeof(*cd))) {
389 scpcd_area->request.length = 0x0010;
390 scpcd_area->request.code = 0x0028;
391 scpcd_area->m = cd->m;
392 scpcd_area->fmt1 = cd->fmt;
393 scpcd_area->cssid = cd->chpid.cssid;
394 scpcd_area->first_chpid = cd->chpid.id;
395 scpcd_area->last_chpid = cd->chpid.id;
397 ccode = chsc(scpcd_area);
402 if (scpcd_area->response.code != 0x0001) {
404 CHSC_MSG(0, "scpcd: response code=%x\n",
405 scpcd_area->response.code);
408 memcpy(&cd->cpcb, &scpcd_area->response, scpcd_area->response.length);
409 if (copy_to_user(user_cd, cd, sizeof(*cd)))
415 free_page((unsigned long)scpcd_area);
419 static int chsc_ioctl_info_cu(void __user *user_cd)
421 struct chsc_cu_cd *cd;
424 struct chsc_header request;
435 struct chsc_header response;
436 u8 data[PAGE_SIZE - 20];
437 } __attribute__ ((packed)) *scucd_area;
439 scucd_area = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
442 cd = kzalloc(sizeof(*cd), GFP_KERNEL);
447 if (copy_from_user(cd, user_cd, sizeof(*cd))) {
451 scucd_area->request.length = 0x0010;
452 scucd_area->request.code = 0x0028;
453 scucd_area->m = cd->m;
454 scucd_area->fmt1 = cd->fmt;
455 scucd_area->cssid = cd->cssid;
456 scucd_area->first_cun = cd->cun;
457 scucd_area->last_cun = cd->cun;
459 ccode = chsc(scucd_area);
464 if (scucd_area->response.code != 0x0001) {
466 CHSC_MSG(0, "scucd: response code=%x\n",
467 scucd_area->response.code);
470 memcpy(&cd->cucb, &scucd_area->response, scucd_area->response.length);
471 if (copy_to_user(user_cd, cd, sizeof(*cd)))
477 free_page((unsigned long)scucd_area);
481 static int chsc_ioctl_info_sch_cu(void __user *user_cud)
483 struct chsc_sch_cud *cud;
486 struct chsc_header request;
498 struct chsc_header response;
499 u8 data[PAGE_SIZE - 20];
500 } __attribute__ ((packed)) *sscud_area;
502 sscud_area = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
505 cud = kzalloc(sizeof(*cud), GFP_KERNEL);
510 if (copy_from_user(cud, user_cud, sizeof(*cud))) {
514 sscud_area->request.length = 0x0010;
515 sscud_area->request.code = 0x0006;
516 sscud_area->m = cud->schid.m;
517 sscud_area->fmt1 = cud->fmt;
518 sscud_area->ssid = cud->schid.ssid;
519 sscud_area->first_sch = cud->schid.sch_no;
520 sscud_area->cssid = cud->schid.cssid;
521 sscud_area->last_sch = cud->schid.sch_no;
523 ccode = chsc(sscud_area);
528 if (sscud_area->response.code != 0x0001) {
530 CHSC_MSG(0, "sscud: response code=%x\n",
531 sscud_area->response.code);
534 memcpy(&cud->scub, &sscud_area->response, sscud_area->response.length);
535 if (copy_to_user(user_cud, cud, sizeof(*cud)))
541 free_page((unsigned long)sscud_area);
545 static int chsc_ioctl_conf_info(void __user *user_ci)
547 struct chsc_conf_info *ci;
550 struct chsc_header request;
560 struct chsc_header response;
561 u8 data[PAGE_SIZE - 20];
562 } __attribute__ ((packed)) *sci_area;
564 sci_area = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
567 ci = kzalloc(sizeof(*ci), GFP_KERNEL);
572 if (copy_from_user(ci, user_ci, sizeof(*ci))) {
576 sci_area->request.length = 0x0010;
577 sci_area->request.code = 0x0012;
578 sci_area->m = ci->id.m;
579 sci_area->fmt1 = ci->fmt;
580 sci_area->cssid = ci->id.cssid;
581 sci_area->ssid = ci->id.ssid;
583 ccode = chsc(sci_area);
588 if (sci_area->response.code != 0x0001) {
590 CHSC_MSG(0, "sci: response code=%x\n",
591 sci_area->response.code);
594 memcpy(&ci->scid, &sci_area->response, sci_area->response.length);
595 if (copy_to_user(user_ci, ci, sizeof(*ci)))
601 free_page((unsigned long)sci_area);
605 static int chsc_ioctl_conf_comp_list(void __user *user_ccl)
607 struct chsc_comp_list *ccl;
610 struct chsc_header request;
618 struct chsc_header response;
619 u8 data[PAGE_SIZE - 36];
620 } __attribute__ ((packed)) *sccl_area;
627 } __attribute__ ((packed)) *chpid_parm;
633 } __attribute__ ((packed)) *cssids_parm;
635 sccl_area = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
638 ccl = kzalloc(sizeof(*ccl), GFP_KERNEL);
643 if (copy_from_user(ccl, user_ccl, sizeof(*ccl))) {
647 sccl_area->request.length = 0x0020;
648 sccl_area->request.code = 0x0030;
649 sccl_area->fmt = ccl->req.fmt;
650 sccl_area->ctype = ccl->req.ctype;
651 switch (sccl_area->ctype) {
654 chpid_parm = (void *)&sccl_area->list_parm;
655 chpid_parm->m = ccl->req.chpid.m;
656 chpid_parm->cssid = ccl->req.chpid.chp.cssid;
657 chpid_parm->chpid = ccl->req.chpid.chp.id;
660 case CCL_CSS_IMG_CONF_CHAR:
661 cssids_parm = (void *)&sccl_area->list_parm;
662 cssids_parm->f_cssid = ccl->req.cssids.f_cssid;
663 cssids_parm->l_cssid = ccl->req.cssids.l_cssid;
666 ccode = chsc(sccl_area);
671 if (sccl_area->response.code != 0x0001) {
673 CHSC_MSG(0, "sccl: response code=%x\n",
674 sccl_area->response.code);
677 memcpy(&ccl->sccl, &sccl_area->response, sccl_area->response.length);
678 if (copy_to_user(user_ccl, ccl, sizeof(*ccl)))
684 free_page((unsigned long)sccl_area);
688 static int chsc_ioctl_chpd(void __user *user_chpd)
690 struct chsc_cpd_info *chpd;
693 chpd = kzalloc(sizeof(*chpd), GFP_KERNEL);
696 if (copy_from_user(chpd, user_chpd, sizeof(*chpd))) {
700 ret = chsc_determine_channel_path_desc(chpd->chpid, chpd->fmt,
701 chpd->rfmt, chpd->c, chpd->m,
705 if (copy_to_user(user_chpd, chpd, sizeof(*chpd)))
712 static int chsc_ioctl_dcal(void __user *user_dcal)
714 struct chsc_dcal *dcal;
717 struct chsc_header request;
725 struct chsc_header response;
726 u8 data[PAGE_SIZE - 36];
727 } __attribute__ ((packed)) *sdcal_area;
729 sdcal_area = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
732 dcal = kzalloc(sizeof(*dcal), GFP_KERNEL);
737 if (copy_from_user(dcal, user_dcal, sizeof(*dcal))) {
741 sdcal_area->request.length = 0x0020;
742 sdcal_area->request.code = 0x0034;
743 sdcal_area->atype = dcal->req.atype;
744 sdcal_area->fmt = dcal->req.fmt;
745 memcpy(&sdcal_area->list_parm, &dcal->req.list_parm,
746 sizeof(sdcal_area->list_parm));
748 ccode = chsc(sdcal_area);
753 if (sdcal_area->response.code != 0x0001) {
755 CHSC_MSG(0, "sdcal: response code=%x\n",
756 sdcal_area->response.code);
759 memcpy(&dcal->sdcal, &sdcal_area->response,
760 sdcal_area->response.length);
761 if (copy_to_user(user_dcal, dcal, sizeof(*dcal)))
767 free_page((unsigned long)sdcal_area);
771 static long chsc_ioctl(struct file *filp, unsigned int cmd,
776 CHSC_MSG(2, "chsc_ioctl called, cmd=%x\n", cmd);
777 if (is_compat_task())
778 argp = compat_ptr(arg);
780 argp = (void __user *)arg;
783 return chsc_ioctl_start(argp);
784 case CHSC_INFO_CHANNEL_PATH:
785 return chsc_ioctl_info_channel_path(argp);
787 return chsc_ioctl_info_cu(argp);
788 case CHSC_INFO_SCH_CU:
789 return chsc_ioctl_info_sch_cu(argp);
791 return chsc_ioctl_conf_info(argp);
793 return chsc_ioctl_conf_comp_list(argp);
795 return chsc_ioctl_chpd(argp);
797 return chsc_ioctl_dcal(argp);
798 default: /* unknown ioctl number */
803 static const struct file_operations chsc_fops = {
804 .owner = THIS_MODULE,
805 .unlocked_ioctl = chsc_ioctl,
806 .compat_ioctl = chsc_ioctl,
809 static struct miscdevice chsc_misc_device = {
810 .minor = MISC_DYNAMIC_MINOR,
815 static int __init chsc_misc_init(void)
817 return misc_register(&chsc_misc_device);
820 static void chsc_misc_cleanup(void)
822 misc_deregister(&chsc_misc_device);
825 static int __init chsc_sch_init(void)
829 ret = chsc_init_dbfs();
832 isc_register(CHSC_SCH_ISC);
833 ret = chsc_init_sch_driver();
836 ret = chsc_misc_init();
841 chsc_cleanup_sch_driver();
843 isc_unregister(CHSC_SCH_ISC);
848 static void __exit chsc_sch_exit(void)
851 chsc_cleanup_sch_driver();
852 isc_unregister(CHSC_SCH_ISC);
856 module_init(chsc_sch_init);
857 module_exit(chsc_sch_exit);