Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /* sunvdc.c: Sun LDOM Virtual Disk Client.
0003  *
0004  * Copyright (C) 2007, 2008 David S. Miller <davem@davemloft.net>
0005  */
0006 
0007 #include <linux/module.h>
0008 #include <linux/kernel.h>
0009 #include <linux/types.h>
0010 #include <linux/blk-mq.h>
0011 #include <linux/hdreg.h>
0012 #include <linux/cdrom.h>
0013 #include <linux/slab.h>
0014 #include <linux/spinlock.h>
0015 #include <linux/completion.h>
0016 #include <linux/delay.h>
0017 #include <linux/init.h>
0018 #include <linux/list.h>
0019 #include <linux/scatterlist.h>
0020 
0021 #include <asm/vio.h>
0022 #include <asm/ldc.h>
0023 
0024 #define DRV_MODULE_NAME     "sunvdc"
0025 #define PFX DRV_MODULE_NAME ": "
0026 #define DRV_MODULE_VERSION  "1.2"
0027 #define DRV_MODULE_RELDATE  "November 24, 2014"
0028 
0029 static char version[] =
0030     DRV_MODULE_NAME ".c:v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")\n";
0031 MODULE_AUTHOR("David S. Miller (davem@davemloft.net)");
0032 MODULE_DESCRIPTION("Sun LDOM virtual disk client driver");
0033 MODULE_LICENSE("GPL");
0034 MODULE_VERSION(DRV_MODULE_VERSION);
0035 
0036 #define VDC_TX_RING_SIZE    512
0037 #define VDC_DEFAULT_BLK_SIZE    512
0038 
0039 #define MAX_XFER_BLKS       (128 * 1024)
0040 #define MAX_XFER_SIZE       (MAX_XFER_BLKS / VDC_DEFAULT_BLK_SIZE)
0041 #define MAX_RING_COOKIES    ((MAX_XFER_BLKS / PAGE_SIZE) + 2)
0042 
0043 #define WAITING_FOR_LINK_UP 0x01
0044 #define WAITING_FOR_TX_SPACE    0x02
0045 #define WAITING_FOR_GEN_CMD 0x04
0046 #define WAITING_FOR_ANY     -1
0047 
0048 #define VDC_MAX_RETRIES 10
0049 
0050 static struct workqueue_struct *sunvdc_wq;
0051 
0052 struct vdc_req_entry {
0053     struct request      *req;
0054 };
0055 
0056 struct vdc_port {
0057     struct vio_driver_state vio;
0058 
0059     struct gendisk      *disk;
0060 
0061     struct vdc_completion   *cmp;
0062 
0063     u64         req_id;
0064     u64         seq;
0065     struct vdc_req_entry    rq_arr[VDC_TX_RING_SIZE];
0066 
0067     unsigned long       ring_cookies;
0068 
0069     u64         max_xfer_size;
0070     u32         vdisk_block_size;
0071     u32         drain;
0072 
0073     u64         ldc_timeout;
0074     struct delayed_work ldc_reset_timer_work;
0075     struct work_struct  ldc_reset_work;
0076 
0077     /* The server fills these in for us in the disk attribute
0078      * ACK packet.
0079      */
0080     u64         operations;
0081     u32         vdisk_size;
0082     u8          vdisk_type;
0083     u8          vdisk_mtype;
0084     u32         vdisk_phys_blksz;
0085 
0086     struct blk_mq_tag_set   tag_set;
0087 
0088     char            disk_name[32];
0089 };
0090 
0091 static void vdc_ldc_reset(struct vdc_port *port);
0092 static void vdc_ldc_reset_work(struct work_struct *work);
0093 static void vdc_ldc_reset_timer_work(struct work_struct *work);
0094 
0095 static inline struct vdc_port *to_vdc_port(struct vio_driver_state *vio)
0096 {
0097     return container_of(vio, struct vdc_port, vio);
0098 }
0099 
0100 /* Ordered from largest major to lowest */
0101 static struct vio_version vdc_versions[] = {
0102     { .major = 1, .minor = 2 },
0103     { .major = 1, .minor = 1 },
0104     { .major = 1, .minor = 0 },
0105 };
0106 
0107 static inline int vdc_version_supported(struct vdc_port *port,
0108                     u16 major, u16 minor)
0109 {
0110     return port->vio.ver.major == major && port->vio.ver.minor >= minor;
0111 }
0112 
0113 #define VDCBLK_NAME "vdisk"
0114 static int vdc_major;
0115 #define PARTITION_SHIFT 3
0116 
0117 static inline u32 vdc_tx_dring_avail(struct vio_dring_state *dr)
0118 {
0119     return vio_dring_avail(dr, VDC_TX_RING_SIZE);
0120 }
0121 
0122 static int vdc_getgeo(struct block_device *bdev, struct hd_geometry *geo)
0123 {
0124     struct gendisk *disk = bdev->bd_disk;
0125     sector_t nsect = get_capacity(disk);
0126     sector_t cylinders = nsect;
0127 
0128     geo->heads = 0xff;
0129     geo->sectors = 0x3f;
0130     sector_div(cylinders, geo->heads * geo->sectors);
0131     geo->cylinders = cylinders;
0132     if ((sector_t)(geo->cylinders + 1) * geo->heads * geo->sectors < nsect)
0133         geo->cylinders = 0xffff;
0134 
0135     return 0;
0136 }
0137 
0138 /* Add ioctl/CDROM_GET_CAPABILITY to support cdrom_id in udev
0139  * when vdisk_mtype is VD_MEDIA_TYPE_CD or VD_MEDIA_TYPE_DVD.
0140  * Needed to be able to install inside an ldom from an iso image.
0141  */
0142 static int vdc_ioctl(struct block_device *bdev, fmode_t mode,
0143              unsigned command, unsigned long argument)
0144 {
0145     struct vdc_port *port = bdev->bd_disk->private_data;
0146     int i;
0147 
0148     switch (command) {
0149     case CDROMMULTISESSION:
0150         pr_debug(PFX "Multisession CDs not supported\n");
0151         for (i = 0; i < sizeof(struct cdrom_multisession); i++)
0152             if (put_user(0, (char __user *)(argument + i)))
0153                 return -EFAULT;
0154         return 0;
0155 
0156     case CDROM_GET_CAPABILITY:
0157         if (!vdc_version_supported(port, 1, 1))
0158             return -EINVAL;
0159         switch (port->vdisk_mtype) {
0160         case VD_MEDIA_TYPE_CD:
0161         case VD_MEDIA_TYPE_DVD:
0162             return 0;
0163         default:
0164             return -EINVAL;
0165         }
0166     default:
0167         pr_debug(PFX "ioctl %08x not supported\n", command);
0168         return -EINVAL;
0169     }
0170 }
0171 
0172 static const struct block_device_operations vdc_fops = {
0173     .owner      = THIS_MODULE,
0174     .getgeo     = vdc_getgeo,
0175     .ioctl      = vdc_ioctl,
0176     .compat_ioctl   = blkdev_compat_ptr_ioctl,
0177 };
0178 
0179 static void vdc_blk_queue_start(struct vdc_port *port)
0180 {
0181     struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING];
0182 
0183     /* restart blk queue when ring is half emptied. also called after
0184      * handshake completes, so check for initial handshake before we've
0185      * allocated a disk.
0186      */
0187     if (port->disk && vdc_tx_dring_avail(dr) * 100 / VDC_TX_RING_SIZE >= 50)
0188         blk_mq_start_stopped_hw_queues(port->disk->queue, true);
0189 }
0190 
0191 static void vdc_finish(struct vio_driver_state *vio, int err, int waiting_for)
0192 {
0193     if (vio->cmp &&
0194         (waiting_for == -1 ||
0195          vio->cmp->waiting_for == waiting_for)) {
0196         vio->cmp->err = err;
0197         complete(&vio->cmp->com);
0198         vio->cmp = NULL;
0199     }
0200 }
0201 
0202 static void vdc_handshake_complete(struct vio_driver_state *vio)
0203 {
0204     struct vdc_port *port = to_vdc_port(vio);
0205 
0206     cancel_delayed_work(&port->ldc_reset_timer_work);
0207     vdc_finish(vio, 0, WAITING_FOR_LINK_UP);
0208     vdc_blk_queue_start(port);
0209 }
0210 
0211 static int vdc_handle_unknown(struct vdc_port *port, void *arg)
0212 {
0213     struct vio_msg_tag *pkt = arg;
0214 
0215     printk(KERN_ERR PFX "Received unknown msg [%02x:%02x:%04x:%08x]\n",
0216            pkt->type, pkt->stype, pkt->stype_env, pkt->sid);
0217     printk(KERN_ERR PFX "Resetting connection.\n");
0218 
0219     ldc_disconnect(port->vio.lp);
0220 
0221     return -ECONNRESET;
0222 }
0223 
0224 static int vdc_send_attr(struct vio_driver_state *vio)
0225 {
0226     struct vdc_port *port = to_vdc_port(vio);
0227     struct vio_disk_attr_info pkt;
0228 
0229     memset(&pkt, 0, sizeof(pkt));
0230 
0231     pkt.tag.type = VIO_TYPE_CTRL;
0232     pkt.tag.stype = VIO_SUBTYPE_INFO;
0233     pkt.tag.stype_env = VIO_ATTR_INFO;
0234     pkt.tag.sid = vio_send_sid(vio);
0235 
0236     pkt.xfer_mode = VIO_DRING_MODE;
0237     pkt.vdisk_block_size = port->vdisk_block_size;
0238     pkt.max_xfer_size = port->max_xfer_size;
0239 
0240     viodbg(HS, "SEND ATTR xfer_mode[0x%x] blksz[%u] max_xfer[%llu]\n",
0241            pkt.xfer_mode, pkt.vdisk_block_size, pkt.max_xfer_size);
0242 
0243     return vio_ldc_send(&port->vio, &pkt, sizeof(pkt));
0244 }
0245 
0246 static int vdc_handle_attr(struct vio_driver_state *vio, void *arg)
0247 {
0248     struct vdc_port *port = to_vdc_port(vio);
0249     struct vio_disk_attr_info *pkt = arg;
0250 
0251     viodbg(HS, "GOT ATTR stype[0x%x] ops[%llx] disk_size[%llu] disk_type[%x] "
0252            "mtype[0x%x] xfer_mode[0x%x] blksz[%u] max_xfer[%llu]\n",
0253            pkt->tag.stype, pkt->operations,
0254            pkt->vdisk_size, pkt->vdisk_type, pkt->vdisk_mtype,
0255            pkt->xfer_mode, pkt->vdisk_block_size,
0256            pkt->max_xfer_size);
0257 
0258     if (pkt->tag.stype == VIO_SUBTYPE_ACK) {
0259         switch (pkt->vdisk_type) {
0260         case VD_DISK_TYPE_DISK:
0261         case VD_DISK_TYPE_SLICE:
0262             break;
0263 
0264         default:
0265             printk(KERN_ERR PFX "%s: Bogus vdisk_type 0x%x\n",
0266                    vio->name, pkt->vdisk_type);
0267             return -ECONNRESET;
0268         }
0269 
0270         if (pkt->vdisk_block_size > port->vdisk_block_size) {
0271             printk(KERN_ERR PFX "%s: BLOCK size increased "
0272                    "%u --> %u\n",
0273                    vio->name,
0274                    port->vdisk_block_size, pkt->vdisk_block_size);
0275             return -ECONNRESET;
0276         }
0277 
0278         port->operations = pkt->operations;
0279         port->vdisk_type = pkt->vdisk_type;
0280         if (vdc_version_supported(port, 1, 1)) {
0281             port->vdisk_size = pkt->vdisk_size;
0282             port->vdisk_mtype = pkt->vdisk_mtype;
0283         }
0284         if (pkt->max_xfer_size < port->max_xfer_size)
0285             port->max_xfer_size = pkt->max_xfer_size;
0286         port->vdisk_block_size = pkt->vdisk_block_size;
0287 
0288         port->vdisk_phys_blksz = VDC_DEFAULT_BLK_SIZE;
0289         if (vdc_version_supported(port, 1, 2))
0290             port->vdisk_phys_blksz = pkt->phys_block_size;
0291 
0292         return 0;
0293     } else {
0294         printk(KERN_ERR PFX "%s: Attribute NACK\n", vio->name);
0295 
0296         return -ECONNRESET;
0297     }
0298 }
0299 
0300 static void vdc_end_special(struct vdc_port *port, struct vio_disk_desc *desc)
0301 {
0302     int err = desc->status;
0303 
0304     vdc_finish(&port->vio, -err, WAITING_FOR_GEN_CMD);
0305 }
0306 
0307 static void vdc_end_one(struct vdc_port *port, struct vio_dring_state *dr,
0308             unsigned int index)
0309 {
0310     struct vio_disk_desc *desc = vio_dring_entry(dr, index);
0311     struct vdc_req_entry *rqe = &port->rq_arr[index];
0312     struct request *req;
0313 
0314     if (unlikely(desc->hdr.state != VIO_DESC_DONE))
0315         return;
0316 
0317     ldc_unmap(port->vio.lp, desc->cookies, desc->ncookies);
0318     desc->hdr.state = VIO_DESC_FREE;
0319     dr->cons = vio_dring_next(dr, index);
0320 
0321     req = rqe->req;
0322     if (req == NULL) {
0323         vdc_end_special(port, desc);
0324         return;
0325     }
0326 
0327     rqe->req = NULL;
0328 
0329     blk_mq_end_request(req, desc->status ? BLK_STS_IOERR : 0);
0330 
0331     vdc_blk_queue_start(port);
0332 }
0333 
0334 static int vdc_ack(struct vdc_port *port, void *msgbuf)
0335 {
0336     struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING];
0337     struct vio_dring_data *pkt = msgbuf;
0338 
0339     if (unlikely(pkt->dring_ident != dr->ident ||
0340              pkt->start_idx != pkt->end_idx ||
0341              pkt->start_idx >= VDC_TX_RING_SIZE))
0342         return 0;
0343 
0344     vdc_end_one(port, dr, pkt->start_idx);
0345 
0346     return 0;
0347 }
0348 
0349 static int vdc_nack(struct vdc_port *port, void *msgbuf)
0350 {
0351     /* XXX Implement me XXX */
0352     return 0;
0353 }
0354 
0355 static void vdc_event(void *arg, int event)
0356 {
0357     struct vdc_port *port = arg;
0358     struct vio_driver_state *vio = &port->vio;
0359     unsigned long flags;
0360     int err;
0361 
0362     spin_lock_irqsave(&vio->lock, flags);
0363 
0364     if (unlikely(event == LDC_EVENT_RESET)) {
0365         vio_link_state_change(vio, event);
0366         queue_work(sunvdc_wq, &port->ldc_reset_work);
0367         goto out;
0368     }
0369 
0370     if (unlikely(event == LDC_EVENT_UP)) {
0371         vio_link_state_change(vio, event);
0372         goto out;
0373     }
0374 
0375     if (unlikely(event != LDC_EVENT_DATA_READY)) {
0376         pr_warn(PFX "Unexpected LDC event %d\n", event);
0377         goto out;
0378     }
0379 
0380     err = 0;
0381     while (1) {
0382         union {
0383             struct vio_msg_tag tag;
0384             u64 raw[8];
0385         } msgbuf;
0386 
0387         err = ldc_read(vio->lp, &msgbuf, sizeof(msgbuf));
0388         if (unlikely(err < 0)) {
0389             if (err == -ECONNRESET)
0390                 vio_conn_reset(vio);
0391             break;
0392         }
0393         if (err == 0)
0394             break;
0395         viodbg(DATA, "TAG [%02x:%02x:%04x:%08x]\n",
0396                msgbuf.tag.type,
0397                msgbuf.tag.stype,
0398                msgbuf.tag.stype_env,
0399                msgbuf.tag.sid);
0400         err = vio_validate_sid(vio, &msgbuf.tag);
0401         if (err < 0)
0402             break;
0403 
0404         if (likely(msgbuf.tag.type == VIO_TYPE_DATA)) {
0405             if (msgbuf.tag.stype == VIO_SUBTYPE_ACK)
0406                 err = vdc_ack(port, &msgbuf);
0407             else if (msgbuf.tag.stype == VIO_SUBTYPE_NACK)
0408                 err = vdc_nack(port, &msgbuf);
0409             else
0410                 err = vdc_handle_unknown(port, &msgbuf);
0411         } else if (msgbuf.tag.type == VIO_TYPE_CTRL) {
0412             err = vio_control_pkt_engine(vio, &msgbuf);
0413         } else {
0414             err = vdc_handle_unknown(port, &msgbuf);
0415         }
0416         if (err < 0)
0417             break;
0418     }
0419     if (err < 0)
0420         vdc_finish(&port->vio, err, WAITING_FOR_ANY);
0421 out:
0422     spin_unlock_irqrestore(&vio->lock, flags);
0423 }
0424 
0425 static int __vdc_tx_trigger(struct vdc_port *port)
0426 {
0427     struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING];
0428     struct vio_dring_data hdr = {
0429         .tag = {
0430             .type       = VIO_TYPE_DATA,
0431             .stype      = VIO_SUBTYPE_INFO,
0432             .stype_env  = VIO_DRING_DATA,
0433             .sid        = vio_send_sid(&port->vio),
0434         },
0435         .dring_ident        = dr->ident,
0436         .start_idx      = dr->prod,
0437         .end_idx        = dr->prod,
0438     };
0439     int err, delay;
0440     int retries = 0;
0441 
0442     hdr.seq = dr->snd_nxt;
0443     delay = 1;
0444     do {
0445         err = vio_ldc_send(&port->vio, &hdr, sizeof(hdr));
0446         if (err > 0) {
0447             dr->snd_nxt++;
0448             break;
0449         }
0450         udelay(delay);
0451         if ((delay <<= 1) > 128)
0452             delay = 128;
0453         if (retries++ > VDC_MAX_RETRIES)
0454             break;
0455     } while (err == -EAGAIN);
0456 
0457     if (err == -ENOTCONN)
0458         vdc_ldc_reset(port);
0459     return err;
0460 }
0461 
0462 static int __send_request(struct request *req)
0463 {
0464     struct vdc_port *port = req->q->disk->private_data;
0465     struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING];
0466     struct scatterlist sg[MAX_RING_COOKIES];
0467     struct vdc_req_entry *rqe;
0468     struct vio_disk_desc *desc;
0469     unsigned int map_perm;
0470     int nsg, err, i;
0471     u64 len;
0472     u8 op;
0473 
0474     if (WARN_ON(port->ring_cookies > MAX_RING_COOKIES))
0475         return -EINVAL;
0476 
0477     map_perm = LDC_MAP_SHADOW | LDC_MAP_DIRECT | LDC_MAP_IO;
0478 
0479     if (rq_data_dir(req) == READ) {
0480         map_perm |= LDC_MAP_W;
0481         op = VD_OP_BREAD;
0482     } else {
0483         map_perm |= LDC_MAP_R;
0484         op = VD_OP_BWRITE;
0485     }
0486 
0487     sg_init_table(sg, port->ring_cookies);
0488     nsg = blk_rq_map_sg(req->q, req, sg);
0489 
0490     len = 0;
0491     for (i = 0; i < nsg; i++)
0492         len += sg[i].length;
0493 
0494     desc = vio_dring_cur(dr);
0495 
0496     err = ldc_map_sg(port->vio.lp, sg, nsg,
0497              desc->cookies, port->ring_cookies,
0498              map_perm);
0499     if (err < 0) {
0500         printk(KERN_ERR PFX "ldc_map_sg() failure, err=%d.\n", err);
0501         return err;
0502     }
0503 
0504     rqe = &port->rq_arr[dr->prod];
0505     rqe->req = req;
0506 
0507     desc->hdr.ack = VIO_ACK_ENABLE;
0508     desc->req_id = port->req_id;
0509     desc->operation = op;
0510     if (port->vdisk_type == VD_DISK_TYPE_DISK) {
0511         desc->slice = 0xff;
0512     } else {
0513         desc->slice = 0;
0514     }
0515     desc->status = ~0;
0516     desc->offset = (blk_rq_pos(req) << 9) / port->vdisk_block_size;
0517     desc->size = len;
0518     desc->ncookies = err;
0519 
0520     /* This has to be a non-SMP write barrier because we are writing
0521      * to memory which is shared with the peer LDOM.
0522      */
0523     wmb();
0524     desc->hdr.state = VIO_DESC_READY;
0525 
0526     err = __vdc_tx_trigger(port);
0527     if (err < 0) {
0528         printk(KERN_ERR PFX "vdc_tx_trigger() failure, err=%d\n", err);
0529     } else {
0530         port->req_id++;
0531         dr->prod = vio_dring_next(dr, dr->prod);
0532     }
0533 
0534     return err;
0535 }
0536 
0537 static blk_status_t vdc_queue_rq(struct blk_mq_hw_ctx *hctx,
0538                  const struct blk_mq_queue_data *bd)
0539 {
0540     struct vdc_port *port = hctx->queue->queuedata;
0541     struct vio_dring_state *dr;
0542     unsigned long flags;
0543 
0544     dr = &port->vio.drings[VIO_DRIVER_TX_RING];
0545 
0546     blk_mq_start_request(bd->rq);
0547 
0548     spin_lock_irqsave(&port->vio.lock, flags);
0549 
0550     /*
0551      * Doing drain, just end the request in error
0552      */
0553     if (unlikely(port->drain)) {
0554         spin_unlock_irqrestore(&port->vio.lock, flags);
0555         return BLK_STS_IOERR;
0556     }
0557 
0558     if (unlikely(vdc_tx_dring_avail(dr) < 1)) {
0559         spin_unlock_irqrestore(&port->vio.lock, flags);
0560         blk_mq_stop_hw_queue(hctx);
0561         return BLK_STS_DEV_RESOURCE;
0562     }
0563 
0564     if (__send_request(bd->rq) < 0) {
0565         spin_unlock_irqrestore(&port->vio.lock, flags);
0566         return BLK_STS_IOERR;
0567     }
0568 
0569     spin_unlock_irqrestore(&port->vio.lock, flags);
0570     return BLK_STS_OK;
0571 }
0572 
0573 static int generic_request(struct vdc_port *port, u8 op, void *buf, int len)
0574 {
0575     struct vio_dring_state *dr;
0576     struct vio_completion comp;
0577     struct vio_disk_desc *desc;
0578     unsigned int map_perm;
0579     unsigned long flags;
0580     int op_len, err;
0581     void *req_buf;
0582 
0583     if (!(((u64)1 << (u64)op) & port->operations))
0584         return -EOPNOTSUPP;
0585 
0586     switch (op) {
0587     case VD_OP_BREAD:
0588     case VD_OP_BWRITE:
0589     default:
0590         return -EINVAL;
0591 
0592     case VD_OP_FLUSH:
0593         op_len = 0;
0594         map_perm = 0;
0595         break;
0596 
0597     case VD_OP_GET_WCE:
0598         op_len = sizeof(u32);
0599         map_perm = LDC_MAP_W;
0600         break;
0601 
0602     case VD_OP_SET_WCE:
0603         op_len = sizeof(u32);
0604         map_perm = LDC_MAP_R;
0605         break;
0606 
0607     case VD_OP_GET_VTOC:
0608         op_len = sizeof(struct vio_disk_vtoc);
0609         map_perm = LDC_MAP_W;
0610         break;
0611 
0612     case VD_OP_SET_VTOC:
0613         op_len = sizeof(struct vio_disk_vtoc);
0614         map_perm = LDC_MAP_R;
0615         break;
0616 
0617     case VD_OP_GET_DISKGEOM:
0618         op_len = sizeof(struct vio_disk_geom);
0619         map_perm = LDC_MAP_W;
0620         break;
0621 
0622     case VD_OP_SET_DISKGEOM:
0623         op_len = sizeof(struct vio_disk_geom);
0624         map_perm = LDC_MAP_R;
0625         break;
0626 
0627     case VD_OP_SCSICMD:
0628         op_len = 16;
0629         map_perm = LDC_MAP_RW;
0630         break;
0631 
0632     case VD_OP_GET_DEVID:
0633         op_len = sizeof(struct vio_disk_devid);
0634         map_perm = LDC_MAP_W;
0635         break;
0636 
0637     case VD_OP_GET_EFI:
0638     case VD_OP_SET_EFI:
0639         return -EOPNOTSUPP;
0640     }
0641 
0642     map_perm |= LDC_MAP_SHADOW | LDC_MAP_DIRECT | LDC_MAP_IO;
0643 
0644     op_len = (op_len + 7) & ~7;
0645     req_buf = kzalloc(op_len, GFP_KERNEL);
0646     if (!req_buf)
0647         return -ENOMEM;
0648 
0649     if (len > op_len)
0650         len = op_len;
0651 
0652     if (map_perm & LDC_MAP_R)
0653         memcpy(req_buf, buf, len);
0654 
0655     spin_lock_irqsave(&port->vio.lock, flags);
0656 
0657     dr = &port->vio.drings[VIO_DRIVER_TX_RING];
0658 
0659     /* XXX If we want to use this code generically we have to
0660      * XXX handle TX ring exhaustion etc.
0661      */
0662     desc = vio_dring_cur(dr);
0663 
0664     err = ldc_map_single(port->vio.lp, req_buf, op_len,
0665                  desc->cookies, port->ring_cookies,
0666                  map_perm);
0667     if (err < 0) {
0668         spin_unlock_irqrestore(&port->vio.lock, flags);
0669         kfree(req_buf);
0670         return err;
0671     }
0672 
0673     init_completion(&comp.com);
0674     comp.waiting_for = WAITING_FOR_GEN_CMD;
0675     port->vio.cmp = &comp;
0676 
0677     desc->hdr.ack = VIO_ACK_ENABLE;
0678     desc->req_id = port->req_id;
0679     desc->operation = op;
0680     desc->slice = 0;
0681     desc->status = ~0;
0682     desc->offset = 0;
0683     desc->size = op_len;
0684     desc->ncookies = err;
0685 
0686     /* This has to be a non-SMP write barrier because we are writing
0687      * to memory which is shared with the peer LDOM.
0688      */
0689     wmb();
0690     desc->hdr.state = VIO_DESC_READY;
0691 
0692     err = __vdc_tx_trigger(port);
0693     if (err >= 0) {
0694         port->req_id++;
0695         dr->prod = vio_dring_next(dr, dr->prod);
0696         spin_unlock_irqrestore(&port->vio.lock, flags);
0697 
0698         wait_for_completion(&comp.com);
0699         err = comp.err;
0700     } else {
0701         port->vio.cmp = NULL;
0702         spin_unlock_irqrestore(&port->vio.lock, flags);
0703     }
0704 
0705     if (map_perm & LDC_MAP_W)
0706         memcpy(buf, req_buf, len);
0707 
0708     kfree(req_buf);
0709 
0710     return err;
0711 }
0712 
0713 static int vdc_alloc_tx_ring(struct vdc_port *port)
0714 {
0715     struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING];
0716     unsigned long len, entry_size;
0717     int ncookies;
0718     void *dring;
0719 
0720     entry_size = sizeof(struct vio_disk_desc) +
0721         (sizeof(struct ldc_trans_cookie) * port->ring_cookies);
0722     len = (VDC_TX_RING_SIZE * entry_size);
0723 
0724     ncookies = VIO_MAX_RING_COOKIES;
0725     dring = ldc_alloc_exp_dring(port->vio.lp, len,
0726                     dr->cookies, &ncookies,
0727                     (LDC_MAP_SHADOW |
0728                      LDC_MAP_DIRECT |
0729                      LDC_MAP_RW));
0730     if (IS_ERR(dring))
0731         return PTR_ERR(dring);
0732 
0733     dr->base = dring;
0734     dr->entry_size = entry_size;
0735     dr->num_entries = VDC_TX_RING_SIZE;
0736     dr->prod = dr->cons = 0;
0737     dr->pending = VDC_TX_RING_SIZE;
0738     dr->ncookies = ncookies;
0739 
0740     return 0;
0741 }
0742 
0743 static void vdc_free_tx_ring(struct vdc_port *port)
0744 {
0745     struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING];
0746 
0747     if (dr->base) {
0748         ldc_free_exp_dring(port->vio.lp, dr->base,
0749                    (dr->entry_size * dr->num_entries),
0750                    dr->cookies, dr->ncookies);
0751         dr->base = NULL;
0752         dr->entry_size = 0;
0753         dr->num_entries = 0;
0754         dr->pending = 0;
0755         dr->ncookies = 0;
0756     }
0757 }
0758 
0759 static int vdc_port_up(struct vdc_port *port)
0760 {
0761     struct vio_completion comp;
0762 
0763     init_completion(&comp.com);
0764     comp.err = 0;
0765     comp.waiting_for = WAITING_FOR_LINK_UP;
0766     port->vio.cmp = &comp;
0767 
0768     vio_port_up(&port->vio);
0769     wait_for_completion(&comp.com);
0770     return comp.err;
0771 }
0772 
0773 static void vdc_port_down(struct vdc_port *port)
0774 {
0775     ldc_disconnect(port->vio.lp);
0776     ldc_unbind(port->vio.lp);
0777     vdc_free_tx_ring(port);
0778     vio_ldc_free(&port->vio);
0779 }
0780 
0781 static const struct blk_mq_ops vdc_mq_ops = {
0782     .queue_rq   = vdc_queue_rq,
0783 };
0784 
0785 static int probe_disk(struct vdc_port *port)
0786 {
0787     struct request_queue *q;
0788     struct gendisk *g;
0789     int err;
0790 
0791     err = vdc_port_up(port);
0792     if (err)
0793         return err;
0794 
0795     /* Using version 1.2 means vdisk_phys_blksz should be set unless the
0796      * disk is reserved by another system.
0797      */
0798     if (vdc_version_supported(port, 1, 2) && !port->vdisk_phys_blksz)
0799         return -ENODEV;
0800 
0801     if (vdc_version_supported(port, 1, 1)) {
0802         /* vdisk_size should be set during the handshake, if it wasn't
0803          * then the underlying disk is reserved by another system
0804          */
0805         if (port->vdisk_size == -1)
0806             return -ENODEV;
0807     } else {
0808         struct vio_disk_geom geom;
0809 
0810         err = generic_request(port, VD_OP_GET_DISKGEOM,
0811                       &geom, sizeof(geom));
0812         if (err < 0) {
0813             printk(KERN_ERR PFX "VD_OP_GET_DISKGEOM returns "
0814                    "error %d\n", err);
0815             return err;
0816         }
0817         port->vdisk_size = ((u64)geom.num_cyl *
0818                     (u64)geom.num_hd *
0819                     (u64)geom.num_sec);
0820     }
0821 
0822     err = blk_mq_alloc_sq_tag_set(&port->tag_set, &vdc_mq_ops,
0823             VDC_TX_RING_SIZE, BLK_MQ_F_SHOULD_MERGE);
0824     if (err)
0825         return err;
0826 
0827     g = blk_mq_alloc_disk(&port->tag_set, port);
0828     if (IS_ERR(g)) {
0829         printk(KERN_ERR PFX "%s: Could not allocate gendisk.\n",
0830                port->vio.name);
0831         err = PTR_ERR(g);
0832         goto out_free_tag;
0833     }
0834 
0835     port->disk = g;
0836     q = g->queue;
0837 
0838     /* Each segment in a request is up to an aligned page in size. */
0839     blk_queue_segment_boundary(q, PAGE_SIZE - 1);
0840     blk_queue_max_segment_size(q, PAGE_SIZE);
0841 
0842     blk_queue_max_segments(q, port->ring_cookies);
0843     blk_queue_max_hw_sectors(q, port->max_xfer_size);
0844     g->major = vdc_major;
0845     g->first_minor = port->vio.vdev->dev_no << PARTITION_SHIFT;
0846     g->minors = 1 << PARTITION_SHIFT;
0847     strcpy(g->disk_name, port->disk_name);
0848 
0849     g->fops = &vdc_fops;
0850     g->queue = q;
0851     g->private_data = port;
0852 
0853     set_capacity(g, port->vdisk_size);
0854 
0855     if (vdc_version_supported(port, 1, 1)) {
0856         switch (port->vdisk_mtype) {
0857         case VD_MEDIA_TYPE_CD:
0858             pr_info(PFX "Virtual CDROM %s\n", port->disk_name);
0859             g->flags |= GENHD_FL_REMOVABLE;
0860             set_disk_ro(g, 1);
0861             break;
0862 
0863         case VD_MEDIA_TYPE_DVD:
0864             pr_info(PFX "Virtual DVD %s\n", port->disk_name);
0865             g->flags |= GENHD_FL_REMOVABLE;
0866             set_disk_ro(g, 1);
0867             break;
0868 
0869         case VD_MEDIA_TYPE_FIXED:
0870             pr_info(PFX "Virtual Hard disk %s\n", port->disk_name);
0871             break;
0872         }
0873     }
0874 
0875     blk_queue_physical_block_size(q, port->vdisk_phys_blksz);
0876 
0877     pr_info(PFX "%s: %u sectors (%u MB) protocol %d.%d\n",
0878            g->disk_name,
0879            port->vdisk_size, (port->vdisk_size >> (20 - 9)),
0880            port->vio.ver.major, port->vio.ver.minor);
0881 
0882     err = device_add_disk(&port->vio.vdev->dev, g, NULL);
0883     if (err)
0884         goto out_cleanup_disk;
0885 
0886     return 0;
0887 
0888 out_cleanup_disk:
0889     put_disk(g);
0890 out_free_tag:
0891     blk_mq_free_tag_set(&port->tag_set);
0892     return err;
0893 }
0894 
0895 static struct ldc_channel_config vdc_ldc_cfg = {
0896     .event      = vdc_event,
0897     .mtu        = 64,
0898     .mode       = LDC_MODE_UNRELIABLE,
0899 };
0900 
0901 static struct vio_driver_ops vdc_vio_ops = {
0902     .send_attr      = vdc_send_attr,
0903     .handle_attr        = vdc_handle_attr,
0904     .handshake_complete = vdc_handshake_complete,
0905 };
0906 
0907 static void print_version(void)
0908 {
0909     static int version_printed;
0910 
0911     if (version_printed++ == 0)
0912         printk(KERN_INFO "%s", version);
0913 }
0914 
0915 struct vdc_check_port_data {
0916     int dev_no;
0917     char    *type;
0918 };
0919 
0920 static int vdc_device_probed(struct device *dev, void *arg)
0921 {
0922     struct vio_dev *vdev = to_vio_dev(dev);
0923     struct vdc_check_port_data *port_data;
0924 
0925     port_data = (struct vdc_check_port_data *)arg;
0926 
0927     if ((vdev->dev_no == port_data->dev_no) &&
0928         (!(strcmp((char *)&vdev->type, port_data->type))) &&
0929         dev_get_drvdata(dev)) {
0930         /* This device has already been configured
0931          * by vdc_port_probe()
0932          */
0933         return 1;
0934     } else {
0935         return 0;
0936     }
0937 }
0938 
0939 /* Determine whether the VIO device is part of an mpgroup
0940  * by locating all the virtual-device-port nodes associated
0941  * with the parent virtual-device node for the VIO device
0942  * and checking whether any of these nodes are vdc-ports
0943  * which have already been configured.
0944  *
0945  * Returns true if this device is part of an mpgroup and has
0946  * already been probed.
0947  */
0948 static bool vdc_port_mpgroup_check(struct vio_dev *vdev)
0949 {
0950     struct vdc_check_port_data port_data;
0951     struct device *dev;
0952 
0953     port_data.dev_no = vdev->dev_no;
0954     port_data.type = (char *)&vdev->type;
0955 
0956     dev = device_find_child(vdev->dev.parent, &port_data,
0957                 vdc_device_probed);
0958 
0959     if (dev)
0960         return true;
0961 
0962     return false;
0963 }
0964 
0965 static int vdc_port_probe(struct vio_dev *vdev, const struct vio_device_id *id)
0966 {
0967     struct mdesc_handle *hp;
0968     struct vdc_port *port;
0969     int err;
0970     const u64 *ldc_timeout;
0971 
0972     print_version();
0973 
0974     hp = mdesc_grab();
0975 
0976     err = -ENODEV;
0977     if ((vdev->dev_no << PARTITION_SHIFT) & ~(u64)MINORMASK) {
0978         printk(KERN_ERR PFX "Port id [%llu] too large.\n",
0979                vdev->dev_no);
0980         goto err_out_release_mdesc;
0981     }
0982 
0983     /* Check if this device is part of an mpgroup */
0984     if (vdc_port_mpgroup_check(vdev)) {
0985         printk(KERN_WARNING
0986             "VIO: Ignoring extra vdisk port %s",
0987             dev_name(&vdev->dev));
0988         goto err_out_release_mdesc;
0989     }
0990 
0991     port = kzalloc(sizeof(*port), GFP_KERNEL);
0992     if (!port) {
0993         err = -ENOMEM;
0994         goto err_out_release_mdesc;
0995     }
0996 
0997     if (vdev->dev_no >= 26)
0998         snprintf(port->disk_name, sizeof(port->disk_name),
0999              VDCBLK_NAME "%c%c",
1000              'a' + ((int)vdev->dev_no / 26) - 1,
1001              'a' + ((int)vdev->dev_no % 26));
1002     else
1003         snprintf(port->disk_name, sizeof(port->disk_name),
1004              VDCBLK_NAME "%c", 'a' + ((int)vdev->dev_no % 26));
1005     port->vdisk_size = -1;
1006 
1007     /* Actual wall time may be double due to do_generic_file_read() doing
1008      * a readahead I/O first, and once that fails it will try to read a
1009      * single page.
1010      */
1011     ldc_timeout = mdesc_get_property(hp, vdev->mp, "vdc-timeout", NULL);
1012     port->ldc_timeout = ldc_timeout ? *ldc_timeout : 0;
1013     INIT_DELAYED_WORK(&port->ldc_reset_timer_work, vdc_ldc_reset_timer_work);
1014     INIT_WORK(&port->ldc_reset_work, vdc_ldc_reset_work);
1015 
1016     err = vio_driver_init(&port->vio, vdev, VDEV_DISK,
1017                   vdc_versions, ARRAY_SIZE(vdc_versions),
1018                   &vdc_vio_ops, port->disk_name);
1019     if (err)
1020         goto err_out_free_port;
1021 
1022     port->vdisk_block_size = VDC_DEFAULT_BLK_SIZE;
1023     port->max_xfer_size = MAX_XFER_SIZE;
1024     port->ring_cookies = MAX_RING_COOKIES;
1025 
1026     err = vio_ldc_alloc(&port->vio, &vdc_ldc_cfg, port);
1027     if (err)
1028         goto err_out_free_port;
1029 
1030     err = vdc_alloc_tx_ring(port);
1031     if (err)
1032         goto err_out_free_ldc;
1033 
1034     err = probe_disk(port);
1035     if (err)
1036         goto err_out_free_tx_ring;
1037 
1038     /* Note that the device driver_data is used to determine
1039      * whether the port has been probed.
1040      */
1041     dev_set_drvdata(&vdev->dev, port);
1042 
1043     mdesc_release(hp);
1044 
1045     return 0;
1046 
1047 err_out_free_tx_ring:
1048     vdc_free_tx_ring(port);
1049 
1050 err_out_free_ldc:
1051     vio_ldc_free(&port->vio);
1052 
1053 err_out_free_port:
1054     kfree(port);
1055 
1056 err_out_release_mdesc:
1057     mdesc_release(hp);
1058     return err;
1059 }
1060 
1061 static void vdc_port_remove(struct vio_dev *vdev)
1062 {
1063     struct vdc_port *port = dev_get_drvdata(&vdev->dev);
1064 
1065     if (port) {
1066         blk_mq_stop_hw_queues(port->disk->queue);
1067 
1068         flush_work(&port->ldc_reset_work);
1069         cancel_delayed_work_sync(&port->ldc_reset_timer_work);
1070         del_timer_sync(&port->vio.timer);
1071 
1072         del_gendisk(port->disk);
1073         put_disk(port->disk);
1074         blk_mq_free_tag_set(&port->tag_set);
1075 
1076         vdc_free_tx_ring(port);
1077         vio_ldc_free(&port->vio);
1078 
1079         dev_set_drvdata(&vdev->dev, NULL);
1080 
1081         kfree(port);
1082     }
1083 }
1084 
1085 static void vdc_requeue_inflight(struct vdc_port *port)
1086 {
1087     struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING];
1088     u32 idx;
1089 
1090     for (idx = dr->cons; idx != dr->prod; idx = vio_dring_next(dr, idx)) {
1091         struct vio_disk_desc *desc = vio_dring_entry(dr, idx);
1092         struct vdc_req_entry *rqe = &port->rq_arr[idx];
1093         struct request *req;
1094 
1095         ldc_unmap(port->vio.lp, desc->cookies, desc->ncookies);
1096         desc->hdr.state = VIO_DESC_FREE;
1097         dr->cons = vio_dring_next(dr, idx);
1098 
1099         req = rqe->req;
1100         if (req == NULL) {
1101             vdc_end_special(port, desc);
1102             continue;
1103         }
1104 
1105         rqe->req = NULL;
1106         blk_mq_requeue_request(req, false);
1107     }
1108 }
1109 
1110 static void vdc_queue_drain(struct vdc_port *port)
1111 {
1112     struct request_queue *q = port->disk->queue;
1113 
1114     /*
1115      * Mark the queue as draining, then freeze/quiesce to ensure
1116      * that all existing requests are seen in ->queue_rq() and killed
1117      */
1118     port->drain = 1;
1119     spin_unlock_irq(&port->vio.lock);
1120 
1121     blk_mq_freeze_queue(q);
1122     blk_mq_quiesce_queue(q);
1123 
1124     spin_lock_irq(&port->vio.lock);
1125     port->drain = 0;
1126     blk_mq_unquiesce_queue(q);
1127     blk_mq_unfreeze_queue(q);
1128 }
1129 
1130 static void vdc_ldc_reset_timer_work(struct work_struct *work)
1131 {
1132     struct vdc_port *port;
1133     struct vio_driver_state *vio;
1134 
1135     port = container_of(work, struct vdc_port, ldc_reset_timer_work.work);
1136     vio = &port->vio;
1137 
1138     spin_lock_irq(&vio->lock);
1139     if (!(port->vio.hs_state & VIO_HS_COMPLETE)) {
1140         pr_warn(PFX "%s ldc down %llu seconds, draining queue\n",
1141             port->disk_name, port->ldc_timeout);
1142         vdc_queue_drain(port);
1143         vdc_blk_queue_start(port);
1144     }
1145     spin_unlock_irq(&vio->lock);
1146 }
1147 
1148 static void vdc_ldc_reset_work(struct work_struct *work)
1149 {
1150     struct vdc_port *port;
1151     struct vio_driver_state *vio;
1152     unsigned long flags;
1153 
1154     port = container_of(work, struct vdc_port, ldc_reset_work);
1155     vio = &port->vio;
1156 
1157     spin_lock_irqsave(&vio->lock, flags);
1158     vdc_ldc_reset(port);
1159     spin_unlock_irqrestore(&vio->lock, flags);
1160 }
1161 
1162 static void vdc_ldc_reset(struct vdc_port *port)
1163 {
1164     int err;
1165 
1166     assert_spin_locked(&port->vio.lock);
1167 
1168     pr_warn(PFX "%s ldc link reset\n", port->disk_name);
1169     blk_mq_stop_hw_queues(port->disk->queue);
1170     vdc_requeue_inflight(port);
1171     vdc_port_down(port);
1172 
1173     err = vio_ldc_alloc(&port->vio, &vdc_ldc_cfg, port);
1174     if (err) {
1175         pr_err(PFX "%s vio_ldc_alloc:%d\n", port->disk_name, err);
1176         return;
1177     }
1178 
1179     err = vdc_alloc_tx_ring(port);
1180     if (err) {
1181         pr_err(PFX "%s vio_alloc_tx_ring:%d\n", port->disk_name, err);
1182         goto err_free_ldc;
1183     }
1184 
1185     if (port->ldc_timeout)
1186         mod_delayed_work(system_wq, &port->ldc_reset_timer_work,
1187               round_jiffies(jiffies + HZ * port->ldc_timeout));
1188     mod_timer(&port->vio.timer, round_jiffies(jiffies + HZ));
1189     return;
1190 
1191 err_free_ldc:
1192     vio_ldc_free(&port->vio);
1193 }
1194 
1195 static const struct vio_device_id vdc_port_match[] = {
1196     {
1197         .type = "vdc-port",
1198     },
1199     {},
1200 };
1201 MODULE_DEVICE_TABLE(vio, vdc_port_match);
1202 
1203 static struct vio_driver vdc_port_driver = {
1204     .id_table   = vdc_port_match,
1205     .probe      = vdc_port_probe,
1206     .remove     = vdc_port_remove,
1207     .name       = "vdc_port",
1208 };
1209 
1210 static int __init vdc_init(void)
1211 {
1212     int err;
1213 
1214     sunvdc_wq = alloc_workqueue("sunvdc", 0, 0);
1215     if (!sunvdc_wq)
1216         return -ENOMEM;
1217 
1218     err = register_blkdev(0, VDCBLK_NAME);
1219     if (err < 0)
1220         goto out_free_wq;
1221 
1222     vdc_major = err;
1223 
1224     err = vio_register_driver(&vdc_port_driver);
1225     if (err)
1226         goto out_unregister_blkdev;
1227 
1228     return 0;
1229 
1230 out_unregister_blkdev:
1231     unregister_blkdev(vdc_major, VDCBLK_NAME);
1232     vdc_major = 0;
1233 
1234 out_free_wq:
1235     destroy_workqueue(sunvdc_wq);
1236     return err;
1237 }
1238 
1239 static void __exit vdc_exit(void)
1240 {
1241     vio_unregister_driver(&vdc_port_driver);
1242     unregister_blkdev(vdc_major, VDCBLK_NAME);
1243     destroy_workqueue(sunvdc_wq);
1244 }
1245 
1246 module_init(vdc_init);
1247 module_exit(vdc_exit);