viotape.c 27 KB

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