1 /* sunvdc.c: Sun LDOM Virtual Disk Client.
3 * Copyright (C) 2007, 2008 David S. Miller <davem@davemloft.net>
6 #include <linux/module.h>
7 #include <linux/kernel.h>
8 #include <linux/types.h>
9 #include <linux/blkdev.h>
10 #include <linux/hdreg.h>
11 #include <linux/genhd.h>
12 #include <linux/cdrom.h>
13 #include <linux/slab.h>
14 #include <linux/spinlock.h>
15 #include <linux/completion.h>
16 #include <linux/delay.h>
17 #include <linux/init.h>
18 #include <linux/list.h>
19 #include <linux/scatterlist.h>
24 #define DRV_MODULE_NAME "sunvdc"
25 #define PFX DRV_MODULE_NAME ": "
26 #define DRV_MODULE_VERSION "1.1"
27 #define DRV_MODULE_RELDATE "February 13, 2013"
29 static char version[] =
30 DRV_MODULE_NAME ".c:v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")\n";
31 MODULE_AUTHOR("David S. Miller (davem@davemloft.net)");
32 MODULE_DESCRIPTION("Sun LDOM virtual disk client driver");
33 MODULE_LICENSE("GPL");
34 MODULE_VERSION(DRV_MODULE_VERSION);
36 #define VDC_TX_RING_SIZE 256
38 #define WAITING_FOR_LINK_UP 0x01
39 #define WAITING_FOR_TX_SPACE 0x02
40 #define WAITING_FOR_GEN_CMD 0x04
41 #define WAITING_FOR_ANY -1
43 struct vdc_req_entry {
48 struct vio_driver_state vio;
52 struct vdc_completion *cmp;
56 struct vdc_req_entry rq_arr[VDC_TX_RING_SIZE];
58 unsigned long ring_cookies;
63 /* The server fills these in for us in the disk attribute
73 struct vio_disk_geom geom;
74 struct vio_disk_vtoc label;
77 static inline struct vdc_port *to_vdc_port(struct vio_driver_state *vio)
79 return container_of(vio, struct vdc_port, vio);
82 /* Ordered from largest major to lowest */
83 static struct vio_version vdc_versions[] = {
84 { .major = 1, .minor = 1 },
85 { .major = 1, .minor = 0 },
88 static inline int vdc_version_supported(struct vdc_port *port,
91 return port->vio.ver.major == major && port->vio.ver.minor >= minor;
94 #define VDCBLK_NAME "vdisk"
96 #define PARTITION_SHIFT 3
98 static inline u32 vdc_tx_dring_avail(struct vio_dring_state *dr)
100 return vio_dring_avail(dr, VDC_TX_RING_SIZE);
103 static int vdc_getgeo(struct block_device *bdev, struct hd_geometry *geo)
105 struct gendisk *disk = bdev->bd_disk;
106 struct vdc_port *port = disk->private_data;
108 geo->heads = (u8) port->geom.num_hd;
109 geo->sectors = (u8) port->geom.num_sec;
110 geo->cylinders = port->geom.num_cyl;
115 /* Add ioctl/CDROM_GET_CAPABILITY to support cdrom_id in udev
116 * when vdisk_mtype is VD_MEDIA_TYPE_CD or VD_MEDIA_TYPE_DVD.
117 * Needed to be able to install inside an ldom from an iso image.
119 static int vdc_ioctl(struct block_device *bdev, fmode_t mode,
120 unsigned command, unsigned long argument)
123 struct gendisk *disk;
126 case CDROMMULTISESSION:
127 pr_debug(PFX "Multisession CDs not supported\n");
128 for (i = 0; i < sizeof(struct cdrom_multisession); i++)
129 if (put_user(0, (char __user *)(argument + i)))
133 case CDROM_GET_CAPABILITY:
134 disk = bdev->bd_disk;
136 if (bdev->bd_disk && (disk->flags & GENHD_FL_CD))
141 pr_debug(PFX "ioctl %08x not supported\n", command);
146 static const struct block_device_operations vdc_fops = {
147 .owner = THIS_MODULE,
148 .getgeo = vdc_getgeo,
152 static void vdc_finish(struct vio_driver_state *vio, int err, int waiting_for)
155 (waiting_for == -1 ||
156 vio->cmp->waiting_for == waiting_for)) {
158 complete(&vio->cmp->com);
163 static void vdc_handshake_complete(struct vio_driver_state *vio)
165 vdc_finish(vio, 0, WAITING_FOR_LINK_UP);
168 static int vdc_handle_unknown(struct vdc_port *port, void *arg)
170 struct vio_msg_tag *pkt = arg;
172 printk(KERN_ERR PFX "Received unknown msg [%02x:%02x:%04x:%08x]\n",
173 pkt->type, pkt->stype, pkt->stype_env, pkt->sid);
174 printk(KERN_ERR PFX "Resetting connection.\n");
176 ldc_disconnect(port->vio.lp);
181 static int vdc_send_attr(struct vio_driver_state *vio)
183 struct vdc_port *port = to_vdc_port(vio);
184 struct vio_disk_attr_info pkt;
186 memset(&pkt, 0, sizeof(pkt));
188 pkt.tag.type = VIO_TYPE_CTRL;
189 pkt.tag.stype = VIO_SUBTYPE_INFO;
190 pkt.tag.stype_env = VIO_ATTR_INFO;
191 pkt.tag.sid = vio_send_sid(vio);
193 pkt.xfer_mode = VIO_DRING_MODE;
194 pkt.vdisk_block_size = port->vdisk_block_size;
195 pkt.max_xfer_size = port->max_xfer_size;
197 viodbg(HS, "SEND ATTR xfer_mode[0x%x] blksz[%u] max_xfer[%llu]\n",
198 pkt.xfer_mode, pkt.vdisk_block_size, pkt.max_xfer_size);
200 return vio_ldc_send(&port->vio, &pkt, sizeof(pkt));
203 static int vdc_handle_attr(struct vio_driver_state *vio, void *arg)
205 struct vdc_port *port = to_vdc_port(vio);
206 struct vio_disk_attr_info *pkt = arg;
208 viodbg(HS, "GOT ATTR stype[0x%x] ops[%llx] disk_size[%llu] disk_type[%x] "
209 "mtype[0x%x] xfer_mode[0x%x] blksz[%u] max_xfer[%llu]\n",
210 pkt->tag.stype, pkt->operations,
211 pkt->vdisk_size, pkt->vdisk_type, pkt->vdisk_mtype,
212 pkt->xfer_mode, pkt->vdisk_block_size,
215 if (pkt->tag.stype == VIO_SUBTYPE_ACK) {
216 switch (pkt->vdisk_type) {
217 case VD_DISK_TYPE_DISK:
218 case VD_DISK_TYPE_SLICE:
222 printk(KERN_ERR PFX "%s: Bogus vdisk_type 0x%x\n",
223 vio->name, pkt->vdisk_type);
227 if (pkt->vdisk_block_size > port->vdisk_block_size) {
228 printk(KERN_ERR PFX "%s: BLOCK size increased "
231 port->vdisk_block_size, pkt->vdisk_block_size);
235 port->operations = pkt->operations;
236 port->vdisk_type = pkt->vdisk_type;
237 if (vdc_version_supported(port, 1, 1)) {
238 port->vdisk_size = pkt->vdisk_size;
239 port->vdisk_mtype = pkt->vdisk_mtype;
241 if (pkt->max_xfer_size < port->max_xfer_size)
242 port->max_xfer_size = pkt->max_xfer_size;
243 port->vdisk_block_size = pkt->vdisk_block_size;
246 printk(KERN_ERR PFX "%s: Attribute NACK\n", vio->name);
252 static void vdc_end_special(struct vdc_port *port, struct vio_disk_desc *desc)
254 int err = desc->status;
256 vdc_finish(&port->vio, -err, WAITING_FOR_GEN_CMD);
259 static void vdc_end_one(struct vdc_port *port, struct vio_dring_state *dr,
262 struct vio_disk_desc *desc = vio_dring_entry(dr, index);
263 struct vdc_req_entry *rqe = &port->rq_arr[index];
266 if (unlikely(desc->hdr.state != VIO_DESC_DONE))
269 ldc_unmap(port->vio.lp, desc->cookies, desc->ncookies);
270 desc->hdr.state = VIO_DESC_FREE;
271 dr->cons = (index + 1) & (VDC_TX_RING_SIZE - 1);
275 vdc_end_special(port, desc);
281 __blk_end_request(req, (desc->status ? -EIO : 0), desc->size);
283 if (blk_queue_stopped(port->disk->queue))
284 blk_start_queue(port->disk->queue);
287 static int vdc_ack(struct vdc_port *port, void *msgbuf)
289 struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING];
290 struct vio_dring_data *pkt = msgbuf;
292 if (unlikely(pkt->dring_ident != dr->ident ||
293 pkt->start_idx != pkt->end_idx ||
294 pkt->start_idx >= VDC_TX_RING_SIZE))
297 vdc_end_one(port, dr, pkt->start_idx);
302 static int vdc_nack(struct vdc_port *port, void *msgbuf)
304 /* XXX Implement me XXX */
308 static void vdc_event(void *arg, int event)
310 struct vdc_port *port = arg;
311 struct vio_driver_state *vio = &port->vio;
315 spin_lock_irqsave(&vio->lock, flags);
317 if (unlikely(event == LDC_EVENT_RESET ||
318 event == LDC_EVENT_UP)) {
319 vio_link_state_change(vio, event);
320 spin_unlock_irqrestore(&vio->lock, flags);
324 if (unlikely(event != LDC_EVENT_DATA_READY)) {
325 printk(KERN_WARNING PFX "Unexpected LDC event %d\n", event);
326 spin_unlock_irqrestore(&vio->lock, flags);
333 struct vio_msg_tag tag;
337 err = ldc_read(vio->lp, &msgbuf, sizeof(msgbuf));
338 if (unlikely(err < 0)) {
339 if (err == -ECONNRESET)
345 viodbg(DATA, "TAG [%02x:%02x:%04x:%08x]\n",
348 msgbuf.tag.stype_env,
350 err = vio_validate_sid(vio, &msgbuf.tag);
354 if (likely(msgbuf.tag.type == VIO_TYPE_DATA)) {
355 if (msgbuf.tag.stype == VIO_SUBTYPE_ACK)
356 err = vdc_ack(port, &msgbuf);
357 else if (msgbuf.tag.stype == VIO_SUBTYPE_NACK)
358 err = vdc_nack(port, &msgbuf);
360 err = vdc_handle_unknown(port, &msgbuf);
361 } else if (msgbuf.tag.type == VIO_TYPE_CTRL) {
362 err = vio_control_pkt_engine(vio, &msgbuf);
364 err = vdc_handle_unknown(port, &msgbuf);
370 vdc_finish(&port->vio, err, WAITING_FOR_ANY);
371 spin_unlock_irqrestore(&vio->lock, flags);
374 static int __vdc_tx_trigger(struct vdc_port *port)
376 struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING];
377 struct vio_dring_data hdr = {
379 .type = VIO_TYPE_DATA,
380 .stype = VIO_SUBTYPE_INFO,
381 .stype_env = VIO_DRING_DATA,
382 .sid = vio_send_sid(&port->vio),
384 .dring_ident = dr->ident,
385 .start_idx = dr->prod,
390 hdr.seq = dr->snd_nxt;
393 err = vio_ldc_send(&port->vio, &hdr, sizeof(hdr));
399 if ((delay <<= 1) > 128)
401 } while (err == -EAGAIN);
406 static int __send_request(struct request *req)
408 struct vdc_port *port = req->rq_disk->private_data;
409 struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING];
410 struct scatterlist sg[port->ring_cookies];
411 struct vdc_req_entry *rqe;
412 struct vio_disk_desc *desc;
413 unsigned int map_perm;
418 map_perm = LDC_MAP_SHADOW | LDC_MAP_DIRECT | LDC_MAP_IO;
420 if (rq_data_dir(req) == READ) {
421 map_perm |= LDC_MAP_W;
424 map_perm |= LDC_MAP_R;
428 sg_init_table(sg, port->ring_cookies);
429 nsg = blk_rq_map_sg(req->q, req, sg);
432 for (i = 0; i < nsg; i++)
435 if (unlikely(vdc_tx_dring_avail(dr) < 1)) {
436 blk_stop_queue(port->disk->queue);
441 desc = vio_dring_cur(dr);
443 err = ldc_map_sg(port->vio.lp, sg, nsg,
444 desc->cookies, port->ring_cookies,
447 printk(KERN_ERR PFX "ldc_map_sg() failure, err=%d.\n", err);
451 rqe = &port->rq_arr[dr->prod];
454 desc->hdr.ack = VIO_ACK_ENABLE;
455 desc->req_id = port->req_id;
456 desc->operation = op;
457 if (port->vdisk_type == VD_DISK_TYPE_DISK) {
463 desc->offset = (blk_rq_pos(req) << 9) / port->vdisk_block_size;
465 desc->ncookies = err;
467 /* This has to be a non-SMP write barrier because we are writing
468 * to memory which is shared with the peer LDOM.
471 desc->hdr.state = VIO_DESC_READY;
473 err = __vdc_tx_trigger(port);
475 printk(KERN_ERR PFX "vdc_tx_trigger() failure, err=%d\n", err);
478 dr->prod = (dr->prod + 1) & (VDC_TX_RING_SIZE - 1);
485 static void do_vdc_request(struct request_queue *q)
488 struct request *req = blk_fetch_request(q);
493 if (__send_request(req) < 0)
494 __blk_end_request_all(req, -EIO);
498 static int generic_request(struct vdc_port *port, u8 op, void *buf, int len)
500 struct vio_dring_state *dr;
501 struct vio_completion comp;
502 struct vio_disk_desc *desc;
503 unsigned int map_perm;
508 if (!(((u64)1 << (u64)op) & port->operations))
523 op_len = sizeof(u32);
524 map_perm = LDC_MAP_W;
528 op_len = sizeof(u32);
529 map_perm = LDC_MAP_R;
533 op_len = sizeof(struct vio_disk_vtoc);
534 map_perm = LDC_MAP_W;
538 op_len = sizeof(struct vio_disk_vtoc);
539 map_perm = LDC_MAP_R;
542 case VD_OP_GET_DISKGEOM:
543 op_len = sizeof(struct vio_disk_geom);
544 map_perm = LDC_MAP_W;
547 case VD_OP_SET_DISKGEOM:
548 op_len = sizeof(struct vio_disk_geom);
549 map_perm = LDC_MAP_R;
554 map_perm = LDC_MAP_RW;
557 case VD_OP_GET_DEVID:
558 op_len = sizeof(struct vio_disk_devid);
559 map_perm = LDC_MAP_W;
568 map_perm |= LDC_MAP_SHADOW | LDC_MAP_DIRECT | LDC_MAP_IO;
570 op_len = (op_len + 7) & ~7;
571 req_buf = kzalloc(op_len, GFP_KERNEL);
578 if (map_perm & LDC_MAP_R)
579 memcpy(req_buf, buf, len);
581 spin_lock_irqsave(&port->vio.lock, flags);
583 dr = &port->vio.drings[VIO_DRIVER_TX_RING];
585 /* XXX If we want to use this code generically we have to
586 * XXX handle TX ring exhaustion etc.
588 desc = vio_dring_cur(dr);
590 err = ldc_map_single(port->vio.lp, req_buf, op_len,
591 desc->cookies, port->ring_cookies,
594 spin_unlock_irqrestore(&port->vio.lock, flags);
599 init_completion(&comp.com);
600 comp.waiting_for = WAITING_FOR_GEN_CMD;
601 port->vio.cmp = ∁
603 desc->hdr.ack = VIO_ACK_ENABLE;
604 desc->req_id = port->req_id;
605 desc->operation = op;
610 desc->ncookies = err;
612 /* This has to be a non-SMP write barrier because we are writing
613 * to memory which is shared with the peer LDOM.
616 desc->hdr.state = VIO_DESC_READY;
618 err = __vdc_tx_trigger(port);
621 dr->prod = (dr->prod + 1) & (VDC_TX_RING_SIZE - 1);
622 spin_unlock_irqrestore(&port->vio.lock, flags);
624 wait_for_completion(&comp.com);
627 port->vio.cmp = NULL;
628 spin_unlock_irqrestore(&port->vio.lock, flags);
631 if (map_perm & LDC_MAP_W)
632 memcpy(buf, req_buf, len);
639 static int vdc_alloc_tx_ring(struct vdc_port *port)
641 struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING];
642 unsigned long len, entry_size;
646 entry_size = sizeof(struct vio_disk_desc) +
647 (sizeof(struct ldc_trans_cookie) * port->ring_cookies);
648 len = (VDC_TX_RING_SIZE * entry_size);
650 ncookies = VIO_MAX_RING_COOKIES;
651 dring = ldc_alloc_exp_dring(port->vio.lp, len,
652 dr->cookies, &ncookies,
657 return PTR_ERR(dring);
660 dr->entry_size = entry_size;
661 dr->num_entries = VDC_TX_RING_SIZE;
662 dr->prod = dr->cons = 0;
663 dr->pending = VDC_TX_RING_SIZE;
664 dr->ncookies = ncookies;
669 static void vdc_free_tx_ring(struct vdc_port *port)
671 struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING];
674 ldc_free_exp_dring(port->vio.lp, dr->base,
675 (dr->entry_size * dr->num_entries),
676 dr->cookies, dr->ncookies);
685 static int probe_disk(struct vdc_port *port)
687 struct vio_completion comp;
688 struct request_queue *q;
692 init_completion(&comp.com);
694 comp.waiting_for = WAITING_FOR_LINK_UP;
695 port->vio.cmp = ∁
697 vio_port_up(&port->vio);
699 wait_for_completion(&comp.com);
703 err = generic_request(port, VD_OP_GET_VTOC,
704 &port->label, sizeof(port->label));
706 printk(KERN_ERR PFX "VD_OP_GET_VTOC returns error %d\n", err);
710 if (vdc_version_supported(port, 1, 1)) {
711 /* vdisk_size should be set during the handshake, if it wasn't
712 * then the underlying disk is reserved by another system
714 if (port->vdisk_size == -1)
717 err = generic_request(port, VD_OP_GET_DISKGEOM,
718 &port->geom, sizeof(port->geom));
720 printk(KERN_ERR PFX "VD_OP_GET_DISKGEOM returns "
724 port->vdisk_size = ((u64)port->geom.num_cyl *
725 (u64)port->geom.num_hd *
726 (u64)port->geom.num_sec);
729 q = blk_init_queue(do_vdc_request, &port->vio.lock);
731 printk(KERN_ERR PFX "%s: Could not allocate queue.\n",
735 g = alloc_disk(1 << PARTITION_SHIFT);
737 printk(KERN_ERR PFX "%s: Could not allocate gendisk.\n",
739 blk_cleanup_queue(q);
745 blk_queue_max_segments(q, port->ring_cookies);
746 blk_queue_max_hw_sectors(q, port->max_xfer_size);
747 g->major = vdc_major;
748 g->first_minor = port->vio.vdev->dev_no << PARTITION_SHIFT;
749 strcpy(g->disk_name, port->disk_name);
753 g->private_data = port;
754 g->driverfs_dev = &port->vio.vdev->dev;
756 set_capacity(g, port->vdisk_size);
758 if (vdc_version_supported(port, 1, 1)) {
759 switch (port->vdisk_mtype) {
760 case VD_MEDIA_TYPE_CD:
761 pr_info(PFX "Virtual CDROM %s\n", port->disk_name);
762 g->flags |= GENHD_FL_CD;
763 g->flags |= GENHD_FL_REMOVABLE;
767 case VD_MEDIA_TYPE_DVD:
768 pr_info(PFX "Virtual DVD %s\n", port->disk_name);
769 g->flags |= GENHD_FL_CD;
770 g->flags |= GENHD_FL_REMOVABLE;
774 case VD_MEDIA_TYPE_FIXED:
775 pr_info(PFX "Virtual Hard disk %s\n", port->disk_name);
780 pr_info(PFX "%s: %u sectors (%u MB) protocol %d.%d\n",
782 port->vdisk_size, (port->vdisk_size >> (20 - 9)),
783 port->vio.ver.major, port->vio.ver.minor);
790 static struct ldc_channel_config vdc_ldc_cfg = {
793 .mode = LDC_MODE_UNRELIABLE,
796 static struct vio_driver_ops vdc_vio_ops = {
797 .send_attr = vdc_send_attr,
798 .handle_attr = vdc_handle_attr,
799 .handshake_complete = vdc_handshake_complete,
802 static void print_version(void)
804 static int version_printed;
806 if (version_printed++ == 0)
807 printk(KERN_INFO "%s", version);
810 static int vdc_port_probe(struct vio_dev *vdev, const struct vio_device_id *id)
812 struct mdesc_handle *hp;
813 struct vdc_port *port;
821 if ((vdev->dev_no << PARTITION_SHIFT) & ~(u64)MINORMASK) {
822 printk(KERN_ERR PFX "Port id [%llu] too large.\n",
824 goto err_out_release_mdesc;
827 port = kzalloc(sizeof(*port), GFP_KERNEL);
830 printk(KERN_ERR PFX "Cannot allocate vdc_port.\n");
831 goto err_out_release_mdesc;
834 if (vdev->dev_no >= 26)
835 snprintf(port->disk_name, sizeof(port->disk_name),
837 'a' + ((int)vdev->dev_no / 26) - 1,
838 'a' + ((int)vdev->dev_no % 26));
840 snprintf(port->disk_name, sizeof(port->disk_name),
841 VDCBLK_NAME "%c", 'a' + ((int)vdev->dev_no % 26));
842 port->vdisk_size = -1;
844 err = vio_driver_init(&port->vio, vdev, VDEV_DISK,
845 vdc_versions, ARRAY_SIZE(vdc_versions),
846 &vdc_vio_ops, port->disk_name);
848 goto err_out_free_port;
850 port->vdisk_block_size = 512;
851 port->max_xfer_size = ((128 * 1024) / port->vdisk_block_size);
852 port->ring_cookies = ((port->max_xfer_size *
853 port->vdisk_block_size) / PAGE_SIZE) + 2;
855 err = vio_ldc_alloc(&port->vio, &vdc_ldc_cfg, port);
857 goto err_out_free_port;
859 err = vdc_alloc_tx_ring(port);
861 goto err_out_free_ldc;
863 err = probe_disk(port);
865 goto err_out_free_tx_ring;
867 dev_set_drvdata(&vdev->dev, port);
873 err_out_free_tx_ring:
874 vdc_free_tx_ring(port);
877 vio_ldc_free(&port->vio);
882 err_out_release_mdesc:
887 static int vdc_port_remove(struct vio_dev *vdev)
889 struct vdc_port *port = dev_get_drvdata(&vdev->dev);
892 del_timer_sync(&port->vio.timer);
894 vdc_free_tx_ring(port);
895 vio_ldc_free(&port->vio);
897 dev_set_drvdata(&vdev->dev, NULL);
904 static const struct vio_device_id vdc_port_match[] = {
910 MODULE_DEVICE_TABLE(vio, vdc_port_match);
912 static struct vio_driver vdc_port_driver = {
913 .id_table = vdc_port_match,
914 .probe = vdc_port_probe,
915 .remove = vdc_port_remove,
919 static int __init vdc_init(void)
923 err = register_blkdev(0, VDCBLK_NAME);
929 err = vio_register_driver(&vdc_port_driver);
931 goto out_unregister_blkdev;
935 out_unregister_blkdev:
936 unregister_blkdev(vdc_major, VDCBLK_NAME);
943 static void __exit vdc_exit(void)
945 vio_unregister_driver(&vdc_port_driver);
946 unregister_blkdev(vdc_major, VDCBLK_NAME);
949 module_init(vdc_init);
950 module_exit(vdc_exit);