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