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