usb: gadget zero timer init fix
[linux-2.6/mini2440.git] / drivers / char / viotape.c
blob58aad63831f4da854216bde2cd7b78ca87c0e7c0
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/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>
50 #include <asm/uaccess.h>
51 #include <asm/ioctls.h>
52 #include <asm/firmware.h>
53 #include <asm/vio.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
70 * them out
72 struct viot_devinfo_struct {
73 int devno;
74 int mode;
75 int rewind;
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 enum viotaperc {
96 viotape_InvalidRange = 0x0601,
97 viotape_InvalidToken = 0x0602,
98 viotape_DMAError = 0x0603,
99 viotape_UseError = 0x0604,
100 viotape_ReleaseError = 0x0605,
101 viotape_InvalidTape = 0x0606,
102 viotape_InvalidOp = 0x0607,
103 viotape_TapeErr = 0x0608,
105 viotape_AllocTimedOut = 0x0640,
106 viotape_BOTEnc = 0x0641,
107 viotape_BlankTape = 0x0642,
108 viotape_BufferEmpty = 0x0643,
109 viotape_CleanCartFound = 0x0644,
110 viotape_CmdNotAllowed = 0x0645,
111 viotape_CmdNotSupported = 0x0646,
112 viotape_DataCheck = 0x0647,
113 viotape_DecompressErr = 0x0648,
114 viotape_DeviceTimeout = 0x0649,
115 viotape_DeviceUnavail = 0x064a,
116 viotape_DeviceBusy = 0x064b,
117 viotape_EndOfMedia = 0x064c,
118 viotape_EndOfTape = 0x064d,
119 viotape_EquipCheck = 0x064e,
120 viotape_InsufficientRs = 0x064f,
121 viotape_InvalidLogBlk = 0x0650,
122 viotape_LengthError = 0x0651,
123 viotape_LibDoorOpen = 0x0652,
124 viotape_LoadFailure = 0x0653,
125 viotape_NotCapable = 0x0654,
126 viotape_NotOperational = 0x0655,
127 viotape_NotReady = 0x0656,
128 viotape_OpCancelled = 0x0657,
129 viotape_PhyLinkErr = 0x0658,
130 viotape_RdyNotBOT = 0x0659,
131 viotape_TapeMark = 0x065a,
132 viotape_WriteProt = 0x065b
135 static const struct vio_error_entry viotape_err_table[] = {
136 { viotape_InvalidRange, EIO, "Internal error" },
137 { viotape_InvalidToken, EIO, "Internal error" },
138 { viotape_DMAError, EIO, "DMA error" },
139 { viotape_UseError, EIO, "Internal error" },
140 { viotape_ReleaseError, EIO, "Internal error" },
141 { viotape_InvalidTape, EIO, "Invalid tape device" },
142 { viotape_InvalidOp, EIO, "Invalid operation" },
143 { viotape_TapeErr, EIO, "Tape error" },
144 { viotape_AllocTimedOut, EBUSY, "Allocate timed out" },
145 { viotape_BOTEnc, EIO, "Beginning of tape encountered" },
146 { viotape_BlankTape, EIO, "Blank tape" },
147 { viotape_BufferEmpty, EIO, "Buffer empty" },
148 { viotape_CleanCartFound, ENOMEDIUM, "Cleaning cartridge found" },
149 { viotape_CmdNotAllowed, EIO, "Command not allowed" },
150 { viotape_CmdNotSupported, EIO, "Command not supported" },
151 { viotape_DataCheck, EIO, "Data check" },
152 { viotape_DecompressErr, EIO, "Decompression error" },
153 { viotape_DeviceTimeout, EBUSY, "Device timeout" },
154 { viotape_DeviceUnavail, EIO, "Device unavailable" },
155 { viotape_DeviceBusy, EBUSY, "Device busy" },
156 { viotape_EndOfMedia, ENOSPC, "End of media" },
157 { viotape_EndOfTape, ENOSPC, "End of tape" },
158 { viotape_EquipCheck, EIO, "Equipment check" },
159 { viotape_InsufficientRs, EOVERFLOW, "Insufficient tape resources" },
160 { viotape_InvalidLogBlk, EIO, "Invalid logical block location" },
161 { viotape_LengthError, EOVERFLOW, "Length error" },
162 { viotape_LibDoorOpen, EBUSY, "Door open" },
163 { viotape_LoadFailure, ENOMEDIUM, "Load failure" },
164 { viotape_NotCapable, EIO, "Not capable" },
165 { viotape_NotOperational, EIO, "Not operational" },
166 { viotape_NotReady, EIO, "Not ready" },
167 { viotape_OpCancelled, EIO, "Operation cancelled" },
168 { viotape_PhyLinkErr, EIO, "Physical link error" },
169 { viotape_RdyNotBOT, EIO, "Ready but not beginning of tape" },
170 { viotape_TapeMark, EIO, "Tape mark" },
171 { viotape_WriteProt, EROFS, "Write protection error" },
172 { 0, 0, NULL },
175 /* Maximum number of tapes we support */
176 #define VIOTAPE_MAX_TAPE HVMAXARCHITECTEDVIRTUALTAPES
177 #define MAX_PARTITIONS 4
179 /* defines for current tape state */
180 #define VIOT_IDLE 0
181 #define VIOT_READING 1
182 #define VIOT_WRITING 2
184 /* Our info on the tapes */
185 static struct {
186 const char *rsrcname;
187 const char *type;
188 const char *model;
189 } viotape_unitinfo[VIOTAPE_MAX_TAPE];
191 static struct mtget viomtget[VIOTAPE_MAX_TAPE];
193 static struct class *tape_class;
195 static struct device *tape_device[VIOTAPE_MAX_TAPE];
198 * maintain the current state of each tape (and partition)
199 * so that we know when to write EOF marks.
201 static struct {
202 unsigned char cur_part;
203 unsigned char part_stat_rwi[MAX_PARTITIONS];
204 } state[VIOTAPE_MAX_TAPE];
206 /* We single-thread */
207 static struct semaphore reqSem;
210 * When we send a request, we use this struct to get the response back
211 * from the interrupt handler
213 struct op_struct {
214 void *buffer;
215 dma_addr_t dmaaddr;
216 size_t count;
217 int rc;
218 int non_blocking;
219 struct completion com;
220 struct device *dev;
221 struct op_struct *next;
224 static spinlock_t op_struct_list_lock;
225 static struct op_struct *op_struct_list;
227 /* forward declaration to resolve interdependence */
228 static int chg_state(int index, unsigned char new_state, struct file *file);
230 /* procfs support */
231 static int proc_viotape_show(struct seq_file *m, void *v)
233 int i;
235 seq_printf(m, "viotape driver version " VIOTAPE_VERSION "\n");
236 for (i = 0; i < viotape_numdev; i++) {
237 seq_printf(m, "viotape device %d is iSeries resource %10.10s"
238 "type %4.4s, model %3.3s\n",
239 i, viotape_unitinfo[i].rsrcname,
240 viotape_unitinfo[i].type,
241 viotape_unitinfo[i].model);
243 return 0;
246 static int proc_viotape_open(struct inode *inode, struct file *file)
248 return single_open(file, proc_viotape_show, NULL);
251 static const struct file_operations proc_viotape_operations = {
252 .owner = THIS_MODULE,
253 .open = proc_viotape_open,
254 .read = seq_read,
255 .llseek = seq_lseek,
256 .release = single_release,
259 /* Decode the device minor number into its parts */
260 void get_dev_info(struct inode *ino, struct viot_devinfo_struct *devi)
262 devi->devno = iminor(ino) & 0x1F;
263 devi->mode = (iminor(ino) & 0x60) >> 5;
264 /* if bit is set in the minor, do _not_ rewind automatically */
265 devi->rewind = (iminor(ino) & 0x80) == 0;
268 /* This is called only from the exit and init paths, so no need for locking */
269 static void clear_op_struct_pool(void)
271 while (op_struct_list) {
272 struct op_struct *toFree = op_struct_list;
273 op_struct_list = op_struct_list->next;
274 kfree(toFree);
278 /* Likewise, this is only called from the init path */
279 static int add_op_structs(int structs)
281 int i;
283 for (i = 0; i < structs; ++i) {
284 struct op_struct *new_struct =
285 kmalloc(sizeof(*new_struct), GFP_KERNEL);
286 if (!new_struct) {
287 clear_op_struct_pool();
288 return -ENOMEM;
290 new_struct->next = op_struct_list;
291 op_struct_list = new_struct;
293 return 0;
296 /* Allocate an op structure from our pool */
297 static struct op_struct *get_op_struct(void)
299 struct op_struct *retval;
300 unsigned long flags;
302 spin_lock_irqsave(&op_struct_list_lock, flags);
303 retval = op_struct_list;
304 if (retval)
305 op_struct_list = retval->next;
306 spin_unlock_irqrestore(&op_struct_list_lock, flags);
307 if (retval) {
308 memset(retval, 0, sizeof(*retval));
309 init_completion(&retval->com);
312 return retval;
315 /* Return an op structure to our pool */
316 static void free_op_struct(struct op_struct *op_struct)
318 unsigned long flags;
320 spin_lock_irqsave(&op_struct_list_lock, flags);
321 op_struct->next = op_struct_list;
322 op_struct_list = op_struct;
323 spin_unlock_irqrestore(&op_struct_list_lock, flags);
326 /* Map our tape return codes to errno values */
327 int tape_rc_to_errno(int tape_rc, char *operation, int tapeno)
329 const struct vio_error_entry *err;
331 if (tape_rc == 0)
332 return 0;
334 err = vio_lookup_rc(viotape_err_table, tape_rc);
335 printk(VIOTAPE_KERN_WARN "error(%s) 0x%04x on Device %d (%-10s): %s\n",
336 operation, tape_rc, tapeno,
337 viotape_unitinfo[tapeno].rsrcname, err->msg);
338 return -err->errno;
341 /* Write */
342 static ssize_t viotap_write(struct file *file, const char *buf,
343 size_t count, loff_t * ppos)
345 HvLpEvent_Rc hvrc;
346 unsigned short flags = file->f_flags;
347 int noblock = ((flags & O_NONBLOCK) != 0);
348 ssize_t ret;
349 struct viot_devinfo_struct devi;
350 struct op_struct *op = get_op_struct();
352 if (op == NULL)
353 return -ENOMEM;
355 get_dev_info(file->f_path.dentry->d_inode, &devi);
358 * We need to make sure we can send a request. We use
359 * a semaphore to keep track of # requests in use. If
360 * we are non-blocking, make sure we don't block on the
361 * semaphore
363 if (noblock) {
364 if (down_trylock(&reqSem)) {
365 ret = -EWOULDBLOCK;
366 goto free_op;
368 } else
369 down(&reqSem);
371 /* Allocate a DMA buffer */
372 op->dev = tape_device[devi.devno];
373 op->buffer = dma_alloc_coherent(op->dev, count, &op->dmaaddr,
374 GFP_ATOMIC);
376 if (op->buffer == NULL) {
377 printk(VIOTAPE_KERN_WARN
378 "error allocating dma buffer for len %ld\n",
379 count);
380 ret = -EFAULT;
381 goto up_sem;
384 /* Copy the data into the buffer */
385 if (copy_from_user(op->buffer, buf, count)) {
386 printk(VIOTAPE_KERN_WARN "tape: error on copy from user\n");
387 ret = -EFAULT;
388 goto free_dma;
391 op->non_blocking = noblock;
392 init_completion(&op->com);
393 op->count = count;
395 hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
396 HvLpEvent_Type_VirtualIo,
397 viomajorsubtype_tape | viotapewrite,
398 HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck,
399 viopath_sourceinst(viopath_hostLp),
400 viopath_targetinst(viopath_hostLp),
401 (u64)(unsigned long)op, VIOVERSION << 16,
402 ((u64)devi.devno << 48) | op->dmaaddr, count, 0, 0);
403 if (hvrc != HvLpEvent_Rc_Good) {
404 printk(VIOTAPE_KERN_WARN "hv error on op %d\n",
405 (int)hvrc);
406 ret = -EIO;
407 goto free_dma;
410 if (noblock)
411 return count;
413 wait_for_completion(&op->com);
415 if (op->rc)
416 ret = tape_rc_to_errno(op->rc, "write", devi.devno);
417 else {
418 chg_state(devi.devno, VIOT_WRITING, file);
419 ret = op->count;
422 free_dma:
423 dma_free_coherent(op->dev, count, op->buffer, op->dmaaddr);
424 up_sem:
425 up(&reqSem);
426 free_op:
427 free_op_struct(op);
428 return ret;
431 /* read */
432 static ssize_t viotap_read(struct file *file, char *buf, size_t count,
433 loff_t *ptr)
435 HvLpEvent_Rc hvrc;
436 unsigned short flags = file->f_flags;
437 struct op_struct *op = get_op_struct();
438 int noblock = ((flags & O_NONBLOCK) != 0);
439 ssize_t ret;
440 struct viot_devinfo_struct devi;
442 if (op == NULL)
443 return -ENOMEM;
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
451 * semaphore
453 if (noblock) {
454 if (down_trylock(&reqSem)) {
455 ret = -EWOULDBLOCK;
456 goto free_op;
458 } else
459 down(&reqSem);
461 chg_state(devi.devno, VIOT_READING, file);
463 /* Allocate a DMA buffer */
464 op->dev = tape_device[devi.devno];
465 op->buffer = dma_alloc_coherent(op->dev, count, &op->dmaaddr,
466 GFP_ATOMIC);
467 if (op->buffer == NULL) {
468 ret = -EFAULT;
469 goto up_sem;
472 op->count = count;
473 init_completion(&op->com);
475 hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
476 HvLpEvent_Type_VirtualIo,
477 viomajorsubtype_tape | viotaperead,
478 HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck,
479 viopath_sourceinst(viopath_hostLp),
480 viopath_targetinst(viopath_hostLp),
481 (u64)(unsigned long)op, VIOVERSION << 16,
482 ((u64)devi.devno << 48) | op->dmaaddr, count, 0, 0);
483 if (hvrc != HvLpEvent_Rc_Good) {
484 printk(VIOTAPE_KERN_WARN "tape hv error on op %d\n",
485 (int)hvrc);
486 ret = -EIO;
487 goto free_dma;
490 wait_for_completion(&op->com);
492 if (op->rc)
493 ret = tape_rc_to_errno(op->rc, "read", devi.devno);
494 else {
495 ret = op->count;
496 if (ret && copy_to_user(buf, op->buffer, ret)) {
497 printk(VIOTAPE_KERN_WARN "error on copy_to_user\n");
498 ret = -EFAULT;
502 free_dma:
503 dma_free_coherent(op->dev, count, op->buffer, op->dmaaddr);
504 up_sem:
505 up(&reqSem);
506 free_op:
507 free_op_struct(op);
508 return ret;
511 /* ioctl */
512 static int viotap_ioctl(struct inode *inode, struct file *file,
513 unsigned int cmd, unsigned long arg)
515 HvLpEvent_Rc hvrc;
516 int ret;
517 struct viot_devinfo_struct devi;
518 struct mtop mtc;
519 u32 myOp;
520 struct op_struct *op = get_op_struct();
522 if (op == NULL)
523 return -ENOMEM;
525 get_dev_info(file->f_path.dentry->d_inode, &devi);
527 down(&reqSem);
529 ret = -EINVAL;
531 switch (cmd) {
532 case MTIOCTOP:
533 ret = -EFAULT;
535 * inode is null if and only if we (the kernel)
536 * made the request
538 if (inode == NULL)
539 memcpy(&mtc, (void *) arg, sizeof(struct mtop));
540 else if (copy_from_user((char *)&mtc, (char *)arg,
541 sizeof(struct mtop)))
542 goto free_op;
544 ret = -EIO;
545 switch (mtc.mt_op) {
546 case MTRESET:
547 myOp = VIOTAPOP_RESET;
548 break;
549 case MTFSF:
550 myOp = VIOTAPOP_FSF;
551 break;
552 case MTBSF:
553 myOp = VIOTAPOP_BSF;
554 break;
555 case MTFSR:
556 myOp = VIOTAPOP_FSR;
557 break;
558 case MTBSR:
559 myOp = VIOTAPOP_BSR;
560 break;
561 case MTWEOF:
562 myOp = VIOTAPOP_WEOF;
563 break;
564 case MTREW:
565 myOp = VIOTAPOP_REW;
566 break;
567 case MTNOP:
568 myOp = VIOTAPOP_NOP;
569 break;
570 case MTEOM:
571 myOp = VIOTAPOP_EOM;
572 break;
573 case MTERASE:
574 myOp = VIOTAPOP_ERASE;
575 break;
576 case MTSETBLK:
577 myOp = VIOTAPOP_SETBLK;
578 break;
579 case MTSETDENSITY:
580 myOp = VIOTAPOP_SETDENSITY;
581 break;
582 case MTTELL:
583 myOp = VIOTAPOP_GETPOS;
584 break;
585 case MTSEEK:
586 myOp = VIOTAPOP_SETPOS;
587 break;
588 case MTSETPART:
589 myOp = VIOTAPOP_SETPART;
590 break;
591 case MTOFFL:
592 myOp = VIOTAPOP_UNLOAD;
593 break;
594 default:
595 printk(VIOTAPE_KERN_WARN "MTIOCTOP called "
596 "with invalid op 0x%x\n", mtc.mt_op);
597 goto free_op;
601 * if we moved the head, we are no longer
602 * reading or writing
604 switch (mtc.mt_op) {
605 case MTFSF:
606 case MTBSF:
607 case MTFSR:
608 case MTBSR:
609 case MTTELL:
610 case MTSEEK:
611 case MTREW:
612 chg_state(devi.devno, VIOT_IDLE, file);
615 init_completion(&op->com);
616 hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
617 HvLpEvent_Type_VirtualIo,
618 viomajorsubtype_tape | viotapeop,
619 HvLpEvent_AckInd_DoAck,
620 HvLpEvent_AckType_ImmediateAck,
621 viopath_sourceinst(viopath_hostLp),
622 viopath_targetinst(viopath_hostLp),
623 (u64)(unsigned long)op,
624 VIOVERSION << 16,
625 ((u64)devi.devno << 48), 0,
626 (((u64)myOp) << 32) | mtc.mt_count, 0);
627 if (hvrc != HvLpEvent_Rc_Good) {
628 printk(VIOTAPE_KERN_WARN "hv error on op %d\n",
629 (int)hvrc);
630 goto free_op;
632 wait_for_completion(&op->com);
633 ret = tape_rc_to_errno(op->rc, "tape operation", devi.devno);
634 goto free_op;
636 case MTIOCGET:
637 ret = -EIO;
638 init_completion(&op->com);
639 hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
640 HvLpEvent_Type_VirtualIo,
641 viomajorsubtype_tape | viotapegetstatus,
642 HvLpEvent_AckInd_DoAck,
643 HvLpEvent_AckType_ImmediateAck,
644 viopath_sourceinst(viopath_hostLp),
645 viopath_targetinst(viopath_hostLp),
646 (u64)(unsigned long)op, VIOVERSION << 16,
647 ((u64)devi.devno << 48), 0, 0, 0);
648 if (hvrc != HvLpEvent_Rc_Good) {
649 printk(VIOTAPE_KERN_WARN "hv error on op %d\n",
650 (int)hvrc);
651 goto free_op;
653 wait_for_completion(&op->com);
655 /* Operation is complete - grab the error code */
656 ret = tape_rc_to_errno(op->rc, "get status", devi.devno);
657 free_op_struct(op);
658 up(&reqSem);
660 if ((ret == 0) && copy_to_user((void *)arg,
661 &viomtget[devi.devno],
662 sizeof(viomtget[0])))
663 ret = -EFAULT;
664 return ret;
665 case MTIOCPOS:
666 printk(VIOTAPE_KERN_WARN "Got an (unsupported) MTIOCPOS\n");
667 break;
668 default:
669 printk(VIOTAPE_KERN_WARN "got an unsupported ioctl 0x%0x\n",
670 cmd);
671 break;
674 free_op:
675 free_op_struct(op);
676 up(&reqSem);
677 return ret;
680 static int viotap_open(struct inode *inode, struct file *file)
682 HvLpEvent_Rc hvrc;
683 struct viot_devinfo_struct devi;
684 int ret;
685 struct op_struct *op = get_op_struct();
687 if (op == NULL)
688 return -ENOMEM;
690 get_dev_info(file->f_path.dentry->d_inode, &devi);
692 /* Note: We currently only support one mode! */
693 if ((devi.devno >= viotape_numdev) || (devi.mode)) {
694 ret = -ENODEV;
695 goto free_op;
698 init_completion(&op->com);
700 hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
701 HvLpEvent_Type_VirtualIo,
702 viomajorsubtype_tape | viotapeopen,
703 HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck,
704 viopath_sourceinst(viopath_hostLp),
705 viopath_targetinst(viopath_hostLp),
706 (u64)(unsigned long)op, VIOVERSION << 16,
707 ((u64)devi.devno << 48), 0, 0, 0);
708 if (hvrc != 0) {
709 printk(VIOTAPE_KERN_WARN "bad rc on signalLpEvent %d\n",
710 (int) hvrc);
711 ret = -EIO;
712 goto free_op;
715 wait_for_completion(&op->com);
716 ret = tape_rc_to_errno(op->rc, "open", devi.devno);
718 free_op:
719 free_op_struct(op);
720 return ret;
724 static int viotap_release(struct inode *inode, struct file *file)
726 HvLpEvent_Rc hvrc;
727 struct viot_devinfo_struct devi;
728 int ret = 0;
729 struct op_struct *op = get_op_struct();
731 if (op == NULL)
732 return -ENOMEM;
733 init_completion(&op->com);
735 get_dev_info(file->f_path.dentry->d_inode, &devi);
737 if (devi.devno >= viotape_numdev) {
738 ret = -ENODEV;
739 goto free_op;
742 chg_state(devi.devno, VIOT_IDLE, file);
744 if (devi.rewind) {
745 hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
746 HvLpEvent_Type_VirtualIo,
747 viomajorsubtype_tape | viotapeop,
748 HvLpEvent_AckInd_DoAck,
749 HvLpEvent_AckType_ImmediateAck,
750 viopath_sourceinst(viopath_hostLp),
751 viopath_targetinst(viopath_hostLp),
752 (u64)(unsigned long)op, VIOVERSION << 16,
753 ((u64)devi.devno << 48), 0,
754 ((u64)VIOTAPOP_REW) << 32, 0);
755 wait_for_completion(&op->com);
757 tape_rc_to_errno(op->rc, "rewind", devi.devno);
760 hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
761 HvLpEvent_Type_VirtualIo,
762 viomajorsubtype_tape | viotapeclose,
763 HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck,
764 viopath_sourceinst(viopath_hostLp),
765 viopath_targetinst(viopath_hostLp),
766 (u64)(unsigned long)op, VIOVERSION << 16,
767 ((u64)devi.devno << 48), 0, 0, 0);
768 if (hvrc != 0) {
769 printk(VIOTAPE_KERN_WARN "bad rc on signalLpEvent %d\n",
770 (int) hvrc);
771 ret = -EIO;
772 goto free_op;
775 wait_for_completion(&op->com);
777 if (op->rc)
778 printk(VIOTAPE_KERN_WARN "close failed\n");
780 free_op:
781 free_op_struct(op);
782 return ret;
785 const struct file_operations viotap_fops = {
786 .owner = THIS_MODULE,
787 .read = viotap_read,
788 .write = viotap_write,
789 .ioctl = viotap_ioctl,
790 .open = viotap_open,
791 .release = viotap_release,
794 /* Handle interrupt events for tape */
795 static void vioHandleTapeEvent(struct HvLpEvent *event)
797 int tapeminor;
798 struct op_struct *op;
799 struct viotapelpevent *tevent = (struct viotapelpevent *)event;
801 if (event == NULL) {
802 /* Notification that a partition went away! */
803 if (!viopath_isactive(viopath_hostLp)) {
804 /* TODO! Clean up */
806 return;
809 tapeminor = event->xSubtype & VIOMINOR_SUBTYPE_MASK;
810 op = (struct op_struct *)event->xCorrelationToken;
811 switch (tapeminor) {
812 case viotapeopen:
813 case viotapeclose:
814 op->rc = tevent->sub_type_result;
815 complete(&op->com);
816 break;
817 case viotaperead:
818 op->rc = tevent->sub_type_result;
819 op->count = tevent->len;
820 complete(&op->com);
821 break;
822 case viotapewrite:
823 if (op->non_blocking) {
824 dma_free_coherent(op->dev, op->count,
825 op->buffer, op->dmaaddr);
826 free_op_struct(op);
827 up(&reqSem);
828 } else {
829 op->rc = tevent->sub_type_result;
830 op->count = tevent->len;
831 complete(&op->com);
833 break;
834 case viotapeop:
835 case viotapegetpos:
836 case viotapesetpos:
837 case viotapegetstatus:
838 if (op) {
839 op->count = tevent->u.op.count;
840 op->rc = tevent->sub_type_result;
841 if (!op->non_blocking)
842 complete(&op->com);
844 break;
845 default:
846 printk(VIOTAPE_KERN_WARN "weird ack\n");
850 static int viotape_probe(struct vio_dev *vdev, const struct vio_device_id *id)
852 int i = vdev->unit_address;
853 int j;
854 struct device_node *node = vdev->dev.archdata.of_node;
856 if (i > VIOTAPE_MAX_TAPE)
857 return -ENODEV;
858 if (!node)
859 return -ENODEV;
861 if (i >= viotape_numdev)
862 viotape_numdev = i + 1;
864 tape_device[i] = &vdev->dev;
865 viotape_unitinfo[i].rsrcname = of_get_property(node,
866 "linux,vio_rsrcname", NULL);
867 viotape_unitinfo[i].type = of_get_property(node, "linux,vio_type",
868 NULL);
869 viotape_unitinfo[i].model = of_get_property(node, "linux,vio_model",
870 NULL);
872 state[i].cur_part = 0;
873 for (j = 0; j < MAX_PARTITIONS; ++j)
874 state[i].part_stat_rwi[j] = VIOT_IDLE;
875 device_create(tape_class, NULL, MKDEV(VIOTAPE_MAJOR, i),
876 "iseries!vt%d", i);
877 device_create(tape_class, NULL, MKDEV(VIOTAPE_MAJOR, i | 0x80),
878 "iseries!nvt%d", i);
879 printk(VIOTAPE_KERN_INFO "tape iseries/vt%d is iSeries "
880 "resource %10.10s type %4.4s, model %3.3s\n",
881 i, viotape_unitinfo[i].rsrcname,
882 viotape_unitinfo[i].type, viotape_unitinfo[i].model);
883 return 0;
886 static int viotape_remove(struct vio_dev *vdev)
888 int i = vdev->unit_address;
890 device_destroy(tape_class, MKDEV(VIOTAPE_MAJOR, i | 0x80));
891 device_destroy(tape_class, MKDEV(VIOTAPE_MAJOR, i));
892 return 0;
896 * viotape_device_table: Used by vio.c to match devices that we
897 * support.
899 static struct vio_device_id viotape_device_table[] __devinitdata = {
900 { "byte", "IBM,iSeries-viotape" },
901 { "", "" }
903 MODULE_DEVICE_TABLE(vio, viotape_device_table);
905 static struct vio_driver viotape_driver = {
906 .id_table = viotape_device_table,
907 .probe = viotape_probe,
908 .remove = viotape_remove,
909 .driver = {
910 .name = "viotape",
911 .owner = THIS_MODULE,
916 int __init viotap_init(void)
918 int ret;
920 if (!firmware_has_feature(FW_FEATURE_ISERIES))
921 return -ENODEV;
923 op_struct_list = NULL;
924 if ((ret = add_op_structs(VIOTAPE_MAXREQ)) < 0) {
925 printk(VIOTAPE_KERN_WARN "couldn't allocate op structs\n");
926 return ret;
928 spin_lock_init(&op_struct_list_lock);
930 sema_init(&reqSem, VIOTAPE_MAXREQ);
932 if (viopath_hostLp == HvLpIndexInvalid) {
933 vio_set_hostlp();
934 if (viopath_hostLp == HvLpIndexInvalid) {
935 ret = -ENODEV;
936 goto clear_op;
940 ret = viopath_open(viopath_hostLp, viomajorsubtype_tape,
941 VIOTAPE_MAXREQ + 2);
942 if (ret) {
943 printk(VIOTAPE_KERN_WARN
944 "error on viopath_open to hostlp %d\n", ret);
945 ret = -EIO;
946 goto clear_op;
949 printk(VIOTAPE_KERN_INFO "vers " VIOTAPE_VERSION
950 ", hosting partition %d\n", viopath_hostLp);
952 vio_setHandler(viomajorsubtype_tape, vioHandleTapeEvent);
954 ret = register_chrdev(VIOTAPE_MAJOR, "viotape", &viotap_fops);
955 if (ret < 0) {
956 printk(VIOTAPE_KERN_WARN "Error registering viotape device\n");
957 goto clear_handler;
960 tape_class = class_create(THIS_MODULE, "tape");
961 if (IS_ERR(tape_class)) {
962 printk(VIOTAPE_KERN_WARN "Unable to allocat class\n");
963 ret = PTR_ERR(tape_class);
964 goto unreg_chrdev;
967 ret = vio_register_driver(&viotape_driver);
968 if (ret)
969 goto unreg_class;
971 proc_create("iSeries/viotape", S_IFREG|S_IRUGO, NULL,
972 &proc_viotape_operations);
974 return 0;
976 unreg_class:
977 class_destroy(tape_class);
978 unreg_chrdev:
979 unregister_chrdev(VIOTAPE_MAJOR, "viotape");
980 clear_handler:
981 vio_clearHandler(viomajorsubtype_tape);
982 viopath_close(viopath_hostLp, viomajorsubtype_tape, VIOTAPE_MAXREQ + 2);
983 clear_op:
984 clear_op_struct_pool();
985 return ret;
988 /* Give a new state to the tape object */
989 static int chg_state(int index, unsigned char new_state, struct file *file)
991 unsigned char *cur_state =
992 &state[index].part_stat_rwi[state[index].cur_part];
993 int rc = 0;
995 /* if the same state, don't bother */
996 if (*cur_state == new_state)
997 return 0;
999 /* write an EOF if changing from writing to some other state */
1000 if (*cur_state == VIOT_WRITING) {
1001 struct mtop write_eof = { MTWEOF, 1 };
1003 rc = viotap_ioctl(NULL, file, MTIOCTOP,
1004 (unsigned long)&write_eof);
1006 *cur_state = new_state;
1007 return rc;
1010 /* Cleanup */
1011 static void __exit viotap_exit(void)
1013 remove_proc_entry("iSeries/viotape", NULL);
1014 vio_unregister_driver(&viotape_driver);
1015 class_destroy(tape_class);
1016 unregister_chrdev(VIOTAPE_MAJOR, "viotape");
1017 viopath_close(viopath_hostLp, viomajorsubtype_tape, VIOTAPE_MAXREQ + 2);
1018 vio_clearHandler(viomajorsubtype_tape);
1019 clear_op_struct_pool();
1022 MODULE_LICENSE("GPL");
1023 module_init(viotap_init);
1024 module_exit(viotap_exit);