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