2 * I2O Configuration Interface Driver
4 * (C) Copyright 1999-2002 Red Hat
6 * Written by Alan Cox, Building Number Three Ltd
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):
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
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.
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>
42 #include <linux/spinlock.h>
43 #include <linux/smp_lock.h>
44 #include <linux/ioctl32.h>
45 #include <linux/compat.h>
46 #include <linux/syscalls.h>
48 #include <asm/uaccess.h>
51 #define OSM_NAME "config-osm"
52 #define OSM_VERSION "$Rev$"
53 #define OSM_DESCRIPTION "I2O Configuration OSM"
55 extern int i2o_parm_issue(struct i2o_device *, int, void *, int, void *, int);
57 static int i2o_cfg_ioctl(struct inode *inode, struct file *fp, unsigned int cmd,
60 static spinlock_t i2o_config_lock;
62 #define MODINC(x,y) ((x) = ((x) + 1) % (y))
64 struct sg_simple_element {
71 struct fasync_struct *fasync;
72 struct i2o_evt_info event_q[I2O_EVT_Q_LEN];
73 u16 q_in; // Queue head index
74 u16 q_out; // Queue tail index
75 u16 q_len; // Queue length
76 u16 q_lost; // Number of lost events
77 ulong q_id; // Event queue ID...used as tx_context
78 struct i2o_cfg_info *next;
80 static struct i2o_cfg_info *open_files = NULL;
81 static ulong i2o_cfg_info_id = 0;
84 * i2o_config_read_hrt - Returns the HRT of the controller
85 * @kob: kernel object handle
86 * @buf: buffer into which the HRT should be copied
88 * @count: number of bytes to read
90 * Put @count bytes starting at @off into @buf from the HRT of the I2O
91 * controller corresponding to @kobj.
93 * Returns number of bytes copied into buffer.
95 static ssize_t i2o_config_read_hrt(struct kobject *kobj, char *buf,
96 loff_t offset, size_t count)
98 struct i2o_controller *c = to_i2o_controller(container_of(kobj,
101 i2o_hrt *hrt = c->hrt.virt;
103 u32 size = (hrt->num_entries * hrt->entry_len + 2) * 4;
108 if(offset + count > size)
109 count = size - offset;
111 memcpy(buf, (u8 *) hrt + offset, count);
117 * i2o_config_read_lct - Returns the LCT of the controller
118 * @kob: kernel object handle
119 * @buf: buffer into which the LCT should be copied
121 * @count: number of bytes to read
123 * Put @count bytes starting at @off into @buf from the LCT of the I2O
124 * controller corresponding to @kobj.
126 * Returns number of bytes copied into buffer.
128 static ssize_t i2o_config_read_lct(struct kobject *kobj, char *buf,
129 loff_t offset, size_t count)
131 struct i2o_controller *c = to_i2o_controller(container_of(kobj,
134 u32 size = c->lct->table_size * 4;
139 if(offset + count > size)
140 count = size - offset;
142 memcpy(buf, (u8 *) c->lct + offset, count);
147 /* attribute for HRT in sysfs */
148 static struct bin_attribute i2o_config_hrt_attr = {
155 .read = i2o_config_read_hrt
158 /* attribute for LCT in sysfs */
159 static struct bin_attribute i2o_config_lct_attr = {
166 .read = i2o_config_read_lct
170 * i2o_config_notify_controller_add - Notify of added controller
171 * @c: the controller which was added
173 * If a I2O controller is added, we catch the notification to add sysfs
176 static void i2o_config_notify_controller_add(struct i2o_controller *c)
178 sysfs_create_bin_file(&(c->device.kobj), &i2o_config_hrt_attr);
179 sysfs_create_bin_file(&(c->device.kobj), &i2o_config_lct_attr);
183 * i2o_config_notify_controller_remove - Notify of removed controller
184 * @c: the controller which was removed
186 * If a I2O controller is removed, we catch the notification to remove the
189 static void i2o_config_notify_controller_remove(struct i2o_controller *c)
191 sysfs_remove_bin_file(&c->device.kobj, &i2o_config_lct_attr);
192 sysfs_remove_bin_file(&c->device.kobj, &i2o_config_hrt_attr);
195 /* Config OSM driver struct */
196 static struct i2o_driver i2o_config_driver = {
198 .notify_controller_add = i2o_config_notify_controller_add,
199 .notify_controller_remove = i2o_config_notify_controller_remove
202 static int i2o_cfg_getiops(unsigned long arg)
204 struct i2o_controller *c;
205 u8 __user *user_iop_table = (void __user *)arg;
206 u8 tmp[MAX_I2O_CONTROLLERS];
209 memset(tmp, 0, MAX_I2O_CONTROLLERS);
211 list_for_each_entry(c, &i2o_controllers, list)
214 if (copy_to_user(user_iop_table, tmp, MAX_I2O_CONTROLLERS))
220 static int i2o_cfg_gethrt(unsigned long arg)
222 struct i2o_controller *c;
223 struct i2o_cmd_hrtlct __user *cmd = (struct i2o_cmd_hrtlct __user *)arg;
224 struct i2o_cmd_hrtlct kcmd;
230 if (copy_from_user(&kcmd, cmd, sizeof(struct i2o_cmd_hrtlct)))
233 if (get_user(reslen, kcmd.reslen) < 0)
236 if (kcmd.resbuf == NULL)
239 c = i2o_find_iop(kcmd.iop);
243 hrt = (i2o_hrt *) c->hrt.virt;
245 len = 8 + ((hrt->entry_len * hrt->num_entries) << 2);
247 /* We did a get user...so assuming mem is ok...is this bad? */
248 put_user(len, kcmd.reslen);
251 if (copy_to_user(kcmd.resbuf, (void *)hrt, len))
257 static int i2o_cfg_getlct(unsigned long arg)
259 struct i2o_controller *c;
260 struct i2o_cmd_hrtlct __user *cmd = (struct i2o_cmd_hrtlct __user *)arg;
261 struct i2o_cmd_hrtlct kcmd;
267 if (copy_from_user(&kcmd, cmd, sizeof(struct i2o_cmd_hrtlct)))
270 if (get_user(reslen, kcmd.reslen) < 0)
273 if (kcmd.resbuf == NULL)
276 c = i2o_find_iop(kcmd.iop);
280 lct = (i2o_lct *) c->lct;
282 len = (unsigned int)lct->table_size << 2;
283 put_user(len, kcmd.reslen);
286 else if (copy_to_user(kcmd.resbuf, lct, len))
292 static int i2o_cfg_parms(unsigned long arg, unsigned int type)
295 struct i2o_controller *c;
296 struct i2o_device *dev;
297 struct i2o_cmd_psetget __user *cmd =
298 (struct i2o_cmd_psetget __user *)arg;
299 struct i2o_cmd_psetget kcmd;
305 u32 i2o_cmd = (type == I2OPARMGET ?
306 I2O_CMD_UTIL_PARAMS_GET : I2O_CMD_UTIL_PARAMS_SET);
308 if (copy_from_user(&kcmd, cmd, sizeof(struct i2o_cmd_psetget)))
311 if (get_user(reslen, kcmd.reslen))
314 c = i2o_find_iop(kcmd.iop);
318 dev = i2o_iop_find_device(c, kcmd.tid);
322 ops = (u8 *) kmalloc(kcmd.oplen, GFP_KERNEL);
326 if (copy_from_user(ops, kcmd.opbuf, kcmd.oplen)) {
332 * It's possible to have a _very_ large table
333 * and that the user asks for all of it at once...
335 res = (u8 *) kmalloc(65536, GFP_KERNEL);
341 len = i2o_parm_issue(dev, i2o_cmd, ops, kcmd.oplen, res, 65536);
349 put_user(len, kcmd.reslen);
352 else if (copy_to_user(kcmd.resbuf, res, len))
360 static int i2o_cfg_swdl(unsigned long arg)
362 struct i2o_sw_xfer kxfer;
363 struct i2o_sw_xfer __user *pxfer = (struct i2o_sw_xfer __user *)arg;
364 unsigned char maxfrag = 0, curfrag = 1;
365 struct i2o_dma buffer;
366 struct i2o_message __iomem *msg;
368 unsigned int status = 0, swlen = 0, fragsize = 8192;
369 struct i2o_controller *c;
371 if (copy_from_user(&kxfer, pxfer, sizeof(struct i2o_sw_xfer)))
374 if (get_user(swlen, kxfer.swlen) < 0)
377 if (get_user(maxfrag, kxfer.maxfrag) < 0)
380 if (get_user(curfrag, kxfer.curfrag) < 0)
383 if (curfrag == maxfrag)
384 fragsize = swlen - (maxfrag - 1) * 8192;
386 if (!kxfer.buf || !access_ok(VERIFY_READ, kxfer.buf, fragsize))
389 c = i2o_find_iop(kxfer.iop);
393 m = i2o_msg_get_wait(c, &msg, I2O_TIMEOUT_MESSAGE_GET);
394 if (m == I2O_QUEUE_EMPTY)
397 if (i2o_dma_alloc(&c->pdev->dev, &buffer, fragsize, GFP_KERNEL)) {
402 __copy_from_user(buffer.virt, kxfer.buf, fragsize);
404 writel(NINE_WORD_MSG_SIZE | SGL_OFFSET_7, &msg->u.head[0]);
405 writel(I2O_CMD_SW_DOWNLOAD << 24 | HOST_TID << 12 | ADAPTER_TID,
407 writel(i2o_config_driver.context, &msg->u.head[2]);
408 writel(0, &msg->u.head[3]);
409 writel((((u32) kxfer.flags) << 24) | (((u32) kxfer.sw_type) << 16) |
410 (((u32) maxfrag) << 8) | (((u32) curfrag)), &msg->body[0]);
411 writel(swlen, &msg->body[1]);
412 writel(kxfer.sw_id, &msg->body[2]);
413 writel(0xD0000000 | fragsize, &msg->body[3]);
414 writel(buffer.phys, &msg->body[4]);
416 osm_debug("swdl frag %d/%d (size %d)\n", curfrag, maxfrag, fragsize);
417 status = i2o_msg_post_wait_mem(c, m, 60, &buffer);
419 if (status != -ETIMEDOUT)
420 i2o_dma_free(&c->pdev->dev, &buffer);
422 if (status != I2O_POST_WAIT_OK) {
423 // it fails if you try and send frags out of order
424 // and for some yet unknown reasons too
425 osm_info("swdl failed, DetailedStatus = %d\n", status);
432 static int i2o_cfg_swul(unsigned long arg)
434 struct i2o_sw_xfer kxfer;
435 struct i2o_sw_xfer __user *pxfer = (struct i2o_sw_xfer __user *)arg;
436 unsigned char maxfrag = 0, curfrag = 1;
437 struct i2o_dma buffer;
438 struct i2o_message __iomem *msg;
440 unsigned int status = 0, swlen = 0, fragsize = 8192;
441 struct i2o_controller *c;
444 if (copy_from_user(&kxfer, pxfer, sizeof(struct i2o_sw_xfer)))
447 if (get_user(swlen, kxfer.swlen) < 0)
450 if (get_user(maxfrag, kxfer.maxfrag) < 0)
453 if (get_user(curfrag, kxfer.curfrag) < 0)
456 if (curfrag == maxfrag)
457 fragsize = swlen - (maxfrag - 1) * 8192;
462 c = i2o_find_iop(kxfer.iop);
466 m = i2o_msg_get_wait(c, &msg, I2O_TIMEOUT_MESSAGE_GET);
467 if (m == I2O_QUEUE_EMPTY)
470 if (i2o_dma_alloc(&c->pdev->dev, &buffer, fragsize, GFP_KERNEL)) {
475 writel(NINE_WORD_MSG_SIZE | SGL_OFFSET_7, &msg->u.head[0]);
476 writel(I2O_CMD_SW_UPLOAD << 24 | HOST_TID << 12 | ADAPTER_TID,
478 writel(i2o_config_driver.context, &msg->u.head[2]);
479 writel(0, &msg->u.head[3]);
480 writel((u32) kxfer.flags << 24 | (u32) kxfer.
481 sw_type << 16 | (u32) maxfrag << 8 | (u32) curfrag,
483 writel(swlen, &msg->body[1]);
484 writel(kxfer.sw_id, &msg->body[2]);
485 writel(0xD0000000 | fragsize, &msg->body[3]);
486 writel(buffer.phys, &msg->body[4]);
488 osm_debug("swul frag %d/%d (size %d)\n", curfrag, maxfrag, fragsize);
489 status = i2o_msg_post_wait_mem(c, m, 60, &buffer);
491 if (status != I2O_POST_WAIT_OK) {
492 if (status != -ETIMEDOUT)
493 i2o_dma_free(&c->pdev->dev, &buffer);
495 osm_info("swul failed, DetailedStatus = %d\n", status);
499 if (copy_to_user(kxfer.buf, buffer.virt, fragsize))
502 i2o_dma_free(&c->pdev->dev, &buffer);
511 static int i2o_cfg_swdel(unsigned long arg)
513 struct i2o_controller *c;
514 struct i2o_sw_xfer kxfer;
515 struct i2o_sw_xfer __user *pxfer = (struct i2o_sw_xfer __user *)arg;
516 struct i2o_message __iomem *msg;
521 if (copy_from_user(&kxfer, pxfer, sizeof(struct i2o_sw_xfer)))
524 if (get_user(swlen, kxfer.swlen) < 0)
527 c = i2o_find_iop(kxfer.iop);
531 m = i2o_msg_get_wait(c, &msg, I2O_TIMEOUT_MESSAGE_GET);
532 if (m == I2O_QUEUE_EMPTY)
535 writel(SEVEN_WORD_MSG_SIZE | SGL_OFFSET_0, &msg->u.head[0]);
536 writel(I2O_CMD_SW_REMOVE << 24 | HOST_TID << 12 | ADAPTER_TID,
538 writel(i2o_config_driver.context, &msg->u.head[2]);
539 writel(0, &msg->u.head[3]);
540 writel((u32) kxfer.flags << 24 | (u32) kxfer.sw_type << 16,
542 writel(swlen, &msg->body[1]);
543 writel(kxfer.sw_id, &msg->body[2]);
545 token = i2o_msg_post_wait(c, m, 10);
547 if (token != I2O_POST_WAIT_OK) {
548 osm_info("swdel failed, DetailedStatus = %d\n", token);
555 static int i2o_cfg_validate(unsigned long arg)
559 struct i2o_message __iomem *msg;
561 struct i2o_controller *c;
563 c = i2o_find_iop(iop);
567 m = i2o_msg_get_wait(c, &msg, I2O_TIMEOUT_MESSAGE_GET);
568 if (m == I2O_QUEUE_EMPTY)
571 writel(FOUR_WORD_MSG_SIZE | SGL_OFFSET_0, &msg->u.head[0]);
572 writel(I2O_CMD_CONFIG_VALIDATE << 24 | HOST_TID << 12 | iop,
574 writel(i2o_config_driver.context, &msg->u.head[2]);
575 writel(0, &msg->u.head[3]);
577 token = i2o_msg_post_wait(c, m, 10);
579 if (token != I2O_POST_WAIT_OK) {
580 osm_info("Can't validate configuration, ErrorStatus = %d\n",
588 static int i2o_cfg_evt_reg(unsigned long arg, struct file *fp)
590 struct i2o_message __iomem *msg;
592 struct i2o_evt_id __user *pdesc = (struct i2o_evt_id __user *)arg;
593 struct i2o_evt_id kdesc;
594 struct i2o_controller *c;
595 struct i2o_device *d;
597 if (copy_from_user(&kdesc, pdesc, sizeof(struct i2o_evt_id)))
601 c = i2o_find_iop(kdesc.iop);
606 d = i2o_iop_find_device(c, kdesc.tid);
610 m = i2o_msg_get_wait(c, &msg, I2O_TIMEOUT_MESSAGE_GET);
611 if (m == I2O_QUEUE_EMPTY)
614 writel(FOUR_WORD_MSG_SIZE | SGL_OFFSET_0, &msg->u.head[0]);
615 writel(I2O_CMD_UTIL_EVT_REGISTER << 24 | HOST_TID << 12 | kdesc.tid,
617 writel(i2o_config_driver.context, &msg->u.head[2]);
618 writel(i2o_cntxt_list_add(c, fp->private_data), &msg->u.head[3]);
619 writel(kdesc.evt_mask, &msg->body[0]);
626 static int i2o_cfg_evt_get(unsigned long arg, struct file *fp)
628 struct i2o_cfg_info *p = NULL;
629 struct i2o_evt_get __user *uget = (struct i2o_evt_get __user *)arg;
630 struct i2o_evt_get kget;
633 for (p = open_files; p; p = p->next)
634 if (p->q_id == (ulong) fp->private_data)
640 memcpy(&kget.info, &p->event_q[p->q_out], sizeof(struct i2o_evt_info));
641 MODINC(p->q_out, I2O_EVT_Q_LEN);
642 spin_lock_irqsave(&i2o_config_lock, flags);
644 kget.pending = p->q_len;
645 kget.lost = p->q_lost;
646 spin_unlock_irqrestore(&i2o_config_lock, flags);
648 if (copy_to_user(uget, &kget, sizeof(struct i2o_evt_get)))
654 static int i2o_cfg_passthru32(struct file *file, unsigned cmnd, unsigned long arg)
656 struct i2o_cmd_passthru32 __user *cmd;
657 struct i2o_controller *c;
658 u32 __user *user_msg;
660 u32 __user *user_reply = NULL;
664 struct i2o_dma sg_list[SG_TABLESIZE];
669 i2o_status_block *sb;
670 struct i2o_message *msg;
674 cmd = (struct i2o_cmd_passthru32 __user *)arg;
676 if (get_user(iop, &cmd->iop) || get_user(i, &cmd->msg))
679 user_msg = compat_ptr(i);
681 c = i2o_find_iop(iop);
683 osm_debug("controller %d not found\n", iop);
687 m = i2o_msg_get_wait(c, &msg, I2O_TIMEOUT_MESSAGE_GET);
689 sb = c->status_block.virt;
691 if (get_user(size, &user_msg[0])) {
692 osm_warn("unable to get size!\n");
697 if (size > sb->inbound_frame_size) {
698 osm_warn("size of message > inbound_frame_size");
702 user_reply = &user_msg[size];
704 size <<= 2; // Convert to bytes
706 /* Copy in the user's I2O command */
707 if (copy_from_user(msg, user_msg, size)) {
708 osm_warn("unable to copy user message\n");
711 i2o_dump_message(msg);
713 if (get_user(reply_size, &user_reply[0]) < 0)
719 reply = kmalloc(reply_size, GFP_KERNEL);
721 printk(KERN_WARNING "%s: Could not allocate reply buffer\n",
725 memset(reply, 0, reply_size);
727 sg_offset = (msg->u.head[0] >> 4) & 0x0f;
729 writel(i2o_config_driver.context, &msg->u.s.icntxt);
730 writel(i2o_cntxt_list_add(c, reply), &msg->u.s.tcntxt);
732 memset(sg_list, 0, sizeof(sg_list[0]) * SG_TABLESIZE);
734 struct sg_simple_element *sg;
736 if (sg_offset * 4 >= size) {
741 sg = (struct sg_simple_element *)((&msg->u.head[0]) +
744 (size - sg_offset * 4) / sizeof(struct sg_simple_element);
745 if (sg_count > SG_TABLESIZE) {
746 printk(KERN_DEBUG "%s:IOCTL SG List too large (%u)\n",
752 for (i = 0; i < sg_count; i++) {
756 if (!(sg[i].flag_count & 0x10000000
757 /*I2O_SGL_FLAGS_SIMPLE_ADDRESS_ELEMENT */ )) {
759 "%s:Bad SG element %d - not simple (%x)\n",
760 c->name, i, sg[i].flag_count);
764 sg_size = sg[i].flag_count & 0xffffff;
765 p = &(sg_list[sg_index++]);
766 /* Allocate memory for the transfer */
768 (&c->pdev->dev, p, sg_size,
769 PCI_DMA_BIDIRECTIONAL)) {
771 "%s: Could not allocate SG buffer - size = %d buffer number %d of %d\n",
772 c->name, sg_size, i, sg_count);
774 goto sg_list_cleanup;
776 /* Copy in the user's SG buffer if necessary */
778 flag_count & 0x04000000 /*I2O_SGL_FLAGS_DIR */ ) {
781 (p->virt, (void __user *)(unsigned long)sg[i].addr_bus,
784 "%s: Could not copy SG buf %d FROM user\n",
787 goto sg_list_cleanup;
791 sg[i].addr_bus = (u32) p->phys;
795 rcode = i2o_msg_post_wait(c, m, 60);
797 goto sg_list_cleanup;
800 u32 msg[MSG_FRAME_SIZE];
801 /* Copy back the Scatter Gather buffers back to user space */
804 struct sg_simple_element *sg;
807 // re-acquire the original message to handle correctly the sg copy operation
808 memset(&msg, 0, MSG_FRAME_SIZE * 4);
809 // get user msg size in u32s
810 if (get_user(size, &user_msg[0])) {
812 goto sg_list_cleanup;
816 /* Copy in the user's I2O command */
817 if (copy_from_user(msg, user_msg, size)) {
819 goto sg_list_cleanup;
822 (size - sg_offset * 4) / sizeof(struct sg_simple_element);
825 sg = (struct sg_simple_element *)(msg + sg_offset);
826 for (j = 0; j < sg_count; j++) {
827 /* Copy out the SG list to user's buffer if necessary */
830 flag_count & 0x4000000 /*I2O_SGL_FLAGS_DIR */ )) {
831 sg_size = sg[j].flag_count & 0xffffff;
834 ((void __user *)(u64) sg[j].addr_bus,
835 sg_list[j].virt, sg_size)) {
837 "%s: Could not copy %p TO user %x\n",
838 c->name, sg_list[j].virt,
841 goto sg_list_cleanup;
847 /* Copy back the reply to user space */
849 // we wrote our own values for context - now restore the user supplied ones
850 if (copy_from_user(reply + 2, user_msg + 2, sizeof(u32) * 2)) {
852 "%s: Could not copy message context FROM user\n",
855 goto sg_list_cleanup;
857 if (copy_to_user(user_reply, reply, reply_size)) {
859 "%s: Could not copy reply TO user\n", c->name);
865 for (i = 0; i < sg_index; i++)
866 i2o_dma_free(&c->pdev->dev, &sg_list[i]);
873 static long i2o_cfg_compat_ioctl(struct file *file, unsigned cmd, unsigned long arg)
879 ret = i2o_cfg_ioctl(NULL, file, cmd, arg);
882 ret = i2o_cfg_passthru32(file, cmd, arg);
894 static int i2o_cfg_passthru(unsigned long arg)
896 struct i2o_cmd_passthru __user *cmd =
897 (struct i2o_cmd_passthru __user *)arg;
898 struct i2o_controller *c;
899 u32 __user *user_msg;
901 u32 __user *user_reply = NULL;
905 void *sg_list[SG_TABLESIZE];
911 i2o_status_block *sb;
912 struct i2o_message __iomem *msg;
916 if (get_user(iop, &cmd->iop) || get_user(user_msg, &cmd->msg))
919 c = i2o_find_iop(iop);
921 osm_warn("controller %d not found\n", iop);
925 m = i2o_msg_get_wait(c, &msg, I2O_TIMEOUT_MESSAGE_GET);
927 sb = c->status_block.virt;
929 if (get_user(size, &user_msg[0]))
933 if (size > sb->inbound_frame_size) {
934 osm_warn("size of message > inbound_frame_size");
938 user_reply = &user_msg[size];
940 size <<= 2; // Convert to bytes
942 /* Copy in the user's I2O command */
943 if (copy_from_user(msg, user_msg, size))
946 if (get_user(reply_size, &user_reply[0]) < 0)
952 reply = kmalloc(reply_size, GFP_KERNEL);
954 printk(KERN_WARNING "%s: Could not allocate reply buffer\n",
958 memset(reply, 0, reply_size);
960 sg_offset = (msg->u.head[0] >> 4) & 0x0f;
962 writel(i2o_config_driver.context, &msg->u.s.icntxt);
963 writel(i2o_cntxt_list_add(c, reply), &msg->u.s.tcntxt);
965 memset(sg_list, 0, sizeof(sg_list[0]) * SG_TABLESIZE);
967 struct sg_simple_element *sg;
969 if (sg_offset * 4 >= size) {
974 sg = (struct sg_simple_element *)((&msg->u.head[0]) +
977 (size - sg_offset * 4) / sizeof(struct sg_simple_element);
978 if (sg_count > SG_TABLESIZE) {
979 printk(KERN_DEBUG "%s:IOCTL SG List too large (%u)\n",
985 for (i = 0; i < sg_count; i++) {
988 if (!(sg[i].flag_count & 0x10000000
989 /*I2O_SGL_FLAGS_SIMPLE_ADDRESS_ELEMENT */ )) {
991 "%s:Bad SG element %d - not simple (%x)\n",
992 c->name, i, sg[i].flag_count);
994 goto sg_list_cleanup;
996 sg_size = sg[i].flag_count & 0xffffff;
997 /* Allocate memory for the transfer */
998 p = kmalloc(sg_size, GFP_KERNEL);
1001 "%s: Could not allocate SG buffer - size = %d buffer number %d of %d\n",
1002 c->name, sg_size, i, sg_count);
1004 goto sg_list_cleanup;
1006 sg_list[sg_index++] = p; // sglist indexed with input frame, not our internal frame.
1007 /* Copy in the user's SG buffer if necessary */
1009 flag_count & 0x04000000 /*I2O_SGL_FLAGS_DIR */ ) {
1012 (p, (void __user *)sg[i].addr_bus,
1015 "%s: Could not copy SG buf %d FROM user\n",
1018 goto sg_list_cleanup;
1022 sg[i].addr_bus = virt_to_bus(p);
1026 rcode = i2o_msg_post_wait(c, m, 60);
1028 goto sg_list_cleanup;
1032 /* Copy back the Scatter Gather buffers back to user space */
1035 struct sg_simple_element *sg;
1038 // re-acquire the original message to handle correctly the sg copy operation
1039 memset(&msg, 0, MSG_FRAME_SIZE * 4);
1040 // get user msg size in u32s
1041 if (get_user(size, &user_msg[0])) {
1043 goto sg_list_cleanup;
1047 /* Copy in the user's I2O command */
1048 if (copy_from_user(msg, user_msg, size)) {
1050 goto sg_list_cleanup;
1053 (size - sg_offset * 4) / sizeof(struct sg_simple_element);
1056 sg = (struct sg_simple_element *)(msg + sg_offset);
1057 for (j = 0; j < sg_count; j++) {
1058 /* Copy out the SG list to user's buffer if necessary */
1061 flag_count & 0x4000000 /*I2O_SGL_FLAGS_DIR */ )) {
1062 sg_size = sg[j].flag_count & 0xffffff;
1065 ((void __user *)sg[j].addr_bus, sg_list[j],
1068 "%s: Could not copy %p TO user %x\n",
1069 c->name, sg_list[j],
1072 goto sg_list_cleanup;
1078 /* Copy back the reply to user space */
1080 // we wrote our own values for context - now restore the user supplied ones
1081 if (copy_from_user(reply + 2, user_msg + 2, sizeof(u32) * 2)) {
1083 "%s: Could not copy message context FROM user\n",
1087 if (copy_to_user(user_reply, reply, reply_size)) {
1089 "%s: Could not copy reply TO user\n", c->name);
1095 for (i = 0; i < sg_index; i++)
1106 static int i2o_cfg_ioctl(struct inode *inode, struct file *fp, unsigned int cmd,
1113 ret = i2o_cfg_getiops(arg);
1117 ret = i2o_cfg_gethrt(arg);
1121 ret = i2o_cfg_getlct(arg);
1125 ret = i2o_cfg_parms(arg, I2OPARMSET);
1129 ret = i2o_cfg_parms(arg, I2OPARMGET);
1133 ret = i2o_cfg_swdl(arg);
1137 ret = i2o_cfg_swul(arg);
1141 ret = i2o_cfg_swdel(arg);
1145 ret = i2o_cfg_validate(arg);
1149 ret = i2o_cfg_evt_reg(arg, fp);
1153 ret = i2o_cfg_evt_get(arg, fp);
1157 ret = i2o_cfg_passthru(arg);
1161 osm_debug("unknown ioctl called!\n");
1168 static int cfg_open(struct inode *inode, struct file *file)
1170 struct i2o_cfg_info *tmp =
1171 (struct i2o_cfg_info *)kmalloc(sizeof(struct i2o_cfg_info),
1173 unsigned long flags;
1178 file->private_data = (void *)(i2o_cfg_info_id++);
1181 tmp->q_id = (ulong) file->private_data;
1186 tmp->next = open_files;
1188 spin_lock_irqsave(&i2o_config_lock, flags);
1190 spin_unlock_irqrestore(&i2o_config_lock, flags);
1195 static int cfg_fasync(int fd, struct file *fp, int on)
1197 ulong id = (ulong) fp->private_data;
1198 struct i2o_cfg_info *p;
1200 for (p = open_files; p; p = p->next)
1207 return fasync_helper(fd, fp, on, &p->fasync);
1210 static int cfg_release(struct inode *inode, struct file *file)
1212 ulong id = (ulong) file->private_data;
1213 struct i2o_cfg_info *p1, *p2;
1214 unsigned long flags;
1219 spin_lock_irqsave(&i2o_config_lock, flags);
1220 for (p1 = open_files; p1;) {
1221 if (p1->q_id == id) {
1224 cfg_fasync(-1, file, 0);
1226 p2->next = p1->next;
1228 open_files = p1->next;
1236 spin_unlock_irqrestore(&i2o_config_lock, flags);
1242 static struct file_operations config_fops = {
1243 .owner = THIS_MODULE,
1244 .llseek = no_llseek,
1245 .ioctl = i2o_cfg_ioctl,
1246 #ifdef CONFIG_COMPAT
1247 .compat_ioctl = i2o_cfg_compat_ioctl,
1250 .release = cfg_release,
1251 .fasync = cfg_fasync,
1254 static struct miscdevice i2o_miscdev = {
1260 static int __init i2o_config_init(void)
1262 printk(KERN_INFO OSM_DESCRIPTION " v" OSM_VERSION "\n");
1264 spin_lock_init(&i2o_config_lock);
1266 if (misc_register(&i2o_miscdev) < 0) {
1267 osm_err("can't register device.\n");
1271 * Install our handler
1273 if (i2o_driver_register(&i2o_config_driver)) {
1274 osm_err("handler register failed.\n");
1275 misc_deregister(&i2o_miscdev);
1281 static void i2o_config_exit(void)
1283 misc_deregister(&i2o_miscdev);
1284 i2o_driver_unregister(&i2o_config_driver);
1287 MODULE_AUTHOR("Red Hat Software");
1288 MODULE_LICENSE("GPL");
1289 MODULE_DESCRIPTION(OSM_DESCRIPTION);
1290 MODULE_VERSION(OSM_VERSION);
1292 module_init(i2o_config_init);
1293 module_exit(i2o_config_exit);