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>
46 #include <asm/scatterlist.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>
52 #define VIOCD_DEVICE "iseries/vcd"
54 #define VIOCD_VERS "1.06"
56 #define VIOCD_KERN_WARNING KERN_WARNING "viocd: "
57 #define VIOCD_KERN_INFO KERN_INFO "viocd: "
60 * Should probably make this a module parameter....sigh
62 #define VIOCD_MAX_CD HVMAXARCHITECTEDVIRTUALCDROMS
64 static const struct vio_error_entry viocd_err_table[] = {
65 {0x0201, EINVAL, "Invalid Range"},
66 {0x0202, EINVAL, "Invalid Token"},
67 {0x0203, EIO, "DMA Error"},
68 {0x0204, EIO, "Use Error"},
69 {0x0205, EIO, "Release Error"},
70 {0x0206, EINVAL, "Invalid CD"},
71 {0x020C, EROFS, "Read Only Device"},
72 {0x020D, ENOMEDIUM, "Changed or Missing Volume (or Varied Off?)"},
73 {0x020E, EIO, "Optical System Error (Varied Off?)"},
74 {0x02FF, EIO, "Internal Error"},
75 {0x3010, EIO, "Changed Volume"},
76 {0xC100, EIO, "Optical System Error"},
81 * This is the structure we use to exchange info between driver and interrupt
84 struct viocd_waitevent {
85 struct completion com;
91 /* this is a lookup table for the true capabilities of a device */
92 struct capability_entry {
97 static struct capability_entry capability_table[] __initdata = {
98 { "6330", CDC_LOCK | CDC_DVD_RAM | CDC_RAM },
99 { "6331", CDC_LOCK | CDC_DVD_RAM | CDC_RAM },
100 { "6333", CDC_LOCK | CDC_DVD_RAM | CDC_RAM },
101 { "632A", CDC_LOCK | CDC_DVD_RAM | CDC_RAM },
102 { "6321", CDC_LOCK },
107 /* These are our internal structures for keeping track of devices */
108 static int viocd_numdev;
111 struct gendisk *viocd_disk;
112 struct cdrom_device_info viocd_info;
114 const char *rsrcname;
118 static struct disk_info viocd_diskinfo[VIOCD_MAX_CD];
120 #define DEVICE_NR(di) ((di) - &viocd_diskinfo[0])
122 static spinlock_t viocd_reqlock;
127 static int proc_viocd_show(struct seq_file *m, void *v)
131 for (i = 0; i < viocd_numdev; i++) {
132 seq_printf(m, "viocd device %d is iSeries resource %10.10s"
133 "type %4.4s, model %3.3s\n",
134 i, viocd_diskinfo[i].rsrcname,
135 viocd_diskinfo[i].type,
136 viocd_diskinfo[i].model);
141 static int proc_viocd_open(struct inode *inode, struct file *file)
143 return single_open(file, proc_viocd_show, NULL);
146 static const struct file_operations proc_viocd_operations = {
147 .open = proc_viocd_open,
150 .release = single_release,
153 static int viocd_blk_open(struct inode *inode, struct file *file)
155 struct disk_info *di = inode->i_bdev->bd_disk->private_data;
156 return cdrom_open(&di->viocd_info, inode, file);
159 static int viocd_blk_release(struct inode *inode, struct file *file)
161 struct disk_info *di = inode->i_bdev->bd_disk->private_data;
162 return cdrom_release(&di->viocd_info, file);
165 static int viocd_blk_ioctl(struct inode *inode, struct file *file,
166 unsigned cmd, unsigned long arg)
168 struct disk_info *di = inode->i_bdev->bd_disk->private_data;
169 return cdrom_ioctl(file, &di->viocd_info, inode, cmd, arg);
172 static int viocd_blk_media_changed(struct gendisk *disk)
174 struct disk_info *di = disk->private_data;
175 return cdrom_media_changed(&di->viocd_info);
178 struct block_device_operations viocd_fops = {
179 .owner = THIS_MODULE,
180 .open = viocd_blk_open,
181 .release = viocd_blk_release,
182 .ioctl = viocd_blk_ioctl,
183 .media_changed = viocd_blk_media_changed,
186 static int viocd_open(struct cdrom_device_info *cdi, int purpose)
188 struct disk_info *diskinfo = cdi->handle;
189 int device_no = DEVICE_NR(diskinfo);
191 struct viocd_waitevent we;
193 init_completion(&we.com);
194 hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
195 HvLpEvent_Type_VirtualIo,
196 viomajorsubtype_cdio | viocdopen,
197 HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck,
198 viopath_sourceinst(viopath_hostLp),
199 viopath_targetinst(viopath_hostLp),
200 (u64)&we, VIOVERSION << 16, ((u64)device_no << 48),
203 printk(VIOCD_KERN_WARNING
204 "bad rc on HvCallEvent_signalLpEventFast %d\n",
209 wait_for_completion(&we.com);
212 const struct vio_error_entry *err =
213 vio_lookup_rc(viocd_err_table, we.sub_result);
214 printk(VIOCD_KERN_WARNING "bad rc %d:0x%04X on open: %s\n",
215 we.rc, we.sub_result, err->msg);
222 static void viocd_release(struct cdrom_device_info *cdi)
224 int device_no = DEVICE_NR((struct disk_info *)cdi->handle);
227 hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
228 HvLpEvent_Type_VirtualIo,
229 viomajorsubtype_cdio | viocdclose,
230 HvLpEvent_AckInd_NoAck, HvLpEvent_AckType_ImmediateAck,
231 viopath_sourceinst(viopath_hostLp),
232 viopath_targetinst(viopath_hostLp), 0,
233 VIOVERSION << 16, ((u64)device_no << 48), 0, 0, 0);
235 printk(VIOCD_KERN_WARNING
236 "bad rc on HvCallEvent_signalLpEventFast %d\n",
240 /* Send a read or write request to OS/400 */
241 static int send_request(struct request *req)
244 struct disk_info *diskinfo = req->rq_disk->private_data;
249 struct scatterlist sg;
251 BUG_ON(req->nr_phys_segments > 1);
253 if (rq_data_dir(req) == READ) {
254 direction = DMA_FROM_DEVICE;
255 cmd = viomajorsubtype_cdio | viocdread;
257 direction = DMA_TO_DEVICE;
258 cmd = viomajorsubtype_cdio | viocdwrite;
261 if (blk_rq_map_sg(req->q, req, &sg) == 0) {
262 printk(VIOCD_KERN_WARNING
263 "error setting up scatter/gather list\n");
267 if (dma_map_sg(diskinfo->dev, &sg, 1, direction) == 0) {
268 printk(VIOCD_KERN_WARNING "error allocating sg tce\n");
271 dmaaddr = sg_dma_address(&sg);
272 len = sg_dma_len(&sg);
274 hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
275 HvLpEvent_Type_VirtualIo, cmd,
276 HvLpEvent_AckInd_DoAck,
277 HvLpEvent_AckType_ImmediateAck,
278 viopath_sourceinst(viopath_hostLp),
279 viopath_targetinst(viopath_hostLp),
280 (u64)req, VIOVERSION << 16,
281 ((u64)DEVICE_NR(diskinfo) << 48) | dmaaddr,
282 (u64)req->sector * 512, len, 0);
283 if (hvrc != HvLpEvent_Rc_Good) {
284 printk(VIOCD_KERN_WARNING "hv error on op %d\n", (int)hvrc);
291 static void viocd_end_request(struct request *req, int uptodate)
293 int nsectors = req->hard_nr_sectors;
296 * Make sure it's fully ended, and ensure that we process
297 * at least one sector.
299 if (blk_pc_request(req))
300 nsectors = (req->data_len + 511) >> 9;
304 if (end_that_request_first(req, uptodate, nsectors))
306 add_disk_randomness(req->rq_disk);
307 blkdev_dequeue_request(req);
308 end_that_request_last(req, uptodate);
313 static void do_viocd_request(struct request_queue *q)
317 while ((rwreq == 0) && ((req = elv_next_request(q)) != NULL)) {
318 if (!blk_fs_request(req))
319 viocd_end_request(req, 0);
320 else if (send_request(req) < 0) {
321 printk(VIOCD_KERN_WARNING
322 "unable to send message to OS/400!");
323 viocd_end_request(req, 0);
329 static int viocd_media_changed(struct cdrom_device_info *cdi, int disc_nr)
331 struct viocd_waitevent we;
333 int device_no = DEVICE_NR((struct disk_info *)cdi->handle);
335 init_completion(&we.com);
337 /* Send the open event to OS/400 */
338 hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
339 HvLpEvent_Type_VirtualIo,
340 viomajorsubtype_cdio | viocdcheck,
341 HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck,
342 viopath_sourceinst(viopath_hostLp),
343 viopath_targetinst(viopath_hostLp),
344 (u64)&we, VIOVERSION << 16, ((u64)device_no << 48),
347 printk(VIOCD_KERN_WARNING "bad rc on HvCallEvent_signalLpEventFast %d\n",
352 wait_for_completion(&we.com);
354 /* Check the return code. If bad, assume no change */
356 const struct vio_error_entry *err =
357 vio_lookup_rc(viocd_err_table, we.sub_result);
358 printk(VIOCD_KERN_WARNING
359 "bad rc %d:0x%04X on check_change: %s; Assuming no change\n",
360 we.rc, we.sub_result, err->msg);
367 static int viocd_lock_door(struct cdrom_device_info *cdi, int locking)
370 u64 device_no = DEVICE_NR((struct disk_info *)cdi->handle);
371 /* NOTE: flags is 1 or 0 so it won't overwrite the device_no */
372 u64 flags = !!locking;
373 struct viocd_waitevent we;
375 init_completion(&we.com);
377 /* Send the lockdoor event to OS/400 */
378 hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
379 HvLpEvent_Type_VirtualIo,
380 viomajorsubtype_cdio | viocdlockdoor,
381 HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck,
382 viopath_sourceinst(viopath_hostLp),
383 viopath_targetinst(viopath_hostLp),
384 (u64)&we, VIOVERSION << 16,
385 (device_no << 48) | (flags << 32), 0, 0, 0);
387 printk(VIOCD_KERN_WARNING "bad rc on HvCallEvent_signalLpEventFast %d\n",
392 wait_for_completion(&we.com);
399 static int viocd_packet(struct cdrom_device_info *cdi,
400 struct packet_command *cgc)
402 unsigned int buflen = cgc->buflen;
405 switch (cgc->cmd[0]) {
406 case GPCMD_READ_DISC_INFO:
408 disc_information *di = (disc_information *)cgc->buffer;
411 di->disc_information_length = cpu_to_be16(1);
416 (cdi->ops->capability & ~cdi->mask
417 & (CDC_DVD_RAM | CDC_RAM)) != 0;
420 case GPCMD_GET_CONFIGURATION:
421 if (cgc->cmd[3] == CDF_RWRT) {
422 struct rwrt_feature_desc *rfd = (struct rwrt_feature_desc *)(cgc->buffer + sizeof(struct feature_header));
425 (sizeof(struct feature_header) + sizeof(*rfd))) &&
426 (cdi->ops->capability & ~cdi->mask
427 & (CDC_DVD_RAM | CDC_RAM))) {
428 rfd->feature_code = cpu_to_be16(CDF_RWRT);
436 /* indicate Unknown code */
437 cgc->sense->sense_key = 0x05;
438 cgc->sense->asc = 0x20;
439 cgc->sense->ascq = 0x00;
448 static void restart_all_queues(int first_index)
452 for (i = first_index + 1; i < viocd_numdev; i++)
453 if (viocd_diskinfo[i].viocd_disk)
454 blk_run_queue(viocd_diskinfo[i].viocd_disk->queue);
455 for (i = 0; i <= first_index; i++)
456 if (viocd_diskinfo[i].viocd_disk)
457 blk_run_queue(viocd_diskinfo[i].viocd_disk->queue);
460 /* This routine handles incoming CD LP events */
461 static void vio_handle_cd_event(struct HvLpEvent *event)
463 struct viocdlpevent *bevent;
464 struct viocd_waitevent *pwe;
465 struct disk_info *di;
471 /* Notification that a partition went away! */
473 /* First, we should NEVER get an int here...only acks */
474 if (hvlpevent_is_int(event)) {
475 printk(VIOCD_KERN_WARNING
476 "Yikes! got an int in viocd event handler!\n");
477 if (hvlpevent_need_ack(event)) {
478 event->xRc = HvLpEvent_Rc_InvalidSubtype;
479 HvCallEvent_ackLpEvent(event);
483 bevent = (struct viocdlpevent *)event;
485 switch (event->xSubtype & VIOMINOR_SUBTYPE_MASK) {
487 if (event->xRc == 0) {
488 di = &viocd_diskinfo[bevent->disk];
489 blk_queue_hardsect_size(di->viocd_disk->queue,
491 set_capacity(di->viocd_disk,
493 bevent->block_size / 512);
497 pwe = (struct viocd_waitevent *)event->xCorrelationToken;
499 pwe->rc = event->xRc;
500 pwe->sub_result = bevent->sub_result;
505 pwe = (struct viocd_waitevent *)event->xCorrelationToken;
506 pwe->changed = bevent->flags;
507 goto return_complete;
515 * Since this is running in interrupt mode, we need to
516 * make sure we're not stepping on any global I/O operations
518 di = &viocd_diskinfo[bevent->disk];
519 spin_lock_irqsave(&viocd_reqlock, flags);
520 dma_unmap_single(di->dev, bevent->token, bevent->len,
521 ((event->xSubtype & VIOMINOR_SUBTYPE_MASK) == viocdread)
522 ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
523 req = (struct request *)bevent->event.xCorrelationToken;
526 if (event->xRc != HvLpEvent_Rc_Good) {
527 const struct vio_error_entry *err =
528 vio_lookup_rc(viocd_err_table,
530 printk(VIOCD_KERN_WARNING "request %p failed "
531 "with rc %d:0x%04X: %s\n",
533 bevent->sub_result, err->msg);
534 viocd_end_request(req, 0);
536 viocd_end_request(req, 1);
538 /* restart handling of incoming requests */
539 spin_unlock_irqrestore(&viocd_reqlock, flags);
540 restart_all_queues(bevent->disk);
544 printk(VIOCD_KERN_WARNING
545 "message with invalid subtype %0x04X!\n",
546 event->xSubtype & VIOMINOR_SUBTYPE_MASK);
547 if (hvlpevent_need_ack(event)) {
548 event->xRc = HvLpEvent_Rc_InvalidSubtype;
549 HvCallEvent_ackLpEvent(event);
554 static struct cdrom_device_ops viocd_dops = {
556 .release = viocd_release,
557 .media_changed = viocd_media_changed,
558 .lock_door = viocd_lock_door,
559 .generic_packet = viocd_packet,
560 .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
563 static int __init find_capability(const char *type)
565 struct capability_entry *entry;
567 for(entry = capability_table; entry->type; ++entry)
568 if(!strncmp(entry->type, type, 4))
570 return entry->capability;
573 static int viocd_probe(struct vio_dev *vdev, const struct vio_device_id *id)
575 struct gendisk *gendisk;
578 struct cdrom_device_info *c;
579 struct request_queue *q;
580 struct device_node *node = vdev->dev.archdata.of_node;
582 deviceno = vdev->unit_address;
583 if (deviceno > VIOCD_MAX_CD)
588 if (deviceno >= viocd_numdev)
589 viocd_numdev = deviceno + 1;
591 d = &viocd_diskinfo[deviceno];
592 d->rsrcname = of_get_property(node, "linux,vio_rsrcname", NULL);
593 d->type = of_get_property(node, "linux,vio_type", NULL);
594 d->model = of_get_property(node, "linux,vio_model", NULL);
598 c->ops = &viocd_dops;
602 c->mask = ~find_capability(d->type);
603 sprintf(c->name, VIOCD_DEVICE "%c", 'a' + deviceno);
605 if (register_cdrom(c) != 0) {
606 printk(VIOCD_KERN_WARNING "Cannot register viocd CD-ROM %s!\n",
610 printk(VIOCD_KERN_INFO "cd %s is iSeries resource %10.10s "
611 "type %4.4s, model %3.3s\n",
612 c->name, d->rsrcname, d->type, d->model);
613 q = blk_init_queue(do_viocd_request, &viocd_reqlock);
615 printk(VIOCD_KERN_WARNING "Cannot allocate queue for %s!\n",
617 goto out_unregister_cdrom;
619 gendisk = alloc_disk(1);
620 if (gendisk == NULL) {
621 printk(VIOCD_KERN_WARNING "Cannot create gendisk for %s!\n",
623 goto out_cleanup_queue;
625 gendisk->major = VIOCD_MAJOR;
626 gendisk->first_minor = deviceno;
627 strncpy(gendisk->disk_name, c->name,
628 sizeof(gendisk->disk_name));
629 blk_queue_max_hw_segments(q, 1);
630 blk_queue_max_phys_segments(q, 1);
631 blk_queue_max_sectors(q, 4096 / 512);
633 gendisk->fops = &viocd_fops;
634 gendisk->flags = GENHD_FL_CD|GENHD_FL_REMOVABLE;
635 set_capacity(gendisk, 0);
636 gendisk->private_data = d;
637 d->viocd_disk = gendisk;
639 gendisk->driverfs_dev = d->dev;
644 blk_cleanup_queue(q);
645 out_unregister_cdrom:
651 static int viocd_remove(struct vio_dev *vdev)
653 struct disk_info *d = &viocd_diskinfo[vdev->unit_address];
655 if (unregister_cdrom(&d->viocd_info) != 0)
656 printk(VIOCD_KERN_WARNING
657 "Cannot unregister viocd CD-ROM %s!\n",
659 del_gendisk(d->viocd_disk);
660 blk_cleanup_queue(d->viocd_disk->queue);
661 put_disk(d->viocd_disk);
666 * viocd_device_table: Used by vio.c to match devices that we
669 static struct vio_device_id viocd_device_table[] __devinitdata = {
670 { "block", "IBM,iSeries-viocd" },
673 MODULE_DEVICE_TABLE(vio, viocd_device_table);
675 static struct vio_driver viocd_driver = {
676 .id_table = viocd_device_table,
677 .probe = viocd_probe,
678 .remove = viocd_remove,
681 .owner = THIS_MODULE,
685 static int __init viocd_init(void)
687 struct proc_dir_entry *e;
690 if (!firmware_has_feature(FW_FEATURE_ISERIES))
693 if (viopath_hostLp == HvLpIndexInvalid) {
695 /* If we don't have a host, bail out */
696 if (viopath_hostLp == HvLpIndexInvalid)
700 printk(VIOCD_KERN_INFO "vers " VIOCD_VERS ", hosting partition %d\n",
703 if (register_blkdev(VIOCD_MAJOR, VIOCD_DEVICE) != 0) {
704 printk(VIOCD_KERN_WARNING "Unable to get major %d for %s\n",
705 VIOCD_MAJOR, VIOCD_DEVICE);
709 ret = viopath_open(viopath_hostLp, viomajorsubtype_cdio,
712 printk(VIOCD_KERN_WARNING
713 "error opening path to host partition %d\n",
718 /* Initialize our request handler */
719 vio_setHandler(viomajorsubtype_cdio, vio_handle_cd_event);
721 spin_lock_init(&viocd_reqlock);
723 ret = vio_register_driver(&viocd_driver);
727 e = create_proc_entry("iSeries/viocd", S_IFREG|S_IRUGO, NULL);
729 e->owner = THIS_MODULE;
730 e->proc_fops = &proc_viocd_operations;
736 vio_clearHandler(viomajorsubtype_cdio);
737 viopath_close(viopath_hostLp, viomajorsubtype_cdio, MAX_CD_REQ + 2);
739 unregister_blkdev(VIOCD_MAJOR, VIOCD_DEVICE);
743 static void __exit viocd_exit(void)
745 remove_proc_entry("iSeries/viocd", NULL);
746 vio_unregister_driver(&viocd_driver);
747 viopath_close(viopath_hostLp, viomajorsubtype_cdio, MAX_CD_REQ + 2);
748 vio_clearHandler(viomajorsubtype_cdio);
749 unregister_blkdev(VIOCD_MAJOR, VIOCD_DEVICE);
752 module_init(viocd_init);
753 module_exit(viocd_exit);
754 MODULE_LICENSE("GPL");