blob: abc4079c3f419c9a0455008a0ba36816b3bb204e [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
154static int viocd_blk_open(struct inode *inode, struct file *file)
155{
156 struct disk_info *di = inode->i_bdev->bd_disk->private_data;
Al Virobbc1cc92007-10-07 17:54:28 -0400157 return cdrom_open(&di->viocd_info, inode->i_bdev, file->f_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158}
159
160static int viocd_blk_release(struct inode *inode, struct file *file)
161{
162 struct disk_info *di = inode->i_bdev->bd_disk->private_data;
Al Virobbc1cc92007-10-07 17:54:28 -0400163 cdrom_release(&di->viocd_info, file ? file->f_mode : 0);
164 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165}
166
167static int viocd_blk_ioctl(struct inode *inode, struct file *file,
168 unsigned cmd, unsigned long arg)
169{
170 struct disk_info *di = inode->i_bdev->bd_disk->private_data;
Al Virobbc1cc92007-10-07 17:54:28 -0400171 return cdrom_ioctl(&di->viocd_info, inode->i_bdev,
172 file ? file->f_mode : 0, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173}
174
175static int viocd_blk_media_changed(struct gendisk *disk)
176{
177 struct disk_info *di = disk->private_data;
178 return cdrom_media_changed(&di->viocd_info);
179}
180
181struct block_device_operations viocd_fops = {
182 .owner = THIS_MODULE,
183 .open = viocd_blk_open,
184 .release = viocd_blk_release,
185 .ioctl = viocd_blk_ioctl,
186 .media_changed = viocd_blk_media_changed,
187};
188
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189static int viocd_open(struct cdrom_device_info *cdi, int purpose)
190{
191 struct disk_info *diskinfo = cdi->handle;
192 int device_no = DEVICE_NR(diskinfo);
193 HvLpEvent_Rc hvrc;
194 struct viocd_waitevent we;
195
196 init_completion(&we.com);
197 hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
198 HvLpEvent_Type_VirtualIo,
199 viomajorsubtype_cdio | viocdopen,
200 HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck,
201 viopath_sourceinst(viopath_hostLp),
202 viopath_targetinst(viopath_hostLp),
203 (u64)&we, VIOVERSION << 16, ((u64)device_no << 48),
204 0, 0, 0);
205 if (hvrc != 0) {
206 printk(VIOCD_KERN_WARNING
207 "bad rc on HvCallEvent_signalLpEventFast %d\n",
208 (int)hvrc);
209 return -EIO;
210 }
211
212 wait_for_completion(&we.com);
213
214 if (we.rc) {
215 const struct vio_error_entry *err =
216 vio_lookup_rc(viocd_err_table, we.sub_result);
217 printk(VIOCD_KERN_WARNING "bad rc %d:0x%04X on open: %s\n",
218 we.rc, we.sub_result, err->msg);
219 return -err->errno;
220 }
221
222 return 0;
223}
224
225static void viocd_release(struct cdrom_device_info *cdi)
226{
227 int device_no = DEVICE_NR((struct disk_info *)cdi->handle);
228 HvLpEvent_Rc hvrc;
229
230 hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
231 HvLpEvent_Type_VirtualIo,
232 viomajorsubtype_cdio | viocdclose,
233 HvLpEvent_AckInd_NoAck, HvLpEvent_AckType_ImmediateAck,
234 viopath_sourceinst(viopath_hostLp),
235 viopath_targetinst(viopath_hostLp), 0,
236 VIOVERSION << 16, ((u64)device_no << 48), 0, 0, 0);
237 if (hvrc != 0)
238 printk(VIOCD_KERN_WARNING
239 "bad rc on HvCallEvent_signalLpEventFast %d\n",
240 (int)hvrc);
241}
242
243/* Send a read or write request to OS/400 */
244static int send_request(struct request *req)
245{
246 HvLpEvent_Rc hvrc;
247 struct disk_info *diskinfo = req->rq_disk->private_data;
248 u64 len;
249 dma_addr_t dmaaddr;
250 int direction;
251 u16 cmd;
252 struct scatterlist sg;
253
254 BUG_ON(req->nr_phys_segments > 1);
255
256 if (rq_data_dir(req) == READ) {
257 direction = DMA_FROM_DEVICE;
258 cmd = viomajorsubtype_cdio | viocdread;
259 } else {
260 direction = DMA_TO_DEVICE;
261 cmd = viomajorsubtype_cdio | viocdwrite;
262 }
263
Jens Axboe3d1266c2007-10-24 13:21:21 +0200264 sg_init_table(&sg, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 if (blk_rq_map_sg(req->q, req, &sg) == 0) {
266 printk(VIOCD_KERN_WARNING
267 "error setting up scatter/gather list\n");
268 return -1;
269 }
270
271 if (dma_map_sg(diskinfo->dev, &sg, 1, direction) == 0) {
272 printk(VIOCD_KERN_WARNING "error allocating sg tce\n");
273 return -1;
274 }
275 dmaaddr = sg_dma_address(&sg);
276 len = sg_dma_len(&sg);
277
278 hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
279 HvLpEvent_Type_VirtualIo, cmd,
280 HvLpEvent_AckInd_DoAck,
281 HvLpEvent_AckType_ImmediateAck,
282 viopath_sourceinst(viopath_hostLp),
283 viopath_targetinst(viopath_hostLp),
284 (u64)req, VIOVERSION << 16,
285 ((u64)DEVICE_NR(diskinfo) << 48) | dmaaddr,
286 (u64)req->sector * 512, len, 0);
287 if (hvrc != HvLpEvent_Rc_Good) {
288 printk(VIOCD_KERN_WARNING "hv error on op %d\n", (int)hvrc);
289 return -1;
290 }
291
292 return 0;
293}
294
Kiyoshi Uedae935eb92007-12-11 17:47:52 -0500295static void viocd_end_request(struct request *req, int error)
Tony Breeds1ad7c312007-03-05 00:30:14 -0800296{
297 int nsectors = req->hard_nr_sectors;
298
299 /*
300 * Make sure it's fully ended, and ensure that we process
301 * at least one sector.
302 */
303 if (blk_pc_request(req))
304 nsectors = (req->data_len + 511) >> 9;
305 if (!nsectors)
306 nsectors = 1;
307
Kiyoshi Uedae935eb92007-12-11 17:47:52 -0500308 if (__blk_end_request(req, error, nsectors << 9))
Tony Breeds1ad7c312007-03-05 00:30:14 -0800309 BUG();
Tony Breeds1ad7c312007-03-05 00:30:14 -0800310}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
312static int rwreq;
313
Jens Axboe165125e2007-07-24 09:28:11 +0200314static void do_viocd_request(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315{
316 struct request *req;
317
318 while ((rwreq == 0) && ((req = elv_next_request(q)) != NULL)) {
319 if (!blk_fs_request(req))
Kiyoshi Uedae935eb92007-12-11 17:47:52 -0500320 viocd_end_request(req, -EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 else if (send_request(req) < 0) {
322 printk(VIOCD_KERN_WARNING
323 "unable to send message to OS/400!");
Kiyoshi Uedae935eb92007-12-11 17:47:52 -0500324 viocd_end_request(req, -EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 } else
326 rwreq++;
327 }
328}
329
330static int viocd_media_changed(struct cdrom_device_info *cdi, int disc_nr)
331{
332 struct viocd_waitevent we;
333 HvLpEvent_Rc hvrc;
334 int device_no = DEVICE_NR((struct disk_info *)cdi->handle);
335
336 init_completion(&we.com);
337
338 /* Send the open event to OS/400 */
339 hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
340 HvLpEvent_Type_VirtualIo,
341 viomajorsubtype_cdio | viocdcheck,
342 HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck,
343 viopath_sourceinst(viopath_hostLp),
344 viopath_targetinst(viopath_hostLp),
345 (u64)&we, VIOVERSION << 16, ((u64)device_no << 48),
346 0, 0, 0);
347 if (hvrc != 0) {
348 printk(VIOCD_KERN_WARNING "bad rc on HvCallEvent_signalLpEventFast %d\n",
349 (int)hvrc);
350 return -EIO;
351 }
352
353 wait_for_completion(&we.com);
354
355 /* Check the return code. If bad, assume no change */
356 if (we.rc) {
357 const struct vio_error_entry *err =
358 vio_lookup_rc(viocd_err_table, we.sub_result);
359 printk(VIOCD_KERN_WARNING
360 "bad rc %d:0x%04X on check_change: %s; Assuming no change\n",
361 we.rc, we.sub_result, err->msg);
362 return 0;
363 }
364
365 return we.changed;
366}
367
368static int viocd_lock_door(struct cdrom_device_info *cdi, int locking)
369{
370 HvLpEvent_Rc hvrc;
371 u64 device_no = DEVICE_NR((struct disk_info *)cdi->handle);
372 /* NOTE: flags is 1 or 0 so it won't overwrite the device_no */
373 u64 flags = !!locking;
374 struct viocd_waitevent we;
375
376 init_completion(&we.com);
377
378 /* Send the lockdoor event to OS/400 */
379 hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
380 HvLpEvent_Type_VirtualIo,
381 viomajorsubtype_cdio | viocdlockdoor,
382 HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck,
383 viopath_sourceinst(viopath_hostLp),
384 viopath_targetinst(viopath_hostLp),
385 (u64)&we, VIOVERSION << 16,
386 (device_no << 48) | (flags << 32), 0, 0, 0);
387 if (hvrc != 0) {
388 printk(VIOCD_KERN_WARNING "bad rc on HvCallEvent_signalLpEventFast %d\n",
389 (int)hvrc);
390 return -EIO;
391 }
392
393 wait_for_completion(&we.com);
394
395 if (we.rc != 0)
396 return -EIO;
397 return 0;
398}
399
400static int viocd_packet(struct cdrom_device_info *cdi,
401 struct packet_command *cgc)
402{
403 unsigned int buflen = cgc->buflen;
404 int ret = -EIO;
405
406 switch (cgc->cmd[0]) {
407 case GPCMD_READ_DISC_INFO:
408 {
409 disc_information *di = (disc_information *)cgc->buffer;
410
411 if (buflen >= 2) {
412 di->disc_information_length = cpu_to_be16(1);
413 ret = 0;
414 }
415 if (buflen >= 3)
416 di->erasable =
417 (cdi->ops->capability & ~cdi->mask
418 & (CDC_DVD_RAM | CDC_RAM)) != 0;
419 }
420 break;
Stephen Rothwell9db29252005-05-27 12:52:58 -0700421 case GPCMD_GET_CONFIGURATION:
422 if (cgc->cmd[3] == CDF_RWRT) {
423 struct rwrt_feature_desc *rfd = (struct rwrt_feature_desc *)(cgc->buffer + sizeof(struct feature_header));
424
425 if ((buflen >=
426 (sizeof(struct feature_header) + sizeof(*rfd))) &&
427 (cdi->ops->capability & ~cdi->mask
428 & (CDC_DVD_RAM | CDC_RAM))) {
429 rfd->feature_code = cpu_to_be16(CDF_RWRT);
430 rfd->curr = 1;
431 ret = 0;
432 }
433 }
434 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 default:
436 if (cgc->sense) {
437 /* indicate Unknown code */
438 cgc->sense->sense_key = 0x05;
439 cgc->sense->asc = 0x20;
440 cgc->sense->ascq = 0x00;
441 }
442 break;
443 }
444
445 cgc->stat = ret;
446 return ret;
447}
448
449static void restart_all_queues(int first_index)
450{
451 int i;
452
453 for (i = first_index + 1; i < viocd_numdev; i++)
454 if (viocd_diskinfo[i].viocd_disk)
455 blk_run_queue(viocd_diskinfo[i].viocd_disk->queue);
456 for (i = 0; i <= first_index; i++)
457 if (viocd_diskinfo[i].viocd_disk)
458 blk_run_queue(viocd_diskinfo[i].viocd_disk->queue);
459}
460
461/* This routine handles incoming CD LP events */
462static void vio_handle_cd_event(struct HvLpEvent *event)
463{
464 struct viocdlpevent *bevent;
465 struct viocd_waitevent *pwe;
466 struct disk_info *di;
467 unsigned long flags;
468 struct request *req;
469
470
471 if (event == NULL)
472 /* Notification that a partition went away! */
473 return;
474 /* First, we should NEVER get an int here...only acks */
Stephen Rothwell677f8c02006-01-12 13:47:43 +1100475 if (hvlpevent_is_int(event)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 printk(VIOCD_KERN_WARNING
477 "Yikes! got an int in viocd event handler!\n");
Stephen Rothwell677f8c02006-01-12 13:47:43 +1100478 if (hvlpevent_need_ack(event)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 event->xRc = HvLpEvent_Rc_InvalidSubtype;
480 HvCallEvent_ackLpEvent(event);
481 }
482 }
483
484 bevent = (struct viocdlpevent *)event;
485
486 switch (event->xSubtype & VIOMINOR_SUBTYPE_MASK) {
487 case viocdopen:
488 if (event->xRc == 0) {
489 di = &viocd_diskinfo[bevent->disk];
490 blk_queue_hardsect_size(di->viocd_disk->queue,
491 bevent->block_size);
492 set_capacity(di->viocd_disk,
493 bevent->media_size *
494 bevent->block_size / 512);
495 }
496 /* FALLTHROUGH !! */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 case viocdlockdoor:
498 pwe = (struct viocd_waitevent *)event->xCorrelationToken;
499return_complete:
500 pwe->rc = event->xRc;
501 pwe->sub_result = bevent->sub_result;
502 complete(&pwe->com);
503 break;
504
505 case viocdcheck:
506 pwe = (struct viocd_waitevent *)event->xCorrelationToken;
507 pwe->changed = bevent->flags;
508 goto return_complete;
509
510 case viocdclose:
511 break;
512
513 case viocdwrite:
514 case viocdread:
515 /*
516 * Since this is running in interrupt mode, we need to
517 * make sure we're not stepping on any global I/O operations
518 */
519 di = &viocd_diskinfo[bevent->disk];
520 spin_lock_irqsave(&viocd_reqlock, flags);
521 dma_unmap_single(di->dev, bevent->token, bevent->len,
522 ((event->xSubtype & VIOMINOR_SUBTYPE_MASK) == viocdread)
523 ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
524 req = (struct request *)bevent->event.xCorrelationToken;
525 rwreq--;
526
527 if (event->xRc != HvLpEvent_Rc_Good) {
528 const struct vio_error_entry *err =
529 vio_lookup_rc(viocd_err_table,
530 bevent->sub_result);
531 printk(VIOCD_KERN_WARNING "request %p failed "
532 "with rc %d:0x%04X: %s\n",
533 req, event->xRc,
534 bevent->sub_result, err->msg);
Kiyoshi Uedae935eb92007-12-11 17:47:52 -0500535 viocd_end_request(req, -EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 } else
Kiyoshi Uedae935eb92007-12-11 17:47:52 -0500537 viocd_end_request(req, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
539 /* restart handling of incoming requests */
540 spin_unlock_irqrestore(&viocd_reqlock, flags);
541 restart_all_queues(bevent->disk);
542 break;
543
544 default:
545 printk(VIOCD_KERN_WARNING
546 "message with invalid subtype %0x04X!\n",
547 event->xSubtype & VIOMINOR_SUBTYPE_MASK);
Stephen Rothwell677f8c02006-01-12 13:47:43 +1100548 if (hvlpevent_need_ack(event)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 event->xRc = HvLpEvent_Rc_InvalidSubtype;
550 HvCallEvent_ackLpEvent(event);
551 }
552 }
553}
554
Borislav Petkov3e636f72008-08-18 21:40:03 +0200555static int viocd_audio_ioctl(struct cdrom_device_info *cdi, unsigned int cmd,
556 void *arg)
557{
558 return -EINVAL;
559}
560
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561static struct cdrom_device_ops viocd_dops = {
562 .open = viocd_open,
563 .release = viocd_release,
564 .media_changed = viocd_media_changed,
565 .lock_door = viocd_lock_door,
566 .generic_packet = viocd_packet,
Borislav Petkov3e636f72008-08-18 21:40:03 +0200567 .audio_ioctl = viocd_audio_ioctl,
Christoph Hellwig6a2900b2006-03-23 03:00:15 -0800568 .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 -0700569};
570
Stephen Rothwellc7211922008-02-05 14:17:31 +1100571static int find_capability(const char *type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572{
573 struct capability_entry *entry;
574
575 for(entry = capability_table; entry->type; ++entry)
576 if(!strncmp(entry->type, type, 4))
577 break;
578 return entry->capability;
579}
580
581static int viocd_probe(struct vio_dev *vdev, const struct vio_device_id *id)
582{
583 struct gendisk *gendisk;
584 int deviceno;
585 struct disk_info *d;
586 struct cdrom_device_info *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 struct request_queue *q;
Stephen Rothwellb833b482007-10-11 14:57:26 +1000588 struct device_node *node = vdev->dev.archdata.of_node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589
590 deviceno = vdev->unit_address;
Stephen Rothwellb833b482007-10-11 14:57:26 +1000591 if (deviceno > VIOCD_MAX_CD)
592 return -ENODEV;
593 if (!node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 return -ENODEV;
595
Stephen Rothwellb833b482007-10-11 14:57:26 +1000596 if (deviceno >= viocd_numdev)
597 viocd_numdev = deviceno + 1;
598
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 d = &viocd_diskinfo[deviceno];
Stephen Rothwellb833b482007-10-11 14:57:26 +1000600 d->rsrcname = of_get_property(node, "linux,vio_rsrcname", NULL);
601 d->type = of_get_property(node, "linux,vio_type", NULL);
602 d->model = of_get_property(node, "linux,vio_model", NULL);
603
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 c = &d->viocd_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
606 c->ops = &viocd_dops;
607 c->speed = 4;
608 c->capacity = 1;
609 c->handle = d;
Stephen Rothwellb833b482007-10-11 14:57:26 +1000610 c->mask = ~find_capability(d->type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 sprintf(c->name, VIOCD_DEVICE "%c", 'a' + deviceno);
612
613 if (register_cdrom(c) != 0) {
614 printk(VIOCD_KERN_WARNING "Cannot register viocd CD-ROM %s!\n",
615 c->name);
616 goto out;
617 }
618 printk(VIOCD_KERN_INFO "cd %s is iSeries resource %10.10s "
619 "type %4.4s, model %3.3s\n",
Stephen Rothwellb833b482007-10-11 14:57:26 +1000620 c->name, d->rsrcname, d->type, d->model);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 q = blk_init_queue(do_viocd_request, &viocd_reqlock);
622 if (q == NULL) {
623 printk(VIOCD_KERN_WARNING "Cannot allocate queue for %s!\n",
624 c->name);
625 goto out_unregister_cdrom;
626 }
627 gendisk = alloc_disk(1);
628 if (gendisk == NULL) {
629 printk(VIOCD_KERN_WARNING "Cannot create gendisk for %s!\n",
630 c->name);
631 goto out_cleanup_queue;
632 }
633 gendisk->major = VIOCD_MAJOR;
634 gendisk->first_minor = deviceno;
635 strncpy(gendisk->disk_name, c->name,
636 sizeof(gendisk->disk_name));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 blk_queue_max_hw_segments(q, 1);
638 blk_queue_max_phys_segments(q, 1);
639 blk_queue_max_sectors(q, 4096 / 512);
640 gendisk->queue = q;
641 gendisk->fops = &viocd_fops;
642 gendisk->flags = GENHD_FL_CD|GENHD_FL_REMOVABLE;
643 set_capacity(gendisk, 0);
644 gendisk->private_data = d;
645 d->viocd_disk = gendisk;
646 d->dev = &vdev->dev;
647 gendisk->driverfs_dev = d->dev;
648 add_disk(gendisk);
649 return 0;
650
651out_cleanup_queue:
652 blk_cleanup_queue(q);
653out_unregister_cdrom:
654 unregister_cdrom(c);
655out:
656 return -ENODEV;
657}
658
659static int viocd_remove(struct vio_dev *vdev)
660{
661 struct disk_info *d = &viocd_diskinfo[vdev->unit_address];
662
Akinobu Mita0a0c4112008-03-26 12:09:02 +0100663 unregister_cdrom(&d->viocd_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 del_gendisk(d->viocd_disk);
665 blk_cleanup_queue(d->viocd_disk->queue);
666 put_disk(d->viocd_disk);
667 return 0;
668}
669
670/**
671 * viocd_device_table: Used by vio.c to match devices that we
672 * support.
673 */
674static struct vio_device_id viocd_device_table[] __devinitdata = {
Stephen Rothwellde0fe3b2006-05-15 13:44:01 +1000675 { "block", "IBM,iSeries-viocd" },
Stephen Rothwellfb120da2005-08-17 16:42:59 +1000676 { "", "" }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678MODULE_DEVICE_TABLE(vio, viocd_device_table);
Stephen Rothwell915124d2005-10-24 15:12:22 +1000679
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680static struct vio_driver viocd_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 .id_table = viocd_device_table,
682 .probe = viocd_probe,
Stephen Rothwell6fdf5392005-10-24 14:53:21 +1000683 .remove = viocd_remove,
684 .driver = {
685 .name = "viocd",
Stephen Rothwell915124d2005-10-24 15:12:22 +1000686 .owner = THIS_MODULE,
Stephen Rothwell6fdf5392005-10-24 14:53:21 +1000687 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688};
689
690static int __init viocd_init(void)
691{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 int ret = 0;
693
Stephen Rothwell31c72ad2006-12-15 15:44:04 +1100694 if (!firmware_has_feature(FW_FEATURE_ISERIES))
695 return -ENODEV;
696
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 if (viopath_hostLp == HvLpIndexInvalid) {
698 vio_set_hostlp();
699 /* If we don't have a host, bail out */
700 if (viopath_hostLp == HvLpIndexInvalid)
701 return -ENODEV;
702 }
703
704 printk(VIOCD_KERN_INFO "vers " VIOCD_VERS ", hosting partition %d\n",
705 viopath_hostLp);
706
707 if (register_blkdev(VIOCD_MAJOR, VIOCD_DEVICE) != 0) {
708 printk(VIOCD_KERN_WARNING "Unable to get major %d for %s\n",
709 VIOCD_MAJOR, VIOCD_DEVICE);
710 return -EIO;
711 }
712
713 ret = viopath_open(viopath_hostLp, viomajorsubtype_cdio,
714 MAX_CD_REQ + 2);
715 if (ret) {
716 printk(VIOCD_KERN_WARNING
717 "error opening path to host partition %d\n",
718 viopath_hostLp);
719 goto out_unregister;
720 }
721
722 /* Initialize our request handler */
723 vio_setHandler(viomajorsubtype_cdio, vio_handle_cd_event);
724
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 spin_lock_init(&viocd_reqlock);
726
727 ret = vio_register_driver(&viocd_driver);
728 if (ret)
729 goto out_free_info;
730
Denis V. Lunevc7705f3442008-04-29 01:02:35 -0700731 proc_create("iSeries/viocd", S_IFREG|S_IRUGO, NULL,
732 &proc_viocd_operations);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 return 0;
734
735out_free_info:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 vio_clearHandler(viomajorsubtype_cdio);
737 viopath_close(viopath_hostLp, viomajorsubtype_cdio, MAX_CD_REQ + 2);
738out_unregister:
739 unregister_blkdev(VIOCD_MAJOR, VIOCD_DEVICE);
740 return ret;
741}
742
743static void __exit viocd_exit(void)
744{
745 remove_proc_entry("iSeries/viocd", NULL);
746 vio_unregister_driver(&viocd_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 viopath_close(viopath_hostLp, viomajorsubtype_cdio, MAX_CD_REQ + 2);
748 vio_clearHandler(viomajorsubtype_cdio);
749 unregister_blkdev(VIOCD_MAJOR, VIOCD_DEVICE);
750}
751
752module_init(viocd_init);
753module_exit(viocd_exit);
754MODULE_LICENSE("GPL");