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;
249 unsigned char part_stat_rwi[MAX_PARTITIONS];
250 } state[VIOTAPE_MAX_TAPE];
252 /* We single-thread */
253 static struct semaphore reqSem;
256 * When we send a request, we use this struct to get the response back
257 * from the interrupt handler
265 struct completion com;
267 struct op_struct *next;
270 static spinlock_t op_struct_list_lock;
271 static struct op_struct *op_struct_list;
273 /* forward declaration to resolve interdependence */
274 static int chg_state(int index, unsigned char new_state, struct file *file);
277 static int proc_viotape_show(struct seq_file *m, void *v)
281 seq_printf(m, "viotape driver version " VIOTAPE_VERSION "\n");
282 for (i = 0; i < viotape_numdev; i++) {
283 seq_printf(m, "viotape device %d is iSeries resource %10.10s"
284 "type %4.4s, model %3.3s\n",
285 i, viotape_unitinfo[i].rsrcname,
286 viotape_unitinfo[i].type,
287 viotape_unitinfo[i].model);
292 static int proc_viotape_open(struct inode *inode, struct file *file)
294 return single_open(file, proc_viotape_show, NULL);
297 static struct file_operations proc_viotape_operations = {
298 .open = proc_viotape_open,
301 .release = single_release,
304 /* Decode the device minor number into its parts */
305 void get_dev_info(struct inode *ino, struct viot_devinfo_struct *devi)
307 devi->devno = iminor(ino) & 0x1F;
308 devi->mode = (iminor(ino) & 0x60) >> 5;
309 /* if bit is set in the minor, do _not_ rewind automatically */
310 devi->rewind = (iminor(ino) & 0x80) == 0;
313 /* This is called only from the exit and init paths, so no need for locking */
314 static void clear_op_struct_pool(void)
316 while (op_struct_list) {
317 struct op_struct *toFree = op_struct_list;
318 op_struct_list = op_struct_list->next;
323 /* Likewise, this is only called from the init path */
324 static int add_op_structs(int structs)
328 for (i = 0; i < structs; ++i) {
329 struct op_struct *new_struct =
330 kmalloc(sizeof(*new_struct), GFP_KERNEL);
332 clear_op_struct_pool();
335 new_struct->next = op_struct_list;
336 op_struct_list = new_struct;
341 /* Allocate an op structure from our pool */
342 static struct op_struct *get_op_struct(void)
344 struct op_struct *retval;
347 spin_lock_irqsave(&op_struct_list_lock, flags);
348 retval = op_struct_list;
350 op_struct_list = retval->next;
351 spin_unlock_irqrestore(&op_struct_list_lock, flags);
353 memset(retval, 0, sizeof(*retval));
354 init_completion(&retval->com);
360 /* Return an op structure to our pool */
361 static void free_op_struct(struct op_struct *op_struct)
365 spin_lock_irqsave(&op_struct_list_lock, flags);
366 op_struct->next = op_struct_list;
367 op_struct_list = op_struct;
368 spin_unlock_irqrestore(&op_struct_list_lock, flags);
371 /* Map our tape return codes to errno values */
372 int tape_rc_to_errno(int tape_rc, char *operation, int tapeno)
374 const struct vio_error_entry *err;
379 err = vio_lookup_rc(viotape_err_table, tape_rc);
380 printk(VIOTAPE_KERN_WARN "error(%s) 0x%04x on Device %d (%-10s): %s\n",
381 operation, tape_rc, tapeno,
382 viotape_unitinfo[tapeno].rsrcname, err->msg);
386 /* Get info on all tapes from OS/400 */
387 static int get_viotape_info(void)
391 size_t len = sizeof(*viotape_unitinfo) * VIOTAPE_MAX_TAPE;
392 struct op_struct *op = get_op_struct();
397 viotape_unitinfo = dma_alloc_coherent(iSeries_vio_dev, len,
398 &viotape_unitinfo_token, GFP_ATOMIC);
399 if (viotape_unitinfo == NULL) {
404 memset(viotape_unitinfo, 0, len);
406 hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
407 HvLpEvent_Type_VirtualIo,
408 viomajorsubtype_tape | viotapegetinfo,
409 HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck,
410 viopath_sourceinst(viopath_hostLp),
411 viopath_targetinst(viopath_hostLp),
412 (u64) (unsigned long) op, VIOVERSION << 16,
413 viotape_unitinfo_token, len, 0, 0);
414 if (hvrc != HvLpEvent_Rc_Good) {
415 printk(VIOTAPE_KERN_WARN "hv error on op %d\n",
421 wait_for_completion(&op->com);
426 ((i < VIOTAPE_MAX_TAPE) && (viotape_unitinfo[i].rsrcname[0]));
434 static ssize_t viotap_write(struct file *file, const char *buf,
435 size_t count, loff_t * ppos)
438 unsigned short flags = file->f_flags;
439 int noblock = ((flags & O_NONBLOCK) != 0);
441 struct viot_devinfo_struct devi;
442 struct op_struct *op = get_op_struct();
447 get_dev_info(file->f_dentry->d_inode, &devi);
450 * We need to make sure we can send a request. We use
451 * a semaphore to keep track of # requests in use. If
452 * we are non-blocking, make sure we don't block on the
456 if (down_trylock(&reqSem)) {
463 /* Allocate a DMA buffer */
464 op->dev = tape_device[devi.devno];
465 op->buffer = dma_alloc_coherent(op->dev, count, &op->dmaaddr,
468 if (op->buffer == NULL) {
469 printk(VIOTAPE_KERN_WARN
470 "error allocating dma buffer for len %ld\n",
476 /* Copy the data into the buffer */
477 if (copy_from_user(op->buffer, buf, count)) {
478 printk(VIOTAPE_KERN_WARN "tape: error on copy from user\n");
483 op->non_blocking = noblock;
484 init_completion(&op->com);
487 hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
488 HvLpEvent_Type_VirtualIo,
489 viomajorsubtype_tape | viotapewrite,
490 HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck,
491 viopath_sourceinst(viopath_hostLp),
492 viopath_targetinst(viopath_hostLp),
493 (u64)(unsigned long)op, VIOVERSION << 16,
494 ((u64)devi.devno << 48) | op->dmaaddr, count, 0, 0);
495 if (hvrc != HvLpEvent_Rc_Good) {
496 printk(VIOTAPE_KERN_WARN "hv error on op %d\n",
505 wait_for_completion(&op->com);
508 ret = tape_rc_to_errno(op->rc, "write", devi.devno);
510 chg_state(devi.devno, VIOT_WRITING, file);
515 dma_free_coherent(op->dev, count, op->buffer, op->dmaaddr);
524 static ssize_t viotap_read(struct file *file, char *buf, size_t count,
528 unsigned short flags = file->f_flags;
529 struct op_struct *op = get_op_struct();
530 int noblock = ((flags & O_NONBLOCK) != 0);
532 struct viot_devinfo_struct devi;
537 get_dev_info(file->f_dentry->d_inode, &devi);
540 * We need to make sure we can send a request. We use
541 * a semaphore to keep track of # requests in use. If
542 * we are non-blocking, make sure we don't block on the
546 if (down_trylock(&reqSem)) {
553 chg_state(devi.devno, VIOT_READING, file);
555 /* Allocate a DMA buffer */
556 op->dev = tape_device[devi.devno];
557 op->buffer = dma_alloc_coherent(op->dev, count, &op->dmaaddr,
559 if (op->buffer == NULL) {
565 init_completion(&op->com);
567 hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
568 HvLpEvent_Type_VirtualIo,
569 viomajorsubtype_tape | viotaperead,
570 HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck,
571 viopath_sourceinst(viopath_hostLp),
572 viopath_targetinst(viopath_hostLp),
573 (u64)(unsigned long)op, VIOVERSION << 16,
574 ((u64)devi.devno << 48) | op->dmaaddr, count, 0, 0);
575 if (hvrc != HvLpEvent_Rc_Good) {
576 printk(VIOTAPE_KERN_WARN "tape hv error on op %d\n",
582 wait_for_completion(&op->com);
585 ret = tape_rc_to_errno(op->rc, "read", devi.devno);
588 if (ret && copy_to_user(buf, op->buffer, ret)) {
589 printk(VIOTAPE_KERN_WARN "error on copy_to_user\n");
595 dma_free_coherent(op->dev, count, op->buffer, op->dmaaddr);
604 static int viotap_ioctl(struct inode *inode, struct file *file,
605 unsigned int cmd, unsigned long arg)
609 struct viot_devinfo_struct devi;
612 struct op_struct *op = get_op_struct();
617 get_dev_info(file->f_dentry->d_inode, &devi);
627 * inode is null if and only if we (the kernel)
631 memcpy(&mtc, (void *) arg, sizeof(struct mtop));
632 else if (copy_from_user((char *)&mtc, (char *)arg,
633 sizeof(struct mtop)))
639 myOp = VIOTAPOP_RESET;
654 myOp = VIOTAPOP_WEOF;
666 myOp = VIOTAPOP_ERASE;
669 myOp = VIOTAPOP_SETBLK;
672 myOp = VIOTAPOP_SETDENSITY;
675 myOp = VIOTAPOP_GETPOS;
678 myOp = VIOTAPOP_SETPOS;
681 myOp = VIOTAPOP_SETPART;
684 myOp = VIOTAPOP_UNLOAD;
687 printk(VIOTAPE_KERN_WARN "MTIOCTOP called "
688 "with invalid op 0x%x\n", mtc.mt_op);
693 * if we moved the head, we are no longer
704 chg_state(devi.devno, VIOT_IDLE, file);
707 init_completion(&op->com);
708 hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
709 HvLpEvent_Type_VirtualIo,
710 viomajorsubtype_tape | viotapeop,
711 HvLpEvent_AckInd_DoAck,
712 HvLpEvent_AckType_ImmediateAck,
713 viopath_sourceinst(viopath_hostLp),
714 viopath_targetinst(viopath_hostLp),
715 (u64)(unsigned long)op,
717 ((u64)devi.devno << 48), 0,
718 (((u64)myOp) << 32) | mtc.mt_count, 0);
719 if (hvrc != HvLpEvent_Rc_Good) {
720 printk(VIOTAPE_KERN_WARN "hv error on op %d\n",
724 wait_for_completion(&op->com);
725 ret = tape_rc_to_errno(op->rc, "tape operation", devi.devno);
730 init_completion(&op->com);
731 hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
732 HvLpEvent_Type_VirtualIo,
733 viomajorsubtype_tape | viotapegetstatus,
734 HvLpEvent_AckInd_DoAck,
735 HvLpEvent_AckType_ImmediateAck,
736 viopath_sourceinst(viopath_hostLp),
737 viopath_targetinst(viopath_hostLp),
738 (u64)(unsigned long)op, VIOVERSION << 16,
739 ((u64)devi.devno << 48), 0, 0, 0);
740 if (hvrc != HvLpEvent_Rc_Good) {
741 printk(VIOTAPE_KERN_WARN "hv error on op %d\n",
745 wait_for_completion(&op->com);
747 /* Operation is complete - grab the error code */
748 ret = tape_rc_to_errno(op->rc, "get status", devi.devno);
752 if ((ret == 0) && copy_to_user((void *)arg,
753 &viomtget[devi.devno],
754 sizeof(viomtget[0])))
758 printk(VIOTAPE_KERN_WARN "Got an (unsupported) MTIOCPOS\n");
761 printk(VIOTAPE_KERN_WARN "got an unsupported ioctl 0x%0x\n",
772 static int viotap_open(struct inode *inode, struct file *file)
775 struct viot_devinfo_struct devi;
777 struct op_struct *op = get_op_struct();
782 get_dev_info(file->f_dentry->d_inode, &devi);
784 /* Note: We currently only support one mode! */
785 if ((devi.devno >= viotape_numdev) || (devi.mode)) {
790 init_completion(&op->com);
792 hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
793 HvLpEvent_Type_VirtualIo,
794 viomajorsubtype_tape | viotapeopen,
795 HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck,
796 viopath_sourceinst(viopath_hostLp),
797 viopath_targetinst(viopath_hostLp),
798 (u64)(unsigned long)op, VIOVERSION << 16,
799 ((u64)devi.devno << 48), 0, 0, 0);
801 printk(VIOTAPE_KERN_WARN "bad rc on signalLpEvent %d\n",
807 wait_for_completion(&op->com);
808 ret = tape_rc_to_errno(op->rc, "open", devi.devno);
816 static int viotap_release(struct inode *inode, struct file *file)
819 struct viot_devinfo_struct devi;
821 struct op_struct *op = get_op_struct();
825 init_completion(&op->com);
827 get_dev_info(file->f_dentry->d_inode, &devi);
829 if (devi.devno >= viotape_numdev) {
834 chg_state(devi.devno, VIOT_IDLE, file);
837 hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
838 HvLpEvent_Type_VirtualIo,
839 viomajorsubtype_tape | viotapeop,
840 HvLpEvent_AckInd_DoAck,
841 HvLpEvent_AckType_ImmediateAck,
842 viopath_sourceinst(viopath_hostLp),
843 viopath_targetinst(viopath_hostLp),
844 (u64)(unsigned long)op, VIOVERSION << 16,
845 ((u64)devi.devno << 48), 0,
846 ((u64)VIOTAPOP_REW) << 32, 0);
847 wait_for_completion(&op->com);
849 tape_rc_to_errno(op->rc, "rewind", devi.devno);
852 hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
853 HvLpEvent_Type_VirtualIo,
854 viomajorsubtype_tape | viotapeclose,
855 HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck,
856 viopath_sourceinst(viopath_hostLp),
857 viopath_targetinst(viopath_hostLp),
858 (u64)(unsigned long)op, VIOVERSION << 16,
859 ((u64)devi.devno << 48), 0, 0, 0);
861 printk(VIOTAPE_KERN_WARN "bad rc on signalLpEvent %d\n",
867 wait_for_completion(&op->com);
870 printk(VIOTAPE_KERN_WARN "close failed\n");
877 struct file_operations viotap_fops = {
883 release: viotap_release,
886 /* Handle interrupt events for tape */
887 static void vioHandleTapeEvent(struct HvLpEvent *event)
890 struct op_struct *op;
891 struct viotapelpevent *tevent = (struct viotapelpevent *)event;
894 /* Notification that a partition went away! */
895 if (!viopath_isactive(viopath_hostLp)) {
901 tapeminor = event->xSubtype & VIOMINOR_SUBTYPE_MASK;
902 op = (struct op_struct *)event->xCorrelationToken;
907 op->rc = tevent->sub_type_result;
911 op->rc = tevent->sub_type_result;
912 op->count = tevent->len;
916 if (op->non_blocking) {
917 dma_free_coherent(op->dev, op->count,
918 op->buffer, op->dmaaddr);
922 op->rc = tevent->sub_type_result;
923 op->count = tevent->len;
930 case viotapegetstatus:
932 op->count = tevent->u.op.count;
933 op->rc = tevent->sub_type_result;
934 if (!op->non_blocking)
939 printk(VIOTAPE_KERN_WARN "weird ack\n");
943 static int viotape_probe(struct vio_dev *vdev, const struct vio_device_id *id)
946 int i = vdev->unit_address;
949 if (i >= viotape_numdev)
952 tape_device[i] = &vdev->dev;
954 state[i].cur_part = 0;
955 for (j = 0; j < MAX_PARTITIONS; ++j)
956 state[i].part_stat_rwi[j] = VIOT_IDLE;
957 class_device_create(tape_class, NULL, MKDEV(VIOTAPE_MAJOR, i), NULL,
959 class_device_create(tape_class, NULL, MKDEV(VIOTAPE_MAJOR, i | 0x80),
960 NULL, "iseries!nvt%d", i);
961 sprintf(tapename, "iseries/vt%d", i);
962 printk(VIOTAPE_KERN_INFO "tape %s is iSeries "
963 "resource %10.10s type %4.4s, model %3.3s\n",
964 tapename, viotape_unitinfo[i].rsrcname,
965 viotape_unitinfo[i].type, viotape_unitinfo[i].model);
969 static int viotape_remove(struct vio_dev *vdev)
971 int i = vdev->unit_address;
973 class_device_destroy(tape_class, MKDEV(VIOTAPE_MAJOR, i | 0x80));
974 class_device_destroy(tape_class, MKDEV(VIOTAPE_MAJOR, i));
979 * viotape_device_table: Used by vio.c to match devices that we
982 static struct vio_device_id viotape_device_table[] __devinitdata = {
983 { "byte", "IBM,iSeries-viotape" },
986 MODULE_DEVICE_TABLE(vio, viotape_device_table);
988 static struct vio_driver viotape_driver = {
989 .id_table = viotape_device_table,
990 .probe = viotape_probe,
991 .remove = viotape_remove,
994 .owner = THIS_MODULE,
999 int __init viotap_init(void)
1002 struct proc_dir_entry *e;
1004 op_struct_list = NULL;
1005 if ((ret = add_op_structs(VIOTAPE_MAXREQ)) < 0) {
1006 printk(VIOTAPE_KERN_WARN "couldn't allocate op structs\n");
1009 spin_lock_init(&op_struct_list_lock);
1011 sema_init(&reqSem, VIOTAPE_MAXREQ);
1013 if (viopath_hostLp == HvLpIndexInvalid) {
1015 if (viopath_hostLp == HvLpIndexInvalid) {
1021 ret = viopath_open(viopath_hostLp, viomajorsubtype_tape,
1022 VIOTAPE_MAXREQ + 2);
1024 printk(VIOTAPE_KERN_WARN
1025 "error on viopath_open to hostlp %d\n", ret);
1030 printk(VIOTAPE_KERN_INFO "vers " VIOTAPE_VERSION
1031 ", hosting partition %d\n", viopath_hostLp);
1033 vio_setHandler(viomajorsubtype_tape, vioHandleTapeEvent);
1035 ret = register_chrdev(VIOTAPE_MAJOR, "viotape", &viotap_fops);
1037 printk(VIOTAPE_KERN_WARN "Error registering viotape device\n");
1041 tape_class = class_create(THIS_MODULE, "tape");
1042 if (IS_ERR(tape_class)) {
1043 printk(VIOTAPE_KERN_WARN "Unable to allocat class\n");
1044 ret = PTR_ERR(tape_class);
1048 if ((ret = get_viotape_info()) < 0) {
1049 printk(VIOTAPE_KERN_WARN "Unable to obtain virtual device information");
1053 ret = vio_register_driver(&viotape_driver);
1057 e = create_proc_entry("iSeries/viotape", S_IFREG|S_IRUGO, NULL);
1059 e->owner = THIS_MODULE;
1060 e->proc_fops = &proc_viotape_operations;
1066 class_destroy(tape_class);
1068 unregister_chrdev(VIOTAPE_MAJOR, "viotape");
1070 vio_clearHandler(viomajorsubtype_tape);
1071 viopath_close(viopath_hostLp, viomajorsubtype_tape, VIOTAPE_MAXREQ + 2);
1073 clear_op_struct_pool();
1077 /* Give a new state to the tape object */
1078 static int chg_state(int index, unsigned char new_state, struct file *file)
1080 unsigned char *cur_state =
1081 &state[index].part_stat_rwi[state[index].cur_part];
1084 /* if the same state, don't bother */
1085 if (*cur_state == new_state)
1088 /* write an EOF if changing from writing to some other state */
1089 if (*cur_state == VIOT_WRITING) {
1090 struct mtop write_eof = { MTWEOF, 1 };
1092 rc = viotap_ioctl(NULL, file, MTIOCTOP,
1093 (unsigned long)&write_eof);
1095 *cur_state = new_state;
1100 static void __exit viotap_exit(void)
1104 remove_proc_entry("iSeries/viotape", NULL);
1105 vio_unregister_driver(&viotape_driver);
1106 class_destroy(tape_class);
1107 ret = unregister_chrdev(VIOTAPE_MAJOR, "viotape");
1109 printk(VIOTAPE_KERN_WARN "Error unregistering device: %d\n",
1111 if (viotape_unitinfo)
1112 dma_free_coherent(iSeries_vio_dev,
1113 sizeof(viotape_unitinfo[0]) * VIOTAPE_MAX_TAPE,
1114 viotape_unitinfo, viotape_unitinfo_token);
1115 viopath_close(viopath_hostLp, viomajorsubtype_tape, VIOTAPE_MAXREQ + 2);
1116 vio_clearHandler(viomajorsubtype_tape);
1117 clear_op_struct_pool();
1120 MODULE_LICENSE("GPL");
1121 module_init(viotap_init);
1122 module_exit(viotap_exit);