Import 2.3.18pre1
[davej-history.git] / drivers / i2o / i2o_config.c
blob17355d9f2a9abf69feee1f7bc4ecf32c29f553e3
1 /*
2 * I2O Configuration Interface Driver
4 * (C) Copyright 1999 Red Hat Software
5 *
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>
27 #include <linux/mm.h>
28 #include <linux/spinlock.h>
30 #include <asm/uaccess.h>
31 #include <asm/io.h>
33 #include "i2o_proc.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;
39 static int i2o_ready;
40 static int i2o_pagelen;
41 static int i2o_error;
42 static int cfg_inuse;
43 static int i2o_eof;
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;
66 return;
70 * Each of these describes an i2o message handler. They are
71 * multiplexed by the i2o_core code
74 struct i2o_handler cfg_handler=
76 i2o_cfg_reply,
77 "Configuration",
79 0xffffffff // All classes
82 static long long cfg_llseek(struct file *file, long long offset, int origin)
84 return -ESPIPE;
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");
93 return 0;
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)
99 return 0;
102 static int cfg_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
103 unsigned long arg)
105 int ret;
107 /* Only 1 token, so lock... */
108 spin_lock(&i2o_config_lock);
110 switch(cmd)
112 case I2OGETIOPS:
113 ret = ioctl_getiops(arg);
114 break;
116 case I2OHRTGET:
117 ret = ioctl_gethrt(arg);
118 break;
120 case I2OLCTGET:
121 ret = ioctl_getlct(arg);
122 break;
124 case I2OPARMSET:
125 ret = ioctl_parms(arg, I2OPARMSET);
126 break;
128 case I2OPARMGET:
129 ret = ioctl_parms(arg, I2OPARMGET);
130 break;
132 case I2OSWDL:
133 ret = ioctl_swdl(arg);
134 break;
136 case I2OSWUL:
137 ret = ioctl_swul(arg);
138 break;
140 case I2OSWDEL:
141 ret = ioctl_swdel(arg);
142 break;
144 case I2OHTML:
145 ret = ioctl_html(arg);
146 break;
148 default:
149 ret = -EINVAL;
152 spin_unlock(&i2o_config_lock);
153 return ret;
156 int ioctl_getiops(unsigned long arg)
158 u8 *user_iop_table = (u8*)arg;
159 struct i2o_controller *c = NULL;
160 int i;
161 u8 foo[MAX_I2O_CONTROLLERS];
163 if(!access_ok(VERIFY_WRITE, user_iop_table, MAX_I2O_CONTROLLERS))
164 return -EFAULT;
166 for(i = 0; i < MAX_I2O_CONTROLLERS; i++)
168 c = i2o_find_controller(i);
169 if(c)
171 printk(KERN_INFO "ioctl: iop%d found\n", i);
172 foo[i] = 1;
173 i2o_unlock_controller(c);
175 else
177 printk(KERN_INFO "ioctl: iop%d not found\n", i);
178 foo[i] = 0;
182 __copy_to_user(user_iop_table, foo, MAX_I2O_CONTROLLERS);
183 return 0;
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;
191 pi2o_hrt hrt;
192 u32 msg[6];
193 u32 *workspace;
194 int len;
195 int token;
196 u32 reslen;
197 int ret = 0;
199 if(copy_from_user(&kcmd, cmd, sizeof(struct i2o_cmd_hrtlct)))
200 return -EFAULT;
202 if(get_user(reslen, kcmd.reslen) < 0)
203 return -EFAULT;
205 if(kcmd.resbuf == NULL)
206 return -EFAULT;
208 c = i2o_find_controller(kcmd.iop);
209 if(!c)
210 return -ENXIO;
212 workspace = kmalloc(8192, GFP_KERNEL);
213 hrt = (pi2o_hrt)workspace;
214 if(workspace==NULL)
216 i2o_unlock_controller(c);
217 return -ENOMEM;
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;
225 msg[3]= 0;
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)
232 kfree(workspace);
233 i2o_unlock_controller(c);
234 return -ETIMEDOUT;
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);
241 if(len > reslen)
242 ret = -ENOBUFS;
243 if(copy_to_user(kcmd.resbuf, (void*)hrt, len))
244 ret = -EINVAL;
246 kfree(workspace);
247 return ret;
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;
255 pi2o_lct lct;
256 u32 msg[9];
257 u32 *workspace;
258 int len;
259 int token;
260 int ret = 0;
261 u32 reslen;
263 if(copy_from_user(&kcmd, cmd, sizeof(struct i2o_cmd_hrtlct)))
264 return -EFAULT;
266 if(get_user(reslen, kcmd.reslen) < 0)
267 return -EFAULT;
269 if(kcmd.resbuf == NULL)
270 return -EFAULT;
272 c = i2o_find_controller(kcmd.iop);
273 if(!c)
274 return -ENXIO;
276 workspace = kmalloc(8192, GFP_KERNEL);
277 lct = (pi2o_lct)workspace;
278 if(workspace==NULL)
280 i2o_unlock_controller(c);
281 return -ENOMEM;
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;
289 msg[3]= 0;
290 msg[4]= 0xFFFFFFFF;
291 msg[5]= 0;
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)
298 kfree(workspace);
299 i2o_unlock_controller(c);
300 return -ETIMEDOUT;
302 i2o_unlock_controller(c);
304 len = (unsigned int)lct->table_size << 2;
305 put_user(len, kcmd.reslen);
306 if(len > reslen)
307 ret = -ENOBUFS;
308 else if(copy_to_user(kcmd.resbuf, (void*)lct, len))
309 ret = -EINVAL;
311 kfree(workspace);
312 return ret;
315 static int ioctl_parms(unsigned long arg, unsigned int type)
317 int ret = 0;
318 struct i2o_controller *c;
319 struct i2o_cmd_psetget *cmd = (struct i2o_cmd_psetget*)arg;
320 struct i2o_cmd_psetget kcmd;
321 u32 msg[9];
322 u32 reslen;
323 int token;
324 u8 *ops;
325 u8 *res;
326 u16 *res16;
327 u32 *res32;
328 u16 count;
329 int len;
330 int i,j;
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)))
337 return -EFAULT;
339 if(get_user(reslen, kcmd.reslen))
340 return -EFAULT;
342 c = i2o_find_controller(kcmd.iop);
343 if(!c)
344 return -ENXIO;
346 ops = (u8*)kmalloc(kcmd.oplen, GFP_KERNEL);
347 if(!ops)
349 i2o_unlock_controller(c);
350 return -ENOMEM;
353 if(copy_from_user(ops, kcmd.opbuf, kcmd.oplen))
355 i2o_unlock_controller(c);
356 kfree(ops);
357 return -EFAULT;
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);
365 if(!res)
367 i2o_unlock_controller(c);
368 kfree(ops);
369 return -ENOMEM;
372 res16 = (u16*)res;
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;
377 msg[3]=0;
378 msg[4]=0;
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);
391 kfree(ops);
392 kfree(res);
393 return -ETIMEDOUT;
395 i2o_unlock_controller(c);
396 kfree(ops);
399 * Determine required size...there's got to be a quicker way?
400 * Dump data to syslog for debugging failures
402 count = res16[0];
403 printk(KERN_INFO "%0#6x\n%0#6x\n", res16[0], res16[1]);
404 len = 4;
405 res16 += 2;
406 for(i = 0; i < count; i++ )
408 len += res16[0] << 2; /* BlockSize field in ResultBlock */
409 res32 = (u32*)res16;
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);
416 if(len > reslen)
417 ret = -ENOBUFS;
418 else if(copy_to_user(cmd->resbuf, res, len))
419 ret = -EFAULT;
421 kfree(res);
423 return ret;
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;
431 u8 *res = NULL;
432 void *query = NULL;
433 int ret = 0;
434 int token;
435 u32 len;
436 u32 reslen;
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");
442 return -EFAULT;
445 if(get_user(reslen, kcmd.reslen) < 0)
447 printk(KERN_INFO "i2o_config: can't copy html reslen\n");
448 return -EFAULT;
451 if(!kcmd.resbuf)
453 printk(KERN_INFO "i2o_config: NULL html buffer\n");
454 return -EFAULT;
457 c = i2o_find_controller(kcmd.iop);
458 if(!c)
459 return -ENXIO;
461 if(kcmd.qlen) /* Check for post data */
463 query = kmalloc(kcmd.qlen, GFP_KERNEL);
464 if(!query)
466 i2o_unlock_controller(c);
467 return -ENOMEM;
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");
473 kfree(query);
474 return -EFAULT;
478 res = kmalloc(4096, GFP_KERNEL);
479 if(!res)
481 i2o_unlock_controller(c);
482 return -ENOMEM;
485 msg[1] = (I2O_CMD_UTIL_CONFIG_DIALOG << 24)|HOST_TID<<12|kcmd.tid;
486 msg[2] = i2o_cfg_context;
487 msg[3] = 0;
488 msg[4] = kcmd.page;
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;
493 else
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);
505 kfree(res);
506 if(kcmd.qlen) kfree(query);
508 return -ETIMEDOUT;
510 i2o_unlock_controller(c);
512 len = strnlen(res, 8192);
513 put_user(len, kcmd.reslen);
514 if(len > reslen)
515 ret = -ENOMEM;
516 if(copy_to_user(kcmd.resbuf, res, len))
517 ret = -EFAULT;
519 kfree(res);
520 if(kcmd.qlen)
521 kfree(query);
523 return ret;
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;
535 int foo = 0;
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);
541 return -EFAULT;
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");
549 return -EFAULT;
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");
559 return -EFAULT;
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");
567 return -EFAULT;
569 printk("*** foo%d ***\n", foo++);
571 if(!kxfer.buf)
573 printk( "i2o_config: NULL software buffer\n");
574 return -EFAULT;
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");
582 return -EFAULT;
584 printk("*** foo%d ***\n", foo++);
586 c = i2o_find_controller(kxfer.iop);
587 if(!c)
588 return -ENXIO;
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;
594 msg[3]= 0;
595 msg[4]= ((u32)kxfer.dl_flags)<<24|((u32)kxfer.sw_type)<<16|((u32)maxfrag)<<8|((u32)curfrag);
596 msg[5]= swlen;
597 msg[6]= kxfer.sw_id;
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);
614 swxfer += 8129;
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");
623 return -ETIMEDOUT;
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");
638 return -ETIMEDOUT;
640 __put_user(curfrag, kxfer.curfrag);
642 return 0;
645 /* To be written */
646 int ioctl_swul(unsigned long arg)
648 return -EINVAL;
651 /* To be written */
652 int ioctl_swdel(unsigned long arg)
654 return 0;
657 static int cfg_open(struct inode *inode, struct file *file)
660 * Should support multiple management users
662 MOD_INC_USE_COUNT;
663 return 0;
666 static int cfg_release(struct inode *inode, struct file *file)
668 MOD_DEC_USE_COUNT;
669 return 0;
673 static struct file_operations config_fops =
675 cfg_llseek,
676 cfg_read,
677 cfg_write,
678 NULL,
679 NULL /*cfg_poll*/,
680 cfg_ioctl,
681 NULL, /* No mmap */
682 cfg_open,
683 NULL, /* No flush */
684 cfg_release
687 static struct miscdevice i2o_miscdev = {
688 I2O_MINOR,
689 "i2octl",
690 &config_fops
693 #ifdef MODULE
694 int init_module(void)
695 #else
696 __init int i2o_config_init(void)
697 #endif
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");
704 return -ENOBUFS;
706 if(misc_register(&i2o_miscdev)==-1)
708 printk(KERN_ERR "i2o_config: can't register device.\n");
709 kfree(page_buf);
710 return -EBUSY;
713 * Install our handler
715 if(i2o_install_handler(&cfg_handler)<0)
717 kfree(page_buf);
718 printk(KERN_ERR "i2o_config: handler register failed.\n");
719 misc_deregister(&i2o_miscdev);
720 return -EBUSY;
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;
727 return 0;
730 #ifdef MODULE
732 void cleanup_module(void)
734 misc_deregister(&i2o_miscdev);
736 if(page_buf)
737 kfree(page_buf);
738 if(i2o_cfg_context != -1)
739 i2o_remove_handler(&cfg_handler);
740 if(i2o_buffer)
741 kfree(i2o_buffer);
744 EXPORT_NO_SYMBOLS;
745 MODULE_AUTHOR("Red Hat Software");
746 MODULE_DESCRIPTION("I2O Configuration");
748 #endif