1 /* sunvdc.c: Sun LDOM Virtual Disk Client.
3 * Copyright (C) 2007, 2008 David S. Miller <davem@davemloft.net>
6 #include <linux/module.h>
7 #include <linux/kernel.h>
8 #include <linux/types.h>
9 #include <linux/blkdev.h>
10 #include <linux/hdreg.h>
11 #include <linux/genhd.h>
12 #include <linux/cdrom.h>
13 #include <linux/slab.h>
14 #include <linux/spinlock.h>
15 #include <linux/completion.h>
16 #include <linux/delay.h>
17 #include <linux/init.h>
18 #include <linux/list.h>
19 #include <linux/scatterlist.h>
24 #define DRV_MODULE_NAME "sunvdc"
25 #define PFX DRV_MODULE_NAME ": "
26 #define DRV_MODULE_VERSION "1.2"
27 #define DRV_MODULE_RELDATE "November 24, 2014"
29 static char version[] =
30 DRV_MODULE_NAME ".c:v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")\n";
31 MODULE_AUTHOR("David S. Miller (davem@davemloft.net)");
32 MODULE_DESCRIPTION("Sun LDOM virtual disk client driver");
33 MODULE_LICENSE("GPL");
34 MODULE_VERSION(DRV_MODULE_VERSION);
36 #define VDC_TX_RING_SIZE 512
37 #define VDC_DEFAULT_BLK_SIZE 512
39 #define WAITING_FOR_LINK_UP 0x01
40 #define WAITING_FOR_TX_SPACE 0x02
41 #define WAITING_FOR_GEN_CMD 0x04
42 #define WAITING_FOR_ANY -1
44 #define VDC_MAX_RETRIES 10
46 static struct workqueue_struct *sunvdc_wq;
48 struct vdc_req_entry {
53 struct vio_driver_state vio;
57 struct vdc_completion *cmp;
61 struct vdc_req_entry rq_arr[VDC_TX_RING_SIZE];
63 unsigned long ring_cookies;
69 struct timer_list ldc_reset_timer;
70 struct work_struct ldc_reset_work;
72 /* The server fills these in for us in the disk attribute
84 static void vdc_ldc_reset(struct vdc_port *port);
85 static void vdc_ldc_reset_work(struct work_struct *work);
86 static void vdc_ldc_reset_timer(unsigned long _arg);
88 static inline struct vdc_port *to_vdc_port(struct vio_driver_state *vio)
90 return container_of(vio, struct vdc_port, vio);
93 /* Ordered from largest major to lowest */
94 static struct vio_version vdc_versions[] = {
95 { .major = 1, .minor = 2 },
96 { .major = 1, .minor = 1 },
97 { .major = 1, .minor = 0 },
100 static inline int vdc_version_supported(struct vdc_port *port,
101 u16 major, u16 minor)
103 return port->vio.ver.major == major && port->vio.ver.minor >= minor;
106 #define VDCBLK_NAME "vdisk"
107 static int vdc_major;
108 #define PARTITION_SHIFT 3
110 static inline u32 vdc_tx_dring_avail(struct vio_dring_state *dr)
112 return vio_dring_avail(dr, VDC_TX_RING_SIZE);
115 static int vdc_getgeo(struct block_device *bdev, struct hd_geometry *geo)
117 struct gendisk *disk = bdev->bd_disk;
118 sector_t nsect = get_capacity(disk);
119 sector_t cylinders = nsect;
123 sector_div(cylinders, geo->heads * geo->sectors);
124 geo->cylinders = cylinders;
125 if ((sector_t)(geo->cylinders + 1) * geo->heads * geo->sectors < nsect)
126 geo->cylinders = 0xffff;
131 /* Add ioctl/CDROM_GET_CAPABILITY to support cdrom_id in udev
132 * when vdisk_mtype is VD_MEDIA_TYPE_CD or VD_MEDIA_TYPE_DVD.
133 * Needed to be able to install inside an ldom from an iso image.
135 static int vdc_ioctl(struct block_device *bdev, fmode_t mode,
136 unsigned command, unsigned long argument)
139 struct gendisk *disk;
142 case CDROMMULTISESSION:
143 pr_debug(PFX "Multisession CDs not supported\n");
144 for (i = 0; i < sizeof(struct cdrom_multisession); i++)
145 if (put_user(0, (char __user *)(argument + i)))
149 case CDROM_GET_CAPABILITY:
150 disk = bdev->bd_disk;
152 if (bdev->bd_disk && (disk->flags & GENHD_FL_CD))
157 pr_debug(PFX "ioctl %08x not supported\n", command);
162 static const struct block_device_operations vdc_fops = {
163 .owner = THIS_MODULE,
164 .getgeo = vdc_getgeo,
168 static void vdc_blk_queue_start(struct vdc_port *port)
170 struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING];
172 /* restart blk queue when ring is half emptied. also called after
173 * handshake completes, so check for initial handshake before we've
176 if (port->disk && blk_queue_stopped(port->disk->queue) &&
177 vdc_tx_dring_avail(dr) * 100 / VDC_TX_RING_SIZE >= 50) {
178 blk_start_queue(port->disk->queue);
183 static void vdc_finish(struct vio_driver_state *vio, int err, int waiting_for)
186 (waiting_for == -1 ||
187 vio->cmp->waiting_for == waiting_for)) {
189 complete(&vio->cmp->com);
194 static void vdc_handshake_complete(struct vio_driver_state *vio)
196 struct vdc_port *port = to_vdc_port(vio);
198 del_timer(&port->ldc_reset_timer);
199 vdc_finish(vio, 0, WAITING_FOR_LINK_UP);
200 vdc_blk_queue_start(port);
203 static int vdc_handle_unknown(struct vdc_port *port, void *arg)
205 struct vio_msg_tag *pkt = arg;
207 printk(KERN_ERR PFX "Received unknown msg [%02x:%02x:%04x:%08x]\n",
208 pkt->type, pkt->stype, pkt->stype_env, pkt->sid);
209 printk(KERN_ERR PFX "Resetting connection.\n");
211 ldc_disconnect(port->vio.lp);
216 static int vdc_send_attr(struct vio_driver_state *vio)
218 struct vdc_port *port = to_vdc_port(vio);
219 struct vio_disk_attr_info pkt;
221 memset(&pkt, 0, sizeof(pkt));
223 pkt.tag.type = VIO_TYPE_CTRL;
224 pkt.tag.stype = VIO_SUBTYPE_INFO;
225 pkt.tag.stype_env = VIO_ATTR_INFO;
226 pkt.tag.sid = vio_send_sid(vio);
228 pkt.xfer_mode = VIO_DRING_MODE;
229 pkt.vdisk_block_size = port->vdisk_block_size;
230 pkt.max_xfer_size = port->max_xfer_size;
232 viodbg(HS, "SEND ATTR xfer_mode[0x%x] blksz[%u] max_xfer[%llu]\n",
233 pkt.xfer_mode, pkt.vdisk_block_size, pkt.max_xfer_size);
235 return vio_ldc_send(&port->vio, &pkt, sizeof(pkt));
238 static int vdc_handle_attr(struct vio_driver_state *vio, void *arg)
240 struct vdc_port *port = to_vdc_port(vio);
241 struct vio_disk_attr_info *pkt = arg;
243 viodbg(HS, "GOT ATTR stype[0x%x] ops[%llx] disk_size[%llu] disk_type[%x] "
244 "mtype[0x%x] xfer_mode[0x%x] blksz[%u] max_xfer[%llu]\n",
245 pkt->tag.stype, pkt->operations,
246 pkt->vdisk_size, pkt->vdisk_type, pkt->vdisk_mtype,
247 pkt->xfer_mode, pkt->vdisk_block_size,
250 if (pkt->tag.stype == VIO_SUBTYPE_ACK) {
251 switch (pkt->vdisk_type) {
252 case VD_DISK_TYPE_DISK:
253 case VD_DISK_TYPE_SLICE:
257 printk(KERN_ERR PFX "%s: Bogus vdisk_type 0x%x\n",
258 vio->name, pkt->vdisk_type);
262 if (pkt->vdisk_block_size > port->vdisk_block_size) {
263 printk(KERN_ERR PFX "%s: BLOCK size increased "
266 port->vdisk_block_size, pkt->vdisk_block_size);
270 port->operations = pkt->operations;
271 port->vdisk_type = pkt->vdisk_type;
272 if (vdc_version_supported(port, 1, 1)) {
273 port->vdisk_size = pkt->vdisk_size;
274 port->vdisk_mtype = pkt->vdisk_mtype;
276 if (pkt->max_xfer_size < port->max_xfer_size)
277 port->max_xfer_size = pkt->max_xfer_size;
278 port->vdisk_block_size = pkt->vdisk_block_size;
280 port->vdisk_phys_blksz = VDC_DEFAULT_BLK_SIZE;
281 if (vdc_version_supported(port, 1, 2))
282 port->vdisk_phys_blksz = pkt->phys_block_size;
286 printk(KERN_ERR PFX "%s: Attribute NACK\n", vio->name);
292 static void vdc_end_special(struct vdc_port *port, struct vio_disk_desc *desc)
294 int err = desc->status;
296 vdc_finish(&port->vio, -err, WAITING_FOR_GEN_CMD);
299 static void vdc_end_one(struct vdc_port *port, struct vio_dring_state *dr,
302 struct vio_disk_desc *desc = vio_dring_entry(dr, index);
303 struct vdc_req_entry *rqe = &port->rq_arr[index];
306 if (unlikely(desc->hdr.state != VIO_DESC_DONE))
309 ldc_unmap(port->vio.lp, desc->cookies, desc->ncookies);
310 desc->hdr.state = VIO_DESC_FREE;
311 dr->cons = vio_dring_next(dr, index);
315 vdc_end_special(port, desc);
321 __blk_end_request(req, (desc->status ? BLK_STS_IOERR : 0), desc->size);
323 vdc_blk_queue_start(port);
326 static int vdc_ack(struct vdc_port *port, void *msgbuf)
328 struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING];
329 struct vio_dring_data *pkt = msgbuf;
331 if (unlikely(pkt->dring_ident != dr->ident ||
332 pkt->start_idx != pkt->end_idx ||
333 pkt->start_idx >= VDC_TX_RING_SIZE))
336 vdc_end_one(port, dr, pkt->start_idx);
341 static int vdc_nack(struct vdc_port *port, void *msgbuf)
343 /* XXX Implement me XXX */
347 static void vdc_event(void *arg, int event)
349 struct vdc_port *port = arg;
350 struct vio_driver_state *vio = &port->vio;
354 spin_lock_irqsave(&vio->lock, flags);
356 if (unlikely(event == LDC_EVENT_RESET)) {
357 vio_link_state_change(vio, event);
358 queue_work(sunvdc_wq, &port->ldc_reset_work);
362 if (unlikely(event == LDC_EVENT_UP)) {
363 vio_link_state_change(vio, event);
367 if (unlikely(event != LDC_EVENT_DATA_READY)) {
368 pr_warn(PFX "Unexpected LDC event %d\n", event);
375 struct vio_msg_tag tag;
379 err = ldc_read(vio->lp, &msgbuf, sizeof(msgbuf));
380 if (unlikely(err < 0)) {
381 if (err == -ECONNRESET)
387 viodbg(DATA, "TAG [%02x:%02x:%04x:%08x]\n",
390 msgbuf.tag.stype_env,
392 err = vio_validate_sid(vio, &msgbuf.tag);
396 if (likely(msgbuf.tag.type == VIO_TYPE_DATA)) {
397 if (msgbuf.tag.stype == VIO_SUBTYPE_ACK)
398 err = vdc_ack(port, &msgbuf);
399 else if (msgbuf.tag.stype == VIO_SUBTYPE_NACK)
400 err = vdc_nack(port, &msgbuf);
402 err = vdc_handle_unknown(port, &msgbuf);
403 } else if (msgbuf.tag.type == VIO_TYPE_CTRL) {
404 err = vio_control_pkt_engine(vio, &msgbuf);
406 err = vdc_handle_unknown(port, &msgbuf);
412 vdc_finish(&port->vio, err, WAITING_FOR_ANY);
414 spin_unlock_irqrestore(&vio->lock, flags);
417 static int __vdc_tx_trigger(struct vdc_port *port)
419 struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING];
420 struct vio_dring_data hdr = {
422 .type = VIO_TYPE_DATA,
423 .stype = VIO_SUBTYPE_INFO,
424 .stype_env = VIO_DRING_DATA,
425 .sid = vio_send_sid(&port->vio),
427 .dring_ident = dr->ident,
428 .start_idx = dr->prod,
434 hdr.seq = dr->snd_nxt;
437 err = vio_ldc_send(&port->vio, &hdr, sizeof(hdr));
443 if ((delay <<= 1) > 128)
445 if (retries++ > VDC_MAX_RETRIES)
447 } while (err == -EAGAIN);
449 if (err == -ENOTCONN)
454 static int __send_request(struct request *req)
456 struct vdc_port *port = req->rq_disk->private_data;
457 struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING];
458 struct scatterlist sg[port->ring_cookies];
459 struct vdc_req_entry *rqe;
460 struct vio_disk_desc *desc;
461 unsigned int map_perm;
466 map_perm = LDC_MAP_SHADOW | LDC_MAP_DIRECT | LDC_MAP_IO;
468 if (rq_data_dir(req) == READ) {
469 map_perm |= LDC_MAP_W;
472 map_perm |= LDC_MAP_R;
476 sg_init_table(sg, port->ring_cookies);
477 nsg = blk_rq_map_sg(req->q, req, sg);
480 for (i = 0; i < nsg; i++)
483 desc = vio_dring_cur(dr);
485 err = ldc_map_sg(port->vio.lp, sg, nsg,
486 desc->cookies, port->ring_cookies,
489 printk(KERN_ERR PFX "ldc_map_sg() failure, err=%d.\n", err);
493 rqe = &port->rq_arr[dr->prod];
496 desc->hdr.ack = VIO_ACK_ENABLE;
497 desc->req_id = port->req_id;
498 desc->operation = op;
499 if (port->vdisk_type == VD_DISK_TYPE_DISK) {
505 desc->offset = (blk_rq_pos(req) << 9) / port->vdisk_block_size;
507 desc->ncookies = err;
509 /* This has to be a non-SMP write barrier because we are writing
510 * to memory which is shared with the peer LDOM.
513 desc->hdr.state = VIO_DESC_READY;
515 err = __vdc_tx_trigger(port);
517 printk(KERN_ERR PFX "vdc_tx_trigger() failure, err=%d\n", err);
520 dr->prod = vio_dring_next(dr, dr->prod);
526 static void do_vdc_request(struct request_queue *rq)
530 while ((req = blk_peek_request(rq)) != NULL) {
531 struct vdc_port *port;
532 struct vio_dring_state *dr;
534 port = req->rq_disk->private_data;
535 dr = &port->vio.drings[VIO_DRIVER_TX_RING];
536 if (unlikely(vdc_tx_dring_avail(dr) < 1))
539 blk_start_request(req);
541 if (__send_request(req) < 0) {
542 blk_requeue_request(rq, req);
544 /* Avoid pointless unplugs. */
551 static int generic_request(struct vdc_port *port, u8 op, void *buf, int len)
553 struct vio_dring_state *dr;
554 struct vio_completion comp;
555 struct vio_disk_desc *desc;
556 unsigned int map_perm;
561 if (!(((u64)1 << (u64)op) & port->operations))
576 op_len = sizeof(u32);
577 map_perm = LDC_MAP_W;
581 op_len = sizeof(u32);
582 map_perm = LDC_MAP_R;
586 op_len = sizeof(struct vio_disk_vtoc);
587 map_perm = LDC_MAP_W;
591 op_len = sizeof(struct vio_disk_vtoc);
592 map_perm = LDC_MAP_R;
595 case VD_OP_GET_DISKGEOM:
596 op_len = sizeof(struct vio_disk_geom);
597 map_perm = LDC_MAP_W;
600 case VD_OP_SET_DISKGEOM:
601 op_len = sizeof(struct vio_disk_geom);
602 map_perm = LDC_MAP_R;
607 map_perm = LDC_MAP_RW;
610 case VD_OP_GET_DEVID:
611 op_len = sizeof(struct vio_disk_devid);
612 map_perm = LDC_MAP_W;
621 map_perm |= LDC_MAP_SHADOW | LDC_MAP_DIRECT | LDC_MAP_IO;
623 op_len = (op_len + 7) & ~7;
624 req_buf = kzalloc(op_len, GFP_KERNEL);
631 if (map_perm & LDC_MAP_R)
632 memcpy(req_buf, buf, len);
634 spin_lock_irqsave(&port->vio.lock, flags);
636 dr = &port->vio.drings[VIO_DRIVER_TX_RING];
638 /* XXX If we want to use this code generically we have to
639 * XXX handle TX ring exhaustion etc.
641 desc = vio_dring_cur(dr);
643 err = ldc_map_single(port->vio.lp, req_buf, op_len,
644 desc->cookies, port->ring_cookies,
647 spin_unlock_irqrestore(&port->vio.lock, flags);
652 init_completion(&comp.com);
653 comp.waiting_for = WAITING_FOR_GEN_CMD;
654 port->vio.cmp = ∁
656 desc->hdr.ack = VIO_ACK_ENABLE;
657 desc->req_id = port->req_id;
658 desc->operation = op;
663 desc->ncookies = err;
665 /* This has to be a non-SMP write barrier because we are writing
666 * to memory which is shared with the peer LDOM.
669 desc->hdr.state = VIO_DESC_READY;
671 err = __vdc_tx_trigger(port);
674 dr->prod = vio_dring_next(dr, dr->prod);
675 spin_unlock_irqrestore(&port->vio.lock, flags);
677 wait_for_completion(&comp.com);
680 port->vio.cmp = NULL;
681 spin_unlock_irqrestore(&port->vio.lock, flags);
684 if (map_perm & LDC_MAP_W)
685 memcpy(buf, req_buf, len);
692 static int vdc_alloc_tx_ring(struct vdc_port *port)
694 struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING];
695 unsigned long len, entry_size;
699 entry_size = sizeof(struct vio_disk_desc) +
700 (sizeof(struct ldc_trans_cookie) * port->ring_cookies);
701 len = (VDC_TX_RING_SIZE * entry_size);
703 ncookies = VIO_MAX_RING_COOKIES;
704 dring = ldc_alloc_exp_dring(port->vio.lp, len,
705 dr->cookies, &ncookies,
710 return PTR_ERR(dring);
713 dr->entry_size = entry_size;
714 dr->num_entries = VDC_TX_RING_SIZE;
715 dr->prod = dr->cons = 0;
716 dr->pending = VDC_TX_RING_SIZE;
717 dr->ncookies = ncookies;
722 static void vdc_free_tx_ring(struct vdc_port *port)
724 struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING];
727 ldc_free_exp_dring(port->vio.lp, dr->base,
728 (dr->entry_size * dr->num_entries),
729 dr->cookies, dr->ncookies);
738 static int vdc_port_up(struct vdc_port *port)
740 struct vio_completion comp;
742 init_completion(&comp.com);
744 comp.waiting_for = WAITING_FOR_LINK_UP;
745 port->vio.cmp = ∁
747 vio_port_up(&port->vio);
748 wait_for_completion(&comp.com);
752 static void vdc_port_down(struct vdc_port *port)
754 ldc_disconnect(port->vio.lp);
755 ldc_unbind(port->vio.lp);
756 vdc_free_tx_ring(port);
757 vio_ldc_free(&port->vio);
760 static int probe_disk(struct vdc_port *port)
762 struct request_queue *q;
766 err = vdc_port_up(port);
770 /* Using version 1.2 means vdisk_phys_blksz should be set unless the
771 * disk is reserved by another system.
773 if (vdc_version_supported(port, 1, 2) && !port->vdisk_phys_blksz)
776 if (vdc_version_supported(port, 1, 1)) {
777 /* vdisk_size should be set during the handshake, if it wasn't
778 * then the underlying disk is reserved by another system
780 if (port->vdisk_size == -1)
783 struct vio_disk_geom geom;
785 err = generic_request(port, VD_OP_GET_DISKGEOM,
786 &geom, sizeof(geom));
788 printk(KERN_ERR PFX "VD_OP_GET_DISKGEOM returns "
792 port->vdisk_size = ((u64)geom.num_cyl *
797 q = blk_init_queue(do_vdc_request, &port->vio.lock);
799 printk(KERN_ERR PFX "%s: Could not allocate queue.\n",
803 g = alloc_disk(1 << PARTITION_SHIFT);
805 printk(KERN_ERR PFX "%s: Could not allocate gendisk.\n",
807 blk_cleanup_queue(q);
813 /* Each segment in a request is up to an aligned page in size. */
814 blk_queue_segment_boundary(q, PAGE_SIZE - 1);
815 blk_queue_max_segment_size(q, PAGE_SIZE);
817 blk_queue_max_segments(q, port->ring_cookies);
818 blk_queue_max_hw_sectors(q, port->max_xfer_size);
819 g->major = vdc_major;
820 g->first_minor = port->vio.vdev->dev_no << PARTITION_SHIFT;
821 strcpy(g->disk_name, port->disk_name);
825 g->private_data = port;
827 set_capacity(g, port->vdisk_size);
829 if (vdc_version_supported(port, 1, 1)) {
830 switch (port->vdisk_mtype) {
831 case VD_MEDIA_TYPE_CD:
832 pr_info(PFX "Virtual CDROM %s\n", port->disk_name);
833 g->flags |= GENHD_FL_CD;
834 g->flags |= GENHD_FL_REMOVABLE;
838 case VD_MEDIA_TYPE_DVD:
839 pr_info(PFX "Virtual DVD %s\n", port->disk_name);
840 g->flags |= GENHD_FL_CD;
841 g->flags |= GENHD_FL_REMOVABLE;
845 case VD_MEDIA_TYPE_FIXED:
846 pr_info(PFX "Virtual Hard disk %s\n", port->disk_name);
851 blk_queue_physical_block_size(q, port->vdisk_phys_blksz);
853 pr_info(PFX "%s: %u sectors (%u MB) protocol %d.%d\n",
855 port->vdisk_size, (port->vdisk_size >> (20 - 9)),
856 port->vio.ver.major, port->vio.ver.minor);
858 device_add_disk(&port->vio.vdev->dev, g);
863 static struct ldc_channel_config vdc_ldc_cfg = {
866 .mode = LDC_MODE_UNRELIABLE,
869 static struct vio_driver_ops vdc_vio_ops = {
870 .send_attr = vdc_send_attr,
871 .handle_attr = vdc_handle_attr,
872 .handshake_complete = vdc_handshake_complete,
875 static void print_version(void)
877 static int version_printed;
879 if (version_printed++ == 0)
880 printk(KERN_INFO "%s", version);
883 struct vdc_check_port_data {
888 static int vdc_device_probed(struct device *dev, void *arg)
890 struct vio_dev *vdev = to_vio_dev(dev);
891 struct vdc_check_port_data *port_data;
893 port_data = (struct vdc_check_port_data *)arg;
895 if ((vdev->dev_no == port_data->dev_no) &&
896 (!(strcmp((char *)&vdev->type, port_data->type))) &&
897 dev_get_drvdata(dev)) {
898 /* This device has already been configured
899 * by vdc_port_probe()
907 /* Determine whether the VIO device is part of an mpgroup
908 * by locating all the virtual-device-port nodes associated
909 * with the parent virtual-device node for the VIO device
910 * and checking whether any of these nodes are vdc-ports
911 * which have already been configured.
913 * Returns true if this device is part of an mpgroup and has
914 * already been probed.
916 static bool vdc_port_mpgroup_check(struct vio_dev *vdev)
918 struct vdc_check_port_data port_data;
921 port_data.dev_no = vdev->dev_no;
922 port_data.type = (char *)&vdev->type;
924 dev = device_find_child(vdev->dev.parent, &port_data,
933 static int vdc_port_probe(struct vio_dev *vdev, const struct vio_device_id *id)
935 struct mdesc_handle *hp;
936 struct vdc_port *port;
938 const u64 *ldc_timeout;
947 if ((vdev->dev_no << PARTITION_SHIFT) & ~(u64)MINORMASK) {
948 printk(KERN_ERR PFX "Port id [%llu] too large.\n",
950 goto err_out_release_mdesc;
953 /* Check if this device is part of an mpgroup */
954 if (vdc_port_mpgroup_check(vdev)) {
956 "VIO: Ignoring extra vdisk port %s",
957 dev_name(&vdev->dev));
958 goto err_out_release_mdesc;
961 port = kzalloc(sizeof(*port), GFP_KERNEL);
964 printk(KERN_ERR PFX "Cannot allocate vdc_port.\n");
965 goto err_out_release_mdesc;
968 if (vdev->dev_no >= 26)
969 snprintf(port->disk_name, sizeof(port->disk_name),
971 'a' + ((int)vdev->dev_no / 26) - 1,
972 'a' + ((int)vdev->dev_no % 26));
974 snprintf(port->disk_name, sizeof(port->disk_name),
975 VDCBLK_NAME "%c", 'a' + ((int)vdev->dev_no % 26));
976 port->vdisk_size = -1;
978 /* Actual wall time may be double due to do_generic_file_read() doing
979 * a readahead I/O first, and once that fails it will try to read a
982 ldc_timeout = mdesc_get_property(hp, vdev->mp, "vdc-timeout", NULL);
983 port->ldc_timeout = ldc_timeout ? *ldc_timeout : 0;
984 setup_timer(&port->ldc_reset_timer, vdc_ldc_reset_timer,
985 (unsigned long)port);
986 INIT_WORK(&port->ldc_reset_work, vdc_ldc_reset_work);
988 err = vio_driver_init(&port->vio, vdev, VDEV_DISK,
989 vdc_versions, ARRAY_SIZE(vdc_versions),
990 &vdc_vio_ops, port->disk_name);
992 goto err_out_free_port;
994 port->vdisk_block_size = VDC_DEFAULT_BLK_SIZE;
995 port->max_xfer_size = ((128 * 1024) / port->vdisk_block_size);
996 port->ring_cookies = ((port->max_xfer_size *
997 port->vdisk_block_size) / PAGE_SIZE) + 2;
999 err = vio_ldc_alloc(&port->vio, &vdc_ldc_cfg, port);
1001 goto err_out_free_port;
1003 err = vdc_alloc_tx_ring(port);
1005 goto err_out_free_ldc;
1007 err = probe_disk(port);
1009 goto err_out_free_tx_ring;
1011 /* Note that the device driver_data is used to determine
1012 * whether the port has been probed.
1014 dev_set_drvdata(&vdev->dev, port);
1020 err_out_free_tx_ring:
1021 vdc_free_tx_ring(port);
1024 vio_ldc_free(&port->vio);
1029 err_out_release_mdesc:
1034 static int vdc_port_remove(struct vio_dev *vdev)
1036 struct vdc_port *port = dev_get_drvdata(&vdev->dev);
1039 unsigned long flags;
1041 spin_lock_irqsave(&port->vio.lock, flags);
1042 blk_stop_queue(port->disk->queue);
1043 spin_unlock_irqrestore(&port->vio.lock, flags);
1045 flush_work(&port->ldc_reset_work);
1046 del_timer_sync(&port->ldc_reset_timer);
1047 del_timer_sync(&port->vio.timer);
1049 del_gendisk(port->disk);
1050 blk_cleanup_queue(port->disk->queue);
1051 put_disk(port->disk);
1054 vdc_free_tx_ring(port);
1055 vio_ldc_free(&port->vio);
1057 dev_set_drvdata(&vdev->dev, NULL);
1064 static void vdc_requeue_inflight(struct vdc_port *port)
1066 struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING];
1069 for (idx = dr->cons; idx != dr->prod; idx = vio_dring_next(dr, idx)) {
1070 struct vio_disk_desc *desc = vio_dring_entry(dr, idx);
1071 struct vdc_req_entry *rqe = &port->rq_arr[idx];
1072 struct request *req;
1074 ldc_unmap(port->vio.lp, desc->cookies, desc->ncookies);
1075 desc->hdr.state = VIO_DESC_FREE;
1076 dr->cons = vio_dring_next(dr, idx);
1080 vdc_end_special(port, desc);
1085 blk_requeue_request(port->disk->queue, req);
1089 static void vdc_queue_drain(struct vdc_port *port)
1091 struct request *req;
1093 while ((req = blk_fetch_request(port->disk->queue)) != NULL)
1094 __blk_end_request_all(req, BLK_STS_IOERR);
1097 static void vdc_ldc_reset_timer(unsigned long _arg)
1099 struct vdc_port *port = (struct vdc_port *) _arg;
1100 struct vio_driver_state *vio = &port->vio;
1101 unsigned long flags;
1103 spin_lock_irqsave(&vio->lock, flags);
1104 if (!(port->vio.hs_state & VIO_HS_COMPLETE)) {
1105 pr_warn(PFX "%s ldc down %llu seconds, draining queue\n",
1106 port->disk_name, port->ldc_timeout);
1107 vdc_queue_drain(port);
1108 vdc_blk_queue_start(port);
1110 spin_unlock_irqrestore(&vio->lock, flags);
1113 static void vdc_ldc_reset_work(struct work_struct *work)
1115 struct vdc_port *port;
1116 struct vio_driver_state *vio;
1117 unsigned long flags;
1119 port = container_of(work, struct vdc_port, ldc_reset_work);
1122 spin_lock_irqsave(&vio->lock, flags);
1123 vdc_ldc_reset(port);
1124 spin_unlock_irqrestore(&vio->lock, flags);
1127 static void vdc_ldc_reset(struct vdc_port *port)
1131 assert_spin_locked(&port->vio.lock);
1133 pr_warn(PFX "%s ldc link reset\n", port->disk_name);
1134 blk_stop_queue(port->disk->queue);
1135 vdc_requeue_inflight(port);
1136 vdc_port_down(port);
1138 err = vio_ldc_alloc(&port->vio, &vdc_ldc_cfg, port);
1140 pr_err(PFX "%s vio_ldc_alloc:%d\n", port->disk_name, err);
1144 err = vdc_alloc_tx_ring(port);
1146 pr_err(PFX "%s vio_alloc_tx_ring:%d\n", port->disk_name, err);
1150 if (port->ldc_timeout)
1151 mod_timer(&port->ldc_reset_timer,
1152 round_jiffies(jiffies + HZ * port->ldc_timeout));
1153 mod_timer(&port->vio.timer, round_jiffies(jiffies + HZ));
1157 vio_ldc_free(&port->vio);
1160 static const struct vio_device_id vdc_port_match[] = {
1166 MODULE_DEVICE_TABLE(vio, vdc_port_match);
1168 static struct vio_driver vdc_port_driver = {
1169 .id_table = vdc_port_match,
1170 .probe = vdc_port_probe,
1171 .remove = vdc_port_remove,
1175 static int __init vdc_init(void)
1179 sunvdc_wq = alloc_workqueue("sunvdc", 0, 0);
1183 err = register_blkdev(0, VDCBLK_NAME);
1189 err = vio_register_driver(&vdc_port_driver);
1191 goto out_unregister_blkdev;
1195 out_unregister_blkdev:
1196 unregister_blkdev(vdc_major, VDCBLK_NAME);
1200 destroy_workqueue(sunvdc_wq);
1204 static void __exit vdc_exit(void)
1206 vio_unregister_driver(&vdc_port_driver);
1207 unregister_blkdev(vdc_major, VDCBLK_NAME);
1208 destroy_workqueue(sunvdc_wq);
1211 module_init(vdc_init);
1212 module_exit(vdc_exit);