viocd.c 21 KB

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