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