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