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/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 #define VDC_MAX_RETRIES 10
50 static struct workqueue_struct
*sunvdc_wq
;
52 struct vdc_req_entry
{
57 struct vio_driver_state vio
;
61 struct vdc_completion
*cmp
;
65 struct vdc_req_entry rq_arr
[VDC_TX_RING_SIZE
];
67 unsigned long ring_cookies
;
74 struct delayed_work ldc_reset_timer_work
;
75 struct work_struct ldc_reset_work
;
77 /* The server fills these in for us in the disk attribute
86 struct blk_mq_tag_set tag_set
;
91 static void vdc_ldc_reset(struct vdc_port
*port
);
92 static void vdc_ldc_reset_work(struct work_struct
*work
);
93 static void vdc_ldc_reset_timer_work(struct work_struct
*work
);
95 static inline struct vdc_port
*to_vdc_port(struct vio_driver_state
*vio
)
97 return container_of(vio
, struct vdc_port
, vio
);
100 /* Ordered from largest major to lowest */
101 static struct vio_version vdc_versions
[] = {
102 { .major
= 1, .minor
= 2 },
103 { .major
= 1, .minor
= 1 },
104 { .major
= 1, .minor
= 0 },
107 static inline int vdc_version_supported(struct vdc_port
*port
,
108 u16 major
, u16 minor
)
110 return port
->vio
.ver
.major
== major
&& port
->vio
.ver
.minor
>= minor
;
113 #define VDCBLK_NAME "vdisk"
114 static int vdc_major
;
115 #define PARTITION_SHIFT 3
117 static inline u32
vdc_tx_dring_avail(struct vio_dring_state
*dr
)
119 return vio_dring_avail(dr
, VDC_TX_RING_SIZE
);
122 static int vdc_getgeo(struct block_device
*bdev
, struct hd_geometry
*geo
)
124 struct gendisk
*disk
= bdev
->bd_disk
;
125 sector_t nsect
= get_capacity(disk
);
126 sector_t cylinders
= nsect
;
130 sector_div(cylinders
, geo
->heads
* geo
->sectors
);
131 geo
->cylinders
= cylinders
;
132 if ((sector_t
)(geo
->cylinders
+ 1) * geo
->heads
* geo
->sectors
< nsect
)
133 geo
->cylinders
= 0xffff;
138 /* Add ioctl/CDROM_GET_CAPABILITY to support cdrom_id in udev
139 * when vdisk_mtype is VD_MEDIA_TYPE_CD or VD_MEDIA_TYPE_DVD.
140 * Needed to be able to install inside an ldom from an iso image.
142 static int vdc_ioctl(struct block_device
*bdev
, blk_mode_t mode
,
143 unsigned command
, unsigned long argument
)
145 struct vdc_port
*port
= bdev
->bd_disk
->private_data
;
149 case CDROMMULTISESSION
:
150 pr_debug(PFX
"Multisession CDs not supported\n");
151 for (i
= 0; i
< sizeof(struct cdrom_multisession
); i
++)
152 if (put_user(0, (char __user
*)(argument
+ i
)))
156 case CDROM_GET_CAPABILITY
:
157 if (!vdc_version_supported(port
, 1, 1))
159 switch (port
->vdisk_mtype
) {
160 case VD_MEDIA_TYPE_CD
:
161 case VD_MEDIA_TYPE_DVD
:
167 pr_debug(PFX
"ioctl %08x not supported\n", command
);
172 static const struct block_device_operations vdc_fops
= {
173 .owner
= THIS_MODULE
,
174 .getgeo
= vdc_getgeo
,
176 .compat_ioctl
= blkdev_compat_ptr_ioctl
,
179 static void vdc_blk_queue_start(struct vdc_port
*port
)
181 struct vio_dring_state
*dr
= &port
->vio
.drings
[VIO_DRIVER_TX_RING
];
183 /* restart blk queue when ring is half emptied. also called after
184 * handshake completes, so check for initial handshake before we've
187 if (port
->disk
&& vdc_tx_dring_avail(dr
) * 100 / VDC_TX_RING_SIZE
>= 50)
188 blk_mq_start_stopped_hw_queues(port
->disk
->queue
, true);
191 static void vdc_finish(struct vio_driver_state
*vio
, int err
, int waiting_for
)
194 (waiting_for
== -1 ||
195 vio
->cmp
->waiting_for
== waiting_for
)) {
197 complete(&vio
->cmp
->com
);
202 static void vdc_handshake_complete(struct vio_driver_state
*vio
)
204 struct vdc_port
*port
= to_vdc_port(vio
);
206 cancel_delayed_work(&port
->ldc_reset_timer_work
);
207 vdc_finish(vio
, 0, WAITING_FOR_LINK_UP
);
208 vdc_blk_queue_start(port
);
211 static int vdc_handle_unknown(struct vdc_port
*port
, void *arg
)
213 struct vio_msg_tag
*pkt
= arg
;
215 printk(KERN_ERR PFX
"Received unknown msg [%02x:%02x:%04x:%08x]\n",
216 pkt
->type
, pkt
->stype
, pkt
->stype_env
, pkt
->sid
);
217 printk(KERN_ERR PFX
"Resetting connection.\n");
219 ldc_disconnect(port
->vio
.lp
);
224 static int vdc_send_attr(struct vio_driver_state
*vio
)
226 struct vdc_port
*port
= to_vdc_port(vio
);
227 struct vio_disk_attr_info pkt
;
229 memset(&pkt
, 0, sizeof(pkt
));
231 pkt
.tag
.type
= VIO_TYPE_CTRL
;
232 pkt
.tag
.stype
= VIO_SUBTYPE_INFO
;
233 pkt
.tag
.stype_env
= VIO_ATTR_INFO
;
234 pkt
.tag
.sid
= vio_send_sid(vio
);
236 pkt
.xfer_mode
= VIO_DRING_MODE
;
237 pkt
.vdisk_block_size
= port
->vdisk_block_size
;
238 pkt
.max_xfer_size
= port
->max_xfer_size
;
240 viodbg(HS
, "SEND ATTR xfer_mode[0x%x] blksz[%u] max_xfer[%llu]\n",
241 pkt
.xfer_mode
, pkt
.vdisk_block_size
, pkt
.max_xfer_size
);
243 return vio_ldc_send(&port
->vio
, &pkt
, sizeof(pkt
));
246 static int vdc_handle_attr(struct vio_driver_state
*vio
, void *arg
)
248 struct vdc_port
*port
= to_vdc_port(vio
);
249 struct vio_disk_attr_info
*pkt
= arg
;
251 viodbg(HS
, "GOT ATTR stype[0x%x] ops[%llx] disk_size[%llu] disk_type[%x] "
252 "mtype[0x%x] xfer_mode[0x%x] blksz[%u] max_xfer[%llu]\n",
253 pkt
->tag
.stype
, pkt
->operations
,
254 pkt
->vdisk_size
, pkt
->vdisk_type
, pkt
->vdisk_mtype
,
255 pkt
->xfer_mode
, pkt
->vdisk_block_size
,
258 if (pkt
->tag
.stype
== VIO_SUBTYPE_ACK
) {
259 switch (pkt
->vdisk_type
) {
260 case VD_DISK_TYPE_DISK
:
261 case VD_DISK_TYPE_SLICE
:
265 printk(KERN_ERR PFX
"%s: Bogus vdisk_type 0x%x\n",
266 vio
->name
, pkt
->vdisk_type
);
270 if (pkt
->vdisk_block_size
> port
->vdisk_block_size
) {
271 printk(KERN_ERR PFX
"%s: BLOCK size increased "
274 port
->vdisk_block_size
, pkt
->vdisk_block_size
);
278 port
->operations
= pkt
->operations
;
279 port
->vdisk_type
= pkt
->vdisk_type
;
280 if (vdc_version_supported(port
, 1, 1)) {
281 port
->vdisk_size
= pkt
->vdisk_size
;
282 port
->vdisk_mtype
= pkt
->vdisk_mtype
;
284 if (pkt
->max_xfer_size
< port
->max_xfer_size
)
285 port
->max_xfer_size
= pkt
->max_xfer_size
;
286 port
->vdisk_block_size
= pkt
->vdisk_block_size
;
288 port
->vdisk_phys_blksz
= VDC_DEFAULT_BLK_SIZE
;
289 if (vdc_version_supported(port
, 1, 2))
290 port
->vdisk_phys_blksz
= pkt
->phys_block_size
;
294 printk(KERN_ERR PFX
"%s: Attribute NACK\n", vio
->name
);
300 static void vdc_end_special(struct vdc_port
*port
, struct vio_disk_desc
*desc
)
302 int err
= desc
->status
;
304 vdc_finish(&port
->vio
, -err
, WAITING_FOR_GEN_CMD
);
307 static void vdc_end_one(struct vdc_port
*port
, struct vio_dring_state
*dr
,
310 struct vio_disk_desc
*desc
= vio_dring_entry(dr
, index
);
311 struct vdc_req_entry
*rqe
= &port
->rq_arr
[index
];
314 if (unlikely(desc
->hdr
.state
!= VIO_DESC_DONE
))
317 ldc_unmap(port
->vio
.lp
, desc
->cookies
, desc
->ncookies
);
318 desc
->hdr
.state
= VIO_DESC_FREE
;
319 dr
->cons
= vio_dring_next(dr
, index
);
323 vdc_end_special(port
, desc
);
329 blk_mq_end_request(req
, desc
->status
? BLK_STS_IOERR
: 0);
331 vdc_blk_queue_start(port
);
334 static int vdc_ack(struct vdc_port
*port
, void *msgbuf
)
336 struct vio_dring_state
*dr
= &port
->vio
.drings
[VIO_DRIVER_TX_RING
];
337 struct vio_dring_data
*pkt
= msgbuf
;
339 if (unlikely(pkt
->dring_ident
!= dr
->ident
||
340 pkt
->start_idx
!= pkt
->end_idx
||
341 pkt
->start_idx
>= VDC_TX_RING_SIZE
))
344 vdc_end_one(port
, dr
, pkt
->start_idx
);
349 static int vdc_nack(struct vdc_port
*port
, void *msgbuf
)
351 /* XXX Implement me XXX */
355 static void vdc_event(void *arg
, int event
)
357 struct vdc_port
*port
= arg
;
358 struct vio_driver_state
*vio
= &port
->vio
;
362 spin_lock_irqsave(&vio
->lock
, flags
);
364 if (unlikely(event
== LDC_EVENT_RESET
)) {
365 vio_link_state_change(vio
, event
);
366 queue_work(sunvdc_wq
, &port
->ldc_reset_work
);
370 if (unlikely(event
== LDC_EVENT_UP
)) {
371 vio_link_state_change(vio
, event
);
375 if (unlikely(event
!= LDC_EVENT_DATA_READY
)) {
376 pr_warn(PFX
"Unexpected LDC event %d\n", event
);
383 struct vio_msg_tag tag
;
387 err
= ldc_read(vio
->lp
, &msgbuf
, sizeof(msgbuf
));
388 if (unlikely(err
< 0)) {
389 if (err
== -ECONNRESET
)
395 viodbg(DATA
, "TAG [%02x:%02x:%04x:%08x]\n",
398 msgbuf
.tag
.stype_env
,
400 err
= vio_validate_sid(vio
, &msgbuf
.tag
);
404 if (likely(msgbuf
.tag
.type
== VIO_TYPE_DATA
)) {
405 if (msgbuf
.tag
.stype
== VIO_SUBTYPE_ACK
)
406 err
= vdc_ack(port
, &msgbuf
);
407 else if (msgbuf
.tag
.stype
== VIO_SUBTYPE_NACK
)
408 err
= vdc_nack(port
, &msgbuf
);
410 err
= vdc_handle_unknown(port
, &msgbuf
);
411 } else if (msgbuf
.tag
.type
== VIO_TYPE_CTRL
) {
412 err
= vio_control_pkt_engine(vio
, &msgbuf
);
414 err
= vdc_handle_unknown(port
, &msgbuf
);
420 vdc_finish(&port
->vio
, err
, WAITING_FOR_ANY
);
422 spin_unlock_irqrestore(&vio
->lock
, flags
);
425 static int __vdc_tx_trigger(struct vdc_port
*port
)
427 struct vio_dring_state
*dr
= &port
->vio
.drings
[VIO_DRIVER_TX_RING
];
428 struct vio_dring_data hdr
= {
430 .type
= VIO_TYPE_DATA
,
431 .stype
= VIO_SUBTYPE_INFO
,
432 .stype_env
= VIO_DRING_DATA
,
433 .sid
= vio_send_sid(&port
->vio
),
435 .dring_ident
= dr
->ident
,
436 .start_idx
= dr
->prod
,
442 hdr
.seq
= dr
->snd_nxt
;
445 err
= vio_ldc_send(&port
->vio
, &hdr
, sizeof(hdr
));
451 if ((delay
<<= 1) > 128)
453 if (retries
++ > VDC_MAX_RETRIES
)
455 } while (err
== -EAGAIN
);
457 if (err
== -ENOTCONN
)
462 static int __send_request(struct request
*req
)
464 struct vdc_port
*port
= req
->q
->disk
->private_data
;
465 struct vio_dring_state
*dr
= &port
->vio
.drings
[VIO_DRIVER_TX_RING
];
466 struct scatterlist sg
[MAX_RING_COOKIES
];
467 struct vdc_req_entry
*rqe
;
468 struct vio_disk_desc
*desc
;
469 unsigned int map_perm
;
474 if (WARN_ON(port
->ring_cookies
> MAX_RING_COOKIES
))
477 map_perm
= LDC_MAP_SHADOW
| LDC_MAP_DIRECT
| LDC_MAP_IO
;
479 if (rq_data_dir(req
) == READ
) {
480 map_perm
|= LDC_MAP_W
;
483 map_perm
|= LDC_MAP_R
;
487 sg_init_table(sg
, port
->ring_cookies
);
488 nsg
= blk_rq_map_sg(req
->q
, req
, sg
);
491 for (i
= 0; i
< nsg
; i
++)
494 desc
= vio_dring_cur(dr
);
496 err
= ldc_map_sg(port
->vio
.lp
, sg
, nsg
,
497 desc
->cookies
, port
->ring_cookies
,
500 printk(KERN_ERR PFX
"ldc_map_sg() failure, err=%d.\n", err
);
504 rqe
= &port
->rq_arr
[dr
->prod
];
507 desc
->hdr
.ack
= VIO_ACK_ENABLE
;
508 desc
->req_id
= port
->req_id
;
509 desc
->operation
= op
;
510 if (port
->vdisk_type
== VD_DISK_TYPE_DISK
) {
516 desc
->offset
= (blk_rq_pos(req
) << 9) / port
->vdisk_block_size
;
518 desc
->ncookies
= err
;
520 /* This has to be a non-SMP write barrier because we are writing
521 * to memory which is shared with the peer LDOM.
524 desc
->hdr
.state
= VIO_DESC_READY
;
526 err
= __vdc_tx_trigger(port
);
528 printk(KERN_ERR PFX
"vdc_tx_trigger() failure, err=%d\n", err
);
531 dr
->prod
= vio_dring_next(dr
, dr
->prod
);
537 static blk_status_t
vdc_queue_rq(struct blk_mq_hw_ctx
*hctx
,
538 const struct blk_mq_queue_data
*bd
)
540 struct vdc_port
*port
= hctx
->queue
->queuedata
;
541 struct vio_dring_state
*dr
;
544 dr
= &port
->vio
.drings
[VIO_DRIVER_TX_RING
];
546 blk_mq_start_request(bd
->rq
);
548 spin_lock_irqsave(&port
->vio
.lock
, flags
);
551 * Doing drain, just end the request in error
553 if (unlikely(port
->drain
)) {
554 spin_unlock_irqrestore(&port
->vio
.lock
, flags
);
555 return BLK_STS_IOERR
;
558 if (unlikely(vdc_tx_dring_avail(dr
) < 1)) {
559 spin_unlock_irqrestore(&port
->vio
.lock
, flags
);
560 blk_mq_stop_hw_queue(hctx
);
561 return BLK_STS_DEV_RESOURCE
;
564 if (__send_request(bd
->rq
) < 0) {
565 spin_unlock_irqrestore(&port
->vio
.lock
, flags
);
566 return BLK_STS_IOERR
;
569 spin_unlock_irqrestore(&port
->vio
.lock
, flags
);
573 static int generic_request(struct vdc_port
*port
, u8 op
, void *buf
, int len
)
575 struct vio_dring_state
*dr
;
576 struct vio_completion comp
;
577 struct vio_disk_desc
*desc
;
578 unsigned int map_perm
;
583 if (!(((u64
)1 << (u64
)op
) & port
->operations
))
598 op_len
= sizeof(u32
);
599 map_perm
= LDC_MAP_W
;
603 op_len
= sizeof(u32
);
604 map_perm
= LDC_MAP_R
;
608 op_len
= sizeof(struct vio_disk_vtoc
);
609 map_perm
= LDC_MAP_W
;
613 op_len
= sizeof(struct vio_disk_vtoc
);
614 map_perm
= LDC_MAP_R
;
617 case VD_OP_GET_DISKGEOM
:
618 op_len
= sizeof(struct vio_disk_geom
);
619 map_perm
= LDC_MAP_W
;
622 case VD_OP_SET_DISKGEOM
:
623 op_len
= sizeof(struct vio_disk_geom
);
624 map_perm
= LDC_MAP_R
;
629 map_perm
= LDC_MAP_RW
;
632 case VD_OP_GET_DEVID
:
633 op_len
= sizeof(struct vio_disk_devid
);
634 map_perm
= LDC_MAP_W
;
642 map_perm
|= LDC_MAP_SHADOW
| LDC_MAP_DIRECT
| LDC_MAP_IO
;
644 op_len
= (op_len
+ 7) & ~7;
645 req_buf
= kzalloc(op_len
, GFP_KERNEL
);
652 if (map_perm
& LDC_MAP_R
)
653 memcpy(req_buf
, buf
, len
);
655 spin_lock_irqsave(&port
->vio
.lock
, flags
);
657 dr
= &port
->vio
.drings
[VIO_DRIVER_TX_RING
];
659 /* XXX If we want to use this code generically we have to
660 * XXX handle TX ring exhaustion etc.
662 desc
= vio_dring_cur(dr
);
664 err
= ldc_map_single(port
->vio
.lp
, req_buf
, op_len
,
665 desc
->cookies
, port
->ring_cookies
,
668 spin_unlock_irqrestore(&port
->vio
.lock
, flags
);
673 init_completion(&comp
.com
);
674 comp
.waiting_for
= WAITING_FOR_GEN_CMD
;
675 port
->vio
.cmp
= &comp
;
677 desc
->hdr
.ack
= VIO_ACK_ENABLE
;
678 desc
->req_id
= port
->req_id
;
679 desc
->operation
= op
;
684 desc
->ncookies
= err
;
686 /* This has to be a non-SMP write barrier because we are writing
687 * to memory which is shared with the peer LDOM.
690 desc
->hdr
.state
= VIO_DESC_READY
;
692 err
= __vdc_tx_trigger(port
);
695 dr
->prod
= vio_dring_next(dr
, dr
->prod
);
696 spin_unlock_irqrestore(&port
->vio
.lock
, flags
);
698 wait_for_completion(&comp
.com
);
701 port
->vio
.cmp
= NULL
;
702 spin_unlock_irqrestore(&port
->vio
.lock
, flags
);
705 if (map_perm
& LDC_MAP_W
)
706 memcpy(buf
, req_buf
, len
);
713 static int vdc_alloc_tx_ring(struct vdc_port
*port
)
715 struct vio_dring_state
*dr
= &port
->vio
.drings
[VIO_DRIVER_TX_RING
];
716 unsigned long len
, entry_size
;
720 entry_size
= sizeof(struct vio_disk_desc
) +
721 (sizeof(struct ldc_trans_cookie
) * port
->ring_cookies
);
722 len
= (VDC_TX_RING_SIZE
* entry_size
);
724 ncookies
= VIO_MAX_RING_COOKIES
;
725 dring
= ldc_alloc_exp_dring(port
->vio
.lp
, len
,
726 dr
->cookies
, &ncookies
,
731 return PTR_ERR(dring
);
734 dr
->entry_size
= entry_size
;
735 dr
->num_entries
= VDC_TX_RING_SIZE
;
736 dr
->prod
= dr
->cons
= 0;
737 dr
->pending
= VDC_TX_RING_SIZE
;
738 dr
->ncookies
= ncookies
;
743 static void vdc_free_tx_ring(struct vdc_port
*port
)
745 struct vio_dring_state
*dr
= &port
->vio
.drings
[VIO_DRIVER_TX_RING
];
748 ldc_free_exp_dring(port
->vio
.lp
, dr
->base
,
749 (dr
->entry_size
* dr
->num_entries
),
750 dr
->cookies
, dr
->ncookies
);
759 static int vdc_port_up(struct vdc_port
*port
)
761 struct vio_completion comp
;
763 init_completion(&comp
.com
);
765 comp
.waiting_for
= WAITING_FOR_LINK_UP
;
766 port
->vio
.cmp
= &comp
;
768 vio_port_up(&port
->vio
);
769 wait_for_completion(&comp
.com
);
773 static void vdc_port_down(struct vdc_port
*port
)
775 ldc_disconnect(port
->vio
.lp
);
776 ldc_unbind(port
->vio
.lp
);
777 vdc_free_tx_ring(port
);
778 vio_ldc_free(&port
->vio
);
781 static const struct blk_mq_ops vdc_mq_ops
= {
782 .queue_rq
= vdc_queue_rq
,
785 static int probe_disk(struct vdc_port
*port
)
787 struct queue_limits lim
= {
788 .physical_block_size
= port
->vdisk_phys_blksz
,
789 .max_hw_sectors
= port
->max_xfer_size
,
790 /* Each segment in a request is up to an aligned page in size. */
791 .seg_boundary_mask
= PAGE_SIZE
- 1,
792 .max_segment_size
= PAGE_SIZE
,
793 .max_segments
= port
->ring_cookies
,
794 .features
= BLK_FEAT_ROTATIONAL
,
796 struct request_queue
*q
;
800 err
= vdc_port_up(port
);
804 /* Using version 1.2 means vdisk_phys_blksz should be set unless the
805 * disk is reserved by another system.
807 if (vdc_version_supported(port
, 1, 2) && !port
->vdisk_phys_blksz
)
810 if (vdc_version_supported(port
, 1, 1)) {
811 /* vdisk_size should be set during the handshake, if it wasn't
812 * then the underlying disk is reserved by another system
814 if (port
->vdisk_size
== -1)
817 struct vio_disk_geom geom
;
819 err
= generic_request(port
, VD_OP_GET_DISKGEOM
,
820 &geom
, sizeof(geom
));
822 printk(KERN_ERR PFX
"VD_OP_GET_DISKGEOM returns "
826 port
->vdisk_size
= ((u64
)geom
.num_cyl
*
831 err
= blk_mq_alloc_sq_tag_set(&port
->tag_set
, &vdc_mq_ops
,
832 VDC_TX_RING_SIZE
, BLK_MQ_F_SHOULD_MERGE
);
836 g
= blk_mq_alloc_disk(&port
->tag_set
, &lim
, port
);
838 printk(KERN_ERR PFX
"%s: Could not allocate gendisk.\n",
847 g
->major
= vdc_major
;
848 g
->first_minor
= port
->vio
.vdev
->dev_no
<< PARTITION_SHIFT
;
849 g
->minors
= 1 << PARTITION_SHIFT
;
850 strcpy(g
->disk_name
, port
->disk_name
);
854 g
->private_data
= port
;
856 set_capacity(g
, port
->vdisk_size
);
858 if (vdc_version_supported(port
, 1, 1)) {
859 switch (port
->vdisk_mtype
) {
860 case VD_MEDIA_TYPE_CD
:
861 pr_info(PFX
"Virtual CDROM %s\n", port
->disk_name
);
862 g
->flags
|= GENHD_FL_REMOVABLE
;
866 case VD_MEDIA_TYPE_DVD
:
867 pr_info(PFX
"Virtual DVD %s\n", port
->disk_name
);
868 g
->flags
|= GENHD_FL_REMOVABLE
;
872 case VD_MEDIA_TYPE_FIXED
:
873 pr_info(PFX
"Virtual Hard disk %s\n", port
->disk_name
);
878 pr_info(PFX
"%s: %u sectors (%u MB) protocol %d.%d\n",
880 port
->vdisk_size
, (port
->vdisk_size
>> (20 - 9)),
881 port
->vio
.ver
.major
, port
->vio
.ver
.minor
);
883 err
= device_add_disk(&port
->vio
.vdev
->dev
, g
, NULL
);
885 goto out_cleanup_disk
;
892 blk_mq_free_tag_set(&port
->tag_set
);
896 static struct ldc_channel_config vdc_ldc_cfg
= {
899 .mode
= LDC_MODE_UNRELIABLE
,
902 static struct vio_driver_ops vdc_vio_ops
= {
903 .send_attr
= vdc_send_attr
,
904 .handle_attr
= vdc_handle_attr
,
905 .handshake_complete
= vdc_handshake_complete
,
908 static void print_version(void)
910 static int version_printed
;
912 if (version_printed
++ == 0)
913 printk(KERN_INFO
"%s", version
);
916 struct vdc_check_port_data
{
921 static int vdc_device_probed(struct device
*dev
, void *arg
)
923 struct vio_dev
*vdev
= to_vio_dev(dev
);
924 struct vdc_check_port_data
*port_data
;
926 port_data
= (struct vdc_check_port_data
*)arg
;
928 if ((vdev
->dev_no
== port_data
->dev_no
) &&
929 (!(strcmp((char *)&vdev
->type
, port_data
->type
))) &&
930 dev_get_drvdata(dev
)) {
931 /* This device has already been configured
932 * by vdc_port_probe()
940 /* Determine whether the VIO device is part of an mpgroup
941 * by locating all the virtual-device-port nodes associated
942 * with the parent virtual-device node for the VIO device
943 * and checking whether any of these nodes are vdc-ports
944 * which have already been configured.
946 * Returns true if this device is part of an mpgroup and has
947 * already been probed.
949 static bool vdc_port_mpgroup_check(struct vio_dev
*vdev
)
951 struct vdc_check_port_data port_data
;
954 port_data
.dev_no
= vdev
->dev_no
;
955 port_data
.type
= (char *)&vdev
->type
;
957 dev
= device_find_child(vdev
->dev
.parent
, &port_data
,
966 static int vdc_port_probe(struct vio_dev
*vdev
, const struct vio_device_id
*id
)
968 struct mdesc_handle
*hp
;
969 struct vdc_port
*port
;
971 const u64
*ldc_timeout
;
980 if ((vdev
->dev_no
<< PARTITION_SHIFT
) & ~(u64
)MINORMASK
) {
981 printk(KERN_ERR PFX
"Port id [%llu] too large.\n",
983 goto err_out_release_mdesc
;
986 /* Check if this device is part of an mpgroup */
987 if (vdc_port_mpgroup_check(vdev
)) {
989 "VIO: Ignoring extra vdisk port %s",
990 dev_name(&vdev
->dev
));
991 goto err_out_release_mdesc
;
994 port
= kzalloc(sizeof(*port
), GFP_KERNEL
);
997 goto err_out_release_mdesc
;
1000 if (vdev
->dev_no
>= 26)
1001 snprintf(port
->disk_name
, sizeof(port
->disk_name
),
1003 'a' + ((int)vdev
->dev_no
/ 26) - 1,
1004 'a' + ((int)vdev
->dev_no
% 26));
1006 snprintf(port
->disk_name
, sizeof(port
->disk_name
),
1007 VDCBLK_NAME
"%c", 'a' + ((int)vdev
->dev_no
% 26));
1008 port
->vdisk_size
= -1;
1010 /* Actual wall time may be double due to do_generic_file_read() doing
1011 * a readahead I/O first, and once that fails it will try to read a
1014 ldc_timeout
= mdesc_get_property(hp
, vdev
->mp
, "vdc-timeout", NULL
);
1015 port
->ldc_timeout
= ldc_timeout
? *ldc_timeout
: 0;
1016 INIT_DELAYED_WORK(&port
->ldc_reset_timer_work
, vdc_ldc_reset_timer_work
);
1017 INIT_WORK(&port
->ldc_reset_work
, vdc_ldc_reset_work
);
1019 err
= vio_driver_init(&port
->vio
, vdev
, VDEV_DISK
,
1020 vdc_versions
, ARRAY_SIZE(vdc_versions
),
1021 &vdc_vio_ops
, port
->disk_name
);
1023 goto err_out_free_port
;
1025 port
->vdisk_block_size
= VDC_DEFAULT_BLK_SIZE
;
1026 port
->max_xfer_size
= MAX_XFER_SIZE
;
1027 port
->ring_cookies
= MAX_RING_COOKIES
;
1029 err
= vio_ldc_alloc(&port
->vio
, &vdc_ldc_cfg
, port
);
1031 goto err_out_free_port
;
1033 err
= vdc_alloc_tx_ring(port
);
1035 goto err_out_free_ldc
;
1037 err
= probe_disk(port
);
1039 goto err_out_free_tx_ring
;
1041 /* Note that the device driver_data is used to determine
1042 * whether the port has been probed.
1044 dev_set_drvdata(&vdev
->dev
, port
);
1050 err_out_free_tx_ring
:
1051 vdc_free_tx_ring(port
);
1054 vio_ldc_free(&port
->vio
);
1059 err_out_release_mdesc
:
1064 static void vdc_port_remove(struct vio_dev
*vdev
)
1066 struct vdc_port
*port
= dev_get_drvdata(&vdev
->dev
);
1069 blk_mq_stop_hw_queues(port
->disk
->queue
);
1071 flush_work(&port
->ldc_reset_work
);
1072 cancel_delayed_work_sync(&port
->ldc_reset_timer_work
);
1073 del_timer_sync(&port
->vio
.timer
);
1075 del_gendisk(port
->disk
);
1076 put_disk(port
->disk
);
1077 blk_mq_free_tag_set(&port
->tag_set
);
1079 vdc_free_tx_ring(port
);
1080 vio_ldc_free(&port
->vio
);
1082 dev_set_drvdata(&vdev
->dev
, NULL
);
1088 static void vdc_requeue_inflight(struct vdc_port
*port
)
1090 struct vio_dring_state
*dr
= &port
->vio
.drings
[VIO_DRIVER_TX_RING
];
1093 for (idx
= dr
->cons
; idx
!= dr
->prod
; idx
= vio_dring_next(dr
, idx
)) {
1094 struct vio_disk_desc
*desc
= vio_dring_entry(dr
, idx
);
1095 struct vdc_req_entry
*rqe
= &port
->rq_arr
[idx
];
1096 struct request
*req
;
1098 ldc_unmap(port
->vio
.lp
, desc
->cookies
, desc
->ncookies
);
1099 desc
->hdr
.state
= VIO_DESC_FREE
;
1100 dr
->cons
= vio_dring_next(dr
, idx
);
1104 vdc_end_special(port
, desc
);
1109 blk_mq_requeue_request(req
, false);
1113 static void vdc_queue_drain(struct vdc_port
*port
)
1115 struct request_queue
*q
= port
->disk
->queue
;
1118 * Mark the queue as draining, then freeze/quiesce to ensure
1119 * that all existing requests are seen in ->queue_rq() and killed
1122 spin_unlock_irq(&port
->vio
.lock
);
1124 blk_mq_freeze_queue(q
);
1125 blk_mq_quiesce_queue(q
);
1127 spin_lock_irq(&port
->vio
.lock
);
1129 blk_mq_unquiesce_queue(q
);
1130 blk_mq_unfreeze_queue(q
);
1133 static void vdc_ldc_reset_timer_work(struct work_struct
*work
)
1135 struct vdc_port
*port
;
1136 struct vio_driver_state
*vio
;
1138 port
= container_of(work
, struct vdc_port
, ldc_reset_timer_work
.work
);
1141 spin_lock_irq(&vio
->lock
);
1142 if (!(port
->vio
.hs_state
& VIO_HS_COMPLETE
)) {
1143 pr_warn(PFX
"%s ldc down %llu seconds, draining queue\n",
1144 port
->disk_name
, port
->ldc_timeout
);
1145 vdc_queue_drain(port
);
1146 vdc_blk_queue_start(port
);
1148 spin_unlock_irq(&vio
->lock
);
1151 static void vdc_ldc_reset_work(struct work_struct
*work
)
1153 struct vdc_port
*port
;
1154 struct vio_driver_state
*vio
;
1155 unsigned long flags
;
1157 port
= container_of(work
, struct vdc_port
, ldc_reset_work
);
1160 spin_lock_irqsave(&vio
->lock
, flags
);
1161 vdc_ldc_reset(port
);
1162 spin_unlock_irqrestore(&vio
->lock
, flags
);
1165 static void vdc_ldc_reset(struct vdc_port
*port
)
1169 assert_spin_locked(&port
->vio
.lock
);
1171 pr_warn(PFX
"%s ldc link reset\n", port
->disk_name
);
1172 blk_mq_stop_hw_queues(port
->disk
->queue
);
1173 vdc_requeue_inflight(port
);
1174 vdc_port_down(port
);
1176 err
= vio_ldc_alloc(&port
->vio
, &vdc_ldc_cfg
, port
);
1178 pr_err(PFX
"%s vio_ldc_alloc:%d\n", port
->disk_name
, err
);
1182 err
= vdc_alloc_tx_ring(port
);
1184 pr_err(PFX
"%s vio_alloc_tx_ring:%d\n", port
->disk_name
, err
);
1188 if (port
->ldc_timeout
)
1189 mod_delayed_work(system_wq
, &port
->ldc_reset_timer_work
,
1190 round_jiffies(jiffies
+ HZ
* port
->ldc_timeout
));
1191 mod_timer(&port
->vio
.timer
, round_jiffies(jiffies
+ HZ
));
1195 vio_ldc_free(&port
->vio
);
1198 static const struct vio_device_id vdc_port_match
[] = {
1204 MODULE_DEVICE_TABLE(vio
, vdc_port_match
);
1206 static struct vio_driver vdc_port_driver
= {
1207 .id_table
= vdc_port_match
,
1208 .probe
= vdc_port_probe
,
1209 .remove
= vdc_port_remove
,
1213 static int __init
vdc_init(void)
1217 sunvdc_wq
= alloc_workqueue("sunvdc", 0, 0);
1221 err
= register_blkdev(0, VDCBLK_NAME
);
1227 err
= vio_register_driver(&vdc_port_driver
);
1229 goto out_unregister_blkdev
;
1233 out_unregister_blkdev
:
1234 unregister_blkdev(vdc_major
, VDCBLK_NAME
);
1238 destroy_workqueue(sunvdc_wq
);
1242 static void __exit
vdc_exit(void)
1244 vio_unregister_driver(&vdc_port_driver
);
1245 unregister_blkdev(vdc_major
, VDCBLK_NAME
);
1246 destroy_workqueue(sunvdc_wq
);
1249 module_init(vdc_init
);
1250 module_exit(vdc_exit
);