viotape.c 26 KB

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