viotape.c 28 KB

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