1 // SPDX-License-Identifier: GPL-2.0-only
2 /* sunvdc.c: Sun LDOM Virtual Disk Client.
4 * Copyright (C) 2007, 2008 David S. Miller <davem@davemloft.net>
7 #include <linux/module.h>
8 #include <linux/kernel.h>
9 #include <linux/types.h>
10 #include <linux/blk-mq.h>
11 #include <linux/hdreg.h>
12 #include <linux/genhd.h>
13 #include <linux/cdrom.h>
14 #include <linux/slab.h>
15 #include <linux/spinlock.h>
16 #include <linux/completion.h>
17 #include <linux/delay.h>
18 #include <linux/init.h>
19 #include <linux/list.h>
20 #include <linux/scatterlist.h>
25 #define DRV_MODULE_NAME "sunvdc"
26 #define PFX DRV_MODULE_NAME ": "
27 #define DRV_MODULE_VERSION "1.2"
28 #define DRV_MODULE_RELDATE "November 24, 2014"
30 static char version
[] =
31 DRV_MODULE_NAME
".c:v" DRV_MODULE_VERSION
" (" DRV_MODULE_RELDATE
")\n";
32 MODULE_AUTHOR("David S. Miller (davem@davemloft.net)");
33 MODULE_DESCRIPTION("Sun LDOM virtual disk client driver");
34 MODULE_LICENSE("GPL");
35 MODULE_VERSION(DRV_MODULE_VERSION
);
37 #define VDC_TX_RING_SIZE 512
38 #define VDC_DEFAULT_BLK_SIZE 512
40 #define MAX_XFER_BLKS (128 * 1024)
41 #define MAX_XFER_SIZE (MAX_XFER_BLKS / VDC_DEFAULT_BLK_SIZE)
42 #define MAX_RING_COOKIES ((MAX_XFER_BLKS / PAGE_SIZE) + 2)
44 #define WAITING_FOR_LINK_UP 0x01
45 #define WAITING_FOR_TX_SPACE 0x02
46 #define WAITING_FOR_GEN_CMD 0x04
47 #define WAITING_FOR_ANY -1
49 #define VDC_MAX_RETRIES 10
51 static struct workqueue_struct
*sunvdc_wq
;
53 struct vdc_req_entry
{
58 struct vio_driver_state vio
;
62 struct vdc_completion
*cmp
;
66 struct vdc_req_entry rq_arr
[VDC_TX_RING_SIZE
];
68 unsigned long ring_cookies
;
75 struct delayed_work ldc_reset_timer_work
;
76 struct work_struct ldc_reset_work
;
78 /* The server fills these in for us in the disk attribute
87 struct blk_mq_tag_set tag_set
;
92 static void vdc_ldc_reset(struct vdc_port
*port
);
93 static void vdc_ldc_reset_work(struct work_struct
*work
);
94 static void vdc_ldc_reset_timer_work(struct work_struct
*work
);
96 static inline struct vdc_port
*to_vdc_port(struct vio_driver_state
*vio
)
98 return container_of(vio
, struct vdc_port
, vio
);
101 /* Ordered from largest major to lowest */
102 static struct vio_version vdc_versions
[] = {
103 { .major
= 1, .minor
= 2 },
104 { .major
= 1, .minor
= 1 },
105 { .major
= 1, .minor
= 0 },
108 static inline int vdc_version_supported(struct vdc_port
*port
,
109 u16 major
, u16 minor
)
111 return port
->vio
.ver
.major
== major
&& port
->vio
.ver
.minor
>= minor
;
114 #define VDCBLK_NAME "vdisk"
115 static int vdc_major
;
116 #define PARTITION_SHIFT 3
118 static inline u32
vdc_tx_dring_avail(struct vio_dring_state
*dr
)
120 return vio_dring_avail(dr
, VDC_TX_RING_SIZE
);
123 static int vdc_getgeo(struct block_device
*bdev
, struct hd_geometry
*geo
)
125 struct gendisk
*disk
= bdev
->bd_disk
;
126 sector_t nsect
= get_capacity(disk
);
127 sector_t cylinders
= nsect
;
131 sector_div(cylinders
, geo
->heads
* geo
->sectors
);
132 geo
->cylinders
= cylinders
;
133 if ((sector_t
)(geo
->cylinders
+ 1) * geo
->heads
* geo
->sectors
< nsect
)
134 geo
->cylinders
= 0xffff;
139 /* Add ioctl/CDROM_GET_CAPABILITY to support cdrom_id in udev
140 * when vdisk_mtype is VD_MEDIA_TYPE_CD or VD_MEDIA_TYPE_DVD.
141 * Needed to be able to install inside an ldom from an iso image.
143 static int vdc_ioctl(struct block_device
*bdev
, fmode_t mode
,
144 unsigned command
, unsigned long argument
)
147 struct gendisk
*disk
;
150 case CDROMMULTISESSION
:
151 pr_debug(PFX
"Multisession CDs not supported\n");
152 for (i
= 0; i
< sizeof(struct cdrom_multisession
); i
++)
153 if (put_user(0, (char __user
*)(argument
+ i
)))
157 case CDROM_GET_CAPABILITY
:
158 disk
= bdev
->bd_disk
;
160 if (bdev
->bd_disk
&& (disk
->flags
& GENHD_FL_CD
))
165 pr_debug(PFX
"ioctl %08x not supported\n", command
);
170 static const struct block_device_operations vdc_fops
= {
171 .owner
= THIS_MODULE
,
172 .getgeo
= vdc_getgeo
,
176 static void vdc_blk_queue_start(struct vdc_port
*port
)
178 struct vio_dring_state
*dr
= &port
->vio
.drings
[VIO_DRIVER_TX_RING
];
180 /* restart blk queue when ring is half emptied. also called after
181 * handshake completes, so check for initial handshake before we've
184 if (port
->disk
&& vdc_tx_dring_avail(dr
) * 100 / VDC_TX_RING_SIZE
>= 50)
185 blk_mq_start_stopped_hw_queues(port
->disk
->queue
, true);
188 static void vdc_finish(struct vio_driver_state
*vio
, int err
, int waiting_for
)
191 (waiting_for
== -1 ||
192 vio
->cmp
->waiting_for
== waiting_for
)) {
194 complete(&vio
->cmp
->com
);
199 static void vdc_handshake_complete(struct vio_driver_state
*vio
)
201 struct vdc_port
*port
= to_vdc_port(vio
);
203 cancel_delayed_work(&port
->ldc_reset_timer_work
);
204 vdc_finish(vio
, 0, WAITING_FOR_LINK_UP
);
205 vdc_blk_queue_start(port
);
208 static int vdc_handle_unknown(struct vdc_port
*port
, void *arg
)
210 struct vio_msg_tag
*pkt
= arg
;
212 printk(KERN_ERR PFX
"Received unknown msg [%02x:%02x:%04x:%08x]\n",
213 pkt
->type
, pkt
->stype
, pkt
->stype_env
, pkt
->sid
);
214 printk(KERN_ERR PFX
"Resetting connection.\n");
216 ldc_disconnect(port
->vio
.lp
);
221 static int vdc_send_attr(struct vio_driver_state
*vio
)
223 struct vdc_port
*port
= to_vdc_port(vio
);
224 struct vio_disk_attr_info pkt
;
226 memset(&pkt
, 0, sizeof(pkt
));
228 pkt
.tag
.type
= VIO_TYPE_CTRL
;
229 pkt
.tag
.stype
= VIO_SUBTYPE_INFO
;
230 pkt
.tag
.stype_env
= VIO_ATTR_INFO
;
231 pkt
.tag
.sid
= vio_send_sid(vio
);
233 pkt
.xfer_mode
= VIO_DRING_MODE
;
234 pkt
.vdisk_block_size
= port
->vdisk_block_size
;
235 pkt
.max_xfer_size
= port
->max_xfer_size
;
237 viodbg(HS
, "SEND ATTR xfer_mode[0x%x] blksz[%u] max_xfer[%llu]\n",
238 pkt
.xfer_mode
, pkt
.vdisk_block_size
, pkt
.max_xfer_size
);
240 return vio_ldc_send(&port
->vio
, &pkt
, sizeof(pkt
));
243 static int vdc_handle_attr(struct vio_driver_state
*vio
, void *arg
)
245 struct vdc_port
*port
= to_vdc_port(vio
);
246 struct vio_disk_attr_info
*pkt
= arg
;
248 viodbg(HS
, "GOT ATTR stype[0x%x] ops[%llx] disk_size[%llu] disk_type[%x] "
249 "mtype[0x%x] xfer_mode[0x%x] blksz[%u] max_xfer[%llu]\n",
250 pkt
->tag
.stype
, pkt
->operations
,
251 pkt
->vdisk_size
, pkt
->vdisk_type
, pkt
->vdisk_mtype
,
252 pkt
->xfer_mode
, pkt
->vdisk_block_size
,
255 if (pkt
->tag
.stype
== VIO_SUBTYPE_ACK
) {
256 switch (pkt
->vdisk_type
) {
257 case VD_DISK_TYPE_DISK
:
258 case VD_DISK_TYPE_SLICE
:
262 printk(KERN_ERR PFX
"%s: Bogus vdisk_type 0x%x\n",
263 vio
->name
, pkt
->vdisk_type
);
267 if (pkt
->vdisk_block_size
> port
->vdisk_block_size
) {
268 printk(KERN_ERR PFX
"%s: BLOCK size increased "
271 port
->vdisk_block_size
, pkt
->vdisk_block_size
);
275 port
->operations
= pkt
->operations
;
276 port
->vdisk_type
= pkt
->vdisk_type
;
277 if (vdc_version_supported(port
, 1, 1)) {
278 port
->vdisk_size
= pkt
->vdisk_size
;
279 port
->vdisk_mtype
= pkt
->vdisk_mtype
;
281 if (pkt
->max_xfer_size
< port
->max_xfer_size
)
282 port
->max_xfer_size
= pkt
->max_xfer_size
;
283 port
->vdisk_block_size
= pkt
->vdisk_block_size
;
285 port
->vdisk_phys_blksz
= VDC_DEFAULT_BLK_SIZE
;
286 if (vdc_version_supported(port
, 1, 2))
287 port
->vdisk_phys_blksz
= pkt
->phys_block_size
;
291 printk(KERN_ERR PFX
"%s: Attribute NACK\n", vio
->name
);
297 static void vdc_end_special(struct vdc_port
*port
, struct vio_disk_desc
*desc
)
299 int err
= desc
->status
;
301 vdc_finish(&port
->vio
, -err
, WAITING_FOR_GEN_CMD
);
304 static void vdc_end_one(struct vdc_port
*port
, struct vio_dring_state
*dr
,
307 struct vio_disk_desc
*desc
= vio_dring_entry(dr
, index
);
308 struct vdc_req_entry
*rqe
= &port
->rq_arr
[index
];
311 if (unlikely(desc
->hdr
.state
!= VIO_DESC_DONE
))
314 ldc_unmap(port
->vio
.lp
, desc
->cookies
, desc
->ncookies
);
315 desc
->hdr
.state
= VIO_DESC_FREE
;
316 dr
->cons
= vio_dring_next(dr
, index
);
320 vdc_end_special(port
, desc
);
326 blk_mq_end_request(req
, desc
->status
? BLK_STS_IOERR
: 0);
328 vdc_blk_queue_start(port
);
331 static int vdc_ack(struct vdc_port
*port
, void *msgbuf
)
333 struct vio_dring_state
*dr
= &port
->vio
.drings
[VIO_DRIVER_TX_RING
];
334 struct vio_dring_data
*pkt
= msgbuf
;
336 if (unlikely(pkt
->dring_ident
!= dr
->ident
||
337 pkt
->start_idx
!= pkt
->end_idx
||
338 pkt
->start_idx
>= VDC_TX_RING_SIZE
))
341 vdc_end_one(port
, dr
, pkt
->start_idx
);
346 static int vdc_nack(struct vdc_port
*port
, void *msgbuf
)
348 /* XXX Implement me XXX */
352 static void vdc_event(void *arg
, int event
)
354 struct vdc_port
*port
= arg
;
355 struct vio_driver_state
*vio
= &port
->vio
;
359 spin_lock_irqsave(&vio
->lock
, flags
);
361 if (unlikely(event
== LDC_EVENT_RESET
)) {
362 vio_link_state_change(vio
, event
);
363 queue_work(sunvdc_wq
, &port
->ldc_reset_work
);
367 if (unlikely(event
== LDC_EVENT_UP
)) {
368 vio_link_state_change(vio
, event
);
372 if (unlikely(event
!= LDC_EVENT_DATA_READY
)) {
373 pr_warn(PFX
"Unexpected LDC event %d\n", event
);
380 struct vio_msg_tag tag
;
384 err
= ldc_read(vio
->lp
, &msgbuf
, sizeof(msgbuf
));
385 if (unlikely(err
< 0)) {
386 if (err
== -ECONNRESET
)
392 viodbg(DATA
, "TAG [%02x:%02x:%04x:%08x]\n",
395 msgbuf
.tag
.stype_env
,
397 err
= vio_validate_sid(vio
, &msgbuf
.tag
);
401 if (likely(msgbuf
.tag
.type
== VIO_TYPE_DATA
)) {
402 if (msgbuf
.tag
.stype
== VIO_SUBTYPE_ACK
)
403 err
= vdc_ack(port
, &msgbuf
);
404 else if (msgbuf
.tag
.stype
== VIO_SUBTYPE_NACK
)
405 err
= vdc_nack(port
, &msgbuf
);
407 err
= vdc_handle_unknown(port
, &msgbuf
);
408 } else if (msgbuf
.tag
.type
== VIO_TYPE_CTRL
) {
409 err
= vio_control_pkt_engine(vio
, &msgbuf
);
411 err
= vdc_handle_unknown(port
, &msgbuf
);
417 vdc_finish(&port
->vio
, err
, WAITING_FOR_ANY
);
419 spin_unlock_irqrestore(&vio
->lock
, flags
);
422 static int __vdc_tx_trigger(struct vdc_port
*port
)
424 struct vio_dring_state
*dr
= &port
->vio
.drings
[VIO_DRIVER_TX_RING
];
425 struct vio_dring_data hdr
= {
427 .type
= VIO_TYPE_DATA
,
428 .stype
= VIO_SUBTYPE_INFO
,
429 .stype_env
= VIO_DRING_DATA
,
430 .sid
= vio_send_sid(&port
->vio
),
432 .dring_ident
= dr
->ident
,
433 .start_idx
= dr
->prod
,
439 hdr
.seq
= dr
->snd_nxt
;
442 err
= vio_ldc_send(&port
->vio
, &hdr
, sizeof(hdr
));
448 if ((delay
<<= 1) > 128)
450 if (retries
++ > VDC_MAX_RETRIES
)
452 } while (err
== -EAGAIN
);
454 if (err
== -ENOTCONN
)
459 static int __send_request(struct request
*req
)
461 struct vdc_port
*port
= req
->rq_disk
->private_data
;
462 struct vio_dring_state
*dr
= &port
->vio
.drings
[VIO_DRIVER_TX_RING
];
463 struct scatterlist sg
[MAX_RING_COOKIES
];
464 struct vdc_req_entry
*rqe
;
465 struct vio_disk_desc
*desc
;
466 unsigned int map_perm
;
471 if (WARN_ON(port
->ring_cookies
> MAX_RING_COOKIES
))
474 map_perm
= LDC_MAP_SHADOW
| LDC_MAP_DIRECT
| LDC_MAP_IO
;
476 if (rq_data_dir(req
) == READ
) {
477 map_perm
|= LDC_MAP_W
;
480 map_perm
|= LDC_MAP_R
;
484 sg_init_table(sg
, port
->ring_cookies
);
485 nsg
= blk_rq_map_sg(req
->q
, req
, sg
);
488 for (i
= 0; i
< nsg
; i
++)
491 desc
= vio_dring_cur(dr
);
493 err
= ldc_map_sg(port
->vio
.lp
, sg
, nsg
,
494 desc
->cookies
, port
->ring_cookies
,
497 printk(KERN_ERR PFX
"ldc_map_sg() failure, err=%d.\n", err
);
501 rqe
= &port
->rq_arr
[dr
->prod
];
504 desc
->hdr
.ack
= VIO_ACK_ENABLE
;
505 desc
->req_id
= port
->req_id
;
506 desc
->operation
= op
;
507 if (port
->vdisk_type
== VD_DISK_TYPE_DISK
) {
513 desc
->offset
= (blk_rq_pos(req
) << 9) / port
->vdisk_block_size
;
515 desc
->ncookies
= err
;
517 /* This has to be a non-SMP write barrier because we are writing
518 * to memory which is shared with the peer LDOM.
521 desc
->hdr
.state
= VIO_DESC_READY
;
523 err
= __vdc_tx_trigger(port
);
525 printk(KERN_ERR PFX
"vdc_tx_trigger() failure, err=%d\n", err
);
528 dr
->prod
= vio_dring_next(dr
, dr
->prod
);
534 static blk_status_t
vdc_queue_rq(struct blk_mq_hw_ctx
*hctx
,
535 const struct blk_mq_queue_data
*bd
)
537 struct vdc_port
*port
= hctx
->queue
->queuedata
;
538 struct vio_dring_state
*dr
;
541 dr
= &port
->vio
.drings
[VIO_DRIVER_TX_RING
];
543 blk_mq_start_request(bd
->rq
);
545 spin_lock_irqsave(&port
->vio
.lock
, flags
);
548 * Doing drain, just end the request in error
550 if (unlikely(port
->drain
)) {
551 spin_unlock_irqrestore(&port
->vio
.lock
, flags
);
552 return BLK_STS_IOERR
;
555 if (unlikely(vdc_tx_dring_avail(dr
) < 1)) {
556 spin_unlock_irqrestore(&port
->vio
.lock
, flags
);
557 blk_mq_stop_hw_queue(hctx
);
558 return BLK_STS_DEV_RESOURCE
;
561 if (__send_request(bd
->rq
) < 0) {
562 spin_unlock_irqrestore(&port
->vio
.lock
, flags
);
563 return BLK_STS_IOERR
;
566 spin_unlock_irqrestore(&port
->vio
.lock
, flags
);
570 static int generic_request(struct vdc_port
*port
, u8 op
, void *buf
, int len
)
572 struct vio_dring_state
*dr
;
573 struct vio_completion comp
;
574 struct vio_disk_desc
*desc
;
575 unsigned int map_perm
;
580 if (!(((u64
)1 << (u64
)op
) & port
->operations
))
595 op_len
= sizeof(u32
);
596 map_perm
= LDC_MAP_W
;
600 op_len
= sizeof(u32
);
601 map_perm
= LDC_MAP_R
;
605 op_len
= sizeof(struct vio_disk_vtoc
);
606 map_perm
= LDC_MAP_W
;
610 op_len
= sizeof(struct vio_disk_vtoc
);
611 map_perm
= LDC_MAP_R
;
614 case VD_OP_GET_DISKGEOM
:
615 op_len
= sizeof(struct vio_disk_geom
);
616 map_perm
= LDC_MAP_W
;
619 case VD_OP_SET_DISKGEOM
:
620 op_len
= sizeof(struct vio_disk_geom
);
621 map_perm
= LDC_MAP_R
;
626 map_perm
= LDC_MAP_RW
;
629 case VD_OP_GET_DEVID
:
630 op_len
= sizeof(struct vio_disk_devid
);
631 map_perm
= LDC_MAP_W
;
639 map_perm
|= LDC_MAP_SHADOW
| LDC_MAP_DIRECT
| LDC_MAP_IO
;
641 op_len
= (op_len
+ 7) & ~7;
642 req_buf
= kzalloc(op_len
, GFP_KERNEL
);
649 if (map_perm
& LDC_MAP_R
)
650 memcpy(req_buf
, buf
, len
);
652 spin_lock_irqsave(&port
->vio
.lock
, flags
);
654 dr
= &port
->vio
.drings
[VIO_DRIVER_TX_RING
];
656 /* XXX If we want to use this code generically we have to
657 * XXX handle TX ring exhaustion etc.
659 desc
= vio_dring_cur(dr
);
661 err
= ldc_map_single(port
->vio
.lp
, req_buf
, op_len
,
662 desc
->cookies
, port
->ring_cookies
,
665 spin_unlock_irqrestore(&port
->vio
.lock
, flags
);
670 init_completion(&comp
.com
);
671 comp
.waiting_for
= WAITING_FOR_GEN_CMD
;
672 port
->vio
.cmp
= &comp
;
674 desc
->hdr
.ack
= VIO_ACK_ENABLE
;
675 desc
->req_id
= port
->req_id
;
676 desc
->operation
= op
;
681 desc
->ncookies
= err
;
683 /* This has to be a non-SMP write barrier because we are writing
684 * to memory which is shared with the peer LDOM.
687 desc
->hdr
.state
= VIO_DESC_READY
;
689 err
= __vdc_tx_trigger(port
);
692 dr
->prod
= vio_dring_next(dr
, dr
->prod
);
693 spin_unlock_irqrestore(&port
->vio
.lock
, flags
);
695 wait_for_completion(&comp
.com
);
698 port
->vio
.cmp
= NULL
;
699 spin_unlock_irqrestore(&port
->vio
.lock
, flags
);
702 if (map_perm
& LDC_MAP_W
)
703 memcpy(buf
, req_buf
, len
);
710 static int vdc_alloc_tx_ring(struct vdc_port
*port
)
712 struct vio_dring_state
*dr
= &port
->vio
.drings
[VIO_DRIVER_TX_RING
];
713 unsigned long len
, entry_size
;
717 entry_size
= sizeof(struct vio_disk_desc
) +
718 (sizeof(struct ldc_trans_cookie
) * port
->ring_cookies
);
719 len
= (VDC_TX_RING_SIZE
* entry_size
);
721 ncookies
= VIO_MAX_RING_COOKIES
;
722 dring
= ldc_alloc_exp_dring(port
->vio
.lp
, len
,
723 dr
->cookies
, &ncookies
,
728 return PTR_ERR(dring
);
731 dr
->entry_size
= entry_size
;
732 dr
->num_entries
= VDC_TX_RING_SIZE
;
733 dr
->prod
= dr
->cons
= 0;
734 dr
->pending
= VDC_TX_RING_SIZE
;
735 dr
->ncookies
= ncookies
;
740 static void vdc_free_tx_ring(struct vdc_port
*port
)
742 struct vio_dring_state
*dr
= &port
->vio
.drings
[VIO_DRIVER_TX_RING
];
745 ldc_free_exp_dring(port
->vio
.lp
, dr
->base
,
746 (dr
->entry_size
* dr
->num_entries
),
747 dr
->cookies
, dr
->ncookies
);
756 static int vdc_port_up(struct vdc_port
*port
)
758 struct vio_completion comp
;
760 init_completion(&comp
.com
);
762 comp
.waiting_for
= WAITING_FOR_LINK_UP
;
763 port
->vio
.cmp
= &comp
;
765 vio_port_up(&port
->vio
);
766 wait_for_completion(&comp
.com
);
770 static void vdc_port_down(struct vdc_port
*port
)
772 ldc_disconnect(port
->vio
.lp
);
773 ldc_unbind(port
->vio
.lp
);
774 vdc_free_tx_ring(port
);
775 vio_ldc_free(&port
->vio
);
778 static const struct blk_mq_ops vdc_mq_ops
= {
779 .queue_rq
= vdc_queue_rq
,
782 static void cleanup_queue(struct request_queue
*q
)
784 struct vdc_port
*port
= q
->queuedata
;
786 blk_cleanup_queue(q
);
787 blk_mq_free_tag_set(&port
->tag_set
);
790 static struct request_queue
*init_queue(struct vdc_port
*port
)
792 struct request_queue
*q
;
794 q
= blk_mq_init_sq_queue(&port
->tag_set
, &vdc_mq_ops
, VDC_TX_RING_SIZE
,
795 BLK_MQ_F_SHOULD_MERGE
);
803 static int probe_disk(struct vdc_port
*port
)
805 struct request_queue
*q
;
809 err
= vdc_port_up(port
);
813 /* Using version 1.2 means vdisk_phys_blksz should be set unless the
814 * disk is reserved by another system.
816 if (vdc_version_supported(port
, 1, 2) && !port
->vdisk_phys_blksz
)
819 if (vdc_version_supported(port
, 1, 1)) {
820 /* vdisk_size should be set during the handshake, if it wasn't
821 * then the underlying disk is reserved by another system
823 if (port
->vdisk_size
== -1)
826 struct vio_disk_geom geom
;
828 err
= generic_request(port
, VD_OP_GET_DISKGEOM
,
829 &geom
, sizeof(geom
));
831 printk(KERN_ERR PFX
"VD_OP_GET_DISKGEOM returns "
835 port
->vdisk_size
= ((u64
)geom
.num_cyl
*
840 q
= init_queue(port
);
842 printk(KERN_ERR PFX
"%s: Could not allocate queue.\n",
846 g
= alloc_disk(1 << PARTITION_SHIFT
);
848 printk(KERN_ERR PFX
"%s: Could not allocate gendisk.\n",
856 /* Each segment in a request is up to an aligned page in size. */
857 blk_queue_segment_boundary(q
, PAGE_SIZE
- 1);
858 blk_queue_max_segment_size(q
, PAGE_SIZE
);
860 blk_queue_max_segments(q
, port
->ring_cookies
);
861 blk_queue_max_hw_sectors(q
, port
->max_xfer_size
);
862 g
->major
= vdc_major
;
863 g
->first_minor
= port
->vio
.vdev
->dev_no
<< PARTITION_SHIFT
;
864 strcpy(g
->disk_name
, port
->disk_name
);
868 g
->private_data
= port
;
870 set_capacity(g
, port
->vdisk_size
);
872 if (vdc_version_supported(port
, 1, 1)) {
873 switch (port
->vdisk_mtype
) {
874 case VD_MEDIA_TYPE_CD
:
875 pr_info(PFX
"Virtual CDROM %s\n", port
->disk_name
);
876 g
->flags
|= GENHD_FL_CD
;
877 g
->flags
|= GENHD_FL_REMOVABLE
;
881 case VD_MEDIA_TYPE_DVD
:
882 pr_info(PFX
"Virtual DVD %s\n", port
->disk_name
);
883 g
->flags
|= GENHD_FL_CD
;
884 g
->flags
|= GENHD_FL_REMOVABLE
;
888 case VD_MEDIA_TYPE_FIXED
:
889 pr_info(PFX
"Virtual Hard disk %s\n", port
->disk_name
);
894 blk_queue_physical_block_size(q
, port
->vdisk_phys_blksz
);
896 pr_info(PFX
"%s: %u sectors (%u MB) protocol %d.%d\n",
898 port
->vdisk_size
, (port
->vdisk_size
>> (20 - 9)),
899 port
->vio
.ver
.major
, port
->vio
.ver
.minor
);
901 device_add_disk(&port
->vio
.vdev
->dev
, g
, NULL
);
906 static struct ldc_channel_config vdc_ldc_cfg
= {
909 .mode
= LDC_MODE_UNRELIABLE
,
912 static struct vio_driver_ops vdc_vio_ops
= {
913 .send_attr
= vdc_send_attr
,
914 .handle_attr
= vdc_handle_attr
,
915 .handshake_complete
= vdc_handshake_complete
,
918 static void print_version(void)
920 static int version_printed
;
922 if (version_printed
++ == 0)
923 printk(KERN_INFO
"%s", version
);
926 struct vdc_check_port_data
{
931 static int vdc_device_probed(struct device
*dev
, void *arg
)
933 struct vio_dev
*vdev
= to_vio_dev(dev
);
934 struct vdc_check_port_data
*port_data
;
936 port_data
= (struct vdc_check_port_data
*)arg
;
938 if ((vdev
->dev_no
== port_data
->dev_no
) &&
939 (!(strcmp((char *)&vdev
->type
, port_data
->type
))) &&
940 dev_get_drvdata(dev
)) {
941 /* This device has already been configured
942 * by vdc_port_probe()
950 /* Determine whether the VIO device is part of an mpgroup
951 * by locating all the virtual-device-port nodes associated
952 * with the parent virtual-device node for the VIO device
953 * and checking whether any of these nodes are vdc-ports
954 * which have already been configured.
956 * Returns true if this device is part of an mpgroup and has
957 * already been probed.
959 static bool vdc_port_mpgroup_check(struct vio_dev
*vdev
)
961 struct vdc_check_port_data port_data
;
964 port_data
.dev_no
= vdev
->dev_no
;
965 port_data
.type
= (char *)&vdev
->type
;
967 dev
= device_find_child(vdev
->dev
.parent
, &port_data
,
976 static int vdc_port_probe(struct vio_dev
*vdev
, const struct vio_device_id
*id
)
978 struct mdesc_handle
*hp
;
979 struct vdc_port
*port
;
981 const u64
*ldc_timeout
;
988 if ((vdev
->dev_no
<< PARTITION_SHIFT
) & ~(u64
)MINORMASK
) {
989 printk(KERN_ERR PFX
"Port id [%llu] too large.\n",
991 goto err_out_release_mdesc
;
994 /* Check if this device is part of an mpgroup */
995 if (vdc_port_mpgroup_check(vdev
)) {
997 "VIO: Ignoring extra vdisk port %s",
998 dev_name(&vdev
->dev
));
999 goto err_out_release_mdesc
;
1002 port
= kzalloc(sizeof(*port
), GFP_KERNEL
);
1005 printk(KERN_ERR PFX
"Cannot allocate vdc_port.\n");
1006 goto err_out_release_mdesc
;
1009 if (vdev
->dev_no
>= 26)
1010 snprintf(port
->disk_name
, sizeof(port
->disk_name
),
1012 'a' + ((int)vdev
->dev_no
/ 26) - 1,
1013 'a' + ((int)vdev
->dev_no
% 26));
1015 snprintf(port
->disk_name
, sizeof(port
->disk_name
),
1016 VDCBLK_NAME
"%c", 'a' + ((int)vdev
->dev_no
% 26));
1017 port
->vdisk_size
= -1;
1019 /* Actual wall time may be double due to do_generic_file_read() doing
1020 * a readahead I/O first, and once that fails it will try to read a
1023 ldc_timeout
= mdesc_get_property(hp
, vdev
->mp
, "vdc-timeout", NULL
);
1024 port
->ldc_timeout
= ldc_timeout
? *ldc_timeout
: 0;
1025 INIT_DELAYED_WORK(&port
->ldc_reset_timer_work
, vdc_ldc_reset_timer_work
);
1026 INIT_WORK(&port
->ldc_reset_work
, vdc_ldc_reset_work
);
1028 err
= vio_driver_init(&port
->vio
, vdev
, VDEV_DISK
,
1029 vdc_versions
, ARRAY_SIZE(vdc_versions
),
1030 &vdc_vio_ops
, port
->disk_name
);
1032 goto err_out_free_port
;
1034 port
->vdisk_block_size
= VDC_DEFAULT_BLK_SIZE
;
1035 port
->max_xfer_size
= MAX_XFER_SIZE
;
1036 port
->ring_cookies
= MAX_RING_COOKIES
;
1038 err
= vio_ldc_alloc(&port
->vio
, &vdc_ldc_cfg
, port
);
1040 goto err_out_free_port
;
1042 err
= vdc_alloc_tx_ring(port
);
1044 goto err_out_free_ldc
;
1046 err
= probe_disk(port
);
1048 goto err_out_free_tx_ring
;
1050 /* Note that the device driver_data is used to determine
1051 * whether the port has been probed.
1053 dev_set_drvdata(&vdev
->dev
, port
);
1059 err_out_free_tx_ring
:
1060 vdc_free_tx_ring(port
);
1063 vio_ldc_free(&port
->vio
);
1068 err_out_release_mdesc
:
1073 static int vdc_port_remove(struct vio_dev
*vdev
)
1075 struct vdc_port
*port
= dev_get_drvdata(&vdev
->dev
);
1078 blk_mq_stop_hw_queues(port
->disk
->queue
);
1080 flush_work(&port
->ldc_reset_work
);
1081 cancel_delayed_work_sync(&port
->ldc_reset_timer_work
);
1082 del_timer_sync(&port
->vio
.timer
);
1084 del_gendisk(port
->disk
);
1085 cleanup_queue(port
->disk
->queue
);
1086 put_disk(port
->disk
);
1089 vdc_free_tx_ring(port
);
1090 vio_ldc_free(&port
->vio
);
1092 dev_set_drvdata(&vdev
->dev
, NULL
);
1099 static void vdc_requeue_inflight(struct vdc_port
*port
)
1101 struct vio_dring_state
*dr
= &port
->vio
.drings
[VIO_DRIVER_TX_RING
];
1104 for (idx
= dr
->cons
; idx
!= dr
->prod
; idx
= vio_dring_next(dr
, idx
)) {
1105 struct vio_disk_desc
*desc
= vio_dring_entry(dr
, idx
);
1106 struct vdc_req_entry
*rqe
= &port
->rq_arr
[idx
];
1107 struct request
*req
;
1109 ldc_unmap(port
->vio
.lp
, desc
->cookies
, desc
->ncookies
);
1110 desc
->hdr
.state
= VIO_DESC_FREE
;
1111 dr
->cons
= vio_dring_next(dr
, idx
);
1115 vdc_end_special(port
, desc
);
1120 blk_mq_requeue_request(req
, false);
1124 static void vdc_queue_drain(struct vdc_port
*port
)
1126 struct request_queue
*q
= port
->disk
->queue
;
1129 * Mark the queue as draining, then freeze/quiesce to ensure
1130 * that all existing requests are seen in ->queue_rq() and killed
1133 spin_unlock_irq(&port
->vio
.lock
);
1135 blk_mq_freeze_queue(q
);
1136 blk_mq_quiesce_queue(q
);
1138 spin_lock_irq(&port
->vio
.lock
);
1140 blk_mq_unquiesce_queue(q
);
1141 blk_mq_unfreeze_queue(q
);
1144 static void vdc_ldc_reset_timer_work(struct work_struct
*work
)
1146 struct vdc_port
*port
;
1147 struct vio_driver_state
*vio
;
1149 port
= container_of(work
, struct vdc_port
, ldc_reset_timer_work
.work
);
1152 spin_lock_irq(&vio
->lock
);
1153 if (!(port
->vio
.hs_state
& VIO_HS_COMPLETE
)) {
1154 pr_warn(PFX
"%s ldc down %llu seconds, draining queue\n",
1155 port
->disk_name
, port
->ldc_timeout
);
1156 vdc_queue_drain(port
);
1157 vdc_blk_queue_start(port
);
1159 spin_unlock_irq(&vio
->lock
);
1162 static void vdc_ldc_reset_work(struct work_struct
*work
)
1164 struct vdc_port
*port
;
1165 struct vio_driver_state
*vio
;
1166 unsigned long flags
;
1168 port
= container_of(work
, struct vdc_port
, ldc_reset_work
);
1171 spin_lock_irqsave(&vio
->lock
, flags
);
1172 vdc_ldc_reset(port
);
1173 spin_unlock_irqrestore(&vio
->lock
, flags
);
1176 static void vdc_ldc_reset(struct vdc_port
*port
)
1180 assert_spin_locked(&port
->vio
.lock
);
1182 pr_warn(PFX
"%s ldc link reset\n", port
->disk_name
);
1183 blk_mq_stop_hw_queues(port
->disk
->queue
);
1184 vdc_requeue_inflight(port
);
1185 vdc_port_down(port
);
1187 err
= vio_ldc_alloc(&port
->vio
, &vdc_ldc_cfg
, port
);
1189 pr_err(PFX
"%s vio_ldc_alloc:%d\n", port
->disk_name
, err
);
1193 err
= vdc_alloc_tx_ring(port
);
1195 pr_err(PFX
"%s vio_alloc_tx_ring:%d\n", port
->disk_name
, err
);
1199 if (port
->ldc_timeout
)
1200 mod_delayed_work(system_wq
, &port
->ldc_reset_timer_work
,
1201 round_jiffies(jiffies
+ HZ
* port
->ldc_timeout
));
1202 mod_timer(&port
->vio
.timer
, round_jiffies(jiffies
+ HZ
));
1206 vio_ldc_free(&port
->vio
);
1209 static const struct vio_device_id vdc_port_match
[] = {
1215 MODULE_DEVICE_TABLE(vio
, vdc_port_match
);
1217 static struct vio_driver vdc_port_driver
= {
1218 .id_table
= vdc_port_match
,
1219 .probe
= vdc_port_probe
,
1220 .remove
= vdc_port_remove
,
1224 static int __init
vdc_init(void)
1228 sunvdc_wq
= alloc_workqueue("sunvdc", 0, 0);
1232 err
= register_blkdev(0, VDCBLK_NAME
);
1238 err
= vio_register_driver(&vdc_port_driver
);
1240 goto out_unregister_blkdev
;
1244 out_unregister_blkdev
:
1245 unregister_blkdev(vdc_major
, VDCBLK_NAME
);
1249 destroy_workqueue(sunvdc_wq
);
1253 static void __exit
vdc_exit(void)
1255 vio_unregister_driver(&vdc_port_driver
);
1256 unregister_blkdev(vdc_major
, VDCBLK_NAME
);
1257 destroy_workqueue(sunvdc_wq
);
1260 module_init(vdc_init
);
1261 module_exit(vdc_exit
);