ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / char / viotape.c
1 /* -*- linux-c -*-
2  *  drivers/char/viotape.c
3  *
4  *  iSeries Virtual Tape
5  *
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>
10  *
11  * (C) Copyright 2000-2004 IBM Corporation
12  *
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.
17  *
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.
22  *
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
26  *
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.
29  *
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
33  */
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>
52
53 #include <asm/uaccess.h>
54 #include <asm/ioctls.h>
55
56 #include <asm/iSeries/vio.h>
57 #include <asm/iSeries/HvLpEvent.h>
58 #include <asm/iSeries/HvCallEvent.h>
59 #include <asm/iSeries/HvLpConfig.h>
60
61 #define VIOTAPE_VERSION         "1.2"
62 #define VIOTAPE_MAXREQ          1
63
64 #define VIOTAPE_KERN_WARN       KERN_WARNING "viotape: "
65 #define VIOTAPE_KERN_INFO       KERN_INFO "viotape: "
66
67 static int viotape_numdev;
68
69 /*
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
73  */
74 struct viot_devinfo_struct {
75         int devno;
76         int mode;
77         int rewind;
78 };
79
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
96
97 struct viotapelpevent {
98         struct HvLpEvent event;
99         u32 reserved;
100         u16 version;
101         u16 sub_type_result;
102         u16 tape;
103         u16 flags;
104         u32 token;
105         u64 len;
106         union {
107                 struct {
108                         u32 tape_op;
109                         u32 count;
110                 } op;
111                 struct {
112                         u32 type;
113                         u32 resid;
114                         u32 dsreg;
115                         u32 gstat;
116                         u32 erreg;
117                         u32 file_no;
118                         u32 block_no;
119                 } get_status;
120                 struct {
121                         u32 block_no;
122                 } get_pos;
123         } u;
124 };
125
126 enum viotapesubtype {
127         viotapeopen = 0x0001,
128         viotapeclose = 0x0002,
129         viotaperead = 0x0003,
130         viotapewrite = 0x0004,
131         viotapegetinfo = 0x0005,
132         viotapeop = 0x0006,
133         viotapegetpos = 0x0007,
134         viotapesetpos = 0x0008,
135         viotapegetstatus = 0x0009
136 };
137
138 enum viotaperc {
139         viotape_InvalidRange = 0x0601,
140         viotape_InvalidToken = 0x0602,
141         viotape_DMAError = 0x0603,
142         viotape_UseError = 0x0604,
143         viotape_ReleaseError = 0x0605,
144         viotape_InvalidTape = 0x0606,
145         viotape_InvalidOp = 0x0607,
146         viotape_TapeErr = 0x0608,
147
148         viotape_AllocTimedOut = 0x0640,
149         viotape_BOTEnc = 0x0641,
150         viotape_BlankTape = 0x0642,
151         viotape_BufferEmpty = 0x0643,
152         viotape_CleanCartFound = 0x0644,
153         viotape_CmdNotAllowed = 0x0645,
154         viotape_CmdNotSupported = 0x0646,
155         viotape_DataCheck = 0x0647,
156         viotape_DecompressErr = 0x0648,
157         viotape_DeviceTimeout = 0x0649,
158         viotape_DeviceUnavail = 0x064a,
159         viotape_DeviceBusy = 0x064b,
160         viotape_EndOfMedia = 0x064c,
161         viotape_EndOfTape = 0x064d,
162         viotape_EquipCheck = 0x064e,
163         viotape_InsufficientRs = 0x064f,
164         viotape_InvalidLogBlk = 0x0650,
165         viotape_LengthError = 0x0651,
166         viotape_LibDoorOpen = 0x0652,
167         viotape_LoadFailure = 0x0653,
168         viotape_NotCapable = 0x0654,
169         viotape_NotOperational = 0x0655,
170         viotape_NotReady = 0x0656,
171         viotape_OpCancelled = 0x0657,
172         viotape_PhyLinkErr = 0x0658,
173         viotape_RdyNotBOT = 0x0659,
174         viotape_TapeMark = 0x065a,
175         viotape_WriteProt = 0x065b
176 };
177
178 static const struct vio_error_entry viotape_err_table[] = {
179         { viotape_InvalidRange, EIO, "Internal error" },
180         { viotape_InvalidToken, EIO, "Internal error" },
181         { viotape_DMAError, EIO, "DMA error" },
182         { viotape_UseError, EIO, "Internal error" },
183         { viotape_ReleaseError, EIO, "Internal error" },
184         { viotape_InvalidTape, EIO, "Invalid tape device" },
185         { viotape_InvalidOp, EIO, "Invalid operation" },
186         { viotape_TapeErr, EIO, "Tape error" },
187         { viotape_AllocTimedOut, EBUSY, "Allocate timed out" },
188         { viotape_BOTEnc, EIO, "Beginning of tape encountered" },
189         { viotape_BlankTape, EIO, "Blank tape" },
190         { viotape_BufferEmpty, EIO, "Buffer empty" },
191         { viotape_CleanCartFound, ENOMEDIUM, "Cleaning cartridge found" },
192         { viotape_CmdNotAllowed, EIO, "Command not allowed" },
193         { viotape_CmdNotSupported, EIO, "Command not supported" },
194         { viotape_DataCheck, EIO, "Data check" },
195         { viotape_DecompressErr, EIO, "Decompression error" },
196         { viotape_DeviceTimeout, EBUSY, "Device timeout" },
197         { viotape_DeviceUnavail, EIO, "Device unavailable" },
198         { viotape_DeviceBusy, EBUSY, "Device busy" },
199         { viotape_EndOfMedia, ENOSPC, "End of media" },
200         { viotape_EndOfTape, ENOSPC, "End of tape" },
201         { viotape_EquipCheck, EIO, "Equipment check" },
202         { viotape_InsufficientRs, EOVERFLOW, "Insufficient tape resources" },
203         { viotape_InvalidLogBlk, EIO, "Invalid logical block location" },
204         { viotape_LengthError, EOVERFLOW, "Length error" },
205         { viotape_LibDoorOpen, EBUSY, "Door open" },
206         { viotape_LoadFailure, ENOMEDIUM, "Load failure" },
207         { viotape_NotCapable, EIO, "Not capable" },
208         { viotape_NotOperational, EIO, "Not operational" },
209         { viotape_NotReady, EIO, "Not ready" },
210         { viotape_OpCancelled, EIO, "Operation cancelled" },
211         { viotape_PhyLinkErr, EIO, "Physical link error" },
212         { viotape_RdyNotBOT, EIO, "Ready but not beginning of tape" },
213         { viotape_TapeMark, EIO, "Tape mark" },
214         { viotape_WriteProt, EROFS, "Write protection error" },
215         { 0, 0, NULL },
216 };
217
218 /* Maximum number of tapes we support */
219 #define VIOTAPE_MAX_TAPE        8
220 #define MAX_PARTITIONS          4
221
222 /* defines for current tape state */
223 #define VIOT_IDLE               0
224 #define VIOT_READING            1
225 #define VIOT_WRITING            2
226
227 /* Our info on the tapes */
228 struct tape_descr {
229         char rsrcname[10];
230         char type[4];
231         char model[3];
232 };
233
234 static struct tape_descr *viotape_unitinfo;
235 static dma_addr_t viotape_unitinfo_token;
236
237 static struct mtget viomtget[VIOTAPE_MAX_TAPE];
238
239 static struct class_simple *tape_class;
240
241 /*
242  * maintain the current state of each tape (and partition)
243  * so that we know when to write EOF marks.
244  */
245 static struct {
246         unsigned char   cur_part;
247         int             dev_handle;
248         unsigned char   part_stat_rwi[MAX_PARTITIONS];
249 } state[VIOTAPE_MAX_TAPE];
250
251 /* We single-thread */
252 static struct semaphore reqSem;
253
254 /*
255  * When we send a request, we use this struct to get the response back
256  * from the interrupt handler
257  */
258 struct op_struct {
259         void                    *buffer;
260         dma_addr_t              dmaaddr;
261         size_t                  count;
262         int                     rc;
263         int                     non_blocking;
264         struct completion       com;
265         struct op_struct        *next;
266 };
267
268 static spinlock_t       op_struct_list_lock;
269 static struct op_struct *op_struct_list;
270
271 /* forward declaration to resolve interdependence */
272 static int chg_state(int index, unsigned char new_state, struct file *file);
273
274 /* procfs support */
275 static int proc_viotape_show(struct seq_file *m, void *v)
276 {
277         int i;
278
279         seq_printf(m, "viotape driver version " VIOTAPE_VERSION "\n");
280         for (i = 0; i < viotape_numdev; i++) {
281                 seq_printf(m, "viotape device %d is iSeries resource %10.10s"
282                                 "type %4.4s, model %3.3s\n",
283                                 i, viotape_unitinfo[i].rsrcname,
284                                 viotape_unitinfo[i].type,
285                                 viotape_unitinfo[i].model);
286         }
287         return 0;
288 }
289
290 static int proc_viotape_open(struct inode *inode, struct file *file)
291 {
292         return single_open(file, proc_viotape_show, NULL);
293 }
294
295 static struct file_operations proc_viotape_operations = {
296         .open           = proc_viotape_open,
297         .read           = seq_read,
298         .llseek         = seq_lseek,
299         .release        = single_release,
300 };
301
302 /* Decode the device minor number into its parts */
303 void get_dev_info(struct inode *ino, struct viot_devinfo_struct *devi)
304 {
305         devi->devno = iminor(ino) & 0x1F;
306         devi->mode = (iminor(ino) & 0x60) >> 5;
307         /* if bit is set in the minor, do _not_ rewind automatically */
308         devi->rewind = (iminor(ino) & 0x80) == 0;
309 }
310
311 /* This is called only from the exit and init paths, so no need for locking */
312 static void clear_op_struct_pool(void)
313 {
314         while (op_struct_list) {
315                 struct op_struct *toFree = op_struct_list;
316                 op_struct_list = op_struct_list->next;
317                 kfree(toFree);
318         }
319 }
320
321 /* Likewise, this is only called from the init path */
322 static int add_op_structs(int structs)
323 {
324         int i;
325
326         for (i = 0; i < structs; ++i) {
327                 struct op_struct *new_struct =
328                         kmalloc(sizeof(*new_struct), GFP_KERNEL);
329                 if (!new_struct) {
330                         clear_op_struct_pool();
331                         return -ENOMEM;
332                 }
333                 new_struct->next = op_struct_list;
334                 op_struct_list = new_struct;
335         }
336         return 0;
337 }
338
339 /* Allocate an op structure from our pool */
340 static struct op_struct *get_op_struct(void)
341 {
342         struct op_struct *retval;
343         unsigned long flags;
344
345         spin_lock_irqsave(&op_struct_list_lock, flags);
346         retval = op_struct_list;
347         if (retval)
348                 op_struct_list = retval->next;
349         spin_unlock_irqrestore(&op_struct_list_lock, flags);
350         if (retval) {
351                 memset(retval, 0, sizeof(*retval));
352                 init_completion(&retval->com);
353         }
354
355         return retval;
356 }
357
358 /* Return an op structure to our pool */
359 static void free_op_struct(struct op_struct *op_struct)
360 {
361         unsigned long flags;
362
363         spin_lock_irqsave(&op_struct_list_lock, flags);
364         op_struct->next = op_struct_list;
365         op_struct_list = op_struct;
366         spin_unlock_irqrestore(&op_struct_list_lock, flags);
367 }
368
369 /* Map our tape return codes to errno values */
370 int tape_rc_to_errno(int tape_rc, char *operation, int tapeno)
371 {
372         const struct vio_error_entry *err;
373
374         if (tape_rc == 0)
375                 return 0;
376
377         err = vio_lookup_rc(viotape_err_table, tape_rc);
378         printk(VIOTAPE_KERN_WARN "error(%s) 0x%04x on Device %d (%-10s): %s\n",
379                         operation, tape_rc, tapeno,
380                         viotape_unitinfo[tapeno].rsrcname, err->msg);
381         return -err->errno;
382 }
383
384 /* Get info on all tapes from OS/400 */
385 static int get_viotape_info(void)
386 {
387         HvLpEvent_Rc hvrc;
388         int i;
389         size_t len = sizeof(*viotape_unitinfo) * VIOTAPE_MAX_TAPE;
390         struct op_struct *op = get_op_struct();
391
392         if (op == NULL)
393                 return -ENOMEM;
394
395         viotape_unitinfo = dma_alloc_coherent(iSeries_vio_dev, len,
396                 &viotape_unitinfo_token, GFP_ATOMIC);
397         if (viotape_unitinfo == NULL) {
398                 free_op_struct(op);
399                 return -ENOMEM;
400         }
401
402         memset(viotape_unitinfo, 0, len);
403
404         hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
405                         HvLpEvent_Type_VirtualIo,
406                         viomajorsubtype_tape | viotapegetinfo,
407                         HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck,
408                         viopath_sourceinst(viopath_hostLp),
409                         viopath_targetinst(viopath_hostLp),
410                         (u64) (unsigned long) op, VIOVERSION << 16,
411                         viotape_unitinfo_token, len, 0, 0);
412         if (hvrc != HvLpEvent_Rc_Good) {
413                 printk(VIOTAPE_KERN_WARN "hv error on op %d\n",
414                                 (int)hvrc);
415                 free_op_struct(op);
416                 return -EIO;
417         }
418
419         wait_for_completion(&op->com);
420
421         free_op_struct(op);
422
423         for (i = 0;
424              ((i < VIOTAPE_MAX_TAPE) && (viotape_unitinfo[i].rsrcname[0]));
425              i++)
426                 viotape_numdev++;
427         return 0;
428 }
429
430
431 /* Write */
432 static ssize_t viotap_write(struct file *file, const char *buf,
433                 size_t count, loff_t * ppos)
434 {
435         HvLpEvent_Rc hvrc;
436         unsigned short flags = file->f_flags;
437         int noblock = ((flags & O_NONBLOCK) != 0);
438         ssize_t ret;
439         struct viot_devinfo_struct devi;
440         struct op_struct *op = get_op_struct();
441
442         if (op == NULL)
443                 return -ENOMEM;
444
445         get_dev_info(file->f_dentry->d_inode, &devi);
446
447         /*
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
452          */
453         if (noblock) {
454                 if (down_trylock(&reqSem)) {
455                         ret = -EWOULDBLOCK;
456                         goto free_op;
457                 }
458         } else
459                 down(&reqSem);
460
461         /* Allocate a DMA buffer */
462         op->buffer = dma_alloc_coherent(iSeries_vio_dev, count, &op->dmaaddr,
463                         GFP_ATOMIC);
464
465         if (op->buffer == NULL) {
466                 printk(VIOTAPE_KERN_WARN
467                                 "error allocating dma buffer for len %ld\n",
468                                 count);
469                 ret = -EFAULT;
470                 goto up_sem;
471         }
472
473         /* Copy the data into the buffer */
474         if (copy_from_user(op->buffer, buf, count)) {
475                 printk(VIOTAPE_KERN_WARN "tape: error on copy from user\n");
476                 ret = -EFAULT;
477                 goto free_dma;
478         }
479
480         op->non_blocking = noblock;
481         init_completion(&op->com);
482         op->count = count;
483
484         hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
485                         HvLpEvent_Type_VirtualIo,
486                         viomajorsubtype_tape | viotapewrite,
487                         HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck,
488                         viopath_sourceinst(viopath_hostLp),
489                         viopath_targetinst(viopath_hostLp),
490                         (u64)(unsigned long)op, VIOVERSION << 16,
491                         ((u64)devi.devno << 48) | op->dmaaddr, count, 0, 0);
492         if (hvrc != HvLpEvent_Rc_Good) {
493                 printk(VIOTAPE_KERN_WARN "hv error on op %d\n",
494                                 (int)hvrc);
495                 ret = -EIO;
496                 goto free_dma;
497         }
498
499         if (noblock)
500                 return count;
501
502         wait_for_completion(&op->com);
503
504         if (op->rc)
505                 ret = tape_rc_to_errno(op->rc, "write", devi.devno);
506         else {
507                 chg_state(devi.devno, VIOT_WRITING, file);
508                 ret = op->count;
509         }
510
511 free_dma:
512         dma_free_coherent(iSeries_vio_dev, count, op->buffer, op->dmaaddr);
513 up_sem:
514         up(&reqSem);
515 free_op:
516         free_op_struct(op);
517         return ret;
518 }
519
520 /* read */
521 static ssize_t viotap_read(struct file *file, char *buf, size_t count,
522                 loff_t *ptr)
523 {
524         HvLpEvent_Rc hvrc;
525         unsigned short flags = file->f_flags;
526         struct op_struct *op = get_op_struct();
527         int noblock = ((flags & O_NONBLOCK) != 0);
528         ssize_t ret;
529         struct viot_devinfo_struct devi;
530
531         if (op == NULL)
532                 return -ENOMEM;
533
534         get_dev_info(file->f_dentry->d_inode, &devi);
535
536         /*
537          * We need to make sure we can send a request.  We use
538          * a semaphore to keep track of # requests in use.  If
539          * we are non-blocking, make sure we don't block on the
540          * semaphore
541          */
542         if (noblock) {
543                 if (down_trylock(&reqSem)) {
544                         ret = -EWOULDBLOCK;
545                         goto free_op;
546                 }
547         } else
548                 down(&reqSem);
549
550         chg_state(devi.devno, VIOT_READING, file);
551
552         /* Allocate a DMA buffer */
553         op->buffer = dma_alloc_coherent(iSeries_vio_dev, count, &op->dmaaddr,
554                         GFP_ATOMIC);
555         if (op->buffer == NULL) {
556                 ret = -EFAULT;
557                 goto up_sem;
558         }
559
560         op->count = count;
561         init_completion(&op->com);
562
563         hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
564                         HvLpEvent_Type_VirtualIo,
565                         viomajorsubtype_tape | viotaperead,
566                         HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck,
567                         viopath_sourceinst(viopath_hostLp),
568                         viopath_targetinst(viopath_hostLp),
569                         (u64)(unsigned long)op, VIOVERSION << 16,
570                         ((u64)devi.devno << 48) | op->dmaaddr, count, 0, 0);
571         if (hvrc != HvLpEvent_Rc_Good) {
572                 printk(VIOTAPE_KERN_WARN "tape hv error on op %d\n",
573                                 (int)hvrc);
574                 ret = -EIO;
575                 goto free_dma;
576         }
577
578         wait_for_completion(&op->com);
579
580         if (op->rc)
581                 ret = tape_rc_to_errno(op->rc, "read", devi.devno);
582         else {
583                 ret = op->count;
584                 if (ret && copy_to_user(buf, op->buffer, ret)) {
585                         printk(VIOTAPE_KERN_WARN "error on copy_to_user\n");
586                         ret = -EFAULT;
587                 }
588         }
589
590 free_dma:
591         dma_free_coherent(iSeries_vio_dev, count, op->buffer, op->dmaaddr);
592 up_sem:
593         up(&reqSem);
594 free_op:
595         free_op_struct(op);
596         return ret;
597 }
598
599 /* ioctl */
600 static int viotap_ioctl(struct inode *inode, struct file *file,
601                 unsigned int cmd, unsigned long arg)
602 {
603         HvLpEvent_Rc hvrc;
604         int ret;
605         struct viot_devinfo_struct devi;
606         struct mtop mtc;
607         u32 myOp;
608         struct op_struct *op = get_op_struct();
609
610         if (op == NULL)
611                 return -ENOMEM;
612
613         get_dev_info(file->f_dentry->d_inode, &devi);
614
615         down(&reqSem);
616
617         ret = -EINVAL;
618
619         switch (cmd) {
620         case MTIOCTOP:
621                 ret = -EFAULT;
622                 /*
623                  * inode is null if and only if we (the kernel)
624                  * made the request
625                  */
626                 if (inode == NULL)
627                         memcpy(&mtc, (void *) arg, sizeof(struct mtop));
628                 else if (copy_from_user((char *)&mtc, (char *)arg,
629                                         sizeof(struct mtop)))
630                         goto free_op;
631
632                 ret = -EIO;
633                 switch (mtc.mt_op) {
634                 case MTRESET:
635                         myOp = VIOTAPOP_RESET;
636                         break;
637                 case MTFSF:
638                         myOp = VIOTAPOP_FSF;
639                         break;
640                 case MTBSF:
641                         myOp = VIOTAPOP_BSF;
642                         break;
643                 case MTFSR:
644                         myOp = VIOTAPOP_FSR;
645                         break;
646                 case MTBSR:
647                         myOp = VIOTAPOP_BSR;
648                         break;
649                 case MTWEOF:
650                         myOp = VIOTAPOP_WEOF;
651                         break;
652                 case MTREW:
653                         myOp = VIOTAPOP_REW;
654                         break;
655                 case MTNOP:
656                         myOp = VIOTAPOP_NOP;
657                         break;
658                 case MTEOM:
659                         myOp = VIOTAPOP_EOM;
660                         break;
661                 case MTERASE:
662                         myOp = VIOTAPOP_ERASE;
663                         break;
664                 case MTSETBLK:
665                         myOp = VIOTAPOP_SETBLK;
666                         break;
667                 case MTSETDENSITY:
668                         myOp = VIOTAPOP_SETDENSITY;
669                         break;
670                 case MTTELL:
671                         myOp = VIOTAPOP_GETPOS;
672                         break;
673                 case MTSEEK:
674                         myOp = VIOTAPOP_SETPOS;
675                         break;
676                 case MTSETPART:
677                         myOp = VIOTAPOP_SETPART;
678                         break;
679                 case MTOFFL:
680                         myOp = VIOTAPOP_UNLOAD;
681                         break;
682                 default:
683                         printk(VIOTAPE_KERN_WARN "MTIOCTOP called "
684                                         "with invalid op 0x%x\n", mtc.mt_op);
685                         goto free_op;
686                 }
687
688                 /*
689                  * if we moved the head, we are no longer
690                  * reading or writing
691                  */
692                 switch (mtc.mt_op) {
693                 case MTFSF:
694                 case MTBSF:
695                 case MTFSR:
696                 case MTBSR:
697                 case MTTELL:
698                 case MTSEEK:
699                 case MTREW:
700                         chg_state(devi.devno, VIOT_IDLE, file);
701                 }
702
703                 init_completion(&op->com);
704                 hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
705                                 HvLpEvent_Type_VirtualIo,
706                                 viomajorsubtype_tape | viotapeop,
707                                 HvLpEvent_AckInd_DoAck,
708                                 HvLpEvent_AckType_ImmediateAck,
709                                 viopath_sourceinst(viopath_hostLp),
710                                 viopath_targetinst(viopath_hostLp),
711                                 (u64)(unsigned long)op,
712                                 VIOVERSION << 16,
713                                 ((u64)devi.devno << 48), 0,
714                                 (((u64)myOp) << 32) | mtc.mt_count, 0);
715                 if (hvrc != HvLpEvent_Rc_Good) {
716                         printk(VIOTAPE_KERN_WARN "hv error on op %d\n",
717                                         (int)hvrc);
718                         goto free_op;
719                 }
720                 wait_for_completion(&op->com);
721                 ret = tape_rc_to_errno(op->rc, "tape operation", devi.devno);
722                 goto free_op;
723
724         case MTIOCGET:
725                 ret = -EIO;
726                 init_completion(&op->com);
727                 hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
728                                 HvLpEvent_Type_VirtualIo,
729                                 viomajorsubtype_tape | viotapegetstatus,
730                                 HvLpEvent_AckInd_DoAck,
731                                 HvLpEvent_AckType_ImmediateAck,
732                                 viopath_sourceinst(viopath_hostLp),
733                                 viopath_targetinst(viopath_hostLp),
734                                 (u64)(unsigned long)op, VIOVERSION << 16,
735                                 ((u64)devi.devno << 48), 0, 0, 0);
736                 if (hvrc != HvLpEvent_Rc_Good) {
737                         printk(VIOTAPE_KERN_WARN "hv error on op %d\n",
738                                         (int)hvrc);
739                         goto free_op;
740                 }
741                 wait_for_completion(&op->com);
742
743                 /* Operation is complete - grab the error code */
744                 ret = tape_rc_to_errno(op->rc, "get status", devi.devno);
745                 free_op_struct(op);
746                 up(&reqSem);
747
748                 if ((ret == 0) && copy_to_user((void *)arg,
749                                         &viomtget[devi.devno],
750                                         sizeof(viomtget[0])))
751                         ret = -EFAULT;
752                 return ret;
753         case MTIOCPOS:
754                 printk(VIOTAPE_KERN_WARN "Got an (unsupported) MTIOCPOS\n");
755                 break;
756         default:
757                 printk(VIOTAPE_KERN_WARN "got an unsupported ioctl 0x%0x\n",
758                                 cmd);
759                 break;
760         }
761
762 free_op:
763         free_op_struct(op);
764         up(&reqSem);
765         return ret;
766 }
767
768 static int viotap_open(struct inode *inode, struct file *file)
769 {
770         HvLpEvent_Rc hvrc;
771         struct viot_devinfo_struct devi;
772         int ret;
773         struct op_struct *op = get_op_struct();
774
775         if (op == NULL)
776                 return -ENOMEM;
777
778         get_dev_info(file->f_dentry->d_inode, &devi);
779
780         /* Note: We currently only support one mode! */
781         if ((devi.devno >= viotape_numdev) || (devi.mode)) {
782                 ret = -ENODEV;
783                 goto free_op;
784         }
785
786         init_completion(&op->com);
787
788         hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
789                         HvLpEvent_Type_VirtualIo,
790                         viomajorsubtype_tape | viotapeopen,
791                         HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck,
792                         viopath_sourceinst(viopath_hostLp),
793                         viopath_targetinst(viopath_hostLp),
794                         (u64)(unsigned long)op, VIOVERSION << 16,
795                         ((u64)devi.devno << 48), 0, 0, 0);
796         if (hvrc != 0) {
797                 printk(VIOTAPE_KERN_WARN "bad rc on signalLpEvent %d\n",
798                                 (int) hvrc);
799                 ret = -EIO;
800                 goto free_op;
801         }
802
803         wait_for_completion(&op->com);
804         ret = tape_rc_to_errno(op->rc, "open", devi.devno);
805
806 free_op:
807         free_op_struct(op);
808         return ret;
809 }
810
811
812 static int viotap_release(struct inode *inode, struct file *file)
813 {
814         HvLpEvent_Rc hvrc;
815         struct viot_devinfo_struct devi;
816         int ret = 0;
817         struct op_struct *op = get_op_struct();
818
819         if (op == NULL)
820                 return -ENOMEM;
821         init_completion(&op->com);
822
823         get_dev_info(file->f_dentry->d_inode, &devi);
824
825         if (devi.devno >= viotape_numdev) {
826                 ret = -ENODEV;
827                 goto free_op;
828         }
829
830         chg_state(devi.devno, VIOT_IDLE, file);
831
832         if (devi.rewind) {
833                 hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
834                                 HvLpEvent_Type_VirtualIo,
835                                 viomajorsubtype_tape | viotapeop,
836                                 HvLpEvent_AckInd_DoAck,
837                                 HvLpEvent_AckType_ImmediateAck,
838                                 viopath_sourceinst(viopath_hostLp),
839                                 viopath_targetinst(viopath_hostLp),
840                                 (u64)(unsigned long)op, VIOVERSION << 16,
841                                 ((u64)devi.devno << 48), 0,
842                                 ((u64)VIOTAPOP_REW) << 32, 0);
843                 wait_for_completion(&op->com);
844
845                 tape_rc_to_errno(op->rc, "rewind", devi.devno);
846         }
847
848         hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
849                         HvLpEvent_Type_VirtualIo,
850                         viomajorsubtype_tape | viotapeclose,
851                         HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck,
852                         viopath_sourceinst(viopath_hostLp),
853                         viopath_targetinst(viopath_hostLp),
854                         (u64)(unsigned long)op, VIOVERSION << 16,
855                         ((u64)devi.devno << 48), 0, 0, 0);
856         if (hvrc != 0) {
857                 printk(VIOTAPE_KERN_WARN "bad rc on signalLpEvent %d\n",
858                                 (int) hvrc);
859                 ret = -EIO;
860                 goto free_op;
861         }
862
863         wait_for_completion(&op->com);
864
865         if (op->rc)
866                 printk(VIOTAPE_KERN_WARN "close failed\n");
867
868 free_op:
869         free_op_struct(op);
870         return ret;
871 }
872
873 struct file_operations viotap_fops = {
874         owner: THIS_MODULE,
875         read: viotap_read,
876         write: viotap_write,
877         ioctl: viotap_ioctl,
878         open: viotap_open,
879         release: viotap_release,
880 };
881
882 /* Handle interrupt events for tape */
883 static void vioHandleTapeEvent(struct HvLpEvent *event)
884 {
885         int tapeminor;
886         struct op_struct *op;
887         struct viotapelpevent *tevent = (struct viotapelpevent *)event;
888
889         if (event == NULL) {
890                 /* Notification that a partition went away! */
891                 if (!viopath_isactive(viopath_hostLp)) {
892                         /* TODO! Clean up */
893                 }
894                 return;
895         }
896
897         tapeminor = event->xSubtype & VIOMINOR_SUBTYPE_MASK;
898         op = (struct op_struct *)event->xCorrelationToken;
899         switch (tapeminor) {
900         case viotapegetinfo:
901         case viotapeopen:
902         case viotapeclose:
903                 op->rc = tevent->sub_type_result;
904                 complete(&op->com);
905                 break;
906         case viotaperead:
907                 op->rc = tevent->sub_type_result;
908                 op->count = tevent->len;
909                 complete(&op->com);
910                 break;
911         case viotapewrite:
912                 if (op->non_blocking) {
913                         dma_free_coherent(iSeries_vio_dev, op->count,
914                                         op->buffer, op->dmaaddr);
915                         free_op_struct(op);
916                         up(&reqSem);
917                 } else {
918                         op->rc = tevent->sub_type_result;
919                         op->count = tevent->len;
920                         complete(&op->com);
921                 }
922                 break;
923         case viotapeop:
924         case viotapegetpos:
925         case viotapesetpos:
926         case viotapegetstatus:
927                 if (op) {
928                         op->count = tevent->u.op.count;
929                         op->rc = tevent->sub_type_result;
930                         if (!op->non_blocking)
931                                 complete(&op->com);
932                 }
933                 break;
934         default:
935                 printk(VIOTAPE_KERN_WARN "wierd ack\n");
936         }
937 }
938
939
940 int __init viotap_init(void)
941 {
942         int ret;
943         char tapename[32];
944         int i;
945         struct proc_dir_entry *e;
946
947         op_struct_list = NULL;
948         if ((ret = add_op_structs(VIOTAPE_MAXREQ)) < 0) {
949                 printk(VIOTAPE_KERN_WARN "couldn't allocate op structs\n");
950                 return ret;
951         }
952         spin_lock_init(&op_struct_list_lock);
953
954         sema_init(&reqSem, VIOTAPE_MAXREQ);
955
956         if (viopath_hostLp == HvLpIndexInvalid) {
957                 vio_set_hostlp();
958                 if (viopath_hostLp == HvLpIndexInvalid) {
959                         ret = -ENODEV;
960                         goto clear_op;
961                 }
962         }
963
964         ret = viopath_open(viopath_hostLp, viomajorsubtype_tape,
965                         VIOTAPE_MAXREQ + 2);
966         if (ret) {
967                 printk(VIOTAPE_KERN_WARN
968                                 "error on viopath_open to hostlp %d\n", ret);
969                 ret = -EIO;
970                 goto clear_op;
971         }
972
973         printk(VIOTAPE_KERN_INFO "vers " VIOTAPE_VERSION
974                         ", hosting partition %d\n", viopath_hostLp);
975
976         vio_setHandler(viomajorsubtype_tape, vioHandleTapeEvent);
977
978         ret = register_chrdev(VIOTAPE_MAJOR, "viotape", &viotap_fops);
979         if (ret < 0) {
980                 printk(VIOTAPE_KERN_WARN "Error registering viotape device\n");
981                 goto clear_handler;
982         }
983
984         tape_class = class_simple_create(THIS_MODULE, "tape");
985         if (IS_ERR(tape_class)) {
986                 printk(VIOTAPE_KERN_WARN "Unable to allocat class\n");
987                 ret = PTR_ERR(tape_class);
988                 goto unreg_chrdev;
989         }
990
991         if ((ret = get_viotape_info()) < 0) {
992                 printk(VIOTAPE_KERN_WARN "Unable to obtain virtual device information");
993                 goto unreg_class;
994         }
995
996         for (i = 0; i < viotape_numdev; i++) {
997                 int j;
998
999                 state[i].cur_part = 0;
1000                 for (j = 0; j < MAX_PARTITIONS; ++j)
1001                         state[i].part_stat_rwi[j] = VIOT_IDLE;
1002                 class_simple_device_add(tape_class, MKDEV(VIOTAPE_MAJOR, i),
1003                                 NULL, "iseries!vt%d", i);
1004                 class_simple_device_add(tape_class,
1005                                 MKDEV(VIOTAPE_MAJOR, i | 0x80),
1006                                 NULL, "iseries!nvt%d", i);
1007                 devfs_mk_cdev(MKDEV(VIOTAPE_MAJOR, i),
1008                                 S_IFCHR | S_IRUSR | S_IWUSR,
1009                                 "iseries/vt%d", i);
1010                 devfs_mk_cdev(MKDEV(VIOTAPE_MAJOR, i | 0x80),
1011                                 S_IFCHR | S_IRUSR | S_IWUSR,
1012                                 "iseries/nvt%d", i);
1013                 sprintf(tapename, "iseries/vt%d", i);
1014                 state[i].dev_handle = devfs_register_tape(tapename);
1015                 printk(VIOTAPE_KERN_INFO "tape %s is iSeries "
1016                                 "resource %10.10s type %4.4s, model %3.3s\n",
1017                                 tapename, viotape_unitinfo[i].rsrcname,
1018                                 viotape_unitinfo[i].type,
1019                                 viotape_unitinfo[i].model);
1020         }
1021
1022         e = create_proc_entry("iSeries/viotape", S_IFREG|S_IRUGO, NULL);
1023         if (e) {
1024                 e->owner = THIS_MODULE;
1025                 e->proc_fops = &proc_viotape_operations;
1026         }
1027
1028         return 0;
1029
1030 unreg_class:
1031         class_simple_destroy(tape_class);
1032 unreg_chrdev:
1033         unregister_chrdev(VIOTAPE_MAJOR, "viotape");
1034 clear_handler:
1035         vio_clearHandler(viomajorsubtype_tape);
1036         viopath_close(viopath_hostLp, viomajorsubtype_tape, VIOTAPE_MAXREQ + 2);
1037 clear_op:
1038         clear_op_struct_pool();
1039         return ret;
1040 }
1041
1042 /* Give a new state to the tape object */
1043 static int chg_state(int index, unsigned char new_state, struct file *file)
1044 {
1045         unsigned char *cur_state =
1046             &state[index].part_stat_rwi[state[index].cur_part];
1047         int rc = 0;
1048
1049         /* if the same state, don't bother */
1050         if (*cur_state == new_state)
1051                 return 0;
1052
1053         /* write an EOF if changing from writing to some other state */
1054         if (*cur_state == VIOT_WRITING) {
1055                 struct mtop write_eof = { MTWEOF, 1 };
1056
1057                 rc = viotap_ioctl(NULL, file, MTIOCTOP,
1058                                   (unsigned long)&write_eof);
1059         }
1060         *cur_state = new_state;
1061         return rc;
1062 }
1063
1064 /* Cleanup */
1065 static void __exit viotap_exit(void)
1066 {
1067         int i, ret;
1068
1069         remove_proc_entry("iSeries/viotape", NULL);
1070
1071         for (i = 0; i < viotape_numdev; ++i) {
1072                 devfs_remove("iseries/nvt%d", i);
1073                 devfs_remove("iseries/vt%d", i);
1074                 devfs_unregister_tape(state[i].dev_handle);
1075                 class_simple_device_remove(MKDEV(VIOTAPE_MAJOR, i | 0x80));
1076                 class_simple_device_remove(MKDEV(VIOTAPE_MAJOR, i));
1077         }
1078         class_simple_destroy(tape_class);
1079         ret = unregister_chrdev(VIOTAPE_MAJOR, "viotape");
1080         if (ret < 0)
1081                 printk(VIOTAPE_KERN_WARN "Error unregistering device: %d\n",
1082                                 ret);
1083         if (viotape_unitinfo)
1084                 dma_free_coherent(iSeries_vio_dev,
1085                                 sizeof(viotape_unitinfo[0]) * VIOTAPE_MAX_TAPE,
1086                                 viotape_unitinfo, viotape_unitinfo_token);
1087         viopath_close(viopath_hostLp, viomajorsubtype_tape, VIOTAPE_MAXREQ + 2);
1088         vio_clearHandler(viomajorsubtype_tape);
1089         clear_op_struct_pool();
1090 }
1091
1092 MODULE_LICENSE("GPL");
1093 module_init(viotap_init);
1094 module_exit(viotap_exit);