viotape.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024
  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. .owner = THIS_MODULE,
  229. .open = proc_viotape_open,
  230. .read = seq_read,
  231. .llseek = seq_lseek,
  232. .release = single_release,
  233. };
  234. /* Decode the device minor number into its parts */
  235. void get_dev_info(struct inode *ino, struct viot_devinfo_struct *devi)
  236. {
  237. devi->devno = iminor(ino) & 0x1F;
  238. devi->mode = (iminor(ino) & 0x60) >> 5;
  239. /* if bit is set in the minor, do _not_ rewind automatically */
  240. devi->rewind = (iminor(ino) & 0x80) == 0;
  241. }
  242. /* This is called only from the exit and init paths, so no need for locking */
  243. static void clear_op_struct_pool(void)
  244. {
  245. while (op_struct_list) {
  246. struct op_struct *toFree = op_struct_list;
  247. op_struct_list = op_struct_list->next;
  248. kfree(toFree);
  249. }
  250. }
  251. /* Likewise, this is only called from the init path */
  252. static int add_op_structs(int structs)
  253. {
  254. int i;
  255. for (i = 0; i < structs; ++i) {
  256. struct op_struct *new_struct =
  257. kmalloc(sizeof(*new_struct), GFP_KERNEL);
  258. if (!new_struct) {
  259. clear_op_struct_pool();
  260. return -ENOMEM;
  261. }
  262. new_struct->next = op_struct_list;
  263. op_struct_list = new_struct;
  264. }
  265. return 0;
  266. }
  267. /* Allocate an op structure from our pool */
  268. static struct op_struct *get_op_struct(void)
  269. {
  270. struct op_struct *retval;
  271. unsigned long flags;
  272. spin_lock_irqsave(&op_struct_list_lock, flags);
  273. retval = op_struct_list;
  274. if (retval)
  275. op_struct_list = retval->next;
  276. spin_unlock_irqrestore(&op_struct_list_lock, flags);
  277. if (retval) {
  278. memset(retval, 0, sizeof(*retval));
  279. init_completion(&retval->com);
  280. }
  281. return retval;
  282. }
  283. /* Return an op structure to our pool */
  284. static void free_op_struct(struct op_struct *op_struct)
  285. {
  286. unsigned long flags;
  287. spin_lock_irqsave(&op_struct_list_lock, flags);
  288. op_struct->next = op_struct_list;
  289. op_struct_list = op_struct;
  290. spin_unlock_irqrestore(&op_struct_list_lock, flags);
  291. }
  292. /* Map our tape return codes to errno values */
  293. int tape_rc_to_errno(int tape_rc, char *operation, int tapeno)
  294. {
  295. const struct vio_error_entry *err;
  296. if (tape_rc == 0)
  297. return 0;
  298. err = vio_lookup_rc(viotape_err_table, tape_rc);
  299. printk(VIOTAPE_KERN_WARN "error(%s) 0x%04x on Device %d (%-10s): %s\n",
  300. operation, tape_rc, tapeno,
  301. viotape_unitinfo[tapeno].rsrcname, err->msg);
  302. return -err->errno;
  303. }
  304. /* Write */
  305. static ssize_t viotap_write(struct file *file, const char *buf,
  306. size_t count, loff_t * ppos)
  307. {
  308. HvLpEvent_Rc hvrc;
  309. unsigned short flags = file->f_flags;
  310. int noblock = ((flags & O_NONBLOCK) != 0);
  311. ssize_t ret;
  312. struct viot_devinfo_struct devi;
  313. struct op_struct *op = get_op_struct();
  314. if (op == NULL)
  315. return -ENOMEM;
  316. get_dev_info(file->f_path.dentry->d_inode, &devi);
  317. /*
  318. * We need to make sure we can send a request. We use
  319. * a semaphore to keep track of # requests in use. If
  320. * we are non-blocking, make sure we don't block on the
  321. * semaphore
  322. */
  323. if (noblock) {
  324. if (down_trylock(&reqSem)) {
  325. ret = -EWOULDBLOCK;
  326. goto free_op;
  327. }
  328. } else
  329. down(&reqSem);
  330. /* Allocate a DMA buffer */
  331. op->dev = tape_device[devi.devno];
  332. op->buffer = dma_alloc_coherent(op->dev, count, &op->dmaaddr,
  333. GFP_ATOMIC);
  334. if (op->buffer == NULL) {
  335. printk(VIOTAPE_KERN_WARN
  336. "error allocating dma buffer for len %ld\n",
  337. count);
  338. ret = -EFAULT;
  339. goto up_sem;
  340. }
  341. /* Copy the data into the buffer */
  342. if (copy_from_user(op->buffer, buf, count)) {
  343. printk(VIOTAPE_KERN_WARN "tape: error on copy from user\n");
  344. ret = -EFAULT;
  345. goto free_dma;
  346. }
  347. op->non_blocking = noblock;
  348. init_completion(&op->com);
  349. op->count = count;
  350. hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
  351. HvLpEvent_Type_VirtualIo,
  352. viomajorsubtype_tape | viotapewrite,
  353. HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck,
  354. viopath_sourceinst(viopath_hostLp),
  355. viopath_targetinst(viopath_hostLp),
  356. (u64)(unsigned long)op, VIOVERSION << 16,
  357. ((u64)devi.devno << 48) | op->dmaaddr, count, 0, 0);
  358. if (hvrc != HvLpEvent_Rc_Good) {
  359. printk(VIOTAPE_KERN_WARN "hv error on op %d\n",
  360. (int)hvrc);
  361. ret = -EIO;
  362. goto free_dma;
  363. }
  364. if (noblock)
  365. return count;
  366. wait_for_completion(&op->com);
  367. if (op->rc)
  368. ret = tape_rc_to_errno(op->rc, "write", devi.devno);
  369. else {
  370. chg_state(devi.devno, VIOT_WRITING, file);
  371. ret = op->count;
  372. }
  373. free_dma:
  374. dma_free_coherent(op->dev, count, op->buffer, op->dmaaddr);
  375. up_sem:
  376. up(&reqSem);
  377. free_op:
  378. free_op_struct(op);
  379. return ret;
  380. }
  381. /* read */
  382. static ssize_t viotap_read(struct file *file, char *buf, size_t count,
  383. loff_t *ptr)
  384. {
  385. HvLpEvent_Rc hvrc;
  386. unsigned short flags = file->f_flags;
  387. struct op_struct *op = get_op_struct();
  388. int noblock = ((flags & O_NONBLOCK) != 0);
  389. ssize_t ret;
  390. struct viot_devinfo_struct devi;
  391. if (op == NULL)
  392. return -ENOMEM;
  393. get_dev_info(file->f_path.dentry->d_inode, &devi);
  394. /*
  395. * We need to make sure we can send a request. We use
  396. * a semaphore to keep track of # requests in use. If
  397. * we are non-blocking, make sure we don't block on the
  398. * semaphore
  399. */
  400. if (noblock) {
  401. if (down_trylock(&reqSem)) {
  402. ret = -EWOULDBLOCK;
  403. goto free_op;
  404. }
  405. } else
  406. down(&reqSem);
  407. chg_state(devi.devno, VIOT_READING, file);
  408. /* Allocate a DMA buffer */
  409. op->dev = tape_device[devi.devno];
  410. op->buffer = dma_alloc_coherent(op->dev, count, &op->dmaaddr,
  411. GFP_ATOMIC);
  412. if (op->buffer == NULL) {
  413. ret = -EFAULT;
  414. goto up_sem;
  415. }
  416. op->count = count;
  417. init_completion(&op->com);
  418. hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
  419. HvLpEvent_Type_VirtualIo,
  420. viomajorsubtype_tape | viotaperead,
  421. HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck,
  422. viopath_sourceinst(viopath_hostLp),
  423. viopath_targetinst(viopath_hostLp),
  424. (u64)(unsigned long)op, VIOVERSION << 16,
  425. ((u64)devi.devno << 48) | op->dmaaddr, count, 0, 0);
  426. if (hvrc != HvLpEvent_Rc_Good) {
  427. printk(VIOTAPE_KERN_WARN "tape hv error on op %d\n",
  428. (int)hvrc);
  429. ret = -EIO;
  430. goto free_dma;
  431. }
  432. wait_for_completion(&op->com);
  433. if (op->rc)
  434. ret = tape_rc_to_errno(op->rc, "read", devi.devno);
  435. else {
  436. ret = op->count;
  437. if (ret && copy_to_user(buf, op->buffer, ret)) {
  438. printk(VIOTAPE_KERN_WARN "error on copy_to_user\n");
  439. ret = -EFAULT;
  440. }
  441. }
  442. free_dma:
  443. dma_free_coherent(op->dev, count, op->buffer, op->dmaaddr);
  444. up_sem:
  445. up(&reqSem);
  446. free_op:
  447. free_op_struct(op);
  448. return ret;
  449. }
  450. /* ioctl */
  451. static int viotap_ioctl(struct inode *inode, struct file *file,
  452. unsigned int cmd, unsigned long arg)
  453. {
  454. HvLpEvent_Rc hvrc;
  455. int ret;
  456. struct viot_devinfo_struct devi;
  457. struct mtop mtc;
  458. u32 myOp;
  459. struct op_struct *op = get_op_struct();
  460. if (op == NULL)
  461. return -ENOMEM;
  462. get_dev_info(file->f_path.dentry->d_inode, &devi);
  463. down(&reqSem);
  464. ret = -EINVAL;
  465. switch (cmd) {
  466. case MTIOCTOP:
  467. ret = -EFAULT;
  468. /*
  469. * inode is null if and only if we (the kernel)
  470. * made the request
  471. */
  472. if (inode == NULL)
  473. memcpy(&mtc, (void *) arg, sizeof(struct mtop));
  474. else if (copy_from_user((char *)&mtc, (char *)arg,
  475. sizeof(struct mtop)))
  476. goto free_op;
  477. ret = -EIO;
  478. switch (mtc.mt_op) {
  479. case MTRESET:
  480. myOp = VIOTAPOP_RESET;
  481. break;
  482. case MTFSF:
  483. myOp = VIOTAPOP_FSF;
  484. break;
  485. case MTBSF:
  486. myOp = VIOTAPOP_BSF;
  487. break;
  488. case MTFSR:
  489. myOp = VIOTAPOP_FSR;
  490. break;
  491. case MTBSR:
  492. myOp = VIOTAPOP_BSR;
  493. break;
  494. case MTWEOF:
  495. myOp = VIOTAPOP_WEOF;
  496. break;
  497. case MTREW:
  498. myOp = VIOTAPOP_REW;
  499. break;
  500. case MTNOP:
  501. myOp = VIOTAPOP_NOP;
  502. break;
  503. case MTEOM:
  504. myOp = VIOTAPOP_EOM;
  505. break;
  506. case MTERASE:
  507. myOp = VIOTAPOP_ERASE;
  508. break;
  509. case MTSETBLK:
  510. myOp = VIOTAPOP_SETBLK;
  511. break;
  512. case MTSETDENSITY:
  513. myOp = VIOTAPOP_SETDENSITY;
  514. break;
  515. case MTTELL:
  516. myOp = VIOTAPOP_GETPOS;
  517. break;
  518. case MTSEEK:
  519. myOp = VIOTAPOP_SETPOS;
  520. break;
  521. case MTSETPART:
  522. myOp = VIOTAPOP_SETPART;
  523. break;
  524. case MTOFFL:
  525. myOp = VIOTAPOP_UNLOAD;
  526. break;
  527. default:
  528. printk(VIOTAPE_KERN_WARN "MTIOCTOP called "
  529. "with invalid op 0x%x\n", mtc.mt_op);
  530. goto free_op;
  531. }
  532. /*
  533. * if we moved the head, we are no longer
  534. * reading or writing
  535. */
  536. switch (mtc.mt_op) {
  537. case MTFSF:
  538. case MTBSF:
  539. case MTFSR:
  540. case MTBSR:
  541. case MTTELL:
  542. case MTSEEK:
  543. case MTREW:
  544. chg_state(devi.devno, VIOT_IDLE, file);
  545. }
  546. init_completion(&op->com);
  547. hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
  548. HvLpEvent_Type_VirtualIo,
  549. viomajorsubtype_tape | viotapeop,
  550. HvLpEvent_AckInd_DoAck,
  551. HvLpEvent_AckType_ImmediateAck,
  552. viopath_sourceinst(viopath_hostLp),
  553. viopath_targetinst(viopath_hostLp),
  554. (u64)(unsigned long)op,
  555. VIOVERSION << 16,
  556. ((u64)devi.devno << 48), 0,
  557. (((u64)myOp) << 32) | mtc.mt_count, 0);
  558. if (hvrc != HvLpEvent_Rc_Good) {
  559. printk(VIOTAPE_KERN_WARN "hv error on op %d\n",
  560. (int)hvrc);
  561. goto free_op;
  562. }
  563. wait_for_completion(&op->com);
  564. ret = tape_rc_to_errno(op->rc, "tape operation", devi.devno);
  565. goto free_op;
  566. case MTIOCGET:
  567. ret = -EIO;
  568. init_completion(&op->com);
  569. hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
  570. HvLpEvent_Type_VirtualIo,
  571. viomajorsubtype_tape | viotapegetstatus,
  572. HvLpEvent_AckInd_DoAck,
  573. HvLpEvent_AckType_ImmediateAck,
  574. viopath_sourceinst(viopath_hostLp),
  575. viopath_targetinst(viopath_hostLp),
  576. (u64)(unsigned long)op, VIOVERSION << 16,
  577. ((u64)devi.devno << 48), 0, 0, 0);
  578. if (hvrc != HvLpEvent_Rc_Good) {
  579. printk(VIOTAPE_KERN_WARN "hv error on op %d\n",
  580. (int)hvrc);
  581. goto free_op;
  582. }
  583. wait_for_completion(&op->com);
  584. /* Operation is complete - grab the error code */
  585. ret = tape_rc_to_errno(op->rc, "get status", devi.devno);
  586. free_op_struct(op);
  587. up(&reqSem);
  588. if ((ret == 0) && copy_to_user((void *)arg,
  589. &viomtget[devi.devno],
  590. sizeof(viomtget[0])))
  591. ret = -EFAULT;
  592. return ret;
  593. case MTIOCPOS:
  594. printk(VIOTAPE_KERN_WARN "Got an (unsupported) MTIOCPOS\n");
  595. break;
  596. default:
  597. printk(VIOTAPE_KERN_WARN "got an unsupported ioctl 0x%0x\n",
  598. cmd);
  599. break;
  600. }
  601. free_op:
  602. free_op_struct(op);
  603. up(&reqSem);
  604. return ret;
  605. }
  606. static int viotap_open(struct inode *inode, struct file *file)
  607. {
  608. HvLpEvent_Rc hvrc;
  609. struct viot_devinfo_struct devi;
  610. int ret;
  611. struct op_struct *op = get_op_struct();
  612. if (op == NULL)
  613. return -ENOMEM;
  614. get_dev_info(file->f_path.dentry->d_inode, &devi);
  615. /* Note: We currently only support one mode! */
  616. if ((devi.devno >= viotape_numdev) || (devi.mode)) {
  617. ret = -ENODEV;
  618. goto free_op;
  619. }
  620. init_completion(&op->com);
  621. hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
  622. HvLpEvent_Type_VirtualIo,
  623. viomajorsubtype_tape | viotapeopen,
  624. HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck,
  625. viopath_sourceinst(viopath_hostLp),
  626. viopath_targetinst(viopath_hostLp),
  627. (u64)(unsigned long)op, VIOVERSION << 16,
  628. ((u64)devi.devno << 48), 0, 0, 0);
  629. if (hvrc != 0) {
  630. printk(VIOTAPE_KERN_WARN "bad rc on signalLpEvent %d\n",
  631. (int) hvrc);
  632. ret = -EIO;
  633. goto free_op;
  634. }
  635. wait_for_completion(&op->com);
  636. ret = tape_rc_to_errno(op->rc, "open", devi.devno);
  637. free_op:
  638. free_op_struct(op);
  639. return ret;
  640. }
  641. static int viotap_release(struct inode *inode, struct file *file)
  642. {
  643. HvLpEvent_Rc hvrc;
  644. struct viot_devinfo_struct devi;
  645. int ret = 0;
  646. struct op_struct *op = get_op_struct();
  647. if (op == NULL)
  648. return -ENOMEM;
  649. init_completion(&op->com);
  650. get_dev_info(file->f_path.dentry->d_inode, &devi);
  651. if (devi.devno >= viotape_numdev) {
  652. ret = -ENODEV;
  653. goto free_op;
  654. }
  655. chg_state(devi.devno, VIOT_IDLE, file);
  656. if (devi.rewind) {
  657. hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
  658. HvLpEvent_Type_VirtualIo,
  659. viomajorsubtype_tape | viotapeop,
  660. HvLpEvent_AckInd_DoAck,
  661. HvLpEvent_AckType_ImmediateAck,
  662. viopath_sourceinst(viopath_hostLp),
  663. viopath_targetinst(viopath_hostLp),
  664. (u64)(unsigned long)op, VIOVERSION << 16,
  665. ((u64)devi.devno << 48), 0,
  666. ((u64)VIOTAPOP_REW) << 32, 0);
  667. wait_for_completion(&op->com);
  668. tape_rc_to_errno(op->rc, "rewind", devi.devno);
  669. }
  670. hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
  671. HvLpEvent_Type_VirtualIo,
  672. viomajorsubtype_tape | viotapeclose,
  673. HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck,
  674. viopath_sourceinst(viopath_hostLp),
  675. viopath_targetinst(viopath_hostLp),
  676. (u64)(unsigned long)op, VIOVERSION << 16,
  677. ((u64)devi.devno << 48), 0, 0, 0);
  678. if (hvrc != 0) {
  679. printk(VIOTAPE_KERN_WARN "bad rc on signalLpEvent %d\n",
  680. (int) hvrc);
  681. ret = -EIO;
  682. goto free_op;
  683. }
  684. wait_for_completion(&op->com);
  685. if (op->rc)
  686. printk(VIOTAPE_KERN_WARN "close failed\n");
  687. free_op:
  688. free_op_struct(op);
  689. return ret;
  690. }
  691. const struct file_operations viotap_fops = {
  692. .owner = THIS_MODULE,
  693. .read = viotap_read,
  694. .write = viotap_write,
  695. .ioctl = viotap_ioctl,
  696. .open = viotap_open,
  697. .release = viotap_release,
  698. };
  699. /* Handle interrupt events for tape */
  700. static void vioHandleTapeEvent(struct HvLpEvent *event)
  701. {
  702. int tapeminor;
  703. struct op_struct *op;
  704. struct viotapelpevent *tevent = (struct viotapelpevent *)event;
  705. if (event == NULL) {
  706. /* Notification that a partition went away! */
  707. if (!viopath_isactive(viopath_hostLp)) {
  708. /* TODO! Clean up */
  709. }
  710. return;
  711. }
  712. tapeminor = event->xSubtype & VIOMINOR_SUBTYPE_MASK;
  713. op = (struct op_struct *)event->xCorrelationToken;
  714. switch (tapeminor) {
  715. case viotapeopen:
  716. case viotapeclose:
  717. op->rc = tevent->sub_type_result;
  718. complete(&op->com);
  719. break;
  720. case viotaperead:
  721. op->rc = tevent->sub_type_result;
  722. op->count = tevent->len;
  723. complete(&op->com);
  724. break;
  725. case viotapewrite:
  726. if (op->non_blocking) {
  727. dma_free_coherent(op->dev, op->count,
  728. op->buffer, op->dmaaddr);
  729. free_op_struct(op);
  730. up(&reqSem);
  731. } else {
  732. op->rc = tevent->sub_type_result;
  733. op->count = tevent->len;
  734. complete(&op->com);
  735. }
  736. break;
  737. case viotapeop:
  738. case viotapegetpos:
  739. case viotapesetpos:
  740. case viotapegetstatus:
  741. if (op) {
  742. op->count = tevent->u.op.count;
  743. op->rc = tevent->sub_type_result;
  744. if (!op->non_blocking)
  745. complete(&op->com);
  746. }
  747. break;
  748. default:
  749. printk(VIOTAPE_KERN_WARN "weird ack\n");
  750. }
  751. }
  752. static int viotape_probe(struct vio_dev *vdev, const struct vio_device_id *id)
  753. {
  754. int i = vdev->unit_address;
  755. int j;
  756. struct device_node *node = vdev->dev.archdata.of_node;
  757. if (i > VIOTAPE_MAX_TAPE)
  758. return -ENODEV;
  759. if (!node)
  760. return -ENODEV;
  761. if (i >= viotape_numdev)
  762. viotape_numdev = i + 1;
  763. tape_device[i] = &vdev->dev;
  764. viotape_unitinfo[i].rsrcname = of_get_property(node,
  765. "linux,vio_rsrcname", NULL);
  766. viotape_unitinfo[i].type = of_get_property(node, "linux,vio_type",
  767. NULL);
  768. viotape_unitinfo[i].model = of_get_property(node, "linux,vio_model",
  769. NULL);
  770. state[i].cur_part = 0;
  771. for (j = 0; j < MAX_PARTITIONS; ++j)
  772. state[i].part_stat_rwi[j] = VIOT_IDLE;
  773. device_create(tape_class, NULL, MKDEV(VIOTAPE_MAJOR, i),
  774. "iseries!vt%d", i);
  775. device_create(tape_class, NULL, MKDEV(VIOTAPE_MAJOR, i | 0x80),
  776. "iseries!nvt%d", i);
  777. printk(VIOTAPE_KERN_INFO "tape iseries/vt%d is iSeries "
  778. "resource %10.10s type %4.4s, model %3.3s\n",
  779. i, viotape_unitinfo[i].rsrcname,
  780. viotape_unitinfo[i].type, viotape_unitinfo[i].model);
  781. return 0;
  782. }
  783. static int viotape_remove(struct vio_dev *vdev)
  784. {
  785. int i = vdev->unit_address;
  786. device_destroy(tape_class, MKDEV(VIOTAPE_MAJOR, i | 0x80));
  787. device_destroy(tape_class, MKDEV(VIOTAPE_MAJOR, i));
  788. return 0;
  789. }
  790. /**
  791. * viotape_device_table: Used by vio.c to match devices that we
  792. * support.
  793. */
  794. static struct vio_device_id viotape_device_table[] __devinitdata = {
  795. { "byte", "IBM,iSeries-viotape" },
  796. { "", "" }
  797. };
  798. MODULE_DEVICE_TABLE(vio, viotape_device_table);
  799. static struct vio_driver viotape_driver = {
  800. .id_table = viotape_device_table,
  801. .probe = viotape_probe,
  802. .remove = viotape_remove,
  803. .driver = {
  804. .name = "viotape",
  805. .owner = THIS_MODULE,
  806. }
  807. };
  808. int __init viotap_init(void)
  809. {
  810. int ret;
  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. proc_create("iSeries/viotape", S_IFREG|S_IRUGO, NULL,
  853. &proc_viotape_operations);
  854. return 0;
  855. unreg_class:
  856. class_destroy(tape_class);
  857. unreg_chrdev:
  858. unregister_chrdev(VIOTAPE_MAJOR, "viotape");
  859. clear_handler:
  860. vio_clearHandler(viomajorsubtype_tape);
  861. viopath_close(viopath_hostLp, viomajorsubtype_tape, VIOTAPE_MAXREQ + 2);
  862. clear_op:
  863. clear_op_struct_pool();
  864. return ret;
  865. }
  866. /* Give a new state to the tape object */
  867. static int chg_state(int index, unsigned char new_state, struct file *file)
  868. {
  869. unsigned char *cur_state =
  870. &state[index].part_stat_rwi[state[index].cur_part];
  871. int rc = 0;
  872. /* if the same state, don't bother */
  873. if (*cur_state == new_state)
  874. return 0;
  875. /* write an EOF if changing from writing to some other state */
  876. if (*cur_state == VIOT_WRITING) {
  877. struct mtop write_eof = { MTWEOF, 1 };
  878. rc = viotap_ioctl(NULL, file, MTIOCTOP,
  879. (unsigned long)&write_eof);
  880. }
  881. *cur_state = new_state;
  882. return rc;
  883. }
  884. /* Cleanup */
  885. static void __exit viotap_exit(void)
  886. {
  887. remove_proc_entry("iSeries/viotape", NULL);
  888. vio_unregister_driver(&viotape_driver);
  889. class_destroy(tape_class);
  890. unregister_chrdev(VIOTAPE_MAJOR, "viotape");
  891. viopath_close(viopath_hostLp, viomajorsubtype_tape, VIOTAPE_MAXREQ + 2);
  892. vio_clearHandler(viomajorsubtype_tape);
  893. clear_op_struct_pool();
  894. }
  895. MODULE_LICENSE("GPL");
  896. module_init(viotap_init);
  897. module_exit(viotap_exit);