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