linux/drivers/message/i2o/i2o_config.c
<<
>>
Prefs
   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@lxorguk.ukuu.org.uk>:
  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/miscdevice.h>
  34#include <linux/mutex.h>
  35#include <linux/compat.h>
  36#include <linux/slab.h>
  37
  38#include <asm/uaccess.h>
  39
  40#include "core.h"
  41
  42#define SG_TABLESIZE            30
  43
  44static DEFINE_MUTEX(i2o_cfg_mutex);
  45static long i2o_cfg_ioctl(struct file *, unsigned int, unsigned long);
  46
  47static spinlock_t i2o_config_lock;
  48
  49#define MODINC(x,y) ((x) = ((x) + 1) % (y))
  50
  51struct sg_simple_element {
  52        u32 flag_count;
  53        u32 addr_bus;
  54};
  55
  56struct i2o_cfg_info {
  57        struct file *fp;
  58        struct fasync_struct *fasync;
  59        struct i2o_evt_info event_q[I2O_EVT_Q_LEN];
  60        u16 q_in;               // Queue head index
  61        u16 q_out;              // Queue tail index
  62        u16 q_len;              // Queue length
  63        u16 q_lost;             // Number of lost events
  64        ulong q_id;             // Event queue ID...used as tx_context
  65        struct i2o_cfg_info *next;
  66};
  67static struct i2o_cfg_info *open_files = NULL;
  68static ulong i2o_cfg_info_id = 0;
  69
  70static int i2o_cfg_getiops(unsigned long arg)
  71{
  72        struct i2o_controller *c;
  73        u8 __user *user_iop_table = (void __user *)arg;
  74        u8 tmp[MAX_I2O_CONTROLLERS];
  75        int ret = 0;
  76
  77        memset(tmp, 0, MAX_I2O_CONTROLLERS);
  78
  79        list_for_each_entry(c, &i2o_controllers, list)
  80            tmp[c->unit] = 1;
  81
  82        if (copy_to_user(user_iop_table, tmp, MAX_I2O_CONTROLLERS))
  83                ret = -EFAULT;
  84
  85        return ret;
  86};
  87
  88static int i2o_cfg_gethrt(unsigned long arg)
  89{
  90        struct i2o_controller *c;
  91        struct i2o_cmd_hrtlct __user *cmd = (struct i2o_cmd_hrtlct __user *)arg;
  92        struct i2o_cmd_hrtlct kcmd;
  93        i2o_hrt *hrt;
  94        int len;
  95        u32 reslen;
  96        int ret = 0;
  97
  98        if (copy_from_user(&kcmd, cmd, sizeof(struct i2o_cmd_hrtlct)))
  99                return -EFAULT;
 100
 101        if (get_user(reslen, kcmd.reslen) < 0)
 102                return -EFAULT;
 103
 104        if (kcmd.resbuf == NULL)
 105                return -EFAULT;
 106
 107        c = i2o_find_iop(kcmd.iop);
 108        if (!c)
 109                return -ENXIO;
 110
 111        hrt = (i2o_hrt *) c->hrt.virt;
 112
 113        len = 8 + ((hrt->entry_len * hrt->num_entries) << 2);
 114
 115        if (put_user(len, kcmd.reslen))
 116                ret = -EFAULT;
 117        else if (len > reslen)
 118                ret = -ENOBUFS;
 119        else if (copy_to_user(kcmd.resbuf, (void *)hrt, len))
 120                ret = -EFAULT;
 121
 122        return ret;
 123};
 124
 125static int i2o_cfg_getlct(unsigned long arg)
 126{
 127        struct i2o_controller *c;
 128        struct i2o_cmd_hrtlct __user *cmd = (struct i2o_cmd_hrtlct __user *)arg;
 129        struct i2o_cmd_hrtlct kcmd;
 130        i2o_lct *lct;
 131        int len;
 132        int ret = 0;
 133        u32 reslen;
 134
 135        if (copy_from_user(&kcmd, cmd, sizeof(struct i2o_cmd_hrtlct)))
 136                return -EFAULT;
 137
 138        if (get_user(reslen, kcmd.reslen) < 0)
 139                return -EFAULT;
 140
 141        if (kcmd.resbuf == NULL)
 142                return -EFAULT;
 143
 144        c = i2o_find_iop(kcmd.iop);
 145        if (!c)
 146                return -ENXIO;
 147
 148        lct = (i2o_lct *) c->lct;
 149
 150        len = (unsigned int)lct->table_size << 2;
 151        if (put_user(len, kcmd.reslen))
 152                ret = -EFAULT;
 153        else if (len > reslen)
 154                ret = -ENOBUFS;
 155        else if (copy_to_user(kcmd.resbuf, lct, len))
 156                ret = -EFAULT;
 157
 158        return ret;
 159};
 160
 161static int i2o_cfg_parms(unsigned long arg, unsigned int type)
 162{
 163        int ret = 0;
 164        struct i2o_controller *c;
 165        struct i2o_device *dev;
 166        struct i2o_cmd_psetget __user *cmd =
 167            (struct i2o_cmd_psetget __user *)arg;
 168        struct i2o_cmd_psetget kcmd;
 169        u32 reslen;
 170        u8 *ops;
 171        u8 *res;
 172        int len = 0;
 173
 174        u32 i2o_cmd = (type == I2OPARMGET ?
 175                       I2O_CMD_UTIL_PARAMS_GET : I2O_CMD_UTIL_PARAMS_SET);
 176
 177        if (copy_from_user(&kcmd, cmd, sizeof(struct i2o_cmd_psetget)))
 178                return -EFAULT;
 179
 180        if (get_user(reslen, kcmd.reslen))
 181                return -EFAULT;
 182
 183        c = i2o_find_iop(kcmd.iop);
 184        if (!c)
 185                return -ENXIO;
 186
 187        dev = i2o_iop_find_device(c, kcmd.tid);
 188        if (!dev)
 189                return -ENXIO;
 190
 191        /*
 192         * Stop users being able to try and allocate arbitrary amounts
 193         * of DMA space. 64K is way more than sufficient for this.
 194         */
 195        if (kcmd.oplen > 65536)
 196                return -EMSGSIZE;
 197
 198        ops = memdup_user(kcmd.opbuf, kcmd.oplen);
 199        if (IS_ERR(ops))
 200                return PTR_ERR(ops);
 201
 202        /*
 203         * It's possible to have a _very_ large table
 204         * and that the user asks for all of it at once...
 205         */
 206        res = kmalloc(65536, GFP_KERNEL);
 207        if (!res) {
 208                kfree(ops);
 209                return -ENOMEM;
 210        }
 211
 212        len = i2o_parm_issue(dev, i2o_cmd, ops, kcmd.oplen, res, 65536);
 213        kfree(ops);
 214
 215        if (len < 0) {
 216                kfree(res);
 217                return -EAGAIN;
 218        }
 219
 220        if (put_user(len, kcmd.reslen))
 221                ret = -EFAULT;
 222        else if (len > reslen)
 223                ret = -ENOBUFS;
 224        else if (copy_to_user(kcmd.resbuf, res, len))
 225                ret = -EFAULT;
 226
 227        kfree(res);
 228
 229        return ret;
 230};
 231
 232static int i2o_cfg_swdl(unsigned long arg)
 233{
 234        struct i2o_sw_xfer kxfer;
 235        struct i2o_sw_xfer __user *pxfer = (struct i2o_sw_xfer __user *)arg;
 236        unsigned char maxfrag = 0, curfrag = 1;
 237        struct i2o_dma buffer;
 238        struct i2o_message *msg;
 239        unsigned int status = 0, swlen = 0, fragsize = 8192;
 240        struct i2o_controller *c;
 241
 242        if (copy_from_user(&kxfer, pxfer, sizeof(struct i2o_sw_xfer)))
 243                return -EFAULT;
 244
 245        if (get_user(swlen, kxfer.swlen) < 0)
 246                return -EFAULT;
 247
 248        if (get_user(maxfrag, kxfer.maxfrag) < 0)
 249                return -EFAULT;
 250
 251        if (get_user(curfrag, kxfer.curfrag) < 0)
 252                return -EFAULT;
 253
 254        if (curfrag == maxfrag)
 255                fragsize = swlen - (maxfrag - 1) * 8192;
 256
 257        if (!kxfer.buf || !access_ok(VERIFY_READ, kxfer.buf, fragsize))
 258                return -EFAULT;
 259
 260        c = i2o_find_iop(kxfer.iop);
 261        if (!c)
 262                return -ENXIO;
 263
 264        msg = i2o_msg_get_wait(c, I2O_TIMEOUT_MESSAGE_GET);
 265        if (IS_ERR(msg))
 266                return PTR_ERR(msg);
 267
 268        if (i2o_dma_alloc(&c->pdev->dev, &buffer, fragsize)) {
 269                i2o_msg_nop(c, msg);
 270                return -ENOMEM;
 271        }
 272
 273        if (__copy_from_user(buffer.virt, kxfer.buf, fragsize)) {
 274                i2o_msg_nop(c, msg);
 275                i2o_dma_free(&c->pdev->dev, &buffer);
 276                return -EFAULT;
 277        }
 278
 279        msg->u.head[0] = cpu_to_le32(NINE_WORD_MSG_SIZE | SGL_OFFSET_7);
 280        msg->u.head[1] =
 281            cpu_to_le32(I2O_CMD_SW_DOWNLOAD << 24 | HOST_TID << 12 |
 282                        ADAPTER_TID);
 283        msg->u.head[2] = cpu_to_le32(i2o_config_driver.context);
 284        msg->u.head[3] = cpu_to_le32(0);
 285        msg->body[0] =
 286            cpu_to_le32((((u32) kxfer.flags) << 24) | (((u32) kxfer.
 287                                                        sw_type) << 16) |
 288                        (((u32) maxfrag) << 8) | (((u32) curfrag)));
 289        msg->body[1] = cpu_to_le32(swlen);
 290        msg->body[2] = cpu_to_le32(kxfer.sw_id);
 291        msg->body[3] = cpu_to_le32(0xD0000000 | fragsize);
 292        msg->body[4] = cpu_to_le32(buffer.phys);
 293
 294        osm_debug("swdl frag %d/%d (size %d)\n", curfrag, maxfrag, fragsize);
 295        status = i2o_msg_post_wait_mem(c, msg, 60, &buffer);
 296
 297        if (status != -ETIMEDOUT)
 298                i2o_dma_free(&c->pdev->dev, &buffer);
 299
 300        if (status != I2O_POST_WAIT_OK) {
 301                // it fails if you try and send frags out of order
 302                // and for some yet unknown reasons too
 303                osm_info("swdl failed, DetailedStatus = %d\n", status);
 304                return status;
 305        }
 306
 307        return 0;
 308};
 309
 310static int i2o_cfg_swul(unsigned long arg)
 311{
 312        struct i2o_sw_xfer kxfer;
 313        struct i2o_sw_xfer __user *pxfer = (struct i2o_sw_xfer __user *)arg;
 314        unsigned char maxfrag = 0, curfrag = 1;
 315        struct i2o_dma buffer;
 316        struct i2o_message *msg;
 317        unsigned int status = 0, swlen = 0, fragsize = 8192;
 318        struct i2o_controller *c;
 319        int ret = 0;
 320
 321        if (copy_from_user(&kxfer, pxfer, sizeof(struct i2o_sw_xfer)))
 322                return -EFAULT;
 323
 324        if (get_user(swlen, kxfer.swlen) < 0)
 325                return -EFAULT;
 326
 327        if (get_user(maxfrag, kxfer.maxfrag) < 0)
 328                return -EFAULT;
 329
 330        if (get_user(curfrag, kxfer.curfrag) < 0)
 331                return -EFAULT;
 332
 333        if (curfrag == maxfrag)
 334                fragsize = swlen - (maxfrag - 1) * 8192;
 335
 336        if (!kxfer.buf)
 337                return -EFAULT;
 338
 339        c = i2o_find_iop(kxfer.iop);
 340        if (!c)
 341                return -ENXIO;
 342
 343        msg = i2o_msg_get_wait(c, I2O_TIMEOUT_MESSAGE_GET);
 344        if (IS_ERR(msg))
 345                return PTR_ERR(msg);
 346
 347        if (i2o_dma_alloc(&c->pdev->dev, &buffer, fragsize)) {
 348                i2o_msg_nop(c, msg);
 349                return -ENOMEM;
 350        }
 351
 352        msg->u.head[0] = cpu_to_le32(NINE_WORD_MSG_SIZE | SGL_OFFSET_7);
 353        msg->u.head[1] =
 354            cpu_to_le32(I2O_CMD_SW_UPLOAD << 24 | HOST_TID << 12 | ADAPTER_TID);
 355        msg->u.head[2] = cpu_to_le32(i2o_config_driver.context);
 356        msg->u.head[3] = cpu_to_le32(0);
 357        msg->body[0] =
 358            cpu_to_le32((u32) kxfer.flags << 24 | (u32) kxfer.
 359                        sw_type << 16 | (u32) maxfrag << 8 | (u32) curfrag);
 360        msg->body[1] = cpu_to_le32(swlen);
 361        msg->body[2] = cpu_to_le32(kxfer.sw_id);
 362        msg->body[3] = cpu_to_le32(0xD0000000 | fragsize);
 363        msg->body[4] = cpu_to_le32(buffer.phys);
 364
 365        osm_debug("swul frag %d/%d (size %d)\n", curfrag, maxfrag, fragsize);
 366        status = i2o_msg_post_wait_mem(c, msg, 60, &buffer);
 367
 368        if (status != I2O_POST_WAIT_OK) {
 369                if (status != -ETIMEDOUT)
 370                        i2o_dma_free(&c->pdev->dev, &buffer);
 371
 372                osm_info("swul failed, DetailedStatus = %d\n", status);
 373                return status;
 374        }
 375
 376        if (copy_to_user(kxfer.buf, buffer.virt, fragsize))
 377                ret = -EFAULT;
 378
 379        i2o_dma_free(&c->pdev->dev, &buffer);
 380
 381        return ret;
 382}
 383
 384static int i2o_cfg_swdel(unsigned long arg)
 385{
 386        struct i2o_controller *c;
 387        struct i2o_sw_xfer kxfer;
 388        struct i2o_sw_xfer __user *pxfer = (struct i2o_sw_xfer __user *)arg;
 389        struct i2o_message *msg;
 390        unsigned int swlen;
 391        int token;
 392
 393        if (copy_from_user(&kxfer, pxfer, sizeof(struct i2o_sw_xfer)))
 394                return -EFAULT;
 395
 396        if (get_user(swlen, kxfer.swlen) < 0)
 397                return -EFAULT;
 398
 399        c = i2o_find_iop(kxfer.iop);
 400        if (!c)
 401                return -ENXIO;
 402
 403        msg = i2o_msg_get_wait(c, I2O_TIMEOUT_MESSAGE_GET);
 404        if (IS_ERR(msg))
 405                return PTR_ERR(msg);
 406
 407        msg->u.head[0] = cpu_to_le32(SEVEN_WORD_MSG_SIZE | SGL_OFFSET_0);
 408        msg->u.head[1] =
 409            cpu_to_le32(I2O_CMD_SW_REMOVE << 24 | HOST_TID << 12 | ADAPTER_TID);
 410        msg->u.head[2] = cpu_to_le32(i2o_config_driver.context);
 411        msg->u.head[3] = cpu_to_le32(0);
 412        msg->body[0] =
 413            cpu_to_le32((u32) kxfer.flags << 24 | (u32) kxfer.sw_type << 16);
 414        msg->body[1] = cpu_to_le32(swlen);
 415        msg->body[2] = cpu_to_le32(kxfer.sw_id);
 416
 417        token = i2o_msg_post_wait(c, msg, 10);
 418
 419        if (token != I2O_POST_WAIT_OK) {
 420                osm_info("swdel failed, DetailedStatus = %d\n", token);
 421                return -ETIMEDOUT;
 422        }
 423
 424        return 0;
 425};
 426
 427static int i2o_cfg_validate(unsigned long arg)
 428{
 429        int token;
 430        int iop = (int)arg;
 431        struct i2o_message *msg;
 432        struct i2o_controller *c;
 433
 434        c = i2o_find_iop(iop);
 435        if (!c)
 436                return -ENXIO;
 437
 438        msg = i2o_msg_get_wait(c, I2O_TIMEOUT_MESSAGE_GET);
 439        if (IS_ERR(msg))
 440                return PTR_ERR(msg);
 441
 442        msg->u.head[0] = cpu_to_le32(FOUR_WORD_MSG_SIZE | SGL_OFFSET_0);
 443        msg->u.head[1] =
 444            cpu_to_le32(I2O_CMD_CONFIG_VALIDATE << 24 | HOST_TID << 12 | iop);
 445        msg->u.head[2] = cpu_to_le32(i2o_config_driver.context);
 446        msg->u.head[3] = cpu_to_le32(0);
 447
 448        token = i2o_msg_post_wait(c, msg, 10);
 449
 450        if (token != I2O_POST_WAIT_OK) {
 451                osm_info("Can't validate configuration, ErrorStatus = %d\n",
 452                         token);
 453                return -ETIMEDOUT;
 454        }
 455
 456        return 0;
 457};
 458
 459static int i2o_cfg_evt_reg(unsigned long arg, struct file *fp)
 460{
 461        struct i2o_message *msg;
 462        struct i2o_evt_id __user *pdesc = (struct i2o_evt_id __user *)arg;
 463        struct i2o_evt_id kdesc;
 464        struct i2o_controller *c;
 465        struct i2o_device *d;
 466
 467        if (copy_from_user(&kdesc, pdesc, sizeof(struct i2o_evt_id)))
 468                return -EFAULT;
 469
 470        /* IOP exists? */
 471        c = i2o_find_iop(kdesc.iop);
 472        if (!c)
 473                return -ENXIO;
 474
 475        /* Device exists? */
 476        d = i2o_iop_find_device(c, kdesc.tid);
 477        if (!d)
 478                return -ENODEV;
 479
 480        msg = i2o_msg_get_wait(c, I2O_TIMEOUT_MESSAGE_GET);
 481        if (IS_ERR(msg))
 482                return PTR_ERR(msg);
 483
 484        msg->u.head[0] = cpu_to_le32(FOUR_WORD_MSG_SIZE | SGL_OFFSET_0);
 485        msg->u.head[1] =
 486            cpu_to_le32(I2O_CMD_UTIL_EVT_REGISTER << 24 | HOST_TID << 12 |
 487                        kdesc.tid);
 488        msg->u.head[2] = cpu_to_le32(i2o_config_driver.context);
 489        msg->u.head[3] = cpu_to_le32(i2o_cntxt_list_add(c, fp->private_data));
 490        msg->body[0] = cpu_to_le32(kdesc.evt_mask);
 491
 492        i2o_msg_post(c, msg);
 493
 494        return 0;
 495}
 496
 497static int i2o_cfg_evt_get(unsigned long arg, struct file *fp)
 498{
 499        struct i2o_cfg_info *p = NULL;
 500        struct i2o_evt_get __user *uget = (struct i2o_evt_get __user *)arg;
 501        struct i2o_evt_get kget;
 502        unsigned long flags;
 503
 504        for (p = open_files; p; p = p->next)
 505                if (p->q_id == (ulong) fp->private_data)
 506                        break;
 507
 508        if (!p->q_len)
 509                return -ENOENT;
 510
 511        memcpy(&kget.info, &p->event_q[p->q_out], sizeof(struct i2o_evt_info));
 512        MODINC(p->q_out, I2O_EVT_Q_LEN);
 513        spin_lock_irqsave(&i2o_config_lock, flags);
 514        p->q_len--;
 515        kget.pending = p->q_len;
 516        kget.lost = p->q_lost;
 517        spin_unlock_irqrestore(&i2o_config_lock, flags);
 518
 519        if (copy_to_user(uget, &kget, sizeof(struct i2o_evt_get)))
 520                return -EFAULT;
 521        return 0;
 522}
 523
 524#ifdef CONFIG_COMPAT
 525static int i2o_cfg_passthru32(struct file *file, unsigned cmnd,
 526                              unsigned long arg)
 527{
 528        struct i2o_cmd_passthru32 __user *cmd;
 529        struct i2o_controller *c;
 530        u32 __user *user_msg;
 531        u32 *reply = NULL;
 532        u32 __user *user_reply = NULL;
 533        u32 size = 0;
 534        u32 reply_size = 0;
 535        u32 rcode = 0;
 536        struct i2o_dma sg_list[SG_TABLESIZE];
 537        u32 sg_offset = 0;
 538        u32 sg_count = 0;
 539        u32 i = 0;
 540        u32 sg_index = 0;
 541        i2o_status_block *sb;
 542        struct i2o_message *msg;
 543        unsigned int iop;
 544
 545        cmd = (struct i2o_cmd_passthru32 __user *)arg;
 546
 547        if (get_user(iop, &cmd->iop) || get_user(i, &cmd->msg))
 548                return -EFAULT;
 549
 550        user_msg = compat_ptr(i);
 551
 552        c = i2o_find_iop(iop);
 553        if (!c) {
 554                osm_debug("controller %d not found\n", iop);
 555                return -ENXIO;
 556        }
 557
 558        sb = c->status_block.virt;
 559
 560        if (get_user(size, &user_msg[0])) {
 561                osm_warn("unable to get size!\n");
 562                return -EFAULT;
 563        }
 564        size = size >> 16;
 565
 566        if (size > sb->inbound_frame_size) {
 567                osm_warn("size of message > inbound_frame_size");
 568                return -EFAULT;
 569        }
 570
 571        user_reply = &user_msg[size];
 572
 573        size <<= 2;             // Convert to bytes
 574
 575        msg = i2o_msg_get_wait(c, I2O_TIMEOUT_MESSAGE_GET);
 576        if (IS_ERR(msg))
 577                return PTR_ERR(msg);
 578
 579        rcode = -EFAULT;
 580        /* Copy in the user's I2O command */
 581        if (copy_from_user(msg, user_msg, size)) {
 582                osm_warn("unable to copy user message\n");
 583                goto out;
 584        }
 585        i2o_dump_message(msg);
 586
 587        if (get_user(reply_size, &user_reply[0]) < 0)
 588                goto out;
 589
 590        reply_size >>= 16;
 591        reply_size <<= 2;
 592
 593        rcode = -ENOMEM;
 594        reply = kzalloc(reply_size, GFP_KERNEL);
 595        if (!reply) {
 596                printk(KERN_WARNING "%s: Could not allocate reply buffer\n",
 597                       c->name);
 598                goto out;
 599        }
 600
 601        sg_offset = (msg->u.head[0] >> 4) & 0x0f;
 602
 603        memset(sg_list, 0, sizeof(sg_list[0]) * SG_TABLESIZE);
 604        if (sg_offset) {
 605                struct sg_simple_element *sg;
 606
 607                if (sg_offset * 4 >= size) {
 608                        rcode = -EFAULT;
 609                        goto cleanup;
 610                }
 611                // TODO 64bit fix
 612                sg = (struct sg_simple_element *)((&msg->u.head[0]) +
 613                                                  sg_offset);
 614                sg_count =
 615                    (size - sg_offset * 4) / sizeof(struct sg_simple_element);
 616                if (sg_count > SG_TABLESIZE) {
 617                        printk(KERN_DEBUG "%s:IOCTL SG List too large (%u)\n",
 618                               c->name, sg_count);
 619                        rcode = -EINVAL;
 620                        goto cleanup;
 621                }
 622
 623                for (i = 0; i < sg_count; i++) {
 624                        int sg_size;
 625                        struct i2o_dma *p;
 626
 627                        if (!(sg[i].flag_count & 0x10000000
 628                              /*I2O_SGL_FLAGS_SIMPLE_ADDRESS_ELEMENT */ )) {
 629                                printk(KERN_DEBUG
 630                                       "%s:Bad SG element %d - not simple (%x)\n",
 631                                       c->name, i, sg[i].flag_count);
 632                                rcode = -EINVAL;
 633                                goto cleanup;
 634                        }
 635                        sg_size = sg[i].flag_count & 0xffffff;
 636                        p = &(sg_list[sg_index]);
 637                        /* Allocate memory for the transfer */
 638                        if (i2o_dma_alloc(&c->pdev->dev, p, sg_size)) {
 639                                printk(KERN_DEBUG
 640                                       "%s: Could not allocate SG buffer - size = %d buffer number %d of %d\n",
 641                                       c->name, sg_size, i, sg_count);
 642                                rcode = -ENOMEM;
 643                                goto sg_list_cleanup;
 644                        }
 645                        sg_index++;
 646                        /* Copy in the user's SG buffer if necessary */
 647                        if (sg[i].
 648                            flag_count & 0x04000000 /*I2O_SGL_FLAGS_DIR */ ) {
 649                                // TODO 64bit fix
 650                                if (copy_from_user
 651                                    (p->virt,
 652                                     (void __user *)(unsigned long)sg[i].
 653                                     addr_bus, sg_size)) {
 654                                        printk(KERN_DEBUG
 655                                               "%s: Could not copy SG buf %d FROM user\n",
 656                                               c->name, i);
 657                                        rcode = -EFAULT;
 658                                        goto sg_list_cleanup;
 659                                }
 660                        }
 661                        //TODO 64bit fix
 662                        sg[i].addr_bus = (u32) p->phys;
 663                }
 664        }
 665
 666        rcode = i2o_msg_post_wait(c, msg, 60);
 667        msg = NULL;
 668        if (rcode) {
 669                reply[4] = ((u32) rcode) << 24;
 670                goto sg_list_cleanup;
 671        }
 672
 673        if (sg_offset) {
 674                u32 rmsg[I2O_OUTBOUND_MSG_FRAME_SIZE];
 675                /* Copy back the Scatter Gather buffers back to user space */
 676                u32 j;
 677                // TODO 64bit fix
 678                struct sg_simple_element *sg;
 679                int sg_size;
 680
 681                // re-acquire the original message to handle correctly the sg copy operation
 682                memset(&rmsg, 0, I2O_OUTBOUND_MSG_FRAME_SIZE * 4);
 683                // get user msg size in u32s
 684                if (get_user(size, &user_msg[0])) {
 685                        rcode = -EFAULT;
 686                        goto sg_list_cleanup;
 687                }
 688                size = size >> 16;
 689                size *= 4;
 690                if (size > sizeof(rmsg)) {
 691                        rcode = -EINVAL;
 692                        goto sg_list_cleanup;
 693                }
 694
 695                /* Copy in the user's I2O command */
 696                if (copy_from_user(rmsg, user_msg, size)) {
 697                        rcode = -EFAULT;
 698                        goto sg_list_cleanup;
 699                }
 700                sg_count =
 701                    (size - sg_offset * 4) / sizeof(struct sg_simple_element);
 702
 703                // TODO 64bit fix
 704                sg = (struct sg_simple_element *)(rmsg + sg_offset);
 705                for (j = 0; j < sg_count; j++) {
 706                        /* Copy out the SG list to user's buffer if necessary */
 707                        if (!
 708                            (sg[j].
 709                             flag_count & 0x4000000 /*I2O_SGL_FLAGS_DIR */ )) {
 710                                sg_size = sg[j].flag_count & 0xffffff;
 711                                // TODO 64bit fix
 712                                if (copy_to_user
 713                                    ((void __user *)(u64) sg[j].addr_bus,
 714                                     sg_list[j].virt, sg_size)) {
 715                                        printk(KERN_WARNING
 716                                               "%s: Could not copy %p TO user %x\n",
 717                                               c->name, sg_list[j].virt,
 718                                               sg[j].addr_bus);
 719                                        rcode = -EFAULT;
 720                                        goto sg_list_cleanup;
 721                                }
 722                        }
 723                }
 724        }
 725
 726sg_list_cleanup:
 727        /* Copy back the reply to user space */
 728        if (reply_size) {
 729                // we wrote our own values for context - now restore the user supplied ones
 730                if (copy_from_user(reply + 2, user_msg + 2, sizeof(u32) * 2)) {
 731                        printk(KERN_WARNING
 732                               "%s: Could not copy message context FROM user\n",
 733                               c->name);
 734                        rcode = -EFAULT;
 735                }
 736                if (copy_to_user(user_reply, reply, reply_size)) {
 737                        printk(KERN_WARNING
 738                               "%s: Could not copy reply TO user\n", c->name);
 739                        rcode = -EFAULT;
 740                }
 741        }
 742        for (i = 0; i < sg_index; i++)
 743                i2o_dma_free(&c->pdev->dev, &sg_list[i]);
 744
 745cleanup:
 746        kfree(reply);
 747out:
 748        if (msg)
 749                i2o_msg_nop(c, msg);
 750        return rcode;
 751}
 752
 753static long i2o_cfg_compat_ioctl(struct file *file, unsigned cmd,
 754                                 unsigned long arg)
 755{
 756        int ret;
 757        mutex_lock(&i2o_cfg_mutex);
 758        switch (cmd) {
 759        case I2OGETIOPS:
 760                ret = i2o_cfg_ioctl(file, cmd, arg);
 761                break;
 762        case I2OPASSTHRU32:
 763                ret = i2o_cfg_passthru32(file, cmd, arg);
 764                break;
 765        default:
 766                ret = -ENOIOCTLCMD;
 767                break;
 768        }
 769        mutex_unlock(&i2o_cfg_mutex);
 770        return ret;
 771}
 772
 773#endif
 774
 775#ifdef CONFIG_I2O_EXT_ADAPTEC
 776static int i2o_cfg_passthru(unsigned long arg)
 777{
 778        struct i2o_cmd_passthru __user *cmd =
 779            (struct i2o_cmd_passthru __user *)arg;
 780        struct i2o_controller *c;
 781        u32 __user *user_msg;
 782        u32 *reply = NULL;
 783        u32 __user *user_reply = NULL;
 784        u32 size = 0;
 785        u32 reply_size = 0;
 786        u32 rcode = 0;
 787        struct i2o_dma sg_list[SG_TABLESIZE];
 788        u32 sg_offset = 0;
 789        u32 sg_count = 0;
 790        int sg_index = 0;
 791        u32 i = 0;
 792        i2o_status_block *sb;
 793        struct i2o_message *msg;
 794        unsigned int iop;
 795
 796        if (get_user(iop, &cmd->iop) || get_user(user_msg, &cmd->msg))
 797                return -EFAULT;
 798
 799        c = i2o_find_iop(iop);
 800        if (!c) {
 801                osm_warn("controller %d not found\n", iop);
 802                return -ENXIO;
 803        }
 804
 805        sb = c->status_block.virt;
 806
 807        if (get_user(size, &user_msg[0]))
 808                return -EFAULT;
 809        size = size >> 16;
 810
 811        if (size > sb->inbound_frame_size) {
 812                osm_warn("size of message > inbound_frame_size");
 813                return -EFAULT;
 814        }
 815
 816        user_reply = &user_msg[size];
 817
 818        size <<= 2;             // Convert to bytes
 819
 820        msg = i2o_msg_get_wait(c, I2O_TIMEOUT_MESSAGE_GET);
 821        if (IS_ERR(msg))
 822                return PTR_ERR(msg);
 823
 824        rcode = -EFAULT;
 825        /* Copy in the user's I2O command */
 826        if (copy_from_user(msg, user_msg, size))
 827                goto out;
 828
 829        if (get_user(reply_size, &user_reply[0]) < 0)
 830                goto out;
 831
 832        reply_size >>= 16;
 833        reply_size <<= 2;
 834
 835        reply = kzalloc(reply_size, GFP_KERNEL);
 836        if (!reply) {
 837                printk(KERN_WARNING "%s: Could not allocate reply buffer\n",
 838                       c->name);
 839                rcode = -ENOMEM;
 840                goto out;
 841        }
 842
 843        sg_offset = (msg->u.head[0] >> 4) & 0x0f;
 844
 845        memset(sg_list, 0, sizeof(sg_list[0]) * SG_TABLESIZE);
 846        if (sg_offset) {
 847                struct sg_simple_element *sg;
 848                struct i2o_dma *p;
 849
 850                if (sg_offset * 4 >= size) {
 851                        rcode = -EFAULT;
 852                        goto cleanup;
 853                }
 854                // TODO 64bit fix
 855                sg = (struct sg_simple_element *)((&msg->u.head[0]) +
 856                                                  sg_offset);
 857                sg_count =
 858                    (size - sg_offset * 4) / sizeof(struct sg_simple_element);
 859                if (sg_count > SG_TABLESIZE) {
 860                        printk(KERN_DEBUG "%s:IOCTL SG List too large (%u)\n",
 861                               c->name, sg_count);
 862                        rcode = -EINVAL;
 863                        goto cleanup;
 864                }
 865
 866                for (i = 0; i < sg_count; i++) {
 867                        int sg_size;
 868
 869                        if (!(sg[i].flag_count & 0x10000000
 870                              /*I2O_SGL_FLAGS_SIMPLE_ADDRESS_ELEMENT */ )) {
 871                                printk(KERN_DEBUG
 872                                       "%s:Bad SG element %d - not simple (%x)\n",
 873                                       c->name, i, sg[i].flag_count);
 874                                rcode = -EINVAL;
 875                                goto sg_list_cleanup;
 876                        }
 877                        sg_size = sg[i].flag_count & 0xffffff;
 878                        p = &(sg_list[sg_index]);
 879                        if (i2o_dma_alloc(&c->pdev->dev, p, sg_size)) {
 880                        /* Allocate memory for the transfer */
 881                                printk(KERN_DEBUG
 882                                       "%s: Could not allocate SG buffer - size = %d buffer number %d of %d\n",
 883                                       c->name, sg_size, i, sg_count);
 884                                rcode = -ENOMEM;
 885                                goto sg_list_cleanup;
 886                        }
 887                        sg_index++;
 888                        /* Copy in the user's SG buffer if necessary */
 889                        if (sg[i].
 890                            flag_count & 0x04000000 /*I2O_SGL_FLAGS_DIR */ ) {
 891                                // TODO 64bit fix
 892                                if (copy_from_user
 893                                    (p->virt, (void __user *)sg[i].addr_bus,
 894                                     sg_size)) {
 895                                        printk(KERN_DEBUG
 896                                               "%s: Could not copy SG buf %d FROM user\n",
 897                                               c->name, i);
 898                                        rcode = -EFAULT;
 899                                        goto sg_list_cleanup;
 900                                }
 901                        }
 902                        sg[i].addr_bus = p->phys;
 903                }
 904        }
 905
 906        rcode = i2o_msg_post_wait(c, msg, 60);
 907        msg = NULL;
 908        if (rcode) {
 909                reply[4] = ((u32) rcode) << 24;
 910                goto sg_list_cleanup;
 911        }
 912
 913        if (sg_offset) {
 914                u32 rmsg[I2O_OUTBOUND_MSG_FRAME_SIZE];
 915                /* Copy back the Scatter Gather buffers back to user space */
 916                u32 j;
 917                // TODO 64bit fix
 918                struct sg_simple_element *sg;
 919                int sg_size;
 920
 921                // re-acquire the original message to handle correctly the sg copy operation
 922                memset(&rmsg, 0, I2O_OUTBOUND_MSG_FRAME_SIZE * 4);
 923                // get user msg size in u32s
 924                if (get_user(size, &user_msg[0])) {
 925                        rcode = -EFAULT;
 926                        goto sg_list_cleanup;
 927                }
 928                size = size >> 16;
 929                size *= 4;
 930                if (size > sizeof(rmsg)) {
 931                        rcode = -EFAULT;
 932                        goto sg_list_cleanup;
 933                }
 934
 935                /* Copy in the user's I2O command */
 936                if (copy_from_user(rmsg, user_msg, size)) {
 937                        rcode = -EFAULT;
 938                        goto sg_list_cleanup;
 939                }
 940                sg_count =
 941                    (size - sg_offset * 4) / sizeof(struct sg_simple_element);
 942
 943                // TODO 64bit fix
 944                sg = (struct sg_simple_element *)(rmsg + sg_offset);
 945                for (j = 0; j < sg_count; j++) {
 946                        /* Copy out the SG list to user's buffer if necessary */
 947                        if (!
 948                            (sg[j].
 949                             flag_count & 0x4000000 /*I2O_SGL_FLAGS_DIR */ )) {
 950                                sg_size = sg[j].flag_count & 0xffffff;
 951                                // TODO 64bit fix
 952                                if (copy_to_user
 953                                    ((void __user *)sg[j].addr_bus, sg_list[j].virt,
 954                                     sg_size)) {
 955                                        printk(KERN_WARNING
 956                                               "%s: Could not copy %p TO user %x\n",
 957                                               c->name, sg_list[j].virt,
 958                                               sg[j].addr_bus);
 959                                        rcode = -EFAULT;
 960                                        goto sg_list_cleanup;
 961                                }
 962                        }
 963                }
 964        }
 965
 966sg_list_cleanup:
 967        /* Copy back the reply to user space */
 968        if (reply_size) {
 969                // we wrote our own values for context - now restore the user supplied ones
 970                if (copy_from_user(reply + 2, user_msg + 2, sizeof(u32) * 2)) {
 971                        printk(KERN_WARNING
 972                               "%s: Could not copy message context FROM user\n",
 973                               c->name);
 974                        rcode = -EFAULT;
 975                }
 976                if (copy_to_user(user_reply, reply, reply_size)) {
 977                        printk(KERN_WARNING
 978                               "%s: Could not copy reply TO user\n", c->name);
 979                        rcode = -EFAULT;
 980                }
 981        }
 982
 983        for (i = 0; i < sg_index; i++)
 984                i2o_dma_free(&c->pdev->dev, &sg_list[i]);
 985
 986cleanup:
 987        kfree(reply);
 988out:
 989        if (msg)
 990                i2o_msg_nop(c, msg);
 991        return rcode;
 992}
 993#endif
 994
 995/*
 996 * IOCTL Handler
 997 */
 998static long i2o_cfg_ioctl(struct file *fp, unsigned int cmd, unsigned long arg)
 999{
1000        int ret;
1001
1002        mutex_lock(&i2o_cfg_mutex);
1003        switch (cmd) {
1004        case I2OGETIOPS:
1005                ret = i2o_cfg_getiops(arg);
1006                break;
1007
1008        case I2OHRTGET:
1009                ret = i2o_cfg_gethrt(arg);
1010                break;
1011
1012        case I2OLCTGET:
1013                ret = i2o_cfg_getlct(arg);
1014                break;
1015
1016        case I2OPARMSET:
1017                ret = i2o_cfg_parms(arg, I2OPARMSET);
1018                break;
1019
1020        case I2OPARMGET:
1021                ret = i2o_cfg_parms(arg, I2OPARMGET);
1022                break;
1023
1024        case I2OSWDL:
1025                ret = i2o_cfg_swdl(arg);
1026                break;
1027
1028        case I2OSWUL:
1029                ret = i2o_cfg_swul(arg);
1030                break;
1031
1032        case I2OSWDEL:
1033                ret = i2o_cfg_swdel(arg);
1034                break;
1035
1036        case I2OVALIDATE:
1037                ret = i2o_cfg_validate(arg);
1038                break;
1039
1040        case I2OEVTREG:
1041                ret = i2o_cfg_evt_reg(arg, fp);
1042                break;
1043
1044        case I2OEVTGET:
1045                ret = i2o_cfg_evt_get(arg, fp);
1046                break;
1047
1048#ifdef CONFIG_I2O_EXT_ADAPTEC
1049        case I2OPASSTHRU:
1050                ret = i2o_cfg_passthru(arg);
1051                break;
1052#endif
1053
1054        default:
1055                osm_debug("unknown ioctl called!\n");
1056                ret = -EINVAL;
1057        }
1058        mutex_unlock(&i2o_cfg_mutex);
1059        return ret;
1060}
1061
1062static int cfg_open(struct inode *inode, struct file *file)
1063{
1064        struct i2o_cfg_info *tmp = kmalloc(sizeof(struct i2o_cfg_info),
1065                                           GFP_KERNEL);
1066        unsigned long flags;
1067
1068        if (!tmp)
1069                return -ENOMEM;
1070
1071        mutex_lock(&i2o_cfg_mutex);
1072        file->private_data = (void *)(i2o_cfg_info_id++);
1073        tmp->fp = file;
1074        tmp->fasync = NULL;
1075        tmp->q_id = (ulong) file->private_data;
1076        tmp->q_len = 0;
1077        tmp->q_in = 0;
1078        tmp->q_out = 0;
1079        tmp->q_lost = 0;
1080        tmp->next = open_files;
1081
1082        spin_lock_irqsave(&i2o_config_lock, flags);
1083        open_files = tmp;
1084        spin_unlock_irqrestore(&i2o_config_lock, flags);
1085        mutex_unlock(&i2o_cfg_mutex);
1086
1087        return 0;
1088}
1089
1090static int cfg_fasync(int fd, struct file *fp, int on)
1091{
1092        ulong id = (ulong) fp->private_data;
1093        struct i2o_cfg_info *p;
1094        int ret = -EBADF;
1095
1096        mutex_lock(&i2o_cfg_mutex);
1097        for (p = open_files; p; p = p->next)
1098                if (p->q_id == id)
1099                        break;
1100
1101        if (p)
1102                ret = fasync_helper(fd, fp, on, &p->fasync);
1103        mutex_unlock(&i2o_cfg_mutex);
1104        return ret;
1105}
1106
1107static int cfg_release(struct inode *inode, struct file *file)
1108{
1109        ulong id = (ulong) file->private_data;
1110        struct i2o_cfg_info *p, **q;
1111        unsigned long flags;
1112
1113        mutex_lock(&i2o_cfg_mutex);
1114        spin_lock_irqsave(&i2o_config_lock, flags);
1115        for (q = &open_files; (p = *q) != NULL; q = &p->next) {
1116                if (p->q_id == id) {
1117                        *q = p->next;
1118                        kfree(p);
1119                        break;
1120                }
1121        }
1122        spin_unlock_irqrestore(&i2o_config_lock, flags);
1123        mutex_unlock(&i2o_cfg_mutex);
1124
1125        return 0;
1126}
1127
1128static const struct file_operations config_fops = {
1129        .owner = THIS_MODULE,
1130        .llseek = no_llseek,
1131        .unlocked_ioctl = i2o_cfg_ioctl,
1132#ifdef CONFIG_COMPAT
1133        .compat_ioctl = i2o_cfg_compat_ioctl,
1134#endif
1135        .open = cfg_open,
1136        .release = cfg_release,
1137        .fasync = cfg_fasync,
1138};
1139
1140static struct miscdevice i2o_miscdev = {
1141        I2O_MINOR,
1142        "i2octl",
1143        &config_fops
1144};
1145
1146static int __init i2o_config_old_init(void)
1147{
1148        spin_lock_init(&i2o_config_lock);
1149
1150        if (misc_register(&i2o_miscdev) < 0) {
1151                osm_err("can't register device.\n");
1152                return -EBUSY;
1153        }
1154
1155        return 0;
1156}
1157
1158static void i2o_config_old_exit(void)
1159{
1160        misc_deregister(&i2o_miscdev);
1161}
1162
1163MODULE_AUTHOR("Red Hat Software");
1164