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_vtoc label;
76 static inline struct vdc_port *to_vdc_port(struct vio_driver_state *vio)
78 return container_of(vio, struct vdc_port, vio);
81 /* Ordered from largest major to lowest */
82 static struct vio_version vdc_versions[] = {
83 { .major = 1, .minor = 1 },
84 { .major = 1, .minor = 0 },
87 static inline int vdc_version_supported(struct vdc_port *port,
90 return port->vio.ver.major == major && port->vio.ver.minor >= minor;
93 #define VDCBLK_NAME "vdisk"
95 #define PARTITION_SHIFT 3
97 static inline u32 vdc_tx_dring_avail(struct vio_dring_state *dr)
99 return vio_dring_avail(dr, VDC_TX_RING_SIZE);
102 static int vdc_getgeo(struct block_device *bdev, struct hd_geometry *geo)
104 struct gendisk *disk = bdev->bd_disk;
105 sector_t nsect = get_capacity(disk);
106 sector_t cylinders = nsect;
110 sector_div(cylinders, geo->heads * geo->sectors);
111 geo->cylinders = cylinders;
112 if ((sector_t)(geo->cylinders + 1) * geo->heads * geo->sectors < nsect)
113 geo->cylinders = 0xffff;
118 /* Add ioctl/CDROM_GET_CAPABILITY to support cdrom_id in udev
119 * when vdisk_mtype is VD_MEDIA_TYPE_CD or VD_MEDIA_TYPE_DVD.
120 * Needed to be able to install inside an ldom from an iso image.
122 static int vdc_ioctl(struct block_device *bdev, fmode_t mode,
123 unsigned command, unsigned long argument)
126 struct gendisk *disk;
129 case CDROMMULTISESSION:
130 pr_debug(PFX "Multisession CDs not supported\n");
131 for (i = 0; i < sizeof(struct cdrom_multisession); i++)
132 if (put_user(0, (char __user *)(argument + i)))
136 case CDROM_GET_CAPABILITY:
137 disk = bdev->bd_disk;
139 if (bdev->bd_disk && (disk->flags & GENHD_FL_CD))
144 pr_debug(PFX "ioctl %08x not supported\n", command);
149 static const struct block_device_operations vdc_fops = {
150 .owner = THIS_MODULE,
151 .getgeo = vdc_getgeo,
155 static void vdc_finish(struct vio_driver_state *vio, int err, int waiting_for)
158 (waiting_for == -1 ||
159 vio->cmp->waiting_for == waiting_for)) {
161 complete(&vio->cmp->com);
166 static void vdc_handshake_complete(struct vio_driver_state *vio)
168 vdc_finish(vio, 0, WAITING_FOR_LINK_UP);
171 static int vdc_handle_unknown(struct vdc_port *port, void *arg)
173 struct vio_msg_tag *pkt = arg;
175 printk(KERN_ERR PFX "Received unknown msg [%02x:%02x:%04x:%08x]\n",
176 pkt->type, pkt->stype, pkt->stype_env, pkt->sid);
177 printk(KERN_ERR PFX "Resetting connection.\n");
179 ldc_disconnect(port->vio.lp);
184 static int vdc_send_attr(struct vio_driver_state *vio)
186 struct vdc_port *port = to_vdc_port(vio);
187 struct vio_disk_attr_info pkt;
189 memset(&pkt, 0, sizeof(pkt));
191 pkt.tag.type = VIO_TYPE_CTRL;
192 pkt.tag.stype = VIO_SUBTYPE_INFO;
193 pkt.tag.stype_env = VIO_ATTR_INFO;
194 pkt.tag.sid = vio_send_sid(vio);
196 pkt.xfer_mode = VIO_DRING_MODE;
197 pkt.vdisk_block_size = port->vdisk_block_size;
198 pkt.max_xfer_size = port->max_xfer_size;
200 viodbg(HS, "SEND ATTR xfer_mode[0x%x] blksz[%u] max_xfer[%llu]\n",
201 pkt.xfer_mode, pkt.vdisk_block_size, pkt.max_xfer_size);
203 return vio_ldc_send(&port->vio, &pkt, sizeof(pkt));
206 static int vdc_handle_attr(struct vio_driver_state *vio, void *arg)
208 struct vdc_port *port = to_vdc_port(vio);
209 struct vio_disk_attr_info *pkt = arg;
211 viodbg(HS, "GOT ATTR stype[0x%x] ops[%llx] disk_size[%llu] disk_type[%x] "
212 "mtype[0x%x] xfer_mode[0x%x] blksz[%u] max_xfer[%llu]\n",
213 pkt->tag.stype, pkt->operations,
214 pkt->vdisk_size, pkt->vdisk_type, pkt->vdisk_mtype,
215 pkt->xfer_mode, pkt->vdisk_block_size,
218 if (pkt->tag.stype == VIO_SUBTYPE_ACK) {
219 switch (pkt->vdisk_type) {
220 case VD_DISK_TYPE_DISK:
221 case VD_DISK_TYPE_SLICE:
225 printk(KERN_ERR PFX "%s: Bogus vdisk_type 0x%x\n",
226 vio->name, pkt->vdisk_type);
230 if (pkt->vdisk_block_size > port->vdisk_block_size) {
231 printk(KERN_ERR PFX "%s: BLOCK size increased "
234 port->vdisk_block_size, pkt->vdisk_block_size);
238 port->operations = pkt->operations;
239 port->vdisk_type = pkt->vdisk_type;
240 if (vdc_version_supported(port, 1, 1)) {
241 port->vdisk_size = pkt->vdisk_size;
242 port->vdisk_mtype = pkt->vdisk_mtype;
244 if (pkt->max_xfer_size < port->max_xfer_size)
245 port->max_xfer_size = pkt->max_xfer_size;
246 port->vdisk_block_size = pkt->vdisk_block_size;
249 printk(KERN_ERR PFX "%s: Attribute NACK\n", vio->name);
255 static void vdc_end_special(struct vdc_port *port, struct vio_disk_desc *desc)
257 int err = desc->status;
259 vdc_finish(&port->vio, -err, WAITING_FOR_GEN_CMD);
262 static void vdc_end_one(struct vdc_port *port, struct vio_dring_state *dr,
265 struct vio_disk_desc *desc = vio_dring_entry(dr, index);
266 struct vdc_req_entry *rqe = &port->rq_arr[index];
269 if (unlikely(desc->hdr.state != VIO_DESC_DONE))
272 ldc_unmap(port->vio.lp, desc->cookies, desc->ncookies);
273 desc->hdr.state = VIO_DESC_FREE;
274 dr->cons = (index + 1) & (VDC_TX_RING_SIZE - 1);
278 vdc_end_special(port, desc);
284 __blk_end_request(req, (desc->status ? -EIO : 0), desc->size);
286 if (blk_queue_stopped(port->disk->queue))
287 blk_start_queue(port->disk->queue);
290 static int vdc_ack(struct vdc_port *port, void *msgbuf)
292 struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING];
293 struct vio_dring_data *pkt = msgbuf;
295 if (unlikely(pkt->dring_ident != dr->ident ||
296 pkt->start_idx != pkt->end_idx ||
297 pkt->start_idx >= VDC_TX_RING_SIZE))
300 vdc_end_one(port, dr, pkt->start_idx);
305 static int vdc_nack(struct vdc_port *port, void *msgbuf)
307 /* XXX Implement me XXX */
311 static void vdc_event(void *arg, int event)
313 struct vdc_port *port = arg;
314 struct vio_driver_state *vio = &port->vio;
318 spin_lock_irqsave(&vio->lock, flags);
320 if (unlikely(event == LDC_EVENT_RESET ||
321 event == LDC_EVENT_UP)) {
322 vio_link_state_change(vio, event);
323 spin_unlock_irqrestore(&vio->lock, flags);
327 if (unlikely(event != LDC_EVENT_DATA_READY)) {
328 printk(KERN_WARNING PFX "Unexpected LDC event %d\n", event);
329 spin_unlock_irqrestore(&vio->lock, flags);
336 struct vio_msg_tag tag;
340 err = ldc_read(vio->lp, &msgbuf, sizeof(msgbuf));
341 if (unlikely(err < 0)) {
342 if (err == -ECONNRESET)
348 viodbg(DATA, "TAG [%02x:%02x:%04x:%08x]\n",
351 msgbuf.tag.stype_env,
353 err = vio_validate_sid(vio, &msgbuf.tag);
357 if (likely(msgbuf.tag.type == VIO_TYPE_DATA)) {
358 if (msgbuf.tag.stype == VIO_SUBTYPE_ACK)
359 err = vdc_ack(port, &msgbuf);
360 else if (msgbuf.tag.stype == VIO_SUBTYPE_NACK)
361 err = vdc_nack(port, &msgbuf);
363 err = vdc_handle_unknown(port, &msgbuf);
364 } else if (msgbuf.tag.type == VIO_TYPE_CTRL) {
365 err = vio_control_pkt_engine(vio, &msgbuf);
367 err = vdc_handle_unknown(port, &msgbuf);
373 vdc_finish(&port->vio, err, WAITING_FOR_ANY);
374 spin_unlock_irqrestore(&vio->lock, flags);
377 static int __vdc_tx_trigger(struct vdc_port *port)
379 struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING];
380 struct vio_dring_data hdr = {
382 .type = VIO_TYPE_DATA,
383 .stype = VIO_SUBTYPE_INFO,
384 .stype_env = VIO_DRING_DATA,
385 .sid = vio_send_sid(&port->vio),
387 .dring_ident = dr->ident,
388 .start_idx = dr->prod,
393 hdr.seq = dr->snd_nxt;
396 err = vio_ldc_send(&port->vio, &hdr, sizeof(hdr));
402 if ((delay <<= 1) > 128)
404 } while (err == -EAGAIN);
409 static int __send_request(struct request *req)
411 struct vdc_port *port = req->rq_disk->private_data;
412 struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING];
413 struct scatterlist sg[port->ring_cookies];
414 struct vdc_req_entry *rqe;
415 struct vio_disk_desc *desc;
416 unsigned int map_perm;
421 map_perm = LDC_MAP_SHADOW | LDC_MAP_DIRECT | LDC_MAP_IO;
423 if (rq_data_dir(req) == READ) {
424 map_perm |= LDC_MAP_W;
427 map_perm |= LDC_MAP_R;
431 sg_init_table(sg, port->ring_cookies);
432 nsg = blk_rq_map_sg(req->q, req, sg);
435 for (i = 0; i < nsg; i++)
438 if (unlikely(vdc_tx_dring_avail(dr) < 1)) {
439 blk_stop_queue(port->disk->queue);
444 desc = vio_dring_cur(dr);
446 err = ldc_map_sg(port->vio.lp, sg, nsg,
447 desc->cookies, port->ring_cookies,
450 printk(KERN_ERR PFX "ldc_map_sg() failure, err=%d.\n", err);
454 rqe = &port->rq_arr[dr->prod];
457 desc->hdr.ack = VIO_ACK_ENABLE;
458 desc->req_id = port->req_id;
459 desc->operation = op;
460 if (port->vdisk_type == VD_DISK_TYPE_DISK) {
466 desc->offset = (blk_rq_pos(req) << 9) / port->vdisk_block_size;
468 desc->ncookies = err;
470 /* This has to be a non-SMP write barrier because we are writing
471 * to memory which is shared with the peer LDOM.
474 desc->hdr.state = VIO_DESC_READY;
476 err = __vdc_tx_trigger(port);
478 printk(KERN_ERR PFX "vdc_tx_trigger() failure, err=%d\n", err);
481 dr->prod = (dr->prod + 1) & (VDC_TX_RING_SIZE - 1);
488 static void do_vdc_request(struct request_queue *q)
491 struct request *req = blk_fetch_request(q);
496 if (__send_request(req) < 0)
497 __blk_end_request_all(req, -EIO);
501 static int generic_request(struct vdc_port *port, u8 op, void *buf, int len)
503 struct vio_dring_state *dr;
504 struct vio_completion comp;
505 struct vio_disk_desc *desc;
506 unsigned int map_perm;
511 if (!(((u64)1 << (u64)op) & port->operations))
526 op_len = sizeof(u32);
527 map_perm = LDC_MAP_W;
531 op_len = sizeof(u32);
532 map_perm = LDC_MAP_R;
536 op_len = sizeof(struct vio_disk_vtoc);
537 map_perm = LDC_MAP_W;
541 op_len = sizeof(struct vio_disk_vtoc);
542 map_perm = LDC_MAP_R;
545 case VD_OP_GET_DISKGEOM:
546 op_len = sizeof(struct vio_disk_geom);
547 map_perm = LDC_MAP_W;
550 case VD_OP_SET_DISKGEOM:
551 op_len = sizeof(struct vio_disk_geom);
552 map_perm = LDC_MAP_R;
557 map_perm = LDC_MAP_RW;
560 case VD_OP_GET_DEVID:
561 op_len = sizeof(struct vio_disk_devid);
562 map_perm = LDC_MAP_W;
571 map_perm |= LDC_MAP_SHADOW | LDC_MAP_DIRECT | LDC_MAP_IO;
573 op_len = (op_len + 7) & ~7;
574 req_buf = kzalloc(op_len, GFP_KERNEL);
581 if (map_perm & LDC_MAP_R)
582 memcpy(req_buf, buf, len);
584 spin_lock_irqsave(&port->vio.lock, flags);
586 dr = &port->vio.drings[VIO_DRIVER_TX_RING];
588 /* XXX If we want to use this code generically we have to
589 * XXX handle TX ring exhaustion etc.
591 desc = vio_dring_cur(dr);
593 err = ldc_map_single(port->vio.lp, req_buf, op_len,
594 desc->cookies, port->ring_cookies,
597 spin_unlock_irqrestore(&port->vio.lock, flags);
602 init_completion(&comp.com);
603 comp.waiting_for = WAITING_FOR_GEN_CMD;
604 port->vio.cmp = ∁
606 desc->hdr.ack = VIO_ACK_ENABLE;
607 desc->req_id = port->req_id;
608 desc->operation = op;
613 desc->ncookies = err;
615 /* This has to be a non-SMP write barrier because we are writing
616 * to memory which is shared with the peer LDOM.
619 desc->hdr.state = VIO_DESC_READY;
621 err = __vdc_tx_trigger(port);
624 dr->prod = (dr->prod + 1) & (VDC_TX_RING_SIZE - 1);
625 spin_unlock_irqrestore(&port->vio.lock, flags);
627 wait_for_completion(&comp.com);
630 port->vio.cmp = NULL;
631 spin_unlock_irqrestore(&port->vio.lock, flags);
634 if (map_perm & LDC_MAP_W)
635 memcpy(buf, req_buf, len);
642 static int vdc_alloc_tx_ring(struct vdc_port *port)
644 struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING];
645 unsigned long len, entry_size;
649 entry_size = sizeof(struct vio_disk_desc) +
650 (sizeof(struct ldc_trans_cookie) * port->ring_cookies);
651 len = (VDC_TX_RING_SIZE * entry_size);
653 ncookies = VIO_MAX_RING_COOKIES;
654 dring = ldc_alloc_exp_dring(port->vio.lp, len,
655 dr->cookies, &ncookies,
660 return PTR_ERR(dring);
663 dr->entry_size = entry_size;
664 dr->num_entries = VDC_TX_RING_SIZE;
665 dr->prod = dr->cons = 0;
666 dr->pending = VDC_TX_RING_SIZE;
667 dr->ncookies = ncookies;
672 static void vdc_free_tx_ring(struct vdc_port *port)
674 struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING];
677 ldc_free_exp_dring(port->vio.lp, dr->base,
678 (dr->entry_size * dr->num_entries),
679 dr->cookies, dr->ncookies);
688 static int probe_disk(struct vdc_port *port)
690 struct vio_completion comp;
691 struct request_queue *q;
695 init_completion(&comp.com);
697 comp.waiting_for = WAITING_FOR_LINK_UP;
698 port->vio.cmp = ∁
700 vio_port_up(&port->vio);
702 wait_for_completion(&comp.com);
706 err = generic_request(port, VD_OP_GET_VTOC,
707 &port->label, sizeof(port->label));
709 printk(KERN_ERR PFX "VD_OP_GET_VTOC returns error %d\n", err);
713 if (vdc_version_supported(port, 1, 1)) {
714 /* vdisk_size should be set during the handshake, if it wasn't
715 * then the underlying disk is reserved by another system
717 if (port->vdisk_size == -1)
720 struct vio_disk_geom geom;
722 err = generic_request(port, VD_OP_GET_DISKGEOM,
723 &geom, sizeof(geom));
725 printk(KERN_ERR PFX "VD_OP_GET_DISKGEOM returns "
729 port->vdisk_size = ((u64)geom.num_cyl *
734 q = blk_init_queue(do_vdc_request, &port->vio.lock);
736 printk(KERN_ERR PFX "%s: Could not allocate queue.\n",
740 g = alloc_disk(1 << PARTITION_SHIFT);
742 printk(KERN_ERR PFX "%s: Could not allocate gendisk.\n",
744 blk_cleanup_queue(q);
750 /* Each segment in a request is up to an aligned page in size. */
751 blk_queue_segment_boundary(q, PAGE_SIZE - 1);
752 blk_queue_max_segment_size(q, PAGE_SIZE);
754 blk_queue_max_segments(q, port->ring_cookies);
755 blk_queue_max_hw_sectors(q, port->max_xfer_size);
756 g->major = vdc_major;
757 g->first_minor = port->vio.vdev->dev_no << PARTITION_SHIFT;
758 strcpy(g->disk_name, port->disk_name);
762 g->private_data = port;
763 g->driverfs_dev = &port->vio.vdev->dev;
765 set_capacity(g, port->vdisk_size);
767 if (vdc_version_supported(port, 1, 1)) {
768 switch (port->vdisk_mtype) {
769 case VD_MEDIA_TYPE_CD:
770 pr_info(PFX "Virtual CDROM %s\n", port->disk_name);
771 g->flags |= GENHD_FL_CD;
772 g->flags |= GENHD_FL_REMOVABLE;
776 case VD_MEDIA_TYPE_DVD:
777 pr_info(PFX "Virtual DVD %s\n", port->disk_name);
778 g->flags |= GENHD_FL_CD;
779 g->flags |= GENHD_FL_REMOVABLE;
783 case VD_MEDIA_TYPE_FIXED:
784 pr_info(PFX "Virtual Hard disk %s\n", port->disk_name);
789 pr_info(PFX "%s: %u sectors (%u MB) protocol %d.%d\n",
791 port->vdisk_size, (port->vdisk_size >> (20 - 9)),
792 port->vio.ver.major, port->vio.ver.minor);
799 static struct ldc_channel_config vdc_ldc_cfg = {
802 .mode = LDC_MODE_UNRELIABLE,
805 static struct vio_driver_ops vdc_vio_ops = {
806 .send_attr = vdc_send_attr,
807 .handle_attr = vdc_handle_attr,
808 .handshake_complete = vdc_handshake_complete,
811 static void print_version(void)
813 static int version_printed;
815 if (version_printed++ == 0)
816 printk(KERN_INFO "%s", version);
819 static int vdc_port_probe(struct vio_dev *vdev, const struct vio_device_id *id)
821 struct mdesc_handle *hp;
822 struct vdc_port *port;
830 if ((vdev->dev_no << PARTITION_SHIFT) & ~(u64)MINORMASK) {
831 printk(KERN_ERR PFX "Port id [%llu] too large.\n",
833 goto err_out_release_mdesc;
836 port = kzalloc(sizeof(*port), GFP_KERNEL);
839 printk(KERN_ERR PFX "Cannot allocate vdc_port.\n");
840 goto err_out_release_mdesc;
843 if (vdev->dev_no >= 26)
844 snprintf(port->disk_name, sizeof(port->disk_name),
846 'a' + ((int)vdev->dev_no / 26) - 1,
847 'a' + ((int)vdev->dev_no % 26));
849 snprintf(port->disk_name, sizeof(port->disk_name),
850 VDCBLK_NAME "%c", 'a' + ((int)vdev->dev_no % 26));
851 port->vdisk_size = -1;
853 err = vio_driver_init(&port->vio, vdev, VDEV_DISK,
854 vdc_versions, ARRAY_SIZE(vdc_versions),
855 &vdc_vio_ops, port->disk_name);
857 goto err_out_free_port;
859 port->vdisk_block_size = 512;
860 port->max_xfer_size = ((128 * 1024) / port->vdisk_block_size);
861 port->ring_cookies = ((port->max_xfer_size *
862 port->vdisk_block_size) / PAGE_SIZE) + 2;
864 err = vio_ldc_alloc(&port->vio, &vdc_ldc_cfg, port);
866 goto err_out_free_port;
868 err = vdc_alloc_tx_ring(port);
870 goto err_out_free_ldc;
872 err = probe_disk(port);
874 goto err_out_free_tx_ring;
876 dev_set_drvdata(&vdev->dev, port);
882 err_out_free_tx_ring:
883 vdc_free_tx_ring(port);
886 vio_ldc_free(&port->vio);
891 err_out_release_mdesc:
896 static int vdc_port_remove(struct vio_dev *vdev)
898 struct vdc_port *port = dev_get_drvdata(&vdev->dev);
901 del_timer_sync(&port->vio.timer);
903 vdc_free_tx_ring(port);
904 vio_ldc_free(&port->vio);
906 dev_set_drvdata(&vdev->dev, NULL);
913 static const struct vio_device_id vdc_port_match[] = {
919 MODULE_DEVICE_TABLE(vio, vdc_port_match);
921 static struct vio_driver vdc_port_driver = {
922 .id_table = vdc_port_match,
923 .probe = vdc_port_probe,
924 .remove = vdc_port_remove,
928 static int __init vdc_init(void)
932 err = register_blkdev(0, VDCBLK_NAME);
938 err = vio_register_driver(&vdc_port_driver);
940 goto out_unregister_blkdev;
944 out_unregister_blkdev:
945 unregister_blkdev(vdc_major, VDCBLK_NAME);
952 static void __exit vdc_exit(void)
954 vio_unregister_driver(&vdc_port_driver);
955 unregister_blkdev(vdc_major, VDCBLK_NAME);
958 module_init(vdc_init);
959 module_exit(vdc_exit);