viocd.c 21 KB

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