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 /* 1663 * Now register the TCM pvscsi virtual I_T Nexus as active with the 1664 * call to __transport_register_session() 1665 */ 1666 __transport_register_session(se_tpg, tv_nexus->tvn_se_sess->se_node_acl, 1667 tv_nexus->tvn_se_sess, tv_nexus); 1668 tpg->tpg_nexus = tv_nexus; 1669 1670 mutex_unlock(&tpg->tv_tpg_mutex); 1671 return 0; 1672 1673 out: 1674 transport_free_session(se_sess); 1675 kfree(tv_nexus); 1676 return -ENOMEM; 1677 } 1678 1679 static int scsiback_drop_nexus(struct scsiback_tpg *tpg) 1680 { 1681 struct se_session *se_sess; 1682 struct scsiback_nexus *tv_nexus; 1683 1684 mutex_lock(&tpg->tv_tpg_mutex); 1685 tv_nexus = tpg->tpg_nexus; 1686 if (!tv_nexus) { 1687 mutex_unlock(&tpg->tv_tpg_mutex); 1688 return -ENODEV; 1689 } 1690 1691 se_sess = tv_nexus->tvn_se_sess; 1692 if (!se_sess) { 1693 mutex_unlock(&tpg->tv_tpg_mutex); 1694 return -ENODEV; 1695 } 1696 1697 if (tpg->tv_tpg_port_count != 0) { 1698 mutex_unlock(&tpg->tv_tpg_mutex); 1699 pr_err("Unable to remove xen-pvscsi I_T Nexus with active TPG port count: %d\n", 1700 tpg->tv_tpg_port_count); 1701 return -EBUSY; 1702 } 1703 1704 if (tpg->tv_tpg_fe_count != 0) { 1705 mutex_unlock(&tpg->tv_tpg_mutex); 1706 pr_err("Unable to remove xen-pvscsi I_T Nexus with active TPG frontend count: %d\n", 1707 tpg->tv_tpg_fe_count); 1708 return -EBUSY; 1709 } 1710 1711 pr_debug("xen-pvscsi: Removing I_T Nexus to emulated %s Initiator Port: %s\n", 1712 scsiback_dump_proto_id(tpg->tport), 1713 tv_nexus->tvn_se_sess->se_node_acl->initiatorname); 1714 1715 /* 1716 * Release the SCSI I_T Nexus to the emulated xen-pvscsi Target Port 1717 */ 1718 transport_deregister_session(tv_nexus->tvn_se_sess); 1719 tpg->tpg_nexus = NULL; 1720 mutex_unlock(&tpg->tv_tpg_mutex); 1721 1722 kfree(tv_nexus); 1723 return 0; 1724 } 1725 1726 static ssize_t scsiback_tpg_show_nexus(struct se_portal_group *se_tpg, 1727 char *page) 1728 { 1729 struct scsiback_tpg *tpg = container_of(se_tpg, 1730 struct scsiback_tpg, se_tpg); 1731 struct scsiback_nexus *tv_nexus; 1732 ssize_t ret; 1733 1734 mutex_lock(&tpg->tv_tpg_mutex); 1735 tv_nexus = tpg->tpg_nexus; 1736 if (!tv_nexus) { 1737 mutex_unlock(&tpg->tv_tpg_mutex); 1738 return -ENODEV; 1739 } 1740 ret = snprintf(page, PAGE_SIZE, "%s\n", 1741 tv_nexus->tvn_se_sess->se_node_acl->initiatorname); 1742 mutex_unlock(&tpg->tv_tpg_mutex); 1743 1744 return ret; 1745 } 1746 1747 static ssize_t scsiback_tpg_store_nexus(struct se_portal_group *se_tpg, 1748 const char *page, 1749 size_t count) 1750 { 1751 struct scsiback_tpg *tpg = container_of(se_tpg, 1752 struct scsiback_tpg, se_tpg); 1753 struct scsiback_tport *tport_wwn = tpg->tport; 1754 unsigned char i_port[VSCSI_NAMELEN], *ptr, *port_ptr; 1755 int ret; 1756 /* 1757 * Shutdown the active I_T nexus if 'NULL' is passed.. 1758 */ 1759 if (!strncmp(page, "NULL", 4)) { 1760 ret = scsiback_drop_nexus(tpg); 1761 return (!ret) ? count : ret; 1762 } 1763 /* 1764 * Otherwise make sure the passed virtual Initiator port WWN matches 1765 * the fabric protocol_id set in scsiback_make_tport(), and call 1766 * scsiback_make_nexus(). 1767 */ 1768 if (strlen(page) >= VSCSI_NAMELEN) { 1769 pr_err("Emulated NAA Sas Address: %s, exceeds max: %d\n", 1770 page, VSCSI_NAMELEN); 1771 return -EINVAL; 1772 } 1773 snprintf(&i_port[0], VSCSI_NAMELEN, "%s", page); 1774 1775 ptr = strstr(i_port, "naa."); 1776 if (ptr) { 1777 if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_SAS) { 1778 pr_err("Passed SAS Initiator Port %s does not match target port protoid: %s\n", 1779 i_port, scsiback_dump_proto_id(tport_wwn)); 1780 return -EINVAL; 1781 } 1782 port_ptr = &i_port[0]; 1783 goto check_newline; 1784 } 1785 ptr = strstr(i_port, "fc."); 1786 if (ptr) { 1787 if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_FCP) { 1788 pr_err("Passed FCP Initiator Port %s does not match target port protoid: %s\n", 1789 i_port, scsiback_dump_proto_id(tport_wwn)); 1790 return -EINVAL; 1791 } 1792 port_ptr = &i_port[3]; /* Skip over "fc." */ 1793 goto check_newline; 1794 } 1795 ptr = strstr(i_port, "iqn."); 1796 if (ptr) { 1797 if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_ISCSI) { 1798 pr_err("Passed iSCSI Initiator Port %s does not match target port protoid: %s\n", 1799 i_port, scsiback_dump_proto_id(tport_wwn)); 1800 return -EINVAL; 1801 } 1802 port_ptr = &i_port[0]; 1803 goto check_newline; 1804 } 1805 pr_err("Unable to locate prefix for emulated Initiator Port: %s\n", 1806 i_port); 1807 return -EINVAL; 1808 /* 1809 * Clear any trailing newline for the NAA WWN 1810 */ 1811 check_newline: 1812 if (i_port[strlen(i_port) - 1] == '\n') 1813 i_port[strlen(i_port) - 1] = '\0'; 1814 1815 ret = scsiback_make_nexus(tpg, port_ptr); 1816 if (ret < 0) 1817 return ret; 1818 1819 return count; 1820 } 1821 1822 TF_TPG_BASE_ATTR(scsiback, nexus, S_IRUGO | S_IWUSR); 1823 1824 static struct configfs_attribute *scsiback_tpg_attrs[] = { 1825 &scsiback_tpg_nexus.attr, 1826 NULL, 1827 }; 1828 1829 static ssize_t 1830 scsiback_wwn_show_attr_version(struct target_fabric_configfs *tf, 1831 char *page) 1832 { 1833 return sprintf(page, "xen-pvscsi fabric module %s on %s/%s on " 1834 UTS_RELEASE"\n", 1835 VSCSI_VERSION, utsname()->sysname, utsname()->machine); 1836 } 1837 1838 TF_WWN_ATTR_RO(scsiback, version); 1839 1840 static struct configfs_attribute *scsiback_wwn_attrs[] = { 1841 &scsiback_wwn_version.attr, 1842 NULL, 1843 }; 1844 1845 static char *scsiback_get_fabric_name(void) 1846 { 1847 return "xen-pvscsi"; 1848 } 1849 1850 static int scsiback_port_link(struct se_portal_group *se_tpg, 1851 struct se_lun *lun) 1852 { 1853 struct scsiback_tpg *tpg = container_of(se_tpg, 1854 struct scsiback_tpg, se_tpg); 1855 1856 mutex_lock(&tpg->tv_tpg_mutex); 1857 tpg->tv_tpg_port_count++; 1858 mutex_unlock(&tpg->tv_tpg_mutex); 1859 1860 return 0; 1861 } 1862 1863 static void scsiback_port_unlink(struct se_portal_group *se_tpg, 1864 struct se_lun *lun) 1865 { 1866 struct scsiback_tpg *tpg = container_of(se_tpg, 1867 struct scsiback_tpg, se_tpg); 1868 1869 mutex_lock(&tpg->tv_tpg_mutex); 1870 tpg->tv_tpg_port_count--; 1871 mutex_unlock(&tpg->tv_tpg_mutex); 1872 } 1873 1874 static struct se_portal_group * 1875 scsiback_make_tpg(struct se_wwn *wwn, 1876 struct config_group *group, 1877 const char *name) 1878 { 1879 struct scsiback_tport *tport = container_of(wwn, 1880 struct scsiback_tport, tport_wwn); 1881 1882 struct scsiback_tpg *tpg; 1883 u16 tpgt; 1884 int ret; 1885 1886 if (strstr(name, "tpgt_") != name) 1887 return ERR_PTR(-EINVAL); 1888 ret = kstrtou16(name + 5, 10, &tpgt); 1889 if (ret) 1890 return ERR_PTR(ret); 1891 1892 tpg = kzalloc(sizeof(struct scsiback_tpg), GFP_KERNEL); 1893 if (!tpg) 1894 return ERR_PTR(-ENOMEM); 1895 1896 mutex_init(&tpg->tv_tpg_mutex); 1897 INIT_LIST_HEAD(&tpg->tv_tpg_list); 1898 INIT_LIST_HEAD(&tpg->info_list); 1899 tpg->tport = tport; 1900 tpg->tport_tpgt = tpgt; 1901 1902 ret = core_tpg_register(&scsiback_fabric_configfs->tf_ops, wwn, 1903 &tpg->se_tpg, tpg, TRANSPORT_TPG_TYPE_NORMAL); 1904 if (ret < 0) { 1905 kfree(tpg); 1906 return NULL; 1907 } 1908 mutex_lock(&scsiback_mutex); 1909 list_add_tail(&tpg->tv_tpg_list, &scsiback_list); 1910 mutex_unlock(&scsiback_mutex); 1911 1912 return &tpg->se_tpg; 1913 } 1914 1915 static void scsiback_drop_tpg(struct se_portal_group *se_tpg) 1916 { 1917 struct scsiback_tpg *tpg = container_of(se_tpg, 1918 struct scsiback_tpg, se_tpg); 1919 1920 mutex_lock(&scsiback_mutex); 1921 list_del(&tpg->tv_tpg_list); 1922 mutex_unlock(&scsiback_mutex); 1923 /* 1924 * Release the virtual I_T Nexus for this xen-pvscsi TPG 1925 */ 1926 scsiback_drop_nexus(tpg); 1927 /* 1928 * Deregister the se_tpg from TCM.. 1929 */ 1930 core_tpg_deregister(se_tpg); 1931 kfree(tpg); 1932 } 1933 1934 static int scsiback_check_true(struct se_portal_group *se_tpg) 1935 { 1936 return 1; 1937 } 1938 1939 static int scsiback_check_false(struct se_portal_group *se_tpg) 1940 { 1941 return 0; 1942 } 1943 1944 static struct target_core_fabric_ops scsiback_ops = { 1945 .get_fabric_name = scsiback_get_fabric_name, 1946 .get_fabric_proto_ident = scsiback_get_fabric_proto_ident, 1947 .tpg_get_wwn = scsiback_get_fabric_wwn, 1948 .tpg_get_tag = scsiback_get_tag, 1949 .tpg_get_default_depth = scsiback_get_default_depth, 1950 .tpg_get_pr_transport_id = scsiback_get_pr_transport_id, 1951 .tpg_get_pr_transport_id_len = scsiback_get_pr_transport_id_len, 1952 .tpg_parse_pr_out_transport_id = scsiback_parse_pr_out_transport_id, 1953 .tpg_check_demo_mode = scsiback_check_true, 1954 .tpg_check_demo_mode_cache = scsiback_check_true, 1955 .tpg_check_demo_mode_write_protect = scsiback_check_false, 1956 .tpg_check_prod_mode_write_protect = scsiback_check_false, 1957 .tpg_alloc_fabric_acl = scsiback_alloc_fabric_acl, 1958 .tpg_release_fabric_acl = scsiback_release_fabric_acl, 1959 .tpg_get_inst_index = scsiback_tpg_get_inst_index, 1960 .check_stop_free = scsiback_check_stop_free, 1961 .release_cmd = scsiback_release_cmd, 1962 .put_session = NULL, 1963 .shutdown_session = scsiback_shutdown_session, 1964 .close_session = scsiback_close_session, 1965 .sess_get_index = scsiback_sess_get_index, 1966 .sess_get_initiator_sid = NULL, 1967 .write_pending = scsiback_write_pending, 1968 .write_pending_status = scsiback_write_pending_status, 1969 .set_default_node_attributes = scsiback_set_default_node_attrs, 1970 .get_task_tag = scsiback_get_task_tag, 1971 .get_cmd_state = scsiback_get_cmd_state, 1972 .queue_data_in = scsiback_queue_data_in, 1973 .queue_status = scsiback_queue_status, 1974 .queue_tm_rsp = scsiback_queue_tm_rsp, 1975 .aborted_task = scsiback_aborted_task, 1976 /* 1977 * Setup callers for generic logic in target_core_fabric_configfs.c 1978 */ 1979 .fabric_make_wwn = scsiback_make_tport, 1980 .fabric_drop_wwn = scsiback_drop_tport, 1981 .fabric_make_tpg = scsiback_make_tpg, 1982 .fabric_drop_tpg = scsiback_drop_tpg, 1983 .fabric_post_link = scsiback_port_link, 1984 .fabric_pre_unlink = scsiback_port_unlink, 1985 .fabric_make_np = NULL, 1986 .fabric_drop_np = NULL, 1987 #if 0 1988 .fabric_make_nodeacl = scsiback_make_nodeacl, 1989 .fabric_drop_nodeacl = scsiback_drop_nodeacl, 1990 #endif 1991 }; 1992 1993 static int scsiback_register_configfs(void) 1994 { 1995 struct target_fabric_configfs *fabric; 1996 int ret; 1997 1998 pr_debug("xen-pvscsi: fabric module %s on %s/%s on "UTS_RELEASE"\n", 1999 VSCSI_VERSION, utsname()->sysname, utsname()->machine); 2000 /* 2001 * Register the top level struct config_item_type with TCM core 2002 */ 2003 fabric = target_fabric_configfs_init(THIS_MODULE, "xen-pvscsi"); 2004 if (IS_ERR(fabric)) 2005 return PTR_ERR(fabric); 2006 2007 /* 2008 * Setup fabric->tf_ops from our local scsiback_ops 2009 */ 2010 fabric->tf_ops = scsiback_ops; 2011 /* 2012 * Setup default attribute lists for various fabric->tf_cit_tmpl 2013 */ 2014 fabric->tf_cit_tmpl.tfc_wwn_cit.ct_attrs = scsiback_wwn_attrs; 2015 fabric->tf_cit_tmpl.tfc_tpg_base_cit.ct_attrs = scsiback_tpg_attrs; 2016 fabric->tf_cit_tmpl.tfc_tpg_attrib_cit.ct_attrs = NULL; 2017 fabric->tf_cit_tmpl.tfc_tpg_param_cit.ct_attrs = scsiback_param_attrs; 2018 fabric->tf_cit_tmpl.tfc_tpg_np_base_cit.ct_attrs = NULL; 2019 fabric->tf_cit_tmpl.tfc_tpg_nacl_base_cit.ct_attrs = NULL; 2020 fabric->tf_cit_tmpl.tfc_tpg_nacl_attrib_cit.ct_attrs = NULL; 2021 fabric->tf_cit_tmpl.tfc_tpg_nacl_auth_cit.ct_attrs = NULL; 2022 fabric->tf_cit_tmpl.tfc_tpg_nacl_param_cit.ct_attrs = NULL; 2023 /* 2024 * Register the fabric for use within TCM 2025 */ 2026 ret = target_fabric_configfs_register(fabric); 2027 if (ret < 0) { 2028 target_fabric_configfs_free(fabric); 2029 return ret; 2030 } 2031 /* 2032 * Setup our local pointer to *fabric 2033 */ 2034 scsiback_fabric_configfs = fabric; 2035 pr_debug("xen-pvscsi: Set fabric -> scsiback_fabric_configfs\n"); 2036 return 0; 2037 }; 2038 2039 static void scsiback_deregister_configfs(void) 2040 { 2041 if (!scsiback_fabric_configfs) 2042 return; 2043 2044 target_fabric_configfs_deregister(scsiback_fabric_configfs); 2045 scsiback_fabric_configfs = NULL; 2046 pr_debug("xen-pvscsi: Cleared scsiback_fabric_configfs\n"); 2047 }; 2048 2049 static const struct xenbus_device_id scsiback_ids[] = { 2050 { "vscsi" }, 2051 { "" } 2052 }; 2053 2054 static struct xenbus_driver scsiback_driver = { 2055 .ids = scsiback_ids, 2056 .probe = scsiback_probe, 2057 .remove = scsiback_remove, 2058 .otherend_changed = scsiback_frontend_changed 2059 }; 2060 2061 static void scsiback_init_pend(void *p) 2062 { 2063 struct vscsibk_pend *pend = p; 2064 int i; 2065 2066 memset(pend, 0, sizeof(*pend)); 2067 for (i = 0; i < VSCSI_MAX_GRANTS; i++) 2068 pend->grant_handles[i] = SCSIBACK_INVALID_HANDLE; 2069 } 2070 2071 static int __init scsiback_init(void) 2072 { 2073 int ret; 2074 2075 if (!xen_domain()) 2076 return -ENODEV; 2077 2078 scsiback_cachep = kmem_cache_create("vscsiif_cache", 2079 sizeof(struct vscsibk_pend), 0, 0, scsiback_init_pend); 2080 if (!scsiback_cachep) 2081 return -ENOMEM; 2082 2083 ret = xenbus_register_backend(&scsiback_driver); 2084 if (ret) 2085 goto out_cache_destroy; 2086 2087 ret = scsiback_register_configfs(); 2088 if (ret) 2089 goto out_unregister_xenbus; 2090 2091 return 0; 2092 2093 out_unregister_xenbus: 2094 xenbus_unregister_driver(&scsiback_driver); 2095 out_cache_destroy: 2096 kmem_cache_destroy(scsiback_cachep); 2097 pr_err("xen-pvscsi: %s: error %d\n", __func__, ret); 2098 return ret; 2099 } 2100 2101 static void __exit scsiback_exit(void) 2102 { 2103 struct page *page; 2104 2105 while (free_pages_num) { 2106 if (get_free_page(&page)) 2107 BUG(); 2108 gnttab_free_pages(1, &page); 2109 } 2110 scsiback_deregister_configfs(); 2111 xenbus_unregister_driver(&scsiback_driver); 2112 kmem_cache_destroy(scsiback_cachep); 2113 } 2114 2115 module_init(scsiback_init); 2116 module_exit(scsiback_exit); 2117 2118 MODULE_DESCRIPTION("Xen SCSI backend driver"); 2119 MODULE_LICENSE("Dual BSD/GPL"); 2120 MODULE_ALIAS("xen-backend:vscsi"); 2121 MODULE_AUTHOR("Juergen Gross <jgross@suse.com>"); 2122