Linux Audio

Check our new training course

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