blob: 1fa6628d150b7f93ad1a11fbbaf496e55b45e96e [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
Joe Perchese597cd02010-07-01 08:24:32 +020034#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
35
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#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/seq_file.h>
Jens Axboe3d1266c2007-10-24 13:21:21 +020046#include <linux/scatterlist.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <asm/vio.h>
Kelly Daly1ec65d72005-11-02 13:46:07 +110049#include <asm/iseries/hv_types.h>
Kelly Dalye45423e2005-11-02 12:08:31 +110050#include <asm/iseries/hv_lp_event.h>
Kelly Dalyb4206772005-11-02 15:13:57 +110051#include <asm/iseries/vio.h>
Stephen Rothwell31c72ad2006-12-15 15:44:04 +110052#include <asm/firmware.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
54#define VIOCD_DEVICE "iseries/vcd"
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
56#define VIOCD_VERS "1.06"
57
Linus Torvalds1da177e2005-04-16 15:20:36 -070058/*
59 * Should probably make this a module parameter....sigh
60 */
61#define VIOCD_MAX_CD HVMAXARCHITECTEDVIRTUALCDROMS
62
63static const struct vio_error_entry viocd_err_table[] = {
64 {0x0201, EINVAL, "Invalid Range"},
65 {0x0202, EINVAL, "Invalid Token"},
66 {0x0203, EIO, "DMA Error"},
67 {0x0204, EIO, "Use Error"},
68 {0x0205, EIO, "Release Error"},
69 {0x0206, EINVAL, "Invalid CD"},
70 {0x020C, EROFS, "Read Only Device"},
71 {0x020D, ENOMEDIUM, "Changed or Missing Volume (or Varied Off?)"},
72 {0x020E, EIO, "Optical System Error (Varied Off?)"},
73 {0x02FF, EIO, "Internal Error"},
74 {0x3010, EIO, "Changed Volume"},
75 {0xC100, EIO, "Optical System Error"},
76 {0x0000, 0, NULL},
77};
78
79/*
80 * This is the structure we use to exchange info between driver and interrupt
81 * handler
82 */
83struct viocd_waitevent {
84 struct completion com;
85 int rc;
86 u16 sub_result;
87 int changed;
88};
89
90/* this is a lookup table for the true capabilities of a device */
91struct capability_entry {
92 char *type;
93 int capability;
94};
95
96static struct capability_entry capability_table[] __initdata = {
97 { "6330", CDC_LOCK | CDC_DVD_RAM | CDC_RAM },
98 { "6331", CDC_LOCK | CDC_DVD_RAM | CDC_RAM },
99 { "6333", CDC_LOCK | CDC_DVD_RAM | CDC_RAM },
100 { "632A", CDC_LOCK | CDC_DVD_RAM | CDC_RAM },
101 { "6321", CDC_LOCK },
102 { "632B", 0 },
103 { NULL , CDC_LOCK },
104};
105
106/* These are our internal structures for keeping track of devices */
107static int viocd_numdev;
108
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109struct disk_info {
110 struct gendisk *viocd_disk;
111 struct cdrom_device_info viocd_info;
112 struct device *dev;
Stephen Rothwellb833b482007-10-11 14:57:26 +1000113 const char *rsrcname;
114 const char *type;
115 const char *model;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116};
117static struct disk_info viocd_diskinfo[VIOCD_MAX_CD];
118
119#define DEVICE_NR(di) ((di) - &viocd_diskinfo[0])
120
121static spinlock_t viocd_reqlock;
122
123#define MAX_CD_REQ 1
124
125/* procfs support */
126static int proc_viocd_show(struct seq_file *m, void *v)
127{
128 int i;
129
130 for (i = 0; i < viocd_numdev; i++) {
131 seq_printf(m, "viocd device %d is iSeries resource %10.10s"
132 "type %4.4s, model %3.3s\n",
Stephen Rothwellb833b482007-10-11 14:57:26 +1000133 i, viocd_diskinfo[i].rsrcname,
134 viocd_diskinfo[i].type,
135 viocd_diskinfo[i].model);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 }
137 return 0;
138}
139
140static int proc_viocd_open(struct inode *inode, struct file *file)
141{
142 return single_open(file, proc_viocd_show, NULL);
143}
144
Arjan van de Ven2b8693c2007-02-12 00:55:32 -0800145static const struct file_operations proc_viocd_operations = {
Denis V. Lunevc7705f32008-04-29 01:02:35 -0700146 .owner = THIS_MODULE,
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
Al Viro4e379ae2008-03-02 10:25:44 -0500153static int viocd_blk_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154{
Al Viro4e379ae2008-03-02 10:25:44 -0500155 struct disk_info *di = bdev->bd_disk->private_data;
156 return cdrom_open(&di->viocd_info, bdev, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157}
158
Al Viro4e379ae2008-03-02 10:25:44 -0500159static int viocd_blk_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160{
Al Viro4e379ae2008-03-02 10:25:44 -0500161 struct disk_info *di = disk->private_data;
162 cdrom_release(&di->viocd_info, mode);
Al Virobbc1cc92007-10-07 17:54:28 -0400163 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164}
165
Al Viro4e379ae2008-03-02 10:25:44 -0500166static int viocd_blk_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 unsigned cmd, unsigned long arg)
168{
Al Viro4e379ae2008-03-02 10:25:44 -0500169 struct disk_info *di = bdev->bd_disk->private_data;
170 return cdrom_ioctl(&di->viocd_info, bdev, mode, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171}
172
173static int viocd_blk_media_changed(struct gendisk *disk)
174{
175 struct disk_info *di = disk->private_data;
176 return cdrom_media_changed(&di->viocd_info);
177}
178
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700179static const struct block_device_operations viocd_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 .owner = THIS_MODULE,
Al Viro4e379ae2008-03-02 10:25:44 -0500181 .open = viocd_blk_open,
182 .release = viocd_blk_release,
183 .locked_ioctl = viocd_blk_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 .media_changed = viocd_blk_media_changed,
185};
186
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187static int viocd_open(struct cdrom_device_info *cdi, int purpose)
188{
189 struct disk_info *diskinfo = cdi->handle;
190 int device_no = DEVICE_NR(diskinfo);
191 HvLpEvent_Rc hvrc;
192 struct viocd_waitevent we;
193
194 init_completion(&we.com);
195 hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
196 HvLpEvent_Type_VirtualIo,
197 viomajorsubtype_cdio | viocdopen,
198 HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck,
199 viopath_sourceinst(viopath_hostLp),
200 viopath_targetinst(viopath_hostLp),
201 (u64)&we, VIOVERSION << 16, ((u64)device_no << 48),
202 0, 0, 0);
203 if (hvrc != 0) {
Joe Perchese597cd02010-07-01 08:24:32 +0200204 pr_warning("bad rc on HvCallEvent_signalLpEventFast %d\n",
205 (int)hvrc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 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);
Joe Perchese597cd02010-07-01 08:24:32 +0200214 pr_warning("bad rc %d:0x%04X on open: %s\n",
215 we.rc, we.sub_result, err->msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 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)
Joe Perchese597cd02010-07-01 08:24:32 +0200235 pr_warning("bad rc on HvCallEvent_signalLpEventFast %d\n",
236 (int)hvrc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237}
238
239/* Send a read or write request to OS/400 */
240static int send_request(struct request *req)
241{
242 HvLpEvent_Rc hvrc;
243 struct disk_info *diskinfo = req->rq_disk->private_data;
244 u64 len;
245 dma_addr_t dmaaddr;
246 int direction;
247 u16 cmd;
248 struct scatterlist sg;
249
250 BUG_ON(req->nr_phys_segments > 1);
251
252 if (rq_data_dir(req) == READ) {
253 direction = DMA_FROM_DEVICE;
254 cmd = viomajorsubtype_cdio | viocdread;
255 } else {
256 direction = DMA_TO_DEVICE;
257 cmd = viomajorsubtype_cdio | viocdwrite;
258 }
259
Jens Axboe3d1266c2007-10-24 13:21:21 +0200260 sg_init_table(&sg, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 if (blk_rq_map_sg(req->q, req, &sg) == 0) {
Joe Perchese597cd02010-07-01 08:24:32 +0200262 pr_warning("error setting up scatter/gather list\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 return -1;
264 }
265
266 if (dma_map_sg(diskinfo->dev, &sg, 1, direction) == 0) {
Joe Perchese597cd02010-07-01 08:24:32 +0200267 pr_warning("error allocating sg tce\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 return -1;
269 }
270 dmaaddr = sg_dma_address(&sg);
271 len = sg_dma_len(&sg);
272
273 hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
274 HvLpEvent_Type_VirtualIo, cmd,
275 HvLpEvent_AckInd_DoAck,
276 HvLpEvent_AckType_ImmediateAck,
277 viopath_sourceinst(viopath_hostLp),
278 viopath_targetinst(viopath_hostLp),
279 (u64)req, VIOVERSION << 16,
280 ((u64)DEVICE_NR(diskinfo) << 48) | dmaaddr,
Tejun Heo83096eb2009-05-07 22:24:39 +0900281 (u64)blk_rq_pos(req) * 512, len, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 if (hvrc != HvLpEvent_Rc_Good) {
Joe Perchese597cd02010-07-01 08:24:32 +0200283 pr_warning("hv error on op %d\n", (int)hvrc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 return -1;
285 }
286
287 return 0;
288}
289
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290static int rwreq;
291
Jens Axboe165125e2007-07-24 09:28:11 +0200292static void do_viocd_request(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293{
294 struct request *req;
295
Tejun Heo9934c8c2009-05-08 11:54:16 +0900296 while ((rwreq == 0) && ((req = blk_fetch_request(q)) != NULL)) {
Christoph Hellwig33659eb2010-08-07 18:17:56 +0200297 if (req->cmd_type != REQ_TYPE_FS)
Tejun Heo40cbbb72009-04-23 11:05:19 +0900298 __blk_end_request_all(req, -EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 else if (send_request(req) < 0) {
Joe Perchese597cd02010-07-01 08:24:32 +0200300 pr_warning("unable to send message to OS/400!\n");
Tejun Heo40cbbb72009-04-23 11:05:19 +0900301 __blk_end_request_all(req, -EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 } else
303 rwreq++;
304 }
305}
306
307static int viocd_media_changed(struct cdrom_device_info *cdi, int disc_nr)
308{
309 struct viocd_waitevent we;
310 HvLpEvent_Rc hvrc;
311 int device_no = DEVICE_NR((struct disk_info *)cdi->handle);
312
313 init_completion(&we.com);
314
315 /* Send the open event to OS/400 */
316 hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
317 HvLpEvent_Type_VirtualIo,
318 viomajorsubtype_cdio | viocdcheck,
319 HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck,
320 viopath_sourceinst(viopath_hostLp),
321 viopath_targetinst(viopath_hostLp),
322 (u64)&we, VIOVERSION << 16, ((u64)device_no << 48),
323 0, 0, 0);
324 if (hvrc != 0) {
Joe Perchese597cd02010-07-01 08:24:32 +0200325 pr_warning("bad rc on HvCallEvent_signalLpEventFast %d\n",
326 (int)hvrc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 return -EIO;
328 }
329
330 wait_for_completion(&we.com);
331
332 /* Check the return code. If bad, assume no change */
333 if (we.rc) {
334 const struct vio_error_entry *err =
335 vio_lookup_rc(viocd_err_table, we.sub_result);
Joe Perchese597cd02010-07-01 08:24:32 +0200336 pr_warning("bad rc %d:0x%04X on check_change: %s; Assuming no change\n",
337 we.rc, we.sub_result, err->msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 return 0;
339 }
340
341 return we.changed;
342}
343
344static int viocd_lock_door(struct cdrom_device_info *cdi, int locking)
345{
346 HvLpEvent_Rc hvrc;
347 u64 device_no = DEVICE_NR((struct disk_info *)cdi->handle);
348 /* NOTE: flags is 1 or 0 so it won't overwrite the device_no */
349 u64 flags = !!locking;
350 struct viocd_waitevent we;
351
352 init_completion(&we.com);
353
354 /* Send the lockdoor event to OS/400 */
355 hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
356 HvLpEvent_Type_VirtualIo,
357 viomajorsubtype_cdio | viocdlockdoor,
358 HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck,
359 viopath_sourceinst(viopath_hostLp),
360 viopath_targetinst(viopath_hostLp),
361 (u64)&we, VIOVERSION << 16,
362 (device_no << 48) | (flags << 32), 0, 0, 0);
363 if (hvrc != 0) {
Joe Perchese597cd02010-07-01 08:24:32 +0200364 pr_warning("bad rc on HvCallEvent_signalLpEventFast %d\n",
365 (int)hvrc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 return -EIO;
367 }
368
369 wait_for_completion(&we.com);
370
371 if (we.rc != 0)
372 return -EIO;
373 return 0;
374}
375
376static int viocd_packet(struct cdrom_device_info *cdi,
377 struct packet_command *cgc)
378{
379 unsigned int buflen = cgc->buflen;
380 int ret = -EIO;
381
382 switch (cgc->cmd[0]) {
383 case GPCMD_READ_DISC_INFO:
384 {
385 disc_information *di = (disc_information *)cgc->buffer;
386
387 if (buflen >= 2) {
388 di->disc_information_length = cpu_to_be16(1);
389 ret = 0;
390 }
391 if (buflen >= 3)
392 di->erasable =
393 (cdi->ops->capability & ~cdi->mask
394 & (CDC_DVD_RAM | CDC_RAM)) != 0;
395 }
396 break;
Stephen Rothwell9db29252005-05-27 12:52:58 -0700397 case GPCMD_GET_CONFIGURATION:
398 if (cgc->cmd[3] == CDF_RWRT) {
399 struct rwrt_feature_desc *rfd = (struct rwrt_feature_desc *)(cgc->buffer + sizeof(struct feature_header));
400
401 if ((buflen >=
402 (sizeof(struct feature_header) + sizeof(*rfd))) &&
403 (cdi->ops->capability & ~cdi->mask
404 & (CDC_DVD_RAM | CDC_RAM))) {
405 rfd->feature_code = cpu_to_be16(CDF_RWRT);
406 rfd->curr = 1;
407 ret = 0;
408 }
409 }
410 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 default:
412 if (cgc->sense) {
413 /* indicate Unknown code */
414 cgc->sense->sense_key = 0x05;
415 cgc->sense->asc = 0x20;
416 cgc->sense->ascq = 0x00;
417 }
418 break;
419 }
420
421 cgc->stat = ret;
422 return ret;
423}
424
425static void restart_all_queues(int first_index)
426{
427 int i;
428
429 for (i = first_index + 1; i < viocd_numdev; i++)
430 if (viocd_diskinfo[i].viocd_disk)
431 blk_run_queue(viocd_diskinfo[i].viocd_disk->queue);
432 for (i = 0; i <= first_index; i++)
433 if (viocd_diskinfo[i].viocd_disk)
434 blk_run_queue(viocd_diskinfo[i].viocd_disk->queue);
435}
436
437/* This routine handles incoming CD LP events */
438static void vio_handle_cd_event(struct HvLpEvent *event)
439{
440 struct viocdlpevent *bevent;
441 struct viocd_waitevent *pwe;
442 struct disk_info *di;
443 unsigned long flags;
444 struct request *req;
445
446
447 if (event == NULL)
448 /* Notification that a partition went away! */
449 return;
450 /* First, we should NEVER get an int here...only acks */
Stephen Rothwell677f8c02006-01-12 13:47:43 +1100451 if (hvlpevent_is_int(event)) {
Joe Perchese597cd02010-07-01 08:24:32 +0200452 pr_warning("Yikes! got an int in viocd event handler!\n");
Stephen Rothwell677f8c02006-01-12 13:47:43 +1100453 if (hvlpevent_need_ack(event)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 event->xRc = HvLpEvent_Rc_InvalidSubtype;
455 HvCallEvent_ackLpEvent(event);
456 }
457 }
458
459 bevent = (struct viocdlpevent *)event;
460
461 switch (event->xSubtype & VIOMINOR_SUBTYPE_MASK) {
462 case viocdopen:
463 if (event->xRc == 0) {
464 di = &viocd_diskinfo[bevent->disk];
Martin K. Petersene1defc42009-05-22 17:17:49 -0400465 blk_queue_logical_block_size(di->viocd_disk->queue,
466 bevent->block_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 set_capacity(di->viocd_disk,
468 bevent->media_size *
469 bevent->block_size / 512);
470 }
471 /* FALLTHROUGH !! */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 case viocdlockdoor:
473 pwe = (struct viocd_waitevent *)event->xCorrelationToken;
474return_complete:
475 pwe->rc = event->xRc;
476 pwe->sub_result = bevent->sub_result;
477 complete(&pwe->com);
478 break;
479
480 case viocdcheck:
481 pwe = (struct viocd_waitevent *)event->xCorrelationToken;
482 pwe->changed = bevent->flags;
483 goto return_complete;
484
485 case viocdclose:
486 break;
487
488 case viocdwrite:
489 case viocdread:
490 /*
491 * Since this is running in interrupt mode, we need to
492 * make sure we're not stepping on any global I/O operations
493 */
494 di = &viocd_diskinfo[bevent->disk];
495 spin_lock_irqsave(&viocd_reqlock, flags);
496 dma_unmap_single(di->dev, bevent->token, bevent->len,
497 ((event->xSubtype & VIOMINOR_SUBTYPE_MASK) == viocdread)
498 ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
499 req = (struct request *)bevent->event.xCorrelationToken;
500 rwreq--;
501
502 if (event->xRc != HvLpEvent_Rc_Good) {
503 const struct vio_error_entry *err =
504 vio_lookup_rc(viocd_err_table,
505 bevent->sub_result);
Joe Perchese597cd02010-07-01 08:24:32 +0200506 pr_warning("request %p failed with rc %d:0x%04X: %s\n",
507 req, event->xRc,
508 bevent->sub_result, err->msg);
Tejun Heo40cbbb72009-04-23 11:05:19 +0900509 __blk_end_request_all(req, -EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 } else
Tejun Heo40cbbb72009-04-23 11:05:19 +0900511 __blk_end_request_all(req, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
513 /* restart handling of incoming requests */
514 spin_unlock_irqrestore(&viocd_reqlock, flags);
515 restart_all_queues(bevent->disk);
516 break;
517
518 default:
Joe Perchese597cd02010-07-01 08:24:32 +0200519 pr_warning("message with invalid subtype %0x04X!\n",
520 event->xSubtype & VIOMINOR_SUBTYPE_MASK);
Stephen Rothwell677f8c02006-01-12 13:47:43 +1100521 if (hvlpevent_need_ack(event)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 event->xRc = HvLpEvent_Rc_InvalidSubtype;
523 HvCallEvent_ackLpEvent(event);
524 }
525 }
526}
527
Borislav Petkov3e636f72008-08-18 21:40:03 +0200528static int viocd_audio_ioctl(struct cdrom_device_info *cdi, unsigned int cmd,
529 void *arg)
530{
531 return -EINVAL;
532}
533
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534static struct cdrom_device_ops viocd_dops = {
535 .open = viocd_open,
536 .release = viocd_release,
537 .media_changed = viocd_media_changed,
538 .lock_door = viocd_lock_door,
539 .generic_packet = viocd_packet,
Borislav Petkov3e636f72008-08-18 21:40:03 +0200540 .audio_ioctl = viocd_audio_ioctl,
Christoph Hellwig6a2900b2006-03-23 03:00:15 -0800541 .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 -0700542};
543
Stephen Rothwellc7211922008-02-05 14:17:31 +1100544static int find_capability(const char *type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545{
546 struct capability_entry *entry;
547
548 for(entry = capability_table; entry->type; ++entry)
549 if(!strncmp(entry->type, type, 4))
550 break;
551 return entry->capability;
552}
553
554static int viocd_probe(struct vio_dev *vdev, const struct vio_device_id *id)
555{
556 struct gendisk *gendisk;
557 int deviceno;
558 struct disk_info *d;
559 struct cdrom_device_info *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 struct request_queue *q;
Grant Likely61c7a082010-04-13 16:12:29 -0700561 struct device_node *node = vdev->dev.of_node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
563 deviceno = vdev->unit_address;
Roel Kluind3375ea2009-05-20 08:42:06 +0200564 if (deviceno >= VIOCD_MAX_CD)
Stephen Rothwellb833b482007-10-11 14:57:26 +1000565 return -ENODEV;
566 if (!node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 return -ENODEV;
568
Stephen Rothwellb833b482007-10-11 14:57:26 +1000569 if (deviceno >= viocd_numdev)
570 viocd_numdev = deviceno + 1;
571
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 d = &viocd_diskinfo[deviceno];
Stephen Rothwellb833b482007-10-11 14:57:26 +1000573 d->rsrcname = of_get_property(node, "linux,vio_rsrcname", NULL);
574 d->type = of_get_property(node, "linux,vio_type", NULL);
575 d->model = of_get_property(node, "linux,vio_model", NULL);
576
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 c = &d->viocd_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
579 c->ops = &viocd_dops;
580 c->speed = 4;
581 c->capacity = 1;
582 c->handle = d;
Stephen Rothwellb833b482007-10-11 14:57:26 +1000583 c->mask = ~find_capability(d->type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 sprintf(c->name, VIOCD_DEVICE "%c", 'a' + deviceno);
585
586 if (register_cdrom(c) != 0) {
Joe Perchese597cd02010-07-01 08:24:32 +0200587 pr_warning("Cannot register viocd CD-ROM %s!\n", c->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 goto out;
589 }
Joe Perchese597cd02010-07-01 08:24:32 +0200590 pr_info("cd %s is iSeries resource %10.10s type %4.4s, model %3.3s\n",
591 c->name, d->rsrcname, d->type, d->model);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 q = blk_init_queue(do_viocd_request, &viocd_reqlock);
593 if (q == NULL) {
Joe Perchese597cd02010-07-01 08:24:32 +0200594 pr_warning("Cannot allocate queue for %s!\n", c->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 goto out_unregister_cdrom;
596 }
597 gendisk = alloc_disk(1);
598 if (gendisk == NULL) {
Joe Perchese597cd02010-07-01 08:24:32 +0200599 pr_warning("Cannot create gendisk for %s!\n", c->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 goto out_cleanup_queue;
601 }
602 gendisk->major = VIOCD_MAJOR;
603 gendisk->first_minor = deviceno;
604 strncpy(gendisk->disk_name, c->name,
605 sizeof(gendisk->disk_name));
Martin K. Petersen8a783622010-02-26 00:20:39 -0500606 blk_queue_max_segments(q, 1);
Martin K. Petersen086fa5f2010-02-26 00:20:38 -0500607 blk_queue_max_hw_sectors(q, 4096 / 512);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 gendisk->queue = q;
609 gendisk->fops = &viocd_fops;
610 gendisk->flags = GENHD_FL_CD|GENHD_FL_REMOVABLE;
611 set_capacity(gendisk, 0);
612 gendisk->private_data = d;
613 d->viocd_disk = gendisk;
614 d->dev = &vdev->dev;
615 gendisk->driverfs_dev = d->dev;
616 add_disk(gendisk);
617 return 0;
618
619out_cleanup_queue:
620 blk_cleanup_queue(q);
621out_unregister_cdrom:
622 unregister_cdrom(c);
623out:
624 return -ENODEV;
625}
626
627static int viocd_remove(struct vio_dev *vdev)
628{
629 struct disk_info *d = &viocd_diskinfo[vdev->unit_address];
630
Akinobu Mita0a0c4112008-03-26 12:09:02 +0100631 unregister_cdrom(&d->viocd_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 del_gendisk(d->viocd_disk);
633 blk_cleanup_queue(d->viocd_disk->queue);
634 put_disk(d->viocd_disk);
635 return 0;
636}
637
638/**
639 * viocd_device_table: Used by vio.c to match devices that we
640 * support.
641 */
642static struct vio_device_id viocd_device_table[] __devinitdata = {
Stephen Rothwellde0fe3b2006-05-15 13:44:01 +1000643 { "block", "IBM,iSeries-viocd" },
Stephen Rothwellfb120da2005-08-17 16:42:59 +1000644 { "", "" }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646MODULE_DEVICE_TABLE(vio, viocd_device_table);
Stephen Rothwell915124d2005-10-24 15:12:22 +1000647
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648static struct vio_driver viocd_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 .id_table = viocd_device_table,
650 .probe = viocd_probe,
Stephen Rothwell6fdf5392005-10-24 14:53:21 +1000651 .remove = viocd_remove,
652 .driver = {
653 .name = "viocd",
Stephen Rothwell915124d2005-10-24 15:12:22 +1000654 .owner = THIS_MODULE,
Stephen Rothwell6fdf5392005-10-24 14:53:21 +1000655 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656};
657
658static int __init viocd_init(void)
659{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 int ret = 0;
661
Stephen Rothwell31c72ad2006-12-15 15:44:04 +1100662 if (!firmware_has_feature(FW_FEATURE_ISERIES))
663 return -ENODEV;
664
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 if (viopath_hostLp == HvLpIndexInvalid) {
666 vio_set_hostlp();
667 /* If we don't have a host, bail out */
668 if (viopath_hostLp == HvLpIndexInvalid)
669 return -ENODEV;
670 }
671
Joe Perchese597cd02010-07-01 08:24:32 +0200672 pr_info("vers " VIOCD_VERS ", hosting partition %d\n", viopath_hostLp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
674 if (register_blkdev(VIOCD_MAJOR, VIOCD_DEVICE) != 0) {
Joe Perchese597cd02010-07-01 08:24:32 +0200675 pr_warning("Unable to get major %d for %s\n",
676 VIOCD_MAJOR, VIOCD_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 return -EIO;
678 }
679
680 ret = viopath_open(viopath_hostLp, viomajorsubtype_cdio,
681 MAX_CD_REQ + 2);
682 if (ret) {
Joe Perchese597cd02010-07-01 08:24:32 +0200683 pr_warning("error opening path to host partition %d\n",
684 viopath_hostLp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 goto out_unregister;
686 }
687
688 /* Initialize our request handler */
689 vio_setHandler(viomajorsubtype_cdio, vio_handle_cd_event);
690
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 spin_lock_init(&viocd_reqlock);
692
693 ret = vio_register_driver(&viocd_driver);
694 if (ret)
695 goto out_free_info;
696
Denis V. Lunevc7705f32008-04-29 01:02:35 -0700697 proc_create("iSeries/viocd", S_IFREG|S_IRUGO, NULL,
698 &proc_viocd_operations);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 return 0;
700
701out_free_info:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 vio_clearHandler(viomajorsubtype_cdio);
703 viopath_close(viopath_hostLp, viomajorsubtype_cdio, MAX_CD_REQ + 2);
704out_unregister:
705 unregister_blkdev(VIOCD_MAJOR, VIOCD_DEVICE);
706 return ret;
707}
708
709static void __exit viocd_exit(void)
710{
711 remove_proc_entry("iSeries/viocd", NULL);
712 vio_unregister_driver(&viocd_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 viopath_close(viopath_hostLp, viomajorsubtype_cdio, MAX_CD_REQ + 2);
714 vio_clearHandler(viomajorsubtype_cdio);
715 unregister_blkdev(VIOCD_MAJOR, VIOCD_DEVICE);
716}
717
718module_init(viocd_init);
719module_exit(viocd_exit);
720MODULE_LICENSE("GPL");