Linux-2.6.12-rc2
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / char / viotape.c
blobaea3cbf5219d4a0ab89827f6179bf4118264279a
1 /* -*- linux-c -*-
2 * drivers/char/viotape.c
4 * iSeries Virtual Tape
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
32 * iSeries/vio.h
34 #include <linux/config.h>
35 #include <linux/version.h>
36 #include <linux/module.h>
37 #include <linux/kernel.h>
38 #include <linux/errno.h>
39 #include <linux/init.h>
40 #include <linux/wait.h>
41 #include <linux/spinlock.h>
42 #include <linux/mtio.h>
43 #include <linux/device.h>
44 #include <linux/dma-mapping.h>
45 #include <linux/fs.h>
46 #include <linux/cdev.h>
47 #include <linux/devfs_fs_kernel.h>
48 #include <linux/major.h>
49 #include <linux/completion.h>
50 #include <linux/proc_fs.h>
51 #include <linux/seq_file.h>
53 #include <asm/uaccess.h>
54 #include <asm/ioctls.h>
56 #include <asm/vio.h>
57 #include <asm/iSeries/vio.h>
58 #include <asm/iSeries/HvLpEvent.h>
59 #include <asm/iSeries/HvCallEvent.h>
60 #include <asm/iSeries/HvLpConfig.h>
62 #define VIOTAPE_VERSION "1.2"
63 #define VIOTAPE_MAXREQ 1
65 #define VIOTAPE_KERN_WARN KERN_WARNING "viotape: "
66 #define VIOTAPE_KERN_INFO KERN_INFO "viotape: "
68 static int viotape_numdev;
71 * The minor number follows the conventions of the SCSI tape drives. The
72 * rewind and mode are encoded in the minor #. We use this struct to break
73 * them out
75 struct viot_devinfo_struct {
76 int devno;
77 int mode;
78 int rewind;
81 #define VIOTAPOP_RESET 0
82 #define VIOTAPOP_FSF 1
83 #define VIOTAPOP_BSF 2
84 #define VIOTAPOP_FSR 3
85 #define VIOTAPOP_BSR 4
86 #define VIOTAPOP_WEOF 5
87 #define VIOTAPOP_REW 6
88 #define VIOTAPOP_NOP 7
89 #define VIOTAPOP_EOM 8
90 #define VIOTAPOP_ERASE 9
91 #define VIOTAPOP_SETBLK 10
92 #define VIOTAPOP_SETDENSITY 11
93 #define VIOTAPOP_SETPOS 12
94 #define VIOTAPOP_GETPOS 13
95 #define VIOTAPOP_SETPART 14
96 #define VIOTAPOP_UNLOAD 15
98 struct viotapelpevent {
99 struct HvLpEvent event;
100 u32 reserved;
101 u16 version;
102 u16 sub_type_result;
103 u16 tape;
104 u16 flags;
105 u32 token;
106 u64 len;
107 union {
108 struct {
109 u32 tape_op;
110 u32 count;
111 } op;
112 struct {
113 u32 type;
114 u32 resid;
115 u32 dsreg;
116 u32 gstat;
117 u32 erreg;
118 u32 file_no;
119 u32 block_no;
120 } get_status;
121 struct {
122 u32 block_no;
123 } get_pos;
124 } u;
127 enum viotapesubtype {
128 viotapeopen = 0x0001,
129 viotapeclose = 0x0002,
130 viotaperead = 0x0003,
131 viotapewrite = 0x0004,
132 viotapegetinfo = 0x0005,
133 viotapeop = 0x0006,
134 viotapegetpos = 0x0007,
135 viotapesetpos = 0x0008,
136 viotapegetstatus = 0x0009
139 enum viotaperc {
140 viotape_InvalidRange = 0x0601,
141 viotape_InvalidToken = 0x0602,
142 viotape_DMAError = 0x0603,
143 viotape_UseError = 0x0604,
144 viotape_ReleaseError = 0x0605,
145 viotape_InvalidTape = 0x0606,
146 viotape_InvalidOp = 0x0607,
147 viotape_TapeErr = 0x0608,
149 viotape_AllocTimedOut = 0x0640,
150 viotape_BOTEnc = 0x0641,
151 viotape_BlankTape = 0x0642,
152 viotape_BufferEmpty = 0x0643,
153 viotape_CleanCartFound = 0x0644,
154 viotape_CmdNotAllowed = 0x0645,
155 viotape_CmdNotSupported = 0x0646,
156 viotape_DataCheck = 0x0647,
157 viotape_DecompressErr = 0x0648,
158 viotape_DeviceTimeout = 0x0649,
159 viotape_DeviceUnavail = 0x064a,
160 viotape_DeviceBusy = 0x064b,
161 viotape_EndOfMedia = 0x064c,
162 viotape_EndOfTape = 0x064d,
163 viotape_EquipCheck = 0x064e,
164 viotape_InsufficientRs = 0x064f,
165 viotape_InvalidLogBlk = 0x0650,
166 viotape_LengthError = 0x0651,
167 viotape_LibDoorOpen = 0x0652,
168 viotape_LoadFailure = 0x0653,
169 viotape_NotCapable = 0x0654,
170 viotape_NotOperational = 0x0655,
171 viotape_NotReady = 0x0656,
172 viotape_OpCancelled = 0x0657,
173 viotape_PhyLinkErr = 0x0658,
174 viotape_RdyNotBOT = 0x0659,
175 viotape_TapeMark = 0x065a,
176 viotape_WriteProt = 0x065b
179 static const struct vio_error_entry viotape_err_table[] = {
180 { viotape_InvalidRange, EIO, "Internal error" },
181 { viotape_InvalidToken, EIO, "Internal error" },
182 { viotape_DMAError, EIO, "DMA error" },
183 { viotape_UseError, EIO, "Internal error" },
184 { viotape_ReleaseError, EIO, "Internal error" },
185 { viotape_InvalidTape, EIO, "Invalid tape device" },
186 { viotape_InvalidOp, EIO, "Invalid operation" },
187 { viotape_TapeErr, EIO, "Tape error" },
188 { viotape_AllocTimedOut, EBUSY, "Allocate timed out" },
189 { viotape_BOTEnc, EIO, "Beginning of tape encountered" },
190 { viotape_BlankTape, EIO, "Blank tape" },
191 { viotape_BufferEmpty, EIO, "Buffer empty" },
192 { viotape_CleanCartFound, ENOMEDIUM, "Cleaning cartridge found" },
193 { viotape_CmdNotAllowed, EIO, "Command not allowed" },
194 { viotape_CmdNotSupported, EIO, "Command not supported" },
195 { viotape_DataCheck, EIO, "Data check" },
196 { viotape_DecompressErr, EIO, "Decompression error" },
197 { viotape_DeviceTimeout, EBUSY, "Device timeout" },
198 { viotape_DeviceUnavail, EIO, "Device unavailable" },
199 { viotape_DeviceBusy, EBUSY, "Device busy" },
200 { viotape_EndOfMedia, ENOSPC, "End of media" },
201 { viotape_EndOfTape, ENOSPC, "End of tape" },
202 { viotape_EquipCheck, EIO, "Equipment check" },
203 { viotape_InsufficientRs, EOVERFLOW, "Insufficient tape resources" },
204 { viotape_InvalidLogBlk, EIO, "Invalid logical block location" },
205 { viotape_LengthError, EOVERFLOW, "Length error" },
206 { viotape_LibDoorOpen, EBUSY, "Door open" },
207 { viotape_LoadFailure, ENOMEDIUM, "Load failure" },
208 { viotape_NotCapable, EIO, "Not capable" },
209 { viotape_NotOperational, EIO, "Not operational" },
210 { viotape_NotReady, EIO, "Not ready" },
211 { viotape_OpCancelled, EIO, "Operation cancelled" },
212 { viotape_PhyLinkErr, EIO, "Physical link error" },
213 { viotape_RdyNotBOT, EIO, "Ready but not beginning of tape" },
214 { viotape_TapeMark, EIO, "Tape mark" },
215 { viotape_WriteProt, EROFS, "Write protection error" },
216 { 0, 0, NULL },
219 /* Maximum number of tapes we support */
220 #define VIOTAPE_MAX_TAPE HVMAXARCHITECTEDVIRTUALTAPES
221 #define MAX_PARTITIONS 4
223 /* defines for current tape state */
224 #define VIOT_IDLE 0
225 #define VIOT_READING 1
226 #define VIOT_WRITING 2
228 /* Our info on the tapes */
229 struct tape_descr {
230 char rsrcname[10];
231 char type[4];
232 char model[3];
235 static struct tape_descr *viotape_unitinfo;
236 static dma_addr_t viotape_unitinfo_token;
238 static struct mtget viomtget[VIOTAPE_MAX_TAPE];
240 static struct class_simple *tape_class;
242 static struct device *tape_device[VIOTAPE_MAX_TAPE];
245 * maintain the current state of each tape (and partition)
246 * so that we know when to write EOF marks.
248 static struct {
249 unsigned char cur_part;
250 int dev_handle;
251 unsigned char part_stat_rwi[MAX_PARTITIONS];
252 } state[VIOTAPE_MAX_TAPE];
254 /* We single-thread */
255 static struct semaphore reqSem;
258 * When we send a request, we use this struct to get the response back
259 * from the interrupt handler
261 struct op_struct {
262 void *buffer;
263 dma_addr_t dmaaddr;
264 size_t count;
265 int rc;
266 int non_blocking;
267 struct completion com;
268 struct device *dev;
269 struct op_struct *next;
272 static spinlock_t op_struct_list_lock;
273 static struct op_struct *op_struct_list;
275 /* forward declaration to resolve interdependence */
276 static int chg_state(int index, unsigned char new_state, struct file *file);
278 /* procfs support */
279 static int proc_viotape_show(struct seq_file *m, void *v)
281 int i;
283 seq_printf(m, "viotape driver version " VIOTAPE_VERSION "\n");
284 for (i = 0; i < viotape_numdev; i++) {
285 seq_printf(m, "viotape device %d is iSeries resource %10.10s"
286 "type %4.4s, model %3.3s\n",
287 i, viotape_unitinfo[i].rsrcname,
288 viotape_unitinfo[i].type,
289 viotape_unitinfo[i].model);
291 return 0;
294 static int proc_viotape_open(struct inode *inode, struct file *file)
296 return single_open(file, proc_viotape_show, NULL);
299 static struct file_operations proc_viotape_operations = {
300 .open = proc_viotape_open,
301 .read = seq_read,
302 .llseek = seq_lseek,
303 .release = single_release,
306 /* Decode the device minor number into its parts */
307 void get_dev_info(struct inode *ino, struct viot_devinfo_struct *devi)
309 devi->devno = iminor(ino) & 0x1F;
310 devi->mode = (iminor(ino) & 0x60) >> 5;
311 /* if bit is set in the minor, do _not_ rewind automatically */
312 devi->rewind = (iminor(ino) & 0x80) == 0;
315 /* This is called only from the exit and init paths, so no need for locking */
316 static void clear_op_struct_pool(void)
318 while (op_struct_list) {
319 struct op_struct *toFree = op_struct_list;
320 op_struct_list = op_struct_list->next;
321 kfree(toFree);
325 /* Likewise, this is only called from the init path */
326 static int add_op_structs(int structs)
328 int i;
330 for (i = 0; i < structs; ++i) {
331 struct op_struct *new_struct =
332 kmalloc(sizeof(*new_struct), GFP_KERNEL);
333 if (!new_struct) {
334 clear_op_struct_pool();
335 return -ENOMEM;
337 new_struct->next = op_struct_list;
338 op_struct_list = new_struct;
340 return 0;
343 /* Allocate an op structure from our pool */
344 static struct op_struct *get_op_struct(void)
346 struct op_struct *retval;
347 unsigned long flags;
349 spin_lock_irqsave(&op_struct_list_lock, flags);
350 retval = op_struct_list;
351 if (retval)
352 op_struct_list = retval->next;
353 spin_unlock_irqrestore(&op_struct_list_lock, flags);
354 if (retval) {
355 memset(retval, 0, sizeof(*retval));
356 init_completion(&retval->com);
359 return retval;
362 /* Return an op structure to our pool */
363 static void free_op_struct(struct op_struct *op_struct)
365 unsigned long flags;
367 spin_lock_irqsave(&op_struct_list_lock, flags);
368 op_struct->next = op_struct_list;
369 op_struct_list = op_struct;
370 spin_unlock_irqrestore(&op_struct_list_lock, flags);
373 /* Map our tape return codes to errno values */
374 int tape_rc_to_errno(int tape_rc, char *operation, int tapeno)
376 const struct vio_error_entry *err;
378 if (tape_rc == 0)
379 return 0;
381 err = vio_lookup_rc(viotape_err_table, tape_rc);
382 printk(VIOTAPE_KERN_WARN "error(%s) 0x%04x on Device %d (%-10s): %s\n",
383 operation, tape_rc, tapeno,
384 viotape_unitinfo[tapeno].rsrcname, err->msg);
385 return -err->errno;
388 /* Get info on all tapes from OS/400 */
389 static int get_viotape_info(void)
391 HvLpEvent_Rc hvrc;
392 int i;
393 size_t len = sizeof(*viotape_unitinfo) * VIOTAPE_MAX_TAPE;
394 struct op_struct *op = get_op_struct();
396 if (op == NULL)
397 return -ENOMEM;
399 viotape_unitinfo = dma_alloc_coherent(iSeries_vio_dev, len,
400 &viotape_unitinfo_token, GFP_ATOMIC);
401 if (viotape_unitinfo == NULL) {
402 free_op_struct(op);
403 return -ENOMEM;
406 memset(viotape_unitinfo, 0, len);
408 hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
409 HvLpEvent_Type_VirtualIo,
410 viomajorsubtype_tape | viotapegetinfo,
411 HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck,
412 viopath_sourceinst(viopath_hostLp),
413 viopath_targetinst(viopath_hostLp),
414 (u64) (unsigned long) op, VIOVERSION << 16,
415 viotape_unitinfo_token, len, 0, 0);
416 if (hvrc != HvLpEvent_Rc_Good) {
417 printk(VIOTAPE_KERN_WARN "hv error on op %d\n",
418 (int)hvrc);
419 free_op_struct(op);
420 return -EIO;
423 wait_for_completion(&op->com);
425 free_op_struct(op);
427 for (i = 0;
428 ((i < VIOTAPE_MAX_TAPE) && (viotape_unitinfo[i].rsrcname[0]));
429 i++)
430 viotape_numdev++;
431 return 0;
435 /* Write */
436 static ssize_t viotap_write(struct file *file, const char *buf,
437 size_t count, loff_t * ppos)
439 HvLpEvent_Rc hvrc;
440 unsigned short flags = file->f_flags;
441 int noblock = ((flags & O_NONBLOCK) != 0);
442 ssize_t ret;
443 struct viot_devinfo_struct devi;
444 struct op_struct *op = get_op_struct();
446 if (op == NULL)
447 return -ENOMEM;
449 get_dev_info(file->f_dentry->d_inode, &devi);
452 * We need to make sure we can send a request. We use
453 * a semaphore to keep track of # requests in use. If
454 * we are non-blocking, make sure we don't block on the
455 * semaphore
457 if (noblock) {
458 if (down_trylock(&reqSem)) {
459 ret = -EWOULDBLOCK;
460 goto free_op;
462 } else
463 down(&reqSem);
465 /* Allocate a DMA buffer */
466 op->dev = tape_device[devi.devno];
467 op->buffer = dma_alloc_coherent(op->dev, count, &op->dmaaddr,
468 GFP_ATOMIC);
470 if (op->buffer == NULL) {
471 printk(VIOTAPE_KERN_WARN
472 "error allocating dma buffer for len %ld\n",
473 count);
474 ret = -EFAULT;
475 goto up_sem;
478 /* Copy the data into the buffer */
479 if (copy_from_user(op->buffer, buf, count)) {
480 printk(VIOTAPE_KERN_WARN "tape: error on copy from user\n");
481 ret = -EFAULT;
482 goto free_dma;
485 op->non_blocking = noblock;
486 init_completion(&op->com);
487 op->count = count;
489 hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
490 HvLpEvent_Type_VirtualIo,
491 viomajorsubtype_tape | viotapewrite,
492 HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck,
493 viopath_sourceinst(viopath_hostLp),
494 viopath_targetinst(viopath_hostLp),
495 (u64)(unsigned long)op, VIOVERSION << 16,
496 ((u64)devi.devno << 48) | op->dmaaddr, count, 0, 0);
497 if (hvrc != HvLpEvent_Rc_Good) {
498 printk(VIOTAPE_KERN_WARN "hv error on op %d\n",
499 (int)hvrc);
500 ret = -EIO;
501 goto free_dma;
504 if (noblock)
505 return count;
507 wait_for_completion(&op->com);
509 if (op->rc)
510 ret = tape_rc_to_errno(op->rc, "write", devi.devno);
511 else {
512 chg_state(devi.devno, VIOT_WRITING, file);
513 ret = op->count;
516 free_dma:
517 dma_free_coherent(op->dev, count, op->buffer, op->dmaaddr);
518 up_sem:
519 up(&reqSem);
520 free_op:
521 free_op_struct(op);
522 return ret;
525 /* read */
526 static ssize_t viotap_read(struct file *file, char *buf, size_t count,
527 loff_t *ptr)
529 HvLpEvent_Rc hvrc;
530 unsigned short flags = file->f_flags;
531 struct op_struct *op = get_op_struct();
532 int noblock = ((flags & O_NONBLOCK) != 0);
533 ssize_t ret;
534 struct viot_devinfo_struct devi;
536 if (op == NULL)
537 return -ENOMEM;
539 get_dev_info(file->f_dentry->d_inode, &devi);
542 * We need to make sure we can send a request. We use
543 * a semaphore to keep track of # requests in use. If
544 * we are non-blocking, make sure we don't block on the
545 * semaphore
547 if (noblock) {
548 if (down_trylock(&reqSem)) {
549 ret = -EWOULDBLOCK;
550 goto free_op;
552 } else
553 down(&reqSem);
555 chg_state(devi.devno, VIOT_READING, file);
557 /* Allocate a DMA buffer */
558 op->dev = tape_device[devi.devno];
559 op->buffer = dma_alloc_coherent(op->dev, count, &op->dmaaddr,
560 GFP_ATOMIC);
561 if (op->buffer == NULL) {
562 ret = -EFAULT;
563 goto up_sem;
566 op->count = count;
567 init_completion(&op->com);
569 hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
570 HvLpEvent_Type_VirtualIo,
571 viomajorsubtype_tape | viotaperead,
572 HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck,
573 viopath_sourceinst(viopath_hostLp),
574 viopath_targetinst(viopath_hostLp),
575 (u64)(unsigned long)op, VIOVERSION << 16,
576 ((u64)devi.devno << 48) | op->dmaaddr, count, 0, 0);
577 if (hvrc != HvLpEvent_Rc_Good) {
578 printk(VIOTAPE_KERN_WARN "tape hv error on op %d\n",
579 (int)hvrc);
580 ret = -EIO;
581 goto free_dma;
584 wait_for_completion(&op->com);
586 if (op->rc)
587 ret = tape_rc_to_errno(op->rc, "read", devi.devno);
588 else {
589 ret = op->count;
590 if (ret && copy_to_user(buf, op->buffer, ret)) {
591 printk(VIOTAPE_KERN_WARN "error on copy_to_user\n");
592 ret = -EFAULT;
596 free_dma:
597 dma_free_coherent(op->dev, count, op->buffer, op->dmaaddr);
598 up_sem:
599 up(&reqSem);
600 free_op:
601 free_op_struct(op);
602 return ret;
605 /* ioctl */
606 static int viotap_ioctl(struct inode *inode, struct file *file,
607 unsigned int cmd, unsigned long arg)
609 HvLpEvent_Rc hvrc;
610 int ret;
611 struct viot_devinfo_struct devi;
612 struct mtop mtc;
613 u32 myOp;
614 struct op_struct *op = get_op_struct();
616 if (op == NULL)
617 return -ENOMEM;
619 get_dev_info(file->f_dentry->d_inode, &devi);
621 down(&reqSem);
623 ret = -EINVAL;
625 switch (cmd) {
626 case MTIOCTOP:
627 ret = -EFAULT;
629 * inode is null if and only if we (the kernel)
630 * made the request
632 if (inode == NULL)
633 memcpy(&mtc, (void *) arg, sizeof(struct mtop));
634 else if (copy_from_user((char *)&mtc, (char *)arg,
635 sizeof(struct mtop)))
636 goto free_op;
638 ret = -EIO;
639 switch (mtc.mt_op) {
640 case MTRESET:
641 myOp = VIOTAPOP_RESET;
642 break;
643 case MTFSF:
644 myOp = VIOTAPOP_FSF;
645 break;
646 case MTBSF:
647 myOp = VIOTAPOP_BSF;
648 break;
649 case MTFSR:
650 myOp = VIOTAPOP_FSR;
651 break;
652 case MTBSR:
653 myOp = VIOTAPOP_BSR;
654 break;
655 case MTWEOF:
656 myOp = VIOTAPOP_WEOF;
657 break;
658 case MTREW:
659 myOp = VIOTAPOP_REW;
660 break;
661 case MTNOP:
662 myOp = VIOTAPOP_NOP;
663 break;
664 case MTEOM:
665 myOp = VIOTAPOP_EOM;
666 break;
667 case MTERASE:
668 myOp = VIOTAPOP_ERASE;
669 break;
670 case MTSETBLK:
671 myOp = VIOTAPOP_SETBLK;
672 break;
673 case MTSETDENSITY:
674 myOp = VIOTAPOP_SETDENSITY;
675 break;
676 case MTTELL:
677 myOp = VIOTAPOP_GETPOS;
678 break;
679 case MTSEEK:
680 myOp = VIOTAPOP_SETPOS;
681 break;
682 case MTSETPART:
683 myOp = VIOTAPOP_SETPART;
684 break;
685 case MTOFFL:
686 myOp = VIOTAPOP_UNLOAD;
687 break;
688 default:
689 printk(VIOTAPE_KERN_WARN "MTIOCTOP called "
690 "with invalid op 0x%x\n", mtc.mt_op);
691 goto free_op;
695 * if we moved the head, we are no longer
696 * reading or writing
698 switch (mtc.mt_op) {
699 case MTFSF:
700 case MTBSF:
701 case MTFSR:
702 case MTBSR:
703 case MTTELL:
704 case MTSEEK:
705 case MTREW:
706 chg_state(devi.devno, VIOT_IDLE, file);
709 init_completion(&op->com);
710 hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
711 HvLpEvent_Type_VirtualIo,
712 viomajorsubtype_tape | viotapeop,
713 HvLpEvent_AckInd_DoAck,
714 HvLpEvent_AckType_ImmediateAck,
715 viopath_sourceinst(viopath_hostLp),
716 viopath_targetinst(viopath_hostLp),
717 (u64)(unsigned long)op,
718 VIOVERSION << 16,
719 ((u64)devi.devno << 48), 0,
720 (((u64)myOp) << 32) | mtc.mt_count, 0);
721 if (hvrc != HvLpEvent_Rc_Good) {
722 printk(VIOTAPE_KERN_WARN "hv error on op %d\n",
723 (int)hvrc);
724 goto free_op;
726 wait_for_completion(&op->com);
727 ret = tape_rc_to_errno(op->rc, "tape operation", devi.devno);
728 goto free_op;
730 case MTIOCGET:
731 ret = -EIO;
732 init_completion(&op->com);
733 hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
734 HvLpEvent_Type_VirtualIo,
735 viomajorsubtype_tape | viotapegetstatus,
736 HvLpEvent_AckInd_DoAck,
737 HvLpEvent_AckType_ImmediateAck,
738 viopath_sourceinst(viopath_hostLp),
739 viopath_targetinst(viopath_hostLp),
740 (u64)(unsigned long)op, VIOVERSION << 16,
741 ((u64)devi.devno << 48), 0, 0, 0);
742 if (hvrc != HvLpEvent_Rc_Good) {
743 printk(VIOTAPE_KERN_WARN "hv error on op %d\n",
744 (int)hvrc);
745 goto free_op;
747 wait_for_completion(&op->com);
749 /* Operation is complete - grab the error code */
750 ret = tape_rc_to_errno(op->rc, "get status", devi.devno);
751 free_op_struct(op);
752 up(&reqSem);
754 if ((ret == 0) && copy_to_user((void *)arg,
755 &viomtget[devi.devno],
756 sizeof(viomtget[0])))
757 ret = -EFAULT;
758 return ret;
759 case MTIOCPOS:
760 printk(VIOTAPE_KERN_WARN "Got an (unsupported) MTIOCPOS\n");
761 break;
762 default:
763 printk(VIOTAPE_KERN_WARN "got an unsupported ioctl 0x%0x\n",
764 cmd);
765 break;
768 free_op:
769 free_op_struct(op);
770 up(&reqSem);
771 return ret;
774 static int viotap_open(struct inode *inode, struct file *file)
776 HvLpEvent_Rc hvrc;
777 struct viot_devinfo_struct devi;
778 int ret;
779 struct op_struct *op = get_op_struct();
781 if (op == NULL)
782 return -ENOMEM;
784 get_dev_info(file->f_dentry->d_inode, &devi);
786 /* Note: We currently only support one mode! */
787 if ((devi.devno >= viotape_numdev) || (devi.mode)) {
788 ret = -ENODEV;
789 goto free_op;
792 init_completion(&op->com);
794 hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
795 HvLpEvent_Type_VirtualIo,
796 viomajorsubtype_tape | viotapeopen,
797 HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck,
798 viopath_sourceinst(viopath_hostLp),
799 viopath_targetinst(viopath_hostLp),
800 (u64)(unsigned long)op, VIOVERSION << 16,
801 ((u64)devi.devno << 48), 0, 0, 0);
802 if (hvrc != 0) {
803 printk(VIOTAPE_KERN_WARN "bad rc on signalLpEvent %d\n",
804 (int) hvrc);
805 ret = -EIO;
806 goto free_op;
809 wait_for_completion(&op->com);
810 ret = tape_rc_to_errno(op->rc, "open", devi.devno);
812 free_op:
813 free_op_struct(op);
814 return ret;
818 static int viotap_release(struct inode *inode, struct file *file)
820 HvLpEvent_Rc hvrc;
821 struct viot_devinfo_struct devi;
822 int ret = 0;
823 struct op_struct *op = get_op_struct();
825 if (op == NULL)
826 return -ENOMEM;
827 init_completion(&op->com);
829 get_dev_info(file->f_dentry->d_inode, &devi);
831 if (devi.devno >= viotape_numdev) {
832 ret = -ENODEV;
833 goto free_op;
836 chg_state(devi.devno, VIOT_IDLE, file);
838 if (devi.rewind) {
839 hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
840 HvLpEvent_Type_VirtualIo,
841 viomajorsubtype_tape | viotapeop,
842 HvLpEvent_AckInd_DoAck,
843 HvLpEvent_AckType_ImmediateAck,
844 viopath_sourceinst(viopath_hostLp),
845 viopath_targetinst(viopath_hostLp),
846 (u64)(unsigned long)op, VIOVERSION << 16,
847 ((u64)devi.devno << 48), 0,
848 ((u64)VIOTAPOP_REW) << 32, 0);
849 wait_for_completion(&op->com);
851 tape_rc_to_errno(op->rc, "rewind", devi.devno);
854 hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
855 HvLpEvent_Type_VirtualIo,
856 viomajorsubtype_tape | viotapeclose,
857 HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck,
858 viopath_sourceinst(viopath_hostLp),
859 viopath_targetinst(viopath_hostLp),
860 (u64)(unsigned long)op, VIOVERSION << 16,
861 ((u64)devi.devno << 48), 0, 0, 0);
862 if (hvrc != 0) {
863 printk(VIOTAPE_KERN_WARN "bad rc on signalLpEvent %d\n",
864 (int) hvrc);
865 ret = -EIO;
866 goto free_op;
869 wait_for_completion(&op->com);
871 if (op->rc)
872 printk(VIOTAPE_KERN_WARN "close failed\n");
874 free_op:
875 free_op_struct(op);
876 return ret;
879 struct file_operations viotap_fops = {
880 owner: THIS_MODULE,
881 read: viotap_read,
882 write: viotap_write,
883 ioctl: viotap_ioctl,
884 open: viotap_open,
885 release: viotap_release,
888 /* Handle interrupt events for tape */
889 static void vioHandleTapeEvent(struct HvLpEvent *event)
891 int tapeminor;
892 struct op_struct *op;
893 struct viotapelpevent *tevent = (struct viotapelpevent *)event;
895 if (event == NULL) {
896 /* Notification that a partition went away! */
897 if (!viopath_isactive(viopath_hostLp)) {
898 /* TODO! Clean up */
900 return;
903 tapeminor = event->xSubtype & VIOMINOR_SUBTYPE_MASK;
904 op = (struct op_struct *)event->xCorrelationToken;
905 switch (tapeminor) {
906 case viotapegetinfo:
907 case viotapeopen:
908 case viotapeclose:
909 op->rc = tevent->sub_type_result;
910 complete(&op->com);
911 break;
912 case viotaperead:
913 op->rc = tevent->sub_type_result;
914 op->count = tevent->len;
915 complete(&op->com);
916 break;
917 case viotapewrite:
918 if (op->non_blocking) {
919 dma_free_coherent(op->dev, op->count,
920 op->buffer, op->dmaaddr);
921 free_op_struct(op);
922 up(&reqSem);
923 } else {
924 op->rc = tevent->sub_type_result;
925 op->count = tevent->len;
926 complete(&op->com);
928 break;
929 case viotapeop:
930 case viotapegetpos:
931 case viotapesetpos:
932 case viotapegetstatus:
933 if (op) {
934 op->count = tevent->u.op.count;
935 op->rc = tevent->sub_type_result;
936 if (!op->non_blocking)
937 complete(&op->com);
939 break;
940 default:
941 printk(VIOTAPE_KERN_WARN "weird ack\n");
945 static int viotape_probe(struct vio_dev *vdev, const struct vio_device_id *id)
947 char tapename[32];
948 int i = vdev->unit_address;
949 int j;
951 if (i >= viotape_numdev)
952 return -ENODEV;
954 tape_device[i] = &vdev->dev;
956 state[i].cur_part = 0;
957 for (j = 0; j < MAX_PARTITIONS; ++j)
958 state[i].part_stat_rwi[j] = VIOT_IDLE;
959 class_simple_device_add(tape_class, MKDEV(VIOTAPE_MAJOR, i), NULL,
960 "iseries!vt%d", i);
961 class_simple_device_add(tape_class, MKDEV(VIOTAPE_MAJOR, i | 0x80),
962 NULL, "iseries!nvt%d", i);
963 devfs_mk_cdev(MKDEV(VIOTAPE_MAJOR, i), S_IFCHR | S_IRUSR | S_IWUSR,
964 "iseries/vt%d", i);
965 devfs_mk_cdev(MKDEV(VIOTAPE_MAJOR, i | 0x80),
966 S_IFCHR | S_IRUSR | S_IWUSR, "iseries/nvt%d", i);
967 sprintf(tapename, "iseries/vt%d", i);
968 state[i].dev_handle = devfs_register_tape(tapename);
969 printk(VIOTAPE_KERN_INFO "tape %s is iSeries "
970 "resource %10.10s type %4.4s, model %3.3s\n",
971 tapename, viotape_unitinfo[i].rsrcname,
972 viotape_unitinfo[i].type, viotape_unitinfo[i].model);
973 return 0;
976 static int viotape_remove(struct vio_dev *vdev)
978 int i = vdev->unit_address;
980 devfs_remove("iseries/nvt%d", i);
981 devfs_remove("iseries/vt%d", i);
982 devfs_unregister_tape(state[i].dev_handle);
983 class_simple_device_remove(MKDEV(VIOTAPE_MAJOR, i | 0x80));
984 class_simple_device_remove(MKDEV(VIOTAPE_MAJOR, i));
985 return 0;
989 * viotape_device_table: Used by vio.c to match devices that we
990 * support.
992 static struct vio_device_id viotape_device_table[] __devinitdata = {
993 { "viotape", "" },
994 { 0, }
997 MODULE_DEVICE_TABLE(vio, viotape_device_table);
998 static struct vio_driver viotape_driver = {
999 .name = "viotape",
1000 .id_table = viotape_device_table,
1001 .probe = viotape_probe,
1002 .remove = viotape_remove
1006 int __init viotap_init(void)
1008 int ret;
1009 struct proc_dir_entry *e;
1011 op_struct_list = NULL;
1012 if ((ret = add_op_structs(VIOTAPE_MAXREQ)) < 0) {
1013 printk(VIOTAPE_KERN_WARN "couldn't allocate op structs\n");
1014 return ret;
1016 spin_lock_init(&op_struct_list_lock);
1018 sema_init(&reqSem, VIOTAPE_MAXREQ);
1020 if (viopath_hostLp == HvLpIndexInvalid) {
1021 vio_set_hostlp();
1022 if (viopath_hostLp == HvLpIndexInvalid) {
1023 ret = -ENODEV;
1024 goto clear_op;
1028 ret = viopath_open(viopath_hostLp, viomajorsubtype_tape,
1029 VIOTAPE_MAXREQ + 2);
1030 if (ret) {
1031 printk(VIOTAPE_KERN_WARN
1032 "error on viopath_open to hostlp %d\n", ret);
1033 ret = -EIO;
1034 goto clear_op;
1037 printk(VIOTAPE_KERN_INFO "vers " VIOTAPE_VERSION
1038 ", hosting partition %d\n", viopath_hostLp);
1040 vio_setHandler(viomajorsubtype_tape, vioHandleTapeEvent);
1042 ret = register_chrdev(VIOTAPE_MAJOR, "viotape", &viotap_fops);
1043 if (ret < 0) {
1044 printk(VIOTAPE_KERN_WARN "Error registering viotape device\n");
1045 goto clear_handler;
1048 tape_class = class_simple_create(THIS_MODULE, "tape");
1049 if (IS_ERR(tape_class)) {
1050 printk(VIOTAPE_KERN_WARN "Unable to allocat class\n");
1051 ret = PTR_ERR(tape_class);
1052 goto unreg_chrdev;
1055 if ((ret = get_viotape_info()) < 0) {
1056 printk(VIOTAPE_KERN_WARN "Unable to obtain virtual device information");
1057 goto unreg_class;
1060 ret = vio_register_driver(&viotape_driver);
1061 if (ret)
1062 goto unreg_class;
1064 e = create_proc_entry("iSeries/viotape", S_IFREG|S_IRUGO, NULL);
1065 if (e) {
1066 e->owner = THIS_MODULE;
1067 e->proc_fops = &proc_viotape_operations;
1070 return 0;
1072 unreg_class:
1073 class_simple_destroy(tape_class);
1074 unreg_chrdev:
1075 unregister_chrdev(VIOTAPE_MAJOR, "viotape");
1076 clear_handler:
1077 vio_clearHandler(viomajorsubtype_tape);
1078 viopath_close(viopath_hostLp, viomajorsubtype_tape, VIOTAPE_MAXREQ + 2);
1079 clear_op:
1080 clear_op_struct_pool();
1081 return ret;
1084 /* Give a new state to the tape object */
1085 static int chg_state(int index, unsigned char new_state, struct file *file)
1087 unsigned char *cur_state =
1088 &state[index].part_stat_rwi[state[index].cur_part];
1089 int rc = 0;
1091 /* if the same state, don't bother */
1092 if (*cur_state == new_state)
1093 return 0;
1095 /* write an EOF if changing from writing to some other state */
1096 if (*cur_state == VIOT_WRITING) {
1097 struct mtop write_eof = { MTWEOF, 1 };
1099 rc = viotap_ioctl(NULL, file, MTIOCTOP,
1100 (unsigned long)&write_eof);
1102 *cur_state = new_state;
1103 return rc;
1106 /* Cleanup */
1107 static void __exit viotap_exit(void)
1109 int ret;
1111 remove_proc_entry("iSeries/viotape", NULL);
1112 vio_unregister_driver(&viotape_driver);
1113 class_simple_destroy(tape_class);
1114 ret = unregister_chrdev(VIOTAPE_MAJOR, "viotape");
1115 if (ret < 0)
1116 printk(VIOTAPE_KERN_WARN "Error unregistering device: %d\n",
1117 ret);
1118 if (viotape_unitinfo)
1119 dma_free_coherent(iSeries_vio_dev,
1120 sizeof(viotape_unitinfo[0]) * VIOTAPE_MAX_TAPE,
1121 viotape_unitinfo, viotape_unitinfo_token);
1122 viopath_close(viopath_hostLp, viomajorsubtype_tape, VIOTAPE_MAXREQ + 2);
1123 vio_clearHandler(viomajorsubtype_tape);
1124 clear_op_struct_pool();
1127 MODULE_LICENSE("GPL");
1128 module_init(viotap_init);
1129 module_exit(viotap_exit);