2  * Copyright(c) 2007 - 2008 Intel Corporation. All rights reserved.
 
   4  * This program is free software; you can redistribute it and/or modify it
 
   5  * under the terms and conditions of the GNU General Public License,
 
   6  * version 2, as published by the Free Software Foundation.
 
   8  * This program is distributed in the hope it will be useful, but WITHOUT
 
   9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 
  10  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 
  13  * You should have received a copy of the GNU General Public License along with
 
  14  * this program; if not, write to the Free Software Foundation, Inc.,
 
  15  * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
 
  17  * Maintained at www.Open-FCoE.org
 
  23  * This file contains all processing regarding fc_rports. It contains the
 
  24  * rport state machine and does all rport interaction with the transport class.
 
  25  * There should be no other places in libfc that interact directly with the
 
  26  * transport class in regards to adding and deleting rports.
 
  28  * fc_rport's represent N_Port's within the fabric.
 
  34  * The rport should never hold the rport mutex and then attempt to acquire
 
  35  * either the lport or disc mutexes. The rport's mutex is considered lesser
 
  36  * than both the lport's mutex and the disc mutex. Refer to fc_lport.c for
 
  37  * more comments on the heirarchy.
 
  39  * The locking strategy is similar to the lport's strategy. The lock protects
 
  40  * the rport's states and is held and released by the entry points to the rport
 
  41  * block. All _enter_* functions correspond to rport states and expect the rport
 
  42  * mutex to be locked before calling them. This means that rports only handle
 
  43  * one request or response at a time, since they're not critical for the I/O
 
  44  * path this potential over-use of the mutex is acceptable.
 
  47 #include <linux/kernel.h>
 
  48 #include <linux/spinlock.h>
 
  49 #include <linux/interrupt.h>
 
  50 #include <linux/rcupdate.h>
 
  51 #include <linux/timer.h>
 
  52 #include <linux/workqueue.h>
 
  53 #include <asm/unaligned.h>
 
  55 #include <scsi/libfc.h>
 
  56 #include <scsi/fc_encode.h>
 
  58 static int fc_rport_debug;
 
  60 #define FC_DEBUG_RPORT(fmt...)                  \
 
  66 struct workqueue_struct *rport_event_queue;
 
  68 static void fc_rport_enter_plogi(struct fc_rport *);
 
  69 static void fc_rport_enter_prli(struct fc_rport *);
 
  70 static void fc_rport_enter_rtv(struct fc_rport *);
 
  71 static void fc_rport_enter_ready(struct fc_rport *);
 
  72 static void fc_rport_enter_logo(struct fc_rport *);
 
  74 static void fc_rport_recv_plogi_req(struct fc_rport *,
 
  75                                     struct fc_seq *, struct fc_frame *);
 
  76 static void fc_rport_recv_prli_req(struct fc_rport *,
 
  77                                    struct fc_seq *, struct fc_frame *);
 
  78 static void fc_rport_recv_prlo_req(struct fc_rport *,
 
  79                                    struct fc_seq *, struct fc_frame *);
 
  80 static void fc_rport_recv_logo_req(struct fc_rport *,
 
  81                                    struct fc_seq *, struct fc_frame *);
 
  82 static void fc_rport_timeout(struct work_struct *);
 
  83 static void fc_rport_error(struct fc_rport *, struct fc_frame *);
 
  84 static void fc_rport_work(struct work_struct *);
 
  86 static const char *fc_rport_state_names[] = {
 
  87         [RPORT_ST_NONE] = "None",
 
  88         [RPORT_ST_INIT] = "Init",
 
  89         [RPORT_ST_PLOGI] = "PLOGI",
 
  90         [RPORT_ST_PRLI] = "PRLI",
 
  91         [RPORT_ST_RTV] = "RTV",
 
  92         [RPORT_ST_READY] = "Ready",
 
  93         [RPORT_ST_LOGO] = "LOGO",
 
  96 static void fc_rport_rogue_destroy(struct device *dev)
 
  98         struct fc_rport *rport = dev_to_rport(dev);
 
  99         FC_DEBUG_RPORT("Destroying rogue rport (%6x)\n", rport->port_id);
 
 103 struct fc_rport *fc_rport_rogue_create(struct fc_disc_port *dp)
 
 105         struct fc_rport *rport;
 
 106         struct fc_rport_libfc_priv *rdata;
 
 107         rport = kzalloc(sizeof(*rport) + sizeof(*rdata), GFP_KERNEL);
 
 112         rdata = RPORT_TO_PRIV(rport);
 
 114         rport->dd_data = rdata;
 
 115         rport->port_id = dp->ids.port_id;
 
 116         rport->port_name = dp->ids.port_name;
 
 117         rport->node_name = dp->ids.node_name;
 
 118         rport->roles = dp->ids.roles;
 
 119         rport->maxframe_size = FC_MIN_MAX_PAYLOAD;
 
 121          * Note: all this libfc rogue rport code will be removed for
 
 122          * upstream so it fine that this is really ugly and hacky right now.
 
 124         device_initialize(&rport->dev);
 
 125         rport->dev.release = fc_rport_rogue_destroy;
 
 127         mutex_init(&rdata->rp_mutex);
 
 128         rdata->local_port = dp->lp;
 
 129         rdata->trans_state = FC_PORTSTATE_ROGUE;
 
 130         rdata->rp_state = RPORT_ST_INIT;
 
 131         rdata->event = RPORT_EV_NONE;
 
 132         rdata->flags = FC_RP_FLAGS_REC_SUPPORTED;
 
 134         rdata->e_d_tov = dp->lp->e_d_tov;
 
 135         rdata->r_a_tov = dp->lp->r_a_tov;
 
 136         INIT_DELAYED_WORK(&rdata->retry_work, fc_rport_timeout);
 
 137         INIT_WORK(&rdata->event_work, fc_rport_work);
 
 139          * For good measure, but not necessary as we should only
 
 140          * add REAL rport to the lport list.
 
 142         INIT_LIST_HEAD(&rdata->peers);
 
 148  * fc_rport_state - return a string for the state the rport is in
 
 149  * @rport: The rport whose state we want to get a string for
 
 151 static const char *fc_rport_state(struct fc_rport *rport)
 
 154         struct fc_rport_libfc_priv *rdata = rport->dd_data;
 
 156         cp = fc_rport_state_names[rdata->rp_state];
 
 163  * fc_set_rport_loss_tmo - Set the remote port loss timeout in seconds.
 
 164  * @rport: Pointer to Fibre Channel remote port structure
 
 165  * @timeout: timeout in seconds
 
 167 void fc_set_rport_loss_tmo(struct fc_rport *rport, u32 timeout)
 
 170                 rport->dev_loss_tmo = timeout + 5;
 
 172                 rport->dev_loss_tmo = 30;
 
 174 EXPORT_SYMBOL(fc_set_rport_loss_tmo);
 
 177  * fc_plogi_get_maxframe - Get max payload from the common service parameters
 
 178  * @flp: FLOGI payload structure
 
 179  * @maxval: upper limit, may be less than what is in the service parameters
 
 182 fc_plogi_get_maxframe(struct fc_els_flogi *flp, unsigned int maxval)
 
 187          * Get max payload from the common service parameters and the
 
 188          * class 3 receive data field size.
 
 190         mfs = ntohs(flp->fl_csp.sp_bb_data) & FC_SP_BB_DATA_MASK;
 
 191         if (mfs >= FC_SP_MIN_MAX_PAYLOAD && mfs < maxval)
 
 193         mfs = ntohs(flp->fl_cssp[3 - 1].cp_rdfs);
 
 194         if (mfs >= FC_SP_MIN_MAX_PAYLOAD && mfs < maxval)
 
 200  * fc_rport_state_enter - Change the rport's state
 
 201  * @rport: The rport whose state should change
 
 202  * @new: The new state of the rport
 
 204  * Locking Note: Called with the rport lock held
 
 206 static void fc_rport_state_enter(struct fc_rport *rport,
 
 207                                  enum fc_rport_state new)
 
 209         struct fc_rport_libfc_priv *rdata = rport->dd_data;
 
 210         if (rdata->rp_state != new)
 
 212         rdata->rp_state = new;
 
 215 static void fc_rport_work(struct work_struct *work)
 
 217         struct fc_rport_libfc_priv *rdata =
 
 218                 container_of(work, struct fc_rport_libfc_priv, event_work);
 
 219         enum fc_rport_event event;
 
 220         enum fc_rport_trans_state trans_state;
 
 221         struct fc_lport *lport = rdata->local_port;
 
 222         struct fc_rport_operations *rport_ops;
 
 223         struct fc_rport *rport = PRIV_TO_RPORT(rdata);
 
 225         mutex_lock(&rdata->rp_mutex);
 
 226         event = rdata->event;
 
 227         rport_ops = rdata->ops;
 
 229         if (event == RPORT_EV_CREATED) {
 
 230                 struct fc_rport *new_rport;
 
 231                 struct fc_rport_libfc_priv *new_rdata;
 
 232                 struct fc_rport_identifiers ids;
 
 234                 ids.port_id = rport->port_id;
 
 235                 ids.roles = rport->roles;
 
 236                 ids.port_name = rport->port_name;
 
 237                 ids.node_name = rport->node_name;
 
 239                 mutex_unlock(&rdata->rp_mutex);
 
 241                 new_rport = fc_remote_port_add(lport->host, 0, &ids);
 
 244                          * Switch from the rogue rport to the rport
 
 245                          * returned by the FC class.
 
 247                         new_rport->maxframe_size = rport->maxframe_size;
 
 249                         new_rdata = new_rport->dd_data;
 
 250                         new_rdata->e_d_tov = rdata->e_d_tov;
 
 251                         new_rdata->r_a_tov = rdata->r_a_tov;
 
 252                         new_rdata->ops = rdata->ops;
 
 253                         new_rdata->local_port = rdata->local_port;
 
 254                         new_rdata->flags = FC_RP_FLAGS_REC_SUPPORTED;
 
 255                         new_rdata->trans_state = FC_PORTSTATE_REAL;
 
 256                         mutex_init(&new_rdata->rp_mutex);
 
 257                         INIT_DELAYED_WORK(&new_rdata->retry_work,
 
 259                         INIT_LIST_HEAD(&new_rdata->peers);
 
 260                         INIT_WORK(&new_rdata->event_work, fc_rport_work);
 
 262                         fc_rport_state_enter(new_rport, RPORT_ST_READY);
 
 264                         FC_DBG("Failed to create the rport for port "
 
 265                                "(%6x).\n", ids.port_id);
 
 266                         event = RPORT_EV_FAILED;
 
 268                 put_device(&rport->dev);
 
 270                 rdata = new_rport->dd_data;
 
 271                 if (rport_ops->event_callback)
 
 272                         rport_ops->event_callback(lport, rport, event);
 
 273         } else if ((event == RPORT_EV_FAILED) ||
 
 274                    (event == RPORT_EV_LOGO) ||
 
 275                    (event == RPORT_EV_STOP)) {
 
 276                 trans_state = rdata->trans_state;
 
 277                 mutex_unlock(&rdata->rp_mutex);
 
 278                 if (rport_ops->event_callback)
 
 279                         rport_ops->event_callback(lport, rport, event);
 
 280                 if (trans_state == FC_PORTSTATE_ROGUE)
 
 281                         put_device(&rport->dev);
 
 283                         fc_remote_port_delete(rport);
 
 285                 mutex_unlock(&rdata->rp_mutex);
 
 289  * fc_rport_login - Start the remote port login state machine
 
 290  * @rport: Fibre Channel remote port
 
 292  * Locking Note: Called without the rport lock held. This
 
 293  * function will hold the rport lock, call an _enter_*
 
 294  * function and then unlock the rport.
 
 296 int fc_rport_login(struct fc_rport *rport)
 
 298         struct fc_rport_libfc_priv *rdata = rport->dd_data;
 
 300         mutex_lock(&rdata->rp_mutex);
 
 302         FC_DEBUG_RPORT("Login to port (%6x)\n", rport->port_id);
 
 304         fc_rport_enter_plogi(rport);
 
 306         mutex_unlock(&rdata->rp_mutex);
 
 312  * fc_rport_logoff - Logoff and remove an rport
 
 313  * @rport: Fibre Channel remote port to be removed
 
 315  * Locking Note: Called without the rport lock held. This
 
 316  * function will hold the rport lock, call an _enter_*
 
 317  * function and then unlock the rport.
 
 319 int fc_rport_logoff(struct fc_rport *rport)
 
 321         struct fc_rport_libfc_priv *rdata = rport->dd_data;
 
 323         mutex_lock(&rdata->rp_mutex);
 
 325         FC_DEBUG_RPORT("Remove port (%6x)\n", rport->port_id);
 
 327         fc_rport_enter_logo(rport);
 
 330          * Change the state to NONE so that we discard
 
 333         fc_rport_state_enter(rport, RPORT_ST_NONE);
 
 335         mutex_unlock(&rdata->rp_mutex);
 
 337         cancel_delayed_work_sync(&rdata->retry_work);
 
 339         mutex_lock(&rdata->rp_mutex);
 
 341         rdata->event = RPORT_EV_STOP;
 
 342         queue_work(rport_event_queue, &rdata->event_work);
 
 344         mutex_unlock(&rdata->rp_mutex);
 
 350  * fc_rport_enter_ready - The rport is ready
 
 351  * @rport: Fibre Channel remote port that is ready
 
 353  * Locking Note: The rport lock is expected to be held before calling
 
 356 static void fc_rport_enter_ready(struct fc_rport *rport)
 
 358         struct fc_rport_libfc_priv *rdata = rport->dd_data;
 
 360         fc_rport_state_enter(rport, RPORT_ST_READY);
 
 362         FC_DEBUG_RPORT("Port (%6x) is Ready\n", rport->port_id);
 
 364         rdata->event = RPORT_EV_CREATED;
 
 365         queue_work(rport_event_queue, &rdata->event_work);
 
 369  * fc_rport_timeout - Handler for the retry_work timer.
 
 370  * @work: The work struct of the fc_rport_libfc_priv
 
 372  * Locking Note: Called without the rport lock held. This
 
 373  * function will hold the rport lock, call an _enter_*
 
 374  * function and then unlock the rport.
 
 376 static void fc_rport_timeout(struct work_struct *work)
 
 378         struct fc_rport_libfc_priv *rdata =
 
 379                 container_of(work, struct fc_rport_libfc_priv, retry_work.work);
 
 380         struct fc_rport *rport = PRIV_TO_RPORT(rdata);
 
 382         mutex_lock(&rdata->rp_mutex);
 
 384         switch (rdata->rp_state) {
 
 386                 fc_rport_enter_plogi(rport);
 
 389                 fc_rport_enter_prli(rport);
 
 392                 fc_rport_enter_rtv(rport);
 
 395                 fc_rport_enter_logo(rport);
 
 403         mutex_unlock(&rdata->rp_mutex);
 
 404         put_device(&rport->dev);
 
 408  * fc_rport_error - Handler for any errors
 
 409  * @rport: The fc_rport object
 
 410  * @fp: The frame pointer
 
 412  * If the error was caused by a resource allocation failure
 
 413  * then wait for half a second and retry, otherwise retry
 
 416  * Locking Note: The rport lock is expected to be held before
 
 417  * calling this routine
 
 419 static void fc_rport_error(struct fc_rport *rport, struct fc_frame *fp)
 
 421         struct fc_rport_libfc_priv *rdata = rport->dd_data;
 
 422         unsigned long delay = 0;
 
 424         FC_DEBUG_RPORT("Error %ld in state %s, retries %d\n",
 
 425                        PTR_ERR(fp), fc_rport_state(rport), rdata->retries);
 
 427         if (!fp || PTR_ERR(fp) == -FC_EX_TIMEOUT) {
 
 429                  * Memory allocation failure, or the exchange timed out.
 
 432                 if (rdata->retries < rdata->local_port->max_retry_count) {
 
 435                                 delay = msecs_to_jiffies(500);
 
 436                         get_device(&rport->dev);
 
 437                         schedule_delayed_work(&rdata->retry_work, delay);
 
 439                         switch (rdata->rp_state) {
 
 443                                 rdata->event = RPORT_EV_FAILED;
 
 444                                 queue_work(rport_event_queue,
 
 448                                 fc_rport_enter_ready(rport);
 
 460  * fc_rport_plogi_recv_resp - Handle incoming ELS PLOGI response
 
 461  * @sp: current sequence in the PLOGI exchange
 
 462  * @fp: response frame
 
 463  * @rp_arg: Fibre Channel remote port
 
 465  * Locking Note: This function will be called without the rport lock
 
 466  * held, but it will lock, call an _enter_* function or fc_rport_error
 
 467  * and then unlock the rport.
 
 469 static void fc_rport_plogi_resp(struct fc_seq *sp, struct fc_frame *fp,
 
 472         struct fc_rport *rport = rp_arg;
 
 473         struct fc_rport_libfc_priv *rdata = rport->dd_data;
 
 474         struct fc_lport *lport = rdata->local_port;
 
 475         struct fc_els_flogi *plp;
 
 481         mutex_lock(&rdata->rp_mutex);
 
 483         FC_DEBUG_RPORT("Received a PLOGI response from port (%6x)\n",
 
 486         if (rdata->rp_state != RPORT_ST_PLOGI) {
 
 487                 FC_DBG("Received a PLOGI response, but in state %s\n",
 
 488                        fc_rport_state(rport));
 
 493                 fc_rport_error(rport, fp);
 
 497         op = fc_frame_payload_op(fp);
 
 498         if (op == ELS_LS_ACC &&
 
 499             (plp = fc_frame_payload_get(fp, sizeof(*plp))) != NULL) {
 
 500                 rport->port_name = get_unaligned_be64(&plp->fl_wwpn);
 
 501                 rport->node_name = get_unaligned_be64(&plp->fl_wwnn);
 
 503                 tov = ntohl(plp->fl_csp.sp_e_d_tov);
 
 504                 if (ntohs(plp->fl_csp.sp_features) & FC_SP_FT_EDTR)
 
 506                 if (tov > rdata->e_d_tov)
 
 507                         rdata->e_d_tov = tov;
 
 508                 csp_seq = ntohs(plp->fl_csp.sp_tot_seq);
 
 509                 cssp_seq = ntohs(plp->fl_cssp[3 - 1].cp_con_seq);
 
 510                 if (cssp_seq < csp_seq)
 
 512                 rdata->max_seq = csp_seq;
 
 513                 rport->maxframe_size =
 
 514                         fc_plogi_get_maxframe(plp, lport->mfs);
 
 517                  * If the rport is one of the well known addresses
 
 518                  * we skip PRLI and RTV and go straight to READY.
 
 520                 if (rport->port_id >= FC_FID_DOM_MGR)
 
 521                         fc_rport_enter_ready(rport);
 
 523                         fc_rport_enter_prli(rport);
 
 525                 fc_rport_error(rport, fp);
 
 530         mutex_unlock(&rdata->rp_mutex);
 
 531         put_device(&rport->dev);
 
 535  * fc_rport_enter_plogi - Send Port Login (PLOGI) request to peer
 
 536  * @rport: Fibre Channel remote port to send PLOGI to
 
 538  * Locking Note: The rport lock is expected to be held before calling
 
 541 static void fc_rport_enter_plogi(struct fc_rport *rport)
 
 543         struct fc_rport_libfc_priv *rdata = rport->dd_data;
 
 544         struct fc_lport *lport = rdata->local_port;
 
 547         FC_DEBUG_RPORT("Port (%6x) entered PLOGI state from %s state\n",
 
 548                        rport->port_id, fc_rport_state(rport));
 
 550         fc_rport_state_enter(rport, RPORT_ST_PLOGI);
 
 552         rport->maxframe_size = FC_MIN_MAX_PAYLOAD;
 
 553         fp = fc_frame_alloc(lport, sizeof(struct fc_els_flogi));
 
 555                 fc_rport_error(rport, fp);
 
 558         rdata->e_d_tov = lport->e_d_tov;
 
 560         if (!lport->tt.elsct_send(lport, rport, fp, ELS_PLOGI,
 
 561                                   fc_rport_plogi_resp, rport, lport->e_d_tov))
 
 562                 fc_rport_error(rport, fp);
 
 564                 get_device(&rport->dev);
 
 568  * fc_rport_prli_resp - Process Login (PRLI) response handler
 
 569  * @sp: current sequence in the PRLI exchange
 
 570  * @fp: response frame
 
 571  * @rp_arg: Fibre Channel remote port
 
 573  * Locking Note: This function will be called without the rport lock
 
 574  * held, but it will lock, call an _enter_* function or fc_rport_error
 
 575  * and then unlock the rport.
 
 577 static void fc_rport_prli_resp(struct fc_seq *sp, struct fc_frame *fp,
 
 580         struct fc_rport *rport = rp_arg;
 
 581         struct fc_rport_libfc_priv *rdata = rport->dd_data;
 
 583                 struct fc_els_prli prli;
 
 584                 struct fc_els_spp spp;
 
 586         u32 roles = FC_RPORT_ROLE_UNKNOWN;
 
 590         mutex_lock(&rdata->rp_mutex);
 
 592         FC_DEBUG_RPORT("Received a PRLI response from port (%6x)\n",
 
 595         if (rdata->rp_state != RPORT_ST_PRLI) {
 
 596                 FC_DBG("Received a PRLI response, but in state %s\n",
 
 597                        fc_rport_state(rport));
 
 602                 fc_rport_error(rport, fp);
 
 606         op = fc_frame_payload_op(fp);
 
 607         if (op == ELS_LS_ACC) {
 
 608                 pp = fc_frame_payload_get(fp, sizeof(*pp));
 
 609                 if (pp && pp->prli.prli_spp_len >= sizeof(pp->spp)) {
 
 610                         fcp_parm = ntohl(pp->spp.spp_params);
 
 611                         if (fcp_parm & FCP_SPPF_RETRY)
 
 612                                 rdata->flags |= FC_RP_FLAGS_RETRY;
 
 615                 rport->supported_classes = FC_COS_CLASS3;
 
 616                 if (fcp_parm & FCP_SPPF_INIT_FCN)
 
 617                         roles |= FC_RPORT_ROLE_FCP_INITIATOR;
 
 618                 if (fcp_parm & FCP_SPPF_TARG_FCN)
 
 619                         roles |= FC_RPORT_ROLE_FCP_TARGET;
 
 621                 rport->roles = roles;
 
 622                 fc_rport_enter_rtv(rport);
 
 625                 FC_DBG("Bad ELS response\n");
 
 626                 rdata->event = RPORT_EV_FAILED;
 
 627                 queue_work(rport_event_queue, &rdata->event_work);
 
 633         mutex_unlock(&rdata->rp_mutex);
 
 634         put_device(&rport->dev);
 
 638  * fc_rport_logo_resp - Logout (LOGO) response handler
 
 639  * @sp: current sequence in the LOGO exchange
 
 640  * @fp: response frame
 
 641  * @rp_arg: Fibre Channel remote port
 
 643  * Locking Note: This function will be called without the rport lock
 
 644  * held, but it will lock, call an _enter_* function or fc_rport_error
 
 645  * and then unlock the rport.
 
 647 static void fc_rport_logo_resp(struct fc_seq *sp, struct fc_frame *fp,
 
 650         struct fc_rport *rport = rp_arg;
 
 651         struct fc_rport_libfc_priv *rdata = rport->dd_data;
 
 654         mutex_lock(&rdata->rp_mutex);
 
 656         FC_DEBUG_RPORT("Received a LOGO response from port (%6x)\n",
 
 660                 fc_rport_error(rport, fp);
 
 664         if (rdata->rp_state != RPORT_ST_LOGO) {
 
 665                 FC_DEBUG_RPORT("Received a LOGO response, but in state %s\n",
 
 666                                fc_rport_state(rport));
 
 670         op = fc_frame_payload_op(fp);
 
 671         if (op == ELS_LS_ACC) {
 
 672                 fc_rport_enter_rtv(rport);
 
 674                 FC_DBG("Bad ELS response\n");
 
 675                 rdata->event = RPORT_EV_LOGO;
 
 676                 queue_work(rport_event_queue, &rdata->event_work);
 
 682         mutex_unlock(&rdata->rp_mutex);
 
 683         put_device(&rport->dev);
 
 687  * fc_rport_enter_prli - Send Process Login (PRLI) request to peer
 
 688  * @rport: Fibre Channel remote port to send PRLI to
 
 690  * Locking Note: The rport lock is expected to be held before calling
 
 693 static void fc_rport_enter_prli(struct fc_rport *rport)
 
 695         struct fc_rport_libfc_priv *rdata = rport->dd_data;
 
 696         struct fc_lport *lport = rdata->local_port;
 
 698                 struct fc_els_prli prli;
 
 699                 struct fc_els_spp spp;
 
 703         FC_DEBUG_RPORT("Port (%6x) entered PRLI state from %s state\n",
 
 704                        rport->port_id, fc_rport_state(rport));
 
 706         fc_rport_state_enter(rport, RPORT_ST_PRLI);
 
 708         fp = fc_frame_alloc(lport, sizeof(*pp));
 
 710                 fc_rport_error(rport, fp);
 
 714         if (!lport->tt.elsct_send(lport, rport, fp, ELS_PRLI,
 
 715                                   fc_rport_prli_resp, rport, lport->e_d_tov))
 
 716                 fc_rport_error(rport, fp);
 
 718                 get_device(&rport->dev);
 
 722  * fc_rport_els_rtv_resp - Request Timeout Value response handler
 
 723  * @sp: current sequence in the RTV exchange
 
 724  * @fp: response frame
 
 725  * @rp_arg: Fibre Channel remote port
 
 727  * Many targets don't seem to support this.
 
 729  * Locking Note: This function will be called without the rport lock
 
 730  * held, but it will lock, call an _enter_* function or fc_rport_error
 
 731  * and then unlock the rport.
 
 733 static void fc_rport_rtv_resp(struct fc_seq *sp, struct fc_frame *fp,
 
 736         struct fc_rport *rport = rp_arg;
 
 737         struct fc_rport_libfc_priv *rdata = rport->dd_data;
 
 740         mutex_lock(&rdata->rp_mutex);
 
 742         FC_DEBUG_RPORT("Received a RTV response from port (%6x)\n",
 
 745         if (rdata->rp_state != RPORT_ST_RTV) {
 
 746                 FC_DBG("Received a RTV response, but in state %s\n",
 
 747                        fc_rport_state(rport));
 
 752                 fc_rport_error(rport, fp);
 
 756         op = fc_frame_payload_op(fp);
 
 757         if (op == ELS_LS_ACC) {
 
 758                 struct fc_els_rtv_acc *rtv;
 
 762                 rtv = fc_frame_payload_get(fp, sizeof(*rtv));
 
 764                         toq = ntohl(rtv->rtv_toq);
 
 765                         tov = ntohl(rtv->rtv_r_a_tov);
 
 768                         rdata->r_a_tov = tov;
 
 769                         tov = ntohl(rtv->rtv_e_d_tov);
 
 770                         if (toq & FC_ELS_RTV_EDRES)
 
 774                         rdata->e_d_tov = tov;
 
 778         fc_rport_enter_ready(rport);
 
 783         mutex_unlock(&rdata->rp_mutex);
 
 784         put_device(&rport->dev);
 
 788  * fc_rport_enter_rtv - Send Request Timeout Value (RTV) request to peer
 
 789  * @rport: Fibre Channel remote port to send RTV to
 
 791  * Locking Note: The rport lock is expected to be held before calling
 
 794 static void fc_rport_enter_rtv(struct fc_rport *rport)
 
 797         struct fc_rport_libfc_priv *rdata = rport->dd_data;
 
 798         struct fc_lport *lport = rdata->local_port;
 
 800         FC_DEBUG_RPORT("Port (%6x) entered RTV state from %s state\n",
 
 801                        rport->port_id, fc_rport_state(rport));
 
 803         fc_rport_state_enter(rport, RPORT_ST_RTV);
 
 805         fp = fc_frame_alloc(lport, sizeof(struct fc_els_rtv));
 
 807                 fc_rport_error(rport, fp);
 
 811         if (!lport->tt.elsct_send(lport, rport, fp, ELS_RTV,
 
 812                                      fc_rport_rtv_resp, rport, lport->e_d_tov))
 
 813                 fc_rport_error(rport, fp);
 
 815                 get_device(&rport->dev);
 
 819  * fc_rport_enter_logo - Send Logout (LOGO) request to peer
 
 820  * @rport: Fibre Channel remote port to send LOGO to
 
 822  * Locking Note: The rport lock is expected to be held before calling
 
 825 static void fc_rport_enter_logo(struct fc_rport *rport)
 
 827         struct fc_rport_libfc_priv *rdata = rport->dd_data;
 
 828         struct fc_lport *lport = rdata->local_port;
 
 831         FC_DEBUG_RPORT("Port (%6x) entered LOGO state from %s state\n",
 
 832                        rport->port_id, fc_rport_state(rport));
 
 834         fc_rport_state_enter(rport, RPORT_ST_LOGO);
 
 836         fp = fc_frame_alloc(lport, sizeof(struct fc_els_logo));
 
 838                 fc_rport_error(rport, fp);
 
 842         if (!lport->tt.elsct_send(lport, rport, fp, ELS_LOGO,
 
 843                                   fc_rport_logo_resp, rport, lport->e_d_tov))
 
 844                 fc_rport_error(rport, fp);
 
 846                 get_device(&rport->dev);
 
 851  * fc_rport_recv_req - Receive a request from a rport
 
 852  * @sp: current sequence in the PLOGI exchange
 
 853  * @fp: response frame
 
 854  * @rp_arg: Fibre Channel remote port
 
 856  * Locking Note: Called without the rport lock held. This
 
 857  * function will hold the rport lock, call an _enter_*
 
 858  * function and then unlock the rport.
 
 860 void fc_rport_recv_req(struct fc_seq *sp, struct fc_frame *fp,
 
 861                        struct fc_rport *rport)
 
 863         struct fc_rport_libfc_priv *rdata = rport->dd_data;
 
 864         struct fc_lport *lport = rdata->local_port;
 
 866         struct fc_frame_header *fh;
 
 867         struct fc_seq_els_data els_data;
 
 870         mutex_lock(&rdata->rp_mutex);
 
 873         els_data.explan = ELS_EXPL_NONE;
 
 874         els_data.reason = ELS_RJT_NONE;
 
 876         fh = fc_frame_header_get(fp);
 
 878         if (fh->fh_r_ctl == FC_RCTL_ELS_REQ && fh->fh_type == FC_TYPE_ELS) {
 
 879                 op = fc_frame_payload_op(fp);
 
 882                         fc_rport_recv_plogi_req(rport, sp, fp);
 
 885                         fc_rport_recv_prli_req(rport, sp, fp);
 
 888                         fc_rport_recv_prlo_req(rport, sp, fp);
 
 891                         fc_rport_recv_logo_req(rport, sp, fp);
 
 895                         lport->tt.seq_els_rsp_send(sp, ELS_RRQ, &els_data);
 
 899                         lport->tt.seq_els_rsp_send(sp, ELS_REC, &els_data);
 
 902                         els_data.reason = ELS_RJT_UNSUP;
 
 903                         lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &els_data);
 
 908         mutex_unlock(&rdata->rp_mutex);
 
 912  * fc_rport_recv_plogi_req - Handle incoming Port Login (PLOGI) request
 
 913  * @rport: Fibre Channel remote port that initiated PLOGI
 
 914  * @sp: current sequence in the PLOGI exchange
 
 915  * @fp: PLOGI request frame
 
 917  * Locking Note: The rport lock is exected to be held before calling
 
 920 static void fc_rport_recv_plogi_req(struct fc_rport *rport,
 
 921                                     struct fc_seq *sp, struct fc_frame *rx_fp)
 
 923         struct fc_rport_libfc_priv *rdata = rport->dd_data;
 
 924         struct fc_lport *lport = rdata->local_port;
 
 925         struct fc_frame *fp = rx_fp;
 
 927         struct fc_frame_header *fh;
 
 928         struct fc_els_flogi *pl;
 
 929         struct fc_seq_els_data rjt_data;
 
 933         enum fc_els_rjt_reason reject = 0;
 
 937         fh = fc_frame_header_get(fp);
 
 939         FC_DEBUG_RPORT("Received PLOGI request from port (%6x) "
 
 940                        "while in state %s\n", ntoh24(fh->fh_s_id),
 
 941                        fc_rport_state(rport));
 
 943         sid = ntoh24(fh->fh_s_id);
 
 944         pl = fc_frame_payload_get(fp, sizeof(*pl));
 
 946                 FC_DBG("incoming PLOGI from %x too short\n", sid);
 
 948                 /* XXX TBD: send reject? */
 
 952         wwpn = get_unaligned_be64(&pl->fl_wwpn);
 
 953         wwnn = get_unaligned_be64(&pl->fl_wwnn);
 
 956          * If the session was just created, possibly due to the incoming PLOGI,
 
 957          * set the state appropriately and accept the PLOGI.
 
 959          * If we had also sent a PLOGI, and if the received PLOGI is from a
 
 960          * higher WWPN, we accept it, otherwise an LS_RJT is sent with reason
 
 961          * "command already in progress".
 
 963          * XXX TBD: If the session was ready before, the PLOGI should result in
 
 964          * all outstanding exchanges being reset.
 
 966         switch (rdata->rp_state) {
 
 968                 FC_DEBUG_RPORT("incoming PLOGI from %6x wwpn %llx state INIT "
 
 969                                "- reject\n", sid, wwpn);
 
 970                 reject = ELS_RJT_UNSUP;
 
 973                 FC_DEBUG_RPORT("incoming PLOGI from %x in PLOGI state %d\n",
 
 974                                sid, rdata->rp_state);
 
 975                 if (wwpn < lport->wwpn)
 
 976                         reject = ELS_RJT_INPROG;
 
 980                 FC_DEBUG_RPORT("incoming PLOGI from %x in logged-in state %d "
 
 981                                "- ignored for now\n", sid, rdata->rp_state);
 
 982                 /* XXX TBD - should reset */
 
 986                 FC_DEBUG_RPORT("incoming PLOGI from %x in unexpected "
 
 987                                "state %d\n", sid, rdata->rp_state);
 
 992                 rjt_data.reason = reject;
 
 993                 rjt_data.explan = ELS_EXPL_NONE;
 
 994                 lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
 
 997                 fp = fc_frame_alloc(lport, sizeof(*pl));
 
1000                         rjt_data.reason = ELS_RJT_UNAB;
 
1001                         rjt_data.explan = ELS_EXPL_NONE;
 
1002                         lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
 
1005                         sp = lport->tt.seq_start_next(sp);
 
1007                         fc_rport_set_name(rport, wwpn, wwnn);
 
1010                          * Get session payload size from incoming PLOGI.
 
1012                         rport->maxframe_size =
 
1013                                 fc_plogi_get_maxframe(pl, lport->mfs);
 
1014                         fc_frame_free(rx_fp);
 
1015                         fc_plogi_fill(lport, fp, ELS_LS_ACC);
 
1018                          * Send LS_ACC.  If this fails,
 
1019                          * the originator should retry.
 
1021                         f_ctl = FC_FC_EX_CTX | FC_FC_LAST_SEQ;
 
1022                         f_ctl |= FC_FC_END_SEQ | FC_FC_SEQ_INIT;
 
1023                         ep = fc_seq_exch(sp);
 
1024                         fc_fill_fc_hdr(fp, FC_RCTL_ELS_REP, ep->did, ep->sid,
 
1025                                        FC_TYPE_ELS, f_ctl, 0);
 
1026                         lport->tt.seq_send(lport, sp, fp);
 
1027                         if (rdata->rp_state == RPORT_ST_PLOGI)
 
1028                                 fc_rport_enter_prli(rport);
 
1034  * fc_rport_recv_prli_req - Handle incoming Process Login (PRLI) request
 
1035  * @rport: Fibre Channel remote port that initiated PRLI
 
1036  * @sp: current sequence in the PRLI exchange
 
1037  * @fp: PRLI request frame
 
1039  * Locking Note: The rport lock is exected to be held before calling
 
1042 static void fc_rport_recv_prli_req(struct fc_rport *rport,
 
1043                                    struct fc_seq *sp, struct fc_frame *rx_fp)
 
1045         struct fc_rport_libfc_priv *rdata = rport->dd_data;
 
1046         struct fc_lport *lport = rdata->local_port;
 
1048         struct fc_frame *fp;
 
1049         struct fc_frame_header *fh;
 
1051                 struct fc_els_prli prli;
 
1052                 struct fc_els_spp spp;
 
1054         struct fc_els_spp *rspp;        /* request service param page */
 
1055         struct fc_els_spp *spp; /* response spp */
 
1058         enum fc_els_rjt_reason reason = ELS_RJT_UNAB;
 
1059         enum fc_els_rjt_explan explan = ELS_EXPL_NONE;
 
1060         enum fc_els_spp_resp resp;
 
1061         struct fc_seq_els_data rjt_data;
 
1064         u32 roles = FC_RPORT_ROLE_UNKNOWN;
 
1067         fh = fc_frame_header_get(rx_fp);
 
1069         FC_DEBUG_RPORT("Received PRLI request from port (%6x) "
 
1070                        "while in state %s\n", ntoh24(fh->fh_s_id),
 
1071                        fc_rport_state(rport));
 
1073         switch (rdata->rp_state) {
 
1075         case RPORT_ST_READY:
 
1076                 reason = ELS_RJT_NONE;
 
1081         len = fr_len(rx_fp) - sizeof(*fh);
 
1082         pp = fc_frame_payload_get(rx_fp, sizeof(*pp));
 
1084                 reason = ELS_RJT_PROT;
 
1085                 explan = ELS_EXPL_INV_LEN;
 
1087                 plen = ntohs(pp->prli.prli_len);
 
1088                 if ((plen % 4) != 0 || plen > len) {
 
1089                         reason = ELS_RJT_PROT;
 
1090                         explan = ELS_EXPL_INV_LEN;
 
1091                 } else if (plen < len) {
 
1094                 plen = pp->prli.prli_spp_len;
 
1095                 if ((plen % 4) != 0 || plen < sizeof(*spp) ||
 
1096                     plen > len || len < sizeof(*pp)) {
 
1097                         reason = ELS_RJT_PROT;
 
1098                         explan = ELS_EXPL_INV_LEN;
 
1102         if (reason != ELS_RJT_NONE ||
 
1103             (fp = fc_frame_alloc(lport, len)) == NULL) {
 
1104                 rjt_data.reason = reason;
 
1105                 rjt_data.explan = explan;
 
1106                 lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
 
1108                 sp = lport->tt.seq_start_next(sp);
 
1110                 pp = fc_frame_payload_get(fp, len);
 
1113                 pp->prli.prli_cmd = ELS_LS_ACC;
 
1114                 pp->prli.prli_spp_len = plen;
 
1115                 pp->prli.prli_len = htons(len);
 
1116                 len -= sizeof(struct fc_els_prli);
 
1119                  * Go through all the service parameter pages and build
 
1120                  * response.  If plen indicates longer SPP than standard,
 
1121                  * use that.  The entire response has been pre-cleared above.
 
1124                 while (len >= plen) {
 
1125                         spp->spp_type = rspp->spp_type;
 
1126                         spp->spp_type_ext = rspp->spp_type_ext;
 
1127                         spp->spp_flags = rspp->spp_flags & FC_SPP_EST_IMG_PAIR;
 
1128                         resp = FC_SPP_RESP_ACK;
 
1129                         if (rspp->spp_flags & FC_SPP_RPA_VAL)
 
1130                                 resp = FC_SPP_RESP_NO_PA;
 
1131                         switch (rspp->spp_type) {
 
1132                         case 0: /* common to all FC-4 types */
 
1135                                 fcp_parm = ntohl(rspp->spp_params);
 
1136                                 if (fcp_parm * FCP_SPPF_RETRY)
 
1137                                         rdata->flags |= FC_RP_FLAGS_RETRY;
 
1138                                 rport->supported_classes = FC_COS_CLASS3;
 
1139                                 if (fcp_parm & FCP_SPPF_INIT_FCN)
 
1140                                         roles |= FC_RPORT_ROLE_FCP_INITIATOR;
 
1141                                 if (fcp_parm & FCP_SPPF_TARG_FCN)
 
1142                                         roles |= FC_RPORT_ROLE_FCP_TARGET;
 
1143                                 rport->roles = roles;
 
1146                                         htonl(lport->service_params);
 
1149                                 resp = FC_SPP_RESP_INVL;
 
1152                         spp->spp_flags |= resp;
 
1154                         rspp = (struct fc_els_spp *)((char *)rspp + plen);
 
1155                         spp = (struct fc_els_spp *)((char *)spp + plen);
 
1159                  * Send LS_ACC.  If this fails, the originator should retry.
 
1161                 f_ctl = FC_FC_EX_CTX | FC_FC_LAST_SEQ;
 
1162                 f_ctl |= FC_FC_END_SEQ | FC_FC_SEQ_INIT;
 
1163                 ep = fc_seq_exch(sp);
 
1164                 fc_fill_fc_hdr(fp, FC_RCTL_ELS_REP, ep->did, ep->sid,
 
1165                                FC_TYPE_ELS, f_ctl, 0);
 
1166                 lport->tt.seq_send(lport, sp, fp);
 
1169                  * Get lock and re-check state.
 
1171                 switch (rdata->rp_state) {
 
1173                         fc_rport_enter_ready(rport);
 
1175                 case RPORT_ST_READY:
 
1181         fc_frame_free(rx_fp);
 
1185  * fc_rport_recv_prlo_req - Handle incoming Process Logout (PRLO) request
 
1186  * @rport: Fibre Channel remote port that initiated PRLO
 
1187  * @sp: current sequence in the PRLO exchange
 
1188  * @fp: PRLO request frame
 
1190  * Locking Note: The rport lock is exected to be held before calling
 
1193 static void fc_rport_recv_prlo_req(struct fc_rport *rport, struct fc_seq *sp,
 
1194                                    struct fc_frame *fp)
 
1196         struct fc_rport_libfc_priv *rdata = rport->dd_data;
 
1197         struct fc_lport *lport = rdata->local_port;
 
1199         struct fc_frame_header *fh;
 
1200         struct fc_seq_els_data rjt_data;
 
1202         fh = fc_frame_header_get(fp);
 
1204         FC_DEBUG_RPORT("Received PRLO request from port (%6x) "
 
1205                        "while in state %s\n", ntoh24(fh->fh_s_id),
 
1206                        fc_rport_state(rport));
 
1209         rjt_data.reason = ELS_RJT_UNAB;
 
1210         rjt_data.explan = ELS_EXPL_NONE;
 
1211         lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
 
1216  * fc_rport_recv_logo_req - Handle incoming Logout (LOGO) request
 
1217  * @rport: Fibre Channel remote port that initiated LOGO
 
1218  * @sp: current sequence in the LOGO exchange
 
1219  * @fp: LOGO request frame
 
1221  * Locking Note: The rport lock is exected to be held before calling
 
1224 static void fc_rport_recv_logo_req(struct fc_rport *rport, struct fc_seq *sp,
 
1225                                    struct fc_frame *fp)
 
1227         struct fc_frame_header *fh;
 
1228         struct fc_rport_libfc_priv *rdata = rport->dd_data;
 
1229         struct fc_lport *lport = rdata->local_port;
 
1231         fh = fc_frame_header_get(fp);
 
1233         FC_DEBUG_RPORT("Received LOGO request from port (%6x) "
 
1234                        "while in state %s\n", ntoh24(fh->fh_s_id),
 
1235                        fc_rport_state(rport));
 
1237         rdata->event = RPORT_EV_LOGO;
 
1238         queue_work(rport_event_queue, &rdata->event_work);
 
1240         lport->tt.seq_els_rsp_send(sp, ELS_LS_ACC, NULL);
 
1244 static void fc_rport_flush_queue(void)
 
1246         flush_workqueue(rport_event_queue);
 
1250 int fc_rport_init(struct fc_lport *lport)
 
1252         if (!lport->tt.rport_login)
 
1253                 lport->tt.rport_login = fc_rport_login;
 
1255         if (!lport->tt.rport_logoff)
 
1256                 lport->tt.rport_logoff = fc_rport_logoff;
 
1258         if (!lport->tt.rport_recv_req)
 
1259                 lport->tt.rport_recv_req = fc_rport_recv_req;
 
1261         if (!lport->tt.rport_flush_queue)
 
1262                 lport->tt.rport_flush_queue = fc_rport_flush_queue;
 
1266 EXPORT_SYMBOL(fc_rport_init);
 
1268 int fc_setup_rport()
 
1270         rport_event_queue = create_singlethread_workqueue("fc_rport_eq");
 
1271         if (!rport_event_queue)
 
1275 EXPORT_SYMBOL(fc_setup_rport);
 
1277 void fc_destroy_rport()
 
1279         destroy_workqueue(rport_event_queue);
 
1281 EXPORT_SYMBOL(fc_destroy_rport);
 
1283 void fc_rport_terminate_io(struct fc_rport *rport)
 
1285         struct fc_rport_libfc_priv *rdata = rport->dd_data;
 
1286         struct fc_lport *lport = rdata->local_port;
 
1288         lport->tt.exch_mgr_reset(lport->emp, 0, rport->port_id);
 
1289         lport->tt.exch_mgr_reset(lport->emp, rport->port_id, 0);
 
1291 EXPORT_SYMBOL(fc_rport_terminate_io);