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