viocd.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844
  1. /* -*- linux-c -*-
  2. * drivers/cdrom/viocd.c
  3. *
  4. * iSeries Virtual CD Rom
  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 CD ROM drives owned and managed by an
  28. * OS/400 partition running on the same box as this Linux partition.
  29. *
  30. * All operations are performed by sending messages back and forth to
  31. * the OS/400 partition.
  32. */
  33. #include <linux/major.h>
  34. #include <linux/blkdev.h>
  35. #include <linux/cdrom.h>
  36. #include <linux/errno.h>
  37. #include <linux/init.h>
  38. #include <linux/dma-mapping.h>
  39. #include <linux/module.h>
  40. #include <linux/completion.h>
  41. #include <linux/proc_fs.h>
  42. #include <linux/seq_file.h>
  43. #include <asm/vio.h>
  44. #include <asm/scatterlist.h>
  45. #include <asm/iseries/hv_types.h>
  46. #include <asm/iseries/hv_lp_event.h>
  47. #include <asm/iseries/vio.h>
  48. #include <asm/firmware.h>
  49. #define VIOCD_DEVICE "iseries/vcd"
  50. #define VIOCD_VERS "1.06"
  51. #define VIOCD_KERN_WARNING KERN_WARNING "viocd: "
  52. #define VIOCD_KERN_INFO KERN_INFO "viocd: "
  53. struct viocdlpevent {
  54. struct HvLpEvent event;
  55. u32 reserved;
  56. u16 version;
  57. u16 sub_result;
  58. u16 disk;
  59. u16 flags;
  60. u32 token;
  61. u64 offset; /* On open, max number of disks */
  62. u64 len; /* On open, size of the disk */
  63. u32 block_size; /* Only set on open */
  64. u32 media_size; /* Only set on open */
  65. };
  66. enum viocdsubtype {
  67. viocdopen = 0x0001,
  68. viocdclose = 0x0002,
  69. viocdread = 0x0003,
  70. viocdwrite = 0x0004,
  71. viocdlockdoor = 0x0005,
  72. viocdgetinfo = 0x0006,
  73. viocdcheck = 0x0007
  74. };
  75. /*
  76. * Should probably make this a module parameter....sigh
  77. */
  78. #define VIOCD_MAX_CD HVMAXARCHITECTEDVIRTUALCDROMS
  79. static const struct vio_error_entry viocd_err_table[] = {
  80. {0x0201, EINVAL, "Invalid Range"},
  81. {0x0202, EINVAL, "Invalid Token"},
  82. {0x0203, EIO, "DMA Error"},
  83. {0x0204, EIO, "Use Error"},
  84. {0x0205, EIO, "Release Error"},
  85. {0x0206, EINVAL, "Invalid CD"},
  86. {0x020C, EROFS, "Read Only Device"},
  87. {0x020D, ENOMEDIUM, "Changed or Missing Volume (or Varied Off?)"},
  88. {0x020E, EIO, "Optical System Error (Varied Off?)"},
  89. {0x02FF, EIO, "Internal Error"},
  90. {0x3010, EIO, "Changed Volume"},
  91. {0xC100, EIO, "Optical System Error"},
  92. {0x0000, 0, NULL},
  93. };
  94. /*
  95. * This is the structure we use to exchange info between driver and interrupt
  96. * handler
  97. */
  98. struct viocd_waitevent {
  99. struct completion com;
  100. int rc;
  101. u16 sub_result;
  102. int changed;
  103. };
  104. /* this is a lookup table for the true capabilities of a device */
  105. struct capability_entry {
  106. char *type;
  107. int capability;
  108. };
  109. static struct capability_entry capability_table[] __initdata = {
  110. { "6330", CDC_LOCK | CDC_DVD_RAM | CDC_RAM },
  111. { "6331", CDC_LOCK | CDC_DVD_RAM | CDC_RAM },
  112. { "6333", CDC_LOCK | CDC_DVD_RAM | CDC_RAM },
  113. { "632A", CDC_LOCK | CDC_DVD_RAM | CDC_RAM },
  114. { "6321", CDC_LOCK },
  115. { "632B", 0 },
  116. { NULL , CDC_LOCK },
  117. };
  118. /* These are our internal structures for keeping track of devices */
  119. static int viocd_numdev;
  120. struct cdrom_info {
  121. char rsrcname[10];
  122. char type[4];
  123. char model[3];
  124. };
  125. /*
  126. * This needs to be allocated since it is passed to the
  127. * Hypervisor and we may be a module.
  128. */
  129. static struct cdrom_info *viocd_unitinfo;
  130. static dma_addr_t unitinfo_dmaaddr;
  131. struct disk_info {
  132. struct gendisk *viocd_disk;
  133. struct cdrom_device_info viocd_info;
  134. struct device *dev;
  135. };
  136. static struct disk_info viocd_diskinfo[VIOCD_MAX_CD];
  137. #define DEVICE_NR(di) ((di) - &viocd_diskinfo[0])
  138. static spinlock_t viocd_reqlock;
  139. #define MAX_CD_REQ 1
  140. /* procfs support */
  141. static int proc_viocd_show(struct seq_file *m, void *v)
  142. {
  143. int i;
  144. for (i = 0; i < viocd_numdev; i++) {
  145. seq_printf(m, "viocd device %d is iSeries resource %10.10s"
  146. "type %4.4s, model %3.3s\n",
  147. i, viocd_unitinfo[i].rsrcname,
  148. viocd_unitinfo[i].type,
  149. viocd_unitinfo[i].model);
  150. }
  151. return 0;
  152. }
  153. static int proc_viocd_open(struct inode *inode, struct file *file)
  154. {
  155. return single_open(file, proc_viocd_show, NULL);
  156. }
  157. static const struct file_operations proc_viocd_operations = {
  158. .open = proc_viocd_open,
  159. .read = seq_read,
  160. .llseek = seq_lseek,
  161. .release = single_release,
  162. };
  163. static int viocd_blk_open(struct inode *inode, struct file *file)
  164. {
  165. struct disk_info *di = inode->i_bdev->bd_disk->private_data;
  166. return cdrom_open(&di->viocd_info, inode, file);
  167. }
  168. static int viocd_blk_release(struct inode *inode, struct file *file)
  169. {
  170. struct disk_info *di = inode->i_bdev->bd_disk->private_data;
  171. return cdrom_release(&di->viocd_info, file);
  172. }
  173. static int viocd_blk_ioctl(struct inode *inode, struct file *file,
  174. unsigned cmd, unsigned long arg)
  175. {
  176. struct disk_info *di = inode->i_bdev->bd_disk->private_data;
  177. return cdrom_ioctl(file, &di->viocd_info, inode, cmd, arg);
  178. }
  179. static int viocd_blk_media_changed(struct gendisk *disk)
  180. {
  181. struct disk_info *di = disk->private_data;
  182. return cdrom_media_changed(&di->viocd_info);
  183. }
  184. struct block_device_operations viocd_fops = {
  185. .owner = THIS_MODULE,
  186. .open = viocd_blk_open,
  187. .release = viocd_blk_release,
  188. .ioctl = viocd_blk_ioctl,
  189. .media_changed = viocd_blk_media_changed,
  190. };
  191. /* Get info on CD devices from OS/400 */
  192. static void __init get_viocd_info(void)
  193. {
  194. HvLpEvent_Rc hvrc;
  195. int i;
  196. struct viocd_waitevent we;
  197. viocd_unitinfo = dma_alloc_coherent(iSeries_vio_dev,
  198. sizeof(*viocd_unitinfo) * VIOCD_MAX_CD,
  199. &unitinfo_dmaaddr, GFP_ATOMIC);
  200. if (viocd_unitinfo == NULL) {
  201. printk(VIOCD_KERN_WARNING "error allocating unitinfo\n");
  202. return;
  203. }
  204. memset(viocd_unitinfo, 0, sizeof(*viocd_unitinfo) * VIOCD_MAX_CD);
  205. init_completion(&we.com);
  206. hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
  207. HvLpEvent_Type_VirtualIo,
  208. viomajorsubtype_cdio | viocdgetinfo,
  209. HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck,
  210. viopath_sourceinst(viopath_hostLp),
  211. viopath_targetinst(viopath_hostLp),
  212. (u64)&we, VIOVERSION << 16, unitinfo_dmaaddr, 0,
  213. sizeof(*viocd_unitinfo) * VIOCD_MAX_CD, 0);
  214. if (hvrc != HvLpEvent_Rc_Good) {
  215. printk(VIOCD_KERN_WARNING "cdrom error sending event. rc %d\n",
  216. (int)hvrc);
  217. goto error_ret;
  218. }
  219. wait_for_completion(&we.com);
  220. if (we.rc) {
  221. const struct vio_error_entry *err =
  222. vio_lookup_rc(viocd_err_table, we.sub_result);
  223. printk(VIOCD_KERN_WARNING "bad rc %d:0x%04X on getinfo: %s\n",
  224. we.rc, we.sub_result, err->msg);
  225. goto error_ret;
  226. }
  227. for (i = 0; (i < VIOCD_MAX_CD) && viocd_unitinfo[i].rsrcname[0]; i++)
  228. viocd_numdev++;
  229. error_ret:
  230. if (viocd_numdev == 0) {
  231. dma_free_coherent(iSeries_vio_dev,
  232. sizeof(*viocd_unitinfo) * VIOCD_MAX_CD,
  233. viocd_unitinfo, unitinfo_dmaaddr);
  234. viocd_unitinfo = NULL;
  235. }
  236. }
  237. static int viocd_open(struct cdrom_device_info *cdi, int purpose)
  238. {
  239. struct disk_info *diskinfo = cdi->handle;
  240. int device_no = DEVICE_NR(diskinfo);
  241. HvLpEvent_Rc hvrc;
  242. struct viocd_waitevent we;
  243. init_completion(&we.com);
  244. hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
  245. HvLpEvent_Type_VirtualIo,
  246. viomajorsubtype_cdio | viocdopen,
  247. HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck,
  248. viopath_sourceinst(viopath_hostLp),
  249. viopath_targetinst(viopath_hostLp),
  250. (u64)&we, VIOVERSION << 16, ((u64)device_no << 48),
  251. 0, 0, 0);
  252. if (hvrc != 0) {
  253. printk(VIOCD_KERN_WARNING
  254. "bad rc on HvCallEvent_signalLpEventFast %d\n",
  255. (int)hvrc);
  256. return -EIO;
  257. }
  258. wait_for_completion(&we.com);
  259. if (we.rc) {
  260. const struct vio_error_entry *err =
  261. vio_lookup_rc(viocd_err_table, we.sub_result);
  262. printk(VIOCD_KERN_WARNING "bad rc %d:0x%04X on open: %s\n",
  263. we.rc, we.sub_result, err->msg);
  264. return -err->errno;
  265. }
  266. return 0;
  267. }
  268. static void viocd_release(struct cdrom_device_info *cdi)
  269. {
  270. int device_no = DEVICE_NR((struct disk_info *)cdi->handle);
  271. HvLpEvent_Rc hvrc;
  272. hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
  273. HvLpEvent_Type_VirtualIo,
  274. viomajorsubtype_cdio | viocdclose,
  275. HvLpEvent_AckInd_NoAck, HvLpEvent_AckType_ImmediateAck,
  276. viopath_sourceinst(viopath_hostLp),
  277. viopath_targetinst(viopath_hostLp), 0,
  278. VIOVERSION << 16, ((u64)device_no << 48), 0, 0, 0);
  279. if (hvrc != 0)
  280. printk(VIOCD_KERN_WARNING
  281. "bad rc on HvCallEvent_signalLpEventFast %d\n",
  282. (int)hvrc);
  283. }
  284. /* Send a read or write request to OS/400 */
  285. static int send_request(struct request *req)
  286. {
  287. HvLpEvent_Rc hvrc;
  288. struct disk_info *diskinfo = req->rq_disk->private_data;
  289. u64 len;
  290. dma_addr_t dmaaddr;
  291. int direction;
  292. u16 cmd;
  293. struct scatterlist sg;
  294. BUG_ON(req->nr_phys_segments > 1);
  295. if (rq_data_dir(req) == READ) {
  296. direction = DMA_FROM_DEVICE;
  297. cmd = viomajorsubtype_cdio | viocdread;
  298. } else {
  299. direction = DMA_TO_DEVICE;
  300. cmd = viomajorsubtype_cdio | viocdwrite;
  301. }
  302. if (blk_rq_map_sg(req->q, req, &sg) == 0) {
  303. printk(VIOCD_KERN_WARNING
  304. "error setting up scatter/gather list\n");
  305. return -1;
  306. }
  307. if (dma_map_sg(diskinfo->dev, &sg, 1, direction) == 0) {
  308. printk(VIOCD_KERN_WARNING "error allocating sg tce\n");
  309. return -1;
  310. }
  311. dmaaddr = sg_dma_address(&sg);
  312. len = sg_dma_len(&sg);
  313. hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
  314. HvLpEvent_Type_VirtualIo, cmd,
  315. HvLpEvent_AckInd_DoAck,
  316. HvLpEvent_AckType_ImmediateAck,
  317. viopath_sourceinst(viopath_hostLp),
  318. viopath_targetinst(viopath_hostLp),
  319. (u64)req, VIOVERSION << 16,
  320. ((u64)DEVICE_NR(diskinfo) << 48) | dmaaddr,
  321. (u64)req->sector * 512, len, 0);
  322. if (hvrc != HvLpEvent_Rc_Good) {
  323. printk(VIOCD_KERN_WARNING "hv error on op %d\n", (int)hvrc);
  324. return -1;
  325. }
  326. return 0;
  327. }
  328. static void viocd_end_request(struct request *req, int uptodate)
  329. {
  330. int nsectors = req->hard_nr_sectors;
  331. /*
  332. * Make sure it's fully ended, and ensure that we process
  333. * at least one sector.
  334. */
  335. if (blk_pc_request(req))
  336. nsectors = (req->data_len + 511) >> 9;
  337. if (!nsectors)
  338. nsectors = 1;
  339. if (end_that_request_first(req, uptodate, nsectors))
  340. BUG();
  341. add_disk_randomness(req->rq_disk);
  342. blkdev_dequeue_request(req);
  343. end_that_request_last(req, uptodate);
  344. }
  345. static int rwreq;
  346. static void do_viocd_request(request_queue_t *q)
  347. {
  348. struct request *req;
  349. while ((rwreq == 0) && ((req = elv_next_request(q)) != NULL)) {
  350. if (!blk_fs_request(req))
  351. viocd_end_request(req, 0);
  352. else if (send_request(req) < 0) {
  353. printk(VIOCD_KERN_WARNING
  354. "unable to send message to OS/400!");
  355. viocd_end_request(req, 0);
  356. } else
  357. rwreq++;
  358. }
  359. }
  360. static int viocd_media_changed(struct cdrom_device_info *cdi, int disc_nr)
  361. {
  362. struct viocd_waitevent we;
  363. HvLpEvent_Rc hvrc;
  364. int device_no = DEVICE_NR((struct disk_info *)cdi->handle);
  365. init_completion(&we.com);
  366. /* Send the open event to OS/400 */
  367. hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
  368. HvLpEvent_Type_VirtualIo,
  369. viomajorsubtype_cdio | viocdcheck,
  370. HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck,
  371. viopath_sourceinst(viopath_hostLp),
  372. viopath_targetinst(viopath_hostLp),
  373. (u64)&we, VIOVERSION << 16, ((u64)device_no << 48),
  374. 0, 0, 0);
  375. if (hvrc != 0) {
  376. printk(VIOCD_KERN_WARNING "bad rc on HvCallEvent_signalLpEventFast %d\n",
  377. (int)hvrc);
  378. return -EIO;
  379. }
  380. wait_for_completion(&we.com);
  381. /* Check the return code. If bad, assume no change */
  382. if (we.rc) {
  383. const struct vio_error_entry *err =
  384. vio_lookup_rc(viocd_err_table, we.sub_result);
  385. printk(VIOCD_KERN_WARNING
  386. "bad rc %d:0x%04X on check_change: %s; Assuming no change\n",
  387. we.rc, we.sub_result, err->msg);
  388. return 0;
  389. }
  390. return we.changed;
  391. }
  392. static int viocd_lock_door(struct cdrom_device_info *cdi, int locking)
  393. {
  394. HvLpEvent_Rc hvrc;
  395. u64 device_no = DEVICE_NR((struct disk_info *)cdi->handle);
  396. /* NOTE: flags is 1 or 0 so it won't overwrite the device_no */
  397. u64 flags = !!locking;
  398. struct viocd_waitevent we;
  399. init_completion(&we.com);
  400. /* Send the lockdoor event to OS/400 */
  401. hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
  402. HvLpEvent_Type_VirtualIo,
  403. viomajorsubtype_cdio | viocdlockdoor,
  404. HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck,
  405. viopath_sourceinst(viopath_hostLp),
  406. viopath_targetinst(viopath_hostLp),
  407. (u64)&we, VIOVERSION << 16,
  408. (device_no << 48) | (flags << 32), 0, 0, 0);
  409. if (hvrc != 0) {
  410. printk(VIOCD_KERN_WARNING "bad rc on HvCallEvent_signalLpEventFast %d\n",
  411. (int)hvrc);
  412. return -EIO;
  413. }
  414. wait_for_completion(&we.com);
  415. if (we.rc != 0)
  416. return -EIO;
  417. return 0;
  418. }
  419. static int viocd_packet(struct cdrom_device_info *cdi,
  420. struct packet_command *cgc)
  421. {
  422. unsigned int buflen = cgc->buflen;
  423. int ret = -EIO;
  424. switch (cgc->cmd[0]) {
  425. case GPCMD_READ_DISC_INFO:
  426. {
  427. disc_information *di = (disc_information *)cgc->buffer;
  428. if (buflen >= 2) {
  429. di->disc_information_length = cpu_to_be16(1);
  430. ret = 0;
  431. }
  432. if (buflen >= 3)
  433. di->erasable =
  434. (cdi->ops->capability & ~cdi->mask
  435. & (CDC_DVD_RAM | CDC_RAM)) != 0;
  436. }
  437. break;
  438. case GPCMD_GET_CONFIGURATION:
  439. if (cgc->cmd[3] == CDF_RWRT) {
  440. struct rwrt_feature_desc *rfd = (struct rwrt_feature_desc *)(cgc->buffer + sizeof(struct feature_header));
  441. if ((buflen >=
  442. (sizeof(struct feature_header) + sizeof(*rfd))) &&
  443. (cdi->ops->capability & ~cdi->mask
  444. & (CDC_DVD_RAM | CDC_RAM))) {
  445. rfd->feature_code = cpu_to_be16(CDF_RWRT);
  446. rfd->curr = 1;
  447. ret = 0;
  448. }
  449. }
  450. break;
  451. default:
  452. if (cgc->sense) {
  453. /* indicate Unknown code */
  454. cgc->sense->sense_key = 0x05;
  455. cgc->sense->asc = 0x20;
  456. cgc->sense->ascq = 0x00;
  457. }
  458. break;
  459. }
  460. cgc->stat = ret;
  461. return ret;
  462. }
  463. static void restart_all_queues(int first_index)
  464. {
  465. int i;
  466. for (i = first_index + 1; i < viocd_numdev; i++)
  467. if (viocd_diskinfo[i].viocd_disk)
  468. blk_run_queue(viocd_diskinfo[i].viocd_disk->queue);
  469. for (i = 0; i <= first_index; i++)
  470. if (viocd_diskinfo[i].viocd_disk)
  471. blk_run_queue(viocd_diskinfo[i].viocd_disk->queue);
  472. }
  473. /* This routine handles incoming CD LP events */
  474. static void vio_handle_cd_event(struct HvLpEvent *event)
  475. {
  476. struct viocdlpevent *bevent;
  477. struct viocd_waitevent *pwe;
  478. struct disk_info *di;
  479. unsigned long flags;
  480. struct request *req;
  481. if (event == NULL)
  482. /* Notification that a partition went away! */
  483. return;
  484. /* First, we should NEVER get an int here...only acks */
  485. if (hvlpevent_is_int(event)) {
  486. printk(VIOCD_KERN_WARNING
  487. "Yikes! got an int in viocd event handler!\n");
  488. if (hvlpevent_need_ack(event)) {
  489. event->xRc = HvLpEvent_Rc_InvalidSubtype;
  490. HvCallEvent_ackLpEvent(event);
  491. }
  492. }
  493. bevent = (struct viocdlpevent *)event;
  494. switch (event->xSubtype & VIOMINOR_SUBTYPE_MASK) {
  495. case viocdopen:
  496. if (event->xRc == 0) {
  497. di = &viocd_diskinfo[bevent->disk];
  498. blk_queue_hardsect_size(di->viocd_disk->queue,
  499. bevent->block_size);
  500. set_capacity(di->viocd_disk,
  501. bevent->media_size *
  502. bevent->block_size / 512);
  503. }
  504. /* FALLTHROUGH !! */
  505. case viocdgetinfo:
  506. case viocdlockdoor:
  507. pwe = (struct viocd_waitevent *)event->xCorrelationToken;
  508. return_complete:
  509. pwe->rc = event->xRc;
  510. pwe->sub_result = bevent->sub_result;
  511. complete(&pwe->com);
  512. break;
  513. case viocdcheck:
  514. pwe = (struct viocd_waitevent *)event->xCorrelationToken;
  515. pwe->changed = bevent->flags;
  516. goto return_complete;
  517. case viocdclose:
  518. break;
  519. case viocdwrite:
  520. case viocdread:
  521. /*
  522. * Since this is running in interrupt mode, we need to
  523. * make sure we're not stepping on any global I/O operations
  524. */
  525. di = &viocd_diskinfo[bevent->disk];
  526. spin_lock_irqsave(&viocd_reqlock, flags);
  527. dma_unmap_single(di->dev, bevent->token, bevent->len,
  528. ((event->xSubtype & VIOMINOR_SUBTYPE_MASK) == viocdread)
  529. ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
  530. req = (struct request *)bevent->event.xCorrelationToken;
  531. rwreq--;
  532. if (event->xRc != HvLpEvent_Rc_Good) {
  533. const struct vio_error_entry *err =
  534. vio_lookup_rc(viocd_err_table,
  535. bevent->sub_result);
  536. printk(VIOCD_KERN_WARNING "request %p failed "
  537. "with rc %d:0x%04X: %s\n",
  538. req, event->xRc,
  539. bevent->sub_result, err->msg);
  540. viocd_end_request(req, 0);
  541. } else
  542. viocd_end_request(req, 1);
  543. /* restart handling of incoming requests */
  544. spin_unlock_irqrestore(&viocd_reqlock, flags);
  545. restart_all_queues(bevent->disk);
  546. break;
  547. default:
  548. printk(VIOCD_KERN_WARNING
  549. "message with invalid subtype %0x04X!\n",
  550. event->xSubtype & VIOMINOR_SUBTYPE_MASK);
  551. if (hvlpevent_need_ack(event)) {
  552. event->xRc = HvLpEvent_Rc_InvalidSubtype;
  553. HvCallEvent_ackLpEvent(event);
  554. }
  555. }
  556. }
  557. static struct cdrom_device_ops viocd_dops = {
  558. .open = viocd_open,
  559. .release = viocd_release,
  560. .media_changed = viocd_media_changed,
  561. .lock_door = viocd_lock_door,
  562. .generic_packet = viocd_packet,
  563. .capability = CDC_CLOSE_TRAY | CDC_OPEN_TRAY | CDC_LOCK | CDC_SELECT_SPEED | CDC_SELECT_DISC | CDC_MULTI_SESSION | CDC_MCN | CDC_MEDIA_CHANGED | CDC_PLAY_AUDIO | CDC_RESET | CDC_DRIVE_STATUS | CDC_GENERIC_PACKET | CDC_CD_R | CDC_CD_RW | CDC_DVD | CDC_DVD_R | CDC_DVD_RAM | CDC_RAM
  564. };
  565. static int __init find_capability(const char *type)
  566. {
  567. struct capability_entry *entry;
  568. for(entry = capability_table; entry->type; ++entry)
  569. if(!strncmp(entry->type, type, 4))
  570. break;
  571. return entry->capability;
  572. }
  573. static int viocd_probe(struct vio_dev *vdev, const struct vio_device_id *id)
  574. {
  575. struct gendisk *gendisk;
  576. int deviceno;
  577. struct disk_info *d;
  578. struct cdrom_device_info *c;
  579. struct cdrom_info *ci;
  580. struct request_queue *q;
  581. deviceno = vdev->unit_address;
  582. if (deviceno >= viocd_numdev)
  583. return -ENODEV;
  584. d = &viocd_diskinfo[deviceno];
  585. c = &d->viocd_info;
  586. ci = &viocd_unitinfo[deviceno];
  587. c->ops = &viocd_dops;
  588. c->speed = 4;
  589. c->capacity = 1;
  590. c->handle = d;
  591. c->mask = ~find_capability(ci->type);
  592. sprintf(c->name, VIOCD_DEVICE "%c", 'a' + deviceno);
  593. if (register_cdrom(c) != 0) {
  594. printk(VIOCD_KERN_WARNING "Cannot register viocd CD-ROM %s!\n",
  595. c->name);
  596. goto out;
  597. }
  598. printk(VIOCD_KERN_INFO "cd %s is iSeries resource %10.10s "
  599. "type %4.4s, model %3.3s\n",
  600. c->name, ci->rsrcname, ci->type, ci->model);
  601. q = blk_init_queue(do_viocd_request, &viocd_reqlock);
  602. if (q == NULL) {
  603. printk(VIOCD_KERN_WARNING "Cannot allocate queue for %s!\n",
  604. c->name);
  605. goto out_unregister_cdrom;
  606. }
  607. gendisk = alloc_disk(1);
  608. if (gendisk == NULL) {
  609. printk(VIOCD_KERN_WARNING "Cannot create gendisk for %s!\n",
  610. c->name);
  611. goto out_cleanup_queue;
  612. }
  613. gendisk->major = VIOCD_MAJOR;
  614. gendisk->first_minor = deviceno;
  615. strncpy(gendisk->disk_name, c->name,
  616. sizeof(gendisk->disk_name));
  617. blk_queue_max_hw_segments(q, 1);
  618. blk_queue_max_phys_segments(q, 1);
  619. blk_queue_max_sectors(q, 4096 / 512);
  620. gendisk->queue = q;
  621. gendisk->fops = &viocd_fops;
  622. gendisk->flags = GENHD_FL_CD|GENHD_FL_REMOVABLE;
  623. set_capacity(gendisk, 0);
  624. gendisk->private_data = d;
  625. d->viocd_disk = gendisk;
  626. d->dev = &vdev->dev;
  627. gendisk->driverfs_dev = d->dev;
  628. add_disk(gendisk);
  629. return 0;
  630. out_cleanup_queue:
  631. blk_cleanup_queue(q);
  632. out_unregister_cdrom:
  633. unregister_cdrom(c);
  634. out:
  635. return -ENODEV;
  636. }
  637. static int viocd_remove(struct vio_dev *vdev)
  638. {
  639. struct disk_info *d = &viocd_diskinfo[vdev->unit_address];
  640. if (unregister_cdrom(&d->viocd_info) != 0)
  641. printk(VIOCD_KERN_WARNING
  642. "Cannot unregister viocd CD-ROM %s!\n",
  643. d->viocd_info.name);
  644. del_gendisk(d->viocd_disk);
  645. blk_cleanup_queue(d->viocd_disk->queue);
  646. put_disk(d->viocd_disk);
  647. return 0;
  648. }
  649. /**
  650. * viocd_device_table: Used by vio.c to match devices that we
  651. * support.
  652. */
  653. static struct vio_device_id viocd_device_table[] __devinitdata = {
  654. { "block", "IBM,iSeries-viocd" },
  655. { "", "" }
  656. };
  657. MODULE_DEVICE_TABLE(vio, viocd_device_table);
  658. static struct vio_driver viocd_driver = {
  659. .id_table = viocd_device_table,
  660. .probe = viocd_probe,
  661. .remove = viocd_remove,
  662. .driver = {
  663. .name = "viocd",
  664. .owner = THIS_MODULE,
  665. }
  666. };
  667. static int __init viocd_init(void)
  668. {
  669. struct proc_dir_entry *e;
  670. int ret = 0;
  671. if (!firmware_has_feature(FW_FEATURE_ISERIES))
  672. return -ENODEV;
  673. if (viopath_hostLp == HvLpIndexInvalid) {
  674. vio_set_hostlp();
  675. /* If we don't have a host, bail out */
  676. if (viopath_hostLp == HvLpIndexInvalid)
  677. return -ENODEV;
  678. }
  679. printk(VIOCD_KERN_INFO "vers " VIOCD_VERS ", hosting partition %d\n",
  680. viopath_hostLp);
  681. if (register_blkdev(VIOCD_MAJOR, VIOCD_DEVICE) != 0) {
  682. printk(VIOCD_KERN_WARNING "Unable to get major %d for %s\n",
  683. VIOCD_MAJOR, VIOCD_DEVICE);
  684. return -EIO;
  685. }
  686. ret = viopath_open(viopath_hostLp, viomajorsubtype_cdio,
  687. MAX_CD_REQ + 2);
  688. if (ret) {
  689. printk(VIOCD_KERN_WARNING
  690. "error opening path to host partition %d\n",
  691. viopath_hostLp);
  692. goto out_unregister;
  693. }
  694. /* Initialize our request handler */
  695. vio_setHandler(viomajorsubtype_cdio, vio_handle_cd_event);
  696. get_viocd_info();
  697. spin_lock_init(&viocd_reqlock);
  698. ret = vio_register_driver(&viocd_driver);
  699. if (ret)
  700. goto out_free_info;
  701. e = create_proc_entry("iSeries/viocd", S_IFREG|S_IRUGO, NULL);
  702. if (e) {
  703. e->owner = THIS_MODULE;
  704. e->proc_fops = &proc_viocd_operations;
  705. }
  706. return 0;
  707. out_free_info:
  708. dma_free_coherent(iSeries_vio_dev,
  709. sizeof(*viocd_unitinfo) * VIOCD_MAX_CD,
  710. viocd_unitinfo, unitinfo_dmaaddr);
  711. vio_clearHandler(viomajorsubtype_cdio);
  712. viopath_close(viopath_hostLp, viomajorsubtype_cdio, MAX_CD_REQ + 2);
  713. out_unregister:
  714. unregister_blkdev(VIOCD_MAJOR, VIOCD_DEVICE);
  715. return ret;
  716. }
  717. static void __exit viocd_exit(void)
  718. {
  719. remove_proc_entry("iSeries/viocd", NULL);
  720. vio_unregister_driver(&viocd_driver);
  721. if (viocd_unitinfo != NULL)
  722. dma_free_coherent(iSeries_vio_dev,
  723. sizeof(*viocd_unitinfo) * VIOCD_MAX_CD,
  724. viocd_unitinfo, unitinfo_dmaaddr);
  725. viopath_close(viopath_hostLp, viomajorsubtype_cdio, MAX_CD_REQ + 2);
  726. vio_clearHandler(viomajorsubtype_cdio);
  727. unregister_blkdev(VIOCD_MAJOR, VIOCD_DEVICE);
  728. }
  729. module_init(viocd_init);
  730. module_exit(viocd_exit);
  731. MODULE_LICENSE("GPL");