1/* 2 * Xen SCSI frontend driver 3 * 4 * Copyright (c) 2008, FUJITSU Limited 5 * 6 * This program is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU General Public License version 2 8 * as published by the Free Software Foundation; or, when distributed 9 * separately from the Linux kernel or incorporated into other 10 * software packages, subject to the following license: 11 * 12 * Permission is hereby granted, free of charge, to any person obtaining a copy 13 * of this source file (the "Software"), to deal in the Software without 14 * restriction, including without limitation the rights to use, copy, modify, 15 * merge, publish, distribute, sublicense, and/or sell copies of the Software, 16 * and to permit persons to whom the Software is furnished to do so, subject to 17 * the following conditions: 18 * 19 * The above copyright notice and this permission notice shall be included in 20 * all copies or substantial portions of the Software. 21 * 22 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 23 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 24 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 25 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 26 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 27 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 28 * IN THE SOFTWARE. 29 */ 30 31#include <linux/module.h> 32#include <linux/kernel.h> 33#include <linux/device.h> 34#include <linux/wait.h> 35#include <linux/interrupt.h> 36#include <linux/mutex.h> 37#include <linux/spinlock.h> 38#include <linux/sched.h> 39#include <linux/blkdev.h> 40#include <linux/pfn.h> 41#include <linux/slab.h> 42#include <linux/bitops.h> 43 44#include <scsi/scsi_cmnd.h> 45#include <scsi/scsi_device.h> 46#include <scsi/scsi.h> 47#include <scsi/scsi_host.h> 48 49#include <xen/xen.h> 50#include <xen/xenbus.h> 51#include <xen/grant_table.h> 52#include <xen/events.h> 53#include <xen/page.h> 54 55#include <xen/interface/grant_table.h> 56#include <xen/interface/io/vscsiif.h> 57#include <xen/interface/io/protocols.h> 58 59#include <asm/xen/hypervisor.h> 60 61 62#define GRANT_INVALID_REF 0 63 64#define VSCSIFRONT_OP_ADD_LUN 1 65#define VSCSIFRONT_OP_DEL_LUN 2 66 67/* Tuning point. */ 68#define VSCSIIF_DEFAULT_CMD_PER_LUN 10 69#define VSCSIIF_MAX_TARGET 64 70#define VSCSIIF_MAX_LUN 255 71 72#define VSCSIIF_RING_SIZE __CONST_RING_SIZE(vscsiif, PAGE_SIZE) 73#define VSCSIIF_MAX_REQS VSCSIIF_RING_SIZE 74 75#define vscsiif_grants_sg(_sg) (PFN_UP((_sg) * \ 76 sizeof(struct scsiif_request_segment))) 77 78struct vscsifrnt_shadow { 79 /* command between backend and frontend */ 80 unsigned char act; 81 uint16_t rqid; 82 83 unsigned int nr_grants; /* number of grants in gref[] */ 84 struct scsiif_request_segment *sg; /* scatter/gather elements */ 85 86 /* Do reset or abort function. */ 87 wait_queue_head_t wq_reset; /* reset work queue */ 88 int wait_reset; /* reset work queue condition */ 89 int32_t rslt_reset; /* reset response status: */ 90 /* SUCCESS or FAILED or: */ 91#define RSLT_RESET_WAITING 0 92#define RSLT_RESET_ERR -1 93 94 /* Requested struct scsi_cmnd is stored from kernel. */ 95 struct scsi_cmnd *sc; 96 int gref[vscsiif_grants_sg(SG_ALL) + SG_ALL]; 97}; 98 99struct vscsifrnt_info { 100 struct xenbus_device *dev; 101 102 struct Scsi_Host *host; 103 int host_active; 104 105 unsigned int evtchn; 106 unsigned int irq; 107 108 grant_ref_t ring_ref; 109 struct vscsiif_front_ring ring; 110 struct vscsiif_response ring_rsp; 111 112 spinlock_t shadow_lock; 113 DECLARE_BITMAP(shadow_free_bitmap, VSCSIIF_MAX_REQS); 114 struct vscsifrnt_shadow *shadow[VSCSIIF_MAX_REQS]; 115 116 wait_queue_head_t wq_sync; 117 unsigned int wait_ring_available:1; 118 119 char dev_state_path[64]; 120 struct task_struct *curr; 121}; 122 123static DEFINE_MUTEX(scsifront_mutex); 124 125static void scsifront_wake_up(struct vscsifrnt_info *info) 126{ 127 info->wait_ring_available = 0; 128 wake_up(&info->wq_sync); 129} 130 131static int scsifront_get_rqid(struct vscsifrnt_info *info) 132{ 133 unsigned long flags; 134 int free; 135 136 spin_lock_irqsave(&info->shadow_lock, flags); 137 138 free = find_first_bit(info->shadow_free_bitmap, VSCSIIF_MAX_REQS); 139 __clear_bit(free, info->shadow_free_bitmap); 140 141 spin_unlock_irqrestore(&info->shadow_lock, flags); 142 143 return free; 144} 145 146static int _scsifront_put_rqid(struct vscsifrnt_info *info, uint32_t id) 147{ 148 int empty = bitmap_empty(info->shadow_free_bitmap, VSCSIIF_MAX_REQS); 149 150 __set_bit(id, info->shadow_free_bitmap); 151 info->shadow[id] = NULL; 152 153 return empty || info->wait_ring_available; 154} 155 156static void scsifront_put_rqid(struct vscsifrnt_info *info, uint32_t id) 157{ 158 unsigned long flags; 159 int kick; 160 161 spin_lock_irqsave(&info->shadow_lock, flags); 162 kick = _scsifront_put_rqid(info, id); 163 spin_unlock_irqrestore(&info->shadow_lock, flags); 164 165 if (kick) 166 scsifront_wake_up(info); 167} 168 169static struct vscsiif_request *scsifront_pre_req(struct vscsifrnt_info *info) 170{ 171 struct vscsiif_front_ring *ring = &(info->ring); 172 struct vscsiif_request *ring_req; 173 uint32_t id; 174 175 id = scsifront_get_rqid(info); /* use id in response */ 176 if (id >= VSCSIIF_MAX_REQS) 177 return NULL; 178 179 ring_req = RING_GET_REQUEST(&(info->ring), ring->req_prod_pvt); 180 181 ring->req_prod_pvt++; 182 183 ring_req->rqid = (uint16_t)id; 184 185 return ring_req; 186} 187 188static void scsifront_do_request(struct vscsifrnt_info *info) 189{ 190 struct vscsiif_front_ring *ring = &(info->ring); 191 int notify; 192 193 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(ring, notify); 194 if (notify) 195 notify_remote_via_irq(info->irq); 196} 197 198static void scsifront_gnttab_done(struct vscsifrnt_info *info, uint32_t id) 199{ 200 struct vscsifrnt_shadow *s = info->shadow[id]; 201 int i; 202 203 if (s->sc->sc_data_direction == DMA_NONE) 204 return; 205 206 for (i = 0; i < s->nr_grants; i++) { 207 if (unlikely(gnttab_query_foreign_access(s->gref[i]) != 0)) { 208 shost_printk(KERN_ALERT, info->host, KBUILD_MODNAME 209 "grant still in use by backend\n"); 210 BUG(); 211 } 212 gnttab_end_foreign_access(s->gref[i], 0, 0UL); 213 } 214 215 kfree(s->sg); 216} 217 218static void scsifront_cdb_cmd_done(struct vscsifrnt_info *info, 219 struct vscsiif_response *ring_rsp) 220{ 221 struct scsi_cmnd *sc; 222 uint32_t id; 223 uint8_t sense_len; 224 225 id = ring_rsp->rqid; 226 sc = info->shadow[id]->sc; 227 228 BUG_ON(sc == NULL); 229 230 scsifront_gnttab_done(info, id); 231 scsifront_put_rqid(info, id); 232 233 sc->result = ring_rsp->rslt; 234 scsi_set_resid(sc, ring_rsp->residual_len); 235 236 sense_len = min_t(uint8_t, VSCSIIF_SENSE_BUFFERSIZE, 237 ring_rsp->sense_len); 238 239 if (sense_len) 240 memcpy(sc->sense_buffer, ring_rsp->sense_buffer, sense_len); 241 242 sc->scsi_done(sc); 243} 244 245static void scsifront_sync_cmd_done(struct vscsifrnt_info *info, 246 struct vscsiif_response *ring_rsp) 247{ 248 uint16_t id = ring_rsp->rqid; 249 unsigned long flags; 250 struct vscsifrnt_shadow *shadow = info->shadow[id]; 251 int kick; 252 253 spin_lock_irqsave(&info->shadow_lock, flags); 254 shadow->wait_reset = 1; 255 switch (shadow->rslt_reset) { 256 case RSLT_RESET_WAITING: 257 shadow->rslt_reset = ring_rsp->rslt; 258 break; 259 case RSLT_RESET_ERR: 260 kick = _scsifront_put_rqid(info, id); 261 spin_unlock_irqrestore(&info->shadow_lock, flags); 262 kfree(shadow); 263 if (kick) 264 scsifront_wake_up(info); 265 return; 266 default: 267 shost_printk(KERN_ERR, info->host, KBUILD_MODNAME 268 "bad reset state %d, possibly leaking %u\n", 269 shadow->rslt_reset, id); 270 break; 271 } 272 spin_unlock_irqrestore(&info->shadow_lock, flags); 273 274 wake_up(&shadow->wq_reset); 275} 276 277static int scsifront_cmd_done(struct vscsifrnt_info *info) 278{ 279 struct vscsiif_response *ring_rsp; 280 RING_IDX i, rp; 281 int more_to_do = 0; 282 unsigned long flags; 283 284 spin_lock_irqsave(info->host->host_lock, flags); 285 286 rp = info->ring.sring->rsp_prod; 287 rmb(); /* ordering required respective to dom0 */ 288 for (i = info->ring.rsp_cons; i != rp; i++) { 289 290 ring_rsp = RING_GET_RESPONSE(&info->ring, i); 291 292 if (WARN(ring_rsp->rqid >= VSCSIIF_MAX_REQS || 293 test_bit(ring_rsp->rqid, info->shadow_free_bitmap), 294 "illegal rqid %u returned by backend!\n", 295 ring_rsp->rqid)) 296 continue; 297 298 if (info->shadow[ring_rsp->rqid]->act == VSCSIIF_ACT_SCSI_CDB) 299 scsifront_cdb_cmd_done(info, ring_rsp); 300 else 301 scsifront_sync_cmd_done(info, ring_rsp); 302 } 303 304 info->ring.rsp_cons = i; 305 306 if (i != info->ring.req_prod_pvt) 307 RING_FINAL_CHECK_FOR_RESPONSES(&info->ring, more_to_do); 308 else 309 info->ring.sring->rsp_event = i + 1; 310 311 info->wait_ring_available = 0; 312 313 spin_unlock_irqrestore(info->host->host_lock, flags); 314 315 wake_up(&info->wq_sync); 316 317 return more_to_do; 318} 319 320static irqreturn_t scsifront_irq_fn(int irq, void *dev_id) 321{ 322 struct vscsifrnt_info *info = dev_id; 323 324 while (scsifront_cmd_done(info)) 325 /* Yield point for this unbounded loop. */ 326 cond_resched(); 327 328 return IRQ_HANDLED; 329} 330 331static int map_data_for_request(struct vscsifrnt_info *info, 332 struct scsi_cmnd *sc, 333 struct vscsiif_request *ring_req, 334 struct vscsifrnt_shadow *shadow) 335{ 336 grant_ref_t gref_head; 337 struct page *page; 338 int err, ref, ref_cnt = 0; 339 int grant_ro = (sc->sc_data_direction == DMA_TO_DEVICE); 340 unsigned int i, off, len, bytes; 341 unsigned int data_len = scsi_bufflen(sc); 342 unsigned int data_grants = 0, seg_grants = 0; 343 struct scatterlist *sg; 344 unsigned long mfn; 345 struct scsiif_request_segment *seg; 346 347 ring_req->nr_segments = 0; 348 if (sc->sc_data_direction == DMA_NONE || !data_len) 349 return 0; 350 351 scsi_for_each_sg(sc, sg, scsi_sg_count(sc), i) 352 data_grants += PFN_UP(sg->offset + sg->length); 353 354 if (data_grants > VSCSIIF_SG_TABLESIZE) { 355 if (data_grants > info->host->sg_tablesize) { 356 shost_printk(KERN_ERR, info->host, KBUILD_MODNAME 357 "Unable to map request_buffer for command!\n"); 358 return -E2BIG; 359 } 360 seg_grants = vscsiif_grants_sg(data_grants); 361 shadow->sg = kcalloc(data_grants, 362 sizeof(struct scsiif_request_segment), GFP_ATOMIC); 363 if (!shadow->sg) 364 return -ENOMEM; 365 } 366 seg = shadow->sg ? : ring_req->seg; 367 368 err = gnttab_alloc_grant_references(seg_grants + data_grants, 369 &gref_head); 370 if (err) { 371 kfree(shadow->sg); 372 shost_printk(KERN_ERR, info->host, KBUILD_MODNAME 373 "gnttab_alloc_grant_references() error\n"); 374 return -ENOMEM; 375 } 376 377 if (seg_grants) { 378 page = virt_to_page(seg); 379 off = (unsigned long)seg & ~PAGE_MASK; 380 len = sizeof(struct scsiif_request_segment) * data_grants; 381 while (len > 0) { 382 bytes = min_t(unsigned int, len, PAGE_SIZE - off); 383 384 ref = gnttab_claim_grant_reference(&gref_head); 385 BUG_ON(ref == -ENOSPC); 386 387 mfn = pfn_to_mfn(page_to_pfn(page)); 388 gnttab_grant_foreign_access_ref(ref, 389 info->dev->otherend_id, mfn, 1); 390 shadow->gref[ref_cnt] = ref; 391 ring_req->seg[ref_cnt].gref = ref; 392 ring_req->seg[ref_cnt].offset = (uint16_t)off; 393 ring_req->seg[ref_cnt].length = (uint16_t)bytes; 394 395 page++; 396 len -= bytes; 397 off = 0; 398 ref_cnt++; 399 } 400 BUG_ON(seg_grants < ref_cnt); 401 seg_grants = ref_cnt; 402 } 403 404 scsi_for_each_sg(sc, sg, scsi_sg_count(sc), i) { 405 page = sg_page(sg); 406 off = sg->offset; 407 len = sg->length; 408 409 while (len > 0 && data_len > 0) { 410 /* 411 * sg sends a scatterlist that is larger than 412 * the data_len it wants transferred for certain 413 * IO sizes. 414 */ 415 bytes = min_t(unsigned int, len, PAGE_SIZE - off); 416 bytes = min(bytes, data_len); 417 418 ref = gnttab_claim_grant_reference(&gref_head); 419 BUG_ON(ref == -ENOSPC); 420 421 mfn = pfn_to_mfn(page_to_pfn(page)); 422 gnttab_grant_foreign_access_ref(ref, 423 info->dev->otherend_id, mfn, grant_ro); 424 425 shadow->gref[ref_cnt] = ref; 426 seg->gref = ref; 427 seg->offset = (uint16_t)off; 428 seg->length = (uint16_t)bytes; 429 430 page++; 431 seg++; 432 len -= bytes; 433 data_len -= bytes; 434 off = 0; 435 ref_cnt++; 436 } 437 } 438 439 if (seg_grants) 440 ring_req->nr_segments = VSCSIIF_SG_GRANT | seg_grants; 441 else 442 ring_req->nr_segments = (uint8_t)ref_cnt; 443 shadow->nr_grants = ref_cnt; 444 445 return 0; 446} 447 448static struct vscsiif_request *scsifront_command2ring( 449 struct vscsifrnt_info *info, struct scsi_cmnd *sc, 450 struct vscsifrnt_shadow *shadow) 451{ 452 struct vscsiif_request *ring_req; 453 454 memset(shadow, 0, sizeof(*shadow)); 455 456 ring_req = scsifront_pre_req(info); 457 if (!ring_req) 458 return NULL; 459 460 info->shadow[ring_req->rqid] = shadow; 461 shadow->rqid = ring_req->rqid; 462 463 ring_req->id = sc->device->id; 464 ring_req->lun = sc->device->lun; 465 ring_req->channel = sc->device->channel; 466 ring_req->cmd_len = sc->cmd_len; 467 468 BUG_ON(sc->cmd_len > VSCSIIF_MAX_COMMAND_SIZE); 469 470 memcpy(ring_req->cmnd, sc->cmnd, sc->cmd_len); 471 472 ring_req->sc_data_direction = (uint8_t)sc->sc_data_direction; 473 ring_req->timeout_per_command = sc->request->timeout / HZ; 474 475 return ring_req; 476} 477 478static int scsifront_queuecommand(struct Scsi_Host *shost, 479 struct scsi_cmnd *sc) 480{ 481 struct vscsifrnt_info *info = shost_priv(shost); 482 struct vscsiif_request *ring_req; 483 struct vscsifrnt_shadow *shadow = scsi_cmd_priv(sc); 484 unsigned long flags; 485 int err; 486 uint16_t rqid; 487 488 spin_lock_irqsave(shost->host_lock, flags); 489 if (RING_FULL(&info->ring)) 490 goto busy; 491 492 ring_req = scsifront_command2ring(info, sc, shadow); 493 if (!ring_req) 494 goto busy; 495 496 sc->result = 0; 497 498 rqid = ring_req->rqid; 499 ring_req->act = VSCSIIF_ACT_SCSI_CDB; 500 501 shadow->sc = sc; 502 shadow->act = VSCSIIF_ACT_SCSI_CDB; 503 504 err = map_data_for_request(info, sc, ring_req, shadow); 505 if (err < 0) { 506 pr_debug("%s: err %d\n", __func__, err); 507 scsifront_put_rqid(info, rqid); 508 spin_unlock_irqrestore(shost->host_lock, flags); 509 if (err == -ENOMEM) 510 return SCSI_MLQUEUE_HOST_BUSY; 511 sc->result = DID_ERROR << 16; 512 sc->scsi_done(sc); 513 return 0; 514 } 515 516 scsifront_do_request(info); 517 spin_unlock_irqrestore(shost->host_lock, flags); 518 519 return 0; 520 521busy: 522 spin_unlock_irqrestore(shost->host_lock, flags); 523 pr_debug("%s: busy\n", __func__); 524 return SCSI_MLQUEUE_HOST_BUSY; 525} 526 527/* 528 * Any exception handling (reset or abort) must be forwarded to the backend. 529 * We have to wait until an answer is returned. This answer contains the 530 * result to be returned to the requestor. 531 */ 532static int scsifront_action_handler(struct scsi_cmnd *sc, uint8_t act) 533{ 534 struct Scsi_Host *host = sc->device->host; 535 struct vscsifrnt_info *info = shost_priv(host); 536 struct vscsifrnt_shadow *shadow, *s = scsi_cmd_priv(sc); 537 struct vscsiif_request *ring_req; 538 int err = 0; 539 540 shadow = kmalloc(sizeof(*shadow), GFP_NOIO); 541 if (!shadow) 542 return FAILED; 543 544 spin_lock_irq(host->host_lock); 545 546 for (;;) { 547 if (!RING_FULL(&info->ring)) { 548 ring_req = scsifront_command2ring(info, sc, shadow); 549 if (ring_req) 550 break; 551 } 552 if (err) { 553 spin_unlock_irq(host->host_lock); 554 kfree(shadow); 555 return FAILED; 556 } 557 info->wait_ring_available = 1; 558 spin_unlock_irq(host->host_lock); 559 err = wait_event_interruptible(info->wq_sync, 560 !info->wait_ring_available); 561 spin_lock_irq(host->host_lock); 562 } 563 564 ring_req->act = act; 565 ring_req->ref_rqid = s->rqid; 566 567 shadow->act = act; 568 shadow->rslt_reset = RSLT_RESET_WAITING; 569 init_waitqueue_head(&shadow->wq_reset); 570 571 ring_req->nr_segments = 0; 572 573 scsifront_do_request(info); 574 575 spin_unlock_irq(host->host_lock); 576 err = wait_event_interruptible(shadow->wq_reset, shadow->wait_reset); 577 spin_lock_irq(host->host_lock); 578 579 if (!err) { 580 err = shadow->rslt_reset; 581 scsifront_put_rqid(info, shadow->rqid); 582 kfree(shadow); 583 } else { 584 spin_lock(&info->shadow_lock); 585 shadow->rslt_reset = RSLT_RESET_ERR; 586 spin_unlock(&info->shadow_lock); 587 err = FAILED; 588 } 589 590 spin_unlock_irq(host->host_lock); 591 return err; 592} 593 594static int scsifront_eh_abort_handler(struct scsi_cmnd *sc) 595{ 596 pr_debug("%s\n", __func__); 597 return scsifront_action_handler(sc, VSCSIIF_ACT_SCSI_ABORT); 598} 599 600static int scsifront_dev_reset_handler(struct scsi_cmnd *sc) 601{ 602 pr_debug("%s\n", __func__); 603 return scsifront_action_handler(sc, VSCSIIF_ACT_SCSI_RESET); 604} 605 606static int scsifront_sdev_configure(struct scsi_device *sdev) 607{ 608 struct vscsifrnt_info *info = shost_priv(sdev->host); 609 610 if (info && current == info->curr) 611 xenbus_printf(XBT_NIL, info->dev->nodename, 612 info->dev_state_path, "%d", XenbusStateConnected); 613 614 return 0; 615} 616 617static void scsifront_sdev_destroy(struct scsi_device *sdev) 618{ 619 struct vscsifrnt_info *info = shost_priv(sdev->host); 620 621 if (info && current == info->curr) 622 xenbus_printf(XBT_NIL, info->dev->nodename, 623 info->dev_state_path, "%d", XenbusStateClosed); 624} 625 626static struct scsi_host_template scsifront_sht = { 627 .module = THIS_MODULE, 628 .name = "Xen SCSI frontend driver", 629 .queuecommand = scsifront_queuecommand, 630 .eh_abort_handler = scsifront_eh_abort_handler, 631 .eh_device_reset_handler = scsifront_dev_reset_handler, 632 .slave_configure = scsifront_sdev_configure, 633 .slave_destroy = scsifront_sdev_destroy, 634 .cmd_per_lun = VSCSIIF_DEFAULT_CMD_PER_LUN, 635 .can_queue = VSCSIIF_MAX_REQS, 636 .this_id = -1, 637 .cmd_size = sizeof(struct vscsifrnt_shadow), 638 .sg_tablesize = VSCSIIF_SG_TABLESIZE, 639 .use_clustering = DISABLE_CLUSTERING, 640 .proc_name = "scsifront", 641}; 642 643static int scsifront_alloc_ring(struct vscsifrnt_info *info) 644{ 645 struct xenbus_device *dev = info->dev; 646 struct vscsiif_sring *sring; 647 int err = -ENOMEM; 648 649 /***** Frontend to Backend ring start *****/ 650 sring = (struct vscsiif_sring *)__get_free_page(GFP_KERNEL); 651 if (!sring) { 652 xenbus_dev_fatal(dev, err, 653 "fail to allocate shared ring (Front to Back)"); 654 return err; 655 } 656 SHARED_RING_INIT(sring); 657 FRONT_RING_INIT(&info->ring, sring, PAGE_SIZE); 658 659 err = xenbus_grant_ring(dev, virt_to_mfn(sring)); 660 if (err < 0) { 661 free_page((unsigned long)sring); 662 xenbus_dev_fatal(dev, err, 663 "fail to grant shared ring (Front to Back)"); 664 return err; 665 } 666 info->ring_ref = err; 667 668 err = xenbus_alloc_evtchn(dev, &info->evtchn); 669 if (err) { 670 xenbus_dev_fatal(dev, err, "xenbus_alloc_evtchn"); 671 goto free_gnttab; 672 } 673 674 err = bind_evtchn_to_irq(info->evtchn); 675 if (err <= 0) { 676 xenbus_dev_fatal(dev, err, "bind_evtchn_to_irq"); 677 goto free_gnttab; 678 } 679 680 info->irq = err; 681 682 err = request_threaded_irq(info->irq, NULL, scsifront_irq_fn, 683 IRQF_ONESHOT, "scsifront", info); 684 if (err) { 685 xenbus_dev_fatal(dev, err, "request_threaded_irq"); 686 goto free_irq; 687 } 688 689 return 0; 690 691/* free resource */ 692free_irq: 693 unbind_from_irqhandler(info->irq, info); 694free_gnttab: 695 gnttab_end_foreign_access(info->ring_ref, 0, 696 (unsigned long)info->ring.sring); 697 698 return err; 699} 700 701static int scsifront_init_ring(struct vscsifrnt_info *info) 702{ 703 struct xenbus_device *dev = info->dev; 704 struct xenbus_transaction xbt; 705 int err; 706 707 pr_debug("%s\n", __func__); 708 709 err = scsifront_alloc_ring(info); 710 if (err) 711 return err; 712 pr_debug("%s: %u %u\n", __func__, info->ring_ref, info->evtchn); 713 714again: 715 err = xenbus_transaction_start(&xbt); 716 if (err) 717 xenbus_dev_fatal(dev, err, "starting transaction"); 718 719 err = xenbus_printf(xbt, dev->nodename, "ring-ref", "%u", 720 info->ring_ref); 721 if (err) { 722 xenbus_dev_fatal(dev, err, "%s", "writing ring-ref"); 723 goto fail; 724 } 725 726 err = xenbus_printf(xbt, dev->nodename, "event-channel", "%u", 727 info->evtchn); 728 729 if (err) { 730 xenbus_dev_fatal(dev, err, "%s", "writing event-channel"); 731 goto fail; 732 } 733 734 err = xenbus_transaction_end(xbt, 0); 735 if (err) { 736 if (err == -EAGAIN) 737 goto again; 738 xenbus_dev_fatal(dev, err, "completing transaction"); 739 goto free_sring; 740 } 741 742 return 0; 743 744fail: 745 xenbus_transaction_end(xbt, 1); 746free_sring: 747 unbind_from_irqhandler(info->irq, info); 748 gnttab_end_foreign_access(info->ring_ref, 0, 749 (unsigned long)info->ring.sring); 750 751 return err; 752} 753 754 755static int scsifront_probe(struct xenbus_device *dev, 756 const struct xenbus_device_id *id) 757{ 758 struct vscsifrnt_info *info; 759 struct Scsi_Host *host; 760 int err = -ENOMEM; 761 char name[TASK_COMM_LEN]; 762 763 host = scsi_host_alloc(&scsifront_sht, sizeof(*info)); 764 if (!host) { 765 xenbus_dev_fatal(dev, err, "fail to allocate scsi host"); 766 return err; 767 } 768 info = (struct vscsifrnt_info *)host->hostdata; 769 770 dev_set_drvdata(&dev->dev, info); 771 info->dev = dev; 772 773 bitmap_fill(info->shadow_free_bitmap, VSCSIIF_MAX_REQS); 774 775 err = scsifront_init_ring(info); 776 if (err) { 777 scsi_host_put(host); 778 return err; 779 } 780 781 init_waitqueue_head(&info->wq_sync); 782 spin_lock_init(&info->shadow_lock); 783 784 snprintf(name, TASK_COMM_LEN, "vscsiif.%d", host->host_no); 785 786 host->max_id = VSCSIIF_MAX_TARGET; 787 host->max_channel = 0; 788 host->max_lun = VSCSIIF_MAX_LUN; 789 host->max_sectors = (host->sg_tablesize - 1) * PAGE_SIZE / 512; 790 host->max_cmd_len = VSCSIIF_MAX_COMMAND_SIZE; 791 792 err = scsi_add_host(host, &dev->dev); 793 if (err) { 794 dev_err(&dev->dev, "fail to add scsi host %d\n", err); 795 goto free_sring; 796 } 797 info->host = host; 798 info->host_active = 1; 799 800 xenbus_switch_state(dev, XenbusStateInitialised); 801 802 return 0; 803 804free_sring: 805 unbind_from_irqhandler(info->irq, info); 806 gnttab_end_foreign_access(info->ring_ref, 0, 807 (unsigned long)info->ring.sring); 808 scsi_host_put(host); 809 return err; 810} 811 812static int scsifront_remove(struct xenbus_device *dev) 813{ 814 struct vscsifrnt_info *info = dev_get_drvdata(&dev->dev); 815 816 pr_debug("%s: %s removed\n", __func__, dev->nodename); 817 818 mutex_lock(&scsifront_mutex); 819 if (info->host_active) { 820 /* Scsi_host not yet removed */ 821 scsi_remove_host(info->host); 822 info->host_active = 0; 823 } 824 mutex_unlock(&scsifront_mutex); 825 826 gnttab_end_foreign_access(info->ring_ref, 0, 827 (unsigned long)info->ring.sring); 828 unbind_from_irqhandler(info->irq, info); 829 830 scsi_host_put(info->host); 831 832 return 0; 833} 834 835static void scsifront_disconnect(struct vscsifrnt_info *info) 836{ 837 struct xenbus_device *dev = info->dev; 838 struct Scsi_Host *host = info->host; 839 840 pr_debug("%s: %s disconnect\n", __func__, dev->nodename); 841 842 /* 843 * When this function is executed, all devices of 844 * Frontend have been deleted. 845 * Therefore, it need not block I/O before remove_host. 846 */ 847 848 mutex_lock(&scsifront_mutex); 849 if (info->host_active) { 850 scsi_remove_host(host); 851 info->host_active = 0; 852 } 853 mutex_unlock(&scsifront_mutex); 854 855 xenbus_frontend_closed(dev); 856} 857 858static void scsifront_do_lun_hotplug(struct vscsifrnt_info *info, int op) 859{ 860 struct xenbus_device *dev = info->dev; 861 int i, err = 0; 862 char str[64]; 863 char **dir; 864 unsigned int dir_n = 0; 865 unsigned int device_state; 866 unsigned int hst, chn, tgt, lun; 867 struct scsi_device *sdev; 868 869 dir = xenbus_directory(XBT_NIL, dev->otherend, "vscsi-devs", &dir_n); 870 if (IS_ERR(dir)) 871 return; 872 873 /* mark current task as the one allowed to modify device states */ 874 BUG_ON(info->curr); 875 info->curr = current; 876 877 for (i = 0; i < dir_n; i++) { 878 /* read status */ 879 snprintf(str, sizeof(str), "vscsi-devs/%s/state", dir[i]); 880 err = xenbus_scanf(XBT_NIL, dev->otherend, str, "%u", 881 &device_state); 882 if (XENBUS_EXIST_ERR(err)) 883 continue; 884 885 /* virtual SCSI device */ 886 snprintf(str, sizeof(str), "vscsi-devs/%s/v-dev", dir[i]); 887 err = xenbus_scanf(XBT_NIL, dev->otherend, str, 888 "%u:%u:%u:%u", &hst, &chn, &tgt, &lun); 889 if (XENBUS_EXIST_ERR(err)) 890 continue; 891 892 /* 893 * Front device state path, used in slave_configure called 894 * on successfull scsi_add_device, and in slave_destroy called 895 * on remove of a device. 896 */ 897 snprintf(info->dev_state_path, sizeof(info->dev_state_path), 898 "vscsi-devs/%s/state", dir[i]); 899 900 switch (op) { 901 case VSCSIFRONT_OP_ADD_LUN: 902 if (device_state != XenbusStateInitialised) 903 break; 904 905 if (scsi_add_device(info->host, chn, tgt, lun)) { 906 dev_err(&dev->dev, "scsi_add_device\n"); 907 xenbus_printf(XBT_NIL, dev->nodename, 908 info->dev_state_path, 909 "%d", XenbusStateClosed); 910 } 911 break; 912 case VSCSIFRONT_OP_DEL_LUN: 913 if (device_state != XenbusStateClosing) 914 break; 915 916 sdev = scsi_device_lookup(info->host, chn, tgt, lun); 917 if (sdev) { 918 scsi_remove_device(sdev); 919 scsi_device_put(sdev); 920 } 921 break; 922 default: 923 break; 924 } 925 } 926 927 info->curr = NULL; 928 929 kfree(dir); 930} 931 932static void scsifront_read_backend_params(struct xenbus_device *dev, 933 struct vscsifrnt_info *info) 934{ 935 unsigned int sg_grant; 936 int ret; 937 struct Scsi_Host *host = info->host; 938 939 ret = xenbus_scanf(XBT_NIL, dev->otherend, "feature-sg-grant", "%u", 940 &sg_grant); 941 if (ret == 1 && sg_grant) { 942 sg_grant = min_t(unsigned int, sg_grant, SG_ALL); 943 sg_grant = max_t(unsigned int, sg_grant, VSCSIIF_SG_TABLESIZE); 944 host->sg_tablesize = min_t(unsigned int, sg_grant, 945 VSCSIIF_SG_TABLESIZE * PAGE_SIZE / 946 sizeof(struct scsiif_request_segment)); 947 host->max_sectors = (host->sg_tablesize - 1) * PAGE_SIZE / 512; 948 } 949 dev_info(&dev->dev, "using up to %d SG entries\n", host->sg_tablesize); 950} 951 952static void scsifront_backend_changed(struct xenbus_device *dev, 953 enum xenbus_state backend_state) 954{ 955 struct vscsifrnt_info *info = dev_get_drvdata(&dev->dev); 956 957 pr_debug("%s: %p %u %u\n", __func__, dev, dev->state, backend_state); 958 959 switch (backend_state) { 960 case XenbusStateUnknown: 961 case XenbusStateInitialising: 962 case XenbusStateInitWait: 963 case XenbusStateInitialised: 964 break; 965 966 case XenbusStateConnected: 967 scsifront_read_backend_params(dev, info); 968 if (xenbus_read_driver_state(dev->nodename) == 969 XenbusStateInitialised) 970 scsifront_do_lun_hotplug(info, VSCSIFRONT_OP_ADD_LUN); 971 972 if (dev->state != XenbusStateConnected) 973 xenbus_switch_state(dev, XenbusStateConnected); 974 break; 975 976 case XenbusStateClosed: 977 if (dev->state == XenbusStateClosed) 978 break; 979 /* Missed the backend's Closing state -- fallthrough */ 980 case XenbusStateClosing: 981 scsifront_disconnect(info); 982 break; 983 984 case XenbusStateReconfiguring: 985 scsifront_do_lun_hotplug(info, VSCSIFRONT_OP_DEL_LUN); 986 xenbus_switch_state(dev, XenbusStateReconfiguring); 987 break; 988 989 case XenbusStateReconfigured: 990 scsifront_do_lun_hotplug(info, VSCSIFRONT_OP_ADD_LUN); 991 xenbus_switch_state(dev, XenbusStateConnected); 992 break; 993 } 994} 995 996static const struct xenbus_device_id scsifront_ids[] = { 997 { "vscsi" }, 998 { "" } 999}; 1000 1001static struct xenbus_driver scsifront_driver = { 1002 .ids = scsifront_ids, 1003 .probe = scsifront_probe, 1004 .remove = scsifront_remove, 1005 .otherend_changed = scsifront_backend_changed, 1006}; 1007 1008static int __init scsifront_init(void) 1009{ 1010 if (!xen_domain()) 1011 return -ENODEV; 1012 1013 return xenbus_register_frontend(&scsifront_driver); 1014} 1015module_init(scsifront_init); 1016 1017static void __exit scsifront_exit(void) 1018{ 1019 xenbus_unregister_driver(&scsifront_driver); 1020} 1021module_exit(scsifront_exit); 1022 1023MODULE_DESCRIPTION("Xen SCSI frontend driver"); 1024MODULE_LICENSE("GPL"); 1025MODULE_ALIAS("xen:vscsi"); 1026MODULE_AUTHOR("Juergen Gross <jgross@suse.com>"); 1027