1 // SPDX-License-Identifier: GPL-2.0-only 2 /* sunvdc.c: Sun LDOM Virtual Disk Client. 3 * 4 * Copyright (C) 2007, 2008 David S. Miller <davem@davemloft.net> 5 */ 6 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/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> 20 21 #include <asm/vio.h> 22 #include <asm/ldc.h> 23 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" 28 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); 35 36 #define VDC_TX_RING_SIZE 512 37 #define VDC_DEFAULT_BLK_SIZE 512 38 39 #define MAX_XFER_BLKS (128 * 1024) 40 #define MAX_XFER_SIZE (MAX_XFER_BLKS / VDC_DEFAULT_BLK_SIZE) 41 #define MAX_RING_COOKIES ((MAX_XFER_BLKS / PAGE_SIZE) + 2) 42 43 #define WAITING_FOR_LINK_UP 0x01 44 #define WAITING_FOR_TX_SPACE 0x02 45 #define WAITING_FOR_GEN_CMD 0x04 46 #define WAITING_FOR_ANY -1 47 48 #define VDC_MAX_RETRIES 10 49 50 static struct workqueue_struct *sunvdc_wq; 51 52 struct vdc_req_entry { 53 struct request *req; 54 }; 55 56 struct vdc_port { 57 struct vio_driver_state vio; 58 59 struct gendisk *disk; 60 61 struct vdc_completion *cmp; 62 63 u64 req_id; 64 u64 seq; 65 struct vdc_req_entry rq_arr[VDC_TX_RING_SIZE]; 66 67 unsigned long ring_cookies; 68 69 u64 max_xfer_size; 70 u32 vdisk_block_size; 71 u32 drain; 72 73 u64 ldc_timeout; 74 struct delayed_work ldc_reset_timer_work; 75 struct work_struct ldc_reset_work; 76 77 /* The server fills these in for us in the disk attribute 78 * ACK packet. 79 */ 80 u64 operations; 81 u32 vdisk_size; 82 u8 vdisk_type; 83 u8 vdisk_mtype; 84 u32 vdisk_phys_blksz; 85 86 struct blk_mq_tag_set tag_set; 87 88 char disk_name[32]; 89 }; 90 91 static void vdc_ldc_reset(struct vdc_port *port); 92 static void vdc_ldc_reset_work(struct work_struct *work); 93 static void vdc_ldc_reset_timer_work(struct work_struct *work); 94 95 static inline struct vdc_port *to_vdc_port(struct vio_driver_state *vio) 96 { 97 return container_of(vio, struct vdc_port, vio); 98 } 99 100 /* Ordered from largest major to lowest */ 101 static struct vio_version vdc_versions[] = { 102 { .major = 1, .minor = 2 }, 103 { .major = 1, .minor = 1 }, 104 { .major = 1, .minor = 0 }, 105 }; 106 107 static inline int vdc_version_supported(struct vdc_port *port, 108 u16 major, u16 minor) 109 { 110 return port->vio.ver.major == major && port->vio.ver.minor >= minor; 111 } 112 113 #define VDCBLK_NAME "vdisk" 114 static int vdc_major; 115 #define PARTITION_SHIFT 3 116 117 static inline u32 vdc_tx_dring_avail(struct vio_dring_state *dr) 118 { 119 return vio_dring_avail(dr, VDC_TX_RING_SIZE); 120 } 121 122 static int vdc_getgeo(struct gendisk *disk, struct hd_geometry *geo) 123 { 124 sector_t nsect = get_capacity(disk); 125 sector_t cylinders = nsect; 126 127 geo->heads = 0xff; 128 geo->sectors = 0x3f; 129 sector_div(cylinders, geo->heads * geo->sectors); 130 geo->cylinders = cylinders; 131 if ((sector_t)(geo->cylinders + 1) * geo->heads * geo->sectors < nsect) 132 geo->cylinders = 0xffff; 133 134 return 0; 135 } 136 137 /* Add ioctl/CDROM_GET_CAPABILITY to support cdrom_id in udev 138 * when vdisk_mtype is VD_MEDIA_TYPE_CD or VD_MEDIA_TYPE_DVD. 139 * Needed to be able to install inside an ldom from an iso image. 140 */ 141 static int vdc_ioctl(struct block_device *bdev, blk_mode_t mode, 142 unsigned command, unsigned long argument) 143 { 144 struct vdc_port *port = bdev->bd_disk->private_data; 145 int i; 146 147 switch (command) { 148 case CDROMMULTISESSION: 149 pr_debug(PFX "Multisession CDs not supported\n"); 150 for (i = 0; i < sizeof(struct cdrom_multisession); i++) 151 if (put_user(0, (char __user *)(argument + i))) 152 return -EFAULT; 153 return 0; 154 155 case CDROM_GET_CAPABILITY: 156 if (!vdc_version_supported(port, 1, 1)) 157 return -EINVAL; 158 switch (port->vdisk_mtype) { 159 case VD_MEDIA_TYPE_CD: 160 case VD_MEDIA_TYPE_DVD: 161 return 0; 162 default: 163 return -EINVAL; 164 } 165 default: 166 pr_debug(PFX "ioctl %08x not supported\n", command); 167 return -EINVAL; 168 } 169 } 170 171 static const struct block_device_operations vdc_fops = { 172 .owner = THIS_MODULE, 173 .getgeo = vdc_getgeo, 174 .ioctl = vdc_ioctl, 175 .compat_ioctl = blkdev_compat_ptr_ioctl, 176 }; 177 178 static void vdc_blk_queue_start(struct vdc_port *port) 179 { 180 struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING]; 181 182 /* restart blk queue when ring is half emptied. also called after 183 * handshake completes, so check for initial handshake before we've 184 * allocated a disk. 185 */ 186 if (port->disk && vdc_tx_dring_avail(dr) * 100 / VDC_TX_RING_SIZE >= 50) 187 blk_mq_start_stopped_hw_queues(port->disk->queue, true); 188 } 189 190 static void vdc_finish(struct vio_driver_state *vio, int err, int waiting_for) 191 { 192 if (vio->cmp && 193 (waiting_for == -1 || 194 vio->cmp->waiting_for == waiting_for)) { 195 vio->cmp->err = err; 196 complete(&vio->cmp->com); 197 vio->cmp = NULL; 198 } 199 } 200 201 static void vdc_handshake_complete(struct vio_driver_state *vio) 202 { 203 struct vdc_port *port = to_vdc_port(vio); 204 205 cancel_delayed_work(&port->ldc_reset_timer_work); 206 vdc_finish(vio, 0, WAITING_FOR_LINK_UP); 207 vdc_blk_queue_start(port); 208 } 209 210 static int vdc_handle_unknown(struct vdc_port *port, void *arg) 211 { 212 struct vio_msg_tag *pkt = arg; 213 214 printk(KERN_ERR PFX "Received unknown msg [%02x:%02x:%04x:%08x]\n", 215 pkt->type, pkt->stype, pkt->stype_env, pkt->sid); 216 printk(KERN_ERR PFX "Resetting connection.\n"); 217 218 ldc_disconnect(port->vio.lp); 219 220 return -ECONNRESET; 221 } 222 223 static int vdc_send_attr(struct vio_driver_state *vio) 224 { 225 struct vdc_port *port = to_vdc_port(vio); 226 struct vio_disk_attr_info pkt; 227 228 memset(&pkt, 0, sizeof(pkt)); 229 230 pkt.tag.type = VIO_TYPE_CTRL; 231 pkt.tag.stype = VIO_SUBTYPE_INFO; 232 pkt.tag.stype_env = VIO_ATTR_INFO; 233 pkt.tag.sid = vio_send_sid(vio); 234 235 pkt.xfer_mode = VIO_DRING_MODE; 236 pkt.vdisk_block_size = port->vdisk_block_size; 237 pkt.max_xfer_size = port->max_xfer_size; 238 239 viodbg(HS, "SEND ATTR xfer_mode[0x%x] blksz[%u] max_xfer[%llu]\n", 240 pkt.xfer_mode, pkt.vdisk_block_size, pkt.max_xfer_size); 241 242 return vio_ldc_send(&port->vio, &pkt, sizeof(pkt)); 243 } 244 245 static int vdc_handle_attr(struct vio_driver_state *vio, void *arg) 246 { 247 struct vdc_port *port = to_vdc_port(vio); 248 struct vio_disk_attr_info *pkt = arg; 249 250 viodbg(HS, "GOT ATTR stype[0x%x] ops[%llx] disk_size[%llu] disk_type[%x] " 251 "mtype[0x%x] xfer_mode[0x%x] blksz[%u] max_xfer[%llu]\n", 252 pkt->tag.stype, pkt->operations, 253 pkt->vdisk_size, pkt->vdisk_type, pkt->vdisk_mtype, 254 pkt->xfer_mode, pkt->vdisk_block_size, 255 pkt->max_xfer_size); 256 257 if (pkt->tag.stype == VIO_SUBTYPE_ACK) { 258 switch (pkt->vdisk_type) { 259 case VD_DISK_TYPE_DISK: 260 case VD_DISK_TYPE_SLICE: 261 break; 262 263 default: 264 printk(KERN_ERR PFX "%s: Bogus vdisk_type 0x%x\n", 265 vio->name, pkt->vdisk_type); 266 return -ECONNRESET; 267 } 268 269 if (pkt->vdisk_block_size > port->vdisk_block_size) { 270 printk(KERN_ERR PFX "%s: BLOCK size increased " 271 "%u --> %u\n", 272 vio->name, 273 port->vdisk_block_size, pkt->vdisk_block_size); 274 return -ECONNRESET; 275 } 276 277 port->operations = pkt->operations; 278 port->vdisk_type = pkt->vdisk_type; 279 if (vdc_version_supported(port, 1, 1)) { 280 port->vdisk_size = pkt->vdisk_size; 281 port->vdisk_mtype = pkt->vdisk_mtype; 282 } 283 if (pkt->max_xfer_size < port->max_xfer_size) 284 port->max_xfer_size = pkt->max_xfer_size; 285 port->vdisk_block_size = pkt->vdisk_block_size; 286 287 port->vdisk_phys_blksz = VDC_DEFAULT_BLK_SIZE; 288 if (vdc_version_supported(port, 1, 2)) 289 port->vdisk_phys_blksz = pkt->phys_block_size; 290 291 return 0; 292 } else { 293 printk(KERN_ERR PFX "%s: Attribute NACK\n", vio->name); 294 295 return -ECONNRESET; 296 } 297 } 298 299 static void vdc_end_special(struct vdc_port *port, struct vio_disk_desc *desc) 300 { 301 int err = desc->status; 302 303 vdc_finish(&port->vio, -err, WAITING_FOR_GEN_CMD); 304 } 305 306 static void vdc_end_one(struct vdc_port *port, struct vio_dring_state *dr, 307 unsigned int index) 308 { 309 struct vio_disk_desc *desc = vio_dring_entry(dr, index); 310 struct vdc_req_entry *rqe = &port->rq_arr[index]; 311 struct request *req; 312 313 if (unlikely(desc->hdr.state != VIO_DESC_DONE)) 314 return; 315 316 ldc_unmap(port->vio.lp, desc->cookies, desc->ncookies); 317 desc->hdr.state = VIO_DESC_FREE; 318 dr->cons = vio_dring_next(dr, index); 319 320 req = rqe->req; 321 if (req == NULL) { 322 vdc_end_special(port, desc); 323 return; 324 } 325 326 rqe->req = NULL; 327 328 blk_mq_end_request(req, desc->status ? BLK_STS_IOERR : 0); 329 330 vdc_blk_queue_start(port); 331 } 332 333 static int vdc_ack(struct vdc_port *port, void *msgbuf) 334 { 335 struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING]; 336 struct vio_dring_data *pkt = msgbuf; 337 338 if (unlikely(pkt->dring_ident != dr->ident || 339 pkt->start_idx != pkt->end_idx || 340 pkt->start_idx >= VDC_TX_RING_SIZE)) 341 return 0; 342 343 vdc_end_one(port, dr, pkt->start_idx); 344 345 return 0; 346 } 347 348 static int vdc_nack(struct vdc_port *port, void *msgbuf) 349 { 350 /* XXX Implement me XXX */ 351 return 0; 352 } 353 354 static void vdc_event(void *arg, int event) 355 { 356 struct vdc_port *port = arg; 357 struct vio_driver_state *vio = &port->vio; 358 unsigned long flags; 359 int err; 360 361 spin_lock_irqsave(&vio->lock, flags); 362 363 if (unlikely(event == LDC_EVENT_RESET)) { 364 vio_link_state_change(vio, event); 365 queue_work(sunvdc_wq, &port->ldc_reset_work); 366 goto out; 367 } 368 369 if (unlikely(event == LDC_EVENT_UP)) { 370 vio_link_state_change(vio, event); 371 goto out; 372 } 373 374 if (unlikely(event != LDC_EVENT_DATA_READY)) { 375 pr_warn(PFX "Unexpected LDC event %d\n", event); 376 goto out; 377 } 378 379 err = 0; 380 while (1) { 381 union { 382 struct vio_msg_tag tag; 383 u64 raw[8]; 384 } msgbuf; 385 386 err = ldc_read(vio->lp, &msgbuf, sizeof(msgbuf)); 387 if (unlikely(err < 0)) { 388 if (err == -ECONNRESET) 389 vio_conn_reset(vio); 390 break; 391 } 392 if (err == 0) 393 break; 394 viodbg(DATA, "TAG [%02x:%02x:%04x:%08x]\n", 395 msgbuf.tag.type, 396 msgbuf.tag.stype, 397 msgbuf.tag.stype_env, 398 msgbuf.tag.sid); 399 err = vio_validate_sid(vio, &msgbuf.tag); 400 if (err < 0) 401 break; 402 403 if (likely(msgbuf.tag.type == VIO_TYPE_DATA)) { 404 if (msgbuf.tag.stype == VIO_SUBTYPE_ACK) 405 err = vdc_ack(port, &msgbuf); 406 else if (msgbuf.tag.stype == VIO_SUBTYPE_NACK) 407 err = vdc_nack(port, &msgbuf); 408 else 409 err = vdc_handle_unknown(port, &msgbuf); 410 } else if (msgbuf.tag.type == VIO_TYPE_CTRL) { 411 err = vio_control_pkt_engine(vio, &msgbuf); 412 } else { 413 err = vdc_handle_unknown(port, &msgbuf); 414 } 415 if (err < 0) 416 break; 417 } 418 if (err < 0) 419 vdc_finish(&port->vio, err, WAITING_FOR_ANY); 420 out: 421 spin_unlock_irqrestore(&vio->lock, flags); 422 } 423 424 static int __vdc_tx_trigger(struct vdc_port *port) 425 { 426 struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING]; 427 struct vio_dring_data hdr = { 428 .tag = { 429 .type = VIO_TYPE_DATA, 430 .stype = VIO_SUBTYPE_INFO, 431 .stype_env = VIO_DRING_DATA, 432 .sid = vio_send_sid(&port->vio), 433 }, 434 .dring_ident = dr->ident, 435 .start_idx = dr->prod, 436 .end_idx = dr->prod, 437 }; 438 int err, delay; 439 int retries = 0; 440 441 hdr.seq = dr->snd_nxt; 442 delay = 1; 443 do { 444 err = vio_ldc_send(&port->vio, &hdr, sizeof(hdr)); 445 if (err > 0) { 446 dr->snd_nxt++; 447 break; 448 } 449 udelay(delay); 450 if ((delay <<= 1) > 128) 451 delay = 128; 452 if (retries++ > VDC_MAX_RETRIES) 453 break; 454 } while (err == -EAGAIN); 455 456 if (err == -ENOTCONN) 457 vdc_ldc_reset(port); 458 return err; 459 } 460 461 static int __send_request(struct request *req) 462 { 463 struct vdc_port *port = req->q->disk->private_data; 464 struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING]; 465 struct scatterlist sg[MAX_RING_COOKIES]; 466 struct vdc_req_entry *rqe; 467 struct vio_disk_desc *desc; 468 unsigned int map_perm; 469 int nsg, err, i; 470 u64 len; 471 u8 op; 472 473 if (WARN_ON(port->ring_cookies > MAX_RING_COOKIES)) 474 return -EINVAL; 475 476 map_perm = LDC_MAP_SHADOW | LDC_MAP_DIRECT | LDC_MAP_IO; 477 478 if (rq_data_dir(req) == READ) { 479 map_perm |= LDC_MAP_W; 480 op = VD_OP_BREAD; 481 } else { 482 map_perm |= LDC_MAP_R; 483 op = VD_OP_BWRITE; 484 } 485 486 sg_init_table(sg, port->ring_cookies); 487 nsg = blk_rq_map_sg(req, sg); 488 489 len = 0; 490 for (i = 0; i < nsg; i++) 491 len += sg[i].length; 492 493 desc = vio_dring_cur(dr); 494 495 err = ldc_map_sg(port->vio.lp, sg, nsg, 496 desc->cookies, port->ring_cookies, 497 map_perm); 498 if (err < 0) { 499 printk(KERN_ERR PFX "ldc_map_sg() failure, err=%d.\n", err); 500 return err; 501 } 502 503 rqe = &port->rq_arr[dr->prod]; 504 rqe->req = req; 505 506 desc->hdr.ack = VIO_ACK_ENABLE; 507 desc->req_id = port->req_id; 508 desc->operation = op; 509 if (port->vdisk_type == VD_DISK_TYPE_DISK) { 510 desc->slice = 0xff; 511 } else { 512 desc->slice = 0; 513 } 514 desc->status = ~0; 515 desc->offset = (blk_rq_pos(req) << 9) / port->vdisk_block_size; 516 desc->size = len; 517 desc->ncookies = err; 518 519 /* This has to be a non-SMP write barrier because we are writing 520 * to memory which is shared with the peer LDOM. 521 */ 522 wmb(); 523 desc->hdr.state = VIO_DESC_READY; 524 525 err = __vdc_tx_trigger(port); 526 if (err < 0) { 527 printk(KERN_ERR PFX "vdc_tx_trigger() failure, err=%d\n", err); 528 } else { 529 port->req_id++; 530 dr->prod = vio_dring_next(dr, dr->prod); 531 } 532 533 return err; 534 } 535 536 static blk_status_t vdc_queue_rq(struct blk_mq_hw_ctx *hctx, 537 const struct blk_mq_queue_data *bd) 538 { 539 struct vdc_port *port = hctx->queue->queuedata; 540 struct vio_dring_state *dr; 541 unsigned long flags; 542 543 dr = &port->vio.drings[VIO_DRIVER_TX_RING]; 544 545 blk_mq_start_request(bd->rq); 546 547 spin_lock_irqsave(&port->vio.lock, flags); 548 549 /* 550 * Doing drain, just end the request in error 551 */ 552 if (unlikely(port->drain)) { 553 spin_unlock_irqrestore(&port->vio.lock, flags); 554 return BLK_STS_IOERR; 555 } 556 557 if (unlikely(vdc_tx_dring_avail(dr) < 1)) { 558 spin_unlock_irqrestore(&port->vio.lock, flags); 559 blk_mq_stop_hw_queue(hctx); 560 return BLK_STS_DEV_RESOURCE; 561 } 562 563 if (__send_request(bd->rq) < 0) { 564 spin_unlock_irqrestore(&port->vio.lock, flags); 565 return BLK_STS_IOERR; 566 } 567 568 spin_unlock_irqrestore(&port->vio.lock, flags); 569 return BLK_STS_OK; 570 } 571 572 static int generic_request(struct vdc_port *port, u8 op, void *buf, int len) 573 { 574 struct vio_dring_state *dr; 575 struct vio_completion comp; 576 struct vio_disk_desc *desc; 577 unsigned int map_perm; 578 unsigned long flags; 579 int op_len, err; 580 void *req_buf; 581 582 if (!(((u64)1 << (u64)op) & port->operations)) 583 return -EOPNOTSUPP; 584 585 switch (op) { 586 case VD_OP_BREAD: 587 case VD_OP_BWRITE: 588 default: 589 return -EINVAL; 590 591 case VD_OP_FLUSH: 592 op_len = 0; 593 map_perm = 0; 594 break; 595 596 case VD_OP_GET_WCE: 597 op_len = sizeof(u32); 598 map_perm = LDC_MAP_W; 599 break; 600 601 case VD_OP_SET_WCE: 602 op_len = sizeof(u32); 603 map_perm = LDC_MAP_R; 604 break; 605 606 case VD_OP_GET_VTOC: 607 op_len = sizeof(struct vio_disk_vtoc); 608 map_perm = LDC_MAP_W; 609 break; 610 611 case VD_OP_SET_VTOC: 612 op_len = sizeof(struct vio_disk_vtoc); 613 map_perm = LDC_MAP_R; 614 break; 615 616 case VD_OP_GET_DISKGEOM: 617 op_len = sizeof(struct vio_disk_geom); 618 map_perm = LDC_MAP_W; 619 break; 620 621 case VD_OP_SET_DISKGEOM: 622 op_len = sizeof(struct vio_disk_geom); 623 map_perm = LDC_MAP_R; 624 break; 625 626 case VD_OP_SCSICMD: 627 op_len = 16; 628 map_perm = LDC_MAP_RW; 629 break; 630 631 case VD_OP_GET_DEVID: 632 op_len = sizeof(struct vio_disk_devid); 633 map_perm = LDC_MAP_W; 634 break; 635 636 case VD_OP_GET_EFI: 637 case VD_OP_SET_EFI: 638 return -EOPNOTSUPP; 639 } 640 641 map_perm |= LDC_MAP_SHADOW | LDC_MAP_DIRECT | LDC_MAP_IO; 642 643 op_len = (op_len + 7) & ~7; 644 req_buf = kzalloc(op_len, GFP_KERNEL); 645 if (!req_buf) 646 return -ENOMEM; 647 648 if (len > op_len) 649 len = op_len; 650 651 if (map_perm & LDC_MAP_R) 652 memcpy(req_buf, buf, len); 653 654 spin_lock_irqsave(&port->vio.lock, flags); 655 656 dr = &port->vio.drings[VIO_DRIVER_TX_RING]; 657 658 /* XXX If we want to use this code generically we have to 659 * XXX handle TX ring exhaustion etc. 660 */ 661 desc = vio_dring_cur(dr); 662 663 err = ldc_map_single(port->vio.lp, req_buf, op_len, 664 desc->cookies, port->ring_cookies, 665 map_perm); 666 if (err < 0) { 667 spin_unlock_irqrestore(&port->vio.lock, flags); 668 kfree(req_buf); 669 return err; 670 } 671 672 init_completion(&comp.com); 673 comp.waiting_for = WAITING_FOR_GEN_CMD; 674 port->vio.cmp = ∁ 675 676 desc->hdr.ack = VIO_ACK_ENABLE; 677 desc->req_id = port->req_id; 678 desc->operation = op; 679 desc->slice = 0; 680 desc->status = ~0; 681 desc->offset = 0; 682 desc->size = op_len; 683 desc->ncookies = err; 684 685 /* This has to be a non-SMP write barrier because we are writing 686 * to memory which is shared with the peer LDOM. 687 */ 688 wmb(); 689 desc->hdr.state = VIO_DESC_READY; 690 691 err = __vdc_tx_trigger(port); 692 if (err >= 0) { 693 port->req_id++; 694 dr->prod = vio_dring_next(dr, dr->prod); 695 spin_unlock_irqrestore(&port->vio.lock, flags); 696 697 wait_for_completion(&comp.com); 698 err = comp.err; 699 } else { 700 port->vio.cmp = NULL; 701 spin_unlock_irqrestore(&port->vio.lock, flags); 702 } 703 704 if (map_perm & LDC_MAP_W) 705 memcpy(buf, req_buf, len); 706 707 kfree(req_buf); 708 709 return err; 710 } 711 712 static int vdc_alloc_tx_ring(struct vdc_port *port) 713 { 714 struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING]; 715 unsigned long len, entry_size; 716 int ncookies; 717 void *dring; 718 719 entry_size = sizeof(struct vio_disk_desc) + 720 (sizeof(struct ldc_trans_cookie) * port->ring_cookies); 721 len = (VDC_TX_RING_SIZE * entry_size); 722 723 ncookies = VIO_MAX_RING_COOKIES; 724 dring = ldc_alloc_exp_dring(port->vio.lp, len, 725 dr->cookies, &ncookies, 726 (LDC_MAP_SHADOW | 727 LDC_MAP_DIRECT | 728 LDC_MAP_RW)); 729 if (IS_ERR(dring)) 730 return PTR_ERR(dring); 731 732 dr->base = dring; 733 dr->entry_size = entry_size; 734 dr->num_entries = VDC_TX_RING_SIZE; 735 dr->prod = dr->cons = 0; 736 dr->pending = VDC_TX_RING_SIZE; 737 dr->ncookies = ncookies; 738 739 return 0; 740 } 741 742 static void vdc_free_tx_ring(struct vdc_port *port) 743 { 744 struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING]; 745 746 if (dr->base) { 747 ldc_free_exp_dring(port->vio.lp, dr->base, 748 (dr->entry_size * dr->num_entries), 749 dr->cookies, dr->ncookies); 750 dr->base = NULL; 751 dr->entry_size = 0; 752 dr->num_entries = 0; 753 dr->pending = 0; 754 dr->ncookies = 0; 755 } 756 } 757 758 static int vdc_port_up(struct vdc_port *port) 759 { 760 struct vio_completion comp; 761 762 init_completion(&comp.com); 763 comp.err = 0; 764 comp.waiting_for = WAITING_FOR_LINK_UP; 765 port->vio.cmp = ∁ 766 767 vio_port_up(&port->vio); 768 wait_for_completion(&comp.com); 769 return comp.err; 770 } 771 772 static void vdc_port_down(struct vdc_port *port) 773 { 774 ldc_disconnect(port->vio.lp); 775 ldc_unbind(port->vio.lp); 776 vdc_free_tx_ring(port); 777 vio_ldc_free(&port->vio); 778 } 779 780 static const struct blk_mq_ops vdc_mq_ops = { 781 .queue_rq = vdc_queue_rq, 782 }; 783 784 static int probe_disk(struct vdc_port *port) 785 { 786 struct queue_limits lim = { 787 .physical_block_size = port->vdisk_phys_blksz, 788 .max_hw_sectors = port->max_xfer_size, 789 /* Each segment in a request is up to an aligned page in size. */ 790 .seg_boundary_mask = PAGE_SIZE - 1, 791 .max_segment_size = PAGE_SIZE, 792 .max_segments = port->ring_cookies, 793 .features = BLK_FEAT_ROTATIONAL, 794 }; 795 struct request_queue *q; 796 struct gendisk *g; 797 int err; 798 799 err = vdc_port_up(port); 800 if (err) 801 return err; 802 803 /* Using version 1.2 means vdisk_phys_blksz should be set unless the 804 * disk is reserved by another system. 805 */ 806 if (vdc_version_supported(port, 1, 2) && !port->vdisk_phys_blksz) 807 return -ENODEV; 808 809 if (vdc_version_supported(port, 1, 1)) { 810 /* vdisk_size should be set during the handshake, if it wasn't 811 * then the underlying disk is reserved by another system 812 */ 813 if (port->vdisk_size == -1) 814 return -ENODEV; 815 } else { 816 struct vio_disk_geom geom; 817 818 err = generic_request(port, VD_OP_GET_DISKGEOM, 819 &geom, sizeof(geom)); 820 if (err < 0) { 821 printk(KERN_ERR PFX "VD_OP_GET_DISKGEOM returns " 822 "error %d\n", err); 823 return err; 824 } 825 port->vdisk_size = ((u64)geom.num_cyl * 826 (u64)geom.num_hd * 827 (u64)geom.num_sec); 828 } 829 830 err = blk_mq_alloc_sq_tag_set(&port->tag_set, &vdc_mq_ops, 831 VDC_TX_RING_SIZE, 0); 832 if (err) 833 return err; 834 835 g = blk_mq_alloc_disk(&port->tag_set, &lim, port); 836 if (IS_ERR(g)) { 837 printk(KERN_ERR PFX "%s: Could not allocate gendisk.\n", 838 port->vio.name); 839 err = PTR_ERR(g); 840 goto out_free_tag; 841 } 842 843 port->disk = g; 844 q = g->queue; 845 846 g->major = vdc_major; 847 g->first_minor = port->vio.vdev->dev_no << PARTITION_SHIFT; 848 g->minors = 1 << PARTITION_SHIFT; 849 strcpy(g->disk_name, port->disk_name); 850 851 g->fops = &vdc_fops; 852 g->queue = q; 853 g->private_data = port; 854 855 set_capacity(g, port->vdisk_size); 856 857 if (vdc_version_supported(port, 1, 1)) { 858 switch (port->vdisk_mtype) { 859 case VD_MEDIA_TYPE_CD: 860 pr_info(PFX "Virtual CDROM %s\n", port->disk_name); 861 g->flags |= GENHD_FL_REMOVABLE; 862 set_disk_ro(g, 1); 863 break; 864 865 case VD_MEDIA_TYPE_DVD: 866 pr_info(PFX "Virtual DVD %s\n", port->disk_name); 867 g->flags |= GENHD_FL_REMOVABLE; 868 set_disk_ro(g, 1); 869 break; 870 871 case VD_MEDIA_TYPE_FIXED: 872 pr_info(PFX "Virtual Hard disk %s\n", port->disk_name); 873 break; 874 } 875 } 876 877 pr_info(PFX "%s: %u sectors (%u MB) protocol %d.%d\n", 878 g->disk_name, 879 port->vdisk_size, (port->vdisk_size >> (20 - 9)), 880 port->vio.ver.major, port->vio.ver.minor); 881 882 err = device_add_disk(&port->vio.vdev->dev, g, NULL); 883 if (err) 884 goto out_cleanup_disk; 885 886 return 0; 887 888 out_cleanup_disk: 889 put_disk(g); 890 out_free_tag: 891 blk_mq_free_tag_set(&port->tag_set); 892 return err; 893 } 894 895 static struct ldc_channel_config vdc_ldc_cfg = { 896 .event = vdc_event, 897 .mtu = 64, 898 .mode = LDC_MODE_UNRELIABLE, 899 }; 900 901 static struct vio_driver_ops vdc_vio_ops = { 902 .send_attr = vdc_send_attr, 903 .handle_attr = vdc_handle_attr, 904 .handshake_complete = vdc_handshake_complete, 905 }; 906 907 static void print_version(void) 908 { 909 static int version_printed; 910 911 if (version_printed++ == 0) 912 printk(KERN_INFO "%s", version); 913 } 914 915 struct vdc_check_port_data { 916 int dev_no; 917 char *type; 918 }; 919 920 static int vdc_device_probed(struct device *dev, const void *arg) 921 { 922 struct vio_dev *vdev = to_vio_dev(dev); 923 const struct vdc_check_port_data *port_data; 924 925 port_data = (const struct vdc_check_port_data *)arg; 926 927 if ((vdev->dev_no == port_data->dev_no) && 928 (!(strcmp((char *)&vdev->type, port_data->type))) && 929 dev_get_drvdata(dev)) { 930 /* This device has already been configured 931 * by vdc_port_probe() 932 */ 933 return 1; 934 } else { 935 return 0; 936 } 937 } 938 939 /* Determine whether the VIO device is part of an mpgroup 940 * by locating all the virtual-device-port nodes associated 941 * with the parent virtual-device node for the VIO device 942 * and checking whether any of these nodes are vdc-ports 943 * which have already been configured. 944 * 945 * Returns true if this device is part of an mpgroup and has 946 * already been probed. 947 */ 948 static bool vdc_port_mpgroup_check(struct vio_dev *vdev) 949 { 950 struct vdc_check_port_data port_data; 951 struct device *dev; 952 953 port_data.dev_no = vdev->dev_no; 954 port_data.type = (char *)&vdev->type; 955 956 dev = device_find_child(vdev->dev.parent, &port_data, 957 vdc_device_probed); 958 959 if (dev) { 960 put_device(dev); 961 return true; 962 } 963 964 return false; 965 } 966 967 static int vdc_port_probe(struct vio_dev *vdev, const struct vio_device_id *id) 968 { 969 struct mdesc_handle *hp; 970 struct vdc_port *port; 971 int err; 972 const u64 *ldc_timeout; 973 974 print_version(); 975 976 hp = mdesc_grab(); 977 if (!hp) 978 return -ENODEV; 979 980 err = -ENODEV; 981 if ((vdev->dev_no << PARTITION_SHIFT) & ~(u64)MINORMASK) { 982 printk(KERN_ERR PFX "Port id [%llu] too large.\n", 983 vdev->dev_no); 984 goto err_out_release_mdesc; 985 } 986 987 /* Check if this device is part of an mpgroup */ 988 if (vdc_port_mpgroup_check(vdev)) { 989 printk(KERN_WARNING 990 "VIO: Ignoring extra vdisk port %s", 991 dev_name(&vdev->dev)); 992 goto err_out_release_mdesc; 993 } 994 995 port = kzalloc(sizeof(*port), GFP_KERNEL); 996 if (!port) { 997 err = -ENOMEM; 998 goto err_out_release_mdesc; 999 } 1000 1001 if (vdev->dev_no >= 26) 1002 snprintf(port->disk_name, sizeof(port->disk_name), 1003 VDCBLK_NAME "%c%c", 1004 'a' + ((int)vdev->dev_no / 26) - 1, 1005 'a' + ((int)vdev->dev_no % 26)); 1006 else 1007 snprintf(port->disk_name, sizeof(port->disk_name), 1008 VDCBLK_NAME "%c", 'a' + ((int)vdev->dev_no % 26)); 1009 port->vdisk_size = -1; 1010 1011 /* Actual wall time may be double due to do_generic_file_read() doing 1012 * a readahead I/O first, and once that fails it will try to read a 1013 * single page. 1014 */ 1015 ldc_timeout = mdesc_get_property(hp, vdev->mp, "vdc-timeout", NULL); 1016 port->ldc_timeout = ldc_timeout ? *ldc_timeout : 0; 1017 INIT_DELAYED_WORK(&port->ldc_reset_timer_work, vdc_ldc_reset_timer_work); 1018 INIT_WORK(&port->ldc_reset_work, vdc_ldc_reset_work); 1019 1020 err = vio_driver_init(&port->vio, vdev, VDEV_DISK, 1021 vdc_versions, ARRAY_SIZE(vdc_versions), 1022 &vdc_vio_ops, port->disk_name); 1023 if (err) 1024 goto err_out_free_port; 1025 1026 port->vdisk_block_size = VDC_DEFAULT_BLK_SIZE; 1027 port->max_xfer_size = MAX_XFER_SIZE; 1028 port->ring_cookies = MAX_RING_COOKIES; 1029 1030 err = vio_ldc_alloc(&port->vio, &vdc_ldc_cfg, port); 1031 if (err) 1032 goto err_out_free_port; 1033 1034 err = vdc_alloc_tx_ring(port); 1035 if (err) 1036 goto err_out_free_ldc; 1037 1038 err = probe_disk(port); 1039 if (err) 1040 goto err_out_free_tx_ring; 1041 1042 /* Note that the device driver_data is used to determine 1043 * whether the port has been probed. 1044 */ 1045 dev_set_drvdata(&vdev->dev, port); 1046 1047 mdesc_release(hp); 1048 1049 return 0; 1050 1051 err_out_free_tx_ring: 1052 vdc_free_tx_ring(port); 1053 1054 err_out_free_ldc: 1055 vio_ldc_free(&port->vio); 1056 1057 err_out_free_port: 1058 kfree(port); 1059 1060 err_out_release_mdesc: 1061 mdesc_release(hp); 1062 return err; 1063 } 1064 1065 static void vdc_port_remove(struct vio_dev *vdev) 1066 { 1067 struct vdc_port *port = dev_get_drvdata(&vdev->dev); 1068 1069 if (port) { 1070 blk_mq_stop_hw_queues(port->disk->queue); 1071 1072 flush_work(&port->ldc_reset_work); 1073 cancel_delayed_work_sync(&port->ldc_reset_timer_work); 1074 timer_delete_sync(&port->vio.timer); 1075 1076 del_gendisk(port->disk); 1077 put_disk(port->disk); 1078 blk_mq_free_tag_set(&port->tag_set); 1079 1080 vdc_free_tx_ring(port); 1081 vio_ldc_free(&port->vio); 1082 1083 dev_set_drvdata(&vdev->dev, NULL); 1084 1085 kfree(port); 1086 } 1087 } 1088 1089 static void vdc_requeue_inflight(struct vdc_port *port) 1090 { 1091 struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING]; 1092 u32 idx; 1093 1094 for (idx = dr->cons; idx != dr->prod; idx = vio_dring_next(dr, idx)) { 1095 struct vio_disk_desc *desc = vio_dring_entry(dr, idx); 1096 struct vdc_req_entry *rqe = &port->rq_arr[idx]; 1097 struct request *req; 1098 1099 ldc_unmap(port->vio.lp, desc->cookies, desc->ncookies); 1100 desc->hdr.state = VIO_DESC_FREE; 1101 dr->cons = vio_dring_next(dr, idx); 1102 1103 req = rqe->req; 1104 if (req == NULL) { 1105 vdc_end_special(port, desc); 1106 continue; 1107 } 1108 1109 rqe->req = NULL; 1110 blk_mq_requeue_request(req, false); 1111 } 1112 } 1113 1114 static void vdc_queue_drain(struct vdc_port *port) 1115 { 1116 struct request_queue *q = port->disk->queue; 1117 unsigned int memflags; 1118 1119 /* 1120 * Mark the queue as draining, then freeze/quiesce to ensure 1121 * that all existing requests are seen in ->queue_rq() and killed 1122 */ 1123 port->drain = 1; 1124 spin_unlock_irq(&port->vio.lock); 1125 1126 memflags = blk_mq_freeze_queue(q); 1127 blk_mq_quiesce_queue(q); 1128 1129 spin_lock_irq(&port->vio.lock); 1130 port->drain = 0; 1131 blk_mq_unquiesce_queue(q); 1132 blk_mq_unfreeze_queue(q, memflags); 1133 } 1134 1135 static void vdc_ldc_reset_timer_work(struct work_struct *work) 1136 { 1137 struct vdc_port *port; 1138 struct vio_driver_state *vio; 1139 1140 port = container_of(work, struct vdc_port, ldc_reset_timer_work.work); 1141 vio = &port->vio; 1142 1143 spin_lock_irq(&vio->lock); 1144 if (!(port->vio.hs_state & VIO_HS_COMPLETE)) { 1145 pr_warn(PFX "%s ldc down %llu seconds, draining queue\n", 1146 port->disk_name, port->ldc_timeout); 1147 vdc_queue_drain(port); 1148 vdc_blk_queue_start(port); 1149 } 1150 spin_unlock_irq(&vio->lock); 1151 } 1152 1153 static void vdc_ldc_reset_work(struct work_struct *work) 1154 { 1155 struct vdc_port *port; 1156 struct vio_driver_state *vio; 1157 unsigned long flags; 1158 1159 port = container_of(work, struct vdc_port, ldc_reset_work); 1160 vio = &port->vio; 1161 1162 spin_lock_irqsave(&vio->lock, flags); 1163 vdc_ldc_reset(port); 1164 spin_unlock_irqrestore(&vio->lock, flags); 1165 } 1166 1167 static void vdc_ldc_reset(struct vdc_port *port) 1168 { 1169 int err; 1170 1171 assert_spin_locked(&port->vio.lock); 1172 1173 pr_warn(PFX "%s ldc link reset\n", port->disk_name); 1174 blk_mq_stop_hw_queues(port->disk->queue); 1175 vdc_requeue_inflight(port); 1176 vdc_port_down(port); 1177 1178 err = vio_ldc_alloc(&port->vio, &vdc_ldc_cfg, port); 1179 if (err) { 1180 pr_err(PFX "%s vio_ldc_alloc:%d\n", port->disk_name, err); 1181 return; 1182 } 1183 1184 err = vdc_alloc_tx_ring(port); 1185 if (err) { 1186 pr_err(PFX "%s vio_alloc_tx_ring:%d\n", port->disk_name, err); 1187 goto err_free_ldc; 1188 } 1189 1190 if (port->ldc_timeout) 1191 mod_delayed_work(system_percpu_wq, &port->ldc_reset_timer_work, 1192 round_jiffies(jiffies + HZ * port->ldc_timeout)); 1193 mod_timer(&port->vio.timer, round_jiffies(jiffies + HZ)); 1194 return; 1195 1196 err_free_ldc: 1197 vio_ldc_free(&port->vio); 1198 } 1199 1200 static const struct vio_device_id vdc_port_match[] = { 1201 { 1202 .type = "vdc-port", 1203 }, 1204 {}, 1205 }; 1206 MODULE_DEVICE_TABLE(vio, vdc_port_match); 1207 1208 static struct vio_driver vdc_port_driver = { 1209 .id_table = vdc_port_match, 1210 .probe = vdc_port_probe, 1211 .remove = vdc_port_remove, 1212 .name = "vdc_port", 1213 }; 1214 1215 static int __init vdc_init(void) 1216 { 1217 int err; 1218 1219 sunvdc_wq = alloc_workqueue("sunvdc", WQ_PERCPU, 0); 1220 if (!sunvdc_wq) 1221 return -ENOMEM; 1222 1223 err = register_blkdev(0, VDCBLK_NAME); 1224 if (err < 0) 1225 goto out_free_wq; 1226 1227 vdc_major = err; 1228 1229 err = vio_register_driver(&vdc_port_driver); 1230 if (err) 1231 goto out_unregister_blkdev; 1232 1233 return 0; 1234 1235 out_unregister_blkdev: 1236 unregister_blkdev(vdc_major, VDCBLK_NAME); 1237 vdc_major = 0; 1238 1239 out_free_wq: 1240 destroy_workqueue(sunvdc_wq); 1241 return err; 1242 } 1243 1244 static void __exit vdc_exit(void) 1245 { 1246 vio_unregister_driver(&vdc_port_driver); 1247 unregister_blkdev(vdc_major, VDCBLK_NAME); 1248 destroy_workqueue(sunvdc_wq); 1249 } 1250 1251 module_init(vdc_init); 1252 module_exit(vdc_exit); 1253