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