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,
174 .compat_ioctl = blkdev_compat_ptr_ioctl,
177 static void vdc_blk_queue_start(struct vdc_port *port)
179 struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING];
181 /* restart blk queue when ring is half emptied. also called after
182 * handshake completes, so check for initial handshake before we've
185 if (port->disk && vdc_tx_dring_avail(dr) * 100 / VDC_TX_RING_SIZE >= 50)
186 blk_mq_start_stopped_hw_queues(port->disk->queue, true);
189 static void vdc_finish(struct vio_driver_state *vio, int err, int waiting_for)
192 (waiting_for == -1 ||
193 vio->cmp->waiting_for == waiting_for)) {
195 complete(&vio->cmp->com);
200 static void vdc_handshake_complete(struct vio_driver_state *vio)
202 struct vdc_port *port = to_vdc_port(vio);
204 cancel_delayed_work(&port->ldc_reset_timer_work);
205 vdc_finish(vio, 0, WAITING_FOR_LINK_UP);
206 vdc_blk_queue_start(port);
209 static int vdc_handle_unknown(struct vdc_port *port, void *arg)
211 struct vio_msg_tag *pkt = arg;
213 printk(KERN_ERR PFX "Received unknown msg [%02x:%02x:%04x:%08x]\n",
214 pkt->type, pkt->stype, pkt->stype_env, pkt->sid);
215 printk(KERN_ERR PFX "Resetting connection.\n");
217 ldc_disconnect(port->vio.lp);
222 static int vdc_send_attr(struct vio_driver_state *vio)
224 struct vdc_port *port = to_vdc_port(vio);
225 struct vio_disk_attr_info pkt;
227 memset(&pkt, 0, sizeof(pkt));
229 pkt.tag.type = VIO_TYPE_CTRL;
230 pkt.tag.stype = VIO_SUBTYPE_INFO;
231 pkt.tag.stype_env = VIO_ATTR_INFO;
232 pkt.tag.sid = vio_send_sid(vio);
234 pkt.xfer_mode = VIO_DRING_MODE;
235 pkt.vdisk_block_size = port->vdisk_block_size;
236 pkt.max_xfer_size = port->max_xfer_size;
238 viodbg(HS, "SEND ATTR xfer_mode[0x%x] blksz[%u] max_xfer[%llu]\n",
239 pkt.xfer_mode, pkt.vdisk_block_size, pkt.max_xfer_size);
241 return vio_ldc_send(&port->vio, &pkt, sizeof(pkt));
244 static int vdc_handle_attr(struct vio_driver_state *vio, void *arg)
246 struct vdc_port *port = to_vdc_port(vio);
247 struct vio_disk_attr_info *pkt = arg;
249 viodbg(HS, "GOT ATTR stype[0x%x] ops[%llx] disk_size[%llu] disk_type[%x] "
250 "mtype[0x%x] xfer_mode[0x%x] blksz[%u] max_xfer[%llu]\n",
251 pkt->tag.stype, pkt->operations,
252 pkt->vdisk_size, pkt->vdisk_type, pkt->vdisk_mtype,
253 pkt->xfer_mode, pkt->vdisk_block_size,
256 if (pkt->tag.stype == VIO_SUBTYPE_ACK) {
257 switch (pkt->vdisk_type) {
258 case VD_DISK_TYPE_DISK:
259 case VD_DISK_TYPE_SLICE:
263 printk(KERN_ERR PFX "%s: Bogus vdisk_type 0x%x\n",
264 vio->name, pkt->vdisk_type);
268 if (pkt->vdisk_block_size > port->vdisk_block_size) {
269 printk(KERN_ERR PFX "%s: BLOCK size increased "
272 port->vdisk_block_size, pkt->vdisk_block_size);
276 port->operations = pkt->operations;
277 port->vdisk_type = pkt->vdisk_type;
278 if (vdc_version_supported(port, 1, 1)) {
279 port->vdisk_size = pkt->vdisk_size;
280 port->vdisk_mtype = pkt->vdisk_mtype;
282 if (pkt->max_xfer_size < port->max_xfer_size)
283 port->max_xfer_size = pkt->max_xfer_size;
284 port->vdisk_block_size = pkt->vdisk_block_size;
286 port->vdisk_phys_blksz = VDC_DEFAULT_BLK_SIZE;
287 if (vdc_version_supported(port, 1, 2))
288 port->vdisk_phys_blksz = pkt->phys_block_size;
292 printk(KERN_ERR PFX "%s: Attribute NACK\n", vio->name);
298 static void vdc_end_special(struct vdc_port *port, struct vio_disk_desc *desc)
300 int err = desc->status;
302 vdc_finish(&port->vio, -err, WAITING_FOR_GEN_CMD);
305 static void vdc_end_one(struct vdc_port *port, struct vio_dring_state *dr,
308 struct vio_disk_desc *desc = vio_dring_entry(dr, index);
309 struct vdc_req_entry *rqe = &port->rq_arr[index];
312 if (unlikely(desc->hdr.state != VIO_DESC_DONE))
315 ldc_unmap(port->vio.lp, desc->cookies, desc->ncookies);
316 desc->hdr.state = VIO_DESC_FREE;
317 dr->cons = vio_dring_next(dr, index);
321 vdc_end_special(port, desc);
327 blk_mq_end_request(req, desc->status ? BLK_STS_IOERR : 0);
329 vdc_blk_queue_start(port);
332 static int vdc_ack(struct vdc_port *port, void *msgbuf)
334 struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING];
335 struct vio_dring_data *pkt = msgbuf;
337 if (unlikely(pkt->dring_ident != dr->ident ||
338 pkt->start_idx != pkt->end_idx ||
339 pkt->start_idx >= VDC_TX_RING_SIZE))
342 vdc_end_one(port, dr, pkt->start_idx);
347 static int vdc_nack(struct vdc_port *port, void *msgbuf)
349 /* XXX Implement me XXX */
353 static void vdc_event(void *arg, int event)
355 struct vdc_port *port = arg;
356 struct vio_driver_state *vio = &port->vio;
360 spin_lock_irqsave(&vio->lock, flags);
362 if (unlikely(event == LDC_EVENT_RESET)) {
363 vio_link_state_change(vio, event);
364 queue_work(sunvdc_wq, &port->ldc_reset_work);
368 if (unlikely(event == LDC_EVENT_UP)) {
369 vio_link_state_change(vio, event);
373 if (unlikely(event != LDC_EVENT_DATA_READY)) {
374 pr_warn(PFX "Unexpected LDC event %d\n", event);
381 struct vio_msg_tag tag;
385 err = ldc_read(vio->lp, &msgbuf, sizeof(msgbuf));
386 if (unlikely(err < 0)) {
387 if (err == -ECONNRESET)
393 viodbg(DATA, "TAG [%02x:%02x:%04x:%08x]\n",
396 msgbuf.tag.stype_env,
398 err = vio_validate_sid(vio, &msgbuf.tag);
402 if (likely(msgbuf.tag.type == VIO_TYPE_DATA)) {
403 if (msgbuf.tag.stype == VIO_SUBTYPE_ACK)
404 err = vdc_ack(port, &msgbuf);
405 else if (msgbuf.tag.stype == VIO_SUBTYPE_NACK)
406 err = vdc_nack(port, &msgbuf);
408 err = vdc_handle_unknown(port, &msgbuf);
409 } else if (msgbuf.tag.type == VIO_TYPE_CTRL) {
410 err = vio_control_pkt_engine(vio, &msgbuf);
412 err = vdc_handle_unknown(port, &msgbuf);
418 vdc_finish(&port->vio, err, WAITING_FOR_ANY);
420 spin_unlock_irqrestore(&vio->lock, flags);
423 static int __vdc_tx_trigger(struct vdc_port *port)
425 struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING];
426 struct vio_dring_data hdr = {
428 .type = VIO_TYPE_DATA,
429 .stype = VIO_SUBTYPE_INFO,
430 .stype_env = VIO_DRING_DATA,
431 .sid = vio_send_sid(&port->vio),
433 .dring_ident = dr->ident,
434 .start_idx = dr->prod,
440 hdr.seq = dr->snd_nxt;
443 err = vio_ldc_send(&port->vio, &hdr, sizeof(hdr));
449 if ((delay <<= 1) > 128)
451 if (retries++ > VDC_MAX_RETRIES)
453 } while (err == -EAGAIN);
455 if (err == -ENOTCONN)
460 static int __send_request(struct request *req)
462 struct vdc_port *port = req->rq_disk->private_data;
463 struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING];
464 struct scatterlist sg[MAX_RING_COOKIES];
465 struct vdc_req_entry *rqe;
466 struct vio_disk_desc *desc;
467 unsigned int map_perm;
472 if (WARN_ON(port->ring_cookies > MAX_RING_COOKIES))
475 map_perm = LDC_MAP_SHADOW | LDC_MAP_DIRECT | LDC_MAP_IO;
477 if (rq_data_dir(req) == READ) {
478 map_perm |= LDC_MAP_W;
481 map_perm |= LDC_MAP_R;
485 sg_init_table(sg, port->ring_cookies);
486 nsg = blk_rq_map_sg(req->q, req, sg);
489 for (i = 0; i < nsg; i++)
492 desc = vio_dring_cur(dr);
494 err = ldc_map_sg(port->vio.lp, sg, nsg,
495 desc->cookies, port->ring_cookies,
498 printk(KERN_ERR PFX "ldc_map_sg() failure, err=%d.\n", err);
502 rqe = &port->rq_arr[dr->prod];
505 desc->hdr.ack = VIO_ACK_ENABLE;
506 desc->req_id = port->req_id;
507 desc->operation = op;
508 if (port->vdisk_type == VD_DISK_TYPE_DISK) {
514 desc->offset = (blk_rq_pos(req) << 9) / port->vdisk_block_size;
516 desc->ncookies = err;
518 /* This has to be a non-SMP write barrier because we are writing
519 * to memory which is shared with the peer LDOM.
522 desc->hdr.state = VIO_DESC_READY;
524 err = __vdc_tx_trigger(port);
526 printk(KERN_ERR PFX "vdc_tx_trigger() failure, err=%d\n", err);
529 dr->prod = vio_dring_next(dr, dr->prod);
535 static blk_status_t vdc_queue_rq(struct blk_mq_hw_ctx *hctx,
536 const struct blk_mq_queue_data *bd)
538 struct vdc_port *port = hctx->queue->queuedata;
539 struct vio_dring_state *dr;
542 dr = &port->vio.drings[VIO_DRIVER_TX_RING];
544 blk_mq_start_request(bd->rq);
546 spin_lock_irqsave(&port->vio.lock, flags);
549 * Doing drain, just end the request in error
551 if (unlikely(port->drain)) {
552 spin_unlock_irqrestore(&port->vio.lock, flags);
553 return BLK_STS_IOERR;
556 if (unlikely(vdc_tx_dring_avail(dr) < 1)) {
557 spin_unlock_irqrestore(&port->vio.lock, flags);
558 blk_mq_stop_hw_queue(hctx);
559 return BLK_STS_DEV_RESOURCE;
562 if (__send_request(bd->rq) < 0) {
563 spin_unlock_irqrestore(&port->vio.lock, flags);
564 return BLK_STS_IOERR;
567 spin_unlock_irqrestore(&port->vio.lock, flags);
571 static int generic_request(struct vdc_port *port, u8 op, void *buf, int len)
573 struct vio_dring_state *dr;
574 struct vio_completion comp;
575 struct vio_disk_desc *desc;
576 unsigned int map_perm;
581 if (!(((u64)1 << (u64)op) & port->operations))
596 op_len = sizeof(u32);
597 map_perm = LDC_MAP_W;
601 op_len = sizeof(u32);
602 map_perm = LDC_MAP_R;
606 op_len = sizeof(struct vio_disk_vtoc);
607 map_perm = LDC_MAP_W;
611 op_len = sizeof(struct vio_disk_vtoc);
612 map_perm = LDC_MAP_R;
615 case VD_OP_GET_DISKGEOM:
616 op_len = sizeof(struct vio_disk_geom);
617 map_perm = LDC_MAP_W;
620 case VD_OP_SET_DISKGEOM:
621 op_len = sizeof(struct vio_disk_geom);
622 map_perm = LDC_MAP_R;
627 map_perm = LDC_MAP_RW;
630 case VD_OP_GET_DEVID:
631 op_len = sizeof(struct vio_disk_devid);
632 map_perm = LDC_MAP_W;
640 map_perm |= LDC_MAP_SHADOW | LDC_MAP_DIRECT | LDC_MAP_IO;
642 op_len = (op_len + 7) & ~7;
643 req_buf = kzalloc(op_len, GFP_KERNEL);
650 if (map_perm & LDC_MAP_R)
651 memcpy(req_buf, buf, len);
653 spin_lock_irqsave(&port->vio.lock, flags);
655 dr = &port->vio.drings[VIO_DRIVER_TX_RING];
657 /* XXX If we want to use this code generically we have to
658 * XXX handle TX ring exhaustion etc.
660 desc = vio_dring_cur(dr);
662 err = ldc_map_single(port->vio.lp, req_buf, op_len,
663 desc->cookies, port->ring_cookies,
666 spin_unlock_irqrestore(&port->vio.lock, flags);
671 init_completion(&comp.com);
672 comp.waiting_for = WAITING_FOR_GEN_CMD;
673 port->vio.cmp = ∁
675 desc->hdr.ack = VIO_ACK_ENABLE;
676 desc->req_id = port->req_id;
677 desc->operation = op;
682 desc->ncookies = err;
684 /* This has to be a non-SMP write barrier because we are writing
685 * to memory which is shared with the peer LDOM.
688 desc->hdr.state = VIO_DESC_READY;
690 err = __vdc_tx_trigger(port);
693 dr->prod = vio_dring_next(dr, dr->prod);
694 spin_unlock_irqrestore(&port->vio.lock, flags);
696 wait_for_completion(&comp.com);
699 port->vio.cmp = NULL;
700 spin_unlock_irqrestore(&port->vio.lock, flags);
703 if (map_perm & LDC_MAP_W)
704 memcpy(buf, req_buf, len);
711 static int vdc_alloc_tx_ring(struct vdc_port *port)
713 struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING];
714 unsigned long len, entry_size;
718 entry_size = sizeof(struct vio_disk_desc) +
719 (sizeof(struct ldc_trans_cookie) * port->ring_cookies);
720 len = (VDC_TX_RING_SIZE * entry_size);
722 ncookies = VIO_MAX_RING_COOKIES;
723 dring = ldc_alloc_exp_dring(port->vio.lp, len,
724 dr->cookies, &ncookies,
729 return PTR_ERR(dring);
732 dr->entry_size = entry_size;
733 dr->num_entries = VDC_TX_RING_SIZE;
734 dr->prod = dr->cons = 0;
735 dr->pending = VDC_TX_RING_SIZE;
736 dr->ncookies = ncookies;
741 static void vdc_free_tx_ring(struct vdc_port *port)
743 struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING];
746 ldc_free_exp_dring(port->vio.lp, dr->base,
747 (dr->entry_size * dr->num_entries),
748 dr->cookies, dr->ncookies);
757 static int vdc_port_up(struct vdc_port *port)
759 struct vio_completion comp;
761 init_completion(&comp.com);
763 comp.waiting_for = WAITING_FOR_LINK_UP;
764 port->vio.cmp = ∁
766 vio_port_up(&port->vio);
767 wait_for_completion(&comp.com);
771 static void vdc_port_down(struct vdc_port *port)
773 ldc_disconnect(port->vio.lp);
774 ldc_unbind(port->vio.lp);
775 vdc_free_tx_ring(port);
776 vio_ldc_free(&port->vio);
779 static const struct blk_mq_ops vdc_mq_ops = {
780 .queue_rq = vdc_queue_rq,
783 static int probe_disk(struct vdc_port *port)
785 struct request_queue *q;
789 err = vdc_port_up(port);
793 /* Using version 1.2 means vdisk_phys_blksz should be set unless the
794 * disk is reserved by another system.
796 if (vdc_version_supported(port, 1, 2) && !port->vdisk_phys_blksz)
799 if (vdc_version_supported(port, 1, 1)) {
800 /* vdisk_size should be set during the handshake, if it wasn't
801 * then the underlying disk is reserved by another system
803 if (port->vdisk_size == -1)
806 struct vio_disk_geom geom;
808 err = generic_request(port, VD_OP_GET_DISKGEOM,
809 &geom, sizeof(geom));
811 printk(KERN_ERR PFX "VD_OP_GET_DISKGEOM returns "
815 port->vdisk_size = ((u64)geom.num_cyl *
820 err = blk_mq_alloc_sq_tag_set(&port->tag_set, &vdc_mq_ops,
821 VDC_TX_RING_SIZE, BLK_MQ_F_SHOULD_MERGE);
825 g = blk_mq_alloc_disk(&port->tag_set, port);
827 printk(KERN_ERR PFX "%s: Could not allocate gendisk.\n",
829 blk_mq_free_tag_set(&port->tag_set);
836 /* Each segment in a request is up to an aligned page in size. */
837 blk_queue_segment_boundary(q, PAGE_SIZE - 1);
838 blk_queue_max_segment_size(q, PAGE_SIZE);
840 blk_queue_max_segments(q, port->ring_cookies);
841 blk_queue_max_hw_sectors(q, port->max_xfer_size);
842 g->major = vdc_major;
843 g->first_minor = port->vio.vdev->dev_no << PARTITION_SHIFT;
844 g->minors = 1 << PARTITION_SHIFT;
845 strcpy(g->disk_name, port->disk_name);
849 g->private_data = port;
851 set_capacity(g, port->vdisk_size);
853 if (vdc_version_supported(port, 1, 1)) {
854 switch (port->vdisk_mtype) {
855 case VD_MEDIA_TYPE_CD:
856 pr_info(PFX "Virtual CDROM %s\n", port->disk_name);
857 g->flags |= GENHD_FL_CD;
858 g->flags |= GENHD_FL_REMOVABLE;
862 case VD_MEDIA_TYPE_DVD:
863 pr_info(PFX "Virtual DVD %s\n", port->disk_name);
864 g->flags |= GENHD_FL_CD;
865 g->flags |= GENHD_FL_REMOVABLE;
869 case VD_MEDIA_TYPE_FIXED:
870 pr_info(PFX "Virtual Hard disk %s\n", port->disk_name);
875 blk_queue_physical_block_size(q, port->vdisk_phys_blksz);
877 pr_info(PFX "%s: %u sectors (%u MB) protocol %d.%d\n",
879 port->vdisk_size, (port->vdisk_size >> (20 - 9)),
880 port->vio.ver.major, port->vio.ver.minor);
882 device_add_disk(&port->vio.vdev->dev, g, NULL);
887 static struct ldc_channel_config vdc_ldc_cfg = {
890 .mode = LDC_MODE_UNRELIABLE,
893 static struct vio_driver_ops vdc_vio_ops = {
894 .send_attr = vdc_send_attr,
895 .handle_attr = vdc_handle_attr,
896 .handshake_complete = vdc_handshake_complete,
899 static void print_version(void)
901 static int version_printed;
903 if (version_printed++ == 0)
904 printk(KERN_INFO "%s", version);
907 struct vdc_check_port_data {
912 static int vdc_device_probed(struct device *dev, void *arg)
914 struct vio_dev *vdev = to_vio_dev(dev);
915 struct vdc_check_port_data *port_data;
917 port_data = (struct vdc_check_port_data *)arg;
919 if ((vdev->dev_no == port_data->dev_no) &&
920 (!(strcmp((char *)&vdev->type, port_data->type))) &&
921 dev_get_drvdata(dev)) {
922 /* This device has already been configured
923 * by vdc_port_probe()
931 /* Determine whether the VIO device is part of an mpgroup
932 * by locating all the virtual-device-port nodes associated
933 * with the parent virtual-device node for the VIO device
934 * and checking whether any of these nodes are vdc-ports
935 * which have already been configured.
937 * Returns true if this device is part of an mpgroup and has
938 * already been probed.
940 static bool vdc_port_mpgroup_check(struct vio_dev *vdev)
942 struct vdc_check_port_data port_data;
945 port_data.dev_no = vdev->dev_no;
946 port_data.type = (char *)&vdev->type;
948 dev = device_find_child(vdev->dev.parent, &port_data,
957 static int vdc_port_probe(struct vio_dev *vdev, const struct vio_device_id *id)
959 struct mdesc_handle *hp;
960 struct vdc_port *port;
962 const u64 *ldc_timeout;
969 if ((vdev->dev_no << PARTITION_SHIFT) & ~(u64)MINORMASK) {
970 printk(KERN_ERR PFX "Port id [%llu] too large.\n",
972 goto err_out_release_mdesc;
975 /* Check if this device is part of an mpgroup */
976 if (vdc_port_mpgroup_check(vdev)) {
978 "VIO: Ignoring extra vdisk port %s",
979 dev_name(&vdev->dev));
980 goto err_out_release_mdesc;
983 port = kzalloc(sizeof(*port), GFP_KERNEL);
986 goto err_out_release_mdesc;
989 if (vdev->dev_no >= 26)
990 snprintf(port->disk_name, sizeof(port->disk_name),
992 'a' + ((int)vdev->dev_no / 26) - 1,
993 'a' + ((int)vdev->dev_no % 26));
995 snprintf(port->disk_name, sizeof(port->disk_name),
996 VDCBLK_NAME "%c", 'a' + ((int)vdev->dev_no % 26));
997 port->vdisk_size = -1;
999 /* Actual wall time may be double due to do_generic_file_read() doing
1000 * a readahead I/O first, and once that fails it will try to read a
1003 ldc_timeout = mdesc_get_property(hp, vdev->mp, "vdc-timeout", NULL);
1004 port->ldc_timeout = ldc_timeout ? *ldc_timeout : 0;
1005 INIT_DELAYED_WORK(&port->ldc_reset_timer_work, vdc_ldc_reset_timer_work);
1006 INIT_WORK(&port->ldc_reset_work, vdc_ldc_reset_work);
1008 err = vio_driver_init(&port->vio, vdev, VDEV_DISK,
1009 vdc_versions, ARRAY_SIZE(vdc_versions),
1010 &vdc_vio_ops, port->disk_name);
1012 goto err_out_free_port;
1014 port->vdisk_block_size = VDC_DEFAULT_BLK_SIZE;
1015 port->max_xfer_size = MAX_XFER_SIZE;
1016 port->ring_cookies = MAX_RING_COOKIES;
1018 err = vio_ldc_alloc(&port->vio, &vdc_ldc_cfg, port);
1020 goto err_out_free_port;
1022 err = vdc_alloc_tx_ring(port);
1024 goto err_out_free_ldc;
1026 err = probe_disk(port);
1028 goto err_out_free_tx_ring;
1030 /* Note that the device driver_data is used to determine
1031 * whether the port has been probed.
1033 dev_set_drvdata(&vdev->dev, port);
1039 err_out_free_tx_ring:
1040 vdc_free_tx_ring(port);
1043 vio_ldc_free(&port->vio);
1048 err_out_release_mdesc:
1053 static void vdc_port_remove(struct vio_dev *vdev)
1055 struct vdc_port *port = dev_get_drvdata(&vdev->dev);
1058 blk_mq_stop_hw_queues(port->disk->queue);
1060 flush_work(&port->ldc_reset_work);
1061 cancel_delayed_work_sync(&port->ldc_reset_timer_work);
1062 del_timer_sync(&port->vio.timer);
1064 del_gendisk(port->disk);
1065 blk_cleanup_disk(port->disk);
1066 blk_mq_free_tag_set(&port->tag_set);
1068 vdc_free_tx_ring(port);
1069 vio_ldc_free(&port->vio);
1071 dev_set_drvdata(&vdev->dev, NULL);
1077 static void vdc_requeue_inflight(struct vdc_port *port)
1079 struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING];
1082 for (idx = dr->cons; idx != dr->prod; idx = vio_dring_next(dr, idx)) {
1083 struct vio_disk_desc *desc = vio_dring_entry(dr, idx);
1084 struct vdc_req_entry *rqe = &port->rq_arr[idx];
1085 struct request *req;
1087 ldc_unmap(port->vio.lp, desc->cookies, desc->ncookies);
1088 desc->hdr.state = VIO_DESC_FREE;
1089 dr->cons = vio_dring_next(dr, idx);
1093 vdc_end_special(port, desc);
1098 blk_mq_requeue_request(req, false);
1102 static void vdc_queue_drain(struct vdc_port *port)
1104 struct request_queue *q = port->disk->queue;
1107 * Mark the queue as draining, then freeze/quiesce to ensure
1108 * that all existing requests are seen in ->queue_rq() and killed
1111 spin_unlock_irq(&port->vio.lock);
1113 blk_mq_freeze_queue(q);
1114 blk_mq_quiesce_queue(q);
1116 spin_lock_irq(&port->vio.lock);
1118 blk_mq_unquiesce_queue(q);
1119 blk_mq_unfreeze_queue(q);
1122 static void vdc_ldc_reset_timer_work(struct work_struct *work)
1124 struct vdc_port *port;
1125 struct vio_driver_state *vio;
1127 port = container_of(work, struct vdc_port, ldc_reset_timer_work.work);
1130 spin_lock_irq(&vio->lock);
1131 if (!(port->vio.hs_state & VIO_HS_COMPLETE)) {
1132 pr_warn(PFX "%s ldc down %llu seconds, draining queue\n",
1133 port->disk_name, port->ldc_timeout);
1134 vdc_queue_drain(port);
1135 vdc_blk_queue_start(port);
1137 spin_unlock_irq(&vio->lock);
1140 static void vdc_ldc_reset_work(struct work_struct *work)
1142 struct vdc_port *port;
1143 struct vio_driver_state *vio;
1144 unsigned long flags;
1146 port = container_of(work, struct vdc_port, ldc_reset_work);
1149 spin_lock_irqsave(&vio->lock, flags);
1150 vdc_ldc_reset(port);
1151 spin_unlock_irqrestore(&vio->lock, flags);
1154 static void vdc_ldc_reset(struct vdc_port *port)
1158 assert_spin_locked(&port->vio.lock);
1160 pr_warn(PFX "%s ldc link reset\n", port->disk_name);
1161 blk_mq_stop_hw_queues(port->disk->queue);
1162 vdc_requeue_inflight(port);
1163 vdc_port_down(port);
1165 err = vio_ldc_alloc(&port->vio, &vdc_ldc_cfg, port);
1167 pr_err(PFX "%s vio_ldc_alloc:%d\n", port->disk_name, err);
1171 err = vdc_alloc_tx_ring(port);
1173 pr_err(PFX "%s vio_alloc_tx_ring:%d\n", port->disk_name, err);
1177 if (port->ldc_timeout)
1178 mod_delayed_work(system_wq, &port->ldc_reset_timer_work,
1179 round_jiffies(jiffies + HZ * port->ldc_timeout));
1180 mod_timer(&port->vio.timer, round_jiffies(jiffies + HZ));
1184 vio_ldc_free(&port->vio);
1187 static const struct vio_device_id vdc_port_match[] = {
1193 MODULE_DEVICE_TABLE(vio, vdc_port_match);
1195 static struct vio_driver vdc_port_driver = {
1196 .id_table = vdc_port_match,
1197 .probe = vdc_port_probe,
1198 .remove = vdc_port_remove,
1202 static int __init vdc_init(void)
1206 sunvdc_wq = alloc_workqueue("sunvdc", 0, 0);
1210 err = register_blkdev(0, VDCBLK_NAME);
1216 err = vio_register_driver(&vdc_port_driver);
1218 goto out_unregister_blkdev;
1222 out_unregister_blkdev:
1223 unregister_blkdev(vdc_major, VDCBLK_NAME);
1227 destroy_workqueue(sunvdc_wq);
1231 static void __exit vdc_exit(void)
1233 vio_unregister_driver(&vdc_port_driver);
1234 unregister_blkdev(vdc_major, VDCBLK_NAME);
1235 destroy_workqueue(sunvdc_wq);
1238 module_init(vdc_init);
1239 module_exit(vdc_exit);