viocd.c 19 KB

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