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/compat.h>
12 #include <linux/device.h>
13 #include <linux/module.h>
14 #include <linux/uaccess.h>
15 #include <linux/miscdevice.h>
17 #include <asm/compat.h>
23 #include "cio_debug.h"
28 static debug_info_t
*chsc_debug_msg_id
;
29 static debug_info_t
*chsc_debug_log_id
;
31 #define CHSC_MSG(imp, args...) do { \
32 debug_sprintf_event(chsc_debug_msg_id, imp , ##args); \
35 #define CHSC_LOG(imp, txt) do { \
36 debug_text_event(chsc_debug_log_id, imp , txt); \
39 static void CHSC_LOG_HEX(int level
, void *data
, int length
)
42 debug_event(chsc_debug_log_id
, level
, data
, length
);
43 length
-= chsc_debug_log_id
->buf_size
;
44 data
+= chsc_debug_log_id
->buf_size
;
48 MODULE_AUTHOR("IBM Corporation");
49 MODULE_DESCRIPTION("driver for s390 chsc subchannels");
50 MODULE_LICENSE("GPL");
52 static void chsc_subchannel_irq(struct subchannel
*sch
)
54 struct chsc_private
*private = dev_get_drvdata(&sch
->dev
);
55 struct chsc_request
*request
= private->request
;
56 struct irb
*irb
= (struct irb
*)&S390_lowcore
.irb
;
59 CHSC_LOG_HEX(4, irb
, sizeof(*irb
));
60 /* Copy irb to provided request and set done. */
62 CHSC_MSG(0, "Interrupt on sch 0.%x.%04x with no request\n",
63 sch
->schid
.ssid
, sch
->schid
.sch_no
);
66 private->request
= NULL
;
67 memcpy(&request
->irb
, irb
, sizeof(*irb
));
68 cio_update_schib(sch
);
69 complete(&request
->completion
);
70 put_device(&sch
->dev
);
73 static int chsc_subchannel_probe(struct subchannel
*sch
)
75 struct chsc_private
*private;
78 CHSC_MSG(6, "Detected chsc subchannel 0.%x.%04x\n",
79 sch
->schid
.ssid
, sch
->schid
.sch_no
);
80 sch
->isc
= CHSC_SCH_ISC
;
81 private = kzalloc(sizeof(*private), GFP_KERNEL
);
84 dev_set_drvdata(&sch
->dev
, private);
85 ret
= cio_enable_subchannel(sch
, (u32
)(unsigned long)sch
);
87 CHSC_MSG(0, "Failed to enable 0.%x.%04x: %d\n",
88 sch
->schid
.ssid
, sch
->schid
.sch_no
, ret
);
89 dev_set_drvdata(&sch
->dev
, NULL
);
92 if (dev_get_uevent_suppress(&sch
->dev
)) {
93 dev_set_uevent_suppress(&sch
->dev
, 0);
94 kobject_uevent(&sch
->dev
.kobj
, KOBJ_ADD
);
100 static int chsc_subchannel_remove(struct subchannel
*sch
)
102 struct chsc_private
*private;
104 cio_disable_subchannel(sch
);
105 private = dev_get_drvdata(&sch
->dev
);
106 dev_set_drvdata(&sch
->dev
, NULL
);
107 if (private->request
) {
108 complete(&private->request
->completion
);
109 put_device(&sch
->dev
);
115 static void chsc_subchannel_shutdown(struct subchannel
*sch
)
117 cio_disable_subchannel(sch
);
120 static int chsc_subchannel_prepare(struct subchannel
*sch
)
125 * Don't allow suspend while the subchannel is not idle
126 * since we don't have a way to clear the subchannel and
127 * cannot disable it with a request running.
129 cc
= stsch_err(sch
->schid
, &schib
);
130 if (!cc
&& scsw_stctl(&schib
.scsw
))
135 static int chsc_subchannel_freeze(struct subchannel
*sch
)
137 return cio_disable_subchannel(sch
);
140 static int chsc_subchannel_restore(struct subchannel
*sch
)
142 return cio_enable_subchannel(sch
, (u32
)(unsigned long)sch
);
145 static struct css_device_id chsc_subchannel_ids
[] = {
146 { .match_flags
= 0x1, .type
=SUBCHANNEL_TYPE_CHSC
, },
147 { /* end of list */ },
149 MODULE_DEVICE_TABLE(css
, chsc_subchannel_ids
);
151 static struct css_driver chsc_subchannel_driver
= {
153 .owner
= THIS_MODULE
,
154 .name
= "chsc_subchannel",
156 .subchannel_type
= chsc_subchannel_ids
,
157 .irq
= chsc_subchannel_irq
,
158 .probe
= chsc_subchannel_probe
,
159 .remove
= chsc_subchannel_remove
,
160 .shutdown
= chsc_subchannel_shutdown
,
161 .prepare
= chsc_subchannel_prepare
,
162 .freeze
= chsc_subchannel_freeze
,
163 .thaw
= chsc_subchannel_restore
,
164 .restore
= chsc_subchannel_restore
,
167 static int __init
chsc_init_dbfs(void)
169 chsc_debug_msg_id
= debug_register("chsc_msg", 16, 1,
171 if (!chsc_debug_msg_id
)
173 debug_register_view(chsc_debug_msg_id
, &debug_sprintf_view
);
174 debug_set_level(chsc_debug_msg_id
, 2);
175 chsc_debug_log_id
= debug_register("chsc_log", 16, 1, 16);
176 if (!chsc_debug_log_id
)
178 debug_register_view(chsc_debug_log_id
, &debug_hex_ascii_view
);
179 debug_set_level(chsc_debug_log_id
, 2);
182 if (chsc_debug_msg_id
)
183 debug_unregister(chsc_debug_msg_id
);
187 static void chsc_remove_dbfs(void)
189 debug_unregister(chsc_debug_log_id
);
190 debug_unregister(chsc_debug_msg_id
);
193 static int __init
chsc_init_sch_driver(void)
195 return css_driver_register(&chsc_subchannel_driver
);
198 static void chsc_cleanup_sch_driver(void)
200 css_driver_unregister(&chsc_subchannel_driver
);
203 static DEFINE_SPINLOCK(chsc_lock
);
205 static int chsc_subchannel_match_next_free(struct device
*dev
, void *data
)
207 struct subchannel
*sch
= to_subchannel(dev
);
209 return sch
->schib
.pmcw
.ena
&& !scsw_fctl(&sch
->schib
.scsw
);
212 static struct subchannel
*chsc_get_next_subchannel(struct subchannel
*sch
)
216 dev
= driver_find_device(&chsc_subchannel_driver
.drv
,
217 sch
? &sch
->dev
: NULL
, NULL
,
218 chsc_subchannel_match_next_free
);
219 return dev
? to_subchannel(dev
) : NULL
;
223 * chsc_async() - try to start a chsc request asynchronously
224 * @chsc_area: request to be started
225 * @request: request structure to associate
227 * Tries to start a chsc request on one of the existing chsc subchannels.
229 * %0 if the request was performed synchronously
230 * %-EINPROGRESS if the request was successfully started
231 * %-EBUSY if all chsc subchannels are busy
232 * %-ENODEV if no chsc subchannels are available
234 * interrupts disabled, chsc_lock held
236 static int chsc_async(struct chsc_async_area
*chsc_area
,
237 struct chsc_request
*request
)
240 struct chsc_private
*private;
241 struct subchannel
*sch
= NULL
;
245 chsc_area
->header
.key
= PAGE_DEFAULT_KEY
>> 4;
246 while ((sch
= chsc_get_next_subchannel(sch
))) {
247 spin_lock(sch
->lock
);
248 private = dev_get_drvdata(&sch
->dev
);
249 if (private->request
) {
250 spin_unlock(sch
->lock
);
254 chsc_area
->header
.sid
= sch
->schid
;
255 CHSC_LOG(2, "schid");
256 CHSC_LOG_HEX(2, &sch
->schid
, sizeof(sch
->schid
));
257 cc
= chsc(chsc_area
);
258 sprintf(dbf
, "cc:%d", cc
);
265 sch
->schib
.scsw
.cmd
.fctl
|= SCSW_FCTL_START_FUNC
;
267 private->request
= request
;
275 spin_unlock(sch
->lock
);
276 CHSC_MSG(2, "chsc on 0.%x.%04x returned cc=%d\n",
277 sch
->schid
.ssid
, sch
->schid
.sch_no
, cc
);
278 if (ret
== -EINPROGRESS
)
280 put_device(&sch
->dev
);
287 static void chsc_log_command(struct chsc_async_area
*chsc_area
)
291 sprintf(dbf
, "CHSC:%x", chsc_area
->header
.code
);
293 CHSC_LOG_HEX(0, chsc_area
, 32);
296 static int chsc_examine_irb(struct chsc_request
*request
)
300 if (!(scsw_stctl(&request
->irb
.scsw
) & SCSW_STCTL_STATUS_PEND
))
302 backed_up
= scsw_cstat(&request
->irb
.scsw
) & SCHN_STAT_CHAIN_CHECK
;
303 request
->irb
.scsw
.cmd
.cstat
&= ~SCHN_STAT_CHAIN_CHECK
;
304 if (scsw_cstat(&request
->irb
.scsw
) == 0)
308 if (scsw_cstat(&request
->irb
.scsw
) & SCHN_STAT_PROG_CHECK
)
310 if (scsw_cstat(&request
->irb
.scsw
) & SCHN_STAT_PROT_CHECK
)
312 if (scsw_cstat(&request
->irb
.scsw
) & SCHN_STAT_CHN_DATA_CHK
)
314 if (scsw_cstat(&request
->irb
.scsw
) & SCHN_STAT_CHN_CTRL_CHK
)
319 static int chsc_ioctl_start(void __user
*user_area
)
321 struct chsc_request
*request
;
322 struct chsc_async_area
*chsc_area
;
326 if (!css_general_characteristics
.dynio
)
327 /* It makes no sense to try. */
329 chsc_area
= (void *)get_zeroed_page(GFP_DMA
| GFP_KERNEL
);
332 request
= kzalloc(sizeof(*request
), GFP_KERNEL
);
337 init_completion(&request
->completion
);
338 if (copy_from_user(chsc_area
, user_area
, PAGE_SIZE
)) {
342 chsc_log_command(chsc_area
);
343 spin_lock_irq(&chsc_lock
);
344 ret
= chsc_async(chsc_area
, request
);
345 spin_unlock_irq(&chsc_lock
);
346 if (ret
== -EINPROGRESS
) {
347 wait_for_completion(&request
->completion
);
348 ret
= chsc_examine_irb(request
);
350 /* copy area back to user */
352 if (copy_to_user(user_area
, chsc_area
, PAGE_SIZE
))
355 sprintf(dbf
, "ret:%d", ret
);
358 free_page((unsigned long)chsc_area
);
362 static int chsc_ioctl_info_channel_path(void __user
*user_cd
)
364 struct chsc_chp_cd
*cd
;
367 struct chsc_header request
;
378 struct chsc_header response
;
379 u8 data
[PAGE_SIZE
- 20];
380 } __attribute__ ((packed
)) *scpcd_area
;
382 scpcd_area
= (void *)get_zeroed_page(GFP_KERNEL
| GFP_DMA
);
385 cd
= kzalloc(sizeof(*cd
), GFP_KERNEL
);
390 if (copy_from_user(cd
, user_cd
, sizeof(*cd
))) {
394 scpcd_area
->request
.length
= 0x0010;
395 scpcd_area
->request
.code
= 0x0028;
396 scpcd_area
->m
= cd
->m
;
397 scpcd_area
->fmt1
= cd
->fmt
;
398 scpcd_area
->cssid
= cd
->chpid
.cssid
;
399 scpcd_area
->first_chpid
= cd
->chpid
.id
;
400 scpcd_area
->last_chpid
= cd
->chpid
.id
;
402 ccode
= chsc(scpcd_area
);
407 if (scpcd_area
->response
.code
!= 0x0001) {
409 CHSC_MSG(0, "scpcd: response code=%x\n",
410 scpcd_area
->response
.code
);
413 memcpy(&cd
->cpcb
, &scpcd_area
->response
, scpcd_area
->response
.length
);
414 if (copy_to_user(user_cd
, cd
, sizeof(*cd
)))
420 free_page((unsigned long)scpcd_area
);
424 static int chsc_ioctl_info_cu(void __user
*user_cd
)
426 struct chsc_cu_cd
*cd
;
429 struct chsc_header request
;
440 struct chsc_header response
;
441 u8 data
[PAGE_SIZE
- 20];
442 } __attribute__ ((packed
)) *scucd_area
;
444 scucd_area
= (void *)get_zeroed_page(GFP_KERNEL
| GFP_DMA
);
447 cd
= kzalloc(sizeof(*cd
), GFP_KERNEL
);
452 if (copy_from_user(cd
, user_cd
, sizeof(*cd
))) {
456 scucd_area
->request
.length
= 0x0010;
457 scucd_area
->request
.code
= 0x0028;
458 scucd_area
->m
= cd
->m
;
459 scucd_area
->fmt1
= cd
->fmt
;
460 scucd_area
->cssid
= cd
->cssid
;
461 scucd_area
->first_cun
= cd
->cun
;
462 scucd_area
->last_cun
= cd
->cun
;
464 ccode
= chsc(scucd_area
);
469 if (scucd_area
->response
.code
!= 0x0001) {
471 CHSC_MSG(0, "scucd: response code=%x\n",
472 scucd_area
->response
.code
);
475 memcpy(&cd
->cucb
, &scucd_area
->response
, scucd_area
->response
.length
);
476 if (copy_to_user(user_cd
, cd
, sizeof(*cd
)))
482 free_page((unsigned long)scucd_area
);
486 static int chsc_ioctl_info_sch_cu(void __user
*user_cud
)
488 struct chsc_sch_cud
*cud
;
491 struct chsc_header request
;
503 struct chsc_header response
;
504 u8 data
[PAGE_SIZE
- 20];
505 } __attribute__ ((packed
)) *sscud_area
;
507 sscud_area
= (void *)get_zeroed_page(GFP_KERNEL
| GFP_DMA
);
510 cud
= kzalloc(sizeof(*cud
), GFP_KERNEL
);
515 if (copy_from_user(cud
, user_cud
, sizeof(*cud
))) {
519 sscud_area
->request
.length
= 0x0010;
520 sscud_area
->request
.code
= 0x0006;
521 sscud_area
->m
= cud
->schid
.m
;
522 sscud_area
->fmt1
= cud
->fmt
;
523 sscud_area
->ssid
= cud
->schid
.ssid
;
524 sscud_area
->first_sch
= cud
->schid
.sch_no
;
525 sscud_area
->cssid
= cud
->schid
.cssid
;
526 sscud_area
->last_sch
= cud
->schid
.sch_no
;
528 ccode
= chsc(sscud_area
);
533 if (sscud_area
->response
.code
!= 0x0001) {
535 CHSC_MSG(0, "sscud: response code=%x\n",
536 sscud_area
->response
.code
);
539 memcpy(&cud
->scub
, &sscud_area
->response
, sscud_area
->response
.length
);
540 if (copy_to_user(user_cud
, cud
, sizeof(*cud
)))
546 free_page((unsigned long)sscud_area
);
550 static int chsc_ioctl_conf_info(void __user
*user_ci
)
552 struct chsc_conf_info
*ci
;
555 struct chsc_header request
;
565 struct chsc_header response
;
566 u8 data
[PAGE_SIZE
- 20];
567 } __attribute__ ((packed
)) *sci_area
;
569 sci_area
= (void *)get_zeroed_page(GFP_KERNEL
| GFP_DMA
);
572 ci
= kzalloc(sizeof(*ci
), GFP_KERNEL
);
577 if (copy_from_user(ci
, user_ci
, sizeof(*ci
))) {
581 sci_area
->request
.length
= 0x0010;
582 sci_area
->request
.code
= 0x0012;
583 sci_area
->m
= ci
->id
.m
;
584 sci_area
->fmt1
= ci
->fmt
;
585 sci_area
->cssid
= ci
->id
.cssid
;
586 sci_area
->ssid
= ci
->id
.ssid
;
588 ccode
= chsc(sci_area
);
593 if (sci_area
->response
.code
!= 0x0001) {
595 CHSC_MSG(0, "sci: response code=%x\n",
596 sci_area
->response
.code
);
599 memcpy(&ci
->scid
, &sci_area
->response
, sci_area
->response
.length
);
600 if (copy_to_user(user_ci
, ci
, sizeof(*ci
)))
606 free_page((unsigned long)sci_area
);
610 static int chsc_ioctl_conf_comp_list(void __user
*user_ccl
)
612 struct chsc_comp_list
*ccl
;
615 struct chsc_header request
;
623 struct chsc_header response
;
624 u8 data
[PAGE_SIZE
- 36];
625 } __attribute__ ((packed
)) *sccl_area
;
632 } __attribute__ ((packed
)) *chpid_parm
;
638 } __attribute__ ((packed
)) *cssids_parm
;
640 sccl_area
= (void *)get_zeroed_page(GFP_KERNEL
| GFP_DMA
);
643 ccl
= kzalloc(sizeof(*ccl
), GFP_KERNEL
);
648 if (copy_from_user(ccl
, user_ccl
, sizeof(*ccl
))) {
652 sccl_area
->request
.length
= 0x0020;
653 sccl_area
->request
.code
= 0x0030;
654 sccl_area
->fmt
= ccl
->req
.fmt
;
655 sccl_area
->ctype
= ccl
->req
.ctype
;
656 switch (sccl_area
->ctype
) {
659 chpid_parm
= (void *)&sccl_area
->list_parm
;
660 chpid_parm
->m
= ccl
->req
.chpid
.m
;
661 chpid_parm
->cssid
= ccl
->req
.chpid
.chp
.cssid
;
662 chpid_parm
->chpid
= ccl
->req
.chpid
.chp
.id
;
665 case CCL_CSS_IMG_CONF_CHAR
:
666 cssids_parm
= (void *)&sccl_area
->list_parm
;
667 cssids_parm
->f_cssid
= ccl
->req
.cssids
.f_cssid
;
668 cssids_parm
->l_cssid
= ccl
->req
.cssids
.l_cssid
;
671 ccode
= chsc(sccl_area
);
676 if (sccl_area
->response
.code
!= 0x0001) {
678 CHSC_MSG(0, "sccl: response code=%x\n",
679 sccl_area
->response
.code
);
682 memcpy(&ccl
->sccl
, &sccl_area
->response
, sccl_area
->response
.length
);
683 if (copy_to_user(user_ccl
, ccl
, sizeof(*ccl
)))
689 free_page((unsigned long)sccl_area
);
693 static int chsc_ioctl_chpd(void __user
*user_chpd
)
695 struct chsc_scpd
*scpd_area
;
696 struct chsc_cpd_info
*chpd
;
699 chpd
= kzalloc(sizeof(*chpd
), GFP_KERNEL
);
700 scpd_area
= (void *)get_zeroed_page(GFP_KERNEL
| GFP_DMA
);
701 if (!scpd_area
|| !chpd
) {
705 if (copy_from_user(chpd
, user_chpd
, sizeof(*chpd
))) {
709 ret
= chsc_determine_channel_path_desc(chpd
->chpid
, chpd
->fmt
,
710 chpd
->rfmt
, chpd
->c
, chpd
->m
,
714 memcpy(&chpd
->chpdb
, &scpd_area
->response
, scpd_area
->response
.length
);
715 if (copy_to_user(user_chpd
, chpd
, sizeof(*chpd
)))
719 free_page((unsigned long)scpd_area
);
723 static int chsc_ioctl_dcal(void __user
*user_dcal
)
725 struct chsc_dcal
*dcal
;
728 struct chsc_header request
;
736 struct chsc_header response
;
737 u8 data
[PAGE_SIZE
- 36];
738 } __attribute__ ((packed
)) *sdcal_area
;
740 sdcal_area
= (void *)get_zeroed_page(GFP_KERNEL
| GFP_DMA
);
743 dcal
= kzalloc(sizeof(*dcal
), GFP_KERNEL
);
748 if (copy_from_user(dcal
, user_dcal
, sizeof(*dcal
))) {
752 sdcal_area
->request
.length
= 0x0020;
753 sdcal_area
->request
.code
= 0x0034;
754 sdcal_area
->atype
= dcal
->req
.atype
;
755 sdcal_area
->fmt
= dcal
->req
.fmt
;
756 memcpy(&sdcal_area
->list_parm
, &dcal
->req
.list_parm
,
757 sizeof(sdcal_area
->list_parm
));
759 ccode
= chsc(sdcal_area
);
764 if (sdcal_area
->response
.code
!= 0x0001) {
766 CHSC_MSG(0, "sdcal: response code=%x\n",
767 sdcal_area
->response
.code
);
770 memcpy(&dcal
->sdcal
, &sdcal_area
->response
,
771 sdcal_area
->response
.length
);
772 if (copy_to_user(user_dcal
, dcal
, sizeof(*dcal
)))
778 free_page((unsigned long)sdcal_area
);
782 static long chsc_ioctl(struct file
*filp
, unsigned int cmd
,
787 CHSC_MSG(2, "chsc_ioctl called, cmd=%x\n", cmd
);
788 if (is_compat_task())
789 argp
= compat_ptr(arg
);
791 argp
= (void __user
*)arg
;
794 return chsc_ioctl_start(argp
);
795 case CHSC_INFO_CHANNEL_PATH
:
796 return chsc_ioctl_info_channel_path(argp
);
798 return chsc_ioctl_info_cu(argp
);
799 case CHSC_INFO_SCH_CU
:
800 return chsc_ioctl_info_sch_cu(argp
);
802 return chsc_ioctl_conf_info(argp
);
804 return chsc_ioctl_conf_comp_list(argp
);
806 return chsc_ioctl_chpd(argp
);
808 return chsc_ioctl_dcal(argp
);
809 default: /* unknown ioctl number */
814 static const struct file_operations chsc_fops
= {
815 .owner
= THIS_MODULE
,
816 .open
= nonseekable_open
,
817 .unlocked_ioctl
= chsc_ioctl
,
818 .compat_ioctl
= chsc_ioctl
,
822 static struct miscdevice chsc_misc_device
= {
823 .minor
= MISC_DYNAMIC_MINOR
,
828 static int __init
chsc_misc_init(void)
830 return misc_register(&chsc_misc_device
);
833 static void chsc_misc_cleanup(void)
835 misc_deregister(&chsc_misc_device
);
838 static int __init
chsc_sch_init(void)
842 ret
= chsc_init_dbfs();
845 isc_register(CHSC_SCH_ISC
);
846 ret
= chsc_init_sch_driver();
849 ret
= chsc_misc_init();
854 chsc_cleanup_sch_driver();
856 isc_unregister(CHSC_SCH_ISC
);
861 static void __exit
chsc_sch_exit(void)
864 chsc_cleanup_sch_driver();
865 isc_unregister(CHSC_SCH_ISC
);
869 module_init(chsc_sch_init
);
870 module_exit(chsc_sch_exit
);