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