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