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.2"
27 #define DRV_MODULE_RELDATE "November 24, 2014"
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 512
37 #define VDC_DEFAULT_BLK_SIZE 512
39 #define MAX_XFER_BLKS (128 * 1024)
40 #define MAX_XFER_SIZE (MAX_XFER_BLKS / VDC_DEFAULT_BLK_SIZE)
41 #define MAX_RING_COOKIES ((MAX_XFER_BLKS / PAGE_SIZE) + 2)
43 #define WAITING_FOR_LINK_UP 0x01
44 #define WAITING_FOR_TX_SPACE 0x02
45 #define WAITING_FOR_GEN_CMD 0x04
46 #define WAITING_FOR_ANY -1
48 static struct workqueue_struct
*sunvdc_wq
;
50 struct vdc_req_entry
{
55 struct vio_driver_state vio
;
59 struct vdc_completion
*cmp
;
63 struct vdc_req_entry rq_arr
[VDC_TX_RING_SIZE
];
65 unsigned long ring_cookies
;
71 struct timer_list ldc_reset_timer
;
72 struct work_struct ldc_reset_work
;
74 /* The server fills these in for us in the disk attribute
86 static void vdc_ldc_reset(struct vdc_port
*port
);
87 static void vdc_ldc_reset_work(struct work_struct
*work
);
88 static void vdc_ldc_reset_timer(struct timer_list
*t
);
90 static inline struct vdc_port
*to_vdc_port(struct vio_driver_state
*vio
)
92 return container_of(vio
, struct vdc_port
, vio
);
95 /* Ordered from largest major to lowest */
96 static struct vio_version vdc_versions
[] = {
97 { .major
= 1, .minor
= 2 },
98 { .major
= 1, .minor
= 1 },
99 { .major
= 1, .minor
= 0 },
102 static inline int vdc_version_supported(struct vdc_port
*port
,
103 u16 major
, u16 minor
)
105 return port
->vio
.ver
.major
== major
&& port
->vio
.ver
.minor
>= minor
;
108 #define VDCBLK_NAME "vdisk"
109 static int vdc_major
;
110 #define PARTITION_SHIFT 3
112 static inline u32
vdc_tx_dring_avail(struct vio_dring_state
*dr
)
114 return vio_dring_avail(dr
, VDC_TX_RING_SIZE
);
117 static int vdc_getgeo(struct block_device
*bdev
, struct hd_geometry
*geo
)
119 struct gendisk
*disk
= bdev
->bd_disk
;
120 sector_t nsect
= get_capacity(disk
);
121 sector_t cylinders
= nsect
;
125 sector_div(cylinders
, geo
->heads
* geo
->sectors
);
126 geo
->cylinders
= cylinders
;
127 if ((sector_t
)(geo
->cylinders
+ 1) * geo
->heads
* geo
->sectors
< nsect
)
128 geo
->cylinders
= 0xffff;
133 /* Add ioctl/CDROM_GET_CAPABILITY to support cdrom_id in udev
134 * when vdisk_mtype is VD_MEDIA_TYPE_CD or VD_MEDIA_TYPE_DVD.
135 * Needed to be able to install inside an ldom from an iso image.
137 static int vdc_ioctl(struct block_device
*bdev
, fmode_t mode
,
138 unsigned command
, unsigned long argument
)
141 struct gendisk
*disk
;
144 case CDROMMULTISESSION
:
145 pr_debug(PFX
"Multisession CDs not supported\n");
146 for (i
= 0; i
< sizeof(struct cdrom_multisession
); i
++)
147 if (put_user(0, (char __user
*)(argument
+ i
)))
151 case CDROM_GET_CAPABILITY
:
152 disk
= bdev
->bd_disk
;
154 if (bdev
->bd_disk
&& (disk
->flags
& GENHD_FL_CD
))
159 pr_debug(PFX
"ioctl %08x not supported\n", command
);
164 static const struct block_device_operations vdc_fops
= {
165 .owner
= THIS_MODULE
,
166 .getgeo
= vdc_getgeo
,
170 static void vdc_blk_queue_start(struct vdc_port
*port
)
172 struct vio_dring_state
*dr
= &port
->vio
.drings
[VIO_DRIVER_TX_RING
];
174 /* restart blk queue when ring is half emptied. also called after
175 * handshake completes, so check for initial handshake before we've
178 if (port
->disk
&& blk_queue_stopped(port
->disk
->queue
) &&
179 vdc_tx_dring_avail(dr
) * 100 / VDC_TX_RING_SIZE
>= 50) {
180 blk_start_queue(port
->disk
->queue
);
185 static void vdc_finish(struct vio_driver_state
*vio
, int err
, int waiting_for
)
188 (waiting_for
== -1 ||
189 vio
->cmp
->waiting_for
== waiting_for
)) {
191 complete(&vio
->cmp
->com
);
196 static void vdc_handshake_complete(struct vio_driver_state
*vio
)
198 struct vdc_port
*port
= to_vdc_port(vio
);
200 del_timer(&port
->ldc_reset_timer
);
201 vdc_finish(vio
, 0, WAITING_FOR_LINK_UP
);
202 vdc_blk_queue_start(port
);
205 static int vdc_handle_unknown(struct vdc_port
*port
, void *arg
)
207 struct vio_msg_tag
*pkt
= arg
;
209 printk(KERN_ERR PFX
"Received unknown msg [%02x:%02x:%04x:%08x]\n",
210 pkt
->type
, pkt
->stype
, pkt
->stype_env
, pkt
->sid
);
211 printk(KERN_ERR PFX
"Resetting connection.\n");
213 ldc_disconnect(port
->vio
.lp
);
218 static int vdc_send_attr(struct vio_driver_state
*vio
)
220 struct vdc_port
*port
= to_vdc_port(vio
);
221 struct vio_disk_attr_info pkt
;
223 memset(&pkt
, 0, sizeof(pkt
));
225 pkt
.tag
.type
= VIO_TYPE_CTRL
;
226 pkt
.tag
.stype
= VIO_SUBTYPE_INFO
;
227 pkt
.tag
.stype_env
= VIO_ATTR_INFO
;
228 pkt
.tag
.sid
= vio_send_sid(vio
);
230 pkt
.xfer_mode
= VIO_DRING_MODE
;
231 pkt
.vdisk_block_size
= port
->vdisk_block_size
;
232 pkt
.max_xfer_size
= port
->max_xfer_size
;
234 viodbg(HS
, "SEND ATTR xfer_mode[0x%x] blksz[%u] max_xfer[%llu]\n",
235 pkt
.xfer_mode
, pkt
.vdisk_block_size
, pkt
.max_xfer_size
);
237 return vio_ldc_send(&port
->vio
, &pkt
, sizeof(pkt
));
240 static int vdc_handle_attr(struct vio_driver_state
*vio
, void *arg
)
242 struct vdc_port
*port
= to_vdc_port(vio
);
243 struct vio_disk_attr_info
*pkt
= arg
;
245 viodbg(HS
, "GOT ATTR stype[0x%x] ops[%llx] disk_size[%llu] disk_type[%x] "
246 "mtype[0x%x] xfer_mode[0x%x] blksz[%u] max_xfer[%llu]\n",
247 pkt
->tag
.stype
, pkt
->operations
,
248 pkt
->vdisk_size
, pkt
->vdisk_type
, pkt
->vdisk_mtype
,
249 pkt
->xfer_mode
, pkt
->vdisk_block_size
,
252 if (pkt
->tag
.stype
== VIO_SUBTYPE_ACK
) {
253 switch (pkt
->vdisk_type
) {
254 case VD_DISK_TYPE_DISK
:
255 case VD_DISK_TYPE_SLICE
:
259 printk(KERN_ERR PFX
"%s: Bogus vdisk_type 0x%x\n",
260 vio
->name
, pkt
->vdisk_type
);
264 if (pkt
->vdisk_block_size
> port
->vdisk_block_size
) {
265 printk(KERN_ERR PFX
"%s: BLOCK size increased "
268 port
->vdisk_block_size
, pkt
->vdisk_block_size
);
272 port
->operations
= pkt
->operations
;
273 port
->vdisk_type
= pkt
->vdisk_type
;
274 if (vdc_version_supported(port
, 1, 1)) {
275 port
->vdisk_size
= pkt
->vdisk_size
;
276 port
->vdisk_mtype
= pkt
->vdisk_mtype
;
278 if (pkt
->max_xfer_size
< port
->max_xfer_size
)
279 port
->max_xfer_size
= pkt
->max_xfer_size
;
280 port
->vdisk_block_size
= pkt
->vdisk_block_size
;
282 port
->vdisk_phys_blksz
= VDC_DEFAULT_BLK_SIZE
;
283 if (vdc_version_supported(port
, 1, 2))
284 port
->vdisk_phys_blksz
= pkt
->phys_block_size
;
288 printk(KERN_ERR PFX
"%s: Attribute NACK\n", vio
->name
);
294 static void vdc_end_special(struct vdc_port
*port
, struct vio_disk_desc
*desc
)
296 int err
= desc
->status
;
298 vdc_finish(&port
->vio
, -err
, WAITING_FOR_GEN_CMD
);
301 static void vdc_end_one(struct vdc_port
*port
, struct vio_dring_state
*dr
,
304 struct vio_disk_desc
*desc
= vio_dring_entry(dr
, index
);
305 struct vdc_req_entry
*rqe
= &port
->rq_arr
[index
];
308 if (unlikely(desc
->hdr
.state
!= VIO_DESC_DONE
))
311 ldc_unmap(port
->vio
.lp
, desc
->cookies
, desc
->ncookies
);
312 desc
->hdr
.state
= VIO_DESC_FREE
;
313 dr
->cons
= vio_dring_next(dr
, index
);
317 vdc_end_special(port
, desc
);
323 __blk_end_request(req
, (desc
->status
? BLK_STS_IOERR
: 0), desc
->size
);
325 vdc_blk_queue_start(port
);
328 static int vdc_ack(struct vdc_port
*port
, void *msgbuf
)
330 struct vio_dring_state
*dr
= &port
->vio
.drings
[VIO_DRIVER_TX_RING
];
331 struct vio_dring_data
*pkt
= msgbuf
;
333 if (unlikely(pkt
->dring_ident
!= dr
->ident
||
334 pkt
->start_idx
!= pkt
->end_idx
||
335 pkt
->start_idx
>= VDC_TX_RING_SIZE
))
338 vdc_end_one(port
, dr
, pkt
->start_idx
);
343 static int vdc_nack(struct vdc_port
*port
, void *msgbuf
)
345 /* XXX Implement me XXX */
349 static void vdc_event(void *arg
, int event
)
351 struct vdc_port
*port
= arg
;
352 struct vio_driver_state
*vio
= &port
->vio
;
356 spin_lock_irqsave(&vio
->lock
, flags
);
358 if (unlikely(event
== LDC_EVENT_RESET
)) {
359 vio_link_state_change(vio
, event
);
360 queue_work(sunvdc_wq
, &port
->ldc_reset_work
);
364 if (unlikely(event
== LDC_EVENT_UP
)) {
365 vio_link_state_change(vio
, event
);
369 if (unlikely(event
!= LDC_EVENT_DATA_READY
)) {
370 pr_warn(PFX
"Unexpected LDC event %d\n", event
);
377 struct vio_msg_tag tag
;
381 err
= ldc_read(vio
->lp
, &msgbuf
, sizeof(msgbuf
));
382 if (unlikely(err
< 0)) {
383 if (err
== -ECONNRESET
)
389 viodbg(DATA
, "TAG [%02x:%02x:%04x:%08x]\n",
392 msgbuf
.tag
.stype_env
,
394 err
= vio_validate_sid(vio
, &msgbuf
.tag
);
398 if (likely(msgbuf
.tag
.type
== VIO_TYPE_DATA
)) {
399 if (msgbuf
.tag
.stype
== VIO_SUBTYPE_ACK
)
400 err
= vdc_ack(port
, &msgbuf
);
401 else if (msgbuf
.tag
.stype
== VIO_SUBTYPE_NACK
)
402 err
= vdc_nack(port
, &msgbuf
);
404 err
= vdc_handle_unknown(port
, &msgbuf
);
405 } else if (msgbuf
.tag
.type
== VIO_TYPE_CTRL
) {
406 err
= vio_control_pkt_engine(vio
, &msgbuf
);
408 err
= vdc_handle_unknown(port
, &msgbuf
);
414 vdc_finish(&port
->vio
, err
, WAITING_FOR_ANY
);
416 spin_unlock_irqrestore(&vio
->lock
, flags
);
419 static int __vdc_tx_trigger(struct vdc_port
*port
)
421 struct vio_dring_state
*dr
= &port
->vio
.drings
[VIO_DRIVER_TX_RING
];
422 struct vio_dring_data hdr
= {
424 .type
= VIO_TYPE_DATA
,
425 .stype
= VIO_SUBTYPE_INFO
,
426 .stype_env
= VIO_DRING_DATA
,
427 .sid
= vio_send_sid(&port
->vio
),
429 .dring_ident
= dr
->ident
,
430 .start_idx
= dr
->prod
,
435 hdr
.seq
= dr
->snd_nxt
;
438 err
= vio_ldc_send(&port
->vio
, &hdr
, sizeof(hdr
));
444 if ((delay
<<= 1) > 128)
446 } while (err
== -EAGAIN
);
448 if (err
== -ENOTCONN
)
453 static int __send_request(struct request
*req
)
455 struct vdc_port
*port
= req
->rq_disk
->private_data
;
456 struct vio_dring_state
*dr
= &port
->vio
.drings
[VIO_DRIVER_TX_RING
];
457 struct scatterlist sg
[MAX_RING_COOKIES
];
458 struct vdc_req_entry
*rqe
;
459 struct vio_disk_desc
*desc
;
460 unsigned int map_perm
;
465 if (WARN_ON(port
->ring_cookies
> MAX_RING_COOKIES
))
468 map_perm
= LDC_MAP_SHADOW
| LDC_MAP_DIRECT
| LDC_MAP_IO
;
470 if (rq_data_dir(req
) == READ
) {
471 map_perm
|= LDC_MAP_W
;
474 map_perm
|= LDC_MAP_R
;
478 sg_init_table(sg
, port
->ring_cookies
);
479 nsg
= blk_rq_map_sg(req
->q
, req
, sg
);
482 for (i
= 0; i
< nsg
; i
++)
485 desc
= vio_dring_cur(dr
);
487 err
= ldc_map_sg(port
->vio
.lp
, sg
, nsg
,
488 desc
->cookies
, port
->ring_cookies
,
491 printk(KERN_ERR PFX
"ldc_map_sg() failure, err=%d.\n", err
);
495 rqe
= &port
->rq_arr
[dr
->prod
];
498 desc
->hdr
.ack
= VIO_ACK_ENABLE
;
499 desc
->req_id
= port
->req_id
;
500 desc
->operation
= op
;
501 if (port
->vdisk_type
== VD_DISK_TYPE_DISK
) {
507 desc
->offset
= (blk_rq_pos(req
) << 9) / port
->vdisk_block_size
;
509 desc
->ncookies
= err
;
511 /* This has to be a non-SMP write barrier because we are writing
512 * to memory which is shared with the peer LDOM.
515 desc
->hdr
.state
= VIO_DESC_READY
;
517 err
= __vdc_tx_trigger(port
);
519 printk(KERN_ERR PFX
"vdc_tx_trigger() failure, err=%d\n", err
);
522 dr
->prod
= vio_dring_next(dr
, dr
->prod
);
528 static void do_vdc_request(struct request_queue
*rq
)
532 while ((req
= blk_peek_request(rq
)) != NULL
) {
533 struct vdc_port
*port
;
534 struct vio_dring_state
*dr
;
536 port
= req
->rq_disk
->private_data
;
537 dr
= &port
->vio
.drings
[VIO_DRIVER_TX_RING
];
538 if (unlikely(vdc_tx_dring_avail(dr
) < 1))
541 blk_start_request(req
);
543 if (__send_request(req
) < 0) {
544 blk_requeue_request(rq
, req
);
546 /* Avoid pointless unplugs. */
553 static int generic_request(struct vdc_port
*port
, u8 op
, void *buf
, int len
)
555 struct vio_dring_state
*dr
;
556 struct vio_completion comp
;
557 struct vio_disk_desc
*desc
;
558 unsigned int map_perm
;
563 if (!(((u64
)1 << (u64
)op
) & port
->operations
))
578 op_len
= sizeof(u32
);
579 map_perm
= LDC_MAP_W
;
583 op_len
= sizeof(u32
);
584 map_perm
= LDC_MAP_R
;
588 op_len
= sizeof(struct vio_disk_vtoc
);
589 map_perm
= LDC_MAP_W
;
593 op_len
= sizeof(struct vio_disk_vtoc
);
594 map_perm
= LDC_MAP_R
;
597 case VD_OP_GET_DISKGEOM
:
598 op_len
= sizeof(struct vio_disk_geom
);
599 map_perm
= LDC_MAP_W
;
602 case VD_OP_SET_DISKGEOM
:
603 op_len
= sizeof(struct vio_disk_geom
);
604 map_perm
= LDC_MAP_R
;
609 map_perm
= LDC_MAP_RW
;
612 case VD_OP_GET_DEVID
:
613 op_len
= sizeof(struct vio_disk_devid
);
614 map_perm
= LDC_MAP_W
;
623 map_perm
|= LDC_MAP_SHADOW
| LDC_MAP_DIRECT
| LDC_MAP_IO
;
625 op_len
= (op_len
+ 7) & ~7;
626 req_buf
= kzalloc(op_len
, GFP_KERNEL
);
633 if (map_perm
& LDC_MAP_R
)
634 memcpy(req_buf
, buf
, len
);
636 spin_lock_irqsave(&port
->vio
.lock
, flags
);
638 dr
= &port
->vio
.drings
[VIO_DRIVER_TX_RING
];
640 /* XXX If we want to use this code generically we have to
641 * XXX handle TX ring exhaustion etc.
643 desc
= vio_dring_cur(dr
);
645 err
= ldc_map_single(port
->vio
.lp
, req_buf
, op_len
,
646 desc
->cookies
, port
->ring_cookies
,
649 spin_unlock_irqrestore(&port
->vio
.lock
, flags
);
654 init_completion(&comp
.com
);
655 comp
.waiting_for
= WAITING_FOR_GEN_CMD
;
656 port
->vio
.cmp
= &comp
;
658 desc
->hdr
.ack
= VIO_ACK_ENABLE
;
659 desc
->req_id
= port
->req_id
;
660 desc
->operation
= op
;
665 desc
->ncookies
= err
;
667 /* This has to be a non-SMP write barrier because we are writing
668 * to memory which is shared with the peer LDOM.
671 desc
->hdr
.state
= VIO_DESC_READY
;
673 err
= __vdc_tx_trigger(port
);
676 dr
->prod
= vio_dring_next(dr
, dr
->prod
);
677 spin_unlock_irqrestore(&port
->vio
.lock
, flags
);
679 wait_for_completion(&comp
.com
);
682 port
->vio
.cmp
= NULL
;
683 spin_unlock_irqrestore(&port
->vio
.lock
, flags
);
686 if (map_perm
& LDC_MAP_W
)
687 memcpy(buf
, req_buf
, len
);
694 static int vdc_alloc_tx_ring(struct vdc_port
*port
)
696 struct vio_dring_state
*dr
= &port
->vio
.drings
[VIO_DRIVER_TX_RING
];
697 unsigned long len
, entry_size
;
701 entry_size
= sizeof(struct vio_disk_desc
) +
702 (sizeof(struct ldc_trans_cookie
) * port
->ring_cookies
);
703 len
= (VDC_TX_RING_SIZE
* entry_size
);
705 ncookies
= VIO_MAX_RING_COOKIES
;
706 dring
= ldc_alloc_exp_dring(port
->vio
.lp
, len
,
707 dr
->cookies
, &ncookies
,
712 return PTR_ERR(dring
);
715 dr
->entry_size
= entry_size
;
716 dr
->num_entries
= VDC_TX_RING_SIZE
;
717 dr
->prod
= dr
->cons
= 0;
718 dr
->pending
= VDC_TX_RING_SIZE
;
719 dr
->ncookies
= ncookies
;
724 static void vdc_free_tx_ring(struct vdc_port
*port
)
726 struct vio_dring_state
*dr
= &port
->vio
.drings
[VIO_DRIVER_TX_RING
];
729 ldc_free_exp_dring(port
->vio
.lp
, dr
->base
,
730 (dr
->entry_size
* dr
->num_entries
),
731 dr
->cookies
, dr
->ncookies
);
740 static int vdc_port_up(struct vdc_port
*port
)
742 struct vio_completion comp
;
744 init_completion(&comp
.com
);
746 comp
.waiting_for
= WAITING_FOR_LINK_UP
;
747 port
->vio
.cmp
= &comp
;
749 vio_port_up(&port
->vio
);
750 wait_for_completion(&comp
.com
);
754 static void vdc_port_down(struct vdc_port
*port
)
756 ldc_disconnect(port
->vio
.lp
);
757 ldc_unbind(port
->vio
.lp
);
758 vdc_free_tx_ring(port
);
759 vio_ldc_free(&port
->vio
);
762 static int probe_disk(struct vdc_port
*port
)
764 struct request_queue
*q
;
768 err
= vdc_port_up(port
);
772 /* Using version 1.2 means vdisk_phys_blksz should be set unless the
773 * disk is reserved by another system.
775 if (vdc_version_supported(port
, 1, 2) && !port
->vdisk_phys_blksz
)
778 if (vdc_version_supported(port
, 1, 1)) {
779 /* vdisk_size should be set during the handshake, if it wasn't
780 * then the underlying disk is reserved by another system
782 if (port
->vdisk_size
== -1)
785 struct vio_disk_geom geom
;
787 err
= generic_request(port
, VD_OP_GET_DISKGEOM
,
788 &geom
, sizeof(geom
));
790 printk(KERN_ERR PFX
"VD_OP_GET_DISKGEOM returns "
794 port
->vdisk_size
= ((u64
)geom
.num_cyl
*
799 q
= blk_init_queue(do_vdc_request
, &port
->vio
.lock
);
801 printk(KERN_ERR PFX
"%s: Could not allocate queue.\n",
805 g
= alloc_disk(1 << PARTITION_SHIFT
);
807 printk(KERN_ERR PFX
"%s: Could not allocate gendisk.\n",
809 blk_cleanup_queue(q
);
815 /* Each segment in a request is up to an aligned page in size. */
816 blk_queue_segment_boundary(q
, PAGE_SIZE
- 1);
817 blk_queue_max_segment_size(q
, PAGE_SIZE
);
819 blk_queue_max_segments(q
, port
->ring_cookies
);
820 blk_queue_max_hw_sectors(q
, port
->max_xfer_size
);
821 g
->major
= vdc_major
;
822 g
->first_minor
= port
->vio
.vdev
->dev_no
<< PARTITION_SHIFT
;
823 strcpy(g
->disk_name
, port
->disk_name
);
827 g
->private_data
= port
;
829 set_capacity(g
, port
->vdisk_size
);
831 if (vdc_version_supported(port
, 1, 1)) {
832 switch (port
->vdisk_mtype
) {
833 case VD_MEDIA_TYPE_CD
:
834 pr_info(PFX
"Virtual CDROM %s\n", port
->disk_name
);
835 g
->flags
|= GENHD_FL_CD
;
836 g
->flags
|= GENHD_FL_REMOVABLE
;
840 case VD_MEDIA_TYPE_DVD
:
841 pr_info(PFX
"Virtual DVD %s\n", port
->disk_name
);
842 g
->flags
|= GENHD_FL_CD
;
843 g
->flags
|= GENHD_FL_REMOVABLE
;
847 case VD_MEDIA_TYPE_FIXED
:
848 pr_info(PFX
"Virtual Hard disk %s\n", port
->disk_name
);
853 blk_queue_physical_block_size(q
, port
->vdisk_phys_blksz
);
855 pr_info(PFX
"%s: %u sectors (%u MB) protocol %d.%d\n",
857 port
->vdisk_size
, (port
->vdisk_size
>> (20 - 9)),
858 port
->vio
.ver
.major
, port
->vio
.ver
.minor
);
860 device_add_disk(&port
->vio
.vdev
->dev
, g
, NULL
);
865 static struct ldc_channel_config vdc_ldc_cfg
= {
868 .mode
= LDC_MODE_UNRELIABLE
,
871 static struct vio_driver_ops vdc_vio_ops
= {
872 .send_attr
= vdc_send_attr
,
873 .handle_attr
= vdc_handle_attr
,
874 .handshake_complete
= vdc_handshake_complete
,
877 static void print_version(void)
879 static int version_printed
;
881 if (version_printed
++ == 0)
882 printk(KERN_INFO
"%s", version
);
885 struct vdc_check_port_data
{
890 static int vdc_device_probed(struct device
*dev
, void *arg
)
892 struct vio_dev
*vdev
= to_vio_dev(dev
);
893 struct vdc_check_port_data
*port_data
;
895 port_data
= (struct vdc_check_port_data
*)arg
;
897 if ((vdev
->dev_no
== port_data
->dev_no
) &&
898 (!(strcmp((char *)&vdev
->type
, port_data
->type
))) &&
899 dev_get_drvdata(dev
)) {
900 /* This device has already been configured
901 * by vdc_port_probe()
909 /* Determine whether the VIO device is part of an mpgroup
910 * by locating all the virtual-device-port nodes associated
911 * with the parent virtual-device node for the VIO device
912 * and checking whether any of these nodes are vdc-ports
913 * which have already been configured.
915 * Returns true if this device is part of an mpgroup and has
916 * already been probed.
918 static bool vdc_port_mpgroup_check(struct vio_dev
*vdev
)
920 struct vdc_check_port_data port_data
;
923 port_data
.dev_no
= vdev
->dev_no
;
924 port_data
.type
= (char *)&vdev
->type
;
926 dev
= device_find_child(vdev
->dev
.parent
, &port_data
,
935 static int vdc_port_probe(struct vio_dev
*vdev
, const struct vio_device_id
*id
)
937 struct mdesc_handle
*hp
;
938 struct vdc_port
*port
;
940 const u64
*ldc_timeout
;
947 if ((vdev
->dev_no
<< PARTITION_SHIFT
) & ~(u64
)MINORMASK
) {
948 printk(KERN_ERR PFX
"Port id [%llu] too large.\n",
950 goto err_out_release_mdesc
;
953 /* Check if this device is part of an mpgroup */
954 if (vdc_port_mpgroup_check(vdev
)) {
956 "VIO: Ignoring extra vdisk port %s",
957 dev_name(&vdev
->dev
));
958 goto err_out_release_mdesc
;
961 port
= kzalloc(sizeof(*port
), GFP_KERNEL
);
964 printk(KERN_ERR PFX
"Cannot allocate vdc_port.\n");
965 goto err_out_release_mdesc
;
968 if (vdev
->dev_no
>= 26)
969 snprintf(port
->disk_name
, sizeof(port
->disk_name
),
971 'a' + ((int)vdev
->dev_no
/ 26) - 1,
972 'a' + ((int)vdev
->dev_no
% 26));
974 snprintf(port
->disk_name
, sizeof(port
->disk_name
),
975 VDCBLK_NAME
"%c", 'a' + ((int)vdev
->dev_no
% 26));
976 port
->vdisk_size
= -1;
978 /* Actual wall time may be double due to do_generic_file_read() doing
979 * a readahead I/O first, and once that fails it will try to read a
982 ldc_timeout
= mdesc_get_property(hp
, vdev
->mp
, "vdc-timeout", NULL
);
983 port
->ldc_timeout
= ldc_timeout
? *ldc_timeout
: 0;
984 timer_setup(&port
->ldc_reset_timer
, vdc_ldc_reset_timer
, 0);
985 INIT_WORK(&port
->ldc_reset_work
, vdc_ldc_reset_work
);
987 err
= vio_driver_init(&port
->vio
, vdev
, VDEV_DISK
,
988 vdc_versions
, ARRAY_SIZE(vdc_versions
),
989 &vdc_vio_ops
, port
->disk_name
);
991 goto err_out_free_port
;
993 port
->vdisk_block_size
= VDC_DEFAULT_BLK_SIZE
;
994 port
->max_xfer_size
= MAX_XFER_SIZE
;
995 port
->ring_cookies
= MAX_RING_COOKIES
;
997 err
= vio_ldc_alloc(&port
->vio
, &vdc_ldc_cfg
, port
);
999 goto err_out_free_port
;
1001 err
= vdc_alloc_tx_ring(port
);
1003 goto err_out_free_ldc
;
1005 err
= probe_disk(port
);
1007 goto err_out_free_tx_ring
;
1009 /* Note that the device driver_data is used to determine
1010 * whether the port has been probed.
1012 dev_set_drvdata(&vdev
->dev
, port
);
1018 err_out_free_tx_ring
:
1019 vdc_free_tx_ring(port
);
1022 vio_ldc_free(&port
->vio
);
1027 err_out_release_mdesc
:
1032 static int vdc_port_remove(struct vio_dev
*vdev
)
1034 struct vdc_port
*port
= dev_get_drvdata(&vdev
->dev
);
1037 unsigned long flags
;
1039 spin_lock_irqsave(&port
->vio
.lock
, flags
);
1040 blk_stop_queue(port
->disk
->queue
);
1041 spin_unlock_irqrestore(&port
->vio
.lock
, flags
);
1043 flush_work(&port
->ldc_reset_work
);
1044 del_timer_sync(&port
->ldc_reset_timer
);
1045 del_timer_sync(&port
->vio
.timer
);
1047 del_gendisk(port
->disk
);
1048 blk_cleanup_queue(port
->disk
->queue
);
1049 put_disk(port
->disk
);
1052 vdc_free_tx_ring(port
);
1053 vio_ldc_free(&port
->vio
);
1055 dev_set_drvdata(&vdev
->dev
, NULL
);
1062 static void vdc_requeue_inflight(struct vdc_port
*port
)
1064 struct vio_dring_state
*dr
= &port
->vio
.drings
[VIO_DRIVER_TX_RING
];
1067 for (idx
= dr
->cons
; idx
!= dr
->prod
; idx
= vio_dring_next(dr
, idx
)) {
1068 struct vio_disk_desc
*desc
= vio_dring_entry(dr
, idx
);
1069 struct vdc_req_entry
*rqe
= &port
->rq_arr
[idx
];
1070 struct request
*req
;
1072 ldc_unmap(port
->vio
.lp
, desc
->cookies
, desc
->ncookies
);
1073 desc
->hdr
.state
= VIO_DESC_FREE
;
1074 dr
->cons
= vio_dring_next(dr
, idx
);
1078 vdc_end_special(port
, desc
);
1083 blk_requeue_request(port
->disk
->queue
, req
);
1087 static void vdc_queue_drain(struct vdc_port
*port
)
1089 struct request
*req
;
1091 while ((req
= blk_fetch_request(port
->disk
->queue
)) != NULL
)
1092 __blk_end_request_all(req
, BLK_STS_IOERR
);
1095 static void vdc_ldc_reset_timer(struct timer_list
*t
)
1097 struct vdc_port
*port
= from_timer(port
, t
, ldc_reset_timer
);
1098 struct vio_driver_state
*vio
= &port
->vio
;
1099 unsigned long flags
;
1101 spin_lock_irqsave(&vio
->lock
, flags
);
1102 if (!(port
->vio
.hs_state
& VIO_HS_COMPLETE
)) {
1103 pr_warn(PFX
"%s ldc down %llu seconds, draining queue\n",
1104 port
->disk_name
, port
->ldc_timeout
);
1105 vdc_queue_drain(port
);
1106 vdc_blk_queue_start(port
);
1108 spin_unlock_irqrestore(&vio
->lock
, flags
);
1111 static void vdc_ldc_reset_work(struct work_struct
*work
)
1113 struct vdc_port
*port
;
1114 struct vio_driver_state
*vio
;
1115 unsigned long flags
;
1117 port
= container_of(work
, struct vdc_port
, ldc_reset_work
);
1120 spin_lock_irqsave(&vio
->lock
, flags
);
1121 vdc_ldc_reset(port
);
1122 spin_unlock_irqrestore(&vio
->lock
, flags
);
1125 static void vdc_ldc_reset(struct vdc_port
*port
)
1129 assert_spin_locked(&port
->vio
.lock
);
1131 pr_warn(PFX
"%s ldc link reset\n", port
->disk_name
);
1132 blk_stop_queue(port
->disk
->queue
);
1133 vdc_requeue_inflight(port
);
1134 vdc_port_down(port
);
1136 err
= vio_ldc_alloc(&port
->vio
, &vdc_ldc_cfg
, port
);
1138 pr_err(PFX
"%s vio_ldc_alloc:%d\n", port
->disk_name
, err
);
1142 err
= vdc_alloc_tx_ring(port
);
1144 pr_err(PFX
"%s vio_alloc_tx_ring:%d\n", port
->disk_name
, err
);
1148 if (port
->ldc_timeout
)
1149 mod_timer(&port
->ldc_reset_timer
,
1150 round_jiffies(jiffies
+ HZ
* port
->ldc_timeout
));
1151 mod_timer(&port
->vio
.timer
, round_jiffies(jiffies
+ HZ
));
1155 vio_ldc_free(&port
->vio
);
1158 static const struct vio_device_id vdc_port_match
[] = {
1164 MODULE_DEVICE_TABLE(vio
, vdc_port_match
);
1166 static struct vio_driver vdc_port_driver
= {
1167 .id_table
= vdc_port_match
,
1168 .probe
= vdc_port_probe
,
1169 .remove
= vdc_port_remove
,
1173 static int __init
vdc_init(void)
1177 sunvdc_wq
= alloc_workqueue("sunvdc", 0, 0);
1181 err
= register_blkdev(0, VDCBLK_NAME
);
1187 err
= vio_register_driver(&vdc_port_driver
);
1189 goto out_unregister_blkdev
;
1193 out_unregister_blkdev
:
1194 unregister_blkdev(vdc_major
, VDCBLK_NAME
);
1198 destroy_workqueue(sunvdc_wq
);
1202 static void __exit
vdc_exit(void)
1204 vio_unregister_driver(&vdc_port_driver
);
1205 unregister_blkdev(vdc_major
, VDCBLK_NAME
);
1206 destroy_workqueue(sunvdc_wq
);
1209 module_init(vdc_init
);
1210 module_exit(vdc_exit
);