2  *  drivers/char/viotape.c
 
   6  *  Authors: Dave Boutcher <boutcher@us.ibm.com>
 
   7  *           Ryan Arnold <ryanarn@us.ibm.com>
 
   8  *           Colin Devilbiss <devilbis@us.ibm.com>
 
  11  * (C) Copyright 2000-2004 IBM Corporation
 
  13  * This program is free software;  you can redistribute it and/or
 
  14  * modify it under the terms of the GNU General Public License as
 
  15  * published by the Free Software Foundation; either version 2 of the
 
  16  * License, or (at your option) anyu later version.
 
  18  * This program is distributed in the hope that it will be useful, but
 
  19  * WITHOUT ANY WARRANTY; without even the implied warranty of
 
  20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
  21  * General Public License for more details.
 
  23  * You should have received a copy of the GNU General Public License
 
  24  * along with this program; if not, write to the Free Software Foundation,
 
  25  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 
  27  * This routine provides access to tape drives owned and managed by an OS/400
 
  28  * partition running on the same box as this Linux partition.
 
  30  * All tape operations are performed by sending messages back and forth to
 
  31  * the OS/400 partition.  The format of the messages is defined in
 
  34 #include <linux/module.h>
 
  35 #include <linux/kernel.h>
 
  36 #include <linux/errno.h>
 
  37 #include <linux/init.h>
 
  38 #include <linux/wait.h>
 
  39 #include <linux/spinlock.h>
 
  40 #include <linux/mtio.h>
 
  41 #include <linux/device.h>
 
  42 #include <linux/dma-mapping.h>
 
  44 #include <linux/cdev.h>
 
  45 #include <linux/major.h>
 
  46 #include <linux/completion.h>
 
  47 #include <linux/proc_fs.h>
 
  48 #include <linux/seq_file.h>
 
  49 #include <linux/smp_lock.h>
 
  51 #include <asm/uaccess.h>
 
  52 #include <asm/ioctls.h>
 
  53 #include <asm/firmware.h>
 
  55 #include <asm/iseries/vio.h>
 
  56 #include <asm/iseries/hv_lp_event.h>
 
  57 #include <asm/iseries/hv_call_event.h>
 
  58 #include <asm/iseries/hv_lp_config.h>
 
  60 #define VIOTAPE_VERSION         "1.2"
 
  61 #define VIOTAPE_MAXREQ          1
 
  63 #define VIOTAPE_KERN_WARN       KERN_WARNING "viotape: "
 
  64 #define VIOTAPE_KERN_INFO       KERN_INFO "viotape: "
 
  66 static int viotape_numdev;
 
  69  * The minor number follows the conventions of the SCSI tape drives.  The
 
  70  * rewind and mode are encoded in the minor #.  We use this struct to break
 
  73 struct viot_devinfo_struct {
 
  79 #define VIOTAPOP_RESET          0
 
  80 #define VIOTAPOP_FSF            1
 
  81 #define VIOTAPOP_BSF            2
 
  82 #define VIOTAPOP_FSR            3
 
  83 #define VIOTAPOP_BSR            4
 
  84 #define VIOTAPOP_WEOF           5
 
  85 #define VIOTAPOP_REW            6
 
  86 #define VIOTAPOP_NOP            7
 
  87 #define VIOTAPOP_EOM            8
 
  88 #define VIOTAPOP_ERASE          9
 
  89 #define VIOTAPOP_SETBLK        10
 
  90 #define VIOTAPOP_SETDENSITY    11
 
  91 #define VIOTAPOP_SETPOS        12
 
  92 #define VIOTAPOP_GETPOS        13
 
  93 #define VIOTAPOP_SETPART       14
 
  94 #define VIOTAPOP_UNLOAD        15
 
  97         viotape_InvalidRange = 0x0601,
 
  98         viotape_InvalidToken = 0x0602,
 
  99         viotape_DMAError = 0x0603,
 
 100         viotape_UseError = 0x0604,
 
 101         viotape_ReleaseError = 0x0605,
 
 102         viotape_InvalidTape = 0x0606,
 
 103         viotape_InvalidOp = 0x0607,
 
 104         viotape_TapeErr = 0x0608,
 
 106         viotape_AllocTimedOut = 0x0640,
 
 107         viotape_BOTEnc = 0x0641,
 
 108         viotape_BlankTape = 0x0642,
 
 109         viotape_BufferEmpty = 0x0643,
 
 110         viotape_CleanCartFound = 0x0644,
 
 111         viotape_CmdNotAllowed = 0x0645,
 
 112         viotape_CmdNotSupported = 0x0646,
 
 113         viotape_DataCheck = 0x0647,
 
 114         viotape_DecompressErr = 0x0648,
 
 115         viotape_DeviceTimeout = 0x0649,
 
 116         viotape_DeviceUnavail = 0x064a,
 
 117         viotape_DeviceBusy = 0x064b,
 
 118         viotape_EndOfMedia = 0x064c,
 
 119         viotape_EndOfTape = 0x064d,
 
 120         viotape_EquipCheck = 0x064e,
 
 121         viotape_InsufficientRs = 0x064f,
 
 122         viotape_InvalidLogBlk = 0x0650,
 
 123         viotape_LengthError = 0x0651,
 
 124         viotape_LibDoorOpen = 0x0652,
 
 125         viotape_LoadFailure = 0x0653,
 
 126         viotape_NotCapable = 0x0654,
 
 127         viotape_NotOperational = 0x0655,
 
 128         viotape_NotReady = 0x0656,
 
 129         viotape_OpCancelled = 0x0657,
 
 130         viotape_PhyLinkErr = 0x0658,
 
 131         viotape_RdyNotBOT = 0x0659,
 
 132         viotape_TapeMark = 0x065a,
 
 133         viotape_WriteProt = 0x065b
 
 136 static const struct vio_error_entry viotape_err_table[] = {
 
 137         { viotape_InvalidRange, EIO, "Internal error" },
 
 138         { viotape_InvalidToken, EIO, "Internal error" },
 
 139         { viotape_DMAError, EIO, "DMA error" },
 
 140         { viotape_UseError, EIO, "Internal error" },
 
 141         { viotape_ReleaseError, EIO, "Internal error" },
 
 142         { viotape_InvalidTape, EIO, "Invalid tape device" },
 
 143         { viotape_InvalidOp, EIO, "Invalid operation" },
 
 144         { viotape_TapeErr, EIO, "Tape error" },
 
 145         { viotape_AllocTimedOut, EBUSY, "Allocate timed out" },
 
 146         { viotape_BOTEnc, EIO, "Beginning of tape encountered" },
 
 147         { viotape_BlankTape, EIO, "Blank tape" },
 
 148         { viotape_BufferEmpty, EIO, "Buffer empty" },
 
 149         { viotape_CleanCartFound, ENOMEDIUM, "Cleaning cartridge found" },
 
 150         { viotape_CmdNotAllowed, EIO, "Command not allowed" },
 
 151         { viotape_CmdNotSupported, EIO, "Command not supported" },
 
 152         { viotape_DataCheck, EIO, "Data check" },
 
 153         { viotape_DecompressErr, EIO, "Decompression error" },
 
 154         { viotape_DeviceTimeout, EBUSY, "Device timeout" },
 
 155         { viotape_DeviceUnavail, EIO, "Device unavailable" },
 
 156         { viotape_DeviceBusy, EBUSY, "Device busy" },
 
 157         { viotape_EndOfMedia, ENOSPC, "End of media" },
 
 158         { viotape_EndOfTape, ENOSPC, "End of tape" },
 
 159         { viotape_EquipCheck, EIO, "Equipment check" },
 
 160         { viotape_InsufficientRs, EOVERFLOW, "Insufficient tape resources" },
 
 161         { viotape_InvalidLogBlk, EIO, "Invalid logical block location" },
 
 162         { viotape_LengthError, EOVERFLOW, "Length error" },
 
 163         { viotape_LibDoorOpen, EBUSY, "Door open" },
 
 164         { viotape_LoadFailure, ENOMEDIUM, "Load failure" },
 
 165         { viotape_NotCapable, EIO, "Not capable" },
 
 166         { viotape_NotOperational, EIO, "Not operational" },
 
 167         { viotape_NotReady, EIO, "Not ready" },
 
 168         { viotape_OpCancelled, EIO, "Operation cancelled" },
 
 169         { viotape_PhyLinkErr, EIO, "Physical link error" },
 
 170         { viotape_RdyNotBOT, EIO, "Ready but not beginning of tape" },
 
 171         { viotape_TapeMark, EIO, "Tape mark" },
 
 172         { viotape_WriteProt, EROFS, "Write protection error" },
 
 176 /* Maximum number of tapes we support */
 
 177 #define VIOTAPE_MAX_TAPE        HVMAXARCHITECTEDVIRTUALTAPES
 
 178 #define MAX_PARTITIONS          4
 
 180 /* defines for current tape state */
 
 182 #define VIOT_READING            1
 
 183 #define VIOT_WRITING            2
 
 185 /* Our info on the tapes */
 
 187         const char *rsrcname;
 
 190 } viotape_unitinfo[VIOTAPE_MAX_TAPE];
 
 192 static struct mtget viomtget[VIOTAPE_MAX_TAPE];
 
 194 static struct class *tape_class;
 
 196 static struct device *tape_device[VIOTAPE_MAX_TAPE];
 
 199  * maintain the current state of each tape (and partition)
 
 200  * so that we know when to write EOF marks.
 
 203         unsigned char   cur_part;
 
 204         unsigned char   part_stat_rwi[MAX_PARTITIONS];
 
 205 } state[VIOTAPE_MAX_TAPE];
 
 207 /* We single-thread */
 
 208 static struct semaphore reqSem;
 
 211  * When we send a request, we use this struct to get the response back
 
 212  * from the interrupt handler
 
 220         struct completion       com;
 
 222         struct op_struct        *next;
 
 225 static spinlock_t       op_struct_list_lock;
 
 226 static struct op_struct *op_struct_list;
 
 228 /* forward declaration to resolve interdependence */
 
 229 static int chg_state(int index, unsigned char new_state, struct file *file);
 
 232 static int proc_viotape_show(struct seq_file *m, void *v)
 
 236         seq_printf(m, "viotape driver version " VIOTAPE_VERSION "\n");
 
 237         for (i = 0; i < viotape_numdev; i++) {
 
 238                 seq_printf(m, "viotape device %d is iSeries resource %10.10s"
 
 239                                 "type %4.4s, model %3.3s\n",
 
 240                                 i, viotape_unitinfo[i].rsrcname,
 
 241                                 viotape_unitinfo[i].type,
 
 242                                 viotape_unitinfo[i].model);
 
 247 static int proc_viotape_open(struct inode *inode, struct file *file)
 
 249         return single_open(file, proc_viotape_show, NULL);
 
 252 static const struct file_operations proc_viotape_operations = {
 
 253         .owner          = THIS_MODULE,
 
 254         .open           = proc_viotape_open,
 
 257         .release        = single_release,
 
 260 /* Decode the device minor number into its parts */
 
 261 void get_dev_info(struct inode *ino, struct viot_devinfo_struct *devi)
 
 263         devi->devno = iminor(ino) & 0x1F;
 
 264         devi->mode = (iminor(ino) & 0x60) >> 5;
 
 265         /* if bit is set in the minor, do _not_ rewind automatically */
 
 266         devi->rewind = (iminor(ino) & 0x80) == 0;
 
 269 /* This is called only from the exit and init paths, so no need for locking */
 
 270 static void clear_op_struct_pool(void)
 
 272         while (op_struct_list) {
 
 273                 struct op_struct *toFree = op_struct_list;
 
 274                 op_struct_list = op_struct_list->next;
 
 279 /* Likewise, this is only called from the init path */
 
 280 static int add_op_structs(int structs)
 
 284         for (i = 0; i < structs; ++i) {
 
 285                 struct op_struct *new_struct =
 
 286                         kmalloc(sizeof(*new_struct), GFP_KERNEL);
 
 288                         clear_op_struct_pool();
 
 291                 new_struct->next = op_struct_list;
 
 292                 op_struct_list = new_struct;
 
 297 /* Allocate an op structure from our pool */
 
 298 static struct op_struct *get_op_struct(void)
 
 300         struct op_struct *retval;
 
 303         spin_lock_irqsave(&op_struct_list_lock, flags);
 
 304         retval = op_struct_list;
 
 306                 op_struct_list = retval->next;
 
 307         spin_unlock_irqrestore(&op_struct_list_lock, flags);
 
 309                 memset(retval, 0, sizeof(*retval));
 
 310                 init_completion(&retval->com);
 
 316 /* Return an op structure to our pool */
 
 317 static void free_op_struct(struct op_struct *op_struct)
 
 321         spin_lock_irqsave(&op_struct_list_lock, flags);
 
 322         op_struct->next = op_struct_list;
 
 323         op_struct_list = op_struct;
 
 324         spin_unlock_irqrestore(&op_struct_list_lock, flags);
 
 327 /* Map our tape return codes to errno values */
 
 328 int tape_rc_to_errno(int tape_rc, char *operation, int tapeno)
 
 330         const struct vio_error_entry *err;
 
 335         err = vio_lookup_rc(viotape_err_table, tape_rc);
 
 336         printk(VIOTAPE_KERN_WARN "error(%s) 0x%04x on Device %d (%-10s): %s\n",
 
 337                         operation, tape_rc, tapeno,
 
 338                         viotape_unitinfo[tapeno].rsrcname, err->msg);
 
 343 static ssize_t viotap_write(struct file *file, const char *buf,
 
 344                 size_t count, loff_t * ppos)
 
 347         unsigned short flags = file->f_flags;
 
 348         int noblock = ((flags & O_NONBLOCK) != 0);
 
 350         struct viot_devinfo_struct devi;
 
 351         struct op_struct *op = get_op_struct();
 
 356         get_dev_info(file->f_path.dentry->d_inode, &devi);
 
 359          * We need to make sure we can send a request.  We use
 
 360          * a semaphore to keep track of # requests in use.  If
 
 361          * we are non-blocking, make sure we don't block on the
 
 365                 if (down_trylock(&reqSem)) {
 
 372         /* Allocate a DMA buffer */
 
 373         op->dev = tape_device[devi.devno];
 
 374         op->buffer = dma_alloc_coherent(op->dev, count, &op->dmaaddr,
 
 377         if (op->buffer == NULL) {
 
 378                 printk(VIOTAPE_KERN_WARN
 
 379                                 "error allocating dma buffer for len %ld\n",
 
 385         /* Copy the data into the buffer */
 
 386         if (copy_from_user(op->buffer, buf, count)) {
 
 387                 printk(VIOTAPE_KERN_WARN "tape: error on copy from user\n");
 
 392         op->non_blocking = noblock;
 
 393         init_completion(&op->com);
 
 396         hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
 
 397                         HvLpEvent_Type_VirtualIo,
 
 398                         viomajorsubtype_tape | viotapewrite,
 
 399                         HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck,
 
 400                         viopath_sourceinst(viopath_hostLp),
 
 401                         viopath_targetinst(viopath_hostLp),
 
 402                         (u64)(unsigned long)op, VIOVERSION << 16,
 
 403                         ((u64)devi.devno << 48) | op->dmaaddr, count, 0, 0);
 
 404         if (hvrc != HvLpEvent_Rc_Good) {
 
 405                 printk(VIOTAPE_KERN_WARN "hv error on op %d\n",
 
 414         wait_for_completion(&op->com);
 
 417                 ret = tape_rc_to_errno(op->rc, "write", devi.devno);
 
 419                 chg_state(devi.devno, VIOT_WRITING, file);
 
 424         dma_free_coherent(op->dev, count, op->buffer, op->dmaaddr);
 
 433 static ssize_t viotap_read(struct file *file, char *buf, size_t count,
 
 437         unsigned short flags = file->f_flags;
 
 438         struct op_struct *op = get_op_struct();
 
 439         int noblock = ((flags & O_NONBLOCK) != 0);
 
 441         struct viot_devinfo_struct devi;
 
 446         get_dev_info(file->f_path.dentry->d_inode, &devi);
 
 449          * We need to make sure we can send a request.  We use
 
 450          * a semaphore to keep track of # requests in use.  If
 
 451          * we are non-blocking, make sure we don't block on the
 
 455                 if (down_trylock(&reqSem)) {
 
 462         chg_state(devi.devno, VIOT_READING, file);
 
 464         /* Allocate a DMA buffer */
 
 465         op->dev = tape_device[devi.devno];
 
 466         op->buffer = dma_alloc_coherent(op->dev, count, &op->dmaaddr,
 
 468         if (op->buffer == NULL) {
 
 474         init_completion(&op->com);
 
 476         hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
 
 477                         HvLpEvent_Type_VirtualIo,
 
 478                         viomajorsubtype_tape | viotaperead,
 
 479                         HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck,
 
 480                         viopath_sourceinst(viopath_hostLp),
 
 481                         viopath_targetinst(viopath_hostLp),
 
 482                         (u64)(unsigned long)op, VIOVERSION << 16,
 
 483                         ((u64)devi.devno << 48) | op->dmaaddr, count, 0, 0);
 
 484         if (hvrc != HvLpEvent_Rc_Good) {
 
 485                 printk(VIOTAPE_KERN_WARN "tape hv error on op %d\n",
 
 491         wait_for_completion(&op->com);
 
 494                 ret = tape_rc_to_errno(op->rc, "read", devi.devno);
 
 497                 if (ret && copy_to_user(buf, op->buffer, ret)) {
 
 498                         printk(VIOTAPE_KERN_WARN "error on copy_to_user\n");
 
 504         dma_free_coherent(op->dev, count, op->buffer, op->dmaaddr);
 
 513 static int viotap_ioctl(struct inode *inode, struct file *file,
 
 514                 unsigned int cmd, unsigned long arg)
 
 518         struct viot_devinfo_struct devi;
 
 521         struct op_struct *op = get_op_struct();
 
 526         get_dev_info(file->f_path.dentry->d_inode, &devi);
 
 536                  * inode is null if and only if we (the kernel)
 
 540                         memcpy(&mtc, (void *) arg, sizeof(struct mtop));
 
 541                 else if (copy_from_user((char *)&mtc, (char *)arg,
 
 542                                         sizeof(struct mtop)))
 
 548                         myOp = VIOTAPOP_RESET;
 
 563                         myOp = VIOTAPOP_WEOF;
 
 575                         myOp = VIOTAPOP_ERASE;
 
 578                         myOp = VIOTAPOP_SETBLK;
 
 581                         myOp = VIOTAPOP_SETDENSITY;
 
 584                         myOp = VIOTAPOP_GETPOS;
 
 587                         myOp = VIOTAPOP_SETPOS;
 
 590                         myOp = VIOTAPOP_SETPART;
 
 593                         myOp = VIOTAPOP_UNLOAD;
 
 596                         printk(VIOTAPE_KERN_WARN "MTIOCTOP called "
 
 597                                         "with invalid op 0x%x\n", mtc.mt_op);
 
 602                  * if we moved the head, we are no longer
 
 613                         chg_state(devi.devno, VIOT_IDLE, file);
 
 616                 init_completion(&op->com);
 
 617                 hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
 
 618                                 HvLpEvent_Type_VirtualIo,
 
 619                                 viomajorsubtype_tape | viotapeop,
 
 620                                 HvLpEvent_AckInd_DoAck,
 
 621                                 HvLpEvent_AckType_ImmediateAck,
 
 622                                 viopath_sourceinst(viopath_hostLp),
 
 623                                 viopath_targetinst(viopath_hostLp),
 
 624                                 (u64)(unsigned long)op,
 
 626                                 ((u64)devi.devno << 48), 0,
 
 627                                 (((u64)myOp) << 32) | mtc.mt_count, 0);
 
 628                 if (hvrc != HvLpEvent_Rc_Good) {
 
 629                         printk(VIOTAPE_KERN_WARN "hv error on op %d\n",
 
 633                 wait_for_completion(&op->com);
 
 634                 ret = tape_rc_to_errno(op->rc, "tape operation", devi.devno);
 
 639                 init_completion(&op->com);
 
 640                 hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
 
 641                                 HvLpEvent_Type_VirtualIo,
 
 642                                 viomajorsubtype_tape | viotapegetstatus,
 
 643                                 HvLpEvent_AckInd_DoAck,
 
 644                                 HvLpEvent_AckType_ImmediateAck,
 
 645                                 viopath_sourceinst(viopath_hostLp),
 
 646                                 viopath_targetinst(viopath_hostLp),
 
 647                                 (u64)(unsigned long)op, VIOVERSION << 16,
 
 648                                 ((u64)devi.devno << 48), 0, 0, 0);
 
 649                 if (hvrc != HvLpEvent_Rc_Good) {
 
 650                         printk(VIOTAPE_KERN_WARN "hv error on op %d\n",
 
 654                 wait_for_completion(&op->com);
 
 656                 /* Operation is complete - grab the error code */
 
 657                 ret = tape_rc_to_errno(op->rc, "get status", devi.devno);
 
 661                 if ((ret == 0) && copy_to_user((void *)arg,
 
 662                                         &viomtget[devi.devno],
 
 663                                         sizeof(viomtget[0])))
 
 667                 printk(VIOTAPE_KERN_WARN "Got an (unsupported) MTIOCPOS\n");
 
 670                 printk(VIOTAPE_KERN_WARN "got an unsupported ioctl 0x%0x\n",
 
 681 static long viotap_unlocked_ioctl(struct file *file,
 
 682                 unsigned int cmd, unsigned long arg)
 
 687         rc = viotap_ioctl(file->f_path.dentry->d_inode, file, cmd, arg);
 
 692 static int viotap_open(struct inode *inode, struct file *file)
 
 695         struct viot_devinfo_struct devi;
 
 697         struct op_struct *op = get_op_struct();
 
 703         get_dev_info(file->f_path.dentry->d_inode, &devi);
 
 705         /* Note: We currently only support one mode! */
 
 706         if ((devi.devno >= viotape_numdev) || (devi.mode)) {
 
 711         init_completion(&op->com);
 
 713         hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
 
 714                         HvLpEvent_Type_VirtualIo,
 
 715                         viomajorsubtype_tape | viotapeopen,
 
 716                         HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck,
 
 717                         viopath_sourceinst(viopath_hostLp),
 
 718                         viopath_targetinst(viopath_hostLp),
 
 719                         (u64)(unsigned long)op, VIOVERSION << 16,
 
 720                         ((u64)devi.devno << 48), 0, 0, 0);
 
 722                 printk(VIOTAPE_KERN_WARN "bad rc on signalLpEvent %d\n",
 
 728         wait_for_completion(&op->com);
 
 729         ret = tape_rc_to_errno(op->rc, "open", devi.devno);
 
 738 static int viotap_release(struct inode *inode, struct file *file)
 
 741         struct viot_devinfo_struct devi;
 
 743         struct op_struct *op = get_op_struct();
 
 747         init_completion(&op->com);
 
 749         get_dev_info(file->f_path.dentry->d_inode, &devi);
 
 751         if (devi.devno >= viotape_numdev) {
 
 756         chg_state(devi.devno, VIOT_IDLE, file);
 
 759                 hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
 
 760                                 HvLpEvent_Type_VirtualIo,
 
 761                                 viomajorsubtype_tape | viotapeop,
 
 762                                 HvLpEvent_AckInd_DoAck,
 
 763                                 HvLpEvent_AckType_ImmediateAck,
 
 764                                 viopath_sourceinst(viopath_hostLp),
 
 765                                 viopath_targetinst(viopath_hostLp),
 
 766                                 (u64)(unsigned long)op, VIOVERSION << 16,
 
 767                                 ((u64)devi.devno << 48), 0,
 
 768                                 ((u64)VIOTAPOP_REW) << 32, 0);
 
 769                 wait_for_completion(&op->com);
 
 771                 tape_rc_to_errno(op->rc, "rewind", devi.devno);
 
 774         hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
 
 775                         HvLpEvent_Type_VirtualIo,
 
 776                         viomajorsubtype_tape | viotapeclose,
 
 777                         HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck,
 
 778                         viopath_sourceinst(viopath_hostLp),
 
 779                         viopath_targetinst(viopath_hostLp),
 
 780                         (u64)(unsigned long)op, VIOVERSION << 16,
 
 781                         ((u64)devi.devno << 48), 0, 0, 0);
 
 783                 printk(VIOTAPE_KERN_WARN "bad rc on signalLpEvent %d\n",
 
 789         wait_for_completion(&op->com);
 
 792                 printk(VIOTAPE_KERN_WARN "close failed\n");
 
 799 const struct file_operations viotap_fops = {
 
 800         .owner =                THIS_MODULE,
 
 802         .write =                viotap_write,
 
 803         .unlocked_ioctl =       viotap_unlocked_ioctl,
 
 805         .release =              viotap_release,
 
 808 /* Handle interrupt events for tape */
 
 809 static void vioHandleTapeEvent(struct HvLpEvent *event)
 
 812         struct op_struct *op;
 
 813         struct viotapelpevent *tevent = (struct viotapelpevent *)event;
 
 816                 /* Notification that a partition went away! */
 
 817                 if (!viopath_isactive(viopath_hostLp)) {
 
 823         tapeminor = event->xSubtype & VIOMINOR_SUBTYPE_MASK;
 
 824         op = (struct op_struct *)event->xCorrelationToken;
 
 828                 op->rc = tevent->sub_type_result;
 
 832                 op->rc = tevent->sub_type_result;
 
 833                 op->count = tevent->len;
 
 837                 if (op->non_blocking) {
 
 838                         dma_free_coherent(op->dev, op->count,
 
 839                                         op->buffer, op->dmaaddr);
 
 843                         op->rc = tevent->sub_type_result;
 
 844                         op->count = tevent->len;
 
 851         case viotapegetstatus:
 
 853                         op->count = tevent->u.op.count;
 
 854                         op->rc = tevent->sub_type_result;
 
 855                         if (!op->non_blocking)
 
 860                 printk(VIOTAPE_KERN_WARN "weird ack\n");
 
 864 static int viotape_probe(struct vio_dev *vdev, const struct vio_device_id *id)
 
 866         int i = vdev->unit_address;
 
 868         struct device_node *node = vdev->dev.archdata.of_node;
 
 870         if (i > VIOTAPE_MAX_TAPE)
 
 875         if (i >= viotape_numdev)
 
 876                 viotape_numdev = i + 1;
 
 878         tape_device[i] = &vdev->dev;
 
 879         viotape_unitinfo[i].rsrcname = of_get_property(node,
 
 880                                         "linux,vio_rsrcname", NULL);
 
 881         viotape_unitinfo[i].type = of_get_property(node, "linux,vio_type",
 
 883         viotape_unitinfo[i].model = of_get_property(node, "linux,vio_model",
 
 886         state[i].cur_part = 0;
 
 887         for (j = 0; j < MAX_PARTITIONS; ++j)
 
 888                 state[i].part_stat_rwi[j] = VIOT_IDLE;
 
 889         device_create_drvdata(tape_class, NULL, MKDEV(VIOTAPE_MAJOR, i),
 
 890                               NULL, "iseries!vt%d", i);
 
 891         device_create_drvdata(tape_class, NULL, MKDEV(VIOTAPE_MAJOR, i | 0x80),
 
 892                               NULL, "iseries!nvt%d", i);
 
 893         printk(VIOTAPE_KERN_INFO "tape iseries/vt%d is iSeries "
 
 894                         "resource %10.10s type %4.4s, model %3.3s\n",
 
 895                         i, viotape_unitinfo[i].rsrcname,
 
 896                         viotape_unitinfo[i].type, viotape_unitinfo[i].model);
 
 900 static int viotape_remove(struct vio_dev *vdev)
 
 902         int i = vdev->unit_address;
 
 904         device_destroy(tape_class, MKDEV(VIOTAPE_MAJOR, i | 0x80));
 
 905         device_destroy(tape_class, MKDEV(VIOTAPE_MAJOR, i));
 
 910  * viotape_device_table: Used by vio.c to match devices that we
 
 913 static struct vio_device_id viotape_device_table[] __devinitdata = {
 
 914         { "byte", "IBM,iSeries-viotape" },
 
 917 MODULE_DEVICE_TABLE(vio, viotape_device_table);
 
 919 static struct vio_driver viotape_driver = {
 
 920         .id_table = viotape_device_table,
 
 921         .probe = viotape_probe,
 
 922         .remove = viotape_remove,
 
 925                 .owner = THIS_MODULE,
 
 930 int __init viotap_init(void)
 
 934         if (!firmware_has_feature(FW_FEATURE_ISERIES))
 
 937         op_struct_list = NULL;
 
 938         if ((ret = add_op_structs(VIOTAPE_MAXREQ)) < 0) {
 
 939                 printk(VIOTAPE_KERN_WARN "couldn't allocate op structs\n");
 
 942         spin_lock_init(&op_struct_list_lock);
 
 944         sema_init(&reqSem, VIOTAPE_MAXREQ);
 
 946         if (viopath_hostLp == HvLpIndexInvalid) {
 
 948                 if (viopath_hostLp == HvLpIndexInvalid) {
 
 954         ret = viopath_open(viopath_hostLp, viomajorsubtype_tape,
 
 957                 printk(VIOTAPE_KERN_WARN
 
 958                                 "error on viopath_open to hostlp %d\n", ret);
 
 963         printk(VIOTAPE_KERN_INFO "vers " VIOTAPE_VERSION
 
 964                         ", hosting partition %d\n", viopath_hostLp);
 
 966         vio_setHandler(viomajorsubtype_tape, vioHandleTapeEvent);
 
 968         ret = register_chrdev(VIOTAPE_MAJOR, "viotape", &viotap_fops);
 
 970                 printk(VIOTAPE_KERN_WARN "Error registering viotape device\n");
 
 974         tape_class = class_create(THIS_MODULE, "tape");
 
 975         if (IS_ERR(tape_class)) {
 
 976                 printk(VIOTAPE_KERN_WARN "Unable to allocat class\n");
 
 977                 ret = PTR_ERR(tape_class);
 
 981         ret = vio_register_driver(&viotape_driver);
 
 985         proc_create("iSeries/viotape", S_IFREG|S_IRUGO, NULL,
 
 986                     &proc_viotape_operations);
 
 991         class_destroy(tape_class);
 
 993         unregister_chrdev(VIOTAPE_MAJOR, "viotape");
 
 995         vio_clearHandler(viomajorsubtype_tape);
 
 996         viopath_close(viopath_hostLp, viomajorsubtype_tape, VIOTAPE_MAXREQ + 2);
 
 998         clear_op_struct_pool();
 
1002 /* Give a new state to the tape object */
 
1003 static int chg_state(int index, unsigned char new_state, struct file *file)
 
1005         unsigned char *cur_state =
 
1006             &state[index].part_stat_rwi[state[index].cur_part];
 
1009         /* if the same state, don't bother */
 
1010         if (*cur_state == new_state)
 
1013         /* write an EOF if changing from writing to some other state */
 
1014         if (*cur_state == VIOT_WRITING) {
 
1015                 struct mtop write_eof = { MTWEOF, 1 };
 
1017                 rc = viotap_ioctl(NULL, file, MTIOCTOP,
 
1018                                   (unsigned long)&write_eof);
 
1020         *cur_state = new_state;
 
1025 static void __exit viotap_exit(void)
 
1027         remove_proc_entry("iSeries/viotape", NULL);
 
1028         vio_unregister_driver(&viotape_driver);
 
1029         class_destroy(tape_class);
 
1030         unregister_chrdev(VIOTAPE_MAJOR, "viotape");
 
1031         viopath_close(viopath_hostLp, viomajorsubtype_tape, VIOTAPE_MAXREQ + 2);
 
1032         vio_clearHandler(viomajorsubtype_tape);
 
1033         clear_op_struct_pool();
 
1036 MODULE_LICENSE("GPL");
 
1037 module_init(viotap_init);
 
1038 module_exit(viotap_exit);