b88fdebe77f66b9dbdfacfa957945d7ab962055b
[safe/jmp/linux-2.6] / drivers / cdrom / viocd.c
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 <sfr@au1.ibm.com>
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
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
45 #include <asm/vio.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>
51
52 #define VIOCD_DEVICE                    "iseries/vcd"
53
54 #define VIOCD_VERS "1.06"
55
56 #define VIOCD_KERN_WARNING              KERN_WARNING "viocd: "
57 #define VIOCD_KERN_INFO                 KERN_INFO "viocd: "
58
59 struct viocdlpevent {
60         struct HvLpEvent        event;
61         u32                     reserved;
62         u16                     version;
63         u16                     sub_result;
64         u16                     disk;
65         u16                     flags;
66         u32                     token;
67         u64                     offset;         /* On open, max number of disks */
68         u64                     len;            /* On open, size of the disk */
69         u32                     block_size;     /* Only set on open */
70         u32                     media_size;     /* Only set on open */
71 };
72
73 enum viocdsubtype {
74         viocdopen = 0x0001,
75         viocdclose = 0x0002,
76         viocdread = 0x0003,
77         viocdwrite = 0x0004,
78         viocdlockdoor = 0x0005,
79         viocdgetinfo = 0x0006,
80         viocdcheck = 0x0007
81 };
82
83 /*
84  * Should probably make this a module parameter....sigh
85  */
86 #define VIOCD_MAX_CD    HVMAXARCHITECTEDVIRTUALCDROMS
87
88 static const struct vio_error_entry viocd_err_table[] = {
89         {0x0201, EINVAL, "Invalid Range"},
90         {0x0202, EINVAL, "Invalid Token"},
91         {0x0203, EIO, "DMA Error"},
92         {0x0204, EIO, "Use Error"},
93         {0x0205, EIO, "Release Error"},
94         {0x0206, EINVAL, "Invalid CD"},
95         {0x020C, EROFS, "Read Only Device"},
96         {0x020D, ENOMEDIUM, "Changed or Missing Volume (or Varied Off?)"},
97         {0x020E, EIO, "Optical System Error (Varied Off?)"},
98         {0x02FF, EIO, "Internal Error"},
99         {0x3010, EIO, "Changed Volume"},
100         {0xC100, EIO, "Optical System Error"},
101         {0x0000, 0, NULL},
102 };
103
104 /*
105  * This is the structure we use to exchange info between driver and interrupt
106  * handler
107  */
108 struct viocd_waitevent {
109         struct completion       com;
110         int                     rc;
111         u16                     sub_result;
112         int                     changed;
113 };
114
115 /* this is a lookup table for the true capabilities of a device */
116 struct capability_entry {
117         char    *type;
118         int     capability;
119 };
120
121 static struct capability_entry capability_table[] __initdata = {
122         { "6330", CDC_LOCK | CDC_DVD_RAM | CDC_RAM },
123         { "6331", CDC_LOCK | CDC_DVD_RAM | CDC_RAM },
124         { "6333", CDC_LOCK | CDC_DVD_RAM | CDC_RAM },
125         { "632A", CDC_LOCK | CDC_DVD_RAM | CDC_RAM },
126         { "6321", CDC_LOCK },
127         { "632B", 0 },
128         { NULL  , CDC_LOCK },
129 };
130
131 /* These are our internal structures for keeping track of devices */
132 static int viocd_numdev;
133
134 struct cdrom_info {
135         char    rsrcname[10];
136         char    type[4];
137         char    model[3];
138 };
139
140 struct disk_info {
141         struct gendisk                  *viocd_disk;
142         struct cdrom_device_info        viocd_info;
143         struct device                   *dev;
144         struct cdrom_info               unitinfo;
145 };
146 static struct disk_info viocd_diskinfo[VIOCD_MAX_CD];
147
148 #define DEVICE_NR(di)   ((di) - &viocd_diskinfo[0])
149
150 static spinlock_t viocd_reqlock;
151
152 #define MAX_CD_REQ      1
153
154 /* procfs support */
155 static int proc_viocd_show(struct seq_file *m, void *v)
156 {
157         int i;
158
159         for (i = 0; i < viocd_numdev; i++) {
160                 seq_printf(m, "viocd device %d is iSeries resource %10.10s"
161                                 "type %4.4s, model %3.3s\n",
162                                 i, viocd_diskinfo[i].unitinfo.rsrcname,
163                                 viocd_diskinfo[i].unitinfo.type,
164                                 viocd_diskinfo[i].unitinfo.model);
165         }
166         return 0;
167 }
168
169 static int proc_viocd_open(struct inode *inode, struct file *file)
170 {
171         return single_open(file, proc_viocd_show, NULL);
172 }
173
174 static const struct file_operations proc_viocd_operations = {
175         .open           = proc_viocd_open,
176         .read           = seq_read,
177         .llseek         = seq_lseek,
178         .release        = single_release,
179 };
180
181 static int viocd_blk_open(struct inode *inode, struct file *file)
182 {
183         struct disk_info *di = inode->i_bdev->bd_disk->private_data;
184         return cdrom_open(&di->viocd_info, inode, file);
185 }
186
187 static int viocd_blk_release(struct inode *inode, struct file *file)
188 {
189         struct disk_info *di = inode->i_bdev->bd_disk->private_data;
190         return cdrom_release(&di->viocd_info, file);
191 }
192
193 static int viocd_blk_ioctl(struct inode *inode, struct file *file,
194                 unsigned cmd, unsigned long arg)
195 {
196         struct disk_info *di = inode->i_bdev->bd_disk->private_data;
197         return cdrom_ioctl(file, &di->viocd_info, inode, cmd, arg);
198 }
199
200 static int viocd_blk_media_changed(struct gendisk *disk)
201 {
202         struct disk_info *di = disk->private_data;
203         return cdrom_media_changed(&di->viocd_info);
204 }
205
206 struct block_device_operations viocd_fops = {
207         .owner =                THIS_MODULE,
208         .open =                 viocd_blk_open,
209         .release =              viocd_blk_release,
210         .ioctl =                viocd_blk_ioctl,
211         .media_changed =        viocd_blk_media_changed,
212 };
213
214 /* Get info on CD devices from OS/400 */
215 static void __init get_viocd_info(void)
216 {
217         HvLpEvent_Rc hvrc;
218         int i;
219         struct viocd_waitevent we;
220         struct cdrom_info *viocd_unitinfo;
221         dma_addr_t unitinfo_dmaaddr;
222
223         viocd_unitinfo = dma_alloc_coherent(iSeries_vio_dev,
224                         sizeof(*viocd_unitinfo) * VIOCD_MAX_CD,
225                         &unitinfo_dmaaddr, GFP_ATOMIC);
226         if (viocd_unitinfo == NULL) {
227                 printk(VIOCD_KERN_WARNING "error allocating unitinfo\n");
228                 return;
229         }
230
231         memset(viocd_unitinfo, 0, sizeof(*viocd_unitinfo) * VIOCD_MAX_CD);
232
233         init_completion(&we.com);
234
235         hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
236                         HvLpEvent_Type_VirtualIo,
237                         viomajorsubtype_cdio | viocdgetinfo,
238                         HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck,
239                         viopath_sourceinst(viopath_hostLp),
240                         viopath_targetinst(viopath_hostLp),
241                         (u64)&we, VIOVERSION << 16, unitinfo_dmaaddr, 0,
242                         sizeof(*viocd_unitinfo) * VIOCD_MAX_CD, 0);
243         if (hvrc != HvLpEvent_Rc_Good) {
244                 printk(VIOCD_KERN_WARNING "cdrom error sending event. rc %d\n",
245                                 (int)hvrc);
246                 goto error_ret;
247         }
248
249         wait_for_completion(&we.com);
250
251         if (we.rc) {
252                 const struct vio_error_entry *err =
253                         vio_lookup_rc(viocd_err_table, we.sub_result);
254                 printk(VIOCD_KERN_WARNING "bad rc %d:0x%04X on getinfo: %s\n",
255                                 we.rc, we.sub_result, err->msg);
256                 goto error_ret;
257         }
258
259         for (i = 0; (i < VIOCD_MAX_CD) && viocd_unitinfo[i].rsrcname[0]; i++) {
260                 viocd_diskinfo[viocd_numdev].unitinfo = viocd_unitinfo[i];
261                 viocd_numdev++;
262         }
263
264 error_ret:
265         dma_free_coherent(iSeries_vio_dev,
266                         sizeof(*viocd_unitinfo) * VIOCD_MAX_CD,
267                         viocd_unitinfo, unitinfo_dmaaddr);
268 }
269
270 static int viocd_open(struct cdrom_device_info *cdi, int purpose)
271 {
272         struct disk_info *diskinfo = cdi->handle;
273         int device_no = DEVICE_NR(diskinfo);
274         HvLpEvent_Rc hvrc;
275         struct viocd_waitevent we;
276
277         init_completion(&we.com);
278         hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
279                         HvLpEvent_Type_VirtualIo,
280                         viomajorsubtype_cdio | viocdopen,
281                         HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck,
282                         viopath_sourceinst(viopath_hostLp),
283                         viopath_targetinst(viopath_hostLp),
284                         (u64)&we, VIOVERSION << 16, ((u64)device_no << 48),
285                         0, 0, 0);
286         if (hvrc != 0) {
287                 printk(VIOCD_KERN_WARNING
288                                 "bad rc on HvCallEvent_signalLpEventFast %d\n",
289                                 (int)hvrc);
290                 return -EIO;
291         }
292
293         wait_for_completion(&we.com);
294
295         if (we.rc) {
296                 const struct vio_error_entry *err =
297                         vio_lookup_rc(viocd_err_table, we.sub_result);
298                 printk(VIOCD_KERN_WARNING "bad rc %d:0x%04X on open: %s\n",
299                                 we.rc, we.sub_result, err->msg);
300                 return -err->errno;
301         }
302
303         return 0;
304 }
305
306 static void viocd_release(struct cdrom_device_info *cdi)
307 {
308         int device_no = DEVICE_NR((struct disk_info *)cdi->handle);
309         HvLpEvent_Rc hvrc;
310
311         hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
312                         HvLpEvent_Type_VirtualIo,
313                         viomajorsubtype_cdio | viocdclose,
314                         HvLpEvent_AckInd_NoAck, HvLpEvent_AckType_ImmediateAck,
315                         viopath_sourceinst(viopath_hostLp),
316                         viopath_targetinst(viopath_hostLp), 0,
317                         VIOVERSION << 16, ((u64)device_no << 48), 0, 0, 0);
318         if (hvrc != 0)
319                 printk(VIOCD_KERN_WARNING
320                                 "bad rc on HvCallEvent_signalLpEventFast %d\n",
321                                 (int)hvrc);
322 }
323
324 /* Send a read or write request to OS/400 */
325 static int send_request(struct request *req)
326 {
327         HvLpEvent_Rc hvrc;
328         struct disk_info *diskinfo = req->rq_disk->private_data;
329         u64 len;
330         dma_addr_t dmaaddr;
331         int direction;
332         u16 cmd;
333         struct scatterlist sg;
334
335         BUG_ON(req->nr_phys_segments > 1);
336
337         if (rq_data_dir(req) == READ) {
338                 direction = DMA_FROM_DEVICE;
339                 cmd = viomajorsubtype_cdio | viocdread;
340         } else {
341                 direction = DMA_TO_DEVICE;
342                 cmd = viomajorsubtype_cdio | viocdwrite;
343         }
344
345         if (blk_rq_map_sg(req->q, req, &sg) == 0) {
346                 printk(VIOCD_KERN_WARNING
347                                 "error setting up scatter/gather list\n");
348                 return -1;
349         }
350
351         if (dma_map_sg(diskinfo->dev, &sg, 1, direction) == 0) {
352                 printk(VIOCD_KERN_WARNING "error allocating sg tce\n");
353                 return -1;
354         }
355         dmaaddr = sg_dma_address(&sg);
356         len = sg_dma_len(&sg);
357
358         hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
359                         HvLpEvent_Type_VirtualIo, cmd,
360                         HvLpEvent_AckInd_DoAck,
361                         HvLpEvent_AckType_ImmediateAck,
362                         viopath_sourceinst(viopath_hostLp),
363                         viopath_targetinst(viopath_hostLp),
364                         (u64)req, VIOVERSION << 16,
365                         ((u64)DEVICE_NR(diskinfo) << 48) | dmaaddr,
366                         (u64)req->sector * 512, len, 0);
367         if (hvrc != HvLpEvent_Rc_Good) {
368                 printk(VIOCD_KERN_WARNING "hv error on op %d\n", (int)hvrc);
369                 return -1;
370         }
371
372         return 0;
373 }
374
375 static void viocd_end_request(struct request *req, int uptodate)
376 {
377         int nsectors = req->hard_nr_sectors;
378
379         /*
380          * Make sure it's fully ended, and ensure that we process
381          * at least one sector.
382          */
383         if (blk_pc_request(req))
384                 nsectors = (req->data_len + 511) >> 9;
385         if (!nsectors)
386                 nsectors = 1;
387
388         if (end_that_request_first(req, uptodate, nsectors))
389                 BUG();
390         add_disk_randomness(req->rq_disk);
391         blkdev_dequeue_request(req);
392         end_that_request_last(req, uptodate);
393 }
394
395 static int rwreq;
396
397 static void do_viocd_request(struct request_queue *q)
398 {
399         struct request *req;
400
401         while ((rwreq == 0) && ((req = elv_next_request(q)) != NULL)) {
402                 if (!blk_fs_request(req))
403                         viocd_end_request(req, 0);
404                 else if (send_request(req) < 0) {
405                         printk(VIOCD_KERN_WARNING
406                                         "unable to send message to OS/400!");
407                         viocd_end_request(req, 0);
408                 } else
409                         rwreq++;
410         }
411 }
412
413 static int viocd_media_changed(struct cdrom_device_info *cdi, int disc_nr)
414 {
415         struct viocd_waitevent we;
416         HvLpEvent_Rc hvrc;
417         int device_no = DEVICE_NR((struct disk_info *)cdi->handle);
418
419         init_completion(&we.com);
420
421         /* Send the open event to OS/400 */
422         hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
423                         HvLpEvent_Type_VirtualIo,
424                         viomajorsubtype_cdio | viocdcheck,
425                         HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck,
426                         viopath_sourceinst(viopath_hostLp),
427                         viopath_targetinst(viopath_hostLp),
428                         (u64)&we, VIOVERSION << 16, ((u64)device_no << 48),
429                         0, 0, 0);
430         if (hvrc != 0) {
431                 printk(VIOCD_KERN_WARNING "bad rc on HvCallEvent_signalLpEventFast %d\n",
432                                 (int)hvrc);
433                 return -EIO;
434         }
435
436         wait_for_completion(&we.com);
437
438         /* Check the return code.  If bad, assume no change */
439         if (we.rc) {
440                 const struct vio_error_entry *err =
441                         vio_lookup_rc(viocd_err_table, we.sub_result);
442                 printk(VIOCD_KERN_WARNING
443                                 "bad rc %d:0x%04X on check_change: %s; Assuming no change\n",
444                                 we.rc, we.sub_result, err->msg);
445                 return 0;
446         }
447
448         return we.changed;
449 }
450
451 static int viocd_lock_door(struct cdrom_device_info *cdi, int locking)
452 {
453         HvLpEvent_Rc hvrc;
454         u64 device_no = DEVICE_NR((struct disk_info *)cdi->handle);
455         /* NOTE: flags is 1 or 0 so it won't overwrite the device_no */
456         u64 flags = !!locking;
457         struct viocd_waitevent we;
458
459         init_completion(&we.com);
460
461         /* Send the lockdoor event to OS/400 */
462         hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
463                         HvLpEvent_Type_VirtualIo,
464                         viomajorsubtype_cdio | viocdlockdoor,
465                         HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck,
466                         viopath_sourceinst(viopath_hostLp),
467                         viopath_targetinst(viopath_hostLp),
468                         (u64)&we, VIOVERSION << 16,
469                         (device_no << 48) | (flags << 32), 0, 0, 0);
470         if (hvrc != 0) {
471                 printk(VIOCD_KERN_WARNING "bad rc on HvCallEvent_signalLpEventFast %d\n",
472                                 (int)hvrc);
473                 return -EIO;
474         }
475
476         wait_for_completion(&we.com);
477
478         if (we.rc != 0)
479                 return -EIO;
480         return 0;
481 }
482
483 static int viocd_packet(struct cdrom_device_info *cdi,
484                 struct packet_command *cgc)
485 {
486         unsigned int buflen = cgc->buflen;
487         int ret = -EIO;
488
489         switch (cgc->cmd[0]) {
490         case GPCMD_READ_DISC_INFO:
491                 {
492                         disc_information *di = (disc_information *)cgc->buffer;
493
494                         if (buflen >= 2) {
495                                 di->disc_information_length = cpu_to_be16(1);
496                                 ret = 0;
497                         }
498                         if (buflen >= 3)
499                                 di->erasable =
500                                         (cdi->ops->capability & ~cdi->mask
501                                          & (CDC_DVD_RAM | CDC_RAM)) != 0;
502                 }
503                 break;
504         case GPCMD_GET_CONFIGURATION:
505                 if (cgc->cmd[3] == CDF_RWRT) {
506                         struct rwrt_feature_desc *rfd = (struct rwrt_feature_desc *)(cgc->buffer + sizeof(struct feature_header));
507
508                         if ((buflen >=
509                              (sizeof(struct feature_header) + sizeof(*rfd))) &&
510                             (cdi->ops->capability & ~cdi->mask
511                              & (CDC_DVD_RAM | CDC_RAM))) {
512                                 rfd->feature_code = cpu_to_be16(CDF_RWRT);
513                                 rfd->curr = 1;
514                                 ret = 0;
515                         }
516                 }
517                 break;
518         default:
519                 if (cgc->sense) {
520                         /* indicate Unknown code */
521                         cgc->sense->sense_key = 0x05;
522                         cgc->sense->asc = 0x20;
523                         cgc->sense->ascq = 0x00;
524                 }
525                 break;
526         }
527
528         cgc->stat = ret;
529         return ret;
530 }
531
532 static void restart_all_queues(int first_index)
533 {
534         int i;
535
536         for (i = first_index + 1; i < viocd_numdev; i++)
537                 if (viocd_diskinfo[i].viocd_disk)
538                         blk_run_queue(viocd_diskinfo[i].viocd_disk->queue);
539         for (i = 0; i <= first_index; i++)
540                 if (viocd_diskinfo[i].viocd_disk)
541                         blk_run_queue(viocd_diskinfo[i].viocd_disk->queue);
542 }
543
544 /* This routine handles incoming CD LP events */
545 static void vio_handle_cd_event(struct HvLpEvent *event)
546 {
547         struct viocdlpevent *bevent;
548         struct viocd_waitevent *pwe;
549         struct disk_info *di;
550         unsigned long flags;
551         struct request *req;
552
553
554         if (event == NULL)
555                 /* Notification that a partition went away! */
556                 return;
557         /* First, we should NEVER get an int here...only acks */
558         if (hvlpevent_is_int(event)) {
559                 printk(VIOCD_KERN_WARNING
560                                 "Yikes! got an int in viocd event handler!\n");
561                 if (hvlpevent_need_ack(event)) {
562                         event->xRc = HvLpEvent_Rc_InvalidSubtype;
563                         HvCallEvent_ackLpEvent(event);
564                 }
565         }
566
567         bevent = (struct viocdlpevent *)event;
568
569         switch (event->xSubtype & VIOMINOR_SUBTYPE_MASK) {
570         case viocdopen:
571                 if (event->xRc == 0) {
572                         di = &viocd_diskinfo[bevent->disk];
573                         blk_queue_hardsect_size(di->viocd_disk->queue,
574                                         bevent->block_size);
575                         set_capacity(di->viocd_disk,
576                                         bevent->media_size *
577                                         bevent->block_size / 512);
578                 }
579                 /* FALLTHROUGH !! */
580         case viocdgetinfo:
581         case viocdlockdoor:
582                 pwe = (struct viocd_waitevent *)event->xCorrelationToken;
583 return_complete:
584                 pwe->rc = event->xRc;
585                 pwe->sub_result = bevent->sub_result;
586                 complete(&pwe->com);
587                 break;
588
589         case viocdcheck:
590                 pwe = (struct viocd_waitevent *)event->xCorrelationToken;
591                 pwe->changed = bevent->flags;
592                 goto return_complete;
593
594         case viocdclose:
595                 break;
596
597         case viocdwrite:
598         case viocdread:
599                 /*
600                  * Since this is running in interrupt mode, we need to
601                  * make sure we're not stepping on any global I/O operations
602                  */
603                 di = &viocd_diskinfo[bevent->disk];
604                 spin_lock_irqsave(&viocd_reqlock, flags);
605                 dma_unmap_single(di->dev, bevent->token, bevent->len,
606                                 ((event->xSubtype & VIOMINOR_SUBTYPE_MASK) == viocdread)
607                                 ?  DMA_FROM_DEVICE : DMA_TO_DEVICE);
608                 req = (struct request *)bevent->event.xCorrelationToken;
609                 rwreq--;
610
611                 if (event->xRc != HvLpEvent_Rc_Good) {
612                         const struct vio_error_entry *err =
613                                 vio_lookup_rc(viocd_err_table,
614                                                 bevent->sub_result);
615                         printk(VIOCD_KERN_WARNING "request %p failed "
616                                         "with rc %d:0x%04X: %s\n",
617                                         req, event->xRc,
618                                         bevent->sub_result, err->msg);
619                         viocd_end_request(req, 0);
620                 } else
621                         viocd_end_request(req, 1);
622
623                 /* restart handling of incoming requests */
624                 spin_unlock_irqrestore(&viocd_reqlock, flags);
625                 restart_all_queues(bevent->disk);
626                 break;
627
628         default:
629                 printk(VIOCD_KERN_WARNING
630                                 "message with invalid subtype %0x04X!\n",
631                                 event->xSubtype & VIOMINOR_SUBTYPE_MASK);
632                 if (hvlpevent_need_ack(event)) {
633                         event->xRc = HvLpEvent_Rc_InvalidSubtype;
634                         HvCallEvent_ackLpEvent(event);
635                 }
636         }
637 }
638
639 static struct cdrom_device_ops viocd_dops = {
640         .open = viocd_open,
641         .release = viocd_release,
642         .media_changed = viocd_media_changed,
643         .lock_door = viocd_lock_door,
644         .generic_packet = viocd_packet,
645         .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
646 };
647
648 static int __init find_capability(const char *type)
649 {
650         struct capability_entry *entry;
651
652         for(entry = capability_table; entry->type; ++entry)
653                 if(!strncmp(entry->type, type, 4))
654                         break;
655         return entry->capability;
656 }
657
658 static int viocd_probe(struct vio_dev *vdev, const struct vio_device_id *id)
659 {
660         struct gendisk *gendisk;
661         int deviceno;
662         struct disk_info *d;
663         struct cdrom_device_info *c;
664         struct cdrom_info *ci;
665         struct request_queue *q;
666
667         deviceno = vdev->unit_address;
668         if (deviceno >= viocd_numdev)
669                 return -ENODEV;
670
671         d = &viocd_diskinfo[deviceno];
672         c = &d->viocd_info;
673         ci = &d->unitinfo;
674
675         c->ops = &viocd_dops;
676         c->speed = 4;
677         c->capacity = 1;
678         c->handle = d;
679         c->mask = ~find_capability(ci->type);
680         sprintf(c->name, VIOCD_DEVICE "%c", 'a' + deviceno);
681
682         if (register_cdrom(c) != 0) {
683                 printk(VIOCD_KERN_WARNING "Cannot register viocd CD-ROM %s!\n",
684                                 c->name);
685                 goto out;
686         }
687         printk(VIOCD_KERN_INFO "cd %s is iSeries resource %10.10s "
688                         "type %4.4s, model %3.3s\n",
689                         c->name, ci->rsrcname, ci->type, ci->model);
690         q = blk_init_queue(do_viocd_request, &viocd_reqlock);
691         if (q == NULL) {
692                 printk(VIOCD_KERN_WARNING "Cannot allocate queue for %s!\n",
693                                 c->name);
694                 goto out_unregister_cdrom;
695         }
696         gendisk = alloc_disk(1);
697         if (gendisk == NULL) {
698                 printk(VIOCD_KERN_WARNING "Cannot create gendisk for %s!\n",
699                                 c->name);
700                 goto out_cleanup_queue;
701         }
702         gendisk->major = VIOCD_MAJOR;
703         gendisk->first_minor = deviceno;
704         strncpy(gendisk->disk_name, c->name,
705                         sizeof(gendisk->disk_name));
706         blk_queue_max_hw_segments(q, 1);
707         blk_queue_max_phys_segments(q, 1);
708         blk_queue_max_sectors(q, 4096 / 512);
709         gendisk->queue = q;
710         gendisk->fops = &viocd_fops;
711         gendisk->flags = GENHD_FL_CD|GENHD_FL_REMOVABLE;
712         set_capacity(gendisk, 0);
713         gendisk->private_data = d;
714         d->viocd_disk = gendisk;
715         d->dev = &vdev->dev;
716         gendisk->driverfs_dev = d->dev;
717         add_disk(gendisk);
718         return 0;
719
720 out_cleanup_queue:
721         blk_cleanup_queue(q);
722 out_unregister_cdrom:
723         unregister_cdrom(c);
724 out:
725         return -ENODEV;
726 }
727
728 static int viocd_remove(struct vio_dev *vdev)
729 {
730         struct disk_info *d = &viocd_diskinfo[vdev->unit_address];
731
732         if (unregister_cdrom(&d->viocd_info) != 0)
733                 printk(VIOCD_KERN_WARNING
734                                 "Cannot unregister viocd CD-ROM %s!\n",
735                                 d->viocd_info.name);
736         del_gendisk(d->viocd_disk);
737         blk_cleanup_queue(d->viocd_disk->queue);
738         put_disk(d->viocd_disk);
739         return 0;
740 }
741
742 /**
743  * viocd_device_table: Used by vio.c to match devices that we
744  * support.
745  */
746 static struct vio_device_id viocd_device_table[] __devinitdata = {
747         { "block", "IBM,iSeries-viocd" },
748         { "", "" }
749 };
750 MODULE_DEVICE_TABLE(vio, viocd_device_table);
751
752 static struct vio_driver viocd_driver = {
753         .id_table = viocd_device_table,
754         .probe = viocd_probe,
755         .remove = viocd_remove,
756         .driver = {
757                 .name = "viocd",
758                 .owner = THIS_MODULE,
759         }
760 };
761
762 static int __init viocd_init(void)
763 {
764         struct proc_dir_entry *e;
765         int ret = 0;
766
767         if (!firmware_has_feature(FW_FEATURE_ISERIES))
768                 return -ENODEV;
769
770         if (viopath_hostLp == HvLpIndexInvalid) {
771                 vio_set_hostlp();
772                 /* If we don't have a host, bail out */
773                 if (viopath_hostLp == HvLpIndexInvalid)
774                         return -ENODEV;
775         }
776
777         printk(VIOCD_KERN_INFO "vers " VIOCD_VERS ", hosting partition %d\n",
778                         viopath_hostLp);
779
780         if (register_blkdev(VIOCD_MAJOR, VIOCD_DEVICE) != 0) {
781                 printk(VIOCD_KERN_WARNING "Unable to get major %d for %s\n",
782                                 VIOCD_MAJOR, VIOCD_DEVICE);
783                 return -EIO;
784         }
785
786         ret = viopath_open(viopath_hostLp, viomajorsubtype_cdio,
787                         MAX_CD_REQ + 2);
788         if (ret) {
789                 printk(VIOCD_KERN_WARNING
790                                 "error opening path to host partition %d\n",
791                                 viopath_hostLp);
792                 goto out_unregister;
793         }
794
795         /* Initialize our request handler */
796         vio_setHandler(viomajorsubtype_cdio, vio_handle_cd_event);
797
798         get_viocd_info();
799
800         spin_lock_init(&viocd_reqlock);
801
802         ret = vio_register_driver(&viocd_driver);
803         if (ret)
804                 goto out_free_info;
805
806         e = create_proc_entry("iSeries/viocd", S_IFREG|S_IRUGO, NULL);
807         if (e) {
808                 e->owner = THIS_MODULE;
809                 e->proc_fops = &proc_viocd_operations;
810         }
811
812         return 0;
813
814 out_free_info:
815         vio_clearHandler(viomajorsubtype_cdio);
816         viopath_close(viopath_hostLp, viomajorsubtype_cdio, MAX_CD_REQ + 2);
817 out_unregister:
818         unregister_blkdev(VIOCD_MAJOR, VIOCD_DEVICE);
819         return ret;
820 }
821
822 static void __exit viocd_exit(void)
823 {
824         remove_proc_entry("iSeries/viocd", NULL);
825         vio_unregister_driver(&viocd_driver);
826         viopath_close(viopath_hostLp, viomajorsubtype_cdio, MAX_CD_REQ + 2);
827         vio_clearHandler(viomajorsubtype_cdio);
828         unregister_blkdev(VIOCD_MAJOR, VIOCD_DEVICE);
829 }
830
831 module_init(viocd_init);
832 module_exit(viocd_exit);
833 MODULE_LICENSE("GPL");