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>
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>
48 #include <asm/iseries/hv_types.h>
49 #include <asm/iseries/hv_lp_event.h>
50 #include <asm/iseries/hv_lp_config.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"
63 #define VIOD_VERS "1.64"
65 #define VIOD_KERN_WARNING KERN_WARNING "viod: "
66 #define VIOD_KERN_INFO KERN_INFO "viod: "
70 MAX_DISKNO = HVMAXARCHITECTEDVIRTUALDISKS,
71 MAX_DISK_NAME = sizeof(((struct gendisk *)0)->disk_name)
74 static DEFINE_SPINLOCK(viodasd_spinlock);
77 #define VIOMAXBLOCKDMA 12
79 #define DEVICE_NO(cell) ((struct viodasd_device *)(cell) - &viodasd_devices[0])
96 } dma_info[VIOMAXBLOCKDMA];
99 struct vioblocklpevent {
100 struct HvLpEvent event;
107 struct open_data open_data;
108 struct rw_data rw_data;
113 #define vioblockflags_ro 0x0001
115 enum vioblocksubtype {
116 vioblockopen = 0x0001,
117 vioblockclose = 0x0002,
118 vioblockread = 0x0003,
119 vioblockwrite = 0x0004,
120 vioblockflush = 0x0005,
121 vioblockcheck = 0x0007
124 struct viodasd_waitevent {
125 struct completion com;
128 int max_disk; /* open */
131 static const struct vio_error_entry viodasd_err_table[] = {
132 { 0x0201, EINVAL, "Invalid Range" },
133 { 0x0202, EINVAL, "Invalid Token" },
134 { 0x0203, EIO, "DMA Error" },
135 { 0x0204, EIO, "Use Error" },
136 { 0x0205, EIO, "Release Error" },
137 { 0x0206, EINVAL, "Invalid Disk" },
138 { 0x0207, EBUSY, "Cant Lock" },
139 { 0x0208, EIO, "Already Locked" },
140 { 0x0209, EIO, "Already Unlocked" },
141 { 0x020A, EIO, "Invalid Arg" },
142 { 0x020B, EIO, "Bad IFS File" },
143 { 0x020C, EROFS, "Read Only Device" },
144 { 0x02FF, EIO, "Internal Error" },
149 * Figure out the biggest I/O request (in sectors) we can accept
151 #define VIODASD_MAXSECTORS (4096 / 512 * VIOMAXBLOCKDMA)
154 * Number of disk I/O requests we've sent to OS/400
156 static int num_req_outstanding;
159 * This is our internal structure for keeping track of disk devices
161 struct viodasd_device {
165 u16 bytes_per_sector;
169 struct gendisk *disk;
171 } viodasd_devices[MAX_DISKNO];
174 * External open entry point.
176 static int viodasd_open(struct inode *ino, struct file *fil)
178 struct viodasd_device *d = ino->i_bdev->bd_disk->private_data;
180 struct viodasd_waitevent we;
184 if ((fil != NULL) && (fil->f_mode & FMODE_WRITE))
186 flags = vioblockflags_ro;
189 init_completion(&we.com);
191 /* Send the open event to OS/400 */
192 hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
193 HvLpEvent_Type_VirtualIo,
194 viomajorsubtype_blockio | vioblockopen,
195 HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck,
196 viopath_sourceinst(viopath_hostLp),
197 viopath_targetinst(viopath_hostLp),
198 (u64)(unsigned long)&we, VIOVERSION << 16,
199 ((u64)DEVICE_NO(d) << 48) | ((u64)flags << 32),
202 printk(VIOD_KERN_WARNING "HV open failed %d\n", (int)hvrc);
206 wait_for_completion(&we.com);
208 /* Check the return code */
210 const struct vio_error_entry *err =
211 vio_lookup_rc(viodasd_err_table, we.sub_result);
213 printk(VIOD_KERN_WARNING
214 "bad rc opening disk: %d:0x%04x (%s)\n",
215 (int)we.rc, we.sub_result, err->msg);
223 * External release entry point.
225 static int viodasd_release(struct inode *ino, struct file *fil)
227 struct viodasd_device *d = ino->i_bdev->bd_disk->private_data;
230 /* Send the event to OS/400. We DON'T expect a response */
231 hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
232 HvLpEvent_Type_VirtualIo,
233 viomajorsubtype_blockio | vioblockclose,
234 HvLpEvent_AckInd_NoAck, HvLpEvent_AckType_ImmediateAck,
235 viopath_sourceinst(viopath_hostLp),
236 viopath_targetinst(viopath_hostLp),
238 ((u64)DEVICE_NO(d) << 48) /* | ((u64)flags << 32) */,
241 printk(VIOD_KERN_WARNING "HV close call failed %d\n",
247 /* External ioctl entry point.
249 static int viodasd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
251 struct gendisk *disk = bdev->bd_disk;
252 struct viodasd_device *d = disk->private_data;
254 geo->sectors = d->sectors ? d->sectors : 0;
255 geo->heads = d->tracks ? d->tracks : 64;
256 geo->cylinders = d->cylinders ? d->cylinders :
257 get_capacity(disk) / (geo->cylinders * geo->heads);
263 * Our file operations table
265 static struct block_device_operations viodasd_fops = {
266 .owner = THIS_MODULE,
267 .open = viodasd_open,
268 .release = viodasd_release,
269 .getgeo = viodasd_getgeo,
275 static void viodasd_end_request(struct request *req, int uptodate,
278 if (end_that_request_first(req, uptodate, num_sectors))
280 add_disk_randomness(req->rq_disk);
281 end_that_request_last(req, uptodate);
285 * Send an actual I/O request to OS/400
287 static int send_request(struct request *req)
294 struct vioblocklpevent *bevent;
295 struct HvLpEvent *hev;
296 struct scatterlist sg[VIOMAXBLOCKDMA];
299 struct viodasd_device *d;
302 start = (u64)req->sector << 9;
304 if (rq_data_dir(req) == READ) {
305 direction = DMA_FROM_DEVICE;
306 viocmd = viomajorsubtype_blockio | vioblockread;
309 direction = DMA_TO_DEVICE;
310 viocmd = viomajorsubtype_blockio | vioblockwrite;
314 d = req->rq_disk->private_data;
316 /* Now build the scatter-gather list */
317 nsg = blk_rq_map_sg(req->q, req, sg);
318 nsg = dma_map_sg(d->dev, sg, nsg, direction);
320 spin_lock_irqsave(&viodasd_spinlock, flags);
321 num_req_outstanding++;
323 /* This optimization handles a single DMA block */
325 hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
326 HvLpEvent_Type_VirtualIo, viocmd,
327 HvLpEvent_AckInd_DoAck,
328 HvLpEvent_AckType_ImmediateAck,
329 viopath_sourceinst(viopath_hostLp),
330 viopath_targetinst(viopath_hostLp),
331 (u64)(unsigned long)req, VIOVERSION << 16,
332 ((u64)DEVICE_NO(d) << 48), start,
333 ((u64)sg_dma_address(&sg[0])) << 32,
336 bevent = (struct vioblocklpevent *)
337 vio_get_event_buffer(viomajorsubtype_blockio);
338 if (bevent == NULL) {
339 printk(VIOD_KERN_WARNING
340 "error allocating disk event buffer\n");
345 * Now build up the actual request. Note that we store
346 * the pointer to the request in the correlation
347 * token so we can match the response up later
349 memset(bevent, 0, sizeof(struct vioblocklpevent));
350 hev = &bevent->event;
351 hev->flags = HV_LP_EVENT_VALID | HV_LP_EVENT_DO_ACK |
353 hev->xType = HvLpEvent_Type_VirtualIo;
354 hev->xSubtype = viocmd;
355 hev->xSourceLp = HvLpConfig_getLpIndex();
356 hev->xTargetLp = viopath_hostLp;
358 offsetof(struct vioblocklpevent, u.rw_data.dma_info) +
359 (sizeof(bevent->u.rw_data.dma_info[0]) * nsg) - 1;
360 hev->xSourceInstanceId = viopath_sourceinst(viopath_hostLp);
361 hev->xTargetInstanceId = viopath_targetinst(viopath_hostLp);
362 hev->xCorrelationToken = (u64)req;
363 bevent->version = VIOVERSION;
364 bevent->disk = DEVICE_NO(d);
365 bevent->u.rw_data.offset = start;
368 * Copy just the dma information from the sg list
371 for (sgindex = 0; sgindex < nsg; sgindex++) {
372 bevent->u.rw_data.dma_info[sgindex].token =
373 sg_dma_address(&sg[sgindex]);
374 bevent->u.rw_data.dma_info[sgindex].len =
375 sg_dma_len(&sg[sgindex]);
378 /* Send the request */
379 hvrc = HvCallEvent_signalLpEvent(&bevent->event);
380 vio_free_event_buffer(viomajorsubtype_blockio, bevent);
383 if (hvrc != HvLpEvent_Rc_Good) {
384 printk(VIOD_KERN_WARNING
385 "error sending disk event to OS/400 (rc %d)\n",
389 spin_unlock_irqrestore(&viodasd_spinlock, flags);
393 num_req_outstanding--;
394 spin_unlock_irqrestore(&viodasd_spinlock, flags);
395 dma_unmap_sg(d->dev, sg, nsg, direction);
400 * This is the external request processing routine
402 static void do_viodasd_request(request_queue_t *q)
407 * If we already have the maximum number of requests
408 * outstanding to OS/400 just bail out. We'll come
411 while (num_req_outstanding < VIOMAXREQ) {
412 req = elv_next_request(q);
415 /* dequeue the current request from the queue */
416 blkdev_dequeue_request(req);
417 /* check that request contains a valid command */
418 if (!blk_fs_request(req)) {
419 viodasd_end_request(req, 0, req->hard_nr_sectors);
422 /* Try sending the request */
423 if (send_request(req) != 0)
424 viodasd_end_request(req, 0, req->hard_nr_sectors);
429 * Probe a single disk and fill in the viodasd_device structure
432 static void probe_disk(struct viodasd_device *d)
435 struct viodasd_waitevent we;
436 int dev_no = DEVICE_NO(d);
438 struct request_queue *q;
442 init_completion(&we.com);
444 /* Send the open event to OS/400 */
445 hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
446 HvLpEvent_Type_VirtualIo,
447 viomajorsubtype_blockio | vioblockopen,
448 HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck,
449 viopath_sourceinst(viopath_hostLp),
450 viopath_targetinst(viopath_hostLp),
451 (u64)(unsigned long)&we, VIOVERSION << 16,
452 ((u64)dev_no << 48) | ((u64)flags<< 32),
455 printk(VIOD_KERN_WARNING "bad rc on HV open %d\n", (int)hvrc);
459 wait_for_completion(&we.com);
464 /* try again with read only flag set */
465 flags = vioblockflags_ro;
468 if (we.max_disk > (MAX_DISKNO - 1)) {
473 printk(VIOD_KERN_INFO
474 "Only examining the first %d "
475 "of %d disks connected\n",
476 MAX_DISKNO, we.max_disk + 1);
480 /* Send the close event to OS/400. We DON'T expect a response */
481 hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
482 HvLpEvent_Type_VirtualIo,
483 viomajorsubtype_blockio | vioblockclose,
484 HvLpEvent_AckInd_NoAck, HvLpEvent_AckType_ImmediateAck,
485 viopath_sourceinst(viopath_hostLp),
486 viopath_targetinst(viopath_hostLp),
488 ((u64)dev_no << 48) | ((u64)flags << 32),
491 printk(VIOD_KERN_WARNING
492 "bad rc sending event to OS/400 %d\n", (int)hvrc);
495 /* create the request queue for the disk */
496 spin_lock_init(&d->q_lock);
497 q = blk_init_queue(do_viodasd_request, &d->q_lock);
499 printk(VIOD_KERN_WARNING "cannot allocate queue for disk %d\n",
503 g = alloc_disk(1 << PARTITION_SHIFT);
505 printk(VIOD_KERN_WARNING
506 "cannot allocate disk structure for disk %d\n",
508 blk_cleanup_queue(q);
513 blk_queue_max_hw_segments(q, VIOMAXBLOCKDMA);
514 blk_queue_max_phys_segments(q, VIOMAXBLOCKDMA);
515 blk_queue_max_sectors(q, VIODASD_MAXSECTORS);
516 g->major = VIODASD_MAJOR;
517 g->first_minor = dev_no << PARTITION_SHIFT;
519 snprintf(g->disk_name, sizeof(g->disk_name),
520 VIOD_GENHD_NAME "%c%c",
521 'a' + (dev_no / 26) - 1, 'a' + (dev_no % 26));
523 snprintf(g->disk_name, sizeof(g->disk_name),
524 VIOD_GENHD_NAME "%c", 'a' + (dev_no % 26));
525 g->fops = &viodasd_fops;
528 g->driverfs_dev = d->dev;
529 set_capacity(g, d->size >> 9);
531 printk(VIOD_KERN_INFO "disk %d: %lu sectors (%lu MB) "
532 "CHS=%d/%d/%d sector size %d%s\n",
533 dev_no, (unsigned long)(d->size >> 9),
534 (unsigned long)(d->size >> 20),
535 (int)d->cylinders, (int)d->tracks,
536 (int)d->sectors, (int)d->bytes_per_sector,
537 d->read_only ? " (RO)" : "");
539 /* register us in the global list */
543 /* returns the total number of scatterlist elements converted */
544 static int block_event_to_scatterlist(const struct vioblocklpevent *bevent,
545 struct scatterlist *sg, int *total_len)
548 const struct rw_data *rw_data = &bevent->u.rw_data;
549 static const int offset =
550 offsetof(struct vioblocklpevent, u.rw_data.dma_info);
551 static const int element_size = sizeof(rw_data->dma_info[0]);
553 numsg = ((bevent->event.xSizeMinus1 + 1) - offset) / element_size;
554 if (numsg > VIOMAXBLOCKDMA)
555 numsg = VIOMAXBLOCKDMA;
558 memset(sg, 0, sizeof(sg[0]) * VIOMAXBLOCKDMA);
560 for (i = 0; (i < numsg) && (rw_data->dma_info[i].len > 0); ++i) {
561 sg_dma_address(&sg[i]) = rw_data->dma_info[i].token;
562 sg_dma_len(&sg[i]) = rw_data->dma_info[i].len;
563 *total_len += rw_data->dma_info[i].len;
569 * Restart all queues, starting with the one _after_ the disk given,
570 * thus reducing the chance of starvation of higher numbered disks.
572 static void viodasd_restart_all_queues_starting_from(int first_index)
576 for (i = first_index + 1; i < MAX_DISKNO; ++i)
577 if (viodasd_devices[i].disk)
578 blk_run_queue(viodasd_devices[i].disk->queue);
579 for (i = 0; i <= first_index; ++i)
580 if (viodasd_devices[i].disk)
581 blk_run_queue(viodasd_devices[i].disk->queue);
585 * For read and write requests, decrement the number of outstanding requests,
586 * Free the DMA buffers we allocated.
588 static int viodasd_handle_read_write(struct vioblocklpevent *bevent)
590 int num_sg, num_sect, pci_direction, total_len;
592 struct scatterlist sg[VIOMAXBLOCKDMA];
593 struct HvLpEvent *event = &bevent->event;
594 unsigned long irq_flags;
595 struct viodasd_device *d;
599 num_sg = block_event_to_scatterlist(bevent, sg, &total_len);
600 num_sect = total_len >> 9;
601 if (event->xSubtype == (viomajorsubtype_blockio | vioblockread))
602 pci_direction = DMA_FROM_DEVICE;
604 pci_direction = DMA_TO_DEVICE;
605 req = (struct request *)bevent->event.xCorrelationToken;
606 d = req->rq_disk->private_data;
608 dma_unmap_sg(d->dev, sg, num_sg, pci_direction);
611 * Since this is running in interrupt mode, we need to make sure
612 * we're not stepping on any global I/O operations
614 spin_lock_irqsave(&viodasd_spinlock, irq_flags);
615 num_req_outstanding--;
616 spin_unlock_irqrestore(&viodasd_spinlock, irq_flags);
618 error = event->xRc != HvLpEvent_Rc_Good;
620 const struct vio_error_entry *err;
621 err = vio_lookup_rc(viodasd_err_table, bevent->sub_result);
622 printk(VIOD_KERN_WARNING "read/write error %d:0x%04x (%s)\n",
623 event->xRc, bevent->sub_result, err->msg);
624 num_sect = req->hard_nr_sectors;
626 qlock = req->q->queue_lock;
627 spin_lock_irqsave(qlock, irq_flags);
628 viodasd_end_request(req, !error, num_sect);
629 spin_unlock_irqrestore(qlock, irq_flags);
631 /* Finally, try to get more requests off of this device's queue */
632 viodasd_restart_all_queues_starting_from(DEVICE_NO(d));
637 /* This routine handles incoming block LP events */
638 static void handle_block_event(struct HvLpEvent *event)
640 struct vioblocklpevent *bevent = (struct vioblocklpevent *)event;
641 struct viodasd_waitevent *pwe;
644 /* Notification that a partition went away! */
646 /* First, we should NEVER get an int here...only acks */
647 if (hvlpevent_is_int(event)) {
648 printk(VIOD_KERN_WARNING
649 "Yikes! got an int in viodasd event handler!\n");
650 if (hvlpevent_need_ack(event)) {
651 event->xRc = HvLpEvent_Rc_InvalidSubtype;
652 HvCallEvent_ackLpEvent(event);
656 switch (event->xSubtype & VIOMINOR_SUBTYPE_MASK) {
659 * Handle a response to an open request. We get all the
660 * disk information in the response, so update it. The
661 * correlation token contains a pointer to a waitevent
662 * structure that has a completion in it. update the
663 * return code in the waitevent structure and post the
664 * completion to wake up the guy who sent the request
666 pwe = (struct viodasd_waitevent *)event->xCorrelationToken;
667 pwe->rc = event->xRc;
668 pwe->sub_result = bevent->sub_result;
669 if (event->xRc == HvLpEvent_Rc_Good) {
670 const struct open_data *data = &bevent->u.open_data;
671 struct viodasd_device *device =
672 &viodasd_devices[bevent->disk];
674 bevent->flags & vioblockflags_ro;
675 device->size = data->disk_size;
676 device->cylinders = data->cylinders;
677 device->tracks = data->tracks;
678 device->sectors = data->sectors;
679 device->bytes_per_sector = data->bytes_per_sector;
680 pwe->max_disk = data->max_disk;
688 viodasd_handle_read_write(bevent);
692 printk(VIOD_KERN_WARNING "invalid subtype!");
693 if (hvlpevent_need_ack(event)) {
694 event->xRc = HvLpEvent_Rc_InvalidSubtype;
695 HvCallEvent_ackLpEvent(event);
701 * Get the driver to reprobe for more disks.
703 static ssize_t probe_disks(struct device_driver *drv, const char *buf,
706 struct viodasd_device *d;
708 for (d = viodasd_devices; d < &viodasd_devices[MAX_DISKNO]; d++) {
714 static DRIVER_ATTR(probe, S_IWUSR, NULL, probe_disks);
716 static int viodasd_probe(struct vio_dev *vdev, const struct vio_device_id *id)
718 struct viodasd_device *d = &viodasd_devices[vdev->unit_address];
727 static int viodasd_remove(struct vio_dev *vdev)
729 struct viodasd_device *d;
731 d = &viodasd_devices[vdev->unit_address];
733 del_gendisk(d->disk);
734 blk_cleanup_queue(d->disk->queue);
743 * viodasd_device_table: Used by vio.c to match devices that we
746 static struct vio_device_id viodasd_device_table[] __devinitdata = {
747 { "block", "IBM,iSeries-viodasd" },
750 MODULE_DEVICE_TABLE(vio, viodasd_device_table);
752 static struct vio_driver viodasd_driver = {
753 .id_table = viodasd_device_table,
754 .probe = viodasd_probe,
755 .remove = viodasd_remove,
758 .owner = THIS_MODULE,
763 * Initialize the whole device driver. Handle module and non-module
766 static int __init viodasd_init(void)
770 /* Try to open to our host lp */
771 if (viopath_hostLp == HvLpIndexInvalid)
774 if (viopath_hostLp == HvLpIndexInvalid) {
775 printk(VIOD_KERN_WARNING "invalid hosting partition\n");
779 printk(VIOD_KERN_INFO "vers " VIOD_VERS ", hosting partition %d\n",
782 /* register the block device */
783 if (register_blkdev(VIODASD_MAJOR, VIOD_GENHD_NAME)) {
784 printk(VIOD_KERN_WARNING
785 "Unable to get major number %d for %s\n",
786 VIODASD_MAJOR, VIOD_GENHD_NAME);
789 /* Actually open the path to the hosting partition */
790 if (viopath_open(viopath_hostLp, viomajorsubtype_blockio,
792 printk(VIOD_KERN_WARNING
793 "error opening path to host partition %d\n",
795 unregister_blkdev(VIODASD_MAJOR, VIOD_GENHD_NAME);
799 /* Initialize our request handler */
800 vio_setHandler(viomajorsubtype_blockio, handle_block_event);
802 rc = vio_register_driver(&viodasd_driver);
804 driver_create_file(&viodasd_driver.driver, &driver_attr_probe);
807 module_init(viodasd_init);
809 void viodasd_exit(void)
811 driver_remove_file(&viodasd_driver.driver, &driver_attr_probe);
812 vio_unregister_driver(&viodasd_driver);
813 vio_clearHandler(viomajorsubtype_blockio);
814 unregister_blkdev(VIODASD_MAJOR, VIOD_GENHD_NAME);
815 viopath_close(viopath_hostLp, viomajorsubtype_blockio, VIOMAXREQ + 2);
818 module_exit(viodasd_exit);