2 * I2O Configuration Interface Driver
4 * (C) Copyright 1999 Red Hat Software
6 * Written by Alan Cox, Building Number Three Ltd
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)
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation; either version
16 * 2 of the License, or (at your option) any later version.
19 #include <linux/module.h>
20 #include <linux/kernel.h>
21 #include <linux/pci.h>
22 #include <linux/i2o.h>
23 #include <linux/errno.h>
24 #include <linux/init.h>
25 #include <linux/malloc.h>
26 #include <linux/miscdevice.h>
28 #include <linux/spinlock.h>
30 #include <asm/uaccess.h>
35 static int i2o_cfg_token
= 0;
36 static int i2o_cfg_context
= -1;
37 static void *page_buf
;
38 static void *i2o_buffer
;
40 static int i2o_pagelen
;
44 static spinlock_t i2o_config_lock
= SPIN_LOCK_UNLOCKED
;
45 struct wait_queue
*i2o_wait_queue
;
47 static int ioctl_getiops(unsigned long);
48 static int ioctl_gethrt(unsigned long);
49 static int ioctl_getlct(unsigned long);
50 static int ioctl_parms(unsigned long, unsigned int);
51 static int ioctl_html(unsigned long);
52 static int ioctl_swdl(unsigned long);
53 static int ioctl_swul(unsigned long);
54 static int ioctl_swdel(unsigned long);
57 * This is the callback for any message we have posted. The message itself
58 * will be returned to the message pool when we return from the IRQ
60 * This runs in irq context so be short and sweet.
62 static void i2o_cfg_reply(struct i2o_handler
*h
, struct i2o_controller
*c
, struct i2o_message
*m
)
64 i2o_cfg_token
= I2O_POST_WAIT_OK
;
70 * Each of these describes an i2o message handler. They are
71 * multiplexed by the i2o_core code
74 struct i2o_handler cfg_handler
=
79 0xffffffff // All classes
82 static long long cfg_llseek(struct file
*file
, long long offset
, int origin
)
87 /* i2ocontroller/i2odevice/page/?data */
89 static ssize_t
cfg_write(struct file
*file
, const char *buf
, size_t count
, loff_t
*ppos
)
91 printk(KERN_INFO
"i2o_config write not yet supported\n");
96 /* To be written for event management support */
97 static ssize_t
cfg_read(struct file
*file
, char *buf
, size_t count
, loff_t
*ptr
)
102 static int cfg_ioctl(struct inode
*inode
, struct file
*file
, unsigned int cmd
,
107 /* Only 1 token, so lock... */
108 spin_lock(&i2o_config_lock
);
113 ret
= ioctl_getiops(arg
);
117 ret
= ioctl_gethrt(arg
);
121 ret
= ioctl_getlct(arg
);
125 ret
= ioctl_parms(arg
, I2OPARMSET
);
129 ret
= ioctl_parms(arg
, I2OPARMGET
);
133 ret
= ioctl_swdl(arg
);
137 ret
= ioctl_swul(arg
);
141 ret
= ioctl_swdel(arg
);
145 ret
= ioctl_html(arg
);
152 spin_unlock(&i2o_config_lock
);
156 int ioctl_getiops(unsigned long arg
)
158 u8
*user_iop_table
= (u8
*)arg
;
159 struct i2o_controller
*c
= NULL
;
161 u8 foo
[MAX_I2O_CONTROLLERS
];
163 if(!access_ok(VERIFY_WRITE
, user_iop_table
, MAX_I2O_CONTROLLERS
))
166 for(i
= 0; i
< MAX_I2O_CONTROLLERS
; i
++)
168 c
= i2o_find_controller(i
);
171 printk(KERN_INFO
"ioctl: iop%d found\n", i
);
173 i2o_unlock_controller(c
);
177 printk(KERN_INFO
"ioctl: iop%d not found\n", i
);
182 __copy_to_user(user_iop_table
, foo
, MAX_I2O_CONTROLLERS
);
186 int ioctl_gethrt(unsigned long arg
)
188 struct i2o_controller
*c
;
189 struct i2o_cmd_hrtlct
*cmd
= (struct i2o_cmd_hrtlct
*)arg
;
190 struct i2o_cmd_hrtlct kcmd
;
199 if(copy_from_user(&kcmd
, cmd
, sizeof(struct i2o_cmd_hrtlct
)))
202 if(get_user(reslen
, kcmd
.reslen
) < 0)
205 if(kcmd
.resbuf
== NULL
)
208 c
= i2o_find_controller(kcmd
.iop
);
212 workspace
= kmalloc(8192, GFP_KERNEL
);
213 hrt
= (pi2o_hrt
)workspace
;
216 i2o_unlock_controller(c
);
220 memset(workspace
, 0, 8192);
222 msg
[0]= SIX_WORD_MSG_SIZE
| SGL_OFFSET_4
;
223 msg
[1]= I2O_CMD_HRT_GET
<<24 | HOST_TID
<<12 | ADAPTER_TID
;
224 msg
[2]= (u32
)cfg_handler
.context
;
226 msg
[4]= (0xD0000000 | 8192);
227 msg
[5]= virt_to_phys(workspace
);
229 token
= i2o_post_wait(c
, ADAPTER_TID
, msg
, 6*4, &i2o_cfg_token
,2);
230 if(token
== I2O_POST_WAIT_TIMEOUT
)
233 i2o_unlock_controller(c
);
236 i2o_unlock_controller(c
);
238 len
= 8 + ((hrt
->entry_len
* hrt
->num_entries
) << 2);
239 /* We did a get user...so assuming mem is ok...is this bad? */
240 put_user(len
, kcmd
.reslen
);
243 if(copy_to_user(kcmd
.resbuf
, (void*)hrt
, len
))
250 int ioctl_getlct(unsigned long arg
)
252 struct i2o_controller
*c
;
253 struct i2o_cmd_hrtlct
*cmd
= (struct i2o_cmd_hrtlct
*)arg
;
254 struct i2o_cmd_hrtlct kcmd
;
263 if(copy_from_user(&kcmd
, cmd
, sizeof(struct i2o_cmd_hrtlct
)))
266 if(get_user(reslen
, kcmd
.reslen
) < 0)
269 if(kcmd
.resbuf
== NULL
)
272 c
= i2o_find_controller(kcmd
.iop
);
276 workspace
= kmalloc(8192, GFP_KERNEL
);
277 lct
= (pi2o_lct
)workspace
;
280 i2o_unlock_controller(c
);
284 memset(workspace
, 0, 8192);
286 msg
[0]= EIGHT_WORD_MSG_SIZE
| SGL_OFFSET_6
;
287 msg
[1]= I2O_CMD_LCT_NOTIFY
<<24 | HOST_TID
<<12 | ADAPTER_TID
;
288 msg
[2]= (u32
)cfg_handler
.context
;
292 msg
[6]= (0xD0000000 | 8192);
293 msg
[7]= virt_to_phys(workspace
);
295 token
= i2o_post_wait(c
, ADAPTER_TID
, msg
, 8*4, &i2o_cfg_token
,2);
296 if(token
== I2O_POST_WAIT_TIMEOUT
)
299 i2o_unlock_controller(c
);
302 i2o_unlock_controller(c
);
304 len
= (unsigned int)lct
->table_size
<< 2;
305 put_user(len
, kcmd
.reslen
);
308 else if(copy_to_user(kcmd
.resbuf
, (void*)lct
, len
))
315 static int ioctl_parms(unsigned long arg
, unsigned int type
)
318 struct i2o_controller
*c
;
319 struct i2o_cmd_psetget
*cmd
= (struct i2o_cmd_psetget
*)arg
;
320 struct i2o_cmd_psetget kcmd
;
332 u32 i2o_cmd
= (type
== I2OPARMGET
?
333 I2O_CMD_UTIL_PARAMS_GET
:
334 I2O_CMD_UTIL_PARAMS_SET
);
336 if(copy_from_user(&kcmd
, cmd
, sizeof(struct i2o_cmd_psetget
)))
339 if(get_user(reslen
, kcmd
.reslen
))
342 c
= i2o_find_controller(kcmd
.iop
);
346 ops
= (u8
*)kmalloc(kcmd
.oplen
, GFP_KERNEL
);
349 i2o_unlock_controller(c
);
353 if(copy_from_user(ops
, kcmd
.opbuf
, kcmd
.oplen
))
355 i2o_unlock_controller(c
);
361 * It's possible to have a _very_ large table
362 * and that the user asks for all of it at once...
364 res
= (u8
*)kmalloc(65536, GFP_KERNEL
);
367 i2o_unlock_controller(c
);
374 msg
[0]=NINE_WORD_MSG_SIZE
|SGL_OFFSET_5
;
375 msg
[1]=i2o_cmd
<<24|HOST_TID
<<12|cmd
->tid
;
376 msg
[2]=(u32
)cfg_handler
.context
;
379 msg
[5]=0x54000000|kcmd
.oplen
;
380 msg
[6]=virt_to_bus(ops
);
381 msg
[7]=0xD0000000|(65536);
382 msg
[8]=virt_to_bus(res
);
385 * Parm set sometimes takes a little while for some reason
387 token
= i2o_post_wait(c
, kcmd
.tid
, msg
, 9*4, &i2o_cfg_token
,10);
388 if(token
== I2O_POST_WAIT_TIMEOUT
)
390 i2o_unlock_controller(c
);
395 i2o_unlock_controller(c
);
399 * Determine required size...there's got to be a quicker way?
400 * Dump data to syslog for debugging failures
403 printk(KERN_INFO
"%0#6x\n%0#6x\n", res16
[0], res16
[1]);
406 for(i
= 0; i
< count
; i
++ )
408 len
+= res16
[0] << 2; /* BlockSize field in ResultBlock */
410 for(j
= 0; j
< res16
[0]; j
++)
411 printk(KERN_INFO
"%0#10x\n", res32
[j
]);
412 res16
+= res16
[0] << 1; /* Shift to next block */
415 put_user(len
, kcmd
.reslen
);
418 else if(copy_to_user(cmd
->resbuf
, res
, len
))
426 int ioctl_html(unsigned long arg
)
428 struct i2o_html
*cmd
= (struct i2o_html
*)arg
;
429 struct i2o_html kcmd
;
430 struct i2o_controller
*c
;
437 u32 msg
[MSG_FRAME_SIZE
/4];
439 if(copy_from_user(&kcmd
, cmd
, sizeof(struct i2o_html
)))
441 printk(KERN_INFO
"i2o_config: can't copy html cmd\n");
445 if(get_user(reslen
, kcmd
.reslen
) < 0)
447 printk(KERN_INFO
"i2o_config: can't copy html reslen\n");
453 printk(KERN_INFO
"i2o_config: NULL html buffer\n");
457 c
= i2o_find_controller(kcmd
.iop
);
461 if(kcmd
.qlen
) /* Check for post data */
463 query
= kmalloc(kcmd
.qlen
, GFP_KERNEL
);
466 i2o_unlock_controller(c
);
469 if(copy_from_user(query
, kcmd
.qbuf
, kcmd
.qlen
))
471 i2o_unlock_controller(c
);
472 printk(KERN_INFO
"i2o_config: could not get query\n");
478 res
= kmalloc(4096, GFP_KERNEL
);
481 i2o_unlock_controller(c
);
485 msg
[1] = (I2O_CMD_UTIL_CONFIG_DIALOG
<< 24)|HOST_TID
<<12|kcmd
.tid
;
486 msg
[2] = i2o_cfg_context
;
489 msg
[5] = 0xD0000000|4096;
490 msg
[6] = virt_to_bus(res
);
491 if(!kcmd
.qlen
) /* Check for post data */
492 msg
[0] = SEVEN_WORD_MSG_SIZE
|SGL_OFFSET_5
;
495 msg
[0] = NINE_WORD_MSG_SIZE
|SGL_OFFSET_5
;
496 msg
[5] = 0x50000000|4096;
497 msg
[7] = 0xD4000000|(kcmd
.qlen
);
498 msg
[8] = virt_to_phys(query
);
501 token
= i2o_post_wait(c
, cmd
->tid
, msg
, 9*4, &i2o_cfg_token
, 10);
502 if(token
== I2O_POST_WAIT_TIMEOUT
)
504 i2o_unlock_controller(c
);
506 if(kcmd
.qlen
) kfree(query
);
510 i2o_unlock_controller(c
);
512 len
= strnlen(res
, 8192);
513 put_user(len
, kcmd
.reslen
);
516 if(copy_to_user(kcmd
.resbuf
, res
, len
))
526 int ioctl_swdl(unsigned long arg
)
528 struct i2o_sw_xfer kxfer
;
529 struct i2o_sw_xfer
*pxfer
= (struct i2o_sw_xfer
*)arg
;
530 unsigned char maxfrag
= 0, curfrag
= 0;
531 unsigned char buffer
[8192];
532 u32 msg
[MSG_FRAME_SIZE
/4];
533 unsigned int token
= 0, diff
= 0, swlen
= 0, swxfer
= 0;
534 struct i2o_controller
*c
;
537 printk("*** foo%d ***\n", foo
++);
538 if(copy_from_user(&kxfer
, pxfer
, sizeof(struct i2o_sw_xfer
)))
540 printk( "i2o_config: can't copy i2o_sw cmd @ %p\n", pxfer
);
543 printk("*** foo%d ***\n", foo
++);
545 printk("Attempting to copy swlen from %p\n", kxfer
.swlen
);
546 if(get_user(swlen
, kxfer
.swlen
) < 0)
548 printk( "i2o_config: can't copy swlen\n");
551 printk("*** foo%d ***\n", foo
++);
553 maxfrag
= swlen
>> 13; // Transfer in 8k fragments
555 printk("Attempting to write maxfrag @ %p\n", kxfer
.maxfrag
);
556 if(put_user(maxfrag
, kxfer
.maxfrag
) < 0)
558 printk( "i2o_config: can't write maxfrag\n");
561 printk("*** foo%d ***\n", foo
++);
563 printk("Attempting to write curfrag @ %p\n", kxfer
.curfrag
);
564 if(put_user(curfrag
, kxfer
.curfrag
) < 0)
566 printk( "i2o_config: can't write curfrag\n");
569 printk("*** foo%d ***\n", foo
++);
573 printk( "i2o_config: NULL software buffer\n");
576 printk("*** foo%d ***\n", foo
++);
578 // access_ok doesn't check for NULL...
579 if(!access_ok(VERIFY_READ
, kxfer
.buf
, swlen
))
581 printk( "i2o_config: Cannot read sw buffer\n");
584 printk("*** foo%d ***\n", foo
++);
586 c
= i2o_find_controller(kxfer
.iop
);
589 printk("*** foo%d ***\n", foo
++);
591 msg
[0]= EIGHT_WORD_MSG_SIZE
| SGL_OFFSET_7
;
592 msg
[1]= I2O_CMD_SW_DOWNLOAD
<<24 | HOST_TID
<<12 | ADAPTER_TID
;
593 msg
[2]= (u32
)cfg_handler
.context
;
595 msg
[4]= ((u32
)kxfer
.dl_flags
)<<24|((u32
)kxfer
.sw_type
)<<16|((u32
)maxfrag
)<<8|((u32
)curfrag
);
598 msg
[7]= (0xD0000000 | 8192);
599 msg
[8]= virt_to_phys(buffer
);
601 printk("*** foo%d ***\n", foo
++);
604 // Loop through all fragments but last and transfer them...
605 // We already checked memory, so from now we assume it's all good
607 for(curfrag
= 0; curfrag
< maxfrag
-1; curfrag
++)
609 printk("Transfering fragment %d\n", curfrag
);
611 msg
[4] |= (u32
)curfrag
;
613 __copy_from_user(buffer
, kxfer
.buf
, 8192);
616 // Yes...that's one minute, but the spec states that
617 // transfers take a long time, and I've seen just how
618 // long they can take.
619 token
= i2o_post_wait(c
, ADAPTER_TID
, msg
, 6*4, &i2o_cfg_token
,60);
620 if( token
== I2O_POST_WAIT_TIMEOUT
) // Something very wrong
622 printk("Timeout downloading software");
626 __put_user(curfrag
, kxfer
.curfrag
);
629 // Last frag is special case since it's not exactly 8K
630 diff
= swlen
- swxfer
;
631 msg
[4] |= (u32
)maxfrag
;
632 msg
[7] = (0xD0000000 | diff
);
633 __copy_from_user(buffer
, kxfer
.buf
, 8192);
634 token
= i2o_post_wait(c
, ADAPTER_TID
, msg
, 6*4, &i2o_cfg_token
,60);
635 if( token
== I2O_POST_WAIT_TIMEOUT
) // Something very wrong
637 printk("Timeout downloading software");
640 __put_user(curfrag
, kxfer
.curfrag
);
646 int ioctl_swul(unsigned long arg
)
652 int ioctl_swdel(unsigned long arg
)
657 static int cfg_open(struct inode
*inode
, struct file
*file
)
660 * Should support multiple management users
666 static int cfg_release(struct inode
*inode
, struct file
*file
)
673 static struct file_operations config_fops
=
687 static struct miscdevice i2o_miscdev
= {
694 int init_module(void)
696 __init
int i2o_config_init(void)
699 printk(KERN_INFO
"i2o configuration manager v 0.02\n");
701 if((page_buf
= kmalloc(4096, GFP_KERNEL
))==NULL
)
703 printk(KERN_ERR
"i2o_config: no memory for page buffer.\n");
706 if(misc_register(&i2o_miscdev
)==-1)
708 printk(KERN_ERR
"i2o_config: can't register device.\n");
713 * Install our handler
715 if(i2o_install_handler(&cfg_handler
)<0)
718 printk(KERN_ERR
"i2o_config: handler register failed.\n");
719 misc_deregister(&i2o_miscdev
);
723 * The low 16bits of the transaction context must match this
724 * for everything we post. Otherwise someone else gets our mail
726 i2o_cfg_context
= cfg_handler
.context
;
732 void cleanup_module(void)
734 misc_deregister(&i2o_miscdev
);
738 if(i2o_cfg_context
!= -1)
739 i2o_remove_handler(&cfg_handler
);
745 MODULE_AUTHOR("Red Hat Software");
746 MODULE_DESCRIPTION("I2O Configuration");