GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / drivers / char / viotape.c
blob42f7fa442ff80653c3cbabf9c80bd1b2e05de28d
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>
50 #include <linux/slab.h>
52 #include <asm/uaccess.h>
53 #include <asm/ioctls.h>
54 #include <asm/firmware.h>
55 #include <asm/vio.h>
56 #include <asm/iseries/vio.h>
57 #include <asm/iseries/hv_lp_event.h>
58 #include <asm/iseries/hv_call_event.h>
59 #include <asm/iseries/hv_lp_config.h>
61 #define VIOTAPE_VERSION "1.2"
62 #define VIOTAPE_MAXREQ 1
64 #define VIOTAPE_KERN_WARN KERN_WARNING "viotape: "
65 #define VIOTAPE_KERN_INFO KERN_INFO "viotape: "
67 static int viotape_numdev;
70 * The minor number follows the conventions of the SCSI tape drives. The
71 * rewind and mode are encoded in the minor #. We use this struct to break
72 * them out
74 struct viot_devinfo_struct {
75 int devno;
76 int mode;
77 int rewind;
80 #define VIOTAPOP_RESET 0
81 #define VIOTAPOP_FSF 1
82 #define VIOTAPOP_BSF 2
83 #define VIOTAPOP_FSR 3
84 #define VIOTAPOP_BSR 4
85 #define VIOTAPOP_WEOF 5
86 #define VIOTAPOP_REW 6
87 #define VIOTAPOP_NOP 7
88 #define VIOTAPOP_EOM 8
89 #define VIOTAPOP_ERASE 9
90 #define VIOTAPOP_SETBLK 10
91 #define VIOTAPOP_SETDENSITY 11
92 #define VIOTAPOP_SETPOS 12
93 #define VIOTAPOP_GETPOS 13
94 #define VIOTAPOP_SETPART 14
95 #define VIOTAPOP_UNLOAD 15
97 enum viotaperc {
98 viotape_InvalidRange = 0x0601,
99 viotape_InvalidToken = 0x0602,
100 viotape_DMAError = 0x0603,
101 viotape_UseError = 0x0604,
102 viotape_ReleaseError = 0x0605,
103 viotape_InvalidTape = 0x0606,
104 viotape_InvalidOp = 0x0607,
105 viotape_TapeErr = 0x0608,
107 viotape_AllocTimedOut = 0x0640,
108 viotape_BOTEnc = 0x0641,
109 viotape_BlankTape = 0x0642,
110 viotape_BufferEmpty = 0x0643,
111 viotape_CleanCartFound = 0x0644,
112 viotape_CmdNotAllowed = 0x0645,
113 viotape_CmdNotSupported = 0x0646,
114 viotape_DataCheck = 0x0647,
115 viotape_DecompressErr = 0x0648,
116 viotape_DeviceTimeout = 0x0649,
117 viotape_DeviceUnavail = 0x064a,
118 viotape_DeviceBusy = 0x064b,
119 viotape_EndOfMedia = 0x064c,
120 viotape_EndOfTape = 0x064d,
121 viotape_EquipCheck = 0x064e,
122 viotape_InsufficientRs = 0x064f,
123 viotape_InvalidLogBlk = 0x0650,
124 viotape_LengthError = 0x0651,
125 viotape_LibDoorOpen = 0x0652,
126 viotape_LoadFailure = 0x0653,
127 viotape_NotCapable = 0x0654,
128 viotape_NotOperational = 0x0655,
129 viotape_NotReady = 0x0656,
130 viotape_OpCancelled = 0x0657,
131 viotape_PhyLinkErr = 0x0658,
132 viotape_RdyNotBOT = 0x0659,
133 viotape_TapeMark = 0x065a,
134 viotape_WriteProt = 0x065b
137 static const struct vio_error_entry viotape_err_table[] = {
138 { viotape_InvalidRange, EIO, "Internal error" },
139 { viotape_InvalidToken, EIO, "Internal error" },
140 { viotape_DMAError, EIO, "DMA error" },
141 { viotape_UseError, EIO, "Internal error" },
142 { viotape_ReleaseError, EIO, "Internal error" },
143 { viotape_InvalidTape, EIO, "Invalid tape device" },
144 { viotape_InvalidOp, EIO, "Invalid operation" },
145 { viotape_TapeErr, EIO, "Tape error" },
146 { viotape_AllocTimedOut, EBUSY, "Allocate timed out" },
147 { viotape_BOTEnc, EIO, "Beginning of tape encountered" },
148 { viotape_BlankTape, EIO, "Blank tape" },
149 { viotape_BufferEmpty, EIO, "Buffer empty" },
150 { viotape_CleanCartFound, ENOMEDIUM, "Cleaning cartridge found" },
151 { viotape_CmdNotAllowed, EIO, "Command not allowed" },
152 { viotape_CmdNotSupported, EIO, "Command not supported" },
153 { viotape_DataCheck, EIO, "Data check" },
154 { viotape_DecompressErr, EIO, "Decompression error" },
155 { viotape_DeviceTimeout, EBUSY, "Device timeout" },
156 { viotape_DeviceUnavail, EIO, "Device unavailable" },
157 { viotape_DeviceBusy, EBUSY, "Device busy" },
158 { viotape_EndOfMedia, ENOSPC, "End of media" },
159 { viotape_EndOfTape, ENOSPC, "End of tape" },
160 { viotape_EquipCheck, EIO, "Equipment check" },
161 { viotape_InsufficientRs, EOVERFLOW, "Insufficient tape resources" },
162 { viotape_InvalidLogBlk, EIO, "Invalid logical block location" },
163 { viotape_LengthError, EOVERFLOW, "Length error" },
164 { viotape_LibDoorOpen, EBUSY, "Door open" },
165 { viotape_LoadFailure, ENOMEDIUM, "Load failure" },
166 { viotape_NotCapable, EIO, "Not capable" },
167 { viotape_NotOperational, EIO, "Not operational" },
168 { viotape_NotReady, EIO, "Not ready" },
169 { viotape_OpCancelled, EIO, "Operation cancelled" },
170 { viotape_PhyLinkErr, EIO, "Physical link error" },
171 { viotape_RdyNotBOT, EIO, "Ready but not beginning of tape" },
172 { viotape_TapeMark, EIO, "Tape mark" },
173 { viotape_WriteProt, EROFS, "Write protection error" },
174 { 0, 0, NULL },
177 /* Maximum number of tapes we support */
178 #define VIOTAPE_MAX_TAPE HVMAXARCHITECTEDVIRTUALTAPES
179 #define MAX_PARTITIONS 4
181 /* defines for current tape state */
182 #define VIOT_IDLE 0
183 #define VIOT_READING 1
184 #define VIOT_WRITING 2
186 /* Our info on the tapes */
187 static struct {
188 const char *rsrcname;
189 const char *type;
190 const char *model;
191 } viotape_unitinfo[VIOTAPE_MAX_TAPE];
193 static struct mtget viomtget[VIOTAPE_MAX_TAPE];
195 static struct class *tape_class;
197 static struct device *tape_device[VIOTAPE_MAX_TAPE];
200 * maintain the current state of each tape (and partition)
201 * so that we know when to write EOF marks.
203 static struct {
204 unsigned char cur_part;
205 unsigned char part_stat_rwi[MAX_PARTITIONS];
206 } state[VIOTAPE_MAX_TAPE];
208 /* We single-thread */
209 static struct semaphore reqSem;
212 * When we send a request, we use this struct to get the response back
213 * from the interrupt handler
215 struct op_struct {
216 void *buffer;
217 dma_addr_t dmaaddr;
218 size_t count;
219 int rc;
220 int non_blocking;
221 struct completion com;
222 struct device *dev;
223 struct op_struct *next;
226 static spinlock_t op_struct_list_lock;
227 static struct op_struct *op_struct_list;
229 /* forward declaration to resolve interdependence */
230 static int chg_state(int index, unsigned char new_state, struct file *file);
232 /* procfs support */
233 static int proc_viotape_show(struct seq_file *m, void *v)
235 int i;
237 seq_printf(m, "viotape driver version " VIOTAPE_VERSION "\n");
238 for (i = 0; i < viotape_numdev; i++) {
239 seq_printf(m, "viotape device %d is iSeries resource %10.10s"
240 "type %4.4s, model %3.3s\n",
241 i, viotape_unitinfo[i].rsrcname,
242 viotape_unitinfo[i].type,
243 viotape_unitinfo[i].model);
245 return 0;
248 static int proc_viotape_open(struct inode *inode, struct file *file)
250 return single_open(file, proc_viotape_show, NULL);
253 static const struct file_operations proc_viotape_operations = {
254 .owner = THIS_MODULE,
255 .open = proc_viotape_open,
256 .read = seq_read,
257 .llseek = seq_lseek,
258 .release = single_release,
261 /* Decode the device minor number into its parts */
262 void get_dev_info(struct inode *ino, struct viot_devinfo_struct *devi)
264 devi->devno = iminor(ino) & 0x1F;
265 devi->mode = (iminor(ino) & 0x60) >> 5;
266 /* if bit is set in the minor, do _not_ rewind automatically */
267 devi->rewind = (iminor(ino) & 0x80) == 0;
270 /* This is called only from the exit and init paths, so no need for locking */
271 static void clear_op_struct_pool(void)
273 while (op_struct_list) {
274 struct op_struct *toFree = op_struct_list;
275 op_struct_list = op_struct_list->next;
276 kfree(toFree);
280 /* Likewise, this is only called from the init path */
281 static int add_op_structs(int structs)
283 int i;
285 for (i = 0; i < structs; ++i) {
286 struct op_struct *new_struct =
287 kmalloc(sizeof(*new_struct), GFP_KERNEL);
288 if (!new_struct) {
289 clear_op_struct_pool();
290 return -ENOMEM;
292 new_struct->next = op_struct_list;
293 op_struct_list = new_struct;
295 return 0;
298 /* Allocate an op structure from our pool */
299 static struct op_struct *get_op_struct(void)
301 struct op_struct *retval;
302 unsigned long flags;
304 spin_lock_irqsave(&op_struct_list_lock, flags);
305 retval = op_struct_list;
306 if (retval)
307 op_struct_list = retval->next;
308 spin_unlock_irqrestore(&op_struct_list_lock, flags);
309 if (retval) {
310 memset(retval, 0, sizeof(*retval));
311 init_completion(&retval->com);
314 return retval;
317 /* Return an op structure to our pool */
318 static void free_op_struct(struct op_struct *op_struct)
320 unsigned long flags;
322 spin_lock_irqsave(&op_struct_list_lock, flags);
323 op_struct->next = op_struct_list;
324 op_struct_list = op_struct;
325 spin_unlock_irqrestore(&op_struct_list_lock, flags);
328 /* Map our tape return codes to errno values */
329 int tape_rc_to_errno(int tape_rc, char *operation, int tapeno)
331 const struct vio_error_entry *err;
333 if (tape_rc == 0)
334 return 0;
336 err = vio_lookup_rc(viotape_err_table, tape_rc);
337 printk(VIOTAPE_KERN_WARN "error(%s) 0x%04x on Device %d (%-10s): %s\n",
338 operation, tape_rc, tapeno,
339 viotape_unitinfo[tapeno].rsrcname, err->msg);
340 return -err->errno;
343 /* Write */
344 static ssize_t viotap_write(struct file *file, const char *buf,
345 size_t count, loff_t * ppos)
347 HvLpEvent_Rc hvrc;
348 unsigned short flags = file->f_flags;
349 int noblock = ((flags & O_NONBLOCK) != 0);
350 ssize_t ret;
351 struct viot_devinfo_struct devi;
352 struct op_struct *op = get_op_struct();
354 if (op == NULL)
355 return -ENOMEM;
357 get_dev_info(file->f_path.dentry->d_inode, &devi);
360 * We need to make sure we can send a request. We use
361 * a semaphore to keep track of # requests in use. If
362 * we are non-blocking, make sure we don't block on the
363 * semaphore
365 if (noblock) {
366 if (down_trylock(&reqSem)) {
367 ret = -EWOULDBLOCK;
368 goto free_op;
370 } else
371 down(&reqSem);
373 /* Allocate a DMA buffer */
374 op->dev = tape_device[devi.devno];
375 op->buffer = dma_alloc_coherent(op->dev, count, &op->dmaaddr,
376 GFP_ATOMIC);
378 if (op->buffer == NULL) {
379 printk(VIOTAPE_KERN_WARN
380 "error allocating dma buffer for len %ld\n",
381 count);
382 ret = -EFAULT;
383 goto up_sem;
386 /* Copy the data into the buffer */
387 if (copy_from_user(op->buffer, buf, count)) {
388 printk(VIOTAPE_KERN_WARN "tape: error on copy from user\n");
389 ret = -EFAULT;
390 goto free_dma;
393 op->non_blocking = noblock;
394 init_completion(&op->com);
395 op->count = count;
397 hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
398 HvLpEvent_Type_VirtualIo,
399 viomajorsubtype_tape | viotapewrite,
400 HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck,
401 viopath_sourceinst(viopath_hostLp),
402 viopath_targetinst(viopath_hostLp),
403 (u64)(unsigned long)op, VIOVERSION << 16,
404 ((u64)devi.devno << 48) | op->dmaaddr, count, 0, 0);
405 if (hvrc != HvLpEvent_Rc_Good) {
406 printk(VIOTAPE_KERN_WARN "hv error on op %d\n",
407 (int)hvrc);
408 ret = -EIO;
409 goto free_dma;
412 if (noblock)
413 return count;
415 wait_for_completion(&op->com);
417 if (op->rc)
418 ret = tape_rc_to_errno(op->rc, "write", devi.devno);
419 else {
420 chg_state(devi.devno, VIOT_WRITING, file);
421 ret = op->count;
424 free_dma:
425 dma_free_coherent(op->dev, count, op->buffer, op->dmaaddr);
426 up_sem:
427 up(&reqSem);
428 free_op:
429 free_op_struct(op);
430 return ret;
433 /* read */
434 static ssize_t viotap_read(struct file *file, char *buf, size_t count,
435 loff_t *ptr)
437 HvLpEvent_Rc hvrc;
438 unsigned short flags = file->f_flags;
439 struct op_struct *op = get_op_struct();
440 int noblock = ((flags & O_NONBLOCK) != 0);
441 ssize_t ret;
442 struct viot_devinfo_struct devi;
444 if (op == NULL)
445 return -ENOMEM;
447 get_dev_info(file->f_path.dentry->d_inode, &devi);
450 * We need to make sure we can send a request. We use
451 * a semaphore to keep track of # requests in use. If
452 * we are non-blocking, make sure we don't block on the
453 * semaphore
455 if (noblock) {
456 if (down_trylock(&reqSem)) {
457 ret = -EWOULDBLOCK;
458 goto free_op;
460 } else
461 down(&reqSem);
463 chg_state(devi.devno, VIOT_READING, file);
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);
469 if (op->buffer == NULL) {
470 ret = -EFAULT;
471 goto up_sem;
474 op->count = count;
475 init_completion(&op->com);
477 hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
478 HvLpEvent_Type_VirtualIo,
479 viomajorsubtype_tape | viotaperead,
480 HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck,
481 viopath_sourceinst(viopath_hostLp),
482 viopath_targetinst(viopath_hostLp),
483 (u64)(unsigned long)op, VIOVERSION << 16,
484 ((u64)devi.devno << 48) | op->dmaaddr, count, 0, 0);
485 if (hvrc != HvLpEvent_Rc_Good) {
486 printk(VIOTAPE_KERN_WARN "tape hv error on op %d\n",
487 (int)hvrc);
488 ret = -EIO;
489 goto free_dma;
492 wait_for_completion(&op->com);
494 if (op->rc)
495 ret = tape_rc_to_errno(op->rc, "read", devi.devno);
496 else {
497 ret = op->count;
498 if (ret && copy_to_user(buf, op->buffer, ret)) {
499 printk(VIOTAPE_KERN_WARN "error on copy_to_user\n");
500 ret = -EFAULT;
504 free_dma:
505 dma_free_coherent(op->dev, count, op->buffer, op->dmaaddr);
506 up_sem:
507 up(&reqSem);
508 free_op:
509 free_op_struct(op);
510 return ret;
513 /* ioctl */
514 static int viotap_ioctl(struct inode *inode, struct file *file,
515 unsigned int cmd, unsigned long arg)
517 HvLpEvent_Rc hvrc;
518 int ret;
519 struct viot_devinfo_struct devi;
520 struct mtop mtc;
521 u32 myOp;
522 struct op_struct *op = get_op_struct();
524 if (op == NULL)
525 return -ENOMEM;
527 get_dev_info(file->f_path.dentry->d_inode, &devi);
529 down(&reqSem);
531 ret = -EINVAL;
533 switch (cmd) {
534 case MTIOCTOP:
535 ret = -EFAULT;
537 * inode is null if and only if we (the kernel)
538 * made the request
540 if (inode == NULL)
541 memcpy(&mtc, (void *) arg, sizeof(struct mtop));
542 else if (copy_from_user((char *)&mtc, (char *)arg,
543 sizeof(struct mtop)))
544 goto free_op;
546 ret = -EIO;
547 switch (mtc.mt_op) {
548 case MTRESET:
549 myOp = VIOTAPOP_RESET;
550 break;
551 case MTFSF:
552 myOp = VIOTAPOP_FSF;
553 break;
554 case MTBSF:
555 myOp = VIOTAPOP_BSF;
556 break;
557 case MTFSR:
558 myOp = VIOTAPOP_FSR;
559 break;
560 case MTBSR:
561 myOp = VIOTAPOP_BSR;
562 break;
563 case MTWEOF:
564 myOp = VIOTAPOP_WEOF;
565 break;
566 case MTREW:
567 myOp = VIOTAPOP_REW;
568 break;
569 case MTNOP:
570 myOp = VIOTAPOP_NOP;
571 break;
572 case MTEOM:
573 myOp = VIOTAPOP_EOM;
574 break;
575 case MTERASE:
576 myOp = VIOTAPOP_ERASE;
577 break;
578 case MTSETBLK:
579 myOp = VIOTAPOP_SETBLK;
580 break;
581 case MTSETDENSITY:
582 myOp = VIOTAPOP_SETDENSITY;
583 break;
584 case MTTELL:
585 myOp = VIOTAPOP_GETPOS;
586 break;
587 case MTSEEK:
588 myOp = VIOTAPOP_SETPOS;
589 break;
590 case MTSETPART:
591 myOp = VIOTAPOP_SETPART;
592 break;
593 case MTOFFL:
594 myOp = VIOTAPOP_UNLOAD;
595 break;
596 default:
597 printk(VIOTAPE_KERN_WARN "MTIOCTOP called "
598 "with invalid op 0x%x\n", mtc.mt_op);
599 goto free_op;
603 * if we moved the head, we are no longer
604 * reading or writing
606 switch (mtc.mt_op) {
607 case MTFSF:
608 case MTBSF:
609 case MTFSR:
610 case MTBSR:
611 case MTTELL:
612 case MTSEEK:
613 case MTREW:
614 chg_state(devi.devno, VIOT_IDLE, file);
617 init_completion(&op->com);
618 hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
619 HvLpEvent_Type_VirtualIo,
620 viomajorsubtype_tape | viotapeop,
621 HvLpEvent_AckInd_DoAck,
622 HvLpEvent_AckType_ImmediateAck,
623 viopath_sourceinst(viopath_hostLp),
624 viopath_targetinst(viopath_hostLp),
625 (u64)(unsigned long)op,
626 VIOVERSION << 16,
627 ((u64)devi.devno << 48), 0,
628 (((u64)myOp) << 32) | mtc.mt_count, 0);
629 if (hvrc != HvLpEvent_Rc_Good) {
630 printk(VIOTAPE_KERN_WARN "hv error on op %d\n",
631 (int)hvrc);
632 goto free_op;
634 wait_for_completion(&op->com);
635 ret = tape_rc_to_errno(op->rc, "tape operation", devi.devno);
636 goto free_op;
638 case MTIOCGET:
639 ret = -EIO;
640 init_completion(&op->com);
641 hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
642 HvLpEvent_Type_VirtualIo,
643 viomajorsubtype_tape | viotapegetstatus,
644 HvLpEvent_AckInd_DoAck,
645 HvLpEvent_AckType_ImmediateAck,
646 viopath_sourceinst(viopath_hostLp),
647 viopath_targetinst(viopath_hostLp),
648 (u64)(unsigned long)op, VIOVERSION << 16,
649 ((u64)devi.devno << 48), 0, 0, 0);
650 if (hvrc != HvLpEvent_Rc_Good) {
651 printk(VIOTAPE_KERN_WARN "hv error on op %d\n",
652 (int)hvrc);
653 goto free_op;
655 wait_for_completion(&op->com);
657 /* Operation is complete - grab the error code */
658 ret = tape_rc_to_errno(op->rc, "get status", devi.devno);
659 free_op_struct(op);
660 up(&reqSem);
662 if ((ret == 0) && copy_to_user((void *)arg,
663 &viomtget[devi.devno],
664 sizeof(viomtget[0])))
665 ret = -EFAULT;
666 return ret;
667 case MTIOCPOS:
668 printk(VIOTAPE_KERN_WARN "Got an (unsupported) MTIOCPOS\n");
669 break;
670 default:
671 printk(VIOTAPE_KERN_WARN "got an unsupported ioctl 0x%0x\n",
672 cmd);
673 break;
676 free_op:
677 free_op_struct(op);
678 up(&reqSem);
679 return ret;
682 static long viotap_unlocked_ioctl(struct file *file,
683 unsigned int cmd, unsigned long arg)
685 long rc;
687 lock_kernel();
688 rc = viotap_ioctl(file->f_path.dentry->d_inode, file, cmd, arg);
689 unlock_kernel();
690 return rc;
693 static int viotap_open(struct inode *inode, struct file *file)
695 HvLpEvent_Rc hvrc;
696 struct viot_devinfo_struct devi;
697 int ret;
698 struct op_struct *op = get_op_struct();
700 if (op == NULL)
701 return -ENOMEM;
703 lock_kernel();
704 get_dev_info(file->f_path.dentry->d_inode, &devi);
706 /* Note: We currently only support one mode! */
707 if ((devi.devno >= viotape_numdev) || (devi.mode)) {
708 ret = -ENODEV;
709 goto free_op;
712 init_completion(&op->com);
714 hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
715 HvLpEvent_Type_VirtualIo,
716 viomajorsubtype_tape | viotapeopen,
717 HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck,
718 viopath_sourceinst(viopath_hostLp),
719 viopath_targetinst(viopath_hostLp),
720 (u64)(unsigned long)op, VIOVERSION << 16,
721 ((u64)devi.devno << 48), 0, 0, 0);
722 if (hvrc != 0) {
723 printk(VIOTAPE_KERN_WARN "bad rc on signalLpEvent %d\n",
724 (int) hvrc);
725 ret = -EIO;
726 goto free_op;
729 wait_for_completion(&op->com);
730 ret = tape_rc_to_errno(op->rc, "open", devi.devno);
732 free_op:
733 free_op_struct(op);
734 unlock_kernel();
735 return ret;
739 static int viotap_release(struct inode *inode, struct file *file)
741 HvLpEvent_Rc hvrc;
742 struct viot_devinfo_struct devi;
743 int ret = 0;
744 struct op_struct *op = get_op_struct();
746 if (op == NULL)
747 return -ENOMEM;
748 init_completion(&op->com);
750 get_dev_info(file->f_path.dentry->d_inode, &devi);
752 if (devi.devno >= viotape_numdev) {
753 ret = -ENODEV;
754 goto free_op;
757 chg_state(devi.devno, VIOT_IDLE, file);
759 if (devi.rewind) {
760 hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
761 HvLpEvent_Type_VirtualIo,
762 viomajorsubtype_tape | viotapeop,
763 HvLpEvent_AckInd_DoAck,
764 HvLpEvent_AckType_ImmediateAck,
765 viopath_sourceinst(viopath_hostLp),
766 viopath_targetinst(viopath_hostLp),
767 (u64)(unsigned long)op, VIOVERSION << 16,
768 ((u64)devi.devno << 48), 0,
769 ((u64)VIOTAPOP_REW) << 32, 0);
770 wait_for_completion(&op->com);
772 tape_rc_to_errno(op->rc, "rewind", devi.devno);
775 hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
776 HvLpEvent_Type_VirtualIo,
777 viomajorsubtype_tape | viotapeclose,
778 HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck,
779 viopath_sourceinst(viopath_hostLp),
780 viopath_targetinst(viopath_hostLp),
781 (u64)(unsigned long)op, VIOVERSION << 16,
782 ((u64)devi.devno << 48), 0, 0, 0);
783 if (hvrc != 0) {
784 printk(VIOTAPE_KERN_WARN "bad rc on signalLpEvent %d\n",
785 (int) hvrc);
786 ret = -EIO;
787 goto free_op;
790 wait_for_completion(&op->com);
792 if (op->rc)
793 printk(VIOTAPE_KERN_WARN "close failed\n");
795 free_op:
796 free_op_struct(op);
797 return ret;
800 const struct file_operations viotap_fops = {
801 .owner = THIS_MODULE,
802 .read = viotap_read,
803 .write = viotap_write,
804 .unlocked_ioctl = viotap_unlocked_ioctl,
805 .open = viotap_open,
806 .release = viotap_release,
809 /* Handle interrupt events for tape */
810 static void vioHandleTapeEvent(struct HvLpEvent *event)
812 int tapeminor;
813 struct op_struct *op;
814 struct viotapelpevent *tevent = (struct viotapelpevent *)event;
816 if (event == NULL) {
817 /* Notification that a partition went away! */
818 if (!viopath_isactive(viopath_hostLp)) {
819 /* TODO! Clean up */
821 return;
824 tapeminor = event->xSubtype & VIOMINOR_SUBTYPE_MASK;
825 op = (struct op_struct *)event->xCorrelationToken;
826 switch (tapeminor) {
827 case viotapeopen:
828 case viotapeclose:
829 op->rc = tevent->sub_type_result;
830 complete(&op->com);
831 break;
832 case viotaperead:
833 op->rc = tevent->sub_type_result;
834 op->count = tevent->len;
835 complete(&op->com);
836 break;
837 case viotapewrite:
838 if (op->non_blocking) {
839 dma_free_coherent(op->dev, op->count,
840 op->buffer, op->dmaaddr);
841 free_op_struct(op);
842 up(&reqSem);
843 } else {
844 op->rc = tevent->sub_type_result;
845 op->count = tevent->len;
846 complete(&op->com);
848 break;
849 case viotapeop:
850 case viotapegetpos:
851 case viotapesetpos:
852 case viotapegetstatus:
853 if (op) {
854 op->count = tevent->u.op.count;
855 op->rc = tevent->sub_type_result;
856 if (!op->non_blocking)
857 complete(&op->com);
859 break;
860 default:
861 printk(VIOTAPE_KERN_WARN "weird ack\n");
865 static int viotape_probe(struct vio_dev *vdev, const struct vio_device_id *id)
867 int i = vdev->unit_address;
868 int j;
869 struct device_node *node = vdev->dev.of_node;
871 if (i >= VIOTAPE_MAX_TAPE)
872 return -ENODEV;
873 if (!node)
874 return -ENODEV;
876 if (i >= viotape_numdev)
877 viotape_numdev = i + 1;
879 tape_device[i] = &vdev->dev;
880 viotape_unitinfo[i].rsrcname = of_get_property(node,
881 "linux,vio_rsrcname", NULL);
882 viotape_unitinfo[i].type = of_get_property(node, "linux,vio_type",
883 NULL);
884 viotape_unitinfo[i].model = of_get_property(node, "linux,vio_model",
885 NULL);
887 state[i].cur_part = 0;
888 for (j = 0; j < MAX_PARTITIONS; ++j)
889 state[i].part_stat_rwi[j] = VIOT_IDLE;
890 device_create(tape_class, NULL, MKDEV(VIOTAPE_MAJOR, i), NULL,
891 "iseries!vt%d", i);
892 device_create(tape_class, NULL, MKDEV(VIOTAPE_MAJOR, i | 0x80), NULL,
893 "iseries!nvt%d", i);
894 printk(VIOTAPE_KERN_INFO "tape iseries/vt%d is iSeries "
895 "resource %10.10s type %4.4s, model %3.3s\n",
896 i, viotape_unitinfo[i].rsrcname,
897 viotape_unitinfo[i].type, viotape_unitinfo[i].model);
898 return 0;
901 static int viotape_remove(struct vio_dev *vdev)
903 int i = vdev->unit_address;
905 device_destroy(tape_class, MKDEV(VIOTAPE_MAJOR, i | 0x80));
906 device_destroy(tape_class, MKDEV(VIOTAPE_MAJOR, i));
907 return 0;
911 * viotape_device_table: Used by vio.c to match devices that we
912 * support.
914 static struct vio_device_id viotape_device_table[] __devinitdata = {
915 { "byte", "IBM,iSeries-viotape" },
916 { "", "" }
918 MODULE_DEVICE_TABLE(vio, viotape_device_table);
920 static struct vio_driver viotape_driver = {
921 .id_table = viotape_device_table,
922 .probe = viotape_probe,
923 .remove = viotape_remove,
924 .driver = {
925 .name = "viotape",
926 .owner = THIS_MODULE,
931 int __init viotap_init(void)
933 int ret;
935 if (!firmware_has_feature(FW_FEATURE_ISERIES))
936 return -ENODEV;
938 op_struct_list = NULL;
939 if ((ret = add_op_structs(VIOTAPE_MAXREQ)) < 0) {
940 printk(VIOTAPE_KERN_WARN "couldn't allocate op structs\n");
941 return ret;
943 spin_lock_init(&op_struct_list_lock);
945 sema_init(&reqSem, VIOTAPE_MAXREQ);
947 if (viopath_hostLp == HvLpIndexInvalid) {
948 vio_set_hostlp();
949 if (viopath_hostLp == HvLpIndexInvalid) {
950 ret = -ENODEV;
951 goto clear_op;
955 ret = viopath_open(viopath_hostLp, viomajorsubtype_tape,
956 VIOTAPE_MAXREQ + 2);
957 if (ret) {
958 printk(VIOTAPE_KERN_WARN
959 "error on viopath_open to hostlp %d\n", ret);
960 ret = -EIO;
961 goto clear_op;
964 printk(VIOTAPE_KERN_INFO "vers " VIOTAPE_VERSION
965 ", hosting partition %d\n", viopath_hostLp);
967 vio_setHandler(viomajorsubtype_tape, vioHandleTapeEvent);
969 ret = register_chrdev(VIOTAPE_MAJOR, "viotape", &viotap_fops);
970 if (ret < 0) {
971 printk(VIOTAPE_KERN_WARN "Error registering viotape device\n");
972 goto clear_handler;
975 tape_class = class_create(THIS_MODULE, "tape");
976 if (IS_ERR(tape_class)) {
977 printk(VIOTAPE_KERN_WARN "Unable to allocat class\n");
978 ret = PTR_ERR(tape_class);
979 goto unreg_chrdev;
982 ret = vio_register_driver(&viotape_driver);
983 if (ret)
984 goto unreg_class;
986 proc_create("iSeries/viotape", S_IFREG|S_IRUGO, NULL,
987 &proc_viotape_operations);
989 return 0;
991 unreg_class:
992 class_destroy(tape_class);
993 unreg_chrdev:
994 unregister_chrdev(VIOTAPE_MAJOR, "viotape");
995 clear_handler:
996 vio_clearHandler(viomajorsubtype_tape);
997 viopath_close(viopath_hostLp, viomajorsubtype_tape, VIOTAPE_MAXREQ + 2);
998 clear_op:
999 clear_op_struct_pool();
1000 return ret;
1003 /* Give a new state to the tape object */
1004 static int chg_state(int index, unsigned char new_state, struct file *file)
1006 unsigned char *cur_state =
1007 &state[index].part_stat_rwi[state[index].cur_part];
1008 int rc = 0;
1010 /* if the same state, don't bother */
1011 if (*cur_state == new_state)
1012 return 0;
1014 /* write an EOF if changing from writing to some other state */
1015 if (*cur_state == VIOT_WRITING) {
1016 struct mtop write_eof = { MTWEOF, 1 };
1018 rc = viotap_ioctl(NULL, file, MTIOCTOP,
1019 (unsigned long)&write_eof);
1021 *cur_state = new_state;
1022 return rc;
1025 /* Cleanup */
1026 static void __exit viotap_exit(void)
1028 remove_proc_entry("iSeries/viotape", NULL);
1029 vio_unregister_driver(&viotape_driver);
1030 class_destroy(tape_class);
1031 unregister_chrdev(VIOTAPE_MAJOR, "viotape");
1032 viopath_close(viopath_hostLp, viomajorsubtype_tape, VIOTAPE_MAXREQ + 2);
1033 vio_clearHandler(viomajorsubtype_tape);
1034 clear_op_struct_pool();
1037 MODULE_LICENSE("GPL");
1038 module_init(viotap_init);
1039 module_exit(viotap_exit);