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
38 #define WAITING_FOR_LINK_UP 0x01
39 #define WAITING_FOR_TX_SPACE 0x02
40 #define WAITING_FOR_GEN_CMD 0x04
41 #define WAITING_FOR_ANY -1
43 static struct workqueue_struct
*sunvdc_wq
;
45 struct vdc_req_entry
{
50 struct vio_driver_state vio
;
54 struct vdc_completion
*cmp
;
58 struct vdc_req_entry rq_arr
[VDC_TX_RING_SIZE
];
60 unsigned long ring_cookies
;
66 struct timer_list ldc_reset_timer
;
67 struct work_struct ldc_reset_work
;
69 /* The server fills these in for us in the disk attribute
80 static void vdc_ldc_reset(struct vdc_port
*port
);
81 static void vdc_ldc_reset_work(struct work_struct
*work
);
82 static void vdc_ldc_reset_timer(unsigned long _arg
);
84 static inline struct vdc_port
*to_vdc_port(struct vio_driver_state
*vio
)
86 return container_of(vio
, struct vdc_port
, vio
);
89 /* Ordered from largest major to lowest */
90 static struct vio_version vdc_versions
[] = {
91 { .major
= 1, .minor
= 1 },
92 { .major
= 1, .minor
= 0 },
95 static inline int vdc_version_supported(struct vdc_port
*port
,
98 return port
->vio
.ver
.major
== major
&& port
->vio
.ver
.minor
>= minor
;
101 #define VDCBLK_NAME "vdisk"
102 static int vdc_major
;
103 #define PARTITION_SHIFT 3
105 static inline u32
vdc_tx_dring_avail(struct vio_dring_state
*dr
)
107 return vio_dring_avail(dr
, VDC_TX_RING_SIZE
);
110 static int vdc_getgeo(struct block_device
*bdev
, struct hd_geometry
*geo
)
112 struct gendisk
*disk
= bdev
->bd_disk
;
113 sector_t nsect
= get_capacity(disk
);
114 sector_t cylinders
= nsect
;
118 sector_div(cylinders
, geo
->heads
* geo
->sectors
);
119 geo
->cylinders
= cylinders
;
120 if ((sector_t
)(geo
->cylinders
+ 1) * geo
->heads
* geo
->sectors
< nsect
)
121 geo
->cylinders
= 0xffff;
126 /* Add ioctl/CDROM_GET_CAPABILITY to support cdrom_id in udev
127 * when vdisk_mtype is VD_MEDIA_TYPE_CD or VD_MEDIA_TYPE_DVD.
128 * Needed to be able to install inside an ldom from an iso image.
130 static int vdc_ioctl(struct block_device
*bdev
, fmode_t mode
,
131 unsigned command
, unsigned long argument
)
134 struct gendisk
*disk
;
137 case CDROMMULTISESSION
:
138 pr_debug(PFX
"Multisession CDs not supported\n");
139 for (i
= 0; i
< sizeof(struct cdrom_multisession
); i
++)
140 if (put_user(0, (char __user
*)(argument
+ i
)))
144 case CDROM_GET_CAPABILITY
:
145 disk
= bdev
->bd_disk
;
147 if (bdev
->bd_disk
&& (disk
->flags
& GENHD_FL_CD
))
152 pr_debug(PFX
"ioctl %08x not supported\n", command
);
157 static const struct block_device_operations vdc_fops
= {
158 .owner
= THIS_MODULE
,
159 .getgeo
= vdc_getgeo
,
163 static void vdc_blk_queue_start(struct vdc_port
*port
)
165 struct vio_dring_state
*dr
= &port
->vio
.drings
[VIO_DRIVER_TX_RING
];
167 /* restart blk queue when ring is half emptied. also called after
168 * handshake completes, so check for initial handshake before we've
171 if (port
->disk
&& blk_queue_stopped(port
->disk
->queue
) &&
172 vdc_tx_dring_avail(dr
) * 100 / VDC_TX_RING_SIZE
>= 50) {
173 blk_start_queue(port
->disk
->queue
);
178 static void vdc_finish(struct vio_driver_state
*vio
, int err
, int waiting_for
)
181 (waiting_for
== -1 ||
182 vio
->cmp
->waiting_for
== waiting_for
)) {
184 complete(&vio
->cmp
->com
);
189 static void vdc_handshake_complete(struct vio_driver_state
*vio
)
191 struct vdc_port
*port
= to_vdc_port(vio
);
193 del_timer(&port
->ldc_reset_timer
);
194 vdc_finish(vio
, 0, WAITING_FOR_LINK_UP
);
195 vdc_blk_queue_start(port
);
198 static int vdc_handle_unknown(struct vdc_port
*port
, void *arg
)
200 struct vio_msg_tag
*pkt
= arg
;
202 printk(KERN_ERR PFX
"Received unknown msg [%02x:%02x:%04x:%08x]\n",
203 pkt
->type
, pkt
->stype
, pkt
->stype_env
, pkt
->sid
);
204 printk(KERN_ERR PFX
"Resetting connection.\n");
206 ldc_disconnect(port
->vio
.lp
);
211 static int vdc_send_attr(struct vio_driver_state
*vio
)
213 struct vdc_port
*port
= to_vdc_port(vio
);
214 struct vio_disk_attr_info pkt
;
216 memset(&pkt
, 0, sizeof(pkt
));
218 pkt
.tag
.type
= VIO_TYPE_CTRL
;
219 pkt
.tag
.stype
= VIO_SUBTYPE_INFO
;
220 pkt
.tag
.stype_env
= VIO_ATTR_INFO
;
221 pkt
.tag
.sid
= vio_send_sid(vio
);
223 pkt
.xfer_mode
= VIO_DRING_MODE
;
224 pkt
.vdisk_block_size
= port
->vdisk_block_size
;
225 pkt
.max_xfer_size
= port
->max_xfer_size
;
227 viodbg(HS
, "SEND ATTR xfer_mode[0x%x] blksz[%u] max_xfer[%llu]\n",
228 pkt
.xfer_mode
, pkt
.vdisk_block_size
, pkt
.max_xfer_size
);
230 return vio_ldc_send(&port
->vio
, &pkt
, sizeof(pkt
));
233 static int vdc_handle_attr(struct vio_driver_state
*vio
, void *arg
)
235 struct vdc_port
*port
= to_vdc_port(vio
);
236 struct vio_disk_attr_info
*pkt
= arg
;
238 viodbg(HS
, "GOT ATTR stype[0x%x] ops[%llx] disk_size[%llu] disk_type[%x] "
239 "mtype[0x%x] xfer_mode[0x%x] blksz[%u] max_xfer[%llu]\n",
240 pkt
->tag
.stype
, pkt
->operations
,
241 pkt
->vdisk_size
, pkt
->vdisk_type
, pkt
->vdisk_mtype
,
242 pkt
->xfer_mode
, pkt
->vdisk_block_size
,
245 if (pkt
->tag
.stype
== VIO_SUBTYPE_ACK
) {
246 switch (pkt
->vdisk_type
) {
247 case VD_DISK_TYPE_DISK
:
248 case VD_DISK_TYPE_SLICE
:
252 printk(KERN_ERR PFX
"%s: Bogus vdisk_type 0x%x\n",
253 vio
->name
, pkt
->vdisk_type
);
257 if (pkt
->vdisk_block_size
> port
->vdisk_block_size
) {
258 printk(KERN_ERR PFX
"%s: BLOCK size increased "
261 port
->vdisk_block_size
, pkt
->vdisk_block_size
);
265 port
->operations
= pkt
->operations
;
266 port
->vdisk_type
= pkt
->vdisk_type
;
267 if (vdc_version_supported(port
, 1, 1)) {
268 port
->vdisk_size
= pkt
->vdisk_size
;
269 port
->vdisk_mtype
= pkt
->vdisk_mtype
;
271 if (pkt
->max_xfer_size
< port
->max_xfer_size
)
272 port
->max_xfer_size
= pkt
->max_xfer_size
;
273 port
->vdisk_block_size
= pkt
->vdisk_block_size
;
276 printk(KERN_ERR PFX
"%s: Attribute NACK\n", vio
->name
);
282 static void vdc_end_special(struct vdc_port
*port
, struct vio_disk_desc
*desc
)
284 int err
= desc
->status
;
286 vdc_finish(&port
->vio
, -err
, WAITING_FOR_GEN_CMD
);
289 static void vdc_end_one(struct vdc_port
*port
, struct vio_dring_state
*dr
,
292 struct vio_disk_desc
*desc
= vio_dring_entry(dr
, index
);
293 struct vdc_req_entry
*rqe
= &port
->rq_arr
[index
];
296 if (unlikely(desc
->hdr
.state
!= VIO_DESC_DONE
))
299 ldc_unmap(port
->vio
.lp
, desc
->cookies
, desc
->ncookies
);
300 desc
->hdr
.state
= VIO_DESC_FREE
;
301 dr
->cons
= vio_dring_next(dr
, index
);
305 vdc_end_special(port
, desc
);
311 __blk_end_request(req
, (desc
->status
? -EIO
: 0), desc
->size
);
313 vdc_blk_queue_start(port
);
316 static int vdc_ack(struct vdc_port
*port
, void *msgbuf
)
318 struct vio_dring_state
*dr
= &port
->vio
.drings
[VIO_DRIVER_TX_RING
];
319 struct vio_dring_data
*pkt
= msgbuf
;
321 if (unlikely(pkt
->dring_ident
!= dr
->ident
||
322 pkt
->start_idx
!= pkt
->end_idx
||
323 pkt
->start_idx
>= VDC_TX_RING_SIZE
))
326 vdc_end_one(port
, dr
, pkt
->start_idx
);
331 static int vdc_nack(struct vdc_port
*port
, void *msgbuf
)
333 /* XXX Implement me XXX */
337 static void vdc_event(void *arg
, int event
)
339 struct vdc_port
*port
= arg
;
340 struct vio_driver_state
*vio
= &port
->vio
;
344 spin_lock_irqsave(&vio
->lock
, flags
);
346 if (unlikely(event
== LDC_EVENT_RESET
)) {
347 vio_link_state_change(vio
, event
);
348 queue_work(sunvdc_wq
, &port
->ldc_reset_work
);
352 if (unlikely(event
== LDC_EVENT_UP
)) {
353 vio_link_state_change(vio
, event
);
357 if (unlikely(event
!= LDC_EVENT_DATA_READY
)) {
358 pr_warn(PFX
"Unexpected LDC event %d\n", event
);
365 struct vio_msg_tag tag
;
369 err
= ldc_read(vio
->lp
, &msgbuf
, sizeof(msgbuf
));
370 if (unlikely(err
< 0)) {
371 if (err
== -ECONNRESET
)
377 viodbg(DATA
, "TAG [%02x:%02x:%04x:%08x]\n",
380 msgbuf
.tag
.stype_env
,
382 err
= vio_validate_sid(vio
, &msgbuf
.tag
);
386 if (likely(msgbuf
.tag
.type
== VIO_TYPE_DATA
)) {
387 if (msgbuf
.tag
.stype
== VIO_SUBTYPE_ACK
)
388 err
= vdc_ack(port
, &msgbuf
);
389 else if (msgbuf
.tag
.stype
== VIO_SUBTYPE_NACK
)
390 err
= vdc_nack(port
, &msgbuf
);
392 err
= vdc_handle_unknown(port
, &msgbuf
);
393 } else if (msgbuf
.tag
.type
== VIO_TYPE_CTRL
) {
394 err
= vio_control_pkt_engine(vio
, &msgbuf
);
396 err
= vdc_handle_unknown(port
, &msgbuf
);
402 vdc_finish(&port
->vio
, err
, WAITING_FOR_ANY
);
404 spin_unlock_irqrestore(&vio
->lock
, flags
);
407 static int __vdc_tx_trigger(struct vdc_port
*port
)
409 struct vio_dring_state
*dr
= &port
->vio
.drings
[VIO_DRIVER_TX_RING
];
410 struct vio_dring_data hdr
= {
412 .type
= VIO_TYPE_DATA
,
413 .stype
= VIO_SUBTYPE_INFO
,
414 .stype_env
= VIO_DRING_DATA
,
415 .sid
= vio_send_sid(&port
->vio
),
417 .dring_ident
= dr
->ident
,
418 .start_idx
= dr
->prod
,
423 hdr
.seq
= dr
->snd_nxt
;
426 err
= vio_ldc_send(&port
->vio
, &hdr
, sizeof(hdr
));
432 if ((delay
<<= 1) > 128)
434 } while (err
== -EAGAIN
);
436 if (err
== -ENOTCONN
)
441 static int __send_request(struct request
*req
)
443 struct vdc_port
*port
= req
->rq_disk
->private_data
;
444 struct vio_dring_state
*dr
= &port
->vio
.drings
[VIO_DRIVER_TX_RING
];
445 struct scatterlist sg
[port
->ring_cookies
];
446 struct vdc_req_entry
*rqe
;
447 struct vio_disk_desc
*desc
;
448 unsigned int map_perm
;
453 map_perm
= LDC_MAP_SHADOW
| LDC_MAP_DIRECT
| LDC_MAP_IO
;
455 if (rq_data_dir(req
) == READ
) {
456 map_perm
|= LDC_MAP_W
;
459 map_perm
|= LDC_MAP_R
;
463 sg_init_table(sg
, port
->ring_cookies
);
464 nsg
= blk_rq_map_sg(req
->q
, req
, sg
);
467 for (i
= 0; i
< nsg
; i
++)
470 desc
= vio_dring_cur(dr
);
472 err
= ldc_map_sg(port
->vio
.lp
, sg
, nsg
,
473 desc
->cookies
, port
->ring_cookies
,
476 printk(KERN_ERR PFX
"ldc_map_sg() failure, err=%d.\n", err
);
480 rqe
= &port
->rq_arr
[dr
->prod
];
483 desc
->hdr
.ack
= VIO_ACK_ENABLE
;
484 desc
->req_id
= port
->req_id
;
485 desc
->operation
= op
;
486 if (port
->vdisk_type
== VD_DISK_TYPE_DISK
) {
492 desc
->offset
= (blk_rq_pos(req
) << 9) / port
->vdisk_block_size
;
494 desc
->ncookies
= err
;
496 /* This has to be a non-SMP write barrier because we are writing
497 * to memory which is shared with the peer LDOM.
500 desc
->hdr
.state
= VIO_DESC_READY
;
502 err
= __vdc_tx_trigger(port
);
504 printk(KERN_ERR PFX
"vdc_tx_trigger() failure, err=%d\n", err
);
507 dr
->prod
= vio_dring_next(dr
, dr
->prod
);
513 static void do_vdc_request(struct request_queue
*rq
)
517 while ((req
= blk_peek_request(rq
)) != NULL
) {
518 struct vdc_port
*port
;
519 struct vio_dring_state
*dr
;
521 port
= req
->rq_disk
->private_data
;
522 dr
= &port
->vio
.drings
[VIO_DRIVER_TX_RING
];
523 if (unlikely(vdc_tx_dring_avail(dr
) < 1))
526 blk_start_request(req
);
528 if (__send_request(req
) < 0) {
529 blk_requeue_request(rq
, req
);
531 /* Avoid pointless unplugs. */
538 static int generic_request(struct vdc_port
*port
, u8 op
, void *buf
, int len
)
540 struct vio_dring_state
*dr
;
541 struct vio_completion comp
;
542 struct vio_disk_desc
*desc
;
543 unsigned int map_perm
;
548 if (!(((u64
)1 << (u64
)op
) & port
->operations
))
563 op_len
= sizeof(u32
);
564 map_perm
= LDC_MAP_W
;
568 op_len
= sizeof(u32
);
569 map_perm
= LDC_MAP_R
;
573 op_len
= sizeof(struct vio_disk_vtoc
);
574 map_perm
= LDC_MAP_W
;
578 op_len
= sizeof(struct vio_disk_vtoc
);
579 map_perm
= LDC_MAP_R
;
582 case VD_OP_GET_DISKGEOM
:
583 op_len
= sizeof(struct vio_disk_geom
);
584 map_perm
= LDC_MAP_W
;
587 case VD_OP_SET_DISKGEOM
:
588 op_len
= sizeof(struct vio_disk_geom
);
589 map_perm
= LDC_MAP_R
;
594 map_perm
= LDC_MAP_RW
;
597 case VD_OP_GET_DEVID
:
598 op_len
= sizeof(struct vio_disk_devid
);
599 map_perm
= LDC_MAP_W
;
608 map_perm
|= LDC_MAP_SHADOW
| LDC_MAP_DIRECT
| LDC_MAP_IO
;
610 op_len
= (op_len
+ 7) & ~7;
611 req_buf
= kzalloc(op_len
, GFP_KERNEL
);
618 if (map_perm
& LDC_MAP_R
)
619 memcpy(req_buf
, buf
, len
);
621 spin_lock_irqsave(&port
->vio
.lock
, flags
);
623 dr
= &port
->vio
.drings
[VIO_DRIVER_TX_RING
];
625 /* XXX If we want to use this code generically we have to
626 * XXX handle TX ring exhaustion etc.
628 desc
= vio_dring_cur(dr
);
630 err
= ldc_map_single(port
->vio
.lp
, req_buf
, op_len
,
631 desc
->cookies
, port
->ring_cookies
,
634 spin_unlock_irqrestore(&port
->vio
.lock
, flags
);
639 init_completion(&comp
.com
);
640 comp
.waiting_for
= WAITING_FOR_GEN_CMD
;
641 port
->vio
.cmp
= &comp
;
643 desc
->hdr
.ack
= VIO_ACK_ENABLE
;
644 desc
->req_id
= port
->req_id
;
645 desc
->operation
= op
;
650 desc
->ncookies
= err
;
652 /* This has to be a non-SMP write barrier because we are writing
653 * to memory which is shared with the peer LDOM.
656 desc
->hdr
.state
= VIO_DESC_READY
;
658 err
= __vdc_tx_trigger(port
);
661 dr
->prod
= vio_dring_next(dr
, dr
->prod
);
662 spin_unlock_irqrestore(&port
->vio
.lock
, flags
);
664 wait_for_completion(&comp
.com
);
667 port
->vio
.cmp
= NULL
;
668 spin_unlock_irqrestore(&port
->vio
.lock
, flags
);
671 if (map_perm
& LDC_MAP_W
)
672 memcpy(buf
, req_buf
, len
);
679 static int vdc_alloc_tx_ring(struct vdc_port
*port
)
681 struct vio_dring_state
*dr
= &port
->vio
.drings
[VIO_DRIVER_TX_RING
];
682 unsigned long len
, entry_size
;
686 entry_size
= sizeof(struct vio_disk_desc
) +
687 (sizeof(struct ldc_trans_cookie
) * port
->ring_cookies
);
688 len
= (VDC_TX_RING_SIZE
* entry_size
);
690 ncookies
= VIO_MAX_RING_COOKIES
;
691 dring
= ldc_alloc_exp_dring(port
->vio
.lp
, len
,
692 dr
->cookies
, &ncookies
,
697 return PTR_ERR(dring
);
700 dr
->entry_size
= entry_size
;
701 dr
->num_entries
= VDC_TX_RING_SIZE
;
702 dr
->prod
= dr
->cons
= 0;
703 dr
->pending
= VDC_TX_RING_SIZE
;
704 dr
->ncookies
= ncookies
;
709 static void vdc_free_tx_ring(struct vdc_port
*port
)
711 struct vio_dring_state
*dr
= &port
->vio
.drings
[VIO_DRIVER_TX_RING
];
714 ldc_free_exp_dring(port
->vio
.lp
, dr
->base
,
715 (dr
->entry_size
* dr
->num_entries
),
716 dr
->cookies
, dr
->ncookies
);
725 static int vdc_port_up(struct vdc_port
*port
)
727 struct vio_completion comp
;
729 init_completion(&comp
.com
);
731 comp
.waiting_for
= WAITING_FOR_LINK_UP
;
732 port
->vio
.cmp
= &comp
;
734 vio_port_up(&port
->vio
);
735 wait_for_completion(&comp
.com
);
739 static void vdc_port_down(struct vdc_port
*port
)
741 ldc_disconnect(port
->vio
.lp
);
742 ldc_unbind(port
->vio
.lp
);
743 vdc_free_tx_ring(port
);
744 vio_ldc_free(&port
->vio
);
747 static int probe_disk(struct vdc_port
*port
)
749 struct request_queue
*q
;
753 err
= vdc_port_up(port
);
757 if (vdc_version_supported(port
, 1, 1)) {
758 /* vdisk_size should be set during the handshake, if it wasn't
759 * then the underlying disk is reserved by another system
761 if (port
->vdisk_size
== -1)
764 struct vio_disk_geom geom
;
766 err
= generic_request(port
, VD_OP_GET_DISKGEOM
,
767 &geom
, sizeof(geom
));
769 printk(KERN_ERR PFX
"VD_OP_GET_DISKGEOM returns "
773 port
->vdisk_size
= ((u64
)geom
.num_cyl
*
778 q
= blk_init_queue(do_vdc_request
, &port
->vio
.lock
);
780 printk(KERN_ERR PFX
"%s: Could not allocate queue.\n",
784 g
= alloc_disk(1 << PARTITION_SHIFT
);
786 printk(KERN_ERR PFX
"%s: Could not allocate gendisk.\n",
788 blk_cleanup_queue(q
);
794 /* Each segment in a request is up to an aligned page in size. */
795 blk_queue_segment_boundary(q
, PAGE_SIZE
- 1);
796 blk_queue_max_segment_size(q
, PAGE_SIZE
);
798 blk_queue_max_segments(q
, port
->ring_cookies
);
799 blk_queue_max_hw_sectors(q
, port
->max_xfer_size
);
800 g
->major
= vdc_major
;
801 g
->first_minor
= port
->vio
.vdev
->dev_no
<< PARTITION_SHIFT
;
802 strcpy(g
->disk_name
, port
->disk_name
);
806 g
->private_data
= port
;
807 g
->driverfs_dev
= &port
->vio
.vdev
->dev
;
809 set_capacity(g
, port
->vdisk_size
);
811 if (vdc_version_supported(port
, 1, 1)) {
812 switch (port
->vdisk_mtype
) {
813 case VD_MEDIA_TYPE_CD
:
814 pr_info(PFX
"Virtual CDROM %s\n", port
->disk_name
);
815 g
->flags
|= GENHD_FL_CD
;
816 g
->flags
|= GENHD_FL_REMOVABLE
;
820 case VD_MEDIA_TYPE_DVD
:
821 pr_info(PFX
"Virtual DVD %s\n", port
->disk_name
);
822 g
->flags
|= GENHD_FL_CD
;
823 g
->flags
|= GENHD_FL_REMOVABLE
;
827 case VD_MEDIA_TYPE_FIXED
:
828 pr_info(PFX
"Virtual Hard disk %s\n", port
->disk_name
);
833 pr_info(PFX
"%s: %u sectors (%u MB) protocol %d.%d\n",
835 port
->vdisk_size
, (port
->vdisk_size
>> (20 - 9)),
836 port
->vio
.ver
.major
, port
->vio
.ver
.minor
);
843 static struct ldc_channel_config vdc_ldc_cfg
= {
846 .mode
= LDC_MODE_UNRELIABLE
,
849 static struct vio_driver_ops vdc_vio_ops
= {
850 .send_attr
= vdc_send_attr
,
851 .handle_attr
= vdc_handle_attr
,
852 .handshake_complete
= vdc_handshake_complete
,
855 static void print_version(void)
857 static int version_printed
;
859 if (version_printed
++ == 0)
860 printk(KERN_INFO
"%s", version
);
863 static int vdc_port_probe(struct vio_dev
*vdev
, const struct vio_device_id
*id
)
865 struct mdesc_handle
*hp
;
866 struct vdc_port
*port
;
868 const u64
*ldc_timeout
;
875 if ((vdev
->dev_no
<< PARTITION_SHIFT
) & ~(u64
)MINORMASK
) {
876 printk(KERN_ERR PFX
"Port id [%llu] too large.\n",
878 goto err_out_release_mdesc
;
881 port
= kzalloc(sizeof(*port
), GFP_KERNEL
);
884 printk(KERN_ERR PFX
"Cannot allocate vdc_port.\n");
885 goto err_out_release_mdesc
;
888 if (vdev
->dev_no
>= 26)
889 snprintf(port
->disk_name
, sizeof(port
->disk_name
),
891 'a' + ((int)vdev
->dev_no
/ 26) - 1,
892 'a' + ((int)vdev
->dev_no
% 26));
894 snprintf(port
->disk_name
, sizeof(port
->disk_name
),
895 VDCBLK_NAME
"%c", 'a' + ((int)vdev
->dev_no
% 26));
896 port
->vdisk_size
= -1;
898 /* Actual wall time may be double due to do_generic_file_read() doing
899 * a readahead I/O first, and once that fails it will try to read a
902 ldc_timeout
= mdesc_get_property(hp
, vdev
->mp
, "vdc-timeout", NULL
);
903 port
->ldc_timeout
= ldc_timeout
? *ldc_timeout
: 0;
904 setup_timer(&port
->ldc_reset_timer
, vdc_ldc_reset_timer
,
905 (unsigned long)port
);
906 INIT_WORK(&port
->ldc_reset_work
, vdc_ldc_reset_work
);
908 err
= vio_driver_init(&port
->vio
, vdev
, VDEV_DISK
,
909 vdc_versions
, ARRAY_SIZE(vdc_versions
),
910 &vdc_vio_ops
, port
->disk_name
);
912 goto err_out_free_port
;
914 port
->vdisk_block_size
= 512;
915 port
->max_xfer_size
= ((128 * 1024) / port
->vdisk_block_size
);
916 port
->ring_cookies
= ((port
->max_xfer_size
*
917 port
->vdisk_block_size
) / PAGE_SIZE
) + 2;
919 err
= vio_ldc_alloc(&port
->vio
, &vdc_ldc_cfg
, port
);
921 goto err_out_free_port
;
923 err
= vdc_alloc_tx_ring(port
);
925 goto err_out_free_ldc
;
927 err
= probe_disk(port
);
929 goto err_out_free_tx_ring
;
931 dev_set_drvdata(&vdev
->dev
, port
);
937 err_out_free_tx_ring
:
938 vdc_free_tx_ring(port
);
941 vio_ldc_free(&port
->vio
);
946 err_out_release_mdesc
:
951 static int vdc_port_remove(struct vio_dev
*vdev
)
953 struct vdc_port
*port
= dev_get_drvdata(&vdev
->dev
);
958 spin_lock_irqsave(&port
->vio
.lock
, flags
);
959 blk_stop_queue(port
->disk
->queue
);
960 spin_unlock_irqrestore(&port
->vio
.lock
, flags
);
962 flush_work(&port
->ldc_reset_work
);
963 del_timer_sync(&port
->ldc_reset_timer
);
964 del_timer_sync(&port
->vio
.timer
);
966 del_gendisk(port
->disk
);
967 blk_cleanup_queue(port
->disk
->queue
);
968 put_disk(port
->disk
);
971 vdc_free_tx_ring(port
);
972 vio_ldc_free(&port
->vio
);
974 dev_set_drvdata(&vdev
->dev
, NULL
);
981 static void vdc_requeue_inflight(struct vdc_port
*port
)
983 struct vio_dring_state
*dr
= &port
->vio
.drings
[VIO_DRIVER_TX_RING
];
986 for (idx
= dr
->cons
; idx
!= dr
->prod
; idx
= vio_dring_next(dr
, idx
)) {
987 struct vio_disk_desc
*desc
= vio_dring_entry(dr
, idx
);
988 struct vdc_req_entry
*rqe
= &port
->rq_arr
[idx
];
991 ldc_unmap(port
->vio
.lp
, desc
->cookies
, desc
->ncookies
);
992 desc
->hdr
.state
= VIO_DESC_FREE
;
993 dr
->cons
= vio_dring_next(dr
, idx
);
997 vdc_end_special(port
, desc
);
1002 blk_requeue_request(port
->disk
->queue
, req
);
1006 static void vdc_queue_drain(struct vdc_port
*port
)
1008 struct request
*req
;
1010 while ((req
= blk_fetch_request(port
->disk
->queue
)) != NULL
)
1011 __blk_end_request_all(req
, -EIO
);
1014 static void vdc_ldc_reset_timer(unsigned long _arg
)
1016 struct vdc_port
*port
= (struct vdc_port
*) _arg
;
1017 struct vio_driver_state
*vio
= &port
->vio
;
1018 unsigned long flags
;
1020 spin_lock_irqsave(&vio
->lock
, flags
);
1021 if (!(port
->vio
.hs_state
& VIO_HS_COMPLETE
)) {
1022 pr_warn(PFX
"%s ldc down %llu seconds, draining queue\n",
1023 port
->disk_name
, port
->ldc_timeout
);
1024 vdc_queue_drain(port
);
1025 vdc_blk_queue_start(port
);
1027 spin_unlock_irqrestore(&vio
->lock
, flags
);
1030 static void vdc_ldc_reset_work(struct work_struct
*work
)
1032 struct vdc_port
*port
;
1033 struct vio_driver_state
*vio
;
1034 unsigned long flags
;
1036 port
= container_of(work
, struct vdc_port
, ldc_reset_work
);
1039 spin_lock_irqsave(&vio
->lock
, flags
);
1040 vdc_ldc_reset(port
);
1041 spin_unlock_irqrestore(&vio
->lock
, flags
);
1044 static void vdc_ldc_reset(struct vdc_port
*port
)
1048 assert_spin_locked(&port
->vio
.lock
);
1050 pr_warn(PFX
"%s ldc link reset\n", port
->disk_name
);
1051 blk_stop_queue(port
->disk
->queue
);
1052 vdc_requeue_inflight(port
);
1053 vdc_port_down(port
);
1055 err
= vio_ldc_alloc(&port
->vio
, &vdc_ldc_cfg
, port
);
1057 pr_err(PFX
"%s vio_ldc_alloc:%d\n", port
->disk_name
, err
);
1061 err
= vdc_alloc_tx_ring(port
);
1063 pr_err(PFX
"%s vio_alloc_tx_ring:%d\n", port
->disk_name
, err
);
1067 if (port
->ldc_timeout
)
1068 mod_timer(&port
->ldc_reset_timer
,
1069 round_jiffies(jiffies
+ HZ
* port
->ldc_timeout
));
1070 mod_timer(&port
->vio
.timer
, round_jiffies(jiffies
+ HZ
));
1074 vio_ldc_free(&port
->vio
);
1077 static const struct vio_device_id vdc_port_match
[] = {
1083 MODULE_DEVICE_TABLE(vio
, vdc_port_match
);
1085 static struct vio_driver vdc_port_driver
= {
1086 .id_table
= vdc_port_match
,
1087 .probe
= vdc_port_probe
,
1088 .remove
= vdc_port_remove
,
1092 static int __init
vdc_init(void)
1096 sunvdc_wq
= alloc_workqueue("sunvdc", 0, 0);
1100 err
= register_blkdev(0, VDCBLK_NAME
);
1106 err
= vio_register_driver(&vdc_port_driver
);
1108 goto out_unregister_blkdev
;
1112 out_unregister_blkdev
:
1113 unregister_blkdev(vdc_major
, VDCBLK_NAME
);
1117 destroy_workqueue(sunvdc_wq
);
1121 static void __exit
vdc_exit(void)
1123 vio_unregister_driver(&vdc_port_driver
);
1124 unregister_blkdev(vdc_major
, VDCBLK_NAME
);
1125 destroy_workqueue(sunvdc_wq
);
1128 module_init(vdc_init
);
1129 module_exit(vdc_exit
);