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>
 
   9  *           Stephen Rothwell <sfr@au1.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/config.h>
 
  35 #include <linux/module.h>
 
  36 #include <linux/kernel.h>
 
  37 #include <linux/errno.h>
 
  38 #include <linux/init.h>
 
  39 #include <linux/wait.h>
 
  40 #include <linux/spinlock.h>
 
  41 #include <linux/mtio.h>
 
  42 #include <linux/device.h>
 
  43 #include <linux/dma-mapping.h>
 
  45 #include <linux/cdev.h>
 
  46 #include <linux/devfs_fs_kernel.h>
 
  47 #include <linux/major.h>
 
  48 #include <linux/completion.h>
 
  49 #include <linux/proc_fs.h>
 
  50 #include <linux/seq_file.h>
 
  52 #include <asm/uaccess.h>
 
  53 #include <asm/ioctls.h>
 
  56 #include <asm/iseries/vio.h>
 
  57 #include <asm/iseries/hv_lp_event.h>
 
  58 #include <asm/iseries/hv_call_event.h>
 
  59 #include <asm/iseries/hv_lp_config.h>
 
  61 #define VIOTAPE_VERSION         "1.2"
 
  62 #define VIOTAPE_MAXREQ          1
 
  64 #define VIOTAPE_KERN_WARN       KERN_WARNING "viotape: "
 
  65 #define VIOTAPE_KERN_INFO       KERN_INFO "viotape: "
 
  67 static int viotape_numdev;
 
  70  * The minor number follows the conventions of the SCSI tape drives.  The
 
  71  * rewind and mode are encoded in the minor #.  We use this struct to break
 
  74 struct viot_devinfo_struct {
 
  80 #define VIOTAPOP_RESET          0
 
  81 #define VIOTAPOP_FSF            1
 
  82 #define VIOTAPOP_BSF            2
 
  83 #define VIOTAPOP_FSR            3
 
  84 #define VIOTAPOP_BSR            4
 
  85 #define VIOTAPOP_WEOF           5
 
  86 #define VIOTAPOP_REW            6
 
  87 #define VIOTAPOP_NOP            7
 
  88 #define VIOTAPOP_EOM            8
 
  89 #define VIOTAPOP_ERASE          9
 
  90 #define VIOTAPOP_SETBLK        10
 
  91 #define VIOTAPOP_SETDENSITY    11
 
  92 #define VIOTAPOP_SETPOS        12
 
  93 #define VIOTAPOP_GETPOS        13
 
  94 #define VIOTAPOP_SETPART       14
 
  95 #define VIOTAPOP_UNLOAD        15
 
  97 struct viotapelpevent {
 
  98         struct HvLpEvent event;
 
 126 enum viotapesubtype {
 
 127         viotapeopen = 0x0001,
 
 128         viotapeclose = 0x0002,
 
 129         viotaperead = 0x0003,
 
 130         viotapewrite = 0x0004,
 
 131         viotapegetinfo = 0x0005,
 
 133         viotapegetpos = 0x0007,
 
 134         viotapesetpos = 0x0008,
 
 135         viotapegetstatus = 0x0009
 
 139         viotape_InvalidRange = 0x0601,
 
 140         viotape_InvalidToken = 0x0602,
 
 141         viotape_DMAError = 0x0603,
 
 142         viotape_UseError = 0x0604,
 
 143         viotape_ReleaseError = 0x0605,
 
 144         viotape_InvalidTape = 0x0606,
 
 145         viotape_InvalidOp = 0x0607,
 
 146         viotape_TapeErr = 0x0608,
 
 148         viotape_AllocTimedOut = 0x0640,
 
 149         viotape_BOTEnc = 0x0641,
 
 150         viotape_BlankTape = 0x0642,
 
 151         viotape_BufferEmpty = 0x0643,
 
 152         viotape_CleanCartFound = 0x0644,
 
 153         viotape_CmdNotAllowed = 0x0645,
 
 154         viotape_CmdNotSupported = 0x0646,
 
 155         viotape_DataCheck = 0x0647,
 
 156         viotape_DecompressErr = 0x0648,
 
 157         viotape_DeviceTimeout = 0x0649,
 
 158         viotape_DeviceUnavail = 0x064a,
 
 159         viotape_DeviceBusy = 0x064b,
 
 160         viotape_EndOfMedia = 0x064c,
 
 161         viotape_EndOfTape = 0x064d,
 
 162         viotape_EquipCheck = 0x064e,
 
 163         viotape_InsufficientRs = 0x064f,
 
 164         viotape_InvalidLogBlk = 0x0650,
 
 165         viotape_LengthError = 0x0651,
 
 166         viotape_LibDoorOpen = 0x0652,
 
 167         viotape_LoadFailure = 0x0653,
 
 168         viotape_NotCapable = 0x0654,
 
 169         viotape_NotOperational = 0x0655,
 
 170         viotape_NotReady = 0x0656,
 
 171         viotape_OpCancelled = 0x0657,
 
 172         viotape_PhyLinkErr = 0x0658,
 
 173         viotape_RdyNotBOT = 0x0659,
 
 174         viotape_TapeMark = 0x065a,
 
 175         viotape_WriteProt = 0x065b
 
 178 static const struct vio_error_entry viotape_err_table[] = {
 
 179         { viotape_InvalidRange, EIO, "Internal error" },
 
 180         { viotape_InvalidToken, EIO, "Internal error" },
 
 181         { viotape_DMAError, EIO, "DMA error" },
 
 182         { viotape_UseError, EIO, "Internal error" },
 
 183         { viotape_ReleaseError, EIO, "Internal error" },
 
 184         { viotape_InvalidTape, EIO, "Invalid tape device" },
 
 185         { viotape_InvalidOp, EIO, "Invalid operation" },
 
 186         { viotape_TapeErr, EIO, "Tape error" },
 
 187         { viotape_AllocTimedOut, EBUSY, "Allocate timed out" },
 
 188         { viotape_BOTEnc, EIO, "Beginning of tape encountered" },
 
 189         { viotape_BlankTape, EIO, "Blank tape" },
 
 190         { viotape_BufferEmpty, EIO, "Buffer empty" },
 
 191         { viotape_CleanCartFound, ENOMEDIUM, "Cleaning cartridge found" },
 
 192         { viotape_CmdNotAllowed, EIO, "Command not allowed" },
 
 193         { viotape_CmdNotSupported, EIO, "Command not supported" },
 
 194         { viotape_DataCheck, EIO, "Data check" },
 
 195         { viotape_DecompressErr, EIO, "Decompression error" },
 
 196         { viotape_DeviceTimeout, EBUSY, "Device timeout" },
 
 197         { viotape_DeviceUnavail, EIO, "Device unavailable" },
 
 198         { viotape_DeviceBusy, EBUSY, "Device busy" },
 
 199         { viotape_EndOfMedia, ENOSPC, "End of media" },
 
 200         { viotape_EndOfTape, ENOSPC, "End of tape" },
 
 201         { viotape_EquipCheck, EIO, "Equipment check" },
 
 202         { viotape_InsufficientRs, EOVERFLOW, "Insufficient tape resources" },
 
 203         { viotape_InvalidLogBlk, EIO, "Invalid logical block location" },
 
 204         { viotape_LengthError, EOVERFLOW, "Length error" },
 
 205         { viotape_LibDoorOpen, EBUSY, "Door open" },
 
 206         { viotape_LoadFailure, ENOMEDIUM, "Load failure" },
 
 207         { viotape_NotCapable, EIO, "Not capable" },
 
 208         { viotape_NotOperational, EIO, "Not operational" },
 
 209         { viotape_NotReady, EIO, "Not ready" },
 
 210         { viotape_OpCancelled, EIO, "Operation cancelled" },
 
 211         { viotape_PhyLinkErr, EIO, "Physical link error" },
 
 212         { viotape_RdyNotBOT, EIO, "Ready but not beginning of tape" },
 
 213         { viotape_TapeMark, EIO, "Tape mark" },
 
 214         { viotape_WriteProt, EROFS, "Write protection error" },
 
 218 /* Maximum number of tapes we support */
 
 219 #define VIOTAPE_MAX_TAPE        HVMAXARCHITECTEDVIRTUALTAPES
 
 220 #define MAX_PARTITIONS          4
 
 222 /* defines for current tape state */
 
 224 #define VIOT_READING            1
 
 225 #define VIOT_WRITING            2
 
 227 /* Our info on the tapes */
 
 234 static struct tape_descr *viotape_unitinfo;
 
 235 static dma_addr_t viotape_unitinfo_token;
 
 237 static struct mtget viomtget[VIOTAPE_MAX_TAPE];
 
 239 static struct class *tape_class;
 
 241 static struct device *tape_device[VIOTAPE_MAX_TAPE];
 
 244  * maintain the current state of each tape (and partition)
 
 245  * so that we know when to write EOF marks.
 
 248         unsigned char   cur_part;
 
 250         unsigned char   part_stat_rwi[MAX_PARTITIONS];
 
 251 } state[VIOTAPE_MAX_TAPE];
 
 253 /* We single-thread */
 
 254 static struct semaphore reqSem;
 
 257  * When we send a request, we use this struct to get the response back
 
 258  * from the interrupt handler
 
 266         struct completion       com;
 
 268         struct op_struct        *next;
 
 271 static spinlock_t       op_struct_list_lock;
 
 272 static struct op_struct *op_struct_list;
 
 274 /* forward declaration to resolve interdependence */
 
 275 static int chg_state(int index, unsigned char new_state, struct file *file);
 
 278 static int proc_viotape_show(struct seq_file *m, void *v)
 
 282         seq_printf(m, "viotape driver version " VIOTAPE_VERSION "\n");
 
 283         for (i = 0; i < viotape_numdev; i++) {
 
 284                 seq_printf(m, "viotape device %d is iSeries resource %10.10s"
 
 285                                 "type %4.4s, model %3.3s\n",
 
 286                                 i, viotape_unitinfo[i].rsrcname,
 
 287                                 viotape_unitinfo[i].type,
 
 288                                 viotape_unitinfo[i].model);
 
 293 static int proc_viotape_open(struct inode *inode, struct file *file)
 
 295         return single_open(file, proc_viotape_show, NULL);
 
 298 static struct file_operations proc_viotape_operations = {
 
 299         .open           = proc_viotape_open,
 
 302         .release        = single_release,
 
 305 /* Decode the device minor number into its parts */
 
 306 void get_dev_info(struct inode *ino, struct viot_devinfo_struct *devi)
 
 308         devi->devno = iminor(ino) & 0x1F;
 
 309         devi->mode = (iminor(ino) & 0x60) >> 5;
 
 310         /* if bit is set in the minor, do _not_ rewind automatically */
 
 311         devi->rewind = (iminor(ino) & 0x80) == 0;
 
 314 /* This is called only from the exit and init paths, so no need for locking */
 
 315 static void clear_op_struct_pool(void)
 
 317         while (op_struct_list) {
 
 318                 struct op_struct *toFree = op_struct_list;
 
 319                 op_struct_list = op_struct_list->next;
 
 324 /* Likewise, this is only called from the init path */
 
 325 static int add_op_structs(int structs)
 
 329         for (i = 0; i < structs; ++i) {
 
 330                 struct op_struct *new_struct =
 
 331                         kmalloc(sizeof(*new_struct), GFP_KERNEL);
 
 333                         clear_op_struct_pool();
 
 336                 new_struct->next = op_struct_list;
 
 337                 op_struct_list = new_struct;
 
 342 /* Allocate an op structure from our pool */
 
 343 static struct op_struct *get_op_struct(void)
 
 345         struct op_struct *retval;
 
 348         spin_lock_irqsave(&op_struct_list_lock, flags);
 
 349         retval = op_struct_list;
 
 351                 op_struct_list = retval->next;
 
 352         spin_unlock_irqrestore(&op_struct_list_lock, flags);
 
 354                 memset(retval, 0, sizeof(*retval));
 
 355                 init_completion(&retval->com);
 
 361 /* Return an op structure to our pool */
 
 362 static void free_op_struct(struct op_struct *op_struct)
 
 366         spin_lock_irqsave(&op_struct_list_lock, flags);
 
 367         op_struct->next = op_struct_list;
 
 368         op_struct_list = op_struct;
 
 369         spin_unlock_irqrestore(&op_struct_list_lock, flags);
 
 372 /* Map our tape return codes to errno values */
 
 373 int tape_rc_to_errno(int tape_rc, char *operation, int tapeno)
 
 375         const struct vio_error_entry *err;
 
 380         err = vio_lookup_rc(viotape_err_table, tape_rc);
 
 381         printk(VIOTAPE_KERN_WARN "error(%s) 0x%04x on Device %d (%-10s): %s\n",
 
 382                         operation, tape_rc, tapeno,
 
 383                         viotape_unitinfo[tapeno].rsrcname, err->msg);
 
 387 /* Get info on all tapes from OS/400 */
 
 388 static int get_viotape_info(void)
 
 392         size_t len = sizeof(*viotape_unitinfo) * VIOTAPE_MAX_TAPE;
 
 393         struct op_struct *op = get_op_struct();
 
 398         viotape_unitinfo = dma_alloc_coherent(iSeries_vio_dev, len,
 
 399                 &viotape_unitinfo_token, GFP_ATOMIC);
 
 400         if (viotape_unitinfo == NULL) {
 
 405         memset(viotape_unitinfo, 0, len);
 
 407         hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
 
 408                         HvLpEvent_Type_VirtualIo,
 
 409                         viomajorsubtype_tape | viotapegetinfo,
 
 410                         HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck,
 
 411                         viopath_sourceinst(viopath_hostLp),
 
 412                         viopath_targetinst(viopath_hostLp),
 
 413                         (u64) (unsigned long) op, VIOVERSION << 16,
 
 414                         viotape_unitinfo_token, len, 0, 0);
 
 415         if (hvrc != HvLpEvent_Rc_Good) {
 
 416                 printk(VIOTAPE_KERN_WARN "hv error on op %d\n",
 
 422         wait_for_completion(&op->com);
 
 427              ((i < VIOTAPE_MAX_TAPE) && (viotape_unitinfo[i].rsrcname[0]));
 
 435 static ssize_t viotap_write(struct file *file, const char *buf,
 
 436                 size_t count, loff_t * ppos)
 
 439         unsigned short flags = file->f_flags;
 
 440         int noblock = ((flags & O_NONBLOCK) != 0);
 
 442         struct viot_devinfo_struct devi;
 
 443         struct op_struct *op = get_op_struct();
 
 448         get_dev_info(file->f_dentry->d_inode, &devi);
 
 451          * We need to make sure we can send a request.  We use
 
 452          * a semaphore to keep track of # requests in use.  If
 
 453          * we are non-blocking, make sure we don't block on the
 
 457                 if (down_trylock(&reqSem)) {
 
 464         /* Allocate a DMA buffer */
 
 465         op->dev = tape_device[devi.devno];
 
 466         op->buffer = dma_alloc_coherent(op->dev, count, &op->dmaaddr,
 
 469         if (op->buffer == NULL) {
 
 470                 printk(VIOTAPE_KERN_WARN
 
 471                                 "error allocating dma buffer for len %ld\n",
 
 477         /* Copy the data into the buffer */
 
 478         if (copy_from_user(op->buffer, buf, count)) {
 
 479                 printk(VIOTAPE_KERN_WARN "tape: error on copy from user\n");
 
 484         op->non_blocking = noblock;
 
 485         init_completion(&op->com);
 
 488         hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
 
 489                         HvLpEvent_Type_VirtualIo,
 
 490                         viomajorsubtype_tape | viotapewrite,
 
 491                         HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck,
 
 492                         viopath_sourceinst(viopath_hostLp),
 
 493                         viopath_targetinst(viopath_hostLp),
 
 494                         (u64)(unsigned long)op, VIOVERSION << 16,
 
 495                         ((u64)devi.devno << 48) | op->dmaaddr, count, 0, 0);
 
 496         if (hvrc != HvLpEvent_Rc_Good) {
 
 497                 printk(VIOTAPE_KERN_WARN "hv error on op %d\n",
 
 506         wait_for_completion(&op->com);
 
 509                 ret = tape_rc_to_errno(op->rc, "write", devi.devno);
 
 511                 chg_state(devi.devno, VIOT_WRITING, file);
 
 516         dma_free_coherent(op->dev, count, op->buffer, op->dmaaddr);
 
 525 static ssize_t viotap_read(struct file *file, char *buf, size_t count,
 
 529         unsigned short flags = file->f_flags;
 
 530         struct op_struct *op = get_op_struct();
 
 531         int noblock = ((flags & O_NONBLOCK) != 0);
 
 533         struct viot_devinfo_struct devi;
 
 538         get_dev_info(file->f_dentry->d_inode, &devi);
 
 541          * We need to make sure we can send a request.  We use
 
 542          * a semaphore to keep track of # requests in use.  If
 
 543          * we are non-blocking, make sure we don't block on the
 
 547                 if (down_trylock(&reqSem)) {
 
 554         chg_state(devi.devno, VIOT_READING, file);
 
 556         /* Allocate a DMA buffer */
 
 557         op->dev = tape_device[devi.devno];
 
 558         op->buffer = dma_alloc_coherent(op->dev, count, &op->dmaaddr,
 
 560         if (op->buffer == NULL) {
 
 566         init_completion(&op->com);
 
 568         hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
 
 569                         HvLpEvent_Type_VirtualIo,
 
 570                         viomajorsubtype_tape | viotaperead,
 
 571                         HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck,
 
 572                         viopath_sourceinst(viopath_hostLp),
 
 573                         viopath_targetinst(viopath_hostLp),
 
 574                         (u64)(unsigned long)op, VIOVERSION << 16,
 
 575                         ((u64)devi.devno << 48) | op->dmaaddr, count, 0, 0);
 
 576         if (hvrc != HvLpEvent_Rc_Good) {
 
 577                 printk(VIOTAPE_KERN_WARN "tape hv error on op %d\n",
 
 583         wait_for_completion(&op->com);
 
 586                 ret = tape_rc_to_errno(op->rc, "read", devi.devno);
 
 589                 if (ret && copy_to_user(buf, op->buffer, ret)) {
 
 590                         printk(VIOTAPE_KERN_WARN "error on copy_to_user\n");
 
 596         dma_free_coherent(op->dev, count, op->buffer, op->dmaaddr);
 
 605 static int viotap_ioctl(struct inode *inode, struct file *file,
 
 606                 unsigned int cmd, unsigned long arg)
 
 610         struct viot_devinfo_struct devi;
 
 613         struct op_struct *op = get_op_struct();
 
 618         get_dev_info(file->f_dentry->d_inode, &devi);
 
 628                  * inode is null if and only if we (the kernel)
 
 632                         memcpy(&mtc, (void *) arg, sizeof(struct mtop));
 
 633                 else if (copy_from_user((char *)&mtc, (char *)arg,
 
 634                                         sizeof(struct mtop)))
 
 640                         myOp = VIOTAPOP_RESET;
 
 655                         myOp = VIOTAPOP_WEOF;
 
 667                         myOp = VIOTAPOP_ERASE;
 
 670                         myOp = VIOTAPOP_SETBLK;
 
 673                         myOp = VIOTAPOP_SETDENSITY;
 
 676                         myOp = VIOTAPOP_GETPOS;
 
 679                         myOp = VIOTAPOP_SETPOS;
 
 682                         myOp = VIOTAPOP_SETPART;
 
 685                         myOp = VIOTAPOP_UNLOAD;
 
 688                         printk(VIOTAPE_KERN_WARN "MTIOCTOP called "
 
 689                                         "with invalid op 0x%x\n", mtc.mt_op);
 
 694                  * if we moved the head, we are no longer
 
 705                         chg_state(devi.devno, VIOT_IDLE, file);
 
 708                 init_completion(&op->com);
 
 709                 hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
 
 710                                 HvLpEvent_Type_VirtualIo,
 
 711                                 viomajorsubtype_tape | viotapeop,
 
 712                                 HvLpEvent_AckInd_DoAck,
 
 713                                 HvLpEvent_AckType_ImmediateAck,
 
 714                                 viopath_sourceinst(viopath_hostLp),
 
 715                                 viopath_targetinst(viopath_hostLp),
 
 716                                 (u64)(unsigned long)op,
 
 718                                 ((u64)devi.devno << 48), 0,
 
 719                                 (((u64)myOp) << 32) | mtc.mt_count, 0);
 
 720                 if (hvrc != HvLpEvent_Rc_Good) {
 
 721                         printk(VIOTAPE_KERN_WARN "hv error on op %d\n",
 
 725                 wait_for_completion(&op->com);
 
 726                 ret = tape_rc_to_errno(op->rc, "tape operation", devi.devno);
 
 731                 init_completion(&op->com);
 
 732                 hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
 
 733                                 HvLpEvent_Type_VirtualIo,
 
 734                                 viomajorsubtype_tape | viotapegetstatus,
 
 735                                 HvLpEvent_AckInd_DoAck,
 
 736                                 HvLpEvent_AckType_ImmediateAck,
 
 737                                 viopath_sourceinst(viopath_hostLp),
 
 738                                 viopath_targetinst(viopath_hostLp),
 
 739                                 (u64)(unsigned long)op, VIOVERSION << 16,
 
 740                                 ((u64)devi.devno << 48), 0, 0, 0);
 
 741                 if (hvrc != HvLpEvent_Rc_Good) {
 
 742                         printk(VIOTAPE_KERN_WARN "hv error on op %d\n",
 
 746                 wait_for_completion(&op->com);
 
 748                 /* Operation is complete - grab the error code */
 
 749                 ret = tape_rc_to_errno(op->rc, "get status", devi.devno);
 
 753                 if ((ret == 0) && copy_to_user((void *)arg,
 
 754                                         &viomtget[devi.devno],
 
 755                                         sizeof(viomtget[0])))
 
 759                 printk(VIOTAPE_KERN_WARN "Got an (unsupported) MTIOCPOS\n");
 
 762                 printk(VIOTAPE_KERN_WARN "got an unsupported ioctl 0x%0x\n",
 
 773 static int viotap_open(struct inode *inode, struct file *file)
 
 776         struct viot_devinfo_struct devi;
 
 778         struct op_struct *op = get_op_struct();
 
 783         get_dev_info(file->f_dentry->d_inode, &devi);
 
 785         /* Note: We currently only support one mode! */
 
 786         if ((devi.devno >= viotape_numdev) || (devi.mode)) {
 
 791         init_completion(&op->com);
 
 793         hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
 
 794                         HvLpEvent_Type_VirtualIo,
 
 795                         viomajorsubtype_tape | viotapeopen,
 
 796                         HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck,
 
 797                         viopath_sourceinst(viopath_hostLp),
 
 798                         viopath_targetinst(viopath_hostLp),
 
 799                         (u64)(unsigned long)op, VIOVERSION << 16,
 
 800                         ((u64)devi.devno << 48), 0, 0, 0);
 
 802                 printk(VIOTAPE_KERN_WARN "bad rc on signalLpEvent %d\n",
 
 808         wait_for_completion(&op->com);
 
 809         ret = tape_rc_to_errno(op->rc, "open", devi.devno);
 
 817 static int viotap_release(struct inode *inode, struct file *file)
 
 820         struct viot_devinfo_struct devi;
 
 822         struct op_struct *op = get_op_struct();
 
 826         init_completion(&op->com);
 
 828         get_dev_info(file->f_dentry->d_inode, &devi);
 
 830         if (devi.devno >= viotape_numdev) {
 
 835         chg_state(devi.devno, VIOT_IDLE, file);
 
 838                 hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
 
 839                                 HvLpEvent_Type_VirtualIo,
 
 840                                 viomajorsubtype_tape | viotapeop,
 
 841                                 HvLpEvent_AckInd_DoAck,
 
 842                                 HvLpEvent_AckType_ImmediateAck,
 
 843                                 viopath_sourceinst(viopath_hostLp),
 
 844                                 viopath_targetinst(viopath_hostLp),
 
 845                                 (u64)(unsigned long)op, VIOVERSION << 16,
 
 846                                 ((u64)devi.devno << 48), 0,
 
 847                                 ((u64)VIOTAPOP_REW) << 32, 0);
 
 848                 wait_for_completion(&op->com);
 
 850                 tape_rc_to_errno(op->rc, "rewind", devi.devno);
 
 853         hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
 
 854                         HvLpEvent_Type_VirtualIo,
 
 855                         viomajorsubtype_tape | viotapeclose,
 
 856                         HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck,
 
 857                         viopath_sourceinst(viopath_hostLp),
 
 858                         viopath_targetinst(viopath_hostLp),
 
 859                         (u64)(unsigned long)op, VIOVERSION << 16,
 
 860                         ((u64)devi.devno << 48), 0, 0, 0);
 
 862                 printk(VIOTAPE_KERN_WARN "bad rc on signalLpEvent %d\n",
 
 868         wait_for_completion(&op->com);
 
 871                 printk(VIOTAPE_KERN_WARN "close failed\n");
 
 878 struct file_operations viotap_fops = {
 
 884         release: viotap_release,
 
 887 /* Handle interrupt events for tape */
 
 888 static void vioHandleTapeEvent(struct HvLpEvent *event)
 
 891         struct op_struct *op;
 
 892         struct viotapelpevent *tevent = (struct viotapelpevent *)event;
 
 895                 /* Notification that a partition went away! */
 
 896                 if (!viopath_isactive(viopath_hostLp)) {
 
 902         tapeminor = event->xSubtype & VIOMINOR_SUBTYPE_MASK;
 
 903         op = (struct op_struct *)event->xCorrelationToken;
 
 908                 op->rc = tevent->sub_type_result;
 
 912                 op->rc = tevent->sub_type_result;
 
 913                 op->count = tevent->len;
 
 917                 if (op->non_blocking) {
 
 918                         dma_free_coherent(op->dev, op->count,
 
 919                                         op->buffer, op->dmaaddr);
 
 923                         op->rc = tevent->sub_type_result;
 
 924                         op->count = tevent->len;
 
 931         case viotapegetstatus:
 
 933                         op->count = tevent->u.op.count;
 
 934                         op->rc = tevent->sub_type_result;
 
 935                         if (!op->non_blocking)
 
 940                 printk(VIOTAPE_KERN_WARN "weird ack\n");
 
 944 static int viotape_probe(struct vio_dev *vdev, const struct vio_device_id *id)
 
 947         int i = vdev->unit_address;
 
 950         if (i >= viotape_numdev)
 
 953         tape_device[i] = &vdev->dev;
 
 955         state[i].cur_part = 0;
 
 956         for (j = 0; j < MAX_PARTITIONS; ++j)
 
 957                 state[i].part_stat_rwi[j] = VIOT_IDLE;
 
 958         class_device_create(tape_class, NULL, MKDEV(VIOTAPE_MAJOR, i), NULL,
 
 960         class_device_create(tape_class, NULL, MKDEV(VIOTAPE_MAJOR, i | 0x80),
 
 961                         NULL, "iseries!nvt%d", i);
 
 962         devfs_mk_cdev(MKDEV(VIOTAPE_MAJOR, i), S_IFCHR | S_IRUSR | S_IWUSR,
 
 964         devfs_mk_cdev(MKDEV(VIOTAPE_MAJOR, i | 0x80),
 
 965                         S_IFCHR | S_IRUSR | S_IWUSR, "iseries/nvt%d", i);
 
 966         sprintf(tapename, "iseries/vt%d", i);
 
 967         state[i].dev_handle = devfs_register_tape(tapename);
 
 968         printk(VIOTAPE_KERN_INFO "tape %s is iSeries "
 
 969                         "resource %10.10s type %4.4s, model %3.3s\n",
 
 970                         tapename, viotape_unitinfo[i].rsrcname,
 
 971                         viotape_unitinfo[i].type, viotape_unitinfo[i].model);
 
 975 static int viotape_remove(struct vio_dev *vdev)
 
 977         int i = vdev->unit_address;
 
 979         devfs_remove("iseries/nvt%d", i);
 
 980         devfs_remove("iseries/vt%d", i);
 
 981         devfs_unregister_tape(state[i].dev_handle);
 
 982         class_device_destroy(tape_class, MKDEV(VIOTAPE_MAJOR, i | 0x80));
 
 983         class_device_destroy(tape_class, MKDEV(VIOTAPE_MAJOR, i));
 
 988  * viotape_device_table: Used by vio.c to match devices that we
 
 991 static struct vio_device_id viotape_device_table[] __devinitdata = {
 
 995 MODULE_DEVICE_TABLE(vio, viotape_device_table);
 
 997 static struct vio_driver viotape_driver = {
 
 998         .id_table = viotape_device_table,
 
 999         .probe = viotape_probe,
 
1000         .remove = viotape_remove,
 
1003                 .owner = THIS_MODULE,
 
1008 int __init viotap_init(void)
 
1011         struct proc_dir_entry *e;
 
1013         op_struct_list = NULL;
 
1014         if ((ret = add_op_structs(VIOTAPE_MAXREQ)) < 0) {
 
1015                 printk(VIOTAPE_KERN_WARN "couldn't allocate op structs\n");
 
1018         spin_lock_init(&op_struct_list_lock);
 
1020         sema_init(&reqSem, VIOTAPE_MAXREQ);
 
1022         if (viopath_hostLp == HvLpIndexInvalid) {
 
1024                 if (viopath_hostLp == HvLpIndexInvalid) {
 
1030         ret = viopath_open(viopath_hostLp, viomajorsubtype_tape,
 
1031                         VIOTAPE_MAXREQ + 2);
 
1033                 printk(VIOTAPE_KERN_WARN
 
1034                                 "error on viopath_open to hostlp %d\n", ret);
 
1039         printk(VIOTAPE_KERN_INFO "vers " VIOTAPE_VERSION
 
1040                         ", hosting partition %d\n", viopath_hostLp);
 
1042         vio_setHandler(viomajorsubtype_tape, vioHandleTapeEvent);
 
1044         ret = register_chrdev(VIOTAPE_MAJOR, "viotape", &viotap_fops);
 
1046                 printk(VIOTAPE_KERN_WARN "Error registering viotape device\n");
 
1050         tape_class = class_create(THIS_MODULE, "tape");
 
1051         if (IS_ERR(tape_class)) {
 
1052                 printk(VIOTAPE_KERN_WARN "Unable to allocat class\n");
 
1053                 ret = PTR_ERR(tape_class);
 
1057         if ((ret = get_viotape_info()) < 0) {
 
1058                 printk(VIOTAPE_KERN_WARN "Unable to obtain virtual device information");
 
1062         ret = vio_register_driver(&viotape_driver);
 
1066         e = create_proc_entry("iSeries/viotape", S_IFREG|S_IRUGO, NULL);
 
1068                 e->owner = THIS_MODULE;
 
1069                 e->proc_fops = &proc_viotape_operations;
 
1075         class_destroy(tape_class);
 
1077         unregister_chrdev(VIOTAPE_MAJOR, "viotape");
 
1079         vio_clearHandler(viomajorsubtype_tape);
 
1080         viopath_close(viopath_hostLp, viomajorsubtype_tape, VIOTAPE_MAXREQ + 2);
 
1082         clear_op_struct_pool();
 
1086 /* Give a new state to the tape object */
 
1087 static int chg_state(int index, unsigned char new_state, struct file *file)
 
1089         unsigned char *cur_state =
 
1090             &state[index].part_stat_rwi[state[index].cur_part];
 
1093         /* if the same state, don't bother */
 
1094         if (*cur_state == new_state)
 
1097         /* write an EOF if changing from writing to some other state */
 
1098         if (*cur_state == VIOT_WRITING) {
 
1099                 struct mtop write_eof = { MTWEOF, 1 };
 
1101                 rc = viotap_ioctl(NULL, file, MTIOCTOP,
 
1102                                   (unsigned long)&write_eof);
 
1104         *cur_state = new_state;
 
1109 static void __exit viotap_exit(void)
 
1113         remove_proc_entry("iSeries/viotape", NULL);
 
1114         vio_unregister_driver(&viotape_driver);
 
1115         class_destroy(tape_class);
 
1116         ret = unregister_chrdev(VIOTAPE_MAJOR, "viotape");
 
1118                 printk(VIOTAPE_KERN_WARN "Error unregistering device: %d\n",
 
1120         if (viotape_unitinfo)
 
1121                 dma_free_coherent(iSeries_vio_dev,
 
1122                                 sizeof(viotape_unitinfo[0]) * VIOTAPE_MAX_TAPE,
 
1123                                 viotape_unitinfo, viotape_unitinfo_token);
 
1124         viopath_close(viopath_hostLp, viomajorsubtype_tape, VIOTAPE_MAXREQ + 2);
 
1125         vio_clearHandler(viomajorsubtype_tape);
 
1126         clear_op_struct_pool();
 
1129 MODULE_LICENSE("GPL");
 
1130 module_init(viotap_init);
 
1131 module_exit(viotap_exit);