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