viotape.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122
  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. unsigned char part_stat_rwi[MAX_PARTITIONS];
  230. } state[VIOTAPE_MAX_TAPE];
  231. /* We single-thread */
  232. static struct semaphore reqSem;
  233. /*
  234. * When we send a request, we use this struct to get the response back
  235. * from the interrupt handler
  236. */
  237. struct op_struct {
  238. void *buffer;
  239. dma_addr_t dmaaddr;
  240. size_t count;
  241. int rc;
  242. int non_blocking;
  243. struct completion com;
  244. struct device *dev;
  245. struct op_struct *next;
  246. };
  247. static spinlock_t op_struct_list_lock;
  248. static struct op_struct *op_struct_list;
  249. /* forward declaration to resolve interdependence */
  250. static int chg_state(int index, unsigned char new_state, struct file *file);
  251. /* procfs support */
  252. static int proc_viotape_show(struct seq_file *m, void *v)
  253. {
  254. int i;
  255. seq_printf(m, "viotape driver version " VIOTAPE_VERSION "\n");
  256. for (i = 0; i < viotape_numdev; i++) {
  257. seq_printf(m, "viotape device %d is iSeries resource %10.10s"
  258. "type %4.4s, model %3.3s\n",
  259. i, viotape_unitinfo[i].rsrcname,
  260. viotape_unitinfo[i].type,
  261. viotape_unitinfo[i].model);
  262. }
  263. return 0;
  264. }
  265. static int proc_viotape_open(struct inode *inode, struct file *file)
  266. {
  267. return single_open(file, proc_viotape_show, NULL);
  268. }
  269. static struct file_operations proc_viotape_operations = {
  270. .open = proc_viotape_open,
  271. .read = seq_read,
  272. .llseek = seq_lseek,
  273. .release = single_release,
  274. };
  275. /* Decode the device minor number into its parts */
  276. void get_dev_info(struct inode *ino, struct viot_devinfo_struct *devi)
  277. {
  278. devi->devno = iminor(ino) & 0x1F;
  279. devi->mode = (iminor(ino) & 0x60) >> 5;
  280. /* if bit is set in the minor, do _not_ rewind automatically */
  281. devi->rewind = (iminor(ino) & 0x80) == 0;
  282. }
  283. /* This is called only from the exit and init paths, so no need for locking */
  284. static void clear_op_struct_pool(void)
  285. {
  286. while (op_struct_list) {
  287. struct op_struct *toFree = op_struct_list;
  288. op_struct_list = op_struct_list->next;
  289. kfree(toFree);
  290. }
  291. }
  292. /* Likewise, this is only called from the init path */
  293. static int add_op_structs(int structs)
  294. {
  295. int i;
  296. for (i = 0; i < structs; ++i) {
  297. struct op_struct *new_struct =
  298. kmalloc(sizeof(*new_struct), GFP_KERNEL);
  299. if (!new_struct) {
  300. clear_op_struct_pool();
  301. return -ENOMEM;
  302. }
  303. new_struct->next = op_struct_list;
  304. op_struct_list = new_struct;
  305. }
  306. return 0;
  307. }
  308. /* Allocate an op structure from our pool */
  309. static struct op_struct *get_op_struct(void)
  310. {
  311. struct op_struct *retval;
  312. unsigned long flags;
  313. spin_lock_irqsave(&op_struct_list_lock, flags);
  314. retval = op_struct_list;
  315. if (retval)
  316. op_struct_list = retval->next;
  317. spin_unlock_irqrestore(&op_struct_list_lock, flags);
  318. if (retval) {
  319. memset(retval, 0, sizeof(*retval));
  320. init_completion(&retval->com);
  321. }
  322. return retval;
  323. }
  324. /* Return an op structure to our pool */
  325. static void free_op_struct(struct op_struct *op_struct)
  326. {
  327. unsigned long flags;
  328. spin_lock_irqsave(&op_struct_list_lock, flags);
  329. op_struct->next = op_struct_list;
  330. op_struct_list = op_struct;
  331. spin_unlock_irqrestore(&op_struct_list_lock, flags);
  332. }
  333. /* Map our tape return codes to errno values */
  334. int tape_rc_to_errno(int tape_rc, char *operation, int tapeno)
  335. {
  336. const struct vio_error_entry *err;
  337. if (tape_rc == 0)
  338. return 0;
  339. err = vio_lookup_rc(viotape_err_table, tape_rc);
  340. printk(VIOTAPE_KERN_WARN "error(%s) 0x%04x on Device %d (%-10s): %s\n",
  341. operation, tape_rc, tapeno,
  342. viotape_unitinfo[tapeno].rsrcname, err->msg);
  343. return -err->errno;
  344. }
  345. /* Get info on all tapes from OS/400 */
  346. static int get_viotape_info(void)
  347. {
  348. HvLpEvent_Rc hvrc;
  349. int i;
  350. size_t len = sizeof(*viotape_unitinfo) * VIOTAPE_MAX_TAPE;
  351. struct op_struct *op = get_op_struct();
  352. if (op == NULL)
  353. return -ENOMEM;
  354. viotape_unitinfo = dma_alloc_coherent(iSeries_vio_dev, len,
  355. &viotape_unitinfo_token, GFP_ATOMIC);
  356. if (viotape_unitinfo == NULL) {
  357. free_op_struct(op);
  358. return -ENOMEM;
  359. }
  360. memset(viotape_unitinfo, 0, len);
  361. hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
  362. HvLpEvent_Type_VirtualIo,
  363. viomajorsubtype_tape | viotapegetinfo,
  364. HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck,
  365. viopath_sourceinst(viopath_hostLp),
  366. viopath_targetinst(viopath_hostLp),
  367. (u64) (unsigned long) op, VIOVERSION << 16,
  368. viotape_unitinfo_token, len, 0, 0);
  369. if (hvrc != HvLpEvent_Rc_Good) {
  370. printk(VIOTAPE_KERN_WARN "hv error on op %d\n",
  371. (int)hvrc);
  372. free_op_struct(op);
  373. return -EIO;
  374. }
  375. wait_for_completion(&op->com);
  376. free_op_struct(op);
  377. for (i = 0;
  378. ((i < VIOTAPE_MAX_TAPE) && (viotape_unitinfo[i].rsrcname[0]));
  379. i++)
  380. viotape_numdev++;
  381. return 0;
  382. }
  383. /* Write */
  384. static ssize_t viotap_write(struct file *file, const char *buf,
  385. size_t count, loff_t * ppos)
  386. {
  387. HvLpEvent_Rc hvrc;
  388. unsigned short flags = file->f_flags;
  389. int noblock = ((flags & O_NONBLOCK) != 0);
  390. ssize_t ret;
  391. struct viot_devinfo_struct devi;
  392. struct op_struct *op = get_op_struct();
  393. if (op == NULL)
  394. return -ENOMEM;
  395. get_dev_info(file->f_dentry->d_inode, &devi);
  396. /*
  397. * We need to make sure we can send a request. We use
  398. * a semaphore to keep track of # requests in use. If
  399. * we are non-blocking, make sure we don't block on the
  400. * semaphore
  401. */
  402. if (noblock) {
  403. if (down_trylock(&reqSem)) {
  404. ret = -EWOULDBLOCK;
  405. goto free_op;
  406. }
  407. } else
  408. down(&reqSem);
  409. /* 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. printk(VIOTAPE_KERN_WARN
  415. "error allocating dma buffer for len %ld\n",
  416. count);
  417. ret = -EFAULT;
  418. goto up_sem;
  419. }
  420. /* Copy the data into the buffer */
  421. if (copy_from_user(op->buffer, buf, count)) {
  422. printk(VIOTAPE_KERN_WARN "tape: error on copy from user\n");
  423. ret = -EFAULT;
  424. goto free_dma;
  425. }
  426. op->non_blocking = noblock;
  427. init_completion(&op->com);
  428. op->count = count;
  429. hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
  430. HvLpEvent_Type_VirtualIo,
  431. viomajorsubtype_tape | viotapewrite,
  432. HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck,
  433. viopath_sourceinst(viopath_hostLp),
  434. viopath_targetinst(viopath_hostLp),
  435. (u64)(unsigned long)op, VIOVERSION << 16,
  436. ((u64)devi.devno << 48) | op->dmaaddr, count, 0, 0);
  437. if (hvrc != HvLpEvent_Rc_Good) {
  438. printk(VIOTAPE_KERN_WARN "hv error on op %d\n",
  439. (int)hvrc);
  440. ret = -EIO;
  441. goto free_dma;
  442. }
  443. if (noblock)
  444. return count;
  445. wait_for_completion(&op->com);
  446. if (op->rc)
  447. ret = tape_rc_to_errno(op->rc, "write", devi.devno);
  448. else {
  449. chg_state(devi.devno, VIOT_WRITING, file);
  450. ret = op->count;
  451. }
  452. free_dma:
  453. dma_free_coherent(op->dev, count, op->buffer, op->dmaaddr);
  454. up_sem:
  455. up(&reqSem);
  456. free_op:
  457. free_op_struct(op);
  458. return ret;
  459. }
  460. /* read */
  461. static ssize_t viotap_read(struct file *file, char *buf, size_t count,
  462. loff_t *ptr)
  463. {
  464. HvLpEvent_Rc hvrc;
  465. unsigned short flags = file->f_flags;
  466. struct op_struct *op = get_op_struct();
  467. int noblock = ((flags & O_NONBLOCK) != 0);
  468. ssize_t ret;
  469. struct viot_devinfo_struct devi;
  470. if (op == NULL)
  471. return -ENOMEM;
  472. get_dev_info(file->f_dentry->d_inode, &devi);
  473. /*
  474. * We need to make sure we can send a request. We use
  475. * a semaphore to keep track of # requests in use. If
  476. * we are non-blocking, make sure we don't block on the
  477. * semaphore
  478. */
  479. if (noblock) {
  480. if (down_trylock(&reqSem)) {
  481. ret = -EWOULDBLOCK;
  482. goto free_op;
  483. }
  484. } else
  485. down(&reqSem);
  486. chg_state(devi.devno, VIOT_READING, file);
  487. /* Allocate a DMA buffer */
  488. op->dev = tape_device[devi.devno];
  489. op->buffer = dma_alloc_coherent(op->dev, count, &op->dmaaddr,
  490. GFP_ATOMIC);
  491. if (op->buffer == NULL) {
  492. ret = -EFAULT;
  493. goto up_sem;
  494. }
  495. op->count = count;
  496. init_completion(&op->com);
  497. hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
  498. HvLpEvent_Type_VirtualIo,
  499. viomajorsubtype_tape | viotaperead,
  500. HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck,
  501. viopath_sourceinst(viopath_hostLp),
  502. viopath_targetinst(viopath_hostLp),
  503. (u64)(unsigned long)op, VIOVERSION << 16,
  504. ((u64)devi.devno << 48) | op->dmaaddr, count, 0, 0);
  505. if (hvrc != HvLpEvent_Rc_Good) {
  506. printk(VIOTAPE_KERN_WARN "tape hv error on op %d\n",
  507. (int)hvrc);
  508. ret = -EIO;
  509. goto free_dma;
  510. }
  511. wait_for_completion(&op->com);
  512. if (op->rc)
  513. ret = tape_rc_to_errno(op->rc, "read", devi.devno);
  514. else {
  515. ret = op->count;
  516. if (ret && copy_to_user(buf, op->buffer, ret)) {
  517. printk(VIOTAPE_KERN_WARN "error on copy_to_user\n");
  518. ret = -EFAULT;
  519. }
  520. }
  521. free_dma:
  522. dma_free_coherent(op->dev, count, op->buffer, op->dmaaddr);
  523. up_sem:
  524. up(&reqSem);
  525. free_op:
  526. free_op_struct(op);
  527. return ret;
  528. }
  529. /* ioctl */
  530. static int viotap_ioctl(struct inode *inode, struct file *file,
  531. unsigned int cmd, unsigned long arg)
  532. {
  533. HvLpEvent_Rc hvrc;
  534. int ret;
  535. struct viot_devinfo_struct devi;
  536. struct mtop mtc;
  537. u32 myOp;
  538. struct op_struct *op = get_op_struct();
  539. if (op == NULL)
  540. return -ENOMEM;
  541. get_dev_info(file->f_dentry->d_inode, &devi);
  542. down(&reqSem);
  543. ret = -EINVAL;
  544. switch (cmd) {
  545. case MTIOCTOP:
  546. ret = -EFAULT;
  547. /*
  548. * inode is null if and only if we (the kernel)
  549. * made the request
  550. */
  551. if (inode == NULL)
  552. memcpy(&mtc, (void *) arg, sizeof(struct mtop));
  553. else if (copy_from_user((char *)&mtc, (char *)arg,
  554. sizeof(struct mtop)))
  555. goto free_op;
  556. ret = -EIO;
  557. switch (mtc.mt_op) {
  558. case MTRESET:
  559. myOp = VIOTAPOP_RESET;
  560. break;
  561. case MTFSF:
  562. myOp = VIOTAPOP_FSF;
  563. break;
  564. case MTBSF:
  565. myOp = VIOTAPOP_BSF;
  566. break;
  567. case MTFSR:
  568. myOp = VIOTAPOP_FSR;
  569. break;
  570. case MTBSR:
  571. myOp = VIOTAPOP_BSR;
  572. break;
  573. case MTWEOF:
  574. myOp = VIOTAPOP_WEOF;
  575. break;
  576. case MTREW:
  577. myOp = VIOTAPOP_REW;
  578. break;
  579. case MTNOP:
  580. myOp = VIOTAPOP_NOP;
  581. break;
  582. case MTEOM:
  583. myOp = VIOTAPOP_EOM;
  584. break;
  585. case MTERASE:
  586. myOp = VIOTAPOP_ERASE;
  587. break;
  588. case MTSETBLK:
  589. myOp = VIOTAPOP_SETBLK;
  590. break;
  591. case MTSETDENSITY:
  592. myOp = VIOTAPOP_SETDENSITY;
  593. break;
  594. case MTTELL:
  595. myOp = VIOTAPOP_GETPOS;
  596. break;
  597. case MTSEEK:
  598. myOp = VIOTAPOP_SETPOS;
  599. break;
  600. case MTSETPART:
  601. myOp = VIOTAPOP_SETPART;
  602. break;
  603. case MTOFFL:
  604. myOp = VIOTAPOP_UNLOAD;
  605. break;
  606. default:
  607. printk(VIOTAPE_KERN_WARN "MTIOCTOP called "
  608. "with invalid op 0x%x\n", mtc.mt_op);
  609. goto free_op;
  610. }
  611. /*
  612. * if we moved the head, we are no longer
  613. * reading or writing
  614. */
  615. switch (mtc.mt_op) {
  616. case MTFSF:
  617. case MTBSF:
  618. case MTFSR:
  619. case MTBSR:
  620. case MTTELL:
  621. case MTSEEK:
  622. case MTREW:
  623. chg_state(devi.devno, VIOT_IDLE, file);
  624. }
  625. init_completion(&op->com);
  626. hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
  627. HvLpEvent_Type_VirtualIo,
  628. viomajorsubtype_tape | viotapeop,
  629. HvLpEvent_AckInd_DoAck,
  630. HvLpEvent_AckType_ImmediateAck,
  631. viopath_sourceinst(viopath_hostLp),
  632. viopath_targetinst(viopath_hostLp),
  633. (u64)(unsigned long)op,
  634. VIOVERSION << 16,
  635. ((u64)devi.devno << 48), 0,
  636. (((u64)myOp) << 32) | mtc.mt_count, 0);
  637. if (hvrc != HvLpEvent_Rc_Good) {
  638. printk(VIOTAPE_KERN_WARN "hv error on op %d\n",
  639. (int)hvrc);
  640. goto free_op;
  641. }
  642. wait_for_completion(&op->com);
  643. ret = tape_rc_to_errno(op->rc, "tape operation", devi.devno);
  644. goto free_op;
  645. case MTIOCGET:
  646. ret = -EIO;
  647. init_completion(&op->com);
  648. hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
  649. HvLpEvent_Type_VirtualIo,
  650. viomajorsubtype_tape | viotapegetstatus,
  651. HvLpEvent_AckInd_DoAck,
  652. HvLpEvent_AckType_ImmediateAck,
  653. viopath_sourceinst(viopath_hostLp),
  654. viopath_targetinst(viopath_hostLp),
  655. (u64)(unsigned long)op, VIOVERSION << 16,
  656. ((u64)devi.devno << 48), 0, 0, 0);
  657. if (hvrc != HvLpEvent_Rc_Good) {
  658. printk(VIOTAPE_KERN_WARN "hv error on op %d\n",
  659. (int)hvrc);
  660. goto free_op;
  661. }
  662. wait_for_completion(&op->com);
  663. /* Operation is complete - grab the error code */
  664. ret = tape_rc_to_errno(op->rc, "get status", devi.devno);
  665. free_op_struct(op);
  666. up(&reqSem);
  667. if ((ret == 0) && copy_to_user((void *)arg,
  668. &viomtget[devi.devno],
  669. sizeof(viomtget[0])))
  670. ret = -EFAULT;
  671. return ret;
  672. case MTIOCPOS:
  673. printk(VIOTAPE_KERN_WARN "Got an (unsupported) MTIOCPOS\n");
  674. break;
  675. default:
  676. printk(VIOTAPE_KERN_WARN "got an unsupported ioctl 0x%0x\n",
  677. cmd);
  678. break;
  679. }
  680. free_op:
  681. free_op_struct(op);
  682. up(&reqSem);
  683. return ret;
  684. }
  685. static int viotap_open(struct inode *inode, struct file *file)
  686. {
  687. HvLpEvent_Rc hvrc;
  688. struct viot_devinfo_struct devi;
  689. int ret;
  690. struct op_struct *op = get_op_struct();
  691. if (op == NULL)
  692. return -ENOMEM;
  693. get_dev_info(file->f_dentry->d_inode, &devi);
  694. /* Note: We currently only support one mode! */
  695. if ((devi.devno >= viotape_numdev) || (devi.mode)) {
  696. ret = -ENODEV;
  697. goto free_op;
  698. }
  699. init_completion(&op->com);
  700. hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
  701. HvLpEvent_Type_VirtualIo,
  702. viomajorsubtype_tape | viotapeopen,
  703. HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck,
  704. viopath_sourceinst(viopath_hostLp),
  705. viopath_targetinst(viopath_hostLp),
  706. (u64)(unsigned long)op, VIOVERSION << 16,
  707. ((u64)devi.devno << 48), 0, 0, 0);
  708. if (hvrc != 0) {
  709. printk(VIOTAPE_KERN_WARN "bad rc on signalLpEvent %d\n",
  710. (int) hvrc);
  711. ret = -EIO;
  712. goto free_op;
  713. }
  714. wait_for_completion(&op->com);
  715. ret = tape_rc_to_errno(op->rc, "open", devi.devno);
  716. free_op:
  717. free_op_struct(op);
  718. return ret;
  719. }
  720. static int viotap_release(struct inode *inode, struct file *file)
  721. {
  722. HvLpEvent_Rc hvrc;
  723. struct viot_devinfo_struct devi;
  724. int ret = 0;
  725. struct op_struct *op = get_op_struct();
  726. if (op == NULL)
  727. return -ENOMEM;
  728. init_completion(&op->com);
  729. get_dev_info(file->f_dentry->d_inode, &devi);
  730. if (devi.devno >= viotape_numdev) {
  731. ret = -ENODEV;
  732. goto free_op;
  733. }
  734. chg_state(devi.devno, VIOT_IDLE, file);
  735. if (devi.rewind) {
  736. hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
  737. HvLpEvent_Type_VirtualIo,
  738. viomajorsubtype_tape | viotapeop,
  739. HvLpEvent_AckInd_DoAck,
  740. HvLpEvent_AckType_ImmediateAck,
  741. viopath_sourceinst(viopath_hostLp),
  742. viopath_targetinst(viopath_hostLp),
  743. (u64)(unsigned long)op, VIOVERSION << 16,
  744. ((u64)devi.devno << 48), 0,
  745. ((u64)VIOTAPOP_REW) << 32, 0);
  746. wait_for_completion(&op->com);
  747. tape_rc_to_errno(op->rc, "rewind", devi.devno);
  748. }
  749. hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
  750. HvLpEvent_Type_VirtualIo,
  751. viomajorsubtype_tape | viotapeclose,
  752. HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck,
  753. viopath_sourceinst(viopath_hostLp),
  754. viopath_targetinst(viopath_hostLp),
  755. (u64)(unsigned long)op, VIOVERSION << 16,
  756. ((u64)devi.devno << 48), 0, 0, 0);
  757. if (hvrc != 0) {
  758. printk(VIOTAPE_KERN_WARN "bad rc on signalLpEvent %d\n",
  759. (int) hvrc);
  760. ret = -EIO;
  761. goto free_op;
  762. }
  763. wait_for_completion(&op->com);
  764. if (op->rc)
  765. printk(VIOTAPE_KERN_WARN "close failed\n");
  766. free_op:
  767. free_op_struct(op);
  768. return ret;
  769. }
  770. struct file_operations viotap_fops = {
  771. owner: THIS_MODULE,
  772. read: viotap_read,
  773. write: viotap_write,
  774. ioctl: viotap_ioctl,
  775. open: viotap_open,
  776. release: viotap_release,
  777. };
  778. /* Handle interrupt events for tape */
  779. static void vioHandleTapeEvent(struct HvLpEvent *event)
  780. {
  781. int tapeminor;
  782. struct op_struct *op;
  783. struct viotapelpevent *tevent = (struct viotapelpevent *)event;
  784. if (event == NULL) {
  785. /* Notification that a partition went away! */
  786. if (!viopath_isactive(viopath_hostLp)) {
  787. /* TODO! Clean up */
  788. }
  789. return;
  790. }
  791. tapeminor = event->xSubtype & VIOMINOR_SUBTYPE_MASK;
  792. op = (struct op_struct *)event->xCorrelationToken;
  793. switch (tapeminor) {
  794. case viotapegetinfo:
  795. case viotapeopen:
  796. case viotapeclose:
  797. op->rc = tevent->sub_type_result;
  798. complete(&op->com);
  799. break;
  800. case viotaperead:
  801. op->rc = tevent->sub_type_result;
  802. op->count = tevent->len;
  803. complete(&op->com);
  804. break;
  805. case viotapewrite:
  806. if (op->non_blocking) {
  807. dma_free_coherent(op->dev, op->count,
  808. op->buffer, op->dmaaddr);
  809. free_op_struct(op);
  810. up(&reqSem);
  811. } else {
  812. op->rc = tevent->sub_type_result;
  813. op->count = tevent->len;
  814. complete(&op->com);
  815. }
  816. break;
  817. case viotapeop:
  818. case viotapegetpos:
  819. case viotapesetpos:
  820. case viotapegetstatus:
  821. if (op) {
  822. op->count = tevent->u.op.count;
  823. op->rc = tevent->sub_type_result;
  824. if (!op->non_blocking)
  825. complete(&op->com);
  826. }
  827. break;
  828. default:
  829. printk(VIOTAPE_KERN_WARN "weird ack\n");
  830. }
  831. }
  832. static int viotape_probe(struct vio_dev *vdev, const struct vio_device_id *id)
  833. {
  834. char tapename[32];
  835. int i = vdev->unit_address;
  836. int j;
  837. if (i >= viotape_numdev)
  838. return -ENODEV;
  839. tape_device[i] = &vdev->dev;
  840. state[i].cur_part = 0;
  841. for (j = 0; j < MAX_PARTITIONS; ++j)
  842. state[i].part_stat_rwi[j] = VIOT_IDLE;
  843. class_device_create(tape_class, NULL, MKDEV(VIOTAPE_MAJOR, i), NULL,
  844. "iseries!vt%d", i);
  845. class_device_create(tape_class, NULL, MKDEV(VIOTAPE_MAJOR, i | 0x80),
  846. NULL, "iseries!nvt%d", i);
  847. sprintf(tapename, "iseries/vt%d", i);
  848. printk(VIOTAPE_KERN_INFO "tape %s is iSeries "
  849. "resource %10.10s type %4.4s, model %3.3s\n",
  850. tapename, viotape_unitinfo[i].rsrcname,
  851. viotape_unitinfo[i].type, viotape_unitinfo[i].model);
  852. return 0;
  853. }
  854. static int viotape_remove(struct vio_dev *vdev)
  855. {
  856. int i = vdev->unit_address;
  857. class_device_destroy(tape_class, MKDEV(VIOTAPE_MAJOR, i | 0x80));
  858. class_device_destroy(tape_class, MKDEV(VIOTAPE_MAJOR, i));
  859. return 0;
  860. }
  861. /**
  862. * viotape_device_table: Used by vio.c to match devices that we
  863. * support.
  864. */
  865. static struct vio_device_id viotape_device_table[] __devinitdata = {
  866. { "byte", "IBM,iSeries-viotape" },
  867. { "", "" }
  868. };
  869. MODULE_DEVICE_TABLE(vio, viotape_device_table);
  870. static struct vio_driver viotape_driver = {
  871. .id_table = viotape_device_table,
  872. .probe = viotape_probe,
  873. .remove = viotape_remove,
  874. .driver = {
  875. .name = "viotape",
  876. .owner = THIS_MODULE,
  877. }
  878. };
  879. int __init viotap_init(void)
  880. {
  881. int ret;
  882. struct proc_dir_entry *e;
  883. op_struct_list = NULL;
  884. if ((ret = add_op_structs(VIOTAPE_MAXREQ)) < 0) {
  885. printk(VIOTAPE_KERN_WARN "couldn't allocate op structs\n");
  886. return ret;
  887. }
  888. spin_lock_init(&op_struct_list_lock);
  889. sema_init(&reqSem, VIOTAPE_MAXREQ);
  890. if (viopath_hostLp == HvLpIndexInvalid) {
  891. vio_set_hostlp();
  892. if (viopath_hostLp == HvLpIndexInvalid) {
  893. ret = -ENODEV;
  894. goto clear_op;
  895. }
  896. }
  897. ret = viopath_open(viopath_hostLp, viomajorsubtype_tape,
  898. VIOTAPE_MAXREQ + 2);
  899. if (ret) {
  900. printk(VIOTAPE_KERN_WARN
  901. "error on viopath_open to hostlp %d\n", ret);
  902. ret = -EIO;
  903. goto clear_op;
  904. }
  905. printk(VIOTAPE_KERN_INFO "vers " VIOTAPE_VERSION
  906. ", hosting partition %d\n", viopath_hostLp);
  907. vio_setHandler(viomajorsubtype_tape, vioHandleTapeEvent);
  908. ret = register_chrdev(VIOTAPE_MAJOR, "viotape", &viotap_fops);
  909. if (ret < 0) {
  910. printk(VIOTAPE_KERN_WARN "Error registering viotape device\n");
  911. goto clear_handler;
  912. }
  913. tape_class = class_create(THIS_MODULE, "tape");
  914. if (IS_ERR(tape_class)) {
  915. printk(VIOTAPE_KERN_WARN "Unable to allocat class\n");
  916. ret = PTR_ERR(tape_class);
  917. goto unreg_chrdev;
  918. }
  919. if ((ret = get_viotape_info()) < 0) {
  920. printk(VIOTAPE_KERN_WARN "Unable to obtain virtual device information");
  921. goto unreg_class;
  922. }
  923. ret = vio_register_driver(&viotape_driver);
  924. if (ret)
  925. goto unreg_class;
  926. e = create_proc_entry("iSeries/viotape", S_IFREG|S_IRUGO, NULL);
  927. if (e) {
  928. e->owner = THIS_MODULE;
  929. e->proc_fops = &proc_viotape_operations;
  930. }
  931. return 0;
  932. unreg_class:
  933. class_destroy(tape_class);
  934. unreg_chrdev:
  935. unregister_chrdev(VIOTAPE_MAJOR, "viotape");
  936. clear_handler:
  937. vio_clearHandler(viomajorsubtype_tape);
  938. viopath_close(viopath_hostLp, viomajorsubtype_tape, VIOTAPE_MAXREQ + 2);
  939. clear_op:
  940. clear_op_struct_pool();
  941. return ret;
  942. }
  943. /* Give a new state to the tape object */
  944. static int chg_state(int index, unsigned char new_state, struct file *file)
  945. {
  946. unsigned char *cur_state =
  947. &state[index].part_stat_rwi[state[index].cur_part];
  948. int rc = 0;
  949. /* if the same state, don't bother */
  950. if (*cur_state == new_state)
  951. return 0;
  952. /* write an EOF if changing from writing to some other state */
  953. if (*cur_state == VIOT_WRITING) {
  954. struct mtop write_eof = { MTWEOF, 1 };
  955. rc = viotap_ioctl(NULL, file, MTIOCTOP,
  956. (unsigned long)&write_eof);
  957. }
  958. *cur_state = new_state;
  959. return rc;
  960. }
  961. /* Cleanup */
  962. static void __exit viotap_exit(void)
  963. {
  964. int ret;
  965. remove_proc_entry("iSeries/viotape", NULL);
  966. vio_unregister_driver(&viotape_driver);
  967. class_destroy(tape_class);
  968. ret = unregister_chrdev(VIOTAPE_MAJOR, "viotape");
  969. if (ret < 0)
  970. printk(VIOTAPE_KERN_WARN "Error unregistering device: %d\n",
  971. ret);
  972. if (viotape_unitinfo)
  973. dma_free_coherent(iSeries_vio_dev,
  974. sizeof(viotape_unitinfo[0]) * VIOTAPE_MAX_TAPE,
  975. viotape_unitinfo, viotape_unitinfo_token);
  976. viopath_close(viopath_hostLp, viomajorsubtype_tape, VIOTAPE_MAXREQ + 2);
  977. vio_clearHandler(viomajorsubtype_tape);
  978. clear_op_struct_pool();
  979. }
  980. MODULE_LICENSE("GPL");
  981. module_init(viotap_init);
  982. module_exit(viotap_exit);