b101134a8443da6de63f9627247af6517f0c0fef
[safe/jmp/linux-2.6] / 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/module.h>
36 #include <linux/kernel.h>
37 #include <linux/errno.h>
38 #include <linux/init.h>
39 #include <linux/wait.h>
40 #include <linux/spinlock.h>
41 #include <linux/mtio.h>
42 #include <linux/device.h>
43 #include <linux/dma-mapping.h>
44 #include <linux/fs.h>
45 #include <linux/cdev.h>
46 #include <linux/devfs_fs_kernel.h>
47 #include <linux/major.h>
48 #include <linux/completion.h>
49 #include <linux/proc_fs.h>
50 #include <linux/seq_file.h>
51
52 #include <asm/uaccess.h>
53 #include <asm/ioctls.h>
54
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>
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        HVMAXARCHITECTEDVIRTUALTAPES
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 *tape_class;
240
241 static struct device *tape_device[VIOTAPE_MAX_TAPE];
242
243 /*
244  * maintain the current state of each tape (and partition)
245  * so that we know when to write EOF marks.
246  */
247 static struct {
248         unsigned char   cur_part;
249         unsigned char   part_stat_rwi[MAX_PARTITIONS];
250 } state[VIOTAPE_MAX_TAPE];
251
252 /* We single-thread */
253 static struct semaphore reqSem;
254
255 /*
256  * When we send a request, we use this struct to get the response back
257  * from the interrupt handler
258  */
259 struct op_struct {
260         void                    *buffer;
261         dma_addr_t              dmaaddr;
262         size_t                  count;
263         int                     rc;
264         int                     non_blocking;
265         struct completion       com;
266         struct device           *dev;
267         struct op_struct        *next;
268 };
269
270 static spinlock_t       op_struct_list_lock;
271 static struct op_struct *op_struct_list;
272
273 /* forward declaration to resolve interdependence */
274 static int chg_state(int index, unsigned char new_state, struct file *file);
275
276 /* procfs support */
277 static int proc_viotape_show(struct seq_file *m, void *v)
278 {
279         int i;
280
281         seq_printf(m, "viotape driver version " VIOTAPE_VERSION "\n");
282         for (i = 0; i < viotape_numdev; i++) {
283                 seq_printf(m, "viotape device %d is iSeries resource %10.10s"
284                                 "type %4.4s, model %3.3s\n",
285                                 i, viotape_unitinfo[i].rsrcname,
286                                 viotape_unitinfo[i].type,
287                                 viotape_unitinfo[i].model);
288         }
289         return 0;
290 }
291
292 static int proc_viotape_open(struct inode *inode, struct file *file)
293 {
294         return single_open(file, proc_viotape_show, NULL);
295 }
296
297 static struct file_operations proc_viotape_operations = {
298         .open           = proc_viotape_open,
299         .read           = seq_read,
300         .llseek         = seq_lseek,
301         .release        = single_release,
302 };
303
304 /* Decode the device minor number into its parts */
305 void get_dev_info(struct inode *ino, struct viot_devinfo_struct *devi)
306 {
307         devi->devno = iminor(ino) & 0x1F;
308         devi->mode = (iminor(ino) & 0x60) >> 5;
309         /* if bit is set in the minor, do _not_ rewind automatically */
310         devi->rewind = (iminor(ino) & 0x80) == 0;
311 }
312
313 /* This is called only from the exit and init paths, so no need for locking */
314 static void clear_op_struct_pool(void)
315 {
316         while (op_struct_list) {
317                 struct op_struct *toFree = op_struct_list;
318                 op_struct_list = op_struct_list->next;
319                 kfree(toFree);
320         }
321 }
322
323 /* Likewise, this is only called from the init path */
324 static int add_op_structs(int structs)
325 {
326         int i;
327
328         for (i = 0; i < structs; ++i) {
329                 struct op_struct *new_struct =
330                         kmalloc(sizeof(*new_struct), GFP_KERNEL);
331                 if (!new_struct) {
332                         clear_op_struct_pool();
333                         return -ENOMEM;
334                 }
335                 new_struct->next = op_struct_list;
336                 op_struct_list = new_struct;
337         }
338         return 0;
339 }
340
341 /* Allocate an op structure from our pool */
342 static struct op_struct *get_op_struct(void)
343 {
344         struct op_struct *retval;
345         unsigned long flags;
346
347         spin_lock_irqsave(&op_struct_list_lock, flags);
348         retval = op_struct_list;
349         if (retval)
350                 op_struct_list = retval->next;
351         spin_unlock_irqrestore(&op_struct_list_lock, flags);
352         if (retval) {
353                 memset(retval, 0, sizeof(*retval));
354                 init_completion(&retval->com);
355         }
356
357         return retval;
358 }
359
360 /* Return an op structure to our pool */
361 static void free_op_struct(struct op_struct *op_struct)
362 {
363         unsigned long flags;
364
365         spin_lock_irqsave(&op_struct_list_lock, flags);
366         op_struct->next = op_struct_list;
367         op_struct_list = op_struct;
368         spin_unlock_irqrestore(&op_struct_list_lock, flags);
369 }
370
371 /* Map our tape return codes to errno values */
372 int tape_rc_to_errno(int tape_rc, char *operation, int tapeno)
373 {
374         const struct vio_error_entry *err;
375
376         if (tape_rc == 0)
377                 return 0;
378
379         err = vio_lookup_rc(viotape_err_table, tape_rc);
380         printk(VIOTAPE_KERN_WARN "error(%s) 0x%04x on Device %d (%-10s): %s\n",
381                         operation, tape_rc, tapeno,
382                         viotape_unitinfo[tapeno].rsrcname, err->msg);
383         return -err->errno;
384 }
385
386 /* Get info on all tapes from OS/400 */
387 static int get_viotape_info(void)
388 {
389         HvLpEvent_Rc hvrc;
390         int i;
391         size_t len = sizeof(*viotape_unitinfo) * VIOTAPE_MAX_TAPE;
392         struct op_struct *op = get_op_struct();
393
394         if (op == NULL)
395                 return -ENOMEM;
396
397         viotape_unitinfo = dma_alloc_coherent(iSeries_vio_dev, len,
398                 &viotape_unitinfo_token, GFP_ATOMIC);
399         if (viotape_unitinfo == NULL) {
400                 free_op_struct(op);
401                 return -ENOMEM;
402         }
403
404         memset(viotape_unitinfo, 0, len);
405
406         hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
407                         HvLpEvent_Type_VirtualIo,
408                         viomajorsubtype_tape | viotapegetinfo,
409                         HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck,
410                         viopath_sourceinst(viopath_hostLp),
411                         viopath_targetinst(viopath_hostLp),
412                         (u64) (unsigned long) op, VIOVERSION << 16,
413                         viotape_unitinfo_token, len, 0, 0);
414         if (hvrc != HvLpEvent_Rc_Good) {
415                 printk(VIOTAPE_KERN_WARN "hv error on op %d\n",
416                                 (int)hvrc);
417                 free_op_struct(op);
418                 return -EIO;
419         }
420
421         wait_for_completion(&op->com);
422
423         free_op_struct(op);
424
425         for (i = 0;
426              ((i < VIOTAPE_MAX_TAPE) && (viotape_unitinfo[i].rsrcname[0]));
427              i++)
428                 viotape_numdev++;
429         return 0;
430 }
431
432
433 /* Write */
434 static ssize_t viotap_write(struct file *file, const char *buf,
435                 size_t count, loff_t * ppos)
436 {
437         HvLpEvent_Rc hvrc;
438         unsigned short flags = file->f_flags;
439         int noblock = ((flags & O_NONBLOCK) != 0);
440         ssize_t ret;
441         struct viot_devinfo_struct devi;
442         struct op_struct *op = get_op_struct();
443
444         if (op == NULL)
445                 return -ENOMEM;
446
447         get_dev_info(file->f_dentry->d_inode, &devi);
448
449         /*
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
454          */
455         if (noblock) {
456                 if (down_trylock(&reqSem)) {
457                         ret = -EWOULDBLOCK;
458                         goto free_op;
459                 }
460         } else
461                 down(&reqSem);
462
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
468         if (op->buffer == NULL) {
469                 printk(VIOTAPE_KERN_WARN
470                                 "error allocating dma buffer for len %ld\n",
471                                 count);
472                 ret = -EFAULT;
473                 goto up_sem;
474         }
475
476         /* Copy the data into the buffer */
477         if (copy_from_user(op->buffer, buf, count)) {
478                 printk(VIOTAPE_KERN_WARN "tape: error on copy from user\n");
479                 ret = -EFAULT;
480                 goto free_dma;
481         }
482
483         op->non_blocking = noblock;
484         init_completion(&op->com);
485         op->count = count;
486
487         hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
488                         HvLpEvent_Type_VirtualIo,
489                         viomajorsubtype_tape | viotapewrite,
490                         HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck,
491                         viopath_sourceinst(viopath_hostLp),
492                         viopath_targetinst(viopath_hostLp),
493                         (u64)(unsigned long)op, VIOVERSION << 16,
494                         ((u64)devi.devno << 48) | op->dmaaddr, count, 0, 0);
495         if (hvrc != HvLpEvent_Rc_Good) {
496                 printk(VIOTAPE_KERN_WARN "hv error on op %d\n",
497                                 (int)hvrc);
498                 ret = -EIO;
499                 goto free_dma;
500         }
501
502         if (noblock)
503                 return count;
504
505         wait_for_completion(&op->com);
506
507         if (op->rc)
508                 ret = tape_rc_to_errno(op->rc, "write", devi.devno);
509         else {
510                 chg_state(devi.devno, VIOT_WRITING, file);
511                 ret = op->count;
512         }
513
514 free_dma:
515         dma_free_coherent(op->dev, count, op->buffer, op->dmaaddr);
516 up_sem:
517         up(&reqSem);
518 free_op:
519         free_op_struct(op);
520         return ret;
521 }
522
523 /* read */
524 static ssize_t viotap_read(struct file *file, char *buf, size_t count,
525                 loff_t *ptr)
526 {
527         HvLpEvent_Rc hvrc;
528         unsigned short flags = file->f_flags;
529         struct op_struct *op = get_op_struct();
530         int noblock = ((flags & O_NONBLOCK) != 0);
531         ssize_t ret;
532         struct viot_devinfo_struct devi;
533
534         if (op == NULL)
535                 return -ENOMEM;
536
537         get_dev_info(file->f_dentry->d_inode, &devi);
538
539         /*
540          * We need to make sure we can send a request.  We use
541          * a semaphore to keep track of # requests in use.  If
542          * we are non-blocking, make sure we don't block on the
543          * semaphore
544          */
545         if (noblock) {
546                 if (down_trylock(&reqSem)) {
547                         ret = -EWOULDBLOCK;
548                         goto free_op;
549                 }
550         } else
551                 down(&reqSem);
552
553         chg_state(devi.devno, VIOT_READING, file);
554
555         /* Allocate a DMA buffer */
556         op->dev = tape_device[devi.devno];
557         op->buffer = dma_alloc_coherent(op->dev, count, &op->dmaaddr,
558                         GFP_ATOMIC);
559         if (op->buffer == NULL) {
560                 ret = -EFAULT;
561                 goto up_sem;
562         }
563
564         op->count = count;
565         init_completion(&op->com);
566
567         hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
568                         HvLpEvent_Type_VirtualIo,
569                         viomajorsubtype_tape | viotaperead,
570                         HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck,
571                         viopath_sourceinst(viopath_hostLp),
572                         viopath_targetinst(viopath_hostLp),
573                         (u64)(unsigned long)op, VIOVERSION << 16,
574                         ((u64)devi.devno << 48) | op->dmaaddr, count, 0, 0);
575         if (hvrc != HvLpEvent_Rc_Good) {
576                 printk(VIOTAPE_KERN_WARN "tape hv error on op %d\n",
577                                 (int)hvrc);
578                 ret = -EIO;
579                 goto free_dma;
580         }
581
582         wait_for_completion(&op->com);
583
584         if (op->rc)
585                 ret = tape_rc_to_errno(op->rc, "read", devi.devno);
586         else {
587                 ret = op->count;
588                 if (ret && copy_to_user(buf, op->buffer, ret)) {
589                         printk(VIOTAPE_KERN_WARN "error on copy_to_user\n");
590                         ret = -EFAULT;
591                 }
592         }
593
594 free_dma:
595         dma_free_coherent(op->dev, count, op->buffer, op->dmaaddr);
596 up_sem:
597         up(&reqSem);
598 free_op:
599         free_op_struct(op);
600         return ret;
601 }
602
603 /* ioctl */
604 static int viotap_ioctl(struct inode *inode, struct file *file,
605                 unsigned int cmd, unsigned long arg)
606 {
607         HvLpEvent_Rc hvrc;
608         int ret;
609         struct viot_devinfo_struct devi;
610         struct mtop mtc;
611         u32 myOp;
612         struct op_struct *op = get_op_struct();
613
614         if (op == NULL)
615                 return -ENOMEM;
616
617         get_dev_info(file->f_dentry->d_inode, &devi);
618
619         down(&reqSem);
620
621         ret = -EINVAL;
622
623         switch (cmd) {
624         case MTIOCTOP:
625                 ret = -EFAULT;
626                 /*
627                  * inode is null if and only if we (the kernel)
628                  * made the request
629                  */
630                 if (inode == NULL)
631                         memcpy(&mtc, (void *) arg, sizeof(struct mtop));
632                 else if (copy_from_user((char *)&mtc, (char *)arg,
633                                         sizeof(struct mtop)))
634                         goto free_op;
635
636                 ret = -EIO;
637                 switch (mtc.mt_op) {
638                 case MTRESET:
639                         myOp = VIOTAPOP_RESET;
640                         break;
641                 case MTFSF:
642                         myOp = VIOTAPOP_FSF;
643                         break;
644                 case MTBSF:
645                         myOp = VIOTAPOP_BSF;
646                         break;
647                 case MTFSR:
648                         myOp = VIOTAPOP_FSR;
649                         break;
650                 case MTBSR:
651                         myOp = VIOTAPOP_BSR;
652                         break;
653                 case MTWEOF:
654                         myOp = VIOTAPOP_WEOF;
655                         break;
656                 case MTREW:
657                         myOp = VIOTAPOP_REW;
658                         break;
659                 case MTNOP:
660                         myOp = VIOTAPOP_NOP;
661                         break;
662                 case MTEOM:
663                         myOp = VIOTAPOP_EOM;
664                         break;
665                 case MTERASE:
666                         myOp = VIOTAPOP_ERASE;
667                         break;
668                 case MTSETBLK:
669                         myOp = VIOTAPOP_SETBLK;
670                         break;
671                 case MTSETDENSITY:
672                         myOp = VIOTAPOP_SETDENSITY;
673                         break;
674                 case MTTELL:
675                         myOp = VIOTAPOP_GETPOS;
676                         break;
677                 case MTSEEK:
678                         myOp = VIOTAPOP_SETPOS;
679                         break;
680                 case MTSETPART:
681                         myOp = VIOTAPOP_SETPART;
682                         break;
683                 case MTOFFL:
684                         myOp = VIOTAPOP_UNLOAD;
685                         break;
686                 default:
687                         printk(VIOTAPE_KERN_WARN "MTIOCTOP called "
688                                         "with invalid op 0x%x\n", mtc.mt_op);
689                         goto free_op;
690                 }
691
692                 /*
693                  * if we moved the head, we are no longer
694                  * reading or writing
695                  */
696                 switch (mtc.mt_op) {
697                 case MTFSF:
698                 case MTBSF:
699                 case MTFSR:
700                 case MTBSR:
701                 case MTTELL:
702                 case MTSEEK:
703                 case MTREW:
704                         chg_state(devi.devno, VIOT_IDLE, file);
705                 }
706
707                 init_completion(&op->com);
708                 hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
709                                 HvLpEvent_Type_VirtualIo,
710                                 viomajorsubtype_tape | viotapeop,
711                                 HvLpEvent_AckInd_DoAck,
712                                 HvLpEvent_AckType_ImmediateAck,
713                                 viopath_sourceinst(viopath_hostLp),
714                                 viopath_targetinst(viopath_hostLp),
715                                 (u64)(unsigned long)op,
716                                 VIOVERSION << 16,
717                                 ((u64)devi.devno << 48), 0,
718                                 (((u64)myOp) << 32) | mtc.mt_count, 0);
719                 if (hvrc != HvLpEvent_Rc_Good) {
720                         printk(VIOTAPE_KERN_WARN "hv error on op %d\n",
721                                         (int)hvrc);
722                         goto free_op;
723                 }
724                 wait_for_completion(&op->com);
725                 ret = tape_rc_to_errno(op->rc, "tape operation", devi.devno);
726                 goto free_op;
727
728         case MTIOCGET:
729                 ret = -EIO;
730                 init_completion(&op->com);
731                 hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
732                                 HvLpEvent_Type_VirtualIo,
733                                 viomajorsubtype_tape | viotapegetstatus,
734                                 HvLpEvent_AckInd_DoAck,
735                                 HvLpEvent_AckType_ImmediateAck,
736                                 viopath_sourceinst(viopath_hostLp),
737                                 viopath_targetinst(viopath_hostLp),
738                                 (u64)(unsigned long)op, VIOVERSION << 16,
739                                 ((u64)devi.devno << 48), 0, 0, 0);
740                 if (hvrc != HvLpEvent_Rc_Good) {
741                         printk(VIOTAPE_KERN_WARN "hv error on op %d\n",
742                                         (int)hvrc);
743                         goto free_op;
744                 }
745                 wait_for_completion(&op->com);
746
747                 /* Operation is complete - grab the error code */
748                 ret = tape_rc_to_errno(op->rc, "get status", devi.devno);
749                 free_op_struct(op);
750                 up(&reqSem);
751
752                 if ((ret == 0) && copy_to_user((void *)arg,
753                                         &viomtget[devi.devno],
754                                         sizeof(viomtget[0])))
755                         ret = -EFAULT;
756                 return ret;
757         case MTIOCPOS:
758                 printk(VIOTAPE_KERN_WARN "Got an (unsupported) MTIOCPOS\n");
759                 break;
760         default:
761                 printk(VIOTAPE_KERN_WARN "got an unsupported ioctl 0x%0x\n",
762                                 cmd);
763                 break;
764         }
765
766 free_op:
767         free_op_struct(op);
768         up(&reqSem);
769         return ret;
770 }
771
772 static int viotap_open(struct inode *inode, struct file *file)
773 {
774         HvLpEvent_Rc hvrc;
775         struct viot_devinfo_struct devi;
776         int ret;
777         struct op_struct *op = get_op_struct();
778
779         if (op == NULL)
780                 return -ENOMEM;
781
782         get_dev_info(file->f_dentry->d_inode, &devi);
783
784         /* Note: We currently only support one mode! */
785         if ((devi.devno >= viotape_numdev) || (devi.mode)) {
786                 ret = -ENODEV;
787                 goto free_op;
788         }
789
790         init_completion(&op->com);
791
792         hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
793                         HvLpEvent_Type_VirtualIo,
794                         viomajorsubtype_tape | viotapeopen,
795                         HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck,
796                         viopath_sourceinst(viopath_hostLp),
797                         viopath_targetinst(viopath_hostLp),
798                         (u64)(unsigned long)op, VIOVERSION << 16,
799                         ((u64)devi.devno << 48), 0, 0, 0);
800         if (hvrc != 0) {
801                 printk(VIOTAPE_KERN_WARN "bad rc on signalLpEvent %d\n",
802                                 (int) hvrc);
803                 ret = -EIO;
804                 goto free_op;
805         }
806
807         wait_for_completion(&op->com);
808         ret = tape_rc_to_errno(op->rc, "open", devi.devno);
809
810 free_op:
811         free_op_struct(op);
812         return ret;
813 }
814
815
816 static int viotap_release(struct inode *inode, struct file *file)
817 {
818         HvLpEvent_Rc hvrc;
819         struct viot_devinfo_struct devi;
820         int ret = 0;
821         struct op_struct *op = get_op_struct();
822
823         if (op == NULL)
824                 return -ENOMEM;
825         init_completion(&op->com);
826
827         get_dev_info(file->f_dentry->d_inode, &devi);
828
829         if (devi.devno >= viotape_numdev) {
830                 ret = -ENODEV;
831                 goto free_op;
832         }
833
834         chg_state(devi.devno, VIOT_IDLE, file);
835
836         if (devi.rewind) {
837                 hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
838                                 HvLpEvent_Type_VirtualIo,
839                                 viomajorsubtype_tape | viotapeop,
840                                 HvLpEvent_AckInd_DoAck,
841                                 HvLpEvent_AckType_ImmediateAck,
842                                 viopath_sourceinst(viopath_hostLp),
843                                 viopath_targetinst(viopath_hostLp),
844                                 (u64)(unsigned long)op, VIOVERSION << 16,
845                                 ((u64)devi.devno << 48), 0,
846                                 ((u64)VIOTAPOP_REW) << 32, 0);
847                 wait_for_completion(&op->com);
848
849                 tape_rc_to_errno(op->rc, "rewind", devi.devno);
850         }
851
852         hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
853                         HvLpEvent_Type_VirtualIo,
854                         viomajorsubtype_tape | viotapeclose,
855                         HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck,
856                         viopath_sourceinst(viopath_hostLp),
857                         viopath_targetinst(viopath_hostLp),
858                         (u64)(unsigned long)op, VIOVERSION << 16,
859                         ((u64)devi.devno << 48), 0, 0, 0);
860         if (hvrc != 0) {
861                 printk(VIOTAPE_KERN_WARN "bad rc on signalLpEvent %d\n",
862                                 (int) hvrc);
863                 ret = -EIO;
864                 goto free_op;
865         }
866
867         wait_for_completion(&op->com);
868
869         if (op->rc)
870                 printk(VIOTAPE_KERN_WARN "close failed\n");
871
872 free_op:
873         free_op_struct(op);
874         return ret;
875 }
876
877 struct file_operations viotap_fops = {
878         owner: THIS_MODULE,
879         read: viotap_read,
880         write: viotap_write,
881         ioctl: viotap_ioctl,
882         open: viotap_open,
883         release: viotap_release,
884 };
885
886 /* Handle interrupt events for tape */
887 static void vioHandleTapeEvent(struct HvLpEvent *event)
888 {
889         int tapeminor;
890         struct op_struct *op;
891         struct viotapelpevent *tevent = (struct viotapelpevent *)event;
892
893         if (event == NULL) {
894                 /* Notification that a partition went away! */
895                 if (!viopath_isactive(viopath_hostLp)) {
896                         /* TODO! Clean up */
897                 }
898                 return;
899         }
900
901         tapeminor = event->xSubtype & VIOMINOR_SUBTYPE_MASK;
902         op = (struct op_struct *)event->xCorrelationToken;
903         switch (tapeminor) {
904         case viotapegetinfo:
905         case viotapeopen:
906         case viotapeclose:
907                 op->rc = tevent->sub_type_result;
908                 complete(&op->com);
909                 break;
910         case viotaperead:
911                 op->rc = tevent->sub_type_result;
912                 op->count = tevent->len;
913                 complete(&op->com);
914                 break;
915         case viotapewrite:
916                 if (op->non_blocking) {
917                         dma_free_coherent(op->dev, op->count,
918                                         op->buffer, op->dmaaddr);
919                         free_op_struct(op);
920                         up(&reqSem);
921                 } else {
922                         op->rc = tevent->sub_type_result;
923                         op->count = tevent->len;
924                         complete(&op->com);
925                 }
926                 break;
927         case viotapeop:
928         case viotapegetpos:
929         case viotapesetpos:
930         case viotapegetstatus:
931                 if (op) {
932                         op->count = tevent->u.op.count;
933                         op->rc = tevent->sub_type_result;
934                         if (!op->non_blocking)
935                                 complete(&op->com);
936                 }
937                 break;
938         default:
939                 printk(VIOTAPE_KERN_WARN "weird ack\n");
940         }
941 }
942
943 static int viotape_probe(struct vio_dev *vdev, const struct vio_device_id *id)
944 {
945         char tapename[32];
946         int i = vdev->unit_address;
947         int j;
948
949         if (i >= viotape_numdev)
950                 return -ENODEV;
951
952         tape_device[i] = &vdev->dev;
953
954         state[i].cur_part = 0;
955         for (j = 0; j < MAX_PARTITIONS; ++j)
956                 state[i].part_stat_rwi[j] = VIOT_IDLE;
957         class_device_create(tape_class, NULL, MKDEV(VIOTAPE_MAJOR, i), NULL,
958                         "iseries!vt%d", i);
959         class_device_create(tape_class, NULL, MKDEV(VIOTAPE_MAJOR, i | 0x80),
960                         NULL, "iseries!nvt%d", i);
961         sprintf(tapename, "iseries/vt%d", i);
962         printk(VIOTAPE_KERN_INFO "tape %s is iSeries "
963                         "resource %10.10s type %4.4s, model %3.3s\n",
964                         tapename, viotape_unitinfo[i].rsrcname,
965                         viotape_unitinfo[i].type, viotape_unitinfo[i].model);
966         return 0;
967 }
968
969 static int viotape_remove(struct vio_dev *vdev)
970 {
971         int i = vdev->unit_address;
972
973         class_device_destroy(tape_class, MKDEV(VIOTAPE_MAJOR, i | 0x80));
974         class_device_destroy(tape_class, MKDEV(VIOTAPE_MAJOR, i));
975         return 0;
976 }
977
978 /**
979  * viotape_device_table: Used by vio.c to match devices that we
980  * support.
981  */
982 static struct vio_device_id viotape_device_table[] __devinitdata = {
983         { "byte", "IBM,iSeries-viotape" },
984         { "", "" }
985 };
986 MODULE_DEVICE_TABLE(vio, viotape_device_table);
987
988 static struct vio_driver viotape_driver = {
989         .id_table = viotape_device_table,
990         .probe = viotape_probe,
991         .remove = viotape_remove,
992         .driver = {
993                 .name = "viotape",
994                 .owner = THIS_MODULE,
995         }
996 };
997
998
999 int __init viotap_init(void)
1000 {
1001         int ret;
1002         struct proc_dir_entry *e;
1003
1004         op_struct_list = NULL;
1005         if ((ret = add_op_structs(VIOTAPE_MAXREQ)) < 0) {
1006                 printk(VIOTAPE_KERN_WARN "couldn't allocate op structs\n");
1007                 return ret;
1008         }
1009         spin_lock_init(&op_struct_list_lock);
1010
1011         sema_init(&reqSem, VIOTAPE_MAXREQ);
1012
1013         if (viopath_hostLp == HvLpIndexInvalid) {
1014                 vio_set_hostlp();
1015                 if (viopath_hostLp == HvLpIndexInvalid) {
1016                         ret = -ENODEV;
1017                         goto clear_op;
1018                 }
1019         }
1020
1021         ret = viopath_open(viopath_hostLp, viomajorsubtype_tape,
1022                         VIOTAPE_MAXREQ + 2);
1023         if (ret) {
1024                 printk(VIOTAPE_KERN_WARN
1025                                 "error on viopath_open to hostlp %d\n", ret);
1026                 ret = -EIO;
1027                 goto clear_op;
1028         }
1029
1030         printk(VIOTAPE_KERN_INFO "vers " VIOTAPE_VERSION
1031                         ", hosting partition %d\n", viopath_hostLp);
1032
1033         vio_setHandler(viomajorsubtype_tape, vioHandleTapeEvent);
1034
1035         ret = register_chrdev(VIOTAPE_MAJOR, "viotape", &viotap_fops);
1036         if (ret < 0) {
1037                 printk(VIOTAPE_KERN_WARN "Error registering viotape device\n");
1038                 goto clear_handler;
1039         }
1040
1041         tape_class = class_create(THIS_MODULE, "tape");
1042         if (IS_ERR(tape_class)) {
1043                 printk(VIOTAPE_KERN_WARN "Unable to allocat class\n");
1044                 ret = PTR_ERR(tape_class);
1045                 goto unreg_chrdev;
1046         }
1047
1048         if ((ret = get_viotape_info()) < 0) {
1049                 printk(VIOTAPE_KERN_WARN "Unable to obtain virtual device information");
1050                 goto unreg_class;
1051         }
1052
1053         ret = vio_register_driver(&viotape_driver);
1054         if (ret)
1055                 goto unreg_class;
1056
1057         e = create_proc_entry("iSeries/viotape", S_IFREG|S_IRUGO, NULL);
1058         if (e) {
1059                 e->owner = THIS_MODULE;
1060                 e->proc_fops = &proc_viotape_operations;
1061         }
1062
1063         return 0;
1064
1065 unreg_class:
1066         class_destroy(tape_class);
1067 unreg_chrdev:
1068         unregister_chrdev(VIOTAPE_MAJOR, "viotape");
1069 clear_handler:
1070         vio_clearHandler(viomajorsubtype_tape);
1071         viopath_close(viopath_hostLp, viomajorsubtype_tape, VIOTAPE_MAXREQ + 2);
1072 clear_op:
1073         clear_op_struct_pool();
1074         return ret;
1075 }
1076
1077 /* Give a new state to the tape object */
1078 static int chg_state(int index, unsigned char new_state, struct file *file)
1079 {
1080         unsigned char *cur_state =
1081             &state[index].part_stat_rwi[state[index].cur_part];
1082         int rc = 0;
1083
1084         /* if the same state, don't bother */
1085         if (*cur_state == new_state)
1086                 return 0;
1087
1088         /* write an EOF if changing from writing to some other state */
1089         if (*cur_state == VIOT_WRITING) {
1090                 struct mtop write_eof = { MTWEOF, 1 };
1091
1092                 rc = viotap_ioctl(NULL, file, MTIOCTOP,
1093                                   (unsigned long)&write_eof);
1094         }
1095         *cur_state = new_state;
1096         return rc;
1097 }
1098
1099 /* Cleanup */
1100 static void __exit viotap_exit(void)
1101 {
1102         int ret;
1103
1104         remove_proc_entry("iSeries/viotape", NULL);
1105         vio_unregister_driver(&viotape_driver);
1106         class_destroy(tape_class);
1107         ret = unregister_chrdev(VIOTAPE_MAJOR, "viotape");
1108         if (ret < 0)
1109                 printk(VIOTAPE_KERN_WARN "Error unregistering device: %d\n",
1110                                 ret);
1111         if (viotape_unitinfo)
1112                 dma_free_coherent(iSeries_vio_dev,
1113                                 sizeof(viotape_unitinfo[0]) * VIOTAPE_MAX_TAPE,
1114                                 viotape_unitinfo, viotape_unitinfo_token);
1115         viopath_close(viopath_hostLp, viomajorsubtype_tape, VIOTAPE_MAXREQ + 2);
1116         vio_clearHandler(viomajorsubtype_tape);
1117         clear_op_struct_pool();
1118 }
1119
1120 MODULE_LICENSE("GPL");
1121 module_init(viotap_init);
1122 module_exit(viotap_exit);