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>
20 #include "cio_debug.h"
25 static debug_info_t
*chsc_debug_msg_id
;
26 static debug_info_t
*chsc_debug_log_id
;
28 #define CHSC_MSG(imp, args...) do { \
29 debug_sprintf_event(chsc_debug_msg_id, imp , ##args); \
32 #define CHSC_LOG(imp, txt) do { \
33 debug_text_event(chsc_debug_log_id, imp , txt); \
36 static void CHSC_LOG_HEX(int level
, void *data
, int length
)
39 debug_event(chsc_debug_log_id
, level
, data
, length
);
40 length
-= chsc_debug_log_id
->buf_size
;
41 data
+= chsc_debug_log_id
->buf_size
;
45 MODULE_AUTHOR("IBM Corporation");
46 MODULE_DESCRIPTION("driver for s390 chsc subchannels");
47 MODULE_LICENSE("GPL");
49 static void chsc_subchannel_irq(struct subchannel
*sch
)
51 struct chsc_private
*private = sch
->private;
52 struct chsc_request
*request
= private->request
;
53 struct irb
*irb
= (struct irb
*)__LC_IRB
;
56 CHSC_LOG_HEX(4, irb
, sizeof(*irb
));
57 /* Copy irb to provided request and set done. */
59 CHSC_MSG(0, "Interrupt on sch 0.%x.%04x with no request\n",
60 sch
->schid
.ssid
, sch
->schid
.sch_no
);
63 private->request
= NULL
;
64 memcpy(&request
->irb
, irb
, sizeof(*irb
));
65 cio_update_schib(sch
);
66 complete(&request
->completion
);
67 put_device(&sch
->dev
);
70 static int chsc_subchannel_probe(struct subchannel
*sch
)
72 struct chsc_private
*private;
75 CHSC_MSG(6, "Detected chsc subchannel 0.%x.%04x\n",
76 sch
->schid
.ssid
, sch
->schid
.sch_no
);
77 sch
->isc
= CHSC_SCH_ISC
;
78 private = kzalloc(sizeof(*private), GFP_KERNEL
);
81 ret
= cio_enable_subchannel(sch
, (u32
)(unsigned long)sch
);
83 CHSC_MSG(0, "Failed to enable 0.%x.%04x: %d\n",
84 sch
->schid
.ssid
, sch
->schid
.sch_no
, ret
);
87 sch
->private = private;
88 if (dev_get_uevent_suppress(&sch
->dev
)) {
89 dev_set_uevent_suppress(&sch
->dev
, 0);
90 kobject_uevent(&sch
->dev
.kobj
, KOBJ_ADD
);
96 static int chsc_subchannel_remove(struct subchannel
*sch
)
98 struct chsc_private
*private;
100 cio_disable_subchannel(sch
);
101 private = sch
->private;
103 if (private->request
) {
104 complete(&private->request
->completion
);
105 put_device(&sch
->dev
);
111 static void chsc_subchannel_shutdown(struct subchannel
*sch
)
113 cio_disable_subchannel(sch
);
116 static int chsc_subchannel_prepare(struct subchannel
*sch
)
121 * Don't allow suspend while the subchannel is not idle
122 * since we don't have a way to clear the subchannel and
123 * cannot disable it with a request running.
125 cc
= stsch(sch
->schid
, &schib
);
126 if (!cc
&& scsw_stctl(&schib
.scsw
))
131 static int chsc_subchannel_freeze(struct subchannel
*sch
)
133 return cio_disable_subchannel(sch
);
136 static int chsc_subchannel_restore(struct subchannel
*sch
)
138 return cio_enable_subchannel(sch
, (u32
)(unsigned long)sch
);
141 static struct css_device_id chsc_subchannel_ids
[] = {
142 { .match_flags
= 0x1, .type
=SUBCHANNEL_TYPE_CHSC
, },
143 { /* end of list */ },
145 MODULE_DEVICE_TABLE(css
, chsc_subchannel_ids
);
147 static struct css_driver chsc_subchannel_driver
= {
148 .owner
= THIS_MODULE
,
149 .subchannel_type
= chsc_subchannel_ids
,
150 .irq
= chsc_subchannel_irq
,
151 .probe
= chsc_subchannel_probe
,
152 .remove
= chsc_subchannel_remove
,
153 .shutdown
= chsc_subchannel_shutdown
,
154 .prepare
= chsc_subchannel_prepare
,
155 .freeze
= chsc_subchannel_freeze
,
156 .thaw
= chsc_subchannel_restore
,
157 .restore
= chsc_subchannel_restore
,
158 .name
= "chsc_subchannel",
161 static int __init
chsc_init_dbfs(void)
163 chsc_debug_msg_id
= debug_register("chsc_msg", 16, 1,
165 if (!chsc_debug_msg_id
)
167 debug_register_view(chsc_debug_msg_id
, &debug_sprintf_view
);
168 debug_set_level(chsc_debug_msg_id
, 2);
169 chsc_debug_log_id
= debug_register("chsc_log", 16, 1, 16);
170 if (!chsc_debug_log_id
)
172 debug_register_view(chsc_debug_log_id
, &debug_hex_ascii_view
);
173 debug_set_level(chsc_debug_log_id
, 2);
176 if (chsc_debug_msg_id
)
177 debug_unregister(chsc_debug_msg_id
);
181 static void chsc_remove_dbfs(void)
183 debug_unregister(chsc_debug_log_id
);
184 debug_unregister(chsc_debug_msg_id
);
187 static int __init
chsc_init_sch_driver(void)
189 return css_driver_register(&chsc_subchannel_driver
);
192 static void chsc_cleanup_sch_driver(void)
194 css_driver_unregister(&chsc_subchannel_driver
);
197 static DEFINE_SPINLOCK(chsc_lock
);
199 static int chsc_subchannel_match_next_free(struct device
*dev
, void *data
)
201 struct subchannel
*sch
= to_subchannel(dev
);
203 return sch
->schib
.pmcw
.ena
&& !scsw_fctl(&sch
->schib
.scsw
);
206 static struct subchannel
*chsc_get_next_subchannel(struct subchannel
*sch
)
210 dev
= driver_find_device(&chsc_subchannel_driver
.drv
,
211 sch
? &sch
->dev
: NULL
, NULL
,
212 chsc_subchannel_match_next_free
);
213 return dev
? to_subchannel(dev
) : NULL
;
217 * chsc_async() - try to start a chsc request asynchronously
218 * @chsc_area: request to be started
219 * @request: request structure to associate
221 * Tries to start a chsc request on one of the existing chsc subchannels.
223 * %0 if the request was performed synchronously
224 * %-EINPROGRESS if the request was successfully started
225 * %-EBUSY if all chsc subchannels are busy
226 * %-ENODEV if no chsc subchannels are available
228 * interrupts disabled, chsc_lock held
230 static int chsc_async(struct chsc_async_area
*chsc_area
,
231 struct chsc_request
*request
)
234 struct chsc_private
*private;
235 struct subchannel
*sch
= NULL
;
239 chsc_area
->header
.key
= PAGE_DEFAULT_KEY
;
240 while ((sch
= chsc_get_next_subchannel(sch
))) {
241 spin_lock(sch
->lock
);
242 private = sch
->private;
243 if (private->request
) {
244 spin_unlock(sch
->lock
);
248 chsc_area
->header
.sid
= sch
->schid
;
249 CHSC_LOG(2, "schid");
250 CHSC_LOG_HEX(2, &sch
->schid
, sizeof(sch
->schid
));
251 cc
= chsc(chsc_area
);
252 sprintf(dbf
, "cc:%d", cc
);
259 sch
->schib
.scsw
.cmd
.fctl
|= SCSW_FCTL_START_FUNC
;
261 private->request
= request
;
269 spin_unlock(sch
->lock
);
270 CHSC_MSG(2, "chsc on 0.%x.%04x returned cc=%d\n",
271 sch
->schid
.ssid
, sch
->schid
.sch_no
, cc
);
272 if (ret
== -EINPROGRESS
)
274 put_device(&sch
->dev
);
281 static void chsc_log_command(struct chsc_async_area
*chsc_area
)
285 sprintf(dbf
, "CHSC:%x", chsc_area
->header
.code
);
287 CHSC_LOG_HEX(0, chsc_area
, 32);
290 static int chsc_examine_irb(struct chsc_request
*request
)
294 if (!(scsw_stctl(&request
->irb
.scsw
) & SCSW_STCTL_STATUS_PEND
))
296 backed_up
= scsw_cstat(&request
->irb
.scsw
) & SCHN_STAT_CHAIN_CHECK
;
297 request
->irb
.scsw
.cmd
.cstat
&= ~SCHN_STAT_CHAIN_CHECK
;
298 if (scsw_cstat(&request
->irb
.scsw
) == 0)
302 if (scsw_cstat(&request
->irb
.scsw
) & SCHN_STAT_PROG_CHECK
)
304 if (scsw_cstat(&request
->irb
.scsw
) & SCHN_STAT_PROT_CHECK
)
306 if (scsw_cstat(&request
->irb
.scsw
) & SCHN_STAT_CHN_DATA_CHK
)
308 if (scsw_cstat(&request
->irb
.scsw
) & SCHN_STAT_CHN_CTRL_CHK
)
313 static int chsc_ioctl_start(void __user
*user_area
)
315 struct chsc_request
*request
;
316 struct chsc_async_area
*chsc_area
;
320 if (!css_general_characteristics
.dynio
)
321 /* It makes no sense to try. */
323 chsc_area
= (void *)get_zeroed_page(GFP_DMA
| GFP_KERNEL
);
326 request
= kzalloc(sizeof(*request
), GFP_KERNEL
);
331 init_completion(&request
->completion
);
332 if (copy_from_user(chsc_area
, user_area
, PAGE_SIZE
)) {
336 chsc_log_command(chsc_area
);
337 spin_lock_irq(&chsc_lock
);
338 ret
= chsc_async(chsc_area
, request
);
339 spin_unlock_irq(&chsc_lock
);
340 if (ret
== -EINPROGRESS
) {
341 wait_for_completion(&request
->completion
);
342 ret
= chsc_examine_irb(request
);
344 /* copy area back to user */
346 if (copy_to_user(user_area
, chsc_area
, PAGE_SIZE
))
349 sprintf(dbf
, "ret:%d", ret
);
352 free_page((unsigned long)chsc_area
);
356 static int chsc_ioctl_info_channel_path(void __user
*user_cd
)
358 struct chsc_chp_cd
*cd
;
361 struct chsc_header request
;
372 struct chsc_header response
;
373 u8 data
[PAGE_SIZE
- 20];
374 } __attribute__ ((packed
)) *scpcd_area
;
376 scpcd_area
= (void *)get_zeroed_page(GFP_KERNEL
| GFP_DMA
);
379 cd
= kzalloc(sizeof(*cd
), GFP_KERNEL
);
384 if (copy_from_user(cd
, user_cd
, sizeof(*cd
))) {
388 scpcd_area
->request
.length
= 0x0010;
389 scpcd_area
->request
.code
= 0x0028;
390 scpcd_area
->m
= cd
->m
;
391 scpcd_area
->fmt1
= cd
->fmt
;
392 scpcd_area
->cssid
= cd
->chpid
.cssid
;
393 scpcd_area
->first_chpid
= cd
->chpid
.id
;
394 scpcd_area
->last_chpid
= cd
->chpid
.id
;
396 ccode
= chsc(scpcd_area
);
401 if (scpcd_area
->response
.code
!= 0x0001) {
403 CHSC_MSG(0, "scpcd: response code=%x\n",
404 scpcd_area
->response
.code
);
407 memcpy(&cd
->cpcb
, &scpcd_area
->response
, scpcd_area
->response
.length
);
408 if (copy_to_user(user_cd
, cd
, sizeof(*cd
)))
414 free_page((unsigned long)scpcd_area
);
418 static int chsc_ioctl_info_cu(void __user
*user_cd
)
420 struct chsc_cu_cd
*cd
;
423 struct chsc_header request
;
434 struct chsc_header response
;
435 u8 data
[PAGE_SIZE
- 20];
436 } __attribute__ ((packed
)) *scucd_area
;
438 scucd_area
= (void *)get_zeroed_page(GFP_KERNEL
| GFP_DMA
);
441 cd
= kzalloc(sizeof(*cd
), GFP_KERNEL
);
446 if (copy_from_user(cd
, user_cd
, sizeof(*cd
))) {
450 scucd_area
->request
.length
= 0x0010;
451 scucd_area
->request
.code
= 0x0028;
452 scucd_area
->m
= cd
->m
;
453 scucd_area
->fmt1
= cd
->fmt
;
454 scucd_area
->cssid
= cd
->cssid
;
455 scucd_area
->first_cun
= cd
->cun
;
456 scucd_area
->last_cun
= cd
->cun
;
458 ccode
= chsc(scucd_area
);
463 if (scucd_area
->response
.code
!= 0x0001) {
465 CHSC_MSG(0, "scucd: response code=%x\n",
466 scucd_area
->response
.code
);
469 memcpy(&cd
->cucb
, &scucd_area
->response
, scucd_area
->response
.length
);
470 if (copy_to_user(user_cd
, cd
, sizeof(*cd
)))
476 free_page((unsigned long)scucd_area
);
480 static int chsc_ioctl_info_sch_cu(void __user
*user_cud
)
482 struct chsc_sch_cud
*cud
;
485 struct chsc_header request
;
497 struct chsc_header response
;
498 u8 data
[PAGE_SIZE
- 20];
499 } __attribute__ ((packed
)) *sscud_area
;
501 sscud_area
= (void *)get_zeroed_page(GFP_KERNEL
| GFP_DMA
);
504 cud
= kzalloc(sizeof(*cud
), GFP_KERNEL
);
509 if (copy_from_user(cud
, user_cud
, sizeof(*cud
))) {
513 sscud_area
->request
.length
= 0x0010;
514 sscud_area
->request
.code
= 0x0006;
515 sscud_area
->m
= cud
->schid
.m
;
516 sscud_area
->fmt1
= cud
->fmt
;
517 sscud_area
->ssid
= cud
->schid
.ssid
;
518 sscud_area
->first_sch
= cud
->schid
.sch_no
;
519 sscud_area
->cssid
= cud
->schid
.cssid
;
520 sscud_area
->last_sch
= cud
->schid
.sch_no
;
522 ccode
= chsc(sscud_area
);
527 if (sscud_area
->response
.code
!= 0x0001) {
529 CHSC_MSG(0, "sscud: response code=%x\n",
530 sscud_area
->response
.code
);
533 memcpy(&cud
->scub
, &sscud_area
->response
, sscud_area
->response
.length
);
534 if (copy_to_user(user_cud
, cud
, sizeof(*cud
)))
540 free_page((unsigned long)sscud_area
);
544 static int chsc_ioctl_conf_info(void __user
*user_ci
)
546 struct chsc_conf_info
*ci
;
549 struct chsc_header request
;
559 struct chsc_header response
;
560 u8 data
[PAGE_SIZE
- 20];
561 } __attribute__ ((packed
)) *sci_area
;
563 sci_area
= (void *)get_zeroed_page(GFP_KERNEL
| GFP_DMA
);
566 ci
= kzalloc(sizeof(*ci
), GFP_KERNEL
);
571 if (copy_from_user(ci
, user_ci
, sizeof(*ci
))) {
575 sci_area
->request
.length
= 0x0010;
576 sci_area
->request
.code
= 0x0012;
577 sci_area
->m
= ci
->id
.m
;
578 sci_area
->fmt1
= ci
->fmt
;
579 sci_area
->cssid
= ci
->id
.cssid
;
580 sci_area
->ssid
= ci
->id
.ssid
;
582 ccode
= chsc(sci_area
);
587 if (sci_area
->response
.code
!= 0x0001) {
589 CHSC_MSG(0, "sci: response code=%x\n",
590 sci_area
->response
.code
);
593 memcpy(&ci
->scid
, &sci_area
->response
, sci_area
->response
.length
);
594 if (copy_to_user(user_ci
, ci
, sizeof(*ci
)))
600 free_page((unsigned long)sci_area
);
604 static int chsc_ioctl_conf_comp_list(void __user
*user_ccl
)
606 struct chsc_comp_list
*ccl
;
609 struct chsc_header request
;
617 struct chsc_header response
;
618 u8 data
[PAGE_SIZE
- 36];
619 } __attribute__ ((packed
)) *sccl_area
;
626 } __attribute__ ((packed
)) *chpid_parm
;
632 } __attribute__ ((packed
)) *cssids_parm
;
634 sccl_area
= (void *)get_zeroed_page(GFP_KERNEL
| GFP_DMA
);
637 ccl
= kzalloc(sizeof(*ccl
), GFP_KERNEL
);
642 if (copy_from_user(ccl
, user_ccl
, sizeof(*ccl
))) {
646 sccl_area
->request
.length
= 0x0020;
647 sccl_area
->request
.code
= 0x0030;
648 sccl_area
->fmt
= ccl
->req
.fmt
;
649 sccl_area
->ctype
= ccl
->req
.ctype
;
650 switch (sccl_area
->ctype
) {
653 chpid_parm
= (void *)&sccl_area
->list_parm
;
654 chpid_parm
->m
= ccl
->req
.chpid
.m
;
655 chpid_parm
->cssid
= ccl
->req
.chpid
.chp
.cssid
;
656 chpid_parm
->chpid
= ccl
->req
.chpid
.chp
.id
;
659 case CCL_CSS_IMG_CONF_CHAR
:
660 cssids_parm
= (void *)&sccl_area
->list_parm
;
661 cssids_parm
->f_cssid
= ccl
->req
.cssids
.f_cssid
;
662 cssids_parm
->l_cssid
= ccl
->req
.cssids
.l_cssid
;
665 ccode
= chsc(sccl_area
);
670 if (sccl_area
->response
.code
!= 0x0001) {
672 CHSC_MSG(0, "sccl: response code=%x\n",
673 sccl_area
->response
.code
);
676 memcpy(&ccl
->sccl
, &sccl_area
->response
, sccl_area
->response
.length
);
677 if (copy_to_user(user_ccl
, ccl
, sizeof(*ccl
)))
683 free_page((unsigned long)sccl_area
);
687 static int chsc_ioctl_chpd(void __user
*user_chpd
)
689 struct chsc_cpd_info
*chpd
;
692 chpd
= kzalloc(sizeof(*chpd
), GFP_KERNEL
);
695 if (copy_from_user(chpd
, user_chpd
, sizeof(*chpd
))) {
699 ret
= chsc_determine_channel_path_desc(chpd
->chpid
, chpd
->fmt
,
700 chpd
->rfmt
, chpd
->c
, chpd
->m
,
704 if (copy_to_user(user_chpd
, chpd
, sizeof(*chpd
)))
711 static int chsc_ioctl_dcal(void __user
*user_dcal
)
713 struct chsc_dcal
*dcal
;
716 struct chsc_header request
;
724 struct chsc_header response
;
725 u8 data
[PAGE_SIZE
- 36];
726 } __attribute__ ((packed
)) *sdcal_area
;
728 sdcal_area
= (void *)get_zeroed_page(GFP_KERNEL
| GFP_DMA
);
731 dcal
= kzalloc(sizeof(*dcal
), GFP_KERNEL
);
736 if (copy_from_user(dcal
, user_dcal
, sizeof(*dcal
))) {
740 sdcal_area
->request
.length
= 0x0020;
741 sdcal_area
->request
.code
= 0x0034;
742 sdcal_area
->atype
= dcal
->req
.atype
;
743 sdcal_area
->fmt
= dcal
->req
.fmt
;
744 memcpy(&sdcal_area
->list_parm
, &dcal
->req
.list_parm
,
745 sizeof(sdcal_area
->list_parm
));
747 ccode
= chsc(sdcal_area
);
752 if (sdcal_area
->response
.code
!= 0x0001) {
754 CHSC_MSG(0, "sdcal: response code=%x\n",
755 sdcal_area
->response
.code
);
758 memcpy(&dcal
->sdcal
, &sdcal_area
->response
,
759 sdcal_area
->response
.length
);
760 if (copy_to_user(user_dcal
, dcal
, sizeof(*dcal
)))
766 free_page((unsigned long)sdcal_area
);
770 static long chsc_ioctl(struct file
*filp
, unsigned int cmd
,
773 CHSC_MSG(2, "chsc_ioctl called, cmd=%x\n", cmd
);
776 return chsc_ioctl_start((void __user
*)arg
);
777 case CHSC_INFO_CHANNEL_PATH
:
778 return chsc_ioctl_info_channel_path((void __user
*)arg
);
780 return chsc_ioctl_info_cu((void __user
*)arg
);
781 case CHSC_INFO_SCH_CU
:
782 return chsc_ioctl_info_sch_cu((void __user
*)arg
);
784 return chsc_ioctl_conf_info((void __user
*)arg
);
786 return chsc_ioctl_conf_comp_list((void __user
*)arg
);
788 return chsc_ioctl_chpd((void __user
*)arg
);
790 return chsc_ioctl_dcal((void __user
*)arg
);
791 default: /* unknown ioctl number */
796 static const struct file_operations chsc_fops
= {
797 .owner
= THIS_MODULE
,
798 .unlocked_ioctl
= chsc_ioctl
,
799 .compat_ioctl
= chsc_ioctl
,
802 static struct miscdevice chsc_misc_device
= {
803 .minor
= MISC_DYNAMIC_MINOR
,
808 static int __init
chsc_misc_init(void)
810 return misc_register(&chsc_misc_device
);
813 static void chsc_misc_cleanup(void)
815 misc_deregister(&chsc_misc_device
);
818 static int __init
chsc_sch_init(void)
822 ret
= chsc_init_dbfs();
825 isc_register(CHSC_SCH_ISC
);
826 ret
= chsc_init_sch_driver();
829 ret
= chsc_misc_init();
834 chsc_cleanup_sch_driver();
836 isc_unregister(CHSC_SCH_ISC
);
841 static void __exit
chsc_sch_exit(void)
844 chsc_cleanup_sch_driver();
845 isc_unregister(CHSC_SCH_ISC
);
849 module_init(chsc_sch_init
);
850 module_exit(chsc_sch_exit
);