2 * drivers/cdrom/viocd.c
4 * iSeries Virtual CD Rom
6 * Authors: Dave Boutcher <boutcher@us.ibm.com>
7 * Ryan Arnold <ryanarn@us.ibm.com>
8 * Colin Devilbiss <devilbis@us.ibm.com>
9 * Stephen Rothwell <sfr@au1.ibm.com>
11 * (C) Copyright 2000-2004 IBM Corporation
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.
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.
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
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.
30 * All operations are performed by sending messages back and forth to
31 * the OS/400 partition.
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>
47 #include <asm/scatterlist.h>
48 #include <asm/iSeries/HvTypes.h>
49 #include <asm/iSeries/HvLpEvent.h>
50 #include <asm/iSeries/vio.h>
52 #define VIOCD_DEVICE "iseries/vcd"
53 #define VIOCD_DEVICE_DEVFS "iseries/vcd"
55 #define VIOCD_VERS "1.06"
57 #define VIOCD_KERN_WARNING KERN_WARNING "viocd: "
58 #define VIOCD_KERN_INFO KERN_INFO "viocd: "
61 struct HvLpEvent event;
68 u64 offset; /* On open, max number of disks */
69 u64 len; /* On open, size of the disk */
70 u32 block_size; /* Only set on open */
71 u32 media_size; /* Only set on open */
79 viocdlockdoor = 0x0005,
80 viocdgetinfo = 0x0006,
85 * Should probably make this a module parameter....sigh
87 #define VIOCD_MAX_CD 8
89 static const struct vio_error_entry viocd_err_table[] = {
90 {0x0201, EINVAL, "Invalid Range"},
91 {0x0202, EINVAL, "Invalid Token"},
92 {0x0203, EIO, "DMA Error"},
93 {0x0204, EIO, "Use Error"},
94 {0x0205, EIO, "Release Error"},
95 {0x0206, EINVAL, "Invalid CD"},
96 {0x020C, EROFS, "Read Only Device"},
97 {0x020D, ENOMEDIUM, "Changed or Missing Volume (or Varied Off?)"},
98 {0x020E, EIO, "Optical System Error (Varied Off?)"},
99 {0x02FF, EIO, "Internal Error"},
100 {0x3010, EIO, "Changed Volume"},
101 {0xC100, EIO, "Optical System Error"},
106 * This is the structure we use to exchange info between driver and interrupt
109 struct viocd_waitevent {
110 struct completion com;
116 /* this is a lookup table for the true capabilities of a device */
117 struct capability_entry {
122 static struct capability_entry capability_table[] __initdata = {
123 { "6330", CDC_LOCK | CDC_DVD_RAM },
124 { "6321", CDC_LOCK },
129 /* These are our internal structures for keeping track of devices */
130 static int viocd_numdev;
138 * This needs to be allocated since it is passed to the
139 * Hypervisor and we may be a module.
141 static struct cdrom_info *viocd_unitinfo;
142 static dma_addr_t unitinfo_dmaaddr;
145 struct gendisk *viocd_disk;
146 struct cdrom_device_info viocd_info;
148 static struct disk_info viocd_diskinfo[VIOCD_MAX_CD];
150 #define DEVICE_NR(di) ((di) - &viocd_diskinfo[0])
152 static request_queue_t *viocd_queue;
153 static spinlock_t viocd_reqlock;
158 static int proc_viocd_show(struct seq_file *m, void *v)
162 for (i = 0; i < viocd_numdev; i++) {
163 seq_printf(m, "viocd device %d is iSeries resource %10.10s"
164 "type %4.4s, model %3.3s\n",
165 i, viocd_unitinfo[i].rsrcname,
166 viocd_unitinfo[i].type,
167 viocd_unitinfo[i].model);
172 static int proc_viocd_open(struct inode *inode, struct file *file)
174 return single_open(file, proc_viocd_show, NULL);
177 static struct file_operations proc_viocd_operations = {
178 .open = proc_viocd_open,
181 .release = single_release,
184 static int viocd_blk_open(struct inode *inode, struct file *file)
186 struct disk_info *di = inode->i_bdev->bd_disk->private_data;
187 return cdrom_open(&di->viocd_info, inode, file);
190 static int viocd_blk_release(struct inode *inode, struct file *file)
192 struct disk_info *di = inode->i_bdev->bd_disk->private_data;
193 return cdrom_release(&di->viocd_info, file);
196 static int viocd_blk_ioctl(struct inode *inode, struct file *file,
197 unsigned cmd, unsigned long arg)
199 struct disk_info *di = inode->i_bdev->bd_disk->private_data;
200 return cdrom_ioctl(&di->viocd_info, inode, cmd, arg);
203 static int viocd_blk_media_changed(struct gendisk *disk)
205 struct disk_info *di = disk->private_data;
206 return cdrom_media_changed(&di->viocd_info);
209 struct block_device_operations viocd_fops = {
210 .owner = THIS_MODULE,
211 .open = viocd_blk_open,
212 .release = viocd_blk_release,
213 .ioctl = viocd_blk_ioctl,
214 .media_changed = viocd_blk_media_changed,
217 /* Get info on CD devices from OS/400 */
218 static void __init get_viocd_info(void)
222 struct viocd_waitevent we;
224 viocd_unitinfo = dma_alloc_coherent(iSeries_vio_dev,
225 sizeof(*viocd_unitinfo) * VIOCD_MAX_CD,
226 &unitinfo_dmaaddr, GFP_ATOMIC);
227 if (viocd_unitinfo == NULL) {
228 printk(VIOCD_KERN_WARNING "error allocating unitinfo\n");
232 memset(viocd_unitinfo, 0, sizeof(*viocd_unitinfo) * VIOCD_MAX_CD);
234 init_completion(&we.com);
236 hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
237 HvLpEvent_Type_VirtualIo,
238 viomajorsubtype_cdio | viocdgetinfo,
239 HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck,
240 viopath_sourceinst(viopath_hostLp),
241 viopath_targetinst(viopath_hostLp),
242 (u64)&we, VIOVERSION << 16, unitinfo_dmaaddr, 0,
243 sizeof(*viocd_unitinfo) * VIOCD_MAX_CD, 0);
244 if (hvrc != HvLpEvent_Rc_Good) {
245 printk(VIOCD_KERN_WARNING "cdrom error sending event. rc %d\n",
250 wait_for_completion(&we.com);
253 const struct vio_error_entry *err =
254 vio_lookup_rc(viocd_err_table, we.sub_result);
255 printk(VIOCD_KERN_WARNING "bad rc %d:0x%04X on getinfo: %s\n",
256 we.rc, we.sub_result, err->msg);
260 for (i = 0; (i < VIOCD_MAX_CD) && viocd_unitinfo[i].rsrcname[0]; i++)
266 dma_free_coherent(iSeries_vio_dev,
267 sizeof(*viocd_unitinfo) * VIOCD_MAX_CD,
268 viocd_unitinfo, unitinfo_dmaaddr);
269 viocd_unitinfo = NULL;
272 static int viocd_open(struct cdrom_device_info *cdi, int purpose)
274 struct disk_info *diskinfo = cdi->handle;
275 int device_no = DEVICE_NR(diskinfo);
277 struct viocd_waitevent we;
279 init_completion(&we.com);
280 hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
281 HvLpEvent_Type_VirtualIo,
282 viomajorsubtype_cdio | viocdopen,
283 HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck,
284 viopath_sourceinst(viopath_hostLp),
285 viopath_targetinst(viopath_hostLp),
286 (u64)&we, VIOVERSION << 16, ((u64)device_no << 48),
289 printk(VIOCD_KERN_WARNING
290 "bad rc on HvCallEvent_signalLpEventFast %d\n",
295 wait_for_completion(&we.com);
298 const struct vio_error_entry *err =
299 vio_lookup_rc(viocd_err_table, we.sub_result);
300 printk(VIOCD_KERN_WARNING "bad rc %d:0x%04X on open: %s\n",
301 we.rc, we.sub_result, err->msg);
308 static void viocd_release(struct cdrom_device_info *cdi)
310 int device_no = DEVICE_NR((struct disk_info *)cdi->handle);
313 hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
314 HvLpEvent_Type_VirtualIo,
315 viomajorsubtype_cdio | viocdclose,
316 HvLpEvent_AckInd_NoAck, HvLpEvent_AckType_ImmediateAck,
317 viopath_sourceinst(viopath_hostLp),
318 viopath_targetinst(viopath_hostLp), 0,
319 VIOVERSION << 16, ((u64)device_no << 48), 0, 0, 0);
321 printk(VIOCD_KERN_WARNING
322 "bad rc on HvCallEvent_signalLpEventFast %d\n",
326 /* Send a read or write request to OS/400 */
327 static int send_request(struct request *req)
330 struct disk_info *diskinfo = req->rq_disk->private_data;
333 struct scatterlist sg;
335 BUG_ON(req->nr_phys_segments > 1);
336 BUG_ON(rq_data_dir(req) != READ);
338 if (blk_rq_map_sg(req->q, req, &sg) == 0) {
339 printk(VIOCD_KERN_WARNING
340 "error setting up scatter/gather list\n");
344 if (dma_map_sg(iSeries_vio_dev, &sg, 1, DMA_FROM_DEVICE) == 0) {
345 printk(VIOCD_KERN_WARNING "error allocating sg tce\n");
348 dmaaddr = sg_dma_address(&sg);
349 len = sg_dma_len(&sg);
351 hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
352 HvLpEvent_Type_VirtualIo,
353 viomajorsubtype_cdio | viocdread,
354 HvLpEvent_AckInd_DoAck,
355 HvLpEvent_AckType_ImmediateAck,
356 viopath_sourceinst(viopath_hostLp),
357 viopath_targetinst(viopath_hostLp),
358 (u64)req, VIOVERSION << 16,
359 ((u64)DEVICE_NR(diskinfo) << 48) | dmaaddr,
360 (u64)req->sector * 512, len, 0);
361 if (hvrc != HvLpEvent_Rc_Good) {
362 printk(VIOCD_KERN_WARNING "hv error on op %d\n", (int)hvrc);
372 static void do_viocd_request(request_queue_t *q)
376 while ((rwreq == 0) && ((req = elv_next_request(q)) != NULL)) {
377 if (!blk_fs_request(req))
379 else if (send_request(req) < 0) {
380 printk(VIOCD_KERN_WARNING
381 "unable to send message to OS/400!");
388 static int viocd_media_changed(struct cdrom_device_info *cdi, int disc_nr)
390 struct viocd_waitevent we;
392 int device_no = DEVICE_NR((struct disk_info *)cdi->handle);
394 init_completion(&we.com);
396 /* Send the open event to OS/400 */
397 hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
398 HvLpEvent_Type_VirtualIo,
399 viomajorsubtype_cdio | viocdcheck,
400 HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck,
401 viopath_sourceinst(viopath_hostLp),
402 viopath_targetinst(viopath_hostLp),
403 (u64)&we, VIOVERSION << 16, ((u64)device_no << 48),
406 printk(VIOCD_KERN_WARNING "bad rc on HvCallEvent_signalLpEventFast %d\n",
411 wait_for_completion(&we.com);
413 /* Check the return code. If bad, assume no change */
415 const struct vio_error_entry *err =
416 vio_lookup_rc(viocd_err_table, we.sub_result);
417 printk(VIOCD_KERN_WARNING
418 "bad rc %d:0x%04X on check_change: %s; Assuming no change\n",
419 we.rc, we.sub_result, err->msg);
426 static int viocd_lock_door(struct cdrom_device_info *cdi, int locking)
429 u64 device_no = DEVICE_NR((struct disk_info *)cdi->handle);
430 /* NOTE: flags is 1 or 0 so it won't overwrite the device_no */
431 u64 flags = !!locking;
432 struct viocd_waitevent we;
434 init_completion(&we.com);
436 /* Send the lockdoor event to OS/400 */
437 hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
438 HvLpEvent_Type_VirtualIo,
439 viomajorsubtype_cdio | viocdlockdoor,
440 HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck,
441 viopath_sourceinst(viopath_hostLp),
442 viopath_targetinst(viopath_hostLp),
443 (u64)&we, VIOVERSION << 16,
444 (device_no << 48) | (flags << 32), 0, 0, 0);
446 printk(VIOCD_KERN_WARNING "bad rc on HvCallEvent_signalLpEventFast %d\n",
451 wait_for_completion(&we.com);
458 /* This routine handles incoming CD LP events */
459 static void vio_handle_cd_event(struct HvLpEvent *event)
461 struct viocdlpevent *bevent;
462 struct viocd_waitevent *pwe;
463 struct disk_info *di;
469 /* Notification that a partition went away! */
471 /* First, we should NEVER get an int here...only acks */
472 if (event->xFlags.xFunction == HvLpEvent_Function_Int) {
473 printk(VIOCD_KERN_WARNING
474 "Yikes! got an int in viocd event handler!\n");
475 if (event->xFlags.xAckInd == HvLpEvent_AckInd_DoAck) {
476 event->xRc = HvLpEvent_Rc_InvalidSubtype;
477 HvCallEvent_ackLpEvent(event);
481 bevent = (struct viocdlpevent *)event;
483 switch (event->xSubtype & VIOMINOR_SUBTYPE_MASK) {
485 if (event->xRc == 0) {
486 di = &viocd_diskinfo[bevent->disk];
487 blk_queue_hardsect_size(viocd_queue,
489 set_capacity(di->viocd_disk,
491 bevent->block_size / 512);
496 pwe = (struct viocd_waitevent *)event->xCorrelationToken;
498 pwe->rc = event->xRc;
499 pwe->sub_result = bevent->sub_result;
504 pwe = (struct viocd_waitevent *)event->xCorrelationToken;
505 pwe->changed = bevent->flags;
506 goto return_complete;
513 * Since this is running in interrupt mode, we need to
514 * make sure we're not stepping on any global I/O operations
516 spin_lock_irqsave(&viocd_reqlock, flags);
517 dma_unmap_single(iSeries_vio_dev, bevent->token, bevent->len,
519 req = (struct request *)bevent->event.xCorrelationToken;
522 if (event->xRc != HvLpEvent_Rc_Good) {
523 const struct vio_error_entry *err =
524 vio_lookup_rc(viocd_err_table,
526 printk(VIOCD_KERN_WARNING "request %p failed "
527 "with rc %d:0x%04X: %s\n",
529 bevent->sub_result, err->msg);
534 /* restart handling of incoming requests */
535 spin_unlock_irqrestore(&viocd_reqlock, flags);
536 blk_run_queue(viocd_queue);
540 printk(VIOCD_KERN_WARNING
541 "message with invalid subtype %0x04X!\n",
542 event->xSubtype & VIOMINOR_SUBTYPE_MASK);
543 if (event->xFlags.xAckInd == HvLpEvent_AckInd_DoAck) {
544 event->xRc = HvLpEvent_Rc_InvalidSubtype;
545 HvCallEvent_ackLpEvent(event);
550 static struct cdrom_device_ops viocd_dops = {
552 .release = viocd_release,
553 .media_changed = viocd_media_changed,
554 .lock_door = viocd_lock_door,
555 .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_IOCTLS | CDC_DRIVE_STATUS | CDC_GENERIC_PACKET | CDC_CD_R | CDC_CD_RW | CDC_DVD | CDC_DVD_R | CDC_DVD_RAM
558 static int __init find_capability(const char *type)
560 struct capability_entry *entry;
562 for(entry = capability_table; entry->type; ++entry)
563 if(!strncmp(entry->type, type, 4))
565 return entry->capability;
568 static int __init viocd_init(void)
570 struct gendisk *gendisk;
573 struct proc_dir_entry *e;
575 if (viopath_hostLp == HvLpIndexInvalid) {
577 /* If we don't have a host, bail out */
578 if (viopath_hostLp == HvLpIndexInvalid)
582 printk(VIOCD_KERN_INFO "vers " VIOCD_VERS ", hosting partition %d\n",
585 if (register_blkdev(VIOCD_MAJOR, VIOCD_DEVICE) != 0) {
586 printk(VIOCD_KERN_WARNING
587 "Unable to get major %d for %s\n",
588 VIOCD_MAJOR, VIOCD_DEVICE);
592 ret = viopath_open(viopath_hostLp, viomajorsubtype_cdio,
595 printk(VIOCD_KERN_WARNING
596 "error opening path to host partition %d\n",
601 /* Initialize our request handler */
602 vio_setHandler(viomajorsubtype_cdio, vio_handle_cd_event);
605 if (viocd_numdev == 0)
609 spin_lock_init(&viocd_reqlock);
610 viocd_queue = blk_init_queue(do_viocd_request, &viocd_reqlock);
611 if (viocd_queue == NULL)
613 blk_queue_max_hw_segments(viocd_queue, 1);
614 blk_queue_max_phys_segments(viocd_queue, 1);
615 blk_queue_max_sectors(viocd_queue, 4096 / 512);
617 /* initialize units */
618 for (deviceno = 0; deviceno < viocd_numdev; deviceno++) {
619 struct disk_info *d = &viocd_diskinfo[deviceno];
620 struct cdrom_device_info *c = &d->viocd_info;
621 struct cdrom_info *ci = &viocd_unitinfo[deviceno];
623 c->ops = &viocd_dops;
627 c->mask = ~find_capability(ci->type);
628 sprintf(c->name, VIOCD_DEVICE "%c", 'a' + deviceno);
630 if (register_cdrom(c) != 0) {
631 printk(VIOCD_KERN_WARNING
632 "Cannot register viocd CD-ROM %s!\n",
636 printk(VIOCD_KERN_INFO "cd %s is iSeries resource %10.10s "
637 "type %4.4s, model %3.3s\n",
638 c->name, ci->rsrcname, ci->type, ci->model);
639 gendisk = alloc_disk(1);
640 if (gendisk == NULL) {
641 printk(VIOCD_KERN_WARNING
642 "Cannot create gendisk for %s!\n",
647 gendisk->major = VIOCD_MAJOR;
648 gendisk->first_minor = deviceno;
649 strncpy(gendisk->disk_name, c->name,
650 sizeof(gendisk->disk_name));
651 snprintf(gendisk->devfs_name, sizeof(gendisk->devfs_name),
652 VIOCD_DEVICE_DEVFS "%d", deviceno);
653 gendisk->queue = viocd_queue;
654 gendisk->fops = &viocd_fops;
655 gendisk->flags = GENHD_FL_CD|GENHD_FL_REMOVABLE;
656 set_capacity(gendisk, 0);
657 gendisk->private_data = d;
658 d->viocd_disk = gendisk;
662 e = create_proc_entry("iSeries/viocd", S_IFREG|S_IRUGO, NULL);
664 e->owner = THIS_MODULE;
665 e->proc_fops = &proc_viocd_operations;
671 vio_clearHandler(viomajorsubtype_cdio);
672 viopath_close(viopath_hostLp, viomajorsubtype_cdio, MAX_CD_REQ + 2);
674 unregister_blkdev(VIOCD_MAJOR, VIOCD_DEVICE);
678 static void __exit viocd_exit(void)
682 remove_proc_entry("iSeries/viocd", NULL);
683 for (deviceno = 0; deviceno < viocd_numdev; deviceno++) {
684 struct disk_info *d = &viocd_diskinfo[deviceno];
685 if (unregister_cdrom(&d->viocd_info) != 0)
686 printk(VIOCD_KERN_WARNING
687 "Cannot unregister viocd CD-ROM %s!\n",
689 del_gendisk(d->viocd_disk);
690 put_disk(d->viocd_disk);
692 blk_cleanup_queue(viocd_queue);
693 if (viocd_unitinfo != NULL)
694 dma_free_coherent(iSeries_vio_dev,
695 sizeof(*viocd_unitinfo) * VIOCD_MAX_CD,
696 viocd_unitinfo, unitinfo_dmaaddr);
697 viopath_close(viopath_hostLp, viomajorsubtype_cdio, MAX_CD_REQ + 2);
698 vio_clearHandler(viomajorsubtype_cdio);
699 unregister_blkdev(VIOCD_MAJOR, VIOCD_DEVICE);
702 module_init(viocd_init);
703 module_exit(viocd_exit);
704 MODULE_LICENSE("GPL");