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