viocd.c 19 KB

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