ia64/linux-2.6.18-xen.hg

view drivers/char/viotape.c @ 893:f994bfe9b93b

linux/blktap2: reduce TLB flush scope

c/s 885 added very coarse TLB flushing. Since these flushes always
follow single page updates, single page flushes (when available) are
sufficient.

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