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