blob: cac06bc1754b327f7e88605c4f1ed838d8187966 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* -*- 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>
Jens Axboe3d1266c2007-10-24 13:21:21 +020044#include <linux/scatterlist.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <asm/vio.h>
Kelly Daly1ec65d72005-11-02 13:46:07 +110047#include <asm/iseries/hv_types.h>
Kelly Dalye45423e2005-11-02 12:08:31 +110048#include <asm/iseries/hv_lp_event.h>
Kelly Dalyb4206772005-11-02 15:13:57 +110049#include <asm/iseries/vio.h>
Stephen Rothwell31c72ad2006-12-15 15:44:04 +110050#include <asm/firmware.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
52#define VIOCD_DEVICE "iseries/vcd"
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
54#define VIOCD_VERS "1.06"
55
56#define VIOCD_KERN_WARNING KERN_WARNING "viocd: "
57#define VIOCD_KERN_INFO KERN_INFO "viocd: "
58
Linus Torvalds1da177e2005-04-16 15:20:36 -070059/*
60 * Should probably make this a module parameter....sigh
61 */
62#define VIOCD_MAX_CD HVMAXARCHITECTEDVIRTUALCDROMS
63
64static 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 */
84struct 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 */
92struct capability_entry {
93 char *type;
94 int capability;
95};
96
97static 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 */
108static int viocd_numdev;
109
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110struct disk_info {
111 struct gendisk *viocd_disk;
112 struct cdrom_device_info viocd_info;
113 struct device *dev;
Stephen Rothwellb833b482007-10-11 14:57:26 +1000114 const char *rsrcname;
115 const char *type;
116 const char *model;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117};
118static struct disk_info viocd_diskinfo[VIOCD_MAX_CD];
119
120#define DEVICE_NR(di) ((di) - &viocd_diskinfo[0])
121
122static spinlock_t viocd_reqlock;
123
124#define MAX_CD_REQ 1
125
126/* procfs support */
127static 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",
Stephen Rothwellb833b482007-10-11 14:57:26 +1000134 i, viocd_diskinfo[i].rsrcname,
135 viocd_diskinfo[i].type,
136 viocd_diskinfo[i].model);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 }
138 return 0;
139}
140
141static int proc_viocd_open(struct inode *inode, struct file *file)
142{
143 return single_open(file, proc_viocd_show, NULL);
144}
145
Arjan van de Ven2b8693c2007-02-12 00:55:32 -0800146static const struct file_operations proc_viocd_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 .open = proc_viocd_open,
148 .read = seq_read,
149 .llseek = seq_lseek,
150 .release = single_release,
151};
152
153static int viocd_blk_open(struct inode *inode, struct file *file)
154{
155 struct disk_info *di = inode->i_bdev->bd_disk->private_data;
156 return cdrom_open(&di->viocd_info, inode, file);
157}
158
159static int viocd_blk_release(struct inode *inode, struct file *file)
160{
161 struct disk_info *di = inode->i_bdev->bd_disk->private_data;
162 return cdrom_release(&di->viocd_info, file);
163}
164
165static int viocd_blk_ioctl(struct inode *inode, struct file *file,
166 unsigned cmd, unsigned long arg)
167{
168 struct disk_info *di = inode->i_bdev->bd_disk->private_data;
169 return cdrom_ioctl(file, &di->viocd_info, inode, cmd, arg);
170}
171
172static int viocd_blk_media_changed(struct gendisk *disk)
173{
174 struct disk_info *di = disk->private_data;
175 return cdrom_media_changed(&di->viocd_info);
176}
177
178struct block_device_operations viocd_fops = {
179 .owner = THIS_MODULE,
180 .open = viocd_blk_open,
181 .release = viocd_blk_release,
182 .ioctl = viocd_blk_ioctl,
183 .media_changed = viocd_blk_media_changed,
184};
185
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186static int viocd_open(struct cdrom_device_info *cdi, int purpose)
187{
188 struct disk_info *diskinfo = cdi->handle;
189 int device_no = DEVICE_NR(diskinfo);
190 HvLpEvent_Rc hvrc;
191 struct viocd_waitevent we;
192
193 init_completion(&we.com);
194 hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
195 HvLpEvent_Type_VirtualIo,
196 viomajorsubtype_cdio | viocdopen,
197 HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck,
198 viopath_sourceinst(viopath_hostLp),
199 viopath_targetinst(viopath_hostLp),
200 (u64)&we, VIOVERSION << 16, ((u64)device_no << 48),
201 0, 0, 0);
202 if (hvrc != 0) {
203 printk(VIOCD_KERN_WARNING
204 "bad rc on HvCallEvent_signalLpEventFast %d\n",
205 (int)hvrc);
206 return -EIO;
207 }
208
209 wait_for_completion(&we.com);
210
211 if (we.rc) {
212 const struct vio_error_entry *err =
213 vio_lookup_rc(viocd_err_table, we.sub_result);
214 printk(VIOCD_KERN_WARNING "bad rc %d:0x%04X on open: %s\n",
215 we.rc, we.sub_result, err->msg);
216 return -err->errno;
217 }
218
219 return 0;
220}
221
222static void viocd_release(struct cdrom_device_info *cdi)
223{
224 int device_no = DEVICE_NR((struct disk_info *)cdi->handle);
225 HvLpEvent_Rc hvrc;
226
227 hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
228 HvLpEvent_Type_VirtualIo,
229 viomajorsubtype_cdio | viocdclose,
230 HvLpEvent_AckInd_NoAck, HvLpEvent_AckType_ImmediateAck,
231 viopath_sourceinst(viopath_hostLp),
232 viopath_targetinst(viopath_hostLp), 0,
233 VIOVERSION << 16, ((u64)device_no << 48), 0, 0, 0);
234 if (hvrc != 0)
235 printk(VIOCD_KERN_WARNING
236 "bad rc on HvCallEvent_signalLpEventFast %d\n",
237 (int)hvrc);
238}
239
240/* Send a read or write request to OS/400 */
241static int send_request(struct request *req)
242{
243 HvLpEvent_Rc hvrc;
244 struct disk_info *diskinfo = req->rq_disk->private_data;
245 u64 len;
246 dma_addr_t dmaaddr;
247 int direction;
248 u16 cmd;
249 struct scatterlist sg;
250
251 BUG_ON(req->nr_phys_segments > 1);
252
253 if (rq_data_dir(req) == READ) {
254 direction = DMA_FROM_DEVICE;
255 cmd = viomajorsubtype_cdio | viocdread;
256 } else {
257 direction = DMA_TO_DEVICE;
258 cmd = viomajorsubtype_cdio | viocdwrite;
259 }
260
Jens Axboe3d1266c2007-10-24 13:21:21 +0200261 sg_init_table(&sg, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 if (blk_rq_map_sg(req->q, req, &sg) == 0) {
263 printk(VIOCD_KERN_WARNING
264 "error setting up scatter/gather list\n");
265 return -1;
266 }
267
268 if (dma_map_sg(diskinfo->dev, &sg, 1, direction) == 0) {
269 printk(VIOCD_KERN_WARNING "error allocating sg tce\n");
270 return -1;
271 }
272 dmaaddr = sg_dma_address(&sg);
273 len = sg_dma_len(&sg);
274
275 hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
276 HvLpEvent_Type_VirtualIo, cmd,
277 HvLpEvent_AckInd_DoAck,
278 HvLpEvent_AckType_ImmediateAck,
279 viopath_sourceinst(viopath_hostLp),
280 viopath_targetinst(viopath_hostLp),
281 (u64)req, VIOVERSION << 16,
282 ((u64)DEVICE_NR(diskinfo) << 48) | dmaaddr,
283 (u64)req->sector * 512, len, 0);
284 if (hvrc != HvLpEvent_Rc_Good) {
285 printk(VIOCD_KERN_WARNING "hv error on op %d\n", (int)hvrc);
286 return -1;
287 }
288
289 return 0;
290}
291
Kiyoshi Uedae935eb92007-12-11 17:47:52 -0500292static void viocd_end_request(struct request *req, int error)
Tony Breeds1ad7c312007-03-05 00:30:14 -0800293{
294 int nsectors = req->hard_nr_sectors;
295
296 /*
297 * Make sure it's fully ended, and ensure that we process
298 * at least one sector.
299 */
300 if (blk_pc_request(req))
301 nsectors = (req->data_len + 511) >> 9;
302 if (!nsectors)
303 nsectors = 1;
304
Kiyoshi Uedae935eb92007-12-11 17:47:52 -0500305 if (__blk_end_request(req, error, nsectors << 9))
Tony Breeds1ad7c312007-03-05 00:30:14 -0800306 BUG();
Tony Breeds1ad7c312007-03-05 00:30:14 -0800307}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
309static int rwreq;
310
Jens Axboe165125e2007-07-24 09:28:11 +0200311static void do_viocd_request(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312{
313 struct request *req;
314
315 while ((rwreq == 0) && ((req = elv_next_request(q)) != NULL)) {
316 if (!blk_fs_request(req))
Kiyoshi Uedae935eb92007-12-11 17:47:52 -0500317 viocd_end_request(req, -EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 else if (send_request(req) < 0) {
319 printk(VIOCD_KERN_WARNING
320 "unable to send message to OS/400!");
Kiyoshi Uedae935eb92007-12-11 17:47:52 -0500321 viocd_end_request(req, -EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 } else
323 rwreq++;
324 }
325}
326
327static int viocd_media_changed(struct cdrom_device_info *cdi, int disc_nr)
328{
329 struct viocd_waitevent we;
330 HvLpEvent_Rc hvrc;
331 int device_no = DEVICE_NR((struct disk_info *)cdi->handle);
332
333 init_completion(&we.com);
334
335 /* Send the open event to OS/400 */
336 hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
337 HvLpEvent_Type_VirtualIo,
338 viomajorsubtype_cdio | viocdcheck,
339 HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck,
340 viopath_sourceinst(viopath_hostLp),
341 viopath_targetinst(viopath_hostLp),
342 (u64)&we, VIOVERSION << 16, ((u64)device_no << 48),
343 0, 0, 0);
344 if (hvrc != 0) {
345 printk(VIOCD_KERN_WARNING "bad rc on HvCallEvent_signalLpEventFast %d\n",
346 (int)hvrc);
347 return -EIO;
348 }
349
350 wait_for_completion(&we.com);
351
352 /* Check the return code. If bad, assume no change */
353 if (we.rc) {
354 const struct vio_error_entry *err =
355 vio_lookup_rc(viocd_err_table, we.sub_result);
356 printk(VIOCD_KERN_WARNING
357 "bad rc %d:0x%04X on check_change: %s; Assuming no change\n",
358 we.rc, we.sub_result, err->msg);
359 return 0;
360 }
361
362 return we.changed;
363}
364
365static int viocd_lock_door(struct cdrom_device_info *cdi, int locking)
366{
367 HvLpEvent_Rc hvrc;
368 u64 device_no = DEVICE_NR((struct disk_info *)cdi->handle);
369 /* NOTE: flags is 1 or 0 so it won't overwrite the device_no */
370 u64 flags = !!locking;
371 struct viocd_waitevent we;
372
373 init_completion(&we.com);
374
375 /* Send the lockdoor event to OS/400 */
376 hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
377 HvLpEvent_Type_VirtualIo,
378 viomajorsubtype_cdio | viocdlockdoor,
379 HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck,
380 viopath_sourceinst(viopath_hostLp),
381 viopath_targetinst(viopath_hostLp),
382 (u64)&we, VIOVERSION << 16,
383 (device_no << 48) | (flags << 32), 0, 0, 0);
384 if (hvrc != 0) {
385 printk(VIOCD_KERN_WARNING "bad rc on HvCallEvent_signalLpEventFast %d\n",
386 (int)hvrc);
387 return -EIO;
388 }
389
390 wait_for_completion(&we.com);
391
392 if (we.rc != 0)
393 return -EIO;
394 return 0;
395}
396
397static int viocd_packet(struct cdrom_device_info *cdi,
398 struct packet_command *cgc)
399{
400 unsigned int buflen = cgc->buflen;
401 int ret = -EIO;
402
403 switch (cgc->cmd[0]) {
404 case GPCMD_READ_DISC_INFO:
405 {
406 disc_information *di = (disc_information *)cgc->buffer;
407
408 if (buflen >= 2) {
409 di->disc_information_length = cpu_to_be16(1);
410 ret = 0;
411 }
412 if (buflen >= 3)
413 di->erasable =
414 (cdi->ops->capability & ~cdi->mask
415 & (CDC_DVD_RAM | CDC_RAM)) != 0;
416 }
417 break;
Stephen Rothwell9db29252005-05-27 12:52:58 -0700418 case GPCMD_GET_CONFIGURATION:
419 if (cgc->cmd[3] == CDF_RWRT) {
420 struct rwrt_feature_desc *rfd = (struct rwrt_feature_desc *)(cgc->buffer + sizeof(struct feature_header));
421
422 if ((buflen >=
423 (sizeof(struct feature_header) + sizeof(*rfd))) &&
424 (cdi->ops->capability & ~cdi->mask
425 & (CDC_DVD_RAM | CDC_RAM))) {
426 rfd->feature_code = cpu_to_be16(CDF_RWRT);
427 rfd->curr = 1;
428 ret = 0;
429 }
430 }
431 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 default:
433 if (cgc->sense) {
434 /* indicate Unknown code */
435 cgc->sense->sense_key = 0x05;
436 cgc->sense->asc = 0x20;
437 cgc->sense->ascq = 0x00;
438 }
439 break;
440 }
441
442 cgc->stat = ret;
443 return ret;
444}
445
446static void restart_all_queues(int first_index)
447{
448 int i;
449
450 for (i = first_index + 1; i < viocd_numdev; i++)
451 if (viocd_diskinfo[i].viocd_disk)
452 blk_run_queue(viocd_diskinfo[i].viocd_disk->queue);
453 for (i = 0; i <= first_index; i++)
454 if (viocd_diskinfo[i].viocd_disk)
455 blk_run_queue(viocd_diskinfo[i].viocd_disk->queue);
456}
457
458/* This routine handles incoming CD LP events */
459static 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 */
Stephen Rothwell677f8c02006-01-12 13:47:43 +1100472 if (hvlpevent_is_int(event)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 printk(VIOCD_KERN_WARNING
474 "Yikes! got an int in viocd event handler!\n");
Stephen Rothwell677f8c02006-01-12 13:47:43 +1100475 if (hvlpevent_need_ack(event)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 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(di->viocd_disk->queue,
488 bevent->block_size);
489 set_capacity(di->viocd_disk,
490 bevent->media_size *
491 bevent->block_size / 512);
492 }
493 /* FALLTHROUGH !! */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 case viocdlockdoor:
495 pwe = (struct viocd_waitevent *)event->xCorrelationToken;
496return_complete:
497 pwe->rc = event->xRc;
498 pwe->sub_result = bevent->sub_result;
499 complete(&pwe->com);
500 break;
501
502 case viocdcheck:
503 pwe = (struct viocd_waitevent *)event->xCorrelationToken;
504 pwe->changed = bevent->flags;
505 goto return_complete;
506
507 case viocdclose:
508 break;
509
510 case viocdwrite:
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 di = &viocd_diskinfo[bevent->disk];
517 spin_lock_irqsave(&viocd_reqlock, flags);
518 dma_unmap_single(di->dev, bevent->token, bevent->len,
519 ((event->xSubtype & VIOMINOR_SUBTYPE_MASK) == viocdread)
520 ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
521 req = (struct request *)bevent->event.xCorrelationToken;
522 rwreq--;
523
524 if (event->xRc != HvLpEvent_Rc_Good) {
525 const struct vio_error_entry *err =
526 vio_lookup_rc(viocd_err_table,
527 bevent->sub_result);
528 printk(VIOCD_KERN_WARNING "request %p failed "
529 "with rc %d:0x%04X: %s\n",
530 req, event->xRc,
531 bevent->sub_result, err->msg);
Kiyoshi Uedae935eb92007-12-11 17:47:52 -0500532 viocd_end_request(req, -EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 } else
Kiyoshi Uedae935eb92007-12-11 17:47:52 -0500534 viocd_end_request(req, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
536 /* restart handling of incoming requests */
537 spin_unlock_irqrestore(&viocd_reqlock, flags);
538 restart_all_queues(bevent->disk);
539 break;
540
541 default:
542 printk(VIOCD_KERN_WARNING
543 "message with invalid subtype %0x04X!\n",
544 event->xSubtype & VIOMINOR_SUBTYPE_MASK);
Stephen Rothwell677f8c02006-01-12 13:47:43 +1100545 if (hvlpevent_need_ack(event)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 event->xRc = HvLpEvent_Rc_InvalidSubtype;
547 HvCallEvent_ackLpEvent(event);
548 }
549 }
550}
551
552static struct cdrom_device_ops viocd_dops = {
553 .open = viocd_open,
554 .release = viocd_release,
555 .media_changed = viocd_media_changed,
556 .lock_door = viocd_lock_door,
557 .generic_packet = viocd_packet,
Christoph Hellwig6a2900b2006-03-23 03:00:15 -0800558 .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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559};
560
Stephen Rothwellc7211922008-02-05 14:17:31 +1100561static int find_capability(const char *type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562{
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
571static 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 struct request_queue *q;
Stephen Rothwellb833b482007-10-11 14:57:26 +1000578 struct device_node *node = vdev->dev.archdata.of_node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
580 deviceno = vdev->unit_address;
Stephen Rothwellb833b482007-10-11 14:57:26 +1000581 if (deviceno > VIOCD_MAX_CD)
582 return -ENODEV;
583 if (!node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 return -ENODEV;
585
Stephen Rothwellb833b482007-10-11 14:57:26 +1000586 if (deviceno >= viocd_numdev)
587 viocd_numdev = deviceno + 1;
588
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 d = &viocd_diskinfo[deviceno];
Stephen Rothwellb833b482007-10-11 14:57:26 +1000590 d->rsrcname = of_get_property(node, "linux,vio_rsrcname", NULL);
591 d->type = of_get_property(node, "linux,vio_type", NULL);
592 d->model = of_get_property(node, "linux,vio_model", NULL);
593
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 c = &d->viocd_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
596 c->ops = &viocd_dops;
597 c->speed = 4;
598 c->capacity = 1;
599 c->handle = d;
Stephen Rothwellb833b482007-10-11 14:57:26 +1000600 c->mask = ~find_capability(d->type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 sprintf(c->name, VIOCD_DEVICE "%c", 'a' + deviceno);
602
603 if (register_cdrom(c) != 0) {
604 printk(VIOCD_KERN_WARNING "Cannot register viocd CD-ROM %s!\n",
605 c->name);
606 goto out;
607 }
608 printk(VIOCD_KERN_INFO "cd %s is iSeries resource %10.10s "
609 "type %4.4s, model %3.3s\n",
Stephen Rothwellb833b482007-10-11 14:57:26 +1000610 c->name, d->rsrcname, d->type, d->model);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 q = blk_init_queue(do_viocd_request, &viocd_reqlock);
612 if (q == NULL) {
613 printk(VIOCD_KERN_WARNING "Cannot allocate queue for %s!\n",
614 c->name);
615 goto out_unregister_cdrom;
616 }
617 gendisk = alloc_disk(1);
618 if (gendisk == NULL) {
619 printk(VIOCD_KERN_WARNING "Cannot create gendisk for %s!\n",
620 c->name);
621 goto out_cleanup_queue;
622 }
623 gendisk->major = VIOCD_MAJOR;
624 gendisk->first_minor = deviceno;
625 strncpy(gendisk->disk_name, c->name,
626 sizeof(gendisk->disk_name));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 blk_queue_max_hw_segments(q, 1);
628 blk_queue_max_phys_segments(q, 1);
629 blk_queue_max_sectors(q, 4096 / 512);
630 gendisk->queue = q;
631 gendisk->fops = &viocd_fops;
632 gendisk->flags = GENHD_FL_CD|GENHD_FL_REMOVABLE;
633 set_capacity(gendisk, 0);
634 gendisk->private_data = d;
635 d->viocd_disk = gendisk;
636 d->dev = &vdev->dev;
637 gendisk->driverfs_dev = d->dev;
638 add_disk(gendisk);
639 return 0;
640
641out_cleanup_queue:
642 blk_cleanup_queue(q);
643out_unregister_cdrom:
644 unregister_cdrom(c);
645out:
646 return -ENODEV;
647}
648
649static int viocd_remove(struct vio_dev *vdev)
650{
651 struct disk_info *d = &viocd_diskinfo[vdev->unit_address];
652
653 if (unregister_cdrom(&d->viocd_info) != 0)
654 printk(VIOCD_KERN_WARNING
655 "Cannot unregister viocd CD-ROM %s!\n",
656 d->viocd_info.name);
657 del_gendisk(d->viocd_disk);
658 blk_cleanup_queue(d->viocd_disk->queue);
659 put_disk(d->viocd_disk);
660 return 0;
661}
662
663/**
664 * viocd_device_table: Used by vio.c to match devices that we
665 * support.
666 */
667static struct vio_device_id viocd_device_table[] __devinitdata = {
Stephen Rothwellde0fe3b2006-05-15 13:44:01 +1000668 { "block", "IBM,iSeries-viocd" },
Stephen Rothwellfb120da2005-08-17 16:42:59 +1000669 { "", "" }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671MODULE_DEVICE_TABLE(vio, viocd_device_table);
Stephen Rothwell915124d2005-10-24 15:12:22 +1000672
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673static struct vio_driver viocd_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 .id_table = viocd_device_table,
675 .probe = viocd_probe,
Stephen Rothwell6fdf5392005-10-24 14:53:21 +1000676 .remove = viocd_remove,
677 .driver = {
678 .name = "viocd",
Stephen Rothwell915124d2005-10-24 15:12:22 +1000679 .owner = THIS_MODULE,
Stephen Rothwell6fdf5392005-10-24 14:53:21 +1000680 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681};
682
683static int __init viocd_init(void)
684{
685 struct proc_dir_entry *e;
686 int ret = 0;
687
Stephen Rothwell31c72ad2006-12-15 15:44:04 +1100688 if (!firmware_has_feature(FW_FEATURE_ISERIES))
689 return -ENODEV;
690
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 if (viopath_hostLp == HvLpIndexInvalid) {
692 vio_set_hostlp();
693 /* If we don't have a host, bail out */
694 if (viopath_hostLp == HvLpIndexInvalid)
695 return -ENODEV;
696 }
697
698 printk(VIOCD_KERN_INFO "vers " VIOCD_VERS ", hosting partition %d\n",
699 viopath_hostLp);
700
701 if (register_blkdev(VIOCD_MAJOR, VIOCD_DEVICE) != 0) {
702 printk(VIOCD_KERN_WARNING "Unable to get major %d for %s\n",
703 VIOCD_MAJOR, VIOCD_DEVICE);
704 return -EIO;
705 }
706
707 ret = viopath_open(viopath_hostLp, viomajorsubtype_cdio,
708 MAX_CD_REQ + 2);
709 if (ret) {
710 printk(VIOCD_KERN_WARNING
711 "error opening path to host partition %d\n",
712 viopath_hostLp);
713 goto out_unregister;
714 }
715
716 /* Initialize our request handler */
717 vio_setHandler(viomajorsubtype_cdio, vio_handle_cd_event);
718
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 spin_lock_init(&viocd_reqlock);
720
721 ret = vio_register_driver(&viocd_driver);
722 if (ret)
723 goto out_free_info;
724
725 e = create_proc_entry("iSeries/viocd", S_IFREG|S_IRUGO, NULL);
726 if (e) {
727 e->owner = THIS_MODULE;
728 e->proc_fops = &proc_viocd_operations;
729 }
730
731 return 0;
732
733out_free_info:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 vio_clearHandler(viomajorsubtype_cdio);
735 viopath_close(viopath_hostLp, viomajorsubtype_cdio, MAX_CD_REQ + 2);
736out_unregister:
737 unregister_blkdev(VIOCD_MAJOR, VIOCD_DEVICE);
738 return ret;
739}
740
741static void __exit viocd_exit(void)
742{
743 remove_proc_entry("iSeries/viocd", NULL);
744 vio_unregister_driver(&viocd_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 viopath_close(viopath_hostLp, viomajorsubtype_cdio, MAX_CD_REQ + 2);
746 vio_clearHandler(viomajorsubtype_cdio);
747 unregister_blkdev(VIOCD_MAJOR, VIOCD_DEVICE);
748}
749
750module_init(viocd_init);
751module_exit(viocd_exit);
752MODULE_LICENSE("GPL");