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 = sch
->private;
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 ret
= cio_enable_subchannel(sch
, (u32
)(unsigned long)sch
);
85 CHSC_MSG(0, "Failed to enable 0.%x.%04x: %d\n",
86 sch
->schid
.ssid
, sch
->schid
.sch_no
, ret
);
89 sch
->private = private;
90 if (dev_get_uevent_suppress(&sch
->dev
)) {
91 dev_set_uevent_suppress(&sch
->dev
, 0);
92 kobject_uevent(&sch
->dev
.kobj
, KOBJ_ADD
);
98 static int chsc_subchannel_remove(struct subchannel
*sch
)
100 struct chsc_private
*private;
102 cio_disable_subchannel(sch
);
103 private = sch
->private;
105 if (private->request
) {
106 complete(&private->request
->completion
);
107 put_device(&sch
->dev
);
113 static void chsc_subchannel_shutdown(struct subchannel
*sch
)
115 cio_disable_subchannel(sch
);
118 static int chsc_subchannel_prepare(struct subchannel
*sch
)
123 * Don't allow suspend while the subchannel is not idle
124 * since we don't have a way to clear the subchannel and
125 * cannot disable it with a request running.
127 cc
= stsch_err(sch
->schid
, &schib
);
128 if (!cc
&& scsw_stctl(&schib
.scsw
))
133 static int chsc_subchannel_freeze(struct subchannel
*sch
)
135 return cio_disable_subchannel(sch
);
138 static int chsc_subchannel_restore(struct subchannel
*sch
)
140 return cio_enable_subchannel(sch
, (u32
)(unsigned long)sch
);
143 static struct css_device_id chsc_subchannel_ids
[] = {
144 { .match_flags
= 0x1, .type
=SUBCHANNEL_TYPE_CHSC
, },
145 { /* end of list */ },
147 MODULE_DEVICE_TABLE(css
, chsc_subchannel_ids
);
149 static struct css_driver chsc_subchannel_driver
= {
150 .owner
= THIS_MODULE
,
151 .subchannel_type
= chsc_subchannel_ids
,
152 .irq
= chsc_subchannel_irq
,
153 .probe
= chsc_subchannel_probe
,
154 .remove
= chsc_subchannel_remove
,
155 .shutdown
= chsc_subchannel_shutdown
,
156 .prepare
= chsc_subchannel_prepare
,
157 .freeze
= chsc_subchannel_freeze
,
158 .thaw
= chsc_subchannel_restore
,
159 .restore
= chsc_subchannel_restore
,
160 .name
= "chsc_subchannel",
163 static int __init
chsc_init_dbfs(void)
165 chsc_debug_msg_id
= debug_register("chsc_msg", 16, 1,
167 if (!chsc_debug_msg_id
)
169 debug_register_view(chsc_debug_msg_id
, &debug_sprintf_view
);
170 debug_set_level(chsc_debug_msg_id
, 2);
171 chsc_debug_log_id
= debug_register("chsc_log", 16, 1, 16);
172 if (!chsc_debug_log_id
)
174 debug_register_view(chsc_debug_log_id
, &debug_hex_ascii_view
);
175 debug_set_level(chsc_debug_log_id
, 2);
178 if (chsc_debug_msg_id
)
179 debug_unregister(chsc_debug_msg_id
);
183 static void chsc_remove_dbfs(void)
185 debug_unregister(chsc_debug_log_id
);
186 debug_unregister(chsc_debug_msg_id
);
189 static int __init
chsc_init_sch_driver(void)
191 return css_driver_register(&chsc_subchannel_driver
);
194 static void chsc_cleanup_sch_driver(void)
196 css_driver_unregister(&chsc_subchannel_driver
);
199 static DEFINE_SPINLOCK(chsc_lock
);
201 static int chsc_subchannel_match_next_free(struct device
*dev
, void *data
)
203 struct subchannel
*sch
= to_subchannel(dev
);
205 return sch
->schib
.pmcw
.ena
&& !scsw_fctl(&sch
->schib
.scsw
);
208 static struct subchannel
*chsc_get_next_subchannel(struct subchannel
*sch
)
212 dev
= driver_find_device(&chsc_subchannel_driver
.drv
,
213 sch
? &sch
->dev
: NULL
, NULL
,
214 chsc_subchannel_match_next_free
);
215 return dev
? to_subchannel(dev
) : NULL
;
219 * chsc_async() - try to start a chsc request asynchronously
220 * @chsc_area: request to be started
221 * @request: request structure to associate
223 * Tries to start a chsc request on one of the existing chsc subchannels.
225 * %0 if the request was performed synchronously
226 * %-EINPROGRESS if the request was successfully started
227 * %-EBUSY if all chsc subchannels are busy
228 * %-ENODEV if no chsc subchannels are available
230 * interrupts disabled, chsc_lock held
232 static int chsc_async(struct chsc_async_area
*chsc_area
,
233 struct chsc_request
*request
)
236 struct chsc_private
*private;
237 struct subchannel
*sch
= NULL
;
241 chsc_area
->header
.key
= PAGE_DEFAULT_KEY
>> 4;
242 while ((sch
= chsc_get_next_subchannel(sch
))) {
243 spin_lock(sch
->lock
);
244 private = sch
->private;
245 if (private->request
) {
246 spin_unlock(sch
->lock
);
250 chsc_area
->header
.sid
= sch
->schid
;
251 CHSC_LOG(2, "schid");
252 CHSC_LOG_HEX(2, &sch
->schid
, sizeof(sch
->schid
));
253 cc
= chsc(chsc_area
);
254 sprintf(dbf
, "cc:%d", cc
);
261 sch
->schib
.scsw
.cmd
.fctl
|= SCSW_FCTL_START_FUNC
;
263 private->request
= request
;
271 spin_unlock(sch
->lock
);
272 CHSC_MSG(2, "chsc on 0.%x.%04x returned cc=%d\n",
273 sch
->schid
.ssid
, sch
->schid
.sch_no
, cc
);
274 if (ret
== -EINPROGRESS
)
276 put_device(&sch
->dev
);
283 static void chsc_log_command(struct chsc_async_area
*chsc_area
)
287 sprintf(dbf
, "CHSC:%x", chsc_area
->header
.code
);
289 CHSC_LOG_HEX(0, chsc_area
, 32);
292 static int chsc_examine_irb(struct chsc_request
*request
)
296 if (!(scsw_stctl(&request
->irb
.scsw
) & SCSW_STCTL_STATUS_PEND
))
298 backed_up
= scsw_cstat(&request
->irb
.scsw
) & SCHN_STAT_CHAIN_CHECK
;
299 request
->irb
.scsw
.cmd
.cstat
&= ~SCHN_STAT_CHAIN_CHECK
;
300 if (scsw_cstat(&request
->irb
.scsw
) == 0)
304 if (scsw_cstat(&request
->irb
.scsw
) & SCHN_STAT_PROG_CHECK
)
306 if (scsw_cstat(&request
->irb
.scsw
) & SCHN_STAT_PROT_CHECK
)
308 if (scsw_cstat(&request
->irb
.scsw
) & SCHN_STAT_CHN_DATA_CHK
)
310 if (scsw_cstat(&request
->irb
.scsw
) & SCHN_STAT_CHN_CTRL_CHK
)
315 static int chsc_ioctl_start(void __user
*user_area
)
317 struct chsc_request
*request
;
318 struct chsc_async_area
*chsc_area
;
322 if (!css_general_characteristics
.dynio
)
323 /* It makes no sense to try. */
325 chsc_area
= (void *)get_zeroed_page(GFP_DMA
| GFP_KERNEL
);
328 request
= kzalloc(sizeof(*request
), GFP_KERNEL
);
333 init_completion(&request
->completion
);
334 if (copy_from_user(chsc_area
, user_area
, PAGE_SIZE
)) {
338 chsc_log_command(chsc_area
);
339 spin_lock_irq(&chsc_lock
);
340 ret
= chsc_async(chsc_area
, request
);
341 spin_unlock_irq(&chsc_lock
);
342 if (ret
== -EINPROGRESS
) {
343 wait_for_completion(&request
->completion
);
344 ret
= chsc_examine_irb(request
);
346 /* copy area back to user */
348 if (copy_to_user(user_area
, chsc_area
, PAGE_SIZE
))
351 sprintf(dbf
, "ret:%d", ret
);
354 free_page((unsigned long)chsc_area
);
358 static int chsc_ioctl_info_channel_path(void __user
*user_cd
)
360 struct chsc_chp_cd
*cd
;
363 struct chsc_header request
;
374 struct chsc_header response
;
375 u8 data
[PAGE_SIZE
- 20];
376 } __attribute__ ((packed
)) *scpcd_area
;
378 scpcd_area
= (void *)get_zeroed_page(GFP_KERNEL
| GFP_DMA
);
381 cd
= kzalloc(sizeof(*cd
), GFP_KERNEL
);
386 if (copy_from_user(cd
, user_cd
, sizeof(*cd
))) {
390 scpcd_area
->request
.length
= 0x0010;
391 scpcd_area
->request
.code
= 0x0028;
392 scpcd_area
->m
= cd
->m
;
393 scpcd_area
->fmt1
= cd
->fmt
;
394 scpcd_area
->cssid
= cd
->chpid
.cssid
;
395 scpcd_area
->first_chpid
= cd
->chpid
.id
;
396 scpcd_area
->last_chpid
= cd
->chpid
.id
;
398 ccode
= chsc(scpcd_area
);
403 if (scpcd_area
->response
.code
!= 0x0001) {
405 CHSC_MSG(0, "scpcd: response code=%x\n",
406 scpcd_area
->response
.code
);
409 memcpy(&cd
->cpcb
, &scpcd_area
->response
, scpcd_area
->response
.length
);
410 if (copy_to_user(user_cd
, cd
, sizeof(*cd
)))
416 free_page((unsigned long)scpcd_area
);
420 static int chsc_ioctl_info_cu(void __user
*user_cd
)
422 struct chsc_cu_cd
*cd
;
425 struct chsc_header request
;
436 struct chsc_header response
;
437 u8 data
[PAGE_SIZE
- 20];
438 } __attribute__ ((packed
)) *scucd_area
;
440 scucd_area
= (void *)get_zeroed_page(GFP_KERNEL
| GFP_DMA
);
443 cd
= kzalloc(sizeof(*cd
), GFP_KERNEL
);
448 if (copy_from_user(cd
, user_cd
, sizeof(*cd
))) {
452 scucd_area
->request
.length
= 0x0010;
453 scucd_area
->request
.code
= 0x0028;
454 scucd_area
->m
= cd
->m
;
455 scucd_area
->fmt1
= cd
->fmt
;
456 scucd_area
->cssid
= cd
->cssid
;
457 scucd_area
->first_cun
= cd
->cun
;
458 scucd_area
->last_cun
= cd
->cun
;
460 ccode
= chsc(scucd_area
);
465 if (scucd_area
->response
.code
!= 0x0001) {
467 CHSC_MSG(0, "scucd: response code=%x\n",
468 scucd_area
->response
.code
);
471 memcpy(&cd
->cucb
, &scucd_area
->response
, scucd_area
->response
.length
);
472 if (copy_to_user(user_cd
, cd
, sizeof(*cd
)))
478 free_page((unsigned long)scucd_area
);
482 static int chsc_ioctl_info_sch_cu(void __user
*user_cud
)
484 struct chsc_sch_cud
*cud
;
487 struct chsc_header request
;
499 struct chsc_header response
;
500 u8 data
[PAGE_SIZE
- 20];
501 } __attribute__ ((packed
)) *sscud_area
;
503 sscud_area
= (void *)get_zeroed_page(GFP_KERNEL
| GFP_DMA
);
506 cud
= kzalloc(sizeof(*cud
), GFP_KERNEL
);
511 if (copy_from_user(cud
, user_cud
, sizeof(*cud
))) {
515 sscud_area
->request
.length
= 0x0010;
516 sscud_area
->request
.code
= 0x0006;
517 sscud_area
->m
= cud
->schid
.m
;
518 sscud_area
->fmt1
= cud
->fmt
;
519 sscud_area
->ssid
= cud
->schid
.ssid
;
520 sscud_area
->first_sch
= cud
->schid
.sch_no
;
521 sscud_area
->cssid
= cud
->schid
.cssid
;
522 sscud_area
->last_sch
= cud
->schid
.sch_no
;
524 ccode
= chsc(sscud_area
);
529 if (sscud_area
->response
.code
!= 0x0001) {
531 CHSC_MSG(0, "sscud: response code=%x\n",
532 sscud_area
->response
.code
);
535 memcpy(&cud
->scub
, &sscud_area
->response
, sscud_area
->response
.length
);
536 if (copy_to_user(user_cud
, cud
, sizeof(*cud
)))
542 free_page((unsigned long)sscud_area
);
546 static int chsc_ioctl_conf_info(void __user
*user_ci
)
548 struct chsc_conf_info
*ci
;
551 struct chsc_header request
;
561 struct chsc_header response
;
562 u8 data
[PAGE_SIZE
- 20];
563 } __attribute__ ((packed
)) *sci_area
;
565 sci_area
= (void *)get_zeroed_page(GFP_KERNEL
| GFP_DMA
);
568 ci
= kzalloc(sizeof(*ci
), GFP_KERNEL
);
573 if (copy_from_user(ci
, user_ci
, sizeof(*ci
))) {
577 sci_area
->request
.length
= 0x0010;
578 sci_area
->request
.code
= 0x0012;
579 sci_area
->m
= ci
->id
.m
;
580 sci_area
->fmt1
= ci
->fmt
;
581 sci_area
->cssid
= ci
->id
.cssid
;
582 sci_area
->ssid
= ci
->id
.ssid
;
584 ccode
= chsc(sci_area
);
589 if (sci_area
->response
.code
!= 0x0001) {
591 CHSC_MSG(0, "sci: response code=%x\n",
592 sci_area
->response
.code
);
595 memcpy(&ci
->scid
, &sci_area
->response
, sci_area
->response
.length
);
596 if (copy_to_user(user_ci
, ci
, sizeof(*ci
)))
602 free_page((unsigned long)sci_area
);
606 static int chsc_ioctl_conf_comp_list(void __user
*user_ccl
)
608 struct chsc_comp_list
*ccl
;
611 struct chsc_header request
;
619 struct chsc_header response
;
620 u8 data
[PAGE_SIZE
- 36];
621 } __attribute__ ((packed
)) *sccl_area
;
628 } __attribute__ ((packed
)) *chpid_parm
;
634 } __attribute__ ((packed
)) *cssids_parm
;
636 sccl_area
= (void *)get_zeroed_page(GFP_KERNEL
| GFP_DMA
);
639 ccl
= kzalloc(sizeof(*ccl
), GFP_KERNEL
);
644 if (copy_from_user(ccl
, user_ccl
, sizeof(*ccl
))) {
648 sccl_area
->request
.length
= 0x0020;
649 sccl_area
->request
.code
= 0x0030;
650 sccl_area
->fmt
= ccl
->req
.fmt
;
651 sccl_area
->ctype
= ccl
->req
.ctype
;
652 switch (sccl_area
->ctype
) {
655 chpid_parm
= (void *)&sccl_area
->list_parm
;
656 chpid_parm
->m
= ccl
->req
.chpid
.m
;
657 chpid_parm
->cssid
= ccl
->req
.chpid
.chp
.cssid
;
658 chpid_parm
->chpid
= ccl
->req
.chpid
.chp
.id
;
661 case CCL_CSS_IMG_CONF_CHAR
:
662 cssids_parm
= (void *)&sccl_area
->list_parm
;
663 cssids_parm
->f_cssid
= ccl
->req
.cssids
.f_cssid
;
664 cssids_parm
->l_cssid
= ccl
->req
.cssids
.l_cssid
;
667 ccode
= chsc(sccl_area
);
672 if (sccl_area
->response
.code
!= 0x0001) {
674 CHSC_MSG(0, "sccl: response code=%x\n",
675 sccl_area
->response
.code
);
678 memcpy(&ccl
->sccl
, &sccl_area
->response
, sccl_area
->response
.length
);
679 if (copy_to_user(user_ccl
, ccl
, sizeof(*ccl
)))
685 free_page((unsigned long)sccl_area
);
689 static int chsc_ioctl_chpd(void __user
*user_chpd
)
691 struct chsc_cpd_info
*chpd
;
694 chpd
= kzalloc(sizeof(*chpd
), GFP_KERNEL
);
697 if (copy_from_user(chpd
, user_chpd
, sizeof(*chpd
))) {
701 ret
= chsc_determine_channel_path_desc(chpd
->chpid
, chpd
->fmt
,
702 chpd
->rfmt
, chpd
->c
, chpd
->m
,
706 if (copy_to_user(user_chpd
, chpd
, sizeof(*chpd
)))
713 static int chsc_ioctl_dcal(void __user
*user_dcal
)
715 struct chsc_dcal
*dcal
;
718 struct chsc_header request
;
726 struct chsc_header response
;
727 u8 data
[PAGE_SIZE
- 36];
728 } __attribute__ ((packed
)) *sdcal_area
;
730 sdcal_area
= (void *)get_zeroed_page(GFP_KERNEL
| GFP_DMA
);
733 dcal
= kzalloc(sizeof(*dcal
), GFP_KERNEL
);
738 if (copy_from_user(dcal
, user_dcal
, sizeof(*dcal
))) {
742 sdcal_area
->request
.length
= 0x0020;
743 sdcal_area
->request
.code
= 0x0034;
744 sdcal_area
->atype
= dcal
->req
.atype
;
745 sdcal_area
->fmt
= dcal
->req
.fmt
;
746 memcpy(&sdcal_area
->list_parm
, &dcal
->req
.list_parm
,
747 sizeof(sdcal_area
->list_parm
));
749 ccode
= chsc(sdcal_area
);
754 if (sdcal_area
->response
.code
!= 0x0001) {
756 CHSC_MSG(0, "sdcal: response code=%x\n",
757 sdcal_area
->response
.code
);
760 memcpy(&dcal
->sdcal
, &sdcal_area
->response
,
761 sdcal_area
->response
.length
);
762 if (copy_to_user(user_dcal
, dcal
, sizeof(*dcal
)))
768 free_page((unsigned long)sdcal_area
);
772 static long chsc_ioctl(struct file
*filp
, unsigned int cmd
,
777 CHSC_MSG(2, "chsc_ioctl called, cmd=%x\n", cmd
);
778 if (is_compat_task())
779 argp
= compat_ptr(arg
);
781 argp
= (void __user
*)arg
;
784 return chsc_ioctl_start(argp
);
785 case CHSC_INFO_CHANNEL_PATH
:
786 return chsc_ioctl_info_channel_path(argp
);
788 return chsc_ioctl_info_cu(argp
);
789 case CHSC_INFO_SCH_CU
:
790 return chsc_ioctl_info_sch_cu(argp
);
792 return chsc_ioctl_conf_info(argp
);
794 return chsc_ioctl_conf_comp_list(argp
);
796 return chsc_ioctl_chpd(argp
);
798 return chsc_ioctl_dcal(argp
);
799 default: /* unknown ioctl number */
804 static const struct file_operations chsc_fops
= {
805 .owner
= THIS_MODULE
,
806 .open
= nonseekable_open
,
807 .unlocked_ioctl
= chsc_ioctl
,
808 .compat_ioctl
= chsc_ioctl
,
811 static struct miscdevice chsc_misc_device
= {
812 .minor
= MISC_DYNAMIC_MINOR
,
817 static int __init
chsc_misc_init(void)
819 return misc_register(&chsc_misc_device
);
822 static void chsc_misc_cleanup(void)
824 misc_deregister(&chsc_misc_device
);
827 static int __init
chsc_sch_init(void)
831 ret
= chsc_init_dbfs();
834 isc_register(CHSC_SCH_ISC
);
835 ret
= chsc_init_sch_driver();
838 ret
= chsc_misc_init();
843 chsc_cleanup_sch_driver();
845 isc_unregister(CHSC_SCH_ISC
);
850 static void __exit
chsc_sch_exit(void)
853 chsc_cleanup_sch_driver();
854 isc_unregister(CHSC_SCH_ISC
);
858 module_init(chsc_sch_init
);
859 module_exit(chsc_sch_exit
);