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