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>
60 struct workqueue_struct *rport_event_queue;
62 static void fc_rport_enter_plogi(struct fc_rport_priv *);
63 static void fc_rport_enter_prli(struct fc_rport_priv *);
64 static void fc_rport_enter_rtv(struct fc_rport_priv *);
65 static void fc_rport_enter_ready(struct fc_rport_priv *);
66 static void fc_rport_enter_logo(struct fc_rport_priv *);
67 static void fc_rport_enter_adisc(struct fc_rport_priv *);
69 static void fc_rport_recv_plogi_req(struct fc_lport *,
70 struct fc_seq *, struct fc_frame *);
71 static void fc_rport_recv_prli_req(struct fc_rport_priv *,
72 struct fc_seq *, struct fc_frame *);
73 static void fc_rport_recv_prlo_req(struct fc_rport_priv *,
74 struct fc_seq *, struct fc_frame *);
75 static void fc_rport_recv_logo_req(struct fc_lport *,
76 struct fc_seq *, struct fc_frame *);
77 static void fc_rport_timeout(struct work_struct *);
78 static void fc_rport_error(struct fc_rport_priv *, struct fc_frame *);
79 static void fc_rport_error_retry(struct fc_rport_priv *, struct fc_frame *);
80 static void fc_rport_work(struct work_struct *);
82 static const char *fc_rport_state_names[] = {
83 [RPORT_ST_INIT] = "Init",
84 [RPORT_ST_PLOGI] = "PLOGI",
85 [RPORT_ST_PRLI] = "PRLI",
86 [RPORT_ST_RTV] = "RTV",
87 [RPORT_ST_READY] = "Ready",
88 [RPORT_ST_LOGO] = "LOGO",
89 [RPORT_ST_ADISC] = "ADISC",
90 [RPORT_ST_DELETE] = "Delete",
91 [RPORT_ST_RESTART] = "Restart",
95 * fc_rport_lookup() - Lookup a remote port by port_id
96 * @lport: The local port to lookup the remote port on
97 * @port_id: The remote port ID to look up
99 static struct fc_rport_priv *fc_rport_lookup(const struct fc_lport *lport,
102 struct fc_rport_priv *rdata;
104 list_for_each_entry(rdata, &lport->disc.rports, peers)
105 if (rdata->ids.port_id == port_id)
111 * fc_rport_create() - Create a new remote port
112 * @lport: The local port this remote port will be associated with
113 * @ids: The identifiers for the new remote port
115 * The remote port will start in the INIT state.
117 * Locking note: must be called with the disc_mutex held.
119 static struct fc_rport_priv *fc_rport_create(struct fc_lport *lport,
122 struct fc_rport_priv *rdata;
124 rdata = lport->tt.rport_lookup(lport, port_id);
128 rdata = kzalloc(sizeof(*rdata), GFP_KERNEL);
132 rdata->ids.node_name = -1;
133 rdata->ids.port_name = -1;
134 rdata->ids.port_id = port_id;
135 rdata->ids.roles = FC_RPORT_ROLE_UNKNOWN;
137 kref_init(&rdata->kref);
138 mutex_init(&rdata->rp_mutex);
139 rdata->local_port = lport;
140 rdata->rp_state = RPORT_ST_INIT;
141 rdata->event = RPORT_EV_NONE;
142 rdata->flags = FC_RP_FLAGS_REC_SUPPORTED;
143 rdata->e_d_tov = lport->e_d_tov;
144 rdata->r_a_tov = lport->r_a_tov;
145 rdata->maxframe_size = FC_MIN_MAX_PAYLOAD;
146 INIT_DELAYED_WORK(&rdata->retry_work, fc_rport_timeout);
147 INIT_WORK(&rdata->event_work, fc_rport_work);
148 if (port_id != FC_FID_DIR_SERV)
149 list_add(&rdata->peers, &lport->disc.rports);
154 * fc_rport_destroy() - Free a remote port after last reference is released
155 * @kref: The remote port's kref
157 static void fc_rport_destroy(struct kref *kref)
159 struct fc_rport_priv *rdata;
161 rdata = container_of(kref, struct fc_rport_priv, kref);
166 * fc_rport_state() - Return a string identifying the remote port's state
167 * @rdata: The remote port
169 static const char *fc_rport_state(struct fc_rport_priv *rdata)
173 cp = fc_rport_state_names[rdata->rp_state];
180 * fc_set_rport_loss_tmo() - Set the remote port loss timeout
181 * @rport: The remote port that gets a new timeout value
182 * @timeout: The new timeout value (in seconds)
184 void fc_set_rport_loss_tmo(struct fc_rport *rport, u32 timeout)
187 rport->dev_loss_tmo = timeout + 5;
189 rport->dev_loss_tmo = 30;
191 EXPORT_SYMBOL(fc_set_rport_loss_tmo);
194 * fc_plogi_get_maxframe() - Get the maximum payload from the common service
195 * parameters in a FLOGI frame
196 * @flp: The FLOGI payload
197 * @maxval: The maximum frame size upper limit; this may be less than what
198 * is in the service parameters
200 static unsigned int fc_plogi_get_maxframe(struct fc_els_flogi *flp,
206 * Get max payload from the common service parameters and the
207 * class 3 receive data field size.
209 mfs = ntohs(flp->fl_csp.sp_bb_data) & FC_SP_BB_DATA_MASK;
210 if (mfs >= FC_SP_MIN_MAX_PAYLOAD && mfs < maxval)
212 mfs = ntohs(flp->fl_cssp[3 - 1].cp_rdfs);
213 if (mfs >= FC_SP_MIN_MAX_PAYLOAD && mfs < maxval)
219 * fc_rport_state_enter() - Change the state of a remote port
220 * @rdata: The remote port whose state should change
221 * @new: The new state
223 * Locking Note: Called with the rport lock held
225 static void fc_rport_state_enter(struct fc_rport_priv *rdata,
226 enum fc_rport_state new)
228 if (rdata->rp_state != new)
230 rdata->rp_state = new;
234 * fc_rport_work() - Handler for remote port events in the rport_event_queue
235 * @work: Handle to the remote port being dequeued
237 static void fc_rport_work(struct work_struct *work)
240 struct fc_rport_priv *rdata =
241 container_of(work, struct fc_rport_priv, event_work);
242 struct fc_rport_libfc_priv *rpriv;
243 enum fc_rport_event event;
244 struct fc_lport *lport = rdata->local_port;
245 struct fc_rport_operations *rport_ops;
246 struct fc_rport_identifiers ids;
247 struct fc_rport *rport;
250 mutex_lock(&rdata->rp_mutex);
251 event = rdata->event;
252 rport_ops = rdata->ops;
253 rport = rdata->rport;
255 FC_RPORT_DBG(rdata, "work event %u\n", event);
260 rdata->event = RPORT_EV_NONE;
261 kref_get(&rdata->kref);
262 mutex_unlock(&rdata->rp_mutex);
265 rport = fc_remote_port_add(lport->host, 0, &ids);
267 FC_RPORT_DBG(rdata, "Failed to add the rport\n");
268 lport->tt.rport_logoff(rdata);
269 kref_put(&rdata->kref, lport->tt.rport_destroy);
272 mutex_lock(&rdata->rp_mutex);
274 FC_RPORT_DBG(rdata, "rport already allocated\n");
275 rdata->rport = rport;
276 rport->maxframe_size = rdata->maxframe_size;
277 rport->supported_classes = rdata->supported_classes;
279 rpriv = rport->dd_data;
280 rpriv->local_port = lport;
281 rpriv->rp_state = rdata->rp_state;
282 rpriv->flags = rdata->flags;
283 rpriv->e_d_tov = rdata->e_d_tov;
284 rpriv->r_a_tov = rdata->r_a_tov;
285 mutex_unlock(&rdata->rp_mutex);
287 if (rport_ops && rport_ops->event_callback) {
288 FC_RPORT_DBG(rdata, "callback ev %d\n", event);
289 rport_ops->event_callback(lport, rdata, event);
291 kref_put(&rdata->kref, lport->tt.rport_destroy);
294 case RPORT_EV_FAILED:
297 port_id = rdata->ids.port_id;
298 mutex_unlock(&rdata->rp_mutex);
300 if (port_id != FC_FID_DIR_SERV) {
302 * We must drop rp_mutex before taking disc_mutex.
303 * Re-evaluate state to allow for restart.
304 * A transition to RESTART state must only happen
305 * while disc_mutex is held and rdata is on the list.
307 mutex_lock(&lport->disc.disc_mutex);
308 mutex_lock(&rdata->rp_mutex);
309 if (rdata->rp_state == RPORT_ST_RESTART)
312 list_del(&rdata->peers);
313 mutex_unlock(&rdata->rp_mutex);
314 mutex_unlock(&lport->disc.disc_mutex);
317 if (rport_ops && rport_ops->event_callback) {
318 FC_RPORT_DBG(rdata, "callback ev %d\n", event);
319 rport_ops->event_callback(lport, rdata, event);
321 cancel_delayed_work_sync(&rdata->retry_work);
324 * Reset any outstanding exchanges before freeing rport.
326 lport->tt.exch_mgr_reset(lport, 0, port_id);
327 lport->tt.exch_mgr_reset(lport, port_id, 0);
330 rpriv = rport->dd_data;
331 rpriv->rp_state = RPORT_ST_DELETE;
332 mutex_lock(&rdata->rp_mutex);
334 mutex_unlock(&rdata->rp_mutex);
335 fc_remote_port_delete(rport);
338 mutex_lock(&rdata->rp_mutex);
339 FC_RPORT_DBG(rdata, "work restart\n");
340 fc_rport_enter_plogi(rdata);
341 mutex_unlock(&rdata->rp_mutex);
343 kref_put(&rdata->kref, lport->tt.rport_destroy);
347 mutex_unlock(&rdata->rp_mutex);
353 * fc_rport_login() - Start the remote port login state machine
354 * @rdata: The remote port to be logged in to
356 * Locking Note: Called without the rport lock held. This
357 * function will hold the rport lock, call an _enter_*
358 * function and then unlock the rport.
360 * This indicates the intent to be logged into the remote port.
361 * If it appears we are already logged in, ADISC is used to verify
364 int fc_rport_login(struct fc_rport_priv *rdata)
366 mutex_lock(&rdata->rp_mutex);
368 switch (rdata->rp_state) {
370 FC_RPORT_DBG(rdata, "ADISC port\n");
371 fc_rport_enter_adisc(rdata);
373 case RPORT_ST_RESTART:
375 case RPORT_ST_DELETE:
376 FC_RPORT_DBG(rdata, "Restart deleted port\n");
377 fc_rport_state_enter(rdata, RPORT_ST_RESTART);
380 FC_RPORT_DBG(rdata, "Login to port\n");
381 fc_rport_enter_plogi(rdata);
384 mutex_unlock(&rdata->rp_mutex);
390 * fc_rport_enter_delete() - Schedule a remote port to be deleted
391 * @rdata: The remote port to be deleted
392 * @event: The event to report as the reason for deletion
394 * Locking Note: Called with the rport lock held.
396 * Allow state change into DELETE only once.
398 * Call queue_work only if there's no event already pending.
399 * Set the new event so that the old pending event will not occur.
400 * Since we have the mutex, even if fc_rport_work() is already started,
401 * it'll see the new event.
403 static void fc_rport_enter_delete(struct fc_rport_priv *rdata,
404 enum fc_rport_event event)
406 if (rdata->rp_state == RPORT_ST_DELETE)
409 FC_RPORT_DBG(rdata, "Delete port\n");
411 fc_rport_state_enter(rdata, RPORT_ST_DELETE);
413 if (rdata->event == RPORT_EV_NONE)
414 queue_work(rport_event_queue, &rdata->event_work);
415 rdata->event = event;
419 * fc_rport_logoff() - Logoff and remove a remote port
420 * @rdata: The remote port to be logged off of
422 * Locking Note: Called without the rport lock held. This
423 * function will hold the rport lock, call an _enter_*
424 * function and then unlock the rport.
426 int fc_rport_logoff(struct fc_rport_priv *rdata)
428 mutex_lock(&rdata->rp_mutex);
430 FC_RPORT_DBG(rdata, "Remove port\n");
432 if (rdata->rp_state == RPORT_ST_DELETE) {
433 FC_RPORT_DBG(rdata, "Port in Delete state, not removing\n");
437 if (rdata->rp_state == RPORT_ST_RESTART)
438 FC_RPORT_DBG(rdata, "Port in Restart state, deleting\n");
440 fc_rport_enter_logo(rdata);
443 * Change the state to Delete so that we discard
446 fc_rport_enter_delete(rdata, RPORT_EV_STOP);
448 mutex_unlock(&rdata->rp_mutex);
453 * fc_rport_enter_ready() - Transition to the RPORT_ST_READY state
454 * @rdata: The remote port that is ready
456 * Locking Note: The rport lock is expected to be held before calling
459 static void fc_rport_enter_ready(struct fc_rport_priv *rdata)
461 fc_rport_state_enter(rdata, RPORT_ST_READY);
463 FC_RPORT_DBG(rdata, "Port is Ready\n");
465 if (rdata->event == RPORT_EV_NONE)
466 queue_work(rport_event_queue, &rdata->event_work);
467 rdata->event = RPORT_EV_READY;
471 * fc_rport_timeout() - Handler for the retry_work timer
472 * @work: Handle to the remote port that has timed out
474 * Locking Note: Called without the rport lock held. This
475 * function will hold the rport lock, call an _enter_*
476 * function and then unlock the rport.
478 static void fc_rport_timeout(struct work_struct *work)
480 struct fc_rport_priv *rdata =
481 container_of(work, struct fc_rport_priv, retry_work.work);
483 mutex_lock(&rdata->rp_mutex);
485 switch (rdata->rp_state) {
487 fc_rport_enter_plogi(rdata);
490 fc_rport_enter_prli(rdata);
493 fc_rport_enter_rtv(rdata);
496 fc_rport_enter_logo(rdata);
499 fc_rport_enter_adisc(rdata);
503 case RPORT_ST_DELETE:
504 case RPORT_ST_RESTART:
508 mutex_unlock(&rdata->rp_mutex);
512 * fc_rport_error() - Error handler, called once retries have been exhausted
513 * @rdata: The remote port the error is happened on
514 * @fp: The error code encapsulated in a frame pointer
516 * Locking Note: The rport lock is expected to be held before
517 * calling this routine
519 static void fc_rport_error(struct fc_rport_priv *rdata, struct fc_frame *fp)
521 FC_RPORT_DBG(rdata, "Error %ld in state %s, retries %d\n",
522 IS_ERR(fp) ? -PTR_ERR(fp) : 0,
523 fc_rport_state(rdata), rdata->retries);
525 switch (rdata->rp_state) {
528 fc_rport_enter_delete(rdata, RPORT_EV_FAILED);
531 fc_rport_enter_ready(rdata);
535 fc_rport_enter_logo(rdata);
537 case RPORT_ST_DELETE:
538 case RPORT_ST_RESTART:
546 * fc_rport_error_retry() - Handler for remote port state retries
547 * @rdata: The remote port whose state is to be retried
548 * @fp: The error code encapsulated in a frame pointer
550 * If the error was an exchange timeout retry immediately,
551 * otherwise wait for E_D_TOV.
553 * Locking Note: The rport lock is expected to be held before
554 * calling this routine
556 static void fc_rport_error_retry(struct fc_rport_priv *rdata,
559 unsigned long delay = FC_DEF_E_D_TOV;
561 /* make sure this isn't an FC_EX_CLOSED error, never retry those */
562 if (PTR_ERR(fp) == -FC_EX_CLOSED)
563 return fc_rport_error(rdata, fp);
565 if (rdata->retries < rdata->local_port->max_rport_retry_count) {
566 FC_RPORT_DBG(rdata, "Error %ld in state %s, retrying\n",
567 PTR_ERR(fp), fc_rport_state(rdata));
569 /* no additional delay on exchange timeouts */
570 if (PTR_ERR(fp) == -FC_EX_TIMEOUT)
572 schedule_delayed_work(&rdata->retry_work, delay);
576 return fc_rport_error(rdata, fp);
580 * fc_rport_plogi_recv_resp() - Handler for ELS PLOGI responses
581 * @sp: The sequence the PLOGI is on
582 * @fp: The PLOGI response frame
583 * @rdata_arg: The remote port that sent the PLOGI response
585 * Locking Note: This function will be called without the rport lock
586 * held, but it will lock, call an _enter_* function or fc_rport_error
587 * and then unlock the rport.
589 static void fc_rport_plogi_resp(struct fc_seq *sp, struct fc_frame *fp,
592 struct fc_rport_priv *rdata = rdata_arg;
593 struct fc_lport *lport = rdata->local_port;
594 struct fc_els_flogi *plp = NULL;
600 mutex_lock(&rdata->rp_mutex);
602 FC_RPORT_DBG(rdata, "Received a PLOGI %s\n", fc_els_resp_type(fp));
604 if (rdata->rp_state != RPORT_ST_PLOGI) {
605 FC_RPORT_DBG(rdata, "Received a PLOGI response, but in state "
606 "%s\n", fc_rport_state(rdata));
613 fc_rport_error_retry(rdata, fp);
617 op = fc_frame_payload_op(fp);
618 if (op == ELS_LS_ACC &&
619 (plp = fc_frame_payload_get(fp, sizeof(*plp))) != NULL) {
620 rdata->ids.port_name = get_unaligned_be64(&plp->fl_wwpn);
621 rdata->ids.node_name = get_unaligned_be64(&plp->fl_wwnn);
623 tov = ntohl(plp->fl_csp.sp_e_d_tov);
624 if (ntohs(plp->fl_csp.sp_features) & FC_SP_FT_EDTR)
626 if (tov > rdata->e_d_tov)
627 rdata->e_d_tov = tov;
628 csp_seq = ntohs(plp->fl_csp.sp_tot_seq);
629 cssp_seq = ntohs(plp->fl_cssp[3 - 1].cp_con_seq);
630 if (cssp_seq < csp_seq)
632 rdata->max_seq = csp_seq;
633 rdata->maxframe_size = fc_plogi_get_maxframe(plp, lport->mfs);
634 fc_rport_enter_prli(rdata);
636 fc_rport_error_retry(rdata, fp);
641 mutex_unlock(&rdata->rp_mutex);
642 kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
646 * fc_rport_enter_plogi() - Send Port Login (PLOGI) request
647 * @rdata: The remote port to send a PLOGI to
649 * Locking Note: The rport lock is expected to be held before calling
652 static void fc_rport_enter_plogi(struct fc_rport_priv *rdata)
654 struct fc_lport *lport = rdata->local_port;
657 FC_RPORT_DBG(rdata, "Port entered PLOGI state from %s state\n",
658 fc_rport_state(rdata));
660 fc_rport_state_enter(rdata, RPORT_ST_PLOGI);
662 rdata->maxframe_size = FC_MIN_MAX_PAYLOAD;
663 fp = fc_frame_alloc(lport, sizeof(struct fc_els_flogi));
665 fc_rport_error_retry(rdata, fp);
668 rdata->e_d_tov = lport->e_d_tov;
670 if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_PLOGI,
671 fc_rport_plogi_resp, rdata,
673 fc_rport_error_retry(rdata, NULL);
675 kref_get(&rdata->kref);
679 * fc_rport_prli_resp() - Process Login (PRLI) response handler
680 * @sp: The sequence the PRLI response was on
681 * @fp: The PRLI response frame
682 * @rdata_arg: The remote port that sent the PRLI response
684 * Locking Note: This function will be called without the rport lock
685 * held, but it will lock, call an _enter_* function or fc_rport_error
686 * and then unlock the rport.
688 static void fc_rport_prli_resp(struct fc_seq *sp, struct fc_frame *fp,
691 struct fc_rport_priv *rdata = rdata_arg;
693 struct fc_els_prli prli;
694 struct fc_els_spp spp;
696 u32 roles = FC_RPORT_ROLE_UNKNOWN;
700 mutex_lock(&rdata->rp_mutex);
702 FC_RPORT_DBG(rdata, "Received a PRLI %s\n", fc_els_resp_type(fp));
704 if (rdata->rp_state != RPORT_ST_PRLI) {
705 FC_RPORT_DBG(rdata, "Received a PRLI response, but in state "
706 "%s\n", fc_rport_state(rdata));
713 fc_rport_error_retry(rdata, fp);
717 /* reinitialize remote port roles */
718 rdata->ids.roles = FC_RPORT_ROLE_UNKNOWN;
720 op = fc_frame_payload_op(fp);
721 if (op == ELS_LS_ACC) {
722 pp = fc_frame_payload_get(fp, sizeof(*pp));
723 if (pp && pp->prli.prli_spp_len >= sizeof(pp->spp)) {
724 fcp_parm = ntohl(pp->spp.spp_params);
725 if (fcp_parm & FCP_SPPF_RETRY)
726 rdata->flags |= FC_RP_FLAGS_RETRY;
729 rdata->supported_classes = FC_COS_CLASS3;
730 if (fcp_parm & FCP_SPPF_INIT_FCN)
731 roles |= FC_RPORT_ROLE_FCP_INITIATOR;
732 if (fcp_parm & FCP_SPPF_TARG_FCN)
733 roles |= FC_RPORT_ROLE_FCP_TARGET;
735 rdata->ids.roles = roles;
736 fc_rport_enter_rtv(rdata);
739 FC_RPORT_DBG(rdata, "Bad ELS response for PRLI command\n");
740 fc_rport_enter_delete(rdata, RPORT_EV_FAILED);
746 mutex_unlock(&rdata->rp_mutex);
747 kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
751 * fc_rport_logo_resp() - Handler for logout (LOGO) responses
752 * @sp: The sequence the LOGO was on
753 * @fp: The LOGO response frame
754 * @rdata_arg: The remote port that sent the LOGO response
756 * Locking Note: This function will be called without the rport lock
757 * held, but it will lock, call an _enter_* function or fc_rport_error
758 * and then unlock the rport.
760 static void fc_rport_logo_resp(struct fc_seq *sp, struct fc_frame *fp,
763 struct fc_rport_priv *rdata = rdata_arg;
766 mutex_lock(&rdata->rp_mutex);
768 FC_RPORT_DBG(rdata, "Received a LOGO %s\n", fc_els_resp_type(fp));
770 if (rdata->rp_state != RPORT_ST_LOGO) {
771 FC_RPORT_DBG(rdata, "Received a LOGO response, but in state "
772 "%s\n", fc_rport_state(rdata));
779 fc_rport_error_retry(rdata, fp);
783 op = fc_frame_payload_op(fp);
784 if (op != ELS_LS_ACC)
785 FC_RPORT_DBG(rdata, "Bad ELS response op %x for LOGO command\n",
787 fc_rport_enter_delete(rdata, RPORT_EV_LOGO);
792 mutex_unlock(&rdata->rp_mutex);
793 kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
797 * fc_rport_enter_prli() - Send Process Login (PRLI) request
798 * @rdata: The remote port to send the PRLI request to
800 * Locking Note: The rport lock is expected to be held before calling
803 static void fc_rport_enter_prli(struct fc_rport_priv *rdata)
805 struct fc_lport *lport = rdata->local_port;
807 struct fc_els_prli prli;
808 struct fc_els_spp spp;
813 * If the rport is one of the well known addresses
814 * we skip PRLI and RTV and go straight to READY.
816 if (rdata->ids.port_id >= FC_FID_DOM_MGR) {
817 fc_rport_enter_ready(rdata);
821 FC_RPORT_DBG(rdata, "Port entered PRLI state from %s state\n",
822 fc_rport_state(rdata));
824 fc_rport_state_enter(rdata, RPORT_ST_PRLI);
826 fp = fc_frame_alloc(lport, sizeof(*pp));
828 fc_rport_error_retry(rdata, fp);
832 if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_PRLI,
833 fc_rport_prli_resp, rdata,
835 fc_rport_error_retry(rdata, NULL);
837 kref_get(&rdata->kref);
841 * fc_rport_els_rtv_resp() - Handler for Request Timeout Value (RTV) responses
842 * @sp: The sequence the RTV was on
843 * @fp: The RTV response frame
844 * @rdata_arg: The remote port that sent the RTV response
846 * Many targets don't seem to support this.
848 * Locking Note: This function will be called without the rport lock
849 * held, but it will lock, call an _enter_* function or fc_rport_error
850 * and then unlock the rport.
852 static void fc_rport_rtv_resp(struct fc_seq *sp, struct fc_frame *fp,
855 struct fc_rport_priv *rdata = rdata_arg;
858 mutex_lock(&rdata->rp_mutex);
860 FC_RPORT_DBG(rdata, "Received a RTV %s\n", fc_els_resp_type(fp));
862 if (rdata->rp_state != RPORT_ST_RTV) {
863 FC_RPORT_DBG(rdata, "Received a RTV response, but in state "
864 "%s\n", fc_rport_state(rdata));
871 fc_rport_error(rdata, fp);
875 op = fc_frame_payload_op(fp);
876 if (op == ELS_LS_ACC) {
877 struct fc_els_rtv_acc *rtv;
881 rtv = fc_frame_payload_get(fp, sizeof(*rtv));
883 toq = ntohl(rtv->rtv_toq);
884 tov = ntohl(rtv->rtv_r_a_tov);
887 rdata->r_a_tov = tov;
888 tov = ntohl(rtv->rtv_e_d_tov);
889 if (toq & FC_ELS_RTV_EDRES)
893 rdata->e_d_tov = tov;
897 fc_rport_enter_ready(rdata);
902 mutex_unlock(&rdata->rp_mutex);
903 kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
907 * fc_rport_enter_rtv() - Send Request Timeout Value (RTV) request
908 * @rdata: The remote port to send the RTV request to
910 * Locking Note: The rport lock is expected to be held before calling
913 static void fc_rport_enter_rtv(struct fc_rport_priv *rdata)
916 struct fc_lport *lport = rdata->local_port;
918 FC_RPORT_DBG(rdata, "Port entered RTV state from %s state\n",
919 fc_rport_state(rdata));
921 fc_rport_state_enter(rdata, RPORT_ST_RTV);
923 fp = fc_frame_alloc(lport, sizeof(struct fc_els_rtv));
925 fc_rport_error_retry(rdata, fp);
929 if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_RTV,
930 fc_rport_rtv_resp, rdata,
932 fc_rport_error_retry(rdata, NULL);
934 kref_get(&rdata->kref);
938 * fc_rport_enter_logo() - Send a logout (LOGO) request
939 * @rdata: The remote port to send the LOGO request to
941 * Locking Note: The rport lock is expected to be held before calling
944 static void fc_rport_enter_logo(struct fc_rport_priv *rdata)
946 struct fc_lport *lport = rdata->local_port;
949 FC_RPORT_DBG(rdata, "Port entered LOGO state from %s state\n",
950 fc_rport_state(rdata));
952 fc_rport_state_enter(rdata, RPORT_ST_LOGO);
954 fp = fc_frame_alloc(lport, sizeof(struct fc_els_logo));
956 fc_rport_error_retry(rdata, fp);
960 if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_LOGO,
961 fc_rport_logo_resp, rdata,
963 fc_rport_error_retry(rdata, NULL);
965 kref_get(&rdata->kref);
969 * fc_rport_els_adisc_resp() - Handler for Address Discovery (ADISC) responses
970 * @sp: The sequence the ADISC response was on
971 * @fp: The ADISC response frame
972 * @rdata_arg: The remote port that sent the ADISC response
974 * Locking Note: This function will be called without the rport lock
975 * held, but it will lock, call an _enter_* function or fc_rport_error
976 * and then unlock the rport.
978 static void fc_rport_adisc_resp(struct fc_seq *sp, struct fc_frame *fp,
981 struct fc_rport_priv *rdata = rdata_arg;
982 struct fc_els_adisc *adisc;
985 mutex_lock(&rdata->rp_mutex);
987 FC_RPORT_DBG(rdata, "Received a ADISC response\n");
989 if (rdata->rp_state != RPORT_ST_ADISC) {
990 FC_RPORT_DBG(rdata, "Received a ADISC resp but in state %s\n",
991 fc_rport_state(rdata));
998 fc_rport_error(rdata, fp);
1003 * If address verification failed. Consider us logged out of the rport.
1004 * Since the rport is still in discovery, we want to be
1005 * logged in, so go to PLOGI state. Otherwise, go back to READY.
1007 op = fc_frame_payload_op(fp);
1008 adisc = fc_frame_payload_get(fp, sizeof(*adisc));
1009 if (op != ELS_LS_ACC || !adisc ||
1010 ntoh24(adisc->adisc_port_id) != rdata->ids.port_id ||
1011 get_unaligned_be64(&adisc->adisc_wwpn) != rdata->ids.port_name ||
1012 get_unaligned_be64(&adisc->adisc_wwnn) != rdata->ids.node_name) {
1013 FC_RPORT_DBG(rdata, "ADISC error or mismatch\n");
1014 fc_rport_enter_plogi(rdata);
1016 FC_RPORT_DBG(rdata, "ADISC OK\n");
1017 fc_rport_enter_ready(rdata);
1022 mutex_unlock(&rdata->rp_mutex);
1023 kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
1027 * fc_rport_enter_adisc() - Send Address Discover (ADISC) request
1028 * @rdata: The remote port to send the ADISC request to
1030 * Locking Note: The rport lock is expected to be held before calling
1033 static void fc_rport_enter_adisc(struct fc_rport_priv *rdata)
1035 struct fc_lport *lport = rdata->local_port;
1036 struct fc_frame *fp;
1038 FC_RPORT_DBG(rdata, "sending ADISC from %s state\n",
1039 fc_rport_state(rdata));
1041 fc_rport_state_enter(rdata, RPORT_ST_ADISC);
1043 fp = fc_frame_alloc(lport, sizeof(struct fc_els_adisc));
1045 fc_rport_error_retry(rdata, fp);
1048 if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_ADISC,
1049 fc_rport_adisc_resp, rdata,
1050 2 * lport->r_a_tov))
1051 fc_rport_error_retry(rdata, NULL);
1053 kref_get(&rdata->kref);
1057 * fc_rport_recv_adisc_req() - Handler for Address Discovery (ADISC) requests
1058 * @rdata: The remote port that sent the ADISC request
1059 * @sp: The sequence the ADISC request was on
1060 * @in_fp: The ADISC request frame
1062 * Locking Note: Called with the lport and rport locks held.
1064 static void fc_rport_recv_adisc_req(struct fc_rport_priv *rdata,
1065 struct fc_seq *sp, struct fc_frame *in_fp)
1067 struct fc_lport *lport = rdata->local_port;
1068 struct fc_frame *fp;
1069 struct fc_exch *ep = fc_seq_exch(sp);
1070 struct fc_els_adisc *adisc;
1071 struct fc_seq_els_data rjt_data;
1074 FC_RPORT_DBG(rdata, "Received ADISC request\n");
1076 adisc = fc_frame_payload_get(in_fp, sizeof(*adisc));
1079 rjt_data.reason = ELS_RJT_PROT;
1080 rjt_data.explan = ELS_EXPL_INV_LEN;
1081 lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
1085 fp = fc_frame_alloc(lport, sizeof(*adisc));
1088 fc_adisc_fill(lport, fp);
1089 adisc = fc_frame_payload_get(fp, sizeof(*adisc));
1090 adisc->adisc_cmd = ELS_LS_ACC;
1091 sp = lport->tt.seq_start_next(sp);
1092 f_ctl = FC_FC_EX_CTX | FC_FC_LAST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT;
1093 fc_fill_fc_hdr(fp, FC_RCTL_ELS_REP, ep->did, ep->sid,
1094 FC_TYPE_ELS, f_ctl, 0);
1095 lport->tt.seq_send(lport, sp, fp);
1097 fc_frame_free(in_fp);
1101 * fc_rport_recv_rls_req() - Handle received Read Link Status request
1102 * @rdata: The remote port that sent the RLS request
1103 * @sp: The sequence that the RLS was on
1104 * @rx_fp: The PRLI request frame
1106 * Locking Note: The rport lock is expected to be held before calling
1109 static void fc_rport_recv_rls_req(struct fc_rport_priv *rdata,
1110 struct fc_seq *sp, struct fc_frame *rx_fp)
1113 struct fc_lport *lport = rdata->local_port;
1114 struct fc_frame *fp;
1115 struct fc_exch *ep = fc_seq_exch(sp);
1116 struct fc_els_rls *rls;
1117 struct fc_els_rls_resp *rsp;
1118 struct fc_els_lesb *lesb;
1119 struct fc_seq_els_data rjt_data;
1120 struct fc_host_statistics *hst;
1123 FC_RPORT_DBG(rdata, "Received RLS request while in state %s\n",
1124 fc_rport_state(rdata));
1126 rls = fc_frame_payload_get(rx_fp, sizeof(*rls));
1128 rjt_data.reason = ELS_RJT_PROT;
1129 rjt_data.explan = ELS_EXPL_INV_LEN;
1133 fp = fc_frame_alloc(lport, sizeof(*rsp));
1135 rjt_data.reason = ELS_RJT_UNAB;
1136 rjt_data.explan = ELS_EXPL_INSUF_RES;
1140 rsp = fc_frame_payload_get(fp, sizeof(*rsp));
1141 memset(rsp, 0, sizeof(*rsp));
1142 rsp->rls_cmd = ELS_LS_ACC;
1143 lesb = &rsp->rls_lesb;
1144 if (lport->tt.get_lesb) {
1145 /* get LESB from LLD if it supports it */
1146 lport->tt.get_lesb(lport, lesb);
1148 fc_get_host_stats(lport->host);
1149 hst = &lport->host_stats;
1150 lesb->lesb_link_fail = htonl(hst->link_failure_count);
1151 lesb->lesb_sync_loss = htonl(hst->loss_of_sync_count);
1152 lesb->lesb_sig_loss = htonl(hst->loss_of_signal_count);
1153 lesb->lesb_prim_err = htonl(hst->prim_seq_protocol_err_count);
1154 lesb->lesb_inv_word = htonl(hst->invalid_tx_word_count);
1155 lesb->lesb_inv_crc = htonl(hst->invalid_crc_count);
1158 sp = lport->tt.seq_start_next(sp);
1159 f_ctl = FC_FC_EX_CTX | FC_FC_LAST_SEQ | FC_FC_END_SEQ;
1160 fc_fill_fc_hdr(fp, FC_RCTL_ELS_REP, ep->did, ep->sid,
1161 FC_TYPE_ELS, f_ctl, 0);
1162 lport->tt.seq_send(lport, sp, fp);
1167 lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
1169 fc_frame_free(rx_fp);
1173 * fc_rport_recv_els_req() - Handler for validated ELS requests
1174 * @lport: The local port that received the ELS request
1175 * @sp: The sequence that the ELS request was on
1176 * @fp: The ELS request frame
1178 * Handle incoming ELS requests that require port login.
1179 * The ELS opcode has already been validated by the caller.
1181 * Locking Note: Called with the lport lock held.
1183 static void fc_rport_recv_els_req(struct fc_lport *lport,
1184 struct fc_seq *sp, struct fc_frame *fp)
1186 struct fc_rport_priv *rdata;
1187 struct fc_frame_header *fh;
1188 struct fc_seq_els_data els_data;
1191 els_data.reason = ELS_RJT_UNAB;
1192 els_data.explan = ELS_EXPL_PLOGI_REQD;
1194 fh = fc_frame_header_get(fp);
1196 mutex_lock(&lport->disc.disc_mutex);
1197 rdata = lport->tt.rport_lookup(lport, ntoh24(fh->fh_s_id));
1199 mutex_unlock(&lport->disc.disc_mutex);
1202 mutex_lock(&rdata->rp_mutex);
1203 mutex_unlock(&lport->disc.disc_mutex);
1205 switch (rdata->rp_state) {
1208 case RPORT_ST_READY:
1209 case RPORT_ST_ADISC:
1212 mutex_unlock(&rdata->rp_mutex);
1216 switch (fc_frame_payload_op(fp)) {
1218 fc_rport_recv_prli_req(rdata, sp, fp);
1221 fc_rport_recv_prlo_req(rdata, sp, fp);
1224 fc_rport_recv_adisc_req(rdata, sp, fp);
1228 lport->tt.seq_els_rsp_send(sp, ELS_RRQ, &els_data);
1232 lport->tt.seq_els_rsp_send(sp, ELS_REC, &els_data);
1235 fc_rport_recv_rls_req(rdata, sp, fp);
1238 fc_frame_free(fp); /* can't happen */
1242 mutex_unlock(&rdata->rp_mutex);
1246 lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &els_data);
1251 * fc_rport_recv_req() - Handler for requests
1252 * @sp: The sequence the request was on
1253 * @fp: The request frame
1254 * @lport: The local port that received the request
1256 * Locking Note: Called with the lport lock held.
1258 void fc_rport_recv_req(struct fc_seq *sp, struct fc_frame *fp,
1259 struct fc_lport *lport)
1261 struct fc_seq_els_data els_data;
1264 * Handle PLOGI and LOGO requests separately, since they
1265 * don't require prior login.
1266 * Check for unsupported opcodes first and reject them.
1267 * For some ops, it would be incorrect to reject with "PLOGI required".
1269 switch (fc_frame_payload_op(fp)) {
1271 fc_rport_recv_plogi_req(lport, sp, fp);
1274 fc_rport_recv_logo_req(lport, sp, fp);
1282 fc_rport_recv_els_req(lport, sp, fp);
1287 els_data.reason = ELS_RJT_UNSUP;
1288 els_data.explan = ELS_EXPL_NONE;
1289 lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &els_data);
1295 * fc_rport_recv_plogi_req() - Handler for Port Login (PLOGI) requests
1296 * @lport: The local port that received the PLOGI request
1297 * @sp: The sequence that the PLOGI request was on
1298 * @rx_fp: The PLOGI request frame
1300 * Locking Note: The rport lock is held before calling this function.
1302 static void fc_rport_recv_plogi_req(struct fc_lport *lport,
1303 struct fc_seq *sp, struct fc_frame *rx_fp)
1305 struct fc_disc *disc;
1306 struct fc_rport_priv *rdata;
1307 struct fc_frame *fp = rx_fp;
1309 struct fc_frame_header *fh;
1310 struct fc_els_flogi *pl;
1311 struct fc_seq_els_data rjt_data;
1315 fh = fc_frame_header_get(fp);
1316 sid = ntoh24(fh->fh_s_id);
1318 FC_RPORT_ID_DBG(lport, sid, "Received PLOGI request\n");
1320 pl = fc_frame_payload_get(fp, sizeof(*pl));
1322 FC_RPORT_ID_DBG(lport, sid, "Received PLOGI too short\n");
1323 rjt_data.reason = ELS_RJT_PROT;
1324 rjt_data.explan = ELS_EXPL_INV_LEN;
1328 disc = &lport->disc;
1329 mutex_lock(&disc->disc_mutex);
1330 rdata = lport->tt.rport_create(lport, sid);
1332 mutex_unlock(&disc->disc_mutex);
1333 rjt_data.reason = ELS_RJT_UNAB;
1334 rjt_data.explan = ELS_EXPL_INSUF_RES;
1338 mutex_lock(&rdata->rp_mutex);
1339 mutex_unlock(&disc->disc_mutex);
1341 rdata->ids.port_name = get_unaligned_be64(&pl->fl_wwpn);
1342 rdata->ids.node_name = get_unaligned_be64(&pl->fl_wwnn);
1345 * If the rport was just created, possibly due to the incoming PLOGI,
1346 * set the state appropriately and accept the PLOGI.
1348 * If we had also sent a PLOGI, and if the received PLOGI is from a
1349 * higher WWPN, we accept it, otherwise an LS_RJT is sent with reason
1350 * "command already in progress".
1352 * XXX TBD: If the session was ready before, the PLOGI should result in
1353 * all outstanding exchanges being reset.
1355 switch (rdata->rp_state) {
1357 FC_RPORT_DBG(rdata, "Received PLOGI in INIT state\n");
1359 case RPORT_ST_PLOGI:
1360 FC_RPORT_DBG(rdata, "Received PLOGI in PLOGI state\n");
1361 if (rdata->ids.port_name < lport->wwpn) {
1362 mutex_unlock(&rdata->rp_mutex);
1363 rjt_data.reason = ELS_RJT_INPROG;
1364 rjt_data.explan = ELS_EXPL_NONE;
1370 case RPORT_ST_READY:
1371 case RPORT_ST_ADISC:
1372 FC_RPORT_DBG(rdata, "Received PLOGI in logged-in state %d "
1373 "- ignored for now\n", rdata->rp_state);
1374 /* XXX TBD - should reset */
1376 case RPORT_ST_DELETE:
1378 case RPORT_ST_RESTART:
1379 FC_RPORT_DBG(rdata, "Received PLOGI in state %s - send busy\n",
1380 fc_rport_state(rdata));
1381 mutex_unlock(&rdata->rp_mutex);
1382 rjt_data.reason = ELS_RJT_BUSY;
1383 rjt_data.explan = ELS_EXPL_NONE;
1388 * Get session payload size from incoming PLOGI.
1390 rdata->maxframe_size = fc_plogi_get_maxframe(pl, lport->mfs);
1391 fc_frame_free(rx_fp);
1394 * Send LS_ACC. If this fails, the originator should retry.
1396 sp = lport->tt.seq_start_next(sp);
1399 fp = fc_frame_alloc(lport, sizeof(*pl));
1403 fc_plogi_fill(lport, fp, ELS_LS_ACC);
1404 f_ctl = FC_FC_EX_CTX | FC_FC_LAST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT;
1405 ep = fc_seq_exch(sp);
1406 fc_fill_fc_hdr(fp, FC_RCTL_ELS_REP, ep->did, ep->sid,
1407 FC_TYPE_ELS, f_ctl, 0);
1408 lport->tt.seq_send(lport, sp, fp);
1409 fc_rport_enter_prli(rdata);
1411 mutex_unlock(&rdata->rp_mutex);
1415 lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
1420 * fc_rport_recv_prli_req() - Handler for process login (PRLI) requests
1421 * @rdata: The remote port that sent the PRLI request
1422 * @sp: The sequence that the PRLI was on
1423 * @rx_fp: The PRLI request frame
1425 * Locking Note: The rport lock is exected to be held before calling
1428 static void fc_rport_recv_prli_req(struct fc_rport_priv *rdata,
1429 struct fc_seq *sp, struct fc_frame *rx_fp)
1431 struct fc_lport *lport = rdata->local_port;
1433 struct fc_frame *fp;
1434 struct fc_frame_header *fh;
1436 struct fc_els_prli prli;
1437 struct fc_els_spp spp;
1439 struct fc_els_spp *rspp; /* request service param page */
1440 struct fc_els_spp *spp; /* response spp */
1443 enum fc_els_rjt_reason reason = ELS_RJT_UNAB;
1444 enum fc_els_rjt_explan explan = ELS_EXPL_NONE;
1445 enum fc_els_spp_resp resp;
1446 struct fc_seq_els_data rjt_data;
1449 u32 roles = FC_RPORT_ROLE_UNKNOWN;
1452 fh = fc_frame_header_get(rx_fp);
1454 FC_RPORT_DBG(rdata, "Received PRLI request while in state %s\n",
1455 fc_rport_state(rdata));
1457 switch (rdata->rp_state) {
1460 case RPORT_ST_READY:
1461 case RPORT_ST_ADISC:
1462 reason = ELS_RJT_NONE;
1465 fc_frame_free(rx_fp);
1469 len = fr_len(rx_fp) - sizeof(*fh);
1470 pp = fc_frame_payload_get(rx_fp, sizeof(*pp));
1472 reason = ELS_RJT_PROT;
1473 explan = ELS_EXPL_INV_LEN;
1475 plen = ntohs(pp->prli.prli_len);
1476 if ((plen % 4) != 0 || plen > len) {
1477 reason = ELS_RJT_PROT;
1478 explan = ELS_EXPL_INV_LEN;
1479 } else if (plen < len) {
1482 plen = pp->prli.prli_spp_len;
1483 if ((plen % 4) != 0 || plen < sizeof(*spp) ||
1484 plen > len || len < sizeof(*pp)) {
1485 reason = ELS_RJT_PROT;
1486 explan = ELS_EXPL_INV_LEN;
1490 if (reason != ELS_RJT_NONE ||
1491 (fp = fc_frame_alloc(lport, len)) == NULL) {
1492 rjt_data.reason = reason;
1493 rjt_data.explan = explan;
1494 lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
1496 sp = lport->tt.seq_start_next(sp);
1498 pp = fc_frame_payload_get(fp, len);
1501 pp->prli.prli_cmd = ELS_LS_ACC;
1502 pp->prli.prli_spp_len = plen;
1503 pp->prli.prli_len = htons(len);
1504 len -= sizeof(struct fc_els_prli);
1506 /* reinitialize remote port roles */
1507 rdata->ids.roles = FC_RPORT_ROLE_UNKNOWN;
1510 * Go through all the service parameter pages and build
1511 * response. If plen indicates longer SPP than standard,
1512 * use that. The entire response has been pre-cleared above.
1515 while (len >= plen) {
1516 spp->spp_type = rspp->spp_type;
1517 spp->spp_type_ext = rspp->spp_type_ext;
1518 spp->spp_flags = rspp->spp_flags & FC_SPP_EST_IMG_PAIR;
1519 resp = FC_SPP_RESP_ACK;
1520 if (rspp->spp_flags & FC_SPP_RPA_VAL)
1521 resp = FC_SPP_RESP_NO_PA;
1522 switch (rspp->spp_type) {
1523 case 0: /* common to all FC-4 types */
1526 fcp_parm = ntohl(rspp->spp_params);
1527 if (fcp_parm & FCP_SPPF_RETRY)
1528 rdata->flags |= FC_RP_FLAGS_RETRY;
1529 rdata->supported_classes = FC_COS_CLASS3;
1530 if (fcp_parm & FCP_SPPF_INIT_FCN)
1531 roles |= FC_RPORT_ROLE_FCP_INITIATOR;
1532 if (fcp_parm & FCP_SPPF_TARG_FCN)
1533 roles |= FC_RPORT_ROLE_FCP_TARGET;
1534 rdata->ids.roles = roles;
1537 htonl(lport->service_params);
1540 resp = FC_SPP_RESP_INVL;
1543 spp->spp_flags |= resp;
1545 rspp = (struct fc_els_spp *)((char *)rspp + plen);
1546 spp = (struct fc_els_spp *)((char *)spp + plen);
1550 * Send LS_ACC. If this fails, the originator should retry.
1552 f_ctl = FC_FC_EX_CTX | FC_FC_LAST_SEQ;
1553 f_ctl |= FC_FC_END_SEQ | FC_FC_SEQ_INIT;
1554 ep = fc_seq_exch(sp);
1555 fc_fill_fc_hdr(fp, FC_RCTL_ELS_REP, ep->did, ep->sid,
1556 FC_TYPE_ELS, f_ctl, 0);
1557 lport->tt.seq_send(lport, sp, fp);
1560 * Get lock and re-check state.
1562 switch (rdata->rp_state) {
1564 fc_rport_enter_ready(rdata);
1566 case RPORT_ST_READY:
1567 case RPORT_ST_ADISC:
1573 fc_frame_free(rx_fp);
1577 * fc_rport_recv_prlo_req() - Handler for process logout (PRLO) requests
1578 * @rdata: The remote port that sent the PRLO request
1579 * @sp: The sequence that the PRLO was on
1580 * @fp: The PRLO request frame
1582 * Locking Note: The rport lock is exected to be held before calling
1585 static void fc_rport_recv_prlo_req(struct fc_rport_priv *rdata,
1587 struct fc_frame *fp)
1589 struct fc_lport *lport = rdata->local_port;
1591 struct fc_frame_header *fh;
1592 struct fc_seq_els_data rjt_data;
1594 fh = fc_frame_header_get(fp);
1596 FC_RPORT_DBG(rdata, "Received PRLO request while in state %s\n",
1597 fc_rport_state(rdata));
1600 rjt_data.reason = ELS_RJT_UNAB;
1601 rjt_data.explan = ELS_EXPL_NONE;
1602 lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
1607 * fc_rport_recv_logo_req() - Handler for logout (LOGO) requests
1608 * @lport: The local port that received the LOGO request
1609 * @sp: The sequence that the LOGO request was on
1610 * @fp: The LOGO request frame
1612 * Locking Note: The rport lock is exected to be held before calling
1615 static void fc_rport_recv_logo_req(struct fc_lport *lport,
1617 struct fc_frame *fp)
1619 struct fc_frame_header *fh;
1620 struct fc_rport_priv *rdata;
1623 lport->tt.seq_els_rsp_send(sp, ELS_LS_ACC, NULL);
1625 fh = fc_frame_header_get(fp);
1626 sid = ntoh24(fh->fh_s_id);
1628 mutex_lock(&lport->disc.disc_mutex);
1629 rdata = lport->tt.rport_lookup(lport, sid);
1631 mutex_lock(&rdata->rp_mutex);
1632 FC_RPORT_DBG(rdata, "Received LOGO request while in state %s\n",
1633 fc_rport_state(rdata));
1635 fc_rport_enter_delete(rdata, RPORT_EV_LOGO);
1638 * If the remote port was created due to discovery, set state
1639 * to log back in. It may have seen a stale RSCN about us.
1642 fc_rport_state_enter(rdata, RPORT_ST_RESTART);
1643 mutex_unlock(&rdata->rp_mutex);
1645 FC_RPORT_ID_DBG(lport, sid,
1646 "Received LOGO from non-logged-in port\n");
1647 mutex_unlock(&lport->disc.disc_mutex);
1652 * fc_rport_flush_queue() - Flush the rport_event_queue
1654 static void fc_rport_flush_queue(void)
1656 flush_workqueue(rport_event_queue);
1660 * fc_rport_init() - Initialize the remote port layer for a local port
1661 * @lport: The local port to initialize the remote port layer for
1663 int fc_rport_init(struct fc_lport *lport)
1665 if (!lport->tt.rport_lookup)
1666 lport->tt.rport_lookup = fc_rport_lookup;
1668 if (!lport->tt.rport_create)
1669 lport->tt.rport_create = fc_rport_create;
1671 if (!lport->tt.rport_login)
1672 lport->tt.rport_login = fc_rport_login;
1674 if (!lport->tt.rport_logoff)
1675 lport->tt.rport_logoff = fc_rport_logoff;
1677 if (!lport->tt.rport_recv_req)
1678 lport->tt.rport_recv_req = fc_rport_recv_req;
1680 if (!lport->tt.rport_flush_queue)
1681 lport->tt.rport_flush_queue = fc_rport_flush_queue;
1683 if (!lport->tt.rport_destroy)
1684 lport->tt.rport_destroy = fc_rport_destroy;
1688 EXPORT_SYMBOL(fc_rport_init);
1691 * fc_setup_rport() - Initialize the rport_event_queue
1693 int fc_setup_rport()
1695 rport_event_queue = create_singlethread_workqueue("fc_rport_eq");
1696 if (!rport_event_queue)
1702 * fc_destroy_rport() - Destroy the rport_event_queue
1704 void fc_destroy_rport()
1706 destroy_workqueue(rport_event_queue);
1710 * fc_rport_terminate_io() - Stop all outstanding I/O on a remote port
1711 * @rport: The remote port whose I/O should be terminated
1713 void fc_rport_terminate_io(struct fc_rport *rport)
1715 struct fc_rport_libfc_priv *rpriv = rport->dd_data;
1716 struct fc_lport *lport = rpriv->local_port;
1718 lport->tt.exch_mgr_reset(lport, 0, rport->port_id);
1719 lport->tt.exch_mgr_reset(lport, rport->port_id, 0);
1721 EXPORT_SYMBOL(fc_rport_terminate_io);