blob: be73a9b493a69970135663ffd4ed7a83cb568cb9 [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>
Arnd Bergmann2a48fc02010-06-02 14:28:52 +020045#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <linux/seq_file.h>
Jens Axboe3d1266c2007-10-24 13:21:21 +020047#include <linux/scatterlist.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <asm/vio.h>
Kelly Daly1ec65d72005-11-02 13:46:07 +110050#include <asm/iseries/hv_types.h>
Kelly Dalye45423e2005-11-02 12:08:31 +110051#include <asm/iseries/hv_lp_event.h>
Kelly Dalyb4206772005-11-02 15:13:57 +110052#include <asm/iseries/vio.h>
Stephen Rothwell31c72ad2006-12-15 15:44:04 +110053#include <asm/firmware.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
55#define VIOCD_DEVICE "iseries/vcd"
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
57#define VIOCD_VERS "1.06"
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
Arnd Bergmann2a48fc02010-06-02 14:28:52 +020064static DEFINE_MUTEX(viocd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065static const struct vio_error_entry viocd_err_table[] = {
66 {0x0201, EINVAL, "Invalid Range"},
67 {0x0202, EINVAL, "Invalid Token"},
68 {0x0203, EIO, "DMA Error"},
69 {0x0204, EIO, "Use Error"},
70 {0x0205, EIO, "Release Error"},
71 {0x0206, EINVAL, "Invalid CD"},
72 {0x020C, EROFS, "Read Only Device"},
73 {0x020D, ENOMEDIUM, "Changed or Missing Volume (or Varied Off?)"},
74 {0x020E, EIO, "Optical System Error (Varied Off?)"},
75 {0x02FF, EIO, "Internal Error"},
76 {0x3010, EIO, "Changed Volume"},
77 {0xC100, EIO, "Optical System Error"},
78 {0x0000, 0, NULL},
79};
80
81/*
82 * This is the structure we use to exchange info between driver and interrupt
83 * handler
84 */
85struct viocd_waitevent {
86 struct completion com;
87 int rc;
88 u16 sub_result;
89 int changed;
90};
91
92/* this is a lookup table for the true capabilities of a device */
93struct capability_entry {
94 char *type;
95 int capability;
96};
97
98static struct capability_entry capability_table[] __initdata = {
99 { "6330", CDC_LOCK | CDC_DVD_RAM | CDC_RAM },
100 { "6331", CDC_LOCK | CDC_DVD_RAM | CDC_RAM },
101 { "6333", CDC_LOCK | CDC_DVD_RAM | CDC_RAM },
102 { "632A", CDC_LOCK | CDC_DVD_RAM | CDC_RAM },
103 { "6321", CDC_LOCK },
104 { "632B", 0 },
105 { NULL , CDC_LOCK },
106};
107
108/* These are our internal structures for keeping track of devices */
109static int viocd_numdev;
110
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111struct disk_info {
112 struct gendisk *viocd_disk;
113 struct cdrom_device_info viocd_info;
114 struct device *dev;
Stephen Rothwellb833b482007-10-11 14:57:26 +1000115 const char *rsrcname;
116 const char *type;
117 const char *model;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118};
119static struct disk_info viocd_diskinfo[VIOCD_MAX_CD];
120
121#define DEVICE_NR(di) ((di) - &viocd_diskinfo[0])
122
123static spinlock_t viocd_reqlock;
124
125#define MAX_CD_REQ 1
126
127/* procfs support */
128static int proc_viocd_show(struct seq_file *m, void *v)
129{
130 int i;
131
132 for (i = 0; i < viocd_numdev; i++) {
133 seq_printf(m, "viocd device %d is iSeries resource %10.10s"
134 "type %4.4s, model %3.3s\n",
Stephen Rothwellb833b482007-10-11 14:57:26 +1000135 i, viocd_diskinfo[i].rsrcname,
136 viocd_diskinfo[i].type,
137 viocd_diskinfo[i].model);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 }
139 return 0;
140}
141
142static int proc_viocd_open(struct inode *inode, struct file *file)
143{
144 return single_open(file, proc_viocd_show, NULL);
145}
146
Arjan van de Ven2b8693c2007-02-12 00:55:32 -0800147static const struct file_operations proc_viocd_operations = {
Denis V. Lunevc7705f32008-04-29 01:02:35 -0700148 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 .open = proc_viocd_open,
150 .read = seq_read,
151 .llseek = seq_lseek,
152 .release = single_release,
153};
154
Al Viro4e379ae2008-03-02 10:25:44 -0500155static int viocd_blk_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156{
Al Viro4e379ae2008-03-02 10:25:44 -0500157 struct disk_info *di = bdev->bd_disk->private_data;
Arnd Bergmann6e9624b2010-08-07 18:25:34 +0200158 int ret;
159
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200160 mutex_lock(&viocd_mutex);
Arnd Bergmann6e9624b2010-08-07 18:25:34 +0200161 ret = cdrom_open(&di->viocd_info, bdev, mode);
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200162 mutex_unlock(&viocd_mutex);
Arnd Bergmann6e9624b2010-08-07 18:25:34 +0200163
164 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165}
166
Al Viro4e379ae2008-03-02 10:25:44 -0500167static int viocd_blk_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168{
Al Viro4e379ae2008-03-02 10:25:44 -0500169 struct disk_info *di = disk->private_data;
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200170 mutex_lock(&viocd_mutex);
Al Viro4e379ae2008-03-02 10:25:44 -0500171 cdrom_release(&di->viocd_info, mode);
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200172 mutex_unlock(&viocd_mutex);
Al Virobbc1cc92007-10-07 17:54:28 -0400173 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174}
175
Al Viro4e379ae2008-03-02 10:25:44 -0500176static int viocd_blk_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 unsigned cmd, unsigned long arg)
178{
Al Viro4e379ae2008-03-02 10:25:44 -0500179 struct disk_info *di = bdev->bd_disk->private_data;
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +0200180 int ret;
181
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200182 mutex_lock(&viocd_mutex);
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +0200183 ret = cdrom_ioctl(&di->viocd_info, bdev, mode, cmd, arg);
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200184 mutex_unlock(&viocd_mutex);
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +0200185
186 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187}
188
189static int viocd_blk_media_changed(struct gendisk *disk)
190{
191 struct disk_info *di = disk->private_data;
192 return cdrom_media_changed(&di->viocd_info);
193}
194
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700195static const struct block_device_operations viocd_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 .owner = THIS_MODULE,
Al Viro4e379ae2008-03-02 10:25:44 -0500197 .open = viocd_blk_open,
198 .release = viocd_blk_release,
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +0200199 .ioctl = viocd_blk_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 .media_changed = viocd_blk_media_changed,
201};
202
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203static int viocd_open(struct cdrom_device_info *cdi, int purpose)
204{
205 struct disk_info *diskinfo = cdi->handle;
206 int device_no = DEVICE_NR(diskinfo);
207 HvLpEvent_Rc hvrc;
208 struct viocd_waitevent we;
209
210 init_completion(&we.com);
211 hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
212 HvLpEvent_Type_VirtualIo,
213 viomajorsubtype_cdio | viocdopen,
214 HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck,
215 viopath_sourceinst(viopath_hostLp),
216 viopath_targetinst(viopath_hostLp),
217 (u64)&we, VIOVERSION << 16, ((u64)device_no << 48),
218 0, 0, 0);
219 if (hvrc != 0) {
Joe Perchese597cd02010-07-01 08:24:32 +0200220 pr_warning("bad rc on HvCallEvent_signalLpEventFast %d\n",
221 (int)hvrc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 return -EIO;
223 }
224
225 wait_for_completion(&we.com);
226
227 if (we.rc) {
228 const struct vio_error_entry *err =
229 vio_lookup_rc(viocd_err_table, we.sub_result);
Joe Perchese597cd02010-07-01 08:24:32 +0200230 pr_warning("bad rc %d:0x%04X on open: %s\n",
231 we.rc, we.sub_result, err->msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 return -err->errno;
233 }
234
235 return 0;
236}
237
238static void viocd_release(struct cdrom_device_info *cdi)
239{
240 int device_no = DEVICE_NR((struct disk_info *)cdi->handle);
241 HvLpEvent_Rc hvrc;
242
243 hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
244 HvLpEvent_Type_VirtualIo,
245 viomajorsubtype_cdio | viocdclose,
246 HvLpEvent_AckInd_NoAck, HvLpEvent_AckType_ImmediateAck,
247 viopath_sourceinst(viopath_hostLp),
248 viopath_targetinst(viopath_hostLp), 0,
249 VIOVERSION << 16, ((u64)device_no << 48), 0, 0, 0);
250 if (hvrc != 0)
Joe Perchese597cd02010-07-01 08:24:32 +0200251 pr_warning("bad rc on HvCallEvent_signalLpEventFast %d\n",
252 (int)hvrc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253}
254
255/* Send a read or write request to OS/400 */
256static int send_request(struct request *req)
257{
258 HvLpEvent_Rc hvrc;
259 struct disk_info *diskinfo = req->rq_disk->private_data;
260 u64 len;
261 dma_addr_t dmaaddr;
262 int direction;
263 u16 cmd;
264 struct scatterlist sg;
265
266 BUG_ON(req->nr_phys_segments > 1);
267
268 if (rq_data_dir(req) == READ) {
269 direction = DMA_FROM_DEVICE;
270 cmd = viomajorsubtype_cdio | viocdread;
271 } else {
272 direction = DMA_TO_DEVICE;
273 cmd = viomajorsubtype_cdio | viocdwrite;
274 }
275
Jens Axboe3d1266c2007-10-24 13:21:21 +0200276 sg_init_table(&sg, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 if (blk_rq_map_sg(req->q, req, &sg) == 0) {
Joe Perchese597cd02010-07-01 08:24:32 +0200278 pr_warning("error setting up scatter/gather list\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 return -1;
280 }
281
282 if (dma_map_sg(diskinfo->dev, &sg, 1, direction) == 0) {
Joe Perchese597cd02010-07-01 08:24:32 +0200283 pr_warning("error allocating sg tce\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 return -1;
285 }
286 dmaaddr = sg_dma_address(&sg);
287 len = sg_dma_len(&sg);
288
289 hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
290 HvLpEvent_Type_VirtualIo, cmd,
291 HvLpEvent_AckInd_DoAck,
292 HvLpEvent_AckType_ImmediateAck,
293 viopath_sourceinst(viopath_hostLp),
294 viopath_targetinst(viopath_hostLp),
295 (u64)req, VIOVERSION << 16,
296 ((u64)DEVICE_NR(diskinfo) << 48) | dmaaddr,
Tejun Heo83096eb2009-05-07 22:24:39 +0900297 (u64)blk_rq_pos(req) * 512, len, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 if (hvrc != HvLpEvent_Rc_Good) {
Joe Perchese597cd02010-07-01 08:24:32 +0200299 pr_warning("hv error on op %d\n", (int)hvrc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 return -1;
301 }
302
303 return 0;
304}
305
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306static int rwreq;
307
Jens Axboe165125e2007-07-24 09:28:11 +0200308static void do_viocd_request(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309{
310 struct request *req;
311
Tejun Heo9934c8c2009-05-08 11:54:16 +0900312 while ((rwreq == 0) && ((req = blk_fetch_request(q)) != NULL)) {
Christoph Hellwig33659eb2010-08-07 18:17:56 +0200313 if (req->cmd_type != REQ_TYPE_FS)
Tejun Heo40cbbb72009-04-23 11:05:19 +0900314 __blk_end_request_all(req, -EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 else if (send_request(req) < 0) {
Joe Perchese597cd02010-07-01 08:24:32 +0200316 pr_warning("unable to send message to OS/400!\n");
Tejun Heo40cbbb72009-04-23 11:05:19 +0900317 __blk_end_request_all(req, -EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 } else
319 rwreq++;
320 }
321}
322
323static int viocd_media_changed(struct cdrom_device_info *cdi, int disc_nr)
324{
325 struct viocd_waitevent we;
326 HvLpEvent_Rc hvrc;
327 int device_no = DEVICE_NR((struct disk_info *)cdi->handle);
328
329 init_completion(&we.com);
330
331 /* Send the open event to OS/400 */
332 hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
333 HvLpEvent_Type_VirtualIo,
334 viomajorsubtype_cdio | viocdcheck,
335 HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck,
336 viopath_sourceinst(viopath_hostLp),
337 viopath_targetinst(viopath_hostLp),
338 (u64)&we, VIOVERSION << 16, ((u64)device_no << 48),
339 0, 0, 0);
340 if (hvrc != 0) {
Joe Perchese597cd02010-07-01 08:24:32 +0200341 pr_warning("bad rc on HvCallEvent_signalLpEventFast %d\n",
342 (int)hvrc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 return -EIO;
344 }
345
346 wait_for_completion(&we.com);
347
348 /* Check the return code. If bad, assume no change */
349 if (we.rc) {
350 const struct vio_error_entry *err =
351 vio_lookup_rc(viocd_err_table, we.sub_result);
Joe Perchese597cd02010-07-01 08:24:32 +0200352 pr_warning("bad rc %d:0x%04X on check_change: %s; Assuming no change\n",
353 we.rc, we.sub_result, err->msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 return 0;
355 }
356
357 return we.changed;
358}
359
360static int viocd_lock_door(struct cdrom_device_info *cdi, int locking)
361{
362 HvLpEvent_Rc hvrc;
363 u64 device_no = DEVICE_NR((struct disk_info *)cdi->handle);
364 /* NOTE: flags is 1 or 0 so it won't overwrite the device_no */
365 u64 flags = !!locking;
366 struct viocd_waitevent we;
367
368 init_completion(&we.com);
369
370 /* Send the lockdoor event to OS/400 */
371 hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
372 HvLpEvent_Type_VirtualIo,
373 viomajorsubtype_cdio | viocdlockdoor,
374 HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck,
375 viopath_sourceinst(viopath_hostLp),
376 viopath_targetinst(viopath_hostLp),
377 (u64)&we, VIOVERSION << 16,
378 (device_no << 48) | (flags << 32), 0, 0, 0);
379 if (hvrc != 0) {
Joe Perchese597cd02010-07-01 08:24:32 +0200380 pr_warning("bad rc on HvCallEvent_signalLpEventFast %d\n",
381 (int)hvrc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 return -EIO;
383 }
384
385 wait_for_completion(&we.com);
386
387 if (we.rc != 0)
388 return -EIO;
389 return 0;
390}
391
392static int viocd_packet(struct cdrom_device_info *cdi,
393 struct packet_command *cgc)
394{
395 unsigned int buflen = cgc->buflen;
396 int ret = -EIO;
397
398 switch (cgc->cmd[0]) {
399 case GPCMD_READ_DISC_INFO:
400 {
401 disc_information *di = (disc_information *)cgc->buffer;
402
403 if (buflen >= 2) {
404 di->disc_information_length = cpu_to_be16(1);
405 ret = 0;
406 }
407 if (buflen >= 3)
408 di->erasable =
409 (cdi->ops->capability & ~cdi->mask
410 & (CDC_DVD_RAM | CDC_RAM)) != 0;
411 }
412 break;
Stephen Rothwell9db29252005-05-27 12:52:58 -0700413 case GPCMD_GET_CONFIGURATION:
414 if (cgc->cmd[3] == CDF_RWRT) {
415 struct rwrt_feature_desc *rfd = (struct rwrt_feature_desc *)(cgc->buffer + sizeof(struct feature_header));
416
417 if ((buflen >=
418 (sizeof(struct feature_header) + sizeof(*rfd))) &&
419 (cdi->ops->capability & ~cdi->mask
420 & (CDC_DVD_RAM | CDC_RAM))) {
421 rfd->feature_code = cpu_to_be16(CDF_RWRT);
422 rfd->curr = 1;
423 ret = 0;
424 }
425 }
426 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 default:
428 if (cgc->sense) {
429 /* indicate Unknown code */
430 cgc->sense->sense_key = 0x05;
431 cgc->sense->asc = 0x20;
432 cgc->sense->ascq = 0x00;
433 }
434 break;
435 }
436
437 cgc->stat = ret;
438 return ret;
439}
440
441static void restart_all_queues(int first_index)
442{
443 int i;
444
445 for (i = first_index + 1; i < viocd_numdev; i++)
446 if (viocd_diskinfo[i].viocd_disk)
447 blk_run_queue(viocd_diskinfo[i].viocd_disk->queue);
448 for (i = 0; i <= first_index; i++)
449 if (viocd_diskinfo[i].viocd_disk)
450 blk_run_queue(viocd_diskinfo[i].viocd_disk->queue);
451}
452
453/* This routine handles incoming CD LP events */
454static void vio_handle_cd_event(struct HvLpEvent *event)
455{
456 struct viocdlpevent *bevent;
457 struct viocd_waitevent *pwe;
458 struct disk_info *di;
459 unsigned long flags;
460 struct request *req;
461
462
463 if (event == NULL)
464 /* Notification that a partition went away! */
465 return;
466 /* First, we should NEVER get an int here...only acks */
Stephen Rothwell677f8c02006-01-12 13:47:43 +1100467 if (hvlpevent_is_int(event)) {
Joe Perchese597cd02010-07-01 08:24:32 +0200468 pr_warning("Yikes! got an int in viocd event handler!\n");
Stephen Rothwell677f8c02006-01-12 13:47:43 +1100469 if (hvlpevent_need_ack(event)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 event->xRc = HvLpEvent_Rc_InvalidSubtype;
471 HvCallEvent_ackLpEvent(event);
472 }
473 }
474
475 bevent = (struct viocdlpevent *)event;
476
477 switch (event->xSubtype & VIOMINOR_SUBTYPE_MASK) {
478 case viocdopen:
479 if (event->xRc == 0) {
480 di = &viocd_diskinfo[bevent->disk];
Martin K. Petersene1defc42009-05-22 17:17:49 -0400481 blk_queue_logical_block_size(di->viocd_disk->queue,
482 bevent->block_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 set_capacity(di->viocd_disk,
484 bevent->media_size *
485 bevent->block_size / 512);
486 }
487 /* FALLTHROUGH !! */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 case viocdlockdoor:
489 pwe = (struct viocd_waitevent *)event->xCorrelationToken;
490return_complete:
491 pwe->rc = event->xRc;
492 pwe->sub_result = bevent->sub_result;
493 complete(&pwe->com);
494 break;
495
496 case viocdcheck:
497 pwe = (struct viocd_waitevent *)event->xCorrelationToken;
498 pwe->changed = bevent->flags;
499 goto return_complete;
500
501 case viocdclose:
502 break;
503
504 case viocdwrite:
505 case viocdread:
506 /*
507 * Since this is running in interrupt mode, we need to
508 * make sure we're not stepping on any global I/O operations
509 */
510 di = &viocd_diskinfo[bevent->disk];
511 spin_lock_irqsave(&viocd_reqlock, flags);
512 dma_unmap_single(di->dev, bevent->token, bevent->len,
513 ((event->xSubtype & VIOMINOR_SUBTYPE_MASK) == viocdread)
514 ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
515 req = (struct request *)bevent->event.xCorrelationToken;
516 rwreq--;
517
518 if (event->xRc != HvLpEvent_Rc_Good) {
519 const struct vio_error_entry *err =
520 vio_lookup_rc(viocd_err_table,
521 bevent->sub_result);
Joe Perchese597cd02010-07-01 08:24:32 +0200522 pr_warning("request %p failed with rc %d:0x%04X: %s\n",
523 req, event->xRc,
524 bevent->sub_result, err->msg);
Tejun Heo40cbbb72009-04-23 11:05:19 +0900525 __blk_end_request_all(req, -EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 } else
Tejun Heo40cbbb72009-04-23 11:05:19 +0900527 __blk_end_request_all(req, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
529 /* restart handling of incoming requests */
530 spin_unlock_irqrestore(&viocd_reqlock, flags);
531 restart_all_queues(bevent->disk);
532 break;
533
534 default:
Joe Perchese597cd02010-07-01 08:24:32 +0200535 pr_warning("message with invalid subtype %0x04X!\n",
536 event->xSubtype & VIOMINOR_SUBTYPE_MASK);
Stephen Rothwell677f8c02006-01-12 13:47:43 +1100537 if (hvlpevent_need_ack(event)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 event->xRc = HvLpEvent_Rc_InvalidSubtype;
539 HvCallEvent_ackLpEvent(event);
540 }
541 }
542}
543
Borislav Petkov3e636f72008-08-18 21:40:03 +0200544static int viocd_audio_ioctl(struct cdrom_device_info *cdi, unsigned int cmd,
545 void *arg)
546{
547 return -EINVAL;
548}
549
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550static struct cdrom_device_ops viocd_dops = {
551 .open = viocd_open,
552 .release = viocd_release,
553 .media_changed = viocd_media_changed,
554 .lock_door = viocd_lock_door,
555 .generic_packet = viocd_packet,
Borislav Petkov3e636f72008-08-18 21:40:03 +0200556 .audio_ioctl = viocd_audio_ioctl,
Christoph Hellwig6a2900b2006-03-23 03:00:15 -0800557 .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 -0700558};
559
Stephen Rothwellc7211922008-02-05 14:17:31 +1100560static int find_capability(const char *type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561{
562 struct capability_entry *entry;
563
564 for(entry = capability_table; entry->type; ++entry)
565 if(!strncmp(entry->type, type, 4))
566 break;
567 return entry->capability;
568}
569
570static int viocd_probe(struct vio_dev *vdev, const struct vio_device_id *id)
571{
572 struct gendisk *gendisk;
573 int deviceno;
574 struct disk_info *d;
575 struct cdrom_device_info *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 struct request_queue *q;
Grant Likely61c7a082010-04-13 16:12:29 -0700577 struct device_node *node = vdev->dev.of_node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
579 deviceno = vdev->unit_address;
Roel Kluind3375ea2009-05-20 08:42:06 +0200580 if (deviceno >= VIOCD_MAX_CD)
Stephen Rothwellb833b482007-10-11 14:57:26 +1000581 return -ENODEV;
582 if (!node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 return -ENODEV;
584
Stephen Rothwellb833b482007-10-11 14:57:26 +1000585 if (deviceno >= viocd_numdev)
586 viocd_numdev = deviceno + 1;
587
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 d = &viocd_diskinfo[deviceno];
Stephen Rothwellb833b482007-10-11 14:57:26 +1000589 d->rsrcname = of_get_property(node, "linux,vio_rsrcname", NULL);
590 d->type = of_get_property(node, "linux,vio_type", NULL);
591 d->model = of_get_property(node, "linux,vio_model", NULL);
592
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 c = &d->viocd_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
595 c->ops = &viocd_dops;
596 c->speed = 4;
597 c->capacity = 1;
598 c->handle = d;
Stephen Rothwellb833b482007-10-11 14:57:26 +1000599 c->mask = ~find_capability(d->type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 sprintf(c->name, VIOCD_DEVICE "%c", 'a' + deviceno);
601
602 if (register_cdrom(c) != 0) {
Joe Perchese597cd02010-07-01 08:24:32 +0200603 pr_warning("Cannot register viocd CD-ROM %s!\n", c->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 goto out;
605 }
Joe Perchese597cd02010-07-01 08:24:32 +0200606 pr_info("cd %s is iSeries resource %10.10s type %4.4s, model %3.3s\n",
607 c->name, d->rsrcname, d->type, d->model);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 q = blk_init_queue(do_viocd_request, &viocd_reqlock);
609 if (q == NULL) {
Joe Perchese597cd02010-07-01 08:24:32 +0200610 pr_warning("Cannot allocate queue for %s!\n", c->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 goto out_unregister_cdrom;
612 }
613 gendisk = alloc_disk(1);
614 if (gendisk == NULL) {
Joe Perchese597cd02010-07-01 08:24:32 +0200615 pr_warning("Cannot create gendisk for %s!\n", c->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 goto out_cleanup_queue;
617 }
618 gendisk->major = VIOCD_MAJOR;
619 gendisk->first_minor = deviceno;
620 strncpy(gendisk->disk_name, c->name,
621 sizeof(gendisk->disk_name));
Martin K. Petersen8a783622010-02-26 00:20:39 -0500622 blk_queue_max_segments(q, 1);
Martin K. Petersen086fa5f2010-02-26 00:20:38 -0500623 blk_queue_max_hw_sectors(q, 4096 / 512);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 gendisk->queue = q;
625 gendisk->fops = &viocd_fops;
626 gendisk->flags = GENHD_FL_CD|GENHD_FL_REMOVABLE;
627 set_capacity(gendisk, 0);
628 gendisk->private_data = d;
629 d->viocd_disk = gendisk;
630 d->dev = &vdev->dev;
631 gendisk->driverfs_dev = d->dev;
632 add_disk(gendisk);
633 return 0;
634
635out_cleanup_queue:
636 blk_cleanup_queue(q);
637out_unregister_cdrom:
638 unregister_cdrom(c);
639out:
640 return -ENODEV;
641}
642
643static int viocd_remove(struct vio_dev *vdev)
644{
645 struct disk_info *d = &viocd_diskinfo[vdev->unit_address];
646
Akinobu Mita0a0c4112008-03-26 12:09:02 +0100647 unregister_cdrom(&d->viocd_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 del_gendisk(d->viocd_disk);
649 blk_cleanup_queue(d->viocd_disk->queue);
650 put_disk(d->viocd_disk);
651 return 0;
652}
653
654/**
655 * viocd_device_table: Used by vio.c to match devices that we
656 * support.
657 */
658static struct vio_device_id viocd_device_table[] __devinitdata = {
Stephen Rothwellde0fe3b2006-05-15 13:44:01 +1000659 { "block", "IBM,iSeries-viocd" },
Stephen Rothwellfb120da2005-08-17 16:42:59 +1000660 { "", "" }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662MODULE_DEVICE_TABLE(vio, viocd_device_table);
Stephen Rothwell915124d2005-10-24 15:12:22 +1000663
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664static struct vio_driver viocd_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 .id_table = viocd_device_table,
666 .probe = viocd_probe,
Stephen Rothwell6fdf5392005-10-24 14:53:21 +1000667 .remove = viocd_remove,
668 .driver = {
669 .name = "viocd",
Stephen Rothwell915124d2005-10-24 15:12:22 +1000670 .owner = THIS_MODULE,
Stephen Rothwell6fdf5392005-10-24 14:53:21 +1000671 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672};
673
674static int __init viocd_init(void)
675{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 int ret = 0;
677
Stephen Rothwell31c72ad2006-12-15 15:44:04 +1100678 if (!firmware_has_feature(FW_FEATURE_ISERIES))
679 return -ENODEV;
680
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 if (viopath_hostLp == HvLpIndexInvalid) {
682 vio_set_hostlp();
683 /* If we don't have a host, bail out */
684 if (viopath_hostLp == HvLpIndexInvalid)
685 return -ENODEV;
686 }
687
Joe Perchese597cd02010-07-01 08:24:32 +0200688 pr_info("vers " VIOCD_VERS ", hosting partition %d\n", viopath_hostLp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
690 if (register_blkdev(VIOCD_MAJOR, VIOCD_DEVICE) != 0) {
Joe Perchese597cd02010-07-01 08:24:32 +0200691 pr_warning("Unable to get major %d for %s\n",
692 VIOCD_MAJOR, VIOCD_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 return -EIO;
694 }
695
696 ret = viopath_open(viopath_hostLp, viomajorsubtype_cdio,
697 MAX_CD_REQ + 2);
698 if (ret) {
Joe Perchese597cd02010-07-01 08:24:32 +0200699 pr_warning("error opening path to host partition %d\n",
700 viopath_hostLp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 goto out_unregister;
702 }
703
704 /* Initialize our request handler */
705 vio_setHandler(viomajorsubtype_cdio, vio_handle_cd_event);
706
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 spin_lock_init(&viocd_reqlock);
708
709 ret = vio_register_driver(&viocd_driver);
710 if (ret)
711 goto out_free_info;
712
Denis V. Lunevc7705f32008-04-29 01:02:35 -0700713 proc_create("iSeries/viocd", S_IFREG|S_IRUGO, NULL,
714 &proc_viocd_operations);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 return 0;
716
717out_free_info:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 vio_clearHandler(viomajorsubtype_cdio);
719 viopath_close(viopath_hostLp, viomajorsubtype_cdio, MAX_CD_REQ + 2);
720out_unregister:
721 unregister_blkdev(VIOCD_MAJOR, VIOCD_DEVICE);
722 return ret;
723}
724
725static void __exit viocd_exit(void)
726{
727 remove_proc_entry("iSeries/viocd", NULL);
728 vio_unregister_driver(&viocd_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 viopath_close(viopath_hostLp, viomajorsubtype_cdio, MAX_CD_REQ + 2);
730 vio_clearHandler(viomajorsubtype_cdio);
731 unregister_blkdev(VIOCD_MAJOR, VIOCD_DEVICE);
732}
733
734module_init(viocd_init);
735module_exit(viocd_exit);
736MODULE_LICENSE("GPL");