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 hierarchy.
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/slab.h>
51 #include <linux/rcupdate.h>
52 #include <linux/timer.h>
53 #include <linux/workqueue.h>
54 #include <asm/unaligned.h>
56 #include <scsi/libfc.h>
57 #include <scsi/fc_encode.h>
61 static struct workqueue_struct *rport_event_queue;
63 static void fc_rport_enter_flogi(struct fc_rport_priv *);
64 static void fc_rport_enter_plogi(struct fc_rport_priv *);
65 static void fc_rport_enter_prli(struct fc_rport_priv *);
66 static void fc_rport_enter_rtv(struct fc_rport_priv *);
67 static void fc_rport_enter_ready(struct fc_rport_priv *);
68 static void fc_rport_enter_logo(struct fc_rport_priv *);
69 static void fc_rport_enter_adisc(struct fc_rport_priv *);
71 static void fc_rport_recv_plogi_req(struct fc_lport *, struct fc_frame *);
72 static void fc_rport_recv_prli_req(struct fc_rport_priv *, struct fc_frame *);
73 static void fc_rport_recv_prlo_req(struct fc_rport_priv *, struct fc_frame *);
74 static void fc_rport_recv_logo_req(struct fc_lport *, struct fc_frame *);
75 static void fc_rport_timeout(struct work_struct *);
76 static void fc_rport_error(struct fc_rport_priv *, struct fc_frame *);
77 static void fc_rport_error_retry(struct fc_rport_priv *, struct fc_frame *);
78 static void fc_rport_work(struct work_struct *);
80 static const char *fc_rport_state_names[] = {
81 [RPORT_ST_INIT] = "Init",
82 [RPORT_ST_FLOGI] = "FLOGI",
83 [RPORT_ST_PLOGI_WAIT] = "PLOGI_WAIT",
84 [RPORT_ST_PLOGI] = "PLOGI",
85 [RPORT_ST_PRLI] = "PRLI",
86 [RPORT_ST_RTV] = "RTV",
87 [RPORT_ST_READY] = "Ready",
88 [RPORT_ST_ADISC] = "ADISC",
89 [RPORT_ST_DELETE] = "Delete",
93 * fc_rport_lookup() - Lookup a remote port by port_id
94 * @lport: The local port to lookup the remote port on
95 * @port_id: The remote port ID to look up
97 * The caller must hold either disc_mutex or rcu_read_lock().
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_rcu(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) + lport->rport_priv_size, 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 rdata->lld_event_callback = lport->tt.rport_event_callback;
150 list_add_rcu(&rdata->peers, &lport->disc.rports);
156 * fc_rport_free_rcu() - Free a remote port
157 * @rcu: The rcu_head structure inside the remote port
159 static void fc_rport_free_rcu(struct rcu_head *rcu)
161 struct fc_rport_priv *rdata;
163 rdata = container_of(rcu, struct fc_rport_priv, rcu);
168 * fc_rport_destroy() - Free a remote port after last reference is released
169 * @kref: The remote port's kref
171 static void fc_rport_destroy(struct kref *kref)
173 struct fc_rport_priv *rdata;
175 rdata = container_of(kref, struct fc_rport_priv, kref);
176 call_rcu(&rdata->rcu, fc_rport_free_rcu);
180 * fc_rport_state() - Return a string identifying the remote port's state
181 * @rdata: The remote port
183 static const char *fc_rport_state(struct fc_rport_priv *rdata)
187 cp = fc_rport_state_names[rdata->rp_state];
194 * fc_set_rport_loss_tmo() - Set the remote port loss timeout
195 * @rport: The remote port that gets a new timeout value
196 * @timeout: The new timeout value (in seconds)
198 void fc_set_rport_loss_tmo(struct fc_rport *rport, u32 timeout)
201 rport->dev_loss_tmo = timeout;
203 rport->dev_loss_tmo = 1;
205 EXPORT_SYMBOL(fc_set_rport_loss_tmo);
208 * fc_plogi_get_maxframe() - Get the maximum payload from the common service
209 * parameters in a FLOGI frame
210 * @flp: The FLOGI or PLOGI payload
211 * @maxval: The maximum frame size upper limit; this may be less than what
212 * is in the service parameters
214 static unsigned int fc_plogi_get_maxframe(struct fc_els_flogi *flp,
220 * Get max payload from the common service parameters and the
221 * class 3 receive data field size.
223 mfs = ntohs(flp->fl_csp.sp_bb_data) & FC_SP_BB_DATA_MASK;
224 if (mfs >= FC_SP_MIN_MAX_PAYLOAD && mfs < maxval)
226 mfs = ntohs(flp->fl_cssp[3 - 1].cp_rdfs);
227 if (mfs >= FC_SP_MIN_MAX_PAYLOAD && mfs < maxval)
233 * fc_rport_state_enter() - Change the state of a remote port
234 * @rdata: The remote port whose state should change
235 * @new: The new state
237 * Locking Note: Called with the rport lock held
239 static void fc_rport_state_enter(struct fc_rport_priv *rdata,
240 enum fc_rport_state new)
242 if (rdata->rp_state != new)
244 rdata->rp_state = new;
248 * fc_rport_work() - Handler for remote port events in the rport_event_queue
249 * @work: Handle to the remote port being dequeued
251 static void fc_rport_work(struct work_struct *work)
254 struct fc_rport_priv *rdata =
255 container_of(work, struct fc_rport_priv, event_work);
256 struct fc_rport_libfc_priv *rpriv;
257 enum fc_rport_event event;
258 struct fc_lport *lport = rdata->local_port;
259 struct fc_rport_operations *rport_ops;
260 struct fc_rport_identifiers ids;
261 struct fc_rport *rport;
262 struct fc4_prov *prov;
265 mutex_lock(&rdata->rp_mutex);
266 event = rdata->event;
267 rport_ops = rdata->ops;
268 rport = rdata->rport;
270 FC_RPORT_DBG(rdata, "work event %u\n", event);
275 rdata->event = RPORT_EV_NONE;
276 rdata->major_retries = 0;
277 kref_get(&rdata->kref);
278 mutex_unlock(&rdata->rp_mutex);
281 rport = fc_remote_port_add(lport->host, 0, &ids);
283 FC_RPORT_DBG(rdata, "Failed to add the rport\n");
284 lport->tt.rport_logoff(rdata);
285 kref_put(&rdata->kref, lport->tt.rport_destroy);
288 mutex_lock(&rdata->rp_mutex);
290 FC_RPORT_DBG(rdata, "rport already allocated\n");
291 rdata->rport = rport;
292 rport->maxframe_size = rdata->maxframe_size;
293 rport->supported_classes = rdata->supported_classes;
295 rpriv = rport->dd_data;
296 rpriv->local_port = lport;
297 rpriv->rp_state = rdata->rp_state;
298 rpriv->flags = rdata->flags;
299 rpriv->e_d_tov = rdata->e_d_tov;
300 rpriv->r_a_tov = rdata->r_a_tov;
301 mutex_unlock(&rdata->rp_mutex);
303 if (rport_ops && rport_ops->event_callback) {
304 FC_RPORT_DBG(rdata, "callback ev %d\n", event);
305 rport_ops->event_callback(lport, rdata, event);
307 if (rdata->lld_event_callback) {
308 FC_RPORT_DBG(rdata, "lld callback ev %d\n", event);
309 rdata->lld_event_callback(lport, rdata, event);
311 kref_put(&rdata->kref, lport->tt.rport_destroy);
314 case RPORT_EV_FAILED:
317 if (rdata->prli_count) {
318 mutex_lock(&fc_prov_mutex);
319 for (type = 1; type < FC_FC4_PROV_SIZE; type++) {
320 prov = fc_passive_prov[type];
321 if (prov && prov->prlo)
324 mutex_unlock(&fc_prov_mutex);
326 port_id = rdata->ids.port_id;
327 mutex_unlock(&rdata->rp_mutex);
329 if (rport_ops && rport_ops->event_callback) {
330 FC_RPORT_DBG(rdata, "callback ev %d\n", event);
331 rport_ops->event_callback(lport, rdata, event);
333 if (rdata->lld_event_callback) {
334 FC_RPORT_DBG(rdata, "lld callback ev %d\n", event);
335 rdata->lld_event_callback(lport, rdata, event);
337 cancel_delayed_work_sync(&rdata->retry_work);
340 * Reset any outstanding exchanges before freeing rport.
342 lport->tt.exch_mgr_reset(lport, 0, port_id);
343 lport->tt.exch_mgr_reset(lport, port_id, 0);
346 rpriv = rport->dd_data;
347 rpriv->rp_state = RPORT_ST_DELETE;
348 mutex_lock(&rdata->rp_mutex);
350 mutex_unlock(&rdata->rp_mutex);
351 fc_remote_port_delete(rport);
354 mutex_lock(&lport->disc.disc_mutex);
355 mutex_lock(&rdata->rp_mutex);
356 if (rdata->rp_state == RPORT_ST_DELETE) {
357 if (port_id == FC_FID_DIR_SERV) {
358 rdata->event = RPORT_EV_NONE;
359 mutex_unlock(&rdata->rp_mutex);
360 kref_put(&rdata->kref, lport->tt.rport_destroy);
361 } else if ((rdata->flags & FC_RP_STARTED) &&
362 rdata->major_retries <
363 lport->max_rport_retry_count) {
364 rdata->major_retries++;
365 rdata->event = RPORT_EV_NONE;
366 FC_RPORT_DBG(rdata, "work restart\n");
367 fc_rport_enter_flogi(rdata);
368 mutex_unlock(&rdata->rp_mutex);
370 FC_RPORT_DBG(rdata, "work delete\n");
371 list_del_rcu(&rdata->peers);
372 mutex_unlock(&rdata->rp_mutex);
373 kref_put(&rdata->kref, lport->tt.rport_destroy);
377 * Re-open for events. Reissue READY event if ready.
379 rdata->event = RPORT_EV_NONE;
380 if (rdata->rp_state == RPORT_ST_READY)
381 fc_rport_enter_ready(rdata);
382 mutex_unlock(&rdata->rp_mutex);
384 mutex_unlock(&lport->disc.disc_mutex);
388 mutex_unlock(&rdata->rp_mutex);
394 * fc_rport_login() - Start the remote port login state machine
395 * @rdata: The remote port to be logged in to
397 * Locking Note: Called without the rport lock held. This
398 * function will hold the rport lock, call an _enter_*
399 * function and then unlock the rport.
401 * This indicates the intent to be logged into the remote port.
402 * If it appears we are already logged in, ADISC is used to verify
405 int fc_rport_login(struct fc_rport_priv *rdata)
407 mutex_lock(&rdata->rp_mutex);
409 rdata->flags |= FC_RP_STARTED;
410 switch (rdata->rp_state) {
412 FC_RPORT_DBG(rdata, "ADISC port\n");
413 fc_rport_enter_adisc(rdata);
415 case RPORT_ST_DELETE:
416 FC_RPORT_DBG(rdata, "Restart deleted port\n");
419 FC_RPORT_DBG(rdata, "Login to port\n");
420 fc_rport_enter_flogi(rdata);
423 mutex_unlock(&rdata->rp_mutex);
429 * fc_rport_enter_delete() - Schedule a remote port to be deleted
430 * @rdata: The remote port to be deleted
431 * @event: The event to report as the reason for deletion
433 * Locking Note: Called with the rport lock held.
435 * Allow state change into DELETE only once.
437 * Call queue_work only if there's no event already pending.
438 * Set the new event so that the old pending event will not occur.
439 * Since we have the mutex, even if fc_rport_work() is already started,
440 * it'll see the new event.
442 static void fc_rport_enter_delete(struct fc_rport_priv *rdata,
443 enum fc_rport_event event)
445 if (rdata->rp_state == RPORT_ST_DELETE)
448 FC_RPORT_DBG(rdata, "Delete port\n");
450 fc_rport_state_enter(rdata, RPORT_ST_DELETE);
452 if (rdata->event == RPORT_EV_NONE)
453 queue_work(rport_event_queue, &rdata->event_work);
454 rdata->event = event;
458 * fc_rport_logoff() - Logoff and remove a remote port
459 * @rdata: The remote port to be logged off of
461 * Locking Note: Called without the rport lock held. This
462 * function will hold the rport lock, call an _enter_*
463 * function and then unlock the rport.
465 int fc_rport_logoff(struct fc_rport_priv *rdata)
467 mutex_lock(&rdata->rp_mutex);
469 FC_RPORT_DBG(rdata, "Remove port\n");
471 rdata->flags &= ~FC_RP_STARTED;
472 if (rdata->rp_state == RPORT_ST_DELETE) {
473 FC_RPORT_DBG(rdata, "Port in Delete state, not removing\n");
476 fc_rport_enter_logo(rdata);
479 * Change the state to Delete so that we discard
482 fc_rport_enter_delete(rdata, RPORT_EV_STOP);
484 mutex_unlock(&rdata->rp_mutex);
489 * fc_rport_enter_ready() - Transition to the RPORT_ST_READY state
490 * @rdata: The remote port that is ready
492 * Locking Note: The rport lock is expected to be held before calling
495 static void fc_rport_enter_ready(struct fc_rport_priv *rdata)
497 fc_rport_state_enter(rdata, RPORT_ST_READY);
499 FC_RPORT_DBG(rdata, "Port is Ready\n");
501 if (rdata->event == RPORT_EV_NONE)
502 queue_work(rport_event_queue, &rdata->event_work);
503 rdata->event = RPORT_EV_READY;
507 * fc_rport_timeout() - Handler for the retry_work timer
508 * @work: Handle to the remote port that has timed out
510 * Locking Note: Called without the rport lock held. This
511 * function will hold the rport lock, call an _enter_*
512 * function and then unlock the rport.
514 static void fc_rport_timeout(struct work_struct *work)
516 struct fc_rport_priv *rdata =
517 container_of(work, struct fc_rport_priv, retry_work.work);
519 mutex_lock(&rdata->rp_mutex);
521 switch (rdata->rp_state) {
523 fc_rport_enter_flogi(rdata);
526 fc_rport_enter_plogi(rdata);
529 fc_rport_enter_prli(rdata);
532 fc_rport_enter_rtv(rdata);
535 fc_rport_enter_adisc(rdata);
537 case RPORT_ST_PLOGI_WAIT:
540 case RPORT_ST_DELETE:
544 mutex_unlock(&rdata->rp_mutex);
548 * fc_rport_error() - Error handler, called once retries have been exhausted
549 * @rdata: The remote port the error is happened on
550 * @fp: The error code encapsulated in a frame pointer
552 * Locking Note: The rport lock is expected to be held before
553 * calling this routine
555 static void fc_rport_error(struct fc_rport_priv *rdata, struct fc_frame *fp)
557 FC_RPORT_DBG(rdata, "Error %ld in state %s, retries %d\n",
558 IS_ERR(fp) ? -PTR_ERR(fp) : 0,
559 fc_rport_state(rdata), rdata->retries);
561 switch (rdata->rp_state) {
564 rdata->flags &= ~FC_RP_STARTED;
565 fc_rport_enter_delete(rdata, RPORT_EV_FAILED);
568 fc_rport_enter_ready(rdata);
572 fc_rport_enter_logo(rdata);
574 case RPORT_ST_PLOGI_WAIT:
575 case RPORT_ST_DELETE:
583 * fc_rport_error_retry() - Handler for remote port state retries
584 * @rdata: The remote port whose state is to be retried
585 * @fp: The error code encapsulated in a frame pointer
587 * If the error was an exchange timeout retry immediately,
588 * otherwise wait for E_D_TOV.
590 * Locking Note: The rport lock is expected to be held before
591 * calling this routine
593 static void fc_rport_error_retry(struct fc_rport_priv *rdata,
596 unsigned long delay = FC_DEF_E_D_TOV;
598 /* make sure this isn't an FC_EX_CLOSED error, never retry those */
599 if (PTR_ERR(fp) == -FC_EX_CLOSED)
602 if (rdata->retries < rdata->local_port->max_rport_retry_count) {
603 FC_RPORT_DBG(rdata, "Error %ld in state %s, retrying\n",
604 PTR_ERR(fp), fc_rport_state(rdata));
606 /* no additional delay on exchange timeouts */
607 if (PTR_ERR(fp) == -FC_EX_TIMEOUT)
609 schedule_delayed_work(&rdata->retry_work, delay);
614 fc_rport_error(rdata, fp);
618 * fc_rport_login_complete() - Handle parameters and completion of p-mp login.
619 * @rdata: The remote port which we logged into or which logged into us.
620 * @fp: The FLOGI or PLOGI request or response frame
622 * Returns non-zero error if a problem is detected with the frame.
623 * Does not free the frame.
625 * This is only used in point-to-multipoint mode for FIP currently.
627 static int fc_rport_login_complete(struct fc_rport_priv *rdata,
630 struct fc_lport *lport = rdata->local_port;
631 struct fc_els_flogi *flogi;
632 unsigned int e_d_tov;
635 flogi = fc_frame_payload_get(fp, sizeof(*flogi));
639 csp_flags = ntohs(flogi->fl_csp.sp_features);
641 if (fc_frame_payload_op(fp) == ELS_FLOGI) {
642 if (csp_flags & FC_SP_FT_FPORT) {
643 FC_RPORT_DBG(rdata, "Fabric bit set in FLOGI\n");
649 * E_D_TOV is not valid on an incoming FLOGI request.
651 e_d_tov = ntohl(flogi->fl_csp.sp_e_d_tov);
652 if (csp_flags & FC_SP_FT_EDTR)
654 if (e_d_tov > rdata->e_d_tov)
655 rdata->e_d_tov = e_d_tov;
657 rdata->maxframe_size = fc_plogi_get_maxframe(flogi, lport->mfs);
662 * fc_rport_flogi_resp() - Handle response to FLOGI request for p-mp mode
663 * @sp: The sequence that the FLOGI was on
664 * @fp: The FLOGI response frame
665 * @rp_arg: The remote port that received the FLOGI response
667 void fc_rport_flogi_resp(struct fc_seq *sp, struct fc_frame *fp,
670 struct fc_rport_priv *rdata = rp_arg;
671 struct fc_lport *lport = rdata->local_port;
672 struct fc_els_flogi *flogi;
673 unsigned int r_a_tov;
675 FC_RPORT_DBG(rdata, "Received a FLOGI %s\n", fc_els_resp_type(fp));
677 if (fp == ERR_PTR(-FC_EX_CLOSED))
680 mutex_lock(&rdata->rp_mutex);
682 if (rdata->rp_state != RPORT_ST_FLOGI) {
683 FC_RPORT_DBG(rdata, "Received a FLOGI response, but in state "
684 "%s\n", fc_rport_state(rdata));
691 fc_rport_error(rdata, fp);
695 if (fc_frame_payload_op(fp) != ELS_LS_ACC)
697 if (fc_rport_login_complete(rdata, fp))
700 flogi = fc_frame_payload_get(fp, sizeof(*flogi));
703 r_a_tov = ntohl(flogi->fl_csp.sp_r_a_tov);
704 if (r_a_tov > rdata->r_a_tov)
705 rdata->r_a_tov = r_a_tov;
707 if (rdata->ids.port_name < lport->wwpn)
708 fc_rport_enter_plogi(rdata);
710 fc_rport_state_enter(rdata, RPORT_ST_PLOGI_WAIT);
714 mutex_unlock(&rdata->rp_mutex);
716 kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
719 FC_RPORT_DBG(rdata, "Bad FLOGI response\n");
720 fc_rport_error_retry(rdata, fp);
725 * fc_rport_enter_flogi() - Send a FLOGI request to the remote port for p-mp
726 * @rdata: The remote port to send a FLOGI to
728 * Locking Note: The rport lock is expected to be held before calling
731 static void fc_rport_enter_flogi(struct fc_rport_priv *rdata)
733 struct fc_lport *lport = rdata->local_port;
736 if (!lport->point_to_multipoint)
737 return fc_rport_enter_plogi(rdata);
739 FC_RPORT_DBG(rdata, "Entered FLOGI state from %s state\n",
740 fc_rport_state(rdata));
742 fc_rport_state_enter(rdata, RPORT_ST_FLOGI);
744 fp = fc_frame_alloc(lport, sizeof(struct fc_els_flogi));
746 return fc_rport_error_retry(rdata, fp);
748 if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_FLOGI,
749 fc_rport_flogi_resp, rdata,
751 fc_rport_error_retry(rdata, NULL);
753 kref_get(&rdata->kref);
757 * fc_rport_recv_flogi_req() - Handle Fabric Login (FLOGI) request in p-mp mode
758 * @lport: The local port that received the PLOGI request
759 * @rx_fp: The PLOGI request frame
761 static void fc_rport_recv_flogi_req(struct fc_lport *lport,
762 struct fc_frame *rx_fp)
764 struct fc_disc *disc;
765 struct fc_els_flogi *flp;
766 struct fc_rport_priv *rdata;
767 struct fc_frame *fp = rx_fp;
768 struct fc_seq_els_data rjt_data;
771 sid = fc_frame_sid(fp);
773 FC_RPORT_ID_DBG(lport, sid, "Received FLOGI request\n");
776 mutex_lock(&disc->disc_mutex);
778 if (!lport->point_to_multipoint) {
779 rjt_data.reason = ELS_RJT_UNSUP;
780 rjt_data.explan = ELS_EXPL_NONE;
784 flp = fc_frame_payload_get(fp, sizeof(*flp));
786 rjt_data.reason = ELS_RJT_LOGIC;
787 rjt_data.explan = ELS_EXPL_INV_LEN;
791 rdata = lport->tt.rport_lookup(lport, sid);
793 rjt_data.reason = ELS_RJT_FIP;
794 rjt_data.explan = ELS_EXPL_NOT_NEIGHBOR;
797 mutex_lock(&rdata->rp_mutex);
799 FC_RPORT_DBG(rdata, "Received FLOGI in %s state\n",
800 fc_rport_state(rdata));
802 switch (rdata->rp_state) {
805 * If received the FLOGI request on RPORT which is INIT state
806 * (means not transition to FLOGI either fc_rport timeout
807 * function didn;t trigger or this end hasn;t received
808 * beacon yet from other end. In that case only, allow RPORT
809 * state machine to continue, otherwise fall through which
810 * causes the code to send reject response.
811 * NOTE; Not checking for FIP->state such as VNMP_UP or
812 * VNMP_CLAIM because if FIP state is not one of those,
813 * RPORT wouldn;t have created and 'rport_lookup' would have
814 * failed anyway in that case.
816 if (lport->point_to_multipoint)
818 case RPORT_ST_DELETE:
819 mutex_unlock(&rdata->rp_mutex);
820 rjt_data.reason = ELS_RJT_FIP;
821 rjt_data.explan = ELS_EXPL_NOT_NEIGHBOR;
824 case RPORT_ST_PLOGI_WAIT:
832 * Set the remote port to be deleted and to then restart.
833 * This queues work to be sure exchanges are reset.
835 fc_rport_enter_delete(rdata, RPORT_EV_LOGO);
836 mutex_unlock(&rdata->rp_mutex);
837 rjt_data.reason = ELS_RJT_BUSY;
838 rjt_data.explan = ELS_EXPL_NONE;
841 if (fc_rport_login_complete(rdata, fp)) {
842 mutex_unlock(&rdata->rp_mutex);
843 rjt_data.reason = ELS_RJT_LOGIC;
844 rjt_data.explan = ELS_EXPL_NONE;
848 fp = fc_frame_alloc(lport, sizeof(*flp));
852 fc_flogi_fill(lport, fp);
853 flp = fc_frame_payload_get(fp, sizeof(*flp));
854 flp->fl_cmd = ELS_LS_ACC;
856 fc_fill_reply_hdr(fp, rx_fp, FC_RCTL_ELS_REP, 0);
857 lport->tt.frame_send(lport, fp);
859 if (rdata->ids.port_name < lport->wwpn)
860 fc_rport_enter_plogi(rdata);
862 fc_rport_state_enter(rdata, RPORT_ST_PLOGI_WAIT);
864 mutex_unlock(&rdata->rp_mutex);
865 mutex_unlock(&disc->disc_mutex);
866 fc_frame_free(rx_fp);
870 mutex_unlock(&disc->disc_mutex);
871 lport->tt.seq_els_rsp_send(rx_fp, ELS_LS_RJT, &rjt_data);
872 fc_frame_free(rx_fp);
876 * fc_rport_plogi_resp() - Handler for ELS PLOGI responses
877 * @sp: The sequence the PLOGI is on
878 * @fp: The PLOGI response frame
879 * @rdata_arg: The remote port that sent the PLOGI response
881 * Locking Note: This function will be called without the rport lock
882 * held, but it will lock, call an _enter_* function or fc_rport_error
883 * and then unlock the rport.
885 static void fc_rport_plogi_resp(struct fc_seq *sp, struct fc_frame *fp,
888 struct fc_rport_priv *rdata = rdata_arg;
889 struct fc_lport *lport = rdata->local_port;
890 struct fc_els_flogi *plp = NULL;
895 mutex_lock(&rdata->rp_mutex);
897 FC_RPORT_DBG(rdata, "Received a PLOGI %s\n", fc_els_resp_type(fp));
899 if (rdata->rp_state != RPORT_ST_PLOGI) {
900 FC_RPORT_DBG(rdata, "Received a PLOGI response, but in state "
901 "%s\n", fc_rport_state(rdata));
908 fc_rport_error_retry(rdata, fp);
912 op = fc_frame_payload_op(fp);
913 if (op == ELS_LS_ACC &&
914 (plp = fc_frame_payload_get(fp, sizeof(*plp))) != NULL) {
915 rdata->ids.port_name = get_unaligned_be64(&plp->fl_wwpn);
916 rdata->ids.node_name = get_unaligned_be64(&plp->fl_wwnn);
918 /* save plogi response sp_features for further reference */
919 rdata->sp_features = ntohs(plp->fl_csp.sp_features);
921 if (lport->point_to_multipoint)
922 fc_rport_login_complete(rdata, fp);
923 csp_seq = ntohs(plp->fl_csp.sp_tot_seq);
924 cssp_seq = ntohs(plp->fl_cssp[3 - 1].cp_con_seq);
925 if (cssp_seq < csp_seq)
927 rdata->max_seq = csp_seq;
928 rdata->maxframe_size = fc_plogi_get_maxframe(plp, lport->mfs);
929 fc_rport_enter_prli(rdata);
931 fc_rport_error_retry(rdata, fp);
936 mutex_unlock(&rdata->rp_mutex);
937 kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
941 * fc_rport_enter_plogi() - Send Port Login (PLOGI) request
942 * @rdata: The remote port to send a PLOGI to
944 * Locking Note: The rport lock is expected to be held before calling
947 static void fc_rport_enter_plogi(struct fc_rport_priv *rdata)
949 struct fc_lport *lport = rdata->local_port;
952 FC_RPORT_DBG(rdata, "Port entered PLOGI state from %s state\n",
953 fc_rport_state(rdata));
955 fc_rport_state_enter(rdata, RPORT_ST_PLOGI);
957 rdata->maxframe_size = FC_MIN_MAX_PAYLOAD;
958 fp = fc_frame_alloc(lport, sizeof(struct fc_els_flogi));
960 FC_RPORT_DBG(rdata, "%s frame alloc failed\n", __func__);
961 fc_rport_error_retry(rdata, fp);
964 rdata->e_d_tov = lport->e_d_tov;
966 if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_PLOGI,
967 fc_rport_plogi_resp, rdata,
969 fc_rport_error_retry(rdata, NULL);
971 kref_get(&rdata->kref);
975 * fc_rport_prli_resp() - Process Login (PRLI) response handler
976 * @sp: The sequence the PRLI response was on
977 * @fp: The PRLI response frame
978 * @rdata_arg: The remote port that sent the PRLI response
980 * Locking Note: This function will be called without the rport lock
981 * held, but it will lock, call an _enter_* function or fc_rport_error
982 * and then unlock the rport.
984 static void fc_rport_prli_resp(struct fc_seq *sp, struct fc_frame *fp,
987 struct fc_rport_priv *rdata = rdata_arg;
989 struct fc_els_prli prli;
990 struct fc_els_spp spp;
992 struct fc_els_spp temp_spp;
993 struct fc4_prov *prov;
994 u32 roles = FC_RPORT_ROLE_UNKNOWN;
999 mutex_lock(&rdata->rp_mutex);
1001 FC_RPORT_DBG(rdata, "Received a PRLI %s\n", fc_els_resp_type(fp));
1003 if (rdata->rp_state != RPORT_ST_PRLI) {
1004 FC_RPORT_DBG(rdata, "Received a PRLI response, but in state "
1005 "%s\n", fc_rport_state(rdata));
1012 fc_rport_error_retry(rdata, fp);
1016 /* reinitialize remote port roles */
1017 rdata->ids.roles = FC_RPORT_ROLE_UNKNOWN;
1019 op = fc_frame_payload_op(fp);
1020 if (op == ELS_LS_ACC) {
1021 pp = fc_frame_payload_get(fp, sizeof(*pp));
1025 resp_code = (pp->spp.spp_flags & FC_SPP_RESP_MASK);
1026 FC_RPORT_DBG(rdata, "PRLI spp_flags = 0x%x\n",
1028 rdata->spp_type = pp->spp.spp_type;
1029 if (resp_code != FC_SPP_RESP_ACK) {
1030 if (resp_code == FC_SPP_RESP_CONF)
1031 fc_rport_error(rdata, fp);
1033 fc_rport_error_retry(rdata, fp);
1036 if (pp->prli.prli_spp_len < sizeof(pp->spp))
1039 fcp_parm = ntohl(pp->spp.spp_params);
1040 if (fcp_parm & FCP_SPPF_RETRY)
1041 rdata->flags |= FC_RP_FLAGS_RETRY;
1042 if (fcp_parm & FCP_SPPF_CONF_COMPL)
1043 rdata->flags |= FC_RP_FLAGS_CONF_REQ;
1045 prov = fc_passive_prov[FC_TYPE_FCP];
1047 memset(&temp_spp, 0, sizeof(temp_spp));
1048 prov->prli(rdata, pp->prli.prli_spp_len,
1049 &pp->spp, &temp_spp);
1052 rdata->supported_classes = FC_COS_CLASS3;
1053 if (fcp_parm & FCP_SPPF_INIT_FCN)
1054 roles |= FC_RPORT_ROLE_FCP_INITIATOR;
1055 if (fcp_parm & FCP_SPPF_TARG_FCN)
1056 roles |= FC_RPORT_ROLE_FCP_TARGET;
1058 rdata->ids.roles = roles;
1059 fc_rport_enter_rtv(rdata);
1062 FC_RPORT_DBG(rdata, "Bad ELS response for PRLI command\n");
1063 fc_rport_error_retry(rdata, fp);
1069 mutex_unlock(&rdata->rp_mutex);
1070 kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
1074 * fc_rport_enter_prli() - Send Process Login (PRLI) request
1075 * @rdata: The remote port to send the PRLI request to
1077 * Locking Note: The rport lock is expected to be held before calling
1080 static void fc_rport_enter_prli(struct fc_rport_priv *rdata)
1082 struct fc_lport *lport = rdata->local_port;
1084 struct fc_els_prli prli;
1085 struct fc_els_spp spp;
1087 struct fc_frame *fp;
1088 struct fc4_prov *prov;
1091 * If the rport is one of the well known addresses
1092 * we skip PRLI and RTV and go straight to READY.
1094 if (rdata->ids.port_id >= FC_FID_DOM_MGR) {
1095 fc_rport_enter_ready(rdata);
1099 FC_RPORT_DBG(rdata, "Port entered PRLI state from %s state\n",
1100 fc_rport_state(rdata));
1102 fc_rport_state_enter(rdata, RPORT_ST_PRLI);
1104 fp = fc_frame_alloc(lport, sizeof(*pp));
1106 fc_rport_error_retry(rdata, fp);
1110 fc_prli_fill(lport, fp);
1112 prov = fc_passive_prov[FC_TYPE_FCP];
1114 pp = fc_frame_payload_get(fp, sizeof(*pp));
1115 prov->prli(rdata, sizeof(pp->spp), NULL, &pp->spp);
1118 fc_fill_fc_hdr(fp, FC_RCTL_ELS_REQ, rdata->ids.port_id,
1119 fc_host_port_id(lport->host), FC_TYPE_ELS,
1120 FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT, 0);
1122 if (!lport->tt.exch_seq_send(lport, fp, fc_rport_prli_resp,
1123 NULL, rdata, 2 * lport->r_a_tov))
1124 fc_rport_error_retry(rdata, NULL);
1126 kref_get(&rdata->kref);
1130 * fc_rport_els_rtv_resp() - Handler for Request Timeout Value (RTV) responses
1131 * @sp: The sequence the RTV was on
1132 * @fp: The RTV response frame
1133 * @rdata_arg: The remote port that sent the RTV response
1135 * Many targets don't seem to support this.
1137 * Locking Note: This function will be called without the rport lock
1138 * held, but it will lock, call an _enter_* function or fc_rport_error
1139 * and then unlock the rport.
1141 static void fc_rport_rtv_resp(struct fc_seq *sp, struct fc_frame *fp,
1144 struct fc_rport_priv *rdata = rdata_arg;
1147 mutex_lock(&rdata->rp_mutex);
1149 FC_RPORT_DBG(rdata, "Received a RTV %s\n", fc_els_resp_type(fp));
1151 if (rdata->rp_state != RPORT_ST_RTV) {
1152 FC_RPORT_DBG(rdata, "Received a RTV response, but in state "
1153 "%s\n", fc_rport_state(rdata));
1160 fc_rport_error(rdata, fp);
1164 op = fc_frame_payload_op(fp);
1165 if (op == ELS_LS_ACC) {
1166 struct fc_els_rtv_acc *rtv;
1170 rtv = fc_frame_payload_get(fp, sizeof(*rtv));
1172 toq = ntohl(rtv->rtv_toq);
1173 tov = ntohl(rtv->rtv_r_a_tov);
1176 rdata->r_a_tov = tov;
1177 tov = ntohl(rtv->rtv_e_d_tov);
1178 if (toq & FC_ELS_RTV_EDRES)
1182 rdata->e_d_tov = tov;
1186 fc_rport_enter_ready(rdata);
1191 mutex_unlock(&rdata->rp_mutex);
1192 kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
1196 * fc_rport_enter_rtv() - Send Request Timeout Value (RTV) request
1197 * @rdata: The remote port to send the RTV request to
1199 * Locking Note: The rport lock is expected to be held before calling
1202 static void fc_rport_enter_rtv(struct fc_rport_priv *rdata)
1204 struct fc_frame *fp;
1205 struct fc_lport *lport = rdata->local_port;
1207 FC_RPORT_DBG(rdata, "Port entered RTV state from %s state\n",
1208 fc_rport_state(rdata));
1210 fc_rport_state_enter(rdata, RPORT_ST_RTV);
1212 fp = fc_frame_alloc(lport, sizeof(struct fc_els_rtv));
1214 fc_rport_error_retry(rdata, fp);
1218 if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_RTV,
1219 fc_rport_rtv_resp, rdata,
1220 2 * lport->r_a_tov))
1221 fc_rport_error_retry(rdata, NULL);
1223 kref_get(&rdata->kref);
1227 * fc_rport_logo_resp() - Handler for logout (LOGO) responses
1228 * @sp: The sequence the LOGO was on
1229 * @fp: The LOGO response frame
1230 * @lport_arg: The local port
1232 static void fc_rport_logo_resp(struct fc_seq *sp, struct fc_frame *fp,
1235 struct fc_lport *lport = lport_arg;
1237 FC_RPORT_ID_DBG(lport, fc_seq_exch(sp)->did,
1238 "Received a LOGO %s\n", fc_els_resp_type(fp));
1245 * fc_rport_enter_logo() - Send a logout (LOGO) request
1246 * @rdata: The remote port to send the LOGO request to
1248 * Locking Note: The rport lock is expected to be held before calling
1251 static void fc_rport_enter_logo(struct fc_rport_priv *rdata)
1253 struct fc_lport *lport = rdata->local_port;
1254 struct fc_frame *fp;
1256 FC_RPORT_DBG(rdata, "Port sending LOGO from %s state\n",
1257 fc_rport_state(rdata));
1259 fp = fc_frame_alloc(lport, sizeof(struct fc_els_logo));
1262 (void)lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_LOGO,
1263 fc_rport_logo_resp, lport, 0);
1267 * fc_rport_els_adisc_resp() - Handler for Address Discovery (ADISC) responses
1268 * @sp: The sequence the ADISC response was on
1269 * @fp: The ADISC response frame
1270 * @rdata_arg: The remote port that sent the ADISC response
1272 * Locking Note: This function will be called without the rport lock
1273 * held, but it will lock, call an _enter_* function or fc_rport_error
1274 * and then unlock the rport.
1276 static void fc_rport_adisc_resp(struct fc_seq *sp, struct fc_frame *fp,
1279 struct fc_rport_priv *rdata = rdata_arg;
1280 struct fc_els_adisc *adisc;
1283 mutex_lock(&rdata->rp_mutex);
1285 FC_RPORT_DBG(rdata, "Received a ADISC response\n");
1287 if (rdata->rp_state != RPORT_ST_ADISC) {
1288 FC_RPORT_DBG(rdata, "Received a ADISC resp but in state %s\n",
1289 fc_rport_state(rdata));
1296 fc_rport_error(rdata, fp);
1301 * If address verification failed. Consider us logged out of the rport.
1302 * Since the rport is still in discovery, we want to be
1303 * logged in, so go to PLOGI state. Otherwise, go back to READY.
1305 op = fc_frame_payload_op(fp);
1306 adisc = fc_frame_payload_get(fp, sizeof(*adisc));
1307 if (op != ELS_LS_ACC || !adisc ||
1308 ntoh24(adisc->adisc_port_id) != rdata->ids.port_id ||
1309 get_unaligned_be64(&adisc->adisc_wwpn) != rdata->ids.port_name ||
1310 get_unaligned_be64(&adisc->adisc_wwnn) != rdata->ids.node_name) {
1311 FC_RPORT_DBG(rdata, "ADISC error or mismatch\n");
1312 fc_rport_enter_flogi(rdata);
1314 FC_RPORT_DBG(rdata, "ADISC OK\n");
1315 fc_rport_enter_ready(rdata);
1320 mutex_unlock(&rdata->rp_mutex);
1321 kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
1325 * fc_rport_enter_adisc() - Send Address Discover (ADISC) request
1326 * @rdata: The remote port to send the ADISC request to
1328 * Locking Note: The rport lock is expected to be held before calling
1331 static void fc_rport_enter_adisc(struct fc_rport_priv *rdata)
1333 struct fc_lport *lport = rdata->local_port;
1334 struct fc_frame *fp;
1336 FC_RPORT_DBG(rdata, "sending ADISC from %s state\n",
1337 fc_rport_state(rdata));
1339 fc_rport_state_enter(rdata, RPORT_ST_ADISC);
1341 fp = fc_frame_alloc(lport, sizeof(struct fc_els_adisc));
1343 fc_rport_error_retry(rdata, fp);
1346 if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_ADISC,
1347 fc_rport_adisc_resp, rdata,
1348 2 * lport->r_a_tov))
1349 fc_rport_error_retry(rdata, NULL);
1351 kref_get(&rdata->kref);
1355 * fc_rport_recv_adisc_req() - Handler for Address Discovery (ADISC) requests
1356 * @rdata: The remote port that sent the ADISC request
1357 * @in_fp: The ADISC request frame
1359 * Locking Note: Called with the lport and rport locks held.
1361 static void fc_rport_recv_adisc_req(struct fc_rport_priv *rdata,
1362 struct fc_frame *in_fp)
1364 struct fc_lport *lport = rdata->local_port;
1365 struct fc_frame *fp;
1366 struct fc_els_adisc *adisc;
1367 struct fc_seq_els_data rjt_data;
1369 FC_RPORT_DBG(rdata, "Received ADISC request\n");
1371 adisc = fc_frame_payload_get(in_fp, sizeof(*adisc));
1373 rjt_data.reason = ELS_RJT_PROT;
1374 rjt_data.explan = ELS_EXPL_INV_LEN;
1375 lport->tt.seq_els_rsp_send(in_fp, ELS_LS_RJT, &rjt_data);
1379 fp = fc_frame_alloc(lport, sizeof(*adisc));
1382 fc_adisc_fill(lport, fp);
1383 adisc = fc_frame_payload_get(fp, sizeof(*adisc));
1384 adisc->adisc_cmd = ELS_LS_ACC;
1385 fc_fill_reply_hdr(fp, in_fp, FC_RCTL_ELS_REP, 0);
1386 lport->tt.frame_send(lport, fp);
1388 fc_frame_free(in_fp);
1392 * fc_rport_recv_rls_req() - Handle received Read Link Status request
1393 * @rdata: The remote port that sent the RLS request
1394 * @rx_fp: The PRLI request frame
1396 * Locking Note: The rport lock is expected to be held before calling
1399 static void fc_rport_recv_rls_req(struct fc_rport_priv *rdata,
1400 struct fc_frame *rx_fp)
1403 struct fc_lport *lport = rdata->local_port;
1404 struct fc_frame *fp;
1405 struct fc_els_rls *rls;
1406 struct fc_els_rls_resp *rsp;
1407 struct fc_els_lesb *lesb;
1408 struct fc_seq_els_data rjt_data;
1409 struct fc_host_statistics *hst;
1411 FC_RPORT_DBG(rdata, "Received RLS request while in state %s\n",
1412 fc_rport_state(rdata));
1414 rls = fc_frame_payload_get(rx_fp, sizeof(*rls));
1416 rjt_data.reason = ELS_RJT_PROT;
1417 rjt_data.explan = ELS_EXPL_INV_LEN;
1421 fp = fc_frame_alloc(lport, sizeof(*rsp));
1423 rjt_data.reason = ELS_RJT_UNAB;
1424 rjt_data.explan = ELS_EXPL_INSUF_RES;
1428 rsp = fc_frame_payload_get(fp, sizeof(*rsp));
1429 memset(rsp, 0, sizeof(*rsp));
1430 rsp->rls_cmd = ELS_LS_ACC;
1431 lesb = &rsp->rls_lesb;
1432 if (lport->tt.get_lesb) {
1433 /* get LESB from LLD if it supports it */
1434 lport->tt.get_lesb(lport, lesb);
1436 fc_get_host_stats(lport->host);
1437 hst = &lport->host_stats;
1438 lesb->lesb_link_fail = htonl(hst->link_failure_count);
1439 lesb->lesb_sync_loss = htonl(hst->loss_of_sync_count);
1440 lesb->lesb_sig_loss = htonl(hst->loss_of_signal_count);
1441 lesb->lesb_prim_err = htonl(hst->prim_seq_protocol_err_count);
1442 lesb->lesb_inv_word = htonl(hst->invalid_tx_word_count);
1443 lesb->lesb_inv_crc = htonl(hst->invalid_crc_count);
1446 fc_fill_reply_hdr(fp, rx_fp, FC_RCTL_ELS_REP, 0);
1447 lport->tt.frame_send(lport, fp);
1451 lport->tt.seq_els_rsp_send(rx_fp, ELS_LS_RJT, &rjt_data);
1453 fc_frame_free(rx_fp);
1457 * fc_rport_recv_els_req() - Handler for validated ELS requests
1458 * @lport: The local port that received the ELS request
1459 * @fp: The ELS request frame
1461 * Handle incoming ELS requests that require port login.
1462 * The ELS opcode has already been validated by the caller.
1464 * Locking Note: Called with the lport lock held.
1466 static void fc_rport_recv_els_req(struct fc_lport *lport, struct fc_frame *fp)
1468 struct fc_rport_priv *rdata;
1469 struct fc_seq_els_data els_data;
1471 mutex_lock(&lport->disc.disc_mutex);
1472 rdata = lport->tt.rport_lookup(lport, fc_frame_sid(fp));
1474 mutex_unlock(&lport->disc.disc_mutex);
1477 mutex_lock(&rdata->rp_mutex);
1478 mutex_unlock(&lport->disc.disc_mutex);
1480 switch (rdata->rp_state) {
1483 case RPORT_ST_READY:
1484 case RPORT_ST_ADISC:
1487 mutex_unlock(&rdata->rp_mutex);
1491 switch (fc_frame_payload_op(fp)) {
1493 fc_rport_recv_prli_req(rdata, fp);
1496 fc_rport_recv_prlo_req(rdata, fp);
1499 fc_rport_recv_adisc_req(rdata, fp);
1502 lport->tt.seq_els_rsp_send(fp, ELS_RRQ, NULL);
1506 lport->tt.seq_els_rsp_send(fp, ELS_REC, NULL);
1510 fc_rport_recv_rls_req(rdata, fp);
1513 fc_frame_free(fp); /* can't happen */
1517 mutex_unlock(&rdata->rp_mutex);
1521 els_data.reason = ELS_RJT_UNAB;
1522 els_data.explan = ELS_EXPL_PLOGI_REQD;
1523 lport->tt.seq_els_rsp_send(fp, ELS_LS_RJT, &els_data);
1528 * fc_rport_recv_req() - Handler for requests
1529 * @lport: The local port that received the request
1530 * @fp: The request frame
1532 * Locking Note: Called with the lport lock held.
1534 void fc_rport_recv_req(struct fc_lport *lport, struct fc_frame *fp)
1536 struct fc_seq_els_data els_data;
1539 * Handle FLOGI, PLOGI and LOGO requests separately, since they
1540 * don't require prior login.
1541 * Check for unsupported opcodes first and reject them.
1542 * For some ops, it would be incorrect to reject with "PLOGI required".
1544 switch (fc_frame_payload_op(fp)) {
1546 fc_rport_recv_flogi_req(lport, fp);
1549 fc_rport_recv_plogi_req(lport, fp);
1552 fc_rport_recv_logo_req(lport, fp);
1560 fc_rport_recv_els_req(lport, fp);
1563 els_data.reason = ELS_RJT_UNSUP;
1564 els_data.explan = ELS_EXPL_NONE;
1565 lport->tt.seq_els_rsp_send(fp, ELS_LS_RJT, &els_data);
1572 * fc_rport_recv_plogi_req() - Handler for Port Login (PLOGI) requests
1573 * @lport: The local port that received the PLOGI request
1574 * @rx_fp: The PLOGI request frame
1576 * Locking Note: The rport lock is held before calling this function.
1578 static void fc_rport_recv_plogi_req(struct fc_lport *lport,
1579 struct fc_frame *rx_fp)
1581 struct fc_disc *disc;
1582 struct fc_rport_priv *rdata;
1583 struct fc_frame *fp = rx_fp;
1584 struct fc_els_flogi *pl;
1585 struct fc_seq_els_data rjt_data;
1588 sid = fc_frame_sid(fp);
1590 FC_RPORT_ID_DBG(lport, sid, "Received PLOGI request\n");
1592 pl = fc_frame_payload_get(fp, sizeof(*pl));
1594 FC_RPORT_ID_DBG(lport, sid, "Received PLOGI too short\n");
1595 rjt_data.reason = ELS_RJT_PROT;
1596 rjt_data.explan = ELS_EXPL_INV_LEN;
1600 disc = &lport->disc;
1601 mutex_lock(&disc->disc_mutex);
1602 rdata = lport->tt.rport_create(lport, sid);
1604 mutex_unlock(&disc->disc_mutex);
1605 rjt_data.reason = ELS_RJT_UNAB;
1606 rjt_data.explan = ELS_EXPL_INSUF_RES;
1610 mutex_lock(&rdata->rp_mutex);
1611 mutex_unlock(&disc->disc_mutex);
1613 rdata->ids.port_name = get_unaligned_be64(&pl->fl_wwpn);
1614 rdata->ids.node_name = get_unaligned_be64(&pl->fl_wwnn);
1617 * If the rport was just created, possibly due to the incoming PLOGI,
1618 * set the state appropriately and accept the PLOGI.
1620 * If we had also sent a PLOGI, and if the received PLOGI is from a
1621 * higher WWPN, we accept it, otherwise an LS_RJT is sent with reason
1622 * "command already in progress".
1624 * XXX TBD: If the session was ready before, the PLOGI should result in
1625 * all outstanding exchanges being reset.
1627 switch (rdata->rp_state) {
1629 FC_RPORT_DBG(rdata, "Received PLOGI in INIT state\n");
1631 case RPORT_ST_PLOGI_WAIT:
1632 FC_RPORT_DBG(rdata, "Received PLOGI in PLOGI_WAIT state\n");
1634 case RPORT_ST_PLOGI:
1635 FC_RPORT_DBG(rdata, "Received PLOGI in PLOGI state\n");
1636 if (rdata->ids.port_name < lport->wwpn) {
1637 mutex_unlock(&rdata->rp_mutex);
1638 rjt_data.reason = ELS_RJT_INPROG;
1639 rjt_data.explan = ELS_EXPL_NONE;
1645 case RPORT_ST_READY:
1646 case RPORT_ST_ADISC:
1647 FC_RPORT_DBG(rdata, "Received PLOGI in logged-in state %d "
1648 "- ignored for now\n", rdata->rp_state);
1649 /* XXX TBD - should reset */
1651 case RPORT_ST_FLOGI:
1652 case RPORT_ST_DELETE:
1653 FC_RPORT_DBG(rdata, "Received PLOGI in state %s - send busy\n",
1654 fc_rport_state(rdata));
1655 mutex_unlock(&rdata->rp_mutex);
1656 rjt_data.reason = ELS_RJT_BUSY;
1657 rjt_data.explan = ELS_EXPL_NONE;
1662 * Get session payload size from incoming PLOGI.
1664 rdata->maxframe_size = fc_plogi_get_maxframe(pl, lport->mfs);
1667 * Send LS_ACC. If this fails, the originator should retry.
1669 fp = fc_frame_alloc(lport, sizeof(*pl));
1673 fc_plogi_fill(lport, fp, ELS_LS_ACC);
1674 fc_fill_reply_hdr(fp, rx_fp, FC_RCTL_ELS_REP, 0);
1675 lport->tt.frame_send(lport, fp);
1676 fc_rport_enter_prli(rdata);
1678 mutex_unlock(&rdata->rp_mutex);
1679 fc_frame_free(rx_fp);
1683 lport->tt.seq_els_rsp_send(fp, ELS_LS_RJT, &rjt_data);
1688 * fc_rport_recv_prli_req() - Handler for process login (PRLI) requests
1689 * @rdata: The remote port that sent the PRLI request
1690 * @rx_fp: The PRLI request frame
1692 * Locking Note: The rport lock is exected to be held before calling
1695 static void fc_rport_recv_prli_req(struct fc_rport_priv *rdata,
1696 struct fc_frame *rx_fp)
1698 struct fc_lport *lport = rdata->local_port;
1699 struct fc_frame *fp;
1701 struct fc_els_prli prli;
1702 struct fc_els_spp spp;
1704 struct fc_els_spp *rspp; /* request service param page */
1705 struct fc_els_spp *spp; /* response spp */
1708 enum fc_els_spp_resp resp;
1709 enum fc_els_spp_resp passive;
1710 struct fc_seq_els_data rjt_data;
1711 struct fc4_prov *prov;
1713 FC_RPORT_DBG(rdata, "Received PRLI request while in state %s\n",
1714 fc_rport_state(rdata));
1716 len = fr_len(rx_fp) - sizeof(struct fc_frame_header);
1717 pp = fc_frame_payload_get(rx_fp, sizeof(*pp));
1720 plen = ntohs(pp->prli.prli_len);
1721 if ((plen % 4) != 0 || plen > len || plen < 16)
1725 plen = pp->prli.prli_spp_len;
1726 if ((plen % 4) != 0 || plen < sizeof(*spp) ||
1727 plen > len || len < sizeof(*pp) || plen < 12)
1731 fp = fc_frame_alloc(lport, len);
1733 rjt_data.reason = ELS_RJT_UNAB;
1734 rjt_data.explan = ELS_EXPL_INSUF_RES;
1737 pp = fc_frame_payload_get(fp, len);
1740 pp->prli.prli_cmd = ELS_LS_ACC;
1741 pp->prli.prli_spp_len = plen;
1742 pp->prli.prli_len = htons(len);
1743 len -= sizeof(struct fc_els_prli);
1746 * Go through all the service parameter pages and build
1747 * response. If plen indicates longer SPP than standard,
1748 * use that. The entire response has been pre-cleared above.
1751 mutex_lock(&fc_prov_mutex);
1752 while (len >= plen) {
1753 rdata->spp_type = rspp->spp_type;
1754 spp->spp_type = rspp->spp_type;
1755 spp->spp_type_ext = rspp->spp_type_ext;
1758 if (rspp->spp_type < FC_FC4_PROV_SIZE) {
1759 prov = fc_active_prov[rspp->spp_type];
1761 resp = prov->prli(rdata, plen, rspp, spp);
1762 prov = fc_passive_prov[rspp->spp_type];
1764 passive = prov->prli(rdata, plen, rspp, spp);
1765 if (!resp || passive == FC_SPP_RESP_ACK)
1770 if (spp->spp_flags & FC_SPP_EST_IMG_PAIR)
1771 resp |= FC_SPP_RESP_CONF;
1773 resp |= FC_SPP_RESP_INVL;
1775 spp->spp_flags |= resp;
1777 rspp = (struct fc_els_spp *)((char *)rspp + plen);
1778 spp = (struct fc_els_spp *)((char *)spp + plen);
1780 mutex_unlock(&fc_prov_mutex);
1783 * Send LS_ACC. If this fails, the originator should retry.
1785 fc_fill_reply_hdr(fp, rx_fp, FC_RCTL_ELS_REP, 0);
1786 lport->tt.frame_send(lport, fp);
1788 switch (rdata->rp_state) {
1790 fc_rport_enter_ready(rdata);
1798 rjt_data.reason = ELS_RJT_PROT;
1799 rjt_data.explan = ELS_EXPL_INV_LEN;
1801 lport->tt.seq_els_rsp_send(rx_fp, ELS_LS_RJT, &rjt_data);
1803 fc_frame_free(rx_fp);
1807 * fc_rport_recv_prlo_req() - Handler for process logout (PRLO) requests
1808 * @rdata: The remote port that sent the PRLO request
1809 * @rx_fp: The PRLO request frame
1811 * Locking Note: The rport lock is exected to be held before calling
1814 static void fc_rport_recv_prlo_req(struct fc_rport_priv *rdata,
1815 struct fc_frame *rx_fp)
1817 struct fc_lport *lport = rdata->local_port;
1818 struct fc_frame *fp;
1820 struct fc_els_prlo prlo;
1821 struct fc_els_spp spp;
1823 struct fc_els_spp *rspp; /* request service param page */
1824 struct fc_els_spp *spp; /* response spp */
1827 struct fc_seq_els_data rjt_data;
1829 FC_RPORT_DBG(rdata, "Received PRLO request while in state %s\n",
1830 fc_rport_state(rdata));
1832 len = fr_len(rx_fp) - sizeof(struct fc_frame_header);
1833 pp = fc_frame_payload_get(rx_fp, sizeof(*pp));
1836 plen = ntohs(pp->prlo.prlo_len);
1844 fp = fc_frame_alloc(lport, len);
1846 rjt_data.reason = ELS_RJT_UNAB;
1847 rjt_data.explan = ELS_EXPL_INSUF_RES;
1851 pp = fc_frame_payload_get(fp, len);
1854 pp->prlo.prlo_cmd = ELS_LS_ACC;
1855 pp->prlo.prlo_obs = 0x10;
1856 pp->prlo.prlo_len = htons(len);
1858 spp->spp_type = rspp->spp_type;
1859 spp->spp_type_ext = rspp->spp_type_ext;
1860 spp->spp_flags = FC_SPP_RESP_ACK;
1862 fc_rport_enter_delete(rdata, RPORT_EV_LOGO);
1864 fc_fill_reply_hdr(fp, rx_fp, FC_RCTL_ELS_REP, 0);
1865 lport->tt.frame_send(lport, fp);
1869 rjt_data.reason = ELS_RJT_PROT;
1870 rjt_data.explan = ELS_EXPL_INV_LEN;
1872 lport->tt.seq_els_rsp_send(rx_fp, ELS_LS_RJT, &rjt_data);
1874 fc_frame_free(rx_fp);
1878 * fc_rport_recv_logo_req() - Handler for logout (LOGO) requests
1879 * @lport: The local port that received the LOGO request
1880 * @fp: The LOGO request frame
1882 * Locking Note: The rport lock is exected to be held before calling
1885 static void fc_rport_recv_logo_req(struct fc_lport *lport, struct fc_frame *fp)
1887 struct fc_rport_priv *rdata;
1890 lport->tt.seq_els_rsp_send(fp, ELS_LS_ACC, NULL);
1892 sid = fc_frame_sid(fp);
1894 mutex_lock(&lport->disc.disc_mutex);
1895 rdata = lport->tt.rport_lookup(lport, sid);
1897 mutex_lock(&rdata->rp_mutex);
1898 FC_RPORT_DBG(rdata, "Received LOGO request while in state %s\n",
1899 fc_rport_state(rdata));
1901 fc_rport_enter_delete(rdata, RPORT_EV_LOGO);
1902 mutex_unlock(&rdata->rp_mutex);
1904 FC_RPORT_ID_DBG(lport, sid,
1905 "Received LOGO from non-logged-in port\n");
1906 mutex_unlock(&lport->disc.disc_mutex);
1911 * fc_rport_flush_queue() - Flush the rport_event_queue
1913 static void fc_rport_flush_queue(void)
1915 flush_workqueue(rport_event_queue);
1919 * fc_rport_init() - Initialize the remote port layer for a local port
1920 * @lport: The local port to initialize the remote port layer for
1922 int fc_rport_init(struct fc_lport *lport)
1924 if (!lport->tt.rport_lookup)
1925 lport->tt.rport_lookup = fc_rport_lookup;
1927 if (!lport->tt.rport_create)
1928 lport->tt.rport_create = fc_rport_create;
1930 if (!lport->tt.rport_login)
1931 lport->tt.rport_login = fc_rport_login;
1933 if (!lport->tt.rport_logoff)
1934 lport->tt.rport_logoff = fc_rport_logoff;
1936 if (!lport->tt.rport_recv_req)
1937 lport->tt.rport_recv_req = fc_rport_recv_req;
1939 if (!lport->tt.rport_flush_queue)
1940 lport->tt.rport_flush_queue = fc_rport_flush_queue;
1942 if (!lport->tt.rport_destroy)
1943 lport->tt.rport_destroy = fc_rport_destroy;
1947 EXPORT_SYMBOL(fc_rport_init);
1950 * fc_rport_fcp_prli() - Handle incoming PRLI for the FCP initiator.
1951 * @rdata: remote port private
1952 * @spp_len: service parameter page length
1953 * @rspp: received service parameter page
1954 * @spp: response service parameter page
1956 * Returns the value for the response code to be placed in spp_flags;
1957 * Returns 0 if not an initiator.
1959 static int fc_rport_fcp_prli(struct fc_rport_priv *rdata, u32 spp_len,
1960 const struct fc_els_spp *rspp,
1961 struct fc_els_spp *spp)
1963 struct fc_lport *lport = rdata->local_port;
1966 fcp_parm = ntohl(rspp->spp_params);
1967 rdata->ids.roles = FC_RPORT_ROLE_UNKNOWN;
1968 if (fcp_parm & FCP_SPPF_INIT_FCN)
1969 rdata->ids.roles |= FC_RPORT_ROLE_FCP_INITIATOR;
1970 if (fcp_parm & FCP_SPPF_TARG_FCN)
1971 rdata->ids.roles |= FC_RPORT_ROLE_FCP_TARGET;
1972 if (fcp_parm & FCP_SPPF_RETRY)
1973 rdata->flags |= FC_RP_FLAGS_RETRY;
1974 rdata->supported_classes = FC_COS_CLASS3;
1976 if (!(lport->service_params & FC_RPORT_ROLE_FCP_INITIATOR))
1979 spp->spp_flags |= rspp->spp_flags & FC_SPP_EST_IMG_PAIR;
1982 * OR in our service parameters with other providers (target), if any.
1984 fcp_parm = ntohl(spp->spp_params);
1985 spp->spp_params = htonl(fcp_parm | lport->service_params);
1986 return FC_SPP_RESP_ACK;
1990 * FC-4 provider ops for FCP initiator.
1992 struct fc4_prov fc_rport_fcp_init = {
1993 .prli = fc_rport_fcp_prli,
1997 * fc_rport_t0_prli() - Handle incoming PRLI parameters for type 0
1998 * @rdata: remote port private
1999 * @spp_len: service parameter page length
2000 * @rspp: received service parameter page
2001 * @spp: response service parameter page
2003 static int fc_rport_t0_prli(struct fc_rport_priv *rdata, u32 spp_len,
2004 const struct fc_els_spp *rspp,
2005 struct fc_els_spp *spp)
2007 if (rspp->spp_flags & FC_SPP_EST_IMG_PAIR)
2008 return FC_SPP_RESP_INVL;
2009 return FC_SPP_RESP_ACK;
2013 * FC-4 provider ops for type 0 service parameters.
2015 * This handles the special case of type 0 which is always successful
2016 * but doesn't do anything otherwise.
2018 struct fc4_prov fc_rport_t0_prov = {
2019 .prli = fc_rport_t0_prli,
2023 * fc_setup_rport() - Initialize the rport_event_queue
2025 int fc_setup_rport(void)
2027 rport_event_queue = create_singlethread_workqueue("fc_rport_eq");
2028 if (!rport_event_queue)
2034 * fc_destroy_rport() - Destroy the rport_event_queue
2036 void fc_destroy_rport(void)
2038 destroy_workqueue(rport_event_queue);
2042 * fc_rport_terminate_io() - Stop all outstanding I/O on a remote port
2043 * @rport: The remote port whose I/O should be terminated
2045 void fc_rport_terminate_io(struct fc_rport *rport)
2047 struct fc_rport_libfc_priv *rpriv = rport->dd_data;
2048 struct fc_lport *lport = rpriv->local_port;
2050 lport->tt.exch_mgr_reset(lport, 0, rport->port_id);
2051 lport->tt.exch_mgr_reset(lport, rport->port_id, 0);
2053 EXPORT_SYMBOL(fc_rport_terminate_io);