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