viotape.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038
  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 long viotap_unlocked_ioctl(struct file *file,
  608. unsigned int cmd, unsigned long arg)
  609. {
  610. long rc;
  611. lock_kernel();
  612. rc = viotap_ioctl(file->f_path.dentry->d_inode, file, cmd, arg);
  613. unlock_kernel();
  614. return rc;
  615. }
  616. static int viotap_open(struct inode *inode, struct file *file)
  617. {
  618. HvLpEvent_Rc hvrc;
  619. struct viot_devinfo_struct devi;
  620. int ret;
  621. struct op_struct *op = get_op_struct();
  622. if (op == NULL)
  623. return -ENOMEM;
  624. lock_kernel();
  625. get_dev_info(file->f_path.dentry->d_inode, &devi);
  626. /* Note: We currently only support one mode! */
  627. if ((devi.devno >= viotape_numdev) || (devi.mode)) {
  628. ret = -ENODEV;
  629. goto free_op;
  630. }
  631. init_completion(&op->com);
  632. hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
  633. HvLpEvent_Type_VirtualIo,
  634. viomajorsubtype_tape | viotapeopen,
  635. HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck,
  636. viopath_sourceinst(viopath_hostLp),
  637. viopath_targetinst(viopath_hostLp),
  638. (u64)(unsigned long)op, VIOVERSION << 16,
  639. ((u64)devi.devno << 48), 0, 0, 0);
  640. if (hvrc != 0) {
  641. printk(VIOTAPE_KERN_WARN "bad rc on signalLpEvent %d\n",
  642. (int) hvrc);
  643. ret = -EIO;
  644. goto free_op;
  645. }
  646. wait_for_completion(&op->com);
  647. ret = tape_rc_to_errno(op->rc, "open", devi.devno);
  648. free_op:
  649. free_op_struct(op);
  650. unlock_kernel();
  651. return ret;
  652. }
  653. static int viotap_release(struct inode *inode, struct file *file)
  654. {
  655. HvLpEvent_Rc hvrc;
  656. struct viot_devinfo_struct devi;
  657. int ret = 0;
  658. struct op_struct *op = get_op_struct();
  659. if (op == NULL)
  660. return -ENOMEM;
  661. init_completion(&op->com);
  662. get_dev_info(file->f_path.dentry->d_inode, &devi);
  663. if (devi.devno >= viotape_numdev) {
  664. ret = -ENODEV;
  665. goto free_op;
  666. }
  667. chg_state(devi.devno, VIOT_IDLE, file);
  668. if (devi.rewind) {
  669. hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
  670. HvLpEvent_Type_VirtualIo,
  671. viomajorsubtype_tape | viotapeop,
  672. HvLpEvent_AckInd_DoAck,
  673. HvLpEvent_AckType_ImmediateAck,
  674. viopath_sourceinst(viopath_hostLp),
  675. viopath_targetinst(viopath_hostLp),
  676. (u64)(unsigned long)op, VIOVERSION << 16,
  677. ((u64)devi.devno << 48), 0,
  678. ((u64)VIOTAPOP_REW) << 32, 0);
  679. wait_for_completion(&op->com);
  680. tape_rc_to_errno(op->rc, "rewind", devi.devno);
  681. }
  682. hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
  683. HvLpEvent_Type_VirtualIo,
  684. viomajorsubtype_tape | viotapeclose,
  685. HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck,
  686. viopath_sourceinst(viopath_hostLp),
  687. viopath_targetinst(viopath_hostLp),
  688. (u64)(unsigned long)op, VIOVERSION << 16,
  689. ((u64)devi.devno << 48), 0, 0, 0);
  690. if (hvrc != 0) {
  691. printk(VIOTAPE_KERN_WARN "bad rc on signalLpEvent %d\n",
  692. (int) hvrc);
  693. ret = -EIO;
  694. goto free_op;
  695. }
  696. wait_for_completion(&op->com);
  697. if (op->rc)
  698. printk(VIOTAPE_KERN_WARN "close failed\n");
  699. free_op:
  700. free_op_struct(op);
  701. return ret;
  702. }
  703. const struct file_operations viotap_fops = {
  704. .owner = THIS_MODULE,
  705. .read = viotap_read,
  706. .write = viotap_write,
  707. .unlocked_ioctl = viotap_unlocked_ioctl,
  708. .open = viotap_open,
  709. .release = viotap_release,
  710. };
  711. /* Handle interrupt events for tape */
  712. static void vioHandleTapeEvent(struct HvLpEvent *event)
  713. {
  714. int tapeminor;
  715. struct op_struct *op;
  716. struct viotapelpevent *tevent = (struct viotapelpevent *)event;
  717. if (event == NULL) {
  718. /* Notification that a partition went away! */
  719. if (!viopath_isactive(viopath_hostLp)) {
  720. /* TODO! Clean up */
  721. }
  722. return;
  723. }
  724. tapeminor = event->xSubtype & VIOMINOR_SUBTYPE_MASK;
  725. op = (struct op_struct *)event->xCorrelationToken;
  726. switch (tapeminor) {
  727. case viotapeopen:
  728. case viotapeclose:
  729. op->rc = tevent->sub_type_result;
  730. complete(&op->com);
  731. break;
  732. case viotaperead:
  733. op->rc = tevent->sub_type_result;
  734. op->count = tevent->len;
  735. complete(&op->com);
  736. break;
  737. case viotapewrite:
  738. if (op->non_blocking) {
  739. dma_free_coherent(op->dev, op->count,
  740. op->buffer, op->dmaaddr);
  741. free_op_struct(op);
  742. up(&reqSem);
  743. } else {
  744. op->rc = tevent->sub_type_result;
  745. op->count = tevent->len;
  746. complete(&op->com);
  747. }
  748. break;
  749. case viotapeop:
  750. case viotapegetpos:
  751. case viotapesetpos:
  752. case viotapegetstatus:
  753. if (op) {
  754. op->count = tevent->u.op.count;
  755. op->rc = tevent->sub_type_result;
  756. if (!op->non_blocking)
  757. complete(&op->com);
  758. }
  759. break;
  760. default:
  761. printk(VIOTAPE_KERN_WARN "weird ack\n");
  762. }
  763. }
  764. static int viotape_probe(struct vio_dev *vdev, const struct vio_device_id *id)
  765. {
  766. int i = vdev->unit_address;
  767. int j;
  768. struct device_node *node = vdev->dev.archdata.of_node;
  769. if (i >= VIOTAPE_MAX_TAPE)
  770. return -ENODEV;
  771. if (!node)
  772. return -ENODEV;
  773. if (i >= viotape_numdev)
  774. viotape_numdev = i + 1;
  775. tape_device[i] = &vdev->dev;
  776. viotape_unitinfo[i].rsrcname = of_get_property(node,
  777. "linux,vio_rsrcname", NULL);
  778. viotape_unitinfo[i].type = of_get_property(node, "linux,vio_type",
  779. NULL);
  780. viotape_unitinfo[i].model = of_get_property(node, "linux,vio_model",
  781. NULL);
  782. state[i].cur_part = 0;
  783. for (j = 0; j < MAX_PARTITIONS; ++j)
  784. state[i].part_stat_rwi[j] = VIOT_IDLE;
  785. device_create(tape_class, NULL, MKDEV(VIOTAPE_MAJOR, i), NULL,
  786. "iseries!vt%d", i);
  787. device_create(tape_class, NULL, MKDEV(VIOTAPE_MAJOR, i | 0x80), NULL,
  788. "iseries!nvt%d", i);
  789. printk(VIOTAPE_KERN_INFO "tape iseries/vt%d is iSeries "
  790. "resource %10.10s type %4.4s, model %3.3s\n",
  791. i, viotape_unitinfo[i].rsrcname,
  792. viotape_unitinfo[i].type, viotape_unitinfo[i].model);
  793. return 0;
  794. }
  795. static int viotape_remove(struct vio_dev *vdev)
  796. {
  797. int i = vdev->unit_address;
  798. device_destroy(tape_class, MKDEV(VIOTAPE_MAJOR, i | 0x80));
  799. device_destroy(tape_class, MKDEV(VIOTAPE_MAJOR, i));
  800. return 0;
  801. }
  802. /**
  803. * viotape_device_table: Used by vio.c to match devices that we
  804. * support.
  805. */
  806. static struct vio_device_id viotape_device_table[] __devinitdata = {
  807. { "byte", "IBM,iSeries-viotape" },
  808. { "", "" }
  809. };
  810. MODULE_DEVICE_TABLE(vio, viotape_device_table);
  811. static struct vio_driver viotape_driver = {
  812. .id_table = viotape_device_table,
  813. .probe = viotape_probe,
  814. .remove = viotape_remove,
  815. .driver = {
  816. .name = "viotape",
  817. .owner = THIS_MODULE,
  818. }
  819. };
  820. int __init viotap_init(void)
  821. {
  822. int ret;
  823. if (!firmware_has_feature(FW_FEATURE_ISERIES))
  824. return -ENODEV;
  825. op_struct_list = NULL;
  826. if ((ret = add_op_structs(VIOTAPE_MAXREQ)) < 0) {
  827. printk(VIOTAPE_KERN_WARN "couldn't allocate op structs\n");
  828. return ret;
  829. }
  830. spin_lock_init(&op_struct_list_lock);
  831. sema_init(&reqSem, VIOTAPE_MAXREQ);
  832. if (viopath_hostLp == HvLpIndexInvalid) {
  833. vio_set_hostlp();
  834. if (viopath_hostLp == HvLpIndexInvalid) {
  835. ret = -ENODEV;
  836. goto clear_op;
  837. }
  838. }
  839. ret = viopath_open(viopath_hostLp, viomajorsubtype_tape,
  840. VIOTAPE_MAXREQ + 2);
  841. if (ret) {
  842. printk(VIOTAPE_KERN_WARN
  843. "error on viopath_open to hostlp %d\n", ret);
  844. ret = -EIO;
  845. goto clear_op;
  846. }
  847. printk(VIOTAPE_KERN_INFO "vers " VIOTAPE_VERSION
  848. ", hosting partition %d\n", viopath_hostLp);
  849. vio_setHandler(viomajorsubtype_tape, vioHandleTapeEvent);
  850. ret = register_chrdev(VIOTAPE_MAJOR, "viotape", &viotap_fops);
  851. if (ret < 0) {
  852. printk(VIOTAPE_KERN_WARN "Error registering viotape device\n");
  853. goto clear_handler;
  854. }
  855. tape_class = class_create(THIS_MODULE, "tape");
  856. if (IS_ERR(tape_class)) {
  857. printk(VIOTAPE_KERN_WARN "Unable to allocat class\n");
  858. ret = PTR_ERR(tape_class);
  859. goto unreg_chrdev;
  860. }
  861. ret = vio_register_driver(&viotape_driver);
  862. if (ret)
  863. goto unreg_class;
  864. proc_create("iSeries/viotape", S_IFREG|S_IRUGO, NULL,
  865. &proc_viotape_operations);
  866. return 0;
  867. unreg_class:
  868. class_destroy(tape_class);
  869. unreg_chrdev:
  870. unregister_chrdev(VIOTAPE_MAJOR, "viotape");
  871. clear_handler:
  872. vio_clearHandler(viomajorsubtype_tape);
  873. viopath_close(viopath_hostLp, viomajorsubtype_tape, VIOTAPE_MAXREQ + 2);
  874. clear_op:
  875. clear_op_struct_pool();
  876. return ret;
  877. }
  878. /* Give a new state to the tape object */
  879. static int chg_state(int index, unsigned char new_state, struct file *file)
  880. {
  881. unsigned char *cur_state =
  882. &state[index].part_stat_rwi[state[index].cur_part];
  883. int rc = 0;
  884. /* if the same state, don't bother */
  885. if (*cur_state == new_state)
  886. return 0;
  887. /* write an EOF if changing from writing to some other state */
  888. if (*cur_state == VIOT_WRITING) {
  889. struct mtop write_eof = { MTWEOF, 1 };
  890. rc = viotap_ioctl(NULL, file, MTIOCTOP,
  891. (unsigned long)&write_eof);
  892. }
  893. *cur_state = new_state;
  894. return rc;
  895. }
  896. /* Cleanup */
  897. static void __exit viotap_exit(void)
  898. {
  899. remove_proc_entry("iSeries/viotape", NULL);
  900. vio_unregister_driver(&viotape_driver);
  901. class_destroy(tape_class);
  902. unregister_chrdev(VIOTAPE_MAJOR, "viotape");
  903. viopath_close(viopath_hostLp, viomajorsubtype_tape, VIOTAPE_MAXREQ + 2);
  904. vio_clearHandler(viomajorsubtype_tape);
  905. clear_op_struct_pool();
  906. }
  907. MODULE_LICENSE("GPL");
  908. module_init(viotap_init);
  909. module_exit(viotap_exit);