xref: /openbmc/linux/drivers/s390/cio/chsc_sch.c (revision 7ae9fb1b7ecbb5d85d07857943f677fd1a559b18)
1724117b7SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
29d92a7e1SCornelia Huck /*
39d92a7e1SCornelia Huck  * Driver for s390 chsc subchannels
49d92a7e1SCornelia Huck  *
565b4e403SSebastian Ott  * Copyright IBM Corp. 2008, 2011
6ad285ae9SCornelia Huck  *
79d92a7e1SCornelia Huck  * Author(s): Cornelia Huck <cornelia.huck@de.ibm.com>
89d92a7e1SCornelia Huck  *
99d92a7e1SCornelia Huck  */
109d92a7e1SCornelia Huck 
115a0e3ad6STejun Heo #include <linux/slab.h>
12048cd4e5SHeiko Carstens #include <linux/compat.h>
139d92a7e1SCornelia Huck #include <linux/device.h>
14*5c2e5a0cSAlexander Gordeev #include <linux/io.h>
159d92a7e1SCornelia Huck #include <linux/module.h>
169d92a7e1SCornelia Huck #include <linux/uaccess.h>
179d92a7e1SCornelia Huck #include <linux/miscdevice.h>
1865b4e403SSebastian Ott #include <linux/kernel_stat.h>
199d92a7e1SCornelia Huck 
209d92a7e1SCornelia Huck #include <asm/cio.h>
219d92a7e1SCornelia Huck #include <asm/chsc.h>
229d92a7e1SCornelia Huck #include <asm/isc.h>
239d92a7e1SCornelia Huck 
249d92a7e1SCornelia Huck #include "cio.h"
259d92a7e1SCornelia Huck #include "cio_debug.h"
269d92a7e1SCornelia Huck #include "css.h"
279d92a7e1SCornelia Huck #include "chsc_sch.h"
289d92a7e1SCornelia Huck #include "ioasm.h"
299d92a7e1SCornelia Huck 
309d92a7e1SCornelia Huck static debug_info_t *chsc_debug_msg_id;
319d92a7e1SCornelia Huck static debug_info_t *chsc_debug_log_id;
329d92a7e1SCornelia Huck 
33e9a8f32aSMichael Holzheu static struct chsc_request *on_close_request;
34e9a8f32aSMichael Holzheu static struct chsc_async_area *on_close_chsc_area;
35e9a8f32aSMichael Holzheu static DEFINE_MUTEX(on_close_mutex);
36e9a8f32aSMichael Holzheu 
379d92a7e1SCornelia Huck #define CHSC_MSG(imp, args...) do {					\
389d92a7e1SCornelia Huck 		debug_sprintf_event(chsc_debug_msg_id, imp , ##args);	\
399d92a7e1SCornelia Huck 	} while (0)
409d92a7e1SCornelia Huck 
419d92a7e1SCornelia Huck #define CHSC_LOG(imp, txt) do {					\
429d92a7e1SCornelia Huck 		debug_text_event(chsc_debug_log_id, imp , txt);	\
439d92a7e1SCornelia Huck 	} while (0)
449d92a7e1SCornelia Huck 
CHSC_LOG_HEX(int level,void * data,int length)459d92a7e1SCornelia Huck static void CHSC_LOG_HEX(int level, void *data, int length)
469d92a7e1SCornelia Huck {
479d92a7e1SCornelia Huck 	debug_event(chsc_debug_log_id, level, data, length);
489d92a7e1SCornelia Huck }
499d92a7e1SCornelia Huck 
509d92a7e1SCornelia Huck MODULE_AUTHOR("IBM Corporation");
519d92a7e1SCornelia Huck MODULE_DESCRIPTION("driver for s390 chsc subchannels");
529d92a7e1SCornelia Huck MODULE_LICENSE("GPL");
539d92a7e1SCornelia Huck 
chsc_subchannel_irq(struct subchannel * sch)549d92a7e1SCornelia Huck static void chsc_subchannel_irq(struct subchannel *sch)
559d92a7e1SCornelia Huck {
5685fb534eSSebastian Ott 	struct chsc_private *private = dev_get_drvdata(&sch->dev);
579d92a7e1SCornelia Huck 	struct chsc_request *request = private->request;
580bf7fcf1SChristoph Lameter 	struct irb *irb = this_cpu_ptr(&cio_irb);
599d92a7e1SCornelia Huck 
609d92a7e1SCornelia Huck 	CHSC_LOG(4, "irb");
619d92a7e1SCornelia Huck 	CHSC_LOG_HEX(4, irb, sizeof(*irb));
62420f42ecSHeiko Carstens 	inc_irq_stat(IRQIO_CSC);
6365b4e403SSebastian Ott 
649d92a7e1SCornelia Huck 	/* Copy irb to provided request and set done. */
659d92a7e1SCornelia Huck 	if (!request) {
669d92a7e1SCornelia Huck 		CHSC_MSG(0, "Interrupt on sch 0.%x.%04x with no request\n",
679d92a7e1SCornelia Huck 			 sch->schid.ssid, sch->schid.sch_no);
689d92a7e1SCornelia Huck 		return;
699d92a7e1SCornelia Huck 	}
709d92a7e1SCornelia Huck 	private->request = NULL;
719d92a7e1SCornelia Huck 	memcpy(&request->irb, irb, sizeof(*irb));
72cdb912a4SSebastian Ott 	cio_update_schib(sch);
739d92a7e1SCornelia Huck 	complete(&request->completion);
749d92a7e1SCornelia Huck 	put_device(&sch->dev);
759d92a7e1SCornelia Huck }
769d92a7e1SCornelia Huck 
chsc_subchannel_probe(struct subchannel * sch)779d92a7e1SCornelia Huck static int chsc_subchannel_probe(struct subchannel *sch)
789d92a7e1SCornelia Huck {
799d92a7e1SCornelia Huck 	struct chsc_private *private;
809d92a7e1SCornelia Huck 	int ret;
819d92a7e1SCornelia Huck 
829d92a7e1SCornelia Huck 	CHSC_MSG(6, "Detected chsc subchannel 0.%x.%04x\n",
839d92a7e1SCornelia Huck 		 sch->schid.ssid, sch->schid.sch_no);
849d92a7e1SCornelia Huck 	sch->isc = CHSC_SCH_ISC;
859d92a7e1SCornelia Huck 	private = kzalloc(sizeof(*private), GFP_KERNEL);
869d92a7e1SCornelia Huck 	if (!private)
879d92a7e1SCornelia Huck 		return -ENOMEM;
8885fb534eSSebastian Ott 	dev_set_drvdata(&sch->dev, private);
89*5c2e5a0cSAlexander Gordeev 	ret = cio_enable_subchannel(sch, (u32)virt_to_phys(sch));
909d92a7e1SCornelia Huck 	if (ret) {
919d92a7e1SCornelia Huck 		CHSC_MSG(0, "Failed to enable 0.%x.%04x: %d\n",
929d92a7e1SCornelia Huck 			 sch->schid.ssid, sch->schid.sch_no, ret);
9385fb534eSSebastian Ott 		dev_set_drvdata(&sch->dev, NULL);
949d92a7e1SCornelia Huck 		kfree(private);
959d92a7e1SCornelia Huck 	}
969d92a7e1SCornelia Huck 	return ret;
979d92a7e1SCornelia Huck }
989d92a7e1SCornelia Huck 
chsc_subchannel_remove(struct subchannel * sch)99a7bdb9a9SUwe Kleine-König static void chsc_subchannel_remove(struct subchannel *sch)
1009d92a7e1SCornelia Huck {
1019d92a7e1SCornelia Huck 	struct chsc_private *private;
1029d92a7e1SCornelia Huck 
1039d92a7e1SCornelia Huck 	cio_disable_subchannel(sch);
10485fb534eSSebastian Ott 	private = dev_get_drvdata(&sch->dev);
10585fb534eSSebastian Ott 	dev_set_drvdata(&sch->dev, NULL);
1069d92a7e1SCornelia Huck 	if (private->request) {
1079d92a7e1SCornelia Huck 		complete(&private->request->completion);
1089d92a7e1SCornelia Huck 		put_device(&sch->dev);
1099d92a7e1SCornelia Huck 	}
1109d92a7e1SCornelia Huck 	kfree(private);
1119d92a7e1SCornelia Huck }
1129d92a7e1SCornelia Huck 
chsc_subchannel_shutdown(struct subchannel * sch)1139d92a7e1SCornelia Huck static void chsc_subchannel_shutdown(struct subchannel *sch)
1149d92a7e1SCornelia Huck {
1159d92a7e1SCornelia Huck 	cio_disable_subchannel(sch);
1169d92a7e1SCornelia Huck }
1179d92a7e1SCornelia Huck 
1189d92a7e1SCornelia Huck static struct css_device_id chsc_subchannel_ids[] = {
1199d92a7e1SCornelia Huck 	{ .match_flags = 0x1, .type =SUBCHANNEL_TYPE_CHSC, },
1209d92a7e1SCornelia Huck 	{ /* end of list */ },
1219d92a7e1SCornelia Huck };
1229d92a7e1SCornelia Huck MODULE_DEVICE_TABLE(css, chsc_subchannel_ids);
1239d92a7e1SCornelia Huck 
1249d92a7e1SCornelia Huck static struct css_driver chsc_subchannel_driver = {
125e6aed122SSebastian Ott 	.drv = {
1269d92a7e1SCornelia Huck 		.owner = THIS_MODULE,
127e6aed122SSebastian Ott 		.name = "chsc_subchannel",
128e6aed122SSebastian Ott 	},
1299d92a7e1SCornelia Huck 	.subchannel_type = chsc_subchannel_ids,
1309d92a7e1SCornelia Huck 	.irq = chsc_subchannel_irq,
1319d92a7e1SCornelia Huck 	.probe = chsc_subchannel_probe,
1329d92a7e1SCornelia Huck 	.remove = chsc_subchannel_remove,
1339d92a7e1SCornelia Huck 	.shutdown = chsc_subchannel_shutdown,
1349d92a7e1SCornelia Huck };
1359d92a7e1SCornelia Huck 
chsc_init_dbfs(void)1369d92a7e1SCornelia Huck static int __init chsc_init_dbfs(void)
1379d92a7e1SCornelia Huck {
138f7e1e65dSSebastian Ott 	chsc_debug_msg_id = debug_register("chsc_msg", 8, 1, 4 * sizeof(long));
1399d92a7e1SCornelia Huck 	if (!chsc_debug_msg_id)
1409d92a7e1SCornelia Huck 		goto out;
1419d92a7e1SCornelia Huck 	debug_register_view(chsc_debug_msg_id, &debug_sprintf_view);
1429d92a7e1SCornelia Huck 	debug_set_level(chsc_debug_msg_id, 2);
1439d92a7e1SCornelia Huck 	chsc_debug_log_id = debug_register("chsc_log", 16, 1, 16);
1449d92a7e1SCornelia Huck 	if (!chsc_debug_log_id)
1459d92a7e1SCornelia Huck 		goto out;
1469d92a7e1SCornelia Huck 	debug_register_view(chsc_debug_log_id, &debug_hex_ascii_view);
1479d92a7e1SCornelia Huck 	debug_set_level(chsc_debug_log_id, 2);
1489d92a7e1SCornelia Huck 	return 0;
1499d92a7e1SCornelia Huck out:
1509d92a7e1SCornelia Huck 	debug_unregister(chsc_debug_msg_id);
1519d92a7e1SCornelia Huck 	return -ENOMEM;
1529d92a7e1SCornelia Huck }
1539d92a7e1SCornelia Huck 
chsc_remove_dbfs(void)1549d92a7e1SCornelia Huck static void chsc_remove_dbfs(void)
1559d92a7e1SCornelia Huck {
1569d92a7e1SCornelia Huck 	debug_unregister(chsc_debug_log_id);
1579d92a7e1SCornelia Huck 	debug_unregister(chsc_debug_msg_id);
1589d92a7e1SCornelia Huck }
1599d92a7e1SCornelia Huck 
chsc_init_sch_driver(void)1609d92a7e1SCornelia Huck static int __init chsc_init_sch_driver(void)
1619d92a7e1SCornelia Huck {
1629d92a7e1SCornelia Huck 	return css_driver_register(&chsc_subchannel_driver);
1639d92a7e1SCornelia Huck }
1649d92a7e1SCornelia Huck 
chsc_cleanup_sch_driver(void)1659d92a7e1SCornelia Huck static void chsc_cleanup_sch_driver(void)
1669d92a7e1SCornelia Huck {
1679d92a7e1SCornelia Huck 	css_driver_unregister(&chsc_subchannel_driver);
1689d92a7e1SCornelia Huck }
1699d92a7e1SCornelia Huck 
1709d92a7e1SCornelia Huck static DEFINE_SPINLOCK(chsc_lock);
1719d92a7e1SCornelia Huck 
chsc_subchannel_match_next_free(struct device * dev,const void * data)17292ce7e83SSuzuki K Poulose static int chsc_subchannel_match_next_free(struct device *dev, const void *data)
1739d92a7e1SCornelia Huck {
1749d92a7e1SCornelia Huck 	struct subchannel *sch = to_subchannel(dev);
1759d92a7e1SCornelia Huck 
1769d92a7e1SCornelia Huck 	return sch->schib.pmcw.ena && !scsw_fctl(&sch->schib.scsw);
1779d92a7e1SCornelia Huck }
1789d92a7e1SCornelia Huck 
chsc_get_next_subchannel(struct subchannel * sch)1799d92a7e1SCornelia Huck static struct subchannel *chsc_get_next_subchannel(struct subchannel *sch)
1809d92a7e1SCornelia Huck {
1819d92a7e1SCornelia Huck 	struct device *dev;
1829d92a7e1SCornelia Huck 
1839d92a7e1SCornelia Huck 	dev = driver_find_device(&chsc_subchannel_driver.drv,
1849d92a7e1SCornelia Huck 				 sch ? &sch->dev : NULL, NULL,
1859d92a7e1SCornelia Huck 				 chsc_subchannel_match_next_free);
1869d92a7e1SCornelia Huck 	return dev ? to_subchannel(dev) : NULL;
1879d92a7e1SCornelia Huck }
1889d92a7e1SCornelia Huck 
1899d92a7e1SCornelia Huck /**
1909d92a7e1SCornelia Huck  * chsc_async() - try to start a chsc request asynchronously
1919d92a7e1SCornelia Huck  * @chsc_area: request to be started
1929d92a7e1SCornelia Huck  * @request: request structure to associate
1939d92a7e1SCornelia Huck  *
1949d92a7e1SCornelia Huck  * Tries to start a chsc request on one of the existing chsc subchannels.
1959d92a7e1SCornelia Huck  * Returns:
1969d92a7e1SCornelia Huck  *  %0 if the request was performed synchronously
1979d92a7e1SCornelia Huck  *  %-EINPROGRESS if the request was successfully started
1989d92a7e1SCornelia Huck  *  %-EBUSY if all chsc subchannels are busy
1999d92a7e1SCornelia Huck  *  %-ENODEV if no chsc subchannels are available
2009d92a7e1SCornelia Huck  * Context:
2019d92a7e1SCornelia Huck  *  interrupts disabled, chsc_lock held
2029d92a7e1SCornelia Huck  */
chsc_async(struct chsc_async_area * chsc_area,struct chsc_request * request)2039d92a7e1SCornelia Huck static int chsc_async(struct chsc_async_area *chsc_area,
2049d92a7e1SCornelia Huck 		      struct chsc_request *request)
2059d92a7e1SCornelia Huck {
2069d92a7e1SCornelia Huck 	int cc;
2079d92a7e1SCornelia Huck 	struct chsc_private *private;
2089d92a7e1SCornelia Huck 	struct subchannel *sch = NULL;
2099d92a7e1SCornelia Huck 	int ret = -ENODEV;
2109d92a7e1SCornelia Huck 	char dbf[10];
2119d92a7e1SCornelia Huck 
212d1bf8590SHeiko Carstens 	chsc_area->header.key = PAGE_DEFAULT_KEY >> 4;
2139d92a7e1SCornelia Huck 	while ((sch = chsc_get_next_subchannel(sch))) {
2149d92a7e1SCornelia Huck 		spin_lock(sch->lock);
21585fb534eSSebastian Ott 		private = dev_get_drvdata(&sch->dev);
2169d92a7e1SCornelia Huck 		if (private->request) {
2179d92a7e1SCornelia Huck 			spin_unlock(sch->lock);
2189d92a7e1SCornelia Huck 			ret = -EBUSY;
2199d92a7e1SCornelia Huck 			continue;
2209d92a7e1SCornelia Huck 		}
2219d92a7e1SCornelia Huck 		chsc_area->header.sid = sch->schid;
2229d92a7e1SCornelia Huck 		CHSC_LOG(2, "schid");
2239d92a7e1SCornelia Huck 		CHSC_LOG_HEX(2, &sch->schid, sizeof(sch->schid));
2249d92a7e1SCornelia Huck 		cc = chsc(chsc_area);
22560d081c5SMichael Holzheu 		snprintf(dbf, sizeof(dbf), "cc:%d", cc);
2269d92a7e1SCornelia Huck 		CHSC_LOG(2, dbf);
2279d92a7e1SCornelia Huck 		switch (cc) {
2289d92a7e1SCornelia Huck 		case 0:
2299d92a7e1SCornelia Huck 			ret = 0;
2309d92a7e1SCornelia Huck 			break;
2319d92a7e1SCornelia Huck 		case 1:
2329d92a7e1SCornelia Huck 			sch->schib.scsw.cmd.fctl |= SCSW_FCTL_START_FUNC;
2339d92a7e1SCornelia Huck 			ret = -EINPROGRESS;
2349d92a7e1SCornelia Huck 			private->request = request;
2359d92a7e1SCornelia Huck 			break;
2369d92a7e1SCornelia Huck 		case 2:
2379d92a7e1SCornelia Huck 			ret = -EBUSY;
2389d92a7e1SCornelia Huck 			break;
2399d92a7e1SCornelia Huck 		default:
2409d92a7e1SCornelia Huck 			ret = -ENODEV;
2419d92a7e1SCornelia Huck 		}
2429d92a7e1SCornelia Huck 		spin_unlock(sch->lock);
2439d92a7e1SCornelia Huck 		CHSC_MSG(2, "chsc on 0.%x.%04x returned cc=%d\n",
2449d92a7e1SCornelia Huck 			 sch->schid.ssid, sch->schid.sch_no, cc);
2459d92a7e1SCornelia Huck 		if (ret == -EINPROGRESS)
2469d92a7e1SCornelia Huck 			return -EINPROGRESS;
2479d92a7e1SCornelia Huck 		put_device(&sch->dev);
2489d92a7e1SCornelia Huck 		if (ret == 0)
2499d92a7e1SCornelia Huck 			return 0;
2509d92a7e1SCornelia Huck 	}
2519d92a7e1SCornelia Huck 	return ret;
2529d92a7e1SCornelia Huck }
2539d92a7e1SCornelia Huck 
chsc_log_command(void * chsc_area)25464150adfSMichael Holzheu static void chsc_log_command(void *chsc_area)
2559d92a7e1SCornelia Huck {
2569d92a7e1SCornelia Huck 	char dbf[10];
2579d92a7e1SCornelia Huck 
25860d081c5SMichael Holzheu 	snprintf(dbf, sizeof(dbf), "CHSC:%x", ((uint16_t *)chsc_area)[1]);
2599d92a7e1SCornelia Huck 	CHSC_LOG(0, dbf);
2609d92a7e1SCornelia Huck 	CHSC_LOG_HEX(0, chsc_area, 32);
2619d92a7e1SCornelia Huck }
2629d92a7e1SCornelia Huck 
chsc_examine_irb(struct chsc_request * request)2639d92a7e1SCornelia Huck static int chsc_examine_irb(struct chsc_request *request)
2649d92a7e1SCornelia Huck {
2659d92a7e1SCornelia Huck 	int backed_up;
2669d92a7e1SCornelia Huck 
267b2bbb642SJulia Lawall 	if (!(scsw_stctl(&request->irb.scsw) & SCSW_STCTL_STATUS_PEND))
2689d92a7e1SCornelia Huck 		return -EIO;
2699d92a7e1SCornelia Huck 	backed_up = scsw_cstat(&request->irb.scsw) & SCHN_STAT_CHAIN_CHECK;
2709d92a7e1SCornelia Huck 	request->irb.scsw.cmd.cstat &= ~SCHN_STAT_CHAIN_CHECK;
2719d92a7e1SCornelia Huck 	if (scsw_cstat(&request->irb.scsw) == 0)
2729d92a7e1SCornelia Huck 		return 0;
2739d92a7e1SCornelia Huck 	if (!backed_up)
2749d92a7e1SCornelia Huck 		return 0;
2759d92a7e1SCornelia Huck 	if (scsw_cstat(&request->irb.scsw) & SCHN_STAT_PROG_CHECK)
2769d92a7e1SCornelia Huck 		return -EIO;
2779d92a7e1SCornelia Huck 	if (scsw_cstat(&request->irb.scsw) & SCHN_STAT_PROT_CHECK)
2789d92a7e1SCornelia Huck 		return -EPERM;
2799d92a7e1SCornelia Huck 	if (scsw_cstat(&request->irb.scsw) & SCHN_STAT_CHN_DATA_CHK)
2809d92a7e1SCornelia Huck 		return -EAGAIN;
2819d92a7e1SCornelia Huck 	if (scsw_cstat(&request->irb.scsw) & SCHN_STAT_CHN_CTRL_CHK)
2829d92a7e1SCornelia Huck 		return -EAGAIN;
2839d92a7e1SCornelia Huck 	return -EIO;
2849d92a7e1SCornelia Huck }
2859d92a7e1SCornelia Huck 
chsc_ioctl_start(void __user * user_area)2869d92a7e1SCornelia Huck static int chsc_ioctl_start(void __user *user_area)
2879d92a7e1SCornelia Huck {
2889d92a7e1SCornelia Huck 	struct chsc_request *request;
2899d92a7e1SCornelia Huck 	struct chsc_async_area *chsc_area;
2909d92a7e1SCornelia Huck 	int ret;
2919d92a7e1SCornelia Huck 	char dbf[10];
2929d92a7e1SCornelia Huck 
2939d92a7e1SCornelia Huck 	if (!css_general_characteristics.dynio)
2949d92a7e1SCornelia Huck 		/* It makes no sense to try. */
2959d92a7e1SCornelia Huck 		return -EOPNOTSUPP;
2969d92a7e1SCornelia Huck 	chsc_area = (void *)get_zeroed_page(GFP_DMA | GFP_KERNEL);
2979d92a7e1SCornelia Huck 	if (!chsc_area)
2989d92a7e1SCornelia Huck 		return -ENOMEM;
2999d92a7e1SCornelia Huck 	request = kzalloc(sizeof(*request), GFP_KERNEL);
3009d92a7e1SCornelia Huck 	if (!request) {
3019d92a7e1SCornelia Huck 		ret = -ENOMEM;
3029d92a7e1SCornelia Huck 		goto out_free;
3039d92a7e1SCornelia Huck 	}
3049d92a7e1SCornelia Huck 	init_completion(&request->completion);
3059d92a7e1SCornelia Huck 	if (copy_from_user(chsc_area, user_area, PAGE_SIZE)) {
3069d92a7e1SCornelia Huck 		ret = -EFAULT;
3079d92a7e1SCornelia Huck 		goto out_free;
3089d92a7e1SCornelia Huck 	}
3099d92a7e1SCornelia Huck 	chsc_log_command(chsc_area);
3109d92a7e1SCornelia Huck 	spin_lock_irq(&chsc_lock);
3119d92a7e1SCornelia Huck 	ret = chsc_async(chsc_area, request);
3129d92a7e1SCornelia Huck 	spin_unlock_irq(&chsc_lock);
3139d92a7e1SCornelia Huck 	if (ret == -EINPROGRESS) {
3149d92a7e1SCornelia Huck 		wait_for_completion(&request->completion);
3159d92a7e1SCornelia Huck 		ret = chsc_examine_irb(request);
3169d92a7e1SCornelia Huck 	}
3179d92a7e1SCornelia Huck 	/* copy area back to user */
3189d92a7e1SCornelia Huck 	if (!ret)
3199d92a7e1SCornelia Huck 		if (copy_to_user(user_area, chsc_area, PAGE_SIZE))
3209d92a7e1SCornelia Huck 			ret = -EFAULT;
3219d92a7e1SCornelia Huck out_free:
32260d081c5SMichael Holzheu 	snprintf(dbf, sizeof(dbf), "ret:%d", ret);
3239d92a7e1SCornelia Huck 	CHSC_LOG(0, dbf);
3249d92a7e1SCornelia Huck 	kfree(request);
3259d92a7e1SCornelia Huck 	free_page((unsigned long)chsc_area);
3269d92a7e1SCornelia Huck 	return ret;
3279d92a7e1SCornelia Huck }
3289d92a7e1SCornelia Huck 
chsc_ioctl_on_close_set(void __user * user_area)329e9a8f32aSMichael Holzheu static int chsc_ioctl_on_close_set(void __user *user_area)
330e9a8f32aSMichael Holzheu {
331e9a8f32aSMichael Holzheu 	char dbf[13];
332e9a8f32aSMichael Holzheu 	int ret;
333e9a8f32aSMichael Holzheu 
334e9a8f32aSMichael Holzheu 	mutex_lock(&on_close_mutex);
335e9a8f32aSMichael Holzheu 	if (on_close_chsc_area) {
336e9a8f32aSMichael Holzheu 		ret = -EBUSY;
337e9a8f32aSMichael Holzheu 		goto out_unlock;
338e9a8f32aSMichael Holzheu 	}
339e9a8f32aSMichael Holzheu 	on_close_request = kzalloc(sizeof(*on_close_request), GFP_KERNEL);
340e9a8f32aSMichael Holzheu 	if (!on_close_request) {
341e9a8f32aSMichael Holzheu 		ret = -ENOMEM;
342e9a8f32aSMichael Holzheu 		goto out_unlock;
343e9a8f32aSMichael Holzheu 	}
344e9a8f32aSMichael Holzheu 	on_close_chsc_area = (void *)get_zeroed_page(GFP_DMA | GFP_KERNEL);
345e9a8f32aSMichael Holzheu 	if (!on_close_chsc_area) {
346e9a8f32aSMichael Holzheu 		ret = -ENOMEM;
347e9a8f32aSMichael Holzheu 		goto out_free_request;
348e9a8f32aSMichael Holzheu 	}
349e9a8f32aSMichael Holzheu 	if (copy_from_user(on_close_chsc_area, user_area, PAGE_SIZE)) {
350e9a8f32aSMichael Holzheu 		ret = -EFAULT;
351e9a8f32aSMichael Holzheu 		goto out_free_chsc;
352e9a8f32aSMichael Holzheu 	}
353e9a8f32aSMichael Holzheu 	ret = 0;
354e9a8f32aSMichael Holzheu 	goto out_unlock;
355e9a8f32aSMichael Holzheu 
356e9a8f32aSMichael Holzheu out_free_chsc:
357e9a8f32aSMichael Holzheu 	free_page((unsigned long)on_close_chsc_area);
358e9a8f32aSMichael Holzheu 	on_close_chsc_area = NULL;
359e9a8f32aSMichael Holzheu out_free_request:
360e9a8f32aSMichael Holzheu 	kfree(on_close_request);
361e9a8f32aSMichael Holzheu 	on_close_request = NULL;
362e9a8f32aSMichael Holzheu out_unlock:
363e9a8f32aSMichael Holzheu 	mutex_unlock(&on_close_mutex);
36460d081c5SMichael Holzheu 	snprintf(dbf, sizeof(dbf), "ocsret:%d", ret);
365e9a8f32aSMichael Holzheu 	CHSC_LOG(0, dbf);
366e9a8f32aSMichael Holzheu 	return ret;
367e9a8f32aSMichael Holzheu }
368e9a8f32aSMichael Holzheu 
chsc_ioctl_on_close_remove(void)369e9a8f32aSMichael Holzheu static int chsc_ioctl_on_close_remove(void)
370e9a8f32aSMichael Holzheu {
371e9a8f32aSMichael Holzheu 	char dbf[13];
372e9a8f32aSMichael Holzheu 	int ret;
373e9a8f32aSMichael Holzheu 
374e9a8f32aSMichael Holzheu 	mutex_lock(&on_close_mutex);
375e9a8f32aSMichael Holzheu 	if (!on_close_chsc_area) {
376e9a8f32aSMichael Holzheu 		ret = -ENOENT;
377e9a8f32aSMichael Holzheu 		goto out_unlock;
378e9a8f32aSMichael Holzheu 	}
379e9a8f32aSMichael Holzheu 	free_page((unsigned long)on_close_chsc_area);
380e9a8f32aSMichael Holzheu 	on_close_chsc_area = NULL;
381e9a8f32aSMichael Holzheu 	kfree(on_close_request);
382e9a8f32aSMichael Holzheu 	on_close_request = NULL;
383e9a8f32aSMichael Holzheu 	ret = 0;
384e9a8f32aSMichael Holzheu out_unlock:
385e9a8f32aSMichael Holzheu 	mutex_unlock(&on_close_mutex);
38660d081c5SMichael Holzheu 	snprintf(dbf, sizeof(dbf), "ocrret:%d", ret);
387e9a8f32aSMichael Holzheu 	CHSC_LOG(0, dbf);
388e9a8f32aSMichael Holzheu 	return ret;
389e9a8f32aSMichael Holzheu }
390e9a8f32aSMichael Holzheu 
chsc_ioctl_start_sync(void __user * user_area)39164150adfSMichael Holzheu static int chsc_ioctl_start_sync(void __user *user_area)
39264150adfSMichael Holzheu {
39364150adfSMichael Holzheu 	struct chsc_sync_area *chsc_area;
39464150adfSMichael Holzheu 	int ret, ccode;
39564150adfSMichael Holzheu 
39664150adfSMichael Holzheu 	chsc_area = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
39764150adfSMichael Holzheu 	if (!chsc_area)
39864150adfSMichael Holzheu 		return -ENOMEM;
39964150adfSMichael Holzheu 	if (copy_from_user(chsc_area, user_area, PAGE_SIZE)) {
40064150adfSMichael Holzheu 		ret = -EFAULT;
40164150adfSMichael Holzheu 		goto out_free;
40264150adfSMichael Holzheu 	}
40364150adfSMichael Holzheu 	if (chsc_area->header.code & 0x4000) {
40464150adfSMichael Holzheu 		ret = -EINVAL;
40564150adfSMichael Holzheu 		goto out_free;
40664150adfSMichael Holzheu 	}
40764150adfSMichael Holzheu 	chsc_log_command(chsc_area);
40864150adfSMichael Holzheu 	ccode = chsc(chsc_area);
40964150adfSMichael Holzheu 	if (ccode != 0) {
41064150adfSMichael Holzheu 		ret = -EIO;
41164150adfSMichael Holzheu 		goto out_free;
41264150adfSMichael Holzheu 	}
41364150adfSMichael Holzheu 	if (copy_to_user(user_area, chsc_area, PAGE_SIZE))
41464150adfSMichael Holzheu 		ret = -EFAULT;
41564150adfSMichael Holzheu 	else
41664150adfSMichael Holzheu 		ret = 0;
41764150adfSMichael Holzheu out_free:
41864150adfSMichael Holzheu 	free_page((unsigned long)chsc_area);
41964150adfSMichael Holzheu 	return ret;
42064150adfSMichael Holzheu }
42164150adfSMichael Holzheu 
chsc_ioctl_info_channel_path(void __user * user_cd)4229d92a7e1SCornelia Huck static int chsc_ioctl_info_channel_path(void __user *user_cd)
4239d92a7e1SCornelia Huck {
4249d92a7e1SCornelia Huck 	struct chsc_chp_cd *cd;
4259d92a7e1SCornelia Huck 	int ret, ccode;
4269d92a7e1SCornelia Huck 	struct {
4279d92a7e1SCornelia Huck 		struct chsc_header request;
4289d92a7e1SCornelia Huck 		u32 : 2;
4299d92a7e1SCornelia Huck 		u32 m : 1;
4309d92a7e1SCornelia Huck 		u32 : 1;
4319d92a7e1SCornelia Huck 		u32 fmt1 : 4;
4329d92a7e1SCornelia Huck 		u32 cssid : 8;
4339d92a7e1SCornelia Huck 		u32 : 8;
4349d92a7e1SCornelia Huck 		u32 first_chpid : 8;
4359d92a7e1SCornelia Huck 		u32 : 24;
4369d92a7e1SCornelia Huck 		u32 last_chpid : 8;
4379d92a7e1SCornelia Huck 		u32 : 32;
4389d92a7e1SCornelia Huck 		struct chsc_header response;
4399d92a7e1SCornelia Huck 		u8 data[PAGE_SIZE - 20];
4409d92a7e1SCornelia Huck 	} __attribute__ ((packed)) *scpcd_area;
4419d92a7e1SCornelia Huck 
4429d92a7e1SCornelia Huck 	scpcd_area = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
4439d92a7e1SCornelia Huck 	if (!scpcd_area)
4449d92a7e1SCornelia Huck 		return -ENOMEM;
4459d92a7e1SCornelia Huck 	cd = kzalloc(sizeof(*cd), GFP_KERNEL);
4469d92a7e1SCornelia Huck 	if (!cd) {
4479d92a7e1SCornelia Huck 		ret = -ENOMEM;
4489d92a7e1SCornelia Huck 		goto out_free;
4499d92a7e1SCornelia Huck 	}
4509d92a7e1SCornelia Huck 	if (copy_from_user(cd, user_cd, sizeof(*cd))) {
4519d92a7e1SCornelia Huck 		ret = -EFAULT;
4529d92a7e1SCornelia Huck 		goto out_free;
4539d92a7e1SCornelia Huck 	}
4549d92a7e1SCornelia Huck 	scpcd_area->request.length = 0x0010;
4559d92a7e1SCornelia Huck 	scpcd_area->request.code = 0x0028;
4569d92a7e1SCornelia Huck 	scpcd_area->m = cd->m;
4579d92a7e1SCornelia Huck 	scpcd_area->fmt1 = cd->fmt;
4589d92a7e1SCornelia Huck 	scpcd_area->cssid = cd->chpid.cssid;
4599d92a7e1SCornelia Huck 	scpcd_area->first_chpid = cd->chpid.id;
4609d92a7e1SCornelia Huck 	scpcd_area->last_chpid = cd->chpid.id;
4619d92a7e1SCornelia Huck 
4629d92a7e1SCornelia Huck 	ccode = chsc(scpcd_area);
4639d92a7e1SCornelia Huck 	if (ccode != 0) {
4649d92a7e1SCornelia Huck 		ret = -EIO;
4659d92a7e1SCornelia Huck 		goto out_free;
4669d92a7e1SCornelia Huck 	}
4679d92a7e1SCornelia Huck 	if (scpcd_area->response.code != 0x0001) {
4689d92a7e1SCornelia Huck 		ret = -EIO;
4699d92a7e1SCornelia Huck 		CHSC_MSG(0, "scpcd: response code=%x\n",
4709d92a7e1SCornelia Huck 			 scpcd_area->response.code);
4719d92a7e1SCornelia Huck 		goto out_free;
4729d92a7e1SCornelia Huck 	}
4739d92a7e1SCornelia Huck 	memcpy(&cd->cpcb, &scpcd_area->response, scpcd_area->response.length);
4749d92a7e1SCornelia Huck 	if (copy_to_user(user_cd, cd, sizeof(*cd)))
4759d92a7e1SCornelia Huck 		ret = -EFAULT;
4769d92a7e1SCornelia Huck 	else
4779d92a7e1SCornelia Huck 		ret = 0;
4789d92a7e1SCornelia Huck out_free:
4799d92a7e1SCornelia Huck 	kfree(cd);
4809d92a7e1SCornelia Huck 	free_page((unsigned long)scpcd_area);
4819d92a7e1SCornelia Huck 	return ret;
4829d92a7e1SCornelia Huck }
4839d92a7e1SCornelia Huck 
chsc_ioctl_info_cu(void __user * user_cd)4849d92a7e1SCornelia Huck static int chsc_ioctl_info_cu(void __user *user_cd)
4859d92a7e1SCornelia Huck {
4869d92a7e1SCornelia Huck 	struct chsc_cu_cd *cd;
4879d92a7e1SCornelia Huck 	int ret, ccode;
4889d92a7e1SCornelia Huck 	struct {
4899d92a7e1SCornelia Huck 		struct chsc_header request;
4909d92a7e1SCornelia Huck 		u32 : 2;
4919d92a7e1SCornelia Huck 		u32 m : 1;
4929d92a7e1SCornelia Huck 		u32 : 1;
4939d92a7e1SCornelia Huck 		u32 fmt1 : 4;
4949d92a7e1SCornelia Huck 		u32 cssid : 8;
4959d92a7e1SCornelia Huck 		u32 : 8;
4969d92a7e1SCornelia Huck 		u32 first_cun : 8;
4979d92a7e1SCornelia Huck 		u32 : 24;
4989d92a7e1SCornelia Huck 		u32 last_cun : 8;
4999d92a7e1SCornelia Huck 		u32 : 32;
5009d92a7e1SCornelia Huck 		struct chsc_header response;
5019d92a7e1SCornelia Huck 		u8 data[PAGE_SIZE - 20];
5029d92a7e1SCornelia Huck 	} __attribute__ ((packed)) *scucd_area;
5039d92a7e1SCornelia Huck 
5049d92a7e1SCornelia Huck 	scucd_area = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
5059d92a7e1SCornelia Huck 	if (!scucd_area)
5069d92a7e1SCornelia Huck 		return -ENOMEM;
5079d92a7e1SCornelia Huck 	cd = kzalloc(sizeof(*cd), GFP_KERNEL);
5089d92a7e1SCornelia Huck 	if (!cd) {
5099d92a7e1SCornelia Huck 		ret = -ENOMEM;
5109d92a7e1SCornelia Huck 		goto out_free;
5119d92a7e1SCornelia Huck 	}
5129d92a7e1SCornelia Huck 	if (copy_from_user(cd, user_cd, sizeof(*cd))) {
5139d92a7e1SCornelia Huck 		ret = -EFAULT;
5149d92a7e1SCornelia Huck 		goto out_free;
5159d92a7e1SCornelia Huck 	}
5169d92a7e1SCornelia Huck 	scucd_area->request.length = 0x0010;
5176228c2a5SSebastian Ott 	scucd_area->request.code = 0x0026;
5189d92a7e1SCornelia Huck 	scucd_area->m = cd->m;
5199d92a7e1SCornelia Huck 	scucd_area->fmt1 = cd->fmt;
5209d92a7e1SCornelia Huck 	scucd_area->cssid = cd->cssid;
5219d92a7e1SCornelia Huck 	scucd_area->first_cun = cd->cun;
5229d92a7e1SCornelia Huck 	scucd_area->last_cun = cd->cun;
5239d92a7e1SCornelia Huck 
5249d92a7e1SCornelia Huck 	ccode = chsc(scucd_area);
5259d92a7e1SCornelia Huck 	if (ccode != 0) {
5269d92a7e1SCornelia Huck 		ret = -EIO;
5279d92a7e1SCornelia Huck 		goto out_free;
5289d92a7e1SCornelia Huck 	}
5299d92a7e1SCornelia Huck 	if (scucd_area->response.code != 0x0001) {
5309d92a7e1SCornelia Huck 		ret = -EIO;
5319d92a7e1SCornelia Huck 		CHSC_MSG(0, "scucd: response code=%x\n",
5329d92a7e1SCornelia Huck 			 scucd_area->response.code);
5339d92a7e1SCornelia Huck 		goto out_free;
5349d92a7e1SCornelia Huck 	}
5359d92a7e1SCornelia Huck 	memcpy(&cd->cucb, &scucd_area->response, scucd_area->response.length);
5369d92a7e1SCornelia Huck 	if (copy_to_user(user_cd, cd, sizeof(*cd)))
5379d92a7e1SCornelia Huck 		ret = -EFAULT;
5389d92a7e1SCornelia Huck 	else
5399d92a7e1SCornelia Huck 		ret = 0;
5409d92a7e1SCornelia Huck out_free:
5419d92a7e1SCornelia Huck 	kfree(cd);
5429d92a7e1SCornelia Huck 	free_page((unsigned long)scucd_area);
5439d92a7e1SCornelia Huck 	return ret;
5449d92a7e1SCornelia Huck }
5459d92a7e1SCornelia Huck 
chsc_ioctl_info_sch_cu(void __user * user_cud)5469d92a7e1SCornelia Huck static int chsc_ioctl_info_sch_cu(void __user *user_cud)
5479d92a7e1SCornelia Huck {
5489d92a7e1SCornelia Huck 	struct chsc_sch_cud *cud;
5499d92a7e1SCornelia Huck 	int ret, ccode;
5509d92a7e1SCornelia Huck 	struct {
5519d92a7e1SCornelia Huck 		struct chsc_header request;
5529d92a7e1SCornelia Huck 		u32 : 2;
5539d92a7e1SCornelia Huck 		u32 m : 1;
5549d92a7e1SCornelia Huck 		u32 : 5;
5559d92a7e1SCornelia Huck 		u32 fmt1 : 4;
5569d92a7e1SCornelia Huck 		u32 : 2;
5579d92a7e1SCornelia Huck 		u32 ssid : 2;
5589d92a7e1SCornelia Huck 		u32 first_sch : 16;
5599d92a7e1SCornelia Huck 		u32 : 8;
5609d92a7e1SCornelia Huck 		u32 cssid : 8;
5619d92a7e1SCornelia Huck 		u32 last_sch : 16;
5629d92a7e1SCornelia Huck 		u32 : 32;
5639d92a7e1SCornelia Huck 		struct chsc_header response;
5649d92a7e1SCornelia Huck 		u8 data[PAGE_SIZE - 20];
5659d92a7e1SCornelia Huck 	} __attribute__ ((packed)) *sscud_area;
5669d92a7e1SCornelia Huck 
5679d92a7e1SCornelia Huck 	sscud_area = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
5689d92a7e1SCornelia Huck 	if (!sscud_area)
5699d92a7e1SCornelia Huck 		return -ENOMEM;
5709d92a7e1SCornelia Huck 	cud = kzalloc(sizeof(*cud), GFP_KERNEL);
5719d92a7e1SCornelia Huck 	if (!cud) {
5729d92a7e1SCornelia Huck 		ret = -ENOMEM;
5739d92a7e1SCornelia Huck 		goto out_free;
5749d92a7e1SCornelia Huck 	}
5759d92a7e1SCornelia Huck 	if (copy_from_user(cud, user_cud, sizeof(*cud))) {
5769d92a7e1SCornelia Huck 		ret = -EFAULT;
5779d92a7e1SCornelia Huck 		goto out_free;
5789d92a7e1SCornelia Huck 	}
5799d92a7e1SCornelia Huck 	sscud_area->request.length = 0x0010;
5809d92a7e1SCornelia Huck 	sscud_area->request.code = 0x0006;
5819d92a7e1SCornelia Huck 	sscud_area->m = cud->schid.m;
5829d92a7e1SCornelia Huck 	sscud_area->fmt1 = cud->fmt;
5839d92a7e1SCornelia Huck 	sscud_area->ssid = cud->schid.ssid;
5849d92a7e1SCornelia Huck 	sscud_area->first_sch = cud->schid.sch_no;
5859d92a7e1SCornelia Huck 	sscud_area->cssid = cud->schid.cssid;
5869d92a7e1SCornelia Huck 	sscud_area->last_sch = cud->schid.sch_no;
5879d92a7e1SCornelia Huck 
5889d92a7e1SCornelia Huck 	ccode = chsc(sscud_area);
5899d92a7e1SCornelia Huck 	if (ccode != 0) {
5909d92a7e1SCornelia Huck 		ret = -EIO;
5919d92a7e1SCornelia Huck 		goto out_free;
5929d92a7e1SCornelia Huck 	}
5939d92a7e1SCornelia Huck 	if (sscud_area->response.code != 0x0001) {
5949d92a7e1SCornelia Huck 		ret = -EIO;
5959d92a7e1SCornelia Huck 		CHSC_MSG(0, "sscud: response code=%x\n",
5969d92a7e1SCornelia Huck 			 sscud_area->response.code);
5979d92a7e1SCornelia Huck 		goto out_free;
5989d92a7e1SCornelia Huck 	}
5999d92a7e1SCornelia Huck 	memcpy(&cud->scub, &sscud_area->response, sscud_area->response.length);
6009d92a7e1SCornelia Huck 	if (copy_to_user(user_cud, cud, sizeof(*cud)))
6019d92a7e1SCornelia Huck 		ret = -EFAULT;
6029d92a7e1SCornelia Huck 	else
6039d92a7e1SCornelia Huck 		ret = 0;
6049d92a7e1SCornelia Huck out_free:
6059d92a7e1SCornelia Huck 	kfree(cud);
6069d92a7e1SCornelia Huck 	free_page((unsigned long)sscud_area);
6079d92a7e1SCornelia Huck 	return ret;
6089d92a7e1SCornelia Huck }
6099d92a7e1SCornelia Huck 
chsc_ioctl_conf_info(void __user * user_ci)6109d92a7e1SCornelia Huck static int chsc_ioctl_conf_info(void __user *user_ci)
6119d92a7e1SCornelia Huck {
6129d92a7e1SCornelia Huck 	struct chsc_conf_info *ci;
6139d92a7e1SCornelia Huck 	int ret, ccode;
6149d92a7e1SCornelia Huck 	struct {
6159d92a7e1SCornelia Huck 		struct chsc_header request;
6169d92a7e1SCornelia Huck 		u32 : 2;
6179d92a7e1SCornelia Huck 		u32 m : 1;
6189d92a7e1SCornelia Huck 		u32 : 1;
6199d92a7e1SCornelia Huck 		u32 fmt1 : 4;
6209d92a7e1SCornelia Huck 		u32 cssid : 8;
6219d92a7e1SCornelia Huck 		u32 : 6;
6229d92a7e1SCornelia Huck 		u32 ssid : 2;
6239d92a7e1SCornelia Huck 		u32 : 8;
6249d92a7e1SCornelia Huck 		u64 : 64;
6259d92a7e1SCornelia Huck 		struct chsc_header response;
6269d92a7e1SCornelia Huck 		u8 data[PAGE_SIZE - 20];
6279d92a7e1SCornelia Huck 	} __attribute__ ((packed)) *sci_area;
6289d92a7e1SCornelia Huck 
6299d92a7e1SCornelia Huck 	sci_area = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
6309d92a7e1SCornelia Huck 	if (!sci_area)
6319d92a7e1SCornelia Huck 		return -ENOMEM;
6329d92a7e1SCornelia Huck 	ci = kzalloc(sizeof(*ci), GFP_KERNEL);
6339d92a7e1SCornelia Huck 	if (!ci) {
6349d92a7e1SCornelia Huck 		ret = -ENOMEM;
6359d92a7e1SCornelia Huck 		goto out_free;
6369d92a7e1SCornelia Huck 	}
6379d92a7e1SCornelia Huck 	if (copy_from_user(ci, user_ci, sizeof(*ci))) {
6389d92a7e1SCornelia Huck 		ret = -EFAULT;
6399d92a7e1SCornelia Huck 		goto out_free;
6409d92a7e1SCornelia Huck 	}
6419d92a7e1SCornelia Huck 	sci_area->request.length = 0x0010;
6429d92a7e1SCornelia Huck 	sci_area->request.code = 0x0012;
6439d92a7e1SCornelia Huck 	sci_area->m = ci->id.m;
6449d92a7e1SCornelia Huck 	sci_area->fmt1 = ci->fmt;
6459d92a7e1SCornelia Huck 	sci_area->cssid = ci->id.cssid;
6469d92a7e1SCornelia Huck 	sci_area->ssid = ci->id.ssid;
6479d92a7e1SCornelia Huck 
6489d92a7e1SCornelia Huck 	ccode = chsc(sci_area);
6499d92a7e1SCornelia Huck 	if (ccode != 0) {
6509d92a7e1SCornelia Huck 		ret = -EIO;
6519d92a7e1SCornelia Huck 		goto out_free;
6529d92a7e1SCornelia Huck 	}
6539d92a7e1SCornelia Huck 	if (sci_area->response.code != 0x0001) {
6549d92a7e1SCornelia Huck 		ret = -EIO;
6559d92a7e1SCornelia Huck 		CHSC_MSG(0, "sci: response code=%x\n",
6569d92a7e1SCornelia Huck 			 sci_area->response.code);
6579d92a7e1SCornelia Huck 		goto out_free;
6589d92a7e1SCornelia Huck 	}
6599d92a7e1SCornelia Huck 	memcpy(&ci->scid, &sci_area->response, sci_area->response.length);
6609d92a7e1SCornelia Huck 	if (copy_to_user(user_ci, ci, sizeof(*ci)))
6619d92a7e1SCornelia Huck 		ret = -EFAULT;
6629d92a7e1SCornelia Huck 	else
6639d92a7e1SCornelia Huck 		ret = 0;
6649d92a7e1SCornelia Huck out_free:
6659d92a7e1SCornelia Huck 	kfree(ci);
6669d92a7e1SCornelia Huck 	free_page((unsigned long)sci_area);
6679d92a7e1SCornelia Huck 	return ret;
6689d92a7e1SCornelia Huck }
6699d92a7e1SCornelia Huck 
chsc_ioctl_conf_comp_list(void __user * user_ccl)6709d92a7e1SCornelia Huck static int chsc_ioctl_conf_comp_list(void __user *user_ccl)
6719d92a7e1SCornelia Huck {
6729d92a7e1SCornelia Huck 	struct chsc_comp_list *ccl;
6739d92a7e1SCornelia Huck 	int ret, ccode;
6749d92a7e1SCornelia Huck 	struct {
6759d92a7e1SCornelia Huck 		struct chsc_header request;
6769d92a7e1SCornelia Huck 		u32 ctype : 8;
6779d92a7e1SCornelia Huck 		u32 : 4;
6789d92a7e1SCornelia Huck 		u32 fmt : 4;
6799d92a7e1SCornelia Huck 		u32 : 16;
6809d92a7e1SCornelia Huck 		u64 : 64;
6819d92a7e1SCornelia Huck 		u32 list_parm[2];
6829d92a7e1SCornelia Huck 		u64 : 64;
6839d92a7e1SCornelia Huck 		struct chsc_header response;
6849d92a7e1SCornelia Huck 		u8 data[PAGE_SIZE - 36];
6859d92a7e1SCornelia Huck 	} __attribute__ ((packed)) *sccl_area;
6869d92a7e1SCornelia Huck 	struct {
6879d92a7e1SCornelia Huck 		u32 m : 1;
6889d92a7e1SCornelia Huck 		u32 : 31;
6899d92a7e1SCornelia Huck 		u32 cssid : 8;
6909d92a7e1SCornelia Huck 		u32 : 16;
6919d92a7e1SCornelia Huck 		u32 chpid : 8;
6929d92a7e1SCornelia Huck 	} __attribute__ ((packed)) *chpid_parm;
6939d92a7e1SCornelia Huck 	struct {
6949d92a7e1SCornelia Huck 		u32 f_cssid : 8;
6959d92a7e1SCornelia Huck 		u32 l_cssid : 8;
6969d92a7e1SCornelia Huck 		u32 : 16;
6979d92a7e1SCornelia Huck 		u32 res;
6989d92a7e1SCornelia Huck 	} __attribute__ ((packed)) *cssids_parm;
6999d92a7e1SCornelia Huck 
7009d92a7e1SCornelia Huck 	sccl_area = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
7019d92a7e1SCornelia Huck 	if (!sccl_area)
7029d92a7e1SCornelia Huck 		return -ENOMEM;
7039d92a7e1SCornelia Huck 	ccl = kzalloc(sizeof(*ccl), GFP_KERNEL);
7049d92a7e1SCornelia Huck 	if (!ccl) {
7059d92a7e1SCornelia Huck 		ret = -ENOMEM;
7069d92a7e1SCornelia Huck 		goto out_free;
7079d92a7e1SCornelia Huck 	}
7089d92a7e1SCornelia Huck 	if (copy_from_user(ccl, user_ccl, sizeof(*ccl))) {
7099d92a7e1SCornelia Huck 		ret = -EFAULT;
7109d92a7e1SCornelia Huck 		goto out_free;
7119d92a7e1SCornelia Huck 	}
7129d92a7e1SCornelia Huck 	sccl_area->request.length = 0x0020;
7139d92a7e1SCornelia Huck 	sccl_area->request.code = 0x0030;
7149d92a7e1SCornelia Huck 	sccl_area->fmt = ccl->req.fmt;
7159d92a7e1SCornelia Huck 	sccl_area->ctype = ccl->req.ctype;
7169d92a7e1SCornelia Huck 	switch (sccl_area->ctype) {
7179d92a7e1SCornelia Huck 	case CCL_CU_ON_CHP:
7189d92a7e1SCornelia Huck 	case CCL_IOP_CHP:
7199d92a7e1SCornelia Huck 		chpid_parm = (void *)&sccl_area->list_parm;
7209d92a7e1SCornelia Huck 		chpid_parm->m = ccl->req.chpid.m;
7219d92a7e1SCornelia Huck 		chpid_parm->cssid = ccl->req.chpid.chp.cssid;
7229d92a7e1SCornelia Huck 		chpid_parm->chpid = ccl->req.chpid.chp.id;
7239d92a7e1SCornelia Huck 		break;
7249d92a7e1SCornelia Huck 	case CCL_CSS_IMG:
7259d92a7e1SCornelia Huck 	case CCL_CSS_IMG_CONF_CHAR:
7269d92a7e1SCornelia Huck 		cssids_parm = (void *)&sccl_area->list_parm;
7279d92a7e1SCornelia Huck 		cssids_parm->f_cssid = ccl->req.cssids.f_cssid;
7289d92a7e1SCornelia Huck 		cssids_parm->l_cssid = ccl->req.cssids.l_cssid;
7299d92a7e1SCornelia Huck 		break;
7309d92a7e1SCornelia Huck 	}
7319d92a7e1SCornelia Huck 	ccode = chsc(sccl_area);
7329d92a7e1SCornelia Huck 	if (ccode != 0) {
7339d92a7e1SCornelia Huck 		ret = -EIO;
7349d92a7e1SCornelia Huck 		goto out_free;
7359d92a7e1SCornelia Huck 	}
7369d92a7e1SCornelia Huck 	if (sccl_area->response.code != 0x0001) {
7379d92a7e1SCornelia Huck 		ret = -EIO;
7389d92a7e1SCornelia Huck 		CHSC_MSG(0, "sccl: response code=%x\n",
7399d92a7e1SCornelia Huck 			 sccl_area->response.code);
7409d92a7e1SCornelia Huck 		goto out_free;
7419d92a7e1SCornelia Huck 	}
7429d92a7e1SCornelia Huck 	memcpy(&ccl->sccl, &sccl_area->response, sccl_area->response.length);
7439d92a7e1SCornelia Huck 	if (copy_to_user(user_ccl, ccl, sizeof(*ccl)))
7449d92a7e1SCornelia Huck 		ret = -EFAULT;
7459d92a7e1SCornelia Huck 	else
7469d92a7e1SCornelia Huck 		ret = 0;
7479d92a7e1SCornelia Huck out_free:
7489d92a7e1SCornelia Huck 	kfree(ccl);
7499d92a7e1SCornelia Huck 	free_page((unsigned long)sccl_area);
7509d92a7e1SCornelia Huck 	return ret;
7519d92a7e1SCornelia Huck }
7529d92a7e1SCornelia Huck 
chsc_ioctl_chpd(void __user * user_chpd)7539d92a7e1SCornelia Huck static int chsc_ioctl_chpd(void __user *user_chpd)
7549d92a7e1SCornelia Huck {
755906c9768SSebastian Ott 	struct chsc_scpd *scpd_area;
7569d92a7e1SCornelia Huck 	struct chsc_cpd_info *chpd;
7579d92a7e1SCornelia Huck 	int ret;
7589d92a7e1SCornelia Huck 
7599d92a7e1SCornelia Huck 	chpd = kzalloc(sizeof(*chpd), GFP_KERNEL);
760906c9768SSebastian Ott 	scpd_area = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
761906c9768SSebastian Ott 	if (!scpd_area || !chpd) {
762906c9768SSebastian Ott 		ret = -ENOMEM;
763906c9768SSebastian Ott 		goto out_free;
764906c9768SSebastian Ott 	}
7659d92a7e1SCornelia Huck 	if (copy_from_user(chpd, user_chpd, sizeof(*chpd))) {
7669d92a7e1SCornelia Huck 		ret = -EFAULT;
7679d92a7e1SCornelia Huck 		goto out_free;
7689d92a7e1SCornelia Huck 	}
7699d92a7e1SCornelia Huck 	ret = chsc_determine_channel_path_desc(chpd->chpid, chpd->fmt,
7709d92a7e1SCornelia Huck 					       chpd->rfmt, chpd->c, chpd->m,
771906c9768SSebastian Ott 					       scpd_area);
7729d92a7e1SCornelia Huck 	if (ret)
7739d92a7e1SCornelia Huck 		goto out_free;
774906c9768SSebastian Ott 	memcpy(&chpd->chpdb, &scpd_area->response, scpd_area->response.length);
7759d92a7e1SCornelia Huck 	if (copy_to_user(user_chpd, chpd, sizeof(*chpd)))
7769d92a7e1SCornelia Huck 		ret = -EFAULT;
7779d92a7e1SCornelia Huck out_free:
7789d92a7e1SCornelia Huck 	kfree(chpd);
779906c9768SSebastian Ott 	free_page((unsigned long)scpd_area);
7809d92a7e1SCornelia Huck 	return ret;
7819d92a7e1SCornelia Huck }
7829d92a7e1SCornelia Huck 
chsc_ioctl_dcal(void __user * user_dcal)7839d92a7e1SCornelia Huck static int chsc_ioctl_dcal(void __user *user_dcal)
7849d92a7e1SCornelia Huck {
7859d92a7e1SCornelia Huck 	struct chsc_dcal *dcal;
7869d92a7e1SCornelia Huck 	int ret, ccode;
7879d92a7e1SCornelia Huck 	struct {
7889d92a7e1SCornelia Huck 		struct chsc_header request;
7899d92a7e1SCornelia Huck 		u32 atype : 8;
7909d92a7e1SCornelia Huck 		u32 : 4;
7919d92a7e1SCornelia Huck 		u32 fmt : 4;
7929d92a7e1SCornelia Huck 		u32 : 16;
7939d92a7e1SCornelia Huck 		u32 res0[2];
7949d92a7e1SCornelia Huck 		u32 list_parm[2];
7959d92a7e1SCornelia Huck 		u32 res1[2];
7969d92a7e1SCornelia Huck 		struct chsc_header response;
7979d92a7e1SCornelia Huck 		u8 data[PAGE_SIZE - 36];
7989d92a7e1SCornelia Huck 	} __attribute__ ((packed)) *sdcal_area;
7999d92a7e1SCornelia Huck 
8009d92a7e1SCornelia Huck 	sdcal_area = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
8019d92a7e1SCornelia Huck 	if (!sdcal_area)
8029d92a7e1SCornelia Huck 		return -ENOMEM;
8039d92a7e1SCornelia Huck 	dcal = kzalloc(sizeof(*dcal), GFP_KERNEL);
8049d92a7e1SCornelia Huck 	if (!dcal) {
8059d92a7e1SCornelia Huck 		ret = -ENOMEM;
8069d92a7e1SCornelia Huck 		goto out_free;
8079d92a7e1SCornelia Huck 	}
8089d92a7e1SCornelia Huck 	if (copy_from_user(dcal, user_dcal, sizeof(*dcal))) {
8099d92a7e1SCornelia Huck 		ret = -EFAULT;
8109d92a7e1SCornelia Huck 		goto out_free;
8119d92a7e1SCornelia Huck 	}
8129d92a7e1SCornelia Huck 	sdcal_area->request.length = 0x0020;
8139d92a7e1SCornelia Huck 	sdcal_area->request.code = 0x0034;
8149d92a7e1SCornelia Huck 	sdcal_area->atype = dcal->req.atype;
8159d92a7e1SCornelia Huck 	sdcal_area->fmt = dcal->req.fmt;
8169d92a7e1SCornelia Huck 	memcpy(&sdcal_area->list_parm, &dcal->req.list_parm,
8179d92a7e1SCornelia Huck 	       sizeof(sdcal_area->list_parm));
8189d92a7e1SCornelia Huck 
8199d92a7e1SCornelia Huck 	ccode = chsc(sdcal_area);
8209d92a7e1SCornelia Huck 	if (ccode != 0) {
8219d92a7e1SCornelia Huck 		ret = -EIO;
8229d92a7e1SCornelia Huck 		goto out_free;
8239d92a7e1SCornelia Huck 	}
8249d92a7e1SCornelia Huck 	if (sdcal_area->response.code != 0x0001) {
8259d92a7e1SCornelia Huck 		ret = -EIO;
8269d92a7e1SCornelia Huck 		CHSC_MSG(0, "sdcal: response code=%x\n",
8279d92a7e1SCornelia Huck 			 sdcal_area->response.code);
8289d92a7e1SCornelia Huck 		goto out_free;
8299d92a7e1SCornelia Huck 	}
8309d92a7e1SCornelia Huck 	memcpy(&dcal->sdcal, &sdcal_area->response,
8319d92a7e1SCornelia Huck 	       sdcal_area->response.length);
8329d92a7e1SCornelia Huck 	if (copy_to_user(user_dcal, dcal, sizeof(*dcal)))
8339d92a7e1SCornelia Huck 		ret = -EFAULT;
8349d92a7e1SCornelia Huck 	else
8359d92a7e1SCornelia Huck 		ret = 0;
8369d92a7e1SCornelia Huck out_free:
8379d92a7e1SCornelia Huck 	kfree(dcal);
8389d92a7e1SCornelia Huck 	free_page((unsigned long)sdcal_area);
8399d92a7e1SCornelia Huck 	return ret;
8409d92a7e1SCornelia Huck }
8419d92a7e1SCornelia Huck 
chsc_ioctl(struct file * filp,unsigned int cmd,unsigned long arg)8429d92a7e1SCornelia Huck static long chsc_ioctl(struct file *filp, unsigned int cmd,
8439d92a7e1SCornelia Huck 		       unsigned long arg)
8449d92a7e1SCornelia Huck {
84544ee6a85SHeiko Carstens 	void __user *argp;
84644ee6a85SHeiko Carstens 
8479d92a7e1SCornelia Huck 	CHSC_MSG(2, "chsc_ioctl called, cmd=%x\n", cmd);
84844ee6a85SHeiko Carstens 	if (is_compat_task())
84944ee6a85SHeiko Carstens 		argp = compat_ptr(arg);
85044ee6a85SHeiko Carstens 	else
85144ee6a85SHeiko Carstens 		argp = (void __user *)arg;
8529d92a7e1SCornelia Huck 	switch (cmd) {
8539d92a7e1SCornelia Huck 	case CHSC_START:
85444ee6a85SHeiko Carstens 		return chsc_ioctl_start(argp);
85564150adfSMichael Holzheu 	case CHSC_START_SYNC:
85664150adfSMichael Holzheu 		return chsc_ioctl_start_sync(argp);
8579d92a7e1SCornelia Huck 	case CHSC_INFO_CHANNEL_PATH:
85844ee6a85SHeiko Carstens 		return chsc_ioctl_info_channel_path(argp);
8599d92a7e1SCornelia Huck 	case CHSC_INFO_CU:
86044ee6a85SHeiko Carstens 		return chsc_ioctl_info_cu(argp);
8619d92a7e1SCornelia Huck 	case CHSC_INFO_SCH_CU:
86244ee6a85SHeiko Carstens 		return chsc_ioctl_info_sch_cu(argp);
8639d92a7e1SCornelia Huck 	case CHSC_INFO_CI:
86444ee6a85SHeiko Carstens 		return chsc_ioctl_conf_info(argp);
8659d92a7e1SCornelia Huck 	case CHSC_INFO_CCL:
86644ee6a85SHeiko Carstens 		return chsc_ioctl_conf_comp_list(argp);
8679d92a7e1SCornelia Huck 	case CHSC_INFO_CPD:
86844ee6a85SHeiko Carstens 		return chsc_ioctl_chpd(argp);
8699d92a7e1SCornelia Huck 	case CHSC_INFO_DCAL:
87044ee6a85SHeiko Carstens 		return chsc_ioctl_dcal(argp);
871e9a8f32aSMichael Holzheu 	case CHSC_ON_CLOSE_SET:
872e9a8f32aSMichael Holzheu 		return chsc_ioctl_on_close_set(argp);
873e9a8f32aSMichael Holzheu 	case CHSC_ON_CLOSE_REMOVE:
874e9a8f32aSMichael Holzheu 		return chsc_ioctl_on_close_remove();
8759d92a7e1SCornelia Huck 	default: /* unknown ioctl number */
8769d92a7e1SCornelia Huck 		return -ENOIOCTLCMD;
8779d92a7e1SCornelia Huck 	}
8789d92a7e1SCornelia Huck }
8799d92a7e1SCornelia Huck 
8807a9cc6e1SMichael Holzheu static atomic_t chsc_ready_for_use = ATOMIC_INIT(1);
8817a9cc6e1SMichael Holzheu 
chsc_open(struct inode * inode,struct file * file)8827a9cc6e1SMichael Holzheu static int chsc_open(struct inode *inode, struct file *file)
8837a9cc6e1SMichael Holzheu {
8847a9cc6e1SMichael Holzheu 	if (!atomic_dec_and_test(&chsc_ready_for_use)) {
8857a9cc6e1SMichael Holzheu 		atomic_inc(&chsc_ready_for_use);
8867a9cc6e1SMichael Holzheu 		return -EBUSY;
8877a9cc6e1SMichael Holzheu 	}
8887a9cc6e1SMichael Holzheu 	return nonseekable_open(inode, file);
8897a9cc6e1SMichael Holzheu }
8907a9cc6e1SMichael Holzheu 
chsc_release(struct inode * inode,struct file * filp)8917a9cc6e1SMichael Holzheu static int chsc_release(struct inode *inode, struct file *filp)
8927a9cc6e1SMichael Holzheu {
893e9a8f32aSMichael Holzheu 	char dbf[13];
894e9a8f32aSMichael Holzheu 	int ret;
895e9a8f32aSMichael Holzheu 
896e9a8f32aSMichael Holzheu 	mutex_lock(&on_close_mutex);
897e9a8f32aSMichael Holzheu 	if (!on_close_chsc_area)
898e9a8f32aSMichael Holzheu 		goto out_unlock;
899e9a8f32aSMichael Holzheu 	init_completion(&on_close_request->completion);
900e9a8f32aSMichael Holzheu 	CHSC_LOG(0, "on_close");
901e9a8f32aSMichael Holzheu 	chsc_log_command(on_close_chsc_area);
902e9a8f32aSMichael Holzheu 	spin_lock_irq(&chsc_lock);
903e9a8f32aSMichael Holzheu 	ret = chsc_async(on_close_chsc_area, on_close_request);
904e9a8f32aSMichael Holzheu 	spin_unlock_irq(&chsc_lock);
905e9a8f32aSMichael Holzheu 	if (ret == -EINPROGRESS) {
906e9a8f32aSMichael Holzheu 		wait_for_completion(&on_close_request->completion);
907e9a8f32aSMichael Holzheu 		ret = chsc_examine_irb(on_close_request);
908e9a8f32aSMichael Holzheu 	}
90960d081c5SMichael Holzheu 	snprintf(dbf, sizeof(dbf), "relret:%d", ret);
910e9a8f32aSMichael Holzheu 	CHSC_LOG(0, dbf);
911e9a8f32aSMichael Holzheu 	free_page((unsigned long)on_close_chsc_area);
912e9a8f32aSMichael Holzheu 	on_close_chsc_area = NULL;
913e9a8f32aSMichael Holzheu 	kfree(on_close_request);
914e9a8f32aSMichael Holzheu 	on_close_request = NULL;
915e9a8f32aSMichael Holzheu out_unlock:
916e9a8f32aSMichael Holzheu 	mutex_unlock(&on_close_mutex);
9177a9cc6e1SMichael Holzheu 	atomic_inc(&chsc_ready_for_use);
9187a9cc6e1SMichael Holzheu 	return 0;
9197a9cc6e1SMichael Holzheu }
9207a9cc6e1SMichael Holzheu 
9219d92a7e1SCornelia Huck static const struct file_operations chsc_fops = {
9229d92a7e1SCornelia Huck 	.owner = THIS_MODULE,
9237a9cc6e1SMichael Holzheu 	.open = chsc_open,
9247a9cc6e1SMichael Holzheu 	.release = chsc_release,
9259d92a7e1SCornelia Huck 	.unlocked_ioctl = chsc_ioctl,
9269d92a7e1SCornelia Huck 	.compat_ioctl = chsc_ioctl,
9276038f373SArnd Bergmann 	.llseek = no_llseek,
9289d92a7e1SCornelia Huck };
9299d92a7e1SCornelia Huck 
9309d92a7e1SCornelia Huck static struct miscdevice chsc_misc_device = {
9319d92a7e1SCornelia Huck 	.minor = MISC_DYNAMIC_MINOR,
9329d92a7e1SCornelia Huck 	.name = "chsc",
9339d92a7e1SCornelia Huck 	.fops = &chsc_fops,
9349d92a7e1SCornelia Huck };
9359d92a7e1SCornelia Huck 
chsc_misc_init(void)9369d92a7e1SCornelia Huck static int __init chsc_misc_init(void)
9379d92a7e1SCornelia Huck {
9389d92a7e1SCornelia Huck 	return misc_register(&chsc_misc_device);
9399d92a7e1SCornelia Huck }
9409d92a7e1SCornelia Huck 
chsc_misc_cleanup(void)9419d92a7e1SCornelia Huck static void chsc_misc_cleanup(void)
9429d92a7e1SCornelia Huck {
9439d92a7e1SCornelia Huck 	misc_deregister(&chsc_misc_device);
9449d92a7e1SCornelia Huck }
9459d92a7e1SCornelia Huck 
chsc_sch_init(void)9469d92a7e1SCornelia Huck static int __init chsc_sch_init(void)
9479d92a7e1SCornelia Huck {
9489d92a7e1SCornelia Huck 	int ret;
9499d92a7e1SCornelia Huck 
9509d92a7e1SCornelia Huck 	ret = chsc_init_dbfs();
9519d92a7e1SCornelia Huck 	if (ret)
9529d92a7e1SCornelia Huck 		return ret;
9539d92a7e1SCornelia Huck 	isc_register(CHSC_SCH_ISC);
9549d92a7e1SCornelia Huck 	ret = chsc_init_sch_driver();
9559d92a7e1SCornelia Huck 	if (ret)
9569d92a7e1SCornelia Huck 		goto out_dbf;
9579d92a7e1SCornelia Huck 	ret = chsc_misc_init();
9589d92a7e1SCornelia Huck 	if (ret)
9599d92a7e1SCornelia Huck 		goto out_driver;
9609d92a7e1SCornelia Huck 	return ret;
9619d92a7e1SCornelia Huck out_driver:
9629d92a7e1SCornelia Huck 	chsc_cleanup_sch_driver();
9639d92a7e1SCornelia Huck out_dbf:
9649d92a7e1SCornelia Huck 	isc_unregister(CHSC_SCH_ISC);
9659d92a7e1SCornelia Huck 	chsc_remove_dbfs();
9669d92a7e1SCornelia Huck 	return ret;
9679d92a7e1SCornelia Huck }
9689d92a7e1SCornelia Huck 
chsc_sch_exit(void)9699d92a7e1SCornelia Huck static void __exit chsc_sch_exit(void)
9709d92a7e1SCornelia Huck {
9719d92a7e1SCornelia Huck 	chsc_misc_cleanup();
9729d92a7e1SCornelia Huck 	chsc_cleanup_sch_driver();
9739d92a7e1SCornelia Huck 	isc_unregister(CHSC_SCH_ISC);
9749d92a7e1SCornelia Huck 	chsc_remove_dbfs();
9759d92a7e1SCornelia Huck }
9769d92a7e1SCornelia Huck 
9779d92a7e1SCornelia Huck module_init(chsc_sch_init);
9789d92a7e1SCornelia Huck module_exit(chsc_sch_exit);
979