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/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>
50 #include <asm/uaccess.h>
51 #include <asm/ioctls.h>
52 #include <asm/firmware.h>
54 #include <asm/iseries/vio.h>
55 #include <asm/iseries/hv_lp_event.h>
56 #include <asm/iseries/hv_call_event.h>
57 #include <asm/iseries/hv_lp_config.h>
59 #define VIOTAPE_VERSION "1.2"
60 #define VIOTAPE_MAXREQ 1
62 #define VIOTAPE_KERN_WARN KERN_WARNING "viotape: "
63 #define VIOTAPE_KERN_INFO KERN_INFO "viotape: "
65 static int viotape_numdev
;
68 * The minor number follows the conventions of the SCSI tape drives. The
69 * rewind and mode are encoded in the minor #. We use this struct to break
72 struct viot_devinfo_struct
{
78 #define VIOTAPOP_RESET 0
79 #define VIOTAPOP_FSF 1
80 #define VIOTAPOP_BSF 2
81 #define VIOTAPOP_FSR 3
82 #define VIOTAPOP_BSR 4
83 #define VIOTAPOP_WEOF 5
84 #define VIOTAPOP_REW 6
85 #define VIOTAPOP_NOP 7
86 #define VIOTAPOP_EOM 8
87 #define VIOTAPOP_ERASE 9
88 #define VIOTAPOP_SETBLK 10
89 #define VIOTAPOP_SETDENSITY 11
90 #define VIOTAPOP_SETPOS 12
91 #define VIOTAPOP_GETPOS 13
92 #define VIOTAPOP_SETPART 14
93 #define VIOTAPOP_UNLOAD 15
96 viotape_InvalidRange
= 0x0601,
97 viotape_InvalidToken
= 0x0602,
98 viotape_DMAError
= 0x0603,
99 viotape_UseError
= 0x0604,
100 viotape_ReleaseError
= 0x0605,
101 viotape_InvalidTape
= 0x0606,
102 viotape_InvalidOp
= 0x0607,
103 viotape_TapeErr
= 0x0608,
105 viotape_AllocTimedOut
= 0x0640,
106 viotape_BOTEnc
= 0x0641,
107 viotape_BlankTape
= 0x0642,
108 viotape_BufferEmpty
= 0x0643,
109 viotape_CleanCartFound
= 0x0644,
110 viotape_CmdNotAllowed
= 0x0645,
111 viotape_CmdNotSupported
= 0x0646,
112 viotape_DataCheck
= 0x0647,
113 viotape_DecompressErr
= 0x0648,
114 viotape_DeviceTimeout
= 0x0649,
115 viotape_DeviceUnavail
= 0x064a,
116 viotape_DeviceBusy
= 0x064b,
117 viotape_EndOfMedia
= 0x064c,
118 viotape_EndOfTape
= 0x064d,
119 viotape_EquipCheck
= 0x064e,
120 viotape_InsufficientRs
= 0x064f,
121 viotape_InvalidLogBlk
= 0x0650,
122 viotape_LengthError
= 0x0651,
123 viotape_LibDoorOpen
= 0x0652,
124 viotape_LoadFailure
= 0x0653,
125 viotape_NotCapable
= 0x0654,
126 viotape_NotOperational
= 0x0655,
127 viotape_NotReady
= 0x0656,
128 viotape_OpCancelled
= 0x0657,
129 viotape_PhyLinkErr
= 0x0658,
130 viotape_RdyNotBOT
= 0x0659,
131 viotape_TapeMark
= 0x065a,
132 viotape_WriteProt
= 0x065b
135 static const struct vio_error_entry viotape_err_table
[] = {
136 { viotape_InvalidRange
, EIO
, "Internal error" },
137 { viotape_InvalidToken
, EIO
, "Internal error" },
138 { viotape_DMAError
, EIO
, "DMA error" },
139 { viotape_UseError
, EIO
, "Internal error" },
140 { viotape_ReleaseError
, EIO
, "Internal error" },
141 { viotape_InvalidTape
, EIO
, "Invalid tape device" },
142 { viotape_InvalidOp
, EIO
, "Invalid operation" },
143 { viotape_TapeErr
, EIO
, "Tape error" },
144 { viotape_AllocTimedOut
, EBUSY
, "Allocate timed out" },
145 { viotape_BOTEnc
, EIO
, "Beginning of tape encountered" },
146 { viotape_BlankTape
, EIO
, "Blank tape" },
147 { viotape_BufferEmpty
, EIO
, "Buffer empty" },
148 { viotape_CleanCartFound
, ENOMEDIUM
, "Cleaning cartridge found" },
149 { viotape_CmdNotAllowed
, EIO
, "Command not allowed" },
150 { viotape_CmdNotSupported
, EIO
, "Command not supported" },
151 { viotape_DataCheck
, EIO
, "Data check" },
152 { viotape_DecompressErr
, EIO
, "Decompression error" },
153 { viotape_DeviceTimeout
, EBUSY
, "Device timeout" },
154 { viotape_DeviceUnavail
, EIO
, "Device unavailable" },
155 { viotape_DeviceBusy
, EBUSY
, "Device busy" },
156 { viotape_EndOfMedia
, ENOSPC
, "End of media" },
157 { viotape_EndOfTape
, ENOSPC
, "End of tape" },
158 { viotape_EquipCheck
, EIO
, "Equipment check" },
159 { viotape_InsufficientRs
, EOVERFLOW
, "Insufficient tape resources" },
160 { viotape_InvalidLogBlk
, EIO
, "Invalid logical block location" },
161 { viotape_LengthError
, EOVERFLOW
, "Length error" },
162 { viotape_LibDoorOpen
, EBUSY
, "Door open" },
163 { viotape_LoadFailure
, ENOMEDIUM
, "Load failure" },
164 { viotape_NotCapable
, EIO
, "Not capable" },
165 { viotape_NotOperational
, EIO
, "Not operational" },
166 { viotape_NotReady
, EIO
, "Not ready" },
167 { viotape_OpCancelled
, EIO
, "Operation cancelled" },
168 { viotape_PhyLinkErr
, EIO
, "Physical link error" },
169 { viotape_RdyNotBOT
, EIO
, "Ready but not beginning of tape" },
170 { viotape_TapeMark
, EIO
, "Tape mark" },
171 { viotape_WriteProt
, EROFS
, "Write protection error" },
175 /* Maximum number of tapes we support */
176 #define VIOTAPE_MAX_TAPE HVMAXARCHITECTEDVIRTUALTAPES
177 #define MAX_PARTITIONS 4
179 /* defines for current tape state */
181 #define VIOT_READING 1
182 #define VIOT_WRITING 2
184 /* Our info on the tapes */
186 const char *rsrcname
;
189 } viotape_unitinfo
[VIOTAPE_MAX_TAPE
];
191 static struct mtget viomtget
[VIOTAPE_MAX_TAPE
];
193 static struct class *tape_class
;
195 static struct device
*tape_device
[VIOTAPE_MAX_TAPE
];
198 * maintain the current state of each tape (and partition)
199 * so that we know when to write EOF marks.
202 unsigned char cur_part
;
203 unsigned char part_stat_rwi
[MAX_PARTITIONS
];
204 } state
[VIOTAPE_MAX_TAPE
];
206 /* We single-thread */
207 static struct semaphore reqSem
;
210 * When we send a request, we use this struct to get the response back
211 * from the interrupt handler
219 struct completion com
;
221 struct op_struct
*next
;
224 static spinlock_t op_struct_list_lock
;
225 static struct op_struct
*op_struct_list
;
227 /* forward declaration to resolve interdependence */
228 static int chg_state(int index
, unsigned char new_state
, struct file
*file
);
231 static int proc_viotape_show(struct seq_file
*m
, void *v
)
235 seq_printf(m
, "viotape driver version " VIOTAPE_VERSION
"\n");
236 for (i
= 0; i
< viotape_numdev
; i
++) {
237 seq_printf(m
, "viotape device %d is iSeries resource %10.10s"
238 "type %4.4s, model %3.3s\n",
239 i
, viotape_unitinfo
[i
].rsrcname
,
240 viotape_unitinfo
[i
].type
,
241 viotape_unitinfo
[i
].model
);
246 static int proc_viotape_open(struct inode
*inode
, struct file
*file
)
248 return single_open(file
, proc_viotape_show
, NULL
);
251 static const struct file_operations proc_viotape_operations
= {
252 .owner
= THIS_MODULE
,
253 .open
= proc_viotape_open
,
256 .release
= single_release
,
259 /* Decode the device minor number into its parts */
260 void get_dev_info(struct inode
*ino
, struct viot_devinfo_struct
*devi
)
262 devi
->devno
= iminor(ino
) & 0x1F;
263 devi
->mode
= (iminor(ino
) & 0x60) >> 5;
264 /* if bit is set in the minor, do _not_ rewind automatically */
265 devi
->rewind
= (iminor(ino
) & 0x80) == 0;
268 /* This is called only from the exit and init paths, so no need for locking */
269 static void clear_op_struct_pool(void)
271 while (op_struct_list
) {
272 struct op_struct
*toFree
= op_struct_list
;
273 op_struct_list
= op_struct_list
->next
;
278 /* Likewise, this is only called from the init path */
279 static int add_op_structs(int structs
)
283 for (i
= 0; i
< structs
; ++i
) {
284 struct op_struct
*new_struct
=
285 kmalloc(sizeof(*new_struct
), GFP_KERNEL
);
287 clear_op_struct_pool();
290 new_struct
->next
= op_struct_list
;
291 op_struct_list
= new_struct
;
296 /* Allocate an op structure from our pool */
297 static struct op_struct
*get_op_struct(void)
299 struct op_struct
*retval
;
302 spin_lock_irqsave(&op_struct_list_lock
, flags
);
303 retval
= op_struct_list
;
305 op_struct_list
= retval
->next
;
306 spin_unlock_irqrestore(&op_struct_list_lock
, flags
);
308 memset(retval
, 0, sizeof(*retval
));
309 init_completion(&retval
->com
);
315 /* Return an op structure to our pool */
316 static void free_op_struct(struct op_struct
*op_struct
)
320 spin_lock_irqsave(&op_struct_list_lock
, flags
);
321 op_struct
->next
= op_struct_list
;
322 op_struct_list
= op_struct
;
323 spin_unlock_irqrestore(&op_struct_list_lock
, flags
);
326 /* Map our tape return codes to errno values */
327 int tape_rc_to_errno(int tape_rc
, char *operation
, int tapeno
)
329 const struct vio_error_entry
*err
;
334 err
= vio_lookup_rc(viotape_err_table
, tape_rc
);
335 printk(VIOTAPE_KERN_WARN
"error(%s) 0x%04x on Device %d (%-10s): %s\n",
336 operation
, tape_rc
, tapeno
,
337 viotape_unitinfo
[tapeno
].rsrcname
, err
->msg
);
342 static ssize_t
viotap_write(struct file
*file
, const char *buf
,
343 size_t count
, loff_t
* ppos
)
346 unsigned short flags
= file
->f_flags
;
347 int noblock
= ((flags
& O_NONBLOCK
) != 0);
349 struct viot_devinfo_struct devi
;
350 struct op_struct
*op
= get_op_struct();
355 get_dev_info(file
->f_path
.dentry
->d_inode
, &devi
);
358 * We need to make sure we can send a request. We use
359 * a semaphore to keep track of # requests in use. If
360 * we are non-blocking, make sure we don't block on the
364 if (down_trylock(&reqSem
)) {
371 /* Allocate a DMA buffer */
372 op
->dev
= tape_device
[devi
.devno
];
373 op
->buffer
= dma_alloc_coherent(op
->dev
, count
, &op
->dmaaddr
,
376 if (op
->buffer
== NULL
) {
377 printk(VIOTAPE_KERN_WARN
378 "error allocating dma buffer for len %ld\n",
384 /* Copy the data into the buffer */
385 if (copy_from_user(op
->buffer
, buf
, count
)) {
386 printk(VIOTAPE_KERN_WARN
"tape: error on copy from user\n");
391 op
->non_blocking
= noblock
;
392 init_completion(&op
->com
);
395 hvrc
= HvCallEvent_signalLpEventFast(viopath_hostLp
,
396 HvLpEvent_Type_VirtualIo
,
397 viomajorsubtype_tape
| viotapewrite
,
398 HvLpEvent_AckInd_DoAck
, HvLpEvent_AckType_ImmediateAck
,
399 viopath_sourceinst(viopath_hostLp
),
400 viopath_targetinst(viopath_hostLp
),
401 (u64
)(unsigned long)op
, VIOVERSION
<< 16,
402 ((u64
)devi
.devno
<< 48) | op
->dmaaddr
, count
, 0, 0);
403 if (hvrc
!= HvLpEvent_Rc_Good
) {
404 printk(VIOTAPE_KERN_WARN
"hv error on op %d\n",
413 wait_for_completion(&op
->com
);
416 ret
= tape_rc_to_errno(op
->rc
, "write", devi
.devno
);
418 chg_state(devi
.devno
, VIOT_WRITING
, file
);
423 dma_free_coherent(op
->dev
, count
, op
->buffer
, op
->dmaaddr
);
432 static ssize_t
viotap_read(struct file
*file
, char *buf
, size_t count
,
436 unsigned short flags
= file
->f_flags
;
437 struct op_struct
*op
= get_op_struct();
438 int noblock
= ((flags
& O_NONBLOCK
) != 0);
440 struct viot_devinfo_struct devi
;
445 get_dev_info(file
->f_path
.dentry
->d_inode
, &devi
);
448 * We need to make sure we can send a request. We use
449 * a semaphore to keep track of # requests in use. If
450 * we are non-blocking, make sure we don't block on the
454 if (down_trylock(&reqSem
)) {
461 chg_state(devi
.devno
, VIOT_READING
, file
);
463 /* Allocate a DMA buffer */
464 op
->dev
= tape_device
[devi
.devno
];
465 op
->buffer
= dma_alloc_coherent(op
->dev
, count
, &op
->dmaaddr
,
467 if (op
->buffer
== NULL
) {
473 init_completion(&op
->com
);
475 hvrc
= HvCallEvent_signalLpEventFast(viopath_hostLp
,
476 HvLpEvent_Type_VirtualIo
,
477 viomajorsubtype_tape
| viotaperead
,
478 HvLpEvent_AckInd_DoAck
, HvLpEvent_AckType_ImmediateAck
,
479 viopath_sourceinst(viopath_hostLp
),
480 viopath_targetinst(viopath_hostLp
),
481 (u64
)(unsigned long)op
, VIOVERSION
<< 16,
482 ((u64
)devi
.devno
<< 48) | op
->dmaaddr
, count
, 0, 0);
483 if (hvrc
!= HvLpEvent_Rc_Good
) {
484 printk(VIOTAPE_KERN_WARN
"tape hv error on op %d\n",
490 wait_for_completion(&op
->com
);
493 ret
= tape_rc_to_errno(op
->rc
, "read", devi
.devno
);
496 if (ret
&& copy_to_user(buf
, op
->buffer
, ret
)) {
497 printk(VIOTAPE_KERN_WARN
"error on copy_to_user\n");
503 dma_free_coherent(op
->dev
, count
, op
->buffer
, op
->dmaaddr
);
512 static int viotap_ioctl(struct inode
*inode
, struct file
*file
,
513 unsigned int cmd
, unsigned long arg
)
517 struct viot_devinfo_struct devi
;
520 struct op_struct
*op
= get_op_struct();
525 get_dev_info(file
->f_path
.dentry
->d_inode
, &devi
);
535 * inode is null if and only if we (the kernel)
539 memcpy(&mtc
, (void *) arg
, sizeof(struct mtop
));
540 else if (copy_from_user((char *)&mtc
, (char *)arg
,
541 sizeof(struct mtop
)))
547 myOp
= VIOTAPOP_RESET
;
562 myOp
= VIOTAPOP_WEOF
;
574 myOp
= VIOTAPOP_ERASE
;
577 myOp
= VIOTAPOP_SETBLK
;
580 myOp
= VIOTAPOP_SETDENSITY
;
583 myOp
= VIOTAPOP_GETPOS
;
586 myOp
= VIOTAPOP_SETPOS
;
589 myOp
= VIOTAPOP_SETPART
;
592 myOp
= VIOTAPOP_UNLOAD
;
595 printk(VIOTAPE_KERN_WARN
"MTIOCTOP called "
596 "with invalid op 0x%x\n", mtc
.mt_op
);
601 * if we moved the head, we are no longer
612 chg_state(devi
.devno
, VIOT_IDLE
, file
);
615 init_completion(&op
->com
);
616 hvrc
= HvCallEvent_signalLpEventFast(viopath_hostLp
,
617 HvLpEvent_Type_VirtualIo
,
618 viomajorsubtype_tape
| viotapeop
,
619 HvLpEvent_AckInd_DoAck
,
620 HvLpEvent_AckType_ImmediateAck
,
621 viopath_sourceinst(viopath_hostLp
),
622 viopath_targetinst(viopath_hostLp
),
623 (u64
)(unsigned long)op
,
625 ((u64
)devi
.devno
<< 48), 0,
626 (((u64
)myOp
) << 32) | mtc
.mt_count
, 0);
627 if (hvrc
!= HvLpEvent_Rc_Good
) {
628 printk(VIOTAPE_KERN_WARN
"hv error on op %d\n",
632 wait_for_completion(&op
->com
);
633 ret
= tape_rc_to_errno(op
->rc
, "tape operation", devi
.devno
);
638 init_completion(&op
->com
);
639 hvrc
= HvCallEvent_signalLpEventFast(viopath_hostLp
,
640 HvLpEvent_Type_VirtualIo
,
641 viomajorsubtype_tape
| viotapegetstatus
,
642 HvLpEvent_AckInd_DoAck
,
643 HvLpEvent_AckType_ImmediateAck
,
644 viopath_sourceinst(viopath_hostLp
),
645 viopath_targetinst(viopath_hostLp
),
646 (u64
)(unsigned long)op
, VIOVERSION
<< 16,
647 ((u64
)devi
.devno
<< 48), 0, 0, 0);
648 if (hvrc
!= HvLpEvent_Rc_Good
) {
649 printk(VIOTAPE_KERN_WARN
"hv error on op %d\n",
653 wait_for_completion(&op
->com
);
655 /* Operation is complete - grab the error code */
656 ret
= tape_rc_to_errno(op
->rc
, "get status", devi
.devno
);
660 if ((ret
== 0) && copy_to_user((void *)arg
,
661 &viomtget
[devi
.devno
],
662 sizeof(viomtget
[0])))
666 printk(VIOTAPE_KERN_WARN
"Got an (unsupported) MTIOCPOS\n");
669 printk(VIOTAPE_KERN_WARN
"got an unsupported ioctl 0x%0x\n",
680 static int viotap_open(struct inode
*inode
, struct file
*file
)
683 struct viot_devinfo_struct devi
;
685 struct op_struct
*op
= get_op_struct();
690 get_dev_info(file
->f_path
.dentry
->d_inode
, &devi
);
692 /* Note: We currently only support one mode! */
693 if ((devi
.devno
>= viotape_numdev
) || (devi
.mode
)) {
698 init_completion(&op
->com
);
700 hvrc
= HvCallEvent_signalLpEventFast(viopath_hostLp
,
701 HvLpEvent_Type_VirtualIo
,
702 viomajorsubtype_tape
| viotapeopen
,
703 HvLpEvent_AckInd_DoAck
, HvLpEvent_AckType_ImmediateAck
,
704 viopath_sourceinst(viopath_hostLp
),
705 viopath_targetinst(viopath_hostLp
),
706 (u64
)(unsigned long)op
, VIOVERSION
<< 16,
707 ((u64
)devi
.devno
<< 48), 0, 0, 0);
709 printk(VIOTAPE_KERN_WARN
"bad rc on signalLpEvent %d\n",
715 wait_for_completion(&op
->com
);
716 ret
= tape_rc_to_errno(op
->rc
, "open", devi
.devno
);
724 static int viotap_release(struct inode
*inode
, struct file
*file
)
727 struct viot_devinfo_struct devi
;
729 struct op_struct
*op
= get_op_struct();
733 init_completion(&op
->com
);
735 get_dev_info(file
->f_path
.dentry
->d_inode
, &devi
);
737 if (devi
.devno
>= viotape_numdev
) {
742 chg_state(devi
.devno
, VIOT_IDLE
, file
);
745 hvrc
= HvCallEvent_signalLpEventFast(viopath_hostLp
,
746 HvLpEvent_Type_VirtualIo
,
747 viomajorsubtype_tape
| viotapeop
,
748 HvLpEvent_AckInd_DoAck
,
749 HvLpEvent_AckType_ImmediateAck
,
750 viopath_sourceinst(viopath_hostLp
),
751 viopath_targetinst(viopath_hostLp
),
752 (u64
)(unsigned long)op
, VIOVERSION
<< 16,
753 ((u64
)devi
.devno
<< 48), 0,
754 ((u64
)VIOTAPOP_REW
) << 32, 0);
755 wait_for_completion(&op
->com
);
757 tape_rc_to_errno(op
->rc
, "rewind", devi
.devno
);
760 hvrc
= HvCallEvent_signalLpEventFast(viopath_hostLp
,
761 HvLpEvent_Type_VirtualIo
,
762 viomajorsubtype_tape
| viotapeclose
,
763 HvLpEvent_AckInd_DoAck
, 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, 0, 0);
769 printk(VIOTAPE_KERN_WARN
"bad rc on signalLpEvent %d\n",
775 wait_for_completion(&op
->com
);
778 printk(VIOTAPE_KERN_WARN
"close failed\n");
785 const struct file_operations viotap_fops
= {
786 .owner
= THIS_MODULE
,
788 .write
= viotap_write
,
789 .ioctl
= viotap_ioctl
,
791 .release
= viotap_release
,
794 /* Handle interrupt events for tape */
795 static void vioHandleTapeEvent(struct HvLpEvent
*event
)
798 struct op_struct
*op
;
799 struct viotapelpevent
*tevent
= (struct viotapelpevent
*)event
;
802 /* Notification that a partition went away! */
803 if (!viopath_isactive(viopath_hostLp
)) {
809 tapeminor
= event
->xSubtype
& VIOMINOR_SUBTYPE_MASK
;
810 op
= (struct op_struct
*)event
->xCorrelationToken
;
814 op
->rc
= tevent
->sub_type_result
;
818 op
->rc
= tevent
->sub_type_result
;
819 op
->count
= tevent
->len
;
823 if (op
->non_blocking
) {
824 dma_free_coherent(op
->dev
, op
->count
,
825 op
->buffer
, op
->dmaaddr
);
829 op
->rc
= tevent
->sub_type_result
;
830 op
->count
= tevent
->len
;
837 case viotapegetstatus
:
839 op
->count
= tevent
->u
.op
.count
;
840 op
->rc
= tevent
->sub_type_result
;
841 if (!op
->non_blocking
)
846 printk(VIOTAPE_KERN_WARN
"weird ack\n");
850 static int viotape_probe(struct vio_dev
*vdev
, const struct vio_device_id
*id
)
852 int i
= vdev
->unit_address
;
854 struct device_node
*node
= vdev
->dev
.archdata
.of_node
;
856 if (i
> VIOTAPE_MAX_TAPE
)
861 if (i
>= viotape_numdev
)
862 viotape_numdev
= i
+ 1;
864 tape_device
[i
] = &vdev
->dev
;
865 viotape_unitinfo
[i
].rsrcname
= of_get_property(node
,
866 "linux,vio_rsrcname", NULL
);
867 viotape_unitinfo
[i
].type
= of_get_property(node
, "linux,vio_type",
869 viotape_unitinfo
[i
].model
= of_get_property(node
, "linux,vio_model",
872 state
[i
].cur_part
= 0;
873 for (j
= 0; j
< MAX_PARTITIONS
; ++j
)
874 state
[i
].part_stat_rwi
[j
] = VIOT_IDLE
;
875 device_create(tape_class
, NULL
, MKDEV(VIOTAPE_MAJOR
, i
),
877 device_create(tape_class
, NULL
, MKDEV(VIOTAPE_MAJOR
, i
| 0x80),
879 printk(VIOTAPE_KERN_INFO
"tape iseries/vt%d is iSeries "
880 "resource %10.10s type %4.4s, model %3.3s\n",
881 i
, viotape_unitinfo
[i
].rsrcname
,
882 viotape_unitinfo
[i
].type
, viotape_unitinfo
[i
].model
);
886 static int viotape_remove(struct vio_dev
*vdev
)
888 int i
= vdev
->unit_address
;
890 device_destroy(tape_class
, MKDEV(VIOTAPE_MAJOR
, i
| 0x80));
891 device_destroy(tape_class
, MKDEV(VIOTAPE_MAJOR
, i
));
896 * viotape_device_table: Used by vio.c to match devices that we
899 static struct vio_device_id viotape_device_table
[] __devinitdata
= {
900 { "byte", "IBM,iSeries-viotape" },
903 MODULE_DEVICE_TABLE(vio
, viotape_device_table
);
905 static struct vio_driver viotape_driver
= {
906 .id_table
= viotape_device_table
,
907 .probe
= viotape_probe
,
908 .remove
= viotape_remove
,
911 .owner
= THIS_MODULE
,
916 int __init
viotap_init(void)
920 if (!firmware_has_feature(FW_FEATURE_ISERIES
))
923 op_struct_list
= NULL
;
924 if ((ret
= add_op_structs(VIOTAPE_MAXREQ
)) < 0) {
925 printk(VIOTAPE_KERN_WARN
"couldn't allocate op structs\n");
928 spin_lock_init(&op_struct_list_lock
);
930 sema_init(&reqSem
, VIOTAPE_MAXREQ
);
932 if (viopath_hostLp
== HvLpIndexInvalid
) {
934 if (viopath_hostLp
== HvLpIndexInvalid
) {
940 ret
= viopath_open(viopath_hostLp
, viomajorsubtype_tape
,
943 printk(VIOTAPE_KERN_WARN
944 "error on viopath_open to hostlp %d\n", ret
);
949 printk(VIOTAPE_KERN_INFO
"vers " VIOTAPE_VERSION
950 ", hosting partition %d\n", viopath_hostLp
);
952 vio_setHandler(viomajorsubtype_tape
, vioHandleTapeEvent
);
954 ret
= register_chrdev(VIOTAPE_MAJOR
, "viotape", &viotap_fops
);
956 printk(VIOTAPE_KERN_WARN
"Error registering viotape device\n");
960 tape_class
= class_create(THIS_MODULE
, "tape");
961 if (IS_ERR(tape_class
)) {
962 printk(VIOTAPE_KERN_WARN
"Unable to allocat class\n");
963 ret
= PTR_ERR(tape_class
);
967 ret
= vio_register_driver(&viotape_driver
);
971 proc_create("iSeries/viotape", S_IFREG
|S_IRUGO
, NULL
,
972 &proc_viotape_operations
);
977 class_destroy(tape_class
);
979 unregister_chrdev(VIOTAPE_MAJOR
, "viotape");
981 vio_clearHandler(viomajorsubtype_tape
);
982 viopath_close(viopath_hostLp
, viomajorsubtype_tape
, VIOTAPE_MAXREQ
+ 2);
984 clear_op_struct_pool();
988 /* Give a new state to the tape object */
989 static int chg_state(int index
, unsigned char new_state
, struct file
*file
)
991 unsigned char *cur_state
=
992 &state
[index
].part_stat_rwi
[state
[index
].cur_part
];
995 /* if the same state, don't bother */
996 if (*cur_state
== new_state
)
999 /* write an EOF if changing from writing to some other state */
1000 if (*cur_state
== VIOT_WRITING
) {
1001 struct mtop write_eof
= { MTWEOF
, 1 };
1003 rc
= viotap_ioctl(NULL
, file
, MTIOCTOP
,
1004 (unsigned long)&write_eof
);
1006 *cur_state
= new_state
;
1011 static void __exit
viotap_exit(void)
1013 remove_proc_entry("iSeries/viotape", NULL
);
1014 vio_unregister_driver(&viotape_driver
);
1015 class_destroy(tape_class
);
1016 unregister_chrdev(VIOTAPE_MAJOR
, "viotape");
1017 viopath_close(viopath_hostLp
, viomajorsubtype_tape
, VIOTAPE_MAXREQ
+ 2);
1018 vio_clearHandler(viomajorsubtype_tape
);
1019 clear_op_struct_pool();
1022 MODULE_LICENSE("GPL");
1023 module_init(viotap_init
);
1024 module_exit(viotap_exit
);