[ARM] pxa: Gumstix Verdex PCMCIA support
[linux-2.6/verdex.git] / drivers / char / viotape.c
blob042c8149a6d12248cdbbf2e212628cf497f8f1ea
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
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/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>
43 #include <linux/fs.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>
49 #include <linux/smp_lock.h>
51 #include <asm/uaccess.h>
52 #include <asm/ioctls.h>
53 #include <asm/firmware.h>
54 #include <asm/vio.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
71 * them out
73 struct viot_devinfo_struct {
74 int devno;
75 int mode;
76 int rewind;
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 enum viotaperc {
97 viotape_InvalidRange = 0x0601,
98 viotape_InvalidToken = 0x0602,
99 viotape_DMAError = 0x0603,
100 viotape_UseError = 0x0604,
101 viotape_ReleaseError = 0x0605,
102 viotape_InvalidTape = 0x0606,
103 viotape_InvalidOp = 0x0607,
104 viotape_TapeErr = 0x0608,
106 viotape_AllocTimedOut = 0x0640,
107 viotape_BOTEnc = 0x0641,
108 viotape_BlankTape = 0x0642,
109 viotape_BufferEmpty = 0x0643,
110 viotape_CleanCartFound = 0x0644,
111 viotape_CmdNotAllowed = 0x0645,
112 viotape_CmdNotSupported = 0x0646,
113 viotape_DataCheck = 0x0647,
114 viotape_DecompressErr = 0x0648,
115 viotape_DeviceTimeout = 0x0649,
116 viotape_DeviceUnavail = 0x064a,
117 viotape_DeviceBusy = 0x064b,
118 viotape_EndOfMedia = 0x064c,
119 viotape_EndOfTape = 0x064d,
120 viotape_EquipCheck = 0x064e,
121 viotape_InsufficientRs = 0x064f,
122 viotape_InvalidLogBlk = 0x0650,
123 viotape_LengthError = 0x0651,
124 viotape_LibDoorOpen = 0x0652,
125 viotape_LoadFailure = 0x0653,
126 viotape_NotCapable = 0x0654,
127 viotape_NotOperational = 0x0655,
128 viotape_NotReady = 0x0656,
129 viotape_OpCancelled = 0x0657,
130 viotape_PhyLinkErr = 0x0658,
131 viotape_RdyNotBOT = 0x0659,
132 viotape_TapeMark = 0x065a,
133 viotape_WriteProt = 0x065b
136 static const struct vio_error_entry viotape_err_table[] = {
137 { viotape_InvalidRange, EIO, "Internal error" },
138 { viotape_InvalidToken, EIO, "Internal error" },
139 { viotape_DMAError, EIO, "DMA error" },
140 { viotape_UseError, EIO, "Internal error" },
141 { viotape_ReleaseError, EIO, "Internal error" },
142 { viotape_InvalidTape, EIO, "Invalid tape device" },
143 { viotape_InvalidOp, EIO, "Invalid operation" },
144 { viotape_TapeErr, EIO, "Tape error" },
145 { viotape_AllocTimedOut, EBUSY, "Allocate timed out" },
146 { viotape_BOTEnc, EIO, "Beginning of tape encountered" },
147 { viotape_BlankTape, EIO, "Blank tape" },
148 { viotape_BufferEmpty, EIO, "Buffer empty" },
149 { viotape_CleanCartFound, ENOMEDIUM, "Cleaning cartridge found" },
150 { viotape_CmdNotAllowed, EIO, "Command not allowed" },
151 { viotape_CmdNotSupported, EIO, "Command not supported" },
152 { viotape_DataCheck, EIO, "Data check" },
153 { viotape_DecompressErr, EIO, "Decompression error" },
154 { viotape_DeviceTimeout, EBUSY, "Device timeout" },
155 { viotape_DeviceUnavail, EIO, "Device unavailable" },
156 { viotape_DeviceBusy, EBUSY, "Device busy" },
157 { viotape_EndOfMedia, ENOSPC, "End of media" },
158 { viotape_EndOfTape, ENOSPC, "End of tape" },
159 { viotape_EquipCheck, EIO, "Equipment check" },
160 { viotape_InsufficientRs, EOVERFLOW, "Insufficient tape resources" },
161 { viotape_InvalidLogBlk, EIO, "Invalid logical block location" },
162 { viotape_LengthError, EOVERFLOW, "Length error" },
163 { viotape_LibDoorOpen, EBUSY, "Door open" },
164 { viotape_LoadFailure, ENOMEDIUM, "Load failure" },
165 { viotape_NotCapable, EIO, "Not capable" },
166 { viotape_NotOperational, EIO, "Not operational" },
167 { viotape_NotReady, EIO, "Not ready" },
168 { viotape_OpCancelled, EIO, "Operation cancelled" },
169 { viotape_PhyLinkErr, EIO, "Physical link error" },
170 { viotape_RdyNotBOT, EIO, "Ready but not beginning of tape" },
171 { viotape_TapeMark, EIO, "Tape mark" },
172 { viotape_WriteProt, EROFS, "Write protection error" },
173 { 0, 0, NULL },
176 /* Maximum number of tapes we support */
177 #define VIOTAPE_MAX_TAPE HVMAXARCHITECTEDVIRTUALTAPES
178 #define MAX_PARTITIONS 4
180 /* defines for current tape state */
181 #define VIOT_IDLE 0
182 #define VIOT_READING 1
183 #define VIOT_WRITING 2
185 /* Our info on the tapes */
186 static struct {
187 const char *rsrcname;
188 const char *type;
189 const char *model;
190 } viotape_unitinfo[VIOTAPE_MAX_TAPE];
192 static struct mtget viomtget[VIOTAPE_MAX_TAPE];
194 static struct class *tape_class;
196 static struct device *tape_device[VIOTAPE_MAX_TAPE];
199 * maintain the current state of each tape (and partition)
200 * so that we know when to write EOF marks.
202 static struct {
203 unsigned char cur_part;
204 unsigned char part_stat_rwi[MAX_PARTITIONS];
205 } state[VIOTAPE_MAX_TAPE];
207 /* We single-thread */
208 static struct semaphore reqSem;
211 * When we send a request, we use this struct to get the response back
212 * from the interrupt handler
214 struct op_struct {
215 void *buffer;
216 dma_addr_t dmaaddr;
217 size_t count;
218 int rc;
219 int non_blocking;
220 struct completion com;
221 struct device *dev;
222 struct op_struct *next;
225 static spinlock_t op_struct_list_lock;
226 static struct op_struct *op_struct_list;
228 /* forward declaration to resolve interdependence */
229 static int chg_state(int index, unsigned char new_state, struct file *file);
231 /* procfs support */
232 static int proc_viotape_show(struct seq_file *m, void *v)
234 int i;
236 seq_printf(m, "viotape driver version " VIOTAPE_VERSION "\n");
237 for (i = 0; i < viotape_numdev; i++) {
238 seq_printf(m, "viotape device %d is iSeries resource %10.10s"
239 "type %4.4s, model %3.3s\n",
240 i, viotape_unitinfo[i].rsrcname,
241 viotape_unitinfo[i].type,
242 viotape_unitinfo[i].model);
244 return 0;
247 static int proc_viotape_open(struct inode *inode, struct file *file)
249 return single_open(file, proc_viotape_show, NULL);
252 static const struct file_operations proc_viotape_operations = {
253 .owner = THIS_MODULE,
254 .open = proc_viotape_open,
255 .read = seq_read,
256 .llseek = seq_lseek,
257 .release = single_release,
260 /* Decode the device minor number into its parts */
261 void get_dev_info(struct inode *ino, struct viot_devinfo_struct *devi)
263 devi->devno = iminor(ino) & 0x1F;
264 devi->mode = (iminor(ino) & 0x60) >> 5;
265 /* if bit is set in the minor, do _not_ rewind automatically */
266 devi->rewind = (iminor(ino) & 0x80) == 0;
269 /* This is called only from the exit and init paths, so no need for locking */
270 static void clear_op_struct_pool(void)
272 while (op_struct_list) {
273 struct op_struct *toFree = op_struct_list;
274 op_struct_list = op_struct_list->next;
275 kfree(toFree);
279 /* Likewise, this is only called from the init path */
280 static int add_op_structs(int structs)
282 int i;
284 for (i = 0; i < structs; ++i) {
285 struct op_struct *new_struct =
286 kmalloc(sizeof(*new_struct), GFP_KERNEL);
287 if (!new_struct) {
288 clear_op_struct_pool();
289 return -ENOMEM;
291 new_struct->next = op_struct_list;
292 op_struct_list = new_struct;
294 return 0;
297 /* Allocate an op structure from our pool */
298 static struct op_struct *get_op_struct(void)
300 struct op_struct *retval;
301 unsigned long flags;
303 spin_lock_irqsave(&op_struct_list_lock, flags);
304 retval = op_struct_list;
305 if (retval)
306 op_struct_list = retval->next;
307 spin_unlock_irqrestore(&op_struct_list_lock, flags);
308 if (retval) {
309 memset(retval, 0, sizeof(*retval));
310 init_completion(&retval->com);
313 return retval;
316 /* Return an op structure to our pool */
317 static void free_op_struct(struct op_struct *op_struct)
319 unsigned long flags;
321 spin_lock_irqsave(&op_struct_list_lock, flags);
322 op_struct->next = op_struct_list;
323 op_struct_list = op_struct;
324 spin_unlock_irqrestore(&op_struct_list_lock, flags);
327 /* Map our tape return codes to errno values */
328 int tape_rc_to_errno(int tape_rc, char *operation, int tapeno)
330 const struct vio_error_entry *err;
332 if (tape_rc == 0)
333 return 0;
335 err = vio_lookup_rc(viotape_err_table, tape_rc);
336 printk(VIOTAPE_KERN_WARN "error(%s) 0x%04x on Device %d (%-10s): %s\n",
337 operation, tape_rc, tapeno,
338 viotape_unitinfo[tapeno].rsrcname, err->msg);
339 return -err->errno;
342 /* Write */
343 static ssize_t viotap_write(struct file *file, const char *buf,
344 size_t count, loff_t * ppos)
346 HvLpEvent_Rc hvrc;
347 unsigned short flags = file->f_flags;
348 int noblock = ((flags & O_NONBLOCK) != 0);
349 ssize_t ret;
350 struct viot_devinfo_struct devi;
351 struct op_struct *op = get_op_struct();
353 if (op == NULL)
354 return -ENOMEM;
356 get_dev_info(file->f_path.dentry->d_inode, &devi);
359 * We need to make sure we can send a request. We use
360 * a semaphore to keep track of # requests in use. If
361 * we are non-blocking, make sure we don't block on the
362 * semaphore
364 if (noblock) {
365 if (down_trylock(&reqSem)) {
366 ret = -EWOULDBLOCK;
367 goto free_op;
369 } else
370 down(&reqSem);
372 /* Allocate a DMA buffer */
373 op->dev = tape_device[devi.devno];
374 op->buffer = dma_alloc_coherent(op->dev, count, &op->dmaaddr,
375 GFP_ATOMIC);
377 if (op->buffer == NULL) {
378 printk(VIOTAPE_KERN_WARN
379 "error allocating dma buffer for len %ld\n",
380 count);
381 ret = -EFAULT;
382 goto up_sem;
385 /* Copy the data into the buffer */
386 if (copy_from_user(op->buffer, buf, count)) {
387 printk(VIOTAPE_KERN_WARN "tape: error on copy from user\n");
388 ret = -EFAULT;
389 goto free_dma;
392 op->non_blocking = noblock;
393 init_completion(&op->com);
394 op->count = count;
396 hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
397 HvLpEvent_Type_VirtualIo,
398 viomajorsubtype_tape | viotapewrite,
399 HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck,
400 viopath_sourceinst(viopath_hostLp),
401 viopath_targetinst(viopath_hostLp),
402 (u64)(unsigned long)op, VIOVERSION << 16,
403 ((u64)devi.devno << 48) | op->dmaaddr, count, 0, 0);
404 if (hvrc != HvLpEvent_Rc_Good) {
405 printk(VIOTAPE_KERN_WARN "hv error on op %d\n",
406 (int)hvrc);
407 ret = -EIO;
408 goto free_dma;
411 if (noblock)
412 return count;
414 wait_for_completion(&op->com);
416 if (op->rc)
417 ret = tape_rc_to_errno(op->rc, "write", devi.devno);
418 else {
419 chg_state(devi.devno, VIOT_WRITING, file);
420 ret = op->count;
423 free_dma:
424 dma_free_coherent(op->dev, count, op->buffer, op->dmaaddr);
425 up_sem:
426 up(&reqSem);
427 free_op:
428 free_op_struct(op);
429 return ret;
432 /* read */
433 static ssize_t viotap_read(struct file *file, char *buf, size_t count,
434 loff_t *ptr)
436 HvLpEvent_Rc hvrc;
437 unsigned short flags = file->f_flags;
438 struct op_struct *op = get_op_struct();
439 int noblock = ((flags & O_NONBLOCK) != 0);
440 ssize_t ret;
441 struct viot_devinfo_struct devi;
443 if (op == NULL)
444 return -ENOMEM;
446 get_dev_info(file->f_path.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
452 * semaphore
454 if (noblock) {
455 if (down_trylock(&reqSem)) {
456 ret = -EWOULDBLOCK;
457 goto free_op;
459 } else
460 down(&reqSem);
462 chg_state(devi.devno, VIOT_READING, file);
464 /* Allocate a DMA buffer */
465 op->dev = tape_device[devi.devno];
466 op->buffer = dma_alloc_coherent(op->dev, count, &op->dmaaddr,
467 GFP_ATOMIC);
468 if (op->buffer == NULL) {
469 ret = -EFAULT;
470 goto up_sem;
473 op->count = count;
474 init_completion(&op->com);
476 hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
477 HvLpEvent_Type_VirtualIo,
478 viomajorsubtype_tape | viotaperead,
479 HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck,
480 viopath_sourceinst(viopath_hostLp),
481 viopath_targetinst(viopath_hostLp),
482 (u64)(unsigned long)op, VIOVERSION << 16,
483 ((u64)devi.devno << 48) | op->dmaaddr, count, 0, 0);
484 if (hvrc != HvLpEvent_Rc_Good) {
485 printk(VIOTAPE_KERN_WARN "tape hv error on op %d\n",
486 (int)hvrc);
487 ret = -EIO;
488 goto free_dma;
491 wait_for_completion(&op->com);
493 if (op->rc)
494 ret = tape_rc_to_errno(op->rc, "read", devi.devno);
495 else {
496 ret = op->count;
497 if (ret && copy_to_user(buf, op->buffer, ret)) {
498 printk(VIOTAPE_KERN_WARN "error on copy_to_user\n");
499 ret = -EFAULT;
503 free_dma:
504 dma_free_coherent(op->dev, count, op->buffer, op->dmaaddr);
505 up_sem:
506 up(&reqSem);
507 free_op:
508 free_op_struct(op);
509 return ret;
512 /* ioctl */
513 static int viotap_ioctl(struct inode *inode, struct file *file,
514 unsigned int cmd, unsigned long arg)
516 HvLpEvent_Rc hvrc;
517 int ret;
518 struct viot_devinfo_struct devi;
519 struct mtop mtc;
520 u32 myOp;
521 struct op_struct *op = get_op_struct();
523 if (op == NULL)
524 return -ENOMEM;
526 get_dev_info(file->f_path.dentry->d_inode, &devi);
528 down(&reqSem);
530 ret = -EINVAL;
532 switch (cmd) {
533 case MTIOCTOP:
534 ret = -EFAULT;
536 * inode is null if and only if we (the kernel)
537 * made the request
539 if (inode == NULL)
540 memcpy(&mtc, (void *) arg, sizeof(struct mtop));
541 else if (copy_from_user((char *)&mtc, (char *)arg,
542 sizeof(struct mtop)))
543 goto free_op;
545 ret = -EIO;
546 switch (mtc.mt_op) {
547 case MTRESET:
548 myOp = VIOTAPOP_RESET;
549 break;
550 case MTFSF:
551 myOp = VIOTAPOP_FSF;
552 break;
553 case MTBSF:
554 myOp = VIOTAPOP_BSF;
555 break;
556 case MTFSR:
557 myOp = VIOTAPOP_FSR;
558 break;
559 case MTBSR:
560 myOp = VIOTAPOP_BSR;
561 break;
562 case MTWEOF:
563 myOp = VIOTAPOP_WEOF;
564 break;
565 case MTREW:
566 myOp = VIOTAPOP_REW;
567 break;
568 case MTNOP:
569 myOp = VIOTAPOP_NOP;
570 break;
571 case MTEOM:
572 myOp = VIOTAPOP_EOM;
573 break;
574 case MTERASE:
575 myOp = VIOTAPOP_ERASE;
576 break;
577 case MTSETBLK:
578 myOp = VIOTAPOP_SETBLK;
579 break;
580 case MTSETDENSITY:
581 myOp = VIOTAPOP_SETDENSITY;
582 break;
583 case MTTELL:
584 myOp = VIOTAPOP_GETPOS;
585 break;
586 case MTSEEK:
587 myOp = VIOTAPOP_SETPOS;
588 break;
589 case MTSETPART:
590 myOp = VIOTAPOP_SETPART;
591 break;
592 case MTOFFL:
593 myOp = VIOTAPOP_UNLOAD;
594 break;
595 default:
596 printk(VIOTAPE_KERN_WARN "MTIOCTOP called "
597 "with invalid op 0x%x\n", mtc.mt_op);
598 goto free_op;
602 * if we moved the head, we are no longer
603 * reading or writing
605 switch (mtc.mt_op) {
606 case MTFSF:
607 case MTBSF:
608 case MTFSR:
609 case MTBSR:
610 case MTTELL:
611 case MTSEEK:
612 case MTREW:
613 chg_state(devi.devno, VIOT_IDLE, file);
616 init_completion(&op->com);
617 hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
618 HvLpEvent_Type_VirtualIo,
619 viomajorsubtype_tape | viotapeop,
620 HvLpEvent_AckInd_DoAck,
621 HvLpEvent_AckType_ImmediateAck,
622 viopath_sourceinst(viopath_hostLp),
623 viopath_targetinst(viopath_hostLp),
624 (u64)(unsigned long)op,
625 VIOVERSION << 16,
626 ((u64)devi.devno << 48), 0,
627 (((u64)myOp) << 32) | mtc.mt_count, 0);
628 if (hvrc != HvLpEvent_Rc_Good) {
629 printk(VIOTAPE_KERN_WARN "hv error on op %d\n",
630 (int)hvrc);
631 goto free_op;
633 wait_for_completion(&op->com);
634 ret = tape_rc_to_errno(op->rc, "tape operation", devi.devno);
635 goto free_op;
637 case MTIOCGET:
638 ret = -EIO;
639 init_completion(&op->com);
640 hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
641 HvLpEvent_Type_VirtualIo,
642 viomajorsubtype_tape | viotapegetstatus,
643 HvLpEvent_AckInd_DoAck,
644 HvLpEvent_AckType_ImmediateAck,
645 viopath_sourceinst(viopath_hostLp),
646 viopath_targetinst(viopath_hostLp),
647 (u64)(unsigned long)op, VIOVERSION << 16,
648 ((u64)devi.devno << 48), 0, 0, 0);
649 if (hvrc != HvLpEvent_Rc_Good) {
650 printk(VIOTAPE_KERN_WARN "hv error on op %d\n",
651 (int)hvrc);
652 goto free_op;
654 wait_for_completion(&op->com);
656 /* Operation is complete - grab the error code */
657 ret = tape_rc_to_errno(op->rc, "get status", devi.devno);
658 free_op_struct(op);
659 up(&reqSem);
661 if ((ret == 0) && copy_to_user((void *)arg,
662 &viomtget[devi.devno],
663 sizeof(viomtget[0])))
664 ret = -EFAULT;
665 return ret;
666 case MTIOCPOS:
667 printk(VIOTAPE_KERN_WARN "Got an (unsupported) MTIOCPOS\n");
668 break;
669 default:
670 printk(VIOTAPE_KERN_WARN "got an unsupported ioctl 0x%0x\n",
671 cmd);
672 break;
675 free_op:
676 free_op_struct(op);
677 up(&reqSem);
678 return ret;
681 static long viotap_unlocked_ioctl(struct file *file,
682 unsigned int cmd, unsigned long arg)
684 long rc;
686 lock_kernel();
687 rc = viotap_ioctl(file->f_path.dentry->d_inode, file, cmd, arg);
688 unlock_kernel();
689 return rc;
692 static int viotap_open(struct inode *inode, struct file *file)
694 HvLpEvent_Rc hvrc;
695 struct viot_devinfo_struct devi;
696 int ret;
697 struct op_struct *op = get_op_struct();
699 if (op == NULL)
700 return -ENOMEM;
702 lock_kernel();
703 get_dev_info(file->f_path.dentry->d_inode, &devi);
705 /* Note: We currently only support one mode! */
706 if ((devi.devno >= viotape_numdev) || (devi.mode)) {
707 ret = -ENODEV;
708 goto free_op;
711 init_completion(&op->com);
713 hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
714 HvLpEvent_Type_VirtualIo,
715 viomajorsubtype_tape | viotapeopen,
716 HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck,
717 viopath_sourceinst(viopath_hostLp),
718 viopath_targetinst(viopath_hostLp),
719 (u64)(unsigned long)op, VIOVERSION << 16,
720 ((u64)devi.devno << 48), 0, 0, 0);
721 if (hvrc != 0) {
722 printk(VIOTAPE_KERN_WARN "bad rc on signalLpEvent %d\n",
723 (int) hvrc);
724 ret = -EIO;
725 goto free_op;
728 wait_for_completion(&op->com);
729 ret = tape_rc_to_errno(op->rc, "open", devi.devno);
731 free_op:
732 free_op_struct(op);
733 unlock_kernel();
734 return ret;
738 static int viotap_release(struct inode *inode, struct file *file)
740 HvLpEvent_Rc hvrc;
741 struct viot_devinfo_struct devi;
742 int ret = 0;
743 struct op_struct *op = get_op_struct();
745 if (op == NULL)
746 return -ENOMEM;
747 init_completion(&op->com);
749 get_dev_info(file->f_path.dentry->d_inode, &devi);
751 if (devi.devno >= viotape_numdev) {
752 ret = -ENODEV;
753 goto free_op;
756 chg_state(devi.devno, VIOT_IDLE, file);
758 if (devi.rewind) {
759 hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
760 HvLpEvent_Type_VirtualIo,
761 viomajorsubtype_tape | viotapeop,
762 HvLpEvent_AckInd_DoAck,
763 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,
768 ((u64)VIOTAPOP_REW) << 32, 0);
769 wait_for_completion(&op->com);
771 tape_rc_to_errno(op->rc, "rewind", devi.devno);
774 hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
775 HvLpEvent_Type_VirtualIo,
776 viomajorsubtype_tape | viotapeclose,
777 HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck,
778 viopath_sourceinst(viopath_hostLp),
779 viopath_targetinst(viopath_hostLp),
780 (u64)(unsigned long)op, VIOVERSION << 16,
781 ((u64)devi.devno << 48), 0, 0, 0);
782 if (hvrc != 0) {
783 printk(VIOTAPE_KERN_WARN "bad rc on signalLpEvent %d\n",
784 (int) hvrc);
785 ret = -EIO;
786 goto free_op;
789 wait_for_completion(&op->com);
791 if (op->rc)
792 printk(VIOTAPE_KERN_WARN "close failed\n");
794 free_op:
795 free_op_struct(op);
796 return ret;
799 const struct file_operations viotap_fops = {
800 .owner = THIS_MODULE,
801 .read = viotap_read,
802 .write = viotap_write,
803 .unlocked_ioctl = viotap_unlocked_ioctl,
804 .open = viotap_open,
805 .release = viotap_release,
808 /* Handle interrupt events for tape */
809 static void vioHandleTapeEvent(struct HvLpEvent *event)
811 int tapeminor;
812 struct op_struct *op;
813 struct viotapelpevent *tevent = (struct viotapelpevent *)event;
815 if (event == NULL) {
816 /* Notification that a partition went away! */
817 if (!viopath_isactive(viopath_hostLp)) {
818 /* TODO! Clean up */
820 return;
823 tapeminor = event->xSubtype & VIOMINOR_SUBTYPE_MASK;
824 op = (struct op_struct *)event->xCorrelationToken;
825 switch (tapeminor) {
826 case viotapeopen:
827 case viotapeclose:
828 op->rc = tevent->sub_type_result;
829 complete(&op->com);
830 break;
831 case viotaperead:
832 op->rc = tevent->sub_type_result;
833 op->count = tevent->len;
834 complete(&op->com);
835 break;
836 case viotapewrite:
837 if (op->non_blocking) {
838 dma_free_coherent(op->dev, op->count,
839 op->buffer, op->dmaaddr);
840 free_op_struct(op);
841 up(&reqSem);
842 } else {
843 op->rc = tevent->sub_type_result;
844 op->count = tevent->len;
845 complete(&op->com);
847 break;
848 case viotapeop:
849 case viotapegetpos:
850 case viotapesetpos:
851 case viotapegetstatus:
852 if (op) {
853 op->count = tevent->u.op.count;
854 op->rc = tevent->sub_type_result;
855 if (!op->non_blocking)
856 complete(&op->com);
858 break;
859 default:
860 printk(VIOTAPE_KERN_WARN "weird ack\n");
864 static int viotape_probe(struct vio_dev *vdev, const struct vio_device_id *id)
866 int i = vdev->unit_address;
867 int j;
868 struct device_node *node = vdev->dev.archdata.of_node;
870 if (i >= VIOTAPE_MAX_TAPE)
871 return -ENODEV;
872 if (!node)
873 return -ENODEV;
875 if (i >= viotape_numdev)
876 viotape_numdev = i + 1;
878 tape_device[i] = &vdev->dev;
879 viotape_unitinfo[i].rsrcname = of_get_property(node,
880 "linux,vio_rsrcname", NULL);
881 viotape_unitinfo[i].type = of_get_property(node, "linux,vio_type",
882 NULL);
883 viotape_unitinfo[i].model = of_get_property(node, "linux,vio_model",
884 NULL);
886 state[i].cur_part = 0;
887 for (j = 0; j < MAX_PARTITIONS; ++j)
888 state[i].part_stat_rwi[j] = VIOT_IDLE;
889 device_create(tape_class, NULL, MKDEV(VIOTAPE_MAJOR, i), NULL,
890 "iseries!vt%d", i);
891 device_create(tape_class, NULL, MKDEV(VIOTAPE_MAJOR, i | 0x80), NULL,
892 "iseries!nvt%d", i);
893 printk(VIOTAPE_KERN_INFO "tape iseries/vt%d is iSeries "
894 "resource %10.10s type %4.4s, model %3.3s\n",
895 i, viotape_unitinfo[i].rsrcname,
896 viotape_unitinfo[i].type, viotape_unitinfo[i].model);
897 return 0;
900 static int viotape_remove(struct vio_dev *vdev)
902 int i = vdev->unit_address;
904 device_destroy(tape_class, MKDEV(VIOTAPE_MAJOR, i | 0x80));
905 device_destroy(tape_class, MKDEV(VIOTAPE_MAJOR, i));
906 return 0;
910 * viotape_device_table: Used by vio.c to match devices that we
911 * support.
913 static struct vio_device_id viotape_device_table[] __devinitdata = {
914 { "byte", "IBM,iSeries-viotape" },
915 { "", "" }
917 MODULE_DEVICE_TABLE(vio, viotape_device_table);
919 static struct vio_driver viotape_driver = {
920 .id_table = viotape_device_table,
921 .probe = viotape_probe,
922 .remove = viotape_remove,
923 .driver = {
924 .name = "viotape",
925 .owner = THIS_MODULE,
930 int __init viotap_init(void)
932 int ret;
934 if (!firmware_has_feature(FW_FEATURE_ISERIES))
935 return -ENODEV;
937 op_struct_list = NULL;
938 if ((ret = add_op_structs(VIOTAPE_MAXREQ)) < 0) {
939 printk(VIOTAPE_KERN_WARN "couldn't allocate op structs\n");
940 return ret;
942 spin_lock_init(&op_struct_list_lock);
944 sema_init(&reqSem, VIOTAPE_MAXREQ);
946 if (viopath_hostLp == HvLpIndexInvalid) {
947 vio_set_hostlp();
948 if (viopath_hostLp == HvLpIndexInvalid) {
949 ret = -ENODEV;
950 goto clear_op;
954 ret = viopath_open(viopath_hostLp, viomajorsubtype_tape,
955 VIOTAPE_MAXREQ + 2);
956 if (ret) {
957 printk(VIOTAPE_KERN_WARN
958 "error on viopath_open to hostlp %d\n", ret);
959 ret = -EIO;
960 goto clear_op;
963 printk(VIOTAPE_KERN_INFO "vers " VIOTAPE_VERSION
964 ", hosting partition %d\n", viopath_hostLp);
966 vio_setHandler(viomajorsubtype_tape, vioHandleTapeEvent);
968 ret = register_chrdev(VIOTAPE_MAJOR, "viotape", &viotap_fops);
969 if (ret < 0) {
970 printk(VIOTAPE_KERN_WARN "Error registering viotape device\n");
971 goto clear_handler;
974 tape_class = class_create(THIS_MODULE, "tape");
975 if (IS_ERR(tape_class)) {
976 printk(VIOTAPE_KERN_WARN "Unable to allocat class\n");
977 ret = PTR_ERR(tape_class);
978 goto unreg_chrdev;
981 ret = vio_register_driver(&viotape_driver);
982 if (ret)
983 goto unreg_class;
985 proc_create("iSeries/viotape", S_IFREG|S_IRUGO, NULL,
986 &proc_viotape_operations);
988 return 0;
990 unreg_class:
991 class_destroy(tape_class);
992 unreg_chrdev:
993 unregister_chrdev(VIOTAPE_MAJOR, "viotape");
994 clear_handler:
995 vio_clearHandler(viomajorsubtype_tape);
996 viopath_close(viopath_hostLp, viomajorsubtype_tape, VIOTAPE_MAXREQ + 2);
997 clear_op:
998 clear_op_struct_pool();
999 return ret;
1002 /* Give a new state to the tape object */
1003 static int chg_state(int index, unsigned char new_state, struct file *file)
1005 unsigned char *cur_state =
1006 &state[index].part_stat_rwi[state[index].cur_part];
1007 int rc = 0;
1009 /* if the same state, don't bother */
1010 if (*cur_state == new_state)
1011 return 0;
1013 /* write an EOF if changing from writing to some other state */
1014 if (*cur_state == VIOT_WRITING) {
1015 struct mtop write_eof = { MTWEOF, 1 };
1017 rc = viotap_ioctl(NULL, file, MTIOCTOP,
1018 (unsigned long)&write_eof);
1020 *cur_state = new_state;
1021 return rc;
1024 /* Cleanup */
1025 static void __exit viotap_exit(void)
1027 remove_proc_entry("iSeries/viotape", NULL);
1028 vio_unregister_driver(&viotape_driver);
1029 class_destroy(tape_class);
1030 unregister_chrdev(VIOTAPE_MAJOR, "viotape");
1031 viopath_close(viopath_hostLp, viomajorsubtype_tape, VIOTAPE_MAXREQ + 2);
1032 vio_clearHandler(viomajorsubtype_tape);
1033 clear_op_struct_pool();
1036 MODULE_LICENSE("GPL");
1037 module_init(viotap_init);
1038 module_exit(viotap_exit);