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