viocd.c 20 KB

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