viocd.c 19 KB

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