blob: 4e874c5fa60595036a7e10b26257384715b06b56 [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. Lunevc7705f3442008-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
Tejun Heo1c270302011-03-09 19:54:28 +0100189static unsigned int viocd_blk_check_events(struct gendisk *disk,
190 unsigned int clearing)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191{
192 struct disk_info *di = disk->private_data;
Tejun Heo1c270302011-03-09 19:54:28 +0100193 return cdrom_check_events(&di->viocd_info, clearing);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194}
195
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700196static const struct block_device_operations viocd_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 .owner = THIS_MODULE,
Al Viro4e379ae2008-03-02 10:25:44 -0500198 .open = viocd_blk_open,
199 .release = viocd_blk_release,
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +0200200 .ioctl = viocd_blk_ioctl,
Tejun Heo1c270302011-03-09 19:54:28 +0100201 .check_events = viocd_blk_check_events,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202};
203
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204static int viocd_open(struct cdrom_device_info *cdi, int purpose)
205{
206 struct disk_info *diskinfo = cdi->handle;
207 int device_no = DEVICE_NR(diskinfo);
208 HvLpEvent_Rc hvrc;
209 struct viocd_waitevent we;
210
211 init_completion(&we.com);
212 hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
213 HvLpEvent_Type_VirtualIo,
214 viomajorsubtype_cdio | viocdopen,
215 HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck,
216 viopath_sourceinst(viopath_hostLp),
217 viopath_targetinst(viopath_hostLp),
218 (u64)&we, VIOVERSION << 16, ((u64)device_no << 48),
219 0, 0, 0);
220 if (hvrc != 0) {
Joe Perchese597cd02010-07-01 08:24:32 +0200221 pr_warning("bad rc on HvCallEvent_signalLpEventFast %d\n",
222 (int)hvrc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 return -EIO;
224 }
225
226 wait_for_completion(&we.com);
227
228 if (we.rc) {
229 const struct vio_error_entry *err =
230 vio_lookup_rc(viocd_err_table, we.sub_result);
Joe Perchese597cd02010-07-01 08:24:32 +0200231 pr_warning("bad rc %d:0x%04X on open: %s\n",
232 we.rc, we.sub_result, err->msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 return -err->errno;
234 }
235
236 return 0;
237}
238
239static void viocd_release(struct cdrom_device_info *cdi)
240{
241 int device_no = DEVICE_NR((struct disk_info *)cdi->handle);
242 HvLpEvent_Rc hvrc;
243
244 hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
245 HvLpEvent_Type_VirtualIo,
246 viomajorsubtype_cdio | viocdclose,
247 HvLpEvent_AckInd_NoAck, HvLpEvent_AckType_ImmediateAck,
248 viopath_sourceinst(viopath_hostLp),
249 viopath_targetinst(viopath_hostLp), 0,
250 VIOVERSION << 16, ((u64)device_no << 48), 0, 0, 0);
251 if (hvrc != 0)
Joe Perchese597cd02010-07-01 08:24:32 +0200252 pr_warning("bad rc on HvCallEvent_signalLpEventFast %d\n",
253 (int)hvrc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254}
255
256/* Send a read or write request to OS/400 */
257static int send_request(struct request *req)
258{
259 HvLpEvent_Rc hvrc;
260 struct disk_info *diskinfo = req->rq_disk->private_data;
261 u64 len;
262 dma_addr_t dmaaddr;
263 int direction;
264 u16 cmd;
265 struct scatterlist sg;
266
267 BUG_ON(req->nr_phys_segments > 1);
268
269 if (rq_data_dir(req) == READ) {
270 direction = DMA_FROM_DEVICE;
271 cmd = viomajorsubtype_cdio | viocdread;
272 } else {
273 direction = DMA_TO_DEVICE;
274 cmd = viomajorsubtype_cdio | viocdwrite;
275 }
276
Jens Axboe3d1266c2007-10-24 13:21:21 +0200277 sg_init_table(&sg, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 if (blk_rq_map_sg(req->q, req, &sg) == 0) {
Joe Perchese597cd02010-07-01 08:24:32 +0200279 pr_warning("error setting up scatter/gather list\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 return -1;
281 }
282
283 if (dma_map_sg(diskinfo->dev, &sg, 1, direction) == 0) {
Joe Perchese597cd02010-07-01 08:24:32 +0200284 pr_warning("error allocating sg tce\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 return -1;
286 }
287 dmaaddr = sg_dma_address(&sg);
288 len = sg_dma_len(&sg);
289
290 hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
291 HvLpEvent_Type_VirtualIo, cmd,
292 HvLpEvent_AckInd_DoAck,
293 HvLpEvent_AckType_ImmediateAck,
294 viopath_sourceinst(viopath_hostLp),
295 viopath_targetinst(viopath_hostLp),
296 (u64)req, VIOVERSION << 16,
297 ((u64)DEVICE_NR(diskinfo) << 48) | dmaaddr,
Tejun Heo83096eb2009-05-07 22:24:39 +0900298 (u64)blk_rq_pos(req) * 512, len, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 if (hvrc != HvLpEvent_Rc_Good) {
Joe Perchese597cd02010-07-01 08:24:32 +0200300 pr_warning("hv error on op %d\n", (int)hvrc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 return -1;
302 }
303
304 return 0;
305}
306
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307static int rwreq;
308
Jens Axboe165125e2007-07-24 09:28:11 +0200309static void do_viocd_request(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310{
311 struct request *req;
312
Tejun Heo9934c8c2009-05-08 11:54:16 +0900313 while ((rwreq == 0) && ((req = blk_fetch_request(q)) != NULL)) {
Christoph Hellwig33659eb2010-08-07 18:17:56 +0200314 if (req->cmd_type != REQ_TYPE_FS)
Tejun Heo40cbbb72009-04-23 11:05:19 +0900315 __blk_end_request_all(req, -EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 else if (send_request(req) < 0) {
Joe Perchese597cd02010-07-01 08:24:32 +0200317 pr_warning("unable to send message to OS/400!\n");
Tejun Heo40cbbb72009-04-23 11:05:19 +0900318 __blk_end_request_all(req, -EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 } else
320 rwreq++;
321 }
322}
323
Tejun Heo1c270302011-03-09 19:54:28 +0100324static unsigned int viocd_check_events(struct cdrom_device_info *cdi,
325 unsigned int clearing, int disc_nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326{
327 struct viocd_waitevent we;
328 HvLpEvent_Rc hvrc;
329 int device_no = DEVICE_NR((struct disk_info *)cdi->handle);
330
331 init_completion(&we.com);
332
333 /* Send the open event to OS/400 */
334 hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
335 HvLpEvent_Type_VirtualIo,
336 viomajorsubtype_cdio | viocdcheck,
337 HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck,
338 viopath_sourceinst(viopath_hostLp),
339 viopath_targetinst(viopath_hostLp),
340 (u64)&we, VIOVERSION << 16, ((u64)device_no << 48),
341 0, 0, 0);
342 if (hvrc != 0) {
Joe Perchese597cd02010-07-01 08:24:32 +0200343 pr_warning("bad rc on HvCallEvent_signalLpEventFast %d\n",
344 (int)hvrc);
Tejun Heo1c270302011-03-09 19:54:28 +0100345 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 }
347
348 wait_for_completion(&we.com);
349
350 /* Check the return code. If bad, assume no change */
351 if (we.rc) {
352 const struct vio_error_entry *err =
353 vio_lookup_rc(viocd_err_table, we.sub_result);
Joe Perchese597cd02010-07-01 08:24:32 +0200354 pr_warning("bad rc %d:0x%04X on check_change: %s; Assuming no change\n",
355 we.rc, we.sub_result, err->msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 return 0;
357 }
358
Tejun Heo1c270302011-03-09 19:54:28 +0100359 return we.changed ? DISK_EVENT_MEDIA_CHANGE : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360}
361
362static int viocd_lock_door(struct cdrom_device_info *cdi, int locking)
363{
364 HvLpEvent_Rc hvrc;
365 u64 device_no = DEVICE_NR((struct disk_info *)cdi->handle);
366 /* NOTE: flags is 1 or 0 so it won't overwrite the device_no */
367 u64 flags = !!locking;
368 struct viocd_waitevent we;
369
370 init_completion(&we.com);
371
372 /* Send the lockdoor event to OS/400 */
373 hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
374 HvLpEvent_Type_VirtualIo,
375 viomajorsubtype_cdio | viocdlockdoor,
376 HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck,
377 viopath_sourceinst(viopath_hostLp),
378 viopath_targetinst(viopath_hostLp),
379 (u64)&we, VIOVERSION << 16,
380 (device_no << 48) | (flags << 32), 0, 0, 0);
381 if (hvrc != 0) {
Joe Perchese597cd02010-07-01 08:24:32 +0200382 pr_warning("bad rc on HvCallEvent_signalLpEventFast %d\n",
383 (int)hvrc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 return -EIO;
385 }
386
387 wait_for_completion(&we.com);
388
389 if (we.rc != 0)
390 return -EIO;
391 return 0;
392}
393
394static int viocd_packet(struct cdrom_device_info *cdi,
395 struct packet_command *cgc)
396{
397 unsigned int buflen = cgc->buflen;
398 int ret = -EIO;
399
400 switch (cgc->cmd[0]) {
401 case GPCMD_READ_DISC_INFO:
402 {
403 disc_information *di = (disc_information *)cgc->buffer;
404
405 if (buflen >= 2) {
406 di->disc_information_length = cpu_to_be16(1);
407 ret = 0;
408 }
409 if (buflen >= 3)
410 di->erasable =
411 (cdi->ops->capability & ~cdi->mask
412 & (CDC_DVD_RAM | CDC_RAM)) != 0;
413 }
414 break;
Stephen Rothwell9db29252005-05-27 12:52:58 -0700415 case GPCMD_GET_CONFIGURATION:
416 if (cgc->cmd[3] == CDF_RWRT) {
417 struct rwrt_feature_desc *rfd = (struct rwrt_feature_desc *)(cgc->buffer + sizeof(struct feature_header));
418
419 if ((buflen >=
420 (sizeof(struct feature_header) + sizeof(*rfd))) &&
421 (cdi->ops->capability & ~cdi->mask
422 & (CDC_DVD_RAM | CDC_RAM))) {
423 rfd->feature_code = cpu_to_be16(CDF_RWRT);
424 rfd->curr = 1;
425 ret = 0;
426 }
427 }
428 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 default:
430 if (cgc->sense) {
431 /* indicate Unknown code */
432 cgc->sense->sense_key = 0x05;
433 cgc->sense->asc = 0x20;
434 cgc->sense->ascq = 0x00;
435 }
436 break;
437 }
438
439 cgc->stat = ret;
440 return ret;
441}
442
443static void restart_all_queues(int first_index)
444{
445 int i;
446
447 for (i = first_index + 1; i < viocd_numdev; i++)
448 if (viocd_diskinfo[i].viocd_disk)
449 blk_run_queue(viocd_diskinfo[i].viocd_disk->queue);
450 for (i = 0; i <= first_index; i++)
451 if (viocd_diskinfo[i].viocd_disk)
452 blk_run_queue(viocd_diskinfo[i].viocd_disk->queue);
453}
454
455/* This routine handles incoming CD LP events */
456static void vio_handle_cd_event(struct HvLpEvent *event)
457{
458 struct viocdlpevent *bevent;
459 struct viocd_waitevent *pwe;
460 struct disk_info *di;
461 unsigned long flags;
462 struct request *req;
463
464
465 if (event == NULL)
466 /* Notification that a partition went away! */
467 return;
468 /* First, we should NEVER get an int here...only acks */
Stephen Rothwell677f8c02006-01-12 13:47:43 +1100469 if (hvlpevent_is_int(event)) {
Joe Perchese597cd02010-07-01 08:24:32 +0200470 pr_warning("Yikes! got an int in viocd event handler!\n");
Stephen Rothwell677f8c02006-01-12 13:47:43 +1100471 if (hvlpevent_need_ack(event)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 event->xRc = HvLpEvent_Rc_InvalidSubtype;
473 HvCallEvent_ackLpEvent(event);
474 }
475 }
476
477 bevent = (struct viocdlpevent *)event;
478
479 switch (event->xSubtype & VIOMINOR_SUBTYPE_MASK) {
480 case viocdopen:
481 if (event->xRc == 0) {
482 di = &viocd_diskinfo[bevent->disk];
Martin K. Petersene1defc42009-05-22 17:17:49 -0400483 blk_queue_logical_block_size(di->viocd_disk->queue,
484 bevent->block_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 set_capacity(di->viocd_disk,
486 bevent->media_size *
487 bevent->block_size / 512);
488 }
489 /* FALLTHROUGH !! */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 case viocdlockdoor:
491 pwe = (struct viocd_waitevent *)event->xCorrelationToken;
492return_complete:
493 pwe->rc = event->xRc;
494 pwe->sub_result = bevent->sub_result;
495 complete(&pwe->com);
496 break;
497
498 case viocdcheck:
499 pwe = (struct viocd_waitevent *)event->xCorrelationToken;
500 pwe->changed = bevent->flags;
501 goto return_complete;
502
503 case viocdclose:
504 break;
505
506 case viocdwrite:
507 case viocdread:
508 /*
509 * Since this is running in interrupt mode, we need to
510 * make sure we're not stepping on any global I/O operations
511 */
512 di = &viocd_diskinfo[bevent->disk];
513 spin_lock_irqsave(&viocd_reqlock, flags);
514 dma_unmap_single(di->dev, bevent->token, bevent->len,
515 ((event->xSubtype & VIOMINOR_SUBTYPE_MASK) == viocdread)
516 ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
517 req = (struct request *)bevent->event.xCorrelationToken;
518 rwreq--;
519
520 if (event->xRc != HvLpEvent_Rc_Good) {
521 const struct vio_error_entry *err =
522 vio_lookup_rc(viocd_err_table,
523 bevent->sub_result);
Joe Perchese597cd02010-07-01 08:24:32 +0200524 pr_warning("request %p failed with rc %d:0x%04X: %s\n",
525 req, event->xRc,
526 bevent->sub_result, err->msg);
Tejun Heo40cbbb72009-04-23 11:05:19 +0900527 __blk_end_request_all(req, -EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 } else
Tejun Heo40cbbb72009-04-23 11:05:19 +0900529 __blk_end_request_all(req, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530
531 /* restart handling of incoming requests */
532 spin_unlock_irqrestore(&viocd_reqlock, flags);
533 restart_all_queues(bevent->disk);
534 break;
535
536 default:
Joe Perchese597cd02010-07-01 08:24:32 +0200537 pr_warning("message with invalid subtype %0x04X!\n",
538 event->xSubtype & VIOMINOR_SUBTYPE_MASK);
Stephen Rothwell677f8c02006-01-12 13:47:43 +1100539 if (hvlpevent_need_ack(event)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 event->xRc = HvLpEvent_Rc_InvalidSubtype;
541 HvCallEvent_ackLpEvent(event);
542 }
543 }
544}
545
Borislav Petkov3e636f72008-08-18 21:40:03 +0200546static int viocd_audio_ioctl(struct cdrom_device_info *cdi, unsigned int cmd,
547 void *arg)
548{
549 return -EINVAL;
550}
551
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552static struct cdrom_device_ops viocd_dops = {
553 .open = viocd_open,
554 .release = viocd_release,
Tejun Heo1c270302011-03-09 19:54:28 +0100555 .check_events = viocd_check_events,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 .lock_door = viocd_lock_door,
557 .generic_packet = viocd_packet,
Borislav Petkov3e636f72008-08-18 21:40:03 +0200558 .audio_ioctl = viocd_audio_ioctl,
Christoph Hellwig6a2900b2006-03-23 03:00:15 -0800559 .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 -0700560};
561
Stephen Rothwellc7211922008-02-05 14:17:31 +1100562static int find_capability(const char *type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563{
564 struct capability_entry *entry;
565
566 for(entry = capability_table; entry->type; ++entry)
567 if(!strncmp(entry->type, type, 4))
568 break;
569 return entry->capability;
570}
571
572static int viocd_probe(struct vio_dev *vdev, const struct vio_device_id *id)
573{
574 struct gendisk *gendisk;
575 int deviceno;
576 struct disk_info *d;
577 struct cdrom_device_info *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 struct request_queue *q;
Grant Likely61c7a082010-04-13 16:12:29 -0700579 struct device_node *node = vdev->dev.of_node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
581 deviceno = vdev->unit_address;
Roel Kluind3375ea2009-05-20 08:42:06 +0200582 if (deviceno >= VIOCD_MAX_CD)
Stephen Rothwellb833b482007-10-11 14:57:26 +1000583 return -ENODEV;
584 if (!node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 return -ENODEV;
586
Stephen Rothwellb833b482007-10-11 14:57:26 +1000587 if (deviceno >= viocd_numdev)
588 viocd_numdev = deviceno + 1;
589
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 d = &viocd_diskinfo[deviceno];
Stephen Rothwellb833b482007-10-11 14:57:26 +1000591 d->rsrcname = of_get_property(node, "linux,vio_rsrcname", NULL);
592 d->type = of_get_property(node, "linux,vio_type", NULL);
593 d->model = of_get_property(node, "linux,vio_model", NULL);
594
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 c = &d->viocd_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
597 c->ops = &viocd_dops;
598 c->speed = 4;
599 c->capacity = 1;
600 c->handle = d;
Stephen Rothwellb833b482007-10-11 14:57:26 +1000601 c->mask = ~find_capability(d->type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 sprintf(c->name, VIOCD_DEVICE "%c", 'a' + deviceno);
603
604 if (register_cdrom(c) != 0) {
Joe Perchese597cd02010-07-01 08:24:32 +0200605 pr_warning("Cannot register viocd CD-ROM %s!\n", c->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 goto out;
607 }
Joe Perchese597cd02010-07-01 08:24:32 +0200608 pr_info("cd %s is iSeries resource %10.10s type %4.4s, model %3.3s\n",
609 c->name, d->rsrcname, d->type, d->model);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 q = blk_init_queue(do_viocd_request, &viocd_reqlock);
611 if (q == NULL) {
Joe Perchese597cd02010-07-01 08:24:32 +0200612 pr_warning("Cannot allocate queue for %s!\n", c->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 goto out_unregister_cdrom;
614 }
615 gendisk = alloc_disk(1);
616 if (gendisk == NULL) {
Joe Perchese597cd02010-07-01 08:24:32 +0200617 pr_warning("Cannot create gendisk for %s!\n", c->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 goto out_cleanup_queue;
619 }
620 gendisk->major = VIOCD_MAJOR;
621 gendisk->first_minor = deviceno;
622 strncpy(gendisk->disk_name, c->name,
623 sizeof(gendisk->disk_name));
Martin K. Petersen8a783622010-02-26 00:20:39 -0500624 blk_queue_max_segments(q, 1);
Martin K. Petersen086fa5f2010-02-26 00:20:38 -0500625 blk_queue_max_hw_sectors(q, 4096 / 512);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 gendisk->queue = q;
627 gendisk->fops = &viocd_fops;
628 gendisk->flags = GENHD_FL_CD|GENHD_FL_REMOVABLE;
Tejun Heo1c270302011-03-09 19:54:28 +0100629 gendisk->events = DISK_EVENT_MEDIA_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 set_capacity(gendisk, 0);
631 gendisk->private_data = d;
632 d->viocd_disk = gendisk;
633 d->dev = &vdev->dev;
634 gendisk->driverfs_dev = d->dev;
635 add_disk(gendisk);
636 return 0;
637
638out_cleanup_queue:
639 blk_cleanup_queue(q);
640out_unregister_cdrom:
641 unregister_cdrom(c);
642out:
643 return -ENODEV;
644}
645
646static int viocd_remove(struct vio_dev *vdev)
647{
648 struct disk_info *d = &viocd_diskinfo[vdev->unit_address];
649
Akinobu Mita0a0c4112008-03-26 12:09:02 +0100650 unregister_cdrom(&d->viocd_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 del_gendisk(d->viocd_disk);
652 blk_cleanup_queue(d->viocd_disk->queue);
653 put_disk(d->viocd_disk);
654 return 0;
655}
656
657/**
658 * viocd_device_table: Used by vio.c to match devices that we
659 * support.
660 */
661static struct vio_device_id viocd_device_table[] __devinitdata = {
Stephen Rothwellde0fe3b2006-05-15 13:44:01 +1000662 { "block", "IBM,iSeries-viocd" },
Stephen Rothwellfb120da2005-08-17 16:42:59 +1000663 { "", "" }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665MODULE_DEVICE_TABLE(vio, viocd_device_table);
Stephen Rothwell915124d2005-10-24 15:12:22 +1000666
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667static struct vio_driver viocd_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 .id_table = viocd_device_table,
669 .probe = viocd_probe,
Stephen Rothwell6fdf5392005-10-24 14:53:21 +1000670 .remove = viocd_remove,
671 .driver = {
672 .name = "viocd",
Stephen Rothwell915124d2005-10-24 15:12:22 +1000673 .owner = THIS_MODULE,
Stephen Rothwell6fdf5392005-10-24 14:53:21 +1000674 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675};
676
677static int __init viocd_init(void)
678{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 int ret = 0;
680
Stephen Rothwell31c72ad2006-12-15 15:44:04 +1100681 if (!firmware_has_feature(FW_FEATURE_ISERIES))
682 return -ENODEV;
683
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 if (viopath_hostLp == HvLpIndexInvalid) {
685 vio_set_hostlp();
686 /* If we don't have a host, bail out */
687 if (viopath_hostLp == HvLpIndexInvalid)
688 return -ENODEV;
689 }
690
Joe Perchese597cd02010-07-01 08:24:32 +0200691 pr_info("vers " VIOCD_VERS ", hosting partition %d\n", viopath_hostLp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
693 if (register_blkdev(VIOCD_MAJOR, VIOCD_DEVICE) != 0) {
Joe Perchese597cd02010-07-01 08:24:32 +0200694 pr_warning("Unable to get major %d for %s\n",
695 VIOCD_MAJOR, VIOCD_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 return -EIO;
697 }
698
699 ret = viopath_open(viopath_hostLp, viomajorsubtype_cdio,
700 MAX_CD_REQ + 2);
701 if (ret) {
Joe Perchese597cd02010-07-01 08:24:32 +0200702 pr_warning("error opening path to host partition %d\n",
703 viopath_hostLp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 goto out_unregister;
705 }
706
707 /* Initialize our request handler */
708 vio_setHandler(viomajorsubtype_cdio, vio_handle_cd_event);
709
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 spin_lock_init(&viocd_reqlock);
711
712 ret = vio_register_driver(&viocd_driver);
713 if (ret)
714 goto out_free_info;
715
Denis V. Lunevc7705f3442008-04-29 01:02:35 -0700716 proc_create("iSeries/viocd", S_IFREG|S_IRUGO, NULL,
717 &proc_viocd_operations);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 return 0;
719
720out_free_info:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 vio_clearHandler(viomajorsubtype_cdio);
722 viopath_close(viopath_hostLp, viomajorsubtype_cdio, MAX_CD_REQ + 2);
723out_unregister:
724 unregister_blkdev(VIOCD_MAJOR, VIOCD_DEVICE);
725 return ret;
726}
727
728static void __exit viocd_exit(void)
729{
730 remove_proc_entry("iSeries/viocd", NULL);
731 vio_unregister_driver(&viocd_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 viopath_close(viopath_hostLp, viomajorsubtype_cdio, MAX_CD_REQ + 2);
733 vio_clearHandler(viomajorsubtype_cdio);
734 unregister_blkdev(VIOCD_MAJOR, VIOCD_DEVICE);
735}
736
737module_init(viocd_init);
738module_exit(viocd_exit);
739MODULE_LICENSE("GPL");