1 /* sunvdc.c: Sun LDOM Virtual Disk Client.
3 * Copyright (C) 2007 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/slab.h>
13 #include <linux/spinlock.h>
14 #include <linux/completion.h>
15 #include <linux/delay.h>
16 #include <linux/init.h>
17 #include <linux/list.h>
18 #include <linux/scatterlist.h>
23 #define DRV_MODULE_NAME "sunvdc"
24 #define PFX DRV_MODULE_NAME ": "
25 #define DRV_MODULE_VERSION "1.0"
26 #define DRV_MODULE_RELDATE "June 25, 2007"
28 static char version
[] __devinitdata
=
29 DRV_MODULE_NAME
".c:v" DRV_MODULE_VERSION
" (" DRV_MODULE_RELDATE
")\n";
30 MODULE_AUTHOR("David S. Miller (davem@davemloft.net)");
31 MODULE_DESCRIPTION("Sun LDOM virtual disk client driver");
32 MODULE_LICENSE("GPL");
33 MODULE_VERSION(DRV_MODULE_VERSION
);
35 #define VDC_TX_RING_SIZE 256
37 #define WAITING_FOR_LINK_UP 0x01
38 #define WAITING_FOR_TX_SPACE 0x02
39 #define WAITING_FOR_GEN_CMD 0x04
40 #define WAITING_FOR_ANY -1
42 struct vdc_req_entry
{
47 struct vio_driver_state vio
;
51 struct vdc_completion
*cmp
;
55 struct vdc_req_entry rq_arr
[VDC_TX_RING_SIZE
];
57 unsigned long ring_cookies
;
62 /* The server fills these in for us in the disk attribute
71 struct vio_disk_geom geom
;
72 struct vio_disk_vtoc label
;
75 static inline struct vdc_port
*to_vdc_port(struct vio_driver_state
*vio
)
77 return container_of(vio
, struct vdc_port
, vio
);
80 /* Ordered from largest major to lowest */
81 static struct vio_version vdc_versions
[] = {
82 { .major
= 1, .minor
= 0 },
85 #define VDCBLK_NAME "vdisk"
87 #define PARTITION_SHIFT 3
89 static inline u32
vdc_tx_dring_avail(struct vio_dring_state
*dr
)
91 return vio_dring_avail(dr
, VDC_TX_RING_SIZE
);
94 static int vdc_getgeo(struct block_device
*bdev
, struct hd_geometry
*geo
)
96 struct gendisk
*disk
= bdev
->bd_disk
;
97 struct vdc_port
*port
= disk
->private_data
;
99 geo
->heads
= (u8
) port
->geom
.num_hd
;
100 geo
->sectors
= (u8
) port
->geom
.num_sec
;
101 geo
->cylinders
= port
->geom
.num_cyl
;
106 static struct block_device_operations vdc_fops
= {
107 .owner
= THIS_MODULE
,
108 .getgeo
= vdc_getgeo
,
111 static void vdc_finish(struct vio_driver_state
*vio
, int err
, int waiting_for
)
114 (waiting_for
== -1 ||
115 vio
->cmp
->waiting_for
== waiting_for
)) {
117 complete(&vio
->cmp
->com
);
122 static void vdc_handshake_complete(struct vio_driver_state
*vio
)
124 vdc_finish(vio
, 0, WAITING_FOR_LINK_UP
);
127 static int vdc_handle_unknown(struct vdc_port
*port
, void *arg
)
129 struct vio_msg_tag
*pkt
= arg
;
131 printk(KERN_ERR PFX
"Received unknown msg [%02x:%02x:%04x:%08x]\n",
132 pkt
->type
, pkt
->stype
, pkt
->stype_env
, pkt
->sid
);
133 printk(KERN_ERR PFX
"Resetting connection.\n");
135 ldc_disconnect(port
->vio
.lp
);
140 static int vdc_send_attr(struct vio_driver_state
*vio
)
142 struct vdc_port
*port
= to_vdc_port(vio
);
143 struct vio_disk_attr_info pkt
;
145 memset(&pkt
, 0, sizeof(pkt
));
147 pkt
.tag
.type
= VIO_TYPE_CTRL
;
148 pkt
.tag
.stype
= VIO_SUBTYPE_INFO
;
149 pkt
.tag
.stype_env
= VIO_ATTR_INFO
;
150 pkt
.tag
.sid
= vio_send_sid(vio
);
152 pkt
.xfer_mode
= VIO_DRING_MODE
;
153 pkt
.vdisk_block_size
= port
->vdisk_block_size
;
154 pkt
.max_xfer_size
= port
->max_xfer_size
;
156 viodbg(HS
, "SEND ATTR xfer_mode[0x%x] blksz[%u] max_xfer[%lu]\n",
157 pkt
.xfer_mode
, pkt
.vdisk_block_size
, pkt
.max_xfer_size
);
159 return vio_ldc_send(&port
->vio
, &pkt
, sizeof(pkt
));
162 static int vdc_handle_attr(struct vio_driver_state
*vio
, void *arg
)
164 struct vdc_port
*port
= to_vdc_port(vio
);
165 struct vio_disk_attr_info
*pkt
= arg
;
167 viodbg(HS
, "GOT ATTR stype[0x%x] ops[%lx] disk_size[%lu] disk_type[%x] "
168 "xfer_mode[0x%x] blksz[%u] max_xfer[%lu]\n",
169 pkt
->tag
.stype
, pkt
->operations
,
170 pkt
->vdisk_size
, pkt
->vdisk_type
,
171 pkt
->xfer_mode
, pkt
->vdisk_block_size
,
174 if (pkt
->tag
.stype
== VIO_SUBTYPE_ACK
) {
175 switch (pkt
->vdisk_type
) {
176 case VD_DISK_TYPE_DISK
:
177 case VD_DISK_TYPE_SLICE
:
181 printk(KERN_ERR PFX
"%s: Bogus vdisk_type 0x%x\n",
182 vio
->name
, pkt
->vdisk_type
);
186 if (pkt
->vdisk_block_size
> port
->vdisk_block_size
) {
187 printk(KERN_ERR PFX
"%s: BLOCK size increased "
190 port
->vdisk_block_size
, pkt
->vdisk_block_size
);
194 port
->operations
= pkt
->operations
;
195 port
->vdisk_size
= pkt
->vdisk_size
;
196 port
->vdisk_type
= pkt
->vdisk_type
;
197 if (pkt
->max_xfer_size
< port
->max_xfer_size
)
198 port
->max_xfer_size
= pkt
->max_xfer_size
;
199 port
->vdisk_block_size
= pkt
->vdisk_block_size
;
202 printk(KERN_ERR PFX
"%s: Attribute NACK\n", vio
->name
);
208 static void vdc_end_special(struct vdc_port
*port
, struct vio_disk_desc
*desc
)
210 int err
= desc
->status
;
212 vdc_finish(&port
->vio
, -err
, WAITING_FOR_GEN_CMD
);
215 static void vdc_end_request(struct request
*req
, int uptodate
, int num_sectors
)
217 if (end_that_request_first(req
, uptodate
, num_sectors
))
219 add_disk_randomness(req
->rq_disk
);
220 end_that_request_last(req
, uptodate
);
223 static void vdc_end_one(struct vdc_port
*port
, struct vio_dring_state
*dr
,
226 struct vio_disk_desc
*desc
= vio_dring_entry(dr
, index
);
227 struct vdc_req_entry
*rqe
= &port
->rq_arr
[index
];
230 if (unlikely(desc
->hdr
.state
!= VIO_DESC_DONE
))
233 ldc_unmap(port
->vio
.lp
, desc
->cookies
, desc
->ncookies
);
234 desc
->hdr
.state
= VIO_DESC_FREE
;
235 dr
->cons
= (index
+ 1) & (VDC_TX_RING_SIZE
- 1);
239 vdc_end_special(port
, desc
);
245 vdc_end_request(req
, !desc
->status
, desc
->size
>> 9);
247 if (blk_queue_stopped(port
->disk
->queue
))
248 blk_start_queue(port
->disk
->queue
);
251 static int vdc_ack(struct vdc_port
*port
, void *msgbuf
)
253 struct vio_dring_state
*dr
= &port
->vio
.drings
[VIO_DRIVER_TX_RING
];
254 struct vio_dring_data
*pkt
= msgbuf
;
256 if (unlikely(pkt
->dring_ident
!= dr
->ident
||
257 pkt
->start_idx
!= pkt
->end_idx
||
258 pkt
->start_idx
>= VDC_TX_RING_SIZE
))
261 vdc_end_one(port
, dr
, pkt
->start_idx
);
266 static int vdc_nack(struct vdc_port
*port
, void *msgbuf
)
268 /* XXX Implement me XXX */
272 static void vdc_event(void *arg
, int event
)
274 struct vdc_port
*port
= arg
;
275 struct vio_driver_state
*vio
= &port
->vio
;
279 spin_lock_irqsave(&vio
->lock
, flags
);
281 if (unlikely(event
== LDC_EVENT_RESET
||
282 event
== LDC_EVENT_UP
)) {
283 vio_link_state_change(vio
, event
);
284 spin_unlock_irqrestore(&vio
->lock
, flags
);
288 if (unlikely(event
!= LDC_EVENT_DATA_READY
)) {
289 printk(KERN_WARNING PFX
"Unexpected LDC event %d\n", event
);
290 spin_unlock_irqrestore(&vio
->lock
, flags
);
297 struct vio_msg_tag tag
;
301 err
= ldc_read(vio
->lp
, &msgbuf
, sizeof(msgbuf
));
302 if (unlikely(err
< 0)) {
303 if (err
== -ECONNRESET
)
309 viodbg(DATA
, "TAG [%02x:%02x:%04x:%08x]\n",
312 msgbuf
.tag
.stype_env
,
314 err
= vio_validate_sid(vio
, &msgbuf
.tag
);
318 if (likely(msgbuf
.tag
.type
== VIO_TYPE_DATA
)) {
319 if (msgbuf
.tag
.stype
== VIO_SUBTYPE_ACK
)
320 err
= vdc_ack(port
, &msgbuf
);
321 else if (msgbuf
.tag
.stype
== VIO_SUBTYPE_NACK
)
322 err
= vdc_nack(port
, &msgbuf
);
324 err
= vdc_handle_unknown(port
, &msgbuf
);
325 } else if (msgbuf
.tag
.type
== VIO_TYPE_CTRL
) {
326 err
= vio_control_pkt_engine(vio
, &msgbuf
);
328 err
= vdc_handle_unknown(port
, &msgbuf
);
334 vdc_finish(&port
->vio
, err
, WAITING_FOR_ANY
);
335 spin_unlock_irqrestore(&vio
->lock
, flags
);
338 static int __vdc_tx_trigger(struct vdc_port
*port
)
340 struct vio_dring_state
*dr
= &port
->vio
.drings
[VIO_DRIVER_TX_RING
];
341 struct vio_dring_data hdr
= {
343 .type
= VIO_TYPE_DATA
,
344 .stype
= VIO_SUBTYPE_INFO
,
345 .stype_env
= VIO_DRING_DATA
,
346 .sid
= vio_send_sid(&port
->vio
),
348 .dring_ident
= dr
->ident
,
349 .start_idx
= dr
->prod
,
354 hdr
.seq
= dr
->snd_nxt
;
357 err
= vio_ldc_send(&port
->vio
, &hdr
, sizeof(hdr
));
363 if ((delay
<<= 1) > 128)
365 } while (err
== -EAGAIN
);
370 static int __send_request(struct request
*req
)
372 struct vdc_port
*port
= req
->rq_disk
->private_data
;
373 struct vio_dring_state
*dr
= &port
->vio
.drings
[VIO_DRIVER_TX_RING
];
374 struct scatterlist sg
[port
->ring_cookies
];
375 struct vdc_req_entry
*rqe
;
376 struct vio_disk_desc
*desc
;
377 unsigned int map_perm
;
382 map_perm
= LDC_MAP_SHADOW
| LDC_MAP_DIRECT
| LDC_MAP_IO
;
384 if (rq_data_dir(req
) == READ
) {
385 map_perm
|= LDC_MAP_W
;
388 map_perm
|= LDC_MAP_R
;
392 sg_init_table(sg
, port
->ring_cookies
);
393 nsg
= blk_rq_map_sg(req
->q
, req
, sg
);
396 for (i
= 0; i
< nsg
; i
++)
399 if (unlikely(vdc_tx_dring_avail(dr
) < 1)) {
400 blk_stop_queue(port
->disk
->queue
);
405 desc
= vio_dring_cur(dr
);
407 err
= ldc_map_sg(port
->vio
.lp
, sg
, nsg
,
408 desc
->cookies
, port
->ring_cookies
,
411 printk(KERN_ERR PFX
"ldc_map_sg() failure, err=%d.\n", err
);
415 rqe
= &port
->rq_arr
[dr
->prod
];
418 desc
->hdr
.ack
= VIO_ACK_ENABLE
;
419 desc
->req_id
= port
->req_id
;
420 desc
->operation
= op
;
421 if (port
->vdisk_type
== VD_DISK_TYPE_DISK
) {
427 desc
->offset
= (req
->sector
<< 9) / port
->vdisk_block_size
;
429 desc
->ncookies
= err
;
431 /* This has to be a non-SMP write barrier because we are writing
432 * to memory which is shared with the peer LDOM.
435 desc
->hdr
.state
= VIO_DESC_READY
;
437 err
= __vdc_tx_trigger(port
);
439 printk(KERN_ERR PFX
"vdc_tx_trigger() failure, err=%d\n", err
);
442 dr
->prod
= (dr
->prod
+ 1) & (VDC_TX_RING_SIZE
- 1);
449 static void do_vdc_request(struct request_queue
*q
)
452 struct request
*req
= elv_next_request(q
);
457 blkdev_dequeue_request(req
);
458 if (__send_request(req
) < 0)
459 vdc_end_request(req
, 0, req
->hard_nr_sectors
);
463 static int generic_request(struct vdc_port
*port
, u8 op
, void *buf
, int len
)
465 struct vio_dring_state
*dr
;
466 struct vio_completion comp
;
467 struct vio_disk_desc
*desc
;
468 unsigned int map_perm
;
473 if (!(((u64
)1 << ((u64
)op
- 1)) & port
->operations
))
488 op_len
= sizeof(u32
);
489 map_perm
= LDC_MAP_W
;
493 op_len
= sizeof(u32
);
494 map_perm
= LDC_MAP_R
;
498 op_len
= sizeof(struct vio_disk_vtoc
);
499 map_perm
= LDC_MAP_W
;
503 op_len
= sizeof(struct vio_disk_vtoc
);
504 map_perm
= LDC_MAP_R
;
507 case VD_OP_GET_DISKGEOM
:
508 op_len
= sizeof(struct vio_disk_geom
);
509 map_perm
= LDC_MAP_W
;
512 case VD_OP_SET_DISKGEOM
:
513 op_len
= sizeof(struct vio_disk_geom
);
514 map_perm
= LDC_MAP_R
;
519 map_perm
= LDC_MAP_RW
;
522 case VD_OP_GET_DEVID
:
523 op_len
= sizeof(struct vio_disk_devid
);
524 map_perm
= LDC_MAP_W
;
533 map_perm
|= LDC_MAP_SHADOW
| LDC_MAP_DIRECT
| LDC_MAP_IO
;
535 op_len
= (op_len
+ 7) & ~7;
536 req_buf
= kzalloc(op_len
, GFP_KERNEL
);
543 if (map_perm
& LDC_MAP_R
)
544 memcpy(req_buf
, buf
, len
);
546 spin_lock_irqsave(&port
->vio
.lock
, flags
);
548 dr
= &port
->vio
.drings
[VIO_DRIVER_TX_RING
];
550 /* XXX If we want to use this code generically we have to
551 * XXX handle TX ring exhaustion etc.
553 desc
= vio_dring_cur(dr
);
555 err
= ldc_map_single(port
->vio
.lp
, req_buf
, op_len
,
556 desc
->cookies
, port
->ring_cookies
,
559 spin_unlock_irqrestore(&port
->vio
.lock
, flags
);
564 init_completion(&comp
.com
);
565 comp
.waiting_for
= WAITING_FOR_GEN_CMD
;
566 port
->vio
.cmp
= &comp
;
568 desc
->hdr
.ack
= VIO_ACK_ENABLE
;
569 desc
->req_id
= port
->req_id
;
570 desc
->operation
= op
;
575 desc
->ncookies
= err
;
577 /* This has to be a non-SMP write barrier because we are writing
578 * to memory which is shared with the peer LDOM.
581 desc
->hdr
.state
= VIO_DESC_READY
;
583 err
= __vdc_tx_trigger(port
);
586 dr
->prod
= (dr
->prod
+ 1) & (VDC_TX_RING_SIZE
- 1);
587 spin_unlock_irqrestore(&port
->vio
.lock
, flags
);
589 wait_for_completion(&comp
.com
);
592 port
->vio
.cmp
= NULL
;
593 spin_unlock_irqrestore(&port
->vio
.lock
, flags
);
596 if (map_perm
& LDC_MAP_W
)
597 memcpy(buf
, req_buf
, len
);
604 static int __devinit
vdc_alloc_tx_ring(struct vdc_port
*port
)
606 struct vio_dring_state
*dr
= &port
->vio
.drings
[VIO_DRIVER_TX_RING
];
607 unsigned long len
, entry_size
;
611 entry_size
= sizeof(struct vio_disk_desc
) +
612 (sizeof(struct ldc_trans_cookie
) * port
->ring_cookies
);
613 len
= (VDC_TX_RING_SIZE
* entry_size
);
615 ncookies
= VIO_MAX_RING_COOKIES
;
616 dring
= ldc_alloc_exp_dring(port
->vio
.lp
, len
,
617 dr
->cookies
, &ncookies
,
622 return PTR_ERR(dring
);
625 dr
->entry_size
= entry_size
;
626 dr
->num_entries
= VDC_TX_RING_SIZE
;
627 dr
->prod
= dr
->cons
= 0;
628 dr
->pending
= VDC_TX_RING_SIZE
;
629 dr
->ncookies
= ncookies
;
634 static void vdc_free_tx_ring(struct vdc_port
*port
)
636 struct vio_dring_state
*dr
= &port
->vio
.drings
[VIO_DRIVER_TX_RING
];
639 ldc_free_exp_dring(port
->vio
.lp
, dr
->base
,
640 (dr
->entry_size
* dr
->num_entries
),
641 dr
->cookies
, dr
->ncookies
);
650 static int probe_disk(struct vdc_port
*port
)
652 struct vio_completion comp
;
653 struct request_queue
*q
;
657 init_completion(&comp
.com
);
659 comp
.waiting_for
= WAITING_FOR_LINK_UP
;
660 port
->vio
.cmp
= &comp
;
662 vio_port_up(&port
->vio
);
664 wait_for_completion(&comp
.com
);
668 err
= generic_request(port
, VD_OP_GET_VTOC
,
669 &port
->label
, sizeof(port
->label
));
671 printk(KERN_ERR PFX
"VD_OP_GET_VTOC returns error %d\n", err
);
675 err
= generic_request(port
, VD_OP_GET_DISKGEOM
,
676 &port
->geom
, sizeof(port
->geom
));
678 printk(KERN_ERR PFX
"VD_OP_GET_DISKGEOM returns "
683 port
->vdisk_size
= ((u64
)port
->geom
.num_cyl
*
684 (u64
)port
->geom
.num_hd
*
685 (u64
)port
->geom
.num_sec
);
687 q
= blk_init_queue(do_vdc_request
, &port
->vio
.lock
);
689 printk(KERN_ERR PFX
"%s: Could not allocate queue.\n",
693 g
= alloc_disk(1 << PARTITION_SHIFT
);
695 printk(KERN_ERR PFX
"%s: Could not allocate gendisk.\n",
697 blk_cleanup_queue(q
);
703 blk_queue_max_hw_segments(q
, port
->ring_cookies
);
704 blk_queue_max_phys_segments(q
, port
->ring_cookies
);
705 blk_queue_max_sectors(q
, port
->max_xfer_size
);
706 g
->major
= vdc_major
;
707 g
->first_minor
= port
->vio
.vdev
->dev_no
<< PARTITION_SHIFT
;
708 strcpy(g
->disk_name
, port
->disk_name
);
712 g
->private_data
= port
;
713 g
->driverfs_dev
= &port
->vio
.vdev
->dev
;
715 set_capacity(g
, port
->vdisk_size
);
717 printk(KERN_INFO PFX
"%s: %u sectors (%u MB)\n",
719 port
->vdisk_size
, (port
->vdisk_size
>> (20 - 9)));
726 static struct ldc_channel_config vdc_ldc_cfg
= {
729 .mode
= LDC_MODE_UNRELIABLE
,
732 static struct vio_driver_ops vdc_vio_ops
= {
733 .send_attr
= vdc_send_attr
,
734 .handle_attr
= vdc_handle_attr
,
735 .handshake_complete
= vdc_handshake_complete
,
738 static void print_version(void)
740 static int version_printed
;
742 if (version_printed
++ == 0)
743 printk(KERN_INFO
"%s", version
);
746 static int __devinit
vdc_port_probe(struct vio_dev
*vdev
,
747 const struct vio_device_id
*id
)
749 struct mdesc_handle
*hp
;
750 struct vdc_port
*port
;
758 if ((vdev
->dev_no
<< PARTITION_SHIFT
) & ~(u64
)MINORMASK
) {
759 printk(KERN_ERR PFX
"Port id [%lu] too large.\n",
761 goto err_out_release_mdesc
;
764 port
= kzalloc(sizeof(*port
), GFP_KERNEL
);
767 printk(KERN_ERR PFX
"Cannot allocate vdc_port.\n");
768 goto err_out_release_mdesc
;
771 if (vdev
->dev_no
>= 26)
772 snprintf(port
->disk_name
, sizeof(port
->disk_name
),
774 'a' + ((int)vdev
->dev_no
/ 26) - 1,
775 'a' + ((int)vdev
->dev_no
% 26));
777 snprintf(port
->disk_name
, sizeof(port
->disk_name
),
778 VDCBLK_NAME
"%c", 'a' + ((int)vdev
->dev_no
% 26));
780 err
= vio_driver_init(&port
->vio
, vdev
, VDEV_DISK
,
781 vdc_versions
, ARRAY_SIZE(vdc_versions
),
782 &vdc_vio_ops
, port
->disk_name
);
784 goto err_out_free_port
;
786 port
->vdisk_block_size
= 512;
787 port
->max_xfer_size
= ((128 * 1024) / port
->vdisk_block_size
);
788 port
->ring_cookies
= ((port
->max_xfer_size
*
789 port
->vdisk_block_size
) / PAGE_SIZE
) + 2;
791 err
= vio_ldc_alloc(&port
->vio
, &vdc_ldc_cfg
, port
);
793 goto err_out_free_port
;
795 err
= vdc_alloc_tx_ring(port
);
797 goto err_out_free_ldc
;
799 err
= probe_disk(port
);
801 goto err_out_free_tx_ring
;
803 dev_set_drvdata(&vdev
->dev
, port
);
809 err_out_free_tx_ring
:
810 vdc_free_tx_ring(port
);
813 vio_ldc_free(&port
->vio
);
818 err_out_release_mdesc
:
823 static int vdc_port_remove(struct vio_dev
*vdev
)
825 struct vdc_port
*port
= dev_get_drvdata(&vdev
->dev
);
828 del_timer_sync(&port
->vio
.timer
);
830 vdc_free_tx_ring(port
);
831 vio_ldc_free(&port
->vio
);
833 dev_set_drvdata(&vdev
->dev
, NULL
);
840 static struct vio_device_id vdc_port_match
[] = {
846 MODULE_DEVICE_TABLE(vio
, vdc_port_match
);
848 static struct vio_driver vdc_port_driver
= {
849 .id_table
= vdc_port_match
,
850 .probe
= vdc_port_probe
,
851 .remove
= vdc_port_remove
,
854 .owner
= THIS_MODULE
,
858 static int __init
vdc_init(void)
862 err
= register_blkdev(0, VDCBLK_NAME
);
868 err
= vio_register_driver(&vdc_port_driver
);
870 goto out_unregister_blkdev
;
874 out_unregister_blkdev
:
875 unregister_blkdev(vdc_major
, VDCBLK_NAME
);
882 static void __exit
vdc_exit(void)
884 vio_unregister_driver(&vdc_port_driver
);
885 unregister_blkdev(vdc_major
, VDCBLK_NAME
);
888 module_init(vdc_init
);
889 module_exit(vdc_exit
);