viotape.c 26 KB

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