xref: /openbmc/linux/drivers/s390/cio/chsc_sch.c (revision e3b9f1e8)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Driver for s390 chsc subchannels
4  *
5  * Copyright IBM Corp. 2008, 2011
6  *
7  * Author(s): Cornelia Huck <cornelia.huck@de.ibm.com>
8  *
9  */
10 
11 #include <linux/slab.h>
12 #include <linux/compat.h>
13 #include <linux/device.h>
14 #include <linux/module.h>
15 #include <linux/uaccess.h>
16 #include <linux/miscdevice.h>
17 #include <linux/kernel_stat.h>
18 
19 #include <asm/compat.h>
20 #include <asm/cio.h>
21 #include <asm/chsc.h>
22 #include <asm/isc.h>
23 
24 #include "cio.h"
25 #include "cio_debug.h"
26 #include "css.h"
27 #include "chsc_sch.h"
28 #include "ioasm.h"
29 
30 static debug_info_t *chsc_debug_msg_id;
31 static debug_info_t *chsc_debug_log_id;
32 
33 static struct chsc_request *on_close_request;
34 static struct chsc_async_area *on_close_chsc_area;
35 static DEFINE_MUTEX(on_close_mutex);
36 
37 #define CHSC_MSG(imp, args...) do {					\
38 		debug_sprintf_event(chsc_debug_msg_id, imp , ##args);	\
39 	} while (0)
40 
41 #define CHSC_LOG(imp, txt) do {					\
42 		debug_text_event(chsc_debug_log_id, imp , txt);	\
43 	} while (0)
44 
45 static void CHSC_LOG_HEX(int level, void *data, int length)
46 {
47 	debug_event(chsc_debug_log_id, level, data, length);
48 }
49 
50 MODULE_AUTHOR("IBM Corporation");
51 MODULE_DESCRIPTION("driver for s390 chsc subchannels");
52 MODULE_LICENSE("GPL");
53 
54 static void chsc_subchannel_irq(struct subchannel *sch)
55 {
56 	struct chsc_private *private = dev_get_drvdata(&sch->dev);
57 	struct chsc_request *request = private->request;
58 	struct irb *irb = this_cpu_ptr(&cio_irb);
59 
60 	CHSC_LOG(4, "irb");
61 	CHSC_LOG_HEX(4, irb, sizeof(*irb));
62 	inc_irq_stat(IRQIO_CSC);
63 
64 	/* Copy irb to provided request and set done. */
65 	if (!request) {
66 		CHSC_MSG(0, "Interrupt on sch 0.%x.%04x with no request\n",
67 			 sch->schid.ssid, sch->schid.sch_no);
68 		return;
69 	}
70 	private->request = NULL;
71 	memcpy(&request->irb, irb, sizeof(*irb));
72 	cio_update_schib(sch);
73 	complete(&request->completion);
74 	put_device(&sch->dev);
75 }
76 
77 static int chsc_subchannel_probe(struct subchannel *sch)
78 {
79 	struct chsc_private *private;
80 	int ret;
81 
82 	CHSC_MSG(6, "Detected chsc subchannel 0.%x.%04x\n",
83 		 sch->schid.ssid, sch->schid.sch_no);
84 	sch->isc = CHSC_SCH_ISC;
85 	private = kzalloc(sizeof(*private), GFP_KERNEL);
86 	if (!private)
87 		return -ENOMEM;
88 	dev_set_drvdata(&sch->dev, private);
89 	ret = cio_enable_subchannel(sch, (u32)(unsigned long)sch);
90 	if (ret) {
91 		CHSC_MSG(0, "Failed to enable 0.%x.%04x: %d\n",
92 			 sch->schid.ssid, sch->schid.sch_no, ret);
93 		dev_set_drvdata(&sch->dev, NULL);
94 		kfree(private);
95 	} else {
96 		if (dev_get_uevent_suppress(&sch->dev)) {
97 			dev_set_uevent_suppress(&sch->dev, 0);
98 			kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
99 		}
100 	}
101 	return ret;
102 }
103 
104 static int chsc_subchannel_remove(struct subchannel *sch)
105 {
106 	struct chsc_private *private;
107 
108 	cio_disable_subchannel(sch);
109 	private = dev_get_drvdata(&sch->dev);
110 	dev_set_drvdata(&sch->dev, NULL);
111 	if (private->request) {
112 		complete(&private->request->completion);
113 		put_device(&sch->dev);
114 	}
115 	kfree(private);
116 	return 0;
117 }
118 
119 static void chsc_subchannel_shutdown(struct subchannel *sch)
120 {
121 	cio_disable_subchannel(sch);
122 }
123 
124 static int chsc_subchannel_prepare(struct subchannel *sch)
125 {
126 	int cc;
127 	struct schib schib;
128 	/*
129 	 * Don't allow suspend while the subchannel is not idle
130 	 * since we don't have a way to clear the subchannel and
131 	 * cannot disable it with a request running.
132 	 */
133 	cc = stsch(sch->schid, &schib);
134 	if (!cc && scsw_stctl(&schib.scsw))
135 		return -EAGAIN;
136 	return 0;
137 }
138 
139 static int chsc_subchannel_freeze(struct subchannel *sch)
140 {
141 	return cio_disable_subchannel(sch);
142 }
143 
144 static int chsc_subchannel_restore(struct subchannel *sch)
145 {
146 	return cio_enable_subchannel(sch, (u32)(unsigned long)sch);
147 }
148 
149 static struct css_device_id chsc_subchannel_ids[] = {
150 	{ .match_flags = 0x1, .type =SUBCHANNEL_TYPE_CHSC, },
151 	{ /* end of list */ },
152 };
153 MODULE_DEVICE_TABLE(css, chsc_subchannel_ids);
154 
155 static struct css_driver chsc_subchannel_driver = {
156 	.drv = {
157 		.owner = THIS_MODULE,
158 		.name = "chsc_subchannel",
159 	},
160 	.subchannel_type = chsc_subchannel_ids,
161 	.irq = chsc_subchannel_irq,
162 	.probe = chsc_subchannel_probe,
163 	.remove = chsc_subchannel_remove,
164 	.shutdown = chsc_subchannel_shutdown,
165 	.prepare = chsc_subchannel_prepare,
166 	.freeze = chsc_subchannel_freeze,
167 	.thaw = chsc_subchannel_restore,
168 	.restore = chsc_subchannel_restore,
169 };
170 
171 static int __init chsc_init_dbfs(void)
172 {
173 	chsc_debug_msg_id = debug_register("chsc_msg", 8, 1, 4 * sizeof(long));
174 	if (!chsc_debug_msg_id)
175 		goto out;
176 	debug_register_view(chsc_debug_msg_id, &debug_sprintf_view);
177 	debug_set_level(chsc_debug_msg_id, 2);
178 	chsc_debug_log_id = debug_register("chsc_log", 16, 1, 16);
179 	if (!chsc_debug_log_id)
180 		goto out;
181 	debug_register_view(chsc_debug_log_id, &debug_hex_ascii_view);
182 	debug_set_level(chsc_debug_log_id, 2);
183 	return 0;
184 out:
185 	debug_unregister(chsc_debug_msg_id);
186 	return -ENOMEM;
187 }
188 
189 static void chsc_remove_dbfs(void)
190 {
191 	debug_unregister(chsc_debug_log_id);
192 	debug_unregister(chsc_debug_msg_id);
193 }
194 
195 static int __init chsc_init_sch_driver(void)
196 {
197 	return css_driver_register(&chsc_subchannel_driver);
198 }
199 
200 static void chsc_cleanup_sch_driver(void)
201 {
202 	css_driver_unregister(&chsc_subchannel_driver);
203 }
204 
205 static DEFINE_SPINLOCK(chsc_lock);
206 
207 static int chsc_subchannel_match_next_free(struct device *dev, void *data)
208 {
209 	struct subchannel *sch = to_subchannel(dev);
210 
211 	return sch->schib.pmcw.ena && !scsw_fctl(&sch->schib.scsw);
212 }
213 
214 static struct subchannel *chsc_get_next_subchannel(struct subchannel *sch)
215 {
216 	struct device *dev;
217 
218 	dev = driver_find_device(&chsc_subchannel_driver.drv,
219 				 sch ? &sch->dev : NULL, NULL,
220 				 chsc_subchannel_match_next_free);
221 	return dev ? to_subchannel(dev) : NULL;
222 }
223 
224 /**
225  * chsc_async() - try to start a chsc request asynchronously
226  * @chsc_area: request to be started
227  * @request: request structure to associate
228  *
229  * Tries to start a chsc request on one of the existing chsc subchannels.
230  * Returns:
231  *  %0 if the request was performed synchronously
232  *  %-EINPROGRESS if the request was successfully started
233  *  %-EBUSY if all chsc subchannels are busy
234  *  %-ENODEV if no chsc subchannels are available
235  * Context:
236  *  interrupts disabled, chsc_lock held
237  */
238 static int chsc_async(struct chsc_async_area *chsc_area,
239 		      struct chsc_request *request)
240 {
241 	int cc;
242 	struct chsc_private *private;
243 	struct subchannel *sch = NULL;
244 	int ret = -ENODEV;
245 	char dbf[10];
246 
247 	chsc_area->header.key = PAGE_DEFAULT_KEY >> 4;
248 	while ((sch = chsc_get_next_subchannel(sch))) {
249 		spin_lock(sch->lock);
250 		private = dev_get_drvdata(&sch->dev);
251 		if (private->request) {
252 			spin_unlock(sch->lock);
253 			ret = -EBUSY;
254 			continue;
255 		}
256 		chsc_area->header.sid = sch->schid;
257 		CHSC_LOG(2, "schid");
258 		CHSC_LOG_HEX(2, &sch->schid, sizeof(sch->schid));
259 		cc = chsc(chsc_area);
260 		snprintf(dbf, sizeof(dbf), "cc:%d", cc);
261 		CHSC_LOG(2, dbf);
262 		switch (cc) {
263 		case 0:
264 			ret = 0;
265 			break;
266 		case 1:
267 			sch->schib.scsw.cmd.fctl |= SCSW_FCTL_START_FUNC;
268 			ret = -EINPROGRESS;
269 			private->request = request;
270 			break;
271 		case 2:
272 			ret = -EBUSY;
273 			break;
274 		default:
275 			ret = -ENODEV;
276 		}
277 		spin_unlock(sch->lock);
278 		CHSC_MSG(2, "chsc on 0.%x.%04x returned cc=%d\n",
279 			 sch->schid.ssid, sch->schid.sch_no, cc);
280 		if (ret == -EINPROGRESS)
281 			return -EINPROGRESS;
282 		put_device(&sch->dev);
283 		if (ret == 0)
284 			return 0;
285 	}
286 	return ret;
287 }
288 
289 static void chsc_log_command(void *chsc_area)
290 {
291 	char dbf[10];
292 
293 	snprintf(dbf, sizeof(dbf), "CHSC:%x", ((uint16_t *)chsc_area)[1]);
294 	CHSC_LOG(0, dbf);
295 	CHSC_LOG_HEX(0, chsc_area, 32);
296 }
297 
298 static int chsc_examine_irb(struct chsc_request *request)
299 {
300 	int backed_up;
301 
302 	if (!(scsw_stctl(&request->irb.scsw) & SCSW_STCTL_STATUS_PEND))
303 		return -EIO;
304 	backed_up = scsw_cstat(&request->irb.scsw) & SCHN_STAT_CHAIN_CHECK;
305 	request->irb.scsw.cmd.cstat &= ~SCHN_STAT_CHAIN_CHECK;
306 	if (scsw_cstat(&request->irb.scsw) == 0)
307 		return 0;
308 	if (!backed_up)
309 		return 0;
310 	if (scsw_cstat(&request->irb.scsw) & SCHN_STAT_PROG_CHECK)
311 		return -EIO;
312 	if (scsw_cstat(&request->irb.scsw) & SCHN_STAT_PROT_CHECK)
313 		return -EPERM;
314 	if (scsw_cstat(&request->irb.scsw) & SCHN_STAT_CHN_DATA_CHK)
315 		return -EAGAIN;
316 	if (scsw_cstat(&request->irb.scsw) & SCHN_STAT_CHN_CTRL_CHK)
317 		return -EAGAIN;
318 	return -EIO;
319 }
320 
321 static int chsc_ioctl_start(void __user *user_area)
322 {
323 	struct chsc_request *request;
324 	struct chsc_async_area *chsc_area;
325 	int ret;
326 	char dbf[10];
327 
328 	if (!css_general_characteristics.dynio)
329 		/* It makes no sense to try. */
330 		return -EOPNOTSUPP;
331 	chsc_area = (void *)get_zeroed_page(GFP_DMA | GFP_KERNEL);
332 	if (!chsc_area)
333 		return -ENOMEM;
334 	request = kzalloc(sizeof(*request), GFP_KERNEL);
335 	if (!request) {
336 		ret = -ENOMEM;
337 		goto out_free;
338 	}
339 	init_completion(&request->completion);
340 	if (copy_from_user(chsc_area, user_area, PAGE_SIZE)) {
341 		ret = -EFAULT;
342 		goto out_free;
343 	}
344 	chsc_log_command(chsc_area);
345 	spin_lock_irq(&chsc_lock);
346 	ret = chsc_async(chsc_area, request);
347 	spin_unlock_irq(&chsc_lock);
348 	if (ret == -EINPROGRESS) {
349 		wait_for_completion(&request->completion);
350 		ret = chsc_examine_irb(request);
351 	}
352 	/* copy area back to user */
353 	if (!ret)
354 		if (copy_to_user(user_area, chsc_area, PAGE_SIZE))
355 			ret = -EFAULT;
356 out_free:
357 	snprintf(dbf, sizeof(dbf), "ret:%d", ret);
358 	CHSC_LOG(0, dbf);
359 	kfree(request);
360 	free_page((unsigned long)chsc_area);
361 	return ret;
362 }
363 
364 static int chsc_ioctl_on_close_set(void __user *user_area)
365 {
366 	char dbf[13];
367 	int ret;
368 
369 	mutex_lock(&on_close_mutex);
370 	if (on_close_chsc_area) {
371 		ret = -EBUSY;
372 		goto out_unlock;
373 	}
374 	on_close_request = kzalloc(sizeof(*on_close_request), GFP_KERNEL);
375 	if (!on_close_request) {
376 		ret = -ENOMEM;
377 		goto out_unlock;
378 	}
379 	on_close_chsc_area = (void *)get_zeroed_page(GFP_DMA | GFP_KERNEL);
380 	if (!on_close_chsc_area) {
381 		ret = -ENOMEM;
382 		goto out_free_request;
383 	}
384 	if (copy_from_user(on_close_chsc_area, user_area, PAGE_SIZE)) {
385 		ret = -EFAULT;
386 		goto out_free_chsc;
387 	}
388 	ret = 0;
389 	goto out_unlock;
390 
391 out_free_chsc:
392 	free_page((unsigned long)on_close_chsc_area);
393 	on_close_chsc_area = NULL;
394 out_free_request:
395 	kfree(on_close_request);
396 	on_close_request = NULL;
397 out_unlock:
398 	mutex_unlock(&on_close_mutex);
399 	snprintf(dbf, sizeof(dbf), "ocsret:%d", ret);
400 	CHSC_LOG(0, dbf);
401 	return ret;
402 }
403 
404 static int chsc_ioctl_on_close_remove(void)
405 {
406 	char dbf[13];
407 	int ret;
408 
409 	mutex_lock(&on_close_mutex);
410 	if (!on_close_chsc_area) {
411 		ret = -ENOENT;
412 		goto out_unlock;
413 	}
414 	free_page((unsigned long)on_close_chsc_area);
415 	on_close_chsc_area = NULL;
416 	kfree(on_close_request);
417 	on_close_request = NULL;
418 	ret = 0;
419 out_unlock:
420 	mutex_unlock(&on_close_mutex);
421 	snprintf(dbf, sizeof(dbf), "ocrret:%d", ret);
422 	CHSC_LOG(0, dbf);
423 	return ret;
424 }
425 
426 static int chsc_ioctl_start_sync(void __user *user_area)
427 {
428 	struct chsc_sync_area *chsc_area;
429 	int ret, ccode;
430 
431 	chsc_area = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
432 	if (!chsc_area)
433 		return -ENOMEM;
434 	if (copy_from_user(chsc_area, user_area, PAGE_SIZE)) {
435 		ret = -EFAULT;
436 		goto out_free;
437 	}
438 	if (chsc_area->header.code & 0x4000) {
439 		ret = -EINVAL;
440 		goto out_free;
441 	}
442 	chsc_log_command(chsc_area);
443 	ccode = chsc(chsc_area);
444 	if (ccode != 0) {
445 		ret = -EIO;
446 		goto out_free;
447 	}
448 	if (copy_to_user(user_area, chsc_area, PAGE_SIZE))
449 		ret = -EFAULT;
450 	else
451 		ret = 0;
452 out_free:
453 	free_page((unsigned long)chsc_area);
454 	return ret;
455 }
456 
457 static int chsc_ioctl_info_channel_path(void __user *user_cd)
458 {
459 	struct chsc_chp_cd *cd;
460 	int ret, ccode;
461 	struct {
462 		struct chsc_header request;
463 		u32 : 2;
464 		u32 m : 1;
465 		u32 : 1;
466 		u32 fmt1 : 4;
467 		u32 cssid : 8;
468 		u32 : 8;
469 		u32 first_chpid : 8;
470 		u32 : 24;
471 		u32 last_chpid : 8;
472 		u32 : 32;
473 		struct chsc_header response;
474 		u8 data[PAGE_SIZE - 20];
475 	} __attribute__ ((packed)) *scpcd_area;
476 
477 	scpcd_area = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
478 	if (!scpcd_area)
479 		return -ENOMEM;
480 	cd = kzalloc(sizeof(*cd), GFP_KERNEL);
481 	if (!cd) {
482 		ret = -ENOMEM;
483 		goto out_free;
484 	}
485 	if (copy_from_user(cd, user_cd, sizeof(*cd))) {
486 		ret = -EFAULT;
487 		goto out_free;
488 	}
489 	scpcd_area->request.length = 0x0010;
490 	scpcd_area->request.code = 0x0028;
491 	scpcd_area->m = cd->m;
492 	scpcd_area->fmt1 = cd->fmt;
493 	scpcd_area->cssid = cd->chpid.cssid;
494 	scpcd_area->first_chpid = cd->chpid.id;
495 	scpcd_area->last_chpid = cd->chpid.id;
496 
497 	ccode = chsc(scpcd_area);
498 	if (ccode != 0) {
499 		ret = -EIO;
500 		goto out_free;
501 	}
502 	if (scpcd_area->response.code != 0x0001) {
503 		ret = -EIO;
504 		CHSC_MSG(0, "scpcd: response code=%x\n",
505 			 scpcd_area->response.code);
506 		goto out_free;
507 	}
508 	memcpy(&cd->cpcb, &scpcd_area->response, scpcd_area->response.length);
509 	if (copy_to_user(user_cd, cd, sizeof(*cd)))
510 		ret = -EFAULT;
511 	else
512 		ret = 0;
513 out_free:
514 	kfree(cd);
515 	free_page((unsigned long)scpcd_area);
516 	return ret;
517 }
518 
519 static int chsc_ioctl_info_cu(void __user *user_cd)
520 {
521 	struct chsc_cu_cd *cd;
522 	int ret, ccode;
523 	struct {
524 		struct chsc_header request;
525 		u32 : 2;
526 		u32 m : 1;
527 		u32 : 1;
528 		u32 fmt1 : 4;
529 		u32 cssid : 8;
530 		u32 : 8;
531 		u32 first_cun : 8;
532 		u32 : 24;
533 		u32 last_cun : 8;
534 		u32 : 32;
535 		struct chsc_header response;
536 		u8 data[PAGE_SIZE - 20];
537 	} __attribute__ ((packed)) *scucd_area;
538 
539 	scucd_area = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
540 	if (!scucd_area)
541 		return -ENOMEM;
542 	cd = kzalloc(sizeof(*cd), GFP_KERNEL);
543 	if (!cd) {
544 		ret = -ENOMEM;
545 		goto out_free;
546 	}
547 	if (copy_from_user(cd, user_cd, sizeof(*cd))) {
548 		ret = -EFAULT;
549 		goto out_free;
550 	}
551 	scucd_area->request.length = 0x0010;
552 	scucd_area->request.code = 0x0026;
553 	scucd_area->m = cd->m;
554 	scucd_area->fmt1 = cd->fmt;
555 	scucd_area->cssid = cd->cssid;
556 	scucd_area->first_cun = cd->cun;
557 	scucd_area->last_cun = cd->cun;
558 
559 	ccode = chsc(scucd_area);
560 	if (ccode != 0) {
561 		ret = -EIO;
562 		goto out_free;
563 	}
564 	if (scucd_area->response.code != 0x0001) {
565 		ret = -EIO;
566 		CHSC_MSG(0, "scucd: response code=%x\n",
567 			 scucd_area->response.code);
568 		goto out_free;
569 	}
570 	memcpy(&cd->cucb, &scucd_area->response, scucd_area->response.length);
571 	if (copy_to_user(user_cd, cd, sizeof(*cd)))
572 		ret = -EFAULT;
573 	else
574 		ret = 0;
575 out_free:
576 	kfree(cd);
577 	free_page((unsigned long)scucd_area);
578 	return ret;
579 }
580 
581 static int chsc_ioctl_info_sch_cu(void __user *user_cud)
582 {
583 	struct chsc_sch_cud *cud;
584 	int ret, ccode;
585 	struct {
586 		struct chsc_header request;
587 		u32 : 2;
588 		u32 m : 1;
589 		u32 : 5;
590 		u32 fmt1 : 4;
591 		u32 : 2;
592 		u32 ssid : 2;
593 		u32 first_sch : 16;
594 		u32 : 8;
595 		u32 cssid : 8;
596 		u32 last_sch : 16;
597 		u32 : 32;
598 		struct chsc_header response;
599 		u8 data[PAGE_SIZE - 20];
600 	} __attribute__ ((packed)) *sscud_area;
601 
602 	sscud_area = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
603 	if (!sscud_area)
604 		return -ENOMEM;
605 	cud = kzalloc(sizeof(*cud), GFP_KERNEL);
606 	if (!cud) {
607 		ret = -ENOMEM;
608 		goto out_free;
609 	}
610 	if (copy_from_user(cud, user_cud, sizeof(*cud))) {
611 		ret = -EFAULT;
612 		goto out_free;
613 	}
614 	sscud_area->request.length = 0x0010;
615 	sscud_area->request.code = 0x0006;
616 	sscud_area->m = cud->schid.m;
617 	sscud_area->fmt1 = cud->fmt;
618 	sscud_area->ssid = cud->schid.ssid;
619 	sscud_area->first_sch = cud->schid.sch_no;
620 	sscud_area->cssid = cud->schid.cssid;
621 	sscud_area->last_sch = cud->schid.sch_no;
622 
623 	ccode = chsc(sscud_area);
624 	if (ccode != 0) {
625 		ret = -EIO;
626 		goto out_free;
627 	}
628 	if (sscud_area->response.code != 0x0001) {
629 		ret = -EIO;
630 		CHSC_MSG(0, "sscud: response code=%x\n",
631 			 sscud_area->response.code);
632 		goto out_free;
633 	}
634 	memcpy(&cud->scub, &sscud_area->response, sscud_area->response.length);
635 	if (copy_to_user(user_cud, cud, sizeof(*cud)))
636 		ret = -EFAULT;
637 	else
638 		ret = 0;
639 out_free:
640 	kfree(cud);
641 	free_page((unsigned long)sscud_area);
642 	return ret;
643 }
644 
645 static int chsc_ioctl_conf_info(void __user *user_ci)
646 {
647 	struct chsc_conf_info *ci;
648 	int ret, ccode;
649 	struct {
650 		struct chsc_header request;
651 		u32 : 2;
652 		u32 m : 1;
653 		u32 : 1;
654 		u32 fmt1 : 4;
655 		u32 cssid : 8;
656 		u32 : 6;
657 		u32 ssid : 2;
658 		u32 : 8;
659 		u64 : 64;
660 		struct chsc_header response;
661 		u8 data[PAGE_SIZE - 20];
662 	} __attribute__ ((packed)) *sci_area;
663 
664 	sci_area = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
665 	if (!sci_area)
666 		return -ENOMEM;
667 	ci = kzalloc(sizeof(*ci), GFP_KERNEL);
668 	if (!ci) {
669 		ret = -ENOMEM;
670 		goto out_free;
671 	}
672 	if (copy_from_user(ci, user_ci, sizeof(*ci))) {
673 		ret = -EFAULT;
674 		goto out_free;
675 	}
676 	sci_area->request.length = 0x0010;
677 	sci_area->request.code = 0x0012;
678 	sci_area->m = ci->id.m;
679 	sci_area->fmt1 = ci->fmt;
680 	sci_area->cssid = ci->id.cssid;
681 	sci_area->ssid = ci->id.ssid;
682 
683 	ccode = chsc(sci_area);
684 	if (ccode != 0) {
685 		ret = -EIO;
686 		goto out_free;
687 	}
688 	if (sci_area->response.code != 0x0001) {
689 		ret = -EIO;
690 		CHSC_MSG(0, "sci: response code=%x\n",
691 			 sci_area->response.code);
692 		goto out_free;
693 	}
694 	memcpy(&ci->scid, &sci_area->response, sci_area->response.length);
695 	if (copy_to_user(user_ci, ci, sizeof(*ci)))
696 		ret = -EFAULT;
697 	else
698 		ret = 0;
699 out_free:
700 	kfree(ci);
701 	free_page((unsigned long)sci_area);
702 	return ret;
703 }
704 
705 static int chsc_ioctl_conf_comp_list(void __user *user_ccl)
706 {
707 	struct chsc_comp_list *ccl;
708 	int ret, ccode;
709 	struct {
710 		struct chsc_header request;
711 		u32 ctype : 8;
712 		u32 : 4;
713 		u32 fmt : 4;
714 		u32 : 16;
715 		u64 : 64;
716 		u32 list_parm[2];
717 		u64 : 64;
718 		struct chsc_header response;
719 		u8 data[PAGE_SIZE - 36];
720 	} __attribute__ ((packed)) *sccl_area;
721 	struct {
722 		u32 m : 1;
723 		u32 : 31;
724 		u32 cssid : 8;
725 		u32 : 16;
726 		u32 chpid : 8;
727 	} __attribute__ ((packed)) *chpid_parm;
728 	struct {
729 		u32 f_cssid : 8;
730 		u32 l_cssid : 8;
731 		u32 : 16;
732 		u32 res;
733 	} __attribute__ ((packed)) *cssids_parm;
734 
735 	sccl_area = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
736 	if (!sccl_area)
737 		return -ENOMEM;
738 	ccl = kzalloc(sizeof(*ccl), GFP_KERNEL);
739 	if (!ccl) {
740 		ret = -ENOMEM;
741 		goto out_free;
742 	}
743 	if (copy_from_user(ccl, user_ccl, sizeof(*ccl))) {
744 		ret = -EFAULT;
745 		goto out_free;
746 	}
747 	sccl_area->request.length = 0x0020;
748 	sccl_area->request.code = 0x0030;
749 	sccl_area->fmt = ccl->req.fmt;
750 	sccl_area->ctype = ccl->req.ctype;
751 	switch (sccl_area->ctype) {
752 	case CCL_CU_ON_CHP:
753 	case CCL_IOP_CHP:
754 		chpid_parm = (void *)&sccl_area->list_parm;
755 		chpid_parm->m = ccl->req.chpid.m;
756 		chpid_parm->cssid = ccl->req.chpid.chp.cssid;
757 		chpid_parm->chpid = ccl->req.chpid.chp.id;
758 		break;
759 	case CCL_CSS_IMG:
760 	case CCL_CSS_IMG_CONF_CHAR:
761 		cssids_parm = (void *)&sccl_area->list_parm;
762 		cssids_parm->f_cssid = ccl->req.cssids.f_cssid;
763 		cssids_parm->l_cssid = ccl->req.cssids.l_cssid;
764 		break;
765 	}
766 	ccode = chsc(sccl_area);
767 	if (ccode != 0) {
768 		ret = -EIO;
769 		goto out_free;
770 	}
771 	if (sccl_area->response.code != 0x0001) {
772 		ret = -EIO;
773 		CHSC_MSG(0, "sccl: response code=%x\n",
774 			 sccl_area->response.code);
775 		goto out_free;
776 	}
777 	memcpy(&ccl->sccl, &sccl_area->response, sccl_area->response.length);
778 	if (copy_to_user(user_ccl, ccl, sizeof(*ccl)))
779 		ret = -EFAULT;
780 	else
781 		ret = 0;
782 out_free:
783 	kfree(ccl);
784 	free_page((unsigned long)sccl_area);
785 	return ret;
786 }
787 
788 static int chsc_ioctl_chpd(void __user *user_chpd)
789 {
790 	struct chsc_scpd *scpd_area;
791 	struct chsc_cpd_info *chpd;
792 	int ret;
793 
794 	chpd = kzalloc(sizeof(*chpd), GFP_KERNEL);
795 	scpd_area = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
796 	if (!scpd_area || !chpd) {
797 		ret = -ENOMEM;
798 		goto out_free;
799 	}
800 	if (copy_from_user(chpd, user_chpd, sizeof(*chpd))) {
801 		ret = -EFAULT;
802 		goto out_free;
803 	}
804 	ret = chsc_determine_channel_path_desc(chpd->chpid, chpd->fmt,
805 					       chpd->rfmt, chpd->c, chpd->m,
806 					       scpd_area);
807 	if (ret)
808 		goto out_free;
809 	memcpy(&chpd->chpdb, &scpd_area->response, scpd_area->response.length);
810 	if (copy_to_user(user_chpd, chpd, sizeof(*chpd)))
811 		ret = -EFAULT;
812 out_free:
813 	kfree(chpd);
814 	free_page((unsigned long)scpd_area);
815 	return ret;
816 }
817 
818 static int chsc_ioctl_dcal(void __user *user_dcal)
819 {
820 	struct chsc_dcal *dcal;
821 	int ret, ccode;
822 	struct {
823 		struct chsc_header request;
824 		u32 atype : 8;
825 		u32 : 4;
826 		u32 fmt : 4;
827 		u32 : 16;
828 		u32 res0[2];
829 		u32 list_parm[2];
830 		u32 res1[2];
831 		struct chsc_header response;
832 		u8 data[PAGE_SIZE - 36];
833 	} __attribute__ ((packed)) *sdcal_area;
834 
835 	sdcal_area = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
836 	if (!sdcal_area)
837 		return -ENOMEM;
838 	dcal = kzalloc(sizeof(*dcal), GFP_KERNEL);
839 	if (!dcal) {
840 		ret = -ENOMEM;
841 		goto out_free;
842 	}
843 	if (copy_from_user(dcal, user_dcal, sizeof(*dcal))) {
844 		ret = -EFAULT;
845 		goto out_free;
846 	}
847 	sdcal_area->request.length = 0x0020;
848 	sdcal_area->request.code = 0x0034;
849 	sdcal_area->atype = dcal->req.atype;
850 	sdcal_area->fmt = dcal->req.fmt;
851 	memcpy(&sdcal_area->list_parm, &dcal->req.list_parm,
852 	       sizeof(sdcal_area->list_parm));
853 
854 	ccode = chsc(sdcal_area);
855 	if (ccode != 0) {
856 		ret = -EIO;
857 		goto out_free;
858 	}
859 	if (sdcal_area->response.code != 0x0001) {
860 		ret = -EIO;
861 		CHSC_MSG(0, "sdcal: response code=%x\n",
862 			 sdcal_area->response.code);
863 		goto out_free;
864 	}
865 	memcpy(&dcal->sdcal, &sdcal_area->response,
866 	       sdcal_area->response.length);
867 	if (copy_to_user(user_dcal, dcal, sizeof(*dcal)))
868 		ret = -EFAULT;
869 	else
870 		ret = 0;
871 out_free:
872 	kfree(dcal);
873 	free_page((unsigned long)sdcal_area);
874 	return ret;
875 }
876 
877 static long chsc_ioctl(struct file *filp, unsigned int cmd,
878 		       unsigned long arg)
879 {
880 	void __user *argp;
881 
882 	CHSC_MSG(2, "chsc_ioctl called, cmd=%x\n", cmd);
883 	if (is_compat_task())
884 		argp = compat_ptr(arg);
885 	else
886 		argp = (void __user *)arg;
887 	switch (cmd) {
888 	case CHSC_START:
889 		return chsc_ioctl_start(argp);
890 	case CHSC_START_SYNC:
891 		return chsc_ioctl_start_sync(argp);
892 	case CHSC_INFO_CHANNEL_PATH:
893 		return chsc_ioctl_info_channel_path(argp);
894 	case CHSC_INFO_CU:
895 		return chsc_ioctl_info_cu(argp);
896 	case CHSC_INFO_SCH_CU:
897 		return chsc_ioctl_info_sch_cu(argp);
898 	case CHSC_INFO_CI:
899 		return chsc_ioctl_conf_info(argp);
900 	case CHSC_INFO_CCL:
901 		return chsc_ioctl_conf_comp_list(argp);
902 	case CHSC_INFO_CPD:
903 		return chsc_ioctl_chpd(argp);
904 	case CHSC_INFO_DCAL:
905 		return chsc_ioctl_dcal(argp);
906 	case CHSC_ON_CLOSE_SET:
907 		return chsc_ioctl_on_close_set(argp);
908 	case CHSC_ON_CLOSE_REMOVE:
909 		return chsc_ioctl_on_close_remove();
910 	default: /* unknown ioctl number */
911 		return -ENOIOCTLCMD;
912 	}
913 }
914 
915 static atomic_t chsc_ready_for_use = ATOMIC_INIT(1);
916 
917 static int chsc_open(struct inode *inode, struct file *file)
918 {
919 	if (!atomic_dec_and_test(&chsc_ready_for_use)) {
920 		atomic_inc(&chsc_ready_for_use);
921 		return -EBUSY;
922 	}
923 	return nonseekable_open(inode, file);
924 }
925 
926 static int chsc_release(struct inode *inode, struct file *filp)
927 {
928 	char dbf[13];
929 	int ret;
930 
931 	mutex_lock(&on_close_mutex);
932 	if (!on_close_chsc_area)
933 		goto out_unlock;
934 	init_completion(&on_close_request->completion);
935 	CHSC_LOG(0, "on_close");
936 	chsc_log_command(on_close_chsc_area);
937 	spin_lock_irq(&chsc_lock);
938 	ret = chsc_async(on_close_chsc_area, on_close_request);
939 	spin_unlock_irq(&chsc_lock);
940 	if (ret == -EINPROGRESS) {
941 		wait_for_completion(&on_close_request->completion);
942 		ret = chsc_examine_irb(on_close_request);
943 	}
944 	snprintf(dbf, sizeof(dbf), "relret:%d", ret);
945 	CHSC_LOG(0, dbf);
946 	free_page((unsigned long)on_close_chsc_area);
947 	on_close_chsc_area = NULL;
948 	kfree(on_close_request);
949 	on_close_request = NULL;
950 out_unlock:
951 	mutex_unlock(&on_close_mutex);
952 	atomic_inc(&chsc_ready_for_use);
953 	return 0;
954 }
955 
956 static const struct file_operations chsc_fops = {
957 	.owner = THIS_MODULE,
958 	.open = chsc_open,
959 	.release = chsc_release,
960 	.unlocked_ioctl = chsc_ioctl,
961 	.compat_ioctl = chsc_ioctl,
962 	.llseek = no_llseek,
963 };
964 
965 static struct miscdevice chsc_misc_device = {
966 	.minor = MISC_DYNAMIC_MINOR,
967 	.name = "chsc",
968 	.fops = &chsc_fops,
969 };
970 
971 static int __init chsc_misc_init(void)
972 {
973 	return misc_register(&chsc_misc_device);
974 }
975 
976 static void chsc_misc_cleanup(void)
977 {
978 	misc_deregister(&chsc_misc_device);
979 }
980 
981 static int __init chsc_sch_init(void)
982 {
983 	int ret;
984 
985 	ret = chsc_init_dbfs();
986 	if (ret)
987 		return ret;
988 	isc_register(CHSC_SCH_ISC);
989 	ret = chsc_init_sch_driver();
990 	if (ret)
991 		goto out_dbf;
992 	ret = chsc_misc_init();
993 	if (ret)
994 		goto out_driver;
995 	return ret;
996 out_driver:
997 	chsc_cleanup_sch_driver();
998 out_dbf:
999 	isc_unregister(CHSC_SCH_ISC);
1000 	chsc_remove_dbfs();
1001 	return ret;
1002 }
1003 
1004 static void __exit chsc_sch_exit(void)
1005 {
1006 	chsc_misc_cleanup();
1007 	chsc_cleanup_sch_driver();
1008 	isc_unregister(CHSC_SCH_ISC);
1009 	chsc_remove_dbfs();
1010 }
1011 
1012 module_init(chsc_sch_init);
1013 module_exit(chsc_sch_exit);
1014