1 /* 2 * Xen SCSI backend driver 3 * 4 * Copyright (c) 2008, FUJITSU Limited 5 * 6 * Based on the blkback driver code. 7 * Adaption to kernel taget core infrastructure taken from vhost/scsi.c 8 * 9 * This program is free software; you can redistribute it and/or 10 * modify it under the terms of the GNU General Public License version 2 11 * as published by the Free Software Foundation; or, when distributed 12 * separately from the Linux kernel or incorporated into other 13 * software packages, subject to the following license: 14 * 15 * Permission is hereby granted, free of charge, to any person obtaining a copy 16 * of this source file (the "Software"), to deal in the Software without 17 * restriction, including without limitation the rights to use, copy, modify, 18 * merge, publish, distribute, sublicense, and/or sell copies of the Software, 19 * and to permit persons to whom the Software is furnished to do so, subject to 20 * the following conditions: 21 * 22 * The above copyright notice and this permission notice shall be included in 23 * all copies or substantial portions of the Software. 24 * 25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 26 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 27 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 28 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 29 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 30 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 31 * IN THE SOFTWARE. 32 */ 33 34 #define pr_fmt(fmt) "xen-pvscsi: " fmt 35 36 #include <stdarg.h> 37 38 #include <linux/module.h> 39 #include <linux/utsname.h> 40 #include <linux/interrupt.h> 41 #include <linux/slab.h> 42 #include <linux/wait.h> 43 #include <linux/sched.h> 44 #include <linux/list.h> 45 #include <linux/gfp.h> 46 #include <linux/delay.h> 47 #include <linux/spinlock.h> 48 #include <linux/configfs.h> 49 50 #include <generated/utsrelease.h> 51 52 #include <scsi/scsi.h> 53 #include <scsi/scsi_dbg.h> 54 #include <scsi/scsi_eh.h> 55 #include <scsi/scsi_tcq.h> 56 57 #include <target/target_core_base.h> 58 #include <target/target_core_fabric.h> 59 #include <target/target_core_configfs.h> 60 #include <target/target_core_fabric_configfs.h> 61 62 #include <asm/hypervisor.h> 63 64 #include <xen/xen.h> 65 #include <xen/balloon.h> 66 #include <xen/events.h> 67 #include <xen/xenbus.h> 68 #include <xen/grant_table.h> 69 #include <xen/page.h> 70 71 #include <xen/interface/grant_table.h> 72 #include <xen/interface/io/vscsiif.h> 73 74 #define VSCSI_VERSION "v0.1" 75 #define VSCSI_NAMELEN 32 76 77 struct ids_tuple { 78 unsigned int hst; /* host */ 79 unsigned int chn; /* channel */ 80 unsigned int tgt; /* target */ 81 unsigned int lun; /* LUN */ 82 }; 83 84 struct v2p_entry { 85 struct ids_tuple v; /* translate from */ 86 struct scsiback_tpg *tpg; /* translate to */ 87 unsigned int lun; 88 struct kref kref; 89 struct list_head l; 90 }; 91 92 struct vscsibk_info { 93 struct xenbus_device *dev; 94 95 domid_t domid; 96 unsigned int irq; 97 98 struct vscsiif_back_ring ring; 99 int ring_error; 100 101 spinlock_t ring_lock; 102 atomic_t nr_unreplied_reqs; 103 104 spinlock_t v2p_lock; 105 struct list_head v2p_entry_lists; 106 107 wait_queue_head_t waiting_to_free; 108 }; 109 110 /* theoretical maximum of grants for one request */ 111 #define VSCSI_MAX_GRANTS (SG_ALL + VSCSIIF_SG_TABLESIZE) 112 113 /* 114 * VSCSI_GRANT_BATCH is the maximum number of grants to be processed in one 115 * call to map/unmap grants. Don't choose it too large, as there are arrays 116 * with VSCSI_GRANT_BATCH elements allocated on the stack. 117 */ 118 #define VSCSI_GRANT_BATCH 16 119 120 struct vscsibk_pend { 121 uint16_t rqid; 122 123 uint8_t cmnd[VSCSIIF_MAX_COMMAND_SIZE]; 124 uint8_t cmd_len; 125 126 uint8_t sc_data_direction; 127 uint16_t n_sg; /* real length of SG list */ 128 uint16_t n_grants; /* SG pages and potentially SG list */ 129 uint32_t data_len; 130 uint32_t result; 131 132 struct vscsibk_info *info; 133 struct v2p_entry *v2p; 134 struct scatterlist *sgl; 135 136 uint8_t sense_buffer[VSCSIIF_SENSE_BUFFERSIZE]; 137 138 grant_handle_t grant_handles[VSCSI_MAX_GRANTS]; 139 struct page *pages[VSCSI_MAX_GRANTS]; 140 141 struct se_cmd se_cmd; 142 }; 143 144 struct scsiback_tmr { 145 atomic_t tmr_complete; 146 wait_queue_head_t tmr_wait; 147 }; 148 149 struct scsiback_nexus { 150 /* Pointer to TCM session for I_T Nexus */ 151 struct se_session *tvn_se_sess; 152 }; 153 154 struct scsiback_tport { 155 /* SCSI protocol the tport is providing */ 156 u8 tport_proto_id; 157 /* Binary World Wide unique Port Name for pvscsi Target port */ 158 u64 tport_wwpn; 159 /* ASCII formatted WWPN for pvscsi Target port */ 160 char tport_name[VSCSI_NAMELEN]; 161 /* Returned by scsiback_make_tport() */ 162 struct se_wwn tport_wwn; 163 }; 164 165 struct scsiback_tpg { 166 /* scsiback port target portal group tag for TCM */ 167 u16 tport_tpgt; 168 /* track number of TPG Port/Lun Links wrt explicit I_T Nexus shutdown */ 169 int tv_tpg_port_count; 170 /* xen-pvscsi references to tpg_nexus, protected by tv_tpg_mutex */ 171 int tv_tpg_fe_count; 172 /* list for scsiback_list */ 173 struct list_head tv_tpg_list; 174 /* Used to protect access for tpg_nexus */ 175 struct mutex tv_tpg_mutex; 176 /* Pointer to the TCM pvscsi I_T Nexus for this TPG endpoint */ 177 struct scsiback_nexus *tpg_nexus; 178 /* Pointer back to scsiback_tport */ 179 struct scsiback_tport *tport; 180 /* Returned by scsiback_make_tpg() */ 181 struct se_portal_group se_tpg; 182 /* alias used in xenstore */ 183 char param_alias[VSCSI_NAMELEN]; 184 /* list of info structures related to this target portal group */ 185 struct list_head info_list; 186 }; 187 188 #define SCSIBACK_INVALID_HANDLE (~0) 189 190 static bool log_print_stat; 191 module_param(log_print_stat, bool, 0644); 192 193 static int scsiback_max_buffer_pages = 1024; 194 module_param_named(max_buffer_pages, scsiback_max_buffer_pages, int, 0644); 195 MODULE_PARM_DESC(max_buffer_pages, 196 "Maximum number of free pages to keep in backend buffer"); 197 198 static struct kmem_cache *scsiback_cachep; 199 static DEFINE_SPINLOCK(free_pages_lock); 200 static int free_pages_num; 201 static LIST_HEAD(scsiback_free_pages); 202 203 /* Global spinlock to protect scsiback TPG list */ 204 static DEFINE_MUTEX(scsiback_mutex); 205 static LIST_HEAD(scsiback_list); 206 207 /* Local pointer to allocated TCM configfs fabric module */ 208 static struct target_fabric_configfs *scsiback_fabric_configfs; 209 210 static void scsiback_get(struct vscsibk_info *info) 211 { 212 atomic_inc(&info->nr_unreplied_reqs); 213 } 214 215 static void scsiback_put(struct vscsibk_info *info) 216 { 217 if (atomic_dec_and_test(&info->nr_unreplied_reqs)) 218 wake_up(&info->waiting_to_free); 219 } 220 221 static void put_free_pages(struct page **page, int num) 222 { 223 unsigned long flags; 224 int i = free_pages_num + num, n = num; 225 226 if (num == 0) 227 return; 228 if (i > scsiback_max_buffer_pages) { 229 n = min(num, i - scsiback_max_buffer_pages); 230 gnttab_free_pages(n, page + num - n); 231 n = num - n; 232 } 233 spin_lock_irqsave(&free_pages_lock, flags); 234 for (i = 0; i < n; i++) 235 list_add(&page[i]->lru, &scsiback_free_pages); 236 free_pages_num += n; 237 spin_unlock_irqrestore(&free_pages_lock, flags); 238 } 239 240 static int get_free_page(struct page **page) 241 { 242 unsigned long flags; 243 244 spin_lock_irqsave(&free_pages_lock, flags); 245 if (list_empty(&scsiback_free_pages)) { 246 spin_unlock_irqrestore(&free_pages_lock, flags); 247 return gnttab_alloc_pages(1, page); 248 } 249 page[0] = list_first_entry(&scsiback_free_pages, struct page, lru); 250 list_del(&page[0]->lru); 251 free_pages_num--; 252 spin_unlock_irqrestore(&free_pages_lock, flags); 253 return 0; 254 } 255 256 static unsigned long vaddr_page(struct page *page) 257 { 258 unsigned long pfn = page_to_pfn(page); 259 260 return (unsigned long)pfn_to_kaddr(pfn); 261 } 262 263 static unsigned long vaddr(struct vscsibk_pend *req, int seg) 264 { 265 return vaddr_page(req->pages[seg]); 266 } 267 268 static void scsiback_print_status(char *sense_buffer, int errors, 269 struct vscsibk_pend *pending_req) 270 { 271 struct scsiback_tpg *tpg = pending_req->v2p->tpg; 272 273 pr_err("[%s:%d] cmnd[0]=%02x -> st=%02x msg=%02x host=%02x drv=%02x\n", 274 tpg->tport->tport_name, pending_req->v2p->lun, 275 pending_req->cmnd[0], status_byte(errors), msg_byte(errors), 276 host_byte(errors), driver_byte(errors)); 277 } 278 279 static void scsiback_fast_flush_area(struct vscsibk_pend *req) 280 { 281 struct gnttab_unmap_grant_ref unmap[VSCSI_GRANT_BATCH]; 282 struct page *pages[VSCSI_GRANT_BATCH]; 283 unsigned int i, invcount = 0; 284 grant_handle_t handle; 285 int err; 286 287 kfree(req->sgl); 288 req->sgl = NULL; 289 req->n_sg = 0; 290 291 if (!req->n_grants) 292 return; 293 294 for (i = 0; i < req->n_grants; i++) { 295 handle = req->grant_handles[i]; 296 if (handle == SCSIBACK_INVALID_HANDLE) 297 continue; 298 gnttab_set_unmap_op(&unmap[invcount], vaddr(req, i), 299 GNTMAP_host_map, handle); 300 req->grant_handles[i] = SCSIBACK_INVALID_HANDLE; 301 pages[invcount] = req->pages[i]; 302 put_page(pages[invcount]); 303 invcount++; 304 if (invcount < VSCSI_GRANT_BATCH) 305 continue; 306 err = gnttab_unmap_refs(unmap, NULL, pages, invcount); 307 BUG_ON(err); 308 invcount = 0; 309 } 310 311 if (invcount) { 312 err = gnttab_unmap_refs(unmap, NULL, pages, invcount); 313 BUG_ON(err); 314 } 315 316 put_free_pages(req->pages, req->n_grants); 317 req->n_grants = 0; 318 } 319 320 static void scsiback_free_translation_entry(struct kref *kref) 321 { 322 struct v2p_entry *entry = container_of(kref, struct v2p_entry, kref); 323 struct scsiback_tpg *tpg = entry->tpg; 324 325 mutex_lock(&tpg->tv_tpg_mutex); 326 tpg->tv_tpg_fe_count--; 327 mutex_unlock(&tpg->tv_tpg_mutex); 328 329 kfree(entry); 330 } 331 332 static void scsiback_do_resp_with_sense(char *sense_buffer, int32_t result, 333 uint32_t resid, struct vscsibk_pend *pending_req) 334 { 335 struct vscsiif_response *ring_res; 336 struct vscsibk_info *info = pending_req->info; 337 int notify; 338 struct scsi_sense_hdr sshdr; 339 unsigned long flags; 340 unsigned len; 341 342 spin_lock_irqsave(&info->ring_lock, flags); 343 344 ring_res = RING_GET_RESPONSE(&info->ring, info->ring.rsp_prod_pvt); 345 info->ring.rsp_prod_pvt++; 346 347 ring_res->rslt = result; 348 ring_res->rqid = pending_req->rqid; 349 350 if (sense_buffer != NULL && 351 scsi_normalize_sense(sense_buffer, VSCSIIF_SENSE_BUFFERSIZE, 352 &sshdr)) { 353 len = min_t(unsigned, 8 + sense_buffer[7], 354 VSCSIIF_SENSE_BUFFERSIZE); 355 memcpy(ring_res->sense_buffer, sense_buffer, len); 356 ring_res->sense_len = len; 357 } else { 358 ring_res->sense_len = 0; 359 } 360 361 ring_res->residual_len = resid; 362 363 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&info->ring, notify); 364 spin_unlock_irqrestore(&info->ring_lock, flags); 365 366 if (notify) 367 notify_remote_via_irq(info->irq); 368 369 if (pending_req->v2p) 370 kref_put(&pending_req->v2p->kref, 371 scsiback_free_translation_entry); 372 } 373 374 static void scsiback_cmd_done(struct vscsibk_pend *pending_req) 375 { 376 struct vscsibk_info *info = pending_req->info; 377 unsigned char *sense_buffer; 378 unsigned int resid; 379 int errors; 380 381 sense_buffer = pending_req->sense_buffer; 382 resid = pending_req->se_cmd.residual_count; 383 errors = pending_req->result; 384 385 if (errors && log_print_stat) 386 scsiback_print_status(sense_buffer, errors, pending_req); 387 388 scsiback_fast_flush_area(pending_req); 389 scsiback_do_resp_with_sense(sense_buffer, errors, resid, pending_req); 390 scsiback_put(info); 391 } 392 393 static void scsiback_cmd_exec(struct vscsibk_pend *pending_req) 394 { 395 struct se_cmd *se_cmd = &pending_req->se_cmd; 396 struct se_session *sess = pending_req->v2p->tpg->tpg_nexus->tvn_se_sess; 397 int rc; 398 399 memset(pending_req->sense_buffer, 0, VSCSIIF_SENSE_BUFFERSIZE); 400 401 memset(se_cmd, 0, sizeof(*se_cmd)); 402 403 scsiback_get(pending_req->info); 404 rc = target_submit_cmd_map_sgls(se_cmd, sess, pending_req->cmnd, 405 pending_req->sense_buffer, pending_req->v2p->lun, 406 pending_req->data_len, 0, 407 pending_req->sc_data_direction, 0, 408 pending_req->sgl, pending_req->n_sg, 409 NULL, 0, NULL, 0); 410 if (rc < 0) { 411 transport_send_check_condition_and_sense(se_cmd, 412 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE, 0); 413 transport_generic_free_cmd(se_cmd, 0); 414 } 415 } 416 417 static int scsiback_gnttab_data_map_batch(struct gnttab_map_grant_ref *map, 418 struct page **pg, grant_handle_t *grant, int cnt) 419 { 420 int err, i; 421 422 if (!cnt) 423 return 0; 424 425 err = gnttab_map_refs(map, NULL, pg, cnt); 426 BUG_ON(err); 427 for (i = 0; i < cnt; i++) { 428 if (unlikely(map[i].status != GNTST_okay)) { 429 pr_err("invalid buffer -- could not remap it\n"); 430 map[i].handle = SCSIBACK_INVALID_HANDLE; 431 err = -ENOMEM; 432 } else { 433 get_page(pg[i]); 434 } 435 grant[i] = map[i].handle; 436 } 437 return err; 438 } 439 440 static int scsiback_gnttab_data_map_list(struct vscsibk_pend *pending_req, 441 struct scsiif_request_segment *seg, struct page **pg, 442 grant_handle_t *grant, int cnt, u32 flags) 443 { 444 int mapcount = 0, i, err = 0; 445 struct gnttab_map_grant_ref map[VSCSI_GRANT_BATCH]; 446 struct vscsibk_info *info = pending_req->info; 447 448 for (i = 0; i < cnt; i++) { 449 if (get_free_page(pg + mapcount)) { 450 put_free_pages(pg, mapcount); 451 pr_err("no grant page\n"); 452 return -ENOMEM; 453 } 454 gnttab_set_map_op(&map[mapcount], vaddr_page(pg[mapcount]), 455 flags, seg[i].gref, info->domid); 456 mapcount++; 457 if (mapcount < VSCSI_GRANT_BATCH) 458 continue; 459 err = scsiback_gnttab_data_map_batch(map, pg, grant, mapcount); 460 pg += mapcount; 461 grant += mapcount; 462 pending_req->n_grants += mapcount; 463 if (err) 464 return err; 465 mapcount = 0; 466 } 467 err = scsiback_gnttab_data_map_batch(map, pg, grant, mapcount); 468 pending_req->n_grants += mapcount; 469 return err; 470 } 471 472 static int scsiback_gnttab_data_map(struct vscsiif_request *ring_req, 473 struct vscsibk_pend *pending_req) 474 { 475 u32 flags; 476 int i, err, n_segs, i_seg = 0; 477 struct page **pg; 478 struct scsiif_request_segment *seg; 479 unsigned long end_seg = 0; 480 unsigned int nr_segments = (unsigned int)ring_req->nr_segments; 481 unsigned int nr_sgl = 0; 482 struct scatterlist *sg; 483 grant_handle_t *grant; 484 485 pending_req->n_sg = 0; 486 pending_req->n_grants = 0; 487 pending_req->data_len = 0; 488 489 nr_segments &= ~VSCSIIF_SG_GRANT; 490 if (!nr_segments) 491 return 0; 492 493 if (nr_segments > VSCSIIF_SG_TABLESIZE) { 494 pr_debug("invalid parameter nr_seg = %d\n", 495 ring_req->nr_segments); 496 return -EINVAL; 497 } 498 499 if (ring_req->nr_segments & VSCSIIF_SG_GRANT) { 500 err = scsiback_gnttab_data_map_list(pending_req, ring_req->seg, 501 pending_req->pages, pending_req->grant_handles, 502 nr_segments, GNTMAP_host_map | GNTMAP_readonly); 503 if (err) 504 return err; 505 nr_sgl = nr_segments; 506 nr_segments = 0; 507 for (i = 0; i < nr_sgl; i++) { 508 n_segs = ring_req->seg[i].length / 509 sizeof(struct scsiif_request_segment); 510 if ((unsigned)ring_req->seg[i].offset + 511 (unsigned)ring_req->seg[i].length > PAGE_SIZE || 512 n_segs * sizeof(struct scsiif_request_segment) != 513 ring_req->seg[i].length) 514 return -EINVAL; 515 nr_segments += n_segs; 516 } 517 if (nr_segments > SG_ALL) { 518 pr_debug("invalid nr_seg = %d\n", nr_segments); 519 return -EINVAL; 520 } 521 } 522 523 /* free of (sgl) in fast_flush_area() */ 524 pending_req->sgl = kmalloc_array(nr_segments, 525 sizeof(struct scatterlist), GFP_KERNEL); 526 if (!pending_req->sgl) 527 return -ENOMEM; 528 529 sg_init_table(pending_req->sgl, nr_segments); 530 pending_req->n_sg = nr_segments; 531 532 flags = GNTMAP_host_map; 533 if (pending_req->sc_data_direction == DMA_TO_DEVICE) 534 flags |= GNTMAP_readonly; 535 536 pg = pending_req->pages + nr_sgl; 537 grant = pending_req->grant_handles + nr_sgl; 538 if (!nr_sgl) { 539 seg = ring_req->seg; 540 err = scsiback_gnttab_data_map_list(pending_req, seg, 541 pg, grant, nr_segments, flags); 542 if (err) 543 return err; 544 } else { 545 for (i = 0; i < nr_sgl; i++) { 546 seg = (struct scsiif_request_segment *)( 547 vaddr(pending_req, i) + ring_req->seg[i].offset); 548 n_segs = ring_req->seg[i].length / 549 sizeof(struct scsiif_request_segment); 550 err = scsiback_gnttab_data_map_list(pending_req, seg, 551 pg, grant, n_segs, flags); 552 if (err) 553 return err; 554 pg += n_segs; 555 grant += n_segs; 556 } 557 end_seg = vaddr(pending_req, 0) + ring_req->seg[0].offset; 558 seg = (struct scsiif_request_segment *)end_seg; 559 end_seg += ring_req->seg[0].length; 560 pg = pending_req->pages + nr_sgl; 561 } 562 563 for_each_sg(pending_req->sgl, sg, nr_segments, i) { 564 sg_set_page(sg, pg[i], seg->length, seg->offset); 565 pending_req->data_len += seg->length; 566 seg++; 567 if (nr_sgl && (unsigned long)seg >= end_seg) { 568 i_seg++; 569 end_seg = vaddr(pending_req, i_seg) + 570 ring_req->seg[i_seg].offset; 571 seg = (struct scsiif_request_segment *)end_seg; 572 end_seg += ring_req->seg[i_seg].length; 573 } 574 if (sg->offset >= PAGE_SIZE || 575 sg->length > PAGE_SIZE || 576 sg->offset + sg->length > PAGE_SIZE) 577 return -EINVAL; 578 } 579 580 return 0; 581 } 582 583 static void scsiback_disconnect(struct vscsibk_info *info) 584 { 585 wait_event(info->waiting_to_free, 586 atomic_read(&info->nr_unreplied_reqs) == 0); 587 588 unbind_from_irqhandler(info->irq, info); 589 info->irq = 0; 590 xenbus_unmap_ring_vfree(info->dev, info->ring.sring); 591 } 592 593 static void scsiback_device_action(struct vscsibk_pend *pending_req, 594 enum tcm_tmreq_table act, int tag) 595 { 596 int rc, err = FAILED; 597 struct scsiback_tpg *tpg = pending_req->v2p->tpg; 598 struct se_cmd *se_cmd = &pending_req->se_cmd; 599 struct scsiback_tmr *tmr; 600 601 tmr = kzalloc(sizeof(struct scsiback_tmr), GFP_KERNEL); 602 if (!tmr) 603 goto out; 604 605 init_waitqueue_head(&tmr->tmr_wait); 606 607 transport_init_se_cmd(se_cmd, tpg->se_tpg.se_tpg_tfo, 608 tpg->tpg_nexus->tvn_se_sess, 0, DMA_NONE, TCM_SIMPLE_TAG, 609 &pending_req->sense_buffer[0]); 610 611 rc = core_tmr_alloc_req(se_cmd, tmr, act, GFP_KERNEL); 612 if (rc < 0) 613 goto out; 614 615 se_cmd->se_tmr_req->ref_task_tag = tag; 616 617 if (transport_lookup_tmr_lun(se_cmd, pending_req->v2p->lun) < 0) 618 goto out; 619 620 transport_generic_handle_tmr(se_cmd); 621 wait_event(tmr->tmr_wait, atomic_read(&tmr->tmr_complete)); 622 623 err = (se_cmd->se_tmr_req->response == TMR_FUNCTION_COMPLETE) ? 624 SUCCESS : FAILED; 625 626 out: 627 if (tmr) { 628 transport_generic_free_cmd(&pending_req->se_cmd, 1); 629 kfree(tmr); 630 } 631 632 scsiback_do_resp_with_sense(NULL, err, 0, pending_req); 633 634 kmem_cache_free(scsiback_cachep, pending_req); 635 } 636 637 /* 638 Perform virtual to physical translation 639 */ 640 static struct v2p_entry *scsiback_do_translation(struct vscsibk_info *info, 641 struct ids_tuple *v) 642 { 643 struct v2p_entry *entry; 644 struct list_head *head = &(info->v2p_entry_lists); 645 unsigned long flags; 646 647 spin_lock_irqsave(&info->v2p_lock, flags); 648 list_for_each_entry(entry, head, l) { 649 if ((entry->v.chn == v->chn) && 650 (entry->v.tgt == v->tgt) && 651 (entry->v.lun == v->lun)) { 652 kref_get(&entry->kref); 653 goto out; 654 } 655 } 656 entry = NULL; 657 658 out: 659 spin_unlock_irqrestore(&info->v2p_lock, flags); 660 return entry; 661 } 662 663 static int prepare_pending_reqs(struct vscsibk_info *info, 664 struct vscsiif_request *ring_req, 665 struct vscsibk_pend *pending_req) 666 { 667 struct v2p_entry *v2p; 668 struct ids_tuple vir; 669 670 pending_req->rqid = ring_req->rqid; 671 pending_req->info = info; 672 673 vir.chn = ring_req->channel; 674 vir.tgt = ring_req->id; 675 vir.lun = ring_req->lun; 676 677 v2p = scsiback_do_translation(info, &vir); 678 if (!v2p) { 679 pending_req->v2p = NULL; 680 pr_debug("the v2p of (chn:%d, tgt:%d, lun:%d) doesn't exist.\n", 681 vir.chn, vir.tgt, vir.lun); 682 return -ENODEV; 683 } 684 pending_req->v2p = v2p; 685 686 /* request range check from frontend */ 687 pending_req->sc_data_direction = ring_req->sc_data_direction; 688 if ((pending_req->sc_data_direction != DMA_BIDIRECTIONAL) && 689 (pending_req->sc_data_direction != DMA_TO_DEVICE) && 690 (pending_req->sc_data_direction != DMA_FROM_DEVICE) && 691 (pending_req->sc_data_direction != DMA_NONE)) { 692 pr_debug("invalid parameter data_dir = %d\n", 693 pending_req->sc_data_direction); 694 return -EINVAL; 695 } 696 697 pending_req->cmd_len = ring_req->cmd_len; 698 if (pending_req->cmd_len > VSCSIIF_MAX_COMMAND_SIZE) { 699 pr_debug("invalid parameter cmd_len = %d\n", 700 pending_req->cmd_len); 701 return -EINVAL; 702 } 703 memcpy(pending_req->cmnd, ring_req->cmnd, pending_req->cmd_len); 704 705 return 0; 706 } 707 708 static int scsiback_do_cmd_fn(struct vscsibk_info *info) 709 { 710 struct vscsiif_back_ring *ring = &info->ring; 711 struct vscsiif_request ring_req; 712 struct vscsibk_pend *pending_req; 713 RING_IDX rc, rp; 714 int err, more_to_do; 715 uint32_t result; 716 717 rc = ring->req_cons; 718 rp = ring->sring->req_prod; 719 rmb(); /* guest system is accessing ring, too */ 720 721 if (RING_REQUEST_PROD_OVERFLOW(ring, rp)) { 722 rc = ring->rsp_prod_pvt; 723 pr_warn("Dom%d provided bogus ring requests (%#x - %#x = %u). Halting ring processing\n", 724 info->domid, rp, rc, rp - rc); 725 info->ring_error = 1; 726 return 0; 727 } 728 729 while ((rc != rp)) { 730 if (RING_REQUEST_CONS_OVERFLOW(ring, rc)) 731 break; 732 pending_req = kmem_cache_alloc(scsiback_cachep, GFP_KERNEL); 733 if (!pending_req) 734 return 1; 735 736 ring_req = *RING_GET_REQUEST(ring, rc); 737 ring->req_cons = ++rc; 738 739 err = prepare_pending_reqs(info, &ring_req, pending_req); 740 if (err) { 741 switch (err) { 742 case -ENODEV: 743 result = DID_NO_CONNECT; 744 break; 745 default: 746 result = DRIVER_ERROR; 747 break; 748 } 749 scsiback_do_resp_with_sense(NULL, result << 24, 0, 750 pending_req); 751 kmem_cache_free(scsiback_cachep, pending_req); 752 return 1; 753 } 754 755 switch (ring_req.act) { 756 case VSCSIIF_ACT_SCSI_CDB: 757 if (scsiback_gnttab_data_map(&ring_req, pending_req)) { 758 scsiback_fast_flush_area(pending_req); 759 scsiback_do_resp_with_sense(NULL, 760 DRIVER_ERROR << 24, 0, pending_req); 761 kmem_cache_free(scsiback_cachep, pending_req); 762 } else { 763 scsiback_cmd_exec(pending_req); 764 } 765 break; 766 case VSCSIIF_ACT_SCSI_ABORT: 767 scsiback_device_action(pending_req, TMR_ABORT_TASK, 768 ring_req.ref_rqid); 769 break; 770 case VSCSIIF_ACT_SCSI_RESET: 771 scsiback_device_action(pending_req, TMR_LUN_RESET, 0); 772 break; 773 default: 774 pr_err_ratelimited("invalid request\n"); 775 scsiback_do_resp_with_sense(NULL, DRIVER_ERROR << 24, 776 0, pending_req); 777 kmem_cache_free(scsiback_cachep, pending_req); 778 break; 779 } 780 781 /* Yield point for this unbounded loop. */ 782 cond_resched(); 783 } 784 785 RING_FINAL_CHECK_FOR_REQUESTS(&info->ring, more_to_do); 786 return more_to_do; 787 } 788 789 static irqreturn_t scsiback_irq_fn(int irq, void *dev_id) 790 { 791 struct vscsibk_info *info = dev_id; 792 793 if (info->ring_error) 794 return IRQ_HANDLED; 795 796 while (scsiback_do_cmd_fn(info)) 797 cond_resched(); 798 799 return IRQ_HANDLED; 800 } 801 802 static int scsiback_init_sring(struct vscsibk_info *info, grant_ref_t ring_ref, 803 evtchn_port_t evtchn) 804 { 805 void *area; 806 struct vscsiif_sring *sring; 807 int err; 808 809 if (info->irq) 810 return -1; 811 812 err = xenbus_map_ring_valloc(info->dev, &ring_ref, 1, &area); 813 if (err) 814 return err; 815 816 sring = (struct vscsiif_sring *)area; 817 BACK_RING_INIT(&info->ring, sring, PAGE_SIZE); 818 819 err = bind_interdomain_evtchn_to_irq(info->domid, evtchn); 820 if (err < 0) 821 goto unmap_page; 822 823 info->irq = err; 824 825 err = request_threaded_irq(info->irq, NULL, scsiback_irq_fn, 826 IRQF_ONESHOT, "vscsiif-backend", info); 827 if (err) 828 goto free_irq; 829 830 return 0; 831 832 free_irq: 833 unbind_from_irqhandler(info->irq, info); 834 info->irq = 0; 835 unmap_page: 836 xenbus_unmap_ring_vfree(info->dev, area); 837 838 return err; 839 } 840 841 static int scsiback_map(struct vscsibk_info *info) 842 { 843 struct xenbus_device *dev = info->dev; 844 unsigned int ring_ref, evtchn; 845 int err; 846 847 err = xenbus_gather(XBT_NIL, dev->otherend, 848 "ring-ref", "%u", &ring_ref, 849 "event-channel", "%u", &evtchn, NULL); 850 if (err) { 851 xenbus_dev_fatal(dev, err, "reading %s ring", dev->otherend); 852 return err; 853 } 854 855 return scsiback_init_sring(info, ring_ref, evtchn); 856 } 857 858 /* 859 Add a new translation entry 860 */ 861 static int scsiback_add_translation_entry(struct vscsibk_info *info, 862 char *phy, struct ids_tuple *v) 863 { 864 int err = 0; 865 struct v2p_entry *entry; 866 struct v2p_entry *new; 867 struct list_head *head = &(info->v2p_entry_lists); 868 unsigned long flags; 869 char *lunp; 870 unsigned int lun; 871 struct scsiback_tpg *tpg_entry, *tpg = NULL; 872 char *error = "doesn't exist"; 873 874 lunp = strrchr(phy, ':'); 875 if (!lunp) { 876 pr_err("illegal format of physical device %s\n", phy); 877 return -EINVAL; 878 } 879 *lunp = 0; 880 lunp++; 881 if (kstrtouint(lunp, 10, &lun) || lun >= TRANSPORT_MAX_LUNS_PER_TPG) { 882 pr_err("lun number not valid: %s\n", lunp); 883 return -EINVAL; 884 } 885 886 mutex_lock(&scsiback_mutex); 887 list_for_each_entry(tpg_entry, &scsiback_list, tv_tpg_list) { 888 if (!strcmp(phy, tpg_entry->tport->tport_name) || 889 !strcmp(phy, tpg_entry->param_alias)) { 890 spin_lock(&tpg_entry->se_tpg.tpg_lun_lock); 891 if (tpg_entry->se_tpg.tpg_lun_list[lun]->lun_status == 892 TRANSPORT_LUN_STATUS_ACTIVE) { 893 if (!tpg_entry->tpg_nexus) 894 error = "nexus undefined"; 895 else 896 tpg = tpg_entry; 897 } 898 spin_unlock(&tpg_entry->se_tpg.tpg_lun_lock); 899 break; 900 } 901 } 902 if (tpg) { 903 mutex_lock(&tpg->tv_tpg_mutex); 904 tpg->tv_tpg_fe_count++; 905 mutex_unlock(&tpg->tv_tpg_mutex); 906 } 907 mutex_unlock(&scsiback_mutex); 908 909 if (!tpg) { 910 pr_err("%s:%d %s\n", phy, lun, error); 911 return -ENODEV; 912 } 913 914 new = kmalloc(sizeof(struct v2p_entry), GFP_KERNEL); 915 if (new == NULL) { 916 err = -ENOMEM; 917 goto out_free; 918 } 919 920 spin_lock_irqsave(&info->v2p_lock, flags); 921 922 /* Check double assignment to identical virtual ID */ 923 list_for_each_entry(entry, head, l) { 924 if ((entry->v.chn == v->chn) && 925 (entry->v.tgt == v->tgt) && 926 (entry->v.lun == v->lun)) { 927 pr_warn("Virtual ID is already used. Assignment was not performed.\n"); 928 err = -EEXIST; 929 goto out; 930 } 931 932 } 933 934 /* Create a new translation entry and add to the list */ 935 kref_init(&new->kref); 936 new->v = *v; 937 new->tpg = tpg; 938 new->lun = lun; 939 list_add_tail(&new->l, head); 940 941 out: 942 spin_unlock_irqrestore(&info->v2p_lock, flags); 943 944 out_free: 945 mutex_lock(&tpg->tv_tpg_mutex); 946 tpg->tv_tpg_fe_count--; 947 mutex_unlock(&tpg->tv_tpg_mutex); 948 949 if (err) 950 kfree(new); 951 952 return err; 953 } 954 955 static void __scsiback_del_translation_entry(struct v2p_entry *entry) 956 { 957 list_del(&entry->l); 958 kref_put(&entry->kref, scsiback_free_translation_entry); 959 } 960 961 /* 962 Delete the translation entry specfied 963 */ 964 static int scsiback_del_translation_entry(struct vscsibk_info *info, 965 struct ids_tuple *v) 966 { 967 struct v2p_entry *entry; 968 struct list_head *head = &(info->v2p_entry_lists); 969 unsigned long flags; 970 971 spin_lock_irqsave(&info->v2p_lock, flags); 972 /* Find out the translation entry specified */ 973 list_for_each_entry(entry, head, l) { 974 if ((entry->v.chn == v->chn) && 975 (entry->v.tgt == v->tgt) && 976 (entry->v.lun == v->lun)) { 977 goto found; 978 } 979 } 980 981 spin_unlock_irqrestore(&info->v2p_lock, flags); 982 return 1; 983 984 found: 985 /* Delete the translation entry specfied */ 986 __scsiback_del_translation_entry(entry); 987 988 spin_unlock_irqrestore(&info->v2p_lock, flags); 989 return 0; 990 } 991 992 static void scsiback_do_add_lun(struct vscsibk_info *info, const char *state, 993 char *phy, struct ids_tuple *vir, int try) 994 { 995 if (!scsiback_add_translation_entry(info, phy, vir)) { 996 if (xenbus_printf(XBT_NIL, info->dev->nodename, state, 997 "%d", XenbusStateInitialised)) { 998 pr_err("xenbus_printf error %s\n", state); 999 scsiback_del_translation_entry(info, vir); 1000 } 1001 } else if (!try) { 1002 xenbus_printf(XBT_NIL, info->dev->nodename, state, 1003 "%d", XenbusStateClosed); 1004 } 1005 } 1006 1007 static void scsiback_do_del_lun(struct vscsibk_info *info, const char *state, 1008 struct ids_tuple *vir) 1009 { 1010 if (!scsiback_del_translation_entry(info, vir)) { 1011 if (xenbus_printf(XBT_NIL, info->dev->nodename, state, 1012 "%d", XenbusStateClosed)) 1013 pr_err("xenbus_printf error %s\n", state); 1014 } 1015 } 1016 1017 #define VSCSIBACK_OP_ADD_OR_DEL_LUN 1 1018 #define VSCSIBACK_OP_UPDATEDEV_STATE 2 1019 1020 static void scsiback_do_1lun_hotplug(struct vscsibk_info *info, int op, 1021 char *ent) 1022 { 1023 int err; 1024 struct ids_tuple vir; 1025 char *val; 1026 int device_state; 1027 char phy[VSCSI_NAMELEN]; 1028 char str[64]; 1029 char state[64]; 1030 struct xenbus_device *dev = info->dev; 1031 1032 /* read status */ 1033 snprintf(state, sizeof(state), "vscsi-devs/%s/state", ent); 1034 err = xenbus_scanf(XBT_NIL, dev->nodename, state, "%u", &device_state); 1035 if (XENBUS_EXIST_ERR(err)) 1036 return; 1037 1038 /* physical SCSI device */ 1039 snprintf(str, sizeof(str), "vscsi-devs/%s/p-dev", ent); 1040 val = xenbus_read(XBT_NIL, dev->nodename, str, NULL); 1041 if (IS_ERR(val)) { 1042 xenbus_printf(XBT_NIL, dev->nodename, state, 1043 "%d", XenbusStateClosed); 1044 return; 1045 } 1046 strlcpy(phy, val, VSCSI_NAMELEN); 1047 kfree(val); 1048 1049 /* virtual SCSI device */ 1050 snprintf(str, sizeof(str), "vscsi-devs/%s/v-dev", ent); 1051 err = xenbus_scanf(XBT_NIL, dev->nodename, str, "%u:%u:%u:%u", 1052 &vir.hst, &vir.chn, &vir.tgt, &vir.lun); 1053 if (XENBUS_EXIST_ERR(err)) { 1054 xenbus_printf(XBT_NIL, dev->nodename, state, 1055 "%d", XenbusStateClosed); 1056 return; 1057 } 1058 1059 switch (op) { 1060 case VSCSIBACK_OP_ADD_OR_DEL_LUN: 1061 switch (device_state) { 1062 case XenbusStateInitialising: 1063 scsiback_do_add_lun(info, state, phy, &vir, 0); 1064 break; 1065 case XenbusStateConnected: 1066 scsiback_do_add_lun(info, state, phy, &vir, 1); 1067 break; 1068 case XenbusStateClosing: 1069 scsiback_do_del_lun(info, state, &vir); 1070 break; 1071 default: 1072 break; 1073 } 1074 break; 1075 1076 case VSCSIBACK_OP_UPDATEDEV_STATE: 1077 if (device_state == XenbusStateInitialised) { 1078 /* modify vscsi-devs/dev-x/state */ 1079 if (xenbus_printf(XBT_NIL, dev->nodename, state, 1080 "%d", XenbusStateConnected)) { 1081 pr_err("xenbus_printf error %s\n", str); 1082 scsiback_del_translation_entry(info, &vir); 1083 xenbus_printf(XBT_NIL, dev->nodename, state, 1084 "%d", XenbusStateClosed); 1085 } 1086 } 1087 break; 1088 /* When it is necessary, processing is added here. */ 1089 default: 1090 break; 1091 } 1092 } 1093 1094 static void scsiback_do_lun_hotplug(struct vscsibk_info *info, int op) 1095 { 1096 int i; 1097 char **dir; 1098 unsigned int ndir = 0; 1099 1100 dir = xenbus_directory(XBT_NIL, info->dev->nodename, "vscsi-devs", 1101 &ndir); 1102 if (IS_ERR(dir)) 1103 return; 1104 1105 for (i = 0; i < ndir; i++) 1106 scsiback_do_1lun_hotplug(info, op, dir[i]); 1107 1108 kfree(dir); 1109 } 1110 1111 static void scsiback_frontend_changed(struct xenbus_device *dev, 1112 enum xenbus_state frontend_state) 1113 { 1114 struct vscsibk_info *info = dev_get_drvdata(&dev->dev); 1115 1116 switch (frontend_state) { 1117 case XenbusStateInitialising: 1118 break; 1119 1120 case XenbusStateInitialised: 1121 if (scsiback_map(info)) 1122 break; 1123 1124 scsiback_do_lun_hotplug(info, VSCSIBACK_OP_ADD_OR_DEL_LUN); 1125 xenbus_switch_state(dev, XenbusStateConnected); 1126 break; 1127 1128 case XenbusStateConnected: 1129 scsiback_do_lun_hotplug(info, VSCSIBACK_OP_UPDATEDEV_STATE); 1130 1131 if (dev->state == XenbusStateConnected) 1132 break; 1133 1134 xenbus_switch_state(dev, XenbusStateConnected); 1135 break; 1136 1137 case XenbusStateClosing: 1138 if (info->irq) 1139 scsiback_disconnect(info); 1140 1141 xenbus_switch_state(dev, XenbusStateClosing); 1142 break; 1143 1144 case XenbusStateClosed: 1145 xenbus_switch_state(dev, XenbusStateClosed); 1146 if (xenbus_dev_is_online(dev)) 1147 break; 1148 /* fall through if not online */ 1149 case XenbusStateUnknown: 1150 device_unregister(&dev->dev); 1151 break; 1152 1153 case XenbusStateReconfiguring: 1154 scsiback_do_lun_hotplug(info, VSCSIBACK_OP_ADD_OR_DEL_LUN); 1155 xenbus_switch_state(dev, XenbusStateReconfigured); 1156 1157 break; 1158 1159 default: 1160 xenbus_dev_fatal(dev, -EINVAL, "saw state %d at frontend", 1161 frontend_state); 1162 break; 1163 } 1164 } 1165 1166 /* 1167 Release the translation entry specfied 1168 */ 1169 static void scsiback_release_translation_entry(struct vscsibk_info *info) 1170 { 1171 struct v2p_entry *entry, *tmp; 1172 struct list_head *head = &(info->v2p_entry_lists); 1173 unsigned long flags; 1174 1175 spin_lock_irqsave(&info->v2p_lock, flags); 1176 1177 list_for_each_entry_safe(entry, tmp, head, l) 1178 __scsiback_del_translation_entry(entry); 1179 1180 spin_unlock_irqrestore(&info->v2p_lock, flags); 1181 } 1182 1183 static int scsiback_remove(struct xenbus_device *dev) 1184 { 1185 struct vscsibk_info *info = dev_get_drvdata(&dev->dev); 1186 1187 if (info->irq) 1188 scsiback_disconnect(info); 1189 1190 scsiback_release_translation_entry(info); 1191 1192 dev_set_drvdata(&dev->dev, NULL); 1193 1194 return 0; 1195 } 1196 1197 static int scsiback_probe(struct xenbus_device *dev, 1198 const struct xenbus_device_id *id) 1199 { 1200 int err; 1201 1202 struct vscsibk_info *info = kzalloc(sizeof(struct vscsibk_info), 1203 GFP_KERNEL); 1204 1205 pr_debug("%s %p %d\n", __func__, dev, dev->otherend_id); 1206 1207 if (!info) { 1208 xenbus_dev_fatal(dev, -ENOMEM, "allocating backend structure"); 1209 return -ENOMEM; 1210 } 1211 info->dev = dev; 1212 dev_set_drvdata(&dev->dev, info); 1213 1214 info->domid = dev->otherend_id; 1215 spin_lock_init(&info->ring_lock); 1216 info->ring_error = 0; 1217 atomic_set(&info->nr_unreplied_reqs, 0); 1218 init_waitqueue_head(&info->waiting_to_free); 1219 info->dev = dev; 1220 info->irq = 0; 1221 INIT_LIST_HEAD(&info->v2p_entry_lists); 1222 spin_lock_init(&info->v2p_lock); 1223 1224 err = xenbus_printf(XBT_NIL, dev->nodename, "feature-sg-grant", "%u", 1225 SG_ALL); 1226 if (err) 1227 xenbus_dev_error(dev, err, "writing feature-sg-grant"); 1228 1229 err = xenbus_switch_state(dev, XenbusStateInitWait); 1230 if (err) 1231 goto fail; 1232 1233 return 0; 1234 1235 fail: 1236 pr_warn("%s failed\n", __func__); 1237 scsiback_remove(dev); 1238 1239 return err; 1240 } 1241 1242 static char *scsiback_dump_proto_id(struct scsiback_tport *tport) 1243 { 1244 switch (tport->tport_proto_id) { 1245 case SCSI_PROTOCOL_SAS: 1246 return "SAS"; 1247 case SCSI_PROTOCOL_FCP: 1248 return "FCP"; 1249 case SCSI_PROTOCOL_ISCSI: 1250 return "iSCSI"; 1251 default: 1252 break; 1253 } 1254 1255 return "Unknown"; 1256 } 1257 1258 static u8 scsiback_get_fabric_proto_ident(struct se_portal_group *se_tpg) 1259 { 1260 struct scsiback_tpg *tpg = container_of(se_tpg, 1261 struct scsiback_tpg, se_tpg); 1262 struct scsiback_tport *tport = tpg->tport; 1263 1264 switch (tport->tport_proto_id) { 1265 case SCSI_PROTOCOL_SAS: 1266 return sas_get_fabric_proto_ident(se_tpg); 1267 case SCSI_PROTOCOL_FCP: 1268 return fc_get_fabric_proto_ident(se_tpg); 1269 case SCSI_PROTOCOL_ISCSI: 1270 return iscsi_get_fabric_proto_ident(se_tpg); 1271 default: 1272 pr_err("Unknown tport_proto_id: 0x%02x, using SAS emulation\n", 1273 tport->tport_proto_id); 1274 break; 1275 } 1276 1277 return sas_get_fabric_proto_ident(se_tpg); 1278 } 1279 1280 static char *scsiback_get_fabric_wwn(struct se_portal_group *se_tpg) 1281 { 1282 struct scsiback_tpg *tpg = container_of(se_tpg, 1283 struct scsiback_tpg, se_tpg); 1284 struct scsiback_tport *tport = tpg->tport; 1285 1286 return &tport->tport_name[0]; 1287 } 1288 1289 static u16 scsiback_get_tag(struct se_portal_group *se_tpg) 1290 { 1291 struct scsiback_tpg *tpg = container_of(se_tpg, 1292 struct scsiback_tpg, se_tpg); 1293 return tpg->tport_tpgt; 1294 } 1295 1296 static u32 scsiback_get_default_depth(struct se_portal_group *se_tpg) 1297 { 1298 return 1; 1299 } 1300 1301 static u32 1302 scsiback_get_pr_transport_id(struct se_portal_group *se_tpg, 1303 struct se_node_acl *se_nacl, 1304 struct t10_pr_registration *pr_reg, 1305 int *format_code, 1306 unsigned char *buf) 1307 { 1308 struct scsiback_tpg *tpg = container_of(se_tpg, 1309 struct scsiback_tpg, se_tpg); 1310 struct scsiback_tport *tport = tpg->tport; 1311 1312 switch (tport->tport_proto_id) { 1313 case SCSI_PROTOCOL_SAS: 1314 return sas_get_pr_transport_id(se_tpg, se_nacl, pr_reg, 1315 format_code, buf); 1316 case SCSI_PROTOCOL_FCP: 1317 return fc_get_pr_transport_id(se_tpg, se_nacl, pr_reg, 1318 format_code, buf); 1319 case SCSI_PROTOCOL_ISCSI: 1320 return iscsi_get_pr_transport_id(se_tpg, se_nacl, pr_reg, 1321 format_code, buf); 1322 default: 1323 pr_err("Unknown tport_proto_id: 0x%02x, using SAS emulation\n", 1324 tport->tport_proto_id); 1325 break; 1326 } 1327 1328 return sas_get_pr_transport_id(se_tpg, se_nacl, pr_reg, 1329 format_code, buf); 1330 } 1331 1332 static u32 1333 scsiback_get_pr_transport_id_len(struct se_portal_group *se_tpg, 1334 struct se_node_acl *se_nacl, 1335 struct t10_pr_registration *pr_reg, 1336 int *format_code) 1337 { 1338 struct scsiback_tpg *tpg = container_of(se_tpg, 1339 struct scsiback_tpg, se_tpg); 1340 struct scsiback_tport *tport = tpg->tport; 1341 1342 switch (tport->tport_proto_id) { 1343 case SCSI_PROTOCOL_SAS: 1344 return sas_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg, 1345 format_code); 1346 case SCSI_PROTOCOL_FCP: 1347 return fc_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg, 1348 format_code); 1349 case SCSI_PROTOCOL_ISCSI: 1350 return iscsi_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg, 1351 format_code); 1352 default: 1353 pr_err("Unknown tport_proto_id: 0x%02x, using SAS emulation\n", 1354 tport->tport_proto_id); 1355 break; 1356 } 1357 1358 return sas_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg, 1359 format_code); 1360 } 1361 1362 static char * 1363 scsiback_parse_pr_out_transport_id(struct se_portal_group *se_tpg, 1364 const char *buf, 1365 u32 *out_tid_len, 1366 char **port_nexus_ptr) 1367 { 1368 struct scsiback_tpg *tpg = container_of(se_tpg, 1369 struct scsiback_tpg, se_tpg); 1370 struct scsiback_tport *tport = tpg->tport; 1371 1372 switch (tport->tport_proto_id) { 1373 case SCSI_PROTOCOL_SAS: 1374 return sas_parse_pr_out_transport_id(se_tpg, buf, out_tid_len, 1375 port_nexus_ptr); 1376 case SCSI_PROTOCOL_FCP: 1377 return fc_parse_pr_out_transport_id(se_tpg, buf, out_tid_len, 1378 port_nexus_ptr); 1379 case SCSI_PROTOCOL_ISCSI: 1380 return iscsi_parse_pr_out_transport_id(se_tpg, buf, out_tid_len, 1381 port_nexus_ptr); 1382 default: 1383 pr_err("Unknown tport_proto_id: 0x%02x, using SAS emulation\n", 1384 tport->tport_proto_id); 1385 break; 1386 } 1387 1388 return sas_parse_pr_out_transport_id(se_tpg, buf, out_tid_len, 1389 port_nexus_ptr); 1390 } 1391 1392 static struct se_wwn * 1393 scsiback_make_tport(struct target_fabric_configfs *tf, 1394 struct config_group *group, 1395 const char *name) 1396 { 1397 struct scsiback_tport *tport; 1398 char *ptr; 1399 u64 wwpn = 0; 1400 int off = 0; 1401 1402 tport = kzalloc(sizeof(struct scsiback_tport), GFP_KERNEL); 1403 if (!tport) 1404 return ERR_PTR(-ENOMEM); 1405 1406 tport->tport_wwpn = wwpn; 1407 /* 1408 * Determine the emulated Protocol Identifier and Target Port Name 1409 * based on the incoming configfs directory name. 1410 */ 1411 ptr = strstr(name, "naa."); 1412 if (ptr) { 1413 tport->tport_proto_id = SCSI_PROTOCOL_SAS; 1414 goto check_len; 1415 } 1416 ptr = strstr(name, "fc."); 1417 if (ptr) { 1418 tport->tport_proto_id = SCSI_PROTOCOL_FCP; 1419 off = 3; /* Skip over "fc." */ 1420 goto check_len; 1421 } 1422 ptr = strstr(name, "iqn."); 1423 if (ptr) { 1424 tport->tport_proto_id = SCSI_PROTOCOL_ISCSI; 1425 goto check_len; 1426 } 1427 1428 pr_err("Unable to locate prefix for emulated Target Port: %s\n", name); 1429 kfree(tport); 1430 return ERR_PTR(-EINVAL); 1431 1432 check_len: 1433 if (strlen(name) >= VSCSI_NAMELEN) { 1434 pr_err("Emulated %s Address: %s, exceeds max: %d\n", name, 1435 scsiback_dump_proto_id(tport), VSCSI_NAMELEN); 1436 kfree(tport); 1437 return ERR_PTR(-EINVAL); 1438 } 1439 snprintf(&tport->tport_name[0], VSCSI_NAMELEN, "%s", &name[off]); 1440 1441 pr_debug("Allocated emulated Target %s Address: %s\n", 1442 scsiback_dump_proto_id(tport), name); 1443 1444 return &tport->tport_wwn; 1445 } 1446 1447 static void scsiback_drop_tport(struct se_wwn *wwn) 1448 { 1449 struct scsiback_tport *tport = container_of(wwn, 1450 struct scsiback_tport, tport_wwn); 1451 1452 pr_debug("Deallocating emulated Target %s Address: %s\n", 1453 scsiback_dump_proto_id(tport), tport->tport_name); 1454 1455 kfree(tport); 1456 } 1457 1458 static struct se_node_acl * 1459 scsiback_alloc_fabric_acl(struct se_portal_group *se_tpg) 1460 { 1461 return kzalloc(sizeof(struct se_node_acl), GFP_KERNEL); 1462 } 1463 1464 static void 1465 scsiback_release_fabric_acl(struct se_portal_group *se_tpg, 1466 struct se_node_acl *se_nacl) 1467 { 1468 kfree(se_nacl); 1469 } 1470 1471 static u32 scsiback_tpg_get_inst_index(struct se_portal_group *se_tpg) 1472 { 1473 return 1; 1474 } 1475 1476 static int scsiback_check_stop_free(struct se_cmd *se_cmd) 1477 { 1478 /* 1479 * Do not release struct se_cmd's containing a valid TMR pointer. 1480 * These will be released directly in scsiback_device_action() 1481 * with transport_generic_free_cmd(). 1482 */ 1483 if (se_cmd->se_cmd_flags & SCF_SCSI_TMR_CDB) 1484 return 0; 1485 1486 transport_generic_free_cmd(se_cmd, 0); 1487 return 1; 1488 } 1489 1490 static void scsiback_release_cmd(struct se_cmd *se_cmd) 1491 { 1492 struct vscsibk_pend *pending_req = container_of(se_cmd, 1493 struct vscsibk_pend, se_cmd); 1494 1495 kmem_cache_free(scsiback_cachep, pending_req); 1496 } 1497 1498 static int scsiback_shutdown_session(struct se_session *se_sess) 1499 { 1500 return 0; 1501 } 1502 1503 static void scsiback_close_session(struct se_session *se_sess) 1504 { 1505 } 1506 1507 static u32 scsiback_sess_get_index(struct se_session *se_sess) 1508 { 1509 return 0; 1510 } 1511 1512 static int scsiback_write_pending(struct se_cmd *se_cmd) 1513 { 1514 /* Go ahead and process the write immediately */ 1515 target_execute_cmd(se_cmd); 1516 1517 return 0; 1518 } 1519 1520 static int scsiback_write_pending_status(struct se_cmd *se_cmd) 1521 { 1522 return 0; 1523 } 1524 1525 static void scsiback_set_default_node_attrs(struct se_node_acl *nacl) 1526 { 1527 } 1528 1529 static u32 scsiback_get_task_tag(struct se_cmd *se_cmd) 1530 { 1531 struct vscsibk_pend *pending_req = container_of(se_cmd, 1532 struct vscsibk_pend, se_cmd); 1533 1534 return pending_req->rqid; 1535 } 1536 1537 static int scsiback_get_cmd_state(struct se_cmd *se_cmd) 1538 { 1539 return 0; 1540 } 1541 1542 static int scsiback_queue_data_in(struct se_cmd *se_cmd) 1543 { 1544 struct vscsibk_pend *pending_req = container_of(se_cmd, 1545 struct vscsibk_pend, se_cmd); 1546 1547 pending_req->result = SAM_STAT_GOOD; 1548 scsiback_cmd_done(pending_req); 1549 return 0; 1550 } 1551 1552 static int scsiback_queue_status(struct se_cmd *se_cmd) 1553 { 1554 struct vscsibk_pend *pending_req = container_of(se_cmd, 1555 struct vscsibk_pend, se_cmd); 1556 1557 if (se_cmd->sense_buffer && 1558 ((se_cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) || 1559 (se_cmd->se_cmd_flags & SCF_EMULATED_TASK_SENSE))) 1560 pending_req->result = (DRIVER_SENSE << 24) | 1561 SAM_STAT_CHECK_CONDITION; 1562 else 1563 pending_req->result = se_cmd->scsi_status; 1564 1565 scsiback_cmd_done(pending_req); 1566 return 0; 1567 } 1568 1569 static void scsiback_queue_tm_rsp(struct se_cmd *se_cmd) 1570 { 1571 struct se_tmr_req *se_tmr = se_cmd->se_tmr_req; 1572 struct scsiback_tmr *tmr = se_tmr->fabric_tmr_ptr; 1573 1574 atomic_set(&tmr->tmr_complete, 1); 1575 wake_up(&tmr->tmr_wait); 1576 } 1577 1578 static void scsiback_aborted_task(struct se_cmd *se_cmd) 1579 { 1580 } 1581 1582 static ssize_t scsiback_tpg_param_show_alias(struct se_portal_group *se_tpg, 1583 char *page) 1584 { 1585 struct scsiback_tpg *tpg = container_of(se_tpg, struct scsiback_tpg, 1586 se_tpg); 1587 ssize_t rb; 1588 1589 mutex_lock(&tpg->tv_tpg_mutex); 1590 rb = snprintf(page, PAGE_SIZE, "%s\n", tpg->param_alias); 1591 mutex_unlock(&tpg->tv_tpg_mutex); 1592 1593 return rb; 1594 } 1595 1596 static ssize_t scsiback_tpg_param_store_alias(struct se_portal_group *se_tpg, 1597 const char *page, size_t count) 1598 { 1599 struct scsiback_tpg *tpg = container_of(se_tpg, struct scsiback_tpg, 1600 se_tpg); 1601 int len; 1602 1603 if (strlen(page) >= VSCSI_NAMELEN) { 1604 pr_err("param alias: %s, exceeds max: %d\n", page, 1605 VSCSI_NAMELEN); 1606 return -EINVAL; 1607 } 1608 1609 mutex_lock(&tpg->tv_tpg_mutex); 1610 len = snprintf(tpg->param_alias, VSCSI_NAMELEN, "%s", page); 1611 if (tpg->param_alias[len - 1] == '\n') 1612 tpg->param_alias[len - 1] = '\0'; 1613 mutex_unlock(&tpg->tv_tpg_mutex); 1614 1615 return count; 1616 } 1617 1618 TF_TPG_PARAM_ATTR(scsiback, alias, S_IRUGO | S_IWUSR); 1619 1620 static struct configfs_attribute *scsiback_param_attrs[] = { 1621 &scsiback_tpg_param_alias.attr, 1622 NULL, 1623 }; 1624 1625 static int scsiback_make_nexus(struct scsiback_tpg *tpg, 1626 const char *name) 1627 { 1628 struct se_portal_group *se_tpg; 1629 struct se_session *se_sess; 1630 struct scsiback_nexus *tv_nexus; 1631 1632 mutex_lock(&tpg->tv_tpg_mutex); 1633 if (tpg->tpg_nexus) { 1634 mutex_unlock(&tpg->tv_tpg_mutex); 1635 pr_debug("tpg->tpg_nexus already exists\n"); 1636 return -EEXIST; 1637 } 1638 se_tpg = &tpg->se_tpg; 1639 1640 tv_nexus = kzalloc(sizeof(struct scsiback_nexus), GFP_KERNEL); 1641 if (!tv_nexus) { 1642 mutex_unlock(&tpg->tv_tpg_mutex); 1643 return -ENOMEM; 1644 } 1645 /* 1646 * Initialize the struct se_session pointer 1647 */ 1648 tv_nexus->tvn_se_sess = transport_init_session(TARGET_PROT_NORMAL); 1649 if (IS_ERR(tv_nexus->tvn_se_sess)) { 1650 mutex_unlock(&tpg->tv_tpg_mutex); 1651 kfree(tv_nexus); 1652 return -ENOMEM; 1653 } 1654 se_sess = tv_nexus->tvn_se_sess; 1655 /* 1656 * Since we are running in 'demo mode' this call with generate a 1657 * struct se_node_acl for the scsiback struct se_portal_group with 1658 * the SCSI Initiator port name of the passed configfs group 'name'. 1659 */ 1660 tv_nexus->tvn_se_sess->se_node_acl = core_tpg_check_initiator_node_acl( 1661 se_tpg, (unsigned char *)name); 1662 if (!tv_nexus->tvn_se_sess->se_node_acl) { 1663 mutex_unlock(&tpg->tv_tpg_mutex); 1664 pr_debug("core_tpg_check_initiator_node_acl() failed for %s\n", 1665 name); 1666 goto out; 1667 } 1668 /* Now register the TCM pvscsi virtual I_T Nexus as active. */ 1669 transport_register_session(se_tpg, tv_nexus->tvn_se_sess->se_node_acl, 1670 tv_nexus->tvn_se_sess, tv_nexus); 1671 tpg->tpg_nexus = tv_nexus; 1672 1673 mutex_unlock(&tpg->tv_tpg_mutex); 1674 return 0; 1675 1676 out: 1677 transport_free_session(se_sess); 1678 kfree(tv_nexus); 1679 return -ENOMEM; 1680 } 1681 1682 static int scsiback_drop_nexus(struct scsiback_tpg *tpg) 1683 { 1684 struct se_session *se_sess; 1685 struct scsiback_nexus *tv_nexus; 1686 1687 mutex_lock(&tpg->tv_tpg_mutex); 1688 tv_nexus = tpg->tpg_nexus; 1689 if (!tv_nexus) { 1690 mutex_unlock(&tpg->tv_tpg_mutex); 1691 return -ENODEV; 1692 } 1693 1694 se_sess = tv_nexus->tvn_se_sess; 1695 if (!se_sess) { 1696 mutex_unlock(&tpg->tv_tpg_mutex); 1697 return -ENODEV; 1698 } 1699 1700 if (tpg->tv_tpg_port_count != 0) { 1701 mutex_unlock(&tpg->tv_tpg_mutex); 1702 pr_err("Unable to remove xen-pvscsi I_T Nexus with active TPG port count: %d\n", 1703 tpg->tv_tpg_port_count); 1704 return -EBUSY; 1705 } 1706 1707 if (tpg->tv_tpg_fe_count != 0) { 1708 mutex_unlock(&tpg->tv_tpg_mutex); 1709 pr_err("Unable to remove xen-pvscsi I_T Nexus with active TPG frontend count: %d\n", 1710 tpg->tv_tpg_fe_count); 1711 return -EBUSY; 1712 } 1713 1714 pr_debug("Removing I_T Nexus to emulated %s Initiator Port: %s\n", 1715 scsiback_dump_proto_id(tpg->tport), 1716 tv_nexus->tvn_se_sess->se_node_acl->initiatorname); 1717 1718 /* 1719 * Release the SCSI I_T Nexus to the emulated xen-pvscsi Target Port 1720 */ 1721 transport_deregister_session(tv_nexus->tvn_se_sess); 1722 tpg->tpg_nexus = NULL; 1723 mutex_unlock(&tpg->tv_tpg_mutex); 1724 1725 kfree(tv_nexus); 1726 return 0; 1727 } 1728 1729 static ssize_t scsiback_tpg_show_nexus(struct se_portal_group *se_tpg, 1730 char *page) 1731 { 1732 struct scsiback_tpg *tpg = container_of(se_tpg, 1733 struct scsiback_tpg, se_tpg); 1734 struct scsiback_nexus *tv_nexus; 1735 ssize_t ret; 1736 1737 mutex_lock(&tpg->tv_tpg_mutex); 1738 tv_nexus = tpg->tpg_nexus; 1739 if (!tv_nexus) { 1740 mutex_unlock(&tpg->tv_tpg_mutex); 1741 return -ENODEV; 1742 } 1743 ret = snprintf(page, PAGE_SIZE, "%s\n", 1744 tv_nexus->tvn_se_sess->se_node_acl->initiatorname); 1745 mutex_unlock(&tpg->tv_tpg_mutex); 1746 1747 return ret; 1748 } 1749 1750 static ssize_t scsiback_tpg_store_nexus(struct se_portal_group *se_tpg, 1751 const char *page, 1752 size_t count) 1753 { 1754 struct scsiback_tpg *tpg = container_of(se_tpg, 1755 struct scsiback_tpg, se_tpg); 1756 struct scsiback_tport *tport_wwn = tpg->tport; 1757 unsigned char i_port[VSCSI_NAMELEN], *ptr, *port_ptr; 1758 int ret; 1759 /* 1760 * Shutdown the active I_T nexus if 'NULL' is passed. 1761 */ 1762 if (!strncmp(page, "NULL", 4)) { 1763 ret = scsiback_drop_nexus(tpg); 1764 return (!ret) ? count : ret; 1765 } 1766 /* 1767 * Otherwise make sure the passed virtual Initiator port WWN matches 1768 * the fabric protocol_id set in scsiback_make_tport(), and call 1769 * scsiback_make_nexus(). 1770 */ 1771 if (strlen(page) >= VSCSI_NAMELEN) { 1772 pr_err("Emulated NAA Sas Address: %s, exceeds max: %d\n", 1773 page, VSCSI_NAMELEN); 1774 return -EINVAL; 1775 } 1776 snprintf(&i_port[0], VSCSI_NAMELEN, "%s", page); 1777 1778 ptr = strstr(i_port, "naa."); 1779 if (ptr) { 1780 if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_SAS) { 1781 pr_err("Passed SAS Initiator Port %s does not match target port protoid: %s\n", 1782 i_port, scsiback_dump_proto_id(tport_wwn)); 1783 return -EINVAL; 1784 } 1785 port_ptr = &i_port[0]; 1786 goto check_newline; 1787 } 1788 ptr = strstr(i_port, "fc."); 1789 if (ptr) { 1790 if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_FCP) { 1791 pr_err("Passed FCP Initiator Port %s does not match target port protoid: %s\n", 1792 i_port, scsiback_dump_proto_id(tport_wwn)); 1793 return -EINVAL; 1794 } 1795 port_ptr = &i_port[3]; /* Skip over "fc." */ 1796 goto check_newline; 1797 } 1798 ptr = strstr(i_port, "iqn."); 1799 if (ptr) { 1800 if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_ISCSI) { 1801 pr_err("Passed iSCSI Initiator Port %s does not match target port protoid: %s\n", 1802 i_port, scsiback_dump_proto_id(tport_wwn)); 1803 return -EINVAL; 1804 } 1805 port_ptr = &i_port[0]; 1806 goto check_newline; 1807 } 1808 pr_err("Unable to locate prefix for emulated Initiator Port: %s\n", 1809 i_port); 1810 return -EINVAL; 1811 /* 1812 * Clear any trailing newline for the NAA WWN 1813 */ 1814 check_newline: 1815 if (i_port[strlen(i_port) - 1] == '\n') 1816 i_port[strlen(i_port) - 1] = '\0'; 1817 1818 ret = scsiback_make_nexus(tpg, port_ptr); 1819 if (ret < 0) 1820 return ret; 1821 1822 return count; 1823 } 1824 1825 TF_TPG_BASE_ATTR(scsiback, nexus, S_IRUGO | S_IWUSR); 1826 1827 static struct configfs_attribute *scsiback_tpg_attrs[] = { 1828 &scsiback_tpg_nexus.attr, 1829 NULL, 1830 }; 1831 1832 static ssize_t 1833 scsiback_wwn_show_attr_version(struct target_fabric_configfs *tf, 1834 char *page) 1835 { 1836 return sprintf(page, "xen-pvscsi fabric module %s on %s/%s on " 1837 UTS_RELEASE"\n", 1838 VSCSI_VERSION, utsname()->sysname, utsname()->machine); 1839 } 1840 1841 TF_WWN_ATTR_RO(scsiback, version); 1842 1843 static struct configfs_attribute *scsiback_wwn_attrs[] = { 1844 &scsiback_wwn_version.attr, 1845 NULL, 1846 }; 1847 1848 static char *scsiback_get_fabric_name(void) 1849 { 1850 return "xen-pvscsi"; 1851 } 1852 1853 static int scsiback_port_link(struct se_portal_group *se_tpg, 1854 struct se_lun *lun) 1855 { 1856 struct scsiback_tpg *tpg = container_of(se_tpg, 1857 struct scsiback_tpg, se_tpg); 1858 1859 mutex_lock(&tpg->tv_tpg_mutex); 1860 tpg->tv_tpg_port_count++; 1861 mutex_unlock(&tpg->tv_tpg_mutex); 1862 1863 return 0; 1864 } 1865 1866 static void scsiback_port_unlink(struct se_portal_group *se_tpg, 1867 struct se_lun *lun) 1868 { 1869 struct scsiback_tpg *tpg = container_of(se_tpg, 1870 struct scsiback_tpg, se_tpg); 1871 1872 mutex_lock(&tpg->tv_tpg_mutex); 1873 tpg->tv_tpg_port_count--; 1874 mutex_unlock(&tpg->tv_tpg_mutex); 1875 } 1876 1877 static struct se_portal_group * 1878 scsiback_make_tpg(struct se_wwn *wwn, 1879 struct config_group *group, 1880 const char *name) 1881 { 1882 struct scsiback_tport *tport = container_of(wwn, 1883 struct scsiback_tport, tport_wwn); 1884 1885 struct scsiback_tpg *tpg; 1886 u16 tpgt; 1887 int ret; 1888 1889 if (strstr(name, "tpgt_") != name) 1890 return ERR_PTR(-EINVAL); 1891 ret = kstrtou16(name + 5, 10, &tpgt); 1892 if (ret) 1893 return ERR_PTR(ret); 1894 1895 tpg = kzalloc(sizeof(struct scsiback_tpg), GFP_KERNEL); 1896 if (!tpg) 1897 return ERR_PTR(-ENOMEM); 1898 1899 mutex_init(&tpg->tv_tpg_mutex); 1900 INIT_LIST_HEAD(&tpg->tv_tpg_list); 1901 INIT_LIST_HEAD(&tpg->info_list); 1902 tpg->tport = tport; 1903 tpg->tport_tpgt = tpgt; 1904 1905 ret = core_tpg_register(&scsiback_fabric_configfs->tf_ops, wwn, 1906 &tpg->se_tpg, tpg, TRANSPORT_TPG_TYPE_NORMAL); 1907 if (ret < 0) { 1908 kfree(tpg); 1909 return NULL; 1910 } 1911 mutex_lock(&scsiback_mutex); 1912 list_add_tail(&tpg->tv_tpg_list, &scsiback_list); 1913 mutex_unlock(&scsiback_mutex); 1914 1915 return &tpg->se_tpg; 1916 } 1917 1918 static void scsiback_drop_tpg(struct se_portal_group *se_tpg) 1919 { 1920 struct scsiback_tpg *tpg = container_of(se_tpg, 1921 struct scsiback_tpg, se_tpg); 1922 1923 mutex_lock(&scsiback_mutex); 1924 list_del(&tpg->tv_tpg_list); 1925 mutex_unlock(&scsiback_mutex); 1926 /* 1927 * Release the virtual I_T Nexus for this xen-pvscsi TPG 1928 */ 1929 scsiback_drop_nexus(tpg); 1930 /* 1931 * Deregister the se_tpg from TCM. 1932 */ 1933 core_tpg_deregister(se_tpg); 1934 kfree(tpg); 1935 } 1936 1937 static int scsiback_check_true(struct se_portal_group *se_tpg) 1938 { 1939 return 1; 1940 } 1941 1942 static int scsiback_check_false(struct se_portal_group *se_tpg) 1943 { 1944 return 0; 1945 } 1946 1947 static struct target_core_fabric_ops scsiback_ops = { 1948 .get_fabric_name = scsiback_get_fabric_name, 1949 .get_fabric_proto_ident = scsiback_get_fabric_proto_ident, 1950 .tpg_get_wwn = scsiback_get_fabric_wwn, 1951 .tpg_get_tag = scsiback_get_tag, 1952 .tpg_get_default_depth = scsiback_get_default_depth, 1953 .tpg_get_pr_transport_id = scsiback_get_pr_transport_id, 1954 .tpg_get_pr_transport_id_len = scsiback_get_pr_transport_id_len, 1955 .tpg_parse_pr_out_transport_id = scsiback_parse_pr_out_transport_id, 1956 .tpg_check_demo_mode = scsiback_check_true, 1957 .tpg_check_demo_mode_cache = scsiback_check_true, 1958 .tpg_check_demo_mode_write_protect = scsiback_check_false, 1959 .tpg_check_prod_mode_write_protect = scsiback_check_false, 1960 .tpg_alloc_fabric_acl = scsiback_alloc_fabric_acl, 1961 .tpg_release_fabric_acl = scsiback_release_fabric_acl, 1962 .tpg_get_inst_index = scsiback_tpg_get_inst_index, 1963 .check_stop_free = scsiback_check_stop_free, 1964 .release_cmd = scsiback_release_cmd, 1965 .put_session = NULL, 1966 .shutdown_session = scsiback_shutdown_session, 1967 .close_session = scsiback_close_session, 1968 .sess_get_index = scsiback_sess_get_index, 1969 .sess_get_initiator_sid = NULL, 1970 .write_pending = scsiback_write_pending, 1971 .write_pending_status = scsiback_write_pending_status, 1972 .set_default_node_attributes = scsiback_set_default_node_attrs, 1973 .get_task_tag = scsiback_get_task_tag, 1974 .get_cmd_state = scsiback_get_cmd_state, 1975 .queue_data_in = scsiback_queue_data_in, 1976 .queue_status = scsiback_queue_status, 1977 .queue_tm_rsp = scsiback_queue_tm_rsp, 1978 .aborted_task = scsiback_aborted_task, 1979 /* 1980 * Setup callers for generic logic in target_core_fabric_configfs.c 1981 */ 1982 .fabric_make_wwn = scsiback_make_tport, 1983 .fabric_drop_wwn = scsiback_drop_tport, 1984 .fabric_make_tpg = scsiback_make_tpg, 1985 .fabric_drop_tpg = scsiback_drop_tpg, 1986 .fabric_post_link = scsiback_port_link, 1987 .fabric_pre_unlink = scsiback_port_unlink, 1988 .fabric_make_np = NULL, 1989 .fabric_drop_np = NULL, 1990 #if 0 1991 .fabric_make_nodeacl = scsiback_make_nodeacl, 1992 .fabric_drop_nodeacl = scsiback_drop_nodeacl, 1993 #endif 1994 }; 1995 1996 static int scsiback_register_configfs(void) 1997 { 1998 struct target_fabric_configfs *fabric; 1999 int ret; 2000 2001 pr_debug("fabric module %s on %s/%s on "UTS_RELEASE"\n", 2002 VSCSI_VERSION, utsname()->sysname, utsname()->machine); 2003 /* 2004 * Register the top level struct config_item_type with TCM core 2005 */ 2006 fabric = target_fabric_configfs_init(THIS_MODULE, "xen-pvscsi"); 2007 if (IS_ERR(fabric)) 2008 return PTR_ERR(fabric); 2009 2010 /* 2011 * Setup fabric->tf_ops from our local scsiback_ops 2012 */ 2013 fabric->tf_ops = scsiback_ops; 2014 /* 2015 * Setup default attribute lists for various fabric->tf_cit_tmpl 2016 */ 2017 fabric->tf_cit_tmpl.tfc_wwn_cit.ct_attrs = scsiback_wwn_attrs; 2018 fabric->tf_cit_tmpl.tfc_tpg_base_cit.ct_attrs = scsiback_tpg_attrs; 2019 fabric->tf_cit_tmpl.tfc_tpg_attrib_cit.ct_attrs = NULL; 2020 fabric->tf_cit_tmpl.tfc_tpg_param_cit.ct_attrs = scsiback_param_attrs; 2021 fabric->tf_cit_tmpl.tfc_tpg_np_base_cit.ct_attrs = NULL; 2022 fabric->tf_cit_tmpl.tfc_tpg_nacl_base_cit.ct_attrs = NULL; 2023 fabric->tf_cit_tmpl.tfc_tpg_nacl_attrib_cit.ct_attrs = NULL; 2024 fabric->tf_cit_tmpl.tfc_tpg_nacl_auth_cit.ct_attrs = NULL; 2025 fabric->tf_cit_tmpl.tfc_tpg_nacl_param_cit.ct_attrs = NULL; 2026 /* 2027 * Register the fabric for use within TCM 2028 */ 2029 ret = target_fabric_configfs_register(fabric); 2030 if (ret < 0) { 2031 target_fabric_configfs_free(fabric); 2032 return ret; 2033 } 2034 /* 2035 * Setup our local pointer to *fabric 2036 */ 2037 scsiback_fabric_configfs = fabric; 2038 pr_debug("Set fabric -> scsiback_fabric_configfs\n"); 2039 return 0; 2040 }; 2041 2042 static void scsiback_deregister_configfs(void) 2043 { 2044 if (!scsiback_fabric_configfs) 2045 return; 2046 2047 target_fabric_configfs_deregister(scsiback_fabric_configfs); 2048 scsiback_fabric_configfs = NULL; 2049 pr_debug("Cleared scsiback_fabric_configfs\n"); 2050 }; 2051 2052 static const struct xenbus_device_id scsiback_ids[] = { 2053 { "vscsi" }, 2054 { "" } 2055 }; 2056 2057 static struct xenbus_driver scsiback_driver = { 2058 .ids = scsiback_ids, 2059 .probe = scsiback_probe, 2060 .remove = scsiback_remove, 2061 .otherend_changed = scsiback_frontend_changed 2062 }; 2063 2064 static void scsiback_init_pend(void *p) 2065 { 2066 struct vscsibk_pend *pend = p; 2067 int i; 2068 2069 memset(pend, 0, sizeof(*pend)); 2070 for (i = 0; i < VSCSI_MAX_GRANTS; i++) 2071 pend->grant_handles[i] = SCSIBACK_INVALID_HANDLE; 2072 } 2073 2074 static int __init scsiback_init(void) 2075 { 2076 int ret; 2077 2078 if (!xen_domain()) 2079 return -ENODEV; 2080 2081 scsiback_cachep = kmem_cache_create("vscsiif_cache", 2082 sizeof(struct vscsibk_pend), 0, 0, scsiback_init_pend); 2083 if (!scsiback_cachep) 2084 return -ENOMEM; 2085 2086 ret = xenbus_register_backend(&scsiback_driver); 2087 if (ret) 2088 goto out_cache_destroy; 2089 2090 ret = scsiback_register_configfs(); 2091 if (ret) 2092 goto out_unregister_xenbus; 2093 2094 return 0; 2095 2096 out_unregister_xenbus: 2097 xenbus_unregister_driver(&scsiback_driver); 2098 out_cache_destroy: 2099 kmem_cache_destroy(scsiback_cachep); 2100 pr_err("%s: error %d\n", __func__, ret); 2101 return ret; 2102 } 2103 2104 static void __exit scsiback_exit(void) 2105 { 2106 struct page *page; 2107 2108 while (free_pages_num) { 2109 if (get_free_page(&page)) 2110 BUG(); 2111 gnttab_free_pages(1, &page); 2112 } 2113 scsiback_deregister_configfs(); 2114 xenbus_unregister_driver(&scsiback_driver); 2115 kmem_cache_destroy(scsiback_cachep); 2116 } 2117 2118 module_init(scsiback_init); 2119 module_exit(scsiback_exit); 2120 2121 MODULE_DESCRIPTION("Xen SCSI backend driver"); 2122 MODULE_LICENSE("Dual BSD/GPL"); 2123 MODULE_ALIAS("xen-backend:vscsi"); 2124 MODULE_AUTHOR("Juergen Gross <jgross@suse.com>"); 2125