Linux-2.6.12-rc2
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / block / viodasd.c
blob46e56a25d2c8875966462bef4c53a225ac572090
1 /* -*- linux-c -*-
2 * viodasd.c
3 * Authors: Dave Boutcher <boutcher@us.ibm.com>
4 * Ryan Arnold <ryanarn@us.ibm.com>
5 * Colin Devilbiss <devilbis@us.ibm.com>
6 * Stephen Rothwell <sfr@au1.ibm.com>
8 * (C) Copyright 2000-2004 IBM Corporation
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License as
12 * published by the Free Software Foundation; either version 2 of the
13 * License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 * This routine provides access to disk space (termed "DASD" in historical
25 * IBM terms) owned and managed by an OS/400 partition running on the
26 * same box as this Linux partition.
28 * All disk operations are performed by sending messages back and forth to
29 * the OS/400 partition.
31 #include <linux/major.h>
32 #include <linux/fs.h>
33 #include <linux/module.h>
34 #include <linux/kernel.h>
35 #include <linux/blkdev.h>
36 #include <linux/genhd.h>
37 #include <linux/hdreg.h>
38 #include <linux/errno.h>
39 #include <linux/init.h>
40 #include <linux/string.h>
41 #include <linux/dma-mapping.h>
42 #include <linux/completion.h>
43 #include <linux/device.h>
44 #include <linux/kernel.h>
46 #include <asm/uaccess.h>
47 #include <asm/vio.h>
48 #include <asm/iSeries/HvTypes.h>
49 #include <asm/iSeries/HvLpEvent.h>
50 #include <asm/iSeries/HvLpConfig.h>
51 #include <asm/iSeries/vio.h>
53 MODULE_DESCRIPTION("iSeries Virtual DASD");
54 MODULE_AUTHOR("Dave Boutcher");
55 MODULE_LICENSE("GPL");
58 * We only support 7 partitions per physical disk....so with minor
59 * numbers 0-255 we get a maximum of 32 disks.
61 #define VIOD_GENHD_NAME "iseries/vd"
62 #define VIOD_GENHD_DEVFS_NAME "iseries/disc"
64 #define VIOD_VERS "1.64"
66 #define VIOD_KERN_WARNING KERN_WARNING "viod: "
67 #define VIOD_KERN_INFO KERN_INFO "viod: "
69 enum {
70 PARTITION_SHIFT = 3,
71 MAX_DISKNO = HVMAXARCHITECTEDVIRTUALDISKS,
72 MAX_DISK_NAME = sizeof(((struct gendisk *)0)->disk_name)
75 static DEFINE_SPINLOCK(viodasd_spinlock);
77 #define VIOMAXREQ 16
78 #define VIOMAXBLOCKDMA 12
80 #define DEVICE_NO(cell) ((struct viodasd_device *)(cell) - &viodasd_devices[0])
82 struct open_data {
83 u64 disk_size;
84 u16 max_disk;
85 u16 cylinders;
86 u16 tracks;
87 u16 sectors;
88 u16 bytes_per_sector;
91 struct rw_data {
92 u64 offset;
93 struct {
94 u32 token;
95 u32 reserved;
96 u64 len;
97 } dma_info[VIOMAXBLOCKDMA];
100 struct vioblocklpevent {
101 struct HvLpEvent event;
102 u32 reserved;
103 u16 version;
104 u16 sub_result;
105 u16 disk;
106 u16 flags;
107 union {
108 struct open_data open_data;
109 struct rw_data rw_data;
110 u64 changed;
111 } u;
114 #define vioblockflags_ro 0x0001
116 enum vioblocksubtype {
117 vioblockopen = 0x0001,
118 vioblockclose = 0x0002,
119 vioblockread = 0x0003,
120 vioblockwrite = 0x0004,
121 vioblockflush = 0x0005,
122 vioblockcheck = 0x0007
125 struct viodasd_waitevent {
126 struct completion com;
127 int rc;
128 u16 sub_result;
129 int max_disk; /* open */
132 static const struct vio_error_entry viodasd_err_table[] = {
133 { 0x0201, EINVAL, "Invalid Range" },
134 { 0x0202, EINVAL, "Invalid Token" },
135 { 0x0203, EIO, "DMA Error" },
136 { 0x0204, EIO, "Use Error" },
137 { 0x0205, EIO, "Release Error" },
138 { 0x0206, EINVAL, "Invalid Disk" },
139 { 0x0207, EBUSY, "Cant Lock" },
140 { 0x0208, EIO, "Already Locked" },
141 { 0x0209, EIO, "Already Unlocked" },
142 { 0x020A, EIO, "Invalid Arg" },
143 { 0x020B, EIO, "Bad IFS File" },
144 { 0x020C, EROFS, "Read Only Device" },
145 { 0x02FF, EIO, "Internal Error" },
146 { 0x0000, 0, NULL },
150 * Figure out the biggest I/O request (in sectors) we can accept
152 #define VIODASD_MAXSECTORS (4096 / 512 * VIOMAXBLOCKDMA)
155 * Number of disk I/O requests we've sent to OS/400
157 static int num_req_outstanding;
160 * This is our internal structure for keeping track of disk devices
162 struct viodasd_device {
163 u16 cylinders;
164 u16 tracks;
165 u16 sectors;
166 u16 bytes_per_sector;
167 u64 size;
168 int read_only;
169 spinlock_t q_lock;
170 struct gendisk *disk;
171 struct device *dev;
172 } viodasd_devices[MAX_DISKNO];
175 * External open entry point.
177 static int viodasd_open(struct inode *ino, struct file *fil)
179 struct viodasd_device *d = ino->i_bdev->bd_disk->private_data;
180 HvLpEvent_Rc hvrc;
181 struct viodasd_waitevent we;
182 u16 flags = 0;
184 if (d->read_only) {
185 if ((fil != NULL) && (fil->f_mode & FMODE_WRITE))
186 return -EROFS;
187 flags = vioblockflags_ro;
190 init_completion(&we.com);
192 /* Send the open event to OS/400 */
193 hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
194 HvLpEvent_Type_VirtualIo,
195 viomajorsubtype_blockio | vioblockopen,
196 HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck,
197 viopath_sourceinst(viopath_hostLp),
198 viopath_targetinst(viopath_hostLp),
199 (u64)(unsigned long)&we, VIOVERSION << 16,
200 ((u64)DEVICE_NO(d) << 48) | ((u64)flags << 32),
201 0, 0, 0);
202 if (hvrc != 0) {
203 printk(VIOD_KERN_WARNING "HV open failed %d\n", (int)hvrc);
204 return -EIO;
207 wait_for_completion(&we.com);
209 /* Check the return code */
210 if (we.rc != 0) {
211 const struct vio_error_entry *err =
212 vio_lookup_rc(viodasd_err_table, we.sub_result);
214 printk(VIOD_KERN_WARNING
215 "bad rc opening disk: %d:0x%04x (%s)\n",
216 (int)we.rc, we.sub_result, err->msg);
217 return -EIO;
220 return 0;
224 * External release entry point.
226 static int viodasd_release(struct inode *ino, struct file *fil)
228 struct viodasd_device *d = ino->i_bdev->bd_disk->private_data;
229 HvLpEvent_Rc hvrc;
231 /* Send the event to OS/400. We DON'T expect a response */
232 hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
233 HvLpEvent_Type_VirtualIo,
234 viomajorsubtype_blockio | vioblockclose,
235 HvLpEvent_AckInd_NoAck, HvLpEvent_AckType_ImmediateAck,
236 viopath_sourceinst(viopath_hostLp),
237 viopath_targetinst(viopath_hostLp),
238 0, VIOVERSION << 16,
239 ((u64)DEVICE_NO(d) << 48) /* | ((u64)flags << 32) */,
240 0, 0, 0);
241 if (hvrc != 0)
242 printk(VIOD_KERN_WARNING "HV close call failed %d\n",
243 (int)hvrc);
244 return 0;
248 /* External ioctl entry point.
250 static int viodasd_ioctl(struct inode *ino, struct file *fil,
251 unsigned int cmd, unsigned long arg)
253 unsigned char sectors;
254 unsigned char heads;
255 unsigned short cylinders;
256 struct hd_geometry *geo;
257 struct gendisk *gendisk;
258 struct viodasd_device *d;
260 switch (cmd) {
261 case HDIO_GETGEO:
262 geo = (struct hd_geometry *)arg;
263 if (geo == NULL)
264 return -EINVAL;
265 if (!access_ok(VERIFY_WRITE, geo, sizeof(*geo)))
266 return -EFAULT;
267 gendisk = ino->i_bdev->bd_disk;
268 d = gendisk->private_data;
269 sectors = d->sectors;
270 if (sectors == 0)
271 sectors = 32;
272 heads = d->tracks;
273 if (heads == 0)
274 heads = 64;
275 cylinders = d->cylinders;
276 if (cylinders == 0)
277 cylinders = get_capacity(gendisk) / (sectors * heads);
278 if (__put_user(sectors, &geo->sectors) ||
279 __put_user(heads, &geo->heads) ||
280 __put_user(cylinders, &geo->cylinders) ||
281 __put_user(get_start_sect(ino->i_bdev), &geo->start))
282 return -EFAULT;
283 return 0;
286 return -EINVAL;
290 * Our file operations table
292 static struct block_device_operations viodasd_fops = {
293 .owner = THIS_MODULE,
294 .open = viodasd_open,
295 .release = viodasd_release,
296 .ioctl = viodasd_ioctl,
300 * End a request
302 static void viodasd_end_request(struct request *req, int uptodate,
303 int num_sectors)
305 if (end_that_request_first(req, uptodate, num_sectors))
306 return;
307 add_disk_randomness(req->rq_disk);
308 end_that_request_last(req);
312 * Send an actual I/O request to OS/400
314 static int send_request(struct request *req)
316 u64 start;
317 int direction;
318 int nsg;
319 u16 viocmd;
320 HvLpEvent_Rc hvrc;
321 struct vioblocklpevent *bevent;
322 struct scatterlist sg[VIOMAXBLOCKDMA];
323 int sgindex;
324 int statindex;
325 struct viodasd_device *d;
326 unsigned long flags;
328 start = (u64)req->sector << 9;
330 if (rq_data_dir(req) == READ) {
331 direction = DMA_FROM_DEVICE;
332 viocmd = viomajorsubtype_blockio | vioblockread;
333 statindex = 0;
334 } else {
335 direction = DMA_TO_DEVICE;
336 viocmd = viomajorsubtype_blockio | vioblockwrite;
337 statindex = 1;
340 d = req->rq_disk->private_data;
342 /* Now build the scatter-gather list */
343 nsg = blk_rq_map_sg(req->q, req, sg);
344 nsg = dma_map_sg(d->dev, sg, nsg, direction);
346 spin_lock_irqsave(&viodasd_spinlock, flags);
347 num_req_outstanding++;
349 /* This optimization handles a single DMA block */
350 if (nsg == 1)
351 hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
352 HvLpEvent_Type_VirtualIo, viocmd,
353 HvLpEvent_AckInd_DoAck,
354 HvLpEvent_AckType_ImmediateAck,
355 viopath_sourceinst(viopath_hostLp),
356 viopath_targetinst(viopath_hostLp),
357 (u64)(unsigned long)req, VIOVERSION << 16,
358 ((u64)DEVICE_NO(d) << 48), start,
359 ((u64)sg_dma_address(&sg[0])) << 32,
360 sg_dma_len(&sg[0]));
361 else {
362 bevent = (struct vioblocklpevent *)
363 vio_get_event_buffer(viomajorsubtype_blockio);
364 if (bevent == NULL) {
365 printk(VIOD_KERN_WARNING
366 "error allocating disk event buffer\n");
367 goto error_ret;
371 * Now build up the actual request. Note that we store
372 * the pointer to the request in the correlation
373 * token so we can match the response up later
375 memset(bevent, 0, sizeof(struct vioblocklpevent));
376 bevent->event.xFlags.xValid = 1;
377 bevent->event.xFlags.xFunction = HvLpEvent_Function_Int;
378 bevent->event.xFlags.xAckInd = HvLpEvent_AckInd_DoAck;
379 bevent->event.xFlags.xAckType = HvLpEvent_AckType_ImmediateAck;
380 bevent->event.xType = HvLpEvent_Type_VirtualIo;
381 bevent->event.xSubtype = viocmd;
382 bevent->event.xSourceLp = HvLpConfig_getLpIndex();
383 bevent->event.xTargetLp = viopath_hostLp;
384 bevent->event.xSizeMinus1 =
385 offsetof(struct vioblocklpevent, u.rw_data.dma_info) +
386 (sizeof(bevent->u.rw_data.dma_info[0]) * nsg) - 1;
387 bevent->event.xSourceInstanceId =
388 viopath_sourceinst(viopath_hostLp);
389 bevent->event.xTargetInstanceId =
390 viopath_targetinst(viopath_hostLp);
391 bevent->event.xCorrelationToken = (u64)req;
392 bevent->version = VIOVERSION;
393 bevent->disk = DEVICE_NO(d);
394 bevent->u.rw_data.offset = start;
397 * Copy just the dma information from the sg list
398 * into the request
400 for (sgindex = 0; sgindex < nsg; sgindex++) {
401 bevent->u.rw_data.dma_info[sgindex].token =
402 sg_dma_address(&sg[sgindex]);
403 bevent->u.rw_data.dma_info[sgindex].len =
404 sg_dma_len(&sg[sgindex]);
407 /* Send the request */
408 hvrc = HvCallEvent_signalLpEvent(&bevent->event);
409 vio_free_event_buffer(viomajorsubtype_blockio, bevent);
412 if (hvrc != HvLpEvent_Rc_Good) {
413 printk(VIOD_KERN_WARNING
414 "error sending disk event to OS/400 (rc %d)\n",
415 (int)hvrc);
416 goto error_ret;
418 spin_unlock_irqrestore(&viodasd_spinlock, flags);
419 return 0;
421 error_ret:
422 num_req_outstanding--;
423 spin_unlock_irqrestore(&viodasd_spinlock, flags);
424 dma_unmap_sg(d->dev, sg, nsg, direction);
425 return -1;
429 * This is the external request processing routine
431 static void do_viodasd_request(request_queue_t *q)
433 struct request *req;
436 * If we already have the maximum number of requests
437 * outstanding to OS/400 just bail out. We'll come
438 * back later.
440 while (num_req_outstanding < VIOMAXREQ) {
441 req = elv_next_request(q);
442 if (req == NULL)
443 return;
444 /* dequeue the current request from the queue */
445 blkdev_dequeue_request(req);
446 /* check that request contains a valid command */
447 if (!blk_fs_request(req)) {
448 viodasd_end_request(req, 0, req->hard_nr_sectors);
449 continue;
451 /* Try sending the request */
452 if (send_request(req) != 0)
453 viodasd_end_request(req, 0, req->hard_nr_sectors);
458 * Probe a single disk and fill in the viodasd_device structure
459 * for it.
461 static void probe_disk(struct viodasd_device *d)
463 HvLpEvent_Rc hvrc;
464 struct viodasd_waitevent we;
465 int dev_no = DEVICE_NO(d);
466 struct gendisk *g;
467 struct request_queue *q;
468 u16 flags = 0;
470 retry:
471 init_completion(&we.com);
473 /* Send the open event to OS/400 */
474 hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
475 HvLpEvent_Type_VirtualIo,
476 viomajorsubtype_blockio | vioblockopen,
477 HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck,
478 viopath_sourceinst(viopath_hostLp),
479 viopath_targetinst(viopath_hostLp),
480 (u64)(unsigned long)&we, VIOVERSION << 16,
481 ((u64)dev_no << 48) | ((u64)flags<< 32),
482 0, 0, 0);
483 if (hvrc != 0) {
484 printk(VIOD_KERN_WARNING "bad rc on HV open %d\n", (int)hvrc);
485 return;
488 wait_for_completion(&we.com);
490 if (we.rc != 0) {
491 if (flags != 0)
492 return;
493 /* try again with read only flag set */
494 flags = vioblockflags_ro;
495 goto retry;
497 if (we.max_disk > (MAX_DISKNO - 1)) {
498 static int warned;
500 if (warned == 0) {
501 warned++;
502 printk(VIOD_KERN_INFO
503 "Only examining the first %d "
504 "of %d disks connected\n",
505 MAX_DISKNO, we.max_disk + 1);
509 /* Send the close event to OS/400. We DON'T expect a response */
510 hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
511 HvLpEvent_Type_VirtualIo,
512 viomajorsubtype_blockio | vioblockclose,
513 HvLpEvent_AckInd_NoAck, HvLpEvent_AckType_ImmediateAck,
514 viopath_sourceinst(viopath_hostLp),
515 viopath_targetinst(viopath_hostLp),
516 0, VIOVERSION << 16,
517 ((u64)dev_no << 48) | ((u64)flags << 32),
518 0, 0, 0);
519 if (hvrc != 0) {
520 printk(VIOD_KERN_WARNING
521 "bad rc sending event to OS/400 %d\n", (int)hvrc);
522 return;
524 /* create the request queue for the disk */
525 spin_lock_init(&d->q_lock);
526 q = blk_init_queue(do_viodasd_request, &d->q_lock);
527 if (q == NULL) {
528 printk(VIOD_KERN_WARNING "cannot allocate queue for disk %d\n",
529 dev_no);
530 return;
532 g = alloc_disk(1 << PARTITION_SHIFT);
533 if (g == NULL) {
534 printk(VIOD_KERN_WARNING
535 "cannot allocate disk structure for disk %d\n",
536 dev_no);
537 blk_cleanup_queue(q);
538 return;
541 d->disk = g;
542 blk_queue_max_hw_segments(q, VIOMAXBLOCKDMA);
543 blk_queue_max_phys_segments(q, VIOMAXBLOCKDMA);
544 blk_queue_max_sectors(q, VIODASD_MAXSECTORS);
545 g->major = VIODASD_MAJOR;
546 g->first_minor = dev_no << PARTITION_SHIFT;
547 if (dev_no >= 26)
548 snprintf(g->disk_name, sizeof(g->disk_name),
549 VIOD_GENHD_NAME "%c%c",
550 'a' + (dev_no / 26) - 1, 'a' + (dev_no % 26));
551 else
552 snprintf(g->disk_name, sizeof(g->disk_name),
553 VIOD_GENHD_NAME "%c", 'a' + (dev_no % 26));
554 snprintf(g->devfs_name, sizeof(g->devfs_name),
555 "%s%d", VIOD_GENHD_DEVFS_NAME, dev_no);
556 g->fops = &viodasd_fops;
557 g->queue = q;
558 g->private_data = d;
559 g->driverfs_dev = d->dev;
560 set_capacity(g, d->size >> 9);
562 printk(VIOD_KERN_INFO "disk %d: %lu sectors (%lu MB) "
563 "CHS=%d/%d/%d sector size %d%s\n",
564 dev_no, (unsigned long)(d->size >> 9),
565 (unsigned long)(d->size >> 20),
566 (int)d->cylinders, (int)d->tracks,
567 (int)d->sectors, (int)d->bytes_per_sector,
568 d->read_only ? " (RO)" : "");
570 /* register us in the global list */
571 add_disk(g);
574 /* returns the total number of scatterlist elements converted */
575 static int block_event_to_scatterlist(const struct vioblocklpevent *bevent,
576 struct scatterlist *sg, int *total_len)
578 int i, numsg;
579 const struct rw_data *rw_data = &bevent->u.rw_data;
580 static const int offset =
581 offsetof(struct vioblocklpevent, u.rw_data.dma_info);
582 static const int element_size = sizeof(rw_data->dma_info[0]);
584 numsg = ((bevent->event.xSizeMinus1 + 1) - offset) / element_size;
585 if (numsg > VIOMAXBLOCKDMA)
586 numsg = VIOMAXBLOCKDMA;
588 *total_len = 0;
589 memset(sg, 0, sizeof(sg[0]) * VIOMAXBLOCKDMA);
591 for (i = 0; (i < numsg) && (rw_data->dma_info[i].len > 0); ++i) {
592 sg_dma_address(&sg[i]) = rw_data->dma_info[i].token;
593 sg_dma_len(&sg[i]) = rw_data->dma_info[i].len;
594 *total_len += rw_data->dma_info[i].len;
596 return i;
600 * Restart all queues, starting with the one _after_ the disk given,
601 * thus reducing the chance of starvation of higher numbered disks.
603 static void viodasd_restart_all_queues_starting_from(int first_index)
605 int i;
607 for (i = first_index + 1; i < MAX_DISKNO; ++i)
608 if (viodasd_devices[i].disk)
609 blk_run_queue(viodasd_devices[i].disk->queue);
610 for (i = 0; i <= first_index; ++i)
611 if (viodasd_devices[i].disk)
612 blk_run_queue(viodasd_devices[i].disk->queue);
616 * For read and write requests, decrement the number of outstanding requests,
617 * Free the DMA buffers we allocated.
619 static int viodasd_handle_read_write(struct vioblocklpevent *bevent)
621 int num_sg, num_sect, pci_direction, total_len;
622 struct request *req;
623 struct scatterlist sg[VIOMAXBLOCKDMA];
624 struct HvLpEvent *event = &bevent->event;
625 unsigned long irq_flags;
626 struct viodasd_device *d;
627 int error;
628 spinlock_t *qlock;
630 num_sg = block_event_to_scatterlist(bevent, sg, &total_len);
631 num_sect = total_len >> 9;
632 if (event->xSubtype == (viomajorsubtype_blockio | vioblockread))
633 pci_direction = DMA_FROM_DEVICE;
634 else
635 pci_direction = DMA_TO_DEVICE;
636 req = (struct request *)bevent->event.xCorrelationToken;
637 d = req->rq_disk->private_data;
639 dma_unmap_sg(d->dev, sg, num_sg, pci_direction);
642 * Since this is running in interrupt mode, we need to make sure
643 * we're not stepping on any global I/O operations
645 spin_lock_irqsave(&viodasd_spinlock, irq_flags);
646 num_req_outstanding--;
647 spin_unlock_irqrestore(&viodasd_spinlock, irq_flags);
649 error = event->xRc != HvLpEvent_Rc_Good;
650 if (error) {
651 const struct vio_error_entry *err;
652 err = vio_lookup_rc(viodasd_err_table, bevent->sub_result);
653 printk(VIOD_KERN_WARNING "read/write error %d:0x%04x (%s)\n",
654 event->xRc, bevent->sub_result, err->msg);
655 num_sect = req->hard_nr_sectors;
657 qlock = req->q->queue_lock;
658 spin_lock_irqsave(qlock, irq_flags);
659 viodasd_end_request(req, !error, num_sect);
660 spin_unlock_irqrestore(qlock, irq_flags);
662 /* Finally, try to get more requests off of this device's queue */
663 viodasd_restart_all_queues_starting_from(DEVICE_NO(d));
665 return 0;
668 /* This routine handles incoming block LP events */
669 static void handle_block_event(struct HvLpEvent *event)
671 struct vioblocklpevent *bevent = (struct vioblocklpevent *)event;
672 struct viodasd_waitevent *pwe;
674 if (event == NULL)
675 /* Notification that a partition went away! */
676 return;
677 /* First, we should NEVER get an int here...only acks */
678 if (event->xFlags.xFunction == HvLpEvent_Function_Int) {
679 printk(VIOD_KERN_WARNING
680 "Yikes! got an int in viodasd event handler!\n");
681 if (event->xFlags.xAckInd == HvLpEvent_AckInd_DoAck) {
682 event->xRc = HvLpEvent_Rc_InvalidSubtype;
683 HvCallEvent_ackLpEvent(event);
687 switch (event->xSubtype & VIOMINOR_SUBTYPE_MASK) {
688 case vioblockopen:
690 * Handle a response to an open request. We get all the
691 * disk information in the response, so update it. The
692 * correlation token contains a pointer to a waitevent
693 * structure that has a completion in it. update the
694 * return code in the waitevent structure and post the
695 * completion to wake up the guy who sent the request
697 pwe = (struct viodasd_waitevent *)event->xCorrelationToken;
698 pwe->rc = event->xRc;
699 pwe->sub_result = bevent->sub_result;
700 if (event->xRc == HvLpEvent_Rc_Good) {
701 const struct open_data *data = &bevent->u.open_data;
702 struct viodasd_device *device =
703 &viodasd_devices[bevent->disk];
704 device->read_only =
705 bevent->flags & vioblockflags_ro;
706 device->size = data->disk_size;
707 device->cylinders = data->cylinders;
708 device->tracks = data->tracks;
709 device->sectors = data->sectors;
710 device->bytes_per_sector = data->bytes_per_sector;
711 pwe->max_disk = data->max_disk;
713 complete(&pwe->com);
714 break;
715 case vioblockclose:
716 break;
717 case vioblockread:
718 case vioblockwrite:
719 viodasd_handle_read_write(bevent);
720 break;
722 default:
723 printk(VIOD_KERN_WARNING "invalid subtype!");
724 if (event->xFlags.xAckInd == HvLpEvent_AckInd_DoAck) {
725 event->xRc = HvLpEvent_Rc_InvalidSubtype;
726 HvCallEvent_ackLpEvent(event);
732 * Get the driver to reprobe for more disks.
734 static ssize_t probe_disks(struct device_driver *drv, const char *buf,
735 size_t count)
737 struct viodasd_device *d;
739 for (d = viodasd_devices; d < &viodasd_devices[MAX_DISKNO]; d++) {
740 if (d->disk == NULL)
741 probe_disk(d);
743 return count;
745 static DRIVER_ATTR(probe, S_IWUSR, NULL, probe_disks);
747 static int viodasd_probe(struct vio_dev *vdev, const struct vio_device_id *id)
749 struct viodasd_device *d = &viodasd_devices[vdev->unit_address];
751 d->dev = &vdev->dev;
752 probe_disk(d);
753 if (d->disk == NULL)
754 return -ENODEV;
755 return 0;
758 static int viodasd_remove(struct vio_dev *vdev)
760 struct viodasd_device *d;
762 d = &viodasd_devices[vdev->unit_address];
763 if (d->disk) {
764 del_gendisk(d->disk);
765 blk_cleanup_queue(d->disk->queue);
766 put_disk(d->disk);
767 d->disk = NULL;
769 d->dev = NULL;
770 return 0;
774 * viodasd_device_table: Used by vio.c to match devices that we
775 * support.
777 static struct vio_device_id viodasd_device_table[] __devinitdata = {
778 { "viodasd", "" },
779 { 0, }
782 MODULE_DEVICE_TABLE(vio, viodasd_device_table);
783 static struct vio_driver viodasd_driver = {
784 .name = "viodasd",
785 .id_table = viodasd_device_table,
786 .probe = viodasd_probe,
787 .remove = viodasd_remove
791 * Initialize the whole device driver. Handle module and non-module
792 * versions
794 static int __init viodasd_init(void)
796 int rc;
798 /* Try to open to our host lp */
799 if (viopath_hostLp == HvLpIndexInvalid)
800 vio_set_hostlp();
802 if (viopath_hostLp == HvLpIndexInvalid) {
803 printk(VIOD_KERN_WARNING "invalid hosting partition\n");
804 return -EIO;
807 printk(VIOD_KERN_INFO "vers " VIOD_VERS ", hosting partition %d\n",
808 viopath_hostLp);
810 /* register the block device */
811 if (register_blkdev(VIODASD_MAJOR, VIOD_GENHD_NAME)) {
812 printk(VIOD_KERN_WARNING
813 "Unable to get major number %d for %s\n",
814 VIODASD_MAJOR, VIOD_GENHD_NAME);
815 return -EIO;
817 /* Actually open the path to the hosting partition */
818 if (viopath_open(viopath_hostLp, viomajorsubtype_blockio,
819 VIOMAXREQ + 2)) {
820 printk(VIOD_KERN_WARNING
821 "error opening path to host partition %d\n",
822 viopath_hostLp);
823 unregister_blkdev(VIODASD_MAJOR, VIOD_GENHD_NAME);
824 return -EIO;
827 /* Initialize our request handler */
828 vio_setHandler(viomajorsubtype_blockio, handle_block_event);
830 rc = vio_register_driver(&viodasd_driver);
831 if (rc == 0)
832 driver_create_file(&viodasd_driver.driver, &driver_attr_probe);
833 return rc;
835 module_init(viodasd_init);
837 void viodasd_exit(void)
839 driver_remove_file(&viodasd_driver.driver, &driver_attr_probe);
840 vio_unregister_driver(&viodasd_driver);
841 vio_clearHandler(viomajorsubtype_blockio);
842 unregister_blkdev(VIODASD_MAJOR, VIOD_GENHD_NAME);
843 viopath_close(viopath_hostLp, viomajorsubtype_blockio, VIOMAXREQ + 2);
846 module_exit(viodasd_exit);