blob: 031e0e1a1a3bb4f89f94762b8d6c84e395ee0c06 [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;
157 return cdrom_open(&di->viocd_info, inode, file);
158}
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;
163 return cdrom_release(&di->viocd_info, file);
164}
165
166static int viocd_blk_ioctl(struct inode *inode, struct file *file,
167 unsigned cmd, unsigned long arg)
168{
169 struct disk_info *di = inode->i_bdev->bd_disk->private_data;
170 return cdrom_ioctl(file, &di->viocd_info, inode, cmd, arg);
171}
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
179struct block_device_operations viocd_fops = {
180 .owner = THIS_MODULE,
181 .open = viocd_blk_open,
182 .release = viocd_blk_release,
183 .ioctl = viocd_blk_ioctl,
184 .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) {
204 printk(VIOCD_KERN_WARNING
205 "bad rc on HvCallEvent_signalLpEventFast %d\n",
206 (int)hvrc);
207 return -EIO;
208 }
209
210 wait_for_completion(&we.com);
211
212 if (we.rc) {
213 const struct vio_error_entry *err =
214 vio_lookup_rc(viocd_err_table, we.sub_result);
215 printk(VIOCD_KERN_WARNING "bad rc %d:0x%04X on open: %s\n",
216 we.rc, we.sub_result, err->msg);
217 return -err->errno;
218 }
219
220 return 0;
221}
222
223static void viocd_release(struct cdrom_device_info *cdi)
224{
225 int device_no = DEVICE_NR((struct disk_info *)cdi->handle);
226 HvLpEvent_Rc hvrc;
227
228 hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
229 HvLpEvent_Type_VirtualIo,
230 viomajorsubtype_cdio | viocdclose,
231 HvLpEvent_AckInd_NoAck, HvLpEvent_AckType_ImmediateAck,
232 viopath_sourceinst(viopath_hostLp),
233 viopath_targetinst(viopath_hostLp), 0,
234 VIOVERSION << 16, ((u64)device_no << 48), 0, 0, 0);
235 if (hvrc != 0)
236 printk(VIOCD_KERN_WARNING
237 "bad rc on HvCallEvent_signalLpEventFast %d\n",
238 (int)hvrc);
239}
240
241/* Send a read or write request to OS/400 */
242static int send_request(struct request *req)
243{
244 HvLpEvent_Rc hvrc;
245 struct disk_info *diskinfo = req->rq_disk->private_data;
246 u64 len;
247 dma_addr_t dmaaddr;
248 int direction;
249 u16 cmd;
250 struct scatterlist sg;
251
252 BUG_ON(req->nr_phys_segments > 1);
253
254 if (rq_data_dir(req) == READ) {
255 direction = DMA_FROM_DEVICE;
256 cmd = viomajorsubtype_cdio | viocdread;
257 } else {
258 direction = DMA_TO_DEVICE;
259 cmd = viomajorsubtype_cdio | viocdwrite;
260 }
261
Jens Axboe3d1266c2007-10-24 13:21:21 +0200262 sg_init_table(&sg, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 if (blk_rq_map_sg(req->q, req, &sg) == 0) {
264 printk(VIOCD_KERN_WARNING
265 "error setting up scatter/gather list\n");
266 return -1;
267 }
268
269 if (dma_map_sg(diskinfo->dev, &sg, 1, direction) == 0) {
270 printk(VIOCD_KERN_WARNING "error allocating sg tce\n");
271 return -1;
272 }
273 dmaaddr = sg_dma_address(&sg);
274 len = sg_dma_len(&sg);
275
276 hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
277 HvLpEvent_Type_VirtualIo, cmd,
278 HvLpEvent_AckInd_DoAck,
279 HvLpEvent_AckType_ImmediateAck,
280 viopath_sourceinst(viopath_hostLp),
281 viopath_targetinst(viopath_hostLp),
282 (u64)req, VIOVERSION << 16,
283 ((u64)DEVICE_NR(diskinfo) << 48) | dmaaddr,
284 (u64)req->sector * 512, len, 0);
285 if (hvrc != HvLpEvent_Rc_Good) {
286 printk(VIOCD_KERN_WARNING "hv error on op %d\n", (int)hvrc);
287 return -1;
288 }
289
290 return 0;
291}
292
Kiyoshi Uedae935eb92007-12-11 17:47:52 -0500293static void viocd_end_request(struct request *req, int error)
Tony Breeds1ad7c312007-03-05 00:30:14 -0800294{
295 int nsectors = req->hard_nr_sectors;
296
297 /*
298 * Make sure it's fully ended, and ensure that we process
299 * at least one sector.
300 */
301 if (blk_pc_request(req))
302 nsectors = (req->data_len + 511) >> 9;
303 if (!nsectors)
304 nsectors = 1;
305
Kiyoshi Uedae935eb92007-12-11 17:47:52 -0500306 if (__blk_end_request(req, error, nsectors << 9))
Tony Breeds1ad7c312007-03-05 00:30:14 -0800307 BUG();
Tony Breeds1ad7c312007-03-05 00:30:14 -0800308}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
310static int rwreq;
311
Jens Axboe165125e2007-07-24 09:28:11 +0200312static void do_viocd_request(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313{
314 struct request *req;
315
316 while ((rwreq == 0) && ((req = elv_next_request(q)) != NULL)) {
317 if (!blk_fs_request(req))
Kiyoshi Uedae935eb92007-12-11 17:47:52 -0500318 viocd_end_request(req, -EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 else if (send_request(req) < 0) {
320 printk(VIOCD_KERN_WARNING
321 "unable to send message to OS/400!");
Kiyoshi Uedae935eb92007-12-11 17:47:52 -0500322 viocd_end_request(req, -EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 } else
324 rwreq++;
325 }
326}
327
328static int viocd_media_changed(struct cdrom_device_info *cdi, int disc_nr)
329{
330 struct viocd_waitevent we;
331 HvLpEvent_Rc hvrc;
332 int device_no = DEVICE_NR((struct disk_info *)cdi->handle);
333
334 init_completion(&we.com);
335
336 /* Send the open event to OS/400 */
337 hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
338 HvLpEvent_Type_VirtualIo,
339 viomajorsubtype_cdio | viocdcheck,
340 HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck,
341 viopath_sourceinst(viopath_hostLp),
342 viopath_targetinst(viopath_hostLp),
343 (u64)&we, VIOVERSION << 16, ((u64)device_no << 48),
344 0, 0, 0);
345 if (hvrc != 0) {
346 printk(VIOCD_KERN_WARNING "bad rc on HvCallEvent_signalLpEventFast %d\n",
347 (int)hvrc);
348 return -EIO;
349 }
350
351 wait_for_completion(&we.com);
352
353 /* Check the return code. If bad, assume no change */
354 if (we.rc) {
355 const struct vio_error_entry *err =
356 vio_lookup_rc(viocd_err_table, we.sub_result);
357 printk(VIOCD_KERN_WARNING
358 "bad rc %d:0x%04X on check_change: %s; Assuming no change\n",
359 we.rc, we.sub_result, err->msg);
360 return 0;
361 }
362
363 return we.changed;
364}
365
366static int viocd_lock_door(struct cdrom_device_info *cdi, int locking)
367{
368 HvLpEvent_Rc hvrc;
369 u64 device_no = DEVICE_NR((struct disk_info *)cdi->handle);
370 /* NOTE: flags is 1 or 0 so it won't overwrite the device_no */
371 u64 flags = !!locking;
372 struct viocd_waitevent we;
373
374 init_completion(&we.com);
375
376 /* Send the lockdoor event to OS/400 */
377 hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
378 HvLpEvent_Type_VirtualIo,
379 viomajorsubtype_cdio | viocdlockdoor,
380 HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck,
381 viopath_sourceinst(viopath_hostLp),
382 viopath_targetinst(viopath_hostLp),
383 (u64)&we, VIOVERSION << 16,
384 (device_no << 48) | (flags << 32), 0, 0, 0);
385 if (hvrc != 0) {
386 printk(VIOCD_KERN_WARNING "bad rc on HvCallEvent_signalLpEventFast %d\n",
387 (int)hvrc);
388 return -EIO;
389 }
390
391 wait_for_completion(&we.com);
392
393 if (we.rc != 0)
394 return -EIO;
395 return 0;
396}
397
398static int viocd_packet(struct cdrom_device_info *cdi,
399 struct packet_command *cgc)
400{
401 unsigned int buflen = cgc->buflen;
402 int ret = -EIO;
403
404 switch (cgc->cmd[0]) {
405 case GPCMD_READ_DISC_INFO:
406 {
407 disc_information *di = (disc_information *)cgc->buffer;
408
409 if (buflen >= 2) {
410 di->disc_information_length = cpu_to_be16(1);
411 ret = 0;
412 }
413 if (buflen >= 3)
414 di->erasable =
415 (cdi->ops->capability & ~cdi->mask
416 & (CDC_DVD_RAM | CDC_RAM)) != 0;
417 }
418 break;
Stephen Rothwell9db29252005-05-27 12:52:58 -0700419 case GPCMD_GET_CONFIGURATION:
420 if (cgc->cmd[3] == CDF_RWRT) {
421 struct rwrt_feature_desc *rfd = (struct rwrt_feature_desc *)(cgc->buffer + sizeof(struct feature_header));
422
423 if ((buflen >=
424 (sizeof(struct feature_header) + sizeof(*rfd))) &&
425 (cdi->ops->capability & ~cdi->mask
426 & (CDC_DVD_RAM | CDC_RAM))) {
427 rfd->feature_code = cpu_to_be16(CDF_RWRT);
428 rfd->curr = 1;
429 ret = 0;
430 }
431 }
432 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 default:
434 if (cgc->sense) {
435 /* indicate Unknown code */
436 cgc->sense->sense_key = 0x05;
437 cgc->sense->asc = 0x20;
438 cgc->sense->ascq = 0x00;
439 }
440 break;
441 }
442
443 cgc->stat = ret;
444 return ret;
445}
446
447static void restart_all_queues(int first_index)
448{
449 int i;
450
451 for (i = first_index + 1; i < viocd_numdev; i++)
452 if (viocd_diskinfo[i].viocd_disk)
453 blk_run_queue(viocd_diskinfo[i].viocd_disk->queue);
454 for (i = 0; i <= first_index; i++)
455 if (viocd_diskinfo[i].viocd_disk)
456 blk_run_queue(viocd_diskinfo[i].viocd_disk->queue);
457}
458
459/* This routine handles incoming CD LP events */
460static void vio_handle_cd_event(struct HvLpEvent *event)
461{
462 struct viocdlpevent *bevent;
463 struct viocd_waitevent *pwe;
464 struct disk_info *di;
465 unsigned long flags;
466 struct request *req;
467
468
469 if (event == NULL)
470 /* Notification that a partition went away! */
471 return;
472 /* First, we should NEVER get an int here...only acks */
Stephen Rothwell677f8c02006-01-12 13:47:43 +1100473 if (hvlpevent_is_int(event)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 printk(VIOCD_KERN_WARNING
475 "Yikes! got an int in viocd event handler!\n");
Stephen Rothwell677f8c02006-01-12 13:47:43 +1100476 if (hvlpevent_need_ack(event)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 event->xRc = HvLpEvent_Rc_InvalidSubtype;
478 HvCallEvent_ackLpEvent(event);
479 }
480 }
481
482 bevent = (struct viocdlpevent *)event;
483
484 switch (event->xSubtype & VIOMINOR_SUBTYPE_MASK) {
485 case viocdopen:
486 if (event->xRc == 0) {
487 di = &viocd_diskinfo[bevent->disk];
488 blk_queue_hardsect_size(di->viocd_disk->queue,
489 bevent->block_size);
490 set_capacity(di->viocd_disk,
491 bevent->media_size *
492 bevent->block_size / 512);
493 }
494 /* FALLTHROUGH !! */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 case viocdlockdoor:
496 pwe = (struct viocd_waitevent *)event->xCorrelationToken;
497return_complete:
498 pwe->rc = event->xRc;
499 pwe->sub_result = bevent->sub_result;
500 complete(&pwe->com);
501 break;
502
503 case viocdcheck:
504 pwe = (struct viocd_waitevent *)event->xCorrelationToken;
505 pwe->changed = bevent->flags;
506 goto return_complete;
507
508 case viocdclose:
509 break;
510
511 case viocdwrite:
512 case viocdread:
513 /*
514 * Since this is running in interrupt mode, we need to
515 * make sure we're not stepping on any global I/O operations
516 */
517 di = &viocd_diskinfo[bevent->disk];
518 spin_lock_irqsave(&viocd_reqlock, flags);
519 dma_unmap_single(di->dev, bevent->token, bevent->len,
520 ((event->xSubtype & VIOMINOR_SUBTYPE_MASK) == viocdread)
521 ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
522 req = (struct request *)bevent->event.xCorrelationToken;
523 rwreq--;
524
525 if (event->xRc != HvLpEvent_Rc_Good) {
526 const struct vio_error_entry *err =
527 vio_lookup_rc(viocd_err_table,
528 bevent->sub_result);
529 printk(VIOCD_KERN_WARNING "request %p failed "
530 "with rc %d:0x%04X: %s\n",
531 req, event->xRc,
532 bevent->sub_result, err->msg);
Kiyoshi Uedae935eb92007-12-11 17:47:52 -0500533 viocd_end_request(req, -EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 } else
Kiyoshi Uedae935eb92007-12-11 17:47:52 -0500535 viocd_end_request(req, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
537 /* restart handling of incoming requests */
538 spin_unlock_irqrestore(&viocd_reqlock, flags);
539 restart_all_queues(bevent->disk);
540 break;
541
542 default:
543 printk(VIOCD_KERN_WARNING
544 "message with invalid subtype %0x04X!\n",
545 event->xSubtype & VIOMINOR_SUBTYPE_MASK);
Stephen Rothwell677f8c02006-01-12 13:47:43 +1100546 if (hvlpevent_need_ack(event)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 event->xRc = HvLpEvent_Rc_InvalidSubtype;
548 HvCallEvent_ackLpEvent(event);
549 }
550 }
551}
552
Borislav Petkov3e636f72008-08-18 21:40:03 +0200553static int viocd_audio_ioctl(struct cdrom_device_info *cdi, unsigned int cmd,
554 void *arg)
555{
556 return -EINVAL;
557}
558
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559static struct cdrom_device_ops viocd_dops = {
560 .open = viocd_open,
561 .release = viocd_release,
562 .media_changed = viocd_media_changed,
563 .lock_door = viocd_lock_door,
564 .generic_packet = viocd_packet,
Borislav Petkov3e636f72008-08-18 21:40:03 +0200565 .audio_ioctl = viocd_audio_ioctl,
Christoph Hellwig6a2900b2006-03-23 03:00:15 -0800566 .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 -0700567};
568
Stephen Rothwellc7211922008-02-05 14:17:31 +1100569static int find_capability(const char *type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570{
571 struct capability_entry *entry;
572
573 for(entry = capability_table; entry->type; ++entry)
574 if(!strncmp(entry->type, type, 4))
575 break;
576 return entry->capability;
577}
578
579static int viocd_probe(struct vio_dev *vdev, const struct vio_device_id *id)
580{
581 struct gendisk *gendisk;
582 int deviceno;
583 struct disk_info *d;
584 struct cdrom_device_info *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 struct request_queue *q;
Stephen Rothwellb833b482007-10-11 14:57:26 +1000586 struct device_node *node = vdev->dev.archdata.of_node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
588 deviceno = vdev->unit_address;
Stephen Rothwellb833b482007-10-11 14:57:26 +1000589 if (deviceno > VIOCD_MAX_CD)
590 return -ENODEV;
591 if (!node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 return -ENODEV;
593
Stephen Rothwellb833b482007-10-11 14:57:26 +1000594 if (deviceno >= viocd_numdev)
595 viocd_numdev = deviceno + 1;
596
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 d = &viocd_diskinfo[deviceno];
Stephen Rothwellb833b482007-10-11 14:57:26 +1000598 d->rsrcname = of_get_property(node, "linux,vio_rsrcname", NULL);
599 d->type = of_get_property(node, "linux,vio_type", NULL);
600 d->model = of_get_property(node, "linux,vio_model", NULL);
601
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 c = &d->viocd_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
604 c->ops = &viocd_dops;
605 c->speed = 4;
606 c->capacity = 1;
607 c->handle = d;
Stephen Rothwellb833b482007-10-11 14:57:26 +1000608 c->mask = ~find_capability(d->type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 sprintf(c->name, VIOCD_DEVICE "%c", 'a' + deviceno);
610
611 if (register_cdrom(c) != 0) {
612 printk(VIOCD_KERN_WARNING "Cannot register viocd CD-ROM %s!\n",
613 c->name);
614 goto out;
615 }
616 printk(VIOCD_KERN_INFO "cd %s is iSeries resource %10.10s "
617 "type %4.4s, model %3.3s\n",
Stephen Rothwellb833b482007-10-11 14:57:26 +1000618 c->name, d->rsrcname, d->type, d->model);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 q = blk_init_queue(do_viocd_request, &viocd_reqlock);
620 if (q == NULL) {
621 printk(VIOCD_KERN_WARNING "Cannot allocate queue for %s!\n",
622 c->name);
623 goto out_unregister_cdrom;
624 }
625 gendisk = alloc_disk(1);
626 if (gendisk == NULL) {
627 printk(VIOCD_KERN_WARNING "Cannot create gendisk for %s!\n",
628 c->name);
629 goto out_cleanup_queue;
630 }
631 gendisk->major = VIOCD_MAJOR;
632 gendisk->first_minor = deviceno;
633 strncpy(gendisk->disk_name, c->name,
634 sizeof(gendisk->disk_name));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 blk_queue_max_hw_segments(q, 1);
636 blk_queue_max_phys_segments(q, 1);
637 blk_queue_max_sectors(q, 4096 / 512);
638 gendisk->queue = q;
639 gendisk->fops = &viocd_fops;
640 gendisk->flags = GENHD_FL_CD|GENHD_FL_REMOVABLE;
641 set_capacity(gendisk, 0);
642 gendisk->private_data = d;
643 d->viocd_disk = gendisk;
644 d->dev = &vdev->dev;
645 gendisk->driverfs_dev = d->dev;
646 add_disk(gendisk);
647 return 0;
648
649out_cleanup_queue:
650 blk_cleanup_queue(q);
651out_unregister_cdrom:
652 unregister_cdrom(c);
653out:
654 return -ENODEV;
655}
656
657static int viocd_remove(struct vio_dev *vdev)
658{
659 struct disk_info *d = &viocd_diskinfo[vdev->unit_address];
660
Akinobu Mita0a0c4112008-03-26 12:09:02 +0100661 unregister_cdrom(&d->viocd_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 del_gendisk(d->viocd_disk);
663 blk_cleanup_queue(d->viocd_disk->queue);
664 put_disk(d->viocd_disk);
665 return 0;
666}
667
668/**
669 * viocd_device_table: Used by vio.c to match devices that we
670 * support.
671 */
672static struct vio_device_id viocd_device_table[] __devinitdata = {
Stephen Rothwellde0fe3b2006-05-15 13:44:01 +1000673 { "block", "IBM,iSeries-viocd" },
Stephen Rothwellfb120da2005-08-17 16:42:59 +1000674 { "", "" }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676MODULE_DEVICE_TABLE(vio, viocd_device_table);
Stephen Rothwell915124d2005-10-24 15:12:22 +1000677
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678static struct vio_driver viocd_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 .id_table = viocd_device_table,
680 .probe = viocd_probe,
Stephen Rothwell6fdf5392005-10-24 14:53:21 +1000681 .remove = viocd_remove,
682 .driver = {
683 .name = "viocd",
Stephen Rothwell915124d2005-10-24 15:12:22 +1000684 .owner = THIS_MODULE,
Stephen Rothwell6fdf5392005-10-24 14:53:21 +1000685 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686};
687
688static int __init viocd_init(void)
689{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 int ret = 0;
691
Stephen Rothwell31c72ad2006-12-15 15:44:04 +1100692 if (!firmware_has_feature(FW_FEATURE_ISERIES))
693 return -ENODEV;
694
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 if (viopath_hostLp == HvLpIndexInvalid) {
696 vio_set_hostlp();
697 /* If we don't have a host, bail out */
698 if (viopath_hostLp == HvLpIndexInvalid)
699 return -ENODEV;
700 }
701
702 printk(VIOCD_KERN_INFO "vers " VIOCD_VERS ", hosting partition %d\n",
703 viopath_hostLp);
704
705 if (register_blkdev(VIOCD_MAJOR, VIOCD_DEVICE) != 0) {
706 printk(VIOCD_KERN_WARNING "Unable to get major %d for %s\n",
707 VIOCD_MAJOR, VIOCD_DEVICE);
708 return -EIO;
709 }
710
711 ret = viopath_open(viopath_hostLp, viomajorsubtype_cdio,
712 MAX_CD_REQ + 2);
713 if (ret) {
714 printk(VIOCD_KERN_WARNING
715 "error opening path to host partition %d\n",
716 viopath_hostLp);
717 goto out_unregister;
718 }
719
720 /* Initialize our request handler */
721 vio_setHandler(viomajorsubtype_cdio, vio_handle_cd_event);
722
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 spin_lock_init(&viocd_reqlock);
724
725 ret = vio_register_driver(&viocd_driver);
726 if (ret)
727 goto out_free_info;
728
Denis V. Lunevc7705f3442008-04-29 01:02:35 -0700729 proc_create("iSeries/viocd", S_IFREG|S_IRUGO, NULL,
730 &proc_viocd_operations);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 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");