viocd.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824
  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_DEVICE_DEVFS "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 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 int rwreq;
  329. static void do_viocd_request(request_queue_t *q)
  330. {
  331. struct request *req;
  332. while ((rwreq == 0) && ((req = elv_next_request(q)) != NULL)) {
  333. if (!blk_fs_request(req))
  334. end_request(req, 0);
  335. else if (send_request(req) < 0) {
  336. printk(VIOCD_KERN_WARNING
  337. "unable to send message to OS/400!");
  338. end_request(req, 0);
  339. } else
  340. rwreq++;
  341. }
  342. }
  343. static int viocd_media_changed(struct cdrom_device_info *cdi, int disc_nr)
  344. {
  345. struct viocd_waitevent we;
  346. HvLpEvent_Rc hvrc;
  347. int device_no = DEVICE_NR((struct disk_info *)cdi->handle);
  348. init_completion(&we.com);
  349. /* Send the open event to OS/400 */
  350. hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
  351. HvLpEvent_Type_VirtualIo,
  352. viomajorsubtype_cdio | viocdcheck,
  353. HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck,
  354. viopath_sourceinst(viopath_hostLp),
  355. viopath_targetinst(viopath_hostLp),
  356. (u64)&we, VIOVERSION << 16, ((u64)device_no << 48),
  357. 0, 0, 0);
  358. if (hvrc != 0) {
  359. printk(VIOCD_KERN_WARNING "bad rc on HvCallEvent_signalLpEventFast %d\n",
  360. (int)hvrc);
  361. return -EIO;
  362. }
  363. wait_for_completion(&we.com);
  364. /* Check the return code. If bad, assume no change */
  365. if (we.rc) {
  366. const struct vio_error_entry *err =
  367. vio_lookup_rc(viocd_err_table, we.sub_result);
  368. printk(VIOCD_KERN_WARNING
  369. "bad rc %d:0x%04X on check_change: %s; Assuming no change\n",
  370. we.rc, we.sub_result, err->msg);
  371. return 0;
  372. }
  373. return we.changed;
  374. }
  375. static int viocd_lock_door(struct cdrom_device_info *cdi, int locking)
  376. {
  377. HvLpEvent_Rc hvrc;
  378. u64 device_no = DEVICE_NR((struct disk_info *)cdi->handle);
  379. /* NOTE: flags is 1 or 0 so it won't overwrite the device_no */
  380. u64 flags = !!locking;
  381. struct viocd_waitevent we;
  382. init_completion(&we.com);
  383. /* Send the lockdoor event to OS/400 */
  384. hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
  385. HvLpEvent_Type_VirtualIo,
  386. viomajorsubtype_cdio | viocdlockdoor,
  387. HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck,
  388. viopath_sourceinst(viopath_hostLp),
  389. viopath_targetinst(viopath_hostLp),
  390. (u64)&we, VIOVERSION << 16,
  391. (device_no << 48) | (flags << 32), 0, 0, 0);
  392. if (hvrc != 0) {
  393. printk(VIOCD_KERN_WARNING "bad rc on HvCallEvent_signalLpEventFast %d\n",
  394. (int)hvrc);
  395. return -EIO;
  396. }
  397. wait_for_completion(&we.com);
  398. if (we.rc != 0)
  399. return -EIO;
  400. return 0;
  401. }
  402. static int viocd_packet(struct cdrom_device_info *cdi,
  403. struct packet_command *cgc)
  404. {
  405. unsigned int buflen = cgc->buflen;
  406. int ret = -EIO;
  407. switch (cgc->cmd[0]) {
  408. case GPCMD_READ_DISC_INFO:
  409. {
  410. disc_information *di = (disc_information *)cgc->buffer;
  411. if (buflen >= 2) {
  412. di->disc_information_length = cpu_to_be16(1);
  413. ret = 0;
  414. }
  415. if (buflen >= 3)
  416. di->erasable =
  417. (cdi->ops->capability & ~cdi->mask
  418. & (CDC_DVD_RAM | CDC_RAM)) != 0;
  419. }
  420. break;
  421. case GPCMD_GET_CONFIGURATION:
  422. if (cgc->cmd[3] == CDF_RWRT) {
  423. struct rwrt_feature_desc *rfd = (struct rwrt_feature_desc *)(cgc->buffer + sizeof(struct feature_header));
  424. if ((buflen >=
  425. (sizeof(struct feature_header) + sizeof(*rfd))) &&
  426. (cdi->ops->capability & ~cdi->mask
  427. & (CDC_DVD_RAM | CDC_RAM))) {
  428. rfd->feature_code = cpu_to_be16(CDF_RWRT);
  429. rfd->curr = 1;
  430. ret = 0;
  431. }
  432. }
  433. break;
  434. default:
  435. if (cgc->sense) {
  436. /* indicate Unknown code */
  437. cgc->sense->sense_key = 0x05;
  438. cgc->sense->asc = 0x20;
  439. cgc->sense->ascq = 0x00;
  440. }
  441. break;
  442. }
  443. cgc->stat = ret;
  444. return ret;
  445. }
  446. static void restart_all_queues(int first_index)
  447. {
  448. int i;
  449. for (i = first_index + 1; i < viocd_numdev; i++)
  450. if (viocd_diskinfo[i].viocd_disk)
  451. blk_run_queue(viocd_diskinfo[i].viocd_disk->queue);
  452. for (i = 0; i <= first_index; i++)
  453. if (viocd_diskinfo[i].viocd_disk)
  454. blk_run_queue(viocd_diskinfo[i].viocd_disk->queue);
  455. }
  456. /* This routine handles incoming CD LP events */
  457. static void vio_handle_cd_event(struct HvLpEvent *event)
  458. {
  459. struct viocdlpevent *bevent;
  460. struct viocd_waitevent *pwe;
  461. struct disk_info *di;
  462. unsigned long flags;
  463. struct request *req;
  464. if (event == NULL)
  465. /* Notification that a partition went away! */
  466. return;
  467. /* First, we should NEVER get an int here...only acks */
  468. if (hvlpevent_is_int(event)) {
  469. printk(VIOCD_KERN_WARNING
  470. "Yikes! got an int in viocd event handler!\n");
  471. if (hvlpevent_need_ack(event)) {
  472. event->xRc = HvLpEvent_Rc_InvalidSubtype;
  473. HvCallEvent_ackLpEvent(event);
  474. }
  475. }
  476. bevent = (struct viocdlpevent *)event;
  477. switch (event->xSubtype & VIOMINOR_SUBTYPE_MASK) {
  478. case viocdopen:
  479. if (event->xRc == 0) {
  480. di = &viocd_diskinfo[bevent->disk];
  481. blk_queue_hardsect_size(di->viocd_disk->queue,
  482. bevent->block_size);
  483. set_capacity(di->viocd_disk,
  484. bevent->media_size *
  485. bevent->block_size / 512);
  486. }
  487. /* FALLTHROUGH !! */
  488. case viocdgetinfo:
  489. case viocdlockdoor:
  490. pwe = (struct viocd_waitevent *)event->xCorrelationToken;
  491. return_complete:
  492. pwe->rc = event->xRc;
  493. pwe->sub_result = bevent->sub_result;
  494. complete(&pwe->com);
  495. break;
  496. case viocdcheck:
  497. pwe = (struct viocd_waitevent *)event->xCorrelationToken;
  498. pwe->changed = bevent->flags;
  499. goto return_complete;
  500. case viocdclose:
  501. break;
  502. case viocdwrite:
  503. case viocdread:
  504. /*
  505. * Since this is running in interrupt mode, we need to
  506. * make sure we're not stepping on any global I/O operations
  507. */
  508. di = &viocd_diskinfo[bevent->disk];
  509. spin_lock_irqsave(&viocd_reqlock, flags);
  510. dma_unmap_single(di->dev, bevent->token, bevent->len,
  511. ((event->xSubtype & VIOMINOR_SUBTYPE_MASK) == viocdread)
  512. ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
  513. req = (struct request *)bevent->event.xCorrelationToken;
  514. rwreq--;
  515. if (event->xRc != HvLpEvent_Rc_Good) {
  516. const struct vio_error_entry *err =
  517. vio_lookup_rc(viocd_err_table,
  518. bevent->sub_result);
  519. printk(VIOCD_KERN_WARNING "request %p failed "
  520. "with rc %d:0x%04X: %s\n",
  521. req, event->xRc,
  522. bevent->sub_result, err->msg);
  523. end_request(req, 0);
  524. } else
  525. end_request(req, 1);
  526. /* restart handling of incoming requests */
  527. spin_unlock_irqrestore(&viocd_reqlock, flags);
  528. restart_all_queues(bevent->disk);
  529. break;
  530. default:
  531. printk(VIOCD_KERN_WARNING
  532. "message with invalid subtype %0x04X!\n",
  533. event->xSubtype & VIOMINOR_SUBTYPE_MASK);
  534. if (hvlpevent_need_ack(event)) {
  535. event->xRc = HvLpEvent_Rc_InvalidSubtype;
  536. HvCallEvent_ackLpEvent(event);
  537. }
  538. }
  539. }
  540. static struct cdrom_device_ops viocd_dops = {
  541. .open = viocd_open,
  542. .release = viocd_release,
  543. .media_changed = viocd_media_changed,
  544. .lock_door = viocd_lock_door,
  545. .generic_packet = viocd_packet,
  546. .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
  547. };
  548. static int __init find_capability(const char *type)
  549. {
  550. struct capability_entry *entry;
  551. for(entry = capability_table; entry->type; ++entry)
  552. if(!strncmp(entry->type, type, 4))
  553. break;
  554. return entry->capability;
  555. }
  556. static int viocd_probe(struct vio_dev *vdev, const struct vio_device_id *id)
  557. {
  558. struct gendisk *gendisk;
  559. int deviceno;
  560. struct disk_info *d;
  561. struct cdrom_device_info *c;
  562. struct cdrom_info *ci;
  563. struct request_queue *q;
  564. deviceno = vdev->unit_address;
  565. if (deviceno >= viocd_numdev)
  566. return -ENODEV;
  567. d = &viocd_diskinfo[deviceno];
  568. c = &d->viocd_info;
  569. ci = &viocd_unitinfo[deviceno];
  570. c->ops = &viocd_dops;
  571. c->speed = 4;
  572. c->capacity = 1;
  573. c->handle = d;
  574. c->mask = ~find_capability(ci->type);
  575. sprintf(c->name, VIOCD_DEVICE "%c", 'a' + deviceno);
  576. if (register_cdrom(c) != 0) {
  577. printk(VIOCD_KERN_WARNING "Cannot register viocd CD-ROM %s!\n",
  578. c->name);
  579. goto out;
  580. }
  581. printk(VIOCD_KERN_INFO "cd %s is iSeries resource %10.10s "
  582. "type %4.4s, model %3.3s\n",
  583. c->name, ci->rsrcname, ci->type, ci->model);
  584. q = blk_init_queue(do_viocd_request, &viocd_reqlock);
  585. if (q == NULL) {
  586. printk(VIOCD_KERN_WARNING "Cannot allocate queue for %s!\n",
  587. c->name);
  588. goto out_unregister_cdrom;
  589. }
  590. gendisk = alloc_disk(1);
  591. if (gendisk == NULL) {
  592. printk(VIOCD_KERN_WARNING "Cannot create gendisk for %s!\n",
  593. c->name);
  594. goto out_cleanup_queue;
  595. }
  596. gendisk->major = VIOCD_MAJOR;
  597. gendisk->first_minor = deviceno;
  598. strncpy(gendisk->disk_name, c->name,
  599. sizeof(gendisk->disk_name));
  600. snprintf(gendisk->devfs_name, sizeof(gendisk->devfs_name),
  601. VIOCD_DEVICE_DEVFS "%d", deviceno);
  602. blk_queue_max_hw_segments(q, 1);
  603. blk_queue_max_phys_segments(q, 1);
  604. blk_queue_max_sectors(q, 4096 / 512);
  605. gendisk->queue = q;
  606. gendisk->fops = &viocd_fops;
  607. gendisk->flags = GENHD_FL_CD|GENHD_FL_REMOVABLE;
  608. set_capacity(gendisk, 0);
  609. gendisk->private_data = d;
  610. d->viocd_disk = gendisk;
  611. d->dev = &vdev->dev;
  612. gendisk->driverfs_dev = d->dev;
  613. add_disk(gendisk);
  614. return 0;
  615. out_cleanup_queue:
  616. blk_cleanup_queue(q);
  617. out_unregister_cdrom:
  618. unregister_cdrom(c);
  619. out:
  620. return -ENODEV;
  621. }
  622. static int viocd_remove(struct vio_dev *vdev)
  623. {
  624. struct disk_info *d = &viocd_diskinfo[vdev->unit_address];
  625. if (unregister_cdrom(&d->viocd_info) != 0)
  626. printk(VIOCD_KERN_WARNING
  627. "Cannot unregister viocd CD-ROM %s!\n",
  628. d->viocd_info.name);
  629. del_gendisk(d->viocd_disk);
  630. blk_cleanup_queue(d->viocd_disk->queue);
  631. put_disk(d->viocd_disk);
  632. return 0;
  633. }
  634. /**
  635. * viocd_device_table: Used by vio.c to match devices that we
  636. * support.
  637. */
  638. static struct vio_device_id viocd_device_table[] __devinitdata = {
  639. { "viocd", "" },
  640. { "", "" }
  641. };
  642. MODULE_DEVICE_TABLE(vio, viocd_device_table);
  643. static struct vio_driver viocd_driver = {
  644. .id_table = viocd_device_table,
  645. .probe = viocd_probe,
  646. .remove = viocd_remove,
  647. .driver = {
  648. .name = "viocd",
  649. .owner = THIS_MODULE,
  650. }
  651. };
  652. static int __init viocd_init(void)
  653. {
  654. struct proc_dir_entry *e;
  655. int ret = 0;
  656. if (viopath_hostLp == HvLpIndexInvalid) {
  657. vio_set_hostlp();
  658. /* If we don't have a host, bail out */
  659. if (viopath_hostLp == HvLpIndexInvalid)
  660. return -ENODEV;
  661. }
  662. printk(VIOCD_KERN_INFO "vers " VIOCD_VERS ", hosting partition %d\n",
  663. viopath_hostLp);
  664. if (register_blkdev(VIOCD_MAJOR, VIOCD_DEVICE) != 0) {
  665. printk(VIOCD_KERN_WARNING "Unable to get major %d for %s\n",
  666. VIOCD_MAJOR, VIOCD_DEVICE);
  667. return -EIO;
  668. }
  669. ret = viopath_open(viopath_hostLp, viomajorsubtype_cdio,
  670. MAX_CD_REQ + 2);
  671. if (ret) {
  672. printk(VIOCD_KERN_WARNING
  673. "error opening path to host partition %d\n",
  674. viopath_hostLp);
  675. goto out_unregister;
  676. }
  677. /* Initialize our request handler */
  678. vio_setHandler(viomajorsubtype_cdio, vio_handle_cd_event);
  679. get_viocd_info();
  680. spin_lock_init(&viocd_reqlock);
  681. ret = vio_register_driver(&viocd_driver);
  682. if (ret)
  683. goto out_free_info;
  684. e = create_proc_entry("iSeries/viocd", S_IFREG|S_IRUGO, NULL);
  685. if (e) {
  686. e->owner = THIS_MODULE;
  687. e->proc_fops = &proc_viocd_operations;
  688. }
  689. return 0;
  690. out_free_info:
  691. dma_free_coherent(iSeries_vio_dev,
  692. sizeof(*viocd_unitinfo) * VIOCD_MAX_CD,
  693. viocd_unitinfo, unitinfo_dmaaddr);
  694. vio_clearHandler(viomajorsubtype_cdio);
  695. viopath_close(viopath_hostLp, viomajorsubtype_cdio, MAX_CD_REQ + 2);
  696. out_unregister:
  697. unregister_blkdev(VIOCD_MAJOR, VIOCD_DEVICE);
  698. return ret;
  699. }
  700. static void __exit viocd_exit(void)
  701. {
  702. remove_proc_entry("iSeries/viocd", NULL);
  703. vio_unregister_driver(&viocd_driver);
  704. if (viocd_unitinfo != NULL)
  705. dma_free_coherent(iSeries_vio_dev,
  706. sizeof(*viocd_unitinfo) * VIOCD_MAX_CD,
  707. viocd_unitinfo, unitinfo_dmaaddr);
  708. viopath_close(viopath_hostLp, viomajorsubtype_cdio, MAX_CD_REQ + 2);
  709. vio_clearHandler(viomajorsubtype_cdio);
  710. unregister_blkdev(VIOCD_MAJOR, VIOCD_DEVICE);
  711. }
  712. module_init(viocd_init);
  713. module_exit(viocd_exit);
  714. MODULE_LICENSE("GPL");