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