Blame view

drivers/char/viotape.c 26 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1
2
3
4
5
6
7
8
  /* -*- linux-c -*-
   *  drivers/char/viotape.c
   *
   *  iSeries Virtual Tape
   *
   *  Authors: Dave Boutcher <boutcher@us.ibm.com>
   *           Ryan Arnold <ryanarn@us.ibm.com>
   *           Colin Devilbiss <devilbis@us.ibm.com>
8962cadbe   Stephen Rothwell   [POWERPC] iSeries...
9
   *           Stephen Rothwell
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
   *
   * (C) Copyright 2000-2004 IBM Corporation
   *
   * This program is free software;  you can redistribute it and/or
   * modify it under the terms of the GNU General Public License as
   * published by the Free Software Foundation; either version 2 of the
   * License, or (at your option) anyu later version.
   *
   * This program is distributed in the hope that it will be useful, but
   * WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   * General Public License for more details.
   *
   * You should have received a copy of the GNU General Public License
   * along with this program; if not, write to the Free Software Foundation,
   * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
   *
   * This routine provides access to tape drives owned and managed by an OS/400
   * partition running on the same box as this Linux partition.
   *
   * All tape operations are performed by sending messages back and forth to
   * the OS/400 partition.  The format of the messages is defined in
b42067787   Kelly Daly   merge filename an...
32
   * iseries/vio.h
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
33
   */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
34
35
36
37
38
39
40
41
42
43
44
  #include <linux/module.h>
  #include <linux/kernel.h>
  #include <linux/errno.h>
  #include <linux/init.h>
  #include <linux/wait.h>
  #include <linux/spinlock.h>
  #include <linux/mtio.h>
  #include <linux/device.h>
  #include <linux/dma-mapping.h>
  #include <linux/fs.h>
  #include <linux/cdev.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
45
46
47
48
  #include <linux/major.h>
  #include <linux/completion.h>
  #include <linux/proc_fs.h>
  #include <linux/seq_file.h>
613655fa3   Arnd Bergmann   drivers: autoconv...
49
  #include <linux/mutex.h>
5a0e3ad6a   Tejun Heo   include cleanup: ...
50
  #include <linux/slab.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
51
52
53
  
  #include <asm/uaccess.h>
  #include <asm/ioctls.h>
fd38451f1   Stephen Rothwell   [POWERPC] iSeries...
54
  #include <asm/firmware.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
55
  #include <asm/vio.h>
b42067787   Kelly Daly   merge filename an...
56
  #include <asm/iseries/vio.h>
e45423eac   Kelly Daly   merge filename an...
57
  #include <asm/iseries/hv_lp_event.h>
c0a8d05c8   Kelly Daly   merge filename an...
58
  #include <asm/iseries/hv_call_event.h>
15b171894   Kelly Daly   merge filename an...
59
  #include <asm/iseries/hv_lp_config.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
60
61
62
63
64
65
  
  #define VIOTAPE_VERSION		"1.2"
  #define VIOTAPE_MAXREQ		1
  
  #define VIOTAPE_KERN_WARN	KERN_WARNING "viotape: "
  #define VIOTAPE_KERN_INFO	KERN_INFO "viotape: "
