1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright(c) 2007 - 2008 Intel Corporation. All rights reserved. 4 * 5 * Maintained at www.Open-FCoE.org 6 */ 7 8 /* 9 * RPORT GENERAL INFO 10 * 11 * This file contains all processing regarding fc_rports. It contains the 12 * rport state machine and does all rport interaction with the transport class. 13 * There should be no other places in libfc that interact directly with the 14 * transport class in regards to adding and deleting rports. 15 * 16 * fc_rport's represent N_Port's within the fabric. 17 */ 18 19 /* 20 * RPORT LOCKING 21 * 22 * The rport should never hold the rport mutex and then attempt to acquire 23 * either the lport or disc mutexes. The rport's mutex is considered lesser 24 * than both the lport's mutex and the disc mutex. Refer to fc_lport.c for 25 * more comments on the hierarchy. 26 * 27 * The locking strategy is similar to the lport's strategy. The lock protects 28 * the rport's states and is held and released by the entry points to the rport 29 * block. All _enter_* functions correspond to rport states and expect the rport 30 * mutex to be locked before calling them. This means that rports only handle 31 * one request or response at a time, since they're not critical for the I/O 32 * path this potential over-use of the mutex is acceptable. 33 */ 34 35 /* 36 * RPORT REFERENCE COUNTING 37 * 38 * A rport reference should be taken when: 39 * - an rport is allocated 40 * - a workqueue item is scheduled 41 * - an ELS request is send 42 * The reference should be dropped when: 43 * - the workqueue function has finished 44 * - the ELS response is handled 45 * - an rport is removed 46 */ 47 48 #include <linux/kernel.h> 49 #include <linux/spinlock.h> 50 #include <linux/interrupt.h> 51 #include <linux/slab.h> 52 #include <linux/rcupdate.h> 53 #include <linux/timer.h> 54 #include <linux/workqueue.h> 55 #include <linux/export.h> 56 #include <linux/rculist.h> 57 58 #include <asm/unaligned.h> 59 60 #include <scsi/libfc.h> 61 #include <scsi/fc_encode.h> 62 63 #include "fc_libfc.h" 64 65 static struct workqueue_struct *rport_event_queue; 66 67 static void fc_rport_enter_flogi(struct fc_rport_priv *); 68 static void fc_rport_enter_plogi(struct fc_rport_priv *); 69 static void fc_rport_enter_prli(struct fc_rport_priv *); 70 static void fc_rport_enter_rtv(struct fc_rport_priv *); 71 static void fc_rport_enter_ready(struct fc_rport_priv *); 72 static void fc_rport_enter_logo(struct fc_rport_priv *); 73 static void fc_rport_enter_adisc(struct fc_rport_priv *); 74 75 static void fc_rport_recv_plogi_req(struct fc_lport *, struct fc_frame *); 76 static void fc_rport_recv_prli_req(struct fc_rport_priv *, struct fc_frame *); 77 static void fc_rport_recv_prlo_req(struct fc_rport_priv *, struct fc_frame *); 78 static void fc_rport_recv_logo_req(struct fc_lport *, struct fc_frame *); 79 static void fc_rport_timeout(struct work_struct *); 80 static void fc_rport_error(struct fc_rport_priv *, int); 81 static void fc_rport_error_retry(struct fc_rport_priv *, int); 82 static void fc_rport_work(struct work_struct *); 83 84 static const char *fc_rport_state_names[] = { 85 [RPORT_ST_INIT] = "Init", 86 [RPORT_ST_FLOGI] = "FLOGI", 87 [RPORT_ST_PLOGI_WAIT] = "PLOGI_WAIT", 88 [RPORT_ST_PLOGI] = "PLOGI", 89 [RPORT_ST_PRLI] = "PRLI", 90 [RPORT_ST_RTV] = "RTV", 91 [RPORT_ST_READY] = "Ready", 92 [RPORT_ST_ADISC] = "ADISC", 93 [RPORT_ST_DELETE] = "Delete", 94 }; 95 96 /** 97 * fc_rport_lookup() - Lookup a remote port by port_id 98 * @lport: The local port to lookup the remote port on 99 * @port_id: The remote port ID to look up 100 * 101 * The reference count of the fc_rport_priv structure is 102 * increased by one. 103 */ 104 struct fc_rport_priv *fc_rport_lookup(const struct fc_lport *lport, 105 u32 port_id) 106 { 107 struct fc_rport_priv *rdata = NULL, *tmp_rdata; 108 109 rcu_read_lock(); 110 list_for_each_entry_rcu(tmp_rdata, &lport->disc.rports, peers) 111 if (tmp_rdata->ids.port_id == port_id && 112 kref_get_unless_zero(&tmp_rdata->kref)) { 113 rdata = tmp_rdata; 114 break; 115 } 116 rcu_read_unlock(); 117 return rdata; 118 } 119 EXPORT_SYMBOL(fc_rport_lookup); 120 121 /** 122 * fc_rport_create() - Create a new remote port 123 * @lport: The local port this remote port will be associated with 124 * @ids: The identifiers for the new remote port 125 * 126 * The remote port will start in the INIT state. 127 */ 128 struct fc_rport_priv *fc_rport_create(struct fc_lport *lport, u32 port_id) 129 { 130 struct fc_rport_priv *rdata; 131 size_t rport_priv_size = sizeof(*rdata); 132 133 lockdep_assert_held(&lport->disc.disc_mutex); 134 135 rdata = fc_rport_lookup(lport, port_id); 136 if (rdata) { 137 kref_put(&rdata->kref, fc_rport_destroy); 138 return rdata; 139 } 140 141 if (lport->rport_priv_size > 0) 142 rport_priv_size = lport->rport_priv_size; 143 rdata = kzalloc(rport_priv_size, GFP_KERNEL); 144 if (!rdata) 145 return NULL; 146 147 rdata->ids.node_name = -1; 148 rdata->ids.port_name = -1; 149 rdata->ids.port_id = port_id; 150 rdata->ids.roles = FC_RPORT_ROLE_UNKNOWN; 151 152 kref_init(&rdata->kref); 153 mutex_init(&rdata->rp_mutex); 154 rdata->local_port = lport; 155 rdata->rp_state = RPORT_ST_INIT; 156 rdata->event = RPORT_EV_NONE; 157 rdata->flags = FC_RP_FLAGS_REC_SUPPORTED; 158 rdata->e_d_tov = lport->e_d_tov; 159 rdata->r_a_tov = lport->r_a_tov; 160 rdata->maxframe_size = FC_MIN_MAX_PAYLOAD; 161 INIT_DELAYED_WORK(&rdata->retry_work, fc_rport_timeout); 162 INIT_WORK(&rdata->event_work, fc_rport_work); 163 if (port_id != FC_FID_DIR_SERV) { 164 rdata->lld_event_callback = lport->tt.rport_event_callback; 165 list_add_rcu(&rdata->peers, &lport->disc.rports); 166 } 167 return rdata; 168 } 169 EXPORT_SYMBOL(fc_rport_create); 170 171 /** 172 * fc_rport_destroy() - Free a remote port after last reference is released 173 * @kref: The remote port's kref 174 */ 175 void fc_rport_destroy(struct kref *kref) 176 { 177 struct fc_rport_priv *rdata; 178 179 rdata = container_of(kref, struct fc_rport_priv, kref); 180 kfree_rcu(rdata, rcu); 181 } 182 EXPORT_SYMBOL(fc_rport_destroy); 183 184 /** 185 * fc_rport_state() - Return a string identifying the remote port's state 186 * @rdata: The remote port 187 */ 188 static const char *fc_rport_state(struct fc_rport_priv *rdata) 189 { 190 const char *cp; 191 192 cp = fc_rport_state_names[rdata->rp_state]; 193 if (!cp) 194 cp = "Unknown"; 195 return cp; 196 } 197 198 /** 199 * fc_set_rport_loss_tmo() - Set the remote port loss timeout 200 * @rport: The remote port that gets a new timeout value 201 * @timeout: The new timeout value (in seconds) 202 */ 203 void fc_set_rport_loss_tmo(struct fc_rport *rport, u32 timeout) 204 { 205 if (timeout) 206 rport->dev_loss_tmo = timeout; 207 else 208 rport->dev_loss_tmo = 1; 209 } 210 EXPORT_SYMBOL(fc_set_rport_loss_tmo); 211 212 /** 213 * fc_plogi_get_maxframe() - Get the maximum payload from the common service 214 * parameters in a FLOGI frame 215 * @flp: The FLOGI or PLOGI payload 216 * @maxval: The maximum frame size upper limit; this may be less than what 217 * is in the service parameters 218 */ 219 static unsigned int fc_plogi_get_maxframe(struct fc_els_flogi *flp, 220 unsigned int maxval) 221 { 222 unsigned int mfs; 223 224 /* 225 * Get max payload from the common service parameters and the 226 * class 3 receive data field size. 227 */ 228 mfs = ntohs(flp->fl_csp.sp_bb_data) & FC_SP_BB_DATA_MASK; 229 if (mfs >= FC_SP_MIN_MAX_PAYLOAD && mfs < maxval) 230 maxval = mfs; 231 mfs = ntohs(flp->fl_cssp[3 - 1].cp_rdfs); 232 if (mfs >= FC_SP_MIN_MAX_PAYLOAD && mfs < maxval) 233 maxval = mfs; 234 return maxval; 235 } 236 237 /** 238 * fc_rport_state_enter() - Change the state of a remote port 239 * @rdata: The remote port whose state should change 240 * @new: The new state 241 */ 242 static void fc_rport_state_enter(struct fc_rport_priv *rdata, 243 enum fc_rport_state new) 244 { 245 lockdep_assert_held(&rdata->rp_mutex); 246 247 if (rdata->rp_state != new) 248 rdata->retries = 0; 249 rdata->rp_state = new; 250 } 251 252 /** 253 * fc_rport_work() - Handler for remote port events in the rport_event_queue 254 * @work: Handle to the remote port being dequeued 255 * 256 * Reference counting: drops kref on return 257 */ 258 static void fc_rport_work(struct work_struct *work) 259 { 260 u32 port_id; 261 struct fc_rport_priv *rdata = 262 container_of(work, struct fc_rport_priv, event_work); 263 struct fc_rport_libfc_priv *rpriv; 264 enum fc_rport_event event; 265 struct fc_lport *lport = rdata->local_port; 266 struct fc_rport_operations *rport_ops; 267 struct fc_rport_identifiers ids; 268 struct fc_rport *rport; 269 struct fc4_prov *prov; 270 u8 type; 271 272 mutex_lock(&rdata->rp_mutex); 273 event = rdata->event; 274 rport_ops = rdata->ops; 275 rport = rdata->rport; 276 277 FC_RPORT_DBG(rdata, "work event %u\n", event); 278 279 switch (event) { 280 case RPORT_EV_READY: 281 ids = rdata->ids; 282 rdata->event = RPORT_EV_NONE; 283 rdata->major_retries = 0; 284 kref_get(&rdata->kref); 285 mutex_unlock(&rdata->rp_mutex); 286 287 if (!rport) { 288 FC_RPORT_DBG(rdata, "No rport!\n"); 289 rport = fc_remote_port_add(lport->host, 0, &ids); 290 } 291 if (!rport) { 292 FC_RPORT_DBG(rdata, "Failed to add the rport\n"); 293 fc_rport_logoff(rdata); 294 kref_put(&rdata->kref, fc_rport_destroy); 295 return; 296 } 297 mutex_lock(&rdata->rp_mutex); 298 if (rdata->rport) 299 FC_RPORT_DBG(rdata, "rport already allocated\n"); 300 rdata->rport = rport; 301 rport->maxframe_size = rdata->maxframe_size; 302 rport->supported_classes = rdata->supported_classes; 303 304 rpriv = rport->dd_data; 305 rpriv->local_port = lport; 306 rpriv->rp_state = rdata->rp_state; 307 rpriv->flags = rdata->flags; 308 rpriv->e_d_tov = rdata->e_d_tov; 309 rpriv->r_a_tov = rdata->r_a_tov; 310 mutex_unlock(&rdata->rp_mutex); 311 312 if (rport_ops && rport_ops->event_callback) { 313 FC_RPORT_DBG(rdata, "callback ev %d\n", event); 314 rport_ops->event_callback(lport, rdata, event); 315 } 316 if (rdata->lld_event_callback) { 317 FC_RPORT_DBG(rdata, "lld callback ev %d\n", event); 318 rdata->lld_event_callback(lport, rdata, event); 319 } 320 kref_put(&rdata->kref, fc_rport_destroy); 321 break; 322 323 case RPORT_EV_FAILED: 324 case RPORT_EV_LOGO: 325 case RPORT_EV_STOP: 326 if (rdata->prli_count) { 327 mutex_lock(&fc_prov_mutex); 328 for (type = 1; type < FC_FC4_PROV_SIZE; type++) { 329 prov = fc_passive_prov[type]; 330 if (prov && prov->prlo) 331 prov->prlo(rdata); 332 } 333 mutex_unlock(&fc_prov_mutex); 334 } 335 port_id = rdata->ids.port_id; 336 mutex_unlock(&rdata->rp_mutex); 337 338 if (rport_ops && rport_ops->event_callback) { 339 FC_RPORT_DBG(rdata, "callback ev %d\n", event); 340 rport_ops->event_callback(lport, rdata, event); 341 } 342 if (rdata->lld_event_callback) { 343 FC_RPORT_DBG(rdata, "lld callback ev %d\n", event); 344 rdata->lld_event_callback(lport, rdata, event); 345 } 346 if (cancel_delayed_work_sync(&rdata->retry_work)) 347 kref_put(&rdata->kref, fc_rport_destroy); 348 349 /* 350 * Reset any outstanding exchanges before freeing rport. 351 */ 352 lport->tt.exch_mgr_reset(lport, 0, port_id); 353 lport->tt.exch_mgr_reset(lport, port_id, 0); 354 355 if (rport) { 356 rpriv = rport->dd_data; 357 rpriv->rp_state = RPORT_ST_DELETE; 358 mutex_lock(&rdata->rp_mutex); 359 rdata->rport = NULL; 360 mutex_unlock(&rdata->rp_mutex); 361 fc_remote_port_delete(rport); 362 } 363 364 mutex_lock(&rdata->rp_mutex); 365 if (rdata->rp_state == RPORT_ST_DELETE) { 366 if (port_id == FC_FID_DIR_SERV) { 367 rdata->event = RPORT_EV_NONE; 368 mutex_unlock(&rdata->rp_mutex); 369 kref_put(&rdata->kref, fc_rport_destroy); 370 } else if ((rdata->flags & FC_RP_STARTED) && 371 rdata->major_retries < 372 lport->max_rport_retry_count) { 373 rdata->major_retries++; 374 rdata->event = RPORT_EV_NONE; 375 FC_RPORT_DBG(rdata, "work restart\n"); 376 fc_rport_enter_flogi(rdata); 377 mutex_unlock(&rdata->rp_mutex); 378 } else { 379 mutex_unlock(&rdata->rp_mutex); 380 FC_RPORT_DBG(rdata, "work delete\n"); 381 mutex_lock(&lport->disc.disc_mutex); 382 list_del_rcu(&rdata->peers); 383 mutex_unlock(&lport->disc.disc_mutex); 384 kref_put(&rdata->kref, fc_rport_destroy); 385 } 386 } else { 387 /* 388 * Re-open for events. Reissue READY event if ready. 389 */ 390 rdata->event = RPORT_EV_NONE; 391 if (rdata->rp_state == RPORT_ST_READY) { 392 FC_RPORT_DBG(rdata, "work reopen\n"); 393 fc_rport_enter_ready(rdata); 394 } 395 mutex_unlock(&rdata->rp_mutex); 396 } 397 break; 398 399 default: 400 mutex_unlock(&rdata->rp_mutex); 401 break; 402 } 403 kref_put(&rdata->kref, fc_rport_destroy); 404 } 405 406 /** 407 * fc_rport_login() - Start the remote port login state machine 408 * @rdata: The remote port to be logged in to 409 * 410 * Initiates the RP state machine. It is called from the LP module. 411 * This function will issue the following commands to the N_Port 412 * identified by the FC ID provided. 413 * 414 * - PLOGI 415 * - PRLI 416 * - RTV 417 * 418 * Locking Note: Called without the rport lock held. This 419 * function will hold the rport lock, call an _enter_* 420 * function and then unlock the rport. 421 * 422 * This indicates the intent to be logged into the remote port. 423 * If it appears we are already logged in, ADISC is used to verify 424 * the setup. 425 */ 426 int fc_rport_login(struct fc_rport_priv *rdata) 427 { 428 mutex_lock(&rdata->rp_mutex); 429 430 if (rdata->flags & FC_RP_STARTED) { 431 FC_RPORT_DBG(rdata, "port already started\n"); 432 mutex_unlock(&rdata->rp_mutex); 433 return 0; 434 } 435 436 rdata->flags |= FC_RP_STARTED; 437 switch (rdata->rp_state) { 438 case RPORT_ST_READY: 439 FC_RPORT_DBG(rdata, "ADISC port\n"); 440 fc_rport_enter_adisc(rdata); 441 break; 442 case RPORT_ST_DELETE: 443 FC_RPORT_DBG(rdata, "Restart deleted port\n"); 444 break; 445 case RPORT_ST_INIT: 446 FC_RPORT_DBG(rdata, "Login to port\n"); 447 fc_rport_enter_flogi(rdata); 448 break; 449 default: 450 FC_RPORT_DBG(rdata, "Login in progress, state %s\n", 451 fc_rport_state(rdata)); 452 break; 453 } 454 mutex_unlock(&rdata->rp_mutex); 455 456 return 0; 457 } 458 EXPORT_SYMBOL(fc_rport_login); 459 460 /** 461 * fc_rport_enter_delete() - Schedule a remote port to be deleted 462 * @rdata: The remote port to be deleted 463 * @event: The event to report as the reason for deletion 464 * 465 * Allow state change into DELETE only once. 466 * 467 * Call queue_work only if there's no event already pending. 468 * Set the new event so that the old pending event will not occur. 469 * Since we have the mutex, even if fc_rport_work() is already started, 470 * it'll see the new event. 471 * 472 * Reference counting: does not modify kref 473 */ 474 static void fc_rport_enter_delete(struct fc_rport_priv *rdata, 475 enum fc_rport_event event) 476 { 477 lockdep_assert_held(&rdata->rp_mutex); 478 479 if (rdata->rp_state == RPORT_ST_DELETE) 480 return; 481 482 FC_RPORT_DBG(rdata, "Delete port\n"); 483 484 fc_rport_state_enter(rdata, RPORT_ST_DELETE); 485 486 kref_get(&rdata->kref); 487 if (rdata->event == RPORT_EV_NONE && 488 !queue_work(rport_event_queue, &rdata->event_work)) 489 kref_put(&rdata->kref, fc_rport_destroy); 490 491 rdata->event = event; 492 } 493 494 /** 495 * fc_rport_logoff() - Logoff and remove a remote port 496 * @rdata: The remote port to be logged off of 497 * 498 * Locking Note: Called without the rport lock held. This 499 * function will hold the rport lock, call an _enter_* 500 * function and then unlock the rport. 501 */ 502 int fc_rport_logoff(struct fc_rport_priv *rdata) 503 { 504 struct fc_lport *lport = rdata->local_port; 505 u32 port_id = rdata->ids.port_id; 506 507 mutex_lock(&rdata->rp_mutex); 508 509 FC_RPORT_DBG(rdata, "Remove port\n"); 510 511 rdata->flags &= ~FC_RP_STARTED; 512 if (rdata->rp_state == RPORT_ST_DELETE) { 513 FC_RPORT_DBG(rdata, "Port in Delete state, not removing\n"); 514 goto out; 515 } 516 /* 517 * FC-LS states: 518 * To explicitly Logout, the initiating Nx_Port shall terminate 519 * other open Sequences that it initiated with the destination 520 * Nx_Port prior to performing Logout. 521 */ 522 lport->tt.exch_mgr_reset(lport, 0, port_id); 523 lport->tt.exch_mgr_reset(lport, port_id, 0); 524 525 fc_rport_enter_logo(rdata); 526 527 /* 528 * Change the state to Delete so that we discard 529 * the response. 530 */ 531 fc_rport_enter_delete(rdata, RPORT_EV_STOP); 532 out: 533 mutex_unlock(&rdata->rp_mutex); 534 return 0; 535 } 536 EXPORT_SYMBOL(fc_rport_logoff); 537 538 /** 539 * fc_rport_enter_ready() - Transition to the RPORT_ST_READY state 540 * @rdata: The remote port that is ready 541 * 542 * Reference counting: schedules workqueue, does not modify kref 543 */ 544 static void fc_rport_enter_ready(struct fc_rport_priv *rdata) 545 { 546 lockdep_assert_held(&rdata->rp_mutex); 547 548 fc_rport_state_enter(rdata, RPORT_ST_READY); 549 550 FC_RPORT_DBG(rdata, "Port is Ready\n"); 551 552 kref_get(&rdata->kref); 553 if (rdata->event == RPORT_EV_NONE && 554 !queue_work(rport_event_queue, &rdata->event_work)) 555 kref_put(&rdata->kref, fc_rport_destroy); 556 557 rdata->event = RPORT_EV_READY; 558 } 559 560 /** 561 * fc_rport_timeout() - Handler for the retry_work timer 562 * @work: Handle to the remote port that has timed out 563 * 564 * Locking Note: Called without the rport lock held. This 565 * function will hold the rport lock, call an _enter_* 566 * function and then unlock the rport. 567 * 568 * Reference counting: Drops kref on return. 569 */ 570 static void fc_rport_timeout(struct work_struct *work) 571 { 572 struct fc_rport_priv *rdata = 573 container_of(work, struct fc_rport_priv, retry_work.work); 574 575 mutex_lock(&rdata->rp_mutex); 576 FC_RPORT_DBG(rdata, "Port timeout, state %s\n", fc_rport_state(rdata)); 577 578 switch (rdata->rp_state) { 579 case RPORT_ST_FLOGI: 580 fc_rport_enter_flogi(rdata); 581 break; 582 case RPORT_ST_PLOGI: 583 fc_rport_enter_plogi(rdata); 584 break; 585 case RPORT_ST_PRLI: 586 fc_rport_enter_prli(rdata); 587 break; 588 case RPORT_ST_RTV: 589 fc_rport_enter_rtv(rdata); 590 break; 591 case RPORT_ST_ADISC: 592 fc_rport_enter_adisc(rdata); 593 break; 594 case RPORT_ST_PLOGI_WAIT: 595 case RPORT_ST_READY: 596 case RPORT_ST_INIT: 597 case RPORT_ST_DELETE: 598 break; 599 } 600 601 mutex_unlock(&rdata->rp_mutex); 602 kref_put(&rdata->kref, fc_rport_destroy); 603 } 604 605 /** 606 * fc_rport_error() - Error handler, called once retries have been exhausted 607 * @rdata: The remote port the error is happened on 608 * @err: The error code 609 * 610 * Reference counting: does not modify kref 611 */ 612 static void fc_rport_error(struct fc_rport_priv *rdata, int err) 613 { 614 struct fc_lport *lport = rdata->local_port; 615 616 lockdep_assert_held(&rdata->rp_mutex); 617 618 FC_RPORT_DBG(rdata, "Error %d in state %s, retries %d\n", 619 -err, fc_rport_state(rdata), rdata->retries); 620 621 switch (rdata->rp_state) { 622 case RPORT_ST_FLOGI: 623 rdata->flags &= ~FC_RP_STARTED; 624 fc_rport_enter_delete(rdata, RPORT_EV_FAILED); 625 break; 626 case RPORT_ST_PLOGI: 627 if (lport->point_to_multipoint) { 628 rdata->flags &= ~FC_RP_STARTED; 629 fc_rport_enter_delete(rdata, RPORT_EV_FAILED); 630 } else 631 fc_rport_enter_logo(rdata); 632 break; 633 case RPORT_ST_RTV: 634 fc_rport_enter_ready(rdata); 635 break; 636 case RPORT_ST_PRLI: 637 fc_rport_enter_plogi(rdata); 638 break; 639 case RPORT_ST_ADISC: 640 fc_rport_enter_logo(rdata); 641 break; 642 case RPORT_ST_PLOGI_WAIT: 643 case RPORT_ST_DELETE: 644 case RPORT_ST_READY: 645 case RPORT_ST_INIT: 646 break; 647 } 648 } 649 650 /** 651 * fc_rport_error_retry() - Handler for remote port state retries 652 * @rdata: The remote port whose state is to be retried 653 * @err: The error code 654 * 655 * If the error was an exchange timeout retry immediately, 656 * otherwise wait for E_D_TOV. 657 * 658 * Reference counting: increments kref when scheduling retry_work 659 */ 660 static void fc_rport_error_retry(struct fc_rport_priv *rdata, int err) 661 { 662 unsigned long delay = msecs_to_jiffies(rdata->e_d_tov); 663 664 lockdep_assert_held(&rdata->rp_mutex); 665 666 /* make sure this isn't an FC_EX_CLOSED error, never retry those */ 667 if (err == -FC_EX_CLOSED) 668 goto out; 669 670 if (rdata->retries < rdata->local_port->max_rport_retry_count) { 671 FC_RPORT_DBG(rdata, "Error %d in state %s, retrying\n", 672 err, fc_rport_state(rdata)); 673 rdata->retries++; 674 /* no additional delay on exchange timeouts */ 675 if (err == -FC_EX_TIMEOUT) 676 delay = 0; 677 kref_get(&rdata->kref); 678 if (!schedule_delayed_work(&rdata->retry_work, delay)) 679 kref_put(&rdata->kref, fc_rport_destroy); 680 return; 681 } 682 683 out: 684 fc_rport_error(rdata, err); 685 } 686 687 /** 688 * fc_rport_login_complete() - Handle parameters and completion of p-mp login. 689 * @rdata: The remote port which we logged into or which logged into us. 690 * @fp: The FLOGI or PLOGI request or response frame 691 * 692 * Returns non-zero error if a problem is detected with the frame. 693 * Does not free the frame. 694 * 695 * This is only used in point-to-multipoint mode for FIP currently. 696 */ 697 static int fc_rport_login_complete(struct fc_rport_priv *rdata, 698 struct fc_frame *fp) 699 { 700 struct fc_lport *lport = rdata->local_port; 701 struct fc_els_flogi *flogi; 702 unsigned int e_d_tov; 703 u16 csp_flags; 704 705 flogi = fc_frame_payload_get(fp, sizeof(*flogi)); 706 if (!flogi) 707 return -EINVAL; 708 709 csp_flags = ntohs(flogi->fl_csp.sp_features); 710 711 if (fc_frame_payload_op(fp) == ELS_FLOGI) { 712 if (csp_flags & FC_SP_FT_FPORT) { 713 FC_RPORT_DBG(rdata, "Fabric bit set in FLOGI\n"); 714 return -EINVAL; 715 } 716 } else { 717 718 /* 719 * E_D_TOV is not valid on an incoming FLOGI request. 720 */ 721 e_d_tov = ntohl(flogi->fl_csp.sp_e_d_tov); 722 if (csp_flags & FC_SP_FT_EDTR) 723 e_d_tov /= 1000000; 724 if (e_d_tov > rdata->e_d_tov) 725 rdata->e_d_tov = e_d_tov; 726 } 727 rdata->maxframe_size = fc_plogi_get_maxframe(flogi, lport->mfs); 728 return 0; 729 } 730 731 /** 732 * fc_rport_flogi_resp() - Handle response to FLOGI request for p-mp mode 733 * @sp: The sequence that the FLOGI was on 734 * @fp: The FLOGI response frame 735 * @rp_arg: The remote port that received the FLOGI response 736 */ 737 static void fc_rport_flogi_resp(struct fc_seq *sp, struct fc_frame *fp, 738 void *rp_arg) 739 { 740 struct fc_rport_priv *rdata = rp_arg; 741 struct fc_lport *lport = rdata->local_port; 742 struct fc_els_flogi *flogi; 743 unsigned int r_a_tov; 744 u8 opcode; 745 int err = 0; 746 747 FC_RPORT_DBG(rdata, "Received a FLOGI %s\n", 748 IS_ERR(fp) ? "error" : fc_els_resp_type(fp)); 749 750 if (fp == ERR_PTR(-FC_EX_CLOSED)) 751 goto put; 752 753 mutex_lock(&rdata->rp_mutex); 754 755 if (rdata->rp_state != RPORT_ST_FLOGI) { 756 FC_RPORT_DBG(rdata, "Received a FLOGI response, but in state " 757 "%s\n", fc_rport_state(rdata)); 758 if (IS_ERR(fp)) 759 goto err; 760 goto out; 761 } 762 763 if (IS_ERR(fp)) { 764 fc_rport_error(rdata, PTR_ERR(fp)); 765 goto err; 766 } 767 opcode = fc_frame_payload_op(fp); 768 if (opcode == ELS_LS_RJT) { 769 struct fc_els_ls_rjt *rjt; 770 771 rjt = fc_frame_payload_get(fp, sizeof(*rjt)); 772 FC_RPORT_DBG(rdata, "FLOGI ELS rejected, reason %x expl %x\n", 773 rjt->er_reason, rjt->er_explan); 774 err = -FC_EX_ELS_RJT; 775 goto bad; 776 } else if (opcode != ELS_LS_ACC) { 777 FC_RPORT_DBG(rdata, "FLOGI ELS invalid opcode %x\n", opcode); 778 err = -FC_EX_ELS_RJT; 779 goto bad; 780 } 781 if (fc_rport_login_complete(rdata, fp)) { 782 FC_RPORT_DBG(rdata, "FLOGI failed, no login\n"); 783 err = -FC_EX_INV_LOGIN; 784 goto bad; 785 } 786 787 flogi = fc_frame_payload_get(fp, sizeof(*flogi)); 788 if (!flogi) { 789 err = -FC_EX_ALLOC_ERR; 790 goto bad; 791 } 792 r_a_tov = ntohl(flogi->fl_csp.sp_r_a_tov); 793 if (r_a_tov > rdata->r_a_tov) 794 rdata->r_a_tov = r_a_tov; 795 796 if (rdata->ids.port_name < lport->wwpn) 797 fc_rport_enter_plogi(rdata); 798 else 799 fc_rport_state_enter(rdata, RPORT_ST_PLOGI_WAIT); 800 out: 801 fc_frame_free(fp); 802 err: 803 mutex_unlock(&rdata->rp_mutex); 804 put: 805 kref_put(&rdata->kref, fc_rport_destroy); 806 return; 807 bad: 808 FC_RPORT_DBG(rdata, "Bad FLOGI response\n"); 809 fc_rport_error_retry(rdata, err); 810 goto out; 811 } 812 813 /** 814 * fc_rport_enter_flogi() - Send a FLOGI request to the remote port for p-mp 815 * @rdata: The remote port to send a FLOGI to 816 * 817 * Reference counting: increments kref when sending ELS 818 */ 819 static void fc_rport_enter_flogi(struct fc_rport_priv *rdata) 820 { 821 struct fc_lport *lport = rdata->local_port; 822 struct fc_frame *fp; 823 824 lockdep_assert_held(&rdata->rp_mutex); 825 826 if (!lport->point_to_multipoint) 827 return fc_rport_enter_plogi(rdata); 828 829 FC_RPORT_DBG(rdata, "Entered FLOGI state from %s state\n", 830 fc_rport_state(rdata)); 831 832 fc_rport_state_enter(rdata, RPORT_ST_FLOGI); 833 834 fp = fc_frame_alloc(lport, sizeof(struct fc_els_flogi)); 835 if (!fp) 836 return fc_rport_error_retry(rdata, -FC_EX_ALLOC_ERR); 837 838 kref_get(&rdata->kref); 839 if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_FLOGI, 840 fc_rport_flogi_resp, rdata, 841 2 * lport->r_a_tov)) { 842 fc_rport_error_retry(rdata, -FC_EX_XMIT_ERR); 843 kref_put(&rdata->kref, fc_rport_destroy); 844 } 845 } 846 847 /** 848 * fc_rport_recv_flogi_req() - Handle Fabric Login (FLOGI) request in p-mp mode 849 * @lport: The local port that received the PLOGI request 850 * @rx_fp: The PLOGI request frame 851 * 852 * Reference counting: drops kref on return 853 */ 854 static void fc_rport_recv_flogi_req(struct fc_lport *lport, 855 struct fc_frame *rx_fp) 856 { 857 struct fc_els_flogi *flp; 858 struct fc_rport_priv *rdata; 859 struct fc_frame *fp = rx_fp; 860 struct fc_seq_els_data rjt_data; 861 u32 sid; 862 863 sid = fc_frame_sid(fp); 864 865 FC_RPORT_ID_DBG(lport, sid, "Received FLOGI request\n"); 866 867 if (!lport->point_to_multipoint) { 868 rjt_data.reason = ELS_RJT_UNSUP; 869 rjt_data.explan = ELS_EXPL_NONE; 870 goto reject; 871 } 872 873 flp = fc_frame_payload_get(fp, sizeof(*flp)); 874 if (!flp) { 875 rjt_data.reason = ELS_RJT_LOGIC; 876 rjt_data.explan = ELS_EXPL_INV_LEN; 877 goto reject; 878 } 879 880 rdata = fc_rport_lookup(lport, sid); 881 if (!rdata) { 882 rjt_data.reason = ELS_RJT_FIP; 883 rjt_data.explan = ELS_EXPL_NOT_NEIGHBOR; 884 goto reject; 885 } 886 mutex_lock(&rdata->rp_mutex); 887 888 FC_RPORT_DBG(rdata, "Received FLOGI in %s state\n", 889 fc_rport_state(rdata)); 890 891 switch (rdata->rp_state) { 892 case RPORT_ST_INIT: 893 /* 894 * If received the FLOGI request on RPORT which is INIT state 895 * (means not transition to FLOGI either fc_rport timeout 896 * function didn;t trigger or this end hasn;t received 897 * beacon yet from other end. In that case only, allow RPORT 898 * state machine to continue, otherwise fall through which 899 * causes the code to send reject response. 900 * NOTE; Not checking for FIP->state such as VNMP_UP or 901 * VNMP_CLAIM because if FIP state is not one of those, 902 * RPORT wouldn;t have created and 'rport_lookup' would have 903 * failed anyway in that case. 904 */ 905 break; 906 case RPORT_ST_DELETE: 907 mutex_unlock(&rdata->rp_mutex); 908 rjt_data.reason = ELS_RJT_FIP; 909 rjt_data.explan = ELS_EXPL_NOT_NEIGHBOR; 910 goto reject_put; 911 case RPORT_ST_FLOGI: 912 case RPORT_ST_PLOGI_WAIT: 913 case RPORT_ST_PLOGI: 914 break; 915 case RPORT_ST_PRLI: 916 case RPORT_ST_RTV: 917 case RPORT_ST_READY: 918 case RPORT_ST_ADISC: 919 /* 920 * Set the remote port to be deleted and to then restart. 921 * This queues work to be sure exchanges are reset. 922 */ 923 fc_rport_enter_delete(rdata, RPORT_EV_LOGO); 924 mutex_unlock(&rdata->rp_mutex); 925 rjt_data.reason = ELS_RJT_BUSY; 926 rjt_data.explan = ELS_EXPL_NONE; 927 goto reject_put; 928 } 929 if (fc_rport_login_complete(rdata, fp)) { 930 mutex_unlock(&rdata->rp_mutex); 931 rjt_data.reason = ELS_RJT_LOGIC; 932 rjt_data.explan = ELS_EXPL_NONE; 933 goto reject_put; 934 } 935 936 fp = fc_frame_alloc(lport, sizeof(*flp)); 937 if (!fp) 938 goto out; 939 940 fc_flogi_fill(lport, fp); 941 flp = fc_frame_payload_get(fp, sizeof(*flp)); 942 flp->fl_cmd = ELS_LS_ACC; 943 944 fc_fill_reply_hdr(fp, rx_fp, FC_RCTL_ELS_REP, 0); 945 lport->tt.frame_send(lport, fp); 946 947 /* 948 * Do not proceed with the state machine if our 949 * FLOGI has crossed with an FLOGI from the 950 * remote port; wait for the FLOGI response instead. 951 */ 952 if (rdata->rp_state != RPORT_ST_FLOGI) { 953 if (rdata->ids.port_name < lport->wwpn) 954 fc_rport_enter_plogi(rdata); 955 else 956 fc_rport_state_enter(rdata, RPORT_ST_PLOGI_WAIT); 957 } 958 out: 959 mutex_unlock(&rdata->rp_mutex); 960 kref_put(&rdata->kref, fc_rport_destroy); 961 fc_frame_free(rx_fp); 962 return; 963 964 reject_put: 965 kref_put(&rdata->kref, fc_rport_destroy); 966 reject: 967 fc_seq_els_rsp_send(rx_fp, ELS_LS_RJT, &rjt_data); 968 fc_frame_free(rx_fp); 969 } 970 971 /** 972 * fc_rport_plogi_resp() - Handler for ELS PLOGI responses 973 * @sp: The sequence the PLOGI is on 974 * @fp: The PLOGI response frame 975 * @rdata_arg: The remote port that sent the PLOGI response 976 * 977 * Locking Note: This function will be called without the rport lock 978 * held, but it will lock, call an _enter_* function or fc_rport_error 979 * and then unlock the rport. 980 */ 981 static void fc_rport_plogi_resp(struct fc_seq *sp, struct fc_frame *fp, 982 void *rdata_arg) 983 { 984 struct fc_rport_priv *rdata = rdata_arg; 985 struct fc_lport *lport = rdata->local_port; 986 struct fc_els_flogi *plp = NULL; 987 u16 csp_seq; 988 u16 cssp_seq; 989 u8 op; 990 991 FC_RPORT_DBG(rdata, "Received a PLOGI %s\n", fc_els_resp_type(fp)); 992 993 if (fp == ERR_PTR(-FC_EX_CLOSED)) 994 goto put; 995 996 mutex_lock(&rdata->rp_mutex); 997 998 if (rdata->rp_state != RPORT_ST_PLOGI) { 999 FC_RPORT_DBG(rdata, "Received a PLOGI response, but in state " 1000 "%s\n", fc_rport_state(rdata)); 1001 if (IS_ERR(fp)) 1002 goto err; 1003 goto out; 1004 } 1005 1006 if (IS_ERR(fp)) { 1007 fc_rport_error_retry(rdata, PTR_ERR(fp)); 1008 goto err; 1009 } 1010 1011 op = fc_frame_payload_op(fp); 1012 if (op == ELS_LS_ACC && 1013 (plp = fc_frame_payload_get(fp, sizeof(*plp))) != NULL) { 1014 rdata->ids.port_name = get_unaligned_be64(&plp->fl_wwpn); 1015 rdata->ids.node_name = get_unaligned_be64(&plp->fl_wwnn); 1016 1017 /* save plogi response sp_features for further reference */ 1018 rdata->sp_features = ntohs(plp->fl_csp.sp_features); 1019 1020 if (lport->point_to_multipoint) 1021 fc_rport_login_complete(rdata, fp); 1022 csp_seq = ntohs(plp->fl_csp.sp_tot_seq); 1023 cssp_seq = ntohs(plp->fl_cssp[3 - 1].cp_con_seq); 1024 if (cssp_seq < csp_seq) 1025 csp_seq = cssp_seq; 1026 rdata->max_seq = csp_seq; 1027 rdata->maxframe_size = fc_plogi_get_maxframe(plp, lport->mfs); 1028 fc_rport_enter_prli(rdata); 1029 } else { 1030 struct fc_els_ls_rjt *rjt; 1031 1032 rjt = fc_frame_payload_get(fp, sizeof(*rjt)); 1033 if (!rjt) 1034 FC_RPORT_DBG(rdata, "PLOGI bad response\n"); 1035 else 1036 FC_RPORT_DBG(rdata, "PLOGI ELS rejected, reason %x expl %x\n", 1037 rjt->er_reason, rjt->er_explan); 1038 fc_rport_error_retry(rdata, -FC_EX_ELS_RJT); 1039 } 1040 out: 1041 fc_frame_free(fp); 1042 err: 1043 mutex_unlock(&rdata->rp_mutex); 1044 put: 1045 kref_put(&rdata->kref, fc_rport_destroy); 1046 } 1047 1048 static bool 1049 fc_rport_compatible_roles(struct fc_lport *lport, struct fc_rport_priv *rdata) 1050 { 1051 if (rdata->ids.roles == FC_PORT_ROLE_UNKNOWN) 1052 return true; 1053 if ((rdata->ids.roles & FC_PORT_ROLE_FCP_TARGET) && 1054 (lport->service_params & FCP_SPPF_INIT_FCN)) 1055 return true; 1056 if ((rdata->ids.roles & FC_PORT_ROLE_FCP_INITIATOR) && 1057 (lport->service_params & FCP_SPPF_TARG_FCN)) 1058 return true; 1059 return false; 1060 } 1061 1062 /** 1063 * fc_rport_enter_plogi() - Send Port Login (PLOGI) request 1064 * @rdata: The remote port to send a PLOGI to 1065 * 1066 * Reference counting: increments kref when sending ELS 1067 */ 1068 static void fc_rport_enter_plogi(struct fc_rport_priv *rdata) 1069 { 1070 struct fc_lport *lport = rdata->local_port; 1071 struct fc_frame *fp; 1072 1073 lockdep_assert_held(&rdata->rp_mutex); 1074 1075 if (!fc_rport_compatible_roles(lport, rdata)) { 1076 FC_RPORT_DBG(rdata, "PLOGI suppressed for incompatible role\n"); 1077 fc_rport_state_enter(rdata, RPORT_ST_PLOGI_WAIT); 1078 return; 1079 } 1080 1081 FC_RPORT_DBG(rdata, "Port entered PLOGI state from %s state\n", 1082 fc_rport_state(rdata)); 1083 1084 fc_rport_state_enter(rdata, RPORT_ST_PLOGI); 1085 1086 rdata->maxframe_size = FC_MIN_MAX_PAYLOAD; 1087 fp = fc_frame_alloc(lport, sizeof(struct fc_els_flogi)); 1088 if (!fp) { 1089 FC_RPORT_DBG(rdata, "%s frame alloc failed\n", __func__); 1090 fc_rport_error_retry(rdata, -FC_EX_ALLOC_ERR); 1091 return; 1092 } 1093 rdata->e_d_tov = lport->e_d_tov; 1094 1095 kref_get(&rdata->kref); 1096 if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_PLOGI, 1097 fc_rport_plogi_resp, rdata, 1098 2 * lport->r_a_tov)) { 1099 fc_rport_error_retry(rdata, -FC_EX_XMIT_ERR); 1100 kref_put(&rdata->kref, fc_rport_destroy); 1101 } 1102 } 1103 1104 /** 1105 * fc_rport_prli_resp() - Process Login (PRLI) response handler 1106 * @sp: The sequence the PRLI response was on 1107 * @fp: The PRLI response frame 1108 * @rdata_arg: The remote port that sent the PRLI response 1109 * 1110 * Locking Note: This function will be called without the rport lock 1111 * held, but it will lock, call an _enter_* function or fc_rport_error 1112 * and then unlock the rport. 1113 */ 1114 static void fc_rport_prli_resp(struct fc_seq *sp, struct fc_frame *fp, 1115 void *rdata_arg) 1116 { 1117 struct fc_rport_priv *rdata = rdata_arg; 1118 struct { 1119 struct fc_els_prli prli; 1120 struct fc_els_spp spp; 1121 } *pp; 1122 struct fc_els_spp temp_spp; 1123 struct fc_els_ls_rjt *rjt; 1124 struct fc4_prov *prov; 1125 u32 roles = FC_RPORT_ROLE_UNKNOWN; 1126 u32 fcp_parm = 0; 1127 u8 op; 1128 enum fc_els_spp_resp resp_code; 1129 1130 FC_RPORT_DBG(rdata, "Received a PRLI %s\n", fc_els_resp_type(fp)); 1131 1132 if (fp == ERR_PTR(-FC_EX_CLOSED)) 1133 goto put; 1134 1135 mutex_lock(&rdata->rp_mutex); 1136 1137 if (rdata->rp_state != RPORT_ST_PRLI) { 1138 FC_RPORT_DBG(rdata, "Received a PRLI response, but in state " 1139 "%s\n", fc_rport_state(rdata)); 1140 if (IS_ERR(fp)) 1141 goto err; 1142 goto out; 1143 } 1144 1145 if (IS_ERR(fp)) { 1146 fc_rport_error_retry(rdata, PTR_ERR(fp)); 1147 goto err; 1148 } 1149 1150 /* reinitialize remote port roles */ 1151 rdata->ids.roles = FC_RPORT_ROLE_UNKNOWN; 1152 1153 op = fc_frame_payload_op(fp); 1154 if (op == ELS_LS_ACC) { 1155 pp = fc_frame_payload_get(fp, sizeof(*pp)); 1156 if (!pp) { 1157 fc_rport_error_retry(rdata, -FC_EX_SEQ_ERR); 1158 goto out; 1159 } 1160 1161 resp_code = (pp->spp.spp_flags & FC_SPP_RESP_MASK); 1162 FC_RPORT_DBG(rdata, "PRLI spp_flags = 0x%x spp_type 0x%x\n", 1163 pp->spp.spp_flags, pp->spp.spp_type); 1164 rdata->spp_type = pp->spp.spp_type; 1165 if (resp_code != FC_SPP_RESP_ACK) { 1166 if (resp_code == FC_SPP_RESP_CONF) 1167 fc_rport_error(rdata, -FC_EX_SEQ_ERR); 1168 else 1169 fc_rport_error_retry(rdata, -FC_EX_SEQ_ERR); 1170 goto out; 1171 } 1172 if (pp->prli.prli_spp_len < sizeof(pp->spp)) { 1173 fc_rport_error_retry(rdata, -FC_EX_SEQ_ERR); 1174 goto out; 1175 } 1176 1177 fcp_parm = ntohl(pp->spp.spp_params); 1178 if (fcp_parm & FCP_SPPF_RETRY) 1179 rdata->flags |= FC_RP_FLAGS_RETRY; 1180 if (fcp_parm & FCP_SPPF_CONF_COMPL) 1181 rdata->flags |= FC_RP_FLAGS_CONF_REQ; 1182 1183 /* 1184 * Call prli provider if we should act as a target 1185 */ 1186 prov = fc_passive_prov[rdata->spp_type]; 1187 if (prov) { 1188 memset(&temp_spp, 0, sizeof(temp_spp)); 1189 prov->prli(rdata, pp->prli.prli_spp_len, 1190 &pp->spp, &temp_spp); 1191 } 1192 /* 1193 * Check if the image pair could be established 1194 */ 1195 if (rdata->spp_type != FC_TYPE_FCP || 1196 !(pp->spp.spp_flags & FC_SPP_EST_IMG_PAIR)) { 1197 /* 1198 * Nope; we can't use this port as a target. 1199 */ 1200 fcp_parm &= ~FCP_SPPF_TARG_FCN; 1201 } 1202 rdata->supported_classes = FC_COS_CLASS3; 1203 if (fcp_parm & FCP_SPPF_INIT_FCN) 1204 roles |= FC_RPORT_ROLE_FCP_INITIATOR; 1205 if (fcp_parm & FCP_SPPF_TARG_FCN) 1206 roles |= FC_RPORT_ROLE_FCP_TARGET; 1207 1208 rdata->ids.roles = roles; 1209 fc_rport_enter_rtv(rdata); 1210 1211 } else { 1212 rjt = fc_frame_payload_get(fp, sizeof(*rjt)); 1213 if (!rjt) 1214 FC_RPORT_DBG(rdata, "PRLI bad response\n"); 1215 else { 1216 FC_RPORT_DBG(rdata, "PRLI ELS rejected, reason %x expl %x\n", 1217 rjt->er_reason, rjt->er_explan); 1218 if (rjt->er_reason == ELS_RJT_UNAB && 1219 rjt->er_explan == ELS_EXPL_PLOGI_REQD) { 1220 fc_rport_enter_plogi(rdata); 1221 goto out; 1222 } 1223 } 1224 fc_rport_error_retry(rdata, FC_EX_ELS_RJT); 1225 } 1226 1227 out: 1228 fc_frame_free(fp); 1229 err: 1230 mutex_unlock(&rdata->rp_mutex); 1231 put: 1232 kref_put(&rdata->kref, fc_rport_destroy); 1233 } 1234 1235 /** 1236 * fc_rport_enter_prli() - Send Process Login (PRLI) request 1237 * @rdata: The remote port to send the PRLI request to 1238 * 1239 * Reference counting: increments kref when sending ELS 1240 */ 1241 static void fc_rport_enter_prli(struct fc_rport_priv *rdata) 1242 { 1243 struct fc_lport *lport = rdata->local_port; 1244 struct { 1245 struct fc_els_prli prli; 1246 struct fc_els_spp spp; 1247 } *pp; 1248 struct fc_frame *fp; 1249 struct fc4_prov *prov; 1250 1251 lockdep_assert_held(&rdata->rp_mutex); 1252 1253 /* 1254 * If the rport is one of the well known addresses 1255 * we skip PRLI and RTV and go straight to READY. 1256 */ 1257 if (rdata->ids.port_id >= FC_FID_DOM_MGR) { 1258 fc_rport_enter_ready(rdata); 1259 return; 1260 } 1261 1262 /* 1263 * And if the local port does not support the initiator function 1264 * there's no need to send a PRLI, either. 1265 */ 1266 if (!(lport->service_params & FCP_SPPF_INIT_FCN)) { 1267 fc_rport_enter_ready(rdata); 1268 return; 1269 } 1270 1271 FC_RPORT_DBG(rdata, "Port entered PRLI state from %s state\n", 1272 fc_rport_state(rdata)); 1273 1274 fc_rport_state_enter(rdata, RPORT_ST_PRLI); 1275 1276 fp = fc_frame_alloc(lport, sizeof(*pp)); 1277 if (!fp) { 1278 fc_rport_error_retry(rdata, -FC_EX_ALLOC_ERR); 1279 return; 1280 } 1281 1282 fc_prli_fill(lport, fp); 1283 1284 prov = fc_passive_prov[FC_TYPE_FCP]; 1285 if (prov) { 1286 pp = fc_frame_payload_get(fp, sizeof(*pp)); 1287 prov->prli(rdata, sizeof(pp->spp), NULL, &pp->spp); 1288 } 1289 1290 fc_fill_fc_hdr(fp, FC_RCTL_ELS_REQ, rdata->ids.port_id, 1291 fc_host_port_id(lport->host), FC_TYPE_ELS, 1292 FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT, 0); 1293 1294 kref_get(&rdata->kref); 1295 if (!fc_exch_seq_send(lport, fp, fc_rport_prli_resp, 1296 NULL, rdata, 2 * lport->r_a_tov)) { 1297 fc_rport_error_retry(rdata, -FC_EX_XMIT_ERR); 1298 kref_put(&rdata->kref, fc_rport_destroy); 1299 } 1300 } 1301 1302 /** 1303 * fc_rport_rtv_resp() - Handler for Request Timeout Value (RTV) responses 1304 * @sp: The sequence the RTV was on 1305 * @fp: The RTV response frame 1306 * @rdata_arg: The remote port that sent the RTV response 1307 * 1308 * Many targets don't seem to support this. 1309 * 1310 * Locking Note: This function will be called without the rport lock 1311 * held, but it will lock, call an _enter_* function or fc_rport_error 1312 * and then unlock the rport. 1313 */ 1314 static void fc_rport_rtv_resp(struct fc_seq *sp, struct fc_frame *fp, 1315 void *rdata_arg) 1316 { 1317 struct fc_rport_priv *rdata = rdata_arg; 1318 u8 op; 1319 1320 FC_RPORT_DBG(rdata, "Received a RTV %s\n", fc_els_resp_type(fp)); 1321 1322 if (fp == ERR_PTR(-FC_EX_CLOSED)) 1323 goto put; 1324 1325 mutex_lock(&rdata->rp_mutex); 1326 1327 if (rdata->rp_state != RPORT_ST_RTV) { 1328 FC_RPORT_DBG(rdata, "Received a RTV response, but in state " 1329 "%s\n", fc_rport_state(rdata)); 1330 if (IS_ERR(fp)) 1331 goto err; 1332 goto out; 1333 } 1334 1335 if (IS_ERR(fp)) { 1336 fc_rport_error(rdata, PTR_ERR(fp)); 1337 goto err; 1338 } 1339 1340 op = fc_frame_payload_op(fp); 1341 if (op == ELS_LS_ACC) { 1342 struct fc_els_rtv_acc *rtv; 1343 u32 toq; 1344 u32 tov; 1345 1346 rtv = fc_frame_payload_get(fp, sizeof(*rtv)); 1347 if (rtv) { 1348 toq = ntohl(rtv->rtv_toq); 1349 tov = ntohl(rtv->rtv_r_a_tov); 1350 if (tov == 0) 1351 tov = 1; 1352 if (tov > rdata->r_a_tov) 1353 rdata->r_a_tov = tov; 1354 tov = ntohl(rtv->rtv_e_d_tov); 1355 if (toq & FC_ELS_RTV_EDRES) 1356 tov /= 1000000; 1357 if (tov == 0) 1358 tov = 1; 1359 if (tov > rdata->e_d_tov) 1360 rdata->e_d_tov = tov; 1361 } 1362 } 1363 1364 fc_rport_enter_ready(rdata); 1365 1366 out: 1367 fc_frame_free(fp); 1368 err: 1369 mutex_unlock(&rdata->rp_mutex); 1370 put: 1371 kref_put(&rdata->kref, fc_rport_destroy); 1372 } 1373 1374 /** 1375 * fc_rport_enter_rtv() - Send Request Timeout Value (RTV) request 1376 * @rdata: The remote port to send the RTV request to 1377 * 1378 * Reference counting: increments kref when sending ELS 1379 */ 1380 static void fc_rport_enter_rtv(struct fc_rport_priv *rdata) 1381 { 1382 struct fc_frame *fp; 1383 struct fc_lport *lport = rdata->local_port; 1384 1385 lockdep_assert_held(&rdata->rp_mutex); 1386 1387 FC_RPORT_DBG(rdata, "Port entered RTV state from %s state\n", 1388 fc_rport_state(rdata)); 1389 1390 fc_rport_state_enter(rdata, RPORT_ST_RTV); 1391 1392 fp = fc_frame_alloc(lport, sizeof(struct fc_els_rtv)); 1393 if (!fp) { 1394 fc_rport_error_retry(rdata, -FC_EX_ALLOC_ERR); 1395 return; 1396 } 1397 1398 kref_get(&rdata->kref); 1399 if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_RTV, 1400 fc_rport_rtv_resp, rdata, 1401 2 * lport->r_a_tov)) { 1402 fc_rport_error_retry(rdata, -FC_EX_XMIT_ERR); 1403 kref_put(&rdata->kref, fc_rport_destroy); 1404 } 1405 } 1406 1407 /** 1408 * fc_rport_recv_rtv_req() - Handler for Read Timeout Value (RTV) requests 1409 * @rdata: The remote port that sent the RTV request 1410 * @in_fp: The RTV request frame 1411 */ 1412 static void fc_rport_recv_rtv_req(struct fc_rport_priv *rdata, 1413 struct fc_frame *in_fp) 1414 { 1415 struct fc_lport *lport = rdata->local_port; 1416 struct fc_frame *fp; 1417 struct fc_els_rtv_acc *rtv; 1418 struct fc_seq_els_data rjt_data; 1419 1420 lockdep_assert_held(&rdata->rp_mutex); 1421 lockdep_assert_held(&lport->lp_mutex); 1422 1423 FC_RPORT_DBG(rdata, "Received RTV request\n"); 1424 1425 fp = fc_frame_alloc(lport, sizeof(*rtv)); 1426 if (!fp) { 1427 rjt_data.reason = ELS_RJT_UNAB; 1428 rjt_data.explan = ELS_EXPL_INSUF_RES; 1429 fc_seq_els_rsp_send(in_fp, ELS_LS_RJT, &rjt_data); 1430 goto drop; 1431 } 1432 rtv = fc_frame_payload_get(fp, sizeof(*rtv)); 1433 rtv->rtv_cmd = ELS_LS_ACC; 1434 rtv->rtv_r_a_tov = htonl(lport->r_a_tov); 1435 rtv->rtv_e_d_tov = htonl(lport->e_d_tov); 1436 rtv->rtv_toq = 0; 1437 fc_fill_reply_hdr(fp, in_fp, FC_RCTL_ELS_REP, 0); 1438 lport->tt.frame_send(lport, fp); 1439 drop: 1440 fc_frame_free(in_fp); 1441 } 1442 1443 /** 1444 * fc_rport_logo_resp() - Handler for logout (LOGO) responses 1445 * @sp: The sequence the LOGO was on 1446 * @fp: The LOGO response frame 1447 * @lport_arg: The local port 1448 */ 1449 static void fc_rport_logo_resp(struct fc_seq *sp, struct fc_frame *fp, 1450 void *rdata_arg) 1451 { 1452 struct fc_rport_priv *rdata = rdata_arg; 1453 struct fc_lport *lport = rdata->local_port; 1454 1455 FC_RPORT_ID_DBG(lport, fc_seq_exch(sp)->did, 1456 "Received a LOGO %s\n", fc_els_resp_type(fp)); 1457 if (!IS_ERR(fp)) 1458 fc_frame_free(fp); 1459 kref_put(&rdata->kref, fc_rport_destroy); 1460 } 1461 1462 /** 1463 * fc_rport_enter_logo() - Send a logout (LOGO) request 1464 * @rdata: The remote port to send the LOGO request to 1465 * 1466 * Reference counting: increments kref when sending ELS 1467 */ 1468 static void fc_rport_enter_logo(struct fc_rport_priv *rdata) 1469 { 1470 struct fc_lport *lport = rdata->local_port; 1471 struct fc_frame *fp; 1472 1473 lockdep_assert_held(&rdata->rp_mutex); 1474 1475 FC_RPORT_DBG(rdata, "Port sending LOGO from %s state\n", 1476 fc_rport_state(rdata)); 1477 1478 fp = fc_frame_alloc(lport, sizeof(struct fc_els_logo)); 1479 if (!fp) 1480 return; 1481 kref_get(&rdata->kref); 1482 if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_LOGO, 1483 fc_rport_logo_resp, rdata, 0)) 1484 kref_put(&rdata->kref, fc_rport_destroy); 1485 } 1486 1487 /** 1488 * fc_rport_els_adisc_resp() - Handler for Address Discovery (ADISC) responses 1489 * @sp: The sequence the ADISC response was on 1490 * @fp: The ADISC response frame 1491 * @rdata_arg: The remote port that sent the ADISC response 1492 * 1493 * Locking Note: This function will be called without the rport lock 1494 * held, but it will lock, call an _enter_* function or fc_rport_error 1495 * and then unlock the rport. 1496 */ 1497 static void fc_rport_adisc_resp(struct fc_seq *sp, struct fc_frame *fp, 1498 void *rdata_arg) 1499 { 1500 struct fc_rport_priv *rdata = rdata_arg; 1501 struct fc_els_adisc *adisc; 1502 u8 op; 1503 1504 FC_RPORT_DBG(rdata, "Received a ADISC response\n"); 1505 1506 if (fp == ERR_PTR(-FC_EX_CLOSED)) 1507 goto put; 1508 1509 mutex_lock(&rdata->rp_mutex); 1510 1511 if (rdata->rp_state != RPORT_ST_ADISC) { 1512 FC_RPORT_DBG(rdata, "Received a ADISC resp but in state %s\n", 1513 fc_rport_state(rdata)); 1514 if (IS_ERR(fp)) 1515 goto err; 1516 goto out; 1517 } 1518 1519 if (IS_ERR(fp)) { 1520 fc_rport_error(rdata, PTR_ERR(fp)); 1521 goto err; 1522 } 1523 1524 /* 1525 * If address verification failed. Consider us logged out of the rport. 1526 * Since the rport is still in discovery, we want to be 1527 * logged in, so go to PLOGI state. Otherwise, go back to READY. 1528 */ 1529 op = fc_frame_payload_op(fp); 1530 adisc = fc_frame_payload_get(fp, sizeof(*adisc)); 1531 if (op != ELS_LS_ACC || !adisc || 1532 ntoh24(adisc->adisc_port_id) != rdata->ids.port_id || 1533 get_unaligned_be64(&adisc->adisc_wwpn) != rdata->ids.port_name || 1534 get_unaligned_be64(&adisc->adisc_wwnn) != rdata->ids.node_name) { 1535 FC_RPORT_DBG(rdata, "ADISC error or mismatch\n"); 1536 fc_rport_enter_flogi(rdata); 1537 } else { 1538 FC_RPORT_DBG(rdata, "ADISC OK\n"); 1539 fc_rport_enter_ready(rdata); 1540 } 1541 out: 1542 fc_frame_free(fp); 1543 err: 1544 mutex_unlock(&rdata->rp_mutex); 1545 put: 1546 kref_put(&rdata->kref, fc_rport_destroy); 1547 } 1548 1549 /** 1550 * fc_rport_enter_adisc() - Send Address Discover (ADISC) request 1551 * @rdata: The remote port to send the ADISC request to 1552 * 1553 * Reference counting: increments kref when sending ELS 1554 */ 1555 static void fc_rport_enter_adisc(struct fc_rport_priv *rdata) 1556 { 1557 struct fc_lport *lport = rdata->local_port; 1558 struct fc_frame *fp; 1559 1560 lockdep_assert_held(&rdata->rp_mutex); 1561 1562 FC_RPORT_DBG(rdata, "sending ADISC from %s state\n", 1563 fc_rport_state(rdata)); 1564 1565 fc_rport_state_enter(rdata, RPORT_ST_ADISC); 1566 1567 fp = fc_frame_alloc(lport, sizeof(struct fc_els_adisc)); 1568 if (!fp) { 1569 fc_rport_error_retry(rdata, -FC_EX_ALLOC_ERR); 1570 return; 1571 } 1572 kref_get(&rdata->kref); 1573 if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_ADISC, 1574 fc_rport_adisc_resp, rdata, 1575 2 * lport->r_a_tov)) { 1576 fc_rport_error_retry(rdata, -FC_EX_XMIT_ERR); 1577 kref_put(&rdata->kref, fc_rport_destroy); 1578 } 1579 } 1580 1581 /** 1582 * fc_rport_recv_adisc_req() - Handler for Address Discovery (ADISC) requests 1583 * @rdata: The remote port that sent the ADISC request 1584 * @in_fp: The ADISC request frame 1585 */ 1586 static void fc_rport_recv_adisc_req(struct fc_rport_priv *rdata, 1587 struct fc_frame *in_fp) 1588 { 1589 struct fc_lport *lport = rdata->local_port; 1590 struct fc_frame *fp; 1591 struct fc_els_adisc *adisc; 1592 struct fc_seq_els_data rjt_data; 1593 1594 lockdep_assert_held(&rdata->rp_mutex); 1595 lockdep_assert_held(&lport->lp_mutex); 1596 1597 FC_RPORT_DBG(rdata, "Received ADISC request\n"); 1598 1599 adisc = fc_frame_payload_get(in_fp, sizeof(*adisc)); 1600 if (!adisc) { 1601 rjt_data.reason = ELS_RJT_PROT; 1602 rjt_data.explan = ELS_EXPL_INV_LEN; 1603 fc_seq_els_rsp_send(in_fp, ELS_LS_RJT, &rjt_data); 1604 goto drop; 1605 } 1606 1607 fp = fc_frame_alloc(lport, sizeof(*adisc)); 1608 if (!fp) 1609 goto drop; 1610 fc_adisc_fill(lport, fp); 1611 adisc = fc_frame_payload_get(fp, sizeof(*adisc)); 1612 adisc->adisc_cmd = ELS_LS_ACC; 1613 fc_fill_reply_hdr(fp, in_fp, FC_RCTL_ELS_REP, 0); 1614 lport->tt.frame_send(lport, fp); 1615 drop: 1616 fc_frame_free(in_fp); 1617 } 1618 1619 /** 1620 * fc_rport_recv_rls_req() - Handle received Read Link Status request 1621 * @rdata: The remote port that sent the RLS request 1622 * @rx_fp: The PRLI request frame 1623 */ 1624 static void fc_rport_recv_rls_req(struct fc_rport_priv *rdata, 1625 struct fc_frame *rx_fp) 1626 1627 { 1628 struct fc_lport *lport = rdata->local_port; 1629 struct fc_frame *fp; 1630 struct fc_els_rls *rls; 1631 struct fc_els_rls_resp *rsp; 1632 struct fc_els_lesb *lesb; 1633 struct fc_seq_els_data rjt_data; 1634 struct fc_host_statistics *hst; 1635 1636 lockdep_assert_held(&rdata->rp_mutex); 1637 1638 FC_RPORT_DBG(rdata, "Received RLS request while in state %s\n", 1639 fc_rport_state(rdata)); 1640 1641 rls = fc_frame_payload_get(rx_fp, sizeof(*rls)); 1642 if (!rls) { 1643 rjt_data.reason = ELS_RJT_PROT; 1644 rjt_data.explan = ELS_EXPL_INV_LEN; 1645 goto out_rjt; 1646 } 1647 1648 fp = fc_frame_alloc(lport, sizeof(*rsp)); 1649 if (!fp) { 1650 rjt_data.reason = ELS_RJT_UNAB; 1651 rjt_data.explan = ELS_EXPL_INSUF_RES; 1652 goto out_rjt; 1653 } 1654 1655 rsp = fc_frame_payload_get(fp, sizeof(*rsp)); 1656 memset(rsp, 0, sizeof(*rsp)); 1657 rsp->rls_cmd = ELS_LS_ACC; 1658 lesb = &rsp->rls_lesb; 1659 if (lport->tt.get_lesb) { 1660 /* get LESB from LLD if it supports it */ 1661 lport->tt.get_lesb(lport, lesb); 1662 } else { 1663 fc_get_host_stats(lport->host); 1664 hst = &lport->host_stats; 1665 lesb->lesb_link_fail = htonl(hst->link_failure_count); 1666 lesb->lesb_sync_loss = htonl(hst->loss_of_sync_count); 1667 lesb->lesb_sig_loss = htonl(hst->loss_of_signal_count); 1668 lesb->lesb_prim_err = htonl(hst->prim_seq_protocol_err_count); 1669 lesb->lesb_inv_word = htonl(hst->invalid_tx_word_count); 1670 lesb->lesb_inv_crc = htonl(hst->invalid_crc_count); 1671 } 1672 1673 fc_fill_reply_hdr(fp, rx_fp, FC_RCTL_ELS_REP, 0); 1674 lport->tt.frame_send(lport, fp); 1675 goto out; 1676 1677 out_rjt: 1678 fc_seq_els_rsp_send(rx_fp, ELS_LS_RJT, &rjt_data); 1679 out: 1680 fc_frame_free(rx_fp); 1681 } 1682 1683 /** 1684 * fc_rport_recv_els_req() - Handler for validated ELS requests 1685 * @lport: The local port that received the ELS request 1686 * @fp: The ELS request frame 1687 * 1688 * Handle incoming ELS requests that require port login. 1689 * The ELS opcode has already been validated by the caller. 1690 * 1691 * Reference counting: does not modify kref 1692 */ 1693 static void fc_rport_recv_els_req(struct fc_lport *lport, struct fc_frame *fp) 1694 { 1695 struct fc_rport_priv *rdata; 1696 struct fc_seq_els_data els_data; 1697 1698 lockdep_assert_held(&lport->lp_mutex); 1699 1700 rdata = fc_rport_lookup(lport, fc_frame_sid(fp)); 1701 if (!rdata) { 1702 FC_RPORT_ID_DBG(lport, fc_frame_sid(fp), 1703 "Received ELS 0x%02x from non-logged-in port\n", 1704 fc_frame_payload_op(fp)); 1705 goto reject; 1706 } 1707 1708 mutex_lock(&rdata->rp_mutex); 1709 1710 switch (rdata->rp_state) { 1711 case RPORT_ST_PRLI: 1712 case RPORT_ST_RTV: 1713 case RPORT_ST_READY: 1714 case RPORT_ST_ADISC: 1715 break; 1716 case RPORT_ST_PLOGI: 1717 if (fc_frame_payload_op(fp) == ELS_PRLI) { 1718 FC_RPORT_DBG(rdata, "Reject ELS PRLI " 1719 "while in state %s\n", 1720 fc_rport_state(rdata)); 1721 mutex_unlock(&rdata->rp_mutex); 1722 kref_put(&rdata->kref, fc_rport_destroy); 1723 goto busy; 1724 } 1725 /* fall through */ 1726 default: 1727 FC_RPORT_DBG(rdata, 1728 "Reject ELS 0x%02x while in state %s\n", 1729 fc_frame_payload_op(fp), fc_rport_state(rdata)); 1730 mutex_unlock(&rdata->rp_mutex); 1731 kref_put(&rdata->kref, fc_rport_destroy); 1732 goto reject; 1733 } 1734 1735 switch (fc_frame_payload_op(fp)) { 1736 case ELS_PRLI: 1737 fc_rport_recv_prli_req(rdata, fp); 1738 break; 1739 case ELS_PRLO: 1740 fc_rport_recv_prlo_req(rdata, fp); 1741 break; 1742 case ELS_ADISC: 1743 fc_rport_recv_adisc_req(rdata, fp); 1744 break; 1745 case ELS_RRQ: 1746 fc_seq_els_rsp_send(fp, ELS_RRQ, NULL); 1747 fc_frame_free(fp); 1748 break; 1749 case ELS_REC: 1750 fc_seq_els_rsp_send(fp, ELS_REC, NULL); 1751 fc_frame_free(fp); 1752 break; 1753 case ELS_RLS: 1754 fc_rport_recv_rls_req(rdata, fp); 1755 break; 1756 case ELS_RTV: 1757 fc_rport_recv_rtv_req(rdata, fp); 1758 break; 1759 default: 1760 fc_frame_free(fp); /* can't happen */ 1761 break; 1762 } 1763 1764 mutex_unlock(&rdata->rp_mutex); 1765 kref_put(&rdata->kref, fc_rport_destroy); 1766 return; 1767 1768 reject: 1769 els_data.reason = ELS_RJT_UNAB; 1770 els_data.explan = ELS_EXPL_PLOGI_REQD; 1771 fc_seq_els_rsp_send(fp, ELS_LS_RJT, &els_data); 1772 fc_frame_free(fp); 1773 return; 1774 1775 busy: 1776 els_data.reason = ELS_RJT_BUSY; 1777 els_data.explan = ELS_EXPL_NONE; 1778 fc_seq_els_rsp_send(fp, ELS_LS_RJT, &els_data); 1779 fc_frame_free(fp); 1780 return; 1781 } 1782 1783 /** 1784 * fc_rport_recv_req() - Handler for requests 1785 * @lport: The local port that received the request 1786 * @fp: The request frame 1787 * 1788 * Reference counting: does not modify kref 1789 */ 1790 void fc_rport_recv_req(struct fc_lport *lport, struct fc_frame *fp) 1791 { 1792 struct fc_seq_els_data els_data; 1793 1794 lockdep_assert_held(&lport->lp_mutex); 1795 1796 /* 1797 * Handle FLOGI, PLOGI and LOGO requests separately, since they 1798 * don't require prior login. 1799 * Check for unsupported opcodes first and reject them. 1800 * For some ops, it would be incorrect to reject with "PLOGI required". 1801 */ 1802 switch (fc_frame_payload_op(fp)) { 1803 case ELS_FLOGI: 1804 fc_rport_recv_flogi_req(lport, fp); 1805 break; 1806 case ELS_PLOGI: 1807 fc_rport_recv_plogi_req(lport, fp); 1808 break; 1809 case ELS_LOGO: 1810 fc_rport_recv_logo_req(lport, fp); 1811 break; 1812 case ELS_PRLI: 1813 case ELS_PRLO: 1814 case ELS_ADISC: 1815 case ELS_RRQ: 1816 case ELS_REC: 1817 case ELS_RLS: 1818 case ELS_RTV: 1819 fc_rport_recv_els_req(lport, fp); 1820 break; 1821 default: 1822 els_data.reason = ELS_RJT_UNSUP; 1823 els_data.explan = ELS_EXPL_NONE; 1824 fc_seq_els_rsp_send(fp, ELS_LS_RJT, &els_data); 1825 fc_frame_free(fp); 1826 break; 1827 } 1828 } 1829 EXPORT_SYMBOL(fc_rport_recv_req); 1830 1831 /** 1832 * fc_rport_recv_plogi_req() - Handler for Port Login (PLOGI) requests 1833 * @lport: The local port that received the PLOGI request 1834 * @rx_fp: The PLOGI request frame 1835 * 1836 * Reference counting: increments kref on return 1837 */ 1838 static void fc_rport_recv_plogi_req(struct fc_lport *lport, 1839 struct fc_frame *rx_fp) 1840 { 1841 struct fc_disc *disc; 1842 struct fc_rport_priv *rdata; 1843 struct fc_frame *fp = rx_fp; 1844 struct fc_els_flogi *pl; 1845 struct fc_seq_els_data rjt_data; 1846 u32 sid; 1847 1848 lockdep_assert_held(&lport->lp_mutex); 1849 1850 sid = fc_frame_sid(fp); 1851 1852 FC_RPORT_ID_DBG(lport, sid, "Received PLOGI request\n"); 1853 1854 pl = fc_frame_payload_get(fp, sizeof(*pl)); 1855 if (!pl) { 1856 FC_RPORT_ID_DBG(lport, sid, "Received PLOGI too short\n"); 1857 rjt_data.reason = ELS_RJT_PROT; 1858 rjt_data.explan = ELS_EXPL_INV_LEN; 1859 goto reject; 1860 } 1861 1862 disc = &lport->disc; 1863 mutex_lock(&disc->disc_mutex); 1864 rdata = fc_rport_create(lport, sid); 1865 if (!rdata) { 1866 mutex_unlock(&disc->disc_mutex); 1867 rjt_data.reason = ELS_RJT_UNAB; 1868 rjt_data.explan = ELS_EXPL_INSUF_RES; 1869 goto reject; 1870 } 1871 1872 mutex_lock(&rdata->rp_mutex); 1873 mutex_unlock(&disc->disc_mutex); 1874 1875 rdata->ids.port_name = get_unaligned_be64(&pl->fl_wwpn); 1876 rdata->ids.node_name = get_unaligned_be64(&pl->fl_wwnn); 1877 1878 /* 1879 * If the rport was just created, possibly due to the incoming PLOGI, 1880 * set the state appropriately and accept the PLOGI. 1881 * 1882 * If we had also sent a PLOGI, and if the received PLOGI is from a 1883 * higher WWPN, we accept it, otherwise an LS_RJT is sent with reason 1884 * "command already in progress". 1885 * 1886 * XXX TBD: If the session was ready before, the PLOGI should result in 1887 * all outstanding exchanges being reset. 1888 */ 1889 switch (rdata->rp_state) { 1890 case RPORT_ST_INIT: 1891 FC_RPORT_DBG(rdata, "Received PLOGI in INIT state\n"); 1892 break; 1893 case RPORT_ST_PLOGI_WAIT: 1894 FC_RPORT_DBG(rdata, "Received PLOGI in PLOGI_WAIT state\n"); 1895 break; 1896 case RPORT_ST_PLOGI: 1897 FC_RPORT_DBG(rdata, "Received PLOGI in PLOGI state\n"); 1898 if (rdata->ids.port_name < lport->wwpn) { 1899 mutex_unlock(&rdata->rp_mutex); 1900 rjt_data.reason = ELS_RJT_INPROG; 1901 rjt_data.explan = ELS_EXPL_NONE; 1902 goto reject; 1903 } 1904 break; 1905 case RPORT_ST_PRLI: 1906 case RPORT_ST_RTV: 1907 case RPORT_ST_READY: 1908 case RPORT_ST_ADISC: 1909 FC_RPORT_DBG(rdata, "Received PLOGI in logged-in state %d " 1910 "- ignored for now\n", rdata->rp_state); 1911 /* XXX TBD - should reset */ 1912 break; 1913 case RPORT_ST_FLOGI: 1914 case RPORT_ST_DELETE: 1915 FC_RPORT_DBG(rdata, "Received PLOGI in state %s - send busy\n", 1916 fc_rport_state(rdata)); 1917 mutex_unlock(&rdata->rp_mutex); 1918 rjt_data.reason = ELS_RJT_BUSY; 1919 rjt_data.explan = ELS_EXPL_NONE; 1920 goto reject; 1921 } 1922 if (!fc_rport_compatible_roles(lport, rdata)) { 1923 FC_RPORT_DBG(rdata, "Received PLOGI for incompatible role\n"); 1924 mutex_unlock(&rdata->rp_mutex); 1925 rjt_data.reason = ELS_RJT_LOGIC; 1926 rjt_data.explan = ELS_EXPL_NONE; 1927 goto reject; 1928 } 1929 1930 /* 1931 * Get session payload size from incoming PLOGI. 1932 */ 1933 rdata->maxframe_size = fc_plogi_get_maxframe(pl, lport->mfs); 1934 1935 /* 1936 * Send LS_ACC. If this fails, the originator should retry. 1937 */ 1938 fp = fc_frame_alloc(lport, sizeof(*pl)); 1939 if (!fp) 1940 goto out; 1941 1942 fc_plogi_fill(lport, fp, ELS_LS_ACC); 1943 fc_fill_reply_hdr(fp, rx_fp, FC_RCTL_ELS_REP, 0); 1944 lport->tt.frame_send(lport, fp); 1945 fc_rport_enter_prli(rdata); 1946 out: 1947 mutex_unlock(&rdata->rp_mutex); 1948 fc_frame_free(rx_fp); 1949 return; 1950 1951 reject: 1952 fc_seq_els_rsp_send(fp, ELS_LS_RJT, &rjt_data); 1953 fc_frame_free(fp); 1954 } 1955 1956 /** 1957 * fc_rport_recv_prli_req() - Handler for process login (PRLI) requests 1958 * @rdata: The remote port that sent the PRLI request 1959 * @rx_fp: The PRLI request frame 1960 */ 1961 static void fc_rport_recv_prli_req(struct fc_rport_priv *rdata, 1962 struct fc_frame *rx_fp) 1963 { 1964 struct fc_lport *lport = rdata->local_port; 1965 struct fc_frame *fp; 1966 struct { 1967 struct fc_els_prli prli; 1968 struct fc_els_spp spp; 1969 } *pp; 1970 struct fc_els_spp *rspp; /* request service param page */ 1971 struct fc_els_spp *spp; /* response spp */ 1972 unsigned int len; 1973 unsigned int plen; 1974 enum fc_els_spp_resp resp; 1975 struct fc_seq_els_data rjt_data; 1976 struct fc4_prov *prov; 1977 1978 lockdep_assert_held(&rdata->rp_mutex); 1979 1980 FC_RPORT_DBG(rdata, "Received PRLI request while in state %s\n", 1981 fc_rport_state(rdata)); 1982 1983 len = fr_len(rx_fp) - sizeof(struct fc_frame_header); 1984 pp = fc_frame_payload_get(rx_fp, sizeof(*pp)); 1985 if (!pp) 1986 goto reject_len; 1987 plen = ntohs(pp->prli.prli_len); 1988 if ((plen % 4) != 0 || plen > len || plen < 16) 1989 goto reject_len; 1990 if (plen < len) 1991 len = plen; 1992 plen = pp->prli.prli_spp_len; 1993 if ((plen % 4) != 0 || plen < sizeof(*spp) || 1994 plen > len || len < sizeof(*pp) || plen < 12) 1995 goto reject_len; 1996 rspp = &pp->spp; 1997 1998 fp = fc_frame_alloc(lport, len); 1999 if (!fp) { 2000 rjt_data.reason = ELS_RJT_UNAB; 2001 rjt_data.explan = ELS_EXPL_INSUF_RES; 2002 goto reject; 2003 } 2004 pp = fc_frame_payload_get(fp, len); 2005 WARN_ON(!pp); 2006 memset(pp, 0, len); 2007 pp->prli.prli_cmd = ELS_LS_ACC; 2008 pp->prli.prli_spp_len = plen; 2009 pp->prli.prli_len = htons(len); 2010 len -= sizeof(struct fc_els_prli); 2011 2012 /* 2013 * Go through all the service parameter pages and build 2014 * response. If plen indicates longer SPP than standard, 2015 * use that. The entire response has been pre-cleared above. 2016 */ 2017 spp = &pp->spp; 2018 mutex_lock(&fc_prov_mutex); 2019 while (len >= plen) { 2020 rdata->spp_type = rspp->spp_type; 2021 spp->spp_type = rspp->spp_type; 2022 spp->spp_type_ext = rspp->spp_type_ext; 2023 resp = 0; 2024 2025 if (rspp->spp_type < FC_FC4_PROV_SIZE) { 2026 enum fc_els_spp_resp active = 0, passive = 0; 2027 2028 prov = fc_active_prov[rspp->spp_type]; 2029 if (prov) 2030 active = prov->prli(rdata, plen, rspp, spp); 2031 prov = fc_passive_prov[rspp->spp_type]; 2032 if (prov) 2033 passive = prov->prli(rdata, plen, rspp, spp); 2034 if (!active || passive == FC_SPP_RESP_ACK) 2035 resp = passive; 2036 else 2037 resp = active; 2038 FC_RPORT_DBG(rdata, "PRLI rspp type %x " 2039 "active %x passive %x\n", 2040 rspp->spp_type, active, passive); 2041 } 2042 if (!resp) { 2043 if (spp->spp_flags & FC_SPP_EST_IMG_PAIR) 2044 resp |= FC_SPP_RESP_CONF; 2045 else 2046 resp |= FC_SPP_RESP_INVL; 2047 } 2048 spp->spp_flags |= resp; 2049 len -= plen; 2050 rspp = (struct fc_els_spp *)((char *)rspp + plen); 2051 spp = (struct fc_els_spp *)((char *)spp + plen); 2052 } 2053 mutex_unlock(&fc_prov_mutex); 2054 2055 /* 2056 * Send LS_ACC. If this fails, the originator should retry. 2057 */ 2058 fc_fill_reply_hdr(fp, rx_fp, FC_RCTL_ELS_REP, 0); 2059 lport->tt.frame_send(lport, fp); 2060 2061 goto drop; 2062 2063 reject_len: 2064 rjt_data.reason = ELS_RJT_PROT; 2065 rjt_data.explan = ELS_EXPL_INV_LEN; 2066 reject: 2067 fc_seq_els_rsp_send(rx_fp, ELS_LS_RJT, &rjt_data); 2068 drop: 2069 fc_frame_free(rx_fp); 2070 } 2071 2072 /** 2073 * fc_rport_recv_prlo_req() - Handler for process logout (PRLO) requests 2074 * @rdata: The remote port that sent the PRLO request 2075 * @rx_fp: The PRLO request frame 2076 */ 2077 static void fc_rport_recv_prlo_req(struct fc_rport_priv *rdata, 2078 struct fc_frame *rx_fp) 2079 { 2080 struct fc_lport *lport = rdata->local_port; 2081 struct fc_frame *fp; 2082 struct { 2083 struct fc_els_prlo prlo; 2084 struct fc_els_spp spp; 2085 } *pp; 2086 struct fc_els_spp *rspp; /* request service param page */ 2087 struct fc_els_spp *spp; /* response spp */ 2088 unsigned int len; 2089 unsigned int plen; 2090 struct fc_seq_els_data rjt_data; 2091 2092 lockdep_assert_held(&rdata->rp_mutex); 2093 2094 FC_RPORT_DBG(rdata, "Received PRLO request while in state %s\n", 2095 fc_rport_state(rdata)); 2096 2097 len = fr_len(rx_fp) - sizeof(struct fc_frame_header); 2098 pp = fc_frame_payload_get(rx_fp, sizeof(*pp)); 2099 if (!pp) 2100 goto reject_len; 2101 plen = ntohs(pp->prlo.prlo_len); 2102 if (plen != 20) 2103 goto reject_len; 2104 if (plen < len) 2105 len = plen; 2106 2107 rspp = &pp->spp; 2108 2109 fp = fc_frame_alloc(lport, len); 2110 if (!fp) { 2111 rjt_data.reason = ELS_RJT_UNAB; 2112 rjt_data.explan = ELS_EXPL_INSUF_RES; 2113 goto reject; 2114 } 2115 2116 pp = fc_frame_payload_get(fp, len); 2117 WARN_ON(!pp); 2118 memset(pp, 0, len); 2119 pp->prlo.prlo_cmd = ELS_LS_ACC; 2120 pp->prlo.prlo_obs = 0x10; 2121 pp->prlo.prlo_len = htons(len); 2122 spp = &pp->spp; 2123 spp->spp_type = rspp->spp_type; 2124 spp->spp_type_ext = rspp->spp_type_ext; 2125 spp->spp_flags = FC_SPP_RESP_ACK; 2126 2127 fc_rport_enter_prli(rdata); 2128 2129 fc_fill_reply_hdr(fp, rx_fp, FC_RCTL_ELS_REP, 0); 2130 lport->tt.frame_send(lport, fp); 2131 goto drop; 2132 2133 reject_len: 2134 rjt_data.reason = ELS_RJT_PROT; 2135 rjt_data.explan = ELS_EXPL_INV_LEN; 2136 reject: 2137 fc_seq_els_rsp_send(rx_fp, ELS_LS_RJT, &rjt_data); 2138 drop: 2139 fc_frame_free(rx_fp); 2140 } 2141 2142 /** 2143 * fc_rport_recv_logo_req() - Handler for logout (LOGO) requests 2144 * @lport: The local port that received the LOGO request 2145 * @fp: The LOGO request frame 2146 * 2147 * Reference counting: drops kref on return 2148 */ 2149 static void fc_rport_recv_logo_req(struct fc_lport *lport, struct fc_frame *fp) 2150 { 2151 struct fc_rport_priv *rdata; 2152 u32 sid; 2153 2154 lockdep_assert_held(&lport->lp_mutex); 2155 2156 fc_seq_els_rsp_send(fp, ELS_LS_ACC, NULL); 2157 2158 sid = fc_frame_sid(fp); 2159 2160 rdata = fc_rport_lookup(lport, sid); 2161 if (rdata) { 2162 mutex_lock(&rdata->rp_mutex); 2163 FC_RPORT_DBG(rdata, "Received LOGO request while in state %s\n", 2164 fc_rport_state(rdata)); 2165 2166 fc_rport_enter_delete(rdata, RPORT_EV_STOP); 2167 mutex_unlock(&rdata->rp_mutex); 2168 kref_put(&rdata->kref, fc_rport_destroy); 2169 } else 2170 FC_RPORT_ID_DBG(lport, sid, 2171 "Received LOGO from non-logged-in port\n"); 2172 fc_frame_free(fp); 2173 } 2174 2175 /** 2176 * fc_rport_flush_queue() - Flush the rport_event_queue 2177 */ 2178 void fc_rport_flush_queue(void) 2179 { 2180 flush_workqueue(rport_event_queue); 2181 } 2182 EXPORT_SYMBOL(fc_rport_flush_queue); 2183 2184 /** 2185 * fc_rport_fcp_prli() - Handle incoming PRLI for the FCP initiator. 2186 * @rdata: remote port private 2187 * @spp_len: service parameter page length 2188 * @rspp: received service parameter page 2189 * @spp: response service parameter page 2190 * 2191 * Returns the value for the response code to be placed in spp_flags; 2192 * Returns 0 if not an initiator. 2193 */ 2194 static int fc_rport_fcp_prli(struct fc_rport_priv *rdata, u32 spp_len, 2195 const struct fc_els_spp *rspp, 2196 struct fc_els_spp *spp) 2197 { 2198 struct fc_lport *lport = rdata->local_port; 2199 u32 fcp_parm; 2200 2201 fcp_parm = ntohl(rspp->spp_params); 2202 rdata->ids.roles = FC_RPORT_ROLE_UNKNOWN; 2203 if (fcp_parm & FCP_SPPF_INIT_FCN) 2204 rdata->ids.roles |= FC_RPORT_ROLE_FCP_INITIATOR; 2205 if (fcp_parm & FCP_SPPF_TARG_FCN) 2206 rdata->ids.roles |= FC_RPORT_ROLE_FCP_TARGET; 2207 if (fcp_parm & FCP_SPPF_RETRY) 2208 rdata->flags |= FC_RP_FLAGS_RETRY; 2209 rdata->supported_classes = FC_COS_CLASS3; 2210 2211 if (!(lport->service_params & FCP_SPPF_INIT_FCN)) 2212 return 0; 2213 2214 spp->spp_flags |= rspp->spp_flags & FC_SPP_EST_IMG_PAIR; 2215 2216 /* 2217 * OR in our service parameters with other providers (target), if any. 2218 */ 2219 fcp_parm = ntohl(spp->spp_params); 2220 spp->spp_params = htonl(fcp_parm | lport->service_params); 2221 return FC_SPP_RESP_ACK; 2222 } 2223 2224 /* 2225 * FC-4 provider ops for FCP initiator. 2226 */ 2227 struct fc4_prov fc_rport_fcp_init = { 2228 .prli = fc_rport_fcp_prli, 2229 }; 2230 2231 /** 2232 * fc_rport_t0_prli() - Handle incoming PRLI parameters for type 0 2233 * @rdata: remote port private 2234 * @spp_len: service parameter page length 2235 * @rspp: received service parameter page 2236 * @spp: response service parameter page 2237 */ 2238 static int fc_rport_t0_prli(struct fc_rport_priv *rdata, u32 spp_len, 2239 const struct fc_els_spp *rspp, 2240 struct fc_els_spp *spp) 2241 { 2242 if (rspp->spp_flags & FC_SPP_EST_IMG_PAIR) 2243 return FC_SPP_RESP_INVL; 2244 return FC_SPP_RESP_ACK; 2245 } 2246 2247 /* 2248 * FC-4 provider ops for type 0 service parameters. 2249 * 2250 * This handles the special case of type 0 which is always successful 2251 * but doesn't do anything otherwise. 2252 */ 2253 struct fc4_prov fc_rport_t0_prov = { 2254 .prli = fc_rport_t0_prli, 2255 }; 2256 2257 /** 2258 * fc_setup_rport() - Initialize the rport_event_queue 2259 */ 2260 int fc_setup_rport(void) 2261 { 2262 rport_event_queue = create_singlethread_workqueue("fc_rport_eq"); 2263 if (!rport_event_queue) 2264 return -ENOMEM; 2265 return 0; 2266 } 2267 2268 /** 2269 * fc_destroy_rport() - Destroy the rport_event_queue 2270 */ 2271 void fc_destroy_rport(void) 2272 { 2273 destroy_workqueue(rport_event_queue); 2274 } 2275 2276 /** 2277 * fc_rport_terminate_io() - Stop all outstanding I/O on a remote port 2278 * @rport: The remote port whose I/O should be terminated 2279 */ 2280 void fc_rport_terminate_io(struct fc_rport *rport) 2281 { 2282 struct fc_rport_libfc_priv *rpriv = rport->dd_data; 2283 struct fc_lport *lport = rpriv->local_port; 2284 2285 lport->tt.exch_mgr_reset(lport, 0, rport->port_id); 2286 lport->tt.exch_mgr_reset(lport, rport->port_id, 0); 2287 } 2288 EXPORT_SYMBOL(fc_rport_terminate_io); 2289