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/major.h>
47 #include <linux/completion.h>
48 #include <linux/proc_fs.h>
49 #include <linux/seq_file.h>
51 #include <asm/uaccess.h>
52 #include <asm/ioctls.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
96 struct viotapelpevent {
97 struct HvLpEvent event;
125 enum viotapesubtype {
126 viotapeopen = 0x0001,
127 viotapeclose = 0x0002,
128 viotaperead = 0x0003,
129 viotapewrite = 0x0004,
130 viotapegetinfo = 0x0005,
132 viotapegetpos = 0x0007,
133 viotapesetpos = 0x0008,
134 viotapegetstatus = 0x0009
138 viotape_InvalidRange = 0x0601,
139 viotape_InvalidToken = 0x0602,
140 viotape_DMAError = 0x0603,
141 viotape_UseError = 0x0604,
142 viotape_ReleaseError = 0x0605,
143 viotape_InvalidTape = 0x0606,
144 viotape_InvalidOp = 0x0607,
145 viotape_TapeErr = 0x0608,
147 viotape_AllocTimedOut = 0x0640,
148 viotape_BOTEnc = 0x0641,
149 viotape_BlankTape = 0x0642,
150 viotape_BufferEmpty = 0x0643,
151 viotape_CleanCartFound = 0x0644,
152 viotape_CmdNotAllowed = 0x0645,
153 viotape_CmdNotSupported = 0x0646,
154 viotape_DataCheck = 0x0647,
155 viotape_DecompressErr = 0x0648,
156 viotape_DeviceTimeout = 0x0649,
157 viotape_DeviceUnavail = 0x064a,
158 viotape_DeviceBusy = 0x064b,
159 viotape_EndOfMedia = 0x064c,
160 viotape_EndOfTape = 0x064d,
161 viotape_EquipCheck = 0x064e,
162 viotape_InsufficientRs = 0x064f,
163 viotape_InvalidLogBlk = 0x0650,
164 viotape_LengthError = 0x0651,
165 viotape_LibDoorOpen = 0x0652,
166 viotape_LoadFailure = 0x0653,
167 viotape_NotCapable = 0x0654,
168 viotape_NotOperational = 0x0655,
169 viotape_NotReady = 0x0656,
170 viotape_OpCancelled = 0x0657,
171 viotape_PhyLinkErr = 0x0658,
172 viotape_RdyNotBOT = 0x0659,
173 viotape_TapeMark = 0x065a,
174 viotape_WriteProt = 0x065b
177 static const struct vio_error_entry viotape_err_table[] = {
178 { viotape_InvalidRange, EIO, "Internal error" },
179 { viotape_InvalidToken, EIO, "Internal error" },
180 { viotape_DMAError, EIO, "DMA error" },
181 { viotape_UseError, EIO, "Internal error" },
182 { viotape_ReleaseError, EIO, "Internal error" },
183 { viotape_InvalidTape, EIO, "Invalid tape device" },
184 { viotape_InvalidOp, EIO, "Invalid operation" },
185 { viotape_TapeErr, EIO, "Tape error" },
186 { viotape_AllocTimedOut, EBUSY, "Allocate timed out" },
187 { viotape_BOTEnc, EIO, "Beginning of tape encountered" },
188 { viotape_BlankTape, EIO, "Blank tape" },
189 { viotape_BufferEmpty, EIO, "Buffer empty" },
190 { viotape_CleanCartFound, ENOMEDIUM, "Cleaning cartridge found" },
191 { viotape_CmdNotAllowed, EIO, "Command not allowed" },
192 { viotape_CmdNotSupported, EIO, "Command not supported" },
193 { viotape_DataCheck, EIO, "Data check" },
194 { viotape_DecompressErr, EIO, "Decompression error" },
195 { viotape_DeviceTimeout, EBUSY, "Device timeout" },
196 { viotape_DeviceUnavail, EIO, "Device unavailable" },
197 { viotape_DeviceBusy, EBUSY, "Device busy" },
198 { viotape_EndOfMedia, ENOSPC, "End of media" },
199 { viotape_EndOfTape, ENOSPC, "End of tape" },
200 { viotape_EquipCheck, EIO, "Equipment check" },
201 { viotape_InsufficientRs, EOVERFLOW, "Insufficient tape resources" },
202 { viotape_InvalidLogBlk, EIO, "Invalid logical block location" },
203 { viotape_LengthError, EOVERFLOW, "Length error" },
204 { viotape_LibDoorOpen, EBUSY, "Door open" },
205 { viotape_LoadFailure, ENOMEDIUM, "Load failure" },
206 { viotape_NotCapable, EIO, "Not capable" },
207 { viotape_NotOperational, EIO, "Not operational" },
208 { viotape_NotReady, EIO, "Not ready" },
209 { viotape_OpCancelled, EIO, "Operation cancelled" },
210 { viotape_PhyLinkErr, EIO, "Physical link error" },
211 { viotape_RdyNotBOT, EIO, "Ready but not beginning of tape" },
212 { viotape_TapeMark, EIO, "Tape mark" },
213 { viotape_WriteProt, EROFS, "Write protection error" },
217 /* Maximum number of tapes we support */
218 #define VIOTAPE_MAX_TAPE HVMAXARCHITECTEDVIRTUALTAPES
219 #define MAX_PARTITIONS 4
221 /* defines for current tape state */
223 #define VIOT_READING 1
224 #define VIOT_WRITING 2
226 /* Our info on the tapes */
233 static struct tape_descr *viotape_unitinfo;
234 static dma_addr_t viotape_unitinfo_token;
236 static struct mtget viomtget[VIOTAPE_MAX_TAPE];
238 static struct class *tape_class;
240 static struct device *tape_device[VIOTAPE_MAX_TAPE];
243 * maintain the current state of each tape (and partition)
244 * so that we know when to write EOF marks.
247 unsigned char cur_part;
248 unsigned char part_stat_rwi[MAX_PARTITIONS];
249 } state[VIOTAPE_MAX_TAPE];
251 /* We single-thread */
252 static struct semaphore reqSem;
255 * When we send a request, we use this struct to get the response back
256 * from the interrupt handler
264 struct completion com;
266 struct op_struct *next;
269 static spinlock_t op_struct_list_lock;
270 static struct op_struct *op_struct_list;
272 /* forward declaration to resolve interdependence */
273 static int chg_state(int index, unsigned char new_state, struct file *file);
276 static int proc_viotape_show(struct seq_file *m, void *v)
280 seq_printf(m, "viotape driver version " VIOTAPE_VERSION "\n");
281 for (i = 0; i < viotape_numdev; i++) {
282 seq_printf(m, "viotape device %d is iSeries resource %10.10s"
283 "type %4.4s, model %3.3s\n",
284 i, viotape_unitinfo[i].rsrcname,
285 viotape_unitinfo[i].type,
286 viotape_unitinfo[i].model);
291 static int proc_viotape_open(struct inode *inode, struct file *file)
293 return single_open(file, proc_viotape_show, NULL);
296 static struct file_operations proc_viotape_operations = {
297 .open = proc_viotape_open,
300 .release = single_release,
303 /* Decode the device minor number into its parts */
304 void get_dev_info(struct inode *ino, struct viot_devinfo_struct *devi)
306 devi->devno = iminor(ino) & 0x1F;
307 devi->mode = (iminor(ino) & 0x60) >> 5;
308 /* if bit is set in the minor, do _not_ rewind automatically */
309 devi->rewind = (iminor(ino) & 0x80) == 0;
312 /* This is called only from the exit and init paths, so no need for locking */
313 static void clear_op_struct_pool(void)
315 while (op_struct_list) {
316 struct op_struct *toFree = op_struct_list;
317 op_struct_list = op_struct_list->next;
322 /* Likewise, this is only called from the init path */
323 static int add_op_structs(int structs)
327 for (i = 0; i < structs; ++i) {
328 struct op_struct *new_struct =
329 kmalloc(sizeof(*new_struct), GFP_KERNEL);
331 clear_op_struct_pool();
334 new_struct->next = op_struct_list;
335 op_struct_list = new_struct;
340 /* Allocate an op structure from our pool */
341 static struct op_struct *get_op_struct(void)
343 struct op_struct *retval;
346 spin_lock_irqsave(&op_struct_list_lock, flags);
347 retval = op_struct_list;
349 op_struct_list = retval->next;
350 spin_unlock_irqrestore(&op_struct_list_lock, flags);
352 memset(retval, 0, sizeof(*retval));
353 init_completion(&retval->com);
359 /* Return an op structure to our pool */
360 static void free_op_struct(struct op_struct *op_struct)
364 spin_lock_irqsave(&op_struct_list_lock, flags);
365 op_struct->next = op_struct_list;
366 op_struct_list = op_struct;
367 spin_unlock_irqrestore(&op_struct_list_lock, flags);
370 /* Map our tape return codes to errno values */
371 int tape_rc_to_errno(int tape_rc, char *operation, int tapeno)
373 const struct vio_error_entry *err;
378 err = vio_lookup_rc(viotape_err_table, tape_rc);
379 printk(VIOTAPE_KERN_WARN "error(%s) 0x%04x on Device %d (%-10s): %s\n",
380 operation, tape_rc, tapeno,
381 viotape_unitinfo[tapeno].rsrcname, err->msg);
385 /* Get info on all tapes from OS/400 */
386 static int get_viotape_info(void)
390 size_t len = sizeof(*viotape_unitinfo) * VIOTAPE_MAX_TAPE;
391 struct op_struct *op = get_op_struct();
396 viotape_unitinfo = dma_alloc_coherent(iSeries_vio_dev, len,
397 &viotape_unitinfo_token, GFP_ATOMIC);
398 if (viotape_unitinfo == NULL) {
403 memset(viotape_unitinfo, 0, len);
405 hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
406 HvLpEvent_Type_VirtualIo,
407 viomajorsubtype_tape | viotapegetinfo,
408 HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck,
409 viopath_sourceinst(viopath_hostLp),
410 viopath_targetinst(viopath_hostLp),
411 (u64) (unsigned long) op, VIOVERSION << 16,
412 viotape_unitinfo_token, len, 0, 0);
413 if (hvrc != HvLpEvent_Rc_Good) {
414 printk(VIOTAPE_KERN_WARN "hv error on op %d\n",
420 wait_for_completion(&op->com);
425 ((i < VIOTAPE_MAX_TAPE) && (viotape_unitinfo[i].rsrcname[0]));
433 static ssize_t viotap_write(struct file *file, const char *buf,
434 size_t count, loff_t * ppos)
437 unsigned short flags = file->f_flags;
438 int noblock = ((flags & O_NONBLOCK) != 0);
440 struct viot_devinfo_struct devi;
441 struct op_struct *op = get_op_struct();
446 get_dev_info(file->f_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 /* Allocate a DMA buffer */
463 op->dev = tape_device[devi.devno];
464 op->buffer = dma_alloc_coherent(op->dev, count, &op->dmaaddr,
467 if (op->buffer == NULL) {
468 printk(VIOTAPE_KERN_WARN
469 "error allocating dma buffer for len %ld\n",
475 /* Copy the data into the buffer */
476 if (copy_from_user(op->buffer, buf, count)) {
477 printk(VIOTAPE_KERN_WARN "tape: error on copy from user\n");
482 op->non_blocking = noblock;
483 init_completion(&op->com);
486 hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
487 HvLpEvent_Type_VirtualIo,
488 viomajorsubtype_tape | viotapewrite,
489 HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck,
490 viopath_sourceinst(viopath_hostLp),
491 viopath_targetinst(viopath_hostLp),
492 (u64)(unsigned long)op, VIOVERSION << 16,
493 ((u64)devi.devno << 48) | op->dmaaddr, count, 0, 0);
494 if (hvrc != HvLpEvent_Rc_Good) {
495 printk(VIOTAPE_KERN_WARN "hv error on op %d\n",
504 wait_for_completion(&op->com);
507 ret = tape_rc_to_errno(op->rc, "write", devi.devno);
509 chg_state(devi.devno, VIOT_WRITING, file);
514 dma_free_coherent(op->dev, count, op->buffer, op->dmaaddr);
523 static ssize_t viotap_read(struct file *file, char *buf, size_t count,
527 unsigned short flags = file->f_flags;
528 struct op_struct *op = get_op_struct();
529 int noblock = ((flags & O_NONBLOCK) != 0);
531 struct viot_devinfo_struct devi;
536 get_dev_info(file->f_dentry->d_inode, &devi);
539 * We need to make sure we can send a request. We use
540 * a semaphore to keep track of # requests in use. If
541 * we are non-blocking, make sure we don't block on the
545 if (down_trylock(&reqSem)) {
552 chg_state(devi.devno, VIOT_READING, file);
554 /* Allocate a DMA buffer */
555 op->dev = tape_device[devi.devno];
556 op->buffer = dma_alloc_coherent(op->dev, count, &op->dmaaddr,
558 if (op->buffer == NULL) {
564 init_completion(&op->com);
566 hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
567 HvLpEvent_Type_VirtualIo,
568 viomajorsubtype_tape | viotaperead,
569 HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck,
570 viopath_sourceinst(viopath_hostLp),
571 viopath_targetinst(viopath_hostLp),
572 (u64)(unsigned long)op, VIOVERSION << 16,
573 ((u64)devi.devno << 48) | op->dmaaddr, count, 0, 0);
574 if (hvrc != HvLpEvent_Rc_Good) {
575 printk(VIOTAPE_KERN_WARN "tape hv error on op %d\n",
581 wait_for_completion(&op->com);
584 ret = tape_rc_to_errno(op->rc, "read", devi.devno);
587 if (ret && copy_to_user(buf, op->buffer, ret)) {
588 printk(VIOTAPE_KERN_WARN "error on copy_to_user\n");
594 dma_free_coherent(op->dev, count, op->buffer, op->dmaaddr);
603 static int viotap_ioctl(struct inode *inode, struct file *file,
604 unsigned int cmd, unsigned long arg)
608 struct viot_devinfo_struct devi;
611 struct op_struct *op = get_op_struct();
616 get_dev_info(file->f_dentry->d_inode, &devi);
626 * inode is null if and only if we (the kernel)
630 memcpy(&mtc, (void *) arg, sizeof(struct mtop));
631 else if (copy_from_user((char *)&mtc, (char *)arg,
632 sizeof(struct mtop)))
638 myOp = VIOTAPOP_RESET;
653 myOp = VIOTAPOP_WEOF;
665 myOp = VIOTAPOP_ERASE;
668 myOp = VIOTAPOP_SETBLK;
671 myOp = VIOTAPOP_SETDENSITY;
674 myOp = VIOTAPOP_GETPOS;
677 myOp = VIOTAPOP_SETPOS;
680 myOp = VIOTAPOP_SETPART;
683 myOp = VIOTAPOP_UNLOAD;
686 printk(VIOTAPE_KERN_WARN "MTIOCTOP called "
687 "with invalid op 0x%x\n", mtc.mt_op);
692 * if we moved the head, we are no longer
703 chg_state(devi.devno, VIOT_IDLE, file);
706 init_completion(&op->com);
707 hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
708 HvLpEvent_Type_VirtualIo,
709 viomajorsubtype_tape | viotapeop,
710 HvLpEvent_AckInd_DoAck,
711 HvLpEvent_AckType_ImmediateAck,
712 viopath_sourceinst(viopath_hostLp),
713 viopath_targetinst(viopath_hostLp),
714 (u64)(unsigned long)op,
716 ((u64)devi.devno << 48), 0,
717 (((u64)myOp) << 32) | mtc.mt_count, 0);
718 if (hvrc != HvLpEvent_Rc_Good) {
719 printk(VIOTAPE_KERN_WARN "hv error on op %d\n",
723 wait_for_completion(&op->com);
724 ret = tape_rc_to_errno(op->rc, "tape operation", devi.devno);
729 init_completion(&op->com);
730 hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
731 HvLpEvent_Type_VirtualIo,
732 viomajorsubtype_tape | viotapegetstatus,
733 HvLpEvent_AckInd_DoAck,
734 HvLpEvent_AckType_ImmediateAck,
735 viopath_sourceinst(viopath_hostLp),
736 viopath_targetinst(viopath_hostLp),
737 (u64)(unsigned long)op, VIOVERSION << 16,
738 ((u64)devi.devno << 48), 0, 0, 0);
739 if (hvrc != HvLpEvent_Rc_Good) {
740 printk(VIOTAPE_KERN_WARN "hv error on op %d\n",
744 wait_for_completion(&op->com);
746 /* Operation is complete - grab the error code */
747 ret = tape_rc_to_errno(op->rc, "get status", devi.devno);
751 if ((ret == 0) && copy_to_user((void *)arg,
752 &viomtget[devi.devno],
753 sizeof(viomtget[0])))
757 printk(VIOTAPE_KERN_WARN "Got an (unsupported) MTIOCPOS\n");
760 printk(VIOTAPE_KERN_WARN "got an unsupported ioctl 0x%0x\n",
771 static int viotap_open(struct inode *inode, struct file *file)
774 struct viot_devinfo_struct devi;
776 struct op_struct *op = get_op_struct();
781 get_dev_info(file->f_dentry->d_inode, &devi);
783 /* Note: We currently only support one mode! */
784 if ((devi.devno >= viotape_numdev) || (devi.mode)) {
789 init_completion(&op->com);
791 hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
792 HvLpEvent_Type_VirtualIo,
793 viomajorsubtype_tape | viotapeopen,
794 HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck,
795 viopath_sourceinst(viopath_hostLp),
796 viopath_targetinst(viopath_hostLp),
797 (u64)(unsigned long)op, VIOVERSION << 16,
798 ((u64)devi.devno << 48), 0, 0, 0);
800 printk(VIOTAPE_KERN_WARN "bad rc on signalLpEvent %d\n",
806 wait_for_completion(&op->com);
807 ret = tape_rc_to_errno(op->rc, "open", devi.devno);
815 static int viotap_release(struct inode *inode, struct file *file)
818 struct viot_devinfo_struct devi;
820 struct op_struct *op = get_op_struct();
824 init_completion(&op->com);
826 get_dev_info(file->f_dentry->d_inode, &devi);
828 if (devi.devno >= viotape_numdev) {
833 chg_state(devi.devno, VIOT_IDLE, file);
836 hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
837 HvLpEvent_Type_VirtualIo,
838 viomajorsubtype_tape | viotapeop,
839 HvLpEvent_AckInd_DoAck,
840 HvLpEvent_AckType_ImmediateAck,
841 viopath_sourceinst(viopath_hostLp),
842 viopath_targetinst(viopath_hostLp),
843 (u64)(unsigned long)op, VIOVERSION << 16,
844 ((u64)devi.devno << 48), 0,
845 ((u64)VIOTAPOP_REW) << 32, 0);
846 wait_for_completion(&op->com);
848 tape_rc_to_errno(op->rc, "rewind", devi.devno);
851 hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
852 HvLpEvent_Type_VirtualIo,
853 viomajorsubtype_tape | viotapeclose,
854 HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck,
855 viopath_sourceinst(viopath_hostLp),
856 viopath_targetinst(viopath_hostLp),
857 (u64)(unsigned long)op, VIOVERSION << 16,
858 ((u64)devi.devno << 48), 0, 0, 0);
860 printk(VIOTAPE_KERN_WARN "bad rc on signalLpEvent %d\n",
866 wait_for_completion(&op->com);
869 printk(VIOTAPE_KERN_WARN "close failed\n");
876 struct file_operations viotap_fops = {
882 release: viotap_release,
885 /* Handle interrupt events for tape */
886 static void vioHandleTapeEvent(struct HvLpEvent *event)
889 struct op_struct *op;
890 struct viotapelpevent *tevent = (struct viotapelpevent *)event;
893 /* Notification that a partition went away! */
894 if (!viopath_isactive(viopath_hostLp)) {
900 tapeminor = event->xSubtype & VIOMINOR_SUBTYPE_MASK;
901 op = (struct op_struct *)event->xCorrelationToken;
906 op->rc = tevent->sub_type_result;
910 op->rc = tevent->sub_type_result;
911 op->count = tevent->len;
915 if (op->non_blocking) {
916 dma_free_coherent(op->dev, op->count,
917 op->buffer, op->dmaaddr);
921 op->rc = tevent->sub_type_result;
922 op->count = tevent->len;
929 case viotapegetstatus:
931 op->count = tevent->u.op.count;
932 op->rc = tevent->sub_type_result;
933 if (!op->non_blocking)
938 printk(VIOTAPE_KERN_WARN "weird ack\n");
942 static int viotape_probe(struct vio_dev *vdev, const struct vio_device_id *id)
945 int i = vdev->unit_address;
948 if (i >= viotape_numdev)
951 tape_device[i] = &vdev->dev;
953 state[i].cur_part = 0;
954 for (j = 0; j < MAX_PARTITIONS; ++j)
955 state[i].part_stat_rwi[j] = VIOT_IDLE;
956 class_device_create(tape_class, NULL, MKDEV(VIOTAPE_MAJOR, i), NULL,
958 class_device_create(tape_class, NULL, MKDEV(VIOTAPE_MAJOR, i | 0x80),
959 NULL, "iseries!nvt%d", i);
960 sprintf(tapename, "iseries/vt%d", i);
961 printk(VIOTAPE_KERN_INFO "tape %s is iSeries "
962 "resource %10.10s type %4.4s, model %3.3s\n",
963 tapename, viotape_unitinfo[i].rsrcname,
964 viotape_unitinfo[i].type, viotape_unitinfo[i].model);
968 static int viotape_remove(struct vio_dev *vdev)
970 int i = vdev->unit_address;
972 class_device_destroy(tape_class, MKDEV(VIOTAPE_MAJOR, i | 0x80));
973 class_device_destroy(tape_class, MKDEV(VIOTAPE_MAJOR, i));
978 * viotape_device_table: Used by vio.c to match devices that we
981 static struct vio_device_id viotape_device_table[] __devinitdata = {
982 { "byte", "IBM,iSeries-viotape" },
985 MODULE_DEVICE_TABLE(vio, viotape_device_table);
987 static struct vio_driver viotape_driver = {
988 .id_table = viotape_device_table,
989 .probe = viotape_probe,
990 .remove = viotape_remove,
993 .owner = THIS_MODULE,
998 int __init viotap_init(void)
1001 struct proc_dir_entry *e;
1003 op_struct_list = NULL;
1004 if ((ret = add_op_structs(VIOTAPE_MAXREQ)) < 0) {
1005 printk(VIOTAPE_KERN_WARN "couldn't allocate op structs\n");
1008 spin_lock_init(&op_struct_list_lock);
1010 sema_init(&reqSem, VIOTAPE_MAXREQ);
1012 if (viopath_hostLp == HvLpIndexInvalid) {
1014 if (viopath_hostLp == HvLpIndexInvalid) {
1020 ret = viopath_open(viopath_hostLp, viomajorsubtype_tape,
1021 VIOTAPE_MAXREQ + 2);
1023 printk(VIOTAPE_KERN_WARN
1024 "error on viopath_open to hostlp %d\n", ret);
1029 printk(VIOTAPE_KERN_INFO "vers " VIOTAPE_VERSION
1030 ", hosting partition %d\n", viopath_hostLp);
1032 vio_setHandler(viomajorsubtype_tape, vioHandleTapeEvent);
1034 ret = register_chrdev(VIOTAPE_MAJOR, "viotape", &viotap_fops);
1036 printk(VIOTAPE_KERN_WARN "Error registering viotape device\n");
1040 tape_class = class_create(THIS_MODULE, "tape");
1041 if (IS_ERR(tape_class)) {
1042 printk(VIOTAPE_KERN_WARN "Unable to allocat class\n");
1043 ret = PTR_ERR(tape_class);
1047 if ((ret = get_viotape_info()) < 0) {
1048 printk(VIOTAPE_KERN_WARN "Unable to obtain virtual device information");
1052 ret = vio_register_driver(&viotape_driver);
1056 e = create_proc_entry("iSeries/viotape", S_IFREG|S_IRUGO, NULL);
1058 e->owner = THIS_MODULE;
1059 e->proc_fops = &proc_viotape_operations;
1065 class_destroy(tape_class);
1067 unregister_chrdev(VIOTAPE_MAJOR, "viotape");
1069 vio_clearHandler(viomajorsubtype_tape);
1070 viopath_close(viopath_hostLp, viomajorsubtype_tape, VIOTAPE_MAXREQ + 2);
1072 clear_op_struct_pool();
1076 /* Give a new state to the tape object */
1077 static int chg_state(int index, unsigned char new_state, struct file *file)
1079 unsigned char *cur_state =
1080 &state[index].part_stat_rwi[state[index].cur_part];
1083 /* if the same state, don't bother */
1084 if (*cur_state == new_state)
1087 /* write an EOF if changing from writing to some other state */
1088 if (*cur_state == VIOT_WRITING) {
1089 struct mtop write_eof = { MTWEOF, 1 };
1091 rc = viotap_ioctl(NULL, file, MTIOCTOP,
1092 (unsigned long)&write_eof);
1094 *cur_state = new_state;
1099 static void __exit viotap_exit(void)
1103 remove_proc_entry("iSeries/viotape", NULL);
1104 vio_unregister_driver(&viotape_driver);
1105 class_destroy(tape_class);
1106 ret = unregister_chrdev(VIOTAPE_MAJOR, "viotape");
1108 printk(VIOTAPE_KERN_WARN "Error unregistering device: %d\n",
1110 if (viotape_unitinfo)
1111 dma_free_coherent(iSeries_vio_dev,
1112 sizeof(viotape_unitinfo[0]) * VIOTAPE_MAX_TAPE,
1113 viotape_unitinfo, viotape_unitinfo_token);
1114 viopath_close(viopath_hostLp, viomajorsubtype_tape, VIOTAPE_MAXREQ + 2);
1115 vio_clearHandler(viomajorsubtype_tape);
1116 clear_op_struct_pool();
1119 MODULE_LICENSE("GPL");
1120 module_init(viotap_init);
1121 module_exit(viotap_exit);