viocd.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736
  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)blk_rq_pos(req) * 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 int rwreq;
  256. static void do_viocd_request(struct request_queue *q)
  257. {
  258. struct request *req;
  259. while ((rwreq == 0) && ((req = blk_fetch_request(q)) != NULL)) {
  260. if (!blk_fs_request(req))
  261. __blk_end_request_all(req, -EIO);
  262. else if (send_request(req) < 0) {
  263. printk(VIOCD_KERN_WARNING
  264. "unable to send message to OS/400!");
  265. __blk_end_request_all(req, -EIO);
  266. } else
  267. rwreq++;
  268. }
  269. }
  270. static int viocd_media_changed(struct cdrom_device_info *cdi, int disc_nr)
  271. {
  272. struct viocd_waitevent we;
  273. HvLpEvent_Rc hvrc;
  274. int device_no = DEVICE_NR((struct disk_info *)cdi->handle);
  275. init_completion(&we.com);
  276. /* Send the open event to OS/400 */
  277. hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
  278. HvLpEvent_Type_VirtualIo,
  279. viomajorsubtype_cdio | viocdcheck,
  280. HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck,
  281. viopath_sourceinst(viopath_hostLp),
  282. viopath_targetinst(viopath_hostLp),
  283. (u64)&we, VIOVERSION << 16, ((u64)device_no << 48),
  284. 0, 0, 0);
  285. if (hvrc != 0) {
  286. printk(VIOCD_KERN_WARNING "bad rc on HvCallEvent_signalLpEventFast %d\n",
  287. (int)hvrc);
  288. return -EIO;
  289. }
  290. wait_for_completion(&we.com);
  291. /* Check the return code. If bad, assume no change */
  292. if (we.rc) {
  293. const struct vio_error_entry *err =
  294. vio_lookup_rc(viocd_err_table, we.sub_result);
  295. printk(VIOCD_KERN_WARNING
  296. "bad rc %d:0x%04X on check_change: %s; Assuming no change\n",
  297. we.rc, we.sub_result, err->msg);
  298. return 0;
  299. }
  300. return we.changed;
  301. }
  302. static int viocd_lock_door(struct cdrom_device_info *cdi, int locking)
  303. {
  304. HvLpEvent_Rc hvrc;
  305. u64 device_no = DEVICE_NR((struct disk_info *)cdi->handle);
  306. /* NOTE: flags is 1 or 0 so it won't overwrite the device_no */
  307. u64 flags = !!locking;
  308. struct viocd_waitevent we;
  309. init_completion(&we.com);
  310. /* Send the lockdoor event to OS/400 */
  311. hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
  312. HvLpEvent_Type_VirtualIo,
  313. viomajorsubtype_cdio | viocdlockdoor,
  314. HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck,
  315. viopath_sourceinst(viopath_hostLp),
  316. viopath_targetinst(viopath_hostLp),
  317. (u64)&we, VIOVERSION << 16,
  318. (device_no << 48) | (flags << 32), 0, 0, 0);
  319. if (hvrc != 0) {
  320. printk(VIOCD_KERN_WARNING "bad rc on HvCallEvent_signalLpEventFast %d\n",
  321. (int)hvrc);
  322. return -EIO;
  323. }
  324. wait_for_completion(&we.com);
  325. if (we.rc != 0)
  326. return -EIO;
  327. return 0;
  328. }
  329. static int viocd_packet(struct cdrom_device_info *cdi,
  330. struct packet_command *cgc)
  331. {
  332. unsigned int buflen = cgc->buflen;
  333. int ret = -EIO;
  334. switch (cgc->cmd[0]) {
  335. case GPCMD_READ_DISC_INFO:
  336. {
  337. disc_information *di = (disc_information *)cgc->buffer;
  338. if (buflen >= 2) {
  339. di->disc_information_length = cpu_to_be16(1);
  340. ret = 0;
  341. }
  342. if (buflen >= 3)
  343. di->erasable =
  344. (cdi->ops->capability & ~cdi->mask
  345. & (CDC_DVD_RAM | CDC_RAM)) != 0;
  346. }
  347. break;
  348. case GPCMD_GET_CONFIGURATION:
  349. if (cgc->cmd[3] == CDF_RWRT) {
  350. struct rwrt_feature_desc *rfd = (struct rwrt_feature_desc *)(cgc->buffer + sizeof(struct feature_header));
  351. if ((buflen >=
  352. (sizeof(struct feature_header) + sizeof(*rfd))) &&
  353. (cdi->ops->capability & ~cdi->mask
  354. & (CDC_DVD_RAM | CDC_RAM))) {
  355. rfd->feature_code = cpu_to_be16(CDF_RWRT);
  356. rfd->curr = 1;
  357. ret = 0;
  358. }
  359. }
  360. break;
  361. default:
  362. if (cgc->sense) {
  363. /* indicate Unknown code */
  364. cgc->sense->sense_key = 0x05;
  365. cgc->sense->asc = 0x20;
  366. cgc->sense->ascq = 0x00;
  367. }
  368. break;
  369. }
  370. cgc->stat = ret;
  371. return ret;
  372. }
  373. static void restart_all_queues(int first_index)
  374. {
  375. int i;
  376. for (i = first_index + 1; i < viocd_numdev; i++)
  377. if (viocd_diskinfo[i].viocd_disk)
  378. blk_run_queue(viocd_diskinfo[i].viocd_disk->queue);
  379. for (i = 0; i <= first_index; i++)
  380. if (viocd_diskinfo[i].viocd_disk)
  381. blk_run_queue(viocd_diskinfo[i].viocd_disk->queue);
  382. }
  383. /* This routine handles incoming CD LP events */
  384. static void vio_handle_cd_event(struct HvLpEvent *event)
  385. {
  386. struct viocdlpevent *bevent;
  387. struct viocd_waitevent *pwe;
  388. struct disk_info *di;
  389. unsigned long flags;
  390. struct request *req;
  391. if (event == NULL)
  392. /* Notification that a partition went away! */
  393. return;
  394. /* First, we should NEVER get an int here...only acks */
  395. if (hvlpevent_is_int(event)) {
  396. printk(VIOCD_KERN_WARNING
  397. "Yikes! got an int in viocd event handler!\n");
  398. if (hvlpevent_need_ack(event)) {
  399. event->xRc = HvLpEvent_Rc_InvalidSubtype;
  400. HvCallEvent_ackLpEvent(event);
  401. }
  402. }
  403. bevent = (struct viocdlpevent *)event;
  404. switch (event->xSubtype & VIOMINOR_SUBTYPE_MASK) {
  405. case viocdopen:
  406. if (event->xRc == 0) {
  407. di = &viocd_diskinfo[bevent->disk];
  408. blk_queue_logical_block_size(di->viocd_disk->queue,
  409. bevent->block_size);
  410. set_capacity(di->viocd_disk,
  411. bevent->media_size *
  412. bevent->block_size / 512);
  413. }
  414. /* FALLTHROUGH !! */
  415. case viocdlockdoor:
  416. pwe = (struct viocd_waitevent *)event->xCorrelationToken;
  417. return_complete:
  418. pwe->rc = event->xRc;
  419. pwe->sub_result = bevent->sub_result;
  420. complete(&pwe->com);
  421. break;
  422. case viocdcheck:
  423. pwe = (struct viocd_waitevent *)event->xCorrelationToken;
  424. pwe->changed = bevent->flags;
  425. goto return_complete;
  426. case viocdclose:
  427. break;
  428. case viocdwrite:
  429. case viocdread:
  430. /*
  431. * Since this is running in interrupt mode, we need to
  432. * make sure we're not stepping on any global I/O operations
  433. */
  434. di = &viocd_diskinfo[bevent->disk];
  435. spin_lock_irqsave(&viocd_reqlock, flags);
  436. dma_unmap_single(di->dev, bevent->token, bevent->len,
  437. ((event->xSubtype & VIOMINOR_SUBTYPE_MASK) == viocdread)
  438. ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
  439. req = (struct request *)bevent->event.xCorrelationToken;
  440. rwreq--;
  441. if (event->xRc != HvLpEvent_Rc_Good) {
  442. const struct vio_error_entry *err =
  443. vio_lookup_rc(viocd_err_table,
  444. bevent->sub_result);
  445. printk(VIOCD_KERN_WARNING "request %p failed "
  446. "with rc %d:0x%04X: %s\n",
  447. req, event->xRc,
  448. bevent->sub_result, err->msg);
  449. __blk_end_request_all(req, -EIO);
  450. } else
  451. __blk_end_request_all(req, 0);
  452. /* restart handling of incoming requests */
  453. spin_unlock_irqrestore(&viocd_reqlock, flags);
  454. restart_all_queues(bevent->disk);
  455. break;
  456. default:
  457. printk(VIOCD_KERN_WARNING
  458. "message with invalid subtype %0x04X!\n",
  459. event->xSubtype & VIOMINOR_SUBTYPE_MASK);
  460. if (hvlpevent_need_ack(event)) {
  461. event->xRc = HvLpEvent_Rc_InvalidSubtype;
  462. HvCallEvent_ackLpEvent(event);
  463. }
  464. }
  465. }
  466. static int viocd_audio_ioctl(struct cdrom_device_info *cdi, unsigned int cmd,
  467. void *arg)
  468. {
  469. return -EINVAL;
  470. }
  471. static struct cdrom_device_ops viocd_dops = {
  472. .open = viocd_open,
  473. .release = viocd_release,
  474. .media_changed = viocd_media_changed,
  475. .lock_door = viocd_lock_door,
  476. .generic_packet = viocd_packet,
  477. .audio_ioctl = viocd_audio_ioctl,
  478. .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
  479. };
  480. static int find_capability(const char *type)
  481. {
  482. struct capability_entry *entry;
  483. for(entry = capability_table; entry->type; ++entry)
  484. if(!strncmp(entry->type, type, 4))
  485. break;
  486. return entry->capability;
  487. }
  488. static int viocd_probe(struct vio_dev *vdev, const struct vio_device_id *id)
  489. {
  490. struct gendisk *gendisk;
  491. int deviceno;
  492. struct disk_info *d;
  493. struct cdrom_device_info *c;
  494. struct request_queue *q;
  495. struct device_node *node = vdev->dev.archdata.of_node;
  496. deviceno = vdev->unit_address;
  497. if (deviceno >= VIOCD_MAX_CD)
  498. return -ENODEV;
  499. if (!node)
  500. return -ENODEV;
  501. if (deviceno >= viocd_numdev)
  502. viocd_numdev = deviceno + 1;
  503. d = &viocd_diskinfo[deviceno];
  504. d->rsrcname = of_get_property(node, "linux,vio_rsrcname", NULL);
  505. d->type = of_get_property(node, "linux,vio_type", NULL);
  506. d->model = of_get_property(node, "linux,vio_model", NULL);
  507. c = &d->viocd_info;
  508. c->ops = &viocd_dops;
  509. c->speed = 4;
  510. c->capacity = 1;
  511. c->handle = d;
  512. c->mask = ~find_capability(d->type);
  513. sprintf(c->name, VIOCD_DEVICE "%c", 'a' + deviceno);
  514. if (register_cdrom(c) != 0) {
  515. printk(VIOCD_KERN_WARNING "Cannot register viocd CD-ROM %s!\n",
  516. c->name);
  517. goto out;
  518. }
  519. printk(VIOCD_KERN_INFO "cd %s is iSeries resource %10.10s "
  520. "type %4.4s, model %3.3s\n",
  521. c->name, d->rsrcname, d->type, d->model);
  522. q = blk_init_queue(do_viocd_request, &viocd_reqlock);
  523. if (q == NULL) {
  524. printk(VIOCD_KERN_WARNING "Cannot allocate queue for %s!\n",
  525. c->name);
  526. goto out_unregister_cdrom;
  527. }
  528. gendisk = alloc_disk(1);
  529. if (gendisk == NULL) {
  530. printk(VIOCD_KERN_WARNING "Cannot create gendisk for %s!\n",
  531. c->name);
  532. goto out_cleanup_queue;
  533. }
  534. gendisk->major = VIOCD_MAJOR;
  535. gendisk->first_minor = deviceno;
  536. strncpy(gendisk->disk_name, c->name,
  537. sizeof(gendisk->disk_name));
  538. blk_queue_max_hw_segments(q, 1);
  539. blk_queue_max_phys_segments(q, 1);
  540. blk_queue_max_sectors(q, 4096 / 512);
  541. gendisk->queue = q;
  542. gendisk->fops = &viocd_fops;
  543. gendisk->flags = GENHD_FL_CD|GENHD_FL_REMOVABLE;
  544. set_capacity(gendisk, 0);
  545. gendisk->private_data = d;
  546. d->viocd_disk = gendisk;
  547. d->dev = &vdev->dev;
  548. gendisk->driverfs_dev = d->dev;
  549. add_disk(gendisk);
  550. return 0;
  551. out_cleanup_queue:
  552. blk_cleanup_queue(q);
  553. out_unregister_cdrom:
  554. unregister_cdrom(c);
  555. out:
  556. return -ENODEV;
  557. }
  558. static int viocd_remove(struct vio_dev *vdev)
  559. {
  560. struct disk_info *d = &viocd_diskinfo[vdev->unit_address];
  561. unregister_cdrom(&d->viocd_info);
  562. del_gendisk(d->viocd_disk);
  563. blk_cleanup_queue(d->viocd_disk->queue);
  564. put_disk(d->viocd_disk);
  565. return 0;
  566. }
  567. /**
  568. * viocd_device_table: Used by vio.c to match devices that we
  569. * support.
  570. */
  571. static struct vio_device_id viocd_device_table[] __devinitdata = {
  572. { "block", "IBM,iSeries-viocd" },
  573. { "", "" }
  574. };
  575. MODULE_DEVICE_TABLE(vio, viocd_device_table);
  576. static struct vio_driver viocd_driver = {
  577. .id_table = viocd_device_table,
  578. .probe = viocd_probe,
  579. .remove = viocd_remove,
  580. .driver = {
  581. .name = "viocd",
  582. .owner = THIS_MODULE,
  583. }
  584. };
  585. static int __init viocd_init(void)
  586. {
  587. int ret = 0;
  588. if (!firmware_has_feature(FW_FEATURE_ISERIES))
  589. return -ENODEV;
  590. if (viopath_hostLp == HvLpIndexInvalid) {
  591. vio_set_hostlp();
  592. /* If we don't have a host, bail out */
  593. if (viopath_hostLp == HvLpIndexInvalid)
  594. return -ENODEV;
  595. }
  596. printk(VIOCD_KERN_INFO "vers " VIOCD_VERS ", hosting partition %d\n",
  597. viopath_hostLp);
  598. if (register_blkdev(VIOCD_MAJOR, VIOCD_DEVICE) != 0) {
  599. printk(VIOCD_KERN_WARNING "Unable to get major %d for %s\n",
  600. VIOCD_MAJOR, VIOCD_DEVICE);
  601. return -EIO;
  602. }
  603. ret = viopath_open(viopath_hostLp, viomajorsubtype_cdio,
  604. MAX_CD_REQ + 2);
  605. if (ret) {
  606. printk(VIOCD_KERN_WARNING
  607. "error opening path to host partition %d\n",
  608. viopath_hostLp);
  609. goto out_unregister;
  610. }
  611. /* Initialize our request handler */
  612. vio_setHandler(viomajorsubtype_cdio, vio_handle_cd_event);
  613. spin_lock_init(&viocd_reqlock);
  614. ret = vio_register_driver(&viocd_driver);
  615. if (ret)
  616. goto out_free_info;
  617. proc_create("iSeries/viocd", S_IFREG|S_IRUGO, NULL,
  618. &proc_viocd_operations);
  619. return 0;
  620. out_free_info:
  621. vio_clearHandler(viomajorsubtype_cdio);
  622. viopath_close(viopath_hostLp, viomajorsubtype_cdio, MAX_CD_REQ + 2);
  623. out_unregister:
  624. unregister_blkdev(VIOCD_MAJOR, VIOCD_DEVICE);
  625. return ret;
  626. }
  627. static void __exit viocd_exit(void)
  628. {
  629. remove_proc_entry("iSeries/viocd", NULL);
  630. vio_unregister_driver(&viocd_driver);
  631. viopath_close(viopath_hostLp, viomajorsubtype_cdio, MAX_CD_REQ + 2);
  632. vio_clearHandler(viomajorsubtype_cdio);
  633. unregister_blkdev(VIOCD_MAJOR, VIOCD_DEVICE);
  634. }
  635. module_init(viocd_init);
  636. module_exit(viocd_exit);
  637. MODULE_LICENSE("GPL");