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