• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /netgear-R7000-V1.0.7.12_1.2.5/components/opensource/linux/linux-2.6.36/drivers/message/i2o/
1/*
2 * I2O Configuration Interface Driver
3 *
4 * (C) Copyright 1999-2002  Red Hat
5 *
6 * Written by Alan Cox, Building Number Three Ltd
7 *
8 * Fixes/additions:
9 *	Deepak Saxena (04/20/1999):
10 *		Added basic ioctl() support
11 *	Deepak Saxena (06/07/1999):
12 *		Added software download ioctl (still testing)
13 *	Auvo H��kkinen (09/10/1999):
14 *		Changes to i2o_cfg_reply(), ioctl_parms()
15 *		Added ioct_validate()
16 *	Taneli V��h��kangas (09/30/1999):
17 *		Fixed ioctl_swdl()
18 *	Taneli V��h��kangas (10/04/1999):
19 *		Changed ioctl_swdl(), implemented ioctl_swul() and ioctl_swdel()
20 *	Deepak Saxena (11/18/1999):
21 *		Added event managmenet support
22 *	Alan Cox <alan@lxorguk.ukuu.org.uk>:
23 *		2.4 rewrite ported to 2.5
24 *	Markus Lidel <Markus.Lidel@shadowconnect.com>:
25 *		Added pass-thru support for Adaptec's raidutils
26 *
27 * This program is free software; you can redistribute it and/or
28 * modify it under the terms of the GNU General Public License
29 * as published by the Free Software Foundation; either version
30 * 2 of the License, or (at your option) any later version.
31 */
32
33#include <linux/miscdevice.h>
34#include <linux/smp_lock.h>
35#include <linux/compat.h>
36#include <linux/slab.h>
37
38#include <asm/uaccess.h>
39
40#include "core.h"
41
42#define SG_TABLESIZE		30
43
44static long i2o_cfg_ioctl(struct file *, unsigned int, unsigned long);
45
46static spinlock_t i2o_config_lock;
47
48#define MODINC(x,y) ((x) = ((x) + 1) % (y))
49
50struct sg_simple_element {
51	u32 flag_count;
52	u32 addr_bus;
53};
54
55struct i2o_cfg_info {
56	struct file *fp;
57	struct fasync_struct *fasync;
58	struct i2o_evt_info event_q[I2O_EVT_Q_LEN];
59	u16 q_in;		// Queue head index
60	u16 q_out;		// Queue tail index
61	u16 q_len;		// Queue length
62	u16 q_lost;		// Number of lost events
63	ulong q_id;		// Event queue ID...used as tx_context
64	struct i2o_cfg_info *next;
65};
66static struct i2o_cfg_info *open_files = NULL;
67static ulong i2o_cfg_info_id = 0;
68
69static int i2o_cfg_getiops(unsigned long arg)
70{
71	struct i2o_controller *c;
72	u8 __user *user_iop_table = (void __user *)arg;
73	u8 tmp[MAX_I2O_CONTROLLERS];
74	int ret = 0;
75
76	memset(tmp, 0, MAX_I2O_CONTROLLERS);
77
78	list_for_each_entry(c, &i2o_controllers, list)
79	    tmp[c->unit] = 1;
80
81	if (copy_to_user(user_iop_table, tmp, MAX_I2O_CONTROLLERS))
82		ret = -EFAULT;
83
84	return ret;
85};
86
87static int i2o_cfg_gethrt(unsigned long arg)
88{
89	struct i2o_controller *c;
90	struct i2o_cmd_hrtlct __user *cmd = (struct i2o_cmd_hrtlct __user *)arg;
91	struct i2o_cmd_hrtlct kcmd;
92	i2o_hrt *hrt;
93	int len;
94	u32 reslen;
95	int ret = 0;
96
97	if (copy_from_user(&kcmd, cmd, sizeof(struct i2o_cmd_hrtlct)))
98		return -EFAULT;
99
100	if (get_user(reslen, kcmd.reslen) < 0)
101		return -EFAULT;
102
103	if (kcmd.resbuf == NULL)
104		return -EFAULT;
105
106	c = i2o_find_iop(kcmd.iop);
107	if (!c)
108		return -ENXIO;
109
110	hrt = (i2o_hrt *) c->hrt.virt;
111
112	len = 8 + ((hrt->entry_len * hrt->num_entries) << 2);
113
114	if (put_user(len, kcmd.reslen))
115		ret = -EFAULT;
116	else if (len > reslen)
117		ret = -ENOBUFS;
118	else if (copy_to_user(kcmd.resbuf, (void *)hrt, len))
119		ret = -EFAULT;
120
121	return ret;
122};
123
124static int i2o_cfg_getlct(unsigned long arg)
125{
126	struct i2o_controller *c;
127	struct i2o_cmd_hrtlct __user *cmd = (struct i2o_cmd_hrtlct __user *)arg;
128	struct i2o_cmd_hrtlct kcmd;
129	i2o_lct *lct;
130	int len;
131	int ret = 0;
132	u32 reslen;
133
134	if (copy_from_user(&kcmd, cmd, sizeof(struct i2o_cmd_hrtlct)))
135		return -EFAULT;
136
137	if (get_user(reslen, kcmd.reslen) < 0)
138		return -EFAULT;
139
140	if (kcmd.resbuf == NULL)
141		return -EFAULT;
142
143	c = i2o_find_iop(kcmd.iop);
144	if (!c)
145		return -ENXIO;
146
147	lct = (i2o_lct *) c->lct;
148
149	len = (unsigned int)lct->table_size << 2;
150	if (put_user(len, kcmd.reslen))
151		ret = -EFAULT;
152	else if (len > reslen)
153		ret = -ENOBUFS;
154	else if (copy_to_user(kcmd.resbuf, lct, len))
155		ret = -EFAULT;
156
157	return ret;
158};
159
160static int i2o_cfg_parms(unsigned long arg, unsigned int type)
161{
162	int ret = 0;
163	struct i2o_controller *c;
164	struct i2o_device *dev;
165	struct i2o_cmd_psetget __user *cmd =
166	    (struct i2o_cmd_psetget __user *)arg;
167	struct i2o_cmd_psetget kcmd;
168	u32 reslen;
169	u8 *ops;
170	u8 *res;
171	int len = 0;
172
173	u32 i2o_cmd = (type == I2OPARMGET ?
174		       I2O_CMD_UTIL_PARAMS_GET : I2O_CMD_UTIL_PARAMS_SET);
175
176	if (copy_from_user(&kcmd, cmd, sizeof(struct i2o_cmd_psetget)))
177		return -EFAULT;
178
179	if (get_user(reslen, kcmd.reslen))
180		return -EFAULT;
181
182	c = i2o_find_iop(kcmd.iop);
183	if (!c)
184		return -ENXIO;
185
186	dev = i2o_iop_find_device(c, kcmd.tid);
187	if (!dev)
188		return -ENXIO;
189
190	ops = memdup_user(kcmd.opbuf, kcmd.oplen);
191	if (IS_ERR(ops))
192		return PTR_ERR(ops);
193
194	/*
195	 * It's possible to have a _very_ large table
196	 * and that the user asks for all of it at once...
197	 */
198	res = kmalloc(65536, GFP_KERNEL);
199	if (!res) {
200		kfree(ops);
201		return -ENOMEM;
202	}
203
204	len = i2o_parm_issue(dev, i2o_cmd, ops, kcmd.oplen, res, 65536);
205	kfree(ops);
206
207	if (len < 0) {
208		kfree(res);
209		return -EAGAIN;
210	}
211
212	if (put_user(len, kcmd.reslen))
213		ret = -EFAULT;
214	else if (len > reslen)
215		ret = -ENOBUFS;
216	else if (copy_to_user(kcmd.resbuf, res, len))
217		ret = -EFAULT;
218
219	kfree(res);
220
221	return ret;
222};
223
224static int i2o_cfg_swdl(unsigned long arg)
225{
226	struct i2o_sw_xfer kxfer;
227	struct i2o_sw_xfer __user *pxfer = (struct i2o_sw_xfer __user *)arg;
228	unsigned char maxfrag = 0, curfrag = 1;
229	struct i2o_dma buffer;
230	struct i2o_message *msg;
231	unsigned int status = 0, swlen = 0, fragsize = 8192;
232	struct i2o_controller *c;
233
234	if (copy_from_user(&kxfer, pxfer, sizeof(struct i2o_sw_xfer)))
235		return -EFAULT;
236
237	if (get_user(swlen, kxfer.swlen) < 0)
238		return -EFAULT;
239
240	if (get_user(maxfrag, kxfer.maxfrag) < 0)
241		return -EFAULT;
242
243	if (get_user(curfrag, kxfer.curfrag) < 0)
244		return -EFAULT;
245
246	if (curfrag == maxfrag)
247		fragsize = swlen - (maxfrag - 1) * 8192;
248
249	if (!kxfer.buf || !access_ok(VERIFY_READ, kxfer.buf, fragsize))
250		return -EFAULT;
251
252	c = i2o_find_iop(kxfer.iop);
253	if (!c)
254		return -ENXIO;
255
256	msg = i2o_msg_get_wait(c, I2O_TIMEOUT_MESSAGE_GET);
257	if (IS_ERR(msg))
258		return PTR_ERR(msg);
259
260	if (i2o_dma_alloc(&c->pdev->dev, &buffer, fragsize)) {
261		i2o_msg_nop(c, msg);
262		return -ENOMEM;
263	}
264
265	if (__copy_from_user(buffer.virt, kxfer.buf, fragsize)) {
266		i2o_msg_nop(c, msg);
267		i2o_dma_free(&c->pdev->dev, &buffer);
268		return -EFAULT;
269	}
270
271	msg->u.head[0] = cpu_to_le32(NINE_WORD_MSG_SIZE | SGL_OFFSET_7);
272	msg->u.head[1] =
273	    cpu_to_le32(I2O_CMD_SW_DOWNLOAD << 24 | HOST_TID << 12 |
274			ADAPTER_TID);
275	msg->u.head[2] = cpu_to_le32(i2o_config_driver.context);
276	msg->u.head[3] = cpu_to_le32(0);
277	msg->body[0] =
278	    cpu_to_le32((((u32) kxfer.flags) << 24) | (((u32) kxfer.
279							sw_type) << 16) |
280			(((u32) maxfrag) << 8) | (((u32) curfrag)));
281	msg->body[1] = cpu_to_le32(swlen);
282	msg->body[2] = cpu_to_le32(kxfer.sw_id);
283	msg->body[3] = cpu_to_le32(0xD0000000 | fragsize);
284	msg->body[4] = cpu_to_le32(buffer.phys);
285
286	osm_debug("swdl frag %d/%d (size %d)\n", curfrag, maxfrag, fragsize);
287	status = i2o_msg_post_wait_mem(c, msg, 60, &buffer);
288
289	if (status != -ETIMEDOUT)
290		i2o_dma_free(&c->pdev->dev, &buffer);
291
292	if (status != I2O_POST_WAIT_OK) {
293		// it fails if you try and send frags out of order
294		// and for some yet unknown reasons too
295		osm_info("swdl failed, DetailedStatus = %d\n", status);
296		return status;
297	}
298
299	return 0;
300};
301
302static int i2o_cfg_swul(unsigned long arg)
303{
304	struct i2o_sw_xfer kxfer;
305	struct i2o_sw_xfer __user *pxfer = (struct i2o_sw_xfer __user *)arg;
306	unsigned char maxfrag = 0, curfrag = 1;
307	struct i2o_dma buffer;
308	struct i2o_message *msg;
309	unsigned int status = 0, swlen = 0, fragsize = 8192;
310	struct i2o_controller *c;
311	int ret = 0;
312
313	if (copy_from_user(&kxfer, pxfer, sizeof(struct i2o_sw_xfer)))
314		return -EFAULT;
315
316	if (get_user(swlen, kxfer.swlen) < 0)
317		return -EFAULT;
318
319	if (get_user(maxfrag, kxfer.maxfrag) < 0)
320		return -EFAULT;
321
322	if (get_user(curfrag, kxfer.curfrag) < 0)
323		return -EFAULT;
324
325	if (curfrag == maxfrag)
326		fragsize = swlen - (maxfrag - 1) * 8192;
327
328	if (!kxfer.buf)
329		return -EFAULT;
330
331	c = i2o_find_iop(kxfer.iop);
332	if (!c)
333		return -ENXIO;
334
335	msg = i2o_msg_get_wait(c, I2O_TIMEOUT_MESSAGE_GET);
336	if (IS_ERR(msg))
337		return PTR_ERR(msg);
338
339	if (i2o_dma_alloc(&c->pdev->dev, &buffer, fragsize)) {
340		i2o_msg_nop(c, msg);
341		return -ENOMEM;
342	}
343
344	msg->u.head[0] = cpu_to_le32(NINE_WORD_MSG_SIZE | SGL_OFFSET_7);
345	msg->u.head[1] =
346	    cpu_to_le32(I2O_CMD_SW_UPLOAD << 24 | HOST_TID << 12 | ADAPTER_TID);
347	msg->u.head[2] = cpu_to_le32(i2o_config_driver.context);
348	msg->u.head[3] = cpu_to_le32(0);
349	msg->body[0] =
350	    cpu_to_le32((u32) kxfer.flags << 24 | (u32) kxfer.
351			sw_type << 16 | (u32) maxfrag << 8 | (u32) curfrag);
352	msg->body[1] = cpu_to_le32(swlen);
353	msg->body[2] = cpu_to_le32(kxfer.sw_id);
354	msg->body[3] = cpu_to_le32(0xD0000000 | fragsize);
355	msg->body[4] = cpu_to_le32(buffer.phys);
356
357	osm_debug("swul frag %d/%d (size %d)\n", curfrag, maxfrag, fragsize);
358	status = i2o_msg_post_wait_mem(c, msg, 60, &buffer);
359
360	if (status != I2O_POST_WAIT_OK) {
361		if (status != -ETIMEDOUT)
362			i2o_dma_free(&c->pdev->dev, &buffer);
363
364		osm_info("swul failed, DetailedStatus = %d\n", status);
365		return status;
366	}
367
368	if (copy_to_user(kxfer.buf, buffer.virt, fragsize))
369		ret = -EFAULT;
370
371	i2o_dma_free(&c->pdev->dev, &buffer);
372
373	return ret;
374}
375
376static int i2o_cfg_swdel(unsigned long arg)
377{
378	struct i2o_controller *c;
379	struct i2o_sw_xfer kxfer;
380	struct i2o_sw_xfer __user *pxfer = (struct i2o_sw_xfer __user *)arg;
381	struct i2o_message *msg;
382	unsigned int swlen;
383	int token;
384
385	if (copy_from_user(&kxfer, pxfer, sizeof(struct i2o_sw_xfer)))
386		return -EFAULT;
387
388	if (get_user(swlen, kxfer.swlen) < 0)
389		return -EFAULT;
390
391	c = i2o_find_iop(kxfer.iop);
392	if (!c)
393		return -ENXIO;
394
395	msg = i2o_msg_get_wait(c, I2O_TIMEOUT_MESSAGE_GET);
396	if (IS_ERR(msg))
397		return PTR_ERR(msg);
398
399	msg->u.head[0] = cpu_to_le32(SEVEN_WORD_MSG_SIZE | SGL_OFFSET_0);
400	msg->u.head[1] =
401	    cpu_to_le32(I2O_CMD_SW_REMOVE << 24 | HOST_TID << 12 | ADAPTER_TID);
402	msg->u.head[2] = cpu_to_le32(i2o_config_driver.context);
403	msg->u.head[3] = cpu_to_le32(0);
404	msg->body[0] =
405	    cpu_to_le32((u32) kxfer.flags << 24 | (u32) kxfer.sw_type << 16);
406	msg->body[1] = cpu_to_le32(swlen);
407	msg->body[2] = cpu_to_le32(kxfer.sw_id);
408
409	token = i2o_msg_post_wait(c, msg, 10);
410
411	if (token != I2O_POST_WAIT_OK) {
412		osm_info("swdel failed, DetailedStatus = %d\n", token);
413		return -ETIMEDOUT;
414	}
415
416	return 0;
417};
418
419static int i2o_cfg_validate(unsigned long arg)
420{
421	int token;
422	int iop = (int)arg;
423	struct i2o_message *msg;
424	struct i2o_controller *c;
425
426	c = i2o_find_iop(iop);
427	if (!c)
428		return -ENXIO;
429
430	msg = i2o_msg_get_wait(c, I2O_TIMEOUT_MESSAGE_GET);
431	if (IS_ERR(msg))
432		return PTR_ERR(msg);
433
434	msg->u.head[0] = cpu_to_le32(FOUR_WORD_MSG_SIZE | SGL_OFFSET_0);
435	msg->u.head[1] =
436	    cpu_to_le32(I2O_CMD_CONFIG_VALIDATE << 24 | HOST_TID << 12 | iop);
437	msg->u.head[2] = cpu_to_le32(i2o_config_driver.context);
438	msg->u.head[3] = cpu_to_le32(0);
439
440	token = i2o_msg_post_wait(c, msg, 10);
441
442	if (token != I2O_POST_WAIT_OK) {
443		osm_info("Can't validate configuration, ErrorStatus = %d\n",
444			 token);
445		return -ETIMEDOUT;
446	}
447
448	return 0;
449};
450
451static int i2o_cfg_evt_reg(unsigned long arg, struct file *fp)
452{
453	struct i2o_message *msg;
454	struct i2o_evt_id __user *pdesc = (struct i2o_evt_id __user *)arg;
455	struct i2o_evt_id kdesc;
456	struct i2o_controller *c;
457	struct i2o_device *d;
458
459	if (copy_from_user(&kdesc, pdesc, sizeof(struct i2o_evt_id)))
460		return -EFAULT;
461
462	/* IOP exists? */
463	c = i2o_find_iop(kdesc.iop);
464	if (!c)
465		return -ENXIO;
466
467	/* Device exists? */
468	d = i2o_iop_find_device(c, kdesc.tid);
469	if (!d)
470		return -ENODEV;
471
472	msg = i2o_msg_get_wait(c, I2O_TIMEOUT_MESSAGE_GET);
473	if (IS_ERR(msg))
474		return PTR_ERR(msg);
475
476	msg->u.head[0] = cpu_to_le32(FOUR_WORD_MSG_SIZE | SGL_OFFSET_0);
477	msg->u.head[1] =
478	    cpu_to_le32(I2O_CMD_UTIL_EVT_REGISTER << 24 | HOST_TID << 12 |
479			kdesc.tid);
480	msg->u.head[2] = cpu_to_le32(i2o_config_driver.context);
481	msg->u.head[3] = cpu_to_le32(i2o_cntxt_list_add(c, fp->private_data));
482	msg->body[0] = cpu_to_le32(kdesc.evt_mask);
483
484	i2o_msg_post(c, msg);
485
486	return 0;
487}
488
489static int i2o_cfg_evt_get(unsigned long arg, struct file *fp)
490{
491	struct i2o_cfg_info *p = NULL;
492	struct i2o_evt_get __user *uget = (struct i2o_evt_get __user *)arg;
493	struct i2o_evt_get kget;
494	unsigned long flags;
495
496	for (p = open_files; p; p = p->next)
497		if (p->q_id == (ulong) fp->private_data)
498			break;
499
500	if (!p->q_len)
501		return -ENOENT;
502
503	memcpy(&kget.info, &p->event_q[p->q_out], sizeof(struct i2o_evt_info));
504	MODINC(p->q_out, I2O_EVT_Q_LEN);
505	spin_lock_irqsave(&i2o_config_lock, flags);
506	p->q_len--;
507	kget.pending = p->q_len;
508	kget.lost = p->q_lost;
509	spin_unlock_irqrestore(&i2o_config_lock, flags);
510
511	if (copy_to_user(uget, &kget, sizeof(struct i2o_evt_get)))
512		return -EFAULT;
513	return 0;
514}
515
516#ifdef CONFIG_COMPAT
517static int i2o_cfg_passthru32(struct file *file, unsigned cmnd,
518			      unsigned long arg)
519{
520	struct i2o_cmd_passthru32 __user *cmd;
521	struct i2o_controller *c;
522	u32 __user *user_msg;
523	u32 *reply = NULL;
524	u32 __user *user_reply = NULL;
525	u32 size = 0;
526	u32 reply_size = 0;
527	u32 rcode = 0;
528	struct i2o_dma sg_list[SG_TABLESIZE];
529	u32 sg_offset = 0;
530	u32 sg_count = 0;
531	u32 i = 0;
532	u32 sg_index = 0;
533	i2o_status_block *sb;
534	struct i2o_message *msg;
535	unsigned int iop;
536
537	cmd = (struct i2o_cmd_passthru32 __user *)arg;
538
539	if (get_user(iop, &cmd->iop) || get_user(i, &cmd->msg))
540		return -EFAULT;
541
542	user_msg = compat_ptr(i);
543
544	c = i2o_find_iop(iop);
545	if (!c) {
546		osm_debug("controller %d not found\n", iop);
547		return -ENXIO;
548	}
549
550	sb = c->status_block.virt;
551
552	if (get_user(size, &user_msg[0])) {
553		osm_warn("unable to get size!\n");
554		return -EFAULT;
555	}
556	size = size >> 16;
557
558	if (size > sb->inbound_frame_size) {
559		osm_warn("size of message > inbound_frame_size");
560		return -EFAULT;
561	}
562
563	user_reply = &user_msg[size];
564
565	size <<= 2;		// Convert to bytes
566
567	msg = i2o_msg_get_wait(c, I2O_TIMEOUT_MESSAGE_GET);
568	if (IS_ERR(msg))
569		return PTR_ERR(msg);
570
571	rcode = -EFAULT;
572	/* Copy in the user's I2O command */
573	if (copy_from_user(msg, user_msg, size)) {
574		osm_warn("unable to copy user message\n");
575		goto out;
576	}
577	i2o_dump_message(msg);
578
579	if (get_user(reply_size, &user_reply[0]) < 0)
580		goto out;
581
582	reply_size >>= 16;
583	reply_size <<= 2;
584
585	rcode = -ENOMEM;
586	reply = kzalloc(reply_size, GFP_KERNEL);
587	if (!reply) {
588		printk(KERN_WARNING "%s: Could not allocate reply buffer\n",
589		       c->name);
590		goto out;
591	}
592
593	sg_offset = (msg->u.head[0] >> 4) & 0x0f;
594
595	memset(sg_list, 0, sizeof(sg_list[0]) * SG_TABLESIZE);
596	if (sg_offset) {
597		struct sg_simple_element *sg;
598
599		if (sg_offset * 4 >= size) {
600			rcode = -EFAULT;
601			goto cleanup;
602		}
603		// TODO 64bit fix
604		sg = (struct sg_simple_element *)((&msg->u.head[0]) +
605						  sg_offset);
606		sg_count =
607		    (size - sg_offset * 4) / sizeof(struct sg_simple_element);
608		if (sg_count > SG_TABLESIZE) {
609			printk(KERN_DEBUG "%s:IOCTL SG List too large (%u)\n",
610			       c->name, sg_count);
611			rcode = -EINVAL;
612			goto cleanup;
613		}
614
615		for (i = 0; i < sg_count; i++) {
616			int sg_size;
617			struct i2o_dma *p;
618
619			if (!(sg[i].flag_count & 0x10000000
620			      /*I2O_SGL_FLAGS_SIMPLE_ADDRESS_ELEMENT */ )) {
621				printk(KERN_DEBUG
622				       "%s:Bad SG element %d - not simple (%x)\n",
623				       c->name, i, sg[i].flag_count);
624				rcode = -EINVAL;
625				goto cleanup;
626			}
627			sg_size = sg[i].flag_count & 0xffffff;
628			p = &(sg_list[sg_index]);
629			/* Allocate memory for the transfer */
630			if (i2o_dma_alloc(&c->pdev->dev, p, sg_size)) {
631				printk(KERN_DEBUG
632				       "%s: Could not allocate SG buffer - size = %d buffer number %d of %d\n",
633				       c->name, sg_size, i, sg_count);
634				rcode = -ENOMEM;
635				goto sg_list_cleanup;
636			}
637			sg_index++;
638			/* Copy in the user's SG buffer if necessary */
639			if (sg[i].
640			    flag_count & 0x04000000 /*I2O_SGL_FLAGS_DIR */ ) {
641				// TODO 64bit fix
642				if (copy_from_user
643				    (p->virt,
644				     (void __user *)(unsigned long)sg[i].
645				     addr_bus, sg_size)) {
646					printk(KERN_DEBUG
647					       "%s: Could not copy SG buf %d FROM user\n",
648					       c->name, i);
649					rcode = -EFAULT;
650					goto sg_list_cleanup;
651				}
652			}
653			//TODO 64bit fix
654			sg[i].addr_bus = (u32) p->phys;
655		}
656	}
657
658	rcode = i2o_msg_post_wait(c, msg, 60);
659	msg = NULL;
660	if (rcode) {
661		reply[4] = ((u32) rcode) << 24;
662		goto sg_list_cleanup;
663	}
664
665	if (sg_offset) {
666		u32 rmsg[I2O_OUTBOUND_MSG_FRAME_SIZE];
667		/* Copy back the Scatter Gather buffers back to user space */
668		u32 j;
669		// TODO 64bit fix
670		struct sg_simple_element *sg;
671		int sg_size;
672
673		// re-acquire the original message to handle correctly the sg copy operation
674		memset(&rmsg, 0, I2O_OUTBOUND_MSG_FRAME_SIZE * 4);
675		// get user msg size in u32s
676		if (get_user(size, &user_msg[0])) {
677			rcode = -EFAULT;
678			goto sg_list_cleanup;
679		}
680		size = size >> 16;
681		size *= 4;
682		/* Copy in the user's I2O command */
683		if (copy_from_user(rmsg, user_msg, size)) {
684			rcode = -EFAULT;
685			goto sg_list_cleanup;
686		}
687		sg_count =
688		    (size - sg_offset * 4) / sizeof(struct sg_simple_element);
689
690		// TODO 64bit fix
691		sg = (struct sg_simple_element *)(rmsg + sg_offset);
692		for (j = 0; j < sg_count; j++) {
693			/* Copy out the SG list to user's buffer if necessary */
694			if (!
695			    (sg[j].
696			     flag_count & 0x4000000 /*I2O_SGL_FLAGS_DIR */ )) {
697				sg_size = sg[j].flag_count & 0xffffff;
698				// TODO 64bit fix
699				if (copy_to_user
700				    ((void __user *)(u64) sg[j].addr_bus,
701				     sg_list[j].virt, sg_size)) {
702					printk(KERN_WARNING
703					       "%s: Could not copy %p TO user %x\n",
704					       c->name, sg_list[j].virt,
705					       sg[j].addr_bus);
706					rcode = -EFAULT;
707					goto sg_list_cleanup;
708				}
709			}
710		}
711	}
712
713sg_list_cleanup:
714	/* Copy back the reply to user space */
715	if (reply_size) {
716		// we wrote our own values for context - now restore the user supplied ones
717		if (copy_from_user(reply + 2, user_msg + 2, sizeof(u32) * 2)) {
718			printk(KERN_WARNING
719			       "%s: Could not copy message context FROM user\n",
720			       c->name);
721			rcode = -EFAULT;
722		}
723		if (copy_to_user(user_reply, reply, reply_size)) {
724			printk(KERN_WARNING
725			       "%s: Could not copy reply TO user\n", c->name);
726			rcode = -EFAULT;
727		}
728	}
729	for (i = 0; i < sg_index; i++)
730		i2o_dma_free(&c->pdev->dev, &sg_list[i]);
731
732cleanup:
733	kfree(reply);
734out:
735	if (msg)
736		i2o_msg_nop(c, msg);
737	return rcode;
738}
739
740static long i2o_cfg_compat_ioctl(struct file *file, unsigned cmd,
741				 unsigned long arg)
742{
743	int ret;
744	lock_kernel();
745	switch (cmd) {
746	case I2OGETIOPS:
747		ret = i2o_cfg_ioctl(file, cmd, arg);
748		break;
749	case I2OPASSTHRU32:
750		ret = i2o_cfg_passthru32(file, cmd, arg);
751		break;
752	default:
753		ret = -ENOIOCTLCMD;
754		break;
755	}
756	unlock_kernel();
757	return ret;
758}
759
760#endif
761
762#ifdef CONFIG_I2O_EXT_ADAPTEC
763static int i2o_cfg_passthru(unsigned long arg)
764{
765	struct i2o_cmd_passthru __user *cmd =
766	    (struct i2o_cmd_passthru __user *)arg;
767	struct i2o_controller *c;
768	u32 __user *user_msg;
769	u32 *reply = NULL;
770	u32 __user *user_reply = NULL;
771	u32 size = 0;
772	u32 reply_size = 0;
773	u32 rcode = 0;
774	struct i2o_dma sg_list[SG_TABLESIZE];
775	u32 sg_offset = 0;
776	u32 sg_count = 0;
777	int sg_index = 0;
778	u32 i = 0;
779	i2o_status_block *sb;
780	struct i2o_message *msg;
781	unsigned int iop;
782
783	if (get_user(iop, &cmd->iop) || get_user(user_msg, &cmd->msg))
784		return -EFAULT;
785
786	c = i2o_find_iop(iop);
787	if (!c) {
788		osm_warn("controller %d not found\n", iop);
789		return -ENXIO;
790	}
791
792	sb = c->status_block.virt;
793
794	if (get_user(size, &user_msg[0]))
795		return -EFAULT;
796	size = size >> 16;
797
798	if (size > sb->inbound_frame_size) {
799		osm_warn("size of message > inbound_frame_size");
800		return -EFAULT;
801	}
802
803	user_reply = &user_msg[size];
804
805	size <<= 2;		// Convert to bytes
806
807	msg = i2o_msg_get_wait(c, I2O_TIMEOUT_MESSAGE_GET);
808	if (IS_ERR(msg))
809		return PTR_ERR(msg);
810
811	rcode = -EFAULT;
812	/* Copy in the user's I2O command */
813	if (copy_from_user(msg, user_msg, size))
814		goto out;
815
816	if (get_user(reply_size, &user_reply[0]) < 0)
817		goto out;
818
819	reply_size >>= 16;
820	reply_size <<= 2;
821
822	reply = kzalloc(reply_size, GFP_KERNEL);
823	if (!reply) {
824		printk(KERN_WARNING "%s: Could not allocate reply buffer\n",
825		       c->name);
826		rcode = -ENOMEM;
827		goto out;
828	}
829
830	sg_offset = (msg->u.head[0] >> 4) & 0x0f;
831
832	memset(sg_list, 0, sizeof(sg_list[0]) * SG_TABLESIZE);
833	if (sg_offset) {
834		struct sg_simple_element *sg;
835		struct i2o_dma *p;
836
837		if (sg_offset * 4 >= size) {
838			rcode = -EFAULT;
839			goto cleanup;
840		}
841		// TODO 64bit fix
842		sg = (struct sg_simple_element *)((&msg->u.head[0]) +
843						  sg_offset);
844		sg_count =
845		    (size - sg_offset * 4) / sizeof(struct sg_simple_element);
846		if (sg_count > SG_TABLESIZE) {
847			printk(KERN_DEBUG "%s:IOCTL SG List too large (%u)\n",
848			       c->name, sg_count);
849			rcode = -EINVAL;
850			goto cleanup;
851		}
852
853		for (i = 0; i < sg_count; i++) {
854			int sg_size;
855
856			if (!(sg[i].flag_count & 0x10000000
857			      /*I2O_SGL_FLAGS_SIMPLE_ADDRESS_ELEMENT */ )) {
858				printk(KERN_DEBUG
859				       "%s:Bad SG element %d - not simple (%x)\n",
860				       c->name, i, sg[i].flag_count);
861				rcode = -EINVAL;
862				goto sg_list_cleanup;
863			}
864			sg_size = sg[i].flag_count & 0xffffff;
865			p = &(sg_list[sg_index]);
866			if (i2o_dma_alloc(&c->pdev->dev, p, sg_size)) {
867			/* Allocate memory for the transfer */
868				printk(KERN_DEBUG
869				       "%s: Could not allocate SG buffer - size = %d buffer number %d of %d\n",
870				       c->name, sg_size, i, sg_count);
871				rcode = -ENOMEM;
872				goto sg_list_cleanup;
873			}
874			sg_index++;
875			/* Copy in the user's SG buffer if necessary */
876			if (sg[i].
877			    flag_count & 0x04000000 /*I2O_SGL_FLAGS_DIR */ ) {
878				// TODO 64bit fix
879				if (copy_from_user
880				    (p->virt, (void __user *)sg[i].addr_bus,
881				     sg_size)) {
882					printk(KERN_DEBUG
883					       "%s: Could not copy SG buf %d FROM user\n",
884					       c->name, i);
885					rcode = -EFAULT;
886					goto sg_list_cleanup;
887				}
888			}
889			sg[i].addr_bus = p->phys;
890		}
891	}
892
893	rcode = i2o_msg_post_wait(c, msg, 60);
894	msg = NULL;
895	if (rcode) {
896		reply[4] = ((u32) rcode) << 24;
897		goto sg_list_cleanup;
898	}
899
900	if (sg_offset) {
901		u32 rmsg[I2O_OUTBOUND_MSG_FRAME_SIZE];
902		/* Copy back the Scatter Gather buffers back to user space */
903		u32 j;
904		// TODO 64bit fix
905		struct sg_simple_element *sg;
906		int sg_size;
907
908		// re-acquire the original message to handle correctly the sg copy operation
909		memset(&rmsg, 0, I2O_OUTBOUND_MSG_FRAME_SIZE * 4);
910		// get user msg size in u32s
911		if (get_user(size, &user_msg[0])) {
912			rcode = -EFAULT;
913			goto sg_list_cleanup;
914		}
915		size = size >> 16;
916		size *= 4;
917		/* Copy in the user's I2O command */
918		if (copy_from_user(rmsg, user_msg, size)) {
919			rcode = -EFAULT;
920			goto sg_list_cleanup;
921		}
922		sg_count =
923		    (size - sg_offset * 4) / sizeof(struct sg_simple_element);
924
925		// TODO 64bit fix
926		sg = (struct sg_simple_element *)(rmsg + sg_offset);
927		for (j = 0; j < sg_count; j++) {
928			/* Copy out the SG list to user's buffer if necessary */
929			if (!
930			    (sg[j].
931			     flag_count & 0x4000000 /*I2O_SGL_FLAGS_DIR */ )) {
932				sg_size = sg[j].flag_count & 0xffffff;
933				// TODO 64bit fix
934				if (copy_to_user
935				    ((void __user *)sg[j].addr_bus, sg_list[j].virt,
936				     sg_size)) {
937					printk(KERN_WARNING
938					       "%s: Could not copy %p TO user %x\n",
939					       c->name, sg_list[j].virt,
940					       sg[j].addr_bus);
941					rcode = -EFAULT;
942					goto sg_list_cleanup;
943				}
944			}
945		}
946	}
947
948sg_list_cleanup:
949	/* Copy back the reply to user space */
950	if (reply_size) {
951		// we wrote our own values for context - now restore the user supplied ones
952		if (copy_from_user(reply + 2, user_msg + 2, sizeof(u32) * 2)) {
953			printk(KERN_WARNING
954			       "%s: Could not copy message context FROM user\n",
955			       c->name);
956			rcode = -EFAULT;
957		}
958		if (copy_to_user(user_reply, reply, reply_size)) {
959			printk(KERN_WARNING
960			       "%s: Could not copy reply TO user\n", c->name);
961			rcode = -EFAULT;
962		}
963	}
964
965	for (i = 0; i < sg_index; i++)
966		i2o_dma_free(&c->pdev->dev, &sg_list[i]);
967
968cleanup:
969	kfree(reply);
970out:
971	if (msg)
972		i2o_msg_nop(c, msg);
973	return rcode;
974}
975#endif
976
977/*
978 * IOCTL Handler
979 */
980static long i2o_cfg_ioctl(struct file *fp, unsigned int cmd, unsigned long arg)
981{
982	int ret;
983
984	lock_kernel();
985	switch (cmd) {
986	case I2OGETIOPS:
987		ret = i2o_cfg_getiops(arg);
988		break;
989
990	case I2OHRTGET:
991		ret = i2o_cfg_gethrt(arg);
992		break;
993
994	case I2OLCTGET:
995		ret = i2o_cfg_getlct(arg);
996		break;
997
998	case I2OPARMSET:
999		ret = i2o_cfg_parms(arg, I2OPARMSET);
1000		break;
1001
1002	case I2OPARMGET:
1003		ret = i2o_cfg_parms(arg, I2OPARMGET);
1004		break;
1005
1006	case I2OSWDL:
1007		ret = i2o_cfg_swdl(arg);
1008		break;
1009
1010	case I2OSWUL:
1011		ret = i2o_cfg_swul(arg);
1012		break;
1013
1014	case I2OSWDEL:
1015		ret = i2o_cfg_swdel(arg);
1016		break;
1017
1018	case I2OVALIDATE:
1019		ret = i2o_cfg_validate(arg);
1020		break;
1021
1022	case I2OEVTREG:
1023		ret = i2o_cfg_evt_reg(arg, fp);
1024		break;
1025
1026	case I2OEVTGET:
1027		ret = i2o_cfg_evt_get(arg, fp);
1028		break;
1029
1030#ifdef CONFIG_I2O_EXT_ADAPTEC
1031	case I2OPASSTHRU:
1032		ret = i2o_cfg_passthru(arg);
1033		break;
1034#endif
1035
1036	default:
1037		osm_debug("unknown ioctl called!\n");
1038		ret = -EINVAL;
1039	}
1040	unlock_kernel();
1041	return ret;
1042}
1043
1044static int cfg_open(struct inode *inode, struct file *file)
1045{
1046	struct i2o_cfg_info *tmp =
1047	    (struct i2o_cfg_info *)kmalloc(sizeof(struct i2o_cfg_info),
1048					   GFP_KERNEL);
1049	unsigned long flags;
1050
1051	if (!tmp)
1052		return -ENOMEM;
1053
1054	lock_kernel();
1055	file->private_data = (void *)(i2o_cfg_info_id++);
1056	tmp->fp = file;
1057	tmp->fasync = NULL;
1058	tmp->q_id = (ulong) file->private_data;
1059	tmp->q_len = 0;
1060	tmp->q_in = 0;
1061	tmp->q_out = 0;
1062	tmp->q_lost = 0;
1063	tmp->next = open_files;
1064
1065	spin_lock_irqsave(&i2o_config_lock, flags);
1066	open_files = tmp;
1067	spin_unlock_irqrestore(&i2o_config_lock, flags);
1068	unlock_kernel();
1069
1070	return 0;
1071}
1072
1073static int cfg_fasync(int fd, struct file *fp, int on)
1074{
1075	ulong id = (ulong) fp->private_data;
1076	struct i2o_cfg_info *p;
1077	int ret = -EBADF;
1078
1079	lock_kernel();
1080	for (p = open_files; p; p = p->next)
1081		if (p->q_id == id)
1082			break;
1083
1084	if (p)
1085		ret = fasync_helper(fd, fp, on, &p->fasync);
1086	unlock_kernel();
1087	return ret;
1088}
1089
1090static int cfg_release(struct inode *inode, struct file *file)
1091{
1092	ulong id = (ulong) file->private_data;
1093	struct i2o_cfg_info *p, **q;
1094	unsigned long flags;
1095
1096	lock_kernel();
1097	spin_lock_irqsave(&i2o_config_lock, flags);
1098	for (q = &open_files; (p = *q) != NULL; q = &p->next) {
1099		if (p->q_id == id) {
1100			*q = p->next;
1101			kfree(p);
1102			break;
1103		}
1104	}
1105	spin_unlock_irqrestore(&i2o_config_lock, flags);
1106	unlock_kernel();
1107
1108	return 0;
1109}
1110
1111static const struct file_operations config_fops = {
1112	.owner = THIS_MODULE,
1113	.llseek = no_llseek,
1114	.unlocked_ioctl = i2o_cfg_ioctl,
1115#ifdef CONFIG_COMPAT
1116	.compat_ioctl = i2o_cfg_compat_ioctl,
1117#endif
1118	.open = cfg_open,
1119	.release = cfg_release,
1120	.fasync = cfg_fasync,
1121};
1122
1123static struct miscdevice i2o_miscdev = {
1124	I2O_MINOR,
1125	"i2octl",
1126	&config_fops
1127};
1128
1129static int __init i2o_config_old_init(void)
1130{
1131	spin_lock_init(&i2o_config_lock);
1132
1133	if (misc_register(&i2o_miscdev) < 0) {
1134		osm_err("can't register device.\n");
1135		return -EBUSY;
1136	}
1137
1138	return 0;
1139}
1140
1141static void i2o_config_old_exit(void)
1142{
1143	misc_deregister(&i2o_miscdev);
1144}
1145
1146MODULE_AUTHOR("Red Hat Software");
1147