patch-2_6_7-vs1_9_1_12
[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->mem_offset + 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 *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 *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_iop_table = (u8*)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 *cmd = (struct i2o_cmd_hrtlct*)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 *cmd = (struct i2o_cmd_hrtlct*)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 *cmd = (struct i2o_cmd_psetget*)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 *cmd = (struct i2o_html*)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 *pxfer = (struct i2o_sw_xfer *)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 *pxfer = (struct i2o_sw_xfer *)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, *pxfer = (struct i2o_sw_xfer *)arg;
713         u32 msg[7];
714         unsigned int swlen;
715         int token;
716         
717         if (copy_from_user(&kxfer, pxfer, sizeof(struct i2o_sw_xfer)))
718                 return -EFAULT;
719                 
720         if (get_user(swlen, kxfer.swlen) < 0)
721                 return -EFAULT;
722                 
723         c = i2o_find_controller(kxfer.iop);
724         if (!c)
725                 return -ENXIO;
726
727         msg[0] = SEVEN_WORD_MSG_SIZE | SGL_OFFSET_0;
728         msg[1] = I2O_CMD_SW_REMOVE<<24 | HOST_TID<<12 | ADAPTER_TID;
729         msg[2] = (u32)i2o_cfg_context;
730         msg[3] = 0;
731         msg[4] = (u32)kxfer.flags<<24 | (u32)kxfer.sw_type<<16;
732         msg[5] = swlen;
733         msg[6] = kxfer.sw_id;
734
735         token = i2o_post_wait(c, msg, sizeof(msg), 10);
736         i2o_unlock_controller(c);
737         
738         if (token != I2O_POST_WAIT_OK)
739         {
740                 printk(KERN_INFO "i2o_config: swdel failed, DetailedStatus = %d\n", token);
741                 return -ETIMEDOUT;
742         }
743         
744         return 0;
745 }
746
747 int ioctl_validate(unsigned long arg)
748 {
749         int token;
750         int iop = (int)arg;
751         u32 msg[4];
752         struct i2o_controller *c;
753
754         c=i2o_find_controller(iop);
755         if (!c)
756                 return -ENXIO;
757
758         msg[0] = FOUR_WORD_MSG_SIZE|SGL_OFFSET_0;
759         msg[1] = I2O_CMD_CONFIG_VALIDATE<<24 | HOST_TID<<12 | iop;
760         msg[2] = (u32)i2o_cfg_context;
761         msg[3] = 0;
762
763         token = i2o_post_wait(c, msg, sizeof(msg), 10);
764         i2o_unlock_controller(c);
765
766         if (token != I2O_POST_WAIT_OK)
767         {
768                 printk(KERN_INFO "Can't validate configuration, ErrorStatus = %d\n",
769                         token);
770                 return -ETIMEDOUT;
771         }
772
773         return 0;
774 }   
775
776 static int ioctl_evt_reg(unsigned long arg, struct file *fp)
777 {
778         u32 msg[5];
779         struct i2o_evt_id *pdesc = (struct i2o_evt_id *)arg;
780         struct i2o_evt_id kdesc;
781         struct i2o_controller *iop;
782         struct i2o_device *d;
783
784         if (copy_from_user(&kdesc, pdesc, sizeof(struct i2o_evt_id)))
785                 return -EFAULT;
786
787         /* IOP exists? */
788         iop = i2o_find_controller(kdesc.iop);
789         if(!iop)
790                 return -ENXIO;
791         i2o_unlock_controller(iop);
792
793         /* Device exists? */
794         for(d = iop->devices; d; d = d->next)
795                 if(d->lct_data.tid == kdesc.tid)
796                         break;
797
798         if(!d)
799                 return -ENODEV;
800
801         msg[0] = FOUR_WORD_MSG_SIZE|SGL_OFFSET_0;
802         msg[1] = I2O_CMD_UTIL_EVT_REGISTER<<24 | HOST_TID<<12 | kdesc.tid;
803         msg[2] = (u32)i2o_cfg_context;
804         msg[3] = (u32)fp->private_data;
805         msg[4] = kdesc.evt_mask;
806
807         i2o_post_this(iop, msg, 20);
808
809         return 0;
810 }       
811
812 static int ioctl_evt_get(unsigned long arg, struct file *fp)
813 {
814         u32 id = (u32)fp->private_data;
815         struct i2o_cfg_info *p = NULL;
816         struct i2o_evt_get *uget = (struct i2o_evt_get*)arg;
817         struct i2o_evt_get kget;
818         unsigned long flags;
819
820         for(p = open_files; p; p = p->next)
821                 if(p->q_id == id)
822                         break;
823
824         if(!p->q_len)
825         {
826                 return -ENOENT;
827                 return 0;
828         }
829
830         memcpy(&kget.info, &p->event_q[p->q_out], sizeof(struct i2o_evt_info));
831         MODINC(p->q_out, I2O_EVT_Q_LEN);
832         spin_lock_irqsave(&i2o_config_lock, flags);
833         p->q_len--;
834         kget.pending = p->q_len;
835         kget.lost = p->q_lost;
836         spin_unlock_irqrestore(&i2o_config_lock, flags);
837
838         if(copy_to_user(uget, &kget, sizeof(struct i2o_evt_get)))
839                 return -EFAULT;
840         return 0;
841 }
842
843 static int ioctl_passthru(unsigned long arg)
844 {
845         struct i2o_cmd_passthru *cmd = (struct i2o_cmd_passthru *) arg;
846         struct i2o_controller *c;
847         u32 msg[MSG_FRAME_SIZE];
848         u32 *user_msg = (u32*)cmd->msg;
849         u32 *reply = NULL;
850         u32 *user_reply = NULL;
851         u32 size = 0;
852         u32 reply_size = 0;
853         u32 rcode = 0;
854         ulong sg_list[SG_TABLESIZE];
855         u32 sg_offset = 0;
856         u32 sg_count = 0;
857         int sg_index = 0;
858         u32 i = 0;
859         ulong p = 0;
860
861         c = i2o_find_controller(cmd->iop);
862         if(!c)
863                 return -ENXIO;
864
865         memset(&msg, 0, MSG_FRAME_SIZE*4);
866         if(get_user(size, &user_msg[0]))
867                 return -EFAULT;
868         size = size>>16;
869
870         user_reply = &user_msg[size];
871         if(size > MSG_FRAME_SIZE)
872                 return -EFAULT;
873         size *= 4; // Convert to bytes
874
875         /* Copy in the user's I2O command */
876         if(copy_from_user((void*)msg, (void*)user_msg, size))
877                 return -EFAULT;
878         if(get_user(reply_size, &user_reply[0]) < 0)
879                 return -EFAULT;
880
881         reply_size = reply_size>>16;
882         reply = kmalloc(REPLY_FRAME_SIZE*4, GFP_KERNEL);
883         if(!reply) {
884                 printk(KERN_WARNING"%s: Could not allocate reply buffer\n",c->name);
885                 return -ENOMEM;
886         }
887         memset(reply, 0, REPLY_FRAME_SIZE*4);
888         sg_offset = (msg[0]>>4)&0x0f;
889         msg[2] = (u32)i2o_cfg_context;
890         msg[3] = (u32)reply;
891
892         memset(sg_list,0, sizeof(sg_list[0])*SG_TABLESIZE);
893         if(sg_offset) {
894                 struct sg_simple_element *sg;
895
896                 if(sg_offset * 4 >= size) {
897                         rcode = -EFAULT;
898                         goto cleanup;
899                 }
900                 // TODO 64bit fix
901                 sg = (struct sg_simple_element*) (msg+sg_offset);
902                 sg_count = (size - sg_offset*4) / sizeof(struct sg_simple_element);
903                 if (sg_count > SG_TABLESIZE) {
904                         printk(KERN_DEBUG"%s:IOCTL SG List too large (%u)\n", c->name,sg_count);
905                         kfree (reply);
906                         return -EINVAL;
907                 }
908
909                 for(i = 0; i < sg_count; i++) {
910                         int sg_size;
911
912                         if (!(sg[i].flag_count & 0x10000000 /*I2O_SGL_FLAGS_SIMPLE_ADDRESS_ELEMENT*/)) {
913                                 printk(KERN_DEBUG"%s:Bad SG element %d - not simple (%x)\n",c->name,i,  sg[i].flag_count);
914                                 rcode = -EINVAL;
915                                 goto cleanup;
916                         }
917                         sg_size = sg[i].flag_count & 0xffffff;
918                         /* Allocate memory for the transfer */
919                         p = (ulong)kmalloc(sg_size, GFP_KERNEL);
920                         if (!p) {
921                                 printk(KERN_DEBUG"%s: Could not allocate SG buffer - size = %d buffer number %d of %d\n", c->name,sg_size,i,sg_count);
922                                 rcode = -ENOMEM;
923                                 goto cleanup;
924                         }
925                         sg_list[sg_index++] = p; // sglist indexed with input frame, not our internal frame.
926                         /* Copy in the user's SG buffer if necessary */
927                         if(sg[i].flag_count & 0x04000000 /*I2O_SGL_FLAGS_DIR*/) {
928                                 // TODO 64bit fix
929                                 if (copy_from_user((void*)p,(void*)sg[i].addr_bus, sg_size)) {
930                                         printk(KERN_DEBUG"%s: Could not copy SG buf %d FROM user\n",c->name,i);
931                                         rcode = -EFAULT;
932                                         goto cleanup;
933                                 }
934                         }
935                         //TODO 64bit fix
936                         sg[i].addr_bus = (u32)virt_to_bus((void*)p);
937                 }
938         }
939
940         rcode = i2o_post_wait(c, msg, size, 60);
941         if(rcode)
942                 goto cleanup;
943
944         if(sg_offset) {
945                 /* Copy back the Scatter Gather buffers back to user space */
946                 u32 j;
947                 // TODO 64bit fix
948                 struct sg_simple_element* sg;
949                 int sg_size;
950
951                 // re-acquire the original message to handle correctly the sg copy operation
952                 memset(&msg, 0, MSG_FRAME_SIZE*4);
953                 // get user msg size in u32s
954                 if (get_user(size, &user_msg[0])) {
955                         rcode = -EFAULT;
956                         goto cleanup;
957                 }
958                 size = size>>16;
959                 size *= 4;
960                 /* Copy in the user's I2O command */
961                 if (copy_from_user ((void*)msg, (void*)user_msg, size)) {
962                         rcode = -EFAULT;
963                         goto cleanup;
964                 }
965                 sg_count = (size - sg_offset*4) / sizeof(struct sg_simple_element);
966
967                  // TODO 64bit fix
968                 sg = (struct sg_simple_element*)(msg + sg_offset);
969                 for (j = 0; j < sg_count; j++) {
970                         /* Copy out the SG list to user's buffer if necessary */
971                         if (!(sg[j].flag_count & 0x4000000 /*I2O_SGL_FLAGS_DIR*/)) {
972                                 sg_size = sg[j].flag_count & 0xffffff;
973                                 // TODO 64bit fix
974                                 if (copy_to_user((void*)sg[j].addr_bus,(void*)sg_list[j], sg_size)) {
975                                         printk(KERN_WARNING"%s: Could not copy %lx TO user %x\n",c->name, sg_list[j], sg[j].addr_bus);
976                                         rcode = -EFAULT;
977                                         goto cleanup;
978                                 }
979                         }
980                 }
981         }
982
983         /* Copy back the reply to user space */
984         if (reply_size) {
985                 // we wrote our own values for context - now restore the user supplied ones
986                 if(copy_from_user(reply+2, user_msg+2, sizeof(u32)*2)) {
987                         printk(KERN_WARNING"%s: Could not copy message context FROM user\n",c->name);
988                         rcode = -EFAULT;
989                 }
990                 if(copy_to_user(user_reply, reply, reply_size)) {
991                         printk(KERN_WARNING"%s: Could not copy reply TO user\n",c->name);
992                         rcode = -EFAULT;
993                 }
994         }
995
996 cleanup:
997         kfree(reply);
998         i2o_unlock_controller(c);
999         return rcode;
1000 }
1001
1002 static int cfg_open(struct inode *inode, struct file *file)
1003 {
1004         struct i2o_cfg_info *tmp = 
1005                 (struct i2o_cfg_info *)kmalloc(sizeof(struct i2o_cfg_info), GFP_KERNEL);
1006         unsigned long flags;
1007
1008         if(!tmp)
1009                 return -ENOMEM;
1010
1011         file->private_data = (void*)(i2o_cfg_info_id++);
1012         tmp->fp = file;
1013         tmp->fasync = NULL;
1014         tmp->q_id = (u32)file->private_data;
1015         tmp->q_len = 0;
1016         tmp->q_in = 0;
1017         tmp->q_out = 0;
1018         tmp->q_lost = 0;
1019         tmp->next = open_files;
1020
1021         spin_lock_irqsave(&i2o_config_lock, flags);
1022         open_files = tmp;
1023         spin_unlock_irqrestore(&i2o_config_lock, flags);
1024         
1025         return 0;
1026 }
1027
1028 static int cfg_release(struct inode *inode, struct file *file)
1029 {
1030         u32 id = (u32)file->private_data;
1031         struct i2o_cfg_info *p1, *p2;
1032         unsigned long flags;
1033
1034         lock_kernel();
1035         p1 = p2 = NULL;
1036
1037         spin_lock_irqsave(&i2o_config_lock, flags);
1038         for(p1 = open_files; p1; )
1039         {
1040                 if(p1->q_id == id)
1041                 {
1042
1043                         if(p1->fasync)
1044                                 cfg_fasync(-1, file, 0);
1045                         if(p2)
1046                                 p2->next = p1->next;
1047                         else
1048                                 open_files = p1->next;
1049
1050                         kfree(p1);
1051                         break;
1052                 }
1053                 p2 = p1;
1054                 p1 = p1->next;
1055         }
1056         spin_unlock_irqrestore(&i2o_config_lock, flags);
1057         unlock_kernel();
1058
1059         return 0;
1060 }
1061
1062 static int cfg_fasync(int fd, struct file *fp, int on)
1063 {
1064         u32 id = (u32)fp->private_data;
1065         struct i2o_cfg_info *p;
1066
1067         for(p = open_files; p; p = p->next)
1068                 if(p->q_id == id)
1069                         break;
1070
1071         if(!p)
1072                 return -EBADF;
1073
1074         return fasync_helper(fd, fp, on, &p->fasync);
1075 }
1076
1077 static struct file_operations config_fops =
1078 {
1079         .owner          = THIS_MODULE,
1080         .llseek         = no_llseek,
1081         .read           = cfg_read,
1082         .write          = cfg_write,
1083         .ioctl          = cfg_ioctl,
1084         .open           = cfg_open,
1085         .release        = cfg_release,
1086         .fasync         = cfg_fasync,
1087 };
1088
1089 static struct miscdevice i2o_miscdev = {
1090         I2O_MINOR,
1091         "i2octl",
1092         &config_fops
1093 };      
1094
1095 static int __init i2o_config_init(void)
1096 {
1097         printk(KERN_INFO "I2O configuration manager v 0.04.\n");
1098         printk(KERN_INFO "  (C) Copyright 1999 Red Hat Software\n");
1099         
1100         if((page_buf = kmalloc(4096, GFP_KERNEL))==NULL)
1101         {
1102                 printk(KERN_ERR "i2o_config: no memory for page buffer.\n");
1103                 return -ENOBUFS;
1104         }
1105         if(misc_register(&i2o_miscdev) < 0)
1106         {
1107                 printk(KERN_ERR "i2o_config: can't register device.\n");
1108                 kfree(page_buf);
1109                 return -EBUSY;
1110         }
1111         /*
1112          *      Install our handler
1113          */
1114         if(i2o_install_handler(&cfg_handler)<0)
1115         {
1116                 kfree(page_buf);
1117                 printk(KERN_ERR "i2o_config: handler register failed.\n");
1118                 misc_deregister(&i2o_miscdev);
1119                 return -EBUSY;
1120         }
1121         /*
1122          *      The low 16bits of the transaction context must match this
1123          *      for everything we post. Otherwise someone else gets our mail
1124          */
1125         i2o_cfg_context = cfg_handler.context;
1126         return 0;
1127 }
1128
1129 static void i2o_config_exit(void)
1130 {
1131         misc_deregister(&i2o_miscdev);
1132         
1133         if(page_buf)
1134                 kfree(page_buf);
1135         if(i2o_cfg_context != -1)
1136                 i2o_remove_handler(&cfg_handler);
1137 }
1138  
1139 MODULE_AUTHOR("Red Hat Software");
1140 MODULE_DESCRIPTION("I2O Configuration");
1141 MODULE_LICENSE("GPL");
1142
1143 module_init(i2o_config_init);
1144 module_exit(i2o_config_exit);