blob: 13929356135c837743f130c2f7de4d3cd85cc4fb [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>
Stephen Rothwell8962cad2008-05-23 11:41:46 +10009 * Stephen Rothwell
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 *
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 = {
Denis V. Lunevc7705f3442008-04-29 01:02:35 -0700147 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 .open = proc_viocd_open,
149 .read = seq_read,
150 .llseek = seq_lseek,
151 .release = single_release,
152};
153
Al Viro4e379ae2008-03-02 10:25:44 -0500154static int viocd_blk_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155{
Al Viro4e379ae2008-03-02 10:25:44 -0500156 struct disk_info *di = bdev->bd_disk->private_data;
157 return cdrom_open(&di->viocd_info, bdev, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158}
159
Al Viro4e379ae2008-03-02 10:25:44 -0500160static int viocd_blk_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161{
Al Viro4e379ae2008-03-02 10:25:44 -0500162 struct disk_info *di = disk->private_data;
163 cdrom_release(&di->viocd_info, mode);
Al Virobbc1cc92007-10-07 17:54:28 -0400164 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165}
166
Al Viro4e379ae2008-03-02 10:25:44 -0500167static int viocd_blk_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 unsigned cmd, unsigned long arg)
169{
Al Viro4e379ae2008-03-02 10:25:44 -0500170 struct disk_info *di = bdev->bd_disk->private_data;
171 return cdrom_ioctl(&di->viocd_info, bdev, mode, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172}
173
174static 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
180struct block_device_operations viocd_fops = {
181 .owner = THIS_MODULE,
Al Viro4e379ae2008-03-02 10:25:44 -0500182 .open = viocd_blk_open,
183 .release = viocd_blk_release,
184 .locked_ioctl = viocd_blk_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 .media_changed = viocd_blk_media_changed,
186};
187
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188static 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
224static 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 */
243static 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
Jens Axboe3d1266c2007-10-24 13:21:21 +0200263 sg_init_table(&sg, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 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)req->sector * 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
Kiyoshi Uedae935eb92007-12-11 17:47:52 -0500294static void viocd_end_request(struct request *req, int error)
Tony Breeds1ad7c312007-03-05 00:30:14 -0800295{
296 int nsectors = req->hard_nr_sectors;
297
298 /*
299 * Make sure it's fully ended, and ensure that we process
300 * at least one sector.
301 */
302 if (blk_pc_request(req))
303 nsectors = (req->data_len + 511) >> 9;
304 if (!nsectors)
305 nsectors = 1;
306
Kiyoshi Uedae935eb92007-12-11 17:47:52 -0500307 if (__blk_end_request(req, error, nsectors << 9))
Tony Breeds1ad7c312007-03-05 00:30:14 -0800308 BUG();
Tony Breeds1ad7c312007-03-05 00:30:14 -0800309}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
311static int rwreq;
312
Jens Axboe165125e2007-07-24 09:28:11 +0200313static void do_viocd_request(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314{
315 struct request *req;
316
317 while ((rwreq == 0) && ((req = elv_next_request(q)) != NULL)) {
318 if (!blk_fs_request(req))
Kiyoshi Uedae935eb92007-12-11 17:47:52 -0500319 viocd_end_request(req, -EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 else if (send_request(req) < 0) {
321 printk(VIOCD_KERN_WARNING
322 "unable to send message to OS/400!");
Kiyoshi Uedae935eb92007-12-11 17:47:52 -0500323 viocd_end_request(req, -EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 } else
325 rwreq++;
326 }
327}
328
329static int viocd_media_changed(struct cdrom_device_info *cdi, int disc_nr)
330{
331 struct viocd_waitevent we;
332 HvLpEvent_Rc hvrc;
333 int device_no = DEVICE_NR((struct disk_info *)cdi->handle);
334
335 init_completion(&we.com);
336
337 /* Send the open event to OS/400 */
338 hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
339 HvLpEvent_Type_VirtualIo,
340 viomajorsubtype_cdio | viocdcheck,
341 HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck,
342 viopath_sourceinst(viopath_hostLp),
343 viopath_targetinst(viopath_hostLp),
344 (u64)&we, VIOVERSION << 16, ((u64)device_no << 48),
345 0, 0, 0);
346 if (hvrc != 0) {
347 printk(VIOCD_KERN_WARNING "bad rc on HvCallEvent_signalLpEventFast %d\n",
348 (int)hvrc);
349 return -EIO;
350 }
351
352 wait_for_completion(&we.com);
353
354 /* Check the return code. If bad, assume no change */
355 if (we.rc) {
356 const struct vio_error_entry *err =
357 vio_lookup_rc(viocd_err_table, we.sub_result);
358 printk(VIOCD_KERN_WARNING
359 "bad rc %d:0x%04X on check_change: %s; Assuming no change\n",
360 we.rc, we.sub_result, err->msg);
361 return 0;
362 }
363
364 return we.changed;
365}
366
367static int viocd_lock_door(struct cdrom_device_info *cdi, int locking)
368{
369 HvLpEvent_Rc hvrc;
370 u64 device_no = DEVICE_NR((struct disk_info *)cdi->handle);
371 /* NOTE: flags is 1 or 0 so it won't overwrite the device_no */
372 u64 flags = !!locking;
373 struct viocd_waitevent we;
374
375 init_completion(&we.com);
376
377 /* Send the lockdoor event to OS/400 */
378 hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
379 HvLpEvent_Type_VirtualIo,
380 viomajorsubtype_cdio | viocdlockdoor,
381 HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck,
382 viopath_sourceinst(viopath_hostLp),
383 viopath_targetinst(viopath_hostLp),
384 (u64)&we, VIOVERSION << 16,
385 (device_no << 48) | (flags << 32), 0, 0, 0);
386 if (hvrc != 0) {
387 printk(VIOCD_KERN_WARNING "bad rc on HvCallEvent_signalLpEventFast %d\n",
388 (int)hvrc);
389 return -EIO;
390 }
391
392 wait_for_completion(&we.com);
393
394 if (we.rc != 0)
395 return -EIO;
396 return 0;
397}
398
399static int viocd_packet(struct cdrom_device_info *cdi,
400 struct packet_command *cgc)
401{
402 unsigned int buflen = cgc->buflen;
403 int ret = -EIO;
404
405 switch (cgc->cmd[0]) {
406 case GPCMD_READ_DISC_INFO:
407 {
408 disc_information *di = (disc_information *)cgc->buffer;
409
410 if (buflen >= 2) {
411 di->disc_information_length = cpu_to_be16(1);
412 ret = 0;
413 }
414 if (buflen >= 3)
415 di->erasable =
416 (cdi->ops->capability & ~cdi->mask
417 & (CDC_DVD_RAM | CDC_RAM)) != 0;
418 }
419 break;
Stephen Rothwell9db29252005-05-27 12:52:58 -0700420 case GPCMD_GET_CONFIGURATION:
421 if (cgc->cmd[3] == CDF_RWRT) {
422 struct rwrt_feature_desc *rfd = (struct rwrt_feature_desc *)(cgc->buffer + sizeof(struct feature_header));
423
424 if ((buflen >=
425 (sizeof(struct feature_header) + sizeof(*rfd))) &&
426 (cdi->ops->capability & ~cdi->mask
427 & (CDC_DVD_RAM | CDC_RAM))) {
428 rfd->feature_code = cpu_to_be16(CDF_RWRT);
429 rfd->curr = 1;
430 ret = 0;
431 }
432 }
433 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 default:
435 if (cgc->sense) {
436 /* indicate Unknown code */
437 cgc->sense->sense_key = 0x05;
438 cgc->sense->asc = 0x20;
439 cgc->sense->ascq = 0x00;
440 }
441 break;
442 }
443
444 cgc->stat = ret;
445 return ret;
446}
447
448static void restart_all_queues(int first_index)
449{
450 int i;
451
452 for (i = first_index + 1; i < viocd_numdev; i++)
453 if (viocd_diskinfo[i].viocd_disk)
454 blk_run_queue(viocd_diskinfo[i].viocd_disk->queue);
455 for (i = 0; i <= first_index; i++)
456 if (viocd_diskinfo[i].viocd_disk)
457 blk_run_queue(viocd_diskinfo[i].viocd_disk->queue);
458}
459
460/* This routine handles incoming CD LP events */
461static void vio_handle_cd_event(struct HvLpEvent *event)
462{
463 struct viocdlpevent *bevent;
464 struct viocd_waitevent *pwe;
465 struct disk_info *di;
466 unsigned long flags;
467 struct request *req;
468
469
470 if (event == NULL)
471 /* Notification that a partition went away! */
472 return;
473 /* First, we should NEVER get an int here...only acks */
Stephen Rothwell677f8c02006-01-12 13:47:43 +1100474 if (hvlpevent_is_int(event)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 printk(VIOCD_KERN_WARNING
476 "Yikes! got an int in viocd event handler!\n");
Stephen Rothwell677f8c02006-01-12 13:47:43 +1100477 if (hvlpevent_need_ack(event)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 event->xRc = HvLpEvent_Rc_InvalidSubtype;
479 HvCallEvent_ackLpEvent(event);
480 }
481 }
482
483 bevent = (struct viocdlpevent *)event;
484
485 switch (event->xSubtype & VIOMINOR_SUBTYPE_MASK) {
486 case viocdopen:
487 if (event->xRc == 0) {
488 di = &viocd_diskinfo[bevent->disk];
489 blk_queue_hardsect_size(di->viocd_disk->queue,
490 bevent->block_size);
491 set_capacity(di->viocd_disk,
492 bevent->media_size *
493 bevent->block_size / 512);
494 }
495 /* FALLTHROUGH !! */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 case viocdlockdoor:
497 pwe = (struct viocd_waitevent *)event->xCorrelationToken;
498return_complete:
499 pwe->rc = event->xRc;
500 pwe->sub_result = bevent->sub_result;
501 complete(&pwe->com);
502 break;
503
504 case viocdcheck:
505 pwe = (struct viocd_waitevent *)event->xCorrelationToken;
506 pwe->changed = bevent->flags;
507 goto return_complete;
508
509 case viocdclose:
510 break;
511
512 case viocdwrite:
513 case viocdread:
514 /*
515 * Since this is running in interrupt mode, we need to
516 * make sure we're not stepping on any global I/O operations
517 */
518 di = &viocd_diskinfo[bevent->disk];
519 spin_lock_irqsave(&viocd_reqlock, flags);
520 dma_unmap_single(di->dev, bevent->token, bevent->len,
521 ((event->xSubtype & VIOMINOR_SUBTYPE_MASK) == viocdread)
522 ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
523 req = (struct request *)bevent->event.xCorrelationToken;
524 rwreq--;
525
526 if (event->xRc != HvLpEvent_Rc_Good) {
527 const struct vio_error_entry *err =
528 vio_lookup_rc(viocd_err_table,
529 bevent->sub_result);
530 printk(VIOCD_KERN_WARNING "request %p failed "
531 "with rc %d:0x%04X: %s\n",
532 req, event->xRc,
533 bevent->sub_result, err->msg);
Kiyoshi Uedae935eb92007-12-11 17:47:52 -0500534 viocd_end_request(req, -EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 } else
Kiyoshi Uedae935eb92007-12-11 17:47:52 -0500536 viocd_end_request(req, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
538 /* restart handling of incoming requests */
539 spin_unlock_irqrestore(&viocd_reqlock, flags);
540 restart_all_queues(bevent->disk);
541 break;
542
543 default:
544 printk(VIOCD_KERN_WARNING
545 "message with invalid subtype %0x04X!\n",
546 event->xSubtype & VIOMINOR_SUBTYPE_MASK);
Stephen Rothwell677f8c02006-01-12 13:47:43 +1100547 if (hvlpevent_need_ack(event)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 event->xRc = HvLpEvent_Rc_InvalidSubtype;
549 HvCallEvent_ackLpEvent(event);
550 }
551 }
552}
553
Borislav Petkov3e636f72008-08-18 21:40:03 +0200554static int viocd_audio_ioctl(struct cdrom_device_info *cdi, unsigned int cmd,
555 void *arg)
556{
557 return -EINVAL;
558}
559
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560static struct cdrom_device_ops viocd_dops = {
561 .open = viocd_open,
562 .release = viocd_release,
563 .media_changed = viocd_media_changed,
564 .lock_door = viocd_lock_door,
565 .generic_packet = viocd_packet,
Borislav Petkov3e636f72008-08-18 21:40:03 +0200566 .audio_ioctl = viocd_audio_ioctl,
Christoph Hellwig6a2900b2006-03-23 03:00:15 -0800567 .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 -0700568};
569
Stephen Rothwellc7211922008-02-05 14:17:31 +1100570static int find_capability(const char *type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571{
572 struct capability_entry *entry;
573
574 for(entry = capability_table; entry->type; ++entry)
575 if(!strncmp(entry->type, type, 4))
576 break;
577 return entry->capability;
578}
579
580static int viocd_probe(struct vio_dev *vdev, const struct vio_device_id *id)
581{
582 struct gendisk *gendisk;
583 int deviceno;
584 struct disk_info *d;
585 struct cdrom_device_info *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 struct request_queue *q;
Stephen Rothwellb833b482007-10-11 14:57:26 +1000587 struct device_node *node = vdev->dev.archdata.of_node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
589 deviceno = vdev->unit_address;
Stephen Rothwellb833b482007-10-11 14:57:26 +1000590 if (deviceno > VIOCD_MAX_CD)
591 return -ENODEV;
592 if (!node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 return -ENODEV;
594
Stephen Rothwellb833b482007-10-11 14:57:26 +1000595 if (deviceno >= viocd_numdev)
596 viocd_numdev = deviceno + 1;
597
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 d = &viocd_diskinfo[deviceno];
Stephen Rothwellb833b482007-10-11 14:57:26 +1000599 d->rsrcname = of_get_property(node, "linux,vio_rsrcname", NULL);
600 d->type = of_get_property(node, "linux,vio_type", NULL);
601 d->model = of_get_property(node, "linux,vio_model", NULL);
602
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 c = &d->viocd_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
605 c->ops = &viocd_dops;
606 c->speed = 4;
607 c->capacity = 1;
608 c->handle = d;
Stephen Rothwellb833b482007-10-11 14:57:26 +1000609 c->mask = ~find_capability(d->type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 sprintf(c->name, VIOCD_DEVICE "%c", 'a' + deviceno);
611
612 if (register_cdrom(c) != 0) {
613 printk(VIOCD_KERN_WARNING "Cannot register viocd CD-ROM %s!\n",
614 c->name);
615 goto out;
616 }
617 printk(VIOCD_KERN_INFO "cd %s is iSeries resource %10.10s "
618 "type %4.4s, model %3.3s\n",
Stephen Rothwellb833b482007-10-11 14:57:26 +1000619 c->name, d->rsrcname, d->type, d->model);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 q = blk_init_queue(do_viocd_request, &viocd_reqlock);
621 if (q == NULL) {
622 printk(VIOCD_KERN_WARNING "Cannot allocate queue for %s!\n",
623 c->name);
624 goto out_unregister_cdrom;
625 }
626 gendisk = alloc_disk(1);
627 if (gendisk == NULL) {
628 printk(VIOCD_KERN_WARNING "Cannot create gendisk for %s!\n",
629 c->name);
630 goto out_cleanup_queue;
631 }
632 gendisk->major = VIOCD_MAJOR;
633 gendisk->first_minor = deviceno;
634 strncpy(gendisk->disk_name, c->name,
635 sizeof(gendisk->disk_name));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 blk_queue_max_hw_segments(q, 1);
637 blk_queue_max_phys_segments(q, 1);
638 blk_queue_max_sectors(q, 4096 / 512);
639 gendisk->queue = q;
640 gendisk->fops = &viocd_fops;
641 gendisk->flags = GENHD_FL_CD|GENHD_FL_REMOVABLE;
642 set_capacity(gendisk, 0);
643 gendisk->private_data = d;
644 d->viocd_disk = gendisk;
645 d->dev = &vdev->dev;
646 gendisk->driverfs_dev = d->dev;
647 add_disk(gendisk);
648 return 0;
649
650out_cleanup_queue:
651 blk_cleanup_queue(q);
652out_unregister_cdrom:
653 unregister_cdrom(c);
654out:
655 return -ENODEV;
656}
657
658static int viocd_remove(struct vio_dev *vdev)
659{
660 struct disk_info *d = &viocd_diskinfo[vdev->unit_address];
661
Akinobu Mita0a0c4112008-03-26 12:09:02 +0100662 unregister_cdrom(&d->viocd_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 del_gendisk(d->viocd_disk);
664 blk_cleanup_queue(d->viocd_disk->queue);
665 put_disk(d->viocd_disk);
666 return 0;
667}
668
669/**
670 * viocd_device_table: Used by vio.c to match devices that we
671 * support.
672 */
673static struct vio_device_id viocd_device_table[] __devinitdata = {
Stephen Rothwellde0fe3b2006-05-15 13:44:01 +1000674 { "block", "IBM,iSeries-viocd" },
Stephen Rothwellfb120da2005-08-17 16:42:59 +1000675 { "", "" }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677MODULE_DEVICE_TABLE(vio, viocd_device_table);
Stephen Rothwell915124d2005-10-24 15:12:22 +1000678
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679static struct vio_driver viocd_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 .id_table = viocd_device_table,
681 .probe = viocd_probe,
Stephen Rothwell6fdf5392005-10-24 14:53:21 +1000682 .remove = viocd_remove,
683 .driver = {
684 .name = "viocd",
Stephen Rothwell915124d2005-10-24 15:12:22 +1000685 .owner = THIS_MODULE,
Stephen Rothwell6fdf5392005-10-24 14:53:21 +1000686 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687};
688
689static int __init viocd_init(void)
690{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 int ret = 0;
692
Stephen Rothwell31c72ad2006-12-15 15:44:04 +1100693 if (!firmware_has_feature(FW_FEATURE_ISERIES))
694 return -ENODEV;
695
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 if (viopath_hostLp == HvLpIndexInvalid) {
697 vio_set_hostlp();
698 /* If we don't have a host, bail out */
699 if (viopath_hostLp == HvLpIndexInvalid)
700 return -ENODEV;
701 }
702
703 printk(VIOCD_KERN_INFO "vers " VIOCD_VERS ", hosting partition %d\n",
704 viopath_hostLp);
705
706 if (register_blkdev(VIOCD_MAJOR, VIOCD_DEVICE) != 0) {
707 printk(VIOCD_KERN_WARNING "Unable to get major %d for %s\n",
708 VIOCD_MAJOR, VIOCD_DEVICE);
709 return -EIO;
710 }
711
712 ret = viopath_open(viopath_hostLp, viomajorsubtype_cdio,
713 MAX_CD_REQ + 2);
714 if (ret) {
715 printk(VIOCD_KERN_WARNING
716 "error opening path to host partition %d\n",
717 viopath_hostLp);
718 goto out_unregister;
719 }
720
721 /* Initialize our request handler */
722 vio_setHandler(viomajorsubtype_cdio, vio_handle_cd_event);
723
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 spin_lock_init(&viocd_reqlock);
725
726 ret = vio_register_driver(&viocd_driver);
727 if (ret)
728 goto out_free_info;
729
Denis V. Lunevc7705f3442008-04-29 01:02:35 -0700730 proc_create("iSeries/viocd", S_IFREG|S_IRUGO, NULL,
731 &proc_viocd_operations);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 return 0;
733
734out_free_info:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 vio_clearHandler(viomajorsubtype_cdio);
736 viopath_close(viopath_hostLp, viomajorsubtype_cdio, MAX_CD_REQ + 2);
737out_unregister:
738 unregister_blkdev(VIOCD_MAJOR, VIOCD_DEVICE);
739 return ret;
740}
741
742static void __exit viocd_exit(void)
743{
744 remove_proc_entry("iSeries/viocd", NULL);
745 vio_unregister_driver(&viocd_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 viopath_close(viopath_hostLp, viomajorsubtype_cdio, MAX_CD_REQ + 2);
747 vio_clearHandler(viomajorsubtype_cdio);
748 unregister_blkdev(VIOCD_MAJOR, VIOCD_DEVICE);
749}
750
751module_init(viocd_init);
752module_exit(viocd_exit);
753MODULE_LICENSE("GPL");