VServer 1.9.2 (patch-2.6.8.1-vs1.9.2.diff)
[linux-2.6.git] / 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/bug.h>
46
47 #include <asm/vio.h>
48 #include <asm/scatterlist.h>
49 #include <asm/iSeries/HvTypes.h>
50 #include <asm/iSeries/HvLpEvent.h>
51 #include <asm/iSeries/vio.h>
52
53 #define VIOCD_DEVICE                    "iseries/vcd"
54 #define VIOCD_DEVICE_DEVFS              "iseries/vcd"
55
56 #define VIOCD_VERS "1.06"
57
58 #define VIOCD_KERN_WARNING              KERN_WARNING "viocd: "
59 #define VIOCD_KERN_INFO                 KERN_INFO "viocd: "
60
61 struct viocdlpevent {
62         struct HvLpEvent        event;
63         u32                     reserved;
64         u16                     version;
65         u16                     sub_result;
66         u16                     disk;
67         u16                     flags;
68         u32                     token;
69         u64                     offset;         /* On open, max number of disks */
70         u64                     len;            /* On open, size of the disk */
71         u32                     block_size;     /* Only set on open */
72         u32                     media_size;     /* Only set on open */
73 };
74
75 enum viocdsubtype {
76         viocdopen = 0x0001,
77         viocdclose = 0x0002,
78         viocdread = 0x0003,
79         viocdwrite = 0x0004,
80         viocdlockdoor = 0x0005,
81         viocdgetinfo = 0x0006,
82         viocdcheck = 0x0007
83 };
84
85 /*
86  * Should probably make this a module parameter....sigh
87  */
88 #define VIOCD_MAX_CD    HVMAXARCHITECTEDVIRTUALCDROMS
89
90 static const struct vio_error_entry viocd_err_table[] = {
91         {0x0201, EINVAL, "Invalid Range"},
92         {0x0202, EINVAL, "Invalid Token"},
93         {0x0203, EIO, "DMA Error"},
94         {0x0204, EIO, "Use Error"},
95         {0x0205, EIO, "Release Error"},
96         {0x0206, EINVAL, "Invalid CD"},
97         {0x020C, EROFS, "Read Only Device"},
98         {0x020D, ENOMEDIUM, "Changed or Missing Volume (or Varied Off?)"},
99         {0x020E, EIO, "Optical System Error (Varied Off?)"},
100         {0x02FF, EIO, "Internal Error"},
101         {0x3010, EIO, "Changed Volume"},
102         {0xC100, EIO, "Optical System Error"},
103         {0x0000, 0, NULL},
104 };
105
106 /*
107  * This is the structure we use to exchange info between driver and interrupt
108  * handler
109  */
110 struct viocd_waitevent {
111         struct completion       com;
112         int                     rc;
113         u16                     sub_result;
114         int                     changed;
115 };
116
117 /* this is a lookup table for the true capabilities of a device */
118 struct capability_entry {
119         char    *type;
120         int     capability;
121 };
122
123 static struct capability_entry capability_table[] __initdata = {
124         { "6330", CDC_LOCK | CDC_DVD_RAM },
125         { "6321", CDC_LOCK },
126         { "632B", 0 },
127         { NULL  , CDC_LOCK },
128 };
129
130 /* These are our internal structures for keeping track of devices */
131 static int viocd_numdev;
132
133 struct cdrom_info {
134         char    rsrcname[10];
135         char    type[4];
136         char    model[3];
137 };
138 /*
139  * This needs to be allocated since it is passed to the
140  * Hypervisor and we may be a module.
141  */
142 static struct cdrom_info *viocd_unitinfo;
143 static dma_addr_t unitinfo_dmaaddr;
144
145 struct disk_info {
146         struct gendisk                  *viocd_disk;
147         struct cdrom_device_info        viocd_info;
148         struct device                   *dev;
149 };
150 static struct disk_info viocd_diskinfo[VIOCD_MAX_CD];
151
152 #define DEVICE_NR(di)   ((di) - &viocd_diskinfo[0])
153
154 static request_queue_t *viocd_queue;
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 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         struct scatterlist sg;
336
337         BUG_ON(req->nr_phys_segments > 1);
338         BUG_ON(rq_data_dir(req) != READ);
339
340         if (blk_rq_map_sg(req->q, req, &sg) == 0) {
341                 printk(VIOCD_KERN_WARNING
342                                 "error setting up scatter/gather list\n");
343                 return -1;
344         }
345
346         if (dma_map_sg(diskinfo->dev, &sg, 1, DMA_FROM_DEVICE) == 0) {
347                 printk(VIOCD_KERN_WARNING "error allocating sg tce\n");
348                 return -1;
349         }
350         dmaaddr = sg_dma_address(&sg);
351         len = sg_dma_len(&sg);
352
353         hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
354                         HvLpEvent_Type_VirtualIo,
355                         viomajorsubtype_cdio | viocdread,
356                         HvLpEvent_AckInd_DoAck,
357                         HvLpEvent_AckType_ImmediateAck,
358                         viopath_sourceinst(viopath_hostLp),
359                         viopath_targetinst(viopath_hostLp),
360                         (u64)req, VIOVERSION << 16,
361                         ((u64)DEVICE_NR(diskinfo) << 48) | dmaaddr,
362                         (u64)req->sector * 512, len, 0);
363         if (hvrc != HvLpEvent_Rc_Good) {
364                 printk(VIOCD_KERN_WARNING "hv error on op %d\n", (int)hvrc);
365                 return -1;
366         }
367
368         return 0;
369 }
370
371
372 static int rwreq;
373
374 static void do_viocd_request(request_queue_t *q)
375 {
376         struct request *req;
377
378         while ((rwreq == 0) && ((req = elv_next_request(q)) != NULL)) {
379                 if (!blk_fs_request(req))
380                         end_request(req, 0);
381                 else if (send_request(req) < 0) {
382                         printk(VIOCD_KERN_WARNING
383                                         "unable to send message to OS/400!");
384                         end_request(req, 0);
385                 } else
386                         rwreq++;
387         }
388 }
389
390 static int viocd_media_changed(struct cdrom_device_info *cdi, int disc_nr)
391 {
392         struct viocd_waitevent we;
393         HvLpEvent_Rc hvrc;
394         int device_no = DEVICE_NR((struct disk_info *)cdi->handle);
395
396         init_completion(&we.com);
397
398         /* Send the open event to OS/400 */
399         hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
400                         HvLpEvent_Type_VirtualIo,
401                         viomajorsubtype_cdio | viocdcheck,
402                         HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck,
403                         viopath_sourceinst(viopath_hostLp),
404                         viopath_targetinst(viopath_hostLp),
405                         (u64)&we, VIOVERSION << 16, ((u64)device_no << 48),
406                         0, 0, 0);
407         if (hvrc != 0) {
408                 printk(VIOCD_KERN_WARNING "bad rc on HvCallEvent_signalLpEventFast %d\n",
409                                 (int)hvrc);
410                 return -EIO;
411         }
412
413         wait_for_completion(&we.com);
414
415         /* Check the return code.  If bad, assume no change */
416         if (we.rc) {
417                 const struct vio_error_entry *err =
418                         vio_lookup_rc(viocd_err_table, we.sub_result);
419                 printk(VIOCD_KERN_WARNING
420                                 "bad rc %d:0x%04X on check_change: %s; Assuming no change\n",
421                                 we.rc, we.sub_result, err->msg);
422                 return 0;
423         }
424
425         return we.changed;
426 }
427
428 static int viocd_lock_door(struct cdrom_device_info *cdi, int locking)
429 {
430         HvLpEvent_Rc hvrc;
431         u64 device_no = DEVICE_NR((struct disk_info *)cdi->handle);
432         /* NOTE: flags is 1 or 0 so it won't overwrite the device_no */
433         u64 flags = !!locking;
434         struct viocd_waitevent we;
435
436         init_completion(&we.com);
437
438         /* Send the lockdoor event to OS/400 */
439         hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
440                         HvLpEvent_Type_VirtualIo,
441                         viomajorsubtype_cdio | viocdlockdoor,
442                         HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck,
443                         viopath_sourceinst(viopath_hostLp),
444                         viopath_targetinst(viopath_hostLp),
445                         (u64)&we, VIOVERSION << 16,
446                         (device_no << 48) | (flags << 32), 0, 0, 0);
447         if (hvrc != 0) {
448                 printk(VIOCD_KERN_WARNING "bad rc on HvCallEvent_signalLpEventFast %d\n",
449                                 (int)hvrc);
450                 return -EIO;
451         }
452
453         wait_for_completion(&we.com);
454
455         if (we.rc != 0)
456                 return -EIO;
457         return 0;
458 }
459
460 /* This routine handles incoming CD LP events */
461 static void vio_handle_cd_event(struct HvLpEvent *event)
462 {
463         struct viocdlpevent *bevent;
464         struct viocd_waitevent *pwe;
465         struct disk_info *di;
466         unsigned long flags;
467         struct request *req;
468
469
470         if (event == NULL)
471                 /* Notification that a partition went away! */
472                 return;
473         /* First, we should NEVER get an int here...only acks */
474         if (event->xFlags.xFunction == HvLpEvent_Function_Int) {
475                 printk(VIOCD_KERN_WARNING
476                                 "Yikes! got an int in viocd event handler!\n");
477                 if (event->xFlags.xAckInd == HvLpEvent_AckInd_DoAck) {
478                         event->xRc = HvLpEvent_Rc_InvalidSubtype;
479                         HvCallEvent_ackLpEvent(event);
480                 }
481         }
482
483         bevent = (struct viocdlpevent *)event;
484
485         switch (event->xSubtype & VIOMINOR_SUBTYPE_MASK) {
486         case viocdopen:
487                 if (event->xRc == 0) {
488                         di = &viocd_diskinfo[bevent->disk];
489                         blk_queue_hardsect_size(viocd_queue,
490                                         bevent->block_size);
491                         set_capacity(di->viocd_disk,
492                                         bevent->media_size *
493                                         bevent->block_size / 512);
494                 }
495                 /* FALLTHROUGH !! */
496         case viocdgetinfo:
497         case viocdlockdoor:
498                 pwe = (struct viocd_waitevent *)event->xCorrelationToken;
499 return_complete:
500                 pwe->rc = event->xRc;
501                 pwe->sub_result = bevent->sub_result;
502                 complete(&pwe->com);
503                 break;
504
505         case viocdcheck:
506                 pwe = (struct viocd_waitevent *)event->xCorrelationToken;
507                 pwe->changed = bevent->flags;
508                 goto return_complete;
509
510         case viocdclose:
511                 break;
512
513         case viocdread:
514                 /*
515                  * Since this is running in interrupt mode, we need to
516                  * make sure we're not stepping on any global I/O operations
517                  */
518                 di = &viocd_diskinfo[bevent->disk];
519                 spin_lock_irqsave(&viocd_reqlock, flags);
520                 dma_unmap_single(di->dev, bevent->token, bevent->len,
521                                 DMA_FROM_DEVICE);
522                 req = (struct request *)bevent->event.xCorrelationToken;
523                 rwreq--;
524
525                 if (event->xRc != HvLpEvent_Rc_Good) {
526                         const struct vio_error_entry *err =
527                                 vio_lookup_rc(viocd_err_table,
528                                                 bevent->sub_result);
529                         printk(VIOCD_KERN_WARNING "request %p failed "
530                                         "with rc %d:0x%04X: %s\n",
531                                         req, event->xRc,
532                                         bevent->sub_result, err->msg);
533                         end_request(req, 0);
534                 } else
535                         end_request(req, 1);
536
537                 /* restart handling of incoming requests */
538                 spin_unlock_irqrestore(&viocd_reqlock, flags);
539                 blk_run_queue(viocd_queue);
540                 break;
541
542         default:
543                 printk(VIOCD_KERN_WARNING
544                                 "message with invalid subtype %0x04X!\n",
545                                 event->xSubtype & VIOMINOR_SUBTYPE_MASK);
546                 if (event->xFlags.xAckInd == HvLpEvent_AckInd_DoAck) {
547                         event->xRc = HvLpEvent_Rc_InvalidSubtype;
548                         HvCallEvent_ackLpEvent(event);
549                 }
550         }
551 }
552
553 static struct cdrom_device_ops viocd_dops = {
554         .open = viocd_open,
555         .release = viocd_release,
556         .media_changed = viocd_media_changed,
557         .lock_door = viocd_lock_door,
558         .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
559 };
560
561 static int __init find_capability(const char *type)
562 {
563         struct capability_entry *entry;
564
565         for(entry = capability_table; entry->type; ++entry)
566                 if(!strncmp(entry->type, type, 4))
567                         break;
568         return entry->capability;
569 }
570
571 static int viocd_probe(struct vio_dev *vdev, const struct vio_device_id *id)
572 {
573         struct gendisk *gendisk;
574         int deviceno;
575         struct disk_info *d;
576         struct cdrom_device_info *c;
577         struct cdrom_info *ci;
578
579         deviceno = vdev->unit_address;
580         if (deviceno >= viocd_numdev)
581                 return -ENODEV;
582
583         d = &viocd_diskinfo[deviceno];
584         c = &d->viocd_info;
585         ci = &viocd_unitinfo[deviceno];
586
587         c->ops = &viocd_dops;
588         c->speed = 4;
589         c->capacity = 1;
590         c->handle = d;
591         c->mask = ~find_capability(ci->type);
592         sprintf(c->name, VIOCD_DEVICE "%c", 'a' + deviceno);
593
594         if (register_cdrom(c) != 0) {
595                 printk(VIOCD_KERN_WARNING "Cannot register viocd CD-ROM %s!\n",
596                                 c->name);
597                 return 0;
598         }
599         printk(VIOCD_KERN_INFO "cd %s is iSeries resource %10.10s "
600                         "type %4.4s, model %3.3s\n",
601                         c->name, ci->rsrcname, ci->type, ci->model);
602         gendisk = alloc_disk(1);
603         if (gendisk == NULL) {
604                 printk(VIOCD_KERN_WARNING "Cannot create gendisk for %s!\n",
605                                 c->name);
606                 unregister_cdrom(c);
607                 return 0;
608         }
609         gendisk->major = VIOCD_MAJOR;
610         gendisk->first_minor = deviceno;
611         strncpy(gendisk->disk_name, c->name,
612                         sizeof(gendisk->disk_name));
613         snprintf(gendisk->devfs_name, sizeof(gendisk->devfs_name),
614                         VIOCD_DEVICE_DEVFS "%d", deviceno);
615         gendisk->queue = viocd_queue;
616         gendisk->fops = &viocd_fops;
617         gendisk->flags = GENHD_FL_CD|GENHD_FL_REMOVABLE;
618         set_capacity(gendisk, 0);
619         gendisk->private_data = d;
620         d->viocd_disk = gendisk;
621         d->dev = &vdev->dev;
622         gendisk->driverfs_dev = d->dev;
623         add_disk(gendisk);
624
625         return 0;
626 }
627
628 static int viocd_remove(struct vio_dev *vdev)
629 {
630         struct disk_info *d = &viocd_diskinfo[vdev->unit_address];
631
632         if (unregister_cdrom(&d->viocd_info) != 0)
633                 printk(VIOCD_KERN_WARNING
634                                 "Cannot unregister viocd CD-ROM %s!\n",
635                                 d->viocd_info.name);
636         del_gendisk(d->viocd_disk);
637         put_disk(d->viocd_disk);
638         return 0;
639 }
640
641 /**
642  * viocd_device_table: Used by vio.c to match devices that we
643  * support.
644  */
645 static struct vio_device_id viocd_device_table[] __devinitdata = {
646         { "viocd", "" },
647         { 0, }
648 };
649
650 MODULE_DEVICE_TABLE(vio, viocd_device_table);
651 static struct vio_driver viocd_driver = {
652         .name = "viocd",
653         .id_table = viocd_device_table,
654         .probe = viocd_probe,
655         .remove = viocd_remove
656 };
657
658 static int __init viocd_init(void)
659 {
660         struct proc_dir_entry *e;
661         int ret = 0;
662
663         if (viopath_hostLp == HvLpIndexInvalid) {
664                 vio_set_hostlp();
665                 /* If we don't have a host, bail out */
666                 if (viopath_hostLp == HvLpIndexInvalid)
667                         return -ENODEV;
668         }
669
670         printk(VIOCD_KERN_INFO "vers " VIOCD_VERS ", hosting partition %d\n",
671                         viopath_hostLp);
672
673         if (register_blkdev(VIOCD_MAJOR, VIOCD_DEVICE) != 0) {
674                 printk(VIOCD_KERN_WARNING "Unable to get major %d for %s\n",
675                                 VIOCD_MAJOR, VIOCD_DEVICE);
676                 return -EIO;
677         }
678
679         ret = viopath_open(viopath_hostLp, viomajorsubtype_cdio,
680                         MAX_CD_REQ + 2);
681         if (ret) {
682                 printk(VIOCD_KERN_WARNING
683                                 "error opening path to host partition %d\n",
684                                 viopath_hostLp);
685                 goto out_unregister;
686         }
687
688         /* Initialize our request handler */
689         vio_setHandler(viomajorsubtype_cdio, vio_handle_cd_event);
690
691         get_viocd_info();
692         if (viocd_numdev == 0)
693                 goto out_undo_vio;
694
695         spin_lock_init(&viocd_reqlock);
696         viocd_queue = blk_init_queue(do_viocd_request, &viocd_reqlock);
697         if (viocd_queue == NULL) {
698                 ret = -ENOMEM;
699                 goto out_free_info;
700         }
701         blk_queue_max_hw_segments(viocd_queue, 1);
702         blk_queue_max_phys_segments(viocd_queue, 1);
703         blk_queue_max_sectors(viocd_queue, 4096 / 512);
704
705         ret = vio_register_driver(&viocd_driver);
706         if (ret)
707                 goto out_cleanup_queue;
708
709         e = create_proc_entry("iSeries/viocd", S_IFREG|S_IRUGO, NULL);
710         if (e) {
711                 e->owner = THIS_MODULE;
712                 e->proc_fops = &proc_viocd_operations;
713         }
714
715         return 0;
716
717 out_cleanup_queue:
718         blk_cleanup_queue(viocd_queue);
719 out_free_info:
720         dma_free_coherent(iSeries_vio_dev,
721                         sizeof(*viocd_unitinfo) * VIOCD_MAX_CD,
722                         viocd_unitinfo, unitinfo_dmaaddr);
723 out_undo_vio:
724         vio_clearHandler(viomajorsubtype_cdio);
725         viopath_close(viopath_hostLp, viomajorsubtype_cdio, MAX_CD_REQ + 2);
726 out_unregister:
727         unregister_blkdev(VIOCD_MAJOR, VIOCD_DEVICE);
728         return ret;
729 }
730
731 static void __exit viocd_exit(void)
732 {
733         remove_proc_entry("iSeries/viocd", NULL);
734         vio_unregister_driver(&viocd_driver);
735         blk_cleanup_queue(viocd_queue);
736         if (viocd_unitinfo != NULL)
737                 dma_free_coherent(iSeries_vio_dev,
738                                 sizeof(*viocd_unitinfo) * VIOCD_MAX_CD,
739                                 viocd_unitinfo, unitinfo_dmaaddr);
740         viopath_close(viopath_hostLp, viomajorsubtype_cdio, MAX_CD_REQ + 2);
741         vio_clearHandler(viomajorsubtype_cdio);
742         unregister_blkdev(VIOCD_MAJOR, VIOCD_DEVICE);
743 }
744
745 module_init(viocd_init);
746 module_exit(viocd_exit);
747 MODULE_LICENSE("GPL");