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
95 struct viotapelpevent
{
96 struct HvLpEvent event
;
124 enum viotapesubtype
{
125 viotapeopen
= 0x0001,
126 viotapeclose
= 0x0002,
127 viotaperead
= 0x0003,
128 viotapewrite
= 0x0004,
129 viotapegetinfo
= 0x0005,
131 viotapegetpos
= 0x0007,
132 viotapesetpos
= 0x0008,
133 viotapegetstatus
= 0x0009
137 viotape_InvalidRange
= 0x0601,
138 viotape_InvalidToken
= 0x0602,
139 viotape_DMAError
= 0x0603,
140 viotape_UseError
= 0x0604,
141 viotape_ReleaseError
= 0x0605,
142 viotape_InvalidTape
= 0x0606,
143 viotape_InvalidOp
= 0x0607,
144 viotape_TapeErr
= 0x0608,
146 viotape_AllocTimedOut
= 0x0640,
147 viotape_BOTEnc
= 0x0641,
148 viotape_BlankTape
= 0x0642,
149 viotape_BufferEmpty
= 0x0643,
150 viotape_CleanCartFound
= 0x0644,
151 viotape_CmdNotAllowed
= 0x0645,
152 viotape_CmdNotSupported
= 0x0646,
153 viotape_DataCheck
= 0x0647,
154 viotape_DecompressErr
= 0x0648,
155 viotape_DeviceTimeout
= 0x0649,
156 viotape_DeviceUnavail
= 0x064a,
157 viotape_DeviceBusy
= 0x064b,
158 viotape_EndOfMedia
= 0x064c,
159 viotape_EndOfTape
= 0x064d,
160 viotape_EquipCheck
= 0x064e,
161 viotape_InsufficientRs
= 0x064f,
162 viotape_InvalidLogBlk
= 0x0650,
163 viotape_LengthError
= 0x0651,
164 viotape_LibDoorOpen
= 0x0652,
165 viotape_LoadFailure
= 0x0653,
166 viotape_NotCapable
= 0x0654,
167 viotape_NotOperational
= 0x0655,
168 viotape_NotReady
= 0x0656,
169 viotape_OpCancelled
= 0x0657,
170 viotape_PhyLinkErr
= 0x0658,
171 viotape_RdyNotBOT
= 0x0659,
172 viotape_TapeMark
= 0x065a,
173 viotape_WriteProt
= 0x065b
176 static const struct vio_error_entry viotape_err_table
[] = {
177 { viotape_InvalidRange
, EIO
, "Internal error" },
178 { viotape_InvalidToken
, EIO
, "Internal error" },
179 { viotape_DMAError
, EIO
, "DMA error" },
180 { viotape_UseError
, EIO
, "Internal error" },
181 { viotape_ReleaseError
, EIO
, "Internal error" },
182 { viotape_InvalidTape
, EIO
, "Invalid tape device" },
183 { viotape_InvalidOp
, EIO
, "Invalid operation" },
184 { viotape_TapeErr
, EIO
, "Tape error" },
185 { viotape_AllocTimedOut
, EBUSY
, "Allocate timed out" },
186 { viotape_BOTEnc
, EIO
, "Beginning of tape encountered" },
187 { viotape_BlankTape
, EIO
, "Blank tape" },
188 { viotape_BufferEmpty
, EIO
, "Buffer empty" },
189 { viotape_CleanCartFound
, ENOMEDIUM
, "Cleaning cartridge found" },
190 { viotape_CmdNotAllowed
, EIO
, "Command not allowed" },
191 { viotape_CmdNotSupported
, EIO
, "Command not supported" },
192 { viotape_DataCheck
, EIO
, "Data check" },
193 { viotape_DecompressErr
, EIO
, "Decompression error" },
194 { viotape_DeviceTimeout
, EBUSY
, "Device timeout" },
195 { viotape_DeviceUnavail
, EIO
, "Device unavailable" },
196 { viotape_DeviceBusy
, EBUSY
, "Device busy" },
197 { viotape_EndOfMedia
, ENOSPC
, "End of media" },
198 { viotape_EndOfTape
, ENOSPC
, "End of tape" },
199 { viotape_EquipCheck
, EIO
, "Equipment check" },
200 { viotape_InsufficientRs
, EOVERFLOW
, "Insufficient tape resources" },
201 { viotape_InvalidLogBlk
, EIO
, "Invalid logical block location" },
202 { viotape_LengthError
, EOVERFLOW
, "Length error" },
203 { viotape_LibDoorOpen
, EBUSY
, "Door open" },
204 { viotape_LoadFailure
, ENOMEDIUM
, "Load failure" },
205 { viotape_NotCapable
, EIO
, "Not capable" },
206 { viotape_NotOperational
, EIO
, "Not operational" },
207 { viotape_NotReady
, EIO
, "Not ready" },
208 { viotape_OpCancelled
, EIO
, "Operation cancelled" },
209 { viotape_PhyLinkErr
, EIO
, "Physical link error" },
210 { viotape_RdyNotBOT
, EIO
, "Ready but not beginning of tape" },
211 { viotape_TapeMark
, EIO
, "Tape mark" },
212 { viotape_WriteProt
, EROFS
, "Write protection error" },
216 /* Maximum number of tapes we support */
217 #define VIOTAPE_MAX_TAPE HVMAXARCHITECTEDVIRTUALTAPES
218 #define MAX_PARTITIONS 4
220 /* defines for current tape state */
222 #define VIOT_READING 1
223 #define VIOT_WRITING 2
225 /* Our info on the tapes */
232 static struct tape_descr
*viotape_unitinfo
;
233 static dma_addr_t viotape_unitinfo_token
;
235 static struct mtget viomtget
[VIOTAPE_MAX_TAPE
];
237 static struct class *tape_class
;
239 static struct device
*tape_device
[VIOTAPE_MAX_TAPE
];
242 * maintain the current state of each tape (and partition)
243 * so that we know when to write EOF marks.
246 unsigned char cur_part
;
247 unsigned char part_stat_rwi
[MAX_PARTITIONS
];
248 } state
[VIOTAPE_MAX_TAPE
];
250 /* We single-thread */
251 static struct semaphore reqSem
;
254 * When we send a request, we use this struct to get the response back
255 * from the interrupt handler
263 struct completion com
;
265 struct op_struct
*next
;
268 static spinlock_t op_struct_list_lock
;
269 static struct op_struct
*op_struct_list
;
271 /* forward declaration to resolve interdependence */
272 static int chg_state(int index
, unsigned char new_state
, struct file
*file
);
275 static int proc_viotape_show(struct seq_file
*m
, void *v
)
279 seq_printf(m
, "viotape driver version " VIOTAPE_VERSION
"\n");
280 for (i
= 0; i
< viotape_numdev
; i
++) {
281 seq_printf(m
, "viotape device %d is iSeries resource %10.10s"
282 "type %4.4s, model %3.3s\n",
283 i
, viotape_unitinfo
[i
].rsrcname
,
284 viotape_unitinfo
[i
].type
,
285 viotape_unitinfo
[i
].model
);
290 static int proc_viotape_open(struct inode
*inode
, struct file
*file
)
292 return single_open(file
, proc_viotape_show
, NULL
);
295 static const struct file_operations proc_viotape_operations
= {
296 .open
= proc_viotape_open
,
299 .release
= single_release
,
302 /* Decode the device minor number into its parts */
303 void get_dev_info(struct inode
*ino
, struct viot_devinfo_struct
*devi
)
305 devi
->devno
= iminor(ino
) & 0x1F;
306 devi
->mode
= (iminor(ino
) & 0x60) >> 5;
307 /* if bit is set in the minor, do _not_ rewind automatically */
308 devi
->rewind
= (iminor(ino
) & 0x80) == 0;
311 /* This is called only from the exit and init paths, so no need for locking */
312 static void clear_op_struct_pool(void)
314 while (op_struct_list
) {
315 struct op_struct
*toFree
= op_struct_list
;
316 op_struct_list
= op_struct_list
->next
;
321 /* Likewise, this is only called from the init path */
322 static int add_op_structs(int structs
)
326 for (i
= 0; i
< structs
; ++i
) {
327 struct op_struct
*new_struct
=
328 kmalloc(sizeof(*new_struct
), GFP_KERNEL
);
330 clear_op_struct_pool();
333 new_struct
->next
= op_struct_list
;
334 op_struct_list
= new_struct
;
339 /* Allocate an op structure from our pool */
340 static struct op_struct
*get_op_struct(void)
342 struct op_struct
*retval
;
345 spin_lock_irqsave(&op_struct_list_lock
, flags
);
346 retval
= op_struct_list
;
348 op_struct_list
= retval
->next
;
349 spin_unlock_irqrestore(&op_struct_list_lock
, flags
);
351 memset(retval
, 0, sizeof(*retval
));
352 init_completion(&retval
->com
);
358 /* Return an op structure to our pool */
359 static void free_op_struct(struct op_struct
*op_struct
)
363 spin_lock_irqsave(&op_struct_list_lock
, flags
);
364 op_struct
->next
= op_struct_list
;
365 op_struct_list
= op_struct
;
366 spin_unlock_irqrestore(&op_struct_list_lock
, flags
);
369 /* Map our tape return codes to errno values */
370 int tape_rc_to_errno(int tape_rc
, char *operation
, int tapeno
)
372 const struct vio_error_entry
*err
;
377 err
= vio_lookup_rc(viotape_err_table
, tape_rc
);
378 printk(VIOTAPE_KERN_WARN
"error(%s) 0x%04x on Device %d (%-10s): %s\n",
379 operation
, tape_rc
, tapeno
,
380 viotape_unitinfo
[tapeno
].rsrcname
, err
->msg
);
384 /* Get info on all tapes from OS/400 */
385 static int get_viotape_info(void)
389 size_t len
= sizeof(*viotape_unitinfo
) * VIOTAPE_MAX_TAPE
;
390 struct op_struct
*op
= get_op_struct();
395 viotape_unitinfo
= dma_alloc_coherent(iSeries_vio_dev
, len
,
396 &viotape_unitinfo_token
, GFP_ATOMIC
);
397 if (viotape_unitinfo
== NULL
) {
402 memset(viotape_unitinfo
, 0, len
);
404 hvrc
= HvCallEvent_signalLpEventFast(viopath_hostLp
,
405 HvLpEvent_Type_VirtualIo
,
406 viomajorsubtype_tape
| viotapegetinfo
,
407 HvLpEvent_AckInd_DoAck
, HvLpEvent_AckType_ImmediateAck
,
408 viopath_sourceinst(viopath_hostLp
),
409 viopath_targetinst(viopath_hostLp
),
410 (u64
) (unsigned long) op
, VIOVERSION
<< 16,
411 viotape_unitinfo_token
, len
, 0, 0);
412 if (hvrc
!= HvLpEvent_Rc_Good
) {
413 printk(VIOTAPE_KERN_WARN
"hv error on op %d\n",
419 wait_for_completion(&op
->com
);
424 ((i
< VIOTAPE_MAX_TAPE
) && (viotape_unitinfo
[i
].rsrcname
[0]));
432 static ssize_t
viotap_write(struct file
*file
, const char *buf
,
433 size_t count
, loff_t
* ppos
)
436 unsigned short flags
= file
->f_flags
;
437 int noblock
= ((flags
& O_NONBLOCK
) != 0);
439 struct viot_devinfo_struct devi
;
440 struct op_struct
*op
= get_op_struct();
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 /* Allocate a DMA buffer */
462 op
->dev
= tape_device
[devi
.devno
];
463 op
->buffer
= dma_alloc_coherent(op
->dev
, count
, &op
->dmaaddr
,
466 if (op
->buffer
== NULL
) {
467 printk(VIOTAPE_KERN_WARN
468 "error allocating dma buffer for len %ld\n",
474 /* Copy the data into the buffer */
475 if (copy_from_user(op
->buffer
, buf
, count
)) {
476 printk(VIOTAPE_KERN_WARN
"tape: error on copy from user\n");
481 op
->non_blocking
= noblock
;
482 init_completion(&op
->com
);
485 hvrc
= HvCallEvent_signalLpEventFast(viopath_hostLp
,
486 HvLpEvent_Type_VirtualIo
,
487 viomajorsubtype_tape
| viotapewrite
,
488 HvLpEvent_AckInd_DoAck
, HvLpEvent_AckType_ImmediateAck
,
489 viopath_sourceinst(viopath_hostLp
),
490 viopath_targetinst(viopath_hostLp
),
491 (u64
)(unsigned long)op
, VIOVERSION
<< 16,
492 ((u64
)devi
.devno
<< 48) | op
->dmaaddr
, count
, 0, 0);
493 if (hvrc
!= HvLpEvent_Rc_Good
) {
494 printk(VIOTAPE_KERN_WARN
"hv error on op %d\n",
503 wait_for_completion(&op
->com
);
506 ret
= tape_rc_to_errno(op
->rc
, "write", devi
.devno
);
508 chg_state(devi
.devno
, VIOT_WRITING
, file
);
513 dma_free_coherent(op
->dev
, count
, op
->buffer
, op
->dmaaddr
);
522 static ssize_t
viotap_read(struct file
*file
, char *buf
, size_t count
,
526 unsigned short flags
= file
->f_flags
;
527 struct op_struct
*op
= get_op_struct();
528 int noblock
= ((flags
& O_NONBLOCK
) != 0);
530 struct viot_devinfo_struct devi
;
535 get_dev_info(file
->f_path
.dentry
->d_inode
, &devi
);
538 * We need to make sure we can send a request. We use
539 * a semaphore to keep track of # requests in use. If
540 * we are non-blocking, make sure we don't block on the
544 if (down_trylock(&reqSem
)) {
551 chg_state(devi
.devno
, VIOT_READING
, file
);
553 /* Allocate a DMA buffer */
554 op
->dev
= tape_device
[devi
.devno
];
555 op
->buffer
= dma_alloc_coherent(op
->dev
, count
, &op
->dmaaddr
,
557 if (op
->buffer
== NULL
) {
563 init_completion(&op
->com
);
565 hvrc
= HvCallEvent_signalLpEventFast(viopath_hostLp
,
566 HvLpEvent_Type_VirtualIo
,
567 viomajorsubtype_tape
| viotaperead
,
568 HvLpEvent_AckInd_DoAck
, HvLpEvent_AckType_ImmediateAck
,
569 viopath_sourceinst(viopath_hostLp
),
570 viopath_targetinst(viopath_hostLp
),
571 (u64
)(unsigned long)op
, VIOVERSION
<< 16,
572 ((u64
)devi
.devno
<< 48) | op
->dmaaddr
, count
, 0, 0);
573 if (hvrc
!= HvLpEvent_Rc_Good
) {
574 printk(VIOTAPE_KERN_WARN
"tape hv error on op %d\n",
580 wait_for_completion(&op
->com
);
583 ret
= tape_rc_to_errno(op
->rc
, "read", devi
.devno
);
586 if (ret
&& copy_to_user(buf
, op
->buffer
, ret
)) {
587 printk(VIOTAPE_KERN_WARN
"error on copy_to_user\n");
593 dma_free_coherent(op
->dev
, count
, op
->buffer
, op
->dmaaddr
);
602 static int viotap_ioctl(struct inode
*inode
, struct file
*file
,
603 unsigned int cmd
, unsigned long arg
)
607 struct viot_devinfo_struct devi
;
610 struct op_struct
*op
= get_op_struct();
615 get_dev_info(file
->f_path
.dentry
->d_inode
, &devi
);
625 * inode is null if and only if we (the kernel)
629 memcpy(&mtc
, (void *) arg
, sizeof(struct mtop
));
630 else if (copy_from_user((char *)&mtc
, (char *)arg
,
631 sizeof(struct mtop
)))
637 myOp
= VIOTAPOP_RESET
;
652 myOp
= VIOTAPOP_WEOF
;
664 myOp
= VIOTAPOP_ERASE
;
667 myOp
= VIOTAPOP_SETBLK
;
670 myOp
= VIOTAPOP_SETDENSITY
;
673 myOp
= VIOTAPOP_GETPOS
;
676 myOp
= VIOTAPOP_SETPOS
;
679 myOp
= VIOTAPOP_SETPART
;
682 myOp
= VIOTAPOP_UNLOAD
;
685 printk(VIOTAPE_KERN_WARN
"MTIOCTOP called "
686 "with invalid op 0x%x\n", mtc
.mt_op
);
691 * if we moved the head, we are no longer
702 chg_state(devi
.devno
, VIOT_IDLE
, file
);
705 init_completion(&op
->com
);
706 hvrc
= HvCallEvent_signalLpEventFast(viopath_hostLp
,
707 HvLpEvent_Type_VirtualIo
,
708 viomajorsubtype_tape
| viotapeop
,
709 HvLpEvent_AckInd_DoAck
,
710 HvLpEvent_AckType_ImmediateAck
,
711 viopath_sourceinst(viopath_hostLp
),
712 viopath_targetinst(viopath_hostLp
),
713 (u64
)(unsigned long)op
,
715 ((u64
)devi
.devno
<< 48), 0,
716 (((u64
)myOp
) << 32) | mtc
.mt_count
, 0);
717 if (hvrc
!= HvLpEvent_Rc_Good
) {
718 printk(VIOTAPE_KERN_WARN
"hv error on op %d\n",
722 wait_for_completion(&op
->com
);
723 ret
= tape_rc_to_errno(op
->rc
, "tape operation", devi
.devno
);
728 init_completion(&op
->com
);
729 hvrc
= HvCallEvent_signalLpEventFast(viopath_hostLp
,
730 HvLpEvent_Type_VirtualIo
,
731 viomajorsubtype_tape
| viotapegetstatus
,
732 HvLpEvent_AckInd_DoAck
,
733 HvLpEvent_AckType_ImmediateAck
,
734 viopath_sourceinst(viopath_hostLp
),
735 viopath_targetinst(viopath_hostLp
),
736 (u64
)(unsigned long)op
, VIOVERSION
<< 16,
737 ((u64
)devi
.devno
<< 48), 0, 0, 0);
738 if (hvrc
!= HvLpEvent_Rc_Good
) {
739 printk(VIOTAPE_KERN_WARN
"hv error on op %d\n",
743 wait_for_completion(&op
->com
);
745 /* Operation is complete - grab the error code */
746 ret
= tape_rc_to_errno(op
->rc
, "get status", devi
.devno
);
750 if ((ret
== 0) && copy_to_user((void *)arg
,
751 &viomtget
[devi
.devno
],
752 sizeof(viomtget
[0])))
756 printk(VIOTAPE_KERN_WARN
"Got an (unsupported) MTIOCPOS\n");
759 printk(VIOTAPE_KERN_WARN
"got an unsupported ioctl 0x%0x\n",
770 static int viotap_open(struct inode
*inode
, struct file
*file
)
773 struct viot_devinfo_struct devi
;
775 struct op_struct
*op
= get_op_struct();
780 get_dev_info(file
->f_path
.dentry
->d_inode
, &devi
);
782 /* Note: We currently only support one mode! */
783 if ((devi
.devno
>= viotape_numdev
) || (devi
.mode
)) {
788 init_completion(&op
->com
);
790 hvrc
= HvCallEvent_signalLpEventFast(viopath_hostLp
,
791 HvLpEvent_Type_VirtualIo
,
792 viomajorsubtype_tape
| viotapeopen
,
793 HvLpEvent_AckInd_DoAck
, HvLpEvent_AckType_ImmediateAck
,
794 viopath_sourceinst(viopath_hostLp
),
795 viopath_targetinst(viopath_hostLp
),
796 (u64
)(unsigned long)op
, VIOVERSION
<< 16,
797 ((u64
)devi
.devno
<< 48), 0, 0, 0);
799 printk(VIOTAPE_KERN_WARN
"bad rc on signalLpEvent %d\n",
805 wait_for_completion(&op
->com
);
806 ret
= tape_rc_to_errno(op
->rc
, "open", devi
.devno
);
814 static int viotap_release(struct inode
*inode
, struct file
*file
)
817 struct viot_devinfo_struct devi
;
819 struct op_struct
*op
= get_op_struct();
823 init_completion(&op
->com
);
825 get_dev_info(file
->f_path
.dentry
->d_inode
, &devi
);
827 if (devi
.devno
>= viotape_numdev
) {
832 chg_state(devi
.devno
, VIOT_IDLE
, file
);
835 hvrc
= HvCallEvent_signalLpEventFast(viopath_hostLp
,
836 HvLpEvent_Type_VirtualIo
,
837 viomajorsubtype_tape
| viotapeop
,
838 HvLpEvent_AckInd_DoAck
,
839 HvLpEvent_AckType_ImmediateAck
,
840 viopath_sourceinst(viopath_hostLp
),
841 viopath_targetinst(viopath_hostLp
),
842 (u64
)(unsigned long)op
, VIOVERSION
<< 16,
843 ((u64
)devi
.devno
<< 48), 0,
844 ((u64
)VIOTAPOP_REW
) << 32, 0);
845 wait_for_completion(&op
->com
);
847 tape_rc_to_errno(op
->rc
, "rewind", devi
.devno
);
850 hvrc
= HvCallEvent_signalLpEventFast(viopath_hostLp
,
851 HvLpEvent_Type_VirtualIo
,
852 viomajorsubtype_tape
| viotapeclose
,
853 HvLpEvent_AckInd_DoAck
, HvLpEvent_AckType_ImmediateAck
,
854 viopath_sourceinst(viopath_hostLp
),
855 viopath_targetinst(viopath_hostLp
),
856 (u64
)(unsigned long)op
, VIOVERSION
<< 16,
857 ((u64
)devi
.devno
<< 48), 0, 0, 0);
859 printk(VIOTAPE_KERN_WARN
"bad rc on signalLpEvent %d\n",
865 wait_for_completion(&op
->com
);
868 printk(VIOTAPE_KERN_WARN
"close failed\n");
875 const struct file_operations viotap_fops
= {
876 .owner
= THIS_MODULE
,
878 .write
= viotap_write
,
879 .ioctl
= viotap_ioctl
,
881 .release
= viotap_release
,
884 /* Handle interrupt events for tape */
885 static void vioHandleTapeEvent(struct HvLpEvent
*event
)
888 struct op_struct
*op
;
889 struct viotapelpevent
*tevent
= (struct viotapelpevent
*)event
;
892 /* Notification that a partition went away! */
893 if (!viopath_isactive(viopath_hostLp
)) {
899 tapeminor
= event
->xSubtype
& VIOMINOR_SUBTYPE_MASK
;
900 op
= (struct op_struct
*)event
->xCorrelationToken
;
905 op
->rc
= tevent
->sub_type_result
;
909 op
->rc
= tevent
->sub_type_result
;
910 op
->count
= tevent
->len
;
914 if (op
->non_blocking
) {
915 dma_free_coherent(op
->dev
, op
->count
,
916 op
->buffer
, op
->dmaaddr
);
920 op
->rc
= tevent
->sub_type_result
;
921 op
->count
= tevent
->len
;
928 case viotapegetstatus
:
930 op
->count
= tevent
->u
.op
.count
;
931 op
->rc
= tevent
->sub_type_result
;
932 if (!op
->non_blocking
)
937 printk(VIOTAPE_KERN_WARN
"weird ack\n");
941 static int viotape_probe(struct vio_dev
*vdev
, const struct vio_device_id
*id
)
943 int i
= vdev
->unit_address
;
946 if (i
>= viotape_numdev
)
949 tape_device
[i
] = &vdev
->dev
;
951 state
[i
].cur_part
= 0;
952 for (j
= 0; j
< MAX_PARTITIONS
; ++j
)
953 state
[i
].part_stat_rwi
[j
] = VIOT_IDLE
;
954 class_device_create(tape_class
, NULL
, MKDEV(VIOTAPE_MAJOR
, i
), NULL
,
956 class_device_create(tape_class
, NULL
, MKDEV(VIOTAPE_MAJOR
, i
| 0x80),
957 NULL
, "iseries!nvt%d", i
);
958 printk(VIOTAPE_KERN_INFO
"tape iseries/vt%d is iSeries "
959 "resource %10.10s type %4.4s, model %3.3s\n",
960 i
, viotape_unitinfo
[i
].rsrcname
,
961 viotape_unitinfo
[i
].type
, viotape_unitinfo
[i
].model
);
965 static int viotape_remove(struct vio_dev
*vdev
)
967 int i
= vdev
->unit_address
;
969 class_device_destroy(tape_class
, MKDEV(VIOTAPE_MAJOR
, i
| 0x80));
970 class_device_destroy(tape_class
, MKDEV(VIOTAPE_MAJOR
, i
));
975 * viotape_device_table: Used by vio.c to match devices that we
978 static struct vio_device_id viotape_device_table
[] __devinitdata
= {
979 { "byte", "IBM,iSeries-viotape" },
982 MODULE_DEVICE_TABLE(vio
, viotape_device_table
);
984 static struct vio_driver viotape_driver
= {
985 .id_table
= viotape_device_table
,
986 .probe
= viotape_probe
,
987 .remove
= viotape_remove
,
990 .owner
= THIS_MODULE
,
995 int __init
viotap_init(void)
998 struct proc_dir_entry
*e
;
1000 if (!firmware_has_feature(FW_FEATURE_ISERIES
))
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)
1101 remove_proc_entry("iSeries/viotape", NULL
);
1102 vio_unregister_driver(&viotape_driver
);
1103 class_destroy(tape_class
);
1104 unregister_chrdev(VIOTAPE_MAJOR
, "viotape");
1105 if (viotape_unitinfo
)
1106 dma_free_coherent(iSeries_vio_dev
,
1107 sizeof(viotape_unitinfo
[0]) * VIOTAPE_MAX_TAPE
,
1108 viotape_unitinfo
, viotape_unitinfo_token
);
1109 viopath_close(viopath_hostLp
, viomajorsubtype_tape
, VIOTAPE_MAXREQ
+ 2);
1110 vio_clearHandler(viomajorsubtype_tape
);
1111 clear_op_struct_pool();
1114 MODULE_LICENSE("GPL");
1115 module_init(viotap_init
);
1116 module_exit(viotap_exit
);