613655fa3   Arnd Bergmann   drivers: autoconv...
66
  static DEFINE_MUTEX(proc_viotape_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
  static int viotape_numdev;
  
  /*
   * The minor number follows the conventions of the SCSI tape drives.  The
   * rewind and mode are encoded in the minor #.  We use this struct to break
   * them out
   */
  struct viot_devinfo_struct {
  	int devno;
  	int mode;
  	int rewind;
  };
  
  #define VIOTAPOP_RESET          0
  #define VIOTAPOP_FSF	        1
  #define VIOTAPOP_BSF	        2
  #define VIOTAPOP_FSR	        3
  #define VIOTAPOP_BSR	        4
  #define VIOTAPOP_WEOF	        5
  #define VIOTAPOP_REW	        6
  #define VIOTAPOP_NOP	        7
  #define VIOTAPOP_EOM	        8
  #define VIOTAPOP_ERASE          9
  #define VIOTAPOP_SETBLK        10
  #define VIOTAPOP_SETDENSITY    11
  #define VIOTAPOP_SETPOS	       12
  #define VIOTAPOP_GETPOS	       13
  #define VIOTAPOP_SETPART       14
  #define VIOTAPOP_UNLOAD        15
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
  enum viotaperc {
  	viotape_InvalidRange = 0x0601,
  	viotape_InvalidToken = 0x0602,
  	viotape_DMAError = 0x0603,
  	viotape_UseError = 0x0604,
  	viotape_ReleaseError = 0x0605,
  	viotape_InvalidTape = 0x0606,
  	viotape_InvalidOp = 0x0607,
  	viotape_TapeErr = 0x0608,
  
  	viotape_AllocTimedOut = 0x0640,
  	viotape_BOTEnc = 0x0641,
  	viotape_BlankTape = 0x0642,
  	viotape_BufferEmpty = 0x0643,
  	viotape_CleanCartFound = 0x0644,
  	viotape_CmdNotAllowed = 0x0645,
  	viotape_CmdNotSupported = 0x0646,
  	viotape_DataCheck = 0x0647,
  	viotape_DecompressErr = 0x0648,
  	viotape_DeviceTimeout = 0x0649,
  	viotape_DeviceUnavail = 0x064a,
  	viotape_DeviceBusy = 0x064b,
  	viotape_EndOfMedia = 0x064c,
  	viotape_EndOfTape = 0x064d,
  	viotape_EquipCheck = 0x064e,
  	viotape_InsufficientRs = 0x064f,
  	viotape_InvalidLogBlk = 0x0650,
  	viotape_LengthError = 0x0651,
  	viotape_LibDoorOpen = 0x0652,
  	viotape_LoadFailure = 0x0653,
  	viotape_NotCapable = 0x0654,
  	viotape_NotOperational = 0x0655,
  	viotape_NotReady = 0x0656,
  	viotape_OpCancelled = 0x0657,
  	viotape_PhyLinkErr = 0x0658,
  	viotape_RdyNotBOT = 0x0659,
  	viotape_TapeMark = 0x065a,
  	viotape_WriteProt = 0x065b
  };
  
  static const struct vio_error_entry viotape_err_table[] = {
  	{ viotape_InvalidRange, EIO, "Internal error" },
  	{ viotape_InvalidToken, EIO, "Internal error" },
  	{ viotape_DMAError, EIO, "DMA error" },
  	{ viotape_UseError, EIO, "Internal error" },
  	{ viotape_ReleaseError, EIO, "Internal error" },
  	{ viotape_InvalidTape, EIO, "Invalid tape device" },
  	{ viotape_InvalidOp, EIO, "Invalid operation" },
  	{ viotape_TapeErr, EIO, "Tape error" },
  	{ viotape_AllocTimedOut, EBUSY, "Allocate timed out" },
  	{ viotape_BOTEnc, EIO, "Beginning of tape encountered" },
  	{ viotape_BlankTape, EIO, "Blank tape" },
  	{ viotape_BufferEmpty, EIO, "Buffer empty" },
  	{ viotape_CleanCartFound, ENOMEDIUM, "Cleaning cartridge found" },
  	{ viotape_CmdNotAllowed, EIO, "Command not allowed" },
  	{ viotape_CmdNotSupported, EIO, "Command not supported" },
  	{ viotape_DataCheck, EIO, "Data check" },
  	{ viotape_DecompressErr, EIO, "Decompression error" },
  	{ viotape_DeviceTimeout, EBUSY, "Device timeout" },
  	{ viotape_DeviceUnavail, EIO, "Device unavailable" },
  	{ viotape_DeviceBusy, EBUSY, "Device busy" },
  	{ viotape_EndOfMedia, ENOSPC, "End of media" },
  	{ viotape_EndOfTape, ENOSPC, "End of tape" },
  	{ viotape_EquipCheck, EIO, "Equipment check" },
  	{ viotape_InsufficientRs, EOVERFLOW, "Insufficient tape resources" },
  	{ viotape_InvalidLogBlk, EIO, "Invalid logical block location" },
  	{ viotape_LengthError, EOVERFLOW, "Length error" },
  	{ viotape_LibDoorOpen, EBUSY, "Door open" },
  	{ viotape_LoadFailure, ENOMEDIUM, "Load failure" },
  	{ viotape_NotCapable, EIO, "Not capable" },
  	{ viotape_NotOperational, EIO, "Not operational" },
  	{ viotape_NotReady, EIO, "Not ready" },
  	{ viotape_OpCancelled, EIO, "Operation cancelled" },
  	{ viotape_PhyLinkErr, EIO, "Physical link error" },
  	{ viotape_RdyNotBOT, EIO, "Ready but not beginning of tape" },
  	{ viotape_TapeMark, EIO, "Tape mark" },
  	{ viotape_WriteProt, EROFS, "Write protection error" },
  	{ 0, 0, NULL },
  };
  
  /* Maximum number of tapes we support */
  #define VIOTAPE_MAX_TAPE	HVMAXARCHITECTEDVIRTUALTAPES
  #define MAX_PARTITIONS		4
  
  /* defines for current tape state */
  #define VIOT_IDLE		0
  #define VIOT_READING		1
  #define VIOT_WRITING		2
  
  /* Our info on the tapes */
7465ce0db   Stephen Rothwell   [POWERPC] iSeries...
186
187
188
189
190
  static struct {
  	const char *rsrcname;
  	const char *type;
  	const char *model;
  } viotape_unitinfo[VIOTAPE_MAX_TAPE];
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
191
192
  
  static struct mtget viomtget[VIOTAPE_MAX_TAPE];
ca8eca688   Greg Kroah-Hartman   [PATCH] class: co...
193
  static struct class *tape_class;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
194
195
196
197
198
199
200
201
202
  
  static struct device *tape_device[VIOTAPE_MAX_TAPE];
  
  /*
   * maintain the current state of each tape (and partition)
   * so that we know when to write EOF marks.
   */
  static struct {
  	unsigned char	cur_part;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
  	unsigned char	part_stat_rwi[MAX_PARTITIONS];
  } state[VIOTAPE_MAX_TAPE];
  
  /* We single-thread */
  static struct semaphore reqSem;
  
  /*
   * When we send a request, we use this struct to get the response back
   * from the interrupt handler
   */
  struct op_struct {
  	void			*buffer;
  	dma_addr_t		dmaaddr;
  	size_t			count;
  	int			rc;
  	int			non_blocking;
  	struct completion	com;
  	struct device		*dev;
  	struct op_struct	*next;
  };
  
  static spinlock_t	op_struct_list_lock;
  static struct op_struct	*op_struct_list;
  
  /* forward declaration to resolve interdependence */
  static int chg_state(int index, unsigned char new_state, struct file *file);
  
  /* procfs support */
  static int proc_viotape_show(struct seq_file *m, void *v)
  {
  	int i;
  
  	seq_printf(m, "viotape driver version " VIOTAPE_VERSION "
  ");
  	for (i = 0; i < viotape_numdev; i++) {
  		seq_printf(m, "viotape device %d is iSeries resource %10.10s"
  				"type %4.4s, model %3.3s
  ",
  				i, viotape_unitinfo[i].rsrcname,
  				viotape_unitinfo[i].type,
  				viotape_unitinfo[i].model);
  	}
  	return 0;
  }
  
  static int proc_viotape_open(struct inode *inode, struct file *file)
  {
  	return single_open(file, proc_viotape_show, NULL);
  }
62322d255   Arjan van de Ven   [PATCH] make more...
252
  static const struct file_operations proc_viotape_operations = {
1b5022173   Denis V. Lunev   drivers: use non-...
253
  	.owner		= THIS_MODULE,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
  	.open		= proc_viotape_open,
  	.read		= seq_read,
  	.llseek		= seq_lseek,
  	.release	= single_release,
  };
  
  /* Decode the device minor number into its parts */
  void get_dev_info(struct inode *ino, struct viot_devinfo_struct *devi)
  {
  	devi->devno = iminor(ino) & 0x1F;
  	devi->mode = (iminor(ino) & 0x60) >> 5;
  	/* if bit is set in the minor, do _not_ rewind automatically */
  	devi->rewind = (iminor(ino) & 0x80) == 0;
  }
  
  /* This is called only from the exit and init paths, so no need for locking */
  static void clear_op_struct_pool(void)
  {
  	while (op_struct_list) {
  		struct op_struct *toFree = op_struct_list;
  		op_struct_list = op_struct_list->next;
  		kfree(toFree);
  	}
  }
  
  /* Likewise, this is only called from the init path */
  static int add_op_structs(int structs)
  {
  	int i;
  
  	for (i = 0; i < structs; ++i) {
  		struct op_struct *new_struct =
  			kmalloc(sizeof(*new_struct), GFP_KERNEL);
  		if (!new_struct) {
  			clear_op_struct_pool();
  			return -ENOMEM;
  		}
  		new_struct->next = op_struct_list;
  		op_struct_list = new_struct;
  	}
  	return 0;
  }
  
  /* Allocate an op structure from our pool */
  static struct op_struct *get_op_struct(void)
  {
  	struct op_struct *retval;
  	unsigned long flags;
  
  	spin_lock_irqsave(&op_struct_list_lock, flags);
  	retval = op_struct_list;
  	if (retval)
  		op_struct_list = retval->next;
  	spin_unlock_irqrestore(&op_struct_list_lock, flags);
  	if (retval) {
  		memset(retval, 0, sizeof(*retval));
  		init_completion(&retval->com);
  	}
  
  	return retval;
  }
  
  /* Return an op structure to our pool */
  static void free_op_struct(struct op_struct *op_struct)
  {
  	unsigned long flags;
  
  	spin_lock_irqsave(&op_struct_list_lock, flags);
  	op_struct->next = op_struct_list;
  	op_struct_list = op_struct;
  	spin_unlock_irqrestore(&op_struct_list_lock, flags);
  }
  
  /* Map our tape return codes to errno values */
  int tape_rc_to_errno(int tape_rc, char *operation, int tapeno)
  {
  	const struct vio_error_entry *err;
  
  	if (tape_rc == 0)
  		return 0;
  
  	err = vio_lookup_rc(viotape_err_table, tape_rc);
  	printk(VIOTAPE_KERN_WARN "error(%s) 0x%04x on Device %d (%-10s): %s
  ",
  			operation, tape_rc, tapeno,
  			viotape_unitinfo[tapeno].rsrcname, err->msg);
  	return -err->errno;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
342
343
344
345
346
347
348
349
350
351
352
353
354
  /* Write */
  static ssize_t viotap_write(struct file *file, const char *buf,
  		size_t count, loff_t * ppos)
  {
  	HvLpEvent_Rc hvrc;
  	unsigned short flags = file->f_flags;
  	int noblock = ((flags & O_NONBLOCK) != 0);
  	ssize_t ret;
  	struct viot_devinfo_struct devi;
  	struct op_struct *op = get_op_struct();
  
  	if (op == NULL)
  		return -ENOMEM;
a7113a966   Josef Sipek   [PATCH] struct pa...
355
  	get_dev_info(file->f_path.dentry->d_inode, &devi);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
  
  	/*
  	 * We need to make sure we can send a request.  We use
  	 * a semaphore to keep track of # requests in use.  If
  	 * we are non-blocking, make sure we don't block on the
  	 * semaphore
  	 */
  	if (noblock) {
  		if (down_trylock(&reqSem)) {
  			ret = -EWOULDBLOCK;
  			goto free_op;
  		}
  	} else
  		down(&reqSem);
  
  	/* Allocate a DMA buffer */
  	op->dev = tape_device[devi.devno];
  	op->buffer = dma_alloc_coherent(op->dev, count, &op->dmaaddr,
  			GFP_ATOMIC);
  
  	if (op->buffer == NULL) {
  		printk(VIOTAPE_KERN_WARN
  				"error allocating dma buffer for len %ld
  ",
  				count);
  		ret = -EFAULT;
  		goto up_sem;
  	}
  
  	/* Copy the data into the buffer */
  	if (copy_from_user(op->buffer, buf, count)) {
  		printk(VIOTAPE_KERN_WARN "tape: error on copy from user
  ");
  		ret = -EFAULT;
  		goto free_dma;
  	}
  
  	op->non_blocking = noblock;
  	init_completion(&op->com);
  	op->count = count;
  
  	hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
  			HvLpEvent_Type_VirtualIo,
  			viomajorsubtype_tape | viotapewrite,
  			HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck,
  			viopath_sourceinst(viopath_hostLp),
  			viopath_targetinst(viopath_hostLp),
  			(u64)(unsigned long)op, VIOVERSION << 16,
  			((u64)devi.devno << 48) | op->dmaaddr, count, 0, 0);
  	if (hvrc != HvLpEvent_Rc_Good) {
  		printk(VIOTAPE_KERN_WARN "hv error on op %d
  ",
  				(int)hvrc);
  		ret = -EIO;
  		goto free_dma;
  	}
  
  	if (noblock)
  		return count;
  
  	wait_for_completion(&op->com);
  
  	if (op->rc)
  		ret = tape_rc_to_errno(op->rc, "write", devi.devno);
  	else {
  		chg_state(devi.devno, VIOT_WRITING, file);
  		ret = op->count;
  	}
  
  free_dma:
  	dma_free_coherent(op->dev, count, op->buffer, op->dmaaddr);
  up_sem:
  	up(&reqSem);
  free_op:
  	free_op_struct(op);
  	return ret;
  }
  
  /* read */
  static ssize_t viotap_read(struct file *file, char *buf, size_t count,
  		loff_t *ptr)
  {
  	HvLpEvent_Rc hvrc;
  	unsigned short flags = file->f_flags;
  	struct op_struct *op = get_op_struct();
  	int noblock = ((flags & O_NONBLOCK) != 0);
  	ssize_t ret;
  	struct viot_devinfo_struct devi;
  
  	if (op == NULL)
  		return -ENOMEM;
a7113a966   Josef Sipek   [PATCH] struct pa...
447
  	get_dev_info(file->f_path.dentry->d_inode, &devi);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
  
  	/*
  	 * We need to make sure we can send a request.  We use
  	 * a semaphore to keep track of # requests in use.  If
  	 * we are non-blocking, make sure we don't block on the
  	 * semaphore
  	 */
  	if (noblock) {
  		if (down_trylock(&reqSem)) {
  			ret = -EWOULDBLOCK;
  			goto free_op;
  		}
  	} else
  		down(&reqSem);
  
  	chg_state(devi.devno, VIOT_READING, file);
  
  	/* Allocate a DMA buffer */
  	op->dev = tape_device[devi.devno];
  	op->buffer = dma_alloc_coherent(op->dev, count, &op->dmaaddr,
  			GFP_ATOMIC);
  	if (op->buffer == NULL) {
  		ret = -EFAULT;
  		goto up_sem;
  	}
  
  	op->count = count;
  	init_completion(&op->com);
  
  	hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
  			HvLpEvent_Type_VirtualIo,
  			viomajorsubtype_tape | viotaperead,
  			HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck,
  			viopath_sourceinst(viopath_hostLp),
  			viopath_targetinst(viopath_hostLp),
  			(u64)(unsigned long)op, VIOVERSION << 16,
  			((u64)devi.devno << 48) | op->dmaaddr, count, 0, 0);
  	if (hvrc != HvLpEvent_Rc_Good) {
  		printk(VIOTAPE_KERN_WARN "tape hv error on op %d
  ",
  				(int)hvrc);
  		ret = -EIO;
  		goto free_dma;
  	}
  
  	wait_for_completion(&op->com);
  
  	if (op->rc)
  		ret = tape_rc_to_errno(op->rc, "read", devi.devno);
  	else {
  		ret = op->count;
  		if (ret && copy_to_user(buf, op->buffer, ret)) {
  			printk(VIOTAPE_KERN_WARN "error on copy_to_user
  ");
  			ret = -EFAULT;
  		}
  	}
  
  free_dma:
  	dma_free_coherent(op->dev, count, op->buffer, op->dmaaddr);
  up_sem:
  	up(&reqSem);
  free_op:
  	free_op_struct(op);
  	return ret;
  }
  
  /* ioctl */
  static int viotap_ioctl(struct inode *inode, struct file *file,
  		unsigned int cmd, unsigned long arg)
  {
  	HvLpEvent_Rc hvrc;
  	int ret;
  	struct viot_devinfo_struct devi;
  	struct mtop mtc;
  	u32 myOp;
  	struct op_struct *op = get_op_struct();
  
  	if (op == NULL)
  		return -ENOMEM;
a7113a966   Josef Sipek   [PATCH] struct pa...
528
  	get_dev_info(file->f_path.dentry->d_inode, &devi);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
  
  	down(&reqSem);
  
  	ret = -EINVAL;
  
  	switch (cmd) {
  	case MTIOCTOP:
  		ret = -EFAULT;
  		/*
  		 * inode is null if and only if we (the kernel)
  		 * made the request
  		 */
  		if (inode == NULL)
  			memcpy(&mtc, (void *) arg, sizeof(struct mtop));
  		else if (copy_from_user((char *)&mtc, (char *)arg,
  					sizeof(struct mtop)))
  			goto free_op;
  
  		ret = -EIO;
  		switch (mtc.mt_op) {
  		case MTRESET:
  			myOp = VIOTAPOP_RESET;
  			break;
  		case MTFSF:
  			myOp = VIOTAPOP_FSF;
  			break;
  		case MTBSF:
  			myOp = VIOTAPOP_BSF;
  			break;
  		case MTFSR:
  			myOp = VIOTAPOP_FSR;
  			break;
  		case MTBSR:
  			myOp = VIOTAPOP_BSR;
  			break;
  		case MTWEOF:
  			myOp = VIOTAPOP_WEOF;
  			break;
  		case MTREW:
  			myOp = VIOTAPOP_REW;
  			break;
  		case MTNOP:
  			myOp = VIOTAPOP_NOP;
  			break;
  		case MTEOM:
  			myOp = VIOTAPOP_EOM;
  			break;
  		case MTERASE:
  			myOp = VIOTAPOP_ERASE;
  			break;
  		case MTSETBLK:
  			myOp = VIOTAPOP_SETBLK;
  			break;
  		case MTSETDENSITY:
  			myOp = VIOTAPOP_SETDENSITY;
  			break;
  		case MTTELL:
  			myOp = VIOTAPOP_GETPOS;
  			break;
  		case MTSEEK:
  			myOp = VIOTAPOP_SETPOS;
  			break;
  		case MTSETPART:
  			myOp = VIOTAPOP_SETPART;
  			break;
  		case MTOFFL:
  			myOp = VIOTAPOP_UNLOAD;
  			break;
  		default:
  			printk(VIOTAPE_KERN_WARN "MTIOCTOP called "
  					"with invalid op 0x%x
  ", mtc.mt_op);
  			goto free_op;
  		}
  
  		/*
  		 * if we moved the head, we are no longer
  		 * reading or writing
  		 */
  		switch (mtc.mt_op) {
  		case MTFSF:
  		case MTBSF:
  		case MTFSR:
  		case MTBSR:
  		case MTTELL:
  		case MTSEEK:
  		case MTREW:
  			chg_state(devi.devno, VIOT_IDLE, file);
  		}
  
  		init_completion(&op->com);
  		hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
  				HvLpEvent_Type_VirtualIo,
  				viomajorsubtype_tape | viotapeop,
  				HvLpEvent_AckInd_DoAck,
  				HvLpEvent_AckType_ImmediateAck,
  				viopath_sourceinst(viopath_hostLp),
  				viopath_targetinst(viopath_hostLp),
  				(u64)(unsigned long)op,
  				VIOVERSION << 16,
  				((u64)devi.devno << 48), 0,
  				(((u64)myOp) << 32) | mtc.mt_count, 0);
  		if (hvrc != HvLpEvent_Rc_Good) {
  			printk(VIOTAPE_KERN_WARN "hv error on op %d
  ",
  					(int)hvrc);
  			goto free_op;
  		}
  		wait_for_completion(&op->com);
  		ret = tape_rc_to_errno(op->rc, "tape operation", devi.devno);
  		goto free_op;
  
  	case MTIOCGET:
  		ret = -EIO;
  		init_completion(&op->com);
  		hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
  				HvLpEvent_Type_VirtualIo,
  				viomajorsubtype_tape | viotapegetstatus,
  				HvLpEvent_AckInd_DoAck,
  				HvLpEvent_AckType_ImmediateAck,
  				viopath_sourceinst(viopath_hostLp),
  				viopath_targetinst(viopath_hostLp),
  				(u64)(unsigned long)op, VIOVERSION << 16,
  				((u64)devi.devno << 48), 0, 0, 0);
  		if (hvrc != HvLpEvent_Rc_Good) {
  			printk(VIOTAPE_KERN_WARN "hv error on op %d
  ",
  					(int)hvrc);
  			goto free_op;
  		}
  		wait_for_completion(&op->com);
  
  		/* Operation is complete - grab the error code */
  		ret = tape_rc_to_errno(op->rc, "get status", devi.devno);
  		free_op_struct(op);
  		up(&reqSem);
  
  		if ((ret == 0) && copy_to_user((void *)arg,
  					&viomtget[devi.devno],
  					sizeof(viomtget[0])))
  			ret = -EFAULT;
  		return ret;
  	case MTIOCPOS:
  		printk(VIOTAPE_KERN_WARN "Got an (unsupported) MTIOCPOS
  ");
  		break;
  	default:
  		printk(VIOTAPE_KERN_WARN "got an unsupported ioctl 0x%0x
  ",
  				cmd);
  		break;
  	}
  
  free_op:
  	free_op_struct(op);
  	up(&reqSem);
  	return ret;
  }
10c0ad4dd   Stephen Rothwell   viotape: Use unlo...
687
688
689
690
  static long viotap_unlocked_ioctl(struct file *file,
  		unsigned int cmd, unsigned long arg)
  {
  	long rc;
613655fa3   Arnd Bergmann   drivers: autoconv...
691
  	mutex_lock(&proc_viotape_mutex);
10c0ad4dd   Stephen Rothwell   viotape: Use unlo...
692
  	rc = viotap_ioctl(file->f_path.dentry->d_inode, file, cmd, arg);
613655fa3   Arnd Bergmann   drivers: autoconv...
693
  	mutex_unlock(&proc_viotape_mutex);
10c0ad4dd   Stephen Rothwell   viotape: Use unlo...
694
695
  	return rc;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
696
697
698
699
700
701
702
703
704
  static int viotap_open(struct inode *inode, struct file *file)
  {
  	HvLpEvent_Rc hvrc;
  	struct viot_devinfo_struct devi;
  	int ret;
  	struct op_struct *op = get_op_struct();
  
  	if (op == NULL)
  		return -ENOMEM;
613655fa3   Arnd Bergmann   drivers: autoconv...
705
  	mutex_lock(&proc_viotape_mutex);
a7113a966   Josef Sipek   [PATCH] struct pa...
706
  	get_dev_info(file->f_path.dentry->d_inode, &devi);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
  
  	/* Note: We currently only support one mode! */
  	if ((devi.devno >= viotape_numdev) || (devi.mode)) {
  		ret = -ENODEV;
  		goto free_op;
  	}
  
  	init_completion(&op->com);
  
  	hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
  			HvLpEvent_Type_VirtualIo,
  			viomajorsubtype_tape | viotapeopen,
  			HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck,
  			viopath_sourceinst(viopath_hostLp),
  			viopath_targetinst(viopath_hostLp),
  			(u64)(unsigned long)op, VIOVERSION << 16,
  			((u64)devi.devno << 48), 0, 0, 0);
  	if (hvrc != 0) {
  		printk(VIOTAPE_KERN_WARN "bad rc on signalLpEvent %d
  ",
  				(int) hvrc);
  		ret = -EIO;
  		goto free_op;
  	}
  
  	wait_for_completion(&op->com);
  	ret = tape_rc_to_errno(op->rc, "open", devi.devno);
  
  free_op:
  	free_op_struct(op);
613655fa3   Arnd Bergmann   drivers: autoconv...
737
  	mutex_unlock(&proc_viotape_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
738
739
740
741
742
743
744
745
746
747
748
749
750
751
  	return ret;
  }
  
  
  static int viotap_release(struct inode *inode, struct file *file)
  {
  	HvLpEvent_Rc hvrc;
  	struct viot_devinfo_struct devi;
  	int ret = 0;
  	struct op_struct *op = get_op_struct();
  
  	if (op == NULL)
  		return -ENOMEM;
  	init_completion(&op->com);
a7113a966   Josef Sipek   [PATCH] struct pa...
752
  	get_dev_info(file->f_path.dentry->d_inode, &devi);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
  
  	if (devi.devno >= viotape_numdev) {
  		ret = -ENODEV;
  		goto free_op;
  	}
  
  	chg_state(devi.devno, VIOT_IDLE, file);
  
  	if (devi.rewind) {
  		hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
  				HvLpEvent_Type_VirtualIo,
  				viomajorsubtype_tape | viotapeop,
  				HvLpEvent_AckInd_DoAck,
  				HvLpEvent_AckType_ImmediateAck,
  				viopath_sourceinst(viopath_hostLp),
  				viopath_targetinst(viopath_hostLp),
  				(u64)(unsigned long)op, VIOVERSION << 16,
  				((u64)devi.devno << 48), 0,
  				((u64)VIOTAPOP_REW) << 32, 0);
  		wait_for_completion(&op->com);
  
  		tape_rc_to_errno(op->rc, "rewind", devi.devno);
  	}
  
  	hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
  			HvLpEvent_Type_VirtualIo,
  			viomajorsubtype_tape | viotapeclose,
  			HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck,
  			viopath_sourceinst(viopath_hostLp),
  			viopath_targetinst(viopath_hostLp),
  			(u64)(unsigned long)op, VIOVERSION << 16,
  			((u64)devi.devno << 48), 0, 0, 0);
  	if (hvrc != 0) {
  		printk(VIOTAPE_KERN_WARN "bad rc on signalLpEvent %d
  ",
  				(int) hvrc);
  		ret = -EIO;
  		goto free_op;
  	}
  
  	wait_for_completion(&op->com);
  
  	if (op->rc)
  		printk(VIOTAPE_KERN_WARN "close failed
  ");
  
  free_op:
  	free_op_struct(op);
  	return ret;
  }
2b8693c06   Arjan van de Ven   [PATCH] mark stru...
803
  const struct file_operations viotap_fops = {
10c0ad4dd   Stephen Rothwell   viotape: Use unlo...
804
805
806
807
808
809
  	.owner =		THIS_MODULE,
  	.read =			viotap_read,
  	.write =		viotap_write,
  	.unlocked_ioctl =	viotap_unlocked_ioctl,
  	.open =			viotap_open,
  	.release =		viotap_release,
aadbd4360   Arnd Bergmann   viotape: use noop...
810
  	.llseek = 		noop_llseek,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
  };
  
  /* Handle interrupt events for tape */
  static void vioHandleTapeEvent(struct HvLpEvent *event)
  {
  	int tapeminor;
  	struct op_struct *op;
  	struct viotapelpevent *tevent = (struct viotapelpevent *)event;
  
  	if (event == NULL) {
  		/* Notification that a partition went away! */
  		if (!viopath_isactive(viopath_hostLp)) {
  			/* TODO! Clean up */
  		}
  		return;
  	}
  
  	tapeminor = event->xSubtype & VIOMINOR_SUBTYPE_MASK;
  	op = (struct op_struct *)event->xCorrelationToken;
  	switch (tapeminor) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
  	case viotapeopen:
  	case viotapeclose:
  		op->rc = tevent->sub_type_result;
  		complete(&op->com);
  		break;
  	case viotaperead:
  		op->rc = tevent->sub_type_result;
  		op->count = tevent->len;
  		complete(&op->com);
  		break;
  	case viotapewrite:
  		if (op->non_blocking) {
  			dma_free_coherent(op->dev, op->count,
  					op->buffer, op->dmaaddr);
  			free_op_struct(op);
  			up(&reqSem);
  		} else {
  			op->rc = tevent->sub_type_result;
  			op->count = tevent->len;
  			complete(&op->com);
  		}
  		break;
  	case viotapeop:
  	case viotapegetpos:
  	case viotapesetpos:
  	case viotapegetstatus:
  		if (op) {
  			op->count = tevent->u.op.count;
  			op->rc = tevent->sub_type_result;
  			if (!op->non_blocking)
  				complete(&op->com);
  		}
  		break;
  	default:
  		printk(VIOTAPE_KERN_WARN "weird ack
  ");
  	}
  }
  
  static int viotape_probe(struct vio_dev *vdev, const struct vio_device_id *id)
  {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
872
873
  	int i = vdev->unit_address;
  	int j;
61c7a080a   Grant Likely   of: Always use 's...
874
  	struct device_node *node = vdev->dev.of_node;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
875

a85c8e175   roel kluin   tape: beyond ARRA...
876
  	if (i >= VIOTAPE_MAX_TAPE)
7465ce0db   Stephen Rothwell   [POWERPC] iSeries...
877
878
  		return -ENODEV;
  	if (!node)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
879
  		return -ENODEV;
7465ce0db   Stephen Rothwell   [POWERPC] iSeries...
880
881
  	if (i >= viotape_numdev)
  		viotape_numdev = i + 1;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
882
  	tape_device[i] = &vdev->dev;
7465ce0db   Stephen Rothwell   [POWERPC] iSeries...
883
884
885
886
887
888
  	viotape_unitinfo[i].rsrcname = of_get_property(node,
  					"linux,vio_rsrcname", NULL);
  	viotape_unitinfo[i].type = of_get_property(node, "linux,vio_type",
  					NULL);
  	viotape_unitinfo[i].model = of_get_property(node, "linux,vio_model",
  					NULL);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
889
890
891
892
  
  	state[i].cur_part = 0;
  	for (j = 0; j < MAX_PARTITIONS; ++j)
  		state[i].part_stat_rwi[j] = VIOT_IDLE;
03457cd45   Greg Kroah-Hartman   device create: ch...
893
894
895
896
  	device_create(tape_class, NULL, MKDEV(VIOTAPE_MAJOR, i), NULL,
  		      "iseries!vt%d", i);
  	device_create(tape_class, NULL, MKDEV(VIOTAPE_MAJOR, i | 0x80), NULL,
  		      "iseries!nvt%d", i);
380ed24b1   Stephen Rothwell   [POWERPC] iseries...
897
  	printk(VIOTAPE_KERN_INFO "tape iseries/vt%d is iSeries "
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
898
899
  			"resource %10.10s type %4.4s, model %3.3s
  ",
380ed24b1   Stephen Rothwell   [POWERPC] iseries...
900
  			i, viotape_unitinfo[i].rsrcname,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
901
902
903
904
905
906
907
  			viotape_unitinfo[i].type, viotape_unitinfo[i].model);
  	return 0;
  }
  
  static int viotape_remove(struct vio_dev *vdev)
  {
  	int i = vdev->unit_address;
07c015e76   tonyj@suse.de   Convert from clas...
908
909
  	device_destroy(tape_class, MKDEV(VIOTAPE_MAJOR, i | 0x80));
  	device_destroy(tape_class, MKDEV(VIOTAPE_MAJOR, i));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
910
911
912
913
914
915
916
917
  	return 0;
  }
  
  /**
   * viotape_device_table: Used by vio.c to match devices that we
   * support.
   */
  static struct vio_device_id viotape_device_table[] __devinitdata = {
de0fe3b83   Stephen Rothwell   [PATCH] powerpc: ...
918
  	{ "byte", "IBM,iSeries-viotape" },
fb120da67   Stephen Rothwell   [PATCH] Make MODU...
919
  	{ "", "" }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
920
  };
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
921
  MODULE_DEVICE_TABLE(vio, viotape_device_table);
915124d81   Stephen Rothwell   powerpc: set the ...
922

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
923
  static struct vio_driver viotape_driver = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
924
925
  	.id_table = viotape_device_table,
  	.probe = viotape_probe,
6fdf5392c   Stephen Rothwell   powerpc: don't du...
926
927
928
  	.remove = viotape_remove,
  	.driver = {
  		.name = "viotape",
915124d81   Stephen Rothwell   powerpc: set the ...
929
  		.owner = THIS_MODULE,
6fdf5392c   Stephen Rothwell   powerpc: don't du...
930
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
931
932
933
934
935
936
  };
  
  
  int __init viotap_init(void)
  {
  	int ret;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
937

fd38451f1   Stephen Rothwell   [POWERPC] iSeries...
938
939
  	if (!firmware_has_feature(FW_FEATURE_ISERIES))
  		return -ENODEV;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
  	op_struct_list = NULL;
  	if ((ret = add_op_structs(VIOTAPE_MAXREQ)) < 0) {
  		printk(VIOTAPE_KERN_WARN "couldn't allocate op structs
  ");
  		return ret;
  	}
  	spin_lock_init(&op_struct_list_lock);
  
  	sema_init(&reqSem, VIOTAPE_MAXREQ);
  
  	if (viopath_hostLp == HvLpIndexInvalid) {
  		vio_set_hostlp();
  		if (viopath_hostLp == HvLpIndexInvalid) {
  			ret = -ENODEV;
  			goto clear_op;
  		}
  	}
  
  	ret = viopath_open(viopath_hostLp, viomajorsubtype_tape,
  			VIOTAPE_MAXREQ + 2);
  	if (ret) {
  		printk(VIOTAPE_KERN_WARN
  				"error on viopath_open to hostlp %d
  ", ret);
  		ret = -EIO;
  		goto clear_op;
  	}
  
  	printk(VIOTAPE_KERN_INFO "vers " VIOTAPE_VERSION
  			", hosting partition %d
  ", viopath_hostLp);
  
  	vio_setHandler(viomajorsubtype_tape, vioHandleTapeEvent);
  
  	ret = register_chrdev(VIOTAPE_MAJOR, "viotape", &viotap_fops);
  	if (ret < 0) {
  		printk(VIOTAPE_KERN_WARN "Error registering viotape device
  ");
  		goto clear_handler;
  	}
ca8eca688   Greg Kroah-Hartman   [PATCH] class: co...
980
  	tape_class = class_create(THIS_MODULE, "tape");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
981
982
983
984
985
986
  	if (IS_ERR(tape_class)) {
  		printk(VIOTAPE_KERN_WARN "Unable to allocat class
  ");
  		ret = PTR_ERR(tape_class);
  		goto unreg_chrdev;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
987
988
989
  	ret = vio_register_driver(&viotape_driver);
  	if (ret)
  		goto unreg_class;
1b5022173   Denis V. Lunev   drivers: use non-...
990
991
  	proc_create("iSeries/viotape", S_IFREG|S_IRUGO, NULL,
  		    &proc_viotape_operations);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
992
993
994
995
  
  	return 0;
  
  unreg_class:
ca8eca688   Greg Kroah-Hartman   [PATCH] class: co...
996
  	class_destroy(tape_class);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
  unreg_chrdev:
  	unregister_chrdev(VIOTAPE_MAJOR, "viotape");
  clear_handler:
  	vio_clearHandler(viomajorsubtype_tape);
  	viopath_close(viopath_hostLp, viomajorsubtype_tape, VIOTAPE_MAXREQ + 2);
  clear_op:
  	clear_op_struct_pool();
  	return ret;
  }
  
  /* Give a new state to the tape object */
  static int chg_state(int index, unsigned char new_state, struct file *file)
  {
  	unsigned char *cur_state =
  	    &state[index].part_stat_rwi[state[index].cur_part];
  	int rc = 0;
  
  	/* if the same state, don't bother */
  	if (*cur_state == new_state)
  		return 0;
  
  	/* write an EOF if changing from writing to some other state */
  	if (*cur_state == VIOT_WRITING) {
  		struct mtop write_eof = { MTWEOF, 1 };
  
  		rc = viotap_ioctl(NULL, file, MTIOCTOP,
  				  (unsigned long)&write_eof);
  	}
  	*cur_state = new_state;
  	return rc;
  }
  
  /* Cleanup */
  static void __exit viotap_exit(void)
  {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1032
1033
  	remove_proc_entry("iSeries/viotape", NULL);
  	vio_unregister_driver(&viotape_driver);
ca8eca688   Greg Kroah-Hartman   [PATCH] class: co...
1034
  	class_destroy(tape_class);
68fc4fabc   Akinobu Mita   unregister_chrdev...
1035
  	unregister_chrdev(VIOTAPE_MAJOR, "viotape");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1036
1037
1038
1039
1040
1041
1042
1043
  	viopath_close(viopath_hostLp, viomajorsubtype_tape, VIOTAPE_MAXREQ + 2);
  	vio_clearHandler(viomajorsubtype_tape);
  	clear_op_struct_pool();
  }
  
  MODULE_LICENSE("GPL");
  module_init(viotap_init);
  module_exit(viotap_exit);