viotape.c 27 KB

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