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 static struct workqueue_struct *sunvdc_wq;
46 struct vdc_req_entry {
51 struct vio_driver_state vio;
55 struct vdc_completion *cmp;
59 struct vdc_req_entry rq_arr[VDC_TX_RING_SIZE];
61 unsigned long ring_cookies;
67 struct timer_list ldc_reset_timer;
68 struct work_struct ldc_reset_work;
70 /* The server fills these in for us in the disk attribute
82 static void vdc_ldc_reset(struct vdc_port *port);
83 static void vdc_ldc_reset_work(struct work_struct *work);
84 static void vdc_ldc_reset_timer(unsigned long _arg);
86 static inline struct vdc_port *to_vdc_port(struct vio_driver_state *vio)
88 return container_of(vio, struct vdc_port, vio);
91 /* Ordered from largest major to lowest */
92 static struct vio_version vdc_versions[] = {
93 { .major = 1, .minor = 2 },
94 { .major = 1, .minor = 1 },
95 { .major = 1, .minor = 0 },
98 static inline int vdc_version_supported(struct vdc_port *port,
101 return port->vio.ver.major == major && port->vio.ver.minor >= minor;
104 #define VDCBLK_NAME "vdisk"
105 static int vdc_major;
106 #define PARTITION_SHIFT 3
108 static inline u32 vdc_tx_dring_avail(struct vio_dring_state *dr)
110 return vio_dring_avail(dr, VDC_TX_RING_SIZE);
113 static int vdc_getgeo(struct block_device *bdev, struct hd_geometry *geo)
115 struct gendisk *disk = bdev->bd_disk;
116 sector_t nsect = get_capacity(disk);
117 sector_t cylinders = nsect;
121 sector_div(cylinders, geo->heads * geo->sectors);
122 geo->cylinders = cylinders;
123 if ((sector_t)(geo->cylinders + 1) * geo->heads * geo->sectors < nsect)
124 geo->cylinders = 0xffff;
129 /* Add ioctl/CDROM_GET_CAPABILITY to support cdrom_id in udev
130 * when vdisk_mtype is VD_MEDIA_TYPE_CD or VD_MEDIA_TYPE_DVD.
131 * Needed to be able to install inside an ldom from an iso image.
133 static int vdc_ioctl(struct block_device *bdev, fmode_t mode,
134 unsigned command, unsigned long argument)
137 struct gendisk *disk;
140 case CDROMMULTISESSION:
141 pr_debug(PFX "Multisession CDs not supported\n");
142 for (i = 0; i < sizeof(struct cdrom_multisession); i++)
143 if (put_user(0, (char __user *)(argument + i)))
147 case CDROM_GET_CAPABILITY:
148 disk = bdev->bd_disk;
150 if (bdev->bd_disk && (disk->flags & GENHD_FL_CD))
155 pr_debug(PFX "ioctl %08x not supported\n", command);
160 static const struct block_device_operations vdc_fops = {
161 .owner = THIS_MODULE,
162 .getgeo = vdc_getgeo,
166 static void vdc_blk_queue_start(struct vdc_port *port)
168 struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING];
170 /* restart blk queue when ring is half emptied. also called after
171 * handshake completes, so check for initial handshake before we've
174 if (port->disk && blk_queue_stopped(port->disk->queue) &&
175 vdc_tx_dring_avail(dr) * 100 / VDC_TX_RING_SIZE >= 50) {
176 blk_start_queue(port->disk->queue);
181 static void vdc_finish(struct vio_driver_state *vio, int err, int waiting_for)
184 (waiting_for == -1 ||
185 vio->cmp->waiting_for == waiting_for)) {
187 complete(&vio->cmp->com);
192 static void vdc_handshake_complete(struct vio_driver_state *vio)
194 struct vdc_port *port = to_vdc_port(vio);
196 del_timer(&port->ldc_reset_timer);
197 vdc_finish(vio, 0, WAITING_FOR_LINK_UP);
198 vdc_blk_queue_start(port);
201 static int vdc_handle_unknown(struct vdc_port *port, void *arg)
203 struct vio_msg_tag *pkt = arg;
205 printk(KERN_ERR PFX "Received unknown msg [%02x:%02x:%04x:%08x]\n",
206 pkt->type, pkt->stype, pkt->stype_env, pkt->sid);
207 printk(KERN_ERR PFX "Resetting connection.\n");
209 ldc_disconnect(port->vio.lp);
214 static int vdc_send_attr(struct vio_driver_state *vio)
216 struct vdc_port *port = to_vdc_port(vio);
217 struct vio_disk_attr_info pkt;
219 memset(&pkt, 0, sizeof(pkt));
221 pkt.tag.type = VIO_TYPE_CTRL;
222 pkt.tag.stype = VIO_SUBTYPE_INFO;
223 pkt.tag.stype_env = VIO_ATTR_INFO;
224 pkt.tag.sid = vio_send_sid(vio);
226 pkt.xfer_mode = VIO_DRING_MODE;
227 pkt.vdisk_block_size = port->vdisk_block_size;
228 pkt.max_xfer_size = port->max_xfer_size;
230 viodbg(HS, "SEND ATTR xfer_mode[0x%x] blksz[%u] max_xfer[%llu]\n",
231 pkt.xfer_mode, pkt.vdisk_block_size, pkt.max_xfer_size);
233 return vio_ldc_send(&port->vio, &pkt, sizeof(pkt));
236 static int vdc_handle_attr(struct vio_driver_state *vio, void *arg)
238 struct vdc_port *port = to_vdc_port(vio);
239 struct vio_disk_attr_info *pkt = arg;
241 viodbg(HS, "GOT ATTR stype[0x%x] ops[%llx] disk_size[%llu] disk_type[%x] "
242 "mtype[0x%x] xfer_mode[0x%x] blksz[%u] max_xfer[%llu]\n",
243 pkt->tag.stype, pkt->operations,
244 pkt->vdisk_size, pkt->vdisk_type, pkt->vdisk_mtype,
245 pkt->xfer_mode, pkt->vdisk_block_size,
248 if (pkt->tag.stype == VIO_SUBTYPE_ACK) {
249 switch (pkt->vdisk_type) {
250 case VD_DISK_TYPE_DISK:
251 case VD_DISK_TYPE_SLICE:
255 printk(KERN_ERR PFX "%s: Bogus vdisk_type 0x%x\n",
256 vio->name, pkt->vdisk_type);
260 if (pkt->vdisk_block_size > port->vdisk_block_size) {
261 printk(KERN_ERR PFX "%s: BLOCK size increased "
264 port->vdisk_block_size, pkt->vdisk_block_size);
268 port->operations = pkt->operations;
269 port->vdisk_type = pkt->vdisk_type;
270 if (vdc_version_supported(port, 1, 1)) {
271 port->vdisk_size = pkt->vdisk_size;
272 port->vdisk_mtype = pkt->vdisk_mtype;
274 if (pkt->max_xfer_size < port->max_xfer_size)
275 port->max_xfer_size = pkt->max_xfer_size;
276 port->vdisk_block_size = pkt->vdisk_block_size;
278 port->vdisk_phys_blksz = VDC_DEFAULT_BLK_SIZE;
279 if (vdc_version_supported(port, 1, 2))
280 port->vdisk_phys_blksz = pkt->phys_block_size;
284 printk(KERN_ERR PFX "%s: Attribute NACK\n", vio->name);
290 static void vdc_end_special(struct vdc_port *port, struct vio_disk_desc *desc)
292 int err = desc->status;
294 vdc_finish(&port->vio, -err, WAITING_FOR_GEN_CMD);
297 static void vdc_end_one(struct vdc_port *port, struct vio_dring_state *dr,
300 struct vio_disk_desc *desc = vio_dring_entry(dr, index);
301 struct vdc_req_entry *rqe = &port->rq_arr[index];
304 if (unlikely(desc->hdr.state != VIO_DESC_DONE))
307 ldc_unmap(port->vio.lp, desc->cookies, desc->ncookies);
308 desc->hdr.state = VIO_DESC_FREE;
309 dr->cons = vio_dring_next(dr, index);
313 vdc_end_special(port, desc);
319 __blk_end_request(req, (desc->status ? BLK_STS_IOERR : 0), desc->size);
321 vdc_blk_queue_start(port);
324 static int vdc_ack(struct vdc_port *port, void *msgbuf)
326 struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING];
327 struct vio_dring_data *pkt = msgbuf;
329 if (unlikely(pkt->dring_ident != dr->ident ||
330 pkt->start_idx != pkt->end_idx ||
331 pkt->start_idx >= VDC_TX_RING_SIZE))
334 vdc_end_one(port, dr, pkt->start_idx);
339 static int vdc_nack(struct vdc_port *port, void *msgbuf)
341 /* XXX Implement me XXX */
345 static void vdc_event(void *arg, int event)
347 struct vdc_port *port = arg;
348 struct vio_driver_state *vio = &port->vio;
352 spin_lock_irqsave(&vio->lock, flags);
354 if (unlikely(event == LDC_EVENT_RESET)) {
355 vio_link_state_change(vio, event);
356 queue_work(sunvdc_wq, &port->ldc_reset_work);
360 if (unlikely(event == LDC_EVENT_UP)) {
361 vio_link_state_change(vio, event);
365 if (unlikely(event != LDC_EVENT_DATA_READY)) {
366 pr_warn(PFX "Unexpected LDC event %d\n", event);
373 struct vio_msg_tag tag;
377 err = ldc_read(vio->lp, &msgbuf, sizeof(msgbuf));
378 if (unlikely(err < 0)) {
379 if (err == -ECONNRESET)
385 viodbg(DATA, "TAG [%02x:%02x:%04x:%08x]\n",
388 msgbuf.tag.stype_env,
390 err = vio_validate_sid(vio, &msgbuf.tag);
394 if (likely(msgbuf.tag.type == VIO_TYPE_DATA)) {
395 if (msgbuf.tag.stype == VIO_SUBTYPE_ACK)
396 err = vdc_ack(port, &msgbuf);
397 else if (msgbuf.tag.stype == VIO_SUBTYPE_NACK)
398 err = vdc_nack(port, &msgbuf);
400 err = vdc_handle_unknown(port, &msgbuf);
401 } else if (msgbuf.tag.type == VIO_TYPE_CTRL) {
402 err = vio_control_pkt_engine(vio, &msgbuf);
404 err = vdc_handle_unknown(port, &msgbuf);
410 vdc_finish(&port->vio, err, WAITING_FOR_ANY);
412 spin_unlock_irqrestore(&vio->lock, flags);
415 static int __vdc_tx_trigger(struct vdc_port *port)
417 struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING];
418 struct vio_dring_data hdr = {
420 .type = VIO_TYPE_DATA,
421 .stype = VIO_SUBTYPE_INFO,
422 .stype_env = VIO_DRING_DATA,
423 .sid = vio_send_sid(&port->vio),
425 .dring_ident = dr->ident,
426 .start_idx = dr->prod,
431 hdr.seq = dr->snd_nxt;
434 err = vio_ldc_send(&port->vio, &hdr, sizeof(hdr));
440 if ((delay <<= 1) > 128)
442 } while (err == -EAGAIN);
444 if (err == -ENOTCONN)
449 static int __send_request(struct request *req)
451 struct vdc_port *port = req->rq_disk->private_data;
452 struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING];
453 struct scatterlist sg[port->ring_cookies];
454 struct vdc_req_entry *rqe;
455 struct vio_disk_desc *desc;
456 unsigned int map_perm;
461 map_perm = LDC_MAP_SHADOW | LDC_MAP_DIRECT | LDC_MAP_IO;
463 if (rq_data_dir(req) == READ) {
464 map_perm |= LDC_MAP_W;
467 map_perm |= LDC_MAP_R;
471 sg_init_table(sg, port->ring_cookies);
472 nsg = blk_rq_map_sg(req->q, req, sg);
475 for (i = 0; i < nsg; i++)
478 desc = vio_dring_cur(dr);
480 err = ldc_map_sg(port->vio.lp, sg, nsg,
481 desc->cookies, port->ring_cookies,
484 printk(KERN_ERR PFX "ldc_map_sg() failure, err=%d.\n", err);
488 rqe = &port->rq_arr[dr->prod];
491 desc->hdr.ack = VIO_ACK_ENABLE;
492 desc->req_id = port->req_id;
493 desc->operation = op;
494 if (port->vdisk_type == VD_DISK_TYPE_DISK) {
500 desc->offset = (blk_rq_pos(req) << 9) / port->vdisk_block_size;
502 desc->ncookies = err;
504 /* This has to be a non-SMP write barrier because we are writing
505 * to memory which is shared with the peer LDOM.
508 desc->hdr.state = VIO_DESC_READY;
510 err = __vdc_tx_trigger(port);
512 printk(KERN_ERR PFX "vdc_tx_trigger() failure, err=%d\n", err);
515 dr->prod = vio_dring_next(dr, dr->prod);
521 static void do_vdc_request(struct request_queue *rq)
525 while ((req = blk_peek_request(rq)) != NULL) {
526 struct vdc_port *port;
527 struct vio_dring_state *dr;
529 port = req->rq_disk->private_data;
530 dr = &port->vio.drings[VIO_DRIVER_TX_RING];
531 if (unlikely(vdc_tx_dring_avail(dr) < 1))
534 blk_start_request(req);
536 if (__send_request(req) < 0) {
537 blk_requeue_request(rq, req);
539 /* Avoid pointless unplugs. */
546 static int generic_request(struct vdc_port *port, u8 op, void *buf, int len)
548 struct vio_dring_state *dr;
549 struct vio_completion comp;
550 struct vio_disk_desc *desc;
551 unsigned int map_perm;
556 if (!(((u64)1 << (u64)op) & port->operations))
571 op_len = sizeof(u32);
572 map_perm = LDC_MAP_W;
576 op_len = sizeof(u32);
577 map_perm = LDC_MAP_R;
581 op_len = sizeof(struct vio_disk_vtoc);
582 map_perm = LDC_MAP_W;
586 op_len = sizeof(struct vio_disk_vtoc);
587 map_perm = LDC_MAP_R;
590 case VD_OP_GET_DISKGEOM:
591 op_len = sizeof(struct vio_disk_geom);
592 map_perm = LDC_MAP_W;
595 case VD_OP_SET_DISKGEOM:
596 op_len = sizeof(struct vio_disk_geom);
597 map_perm = LDC_MAP_R;
602 map_perm = LDC_MAP_RW;
605 case VD_OP_GET_DEVID:
606 op_len = sizeof(struct vio_disk_devid);
607 map_perm = LDC_MAP_W;
616 map_perm |= LDC_MAP_SHADOW | LDC_MAP_DIRECT | LDC_MAP_IO;
618 op_len = (op_len + 7) & ~7;
619 req_buf = kzalloc(op_len, GFP_KERNEL);
626 if (map_perm & LDC_MAP_R)
627 memcpy(req_buf, buf, len);
629 spin_lock_irqsave(&port->vio.lock, flags);
631 dr = &port->vio.drings[VIO_DRIVER_TX_RING];
633 /* XXX If we want to use this code generically we have to
634 * XXX handle TX ring exhaustion etc.
636 desc = vio_dring_cur(dr);
638 err = ldc_map_single(port->vio.lp, req_buf, op_len,
639 desc->cookies, port->ring_cookies,
642 spin_unlock_irqrestore(&port->vio.lock, flags);
647 init_completion(&comp.com);
648 comp.waiting_for = WAITING_FOR_GEN_CMD;
649 port->vio.cmp = ∁
651 desc->hdr.ack = VIO_ACK_ENABLE;
652 desc->req_id = port->req_id;
653 desc->operation = op;
658 desc->ncookies = err;
660 /* This has to be a non-SMP write barrier because we are writing
661 * to memory which is shared with the peer LDOM.
664 desc->hdr.state = VIO_DESC_READY;
666 err = __vdc_tx_trigger(port);
669 dr->prod = vio_dring_next(dr, dr->prod);
670 spin_unlock_irqrestore(&port->vio.lock, flags);
672 wait_for_completion(&comp.com);
675 port->vio.cmp = NULL;
676 spin_unlock_irqrestore(&port->vio.lock, flags);
679 if (map_perm & LDC_MAP_W)
680 memcpy(buf, req_buf, len);
687 static int vdc_alloc_tx_ring(struct vdc_port *port)
689 struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING];
690 unsigned long len, entry_size;
694 entry_size = sizeof(struct vio_disk_desc) +
695 (sizeof(struct ldc_trans_cookie) * port->ring_cookies);
696 len = (VDC_TX_RING_SIZE * entry_size);
698 ncookies = VIO_MAX_RING_COOKIES;
699 dring = ldc_alloc_exp_dring(port->vio.lp, len,
700 dr->cookies, &ncookies,
705 return PTR_ERR(dring);
708 dr->entry_size = entry_size;
709 dr->num_entries = VDC_TX_RING_SIZE;
710 dr->prod = dr->cons = 0;
711 dr->pending = VDC_TX_RING_SIZE;
712 dr->ncookies = ncookies;
717 static void vdc_free_tx_ring(struct vdc_port *port)
719 struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING];
722 ldc_free_exp_dring(port->vio.lp, dr->base,
723 (dr->entry_size * dr->num_entries),
724 dr->cookies, dr->ncookies);
733 static int vdc_port_up(struct vdc_port *port)
735 struct vio_completion comp;
737 init_completion(&comp.com);
739 comp.waiting_for = WAITING_FOR_LINK_UP;
740 port->vio.cmp = ∁
742 vio_port_up(&port->vio);
743 wait_for_completion(&comp.com);
747 static void vdc_port_down(struct vdc_port *port)
749 ldc_disconnect(port->vio.lp);
750 ldc_unbind(port->vio.lp);
751 vdc_free_tx_ring(port);
752 vio_ldc_free(&port->vio);
755 static int probe_disk(struct vdc_port *port)
757 struct request_queue *q;
761 err = vdc_port_up(port);
765 /* Using version 1.2 means vdisk_phys_blksz should be set unless the
766 * disk is reserved by another system.
768 if (vdc_version_supported(port, 1, 2) && !port->vdisk_phys_blksz)
771 if (vdc_version_supported(port, 1, 1)) {
772 /* vdisk_size should be set during the handshake, if it wasn't
773 * then the underlying disk is reserved by another system
775 if (port->vdisk_size == -1)
778 struct vio_disk_geom geom;
780 err = generic_request(port, VD_OP_GET_DISKGEOM,
781 &geom, sizeof(geom));
783 printk(KERN_ERR PFX "VD_OP_GET_DISKGEOM returns "
787 port->vdisk_size = ((u64)geom.num_cyl *
792 q = blk_init_queue(do_vdc_request, &port->vio.lock);
794 printk(KERN_ERR PFX "%s: Could not allocate queue.\n",
798 g = alloc_disk(1 << PARTITION_SHIFT);
800 printk(KERN_ERR PFX "%s: Could not allocate gendisk.\n",
802 blk_cleanup_queue(q);
808 /* Each segment in a request is up to an aligned page in size. */
809 blk_queue_segment_boundary(q, PAGE_SIZE - 1);
810 blk_queue_max_segment_size(q, PAGE_SIZE);
812 blk_queue_max_segments(q, port->ring_cookies);
813 blk_queue_max_hw_sectors(q, port->max_xfer_size);
814 g->major = vdc_major;
815 g->first_minor = port->vio.vdev->dev_no << PARTITION_SHIFT;
816 strcpy(g->disk_name, port->disk_name);
820 g->private_data = port;
822 set_capacity(g, port->vdisk_size);
824 if (vdc_version_supported(port, 1, 1)) {
825 switch (port->vdisk_mtype) {
826 case VD_MEDIA_TYPE_CD:
827 pr_info(PFX "Virtual CDROM %s\n", port->disk_name);
828 g->flags |= GENHD_FL_CD;
829 g->flags |= GENHD_FL_REMOVABLE;
833 case VD_MEDIA_TYPE_DVD:
834 pr_info(PFX "Virtual DVD %s\n", port->disk_name);
835 g->flags |= GENHD_FL_CD;
836 g->flags |= GENHD_FL_REMOVABLE;
840 case VD_MEDIA_TYPE_FIXED:
841 pr_info(PFX "Virtual Hard disk %s\n", port->disk_name);
846 blk_queue_physical_block_size(q, port->vdisk_phys_blksz);
848 pr_info(PFX "%s: %u sectors (%u MB) protocol %d.%d\n",
850 port->vdisk_size, (port->vdisk_size >> (20 - 9)),
851 port->vio.ver.major, port->vio.ver.minor);
853 device_add_disk(&port->vio.vdev->dev, g);
858 static struct ldc_channel_config vdc_ldc_cfg = {
861 .mode = LDC_MODE_UNRELIABLE,
864 static struct vio_driver_ops vdc_vio_ops = {
865 .send_attr = vdc_send_attr,
866 .handle_attr = vdc_handle_attr,
867 .handshake_complete = vdc_handshake_complete,
870 static void print_version(void)
872 static int version_printed;
874 if (version_printed++ == 0)
875 printk(KERN_INFO "%s", version);
878 struct vdc_check_port_data {
883 static int vdc_device_probed(struct device *dev, void *arg)
885 struct vio_dev *vdev = to_vio_dev(dev);
886 struct vdc_check_port_data *port_data;
888 port_data = (struct vdc_check_port_data *)arg;
890 if ((vdev->dev_no == port_data->dev_no) &&
891 (!(strcmp((char *)&vdev->type, port_data->type))) &&
892 dev_get_drvdata(dev)) {
893 /* This device has already been configured
894 * by vdc_port_probe()
902 /* Determine whether the VIO device is part of an mpgroup
903 * by locating all the virtual-device-port nodes associated
904 * with the parent virtual-device node for the VIO device
905 * and checking whether any of these nodes are vdc-ports
906 * which have already been configured.
908 * Returns true if this device is part of an mpgroup and has
909 * already been probed.
911 static bool vdc_port_mpgroup_check(struct vio_dev *vdev)
913 struct vdc_check_port_data port_data;
916 port_data.dev_no = vdev->dev_no;
917 port_data.type = (char *)&vdev->type;
919 dev = device_find_child(vdev->dev.parent, &port_data,
928 static int vdc_port_probe(struct vio_dev *vdev, const struct vio_device_id *id)
930 struct mdesc_handle *hp;
931 struct vdc_port *port;
933 const u64 *ldc_timeout;
940 if ((vdev->dev_no << PARTITION_SHIFT) & ~(u64)MINORMASK) {
941 printk(KERN_ERR PFX "Port id [%llu] too large.\n",
943 goto err_out_release_mdesc;
946 /* Check if this device is part of an mpgroup */
947 if (vdc_port_mpgroup_check(vdev)) {
949 "VIO: Ignoring extra vdisk port %s",
950 dev_name(&vdev->dev));
951 goto err_out_release_mdesc;
954 port = kzalloc(sizeof(*port), GFP_KERNEL);
957 printk(KERN_ERR PFX "Cannot allocate vdc_port.\n");
958 goto err_out_release_mdesc;
961 if (vdev->dev_no >= 26)
962 snprintf(port->disk_name, sizeof(port->disk_name),
964 'a' + ((int)vdev->dev_no / 26) - 1,
965 'a' + ((int)vdev->dev_no % 26));
967 snprintf(port->disk_name, sizeof(port->disk_name),
968 VDCBLK_NAME "%c", 'a' + ((int)vdev->dev_no % 26));
969 port->vdisk_size = -1;
971 /* Actual wall time may be double due to do_generic_file_read() doing
972 * a readahead I/O first, and once that fails it will try to read a
975 ldc_timeout = mdesc_get_property(hp, vdev->mp, "vdc-timeout", NULL);
976 port->ldc_timeout = ldc_timeout ? *ldc_timeout : 0;
977 setup_timer(&port->ldc_reset_timer, vdc_ldc_reset_timer,
978 (unsigned long)port);
979 INIT_WORK(&port->ldc_reset_work, vdc_ldc_reset_work);
981 err = vio_driver_init(&port->vio, vdev, VDEV_DISK,
982 vdc_versions, ARRAY_SIZE(vdc_versions),
983 &vdc_vio_ops, port->disk_name);
985 goto err_out_free_port;
987 port->vdisk_block_size = VDC_DEFAULT_BLK_SIZE;
988 port->max_xfer_size = ((128 * 1024) / port->vdisk_block_size);
989 port->ring_cookies = ((port->max_xfer_size *
990 port->vdisk_block_size) / PAGE_SIZE) + 2;
992 err = vio_ldc_alloc(&port->vio, &vdc_ldc_cfg, port);
994 goto err_out_free_port;
996 err = vdc_alloc_tx_ring(port);
998 goto err_out_free_ldc;
1000 err = probe_disk(port);
1002 goto err_out_free_tx_ring;
1004 /* Note that the device driver_data is used to determine
1005 * whether the port has been probed.
1007 dev_set_drvdata(&vdev->dev, port);
1013 err_out_free_tx_ring:
1014 vdc_free_tx_ring(port);
1017 vio_ldc_free(&port->vio);
1022 err_out_release_mdesc:
1027 static int vdc_port_remove(struct vio_dev *vdev)
1029 struct vdc_port *port = dev_get_drvdata(&vdev->dev);
1032 unsigned long flags;
1034 spin_lock_irqsave(&port->vio.lock, flags);
1035 blk_stop_queue(port->disk->queue);
1036 spin_unlock_irqrestore(&port->vio.lock, flags);
1038 flush_work(&port->ldc_reset_work);
1039 del_timer_sync(&port->ldc_reset_timer);
1040 del_timer_sync(&port->vio.timer);
1042 del_gendisk(port->disk);
1043 blk_cleanup_queue(port->disk->queue);
1044 put_disk(port->disk);
1047 vdc_free_tx_ring(port);
1048 vio_ldc_free(&port->vio);
1050 dev_set_drvdata(&vdev->dev, NULL);
1057 static void vdc_requeue_inflight(struct vdc_port *port)
1059 struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING];
1062 for (idx = dr->cons; idx != dr->prod; idx = vio_dring_next(dr, idx)) {
1063 struct vio_disk_desc *desc = vio_dring_entry(dr, idx);
1064 struct vdc_req_entry *rqe = &port->rq_arr[idx];
1065 struct request *req;
1067 ldc_unmap(port->vio.lp, desc->cookies, desc->ncookies);
1068 desc->hdr.state = VIO_DESC_FREE;
1069 dr->cons = vio_dring_next(dr, idx);
1073 vdc_end_special(port, desc);
1078 blk_requeue_request(port->disk->queue, req);
1082 static void vdc_queue_drain(struct vdc_port *port)
1084 struct request *req;
1086 while ((req = blk_fetch_request(port->disk->queue)) != NULL)
1087 __blk_end_request_all(req, BLK_STS_IOERR);
1090 static void vdc_ldc_reset_timer(unsigned long _arg)
1092 struct vdc_port *port = (struct vdc_port *) _arg;
1093 struct vio_driver_state *vio = &port->vio;
1094 unsigned long flags;
1096 spin_lock_irqsave(&vio->lock, flags);
1097 if (!(port->vio.hs_state & VIO_HS_COMPLETE)) {
1098 pr_warn(PFX "%s ldc down %llu seconds, draining queue\n",
1099 port->disk_name, port->ldc_timeout);
1100 vdc_queue_drain(port);
1101 vdc_blk_queue_start(port);
1103 spin_unlock_irqrestore(&vio->lock, flags);
1106 static void vdc_ldc_reset_work(struct work_struct *work)
1108 struct vdc_port *port;
1109 struct vio_driver_state *vio;
1110 unsigned long flags;
1112 port = container_of(work, struct vdc_port, ldc_reset_work);
1115 spin_lock_irqsave(&vio->lock, flags);
1116 vdc_ldc_reset(port);
1117 spin_unlock_irqrestore(&vio->lock, flags);
1120 static void vdc_ldc_reset(struct vdc_port *port)
1124 assert_spin_locked(&port->vio.lock);
1126 pr_warn(PFX "%s ldc link reset\n", port->disk_name);
1127 blk_stop_queue(port->disk->queue);
1128 vdc_requeue_inflight(port);
1129 vdc_port_down(port);
1131 err = vio_ldc_alloc(&port->vio, &vdc_ldc_cfg, port);
1133 pr_err(PFX "%s vio_ldc_alloc:%d\n", port->disk_name, err);
1137 err = vdc_alloc_tx_ring(port);
1139 pr_err(PFX "%s vio_alloc_tx_ring:%d\n", port->disk_name, err);
1143 if (port->ldc_timeout)
1144 mod_timer(&port->ldc_reset_timer,
1145 round_jiffies(jiffies + HZ * port->ldc_timeout));
1146 mod_timer(&port->vio.timer, round_jiffies(jiffies + HZ));
1150 vio_ldc_free(&port->vio);
1153 static const struct vio_device_id vdc_port_match[] = {
1159 MODULE_DEVICE_TABLE(vio, vdc_port_match);
1161 static struct vio_driver vdc_port_driver = {
1162 .id_table = vdc_port_match,
1163 .probe = vdc_port_probe,
1164 .remove = vdc_port_remove,
1168 static int __init vdc_init(void)
1172 sunvdc_wq = alloc_workqueue("sunvdc", 0, 0);
1176 err = register_blkdev(0, VDCBLK_NAME);
1182 err = vio_register_driver(&vdc_port_driver);
1184 goto out_unregister_blkdev;
1188 out_unregister_blkdev:
1189 unregister_blkdev(vdc_major, VDCBLK_NAME);
1193 destroy_workqueue(sunvdc_wq);
1197 static void __exit vdc_exit(void)
1199 vio_unregister_driver(&vdc_port_driver);
1200 unregister_blkdev(vdc_major, VDCBLK_NAME);
1201 destroy_workqueue(sunvdc_wq);
1204 module_init(vdc_init);
1205 module_exit(vdc_exit);