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 <linux/export.h>
55 #include <asm/unaligned.h>
57 #include <scsi/libfc.h>
58 #include <scsi/fc_encode.h>
62 static struct workqueue_struct *rport_event_queue;
64 static void fc_rport_enter_flogi(struct fc_rport_priv *);
65 static void fc_rport_enter_plogi(struct fc_rport_priv *);
66 static void fc_rport_enter_prli(struct fc_rport_priv *);
67 static void fc_rport_enter_rtv(struct fc_rport_priv *);
68 static void fc_rport_enter_ready(struct fc_rport_priv *);
69 static void fc_rport_enter_logo(struct fc_rport_priv *);
70 static void fc_rport_enter_adisc(struct fc_rport_priv *);
72 static void fc_rport_recv_plogi_req(struct fc_lport *, struct fc_frame *);
73 static void fc_rport_recv_prli_req(struct fc_rport_priv *, struct fc_frame *);
74 static void fc_rport_recv_prlo_req(struct fc_rport_priv *, struct fc_frame *);
75 static void fc_rport_recv_logo_req(struct fc_lport *, struct fc_frame *);
76 static void fc_rport_timeout(struct work_struct *);
77 static void fc_rport_error(struct fc_rport_priv *, struct fc_frame *);
78 static void fc_rport_error_retry(struct fc_rport_priv *, struct fc_frame *);
79 static void fc_rport_work(struct work_struct *);
81 static const char *fc_rport_state_names[] = {
82 [RPORT_ST_INIT] = "Init",
83 [RPORT_ST_FLOGI] = "FLOGI",
84 [RPORT_ST_PLOGI_WAIT] = "PLOGI_WAIT",
85 [RPORT_ST_PLOGI] = "PLOGI",
86 [RPORT_ST_PRLI] = "PRLI",
87 [RPORT_ST_RTV] = "RTV",
88 [RPORT_ST_READY] = "Ready",
89 [RPORT_ST_ADISC] = "ADISC",
90 [RPORT_ST_DELETE] = "Delete",
94 * fc_rport_lookup() - Lookup a remote port by port_id
95 * @lport: The local port to lookup the remote port on
96 * @port_id: The remote port ID to look up
98 * The reference count of the fc_rport_priv structure is
101 static struct fc_rport_priv *fc_rport_lookup(const struct fc_lport *lport,
104 struct fc_rport_priv *rdata = NULL, *tmp_rdata;
107 list_for_each_entry_rcu(tmp_rdata, &lport->disc.rports, peers)
108 if (tmp_rdata->ids.port_id == port_id &&
109 kref_get_unless_zero(&tmp_rdata->kref)) {
118 * fc_rport_create() - Create a new remote port
119 * @lport: The local port this remote port will be associated with
120 * @ids: The identifiers for the new remote port
122 * The remote port will start in the INIT state.
124 * Locking note: must be called with the disc_mutex held.
126 static struct fc_rport_priv *fc_rport_create(struct fc_lport *lport,
129 struct fc_rport_priv *rdata;
130 size_t rport_priv_size = sizeof(*rdata);
132 rdata = lport->tt.rport_lookup(lport, port_id);
136 if (lport->rport_priv_size > 0)
137 rport_priv_size = lport->rport_priv_size;
138 rdata = kzalloc(rport_priv_size, GFP_KERNEL);
142 rdata->ids.node_name = -1;
143 rdata->ids.port_name = -1;
144 rdata->ids.port_id = port_id;
145 rdata->ids.roles = FC_RPORT_ROLE_UNKNOWN;
147 kref_init(&rdata->kref);
148 mutex_init(&rdata->rp_mutex);
149 rdata->local_port = lport;
150 rdata->rp_state = RPORT_ST_INIT;
151 rdata->event = RPORT_EV_NONE;
152 rdata->flags = FC_RP_FLAGS_REC_SUPPORTED;
153 rdata->e_d_tov = lport->e_d_tov;
154 rdata->r_a_tov = lport->r_a_tov;
155 rdata->maxframe_size = FC_MIN_MAX_PAYLOAD;
156 INIT_DELAYED_WORK(&rdata->retry_work, fc_rport_timeout);
157 INIT_WORK(&rdata->event_work, fc_rport_work);
158 if (port_id != FC_FID_DIR_SERV) {
159 rdata->lld_event_callback = lport->tt.rport_event_callback;
160 list_add_rcu(&rdata->peers, &lport->disc.rports);
166 * fc_rport_destroy() - Free a remote port after last reference is released
167 * @kref: The remote port's kref
169 static void fc_rport_destroy(struct kref *kref)
171 struct fc_rport_priv *rdata;
173 rdata = container_of(kref, struct fc_rport_priv, kref);
174 kfree_rcu(rdata, rcu);
178 * fc_rport_state() - Return a string identifying the remote port's state
179 * @rdata: The remote port
181 static const char *fc_rport_state(struct fc_rport_priv *rdata)
185 cp = fc_rport_state_names[rdata->rp_state];
192 * fc_set_rport_loss_tmo() - Set the remote port loss timeout
193 * @rport: The remote port that gets a new timeout value
194 * @timeout: The new timeout value (in seconds)
196 void fc_set_rport_loss_tmo(struct fc_rport *rport, u32 timeout)
199 rport->dev_loss_tmo = timeout;
201 rport->dev_loss_tmo = 1;
203 EXPORT_SYMBOL(fc_set_rport_loss_tmo);
206 * fc_plogi_get_maxframe() - Get the maximum payload from the common service
207 * parameters in a FLOGI frame
208 * @flp: The FLOGI or PLOGI payload
209 * @maxval: The maximum frame size upper limit; this may be less than what
210 * is in the service parameters
212 static unsigned int fc_plogi_get_maxframe(struct fc_els_flogi *flp,
218 * Get max payload from the common service parameters and the
219 * class 3 receive data field size.
221 mfs = ntohs(flp->fl_csp.sp_bb_data) & FC_SP_BB_DATA_MASK;
222 if (mfs >= FC_SP_MIN_MAX_PAYLOAD && mfs < maxval)
224 mfs = ntohs(flp->fl_cssp[3 - 1].cp_rdfs);
225 if (mfs >= FC_SP_MIN_MAX_PAYLOAD && mfs < maxval)
231 * fc_rport_state_enter() - Change the state of a remote port
232 * @rdata: The remote port whose state should change
233 * @new: The new state
235 * Locking Note: Called with the rport lock held
237 static void fc_rport_state_enter(struct fc_rport_priv *rdata,
238 enum fc_rport_state new)
240 if (rdata->rp_state != new)
242 rdata->rp_state = new;
246 * fc_rport_work() - Handler for remote port events in the rport_event_queue
247 * @work: Handle to the remote port being dequeued
249 static void fc_rport_work(struct work_struct *work)
252 struct fc_rport_priv *rdata =
253 container_of(work, struct fc_rport_priv, event_work);
254 struct fc_rport_libfc_priv *rpriv;
255 enum fc_rport_event event;
256 struct fc_lport *lport = rdata->local_port;
257 struct fc_rport_operations *rport_ops;
258 struct fc_rport_identifiers ids;
259 struct fc_rport *rport;
260 struct fc4_prov *prov;
263 mutex_lock(&rdata->rp_mutex);
264 event = rdata->event;
265 rport_ops = rdata->ops;
266 rport = rdata->rport;
268 FC_RPORT_DBG(rdata, "work event %u\n", event);
273 rdata->event = RPORT_EV_NONE;
274 rdata->major_retries = 0;
275 kref_get(&rdata->kref);
276 mutex_unlock(&rdata->rp_mutex);
279 rport = fc_remote_port_add(lport->host, 0, &ids);
281 FC_RPORT_DBG(rdata, "Failed to add the rport\n");
282 lport->tt.rport_logoff(rdata);
283 kref_put(&rdata->kref, lport->tt.rport_destroy);
286 mutex_lock(&rdata->rp_mutex);
288 FC_RPORT_DBG(rdata, "rport already allocated\n");
289 rdata->rport = rport;
290 rport->maxframe_size = rdata->maxframe_size;
291 rport->supported_classes = rdata->supported_classes;
293 rpriv = rport->dd_data;
294 rpriv->local_port = lport;
295 rpriv->rp_state = rdata->rp_state;
296 rpriv->flags = rdata->flags;
297 rpriv->e_d_tov = rdata->e_d_tov;
298 rpriv->r_a_tov = rdata->r_a_tov;
299 mutex_unlock(&rdata->rp_mutex);
301 if (rport_ops && rport_ops->event_callback) {
302 FC_RPORT_DBG(rdata, "callback ev %d\n", event);
303 rport_ops->event_callback(lport, rdata, event);
305 if (rdata->lld_event_callback) {
306 FC_RPORT_DBG(rdata, "lld callback ev %d\n", event);
307 rdata->lld_event_callback(lport, rdata, event);
309 kref_put(&rdata->kref, lport->tt.rport_destroy);
312 case RPORT_EV_FAILED:
315 if (rdata->prli_count) {
316 mutex_lock(&fc_prov_mutex);
317 for (type = 1; type < FC_FC4_PROV_SIZE; type++) {
318 prov = fc_passive_prov[type];
319 if (prov && prov->prlo)
322 mutex_unlock(&fc_prov_mutex);
324 port_id = rdata->ids.port_id;
325 mutex_unlock(&rdata->rp_mutex);
327 if (rport_ops && rport_ops->event_callback) {
328 FC_RPORT_DBG(rdata, "callback ev %d\n", event);
329 rport_ops->event_callback(lport, rdata, event);
331 if (rdata->lld_event_callback) {
332 FC_RPORT_DBG(rdata, "lld callback ev %d\n", event);
333 rdata->lld_event_callback(lport, rdata, event);
335 cancel_delayed_work_sync(&rdata->retry_work);
338 * Reset any outstanding exchanges before freeing rport.
340 lport->tt.exch_mgr_reset(lport, 0, port_id);
341 lport->tt.exch_mgr_reset(lport, port_id, 0);
344 rpriv = rport->dd_data;
345 rpriv->rp_state = RPORT_ST_DELETE;
346 mutex_lock(&rdata->rp_mutex);
348 mutex_unlock(&rdata->rp_mutex);
349 fc_remote_port_delete(rport);
352 mutex_lock(&rdata->rp_mutex);
353 if (rdata->rp_state == RPORT_ST_DELETE) {
354 if (port_id == FC_FID_DIR_SERV) {
355 rdata->event = RPORT_EV_NONE;
356 mutex_unlock(&rdata->rp_mutex);
357 kref_put(&rdata->kref, lport->tt.rport_destroy);
358 } else if ((rdata->flags & FC_RP_STARTED) &&
359 rdata->major_retries <
360 lport->max_rport_retry_count) {
361 rdata->major_retries++;
362 rdata->event = RPORT_EV_NONE;
363 FC_RPORT_DBG(rdata, "work restart\n");
364 fc_rport_enter_flogi(rdata);
365 mutex_unlock(&rdata->rp_mutex);
367 FC_RPORT_DBG(rdata, "work delete\n");
368 list_del_rcu(&rdata->peers);
369 mutex_unlock(&rdata->rp_mutex);
370 kref_put(&rdata->kref, lport->tt.rport_destroy);
374 * Re-open for events. Reissue READY event if ready.
376 rdata->event = RPORT_EV_NONE;
377 if (rdata->rp_state == RPORT_ST_READY)
378 fc_rport_enter_ready(rdata);
379 mutex_unlock(&rdata->rp_mutex);
384 mutex_unlock(&rdata->rp_mutex);
390 * fc_rport_login() - Start the remote port login state machine
391 * @rdata: The remote port to be logged in to
393 * Locking Note: Called without the rport lock held. This
394 * function will hold the rport lock, call an _enter_*
395 * function and then unlock the rport.
397 * This indicates the intent to be logged into the remote port.
398 * If it appears we are already logged in, ADISC is used to verify
401 static int fc_rport_login(struct fc_rport_priv *rdata)
403 mutex_lock(&rdata->rp_mutex);
405 rdata->flags |= FC_RP_STARTED;
406 switch (rdata->rp_state) {
408 FC_RPORT_DBG(rdata, "ADISC port\n");
409 fc_rport_enter_adisc(rdata);
411 case RPORT_ST_DELETE:
412 FC_RPORT_DBG(rdata, "Restart deleted port\n");
415 FC_RPORT_DBG(rdata, "Login to port\n");
416 fc_rport_enter_flogi(rdata);
419 mutex_unlock(&rdata->rp_mutex);
425 * fc_rport_enter_delete() - Schedule a remote port to be deleted
426 * @rdata: The remote port to be deleted
427 * @event: The event to report as the reason for deletion
429 * Locking Note: Called with the rport lock held.
431 * Allow state change into DELETE only once.
433 * Call queue_work only if there's no event already pending.
434 * Set the new event so that the old pending event will not occur.
435 * Since we have the mutex, even if fc_rport_work() is already started,
436 * it'll see the new event.
438 static void fc_rport_enter_delete(struct fc_rport_priv *rdata,
439 enum fc_rport_event event)
441 if (rdata->rp_state == RPORT_ST_DELETE)
444 FC_RPORT_DBG(rdata, "Delete port\n");
446 fc_rport_state_enter(rdata, RPORT_ST_DELETE);
448 if (rdata->event == RPORT_EV_NONE)
449 queue_work(rport_event_queue, &rdata->event_work);
450 rdata->event = event;
454 * fc_rport_logoff() - Logoff and remove a remote port
455 * @rdata: The remote port to be logged off of
457 * Locking Note: Called without the rport lock held. This
458 * function will hold the rport lock, call an _enter_*
459 * function and then unlock the rport.
461 static int fc_rport_logoff(struct fc_rport_priv *rdata)
463 struct fc_lport *lport = rdata->local_port;
464 u32 port_id = rdata->ids.port_id;
466 mutex_lock(&rdata->rp_mutex);
468 FC_RPORT_DBG(rdata, "Remove port\n");
470 rdata->flags &= ~FC_RP_STARTED;
471 if (rdata->rp_state == RPORT_ST_DELETE) {
472 FC_RPORT_DBG(rdata, "Port in Delete state, not removing\n");
477 * To explicitly Logout, the initiating Nx_Port shall terminate
478 * other open Sequences that it initiated with the destination
479 * Nx_Port prior to performing Logout.
481 lport->tt.exch_mgr_reset(lport, 0, port_id);
482 lport->tt.exch_mgr_reset(lport, port_id, 0);
484 fc_rport_enter_logo(rdata);
487 * Change the state to Delete so that we discard
490 fc_rport_enter_delete(rdata, RPORT_EV_STOP);
492 mutex_unlock(&rdata->rp_mutex);
497 * fc_rport_enter_ready() - Transition to the RPORT_ST_READY state
498 * @rdata: The remote port that is ready
500 * Locking Note: The rport lock is expected to be held before calling
503 static void fc_rport_enter_ready(struct fc_rport_priv *rdata)
505 fc_rport_state_enter(rdata, RPORT_ST_READY);
507 FC_RPORT_DBG(rdata, "Port is Ready\n");
509 if (rdata->event == RPORT_EV_NONE)
510 queue_work(rport_event_queue, &rdata->event_work);
511 rdata->event = RPORT_EV_READY;
515 * fc_rport_timeout() - Handler for the retry_work timer
516 * @work: Handle to the remote port that has timed out
518 * Locking Note: Called without the rport lock held. This
519 * function will hold the rport lock, call an _enter_*
520 * function and then unlock the rport.
522 static void fc_rport_timeout(struct work_struct *work)
524 struct fc_rport_priv *rdata =
525 container_of(work, struct fc_rport_priv, retry_work.work);
527 mutex_lock(&rdata->rp_mutex);
529 switch (rdata->rp_state) {
531 fc_rport_enter_flogi(rdata);
534 fc_rport_enter_plogi(rdata);
537 fc_rport_enter_prli(rdata);
540 fc_rport_enter_rtv(rdata);
543 fc_rport_enter_adisc(rdata);
545 case RPORT_ST_PLOGI_WAIT:
548 case RPORT_ST_DELETE:
552 mutex_unlock(&rdata->rp_mutex);
556 * fc_rport_error() - Error handler, called once retries have been exhausted
557 * @rdata: The remote port the error is happened on
558 * @fp: The error code encapsulated in a frame pointer
560 * Locking Note: The rport lock is expected to be held before
561 * calling this routine
563 static void fc_rport_error(struct fc_rport_priv *rdata, struct fc_frame *fp)
565 struct fc_lport *lport = rdata->local_port;
567 FC_RPORT_DBG(rdata, "Error %ld in state %s, retries %d\n",
568 IS_ERR(fp) ? -PTR_ERR(fp) : 0,
569 fc_rport_state(rdata), rdata->retries);
571 switch (rdata->rp_state) {
573 rdata->flags &= ~FC_RP_STARTED;
574 fc_rport_enter_delete(rdata, RPORT_EV_FAILED);
577 if (lport->point_to_multipoint) {
578 rdata->flags &= ~FC_RP_STARTED;
579 fc_rport_enter_delete(rdata, RPORT_EV_FAILED);
581 fc_rport_enter_logo(rdata);
584 fc_rport_enter_ready(rdata);
588 fc_rport_enter_logo(rdata);
590 case RPORT_ST_PLOGI_WAIT:
591 case RPORT_ST_DELETE:
599 * fc_rport_error_retry() - Handler for remote port state retries
600 * @rdata: The remote port whose state is to be retried
601 * @fp: The error code encapsulated in a frame pointer
603 * If the error was an exchange timeout retry immediately,
604 * otherwise wait for E_D_TOV.
606 * Locking Note: The rport lock is expected to be held before
607 * calling this routine
609 static void fc_rport_error_retry(struct fc_rport_priv *rdata,
612 unsigned long delay = msecs_to_jiffies(FC_DEF_E_D_TOV);
614 /* make sure this isn't an FC_EX_CLOSED error, never retry those */
615 if (PTR_ERR(fp) == -FC_EX_CLOSED)
618 if (rdata->retries < rdata->local_port->max_rport_retry_count) {
619 FC_RPORT_DBG(rdata, "Error %ld in state %s, retrying\n",
620 PTR_ERR(fp), fc_rport_state(rdata));
622 /* no additional delay on exchange timeouts */
623 if (PTR_ERR(fp) == -FC_EX_TIMEOUT)
625 schedule_delayed_work(&rdata->retry_work, delay);
630 fc_rport_error(rdata, fp);
634 * fc_rport_login_complete() - Handle parameters and completion of p-mp login.
635 * @rdata: The remote port which we logged into or which logged into us.
636 * @fp: The FLOGI or PLOGI request or response frame
638 * Returns non-zero error if a problem is detected with the frame.
639 * Does not free the frame.
641 * This is only used in point-to-multipoint mode for FIP currently.
643 static int fc_rport_login_complete(struct fc_rport_priv *rdata,
646 struct fc_lport *lport = rdata->local_port;
647 struct fc_els_flogi *flogi;
648 unsigned int e_d_tov;
651 flogi = fc_frame_payload_get(fp, sizeof(*flogi));
655 csp_flags = ntohs(flogi->fl_csp.sp_features);
657 if (fc_frame_payload_op(fp) == ELS_FLOGI) {
658 if (csp_flags & FC_SP_FT_FPORT) {
659 FC_RPORT_DBG(rdata, "Fabric bit set in FLOGI\n");
665 * E_D_TOV is not valid on an incoming FLOGI request.
667 e_d_tov = ntohl(flogi->fl_csp.sp_e_d_tov);
668 if (csp_flags & FC_SP_FT_EDTR)
670 if (e_d_tov > rdata->e_d_tov)
671 rdata->e_d_tov = e_d_tov;
673 rdata->maxframe_size = fc_plogi_get_maxframe(flogi, lport->mfs);
678 * fc_rport_flogi_resp() - Handle response to FLOGI request for p-mp mode
679 * @sp: The sequence that the FLOGI was on
680 * @fp: The FLOGI response frame
681 * @rp_arg: The remote port that received the FLOGI response
683 static void fc_rport_flogi_resp(struct fc_seq *sp, struct fc_frame *fp,
686 struct fc_rport_priv *rdata = rp_arg;
687 struct fc_lport *lport = rdata->local_port;
688 struct fc_els_flogi *flogi;
689 unsigned int r_a_tov;
691 FC_RPORT_DBG(rdata, "Received a FLOGI %s\n", fc_els_resp_type(fp));
693 if (fp == ERR_PTR(-FC_EX_CLOSED))
696 mutex_lock(&rdata->rp_mutex);
698 if (rdata->rp_state != RPORT_ST_FLOGI) {
699 FC_RPORT_DBG(rdata, "Received a FLOGI response, but in state "
700 "%s\n", fc_rport_state(rdata));
707 fc_rport_error(rdata, fp);
711 if (fc_frame_payload_op(fp) != ELS_LS_ACC)
713 if (fc_rport_login_complete(rdata, fp))
716 flogi = fc_frame_payload_get(fp, sizeof(*flogi));
719 r_a_tov = ntohl(flogi->fl_csp.sp_r_a_tov);
720 if (r_a_tov > rdata->r_a_tov)
721 rdata->r_a_tov = r_a_tov;
723 if (rdata->ids.port_name < lport->wwpn)
724 fc_rport_enter_plogi(rdata);
726 fc_rport_state_enter(rdata, RPORT_ST_PLOGI_WAIT);
730 mutex_unlock(&rdata->rp_mutex);
732 kref_put(&rdata->kref, lport->tt.rport_destroy);
735 FC_RPORT_DBG(rdata, "Bad FLOGI response\n");
736 fc_rport_error_retry(rdata, fp);
741 * fc_rport_enter_flogi() - Send a FLOGI request to the remote port for p-mp
742 * @rdata: The remote port to send a FLOGI to
744 * Locking Note: The rport lock is expected to be held before calling
747 static void fc_rport_enter_flogi(struct fc_rport_priv *rdata)
749 struct fc_lport *lport = rdata->local_port;
752 if (!lport->point_to_multipoint)
753 return fc_rport_enter_plogi(rdata);
755 FC_RPORT_DBG(rdata, "Entered FLOGI state from %s state\n",
756 fc_rport_state(rdata));
758 fc_rport_state_enter(rdata, RPORT_ST_FLOGI);
760 fp = fc_frame_alloc(lport, sizeof(struct fc_els_flogi));
762 return fc_rport_error_retry(rdata, fp);
764 if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_FLOGI,
765 fc_rport_flogi_resp, rdata,
767 fc_rport_error_retry(rdata, NULL);
769 kref_get(&rdata->kref);
773 * fc_rport_recv_flogi_req() - Handle Fabric Login (FLOGI) request in p-mp mode
774 * @lport: The local port that received the PLOGI request
775 * @rx_fp: The PLOGI request frame
777 static void fc_rport_recv_flogi_req(struct fc_lport *lport,
778 struct fc_frame *rx_fp)
780 struct fc_disc *disc;
781 struct fc_els_flogi *flp;
782 struct fc_rport_priv *rdata;
783 struct fc_frame *fp = rx_fp;
784 struct fc_seq_els_data rjt_data;
787 sid = fc_frame_sid(fp);
789 FC_RPORT_ID_DBG(lport, sid, "Received FLOGI request\n");
792 if (!lport->point_to_multipoint) {
793 rjt_data.reason = ELS_RJT_UNSUP;
794 rjt_data.explan = ELS_EXPL_NONE;
798 flp = fc_frame_payload_get(fp, sizeof(*flp));
800 rjt_data.reason = ELS_RJT_LOGIC;
801 rjt_data.explan = ELS_EXPL_INV_LEN;
805 rdata = lport->tt.rport_lookup(lport, sid);
807 rjt_data.reason = ELS_RJT_FIP;
808 rjt_data.explan = ELS_EXPL_NOT_NEIGHBOR;
811 mutex_lock(&rdata->rp_mutex);
813 FC_RPORT_DBG(rdata, "Received FLOGI in %s state\n",
814 fc_rport_state(rdata));
816 switch (rdata->rp_state) {
819 * If received the FLOGI request on RPORT which is INIT state
820 * (means not transition to FLOGI either fc_rport timeout
821 * function didn;t trigger or this end hasn;t received
822 * beacon yet from other end. In that case only, allow RPORT
823 * state machine to continue, otherwise fall through which
824 * causes the code to send reject response.
825 * NOTE; Not checking for FIP->state such as VNMP_UP or
826 * VNMP_CLAIM because if FIP state is not one of those,
827 * RPORT wouldn;t have created and 'rport_lookup' would have
828 * failed anyway in that case.
830 if (lport->point_to_multipoint)
832 case RPORT_ST_DELETE:
833 mutex_unlock(&rdata->rp_mutex);
834 rjt_data.reason = ELS_RJT_FIP;
835 rjt_data.explan = ELS_EXPL_NOT_NEIGHBOR;
838 case RPORT_ST_PLOGI_WAIT:
846 * Set the remote port to be deleted and to then restart.
847 * This queues work to be sure exchanges are reset.
849 fc_rport_enter_delete(rdata, RPORT_EV_LOGO);
850 mutex_unlock(&rdata->rp_mutex);
851 rjt_data.reason = ELS_RJT_BUSY;
852 rjt_data.explan = ELS_EXPL_NONE;
855 if (fc_rport_login_complete(rdata, fp)) {
856 mutex_unlock(&rdata->rp_mutex);
857 rjt_data.reason = ELS_RJT_LOGIC;
858 rjt_data.explan = ELS_EXPL_NONE;
862 fp = fc_frame_alloc(lport, sizeof(*flp));
866 fc_flogi_fill(lport, fp);
867 flp = fc_frame_payload_get(fp, sizeof(*flp));
868 flp->fl_cmd = ELS_LS_ACC;
870 fc_fill_reply_hdr(fp, rx_fp, FC_RCTL_ELS_REP, 0);
871 lport->tt.frame_send(lport, fp);
873 if (rdata->ids.port_name < lport->wwpn)
874 fc_rport_enter_plogi(rdata);
876 fc_rport_state_enter(rdata, RPORT_ST_PLOGI_WAIT);
878 mutex_unlock(&rdata->rp_mutex);
879 kref_put(&rdata->kref, lport->tt.rport_destroy);
880 fc_frame_free(rx_fp);
884 kref_put(&rdata->kref, lport->tt.rport_destroy);
886 lport->tt.seq_els_rsp_send(rx_fp, ELS_LS_RJT, &rjt_data);
887 fc_frame_free(rx_fp);
891 * fc_rport_plogi_resp() - Handler for ELS PLOGI responses
892 * @sp: The sequence the PLOGI is on
893 * @fp: The PLOGI response frame
894 * @rdata_arg: The remote port that sent the PLOGI response
896 * Locking Note: This function will be called without the rport lock
897 * held, but it will lock, call an _enter_* function or fc_rport_error
898 * and then unlock the rport.
900 static void fc_rport_plogi_resp(struct fc_seq *sp, struct fc_frame *fp,
903 struct fc_rport_priv *rdata = rdata_arg;
904 struct fc_lport *lport = rdata->local_port;
905 struct fc_els_flogi *plp = NULL;
910 mutex_lock(&rdata->rp_mutex);
912 FC_RPORT_DBG(rdata, "Received a PLOGI %s\n", fc_els_resp_type(fp));
914 if (rdata->rp_state != RPORT_ST_PLOGI) {
915 FC_RPORT_DBG(rdata, "Received a PLOGI response, but in state "
916 "%s\n", fc_rport_state(rdata));
923 fc_rport_error_retry(rdata, fp);
927 op = fc_frame_payload_op(fp);
928 if (op == ELS_LS_ACC &&
929 (plp = fc_frame_payload_get(fp, sizeof(*plp))) != NULL) {
930 rdata->ids.port_name = get_unaligned_be64(&plp->fl_wwpn);
931 rdata->ids.node_name = get_unaligned_be64(&plp->fl_wwnn);
933 /* save plogi response sp_features for further reference */
934 rdata->sp_features = ntohs(plp->fl_csp.sp_features);
936 if (lport->point_to_multipoint)
937 fc_rport_login_complete(rdata, fp);
938 csp_seq = ntohs(plp->fl_csp.sp_tot_seq);
939 cssp_seq = ntohs(plp->fl_cssp[3 - 1].cp_con_seq);
940 if (cssp_seq < csp_seq)
942 rdata->max_seq = csp_seq;
943 rdata->maxframe_size = fc_plogi_get_maxframe(plp, lport->mfs);
944 fc_rport_enter_prli(rdata);
946 fc_rport_error_retry(rdata, fp);
951 mutex_unlock(&rdata->rp_mutex);
952 kref_put(&rdata->kref, lport->tt.rport_destroy);
956 fc_rport_compatible_roles(struct fc_lport *lport, struct fc_rport_priv *rdata)
958 if (rdata->ids.roles == FC_PORT_ROLE_UNKNOWN)
960 if ((rdata->ids.roles & FC_PORT_ROLE_FCP_TARGET) &&
961 (lport->service_params & FCP_SPPF_INIT_FCN))
963 if ((rdata->ids.roles & FC_PORT_ROLE_FCP_INITIATOR) &&
964 (lport->service_params & FCP_SPPF_TARG_FCN))
970 * fc_rport_enter_plogi() - Send Port Login (PLOGI) request
971 * @rdata: The remote port to send a PLOGI to
973 * Locking Note: The rport lock is expected to be held before calling
976 static void fc_rport_enter_plogi(struct fc_rport_priv *rdata)
978 struct fc_lport *lport = rdata->local_port;
981 if (!fc_rport_compatible_roles(lport, rdata)) {
982 FC_RPORT_DBG(rdata, "PLOGI suppressed for incompatible role\n");
983 fc_rport_state_enter(rdata, RPORT_ST_PLOGI_WAIT);
987 FC_RPORT_DBG(rdata, "Port entered PLOGI state from %s state\n",
988 fc_rport_state(rdata));
990 fc_rport_state_enter(rdata, RPORT_ST_PLOGI);
992 rdata->maxframe_size = FC_MIN_MAX_PAYLOAD;
993 fp = fc_frame_alloc(lport, sizeof(struct fc_els_flogi));
995 FC_RPORT_DBG(rdata, "%s frame alloc failed\n", __func__);
996 fc_rport_error_retry(rdata, fp);
999 rdata->e_d_tov = lport->e_d_tov;
1001 if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_PLOGI,
1002 fc_rport_plogi_resp, rdata,
1003 2 * lport->r_a_tov))
1004 fc_rport_error_retry(rdata, NULL);
1006 kref_get(&rdata->kref);
1010 * fc_rport_prli_resp() - Process Login (PRLI) response handler
1011 * @sp: The sequence the PRLI response was on
1012 * @fp: The PRLI response frame
1013 * @rdata_arg: The remote port that sent the PRLI response
1015 * Locking Note: This function will be called without the rport lock
1016 * held, but it will lock, call an _enter_* function or fc_rport_error
1017 * and then unlock the rport.
1019 static void fc_rport_prli_resp(struct fc_seq *sp, struct fc_frame *fp,
1022 struct fc_rport_priv *rdata = rdata_arg;
1024 struct fc_els_prli prli;
1025 struct fc_els_spp spp;
1027 struct fc_els_spp temp_spp;
1028 struct fc4_prov *prov;
1029 u32 roles = FC_RPORT_ROLE_UNKNOWN;
1034 mutex_lock(&rdata->rp_mutex);
1036 FC_RPORT_DBG(rdata, "Received a PRLI %s\n", fc_els_resp_type(fp));
1038 if (rdata->rp_state != RPORT_ST_PRLI) {
1039 FC_RPORT_DBG(rdata, "Received a PRLI response, but in state "
1040 "%s\n", fc_rport_state(rdata));
1047 fc_rport_error_retry(rdata, fp);
1051 /* reinitialize remote port roles */
1052 rdata->ids.roles = FC_RPORT_ROLE_UNKNOWN;
1054 op = fc_frame_payload_op(fp);
1055 if (op == ELS_LS_ACC) {
1056 pp = fc_frame_payload_get(fp, sizeof(*pp));
1060 resp_code = (pp->spp.spp_flags & FC_SPP_RESP_MASK);
1061 FC_RPORT_DBG(rdata, "PRLI spp_flags = 0x%x\n",
1063 rdata->spp_type = pp->spp.spp_type;
1064 if (resp_code != FC_SPP_RESP_ACK) {
1065 if (resp_code == FC_SPP_RESP_CONF)
1066 fc_rport_error(rdata, fp);
1068 fc_rport_error_retry(rdata, fp);
1071 if (pp->prli.prli_spp_len < sizeof(pp->spp))
1074 fcp_parm = ntohl(pp->spp.spp_params);
1075 if (fcp_parm & FCP_SPPF_RETRY)
1076 rdata->flags |= FC_RP_FLAGS_RETRY;
1077 if (fcp_parm & FCP_SPPF_CONF_COMPL)
1078 rdata->flags |= FC_RP_FLAGS_CONF_REQ;
1080 prov = fc_passive_prov[FC_TYPE_FCP];
1082 memset(&temp_spp, 0, sizeof(temp_spp));
1083 prov->prli(rdata, pp->prli.prli_spp_len,
1084 &pp->spp, &temp_spp);
1087 rdata->supported_classes = FC_COS_CLASS3;
1088 if (fcp_parm & FCP_SPPF_INIT_FCN)
1089 roles |= FC_RPORT_ROLE_FCP_INITIATOR;
1090 if (fcp_parm & FCP_SPPF_TARG_FCN)
1091 roles |= FC_RPORT_ROLE_FCP_TARGET;
1093 rdata->ids.roles = roles;
1094 fc_rport_enter_rtv(rdata);
1097 FC_RPORT_DBG(rdata, "Bad ELS response for PRLI command\n");
1098 fc_rport_error_retry(rdata, fp);
1104 mutex_unlock(&rdata->rp_mutex);
1105 kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
1109 * fc_rport_enter_prli() - Send Process Login (PRLI) request
1110 * @rdata: The remote port to send the PRLI request to
1112 * Locking Note: The rport lock is expected to be held before calling
1115 static void fc_rport_enter_prli(struct fc_rport_priv *rdata)
1117 struct fc_lport *lport = rdata->local_port;
1119 struct fc_els_prli prli;
1120 struct fc_els_spp spp;
1122 struct fc_frame *fp;
1123 struct fc4_prov *prov;
1126 * If the rport is one of the well known addresses
1127 * we skip PRLI and RTV and go straight to READY.
1129 if (rdata->ids.port_id >= FC_FID_DOM_MGR) {
1130 fc_rport_enter_ready(rdata);
1134 FC_RPORT_DBG(rdata, "Port entered PRLI state from %s state\n",
1135 fc_rport_state(rdata));
1137 fc_rport_state_enter(rdata, RPORT_ST_PRLI);
1139 fp = fc_frame_alloc(lport, sizeof(*pp));
1141 fc_rport_error_retry(rdata, fp);
1145 fc_prli_fill(lport, fp);
1147 prov = fc_passive_prov[FC_TYPE_FCP];
1149 pp = fc_frame_payload_get(fp, sizeof(*pp));
1150 prov->prli(rdata, sizeof(pp->spp), NULL, &pp->spp);
1153 fc_fill_fc_hdr(fp, FC_RCTL_ELS_REQ, rdata->ids.port_id,
1154 fc_host_port_id(lport->host), FC_TYPE_ELS,
1155 FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT, 0);
1157 if (!lport->tt.exch_seq_send(lport, fp, fc_rport_prli_resp,
1158 NULL, rdata, 2 * lport->r_a_tov))
1159 fc_rport_error_retry(rdata, NULL);
1161 kref_get(&rdata->kref);
1165 * fc_rport_els_rtv_resp() - Handler for Request Timeout Value (RTV) responses
1166 * @sp: The sequence the RTV was on
1167 * @fp: The RTV response frame
1168 * @rdata_arg: The remote port that sent the RTV response
1170 * Many targets don't seem to support this.
1172 * Locking Note: This function will be called without the rport lock
1173 * held, but it will lock, call an _enter_* function or fc_rport_error
1174 * and then unlock the rport.
1176 static void fc_rport_rtv_resp(struct fc_seq *sp, struct fc_frame *fp,
1179 struct fc_rport_priv *rdata = rdata_arg;
1182 mutex_lock(&rdata->rp_mutex);
1184 FC_RPORT_DBG(rdata, "Received a RTV %s\n", fc_els_resp_type(fp));
1186 if (rdata->rp_state != RPORT_ST_RTV) {
1187 FC_RPORT_DBG(rdata, "Received a RTV response, but in state "
1188 "%s\n", fc_rport_state(rdata));
1195 fc_rport_error(rdata, fp);
1199 op = fc_frame_payload_op(fp);
1200 if (op == ELS_LS_ACC) {
1201 struct fc_els_rtv_acc *rtv;
1205 rtv = fc_frame_payload_get(fp, sizeof(*rtv));
1207 toq = ntohl(rtv->rtv_toq);
1208 tov = ntohl(rtv->rtv_r_a_tov);
1211 rdata->r_a_tov = tov;
1212 tov = ntohl(rtv->rtv_e_d_tov);
1213 if (toq & FC_ELS_RTV_EDRES)
1217 rdata->e_d_tov = tov;
1221 fc_rport_enter_ready(rdata);
1226 mutex_unlock(&rdata->rp_mutex);
1227 kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
1231 * fc_rport_enter_rtv() - Send Request Timeout Value (RTV) request
1232 * @rdata: The remote port to send the RTV request to
1234 * Locking Note: The rport lock is expected to be held before calling
1237 static void fc_rport_enter_rtv(struct fc_rport_priv *rdata)
1239 struct fc_frame *fp;
1240 struct fc_lport *lport = rdata->local_port;
1242 FC_RPORT_DBG(rdata, "Port entered RTV state from %s state\n",
1243 fc_rport_state(rdata));
1245 fc_rport_state_enter(rdata, RPORT_ST_RTV);
1247 fp = fc_frame_alloc(lport, sizeof(struct fc_els_rtv));
1249 fc_rport_error_retry(rdata, fp);
1253 if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_RTV,
1254 fc_rport_rtv_resp, rdata,
1255 2 * lport->r_a_tov))
1256 fc_rport_error_retry(rdata, NULL);
1258 kref_get(&rdata->kref);
1262 * fc_rport_logo_resp() - Handler for logout (LOGO) responses
1263 * @sp: The sequence the LOGO was on
1264 * @fp: The LOGO response frame
1265 * @lport_arg: The local port
1267 static void fc_rport_logo_resp(struct fc_seq *sp, struct fc_frame *fp,
1270 struct fc_lport *lport = lport_arg;
1272 FC_RPORT_ID_DBG(lport, fc_seq_exch(sp)->did,
1273 "Received a LOGO %s\n", fc_els_resp_type(fp));
1280 * fc_rport_enter_logo() - Send a logout (LOGO) request
1281 * @rdata: The remote port to send the LOGO request to
1283 * Locking Note: The rport lock is expected to be held before calling
1286 static void fc_rport_enter_logo(struct fc_rport_priv *rdata)
1288 struct fc_lport *lport = rdata->local_port;
1289 struct fc_frame *fp;
1291 FC_RPORT_DBG(rdata, "Port sending LOGO from %s state\n",
1292 fc_rport_state(rdata));
1294 fp = fc_frame_alloc(lport, sizeof(struct fc_els_logo));
1297 (void)lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_LOGO,
1298 fc_rport_logo_resp, lport, 0);
1302 * fc_rport_els_adisc_resp() - Handler for Address Discovery (ADISC) responses
1303 * @sp: The sequence the ADISC response was on
1304 * @fp: The ADISC response frame
1305 * @rdata_arg: The remote port that sent the ADISC response
1307 * Locking Note: This function will be called without the rport lock
1308 * held, but it will lock, call an _enter_* function or fc_rport_error
1309 * and then unlock the rport.
1311 static void fc_rport_adisc_resp(struct fc_seq *sp, struct fc_frame *fp,
1314 struct fc_rport_priv *rdata = rdata_arg;
1315 struct fc_els_adisc *adisc;
1318 mutex_lock(&rdata->rp_mutex);
1320 FC_RPORT_DBG(rdata, "Received a ADISC response\n");
1322 if (rdata->rp_state != RPORT_ST_ADISC) {
1323 FC_RPORT_DBG(rdata, "Received a ADISC resp but in state %s\n",
1324 fc_rport_state(rdata));
1331 fc_rport_error(rdata, fp);
1336 * If address verification failed. Consider us logged out of the rport.
1337 * Since the rport is still in discovery, we want to be
1338 * logged in, so go to PLOGI state. Otherwise, go back to READY.
1340 op = fc_frame_payload_op(fp);
1341 adisc = fc_frame_payload_get(fp, sizeof(*adisc));
1342 if (op != ELS_LS_ACC || !adisc ||
1343 ntoh24(adisc->adisc_port_id) != rdata->ids.port_id ||
1344 get_unaligned_be64(&adisc->adisc_wwpn) != rdata->ids.port_name ||
1345 get_unaligned_be64(&adisc->adisc_wwnn) != rdata->ids.node_name) {
1346 FC_RPORT_DBG(rdata, "ADISC error or mismatch\n");
1347 fc_rport_enter_flogi(rdata);
1349 FC_RPORT_DBG(rdata, "ADISC OK\n");
1350 fc_rport_enter_ready(rdata);
1355 mutex_unlock(&rdata->rp_mutex);
1356 kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
1360 * fc_rport_enter_adisc() - Send Address Discover (ADISC) request
1361 * @rdata: The remote port to send the ADISC request to
1363 * Locking Note: The rport lock is expected to be held before calling
1366 static void fc_rport_enter_adisc(struct fc_rport_priv *rdata)
1368 struct fc_lport *lport = rdata->local_port;
1369 struct fc_frame *fp;
1371 FC_RPORT_DBG(rdata, "sending ADISC from %s state\n",
1372 fc_rport_state(rdata));
1374 fc_rport_state_enter(rdata, RPORT_ST_ADISC);
1376 fp = fc_frame_alloc(lport, sizeof(struct fc_els_adisc));
1378 fc_rport_error_retry(rdata, fp);
1381 if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_ADISC,
1382 fc_rport_adisc_resp, rdata,
1383 2 * lport->r_a_tov))
1384 fc_rport_error_retry(rdata, NULL);
1386 kref_get(&rdata->kref);
1390 * fc_rport_recv_adisc_req() - Handler for Address Discovery (ADISC) requests
1391 * @rdata: The remote port that sent the ADISC request
1392 * @in_fp: The ADISC request frame
1394 * Locking Note: Called with the lport and rport locks held.
1396 static void fc_rport_recv_adisc_req(struct fc_rport_priv *rdata,
1397 struct fc_frame *in_fp)
1399 struct fc_lport *lport = rdata->local_port;
1400 struct fc_frame *fp;
1401 struct fc_els_adisc *adisc;
1402 struct fc_seq_els_data rjt_data;
1404 FC_RPORT_DBG(rdata, "Received ADISC request\n");
1406 adisc = fc_frame_payload_get(in_fp, sizeof(*adisc));
1408 rjt_data.reason = ELS_RJT_PROT;
1409 rjt_data.explan = ELS_EXPL_INV_LEN;
1410 lport->tt.seq_els_rsp_send(in_fp, ELS_LS_RJT, &rjt_data);
1414 fp = fc_frame_alloc(lport, sizeof(*adisc));
1417 fc_adisc_fill(lport, fp);
1418 adisc = fc_frame_payload_get(fp, sizeof(*adisc));
1419 adisc->adisc_cmd = ELS_LS_ACC;
1420 fc_fill_reply_hdr(fp, in_fp, FC_RCTL_ELS_REP, 0);
1421 lport->tt.frame_send(lport, fp);
1423 fc_frame_free(in_fp);
1427 * fc_rport_recv_rls_req() - Handle received Read Link Status request
1428 * @rdata: The remote port that sent the RLS request
1429 * @rx_fp: The PRLI request frame
1431 * Locking Note: The rport lock is expected to be held before calling
1434 static void fc_rport_recv_rls_req(struct fc_rport_priv *rdata,
1435 struct fc_frame *rx_fp)
1438 struct fc_lport *lport = rdata->local_port;
1439 struct fc_frame *fp;
1440 struct fc_els_rls *rls;
1441 struct fc_els_rls_resp *rsp;
1442 struct fc_els_lesb *lesb;
1443 struct fc_seq_els_data rjt_data;
1444 struct fc_host_statistics *hst;
1446 FC_RPORT_DBG(rdata, "Received RLS request while in state %s\n",
1447 fc_rport_state(rdata));
1449 rls = fc_frame_payload_get(rx_fp, sizeof(*rls));
1451 rjt_data.reason = ELS_RJT_PROT;
1452 rjt_data.explan = ELS_EXPL_INV_LEN;
1456 fp = fc_frame_alloc(lport, sizeof(*rsp));
1458 rjt_data.reason = ELS_RJT_UNAB;
1459 rjt_data.explan = ELS_EXPL_INSUF_RES;
1463 rsp = fc_frame_payload_get(fp, sizeof(*rsp));
1464 memset(rsp, 0, sizeof(*rsp));
1465 rsp->rls_cmd = ELS_LS_ACC;
1466 lesb = &rsp->rls_lesb;
1467 if (lport->tt.get_lesb) {
1468 /* get LESB from LLD if it supports it */
1469 lport->tt.get_lesb(lport, lesb);
1471 fc_get_host_stats(lport->host);
1472 hst = &lport->host_stats;
1473 lesb->lesb_link_fail = htonl(hst->link_failure_count);
1474 lesb->lesb_sync_loss = htonl(hst->loss_of_sync_count);
1475 lesb->lesb_sig_loss = htonl(hst->loss_of_signal_count);
1476 lesb->lesb_prim_err = htonl(hst->prim_seq_protocol_err_count);
1477 lesb->lesb_inv_word = htonl(hst->invalid_tx_word_count);
1478 lesb->lesb_inv_crc = htonl(hst->invalid_crc_count);
1481 fc_fill_reply_hdr(fp, rx_fp, FC_RCTL_ELS_REP, 0);
1482 lport->tt.frame_send(lport, fp);
1486 lport->tt.seq_els_rsp_send(rx_fp, ELS_LS_RJT, &rjt_data);
1488 fc_frame_free(rx_fp);
1492 * fc_rport_recv_els_req() - Handler for validated ELS requests
1493 * @lport: The local port that received the ELS request
1494 * @fp: The ELS request frame
1496 * Handle incoming ELS requests that require port login.
1497 * The ELS opcode has already been validated by the caller.
1499 * Locking Note: Called with the lport lock held.
1501 static void fc_rport_recv_els_req(struct fc_lport *lport, struct fc_frame *fp)
1503 struct fc_rport_priv *rdata;
1504 struct fc_seq_els_data els_data;
1506 rdata = lport->tt.rport_lookup(lport, fc_frame_sid(fp));
1510 mutex_lock(&rdata->rp_mutex);
1512 switch (rdata->rp_state) {
1515 case RPORT_ST_READY:
1516 case RPORT_ST_ADISC:
1519 mutex_unlock(&rdata->rp_mutex);
1520 kref_put(&rdata->kref, lport->tt.rport_destroy);
1524 switch (fc_frame_payload_op(fp)) {
1526 fc_rport_recv_prli_req(rdata, fp);
1529 fc_rport_recv_prlo_req(rdata, fp);
1532 fc_rport_recv_adisc_req(rdata, fp);
1535 lport->tt.seq_els_rsp_send(fp, ELS_RRQ, NULL);
1539 lport->tt.seq_els_rsp_send(fp, ELS_REC, NULL);
1543 fc_rport_recv_rls_req(rdata, fp);
1546 fc_frame_free(fp); /* can't happen */
1550 mutex_unlock(&rdata->rp_mutex);
1551 kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
1555 els_data.reason = ELS_RJT_UNAB;
1556 els_data.explan = ELS_EXPL_PLOGI_REQD;
1557 lport->tt.seq_els_rsp_send(fp, ELS_LS_RJT, &els_data);
1562 * fc_rport_recv_req() - Handler for requests
1563 * @lport: The local port that received the request
1564 * @fp: The request frame
1566 * Locking Note: Called with the lport lock held.
1568 static void fc_rport_recv_req(struct fc_lport *lport, struct fc_frame *fp)
1570 struct fc_seq_els_data els_data;
1573 * Handle FLOGI, PLOGI and LOGO requests separately, since they
1574 * don't require prior login.
1575 * Check for unsupported opcodes first and reject them.
1576 * For some ops, it would be incorrect to reject with "PLOGI required".
1578 switch (fc_frame_payload_op(fp)) {
1580 fc_rport_recv_flogi_req(lport, fp);
1583 fc_rport_recv_plogi_req(lport, fp);
1586 fc_rport_recv_logo_req(lport, fp);
1594 fc_rport_recv_els_req(lport, fp);
1597 els_data.reason = ELS_RJT_UNSUP;
1598 els_data.explan = ELS_EXPL_NONE;
1599 lport->tt.seq_els_rsp_send(fp, ELS_LS_RJT, &els_data);
1606 * fc_rport_recv_plogi_req() - Handler for Port Login (PLOGI) requests
1607 * @lport: The local port that received the PLOGI request
1608 * @rx_fp: The PLOGI request frame
1610 * Locking Note: The rport lock is held before calling this function.
1612 static void fc_rport_recv_plogi_req(struct fc_lport *lport,
1613 struct fc_frame *rx_fp)
1615 struct fc_disc *disc;
1616 struct fc_rport_priv *rdata;
1617 struct fc_frame *fp = rx_fp;
1618 struct fc_els_flogi *pl;
1619 struct fc_seq_els_data rjt_data;
1622 sid = fc_frame_sid(fp);
1624 FC_RPORT_ID_DBG(lport, sid, "Received PLOGI request\n");
1626 pl = fc_frame_payload_get(fp, sizeof(*pl));
1628 FC_RPORT_ID_DBG(lport, sid, "Received PLOGI too short\n");
1629 rjt_data.reason = ELS_RJT_PROT;
1630 rjt_data.explan = ELS_EXPL_INV_LEN;
1634 disc = &lport->disc;
1635 mutex_lock(&disc->disc_mutex);
1636 rdata = lport->tt.rport_create(lport, sid);
1638 mutex_unlock(&disc->disc_mutex);
1639 rjt_data.reason = ELS_RJT_UNAB;
1640 rjt_data.explan = ELS_EXPL_INSUF_RES;
1644 mutex_lock(&rdata->rp_mutex);
1645 mutex_unlock(&disc->disc_mutex);
1647 rdata->ids.port_name = get_unaligned_be64(&pl->fl_wwpn);
1648 rdata->ids.node_name = get_unaligned_be64(&pl->fl_wwnn);
1651 * If the rport was just created, possibly due to the incoming PLOGI,
1652 * set the state appropriately and accept the PLOGI.
1654 * If we had also sent a PLOGI, and if the received PLOGI is from a
1655 * higher WWPN, we accept it, otherwise an LS_RJT is sent with reason
1656 * "command already in progress".
1658 * XXX TBD: If the session was ready before, the PLOGI should result in
1659 * all outstanding exchanges being reset.
1661 switch (rdata->rp_state) {
1663 FC_RPORT_DBG(rdata, "Received PLOGI in INIT state\n");
1665 case RPORT_ST_PLOGI_WAIT:
1666 FC_RPORT_DBG(rdata, "Received PLOGI in PLOGI_WAIT state\n");
1668 case RPORT_ST_PLOGI:
1669 FC_RPORT_DBG(rdata, "Received PLOGI in PLOGI state\n");
1670 if (rdata->ids.port_name < lport->wwpn) {
1671 mutex_unlock(&rdata->rp_mutex);
1672 rjt_data.reason = ELS_RJT_INPROG;
1673 rjt_data.explan = ELS_EXPL_NONE;
1679 case RPORT_ST_READY:
1680 case RPORT_ST_ADISC:
1681 FC_RPORT_DBG(rdata, "Received PLOGI in logged-in state %d "
1682 "- ignored for now\n", rdata->rp_state);
1683 /* XXX TBD - should reset */
1685 case RPORT_ST_FLOGI:
1686 case RPORT_ST_DELETE:
1687 FC_RPORT_DBG(rdata, "Received PLOGI in state %s - send busy\n",
1688 fc_rport_state(rdata));
1689 mutex_unlock(&rdata->rp_mutex);
1690 rjt_data.reason = ELS_RJT_BUSY;
1691 rjt_data.explan = ELS_EXPL_NONE;
1694 if (!fc_rport_compatible_roles(lport, rdata)) {
1695 FC_RPORT_DBG(rdata, "Received PLOGI for incompatible role\n");
1696 mutex_unlock(&rdata->rp_mutex);
1697 rjt_data.reason = ELS_RJT_LOGIC;
1698 rjt_data.explan = ELS_EXPL_NONE;
1703 * Get session payload size from incoming PLOGI.
1705 rdata->maxframe_size = fc_plogi_get_maxframe(pl, lport->mfs);
1708 * Send LS_ACC. If this fails, the originator should retry.
1710 fp = fc_frame_alloc(lport, sizeof(*pl));
1714 fc_plogi_fill(lport, fp, ELS_LS_ACC);
1715 fc_fill_reply_hdr(fp, rx_fp, FC_RCTL_ELS_REP, 0);
1716 lport->tt.frame_send(lport, fp);
1717 fc_rport_enter_prli(rdata);
1719 mutex_unlock(&rdata->rp_mutex);
1720 fc_frame_free(rx_fp);
1724 lport->tt.seq_els_rsp_send(fp, ELS_LS_RJT, &rjt_data);
1729 * fc_rport_recv_prli_req() - Handler for process login (PRLI) requests
1730 * @rdata: The remote port that sent the PRLI request
1731 * @rx_fp: The PRLI request frame
1733 * Locking Note: The rport lock is expected to be held before calling
1736 static void fc_rport_recv_prli_req(struct fc_rport_priv *rdata,
1737 struct fc_frame *rx_fp)
1739 struct fc_lport *lport = rdata->local_port;
1740 struct fc_frame *fp;
1742 struct fc_els_prli prli;
1743 struct fc_els_spp spp;
1745 struct fc_els_spp *rspp; /* request service param page */
1746 struct fc_els_spp *spp; /* response spp */
1749 enum fc_els_spp_resp resp;
1750 enum fc_els_spp_resp passive;
1751 struct fc_seq_els_data rjt_data;
1752 struct fc4_prov *prov;
1754 FC_RPORT_DBG(rdata, "Received PRLI request while in state %s\n",
1755 fc_rport_state(rdata));
1757 len = fr_len(rx_fp) - sizeof(struct fc_frame_header);
1758 pp = fc_frame_payload_get(rx_fp, sizeof(*pp));
1761 plen = ntohs(pp->prli.prli_len);
1762 if ((plen % 4) != 0 || plen > len || plen < 16)
1766 plen = pp->prli.prli_spp_len;
1767 if ((plen % 4) != 0 || plen < sizeof(*spp) ||
1768 plen > len || len < sizeof(*pp) || plen < 12)
1772 fp = fc_frame_alloc(lport, len);
1774 rjt_data.reason = ELS_RJT_UNAB;
1775 rjt_data.explan = ELS_EXPL_INSUF_RES;
1778 pp = fc_frame_payload_get(fp, len);
1781 pp->prli.prli_cmd = ELS_LS_ACC;
1782 pp->prli.prli_spp_len = plen;
1783 pp->prli.prli_len = htons(len);
1784 len -= sizeof(struct fc_els_prli);
1787 * Go through all the service parameter pages and build
1788 * response. If plen indicates longer SPP than standard,
1789 * use that. The entire response has been pre-cleared above.
1792 mutex_lock(&fc_prov_mutex);
1793 while (len >= plen) {
1794 rdata->spp_type = rspp->spp_type;
1795 spp->spp_type = rspp->spp_type;
1796 spp->spp_type_ext = rspp->spp_type_ext;
1799 if (rspp->spp_type < FC_FC4_PROV_SIZE) {
1800 prov = fc_active_prov[rspp->spp_type];
1802 resp = prov->prli(rdata, plen, rspp, spp);
1803 prov = fc_passive_prov[rspp->spp_type];
1805 passive = prov->prli(rdata, plen, rspp, spp);
1806 if (!resp || passive == FC_SPP_RESP_ACK)
1811 if (spp->spp_flags & FC_SPP_EST_IMG_PAIR)
1812 resp |= FC_SPP_RESP_CONF;
1814 resp |= FC_SPP_RESP_INVL;
1816 spp->spp_flags |= resp;
1818 rspp = (struct fc_els_spp *)((char *)rspp + plen);
1819 spp = (struct fc_els_spp *)((char *)spp + plen);
1821 mutex_unlock(&fc_prov_mutex);
1824 * Send LS_ACC. If this fails, the originator should retry.
1826 fc_fill_reply_hdr(fp, rx_fp, FC_RCTL_ELS_REP, 0);
1827 lport->tt.frame_send(lport, fp);
1829 switch (rdata->rp_state) {
1831 fc_rport_enter_ready(rdata);
1839 rjt_data.reason = ELS_RJT_PROT;
1840 rjt_data.explan = ELS_EXPL_INV_LEN;
1842 lport->tt.seq_els_rsp_send(rx_fp, ELS_LS_RJT, &rjt_data);
1844 fc_frame_free(rx_fp);
1848 * fc_rport_recv_prlo_req() - Handler for process logout (PRLO) requests
1849 * @rdata: The remote port that sent the PRLO request
1850 * @rx_fp: The PRLO request frame
1852 * Locking Note: The rport lock is expected to be held before calling
1855 static void fc_rport_recv_prlo_req(struct fc_rport_priv *rdata,
1856 struct fc_frame *rx_fp)
1858 struct fc_lport *lport = rdata->local_port;
1859 struct fc_frame *fp;
1861 struct fc_els_prlo prlo;
1862 struct fc_els_spp spp;
1864 struct fc_els_spp *rspp; /* request service param page */
1865 struct fc_els_spp *spp; /* response spp */
1868 struct fc_seq_els_data rjt_data;
1870 FC_RPORT_DBG(rdata, "Received PRLO request while in state %s\n",
1871 fc_rport_state(rdata));
1873 len = fr_len(rx_fp) - sizeof(struct fc_frame_header);
1874 pp = fc_frame_payload_get(rx_fp, sizeof(*pp));
1877 plen = ntohs(pp->prlo.prlo_len);
1885 fp = fc_frame_alloc(lport, len);
1887 rjt_data.reason = ELS_RJT_UNAB;
1888 rjt_data.explan = ELS_EXPL_INSUF_RES;
1892 pp = fc_frame_payload_get(fp, len);
1895 pp->prlo.prlo_cmd = ELS_LS_ACC;
1896 pp->prlo.prlo_obs = 0x10;
1897 pp->prlo.prlo_len = htons(len);
1899 spp->spp_type = rspp->spp_type;
1900 spp->spp_type_ext = rspp->spp_type_ext;
1901 spp->spp_flags = FC_SPP_RESP_ACK;
1903 fc_rport_enter_prli(rdata);
1905 fc_fill_reply_hdr(fp, rx_fp, FC_RCTL_ELS_REP, 0);
1906 lport->tt.frame_send(lport, fp);
1910 rjt_data.reason = ELS_RJT_PROT;
1911 rjt_data.explan = ELS_EXPL_INV_LEN;
1913 lport->tt.seq_els_rsp_send(rx_fp, ELS_LS_RJT, &rjt_data);
1915 fc_frame_free(rx_fp);
1919 * fc_rport_recv_logo_req() - Handler for logout (LOGO) requests
1920 * @lport: The local port that received the LOGO request
1921 * @fp: The LOGO request frame
1923 * Locking Note: The rport lock is expected to be held before calling
1926 static void fc_rport_recv_logo_req(struct fc_lport *lport, struct fc_frame *fp)
1928 struct fc_rport_priv *rdata;
1931 lport->tt.seq_els_rsp_send(fp, ELS_LS_ACC, NULL);
1933 sid = fc_frame_sid(fp);
1935 rdata = lport->tt.rport_lookup(lport, sid);
1937 mutex_lock(&rdata->rp_mutex);
1938 FC_RPORT_DBG(rdata, "Received LOGO request while in state %s\n",
1939 fc_rport_state(rdata));
1941 fc_rport_enter_delete(rdata, RPORT_EV_STOP);
1942 mutex_unlock(&rdata->rp_mutex);
1943 kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
1945 FC_RPORT_ID_DBG(lport, sid,
1946 "Received LOGO from non-logged-in port\n");
1951 * fc_rport_flush_queue() - Flush the rport_event_queue
1953 static void fc_rport_flush_queue(void)
1955 flush_workqueue(rport_event_queue);
1959 * fc_rport_init() - Initialize the remote port layer for a local port
1960 * @lport: The local port to initialize the remote port layer for
1962 int fc_rport_init(struct fc_lport *lport)
1964 if (!lport->tt.rport_lookup)
1965 lport->tt.rport_lookup = fc_rport_lookup;
1967 if (!lport->tt.rport_create)
1968 lport->tt.rport_create = fc_rport_create;
1970 if (!lport->tt.rport_login)
1971 lport->tt.rport_login = fc_rport_login;
1973 if (!lport->tt.rport_logoff)
1974 lport->tt.rport_logoff = fc_rport_logoff;
1976 if (!lport->tt.rport_recv_req)
1977 lport->tt.rport_recv_req = fc_rport_recv_req;
1979 if (!lport->tt.rport_flush_queue)
1980 lport->tt.rport_flush_queue = fc_rport_flush_queue;
1982 if (!lport->tt.rport_destroy)
1983 lport->tt.rport_destroy = fc_rport_destroy;
1987 EXPORT_SYMBOL(fc_rport_init);
1990 * fc_rport_fcp_prli() - Handle incoming PRLI for the FCP initiator.
1991 * @rdata: remote port private
1992 * @spp_len: service parameter page length
1993 * @rspp: received service parameter page
1994 * @spp: response service parameter page
1996 * Returns the value for the response code to be placed in spp_flags;
1997 * Returns 0 if not an initiator.
1999 static int fc_rport_fcp_prli(struct fc_rport_priv *rdata, u32 spp_len,
2000 const struct fc_els_spp *rspp,
2001 struct fc_els_spp *spp)
2003 struct fc_lport *lport = rdata->local_port;
2006 fcp_parm = ntohl(rspp->spp_params);
2007 rdata->ids.roles = FC_RPORT_ROLE_UNKNOWN;
2008 if (fcp_parm & FCP_SPPF_INIT_FCN)
2009 rdata->ids.roles |= FC_RPORT_ROLE_FCP_INITIATOR;
2010 if (fcp_parm & FCP_SPPF_TARG_FCN)
2011 rdata->ids.roles |= FC_RPORT_ROLE_FCP_TARGET;
2012 if (fcp_parm & FCP_SPPF_RETRY)
2013 rdata->flags |= FC_RP_FLAGS_RETRY;
2014 rdata->supported_classes = FC_COS_CLASS3;
2016 if (!(lport->service_params & FCP_SPPF_INIT_FCN))
2019 spp->spp_flags |= rspp->spp_flags & FC_SPP_EST_IMG_PAIR;
2022 * OR in our service parameters with other providers (target), if any.
2024 fcp_parm = ntohl(spp->spp_params);
2025 spp->spp_params = htonl(fcp_parm | lport->service_params);
2026 return FC_SPP_RESP_ACK;
2030 * FC-4 provider ops for FCP initiator.
2032 struct fc4_prov fc_rport_fcp_init = {
2033 .prli = fc_rport_fcp_prli,
2037 * fc_rport_t0_prli() - Handle incoming PRLI parameters for type 0
2038 * @rdata: remote port private
2039 * @spp_len: service parameter page length
2040 * @rspp: received service parameter page
2041 * @spp: response service parameter page
2043 static int fc_rport_t0_prli(struct fc_rport_priv *rdata, u32 spp_len,
2044 const struct fc_els_spp *rspp,
2045 struct fc_els_spp *spp)
2047 if (rspp->spp_flags & FC_SPP_EST_IMG_PAIR)
2048 return FC_SPP_RESP_INVL;
2049 return FC_SPP_RESP_ACK;
2053 * FC-4 provider ops for type 0 service parameters.
2055 * This handles the special case of type 0 which is always successful
2056 * but doesn't do anything otherwise.
2058 struct fc4_prov fc_rport_t0_prov = {
2059 .prli = fc_rport_t0_prli,
2063 * fc_setup_rport() - Initialize the rport_event_queue
2065 int fc_setup_rport(void)
2067 rport_event_queue = create_singlethread_workqueue("fc_rport_eq");
2068 if (!rport_event_queue)
2074 * fc_destroy_rport() - Destroy the rport_event_queue
2076 void fc_destroy_rport(void)
2078 destroy_workqueue(rport_event_queue);
2082 * fc_rport_terminate_io() - Stop all outstanding I/O on a remote port
2083 * @rport: The remote port whose I/O should be terminated
2085 void fc_rport_terminate_io(struct fc_rport *rport)
2087 struct fc_rport_libfc_priv *rpriv = rport->dd_data;
2088 struct fc_lport *lport = rpriv->local_port;
2090 lport->tt.exch_mgr_reset(lport, 0, rport->port_id);
2091 lport->tt.exch_mgr_reset(lport, rport->port_id, 0);
2093 EXPORT_SYMBOL(fc_rport_terminate_io);