1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * SBP2 target driver (SCSI over IEEE1394 in target mode) 4 * 5 * Copyright (C) 2011 Chris Boot <bootc@bootc.net> 6 */ 7 8 #define KMSG_COMPONENT "sbp_target" 9 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt 10 11 #include <linux/kernel.h> 12 #include <linux/module.h> 13 #include <linux/init.h> 14 #include <linux/types.h> 15 #include <linux/string.h> 16 #include <linux/configfs.h> 17 #include <linux/ctype.h> 18 #include <linux/delay.h> 19 #include <linux/firewire.h> 20 #include <linux/firewire-constants.h> 21 #include <scsi/scsi_proto.h> 22 #include <scsi/scsi_tcq.h> 23 #include <target/target_core_base.h> 24 #include <target/target_core_backend.h> 25 #include <target/target_core_fabric.h> 26 #include <asm/unaligned.h> 27 28 #include "sbp_target.h" 29 30 /* FireWire address region for management and command block address handlers */ 31 static const struct fw_address_region sbp_register_region = { 32 .start = CSR_REGISTER_BASE + 0x10000, 33 .end = 0x1000000000000ULL, 34 }; 35 36 static const u32 sbp_unit_directory_template[] = { 37 0x1200609e, /* unit_specifier_id: NCITS/T10 */ 38 0x13010483, /* unit_sw_version: 1155D Rev 4 */ 39 0x3800609e, /* command_set_specifier_id: NCITS/T10 */ 40 0x390104d8, /* command_set: SPC-2 */ 41 0x3b000000, /* command_set_revision: 0 */ 42 0x3c000001, /* firmware_revision: 1 */ 43 }; 44 45 #define SESSION_MAINTENANCE_INTERVAL HZ 46 47 static atomic_t login_id = ATOMIC_INIT(0); 48 49 static void session_maintenance_work(struct work_struct *); 50 static int sbp_run_transaction(struct fw_card *, int, int, int, int, 51 unsigned long long, void *, size_t); 52 53 static int read_peer_guid(u64 *guid, const struct sbp_management_request *req) 54 { 55 int ret; 56 __be32 high, low; 57 58 ret = sbp_run_transaction(req->card, TCODE_READ_QUADLET_REQUEST, 59 req->node_addr, req->generation, req->speed, 60 (CSR_REGISTER_BASE | CSR_CONFIG_ROM) + 3 * 4, 61 &high, sizeof(high)); 62 if (ret != RCODE_COMPLETE) 63 return ret; 64 65 ret = sbp_run_transaction(req->card, TCODE_READ_QUADLET_REQUEST, 66 req->node_addr, req->generation, req->speed, 67 (CSR_REGISTER_BASE | CSR_CONFIG_ROM) + 4 * 4, 68 &low, sizeof(low)); 69 if (ret != RCODE_COMPLETE) 70 return ret; 71 72 *guid = (u64)be32_to_cpu(high) << 32 | be32_to_cpu(low); 73 74 return RCODE_COMPLETE; 75 } 76 77 static struct sbp_session *sbp_session_find_by_guid( 78 struct sbp_tpg *tpg, u64 guid) 79 { 80 struct se_session *se_sess; 81 struct sbp_session *sess, *found = NULL; 82 83 spin_lock_bh(&tpg->se_tpg.session_lock); 84 list_for_each_entry(se_sess, &tpg->se_tpg.tpg_sess_list, sess_list) { 85 sess = se_sess->fabric_sess_ptr; 86 if (sess->guid == guid) 87 found = sess; 88 } 89 spin_unlock_bh(&tpg->se_tpg.session_lock); 90 91 return found; 92 } 93 94 static struct sbp_login_descriptor *sbp_login_find_by_lun( 95 struct sbp_session *session, u32 unpacked_lun) 96 { 97 struct sbp_login_descriptor *login, *found = NULL; 98 99 spin_lock_bh(&session->lock); 100 list_for_each_entry(login, &session->login_list, link) { 101 if (login->login_lun == unpacked_lun) 102 found = login; 103 } 104 spin_unlock_bh(&session->lock); 105 106 return found; 107 } 108 109 static int sbp_login_count_all_by_lun( 110 struct sbp_tpg *tpg, 111 u32 unpacked_lun, 112 int exclusive) 113 { 114 struct se_session *se_sess; 115 struct sbp_session *sess; 116 struct sbp_login_descriptor *login; 117 int count = 0; 118 119 spin_lock_bh(&tpg->se_tpg.session_lock); 120 list_for_each_entry(se_sess, &tpg->se_tpg.tpg_sess_list, sess_list) { 121 sess = se_sess->fabric_sess_ptr; 122 123 spin_lock_bh(&sess->lock); 124 list_for_each_entry(login, &sess->login_list, link) { 125 if (login->login_lun != unpacked_lun) 126 continue; 127 128 if (!exclusive || login->exclusive) 129 count++; 130 } 131 spin_unlock_bh(&sess->lock); 132 } 133 spin_unlock_bh(&tpg->se_tpg.session_lock); 134 135 return count; 136 } 137 138 static struct sbp_login_descriptor *sbp_login_find_by_id( 139 struct sbp_tpg *tpg, int login_id) 140 { 141 struct se_session *se_sess; 142 struct sbp_session *sess; 143 struct sbp_login_descriptor *login, *found = NULL; 144 145 spin_lock_bh(&tpg->se_tpg.session_lock); 146 list_for_each_entry(se_sess, &tpg->se_tpg.tpg_sess_list, sess_list) { 147 sess = se_sess->fabric_sess_ptr; 148 149 spin_lock_bh(&sess->lock); 150 list_for_each_entry(login, &sess->login_list, link) { 151 if (login->login_id == login_id) 152 found = login; 153 } 154 spin_unlock_bh(&sess->lock); 155 } 156 spin_unlock_bh(&tpg->se_tpg.session_lock); 157 158 return found; 159 } 160 161 static u32 sbp_get_lun_from_tpg(struct sbp_tpg *tpg, u32 login_lun, int *err) 162 { 163 struct se_portal_group *se_tpg = &tpg->se_tpg; 164 struct se_lun *se_lun; 165 166 rcu_read_lock(); 167 hlist_for_each_entry_rcu(se_lun, &se_tpg->tpg_lun_hlist, link) { 168 if (se_lun->unpacked_lun == login_lun) { 169 rcu_read_unlock(); 170 *err = 0; 171 return login_lun; 172 } 173 } 174 rcu_read_unlock(); 175 176 *err = -ENODEV; 177 return login_lun; 178 } 179 180 static struct sbp_session *sbp_session_create( 181 struct sbp_tpg *tpg, 182 u64 guid) 183 { 184 struct sbp_session *sess; 185 int ret; 186 char guid_str[17]; 187 188 snprintf(guid_str, sizeof(guid_str), "%016llx", guid); 189 190 sess = kmalloc(sizeof(*sess), GFP_KERNEL); 191 if (!sess) 192 return ERR_PTR(-ENOMEM); 193 194 spin_lock_init(&sess->lock); 195 INIT_LIST_HEAD(&sess->login_list); 196 INIT_DELAYED_WORK(&sess->maint_work, session_maintenance_work); 197 sess->guid = guid; 198 199 sess->se_sess = target_setup_session(&tpg->se_tpg, 128, 200 sizeof(struct sbp_target_request), 201 TARGET_PROT_NORMAL, guid_str, 202 sess, NULL); 203 if (IS_ERR(sess->se_sess)) { 204 pr_err("failed to init se_session\n"); 205 ret = PTR_ERR(sess->se_sess); 206 kfree(sess); 207 return ERR_PTR(ret); 208 } 209 210 return sess; 211 } 212 213 static void sbp_session_release(struct sbp_session *sess, bool cancel_work) 214 { 215 spin_lock_bh(&sess->lock); 216 if (!list_empty(&sess->login_list)) { 217 spin_unlock_bh(&sess->lock); 218 return; 219 } 220 spin_unlock_bh(&sess->lock); 221 222 if (cancel_work) 223 cancel_delayed_work_sync(&sess->maint_work); 224 225 target_remove_session(sess->se_sess); 226 227 if (sess->card) 228 fw_card_put(sess->card); 229 230 kfree(sess); 231 } 232 233 static void sbp_target_agent_unregister(struct sbp_target_agent *); 234 235 static void sbp_login_release(struct sbp_login_descriptor *login, 236 bool cancel_work) 237 { 238 struct sbp_session *sess = login->sess; 239 240 /* FIXME: abort/wait on tasks */ 241 242 sbp_target_agent_unregister(login->tgt_agt); 243 244 if (sess) { 245 spin_lock_bh(&sess->lock); 246 list_del(&login->link); 247 spin_unlock_bh(&sess->lock); 248 249 sbp_session_release(sess, cancel_work); 250 } 251 252 kfree(login); 253 } 254 255 static struct sbp_target_agent *sbp_target_agent_register( 256 struct sbp_login_descriptor *); 257 258 static void sbp_management_request_login( 259 struct sbp_management_agent *agent, struct sbp_management_request *req, 260 int *status_data_size) 261 { 262 struct sbp_tport *tport = agent->tport; 263 struct sbp_tpg *tpg = tport->tpg; 264 struct sbp_session *sess; 265 struct sbp_login_descriptor *login; 266 struct sbp_login_response_block *response; 267 u64 guid; 268 u32 unpacked_lun; 269 int login_response_len, ret; 270 271 unpacked_lun = sbp_get_lun_from_tpg(tpg, 272 LOGIN_ORB_LUN(be32_to_cpu(req->orb.misc)), &ret); 273 if (ret) { 274 pr_notice("login to unknown LUN: %d\n", 275 LOGIN_ORB_LUN(be32_to_cpu(req->orb.misc))); 276 277 req->status.status = cpu_to_be32( 278 STATUS_BLOCK_RESP(STATUS_RESP_REQUEST_COMPLETE) | 279 STATUS_BLOCK_SBP_STATUS(SBP_STATUS_LUN_NOTSUPP)); 280 return; 281 } 282 283 ret = read_peer_guid(&guid, req); 284 if (ret != RCODE_COMPLETE) { 285 pr_warn("failed to read peer GUID: %d\n", ret); 286 287 req->status.status = cpu_to_be32( 288 STATUS_BLOCK_RESP(STATUS_RESP_TRANSPORT_FAILURE) | 289 STATUS_BLOCK_SBP_STATUS(SBP_STATUS_UNSPECIFIED_ERROR)); 290 return; 291 } 292 293 pr_notice("mgt_agent LOGIN to LUN %d from %016llx\n", 294 unpacked_lun, guid); 295 296 sess = sbp_session_find_by_guid(tpg, guid); 297 if (sess) { 298 login = sbp_login_find_by_lun(sess, unpacked_lun); 299 if (login) { 300 pr_notice("initiator already logged-in\n"); 301 302 /* 303 * SBP-2 R4 says we should return access denied, but 304 * that can confuse initiators. Instead we need to 305 * treat this like a reconnect, but send the login 306 * response block like a fresh login. 307 * 308 * This is required particularly in the case of Apple 309 * devices booting off the FireWire target, where 310 * the firmware has an active login to the target. When 311 * the OS takes control of the session it issues its own 312 * LOGIN rather than a RECONNECT. To avoid the machine 313 * waiting until the reconnect_hold expires, we can skip 314 * the ACCESS_DENIED errors to speed things up. 315 */ 316 317 goto already_logged_in; 318 } 319 } 320 321 /* 322 * check exclusive bit in login request 323 * reject with access_denied if any logins present 324 */ 325 if (LOGIN_ORB_EXCLUSIVE(be32_to_cpu(req->orb.misc)) && 326 sbp_login_count_all_by_lun(tpg, unpacked_lun, 0)) { 327 pr_warn("refusing exclusive login with other active logins\n"); 328 329 req->status.status = cpu_to_be32( 330 STATUS_BLOCK_RESP(STATUS_RESP_REQUEST_COMPLETE) | 331 STATUS_BLOCK_SBP_STATUS(SBP_STATUS_ACCESS_DENIED)); 332 return; 333 } 334 335 /* 336 * check exclusive bit in any existing login descriptor 337 * reject with access_denied if any exclusive logins present 338 */ 339 if (sbp_login_count_all_by_lun(tpg, unpacked_lun, 1)) { 340 pr_warn("refusing login while another exclusive login present\n"); 341 342 req->status.status = cpu_to_be32( 343 STATUS_BLOCK_RESP(STATUS_RESP_REQUEST_COMPLETE) | 344 STATUS_BLOCK_SBP_STATUS(SBP_STATUS_ACCESS_DENIED)); 345 return; 346 } 347 348 /* 349 * check we haven't exceeded the number of allowed logins 350 * reject with resources_unavailable if we have 351 */ 352 if (sbp_login_count_all_by_lun(tpg, unpacked_lun, 0) >= 353 tport->max_logins_per_lun) { 354 pr_warn("max number of logins reached\n"); 355 356 req->status.status = cpu_to_be32( 357 STATUS_BLOCK_RESP(STATUS_RESP_REQUEST_COMPLETE) | 358 STATUS_BLOCK_SBP_STATUS(SBP_STATUS_RESOURCES_UNAVAIL)); 359 return; 360 } 361 362 if (!sess) { 363 sess = sbp_session_create(tpg, guid); 364 if (IS_ERR(sess)) { 365 switch (PTR_ERR(sess)) { 366 case -EPERM: 367 ret = SBP_STATUS_ACCESS_DENIED; 368 break; 369 default: 370 ret = SBP_STATUS_RESOURCES_UNAVAIL; 371 break; 372 } 373 374 req->status.status = cpu_to_be32( 375 STATUS_BLOCK_RESP( 376 STATUS_RESP_REQUEST_COMPLETE) | 377 STATUS_BLOCK_SBP_STATUS(ret)); 378 return; 379 } 380 381 sess->node_id = req->node_addr; 382 sess->card = fw_card_get(req->card); 383 sess->generation = req->generation; 384 sess->speed = req->speed; 385 386 schedule_delayed_work(&sess->maint_work, 387 SESSION_MAINTENANCE_INTERVAL); 388 } 389 390 /* only take the latest reconnect_hold into account */ 391 sess->reconnect_hold = min( 392 1 << LOGIN_ORB_RECONNECT(be32_to_cpu(req->orb.misc)), 393 tport->max_reconnect_timeout) - 1; 394 395 login = kmalloc(sizeof(*login), GFP_KERNEL); 396 if (!login) { 397 pr_err("failed to allocate login descriptor\n"); 398 399 sbp_session_release(sess, true); 400 401 req->status.status = cpu_to_be32( 402 STATUS_BLOCK_RESP(STATUS_RESP_REQUEST_COMPLETE) | 403 STATUS_BLOCK_SBP_STATUS(SBP_STATUS_RESOURCES_UNAVAIL)); 404 return; 405 } 406 407 login->sess = sess; 408 login->login_lun = unpacked_lun; 409 login->status_fifo_addr = sbp2_pointer_to_addr(&req->orb.status_fifo); 410 login->exclusive = LOGIN_ORB_EXCLUSIVE(be32_to_cpu(req->orb.misc)); 411 login->login_id = atomic_inc_return(&login_id); 412 413 login->tgt_agt = sbp_target_agent_register(login); 414 if (IS_ERR(login->tgt_agt)) { 415 ret = PTR_ERR(login->tgt_agt); 416 pr_err("failed to map command block handler: %d\n", ret); 417 418 sbp_session_release(sess, true); 419 kfree(login); 420 421 req->status.status = cpu_to_be32( 422 STATUS_BLOCK_RESP(STATUS_RESP_REQUEST_COMPLETE) | 423 STATUS_BLOCK_SBP_STATUS(SBP_STATUS_RESOURCES_UNAVAIL)); 424 return; 425 } 426 427 spin_lock_bh(&sess->lock); 428 list_add_tail(&login->link, &sess->login_list); 429 spin_unlock_bh(&sess->lock); 430 431 already_logged_in: 432 response = kzalloc(sizeof(*response), GFP_KERNEL); 433 if (!response) { 434 pr_err("failed to allocate login response block\n"); 435 436 sbp_login_release(login, true); 437 438 req->status.status = cpu_to_be32( 439 STATUS_BLOCK_RESP(STATUS_RESP_REQUEST_COMPLETE) | 440 STATUS_BLOCK_SBP_STATUS(SBP_STATUS_RESOURCES_UNAVAIL)); 441 return; 442 } 443 444 login_response_len = clamp_val( 445 LOGIN_ORB_RESPONSE_LENGTH(be32_to_cpu(req->orb.length)), 446 12, sizeof(*response)); 447 response->misc = cpu_to_be32( 448 ((login_response_len & 0xffff) << 16) | 449 (login->login_id & 0xffff)); 450 response->reconnect_hold = cpu_to_be32(sess->reconnect_hold & 0xffff); 451 addr_to_sbp2_pointer(login->tgt_agt->handler.offset, 452 &response->command_block_agent); 453 454 ret = sbp_run_transaction(sess->card, TCODE_WRITE_BLOCK_REQUEST, 455 sess->node_id, sess->generation, sess->speed, 456 sbp2_pointer_to_addr(&req->orb.ptr2), response, 457 login_response_len); 458 if (ret != RCODE_COMPLETE) { 459 pr_debug("failed to write login response block: %x\n", ret); 460 461 kfree(response); 462 sbp_login_release(login, true); 463 464 req->status.status = cpu_to_be32( 465 STATUS_BLOCK_RESP(STATUS_RESP_TRANSPORT_FAILURE) | 466 STATUS_BLOCK_SBP_STATUS(SBP_STATUS_UNSPECIFIED_ERROR)); 467 return; 468 } 469 470 kfree(response); 471 472 req->status.status = cpu_to_be32( 473 STATUS_BLOCK_RESP(STATUS_RESP_REQUEST_COMPLETE) | 474 STATUS_BLOCK_SBP_STATUS(SBP_STATUS_OK)); 475 } 476 477 static void sbp_management_request_query_logins( 478 struct sbp_management_agent *agent, struct sbp_management_request *req, 479 int *status_data_size) 480 { 481 pr_notice("QUERY LOGINS not implemented\n"); 482 /* FIXME: implement */ 483 484 req->status.status = cpu_to_be32( 485 STATUS_BLOCK_RESP(STATUS_RESP_REQUEST_COMPLETE) | 486 STATUS_BLOCK_SBP_STATUS(SBP_STATUS_REQ_TYPE_NOTSUPP)); 487 } 488 489 static void sbp_management_request_reconnect( 490 struct sbp_management_agent *agent, struct sbp_management_request *req, 491 int *status_data_size) 492 { 493 struct sbp_tport *tport = agent->tport; 494 struct sbp_tpg *tpg = tport->tpg; 495 int ret; 496 u64 guid; 497 struct sbp_login_descriptor *login; 498 499 ret = read_peer_guid(&guid, req); 500 if (ret != RCODE_COMPLETE) { 501 pr_warn("failed to read peer GUID: %d\n", ret); 502 503 req->status.status = cpu_to_be32( 504 STATUS_BLOCK_RESP(STATUS_RESP_TRANSPORT_FAILURE) | 505 STATUS_BLOCK_SBP_STATUS(SBP_STATUS_UNSPECIFIED_ERROR)); 506 return; 507 } 508 509 pr_notice("mgt_agent RECONNECT from %016llx\n", guid); 510 511 login = sbp_login_find_by_id(tpg, 512 RECONNECT_ORB_LOGIN_ID(be32_to_cpu(req->orb.misc))); 513 514 if (!login) { 515 pr_err("mgt_agent RECONNECT unknown login ID\n"); 516 517 req->status.status = cpu_to_be32( 518 STATUS_BLOCK_RESP(STATUS_RESP_REQUEST_COMPLETE) | 519 STATUS_BLOCK_SBP_STATUS(SBP_STATUS_ACCESS_DENIED)); 520 return; 521 } 522 523 if (login->sess->guid != guid) { 524 pr_err("mgt_agent RECONNECT login GUID doesn't match\n"); 525 526 req->status.status = cpu_to_be32( 527 STATUS_BLOCK_RESP(STATUS_RESP_REQUEST_COMPLETE) | 528 STATUS_BLOCK_SBP_STATUS(SBP_STATUS_ACCESS_DENIED)); 529 return; 530 } 531 532 spin_lock_bh(&login->sess->lock); 533 if (login->sess->card) 534 fw_card_put(login->sess->card); 535 536 /* update the node details */ 537 login->sess->generation = req->generation; 538 login->sess->node_id = req->node_addr; 539 login->sess->card = fw_card_get(req->card); 540 login->sess->speed = req->speed; 541 spin_unlock_bh(&login->sess->lock); 542 543 req->status.status = cpu_to_be32( 544 STATUS_BLOCK_RESP(STATUS_RESP_REQUEST_COMPLETE) | 545 STATUS_BLOCK_SBP_STATUS(SBP_STATUS_OK)); 546 } 547 548 static void sbp_management_request_logout( 549 struct sbp_management_agent *agent, struct sbp_management_request *req, 550 int *status_data_size) 551 { 552 struct sbp_tport *tport = agent->tport; 553 struct sbp_tpg *tpg = tport->tpg; 554 int id; 555 struct sbp_login_descriptor *login; 556 557 id = LOGOUT_ORB_LOGIN_ID(be32_to_cpu(req->orb.misc)); 558 559 login = sbp_login_find_by_id(tpg, id); 560 if (!login) { 561 pr_warn("cannot find login: %d\n", id); 562 563 req->status.status = cpu_to_be32( 564 STATUS_BLOCK_RESP(STATUS_RESP_REQUEST_COMPLETE) | 565 STATUS_BLOCK_SBP_STATUS(SBP_STATUS_LOGIN_ID_UNKNOWN)); 566 return; 567 } 568 569 pr_info("mgt_agent LOGOUT from LUN %d session %d\n", 570 login->login_lun, login->login_id); 571 572 if (req->node_addr != login->sess->node_id) { 573 pr_warn("logout from different node ID\n"); 574 575 req->status.status = cpu_to_be32( 576 STATUS_BLOCK_RESP(STATUS_RESP_REQUEST_COMPLETE) | 577 STATUS_BLOCK_SBP_STATUS(SBP_STATUS_ACCESS_DENIED)); 578 return; 579 } 580 581 sbp_login_release(login, true); 582 583 req->status.status = cpu_to_be32( 584 STATUS_BLOCK_RESP(STATUS_RESP_REQUEST_COMPLETE) | 585 STATUS_BLOCK_SBP_STATUS(SBP_STATUS_OK)); 586 } 587 588 static void session_check_for_reset(struct sbp_session *sess) 589 { 590 bool card_valid = false; 591 592 spin_lock_bh(&sess->lock); 593 594 if (sess->card) { 595 spin_lock_irq(&sess->card->lock); 596 card_valid = (sess->card->local_node != NULL); 597 spin_unlock_irq(&sess->card->lock); 598 599 if (!card_valid) { 600 fw_card_put(sess->card); 601 sess->card = NULL; 602 } 603 } 604 605 if (!card_valid || (sess->generation != sess->card->generation)) { 606 pr_info("Waiting for reconnect from node: %016llx\n", 607 sess->guid); 608 609 sess->node_id = -1; 610 sess->reconnect_expires = get_jiffies_64() + 611 ((sess->reconnect_hold + 1) * HZ); 612 } 613 614 spin_unlock_bh(&sess->lock); 615 } 616 617 static void session_reconnect_expired(struct sbp_session *sess) 618 { 619 struct sbp_login_descriptor *login, *temp; 620 LIST_HEAD(login_list); 621 622 pr_info("Reconnect timer expired for node: %016llx\n", sess->guid); 623 624 spin_lock_bh(&sess->lock); 625 list_for_each_entry_safe(login, temp, &sess->login_list, link) { 626 login->sess = NULL; 627 list_move_tail(&login->link, &login_list); 628 } 629 spin_unlock_bh(&sess->lock); 630 631 list_for_each_entry_safe(login, temp, &login_list, link) { 632 list_del(&login->link); 633 sbp_login_release(login, false); 634 } 635 636 sbp_session_release(sess, false); 637 } 638 639 static void session_maintenance_work(struct work_struct *work) 640 { 641 struct sbp_session *sess = container_of(work, struct sbp_session, 642 maint_work.work); 643 644 /* could be called while tearing down the session */ 645 spin_lock_bh(&sess->lock); 646 if (list_empty(&sess->login_list)) { 647 spin_unlock_bh(&sess->lock); 648 return; 649 } 650 spin_unlock_bh(&sess->lock); 651 652 if (sess->node_id != -1) { 653 /* check for bus reset and make node_id invalid */ 654 session_check_for_reset(sess); 655 656 schedule_delayed_work(&sess->maint_work, 657 SESSION_MAINTENANCE_INTERVAL); 658 } else if (!time_after64(get_jiffies_64(), sess->reconnect_expires)) { 659 /* still waiting for reconnect */ 660 schedule_delayed_work(&sess->maint_work, 661 SESSION_MAINTENANCE_INTERVAL); 662 } else { 663 /* reconnect timeout has expired */ 664 session_reconnect_expired(sess); 665 } 666 } 667 668 static int tgt_agent_rw_agent_state(struct fw_card *card, int tcode, void *data, 669 struct sbp_target_agent *agent) 670 { 671 int state; 672 673 switch (tcode) { 674 case TCODE_READ_QUADLET_REQUEST: 675 pr_debug("tgt_agent AGENT_STATE READ\n"); 676 677 spin_lock_bh(&agent->lock); 678 state = agent->state; 679 spin_unlock_bh(&agent->lock); 680 681 *(__be32 *)data = cpu_to_be32(state); 682 683 return RCODE_COMPLETE; 684 685 case TCODE_WRITE_QUADLET_REQUEST: 686 /* ignored */ 687 return RCODE_COMPLETE; 688 689 default: 690 return RCODE_TYPE_ERROR; 691 } 692 } 693 694 static int tgt_agent_rw_agent_reset(struct fw_card *card, int tcode, void *data, 695 struct sbp_target_agent *agent) 696 { 697 switch (tcode) { 698 case TCODE_WRITE_QUADLET_REQUEST: 699 pr_debug("tgt_agent AGENT_RESET\n"); 700 spin_lock_bh(&agent->lock); 701 agent->state = AGENT_STATE_RESET; 702 spin_unlock_bh(&agent->lock); 703 return RCODE_COMPLETE; 704 705 default: 706 return RCODE_TYPE_ERROR; 707 } 708 } 709 710 static int tgt_agent_rw_orb_pointer(struct fw_card *card, int tcode, void *data, 711 struct sbp_target_agent *agent) 712 { 713 struct sbp2_pointer *ptr = data; 714 715 switch (tcode) { 716 case TCODE_WRITE_BLOCK_REQUEST: 717 spin_lock_bh(&agent->lock); 718 if (agent->state != AGENT_STATE_SUSPENDED && 719 agent->state != AGENT_STATE_RESET) { 720 spin_unlock_bh(&agent->lock); 721 pr_notice("Ignoring ORB_POINTER write while active.\n"); 722 return RCODE_CONFLICT_ERROR; 723 } 724 agent->state = AGENT_STATE_ACTIVE; 725 spin_unlock_bh(&agent->lock); 726 727 agent->orb_pointer = sbp2_pointer_to_addr(ptr); 728 agent->doorbell = false; 729 730 pr_debug("tgt_agent ORB_POINTER write: 0x%llx\n", 731 agent->orb_pointer); 732 733 queue_work(system_unbound_wq, &agent->work); 734 735 return RCODE_COMPLETE; 736 737 case TCODE_READ_BLOCK_REQUEST: 738 pr_debug("tgt_agent ORB_POINTER READ\n"); 739 spin_lock_bh(&agent->lock); 740 addr_to_sbp2_pointer(agent->orb_pointer, ptr); 741 spin_unlock_bh(&agent->lock); 742 return RCODE_COMPLETE; 743 744 default: 745 return RCODE_TYPE_ERROR; 746 } 747 } 748 749 static int tgt_agent_rw_doorbell(struct fw_card *card, int tcode, void *data, 750 struct sbp_target_agent *agent) 751 { 752 switch (tcode) { 753 case TCODE_WRITE_QUADLET_REQUEST: 754 spin_lock_bh(&agent->lock); 755 if (agent->state != AGENT_STATE_SUSPENDED) { 756 spin_unlock_bh(&agent->lock); 757 pr_debug("Ignoring DOORBELL while active.\n"); 758 return RCODE_CONFLICT_ERROR; 759 } 760 agent->state = AGENT_STATE_ACTIVE; 761 spin_unlock_bh(&agent->lock); 762 763 agent->doorbell = true; 764 765 pr_debug("tgt_agent DOORBELL\n"); 766 767 queue_work(system_unbound_wq, &agent->work); 768 769 return RCODE_COMPLETE; 770 771 case TCODE_READ_QUADLET_REQUEST: 772 return RCODE_COMPLETE; 773 774 default: 775 return RCODE_TYPE_ERROR; 776 } 777 } 778 779 static int tgt_agent_rw_unsolicited_status_enable(struct fw_card *card, 780 int tcode, void *data, struct sbp_target_agent *agent) 781 { 782 switch (tcode) { 783 case TCODE_WRITE_QUADLET_REQUEST: 784 pr_debug("tgt_agent UNSOLICITED_STATUS_ENABLE\n"); 785 /* ignored as we don't send unsolicited status */ 786 return RCODE_COMPLETE; 787 788 case TCODE_READ_QUADLET_REQUEST: 789 return RCODE_COMPLETE; 790 791 default: 792 return RCODE_TYPE_ERROR; 793 } 794 } 795 796 static void tgt_agent_rw(struct fw_card *card, struct fw_request *request, 797 int tcode, int destination, int source, int generation, 798 unsigned long long offset, void *data, size_t length, 799 void *callback_data) 800 { 801 struct sbp_target_agent *agent = callback_data; 802 struct sbp_session *sess = agent->login->sess; 803 int sess_gen, sess_node, rcode; 804 805 spin_lock_bh(&sess->lock); 806 sess_gen = sess->generation; 807 sess_node = sess->node_id; 808 spin_unlock_bh(&sess->lock); 809 810 if (generation != sess_gen) { 811 pr_notice("ignoring request with wrong generation\n"); 812 rcode = RCODE_TYPE_ERROR; 813 goto out; 814 } 815 816 if (source != sess_node) { 817 pr_notice("ignoring request from foreign node (%x != %x)\n", 818 source, sess_node); 819 rcode = RCODE_TYPE_ERROR; 820 goto out; 821 } 822 823 /* turn offset into the offset from the start of the block */ 824 offset -= agent->handler.offset; 825 826 if (offset == 0x00 && length == 4) { 827 /* AGENT_STATE */ 828 rcode = tgt_agent_rw_agent_state(card, tcode, data, agent); 829 } else if (offset == 0x04 && length == 4) { 830 /* AGENT_RESET */ 831 rcode = tgt_agent_rw_agent_reset(card, tcode, data, agent); 832 } else if (offset == 0x08 && length == 8) { 833 /* ORB_POINTER */ 834 rcode = tgt_agent_rw_orb_pointer(card, tcode, data, agent); 835 } else if (offset == 0x10 && length == 4) { 836 /* DOORBELL */ 837 rcode = tgt_agent_rw_doorbell(card, tcode, data, agent); 838 } else if (offset == 0x14 && length == 4) { 839 /* UNSOLICITED_STATUS_ENABLE */ 840 rcode = tgt_agent_rw_unsolicited_status_enable(card, tcode, 841 data, agent); 842 } else { 843 rcode = RCODE_ADDRESS_ERROR; 844 } 845 846 out: 847 fw_send_response(card, request, rcode); 848 } 849 850 static void sbp_handle_command(struct sbp_target_request *); 851 static int sbp_send_status(struct sbp_target_request *); 852 static void sbp_free_request(struct sbp_target_request *); 853 854 static void tgt_agent_process_work(struct work_struct *work) 855 { 856 struct sbp_target_request *req = 857 container_of(work, struct sbp_target_request, work); 858 859 pr_debug("tgt_orb ptr:0x%llx next_ORB:0x%llx data_descriptor:0x%llx misc:0x%x\n", 860 req->orb_pointer, 861 sbp2_pointer_to_addr(&req->orb.next_orb), 862 sbp2_pointer_to_addr(&req->orb.data_descriptor), 863 be32_to_cpu(req->orb.misc)); 864 865 if (req->orb_pointer >> 32) 866 pr_debug("ORB with high bits set\n"); 867 868 switch (ORB_REQUEST_FORMAT(be32_to_cpu(req->orb.misc))) { 869 case 0:/* Format specified by this standard */ 870 sbp_handle_command(req); 871 return; 872 case 1: /* Reserved for future standardization */ 873 case 2: /* Vendor-dependent */ 874 req->status.status |= cpu_to_be32( 875 STATUS_BLOCK_RESP( 876 STATUS_RESP_REQUEST_COMPLETE) | 877 STATUS_BLOCK_DEAD(0) | 878 STATUS_BLOCK_LEN(1) | 879 STATUS_BLOCK_SBP_STATUS( 880 SBP_STATUS_REQ_TYPE_NOTSUPP)); 881 sbp_send_status(req); 882 return; 883 case 3: /* Dummy ORB */ 884 req->status.status |= cpu_to_be32( 885 STATUS_BLOCK_RESP( 886 STATUS_RESP_REQUEST_COMPLETE) | 887 STATUS_BLOCK_DEAD(0) | 888 STATUS_BLOCK_LEN(1) | 889 STATUS_BLOCK_SBP_STATUS( 890 SBP_STATUS_DUMMY_ORB_COMPLETE)); 891 sbp_send_status(req); 892 return; 893 default: 894 BUG(); 895 } 896 } 897 898 /* used to double-check we haven't been issued an AGENT_RESET */ 899 static inline bool tgt_agent_check_active(struct sbp_target_agent *agent) 900 { 901 bool active; 902 903 spin_lock_bh(&agent->lock); 904 active = (agent->state == AGENT_STATE_ACTIVE); 905 spin_unlock_bh(&agent->lock); 906 907 return active; 908 } 909 910 static struct sbp_target_request *sbp_mgt_get_req(struct sbp_session *sess, 911 struct fw_card *card, u64 next_orb) 912 { 913 struct se_session *se_sess = sess->se_sess; 914 struct sbp_target_request *req; 915 int tag, cpu; 916 917 tag = sbitmap_queue_get(&se_sess->sess_tag_pool, &cpu); 918 if (tag < 0) 919 return ERR_PTR(-ENOMEM); 920 921 req = &((struct sbp_target_request *)se_sess->sess_cmd_map)[tag]; 922 memset(req, 0, sizeof(*req)); 923 req->se_cmd.map_tag = tag; 924 req->se_cmd.map_cpu = cpu; 925 req->se_cmd.tag = next_orb; 926 927 return req; 928 } 929 930 static void tgt_agent_fetch_work(struct work_struct *work) 931 { 932 struct sbp_target_agent *agent = 933 container_of(work, struct sbp_target_agent, work); 934 struct sbp_session *sess = agent->login->sess; 935 struct sbp_target_request *req; 936 int ret; 937 bool doorbell = agent->doorbell; 938 u64 next_orb = agent->orb_pointer; 939 940 while (next_orb && tgt_agent_check_active(agent)) { 941 req = sbp_mgt_get_req(sess, sess->card, next_orb); 942 if (IS_ERR(req)) { 943 spin_lock_bh(&agent->lock); 944 agent->state = AGENT_STATE_DEAD; 945 spin_unlock_bh(&agent->lock); 946 return; 947 } 948 949 req->login = agent->login; 950 req->orb_pointer = next_orb; 951 952 req->status.status = cpu_to_be32(STATUS_BLOCK_ORB_OFFSET_HIGH( 953 req->orb_pointer >> 32)); 954 req->status.orb_low = cpu_to_be32( 955 req->orb_pointer & 0xfffffffc); 956 957 /* read in the ORB */ 958 ret = sbp_run_transaction(sess->card, TCODE_READ_BLOCK_REQUEST, 959 sess->node_id, sess->generation, sess->speed, 960 req->orb_pointer, &req->orb, sizeof(req->orb)); 961 if (ret != RCODE_COMPLETE) { 962 pr_debug("tgt_orb fetch failed: %x\n", ret); 963 req->status.status |= cpu_to_be32( 964 STATUS_BLOCK_SRC( 965 STATUS_SRC_ORB_FINISHED) | 966 STATUS_BLOCK_RESP( 967 STATUS_RESP_TRANSPORT_FAILURE) | 968 STATUS_BLOCK_DEAD(1) | 969 STATUS_BLOCK_LEN(1) | 970 STATUS_BLOCK_SBP_STATUS( 971 SBP_STATUS_UNSPECIFIED_ERROR)); 972 spin_lock_bh(&agent->lock); 973 agent->state = AGENT_STATE_DEAD; 974 spin_unlock_bh(&agent->lock); 975 976 sbp_send_status(req); 977 return; 978 } 979 980 /* check the next_ORB field */ 981 if (be32_to_cpu(req->orb.next_orb.high) & 0x80000000) { 982 next_orb = 0; 983 req->status.status |= cpu_to_be32(STATUS_BLOCK_SRC( 984 STATUS_SRC_ORB_FINISHED)); 985 } else { 986 next_orb = sbp2_pointer_to_addr(&req->orb.next_orb); 987 req->status.status |= cpu_to_be32(STATUS_BLOCK_SRC( 988 STATUS_SRC_ORB_CONTINUING)); 989 } 990 991 if (tgt_agent_check_active(agent) && !doorbell) { 992 INIT_WORK(&req->work, tgt_agent_process_work); 993 queue_work(system_unbound_wq, &req->work); 994 } else { 995 /* don't process this request, just check next_ORB */ 996 sbp_free_request(req); 997 } 998 999 spin_lock_bh(&agent->lock); 1000 doorbell = agent->doorbell = false; 1001 1002 /* check if we should carry on processing */ 1003 if (next_orb) 1004 agent->orb_pointer = next_orb; 1005 else 1006 agent->state = AGENT_STATE_SUSPENDED; 1007 1008 spin_unlock_bh(&agent->lock); 1009 } 1010 } 1011 1012 static struct sbp_target_agent *sbp_target_agent_register( 1013 struct sbp_login_descriptor *login) 1014 { 1015 struct sbp_target_agent *agent; 1016 int ret; 1017 1018 agent = kmalloc(sizeof(*agent), GFP_KERNEL); 1019 if (!agent) 1020 return ERR_PTR(-ENOMEM); 1021 1022 spin_lock_init(&agent->lock); 1023 1024 agent->handler.length = 0x20; 1025 agent->handler.address_callback = tgt_agent_rw; 1026 agent->handler.callback_data = agent; 1027 1028 agent->login = login; 1029 agent->state = AGENT_STATE_RESET; 1030 INIT_WORK(&agent->work, tgt_agent_fetch_work); 1031 agent->orb_pointer = 0; 1032 agent->doorbell = false; 1033 1034 ret = fw_core_add_address_handler(&agent->handler, 1035 &sbp_register_region); 1036 if (ret < 0) { 1037 kfree(agent); 1038 return ERR_PTR(ret); 1039 } 1040 1041 return agent; 1042 } 1043 1044 static void sbp_target_agent_unregister(struct sbp_target_agent *agent) 1045 { 1046 fw_core_remove_address_handler(&agent->handler); 1047 cancel_work_sync(&agent->work); 1048 kfree(agent); 1049 } 1050 1051 /* 1052 * Simple wrapper around fw_run_transaction that retries the transaction several 1053 * times in case of failure, with an exponential backoff. 1054 */ 1055 static int sbp_run_transaction(struct fw_card *card, int tcode, int destination_id, 1056 int generation, int speed, unsigned long long offset, 1057 void *payload, size_t length) 1058 { 1059 int attempt, ret, delay; 1060 1061 for (attempt = 1; attempt <= 5; attempt++) { 1062 ret = fw_run_transaction(card, tcode, destination_id, 1063 generation, speed, offset, payload, length); 1064 1065 switch (ret) { 1066 case RCODE_COMPLETE: 1067 case RCODE_TYPE_ERROR: 1068 case RCODE_ADDRESS_ERROR: 1069 case RCODE_GENERATION: 1070 return ret; 1071 1072 default: 1073 delay = 5 * attempt * attempt; 1074 usleep_range(delay, delay * 2); 1075 } 1076 } 1077 1078 return ret; 1079 } 1080 1081 /* 1082 * Wrapper around sbp_run_transaction that gets the card, destination, 1083 * generation and speed out of the request's session. 1084 */ 1085 static int sbp_run_request_transaction(struct sbp_target_request *req, 1086 int tcode, unsigned long long offset, void *payload, 1087 size_t length) 1088 { 1089 struct sbp_login_descriptor *login = req->login; 1090 struct sbp_session *sess = login->sess; 1091 struct fw_card *card; 1092 int node_id, generation, speed, ret; 1093 1094 spin_lock_bh(&sess->lock); 1095 card = fw_card_get(sess->card); 1096 node_id = sess->node_id; 1097 generation = sess->generation; 1098 speed = sess->speed; 1099 spin_unlock_bh(&sess->lock); 1100 1101 ret = sbp_run_transaction(card, tcode, node_id, generation, speed, 1102 offset, payload, length); 1103 1104 fw_card_put(card); 1105 1106 return ret; 1107 } 1108 1109 static int sbp_fetch_command(struct sbp_target_request *req) 1110 { 1111 int ret, cmd_len, copy_len; 1112 1113 cmd_len = scsi_command_size(req->orb.command_block); 1114 1115 req->cmd_buf = kmalloc(cmd_len, GFP_KERNEL); 1116 if (!req->cmd_buf) 1117 return -ENOMEM; 1118 1119 memcpy(req->cmd_buf, req->orb.command_block, 1120 min_t(int, cmd_len, sizeof(req->orb.command_block))); 1121 1122 if (cmd_len > sizeof(req->orb.command_block)) { 1123 pr_debug("sbp_fetch_command: filling in long command\n"); 1124 copy_len = cmd_len - sizeof(req->orb.command_block); 1125 1126 ret = sbp_run_request_transaction(req, 1127 TCODE_READ_BLOCK_REQUEST, 1128 req->orb_pointer + sizeof(req->orb), 1129 req->cmd_buf + sizeof(req->orb.command_block), 1130 copy_len); 1131 if (ret != RCODE_COMPLETE) 1132 return -EIO; 1133 } 1134 1135 return 0; 1136 } 1137 1138 static int sbp_fetch_page_table(struct sbp_target_request *req) 1139 { 1140 int pg_tbl_sz, ret; 1141 struct sbp_page_table_entry *pg_tbl; 1142 1143 if (!CMDBLK_ORB_PG_TBL_PRESENT(be32_to_cpu(req->orb.misc))) 1144 return 0; 1145 1146 pg_tbl_sz = CMDBLK_ORB_DATA_SIZE(be32_to_cpu(req->orb.misc)) * 1147 sizeof(struct sbp_page_table_entry); 1148 1149 pg_tbl = kmalloc(pg_tbl_sz, GFP_KERNEL); 1150 if (!pg_tbl) 1151 return -ENOMEM; 1152 1153 ret = sbp_run_request_transaction(req, TCODE_READ_BLOCK_REQUEST, 1154 sbp2_pointer_to_addr(&req->orb.data_descriptor), 1155 pg_tbl, pg_tbl_sz); 1156 if (ret != RCODE_COMPLETE) { 1157 kfree(pg_tbl); 1158 return -EIO; 1159 } 1160 1161 req->pg_tbl = pg_tbl; 1162 return 0; 1163 } 1164 1165 static void sbp_calc_data_length_direction(struct sbp_target_request *req, 1166 u32 *data_len, enum dma_data_direction *data_dir) 1167 { 1168 int data_size, direction, idx; 1169 1170 data_size = CMDBLK_ORB_DATA_SIZE(be32_to_cpu(req->orb.misc)); 1171 direction = CMDBLK_ORB_DIRECTION(be32_to_cpu(req->orb.misc)); 1172 1173 if (!data_size) { 1174 *data_len = 0; 1175 *data_dir = DMA_NONE; 1176 return; 1177 } 1178 1179 *data_dir = direction ? DMA_FROM_DEVICE : DMA_TO_DEVICE; 1180 1181 if (req->pg_tbl) { 1182 *data_len = 0; 1183 for (idx = 0; idx < data_size; idx++) { 1184 *data_len += be16_to_cpu( 1185 req->pg_tbl[idx].segment_length); 1186 } 1187 } else { 1188 *data_len = data_size; 1189 } 1190 } 1191 1192 static void sbp_handle_command(struct sbp_target_request *req) 1193 { 1194 struct sbp_login_descriptor *login = req->login; 1195 struct sbp_session *sess = login->sess; 1196 int ret, unpacked_lun; 1197 u32 data_length; 1198 enum dma_data_direction data_dir; 1199 1200 ret = sbp_fetch_command(req); 1201 if (ret) { 1202 pr_debug("sbp_handle_command: fetch command failed: %d\n", ret); 1203 goto err; 1204 } 1205 1206 ret = sbp_fetch_page_table(req); 1207 if (ret) { 1208 pr_debug("sbp_handle_command: fetch page table failed: %d\n", 1209 ret); 1210 goto err; 1211 } 1212 1213 unpacked_lun = req->login->login_lun; 1214 sbp_calc_data_length_direction(req, &data_length, &data_dir); 1215 1216 pr_debug("sbp_handle_command ORB:0x%llx unpacked_lun:%d data_len:%d data_dir:%d\n", 1217 req->orb_pointer, unpacked_lun, data_length, data_dir); 1218 1219 /* only used for printk until we do TMRs */ 1220 req->se_cmd.tag = req->orb_pointer; 1221 target_submit_cmd(&req->se_cmd, sess->se_sess, req->cmd_buf, 1222 req->sense_buf, unpacked_lun, data_length, 1223 TCM_SIMPLE_TAG, data_dir, TARGET_SCF_ACK_KREF); 1224 return; 1225 1226 err: 1227 req->status.status |= cpu_to_be32( 1228 STATUS_BLOCK_RESP(STATUS_RESP_TRANSPORT_FAILURE) | 1229 STATUS_BLOCK_DEAD(0) | 1230 STATUS_BLOCK_LEN(1) | 1231 STATUS_BLOCK_SBP_STATUS(SBP_STATUS_UNSPECIFIED_ERROR)); 1232 sbp_send_status(req); 1233 } 1234 1235 /* 1236 * DMA_TO_DEVICE = read from initiator (SCSI WRITE) 1237 * DMA_FROM_DEVICE = write to initiator (SCSI READ) 1238 */ 1239 static int sbp_rw_data(struct sbp_target_request *req) 1240 { 1241 struct sbp_session *sess = req->login->sess; 1242 int tcode, sg_miter_flags, max_payload, pg_size, speed, node_id, 1243 generation, num_pte, length, tfr_length, 1244 rcode = RCODE_COMPLETE; 1245 struct sbp_page_table_entry *pte; 1246 unsigned long long offset; 1247 struct fw_card *card; 1248 struct sg_mapping_iter iter; 1249 1250 if (req->se_cmd.data_direction == DMA_FROM_DEVICE) { 1251 tcode = TCODE_WRITE_BLOCK_REQUEST; 1252 sg_miter_flags = SG_MITER_FROM_SG; 1253 } else { 1254 tcode = TCODE_READ_BLOCK_REQUEST; 1255 sg_miter_flags = SG_MITER_TO_SG; 1256 } 1257 1258 max_payload = 4 << CMDBLK_ORB_MAX_PAYLOAD(be32_to_cpu(req->orb.misc)); 1259 speed = CMDBLK_ORB_SPEED(be32_to_cpu(req->orb.misc)); 1260 1261 pg_size = CMDBLK_ORB_PG_SIZE(be32_to_cpu(req->orb.misc)); 1262 if (pg_size) { 1263 pr_err("sbp_run_transaction: page size ignored\n"); 1264 pg_size = 0x100 << pg_size; 1265 } 1266 1267 spin_lock_bh(&sess->lock); 1268 card = fw_card_get(sess->card); 1269 node_id = sess->node_id; 1270 generation = sess->generation; 1271 spin_unlock_bh(&sess->lock); 1272 1273 if (req->pg_tbl) { 1274 pte = req->pg_tbl; 1275 num_pte = CMDBLK_ORB_DATA_SIZE(be32_to_cpu(req->orb.misc)); 1276 1277 offset = 0; 1278 length = 0; 1279 } else { 1280 pte = NULL; 1281 num_pte = 0; 1282 1283 offset = sbp2_pointer_to_addr(&req->orb.data_descriptor); 1284 length = req->se_cmd.data_length; 1285 } 1286 1287 sg_miter_start(&iter, req->se_cmd.t_data_sg, req->se_cmd.t_data_nents, 1288 sg_miter_flags); 1289 1290 while (length || num_pte) { 1291 if (!length) { 1292 offset = (u64)be16_to_cpu(pte->segment_base_hi) << 32 | 1293 be32_to_cpu(pte->segment_base_lo); 1294 length = be16_to_cpu(pte->segment_length); 1295 1296 pte++; 1297 num_pte--; 1298 } 1299 1300 sg_miter_next(&iter); 1301 1302 tfr_length = min3(length, max_payload, (int)iter.length); 1303 1304 /* FIXME: take page_size into account */ 1305 1306 rcode = sbp_run_transaction(card, tcode, node_id, 1307 generation, speed, 1308 offset, iter.addr, tfr_length); 1309 1310 if (rcode != RCODE_COMPLETE) 1311 break; 1312 1313 length -= tfr_length; 1314 offset += tfr_length; 1315 iter.consumed = tfr_length; 1316 } 1317 1318 sg_miter_stop(&iter); 1319 fw_card_put(card); 1320 1321 if (rcode == RCODE_COMPLETE) { 1322 WARN_ON(length != 0); 1323 return 0; 1324 } else { 1325 return -EIO; 1326 } 1327 } 1328 1329 static int sbp_send_status(struct sbp_target_request *req) 1330 { 1331 int rc, ret = 0, length; 1332 struct sbp_login_descriptor *login = req->login; 1333 1334 length = (((be32_to_cpu(req->status.status) >> 24) & 0x07) + 1) * 4; 1335 1336 rc = sbp_run_request_transaction(req, TCODE_WRITE_BLOCK_REQUEST, 1337 login->status_fifo_addr, &req->status, length); 1338 if (rc != RCODE_COMPLETE) { 1339 pr_debug("sbp_send_status: write failed: 0x%x\n", rc); 1340 ret = -EIO; 1341 goto put_ref; 1342 } 1343 1344 pr_debug("sbp_send_status: status write complete for ORB: 0x%llx\n", 1345 req->orb_pointer); 1346 /* 1347 * Drop the extra ACK_KREF reference taken by target_submit_cmd() 1348 * ahead of sbp_check_stop_free() -> transport_generic_free_cmd() 1349 * final se_cmd->cmd_kref put. 1350 */ 1351 put_ref: 1352 target_put_sess_cmd(&req->se_cmd); 1353 return ret; 1354 } 1355 1356 static void sbp_sense_mangle(struct sbp_target_request *req) 1357 { 1358 struct se_cmd *se_cmd = &req->se_cmd; 1359 u8 *sense = req->sense_buf; 1360 u8 *status = req->status.data; 1361 1362 WARN_ON(se_cmd->scsi_sense_length < 18); 1363 1364 switch (sense[0] & 0x7f) { /* sfmt */ 1365 case 0x70: /* current, fixed */ 1366 status[0] = 0 << 6; 1367 break; 1368 case 0x71: /* deferred, fixed */ 1369 status[0] = 1 << 6; 1370 break; 1371 case 0x72: /* current, descriptor */ 1372 case 0x73: /* deferred, descriptor */ 1373 default: 1374 /* 1375 * TODO: SBP-3 specifies what we should do with descriptor 1376 * format sense data 1377 */ 1378 pr_err("sbp_send_sense: unknown sense format: 0x%x\n", 1379 sense[0]); 1380 req->status.status |= cpu_to_be32( 1381 STATUS_BLOCK_RESP(STATUS_RESP_REQUEST_COMPLETE) | 1382 STATUS_BLOCK_DEAD(0) | 1383 STATUS_BLOCK_LEN(1) | 1384 STATUS_BLOCK_SBP_STATUS(SBP_STATUS_REQUEST_ABORTED)); 1385 return; 1386 } 1387 1388 status[0] |= se_cmd->scsi_status & 0x3f;/* status */ 1389 status[1] = 1390 (sense[0] & 0x80) | /* valid */ 1391 ((sense[2] & 0xe0) >> 1) | /* mark, eom, ili */ 1392 (sense[2] & 0x0f); /* sense_key */ 1393 status[2] = se_cmd->scsi_asc; /* sense_code */ 1394 status[3] = se_cmd->scsi_ascq; /* sense_qualifier */ 1395 1396 /* information */ 1397 status[4] = sense[3]; 1398 status[5] = sense[4]; 1399 status[6] = sense[5]; 1400 status[7] = sense[6]; 1401 1402 /* CDB-dependent */ 1403 status[8] = sense[8]; 1404 status[9] = sense[9]; 1405 status[10] = sense[10]; 1406 status[11] = sense[11]; 1407 1408 /* fru */ 1409 status[12] = sense[14]; 1410 1411 /* sense_key-dependent */ 1412 status[13] = sense[15]; 1413 status[14] = sense[16]; 1414 status[15] = sense[17]; 1415 1416 req->status.status |= cpu_to_be32( 1417 STATUS_BLOCK_RESP(STATUS_RESP_REQUEST_COMPLETE) | 1418 STATUS_BLOCK_DEAD(0) | 1419 STATUS_BLOCK_LEN(5) | 1420 STATUS_BLOCK_SBP_STATUS(SBP_STATUS_OK)); 1421 } 1422 1423 static int sbp_send_sense(struct sbp_target_request *req) 1424 { 1425 struct se_cmd *se_cmd = &req->se_cmd; 1426 1427 if (se_cmd->scsi_sense_length) { 1428 sbp_sense_mangle(req); 1429 } else { 1430 req->status.status |= cpu_to_be32( 1431 STATUS_BLOCK_RESP(STATUS_RESP_REQUEST_COMPLETE) | 1432 STATUS_BLOCK_DEAD(0) | 1433 STATUS_BLOCK_LEN(1) | 1434 STATUS_BLOCK_SBP_STATUS(SBP_STATUS_OK)); 1435 } 1436 1437 return sbp_send_status(req); 1438 } 1439 1440 static void sbp_free_request(struct sbp_target_request *req) 1441 { 1442 struct se_cmd *se_cmd = &req->se_cmd; 1443 struct se_session *se_sess = se_cmd->se_sess; 1444 1445 kfree(req->pg_tbl); 1446 kfree(req->cmd_buf); 1447 1448 target_free_tag(se_sess, se_cmd); 1449 } 1450 1451 static void sbp_mgt_agent_process(struct work_struct *work) 1452 { 1453 struct sbp_management_agent *agent = 1454 container_of(work, struct sbp_management_agent, work); 1455 struct sbp_management_request *req = agent->request; 1456 int ret; 1457 int status_data_len = 0; 1458 1459 /* fetch the ORB from the initiator */ 1460 ret = sbp_run_transaction(req->card, TCODE_READ_BLOCK_REQUEST, 1461 req->node_addr, req->generation, req->speed, 1462 agent->orb_offset, &req->orb, sizeof(req->orb)); 1463 if (ret != RCODE_COMPLETE) { 1464 pr_debug("mgt_orb fetch failed: %x\n", ret); 1465 goto out; 1466 } 1467 1468 pr_debug("mgt_orb ptr1:0x%llx ptr2:0x%llx misc:0x%x len:0x%x status_fifo:0x%llx\n", 1469 sbp2_pointer_to_addr(&req->orb.ptr1), 1470 sbp2_pointer_to_addr(&req->orb.ptr2), 1471 be32_to_cpu(req->orb.misc), be32_to_cpu(req->orb.length), 1472 sbp2_pointer_to_addr(&req->orb.status_fifo)); 1473 1474 if (!ORB_NOTIFY(be32_to_cpu(req->orb.misc)) || 1475 ORB_REQUEST_FORMAT(be32_to_cpu(req->orb.misc)) != 0) { 1476 pr_err("mgt_orb bad request\n"); 1477 goto out; 1478 } 1479 1480 switch (MANAGEMENT_ORB_FUNCTION(be32_to_cpu(req->orb.misc))) { 1481 case MANAGEMENT_ORB_FUNCTION_LOGIN: 1482 sbp_management_request_login(agent, req, &status_data_len); 1483 break; 1484 1485 case MANAGEMENT_ORB_FUNCTION_QUERY_LOGINS: 1486 sbp_management_request_query_logins(agent, req, 1487 &status_data_len); 1488 break; 1489 1490 case MANAGEMENT_ORB_FUNCTION_RECONNECT: 1491 sbp_management_request_reconnect(agent, req, &status_data_len); 1492 break; 1493 1494 case MANAGEMENT_ORB_FUNCTION_SET_PASSWORD: 1495 pr_notice("SET PASSWORD not implemented\n"); 1496 1497 req->status.status = cpu_to_be32( 1498 STATUS_BLOCK_RESP(STATUS_RESP_REQUEST_COMPLETE) | 1499 STATUS_BLOCK_SBP_STATUS(SBP_STATUS_REQ_TYPE_NOTSUPP)); 1500 1501 break; 1502 1503 case MANAGEMENT_ORB_FUNCTION_LOGOUT: 1504 sbp_management_request_logout(agent, req, &status_data_len); 1505 break; 1506 1507 case MANAGEMENT_ORB_FUNCTION_ABORT_TASK: 1508 pr_notice("ABORT TASK not implemented\n"); 1509 1510 req->status.status = cpu_to_be32( 1511 STATUS_BLOCK_RESP(STATUS_RESP_REQUEST_COMPLETE) | 1512 STATUS_BLOCK_SBP_STATUS(SBP_STATUS_REQ_TYPE_NOTSUPP)); 1513 1514 break; 1515 1516 case MANAGEMENT_ORB_FUNCTION_ABORT_TASK_SET: 1517 pr_notice("ABORT TASK SET not implemented\n"); 1518 1519 req->status.status = cpu_to_be32( 1520 STATUS_BLOCK_RESP(STATUS_RESP_REQUEST_COMPLETE) | 1521 STATUS_BLOCK_SBP_STATUS(SBP_STATUS_REQ_TYPE_NOTSUPP)); 1522 1523 break; 1524 1525 case MANAGEMENT_ORB_FUNCTION_LOGICAL_UNIT_RESET: 1526 pr_notice("LOGICAL UNIT RESET not implemented\n"); 1527 1528 req->status.status = cpu_to_be32( 1529 STATUS_BLOCK_RESP(STATUS_RESP_REQUEST_COMPLETE) | 1530 STATUS_BLOCK_SBP_STATUS(SBP_STATUS_REQ_TYPE_NOTSUPP)); 1531 1532 break; 1533 1534 case MANAGEMENT_ORB_FUNCTION_TARGET_RESET: 1535 pr_notice("TARGET RESET not implemented\n"); 1536 1537 req->status.status = cpu_to_be32( 1538 STATUS_BLOCK_RESP(STATUS_RESP_REQUEST_COMPLETE) | 1539 STATUS_BLOCK_SBP_STATUS(SBP_STATUS_REQ_TYPE_NOTSUPP)); 1540 1541 break; 1542 1543 default: 1544 pr_notice("unknown management function 0x%x\n", 1545 MANAGEMENT_ORB_FUNCTION(be32_to_cpu(req->orb.misc))); 1546 1547 req->status.status = cpu_to_be32( 1548 STATUS_BLOCK_RESP(STATUS_RESP_REQUEST_COMPLETE) | 1549 STATUS_BLOCK_SBP_STATUS(SBP_STATUS_REQ_TYPE_NOTSUPP)); 1550 1551 break; 1552 } 1553 1554 req->status.status |= cpu_to_be32( 1555 STATUS_BLOCK_SRC(1) | /* Response to ORB, next_ORB absent */ 1556 STATUS_BLOCK_LEN(DIV_ROUND_UP(status_data_len, 4) + 1) | 1557 STATUS_BLOCK_ORB_OFFSET_HIGH(agent->orb_offset >> 32)); 1558 req->status.orb_low = cpu_to_be32(agent->orb_offset); 1559 1560 /* write the status block back to the initiator */ 1561 ret = sbp_run_transaction(req->card, TCODE_WRITE_BLOCK_REQUEST, 1562 req->node_addr, req->generation, req->speed, 1563 sbp2_pointer_to_addr(&req->orb.status_fifo), 1564 &req->status, 8 + status_data_len); 1565 if (ret != RCODE_COMPLETE) { 1566 pr_debug("mgt_orb status write failed: %x\n", ret); 1567 goto out; 1568 } 1569 1570 out: 1571 fw_card_put(req->card); 1572 kfree(req); 1573 1574 spin_lock_bh(&agent->lock); 1575 agent->state = MANAGEMENT_AGENT_STATE_IDLE; 1576 spin_unlock_bh(&agent->lock); 1577 } 1578 1579 static void sbp_mgt_agent_rw(struct fw_card *card, 1580 struct fw_request *request, int tcode, int destination, int source, 1581 int generation, unsigned long long offset, void *data, size_t length, 1582 void *callback_data) 1583 { 1584 struct sbp_management_agent *agent = callback_data; 1585 struct sbp2_pointer *ptr = data; 1586 int rcode = RCODE_ADDRESS_ERROR; 1587 1588 if (!agent->tport->enable) 1589 goto out; 1590 1591 if ((offset != agent->handler.offset) || (length != 8)) 1592 goto out; 1593 1594 if (tcode == TCODE_WRITE_BLOCK_REQUEST) { 1595 struct sbp_management_request *req; 1596 int prev_state; 1597 1598 spin_lock_bh(&agent->lock); 1599 prev_state = agent->state; 1600 agent->state = MANAGEMENT_AGENT_STATE_BUSY; 1601 spin_unlock_bh(&agent->lock); 1602 1603 if (prev_state == MANAGEMENT_AGENT_STATE_BUSY) { 1604 pr_notice("ignoring management request while busy\n"); 1605 rcode = RCODE_CONFLICT_ERROR; 1606 goto out; 1607 } 1608 req = kzalloc(sizeof(*req), GFP_ATOMIC); 1609 if (!req) { 1610 rcode = RCODE_CONFLICT_ERROR; 1611 goto out; 1612 } 1613 1614 req->card = fw_card_get(card); 1615 req->generation = generation; 1616 req->node_addr = source; 1617 req->speed = fw_get_request_speed(request); 1618 1619 agent->orb_offset = sbp2_pointer_to_addr(ptr); 1620 agent->request = req; 1621 1622 queue_work(system_unbound_wq, &agent->work); 1623 rcode = RCODE_COMPLETE; 1624 } else if (tcode == TCODE_READ_BLOCK_REQUEST) { 1625 addr_to_sbp2_pointer(agent->orb_offset, ptr); 1626 rcode = RCODE_COMPLETE; 1627 } else { 1628 rcode = RCODE_TYPE_ERROR; 1629 } 1630 1631 out: 1632 fw_send_response(card, request, rcode); 1633 } 1634 1635 static struct sbp_management_agent *sbp_management_agent_register( 1636 struct sbp_tport *tport) 1637 { 1638 int ret; 1639 struct sbp_management_agent *agent; 1640 1641 agent = kmalloc(sizeof(*agent), GFP_KERNEL); 1642 if (!agent) 1643 return ERR_PTR(-ENOMEM); 1644 1645 spin_lock_init(&agent->lock); 1646 agent->tport = tport; 1647 agent->handler.length = 0x08; 1648 agent->handler.address_callback = sbp_mgt_agent_rw; 1649 agent->handler.callback_data = agent; 1650 agent->state = MANAGEMENT_AGENT_STATE_IDLE; 1651 INIT_WORK(&agent->work, sbp_mgt_agent_process); 1652 agent->orb_offset = 0; 1653 agent->request = NULL; 1654 1655 ret = fw_core_add_address_handler(&agent->handler, 1656 &sbp_register_region); 1657 if (ret < 0) { 1658 kfree(agent); 1659 return ERR_PTR(ret); 1660 } 1661 1662 return agent; 1663 } 1664 1665 static void sbp_management_agent_unregister(struct sbp_management_agent *agent) 1666 { 1667 fw_core_remove_address_handler(&agent->handler); 1668 cancel_work_sync(&agent->work); 1669 kfree(agent); 1670 } 1671 1672 static int sbp_check_true(struct se_portal_group *se_tpg) 1673 { 1674 return 1; 1675 } 1676 1677 static int sbp_check_false(struct se_portal_group *se_tpg) 1678 { 1679 return 0; 1680 } 1681 1682 static char *sbp_get_fabric_wwn(struct se_portal_group *se_tpg) 1683 { 1684 struct sbp_tpg *tpg = container_of(se_tpg, struct sbp_tpg, se_tpg); 1685 struct sbp_tport *tport = tpg->tport; 1686 1687 return &tport->tport_name[0]; 1688 } 1689 1690 static u16 sbp_get_tag(struct se_portal_group *se_tpg) 1691 { 1692 struct sbp_tpg *tpg = container_of(se_tpg, struct sbp_tpg, se_tpg); 1693 return tpg->tport_tpgt; 1694 } 1695 1696 static u32 sbp_tpg_get_inst_index(struct se_portal_group *se_tpg) 1697 { 1698 return 1; 1699 } 1700 1701 static void sbp_release_cmd(struct se_cmd *se_cmd) 1702 { 1703 struct sbp_target_request *req = container_of(se_cmd, 1704 struct sbp_target_request, se_cmd); 1705 1706 sbp_free_request(req); 1707 } 1708 1709 static u32 sbp_sess_get_index(struct se_session *se_sess) 1710 { 1711 return 0; 1712 } 1713 1714 static int sbp_write_pending(struct se_cmd *se_cmd) 1715 { 1716 struct sbp_target_request *req = container_of(se_cmd, 1717 struct sbp_target_request, se_cmd); 1718 int ret; 1719 1720 ret = sbp_rw_data(req); 1721 if (ret) { 1722 req->status.status |= cpu_to_be32( 1723 STATUS_BLOCK_RESP( 1724 STATUS_RESP_TRANSPORT_FAILURE) | 1725 STATUS_BLOCK_DEAD(0) | 1726 STATUS_BLOCK_LEN(1) | 1727 STATUS_BLOCK_SBP_STATUS( 1728 SBP_STATUS_UNSPECIFIED_ERROR)); 1729 sbp_send_status(req); 1730 return ret; 1731 } 1732 1733 target_execute_cmd(se_cmd); 1734 return 0; 1735 } 1736 1737 static void sbp_set_default_node_attrs(struct se_node_acl *nacl) 1738 { 1739 return; 1740 } 1741 1742 static int sbp_get_cmd_state(struct se_cmd *se_cmd) 1743 { 1744 return 0; 1745 } 1746 1747 static int sbp_queue_data_in(struct se_cmd *se_cmd) 1748 { 1749 struct sbp_target_request *req = container_of(se_cmd, 1750 struct sbp_target_request, se_cmd); 1751 int ret; 1752 1753 ret = sbp_rw_data(req); 1754 if (ret) { 1755 req->status.status |= cpu_to_be32( 1756 STATUS_BLOCK_RESP(STATUS_RESP_TRANSPORT_FAILURE) | 1757 STATUS_BLOCK_DEAD(0) | 1758 STATUS_BLOCK_LEN(1) | 1759 STATUS_BLOCK_SBP_STATUS(SBP_STATUS_UNSPECIFIED_ERROR)); 1760 sbp_send_status(req); 1761 return ret; 1762 } 1763 1764 return sbp_send_sense(req); 1765 } 1766 1767 /* 1768 * Called after command (no data transfer) or after the write (to device) 1769 * operation is completed 1770 */ 1771 static int sbp_queue_status(struct se_cmd *se_cmd) 1772 { 1773 struct sbp_target_request *req = container_of(se_cmd, 1774 struct sbp_target_request, se_cmd); 1775 1776 return sbp_send_sense(req); 1777 } 1778 1779 static void sbp_queue_tm_rsp(struct se_cmd *se_cmd) 1780 { 1781 } 1782 1783 static void sbp_aborted_task(struct se_cmd *se_cmd) 1784 { 1785 return; 1786 } 1787 1788 static int sbp_check_stop_free(struct se_cmd *se_cmd) 1789 { 1790 struct sbp_target_request *req = container_of(se_cmd, 1791 struct sbp_target_request, se_cmd); 1792 1793 return transport_generic_free_cmd(&req->se_cmd, 0); 1794 } 1795 1796 static int sbp_count_se_tpg_luns(struct se_portal_group *tpg) 1797 { 1798 struct se_lun *lun; 1799 int count = 0; 1800 1801 rcu_read_lock(); 1802 hlist_for_each_entry_rcu(lun, &tpg->tpg_lun_hlist, link) 1803 count++; 1804 rcu_read_unlock(); 1805 1806 return count; 1807 } 1808 1809 static int sbp_update_unit_directory(struct sbp_tport *tport) 1810 { 1811 struct se_lun *lun; 1812 int num_luns, num_entries, idx = 0, mgt_agt_addr, ret; 1813 u32 *data; 1814 1815 if (tport->unit_directory.data) { 1816 fw_core_remove_descriptor(&tport->unit_directory); 1817 kfree(tport->unit_directory.data); 1818 tport->unit_directory.data = NULL; 1819 } 1820 1821 if (!tport->enable || !tport->tpg) 1822 return 0; 1823 1824 num_luns = sbp_count_se_tpg_luns(&tport->tpg->se_tpg); 1825 1826 /* 1827 * Number of entries in the final unit directory: 1828 * - all of those in the template 1829 * - management_agent 1830 * - unit_characteristics 1831 * - reconnect_timeout 1832 * - unit unique ID 1833 * - one for each LUN 1834 * 1835 * MUST NOT include leaf or sub-directory entries 1836 */ 1837 num_entries = ARRAY_SIZE(sbp_unit_directory_template) + 4 + num_luns; 1838 1839 if (tport->directory_id != -1) 1840 num_entries++; 1841 1842 /* allocate num_entries + 4 for the header and unique ID leaf */ 1843 data = kcalloc((num_entries + 4), sizeof(u32), GFP_KERNEL); 1844 if (!data) 1845 return -ENOMEM; 1846 1847 /* directory_length */ 1848 data[idx++] = num_entries << 16; 1849 1850 /* directory_id */ 1851 if (tport->directory_id != -1) 1852 data[idx++] = (CSR_DIRECTORY_ID << 24) | tport->directory_id; 1853 1854 /* unit directory template */ 1855 memcpy(&data[idx], sbp_unit_directory_template, 1856 sizeof(sbp_unit_directory_template)); 1857 idx += ARRAY_SIZE(sbp_unit_directory_template); 1858 1859 /* management_agent */ 1860 mgt_agt_addr = (tport->mgt_agt->handler.offset - CSR_REGISTER_BASE) / 4; 1861 data[idx++] = 0x54000000 | (mgt_agt_addr & 0x00ffffff); 1862 1863 /* unit_characteristics */ 1864 data[idx++] = 0x3a000000 | 1865 (((tport->mgt_orb_timeout * 2) << 8) & 0xff00) | 1866 SBP_ORB_FETCH_SIZE; 1867 1868 /* reconnect_timeout */ 1869 data[idx++] = 0x3d000000 | (tport->max_reconnect_timeout & 0xffff); 1870 1871 /* unit unique ID (leaf is just after LUNs) */ 1872 data[idx++] = 0x8d000000 | (num_luns + 1); 1873 1874 rcu_read_lock(); 1875 hlist_for_each_entry_rcu(lun, &tport->tpg->se_tpg.tpg_lun_hlist, link) { 1876 struct se_device *dev; 1877 int type; 1878 /* 1879 * rcu_dereference_raw protected by se_lun->lun_group symlink 1880 * reference to se_device->dev_group. 1881 */ 1882 dev = rcu_dereference_raw(lun->lun_se_dev); 1883 type = dev->transport->get_device_type(dev); 1884 1885 /* logical_unit_number */ 1886 data[idx++] = 0x14000000 | 1887 ((type << 16) & 0x1f0000) | 1888 (lun->unpacked_lun & 0xffff); 1889 } 1890 rcu_read_unlock(); 1891 1892 /* unit unique ID leaf */ 1893 data[idx++] = 2 << 16; 1894 data[idx++] = tport->guid >> 32; 1895 data[idx++] = tport->guid; 1896 1897 tport->unit_directory.length = idx; 1898 tport->unit_directory.key = (CSR_DIRECTORY | CSR_UNIT) << 24; 1899 tport->unit_directory.data = data; 1900 1901 ret = fw_core_add_descriptor(&tport->unit_directory); 1902 if (ret < 0) { 1903 kfree(tport->unit_directory.data); 1904 tport->unit_directory.data = NULL; 1905 } 1906 1907 return ret; 1908 } 1909 1910 static ssize_t sbp_parse_wwn(const char *name, u64 *wwn) 1911 { 1912 const char *cp; 1913 char c, nibble; 1914 int pos = 0, err; 1915 1916 *wwn = 0; 1917 for (cp = name; cp < &name[SBP_NAMELEN - 1]; cp++) { 1918 c = *cp; 1919 if (c == '\n' && cp[1] == '\0') 1920 continue; 1921 if (c == '\0') { 1922 err = 2; 1923 if (pos != 16) 1924 goto fail; 1925 return cp - name; 1926 } 1927 err = 3; 1928 if (isdigit(c)) 1929 nibble = c - '0'; 1930 else if (isxdigit(c)) 1931 nibble = tolower(c) - 'a' + 10; 1932 else 1933 goto fail; 1934 *wwn = (*wwn << 4) | nibble; 1935 pos++; 1936 } 1937 err = 4; 1938 fail: 1939 printk(KERN_INFO "err %u len %zu pos %u\n", 1940 err, cp - name, pos); 1941 return -1; 1942 } 1943 1944 static ssize_t sbp_format_wwn(char *buf, size_t len, u64 wwn) 1945 { 1946 return snprintf(buf, len, "%016llx", wwn); 1947 } 1948 1949 static int sbp_init_nodeacl(struct se_node_acl *se_nacl, const char *name) 1950 { 1951 u64 guid = 0; 1952 1953 if (sbp_parse_wwn(name, &guid) < 0) 1954 return -EINVAL; 1955 return 0; 1956 } 1957 1958 static int sbp_post_link_lun( 1959 struct se_portal_group *se_tpg, 1960 struct se_lun *se_lun) 1961 { 1962 struct sbp_tpg *tpg = container_of(se_tpg, struct sbp_tpg, se_tpg); 1963 1964 return sbp_update_unit_directory(tpg->tport); 1965 } 1966 1967 static void sbp_pre_unlink_lun( 1968 struct se_portal_group *se_tpg, 1969 struct se_lun *se_lun) 1970 { 1971 struct sbp_tpg *tpg = container_of(se_tpg, struct sbp_tpg, se_tpg); 1972 struct sbp_tport *tport = tpg->tport; 1973 int ret; 1974 1975 if (sbp_count_se_tpg_luns(&tpg->se_tpg) == 0) 1976 tport->enable = 0; 1977 1978 ret = sbp_update_unit_directory(tport); 1979 if (ret < 0) 1980 pr_err("unlink LUN: failed to update unit directory\n"); 1981 } 1982 1983 static struct se_portal_group *sbp_make_tpg(struct se_wwn *wwn, 1984 const char *name) 1985 { 1986 struct sbp_tport *tport = 1987 container_of(wwn, struct sbp_tport, tport_wwn); 1988 1989 struct sbp_tpg *tpg; 1990 unsigned long tpgt; 1991 int ret; 1992 1993 if (strstr(name, "tpgt_") != name) 1994 return ERR_PTR(-EINVAL); 1995 if (kstrtoul(name + 5, 10, &tpgt) || tpgt > UINT_MAX) 1996 return ERR_PTR(-EINVAL); 1997 1998 if (tport->tpg) { 1999 pr_err("Only one TPG per Unit is possible.\n"); 2000 return ERR_PTR(-EBUSY); 2001 } 2002 2003 tpg = kzalloc(sizeof(*tpg), GFP_KERNEL); 2004 if (!tpg) 2005 return ERR_PTR(-ENOMEM); 2006 2007 tpg->tport = tport; 2008 tpg->tport_tpgt = tpgt; 2009 tport->tpg = tpg; 2010 2011 /* default attribute values */ 2012 tport->enable = 0; 2013 tport->directory_id = -1; 2014 tport->mgt_orb_timeout = 15; 2015 tport->max_reconnect_timeout = 5; 2016 tport->max_logins_per_lun = 1; 2017 2018 tport->mgt_agt = sbp_management_agent_register(tport); 2019 if (IS_ERR(tport->mgt_agt)) { 2020 ret = PTR_ERR(tport->mgt_agt); 2021 goto out_free_tpg; 2022 } 2023 2024 ret = core_tpg_register(wwn, &tpg->se_tpg, SCSI_PROTOCOL_SBP); 2025 if (ret < 0) 2026 goto out_unreg_mgt_agt; 2027 2028 return &tpg->se_tpg; 2029 2030 out_unreg_mgt_agt: 2031 sbp_management_agent_unregister(tport->mgt_agt); 2032 out_free_tpg: 2033 tport->tpg = NULL; 2034 kfree(tpg); 2035 return ERR_PTR(ret); 2036 } 2037 2038 static void sbp_drop_tpg(struct se_portal_group *se_tpg) 2039 { 2040 struct sbp_tpg *tpg = container_of(se_tpg, struct sbp_tpg, se_tpg); 2041 struct sbp_tport *tport = tpg->tport; 2042 2043 core_tpg_deregister(se_tpg); 2044 sbp_management_agent_unregister(tport->mgt_agt); 2045 tport->tpg = NULL; 2046 kfree(tpg); 2047 } 2048 2049 static struct se_wwn *sbp_make_tport( 2050 struct target_fabric_configfs *tf, 2051 struct config_group *group, 2052 const char *name) 2053 { 2054 struct sbp_tport *tport; 2055 u64 guid = 0; 2056 2057 if (sbp_parse_wwn(name, &guid) < 0) 2058 return ERR_PTR(-EINVAL); 2059 2060 tport = kzalloc(sizeof(*tport), GFP_KERNEL); 2061 if (!tport) 2062 return ERR_PTR(-ENOMEM); 2063 2064 tport->guid = guid; 2065 sbp_format_wwn(tport->tport_name, SBP_NAMELEN, guid); 2066 2067 return &tport->tport_wwn; 2068 } 2069 2070 static void sbp_drop_tport(struct se_wwn *wwn) 2071 { 2072 struct sbp_tport *tport = 2073 container_of(wwn, struct sbp_tport, tport_wwn); 2074 2075 kfree(tport); 2076 } 2077 2078 static ssize_t sbp_wwn_version_show(struct config_item *item, char *page) 2079 { 2080 return sprintf(page, "FireWire SBP fabric module %s\n", SBP_VERSION); 2081 } 2082 2083 CONFIGFS_ATTR_RO(sbp_wwn_, version); 2084 2085 static struct configfs_attribute *sbp_wwn_attrs[] = { 2086 &sbp_wwn_attr_version, 2087 NULL, 2088 }; 2089 2090 static ssize_t sbp_tpg_directory_id_show(struct config_item *item, char *page) 2091 { 2092 struct se_portal_group *se_tpg = to_tpg(item); 2093 struct sbp_tpg *tpg = container_of(se_tpg, struct sbp_tpg, se_tpg); 2094 struct sbp_tport *tport = tpg->tport; 2095 2096 if (tport->directory_id == -1) 2097 return sprintf(page, "implicit\n"); 2098 else 2099 return sprintf(page, "%06x\n", tport->directory_id); 2100 } 2101 2102 static ssize_t sbp_tpg_directory_id_store(struct config_item *item, 2103 const char *page, size_t count) 2104 { 2105 struct se_portal_group *se_tpg = to_tpg(item); 2106 struct sbp_tpg *tpg = container_of(se_tpg, struct sbp_tpg, se_tpg); 2107 struct sbp_tport *tport = tpg->tport; 2108 unsigned long val; 2109 2110 if (tport->enable) { 2111 pr_err("Cannot change the directory_id on an active target.\n"); 2112 return -EBUSY; 2113 } 2114 2115 if (strstr(page, "implicit") == page) { 2116 tport->directory_id = -1; 2117 } else { 2118 if (kstrtoul(page, 16, &val) < 0) 2119 return -EINVAL; 2120 if (val > 0xffffff) 2121 return -EINVAL; 2122 2123 tport->directory_id = val; 2124 } 2125 2126 return count; 2127 } 2128 2129 static ssize_t sbp_tpg_enable_show(struct config_item *item, char *page) 2130 { 2131 struct se_portal_group *se_tpg = to_tpg(item); 2132 struct sbp_tpg *tpg = container_of(se_tpg, struct sbp_tpg, se_tpg); 2133 struct sbp_tport *tport = tpg->tport; 2134 return sprintf(page, "%d\n", tport->enable); 2135 } 2136 2137 static ssize_t sbp_tpg_enable_store(struct config_item *item, 2138 const char *page, size_t count) 2139 { 2140 struct se_portal_group *se_tpg = to_tpg(item); 2141 struct sbp_tpg *tpg = container_of(se_tpg, struct sbp_tpg, se_tpg); 2142 struct sbp_tport *tport = tpg->tport; 2143 unsigned long val; 2144 int ret; 2145 2146 if (kstrtoul(page, 0, &val) < 0) 2147 return -EINVAL; 2148 if ((val != 0) && (val != 1)) 2149 return -EINVAL; 2150 2151 if (tport->enable == val) 2152 return count; 2153 2154 if (val) { 2155 if (sbp_count_se_tpg_luns(&tpg->se_tpg) == 0) { 2156 pr_err("Cannot enable a target with no LUNs!\n"); 2157 return -EINVAL; 2158 } 2159 } else { 2160 /* XXX: force-shutdown sessions instead? */ 2161 spin_lock_bh(&se_tpg->session_lock); 2162 if (!list_empty(&se_tpg->tpg_sess_list)) { 2163 spin_unlock_bh(&se_tpg->session_lock); 2164 return -EBUSY; 2165 } 2166 spin_unlock_bh(&se_tpg->session_lock); 2167 } 2168 2169 tport->enable = val; 2170 2171 ret = sbp_update_unit_directory(tport); 2172 if (ret < 0) { 2173 pr_err("Could not update Config ROM\n"); 2174 return ret; 2175 } 2176 2177 return count; 2178 } 2179 2180 CONFIGFS_ATTR(sbp_tpg_, directory_id); 2181 CONFIGFS_ATTR(sbp_tpg_, enable); 2182 2183 static struct configfs_attribute *sbp_tpg_base_attrs[] = { 2184 &sbp_tpg_attr_directory_id, 2185 &sbp_tpg_attr_enable, 2186 NULL, 2187 }; 2188 2189 static ssize_t sbp_tpg_attrib_mgt_orb_timeout_show(struct config_item *item, 2190 char *page) 2191 { 2192 struct se_portal_group *se_tpg = attrib_to_tpg(item); 2193 struct sbp_tpg *tpg = container_of(se_tpg, struct sbp_tpg, se_tpg); 2194 struct sbp_tport *tport = tpg->tport; 2195 return sprintf(page, "%d\n", tport->mgt_orb_timeout); 2196 } 2197 2198 static ssize_t sbp_tpg_attrib_mgt_orb_timeout_store(struct config_item *item, 2199 const char *page, size_t count) 2200 { 2201 struct se_portal_group *se_tpg = attrib_to_tpg(item); 2202 struct sbp_tpg *tpg = container_of(se_tpg, struct sbp_tpg, se_tpg); 2203 struct sbp_tport *tport = tpg->tport; 2204 unsigned long val; 2205 int ret; 2206 2207 if (kstrtoul(page, 0, &val) < 0) 2208 return -EINVAL; 2209 if ((val < 1) || (val > 127)) 2210 return -EINVAL; 2211 2212 if (tport->mgt_orb_timeout == val) 2213 return count; 2214 2215 tport->mgt_orb_timeout = val; 2216 2217 ret = sbp_update_unit_directory(tport); 2218 if (ret < 0) 2219 return ret; 2220 2221 return count; 2222 } 2223 2224 static ssize_t sbp_tpg_attrib_max_reconnect_timeout_show(struct config_item *item, 2225 char *page) 2226 { 2227 struct se_portal_group *se_tpg = attrib_to_tpg(item); 2228 struct sbp_tpg *tpg = container_of(se_tpg, struct sbp_tpg, se_tpg); 2229 struct sbp_tport *tport = tpg->tport; 2230 return sprintf(page, "%d\n", tport->max_reconnect_timeout); 2231 } 2232 2233 static ssize_t sbp_tpg_attrib_max_reconnect_timeout_store(struct config_item *item, 2234 const char *page, size_t count) 2235 { 2236 struct se_portal_group *se_tpg = attrib_to_tpg(item); 2237 struct sbp_tpg *tpg = container_of(se_tpg, struct sbp_tpg, se_tpg); 2238 struct sbp_tport *tport = tpg->tport; 2239 unsigned long val; 2240 int ret; 2241 2242 if (kstrtoul(page, 0, &val) < 0) 2243 return -EINVAL; 2244 if ((val < 1) || (val > 32767)) 2245 return -EINVAL; 2246 2247 if (tport->max_reconnect_timeout == val) 2248 return count; 2249 2250 tport->max_reconnect_timeout = val; 2251 2252 ret = sbp_update_unit_directory(tport); 2253 if (ret < 0) 2254 return ret; 2255 2256 return count; 2257 } 2258 2259 static ssize_t sbp_tpg_attrib_max_logins_per_lun_show(struct config_item *item, 2260 char *page) 2261 { 2262 struct se_portal_group *se_tpg = attrib_to_tpg(item); 2263 struct sbp_tpg *tpg = container_of(se_tpg, struct sbp_tpg, se_tpg); 2264 struct sbp_tport *tport = tpg->tport; 2265 return sprintf(page, "%d\n", tport->max_logins_per_lun); 2266 } 2267 2268 static ssize_t sbp_tpg_attrib_max_logins_per_lun_store(struct config_item *item, 2269 const char *page, size_t count) 2270 { 2271 struct se_portal_group *se_tpg = attrib_to_tpg(item); 2272 struct sbp_tpg *tpg = container_of(se_tpg, struct sbp_tpg, se_tpg); 2273 struct sbp_tport *tport = tpg->tport; 2274 unsigned long val; 2275 2276 if (kstrtoul(page, 0, &val) < 0) 2277 return -EINVAL; 2278 if ((val < 1) || (val > 127)) 2279 return -EINVAL; 2280 2281 /* XXX: also check against current count? */ 2282 2283 tport->max_logins_per_lun = val; 2284 2285 return count; 2286 } 2287 2288 CONFIGFS_ATTR(sbp_tpg_attrib_, mgt_orb_timeout); 2289 CONFIGFS_ATTR(sbp_tpg_attrib_, max_reconnect_timeout); 2290 CONFIGFS_ATTR(sbp_tpg_attrib_, max_logins_per_lun); 2291 2292 static struct configfs_attribute *sbp_tpg_attrib_attrs[] = { 2293 &sbp_tpg_attrib_attr_mgt_orb_timeout, 2294 &sbp_tpg_attrib_attr_max_reconnect_timeout, 2295 &sbp_tpg_attrib_attr_max_logins_per_lun, 2296 NULL, 2297 }; 2298 2299 static const struct target_core_fabric_ops sbp_ops = { 2300 .module = THIS_MODULE, 2301 .fabric_name = "sbp", 2302 .tpg_get_wwn = sbp_get_fabric_wwn, 2303 .tpg_get_tag = sbp_get_tag, 2304 .tpg_check_demo_mode = sbp_check_true, 2305 .tpg_check_demo_mode_cache = sbp_check_true, 2306 .tpg_check_demo_mode_write_protect = sbp_check_false, 2307 .tpg_check_prod_mode_write_protect = sbp_check_false, 2308 .tpg_get_inst_index = sbp_tpg_get_inst_index, 2309 .release_cmd = sbp_release_cmd, 2310 .sess_get_index = sbp_sess_get_index, 2311 .write_pending = sbp_write_pending, 2312 .set_default_node_attributes = sbp_set_default_node_attrs, 2313 .get_cmd_state = sbp_get_cmd_state, 2314 .queue_data_in = sbp_queue_data_in, 2315 .queue_status = sbp_queue_status, 2316 .queue_tm_rsp = sbp_queue_tm_rsp, 2317 .aborted_task = sbp_aborted_task, 2318 .check_stop_free = sbp_check_stop_free, 2319 2320 .fabric_make_wwn = sbp_make_tport, 2321 .fabric_drop_wwn = sbp_drop_tport, 2322 .fabric_make_tpg = sbp_make_tpg, 2323 .fabric_drop_tpg = sbp_drop_tpg, 2324 .fabric_post_link = sbp_post_link_lun, 2325 .fabric_pre_unlink = sbp_pre_unlink_lun, 2326 .fabric_make_np = NULL, 2327 .fabric_drop_np = NULL, 2328 .fabric_init_nodeacl = sbp_init_nodeacl, 2329 2330 .tfc_wwn_attrs = sbp_wwn_attrs, 2331 .tfc_tpg_base_attrs = sbp_tpg_base_attrs, 2332 .tfc_tpg_attrib_attrs = sbp_tpg_attrib_attrs, 2333 }; 2334 2335 static int __init sbp_init(void) 2336 { 2337 return target_register_template(&sbp_ops); 2338 }; 2339 2340 static void __exit sbp_exit(void) 2341 { 2342 target_unregister_template(&sbp_ops); 2343 }; 2344 2345 MODULE_DESCRIPTION("FireWire SBP fabric driver"); 2346 MODULE_LICENSE("GPL"); 2347 module_init(sbp_init); 2348 module_exit(sbp_exit); 2349