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