viocd.c 21 KB

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