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