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