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