viotape.c 26 KB

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