1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved. 23 */ 24 25 #include <sys/ddi.h> 26 #include <sys/sunddi.h> 27 28 #include <sys/socket.h> /* networking stuff */ 29 #include <sys/sysmacros.h> /* offsetof */ 30 31 #include <sys/ib/clients/iser/iser.h> 32 #include <sys/ib/clients/iser/iser_idm.h> 33 34 /* 35 * iSER transport routines 36 * 37 * All transport functions except iser_tgt_svc_create() are called through 38 * the ops vector, iser_tgt_svc_create() is called from the async handler 39 * inaddition to being called by the ULP 40 */ 41 42 static void iser_pdu_tx(idm_conn_t *ic, idm_pdu_t *pdu); 43 44 static idm_status_t iser_buf_tx_to_ini(idm_task_t *idt, idm_buf_t *idb); 45 static idm_status_t iser_buf_rx_from_ini(idm_task_t *idt, idm_buf_t *idb); 46 static idm_status_t iser_tgt_enable_datamover(idm_conn_t *ic); 47 static idm_status_t iser_ini_enable_datamover(idm_conn_t *ic); 48 static void iser_notice_key_values(struct idm_conn_s *ic, 49 nvlist_t *negotiated_nvl); 50 static kv_status_t iser_declare_key_values(struct idm_conn_s *ic, 51 nvlist_t *config_nvl, nvlist_t *outgoing_nvl); 52 static idm_status_t iser_free_task_rsrcs(idm_task_t *idt); 53 static kv_status_t iser_negotiate_key_values(idm_conn_t *ic, 54 nvlist_t *request_nvl, nvlist_t *response_nvl, nvlist_t *negotiated_nvl); 55 static kv_status_t iser_handle_numerical(nvpair_t *nvp, uint64_t value, 56 const idm_kv_xlate_t *ikvx, uint64_t min_value, uint64_t max_value, 57 uint64_t iser_max_value, nvlist_t *request_nvl, nvlist_t *response_nvl, 58 nvlist_t *negotiated_nvl); 59 static kv_status_t iser_handle_boolean(nvpair_t *nvp, boolean_t value, 60 const idm_kv_xlate_t *ikvx, boolean_t iser_value, nvlist_t *request_nvl, 61 nvlist_t *response_nvl, nvlist_t *negotiated_nvl); 62 static kv_status_t iser_handle_key(nvpair_t *nvp, const idm_kv_xlate_t *ikvx, 63 nvlist_t *request_nvl, nvlist_t *response_nvl, nvlist_t *negotiated_nvl); 64 static kv_status_t iser_process_request_nvlist(nvlist_t *request_nvl, 65 nvlist_t *response_nvl, nvlist_t *negotiated_nvl); 66 static boolean_t iser_conn_is_capable(idm_conn_req_t *ic, 67 idm_transport_caps_t *caps); 68 static idm_status_t iser_buf_alloc(idm_buf_t *idb, uint64_t buflen); 69 static idm_status_t iser_buf_setup(idm_buf_t *idb); 70 static void iser_buf_teardown(idm_buf_t *idb); 71 static void iser_buf_free(idm_buf_t *idb); 72 static void iser_tgt_svc_destroy(struct idm_svc_s *is); 73 static idm_status_t iser_tgt_svc_online(struct idm_svc_s *is); 74 static void iser_tgt_svc_offline(struct idm_svc_s *is); 75 static idm_status_t iser_tgt_conn_connect(struct idm_conn_s *ic); 76 static idm_status_t iser_ini_conn_create(idm_conn_req_t *cr, 77 struct idm_conn_s *ic); 78 static void iser_conn_destroy(struct idm_conn_s *ic); 79 static idm_status_t iser_ini_conn_connect(struct idm_conn_s *ic); 80 static void iser_conn_disconnect(struct idm_conn_s *ic); 81 82 /* 83 * iSER IDM transport operations 84 */ 85 idm_transport_ops_t iser_transport_ops = { 86 &iser_pdu_tx, /* it_tx_pdu */ 87 &iser_buf_tx_to_ini, /* it_buf_tx_to_ini */ 88 &iser_buf_rx_from_ini, /* it_buf_rx_from_ini */ 89 NULL, /* it_rx_datain */ 90 NULL, /* it_rx_rtt */ 91 NULL, /* it_rx_dataout */ 92 NULL, /* it_alloc_conn_rsrc */ 93 NULL, /* it_free_conn_rsrc */ 94 &iser_tgt_enable_datamover, /* it_tgt_enable_datamover */ 95 &iser_ini_enable_datamover, /* it_ini_enable_datamover */ 96 NULL, /* it_conn_terminate */ 97 &iser_free_task_rsrcs, /* it_free_task_rsrc */ 98 &iser_negotiate_key_values, /* it_negotiate_key_values */ 99 &iser_notice_key_values, /* it_notice_key_values */ 100 &iser_conn_is_capable, /* it_conn_is_capable */ 101 &iser_buf_alloc, /* it_buf_alloc */ 102 &iser_buf_free, /* it_buf_free */ 103 &iser_buf_setup, /* it_buf_setup */ 104 &iser_buf_teardown, /* it_buf_teardown */ 105 &iser_tgt_svc_create, /* it_tgt_svc_create */ 106 &iser_tgt_svc_destroy, /* it_tgt_svc_destroy */ 107 &iser_tgt_svc_online, /* it_tgt_svc_online */ 108 &iser_tgt_svc_offline, /* it_tgt_svc_offline */ 109 &iser_conn_destroy, /* it_tgt_conn_destroy */ 110 &iser_tgt_conn_connect, /* it_tgt_conn_connect */ 111 &iser_conn_disconnect, /* it_tgt_conn_disconnect */ 112 &iser_ini_conn_create, /* it_ini_conn_create */ 113 &iser_conn_destroy, /* it_ini_conn_destroy */ 114 &iser_ini_conn_connect, /* it_ini_conn_connect */ 115 &iser_conn_disconnect, /* it_ini_conn_disconnect */ 116 &iser_declare_key_values /* it_declare_key_values */ 117 }; 118 119 /* 120 * iSER IDM transport capabilities 121 */ 122 idm_transport_caps_t iser_transport_caps = { 123 0 /* flags */ 124 }; 125 126 int 127 iser_idm_register() 128 { 129 idm_transport_attr_t attr; 130 idm_status_t status; 131 132 attr.type = IDM_TRANSPORT_TYPE_ISER; 133 attr.it_ops = &iser_transport_ops; 134 attr.it_caps = &iser_transport_caps; 135 136 status = idm_transport_register(&attr); 137 if (status != IDM_STATUS_SUCCESS) { 138 ISER_LOG(CE_WARN, "Failed to register iSER transport with IDM"); 139 return (DDI_FAILURE); 140 } 141 142 ISER_LOG(CE_NOTE, "Registered iSER transport with IDM"); 143 144 return (DDI_SUCCESS); 145 } 146 147 /* 148 * iser_ini_conn_create() 149 * Allocate an iSER initiator connection context 150 */ 151 static idm_status_t 152 iser_ini_conn_create(idm_conn_req_t *cr, idm_conn_t *ic) 153 { 154 iser_chan_t *iser_chan = NULL; 155 iser_conn_t *iser_conn; 156 157 /* Allocate and set up a connection handle */ 158 iser_conn = kmem_zalloc(sizeof (iser_conn_t), KM_SLEEP); 159 mutex_init(&iser_conn->ic_lock, NULL, MUTEX_DRIVER, NULL); 160 161 /* Allocate and open a channel to the target node */ 162 iser_chan = iser_channel_alloc(NULL, &cr->cr_ini_dst_addr); 163 if (iser_chan == NULL) { 164 ISER_LOG(CE_WARN, "iser: failed to allocate channel"); 165 mutex_destroy(&iser_conn->ic_lock); 166 kmem_free(iser_conn, sizeof (iser_conn_t)); 167 return (IDM_STATUS_FAIL); 168 } 169 170 /* 171 * The local IP and remote IP are filled in iser_channel_alloc. The 172 * remote port needs to be filled in from idm_conn_req_t. The local 173 * port is irrelevant. Internal representation of the port in the 174 * IDM sockaddr structure is in network byte order. IBT expects the 175 * port in host byte order. 176 */ 177 switch (cr->cr_ini_dst_addr.sin.sa_family) { 178 case AF_INET: 179 iser_chan->ic_rport = ntohs(cr->cr_ini_dst_addr.sin4.sin_port); 180 break; 181 case AF_INET6: 182 iser_chan->ic_rport = ntohs(cr->cr_ini_dst_addr.sin6.sin6_port); 183 break; 184 default: 185 iser_chan->ic_rport = ISCSI_LISTEN_PORT; 186 } 187 iser_chan->ic_lport = 0; 188 189 cv_init(&iser_conn->ic_stage_cv, NULL, CV_DEFAULT, NULL); 190 iser_conn->ic_type = ISER_CONN_TYPE_INI; 191 iser_conn->ic_stage = ISER_CONN_STAGE_ALLOCATED; 192 iser_conn->ic_chan = iser_chan; 193 iser_conn->ic_idmc = ic; 194 195 /* 196 * Set a pointer to the iser_conn in the iser_chan for easy 197 * access during CM event handling 198 */ 199 iser_chan->ic_conn = iser_conn; 200 201 /* Set the iSER conn handle in the IDM conn private handle */ 202 ic->ic_transport_private = (void *)iser_conn; 203 204 /* Set the transport header length */ 205 ic->ic_transport_hdrlen = ISER_HEADER_LENGTH; 206 207 return (IDM_STATUS_SUCCESS); 208 } 209 210 /* 211 * iser_internal_conn_destroy() 212 * Tear down iSER-specific connection resources. This is used below 213 * in iser_conn_destroy(), but also from the CM code when we may have 214 * some of the connection established, but not fully connected. 215 */ 216 void 217 iser_internal_conn_destroy(iser_conn_t *ic) 218 { 219 mutex_enter(&ic->ic_lock); 220 iser_channel_free(ic->ic_chan); 221 if ((ic->ic_type == ISER_CONN_TYPE_TGT) && 222 (ic->ic_stage == ISER_CONN_STAGE_ALLOCATED)) { 223 /* 224 * This is a target connection that has yet to be 225 * established. Free our reference on the target 226 * service handle. 227 */ 228 iser_tgt_svc_rele(ic->ic_idms->is_iser_svc); 229 } 230 cv_destroy(&ic->ic_stage_cv); 231 mutex_exit(&ic->ic_lock); 232 mutex_destroy(&ic->ic_lock); 233 kmem_free(ic, sizeof (iser_conn_t)); 234 } 235 236 /* 237 * iser_conn_destroy() 238 * Tear down an initiator or target connection. 239 */ 240 static void 241 iser_conn_destroy(idm_conn_t *ic) 242 { 243 iser_conn_t *iser_conn; 244 iser_conn = (iser_conn_t *)ic->ic_transport_private; 245 246 iser_internal_conn_destroy(iser_conn); 247 ic->ic_transport_private = NULL; 248 } 249 250 /* 251 * iser_ini_conn_connect() 252 * Establish the connection referred to by the handle previously allocated via 253 * iser_ini_conn_create(). 254 */ 255 static idm_status_t 256 iser_ini_conn_connect(idm_conn_t *ic) 257 { 258 iser_conn_t *iser_conn; 259 iser_status_t status; 260 261 iser_conn = (iser_conn_t *)ic->ic_transport_private; 262 263 status = iser_channel_open(iser_conn->ic_chan); 264 if (status != ISER_STATUS_SUCCESS) { 265 ISER_LOG(CE_WARN, "iser: failed to open channel"); 266 return (IDM_STATUS_FAIL); 267 } 268 269 /* 270 * Set the local and remote addresses in the idm conn handle. 271 */ 272 iser_ib_conv_ibtaddr2sockaddr(&ic->ic_laddr, 273 &iser_conn->ic_chan->ic_localip, iser_conn->ic_chan->ic_lport); 274 iser_ib_conv_ibtaddr2sockaddr(&ic->ic_raddr, 275 &iser_conn->ic_chan->ic_remoteip, iser_conn->ic_chan->ic_rport); 276 277 mutex_enter(&iser_conn->ic_lock); 278 /* Hold a reference on the IDM connection handle */ 279 idm_conn_hold(ic); 280 iser_conn->ic_stage = ISER_CONN_STAGE_IC_CONNECTED; 281 mutex_exit(&iser_conn->ic_lock); 282 283 return (IDM_STATUS_SUCCESS); 284 } 285 286 /* 287 * iser_conn_disconnect() 288 * Shutdown this iSER connection 289 */ 290 static void 291 iser_conn_disconnect(idm_conn_t *ic) 292 { 293 iser_conn_t *iser_conn; 294 295 iser_conn = (iser_conn_t *)ic->ic_transport_private; 296 297 mutex_enter(&iser_conn->ic_lock); 298 iser_conn->ic_stage = ISER_CONN_STAGE_CLOSING; 299 mutex_exit(&iser_conn->ic_lock); 300 301 /* Close the channel */ 302 iser_channel_close(iser_conn->ic_chan); 303 304 /* Free our reference held on the IDM conn handle, and set CLOSED */ 305 mutex_enter(&iser_conn->ic_lock); 306 idm_conn_rele(iser_conn->ic_idmc); 307 iser_conn->ic_stage = ISER_CONN_STAGE_CLOSED; 308 mutex_exit(&iser_conn->ic_lock); 309 } 310 311 /* 312 * iser_tgt_svc_create() 313 * Establish the CM service for inbound iSER service requests on the port 314 * indicated by sr->sr_port. 315 * idm_svc_req_t contains the service parameters. 316 */ 317 idm_status_t 318 iser_tgt_svc_create(idm_svc_req_t *sr, idm_svc_t *is) 319 { 320 iser_svc_t *iser_svc; 321 322 int rc; 323 324 iser_svc = kmem_zalloc(sizeof (iser_svc_t), KM_SLEEP); 325 is->is_iser_svc = (void *)iser_svc; 326 327 idm_refcnt_init(&iser_svc->is_refcnt, iser_svc); 328 329 list_create(&iser_svc->is_sbindlist, sizeof (iser_sbind_t), 330 offsetof(iser_sbind_t, is_list_node)); 331 iser_svc->is_svcid = ibt_get_ip_sid(IPPROTO_TCP, sr->sr_port); 332 333 /* 334 * Register an iSER target service for the requested port 335 * and set the iser_svc structure in the idm_svc handle. 336 */ 337 rc = iser_register_service(is); 338 if (rc != DDI_SUCCESS) { 339 ISER_LOG(CE_NOTE, "iser_tgt_svc_create: iser_register_service " 340 "failed on port (%d): rc (0x%x)", sr->sr_port, rc); 341 (void) ibt_release_ip_sid(iser_svc->is_svcid); 342 list_destroy(&iser_svc->is_sbindlist); 343 idm_refcnt_destroy(&iser_svc->is_refcnt); 344 kmem_free(iser_svc, sizeof (iser_svc_t)); 345 return (IDM_STATUS_FAIL); 346 } 347 348 return (IDM_STATUS_SUCCESS); 349 } 350 351 /* IDM refcnt utilities for the iSER service handle */ 352 void 353 iser_tgt_svc_hold(iser_svc_t *is) 354 { 355 idm_refcnt_hold(&is->is_refcnt); 356 } 357 358 void 359 iser_tgt_svc_rele(iser_svc_t *is) 360 { 361 idm_refcnt_rele(&is->is_refcnt); 362 } 363 364 /* 365 * iser_tgt_svc_destroy() 366 * Teardown resources allocated in iser_tgt_svc_create() 367 */ 368 static void 369 iser_tgt_svc_destroy(idm_svc_t *is) 370 { 371 iser_svc_t *iser_svc; 372 373 iser_svc = (iser_svc_t *)is->is_iser_svc; 374 375 /* 376 * Deregister the iSER target service on this port and free 377 * the iser_svc structure from the idm_svc handle. 378 */ 379 iser_deregister_service(is); 380 381 /* Wait for the iSER service handle's refcnt to zero */ 382 idm_refcnt_wait_ref(&iser_svc->is_refcnt); 383 384 list_destroy(&iser_svc->is_sbindlist); 385 386 idm_refcnt_destroy(&iser_svc->is_refcnt); 387 388 kmem_free(iser_svc, sizeof (iser_svc_t)); 389 } 390 391 /* 392 * iser_tgt_svc_online() 393 * Bind the CM service allocated via iser_tgt_svc_create(). 394 */ 395 static idm_status_t 396 iser_tgt_svc_online(idm_svc_t *is) 397 { 398 iser_status_t status; 399 400 mutex_enter(&is->is_mutex); 401 402 /* 403 * Pass the IDM service handle as the client private data for 404 * later use. 405 */ 406 status = iser_bind_service(is); 407 if (status != ISER_STATUS_SUCCESS) { 408 ISER_LOG(CE_NOTE, "iser_tgt_svc_online: failed bind service"); 409 mutex_exit(&is->is_mutex); 410 return (IDM_STATUS_FAIL); 411 } 412 413 mutex_exit(&is->is_mutex); 414 return (IDM_STATUS_SUCCESS); 415 } 416 417 /* 418 * iser_tgt_svc_offline 419 * Unbind the service on all available HCA ports. 420 */ 421 static void 422 iser_tgt_svc_offline(idm_svc_t *is) 423 { 424 mutex_enter(&is->is_mutex); 425 426 iser_unbind_service(is); 427 mutex_exit(&is->is_mutex); 428 429 } 430 431 /* 432 * iser_tgt_conn_connect() 433 * Establish the connection in ic, passed from idm_tgt_conn_finish(), which 434 * is invoked from the SM as a result of an inbound connection request. 435 */ 436 /* ARGSUSED */ 437 static idm_status_t 438 iser_tgt_conn_connect(idm_conn_t *ic) 439 { 440 /* No action required */ 441 return (IDM_STATUS_SUCCESS); 442 } 443 444 /* 445 * iser_tgt_enable_datamover() sets the transport private data on the 446 * idm_conn_t and move the conn stage to indicate logged in. 447 */ 448 static idm_status_t 449 iser_tgt_enable_datamover(idm_conn_t *ic) 450 { 451 iser_conn_t *iser_conn; 452 453 iser_conn = (iser_conn_t *)ic->ic_transport_private; 454 mutex_enter(&iser_conn->ic_lock); 455 456 iser_conn->ic_stage = ISER_CONN_STAGE_LOGGED_IN; 457 mutex_exit(&iser_conn->ic_lock); 458 459 return (IDM_STATUS_SUCCESS); 460 } 461 462 /* 463 * iser_ini_enable_datamover() is used by the iSCSI initator to request that a 464 * specified iSCSI connection be transitioned to iSER-assisted mode. 465 * In the case of iSER, the RDMA resources for a reliable connection have 466 * already been allocated at this time, and the 'RDMAExtensions' is set to 'Yes' 467 * so no further negotiations are required at this time. 468 * The initiator now sends the first iSER Message - 'Hello' to the target 469 * and waits for the 'HelloReply' Message from the target before directing 470 * the initiator to go into the Full Feature Phase. 471 * 472 * No transport op is required on the target side. 473 */ 474 static idm_status_t 475 iser_ini_enable_datamover(idm_conn_t *ic) 476 { 477 478 iser_conn_t *iser_conn; 479 clock_t delay; 480 int status; 481 482 iser_conn = (iser_conn_t *)ic->ic_transport_private; 483 484 mutex_enter(&iser_conn->ic_lock); 485 iser_conn->ic_stage = ISER_CONN_STAGE_HELLO_SENT; 486 mutex_exit(&iser_conn->ic_lock); 487 488 /* Send the iSER Hello Message to the target */ 489 status = iser_xfer_hello_msg(iser_conn->ic_chan); 490 if (status != ISER_STATUS_SUCCESS) { 491 492 mutex_enter(&iser_conn->ic_lock); 493 iser_conn->ic_stage = ISER_CONN_STAGE_HELLO_SENT_FAIL; 494 mutex_exit(&iser_conn->ic_lock); 495 496 return (IDM_STATUS_FAIL); 497 } 498 499 /* 500 * Acquire the iser_conn->ic_lock and wait for the iSER HelloReply 501 * Message from the target, i.e. iser_conn_stage_t to be set to 502 * ISER_CONN_STAGE_HELLOREPLY_RCV. If the handshake does not 503 * complete within a specified time period (.5s), then return failure. 504 * 505 */ 506 delay = ddi_get_lbolt() + drv_usectohz(500000); 507 508 mutex_enter(&iser_conn->ic_lock); 509 while ((iser_conn->ic_stage != ISER_CONN_STAGE_HELLOREPLY_RCV) && 510 (ddi_get_lbolt() < delay)) { 511 512 (void) cv_timedwait(&iser_conn->ic_stage_cv, 513 &iser_conn->ic_lock, delay); 514 } 515 516 switch (iser_conn->ic_stage) { 517 case ISER_CONN_STAGE_HELLOREPLY_RCV: 518 iser_conn->ic_stage = ISER_CONN_STAGE_LOGGED_IN; 519 mutex_exit(&iser_conn->ic_lock); 520 /* 521 * Return suceess to indicate that the initiator connection can 522 * go to the next phase - FFP 523 */ 524 return (IDM_STATUS_SUCCESS); 525 default: 526 iser_conn->ic_stage = ISER_CONN_STAGE_HELLOREPLY_RCV_FAIL; 527 mutex_exit(&iser_conn->ic_lock); 528 return (IDM_STATUS_FAIL); 529 530 } 531 532 /* STATEMENT_NEVER_REACHED */ 533 } 534 535 /* 536 * iser_free_task_rsrcs() 537 * This routine does not currently need to do anything. It is used in 538 * the sockets transport to explicitly complete any buffers on the task, 539 * but we can rely on our RCaP layer to finish up it's work without any 540 * intervention. 541 */ 542 /* ARGSUSED */ 543 idm_status_t 544 iser_free_task_rsrcs(idm_task_t *idt) 545 { 546 return (IDM_STATUS_SUCCESS); 547 } 548 549 /* 550 * iser_negotiate_key_values() validates the key values for this connection 551 */ 552 /* ARGSUSED */ 553 static kv_status_t 554 iser_negotiate_key_values(idm_conn_t *ic, nvlist_t *request_nvl, 555 nvlist_t *response_nvl, nvlist_t *negotiated_nvl) 556 { 557 kv_status_t kvrc = KV_HANDLED; 558 559 /* Process the request nvlist */ 560 kvrc = iser_process_request_nvlist(request_nvl, response_nvl, 561 negotiated_nvl); 562 563 /* We must be using RDMA, so set the flag on the ic handle */ 564 ic->ic_rdma_extensions = B_TRUE; 565 566 return (kvrc); 567 } 568 569 /* Process a list of key=value pairs from a login request */ 570 static kv_status_t 571 iser_process_request_nvlist(nvlist_t *request_nvl, nvlist_t *response_nvl, 572 nvlist_t *negotiated_nvl) 573 { 574 const idm_kv_xlate_t *ikvx; 575 char *nvp_name; 576 nvpair_t *nvp; 577 nvpair_t *next_nvp; 578 kv_status_t kvrc = KV_HANDLED; 579 boolean_t transit = B_TRUE; 580 581 /* Process the list */ 582 nvp = nvlist_next_nvpair(request_nvl, NULL); 583 while (nvp != NULL) { 584 next_nvp = nvlist_next_nvpair(request_nvl, nvp); 585 586 nvp_name = nvpair_name(nvp); 587 ikvx = idm_lookup_kv_xlate(nvp_name, strlen(nvp_name)); 588 589 kvrc = iser_handle_key(nvp, ikvx, request_nvl, response_nvl, 590 negotiated_nvl); 591 if (kvrc != KV_HANDLED) { 592 if (kvrc == KV_HANDLED_NO_TRANSIT) { 593 /* we countered, clear the transit flag */ 594 transit = B_FALSE; 595 } else { 596 /* error, bail out */ 597 break; 598 } 599 } 600 601 nvp = next_nvp; 602 } 603 /* 604 * If the current kv_status_t indicates success, we've handled 605 * the entire list. Explicitly set kvrc to NO_TRANSIT if we've 606 * cleared the transit flag along the way. 607 */ 608 if ((kvrc == KV_HANDLED) && (transit == B_FALSE)) { 609 kvrc = KV_HANDLED_NO_TRANSIT; 610 } 611 612 return (kvrc); 613 } 614 615 /* Handle a given list, boolean or numerical key=value pair */ 616 static kv_status_t 617 iser_handle_key(nvpair_t *nvp, const idm_kv_xlate_t *ikvx, 618 nvlist_t *request_nvl, nvlist_t *response_nvl, nvlist_t *negotiated_nvl) 619 { 620 kv_status_t kvrc = KV_UNHANDLED; 621 boolean_t bool_val; 622 uint64_t num_val; 623 int nvrc; 624 625 /* Retrieve values for booleans and numericals */ 626 switch (ikvx->ik_key_id) { 627 /* Booleans */ 628 case KI_RDMA_EXTENSIONS: 629 case KI_IMMEDIATE_DATA: 630 nvrc = nvpair_value_boolean_value(nvp, &bool_val); 631 ASSERT(nvrc == 0); 632 break; 633 /* Numericals */ 634 case KI_INITIATOR_RECV_DATA_SEGMENT_LENGTH: 635 case KI_TARGET_RECV_DATA_SEGMENT_LENGTH: 636 case KI_MAX_OUTSTANDING_UNEXPECTED_PDUS: 637 nvrc = nvpair_value_uint64(nvp, &num_val); 638 ASSERT(nvrc == 0); 639 break; 640 default: 641 break; 642 } 643 644 /* 645 * Now handle the values according to the key name. Keys not 646 * specifically handled here will be negotiated by the iscsi 647 * target. Negotiated values take effect when 648 * iser_notice_key_values gets called. 649 */ 650 switch (ikvx->ik_key_id) { 651 case KI_RDMA_EXTENSIONS: 652 /* Ensure "Yes" */ 653 kvrc = iser_handle_boolean(nvp, bool_val, ikvx, B_TRUE, 654 request_nvl, response_nvl, negotiated_nvl); 655 break; 656 case KI_TARGET_RECV_DATA_SEGMENT_LENGTH: 657 /* Validate the proposed value */ 658 kvrc = iser_handle_numerical(nvp, num_val, ikvx, 659 ISER_TARGET_RECV_DATA_SEGMENT_LENGTH_MIN, 660 ISER_TARGET_RECV_DATA_SEGMENT_LENGTH_MAX, 661 ISER_TARGET_RECV_DATA_SEGMENT_LENGTH_IMPL_MAX, 662 request_nvl, response_nvl, negotiated_nvl); 663 break; 664 case KI_INITIATOR_RECV_DATA_SEGMENT_LENGTH: 665 /* Validate the proposed value */ 666 kvrc = iser_handle_numerical(nvp, num_val, ikvx, 667 ISER_INITIATOR_RECV_DATA_SEGMENT_LENGTH_MIN, 668 ISER_INITIATOR_RECV_DATA_SEGMENT_LENGTH_MAX, 669 ISER_INITIATOR_RECV_DATA_SEGMENT_LENGTH_IMPL_MAX, 670 request_nvl, response_nvl, negotiated_nvl); 671 break; 672 case KI_IMMEDIATE_DATA: 673 /* Ensure "No" */ 674 kvrc = iser_handle_boolean(nvp, bool_val, ikvx, B_FALSE, 675 request_nvl, response_nvl, negotiated_nvl); 676 break; 677 case KI_MAX_OUTSTANDING_UNEXPECTED_PDUS: 678 /* Validate the proposed value */ 679 kvrc = iser_handle_numerical(nvp, num_val, ikvx, 680 ISER_MAX_OUTSTANDING_UNEXPECTED_PDUS_MIN, 681 ISER_MAX_OUTSTANDING_UNEXPECTED_PDUS_MAX, 682 ISER_MAX_OUTSTANDING_UNEXPECTED_PDUS_IMPL_MAX, 683 request_nvl, response_nvl, negotiated_nvl); 684 break; 685 default: 686 /* 687 * All other keys, including invalid keys, will be 688 * handled at the client layer. 689 */ 690 kvrc = KV_HANDLED; 691 break; 692 } 693 694 return (kvrc); 695 } 696 697 698 /* Validate a proposed boolean value, and set the alternate if necessary */ 699 static kv_status_t 700 iser_handle_boolean(nvpair_t *nvp, boolean_t value, const idm_kv_xlate_t *ikvx, 701 boolean_t iser_value, nvlist_t *request_nvl, nvlist_t *response_nvl, 702 nvlist_t *negotiated_nvl) 703 { 704 kv_status_t kvrc; 705 int nvrc; 706 boolean_t respond; 707 708 if (value != iser_value) { 709 /* 710 * Respond back to initiator with our value, and 711 * set the return value to unset the transit bit. 712 */ 713 value = iser_value; 714 kvrc = KV_HANDLED_NO_TRANSIT; 715 nvrc = 0; 716 respond = B_TRUE; 717 718 } else { 719 /* Add this to our negotiated values */ 720 nvrc = nvlist_add_nvpair(negotiated_nvl, nvp); 721 /* Respond if this is not a declarative */ 722 respond = (ikvx->ik_declarative == B_FALSE); 723 } 724 725 /* Response of Simple-value Negotiation */ 726 if (nvrc == 0 && respond) { 727 nvrc = nvlist_add_boolean_value(response_nvl, 728 ikvx->ik_key_name, value); 729 /* Remove from the request (we've handled it) */ 730 (void) nvlist_remove_all(request_nvl, ikvx->ik_key_name); 731 } 732 733 if (kvrc == KV_HANDLED_NO_TRANSIT) { 734 return (kvrc); 735 } 736 737 return (idm_nvstat_to_kvstat(nvrc)); 738 } 739 740 /* 741 * Validate a proposed value against the iSER and/or iSCSI RFC's minimum and 742 * maximum values, and set an alternate, if necessary. Note that the value 743 * 'iser_max_value" represents our implementation maximum (typically the max). 744 */ 745 static kv_status_t 746 iser_handle_numerical(nvpair_t *nvp, uint64_t value, const idm_kv_xlate_t *ikvx, 747 uint64_t min_value, uint64_t max_value, uint64_t iser_max_value, 748 nvlist_t *request_nvl, nvlist_t *response_nvl, nvlist_t *negotiated_nvl) 749 { 750 kv_status_t kvrc; 751 int nvrc; 752 boolean_t respond; 753 754 /* Validate against standard */ 755 if ((value < min_value) || (value > max_value)) { 756 kvrc = KV_VALUE_ERROR; 757 } else { 758 if (value > iser_max_value) { 759 /* 760 * Respond back to initiator with our value, and 761 * set the return value to unset the transit bit. 762 */ 763 value = iser_max_value; 764 kvrc = KV_HANDLED_NO_TRANSIT; 765 nvrc = 0; 766 respond = B_TRUE; 767 } else { 768 /* Add this to our negotiated values */ 769 nvrc = nvlist_add_nvpair(negotiated_nvl, nvp); 770 /* Respond if this is not a declarative */ 771 respond = (ikvx->ik_declarative == B_FALSE); 772 } 773 774 /* Response of Simple-value Negotiation */ 775 if (nvrc == 0 && respond) { 776 nvrc = nvlist_add_uint64(response_nvl, 777 ikvx->ik_key_name, value); 778 /* Remove from the request (we've handled it) */ 779 (void) nvlist_remove_all(request_nvl, 780 ikvx->ik_key_name); 781 } 782 } 783 784 if (kvrc == KV_HANDLED_NO_TRANSIT) { 785 return (kvrc); 786 } 787 788 return (idm_nvstat_to_kvstat(nvrc)); 789 } 790 791 /* 792 * iser_declare_key_values() declares the declarative key values for 793 * this connection. 794 */ 795 /* ARGSUSED */ 796 static kv_status_t 797 iser_declare_key_values(idm_conn_t *ic, nvlist_t *config_nvl, 798 nvlist_t *outgoing_nvl) 799 { 800 kv_status_t kvrc; 801 int nvrc = 0; 802 int rc; 803 uint64_t uint64_val; 804 805 if ((rc = nvlist_lookup_uint64(config_nvl, 806 ISER_KV_KEY_NAME_MAX_OUTSTANDING_PDU, &uint64_val)) != ENOENT) { 807 ASSERT(rc == 0); 808 if (outgoing_nvl) { 809 nvrc = nvlist_add_uint64(outgoing_nvl, 810 ISER_KV_KEY_NAME_MAX_OUTSTANDING_PDU, uint64_val); 811 } 812 } 813 kvrc = idm_nvstat_to_kvstat(nvrc); 814 return (kvrc); 815 } 816 817 /* 818 * iser_notice_key_values() activates the negotiated key values for 819 * this connection. 820 */ 821 static void 822 iser_notice_key_values(idm_conn_t *ic, nvlist_t *negotiated_nvl) 823 { 824 iser_conn_t *iser_conn; 825 boolean_t boolean_val; 826 uint64_t uint64_val; 827 int nvrc; 828 char *digest_choice_string; 829 830 iser_conn = (iser_conn_t *)ic->ic_transport_private; 831 832 /* 833 * Validate the final negotiated operational parameters, 834 * and save a copy. 835 */ 836 if ((nvrc = nvlist_lookup_string(negotiated_nvl, 837 "HeaderDigest", &digest_choice_string)) != ENOENT) { 838 ASSERT(nvrc == 0); 839 840 /* 841 * Per the iSER RFC, override the negotiated value with "None" 842 */ 843 iser_conn->ic_op_params.op_header_digest = B_FALSE; 844 } 845 846 if ((nvrc = nvlist_lookup_string(negotiated_nvl, 847 "DataDigest", &digest_choice_string)) != ENOENT) { 848 ASSERT(nvrc == 0); 849 850 /* 851 * Per the iSER RFC, override the negotiated value with "None" 852 */ 853 iser_conn->ic_op_params.op_data_digest = B_FALSE; 854 } 855 856 if ((nvrc = nvlist_lookup_boolean_value(negotiated_nvl, 857 "RDMAExtensions", &boolean_val)) != ENOENT) { 858 ASSERT(nvrc == 0); 859 iser_conn->ic_op_params.op_rdma_extensions = boolean_val; 860 } 861 862 if ((nvrc = nvlist_lookup_boolean_value(negotiated_nvl, 863 "OFMarker", &boolean_val)) != ENOENT) { 864 ASSERT(nvrc == 0); 865 /* 866 * Per the iSER RFC, override the negotiated value with "No" 867 */ 868 iser_conn->ic_op_params.op_ofmarker = B_FALSE; 869 } 870 871 if ((nvrc = nvlist_lookup_boolean_value(negotiated_nvl, 872 "IFMarker", &boolean_val)) != ENOENT) { 873 ASSERT(nvrc == 0); 874 /* 875 * Per the iSER RFC, override the negotiated value with "No" 876 */ 877 iser_conn->ic_op_params.op_ifmarker = B_FALSE; 878 } 879 880 if ((nvrc = nvlist_lookup_uint64(negotiated_nvl, 881 "TargetRecvDataSegmentLength", &uint64_val)) != ENOENT) { 882 ASSERT(nvrc == 0); 883 iser_conn->ic_op_params.op_target_recv_data_segment_length = 884 uint64_val; 885 } 886 887 if ((nvrc = nvlist_lookup_uint64(negotiated_nvl, 888 "InitiatorRecvDataSegmentLength", &uint64_val)) != ENOENT) { 889 ASSERT(nvrc == 0); 890 iser_conn->ic_op_params.op_initiator_recv_data_segment_length = 891 uint64_val; 892 } 893 894 if ((nvrc = nvlist_lookup_uint64(negotiated_nvl, 895 "MaxOutstandingUnexpectedPDUs", &uint64_val)) != ENOENT) { 896 ASSERT(nvrc == 0); 897 iser_conn->ic_op_params.op_max_outstanding_unexpected_pdus = 898 uint64_val; 899 } 900 901 /* Test boolean values which are required by RFC 5046 */ 902 #ifdef ISER_DEBUG 903 ASSERT(iser_conn->ic_op_params.op_rdma_extensions == B_TRUE); 904 ASSERT(iser_conn->ic_op_params.op_header_digest == B_FALSE); 905 ASSERT(iser_conn->ic_op_params.op_data_digest == B_FALSE); 906 ASSERT(iser_conn->ic_op_params.op_ofmarker == B_FALSE); 907 ASSERT(iser_conn->ic_op_params.op_ifmarker == B_FALSE); 908 #endif 909 } 910 911 912 /* 913 * iser_conn_is_capable() verifies that the passed connection is provided 914 * for by an iSER-capable link. 915 * NOTE: When utilizing InfiniBand RC as an RCaP, this routine will check 916 * if the link is on IPoIB. This only indicates a chance that the link is 917 * on an RCaP, and thus iSER-capable, since we may be running on an IB-Eth 918 * gateway, or other IB but non-RCaP link. Rather than fully establishing the 919 * link to verify RCaP here, we instead will return B_TRUE 920 * indicating the link is iSER-capable, if the link is IPoIB. If then in 921 * iser_ini_conn_create() the link proves not be RCaP, IDM will fall back 922 * to using the IDM Sockets transport. 923 */ 924 /* ARGSUSED */ 925 static boolean_t 926 iser_conn_is_capable(idm_conn_req_t *cr, idm_transport_caps_t *caps) 927 { 928 /* A NULL value for laddr indicates implicit source */ 929 return (iser_path_exists(NULL, &cr->cr_ini_dst_addr)); 930 } 931 932 /* 933 * iser_pdu_tx() transmits a Control PDU via the iSER channel. We pull the 934 * channel out of the idm_conn_t passed in, and pass it and the pdu to the 935 * iser_xfer routine. 936 */ 937 static void 938 iser_pdu_tx(idm_conn_t *ic, idm_pdu_t *pdu) 939 { 940 iser_conn_t *iser_conn; 941 iser_status_t iser_status; 942 943 iser_conn = (iser_conn_t *)ic->ic_transport_private; 944 945 iser_status = iser_xfer_ctrlpdu(iser_conn->ic_chan, pdu); 946 if (iser_status != ISER_STATUS_SUCCESS) { 947 ISER_LOG(CE_WARN, "iser_pdu_tx: failed iser_xfer_ctrlpdu: " 948 "ic (0x%p) pdu (0x%p)", (void *) ic, (void *) pdu); 949 /* Fail this PDU transmission */ 950 idm_pdu_complete(pdu, IDM_STATUS_FAIL); 951 } 952 953 /* 954 * We successfully posted this PDU for transmission. 955 * The completion handler will invoke idm_pdu_complete() 956 * with the completion status. See iser_cq.c for more 957 * information. 958 */ 959 } 960 961 /* 962 * iser_buf_tx_to_ini() transmits the data buffer encoded in idb to the 963 * initiator to fulfill SCSI Read commands. An iser_xfer routine is invoked 964 * to implement the RDMA operations. 965 * 966 * Caller holds idt->idt_mutex. 967 */ 968 static idm_status_t 969 iser_buf_tx_to_ini(idm_task_t *idt, idm_buf_t *idb) 970 { 971 iser_status_t iser_status; 972 idm_status_t idm_status = IDM_STATUS_SUCCESS; 973 974 ASSERT(mutex_owned(&idt->idt_mutex)); 975 976 iser_status = iser_xfer_buf_to_ini(idt, idb); 977 978 if (iser_status != ISER_STATUS_SUCCESS) { 979 ISER_LOG(CE_WARN, "iser_buf_tx_to_ini: failed " 980 "iser_xfer_buf_to_ini: idt (0x%p) idb (0x%p)", 981 (void *) idt, (void *) idb); 982 idm_buf_tx_to_ini_done(idt, idb, IDM_STATUS_ABORTED); 983 return (IDM_STATUS_FAIL); 984 } 985 986 /* 987 * iSCSIt's Data Completion Notify callback is invoked from 988 * the Work Request Send completion Handler 989 */ 990 991 mutex_exit(&idt->idt_mutex); 992 return (idm_status); 993 } 994 995 /* 996 * iser_buf_tx_from_ini() transmits data from the initiator into the buffer 997 * in idb to fulfill SCSI Write commands. An iser_xfer routine is invoked 998 * to implement the RDMA operations. 999 * 1000 * Caller holds idt->idt_mutex. 1001 */ 1002 static idm_status_t 1003 iser_buf_rx_from_ini(idm_task_t *idt, idm_buf_t *idb) 1004 { 1005 iser_status_t iser_status; 1006 idm_status_t idm_status = IDM_STATUS_SUCCESS; 1007 1008 ASSERT(mutex_owned(&idt->idt_mutex)); 1009 1010 iser_status = iser_xfer_buf_from_ini(idt, idb); 1011 1012 if (iser_status != ISER_STATUS_SUCCESS) { 1013 ISER_LOG(CE_WARN, "iser_buf_rx_from_ini: failed " 1014 "iser_xfer_buf_from_ini: idt (0x%p) idb (0x%p)", 1015 (void *) idt, (void *) idb); 1016 idm_buf_rx_from_ini_done(idt, idb, IDM_STATUS_ABORTED); 1017 return (IDM_STATUS_FAIL); 1018 } 1019 1020 /* 1021 * iSCSIt's Data Completion Notify callback is invoked from 1022 * the Work Request Send completion Handler 1023 */ 1024 1025 mutex_exit(&idt->idt_mutex); 1026 return (idm_status); 1027 } 1028 1029 /* 1030 * iser_buf_alloc() allocates a buffer and registers it with the IBTF for 1031 * use with iSER. Each HCA has it's own kmem cache for establishing a pool 1032 * of registered buffers, when once initially allocated, will remain 1033 * registered with the HCA. This routine is invoked only on the target, 1034 * where we have the requirement to pre-allocate buffers for the upper layers. 1035 * Note: buflen is compared to ISER_DEFAULT_BUFLEN, and allocation is failed 1036 * if the requested buflen is larger than our default. 1037 */ 1038 /* ARGSUSED */ 1039 static idm_status_t 1040 iser_buf_alloc(idm_buf_t *idb, uint64_t buflen) 1041 { 1042 iser_conn_t *iser_conn; 1043 iser_hca_t *iser_hca; 1044 iser_buf_t *iser_buf; 1045 1046 if (buflen > ISER_DEFAULT_BUFLEN) { 1047 return (IDM_STATUS_FAIL); 1048 } 1049 1050 iser_conn = (iser_conn_t *)idb->idb_ic->ic_transport_private; 1051 iser_hca = iser_conn->ic_chan->ic_hca; 1052 1053 /* 1054 * Allocate a buffer from this HCA's cache. Once initialized, these 1055 * will remain allocated and registered (see above). 1056 */ 1057 iser_buf = kmem_cache_alloc(iser_hca->iser_buf_cache, KM_NOSLEEP); 1058 if (iser_buf == NULL) { 1059 ISER_LOG(CE_NOTE, "iser_buf_alloc: alloc failed"); 1060 return (IDM_STATUS_FAIL); 1061 } 1062 1063 /* Set the allocated data buffer pointer in the IDM buf handle */ 1064 idb->idb_buf = iser_buf->buf; 1065 1066 /* Set the private buf and reg handles in the IDM buf handle */ 1067 idb->idb_buf_private = (void *)iser_buf; 1068 idb->idb_reg_private = (void *)iser_buf->iser_mr; 1069 1070 return (IDM_STATUS_SUCCESS); 1071 } 1072 1073 /* 1074 * iser_buf_free() frees the buffer handle passed in. Note that the cached 1075 * kmem object has an HCA-registered buffer in it which will not be freed. 1076 * This allows us to build up a cache of pre-allocated and registered 1077 * buffers for use on the target. 1078 */ 1079 static void 1080 iser_buf_free(idm_buf_t *buf) 1081 { 1082 iser_buf_t *iser_buf; 1083 1084 iser_buf = buf->idb_buf_private; 1085 kmem_cache_free(iser_buf->cache, iser_buf); 1086 } 1087 1088 /* 1089 * iser_buf_setup() is invoked on the initiator in order to register memory 1090 * on demand for use with the iSER layer. 1091 */ 1092 static idm_status_t 1093 iser_buf_setup(idm_buf_t *idb) 1094 { 1095 iser_conn_t *iser_conn; 1096 iser_chan_t *iser_chan; 1097 iser_hca_t *iser_hca; 1098 iser_buf_t *iser_buf; 1099 int status; 1100 1101 ASSERT(idb->idb_buf != NULL); 1102 1103 iser_conn = (iser_conn_t *)idb->idb_ic->ic_transport_private; 1104 ASSERT(iser_conn != NULL); 1105 1106 iser_hca = iser_conn->ic_chan->ic_hca; 1107 1108 iser_chan = iser_conn->ic_chan; 1109 ASSERT(iser_chan != NULL); 1110 1111 /* 1112 * Memory registration is known to be slow, so for small 1113 * transfers, use pre-registered memory buffers and just 1114 * copy the data into/from them at the appropriate time 1115 */ 1116 if (idb->idb_buflen < ISER_BCOPY_THRESHOLD) { 1117 iser_buf = 1118 kmem_cache_alloc(iser_hca->iser_buf_cache, KM_NOSLEEP); 1119 1120 if (iser_buf == NULL) { 1121 1122 /* Fail over to dynamic registration */ 1123 status = iser_reg_rdma_mem(iser_chan->ic_hca, idb); 1124 idb->idb_bufalloc = B_FALSE; 1125 return (status); 1126 } 1127 1128 /* 1129 * Set the allocated data buffer pointer in the IDM buf handle 1130 * Data is to be copied from/to this buffer using bcopy 1131 */ 1132 idb->idb_bufptr = idb->idb_buf; 1133 idb->idb_bufbcopy = B_TRUE; 1134 1135 idb->idb_buf = iser_buf->buf; 1136 1137 /* Set the private buf and reg handles in the IDM buf handle */ 1138 idb->idb_buf_private = (void *)iser_buf; 1139 idb->idb_reg_private = (void *)iser_buf->iser_mr; 1140 1141 /* Ensure bufalloc'd flag is set */ 1142 idb->idb_bufalloc = B_TRUE; 1143 1144 return (IDM_STATUS_SUCCESS); 1145 1146 } else { 1147 1148 /* Dynamically register the memory passed in on the idb */ 1149 status = iser_reg_rdma_mem(iser_chan->ic_hca, idb); 1150 1151 /* Ensure bufalloc'd flag is unset */ 1152 idb->idb_bufalloc = B_FALSE; 1153 1154 return (status); 1155 } 1156 } 1157 1158 /* 1159 * iser_buf_teardown() is invoked on the initiator in order to register memory 1160 * on demand for use with the iSER layer. 1161 */ 1162 static void 1163 iser_buf_teardown(idm_buf_t *idb) 1164 { 1165 iser_conn_t *iser_conn; 1166 1167 iser_conn = (iser_conn_t *)idb->idb_ic->ic_transport_private; 1168 1169 /* Deregister the memory passed in on the idb */ 1170 iser_dereg_rdma_mem(iser_conn->ic_chan->ic_hca, idb); 1171 } 1172