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