xref: /linux/drivers/s390/cio/chsc_sch.c (revision 375d5d57545f53d2bc3e15ada0e9da360fc68bea)
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/device.h>
13 #include <linux/io.h>
14 #include <linux/module.h>
15 #include <linux/uaccess.h>
16 #include <linux/miscdevice.h>
17 #include <linux/kernel_stat.h>
18 #include <linux/cleanup.h>
19 
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 
CHSC_LOG_HEX(int level,void * data,int length)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 
chsc_subchannel_irq(struct subchannel * sch)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 
chsc_subchannel_probe(struct subchannel * sch)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_obj(*private);
86 	if (!private)
87 		return -ENOMEM;
88 	dev_set_drvdata(&sch->dev, private);
89 	ret = cio_enable_subchannel(sch, (u32)virt_to_phys(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 	}
96 	return ret;
97 }
98 
chsc_subchannel_remove(struct subchannel * sch)99 static void chsc_subchannel_remove(struct subchannel *sch)
100 {
101 	struct chsc_private *private;
102 
103 	cio_disable_subchannel(sch);
104 	private = dev_get_drvdata(&sch->dev);
105 	dev_set_drvdata(&sch->dev, NULL);
106 	if (private->request) {
107 		complete(&private->request->completion);
108 		put_device(&sch->dev);
109 	}
110 	kfree(private);
111 }
112 
chsc_subchannel_shutdown(struct subchannel * sch)113 static void chsc_subchannel_shutdown(struct subchannel *sch)
114 {
115 	cio_disable_subchannel(sch);
116 }
117 
118 static struct css_device_id chsc_subchannel_ids[] = {
119 	{ .match_flags = 0x1, .type =SUBCHANNEL_TYPE_CHSC, },
120 	{ /* end of list */ },
121 };
122 MODULE_DEVICE_TABLE(css, chsc_subchannel_ids);
123 
124 static struct css_driver chsc_subchannel_driver = {
125 	.drv = {
126 		.owner = THIS_MODULE,
127 		.name = "chsc_subchannel",
128 	},
129 	.subchannel_type = chsc_subchannel_ids,
130 	.irq = chsc_subchannel_irq,
131 	.probe = chsc_subchannel_probe,
132 	.remove = chsc_subchannel_remove,
133 	.shutdown = chsc_subchannel_shutdown,
134 };
135 
chsc_init_dbfs(void)136 static int __init chsc_init_dbfs(void)
137 {
138 	chsc_debug_msg_id = debug_register("chsc_msg", 8, 1, 4 * sizeof(long));
139 	if (!chsc_debug_msg_id)
140 		goto out;
141 	debug_register_view(chsc_debug_msg_id, &debug_sprintf_view);
142 	debug_set_level(chsc_debug_msg_id, 2);
143 	chsc_debug_log_id = debug_register("chsc_log", 16, 1, 16);
144 	if (!chsc_debug_log_id)
145 		goto out;
146 	debug_register_view(chsc_debug_log_id, &debug_hex_ascii_view);
147 	debug_set_level(chsc_debug_log_id, 2);
148 	return 0;
149 out:
150 	debug_unregister(chsc_debug_msg_id);
151 	return -ENOMEM;
152 }
153 
chsc_remove_dbfs(void)154 static void chsc_remove_dbfs(void)
155 {
156 	debug_unregister(chsc_debug_log_id);
157 	debug_unregister(chsc_debug_msg_id);
158 }
159 
chsc_init_sch_driver(void)160 static int __init chsc_init_sch_driver(void)
161 {
162 	return css_driver_register(&chsc_subchannel_driver);
163 }
164 
chsc_cleanup_sch_driver(void)165 static void chsc_cleanup_sch_driver(void)
166 {
167 	css_driver_unregister(&chsc_subchannel_driver);
168 }
169 
170 static DEFINE_SPINLOCK(chsc_lock);
171 
chsc_subchannel_match_next_free(struct device * dev,const void * data)172 static int chsc_subchannel_match_next_free(struct device *dev, const void *data)
173 {
174 	struct subchannel *sch = to_subchannel(dev);
175 
176 	return sch->schib.pmcw.ena && !scsw_fctl(&sch->schib.scsw);
177 }
178 
chsc_get_next_subchannel(struct subchannel * sch)179 static struct subchannel *chsc_get_next_subchannel(struct subchannel *sch)
180 {
181 	struct device *dev;
182 
183 	dev = driver_find_device(&chsc_subchannel_driver.drv,
184 				 sch ? &sch->dev : NULL, NULL,
185 				 chsc_subchannel_match_next_free);
186 	return dev ? to_subchannel(dev) : NULL;
187 }
188 
189 /**
190  * chsc_async() - try to start a chsc request asynchronously
191  * @chsc_area: request to be started
192  * @request: request structure to associate
193  *
194  * Tries to start a chsc request on one of the existing chsc subchannels.
195  * Returns:
196  *  %0 if the request was performed synchronously
197  *  %-EINPROGRESS if the request was successfully started
198  *  %-EBUSY if all chsc subchannels are busy
199  *  %-ENODEV if no chsc subchannels are available
200  * Context:
201  *  interrupts disabled, chsc_lock held
202  */
chsc_async(struct chsc_async_area * chsc_area,struct chsc_request * request)203 static int chsc_async(struct chsc_async_area *chsc_area,
204 		      struct chsc_request *request)
205 {
206 	int cc;
207 	struct chsc_private *private;
208 	struct subchannel *sch = NULL;
209 	int ret = -ENODEV;
210 	char dbf[10];
211 
212 	chsc_area->header.key = PAGE_DEFAULT_KEY >> 4;
213 	while ((sch = chsc_get_next_subchannel(sch))) {
214 		spin_lock(&sch->lock);
215 		private = dev_get_drvdata(&sch->dev);
216 		if (private->request) {
217 			spin_unlock(&sch->lock);
218 			ret = -EBUSY;
219 			continue;
220 		}
221 		chsc_area->header.sid = sch->schid;
222 		CHSC_LOG(2, "schid");
223 		CHSC_LOG_HEX(2, &sch->schid, sizeof(sch->schid));
224 		cc = chsc(chsc_area);
225 		snprintf(dbf, sizeof(dbf), "cc:%d", cc);
226 		CHSC_LOG(2, dbf);
227 		switch (cc) {
228 		case 0:
229 			ret = 0;
230 			break;
231 		case 1:
232 			sch->schib.scsw.cmd.fctl |= SCSW_FCTL_START_FUNC;
233 			ret = -EINPROGRESS;
234 			private->request = request;
235 			break;
236 		case 2:
237 			ret = -EBUSY;
238 			break;
239 		default:
240 			ret = -ENODEV;
241 		}
242 		spin_unlock(&sch->lock);
243 		CHSC_MSG(2, "chsc on 0.%x.%04x returned cc=%d\n",
244 			 sch->schid.ssid, sch->schid.sch_no, cc);
245 		if (ret == -EINPROGRESS)
246 			return -EINPROGRESS;
247 		put_device(&sch->dev);
248 		if (ret == 0)
249 			return 0;
250 	}
251 	return ret;
252 }
253 
chsc_log_command(void * chsc_area)254 static void chsc_log_command(void *chsc_area)
255 {
256 	char dbf[10];
257 
258 	snprintf(dbf, sizeof(dbf), "CHSC:%x", ((uint16_t *)chsc_area)[1]);
259 	CHSC_LOG(0, dbf);
260 	CHSC_LOG_HEX(0, chsc_area, 32);
261 }
262 
chsc_examine_irb(struct chsc_request * request)263 static int chsc_examine_irb(struct chsc_request *request)
264 {
265 	int backed_up;
266 
267 	if (!(scsw_stctl(&request->irb.scsw) & SCSW_STCTL_STATUS_PEND))
268 		return -EIO;
269 	backed_up = scsw_cstat(&request->irb.scsw) & SCHN_STAT_CHAIN_CHECK;
270 	request->irb.scsw.cmd.cstat &= ~SCHN_STAT_CHAIN_CHECK;
271 	if (scsw_cstat(&request->irb.scsw) == 0)
272 		return 0;
273 	if (!backed_up)
274 		return 0;
275 	if (scsw_cstat(&request->irb.scsw) & SCHN_STAT_PROG_CHECK)
276 		return -EIO;
277 	if (scsw_cstat(&request->irb.scsw) & SCHN_STAT_PROT_CHECK)
278 		return -EPERM;
279 	if (scsw_cstat(&request->irb.scsw) & SCHN_STAT_CHN_DATA_CHK)
280 		return -EAGAIN;
281 	if (scsw_cstat(&request->irb.scsw) & SCHN_STAT_CHN_CTRL_CHK)
282 		return -EAGAIN;
283 	return -EIO;
284 }
285 
chsc_ioctl_start(void __user * user_area)286 static int chsc_ioctl_start(void __user *user_area)
287 {
288 	struct chsc_request *request;
289 	struct chsc_async_area *chsc_area;
290 	int ret;
291 	char dbf[10];
292 
293 	if (!css_general_characteristics.dynio)
294 		/* It makes no sense to try. */
295 		return -EOPNOTSUPP;
296 	chsc_area = kzalloc(PAGE_SIZE, GFP_DMA | GFP_KERNEL);
297 	if (!chsc_area)
298 		return -ENOMEM;
299 	request = kzalloc_obj(*request);
300 	if (!request) {
301 		ret = -ENOMEM;
302 		goto out_free;
303 	}
304 	init_completion(&request->completion);
305 	if (copy_from_user(chsc_area, user_area, PAGE_SIZE)) {
306 		ret = -EFAULT;
307 		goto out_free;
308 	}
309 	chsc_log_command(chsc_area);
310 	spin_lock_irq(&chsc_lock);
311 	ret = chsc_async(chsc_area, request);
312 	spin_unlock_irq(&chsc_lock);
313 	if (ret == -EINPROGRESS) {
314 		wait_for_completion(&request->completion);
315 		ret = chsc_examine_irb(request);
316 	}
317 	/* copy area back to user */
318 	if (!ret)
319 		if (copy_to_user(user_area, chsc_area, PAGE_SIZE))
320 			ret = -EFAULT;
321 out_free:
322 	snprintf(dbf, sizeof(dbf), "ret:%d", ret);
323 	CHSC_LOG(0, dbf);
324 	kfree(request);
325 	kfree(chsc_area);
326 	return ret;
327 }
328 
chsc_ioctl_on_close_set(void __user * user_area)329 static int chsc_ioctl_on_close_set(void __user *user_area)
330 {
331 	char dbf[13];
332 	int ret;
333 
334 	mutex_lock(&on_close_mutex);
335 	if (on_close_chsc_area) {
336 		ret = -EBUSY;
337 		goto out_unlock;
338 	}
339 	on_close_request = kzalloc_obj(*on_close_request);
340 	if (!on_close_request) {
341 		ret = -ENOMEM;
342 		goto out_unlock;
343 	}
344 	on_close_chsc_area = kzalloc(PAGE_SIZE, GFP_DMA | GFP_KERNEL);
345 	if (!on_close_chsc_area) {
346 		ret = -ENOMEM;
347 		goto out_free_request;
348 	}
349 	if (copy_from_user(on_close_chsc_area, user_area, PAGE_SIZE)) {
350 		ret = -EFAULT;
351 		goto out_free_chsc;
352 	}
353 	ret = 0;
354 	goto out_unlock;
355 
356 out_free_chsc:
357 	kfree(on_close_chsc_area);
358 	on_close_chsc_area = NULL;
359 out_free_request:
360 	kfree(on_close_request);
361 	on_close_request = NULL;
362 out_unlock:
363 	mutex_unlock(&on_close_mutex);
364 	snprintf(dbf, sizeof(dbf), "ocsret:%d", ret);
365 	CHSC_LOG(0, dbf);
366 	return ret;
367 }
368 
chsc_ioctl_on_close_remove(void)369 static int chsc_ioctl_on_close_remove(void)
370 {
371 	char dbf[13];
372 	int ret;
373 
374 	mutex_lock(&on_close_mutex);
375 	if (!on_close_chsc_area) {
376 		ret = -ENOENT;
377 		goto out_unlock;
378 	}
379 	kfree(on_close_chsc_area);
380 	on_close_chsc_area = NULL;
381 	kfree(on_close_request);
382 	on_close_request = NULL;
383 	ret = 0;
384 out_unlock:
385 	mutex_unlock(&on_close_mutex);
386 	snprintf(dbf, sizeof(dbf), "ocrret:%d", ret);
387 	CHSC_LOG(0, dbf);
388 	return ret;
389 }
390 
chsc_ioctl_start_sync(void __user * user_area)391 static int chsc_ioctl_start_sync(void __user *user_area)
392 {
393 	struct chsc_sync_area *chsc_area __free(kfree) = NULL;
394 	int ccode;
395 
396 	chsc_area = kzalloc(PAGE_SIZE, GFP_KERNEL | GFP_DMA);
397 	if (!chsc_area)
398 		return -ENOMEM;
399 	if (copy_from_user(chsc_area, user_area, PAGE_SIZE))
400 		return -EFAULT;
401 	if (chsc_area->header.code & 0x4000)
402 		return -EINVAL;
403 	chsc_log_command(chsc_area);
404 	ccode = chsc(chsc_area);
405 	if (ccode != 0)
406 		return -EIO;
407 	if (copy_to_user(user_area, chsc_area, PAGE_SIZE))
408 		return -EFAULT;
409 	return 0;
410 }
411 
chsc_ioctl_info_channel_path(void __user * user_cd)412 static int chsc_ioctl_info_channel_path(void __user *user_cd)
413 {
414 	struct chsc_chp_cd *cd __free(kfree) = NULL;
415 	int ccode;
416 	struct {
417 		struct chsc_header request;
418 		u32 : 2;
419 		u32 m : 1;
420 		u32 : 1;
421 		u32 fmt1 : 4;
422 		u32 cssid : 8;
423 		u32 : 8;
424 		u32 first_chpid : 8;
425 		u32 : 24;
426 		u32 last_chpid : 8;
427 		u32 : 32;
428 		struct chsc_header response;
429 		u8 data[PAGE_SIZE - 20];
430 	} __attribute__ ((packed)) *scpcd_area __free(kfree) = NULL;
431 
432 	scpcd_area = kzalloc(PAGE_SIZE, GFP_KERNEL | GFP_DMA);
433 	if (!scpcd_area)
434 		return -ENOMEM;
435 	cd = kzalloc_obj(*cd);
436 	if (!cd)
437 		return -ENOMEM;
438 	if (copy_from_user(cd, user_cd, sizeof(*cd)))
439 		return -EFAULT;
440 	scpcd_area->request.length = 0x0010;
441 	scpcd_area->request.code = 0x0028;
442 	scpcd_area->m = cd->m;
443 	scpcd_area->fmt1 = cd->fmt;
444 	scpcd_area->cssid = cd->chpid.cssid;
445 	scpcd_area->first_chpid = cd->chpid.id;
446 	scpcd_area->last_chpid = cd->chpid.id;
447 
448 	ccode = chsc(scpcd_area);
449 	if (ccode != 0)
450 		return -EIO;
451 	if (scpcd_area->response.code != 0x0001) {
452 		CHSC_MSG(0, "scpcd: response code=%x\n",
453 			 scpcd_area->response.code);
454 		return -EIO;
455 	}
456 	memcpy(&cd->cpcb, &scpcd_area->response, scpcd_area->response.length);
457 	if (copy_to_user(user_cd, cd, sizeof(*cd)))
458 		return -EFAULT;
459 	return 0;
460 }
461 
chsc_ioctl_info_cu(void __user * user_cd)462 static int chsc_ioctl_info_cu(void __user *user_cd)
463 {
464 	struct chsc_cu_cd *cd __free(kfree) = NULL;
465 	int ccode;
466 	struct {
467 		struct chsc_header request;
468 		u32 : 2;
469 		u32 m : 1;
470 		u32 : 1;
471 		u32 fmt1 : 4;
472 		u32 cssid : 8;
473 		u32 : 8;
474 		u32 first_cun : 8;
475 		u32 : 24;
476 		u32 last_cun : 8;
477 		u32 : 32;
478 		struct chsc_header response;
479 		u8 data[PAGE_SIZE - 20];
480 	} __attribute__ ((packed)) *scucd_area __free(kfree) = NULL;
481 
482 	scucd_area = kzalloc(PAGE_SIZE, GFP_KERNEL | GFP_DMA);
483 	if (!scucd_area)
484 		return -ENOMEM;
485 	cd = kzalloc_obj(*cd);
486 	if (!cd)
487 		return -ENOMEM;
488 	if (copy_from_user(cd, user_cd, sizeof(*cd)))
489 		return -EFAULT;
490 	scucd_area->request.length = 0x0010;
491 	scucd_area->request.code = 0x0026;
492 	scucd_area->m = cd->m;
493 	scucd_area->fmt1 = cd->fmt;
494 	scucd_area->cssid = cd->cssid;
495 	scucd_area->first_cun = cd->cun;
496 	scucd_area->last_cun = cd->cun;
497 
498 	ccode = chsc(scucd_area);
499 	if (ccode != 0)
500 		return -EIO;
501 	if (scucd_area->response.code != 0x0001) {
502 		CHSC_MSG(0, "scucd: response code=%x\n",
503 			 scucd_area->response.code);
504 		return -EIO;
505 	}
506 	memcpy(&cd->cucb, &scucd_area->response, scucd_area->response.length);
507 	if (copy_to_user(user_cd, cd, sizeof(*cd)))
508 		return -EFAULT;
509 	return 0;
510 }
511 
chsc_ioctl_info_sch_cu(void __user * user_cud)512 static int chsc_ioctl_info_sch_cu(void __user *user_cud)
513 {
514 	struct chsc_sch_cud *cud __free(kfree) = NULL;
515 	int ccode;
516 	struct {
517 		struct chsc_header request;
518 		u32 : 2;
519 		u32 m : 1;
520 		u32 : 5;
521 		u32 fmt1 : 4;
522 		u32 : 2;
523 		u32 ssid : 2;
524 		u32 first_sch : 16;
525 		u32 : 8;
526 		u32 cssid : 8;
527 		u32 last_sch : 16;
528 		u32 : 32;
529 		struct chsc_header response;
530 		u8 data[PAGE_SIZE - 20];
531 	} __attribute__ ((packed)) *sscud_area __free(kfree) = NULL;
532 
533 	sscud_area = kzalloc(PAGE_SIZE, GFP_KERNEL | GFP_DMA);
534 	if (!sscud_area)
535 		return -ENOMEM;
536 	cud = kzalloc_obj(*cud);
537 	if (!cud)
538 		return -ENOMEM;
539 	if (copy_from_user(cud, user_cud, sizeof(*cud)))
540 		return -EFAULT;
541 	sscud_area->request.length = 0x0010;
542 	sscud_area->request.code = 0x0006;
543 	sscud_area->m = cud->schid.m;
544 	sscud_area->fmt1 = cud->fmt;
545 	sscud_area->ssid = cud->schid.ssid;
546 	sscud_area->first_sch = cud->schid.sch_no;
547 	sscud_area->cssid = cud->schid.cssid;
548 	sscud_area->last_sch = cud->schid.sch_no;
549 
550 	ccode = chsc(sscud_area);
551 	if (ccode != 0)
552 		return -EIO;
553 	if (sscud_area->response.code != 0x0001) {
554 		CHSC_MSG(0, "sscud: response code=%x\n",
555 			 sscud_area->response.code);
556 		return -EIO;
557 	}
558 	memcpy(&cud->scub, &sscud_area->response, sscud_area->response.length);
559 	if (copy_to_user(user_cud, cud, sizeof(*cud)))
560 		return -EFAULT;
561 	return 0;
562 }
563 
chsc_ioctl_conf_info(void __user * user_ci)564 static int chsc_ioctl_conf_info(void __user *user_ci)
565 {
566 	struct chsc_conf_info *ci __free(kfree) = NULL;
567 	int ccode;
568 	struct {
569 		struct chsc_header request;
570 		u32 : 2;
571 		u32 m : 1;
572 		u32 : 1;
573 		u32 fmt1 : 4;
574 		u32 cssid : 8;
575 		u32 : 6;
576 		u32 ssid : 2;
577 		u32 : 8;
578 		u64 : 64;
579 		struct chsc_header response;
580 		u8 data[PAGE_SIZE - 20];
581 	} __attribute__ ((packed)) *sci_area __free(kfree) = NULL;
582 
583 	sci_area = kzalloc(PAGE_SIZE, GFP_KERNEL | GFP_DMA);
584 	if (!sci_area)
585 		return -ENOMEM;
586 	ci = kzalloc_obj(*ci);
587 	if (!ci)
588 		return -ENOMEM;
589 	if (copy_from_user(ci, user_ci, sizeof(*ci)))
590 		return -EFAULT;
591 	sci_area->request.length = 0x0010;
592 	sci_area->request.code = 0x0012;
593 	sci_area->m = ci->id.m;
594 	sci_area->fmt1 = ci->fmt;
595 	sci_area->cssid = ci->id.cssid;
596 	sci_area->ssid = ci->id.ssid;
597 
598 	ccode = chsc(sci_area);
599 	if (ccode != 0)
600 		return -EIO;
601 	if (sci_area->response.code != 0x0001) {
602 		CHSC_MSG(0, "sci: response code=%x\n",
603 			 sci_area->response.code);
604 		return -EIO;
605 	}
606 	memcpy(&ci->scid, &sci_area->response, sci_area->response.length);
607 	if (copy_to_user(user_ci, ci, sizeof(*ci)))
608 		return -EFAULT;
609 	return 0;
610 }
611 
chsc_ioctl_conf_comp_list(void __user * user_ccl)612 static int chsc_ioctl_conf_comp_list(void __user *user_ccl)
613 {
614 	struct chsc_comp_list *ccl __free(kfree) = NULL;
615 	int ccode;
616 	struct {
617 		struct chsc_header request;
618 		u32 ctype : 8;
619 		u32 : 4;
620 		u32 fmt : 4;
621 		u32 : 16;
622 		u64 : 64;
623 		u32 list_parm[2];
624 		u64 : 64;
625 		struct chsc_header response;
626 		u8 data[PAGE_SIZE - 36];
627 	} __attribute__ ((packed)) *sccl_area __free(kfree) = NULL;
628 	struct {
629 		u32 m : 1;
630 		u32 : 31;
631 		u32 cssid : 8;
632 		u32 : 16;
633 		u32 chpid : 8;
634 	} __attribute__ ((packed)) *chpid_parm;
635 	struct {
636 		u32 f_cssid : 8;
637 		u32 l_cssid : 8;
638 		u32 : 16;
639 		u32 res;
640 	} __attribute__ ((packed)) *cssids_parm;
641 
642 	sccl_area = kzalloc(PAGE_SIZE, GFP_KERNEL | GFP_DMA);
643 	if (!sccl_area)
644 		return -ENOMEM;
645 	ccl = kzalloc_obj(*ccl);
646 	if (!ccl)
647 		return -ENOMEM;
648 	if (copy_from_user(ccl, user_ccl, sizeof(*ccl)))
649 		return -EFAULT;
650 	sccl_area->request.length = 0x0020;
651 	sccl_area->request.code = 0x0030;
652 	sccl_area->fmt = ccl->req.fmt;
653 	sccl_area->ctype = ccl->req.ctype;
654 	switch (sccl_area->ctype) {
655 	case CCL_CU_ON_CHP:
656 	case CCL_IOP_CHP:
657 		chpid_parm = (void *)&sccl_area->list_parm;
658 		chpid_parm->m = ccl->req.chpid.m;
659 		chpid_parm->cssid = ccl->req.chpid.chp.cssid;
660 		chpid_parm->chpid = ccl->req.chpid.chp.id;
661 		break;
662 	case CCL_CSS_IMG:
663 	case CCL_CSS_IMG_CONF_CHAR:
664 		cssids_parm = (void *)&sccl_area->list_parm;
665 		cssids_parm->f_cssid = ccl->req.cssids.f_cssid;
666 		cssids_parm->l_cssid = ccl->req.cssids.l_cssid;
667 		break;
668 	}
669 	ccode = chsc(sccl_area);
670 	if (ccode != 0)
671 		return -EIO;
672 	if (sccl_area->response.code != 0x0001) {
673 		CHSC_MSG(0, "sccl: response code=%x\n",
674 			 sccl_area->response.code);
675 		return -EIO;
676 	}
677 	memcpy(&ccl->sccl, &sccl_area->response, sccl_area->response.length);
678 	if (copy_to_user(user_ccl, ccl, sizeof(*ccl)))
679 		return -EFAULT;
680 	return 0;
681 }
682 
chsc_ioctl_chpd(void __user * user_chpd)683 static int chsc_ioctl_chpd(void __user *user_chpd)
684 {
685 	struct chsc_scpd *scpd_area __free(kfree) = NULL;
686 	struct chsc_cpd_info *chpd __free(kfree) = NULL;
687 	int ret;
688 
689 	chpd = kzalloc_obj(*chpd);
690 	scpd_area = kzalloc(PAGE_SIZE, GFP_KERNEL | GFP_DMA);
691 	if (!scpd_area || !chpd)
692 		return -ENOMEM;
693 	if (copy_from_user(chpd, user_chpd, sizeof(*chpd)))
694 		return -EFAULT;
695 	ret = chsc_determine_channel_path_desc(chpd->chpid, chpd->fmt,
696 					       chpd->rfmt, chpd->c, chpd->m,
697 					       scpd_area);
698 	if (ret)
699 		return ret;
700 	memcpy(&chpd->chpdb, &scpd_area->response, scpd_area->response.length);
701 	if (copy_to_user(user_chpd, chpd, sizeof(*chpd)))
702 		return -EFAULT;
703 	return 0;
704 }
705 
chsc_ioctl_dcal(void __user * user_dcal)706 static int chsc_ioctl_dcal(void __user *user_dcal)
707 {
708 	struct chsc_dcal *dcal __free(kfree) = NULL;
709 	int ccode;
710 	struct {
711 		struct chsc_header request;
712 		u32 atype : 8;
713 		u32 : 4;
714 		u32 fmt : 4;
715 		u32 : 16;
716 		u32 res0[2];
717 		u32 list_parm[2];
718 		u32 res1[2];
719 		struct chsc_header response;
720 		u8 data[PAGE_SIZE - 36];
721 	} __attribute__ ((packed)) *sdcal_area __free(kfree) = NULL;
722 
723 	sdcal_area = kzalloc(PAGE_SIZE, GFP_KERNEL | GFP_DMA);
724 	if (!sdcal_area)
725 		return -ENOMEM;
726 	dcal = kzalloc_obj(*dcal);
727 	if (!dcal)
728 		return -ENOMEM;
729 	if (copy_from_user(dcal, user_dcal, sizeof(*dcal)))
730 		return -EFAULT;
731 	sdcal_area->request.length = 0x0020;
732 	sdcal_area->request.code = 0x0034;
733 	sdcal_area->atype = dcal->req.atype;
734 	sdcal_area->fmt = dcal->req.fmt;
735 	memcpy(&sdcal_area->list_parm, &dcal->req.list_parm,
736 	       sizeof(sdcal_area->list_parm));
737 
738 	ccode = chsc(sdcal_area);
739 	if (ccode != 0)
740 		return -EIO;
741 	if (sdcal_area->response.code != 0x0001) {
742 		CHSC_MSG(0, "sdcal: response code=%x\n",
743 			 sdcal_area->response.code);
744 		return -EIO;
745 	}
746 	memcpy(&dcal->sdcal, &sdcal_area->response,
747 	       sdcal_area->response.length);
748 	if (copy_to_user(user_dcal, dcal, sizeof(*dcal)))
749 		return -EFAULT;
750 	return 0;
751 }
752 
chsc_ioctl(struct file * filp,unsigned int cmd,unsigned long arg)753 static long chsc_ioctl(struct file *filp, unsigned int cmd,
754 		       unsigned long arg)
755 {
756 	void __user *argp;
757 
758 	CHSC_MSG(2, "chsc_ioctl called, cmd=%x\n", cmd);
759 	argp = (void __user *)arg;
760 	switch (cmd) {
761 	case CHSC_START:
762 		return chsc_ioctl_start(argp);
763 	case CHSC_START_SYNC:
764 		return chsc_ioctl_start_sync(argp);
765 	case CHSC_INFO_CHANNEL_PATH:
766 		return chsc_ioctl_info_channel_path(argp);
767 	case CHSC_INFO_CU:
768 		return chsc_ioctl_info_cu(argp);
769 	case CHSC_INFO_SCH_CU:
770 		return chsc_ioctl_info_sch_cu(argp);
771 	case CHSC_INFO_CI:
772 		return chsc_ioctl_conf_info(argp);
773 	case CHSC_INFO_CCL:
774 		return chsc_ioctl_conf_comp_list(argp);
775 	case CHSC_INFO_CPD:
776 		return chsc_ioctl_chpd(argp);
777 	case CHSC_INFO_DCAL:
778 		return chsc_ioctl_dcal(argp);
779 	case CHSC_ON_CLOSE_SET:
780 		return chsc_ioctl_on_close_set(argp);
781 	case CHSC_ON_CLOSE_REMOVE:
782 		return chsc_ioctl_on_close_remove();
783 	default: /* unknown ioctl number */
784 		return -ENOIOCTLCMD;
785 	}
786 }
787 
788 static atomic_t chsc_ready_for_use = ATOMIC_INIT(1);
789 
chsc_open(struct inode * inode,struct file * file)790 static int chsc_open(struct inode *inode, struct file *file)
791 {
792 	if (!atomic_dec_and_test(&chsc_ready_for_use)) {
793 		atomic_inc(&chsc_ready_for_use);
794 		return -EBUSY;
795 	}
796 	return nonseekable_open(inode, file);
797 }
798 
chsc_release(struct inode * inode,struct file * filp)799 static int chsc_release(struct inode *inode, struct file *filp)
800 {
801 	char dbf[13];
802 	int ret;
803 
804 	mutex_lock(&on_close_mutex);
805 	if (!on_close_chsc_area)
806 		goto out_unlock;
807 	init_completion(&on_close_request->completion);
808 	CHSC_LOG(0, "on_close");
809 	chsc_log_command(on_close_chsc_area);
810 	spin_lock_irq(&chsc_lock);
811 	ret = chsc_async(on_close_chsc_area, on_close_request);
812 	spin_unlock_irq(&chsc_lock);
813 	if (ret == -EINPROGRESS) {
814 		wait_for_completion(&on_close_request->completion);
815 		ret = chsc_examine_irb(on_close_request);
816 	}
817 	snprintf(dbf, sizeof(dbf), "relret:%d", ret);
818 	CHSC_LOG(0, dbf);
819 	kfree(on_close_chsc_area);
820 	on_close_chsc_area = NULL;
821 	kfree(on_close_request);
822 	on_close_request = NULL;
823 out_unlock:
824 	mutex_unlock(&on_close_mutex);
825 	atomic_inc(&chsc_ready_for_use);
826 	return 0;
827 }
828 
829 static const struct file_operations chsc_fops = {
830 	.owner = THIS_MODULE,
831 	.open = chsc_open,
832 	.release = chsc_release,
833 	.unlocked_ioctl = chsc_ioctl,
834 };
835 
836 static struct miscdevice chsc_misc_device = {
837 	.minor = MISC_DYNAMIC_MINOR,
838 	.name = "chsc",
839 	.fops = &chsc_fops,
840 };
841 
chsc_misc_init(void)842 static int __init chsc_misc_init(void)
843 {
844 	return misc_register(&chsc_misc_device);
845 }
846 
chsc_misc_cleanup(void)847 static void chsc_misc_cleanup(void)
848 {
849 	misc_deregister(&chsc_misc_device);
850 }
851 
chsc_sch_init(void)852 static int __init chsc_sch_init(void)
853 {
854 	int ret;
855 
856 	ret = chsc_init_dbfs();
857 	if (ret)
858 		return ret;
859 	isc_register(CHSC_SCH_ISC);
860 	ret = chsc_init_sch_driver();
861 	if (ret)
862 		goto out_dbf;
863 	ret = chsc_misc_init();
864 	if (ret)
865 		goto out_driver;
866 	return ret;
867 out_driver:
868 	chsc_cleanup_sch_driver();
869 out_dbf:
870 	isc_unregister(CHSC_SCH_ISC);
871 	chsc_remove_dbfs();
872 	return ret;
873 }
874 
chsc_sch_exit(void)875 static void __exit chsc_sch_exit(void)
876 {
877 	chsc_misc_cleanup();
878 	chsc_cleanup_sch_driver();
879 	isc_unregister(CHSC_SCH_ISC);
880 	chsc_remove_dbfs();
881 }
882 
883 module_init(chsc_sch_init);
884 module_exit(chsc_sch_exit);
885