viotape.c 28 KB

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