Merge to Fedora kernel-2.6.18-1.2224_FC5 patched with stable patch-2.6.18.1-vs2.0...
[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/miscdevice.h>
34 #include <linux/smp_lock.h>
35 #include <linux/compat.h>
36
37 #include <asm/uaccess.h>
38
39 #include "core.h"
40
41 #define SG_TABLESIZE            30
42
43 static int i2o_cfg_ioctl(struct inode *, struct file *, unsigned int,
44                          unsigned long);
45
46 static spinlock_t i2o_config_lock;
47
48 #define MODINC(x,y) ((x) = ((x) + 1) % (y))
49
50 struct sg_simple_element {
51         u32 flag_count;
52         u32 addr_bus;
53 };
54
55 struct 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 };
66 static struct i2o_cfg_info *open_files = NULL;
67 static ulong i2o_cfg_info_id = 0;
68
69 static 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
87 static 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         /* We did a get user...so assuming mem is ok...is this bad? */
115         put_user(len, kcmd.reslen);
116         if (len > reslen)
117                 ret = -ENOBUFS;
118         if (copy_to_user(kcmd.resbuf, (void *)hrt, len))
119                 ret = -EFAULT;
120
121         return ret;
122 };
123
124 static 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         put_user(len, kcmd.reslen);
151         if (len > reslen)
152                 ret = -ENOBUFS;
153         else if (copy_to_user(kcmd.resbuf, lct, len))
154                 ret = -EFAULT;
155
156         return ret;
157 };
158
159 static int i2o_cfg_parms(unsigned long arg, unsigned int type)
160 {
161         int ret = 0;
162         struct i2o_controller *c;
163         struct i2o_device *dev;
164         struct i2o_cmd_psetget __user *cmd =
165             (struct i2o_cmd_psetget __user *)arg;
166         struct i2o_cmd_psetget kcmd;
167         u32 reslen;
168         u8 *ops;
169         u8 *res;
170         int len = 0;
171
172         u32 i2o_cmd = (type == I2OPARMGET ?
173                        I2O_CMD_UTIL_PARAMS_GET : I2O_CMD_UTIL_PARAMS_SET);
174
175         if (copy_from_user(&kcmd, cmd, sizeof(struct i2o_cmd_psetget)))
176                 return -EFAULT;
177
178         if (get_user(reslen, kcmd.reslen))
179                 return -EFAULT;
180
181         c = i2o_find_iop(kcmd.iop);
182         if (!c)
183                 return -ENXIO;
184
185         dev = i2o_iop_find_device(c, kcmd.tid);
186         if (!dev)
187                 return -ENXIO;
188
189         ops = (u8 *) kmalloc(kcmd.oplen, GFP_KERNEL);
190         if (!ops)
191                 return -ENOMEM;
192
193         if (copy_from_user(ops, kcmd.opbuf, kcmd.oplen)) {
194                 kfree(ops);
195                 return -EFAULT;
196         }
197
198         /*
199          * It's possible to have a _very_ large table
200          * and that the user asks for all of it at once...
201          */
202         res = (u8 *) kmalloc(65536, GFP_KERNEL);
203         if (!res) {
204                 kfree(ops);
205                 return -ENOMEM;
206         }
207
208         len = i2o_parm_issue(dev, i2o_cmd, ops, kcmd.oplen, res, 65536);
209         kfree(ops);
210
211         if (len < 0) {
212                 kfree(res);
213                 return -EAGAIN;
214         }
215
216         put_user(len, kcmd.reslen);
217         if (len > reslen)
218                 ret = -ENOBUFS;
219         else if (copy_to_user(kcmd.resbuf, res, len))
220                 ret = -EFAULT;
221
222         kfree(res);
223
224         return ret;
225 };
226
227 static int i2o_cfg_swdl(unsigned long arg)
228 {
229         struct i2o_sw_xfer kxfer;
230         struct i2o_sw_xfer __user *pxfer = (struct i2o_sw_xfer __user *)arg;
231         unsigned char maxfrag = 0, curfrag = 1;
232         struct i2o_dma buffer;
233         struct i2o_message *msg;
234         unsigned int status = 0, swlen = 0, fragsize = 8192;
235         struct i2o_controller *c;
236
237         if (copy_from_user(&kxfer, pxfer, sizeof(struct i2o_sw_xfer)))
238                 return -EFAULT;
239
240         if (get_user(swlen, kxfer.swlen) < 0)
241                 return -EFAULT;
242
243         if (get_user(maxfrag, kxfer.maxfrag) < 0)
244                 return -EFAULT;
245
246         if (get_user(curfrag, kxfer.curfrag) < 0)
247                 return -EFAULT;
248
249         if (curfrag == maxfrag)
250                 fragsize = swlen - (maxfrag - 1) * 8192;
251
252         if (!kxfer.buf || !access_ok(VERIFY_READ, kxfer.buf, fragsize))
253                 return -EFAULT;
254
255         c = i2o_find_iop(kxfer.iop);
256         if (!c)
257                 return -ENXIO;
258
259         msg = i2o_msg_get_wait(c, I2O_TIMEOUT_MESSAGE_GET);
260         if (IS_ERR(msg))
261                 return PTR_ERR(msg);
262
263         if (i2o_dma_alloc(&c->pdev->dev, &buffer, fragsize, GFP_KERNEL)) {
264                 i2o_msg_nop(c, msg);
265                 return -ENOMEM;
266         }
267
268         __copy_from_user(buffer.virt, kxfer.buf, fragsize);
269
270         msg->u.head[0] = cpu_to_le32(NINE_WORD_MSG_SIZE | SGL_OFFSET_7);
271         msg->u.head[1] =
272             cpu_to_le32(I2O_CMD_SW_DOWNLOAD << 24 | HOST_TID << 12 |
273                         ADAPTER_TID);
274         msg->u.head[2] = cpu_to_le32(i2o_config_driver.context);
275         msg->u.head[3] = cpu_to_le32(0);
276         msg->body[0] =
277             cpu_to_le32((((u32) kxfer.flags) << 24) | (((u32) kxfer.
278                                                         sw_type) << 16) |
279                         (((u32) maxfrag) << 8) | (((u32) curfrag)));
280         msg->body[1] = cpu_to_le32(swlen);
281         msg->body[2] = cpu_to_le32(kxfer.sw_id);
282         msg->body[3] = cpu_to_le32(0xD0000000 | fragsize);
283         msg->body[4] = cpu_to_le32(buffer.phys);
284
285         osm_debug("swdl frag %d/%d (size %d)\n", curfrag, maxfrag, fragsize);
286         status = i2o_msg_post_wait_mem(c, msg, 60, &buffer);
287
288         if (status != -ETIMEDOUT)
289                 i2o_dma_free(&c->pdev->dev, &buffer);
290
291         if (status != I2O_POST_WAIT_OK) {
292                 // it fails if you try and send frags out of order
293                 // and for some yet unknown reasons too
294                 osm_info("swdl failed, DetailedStatus = %d\n", status);
295                 return status;
296         }
297
298         return 0;
299 };
300
301 static int i2o_cfg_swul(unsigned long arg)
302 {
303         struct i2o_sw_xfer kxfer;
304         struct i2o_sw_xfer __user *pxfer = (struct i2o_sw_xfer __user *)arg;
305         unsigned char maxfrag = 0, curfrag = 1;
306         struct i2o_dma buffer;
307         struct i2o_message *msg;
308         unsigned int status = 0, swlen = 0, fragsize = 8192;
309         struct i2o_controller *c;
310         int ret = 0;
311
312         if (copy_from_user(&kxfer, pxfer, sizeof(struct i2o_sw_xfer)))
313                 goto return_fault;
314
315         if (get_user(swlen, kxfer.swlen) < 0)
316                 goto return_fault;
317
318         if (get_user(maxfrag, kxfer.maxfrag) < 0)
319                 goto return_fault;
320
321         if (get_user(curfrag, kxfer.curfrag) < 0)
322                 goto return_fault;
323
324         if (curfrag == maxfrag)
325                 fragsize = swlen - (maxfrag - 1) * 8192;
326
327         if (!kxfer.buf)
328                 goto return_fault;
329
330         c = i2o_find_iop(kxfer.iop);
331         if (!c)
332                 return -ENXIO;
333
334         msg = i2o_msg_get_wait(c, I2O_TIMEOUT_MESSAGE_GET);
335         if (IS_ERR(msg))
336                 return PTR_ERR(msg);
337
338         if (i2o_dma_alloc(&c->pdev->dev, &buffer, fragsize, GFP_KERNEL)) {
339                 i2o_msg_nop(c, msg);
340                 return -ENOMEM;
341         }
342
343         msg->u.head[0] = cpu_to_le32(NINE_WORD_MSG_SIZE | SGL_OFFSET_7);
344         msg->u.head[1] =
345             cpu_to_le32(I2O_CMD_SW_UPLOAD << 24 | HOST_TID << 12 | ADAPTER_TID);
346         msg->u.head[2] = cpu_to_le32(i2o_config_driver.context);
347         msg->u.head[3] = cpu_to_le32(0);
348         msg->body[0] =
349             cpu_to_le32((u32) kxfer.flags << 24 | (u32) kxfer.
350                         sw_type << 16 | (u32) maxfrag << 8 | (u32) curfrag);
351         msg->body[1] = cpu_to_le32(swlen);
352         msg->body[2] = cpu_to_le32(kxfer.sw_id);
353         msg->body[3] = cpu_to_le32(0xD0000000 | fragsize);
354         msg->body[4] = cpu_to_le32(buffer.phys);
355
356         osm_debug("swul frag %d/%d (size %d)\n", curfrag, maxfrag, fragsize);
357         status = i2o_msg_post_wait_mem(c, msg, 60, &buffer);
358
359         if (status != I2O_POST_WAIT_OK) {
360                 if (status != -ETIMEDOUT)
361                         i2o_dma_free(&c->pdev->dev, &buffer);
362
363                 osm_info("swul failed, DetailedStatus = %d\n", status);
364                 return status;
365         }
366
367         if (copy_to_user(kxfer.buf, buffer.virt, fragsize))
368                 ret = -EFAULT;
369
370         i2o_dma_free(&c->pdev->dev, &buffer);
371
372       return_ret:
373         return ret;
374       return_fault:
375         ret = -EFAULT;
376         goto return_ret;
377 };
378
379 static int i2o_cfg_swdel(unsigned long arg)
380 {
381         struct i2o_controller *c;
382         struct i2o_sw_xfer kxfer;
383         struct i2o_sw_xfer __user *pxfer = (struct i2o_sw_xfer __user *)arg;
384         struct i2o_message *msg;
385         unsigned int swlen;
386         int token;
387
388         if (copy_from_user(&kxfer, pxfer, sizeof(struct i2o_sw_xfer)))
389                 return -EFAULT;
390
391         if (get_user(swlen, kxfer.swlen) < 0)
392                 return -EFAULT;
393
394         c = i2o_find_iop(kxfer.iop);
395         if (!c)
396                 return -ENXIO;
397
398         msg = i2o_msg_get_wait(c, I2O_TIMEOUT_MESSAGE_GET);
399         if (IS_ERR(msg))
400                 return PTR_ERR(msg);
401
402         msg->u.head[0] = cpu_to_le32(SEVEN_WORD_MSG_SIZE | SGL_OFFSET_0);
403         msg->u.head[1] =
404             cpu_to_le32(I2O_CMD_SW_REMOVE << 24 | HOST_TID << 12 | ADAPTER_TID);
405         msg->u.head[2] = cpu_to_le32(i2o_config_driver.context);
406         msg->u.head[3] = cpu_to_le32(0);
407         msg->body[0] =
408             cpu_to_le32((u32) kxfer.flags << 24 | (u32) kxfer.sw_type << 16);
409         msg->body[1] = cpu_to_le32(swlen);
410         msg->body[2] = cpu_to_le32(kxfer.sw_id);
411
412         token = i2o_msg_post_wait(c, msg, 10);
413
414         if (token != I2O_POST_WAIT_OK) {
415                 osm_info("swdel failed, DetailedStatus = %d\n", token);
416                 return -ETIMEDOUT;
417         }
418
419         return 0;
420 };
421
422 static int i2o_cfg_validate(unsigned long arg)
423 {
424         int token;
425         int iop = (int)arg;
426         struct i2o_message *msg;
427         struct i2o_controller *c;
428
429         c = i2o_find_iop(iop);
430         if (!c)
431                 return -ENXIO;
432
433         msg = i2o_msg_get_wait(c, I2O_TIMEOUT_MESSAGE_GET);
434         if (IS_ERR(msg))
435                 return PTR_ERR(msg);
436
437         msg->u.head[0] = cpu_to_le32(FOUR_WORD_MSG_SIZE | SGL_OFFSET_0);
438         msg->u.head[1] =
439             cpu_to_le32(I2O_CMD_CONFIG_VALIDATE << 24 | HOST_TID << 12 | iop);
440         msg->u.head[2] = cpu_to_le32(i2o_config_driver.context);
441         msg->u.head[3] = cpu_to_le32(0);
442
443         token = i2o_msg_post_wait(c, msg, 10);
444
445         if (token != I2O_POST_WAIT_OK) {
446                 osm_info("Can't validate configuration, ErrorStatus = %d\n",
447                          token);
448                 return -ETIMEDOUT;
449         }
450
451         return 0;
452 };
453
454 static int i2o_cfg_evt_reg(unsigned long arg, struct file *fp)
455 {
456         struct i2o_message *msg;
457         struct i2o_evt_id __user *pdesc = (struct i2o_evt_id __user *)arg;
458         struct i2o_evt_id kdesc;
459         struct i2o_controller *c;
460         struct i2o_device *d;
461
462         if (copy_from_user(&kdesc, pdesc, sizeof(struct i2o_evt_id)))
463                 return -EFAULT;
464
465         /* IOP exists? */
466         c = i2o_find_iop(kdesc.iop);
467         if (!c)
468                 return -ENXIO;
469
470         /* Device exists? */
471         d = i2o_iop_find_device(c, kdesc.tid);
472         if (!d)
473                 return -ENODEV;
474
475         msg = i2o_msg_get_wait(c, I2O_TIMEOUT_MESSAGE_GET);
476         if (IS_ERR(msg))
477                 return PTR_ERR(msg);
478
479         msg->u.head[0] = cpu_to_le32(FOUR_WORD_MSG_SIZE | SGL_OFFSET_0);
480         msg->u.head[1] =
481             cpu_to_le32(I2O_CMD_UTIL_EVT_REGISTER << 24 | HOST_TID << 12 |
482                         kdesc.tid);
483         msg->u.head[2] = cpu_to_le32(i2o_config_driver.context);
484         msg->u.head[3] = cpu_to_le32(i2o_cntxt_list_add(c, fp->private_data));
485         msg->body[0] = cpu_to_le32(kdesc.evt_mask);
486
487         i2o_msg_post(c, msg);
488
489         return 0;
490 }
491
492 static int i2o_cfg_evt_get(unsigned long arg, struct file *fp)
493 {
494         struct i2o_cfg_info *p = NULL;
495         struct i2o_evt_get __user *uget = (struct i2o_evt_get __user *)arg;
496         struct i2o_evt_get kget;
497         unsigned long flags;
498
499         for (p = open_files; p; p = p->next)
500                 if (p->q_id == (ulong) fp->private_data)
501                         break;
502
503         if (!p->q_len)
504                 return -ENOENT;
505
506         memcpy(&kget.info, &p->event_q[p->q_out], sizeof(struct i2o_evt_info));
507         MODINC(p->q_out, I2O_EVT_Q_LEN);
508         spin_lock_irqsave(&i2o_config_lock, flags);
509         p->q_len--;
510         kget.pending = p->q_len;
511         kget.lost = p->q_lost;
512         spin_unlock_irqrestore(&i2o_config_lock, flags);
513
514         if (copy_to_user(uget, &kget, sizeof(struct i2o_evt_get)))
515                 return -EFAULT;
516         return 0;
517 }
518
519 #ifdef CONFIG_I2O_EXT_ADAPTEC
520 #ifdef CONFIG_COMPAT
521 static int i2o_cfg_passthru32(struct file *file, unsigned cmnd,
522                               unsigned long arg)
523 {
524         struct i2o_cmd_passthru32 __user *cmd;
525         struct i2o_controller *c;
526         u32 __user *user_msg;
527         u32 *reply = NULL;
528         u32 __user *user_reply = NULL;
529         u32 size = 0;
530         u32 reply_size = 0;
531         u32 rcode = 0;
532         struct i2o_dma sg_list[SG_TABLESIZE];
533         u32 sg_offset = 0;
534         u32 sg_count = 0;
535         u32 i = 0;
536         u32 sg_index = 0;
537         i2o_status_block *sb;
538         struct i2o_message *msg;
539         unsigned int iop;
540
541         cmd = (struct i2o_cmd_passthru32 __user *)arg;
542
543         if (get_user(iop, &cmd->iop) || get_user(i, &cmd->msg))
544                 return -EFAULT;
545
546         user_msg = compat_ptr(i);
547
548         c = i2o_find_iop(iop);
549         if (!c) {
550                 osm_debug("controller %d not found\n", iop);
551                 return -ENXIO;
552         }
553
554         msg = i2o_msg_get_wait(c, I2O_TIMEOUT_MESSAGE_GET);
555
556         sb = c->status_block.virt;
557
558         if (get_user(size, &user_msg[0])) {
559                 osm_warn("unable to get size!\n");
560                 return -EFAULT;
561         }
562         size = size >> 16;
563
564         if (size > sb->inbound_frame_size) {
565                 osm_warn("size of message > inbound_frame_size");
566                 return -EFAULT;
567         }
568
569         user_reply = &user_msg[size];
570
571         size <<= 2;             // Convert to bytes
572
573         /* Copy in the user's I2O command */
574         if (copy_from_user(msg, user_msg, size)) {
575                 osm_warn("unable to copy user message\n");
576                 return -EFAULT;
577         }
578         i2o_dump_message(msg);
579
580         if (get_user(reply_size, &user_reply[0]) < 0)
581                 return -EFAULT;
582
583         reply_size >>= 16;
584         reply_size <<= 2;
585
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                 return -ENOMEM;
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
631                             (&c->pdev->dev, p, sg_size,
632                              PCI_DMA_BIDIRECTIONAL)) {
633                                 printk(KERN_DEBUG
634                                        "%s: Could not allocate SG buffer - size = %d buffer number %d of %d\n",
635                                        c->name, sg_size, i, sg_count);
636                                 rcode = -ENOMEM;
637                                 goto sg_list_cleanup;
638                         }
639                         sg_index++;
640                         /* Copy in the user's SG buffer if necessary */
641                         if (sg[i].
642                             flag_count & 0x04000000 /*I2O_SGL_FLAGS_DIR */ ) {
643                                 // TODO 64bit fix
644                                 if (copy_from_user
645                                     (p->virt,
646                                      (void __user *)(unsigned long)sg[i].
647                                      addr_bus, sg_size)) {
648                                         printk(KERN_DEBUG
649                                                "%s: Could not copy SG buf %d FROM user\n",
650                                                c->name, i);
651                                         rcode = -EFAULT;
652                                         goto sg_list_cleanup;
653                                 }
654                         }
655                         //TODO 64bit fix
656                         sg[i].addr_bus = (u32) p->phys;
657                 }
658         }
659
660         rcode = i2o_msg_post_wait(c, msg, 60);
661         if (rcode) {
662                 reply[4] = ((u32) rcode) << 24;
663                 goto sg_list_cleanup;
664         }
665
666         if (sg_offset) {
667                 u32 msg[I2O_OUTBOUND_MSG_FRAME_SIZE];
668                 /* Copy back the Scatter Gather buffers back to user space */
669                 u32 j;
670                 // TODO 64bit fix
671                 struct sg_simple_element *sg;
672                 int sg_size;
673
674                 // re-acquire the original message to handle correctly the sg copy operation
675                 memset(&msg, 0, I2O_OUTBOUND_MSG_FRAME_SIZE * 4);
676                 // get user msg size in u32s
677                 if (get_user(size, &user_msg[0])) {
678                         rcode = -EFAULT;
679                         goto sg_list_cleanup;
680                 }
681                 size = size >> 16;
682                 size *= 4;
683                 /* Copy in the user's I2O command */
684                 if (copy_from_user(msg, user_msg, size)) {
685                         rcode = -EFAULT;
686                         goto sg_list_cleanup;
687                 }
688                 sg_count =
689                     (size - sg_offset * 4) / sizeof(struct sg_simple_element);
690
691                 // TODO 64bit fix
692                 sg = (struct sg_simple_element *)(msg + sg_offset);
693                 for (j = 0; j < sg_count; j++) {
694                         /* Copy out the SG list to user's buffer if necessary */
695                         if (!
696                             (sg[j].
697                              flag_count & 0x4000000 /*I2O_SGL_FLAGS_DIR */ )) {
698                                 sg_size = sg[j].flag_count & 0xffffff;
699                                 // TODO 64bit fix
700                                 if (copy_to_user
701                                     ((void __user *)(u64) sg[j].addr_bus,
702                                      sg_list[j].virt, sg_size)) {
703                                         printk(KERN_WARNING
704                                                "%s: Could not copy %p TO user %x\n",
705                                                c->name, sg_list[j].virt,
706                                                sg[j].addr_bus);
707                                         rcode = -EFAULT;
708                                         goto sg_list_cleanup;
709                                 }
710                         }
711                 }
712         }
713
714       sg_list_cleanup:
715         /* Copy back the reply to user space */
716         if (reply_size) {
717                 // we wrote our own values for context - now restore the user supplied ones
718                 if (copy_from_user(reply + 2, user_msg + 2, sizeof(u32) * 2)) {
719                         printk(KERN_WARNING
720                                "%s: Could not copy message context FROM user\n",
721                                c->name);
722                         rcode = -EFAULT;
723                         goto sg_list_cleanup;
724                 }
725                 if (copy_to_user(user_reply, reply, reply_size)) {
726                         printk(KERN_WARNING
727                                "%s: Could not copy reply TO user\n", c->name);
728                         rcode = -EFAULT;
729                 }
730         }
731
732         for (i = 0; i < sg_index; i++)
733                 i2o_dma_free(&c->pdev->dev, &sg_list[i]);
734
735       cleanup:
736         kfree(reply);
737         return rcode;
738 }
739
740 static 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(NULL, 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 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 *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         msg = i2o_msg_get_wait(c, I2O_TIMEOUT_MESSAGE_GET);
793
794         sb = c->status_block.virt;
795
796         if (get_user(size, &user_msg[0]))
797                 return -EFAULT;
798         size = size >> 16;
799
800         if (size > sb->inbound_frame_size) {
801                 osm_warn("size of message > inbound_frame_size");
802                 return -EFAULT;
803         }
804
805         user_reply = &user_msg[size];
806
807         size <<= 2;             // Convert to bytes
808
809         /* Copy in the user's I2O command */
810         if (copy_from_user(msg, user_msg, size))
811                 return -EFAULT;
812
813         if (get_user(reply_size, &user_reply[0]) < 0)
814                 return -EFAULT;
815
816         reply_size >>= 16;
817         reply_size <<= 2;
818
819         reply = kzalloc(reply_size, GFP_KERNEL);
820         if (!reply) {
821                 printk(KERN_WARNING "%s: Could not allocate reply buffer\n",
822                        c->name);
823                 return -ENOMEM;
824         }
825
826         sg_offset = (msg->u.head[0] >> 4) & 0x0f;
827
828         memset(sg_list, 0, sizeof(sg_list[0]) * SG_TABLESIZE);
829         if (sg_offset) {
830                 struct sg_simple_element *sg;
831
832                 if (sg_offset * 4 >= size) {
833                         rcode = -EFAULT;
834                         goto cleanup;
835                 }
836                 // TODO 64bit fix
837                 sg = (struct sg_simple_element *)((&msg->u.head[0]) +
838                                                   sg_offset);
839                 sg_count =
840                     (size - sg_offset * 4) / sizeof(struct sg_simple_element);
841                 if (sg_count > SG_TABLESIZE) {
842                         printk(KERN_DEBUG "%s:IOCTL SG List too large (%u)\n",
843                                c->name, sg_count);
844                         rcode = -EINVAL;
845                         goto cleanup;
846                 }
847
848                 for (i = 0; i < sg_count; i++) {
849                         int sg_size;
850
851                         if (!(sg[i].flag_count & 0x10000000
852                               /*I2O_SGL_FLAGS_SIMPLE_ADDRESS_ELEMENT */ )) {
853                                 printk(KERN_DEBUG
854                                        "%s:Bad SG element %d - not simple (%x)\n",
855                                        c->name, i, sg[i].flag_count);
856                                 rcode = -EINVAL;
857                                 goto sg_list_cleanup;
858                         }
859                         sg_size = sg[i].flag_count & 0xffffff;
860                         /* Allocate memory for the transfer */
861                         p = kmalloc(sg_size, GFP_KERNEL);
862                         if (!p) {
863                                 printk(KERN_DEBUG
864                                        "%s: Could not allocate SG buffer - size = %d buffer number %d of %d\n",
865                                        c->name, sg_size, i, sg_count);
866                                 rcode = -ENOMEM;
867                                 goto sg_list_cleanup;
868                         }
869                         sg_list[sg_index++] = p;        // sglist indexed with input frame, not our internal frame.
870                         /* Copy in the user's SG buffer if necessary */
871                         if (sg[i].
872                             flag_count & 0x04000000 /*I2O_SGL_FLAGS_DIR */ ) {
873                                 // TODO 64bit fix
874                                 if (copy_from_user
875                                     (p, (void __user *)sg[i].addr_bus,
876                                      sg_size)) {
877                                         printk(KERN_DEBUG
878                                                "%s: Could not copy SG buf %d FROM user\n",
879                                                c->name, i);
880                                         rcode = -EFAULT;
881                                         goto sg_list_cleanup;
882                                 }
883                         }
884                         //TODO 64bit fix
885                         sg[i].addr_bus = virt_to_bus(p);
886                 }
887         }
888
889         rcode = i2o_msg_post_wait(c, msg, 60);
890         if (rcode) {
891                 reply[4] = ((u32) rcode) << 24;
892                 goto sg_list_cleanup;
893         }
894
895         if (sg_offset) {
896                 u32 msg[128];
897                 /* Copy back the Scatter Gather buffers back to user space */
898                 u32 j;
899                 // TODO 64bit fix
900                 struct sg_simple_element *sg;
901                 int sg_size;
902
903                 // re-acquire the original message to handle correctly the sg copy operation
904                 memset(&msg, 0, I2O_OUTBOUND_MSG_FRAME_SIZE * 4);
905                 // get user msg size in u32s
906                 if (get_user(size, &user_msg[0])) {
907                         rcode = -EFAULT;
908                         goto sg_list_cleanup;
909                 }
910                 size = size >> 16;
911                 size *= 4;
912                 /* Copy in the user's I2O command */
913                 if (copy_from_user(msg, user_msg, size)) {
914                         rcode = -EFAULT;
915                         goto sg_list_cleanup;
916                 }
917                 sg_count =
918                     (size - sg_offset * 4) / sizeof(struct sg_simple_element);
919
920                 // TODO 64bit fix
921                 sg = (struct sg_simple_element *)(msg + sg_offset);
922                 for (j = 0; j < sg_count; j++) {
923                         /* Copy out the SG list to user's buffer if necessary */
924                         if (!
925                             (sg[j].
926                              flag_count & 0x4000000 /*I2O_SGL_FLAGS_DIR */ )) {
927                                 sg_size = sg[j].flag_count & 0xffffff;
928                                 // TODO 64bit fix
929                                 if (copy_to_user
930                                     ((void __user *)sg[j].addr_bus, sg_list[j],
931                                      sg_size)) {
932                                         printk(KERN_WARNING
933                                                "%s: Could not copy %p TO user %x\n",
934                                                c->name, sg_list[j],
935                                                sg[j].addr_bus);
936                                         rcode = -EFAULT;
937                                         goto sg_list_cleanup;
938                                 }
939                         }
940                 }
941         }
942
943       sg_list_cleanup:
944         /* Copy back the reply to user space */
945         if (reply_size) {
946                 // we wrote our own values for context - now restore the user supplied ones
947                 if (copy_from_user(reply + 2, user_msg + 2, sizeof(u32) * 2)) {
948                         printk(KERN_WARNING
949                                "%s: Could not copy message context FROM user\n",
950                                c->name);
951                         rcode = -EFAULT;
952                 }
953                 if (copy_to_user(user_reply, reply, reply_size)) {
954                         printk(KERN_WARNING
955                                "%s: Could not copy reply TO user\n", c->name);
956                         rcode = -EFAULT;
957                 }
958         }
959
960         for (i = 0; i < sg_index; i++)
961                 kfree(sg_list[i]);
962
963       cleanup:
964         kfree(reply);
965         return rcode;
966 }
967 #endif
968
969 /*
970  * IOCTL Handler
971  */
972 static int i2o_cfg_ioctl(struct inode *inode, struct file *fp, unsigned int cmd,
973                          unsigned long arg)
974 {
975         int ret;
976
977         switch (cmd) {
978         case I2OGETIOPS:
979                 ret = i2o_cfg_getiops(arg);
980                 break;
981
982         case I2OHRTGET:
983                 ret = i2o_cfg_gethrt(arg);
984                 break;
985
986         case I2OLCTGET:
987                 ret = i2o_cfg_getlct(arg);
988                 break;
989
990         case I2OPARMSET:
991                 ret = i2o_cfg_parms(arg, I2OPARMSET);
992                 break;
993
994         case I2OPARMGET:
995                 ret = i2o_cfg_parms(arg, I2OPARMGET);
996                 break;
997
998         case I2OSWDL:
999                 ret = i2o_cfg_swdl(arg);
1000                 break;
1001
1002         case I2OSWUL:
1003                 ret = i2o_cfg_swul(arg);
1004                 break;
1005
1006         case I2OSWDEL:
1007                 ret = i2o_cfg_swdel(arg);
1008                 break;
1009
1010         case I2OVALIDATE:
1011                 ret = i2o_cfg_validate(arg);
1012                 break;
1013
1014         case I2OEVTREG:
1015                 ret = i2o_cfg_evt_reg(arg, fp);
1016                 break;
1017
1018         case I2OEVTGET:
1019                 ret = i2o_cfg_evt_get(arg, fp);
1020                 break;
1021
1022 #ifdef CONFIG_I2O_EXT_ADAPTEC
1023         case I2OPASSTHRU:
1024                 ret = i2o_cfg_passthru(arg);
1025                 break;
1026 #endif
1027
1028         default:
1029                 osm_debug("unknown ioctl called!\n");
1030                 ret = -EINVAL;
1031         }
1032
1033         return ret;
1034 }
1035
1036 static int cfg_open(struct inode *inode, struct file *file)
1037 {
1038         struct i2o_cfg_info *tmp =
1039             (struct i2o_cfg_info *)kmalloc(sizeof(struct i2o_cfg_info),
1040                                            GFP_KERNEL);
1041         unsigned long flags;
1042
1043         if (!tmp)
1044                 return -ENOMEM;
1045
1046         file->private_data = (void *)(i2o_cfg_info_id++);
1047         tmp->fp = file;
1048         tmp->fasync = NULL;
1049         tmp->q_id = (ulong) file->private_data;
1050         tmp->q_len = 0;
1051         tmp->q_in = 0;
1052         tmp->q_out = 0;
1053         tmp->q_lost = 0;
1054         tmp->next = open_files;
1055
1056         spin_lock_irqsave(&i2o_config_lock, flags);
1057         open_files = tmp;
1058         spin_unlock_irqrestore(&i2o_config_lock, flags);
1059
1060         return 0;
1061 }
1062
1063 static int cfg_fasync(int fd, struct file *fp, int on)
1064 {
1065         ulong id = (ulong) fp->private_data;
1066         struct i2o_cfg_info *p;
1067
1068         for (p = open_files; p; p = p->next)
1069                 if (p->q_id == id)
1070                         break;
1071
1072         if (!p)
1073                 return -EBADF;
1074
1075         return fasync_helper(fd, fp, on, &p->fasync);
1076 }
1077
1078 static int cfg_release(struct inode *inode, struct file *file)
1079 {
1080         ulong id = (ulong) file->private_data;
1081         struct i2o_cfg_info *p1, *p2;
1082         unsigned long flags;
1083
1084         lock_kernel();
1085         p1 = p2 = NULL;
1086
1087         spin_lock_irqsave(&i2o_config_lock, flags);
1088         for (p1 = open_files; p1;) {
1089                 if (p1->q_id == id) {
1090
1091                         if (p1->fasync)
1092                                 cfg_fasync(-1, file, 0);
1093                         if (p2)
1094                                 p2->next = p1->next;
1095                         else
1096                                 open_files = p1->next;
1097
1098                         kfree(p1);
1099                         break;
1100                 }
1101                 p2 = p1;
1102                 p1 = p1->next;
1103         }
1104         spin_unlock_irqrestore(&i2o_config_lock, flags);
1105         unlock_kernel();
1106
1107         return 0;
1108 }
1109
1110 static struct file_operations config_fops = {
1111         .owner = THIS_MODULE,
1112         .llseek = no_llseek,
1113         .ioctl = i2o_cfg_ioctl,
1114 #ifdef CONFIG_COMPAT
1115         .compat_ioctl = i2o_cfg_compat_ioctl,
1116 #endif
1117         .open = cfg_open,
1118         .release = cfg_release,
1119         .fasync = cfg_fasync,
1120 };
1121
1122 static struct miscdevice i2o_miscdev = {
1123         I2O_MINOR,
1124         "i2octl",
1125         &config_fops
1126 };
1127
1128 static int __init i2o_config_old_init(void)
1129 {
1130         spin_lock_init(&i2o_config_lock);
1131
1132         if (misc_register(&i2o_miscdev) < 0) {
1133                 osm_err("can't register device.\n");
1134                 return -EBUSY;
1135         }
1136
1137         return 0;
1138 }
1139
1140 static void i2o_config_old_exit(void)
1141 {
1142         misc_deregister(&i2o_miscdev);
1143 }
1144
1145 MODULE_AUTHOR("Red Hat Software");