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;
131 rdata = lport->tt.rport_lookup(lport, port_id);
135 rdata = kzalloc(sizeof(*rdata) + lport->rport_priv_size, GFP_KERNEL);
139 rdata->ids.node_name = -1;
140 rdata->ids.port_name = -1;
141 rdata->ids.port_id = port_id;
142 rdata->ids.roles = FC_RPORT_ROLE_UNKNOWN;
144 kref_init(&rdata->kref);
145 mutex_init(&rdata->rp_mutex);
146 rdata->local_port = lport;
147 rdata->rp_state = RPORT_ST_INIT;
148 rdata->event = RPORT_EV_NONE;
149 rdata->flags = FC_RP_FLAGS_REC_SUPPORTED;
150 rdata->e_d_tov = lport->e_d_tov;
151 rdata->r_a_tov = lport->r_a_tov;
152 rdata->maxframe_size = FC_MIN_MAX_PAYLOAD;
153 INIT_DELAYED_WORK(&rdata->retry_work, fc_rport_timeout);
154 INIT_WORK(&rdata->event_work, fc_rport_work);
155 if (port_id != FC_FID_DIR_SERV) {
156 rdata->lld_event_callback = lport->tt.rport_event_callback;
157 list_add_rcu(&rdata->peers, &lport->disc.rports);
163 * fc_rport_destroy() - Free a remote port after last reference is released
164 * @kref: The remote port's kref
166 static void fc_rport_destroy(struct kref *kref)
168 struct fc_rport_priv *rdata;
170 rdata = container_of(kref, struct fc_rport_priv, kref);
171 kfree_rcu(rdata, rcu);
175 * fc_rport_state() - Return a string identifying the remote port's state
176 * @rdata: The remote port
178 static const char *fc_rport_state(struct fc_rport_priv *rdata)
182 cp = fc_rport_state_names[rdata->rp_state];
189 * fc_set_rport_loss_tmo() - Set the remote port loss timeout
190 * @rport: The remote port that gets a new timeout value
191 * @timeout: The new timeout value (in seconds)
193 void fc_set_rport_loss_tmo(struct fc_rport *rport, u32 timeout)
196 rport->dev_loss_tmo = timeout;
198 rport->dev_loss_tmo = 1;
200 EXPORT_SYMBOL(fc_set_rport_loss_tmo);
203 * fc_plogi_get_maxframe() - Get the maximum payload from the common service
204 * parameters in a FLOGI frame
205 * @flp: The FLOGI or PLOGI payload
206 * @maxval: The maximum frame size upper limit; this may be less than what
207 * is in the service parameters
209 static unsigned int fc_plogi_get_maxframe(struct fc_els_flogi *flp,
215 * Get max payload from the common service parameters and the
216 * class 3 receive data field size.
218 mfs = ntohs(flp->fl_csp.sp_bb_data) & FC_SP_BB_DATA_MASK;
219 if (mfs >= FC_SP_MIN_MAX_PAYLOAD && mfs < maxval)
221 mfs = ntohs(flp->fl_cssp[3 - 1].cp_rdfs);
222 if (mfs >= FC_SP_MIN_MAX_PAYLOAD && mfs < maxval)
228 * fc_rport_state_enter() - Change the state of a remote port
229 * @rdata: The remote port whose state should change
230 * @new: The new state
232 * Locking Note: Called with the rport lock held
234 static void fc_rport_state_enter(struct fc_rport_priv *rdata,
235 enum fc_rport_state new)
237 if (rdata->rp_state != new)
239 rdata->rp_state = new;
243 * fc_rport_work() - Handler for remote port events in the rport_event_queue
244 * @work: Handle to the remote port being dequeued
246 static void fc_rport_work(struct work_struct *work)
249 struct fc_rport_priv *rdata =
250 container_of(work, struct fc_rport_priv, event_work);
251 struct fc_rport_libfc_priv *rpriv;
252 enum fc_rport_event event;
253 struct fc_lport *lport = rdata->local_port;
254 struct fc_rport_operations *rport_ops;
255 struct fc_rport_identifiers ids;
256 struct fc_rport *rport;
257 struct fc4_prov *prov;
260 mutex_lock(&rdata->rp_mutex);
261 event = rdata->event;
262 rport_ops = rdata->ops;
263 rport = rdata->rport;
265 FC_RPORT_DBG(rdata, "work event %u\n", event);
270 rdata->event = RPORT_EV_NONE;
271 rdata->major_retries = 0;
272 kref_get(&rdata->kref);
273 mutex_unlock(&rdata->rp_mutex);
276 rport = fc_remote_port_add(lport->host, 0, &ids);
278 FC_RPORT_DBG(rdata, "Failed to add the rport\n");
279 lport->tt.rport_logoff(rdata);
280 kref_put(&rdata->kref, lport->tt.rport_destroy);
283 mutex_lock(&rdata->rp_mutex);
285 FC_RPORT_DBG(rdata, "rport already allocated\n");
286 rdata->rport = rport;
287 rport->maxframe_size = rdata->maxframe_size;
288 rport->supported_classes = rdata->supported_classes;
290 rpriv = rport->dd_data;
291 rpriv->local_port = lport;
292 rpriv->rp_state = rdata->rp_state;
293 rpriv->flags = rdata->flags;
294 rpriv->e_d_tov = rdata->e_d_tov;
295 rpriv->r_a_tov = rdata->r_a_tov;
296 mutex_unlock(&rdata->rp_mutex);
298 if (rport_ops && rport_ops->event_callback) {
299 FC_RPORT_DBG(rdata, "callback ev %d\n", event);
300 rport_ops->event_callback(lport, rdata, event);
302 if (rdata->lld_event_callback) {
303 FC_RPORT_DBG(rdata, "lld callback ev %d\n", event);
304 rdata->lld_event_callback(lport, rdata, event);
306 kref_put(&rdata->kref, lport->tt.rport_destroy);
309 case RPORT_EV_FAILED:
312 if (rdata->prli_count) {
313 mutex_lock(&fc_prov_mutex);
314 for (type = 1; type < FC_FC4_PROV_SIZE; type++) {
315 prov = fc_passive_prov[type];
316 if (prov && prov->prlo)
319 mutex_unlock(&fc_prov_mutex);
321 port_id = rdata->ids.port_id;
322 mutex_unlock(&rdata->rp_mutex);
324 if (rport_ops && rport_ops->event_callback) {
325 FC_RPORT_DBG(rdata, "callback ev %d\n", event);
326 rport_ops->event_callback(lport, rdata, event);
328 if (rdata->lld_event_callback) {
329 FC_RPORT_DBG(rdata, "lld callback ev %d\n", event);
330 rdata->lld_event_callback(lport, rdata, event);
332 cancel_delayed_work_sync(&rdata->retry_work);
335 * Reset any outstanding exchanges before freeing rport.
337 lport->tt.exch_mgr_reset(lport, 0, port_id);
338 lport->tt.exch_mgr_reset(lport, port_id, 0);
341 rpriv = rport->dd_data;
342 rpriv->rp_state = RPORT_ST_DELETE;
343 mutex_lock(&rdata->rp_mutex);
345 mutex_unlock(&rdata->rp_mutex);
346 fc_remote_port_delete(rport);
349 mutex_lock(&rdata->rp_mutex);
350 if (rdata->rp_state == RPORT_ST_DELETE) {
351 if (port_id == FC_FID_DIR_SERV) {
352 rdata->event = RPORT_EV_NONE;
353 mutex_unlock(&rdata->rp_mutex);
354 kref_put(&rdata->kref, lport->tt.rport_destroy);
355 } else if ((rdata->flags & FC_RP_STARTED) &&
356 rdata->major_retries <
357 lport->max_rport_retry_count) {
358 rdata->major_retries++;
359 rdata->event = RPORT_EV_NONE;
360 FC_RPORT_DBG(rdata, "work restart\n");
361 fc_rport_enter_flogi(rdata);
362 mutex_unlock(&rdata->rp_mutex);
364 FC_RPORT_DBG(rdata, "work delete\n");
365 list_del_rcu(&rdata->peers);
366 mutex_unlock(&rdata->rp_mutex);
367 kref_put(&rdata->kref, lport->tt.rport_destroy);
371 * Re-open for events. Reissue READY event if ready.
373 rdata->event = RPORT_EV_NONE;
374 if (rdata->rp_state == RPORT_ST_READY)
375 fc_rport_enter_ready(rdata);
376 mutex_unlock(&rdata->rp_mutex);
381 mutex_unlock(&rdata->rp_mutex);
387 * fc_rport_login() - Start the remote port login state machine
388 * @rdata: The remote port to be logged in to
390 * Locking Note: Called without the rport lock held. This
391 * function will hold the rport lock, call an _enter_*
392 * function and then unlock the rport.
394 * This indicates the intent to be logged into the remote port.
395 * If it appears we are already logged in, ADISC is used to verify
398 static int fc_rport_login(struct fc_rport_priv *rdata)
400 mutex_lock(&rdata->rp_mutex);
402 rdata->flags |= FC_RP_STARTED;
403 switch (rdata->rp_state) {
405 FC_RPORT_DBG(rdata, "ADISC port\n");
406 fc_rport_enter_adisc(rdata);
408 case RPORT_ST_DELETE:
409 FC_RPORT_DBG(rdata, "Restart deleted port\n");
412 FC_RPORT_DBG(rdata, "Login to port\n");
413 fc_rport_enter_flogi(rdata);
416 mutex_unlock(&rdata->rp_mutex);
422 * fc_rport_enter_delete() - Schedule a remote port to be deleted
423 * @rdata: The remote port to be deleted
424 * @event: The event to report as the reason for deletion
426 * Locking Note: Called with the rport lock held.
428 * Allow state change into DELETE only once.
430 * Call queue_work only if there's no event already pending.
431 * Set the new event so that the old pending event will not occur.
432 * Since we have the mutex, even if fc_rport_work() is already started,
433 * it'll see the new event.
435 static void fc_rport_enter_delete(struct fc_rport_priv *rdata,
436 enum fc_rport_event event)
438 if (rdata->rp_state == RPORT_ST_DELETE)
441 FC_RPORT_DBG(rdata, "Delete port\n");
443 fc_rport_state_enter(rdata, RPORT_ST_DELETE);
445 if (rdata->event == RPORT_EV_NONE)
446 queue_work(rport_event_queue, &rdata->event_work);
447 rdata->event = event;
451 * fc_rport_logoff() - Logoff and remove a remote port
452 * @rdata: The remote port to be logged off of
454 * Locking Note: Called without the rport lock held. This
455 * function will hold the rport lock, call an _enter_*
456 * function and then unlock the rport.
458 static int fc_rport_logoff(struct fc_rport_priv *rdata)
460 struct fc_lport *lport = rdata->local_port;
461 u32 port_id = rdata->ids.port_id;
463 mutex_lock(&rdata->rp_mutex);
465 FC_RPORT_DBG(rdata, "Remove port\n");
467 rdata->flags &= ~FC_RP_STARTED;
468 if (rdata->rp_state == RPORT_ST_DELETE) {
469 FC_RPORT_DBG(rdata, "Port in Delete state, not removing\n");
474 * To explicitly Logout, the initiating Nx_Port shall terminate
475 * other open Sequences that it initiated with the destination
476 * Nx_Port prior to performing Logout.
478 lport->tt.exch_mgr_reset(lport, 0, port_id);
479 lport->tt.exch_mgr_reset(lport, port_id, 0);
481 fc_rport_enter_logo(rdata);
484 * Change the state to Delete so that we discard
487 fc_rport_enter_delete(rdata, RPORT_EV_STOP);
489 mutex_unlock(&rdata->rp_mutex);
494 * fc_rport_enter_ready() - Transition to the RPORT_ST_READY state
495 * @rdata: The remote port that is ready
497 * Locking Note: The rport lock is expected to be held before calling
500 static void fc_rport_enter_ready(struct fc_rport_priv *rdata)
502 fc_rport_state_enter(rdata, RPORT_ST_READY);
504 FC_RPORT_DBG(rdata, "Port is Ready\n");
506 if (rdata->event == RPORT_EV_NONE)
507 queue_work(rport_event_queue, &rdata->event_work);
508 rdata->event = RPORT_EV_READY;
512 * fc_rport_timeout() - Handler for the retry_work timer
513 * @work: Handle to the remote port that has timed out
515 * Locking Note: Called without the rport lock held. This
516 * function will hold the rport lock, call an _enter_*
517 * function and then unlock the rport.
519 static void fc_rport_timeout(struct work_struct *work)
521 struct fc_rport_priv *rdata =
522 container_of(work, struct fc_rport_priv, retry_work.work);
524 mutex_lock(&rdata->rp_mutex);
526 switch (rdata->rp_state) {
528 fc_rport_enter_flogi(rdata);
531 fc_rport_enter_plogi(rdata);
534 fc_rport_enter_prli(rdata);
537 fc_rport_enter_rtv(rdata);
540 fc_rport_enter_adisc(rdata);
542 case RPORT_ST_PLOGI_WAIT:
545 case RPORT_ST_DELETE:
549 mutex_unlock(&rdata->rp_mutex);
553 * fc_rport_error() - Error handler, called once retries have been exhausted
554 * @rdata: The remote port the error is happened on
555 * @fp: The error code encapsulated in a frame pointer
557 * Locking Note: The rport lock is expected to be held before
558 * calling this routine
560 static void fc_rport_error(struct fc_rport_priv *rdata, struct fc_frame *fp)
562 struct fc_lport *lport = rdata->local_port;
564 FC_RPORT_DBG(rdata, "Error %ld in state %s, retries %d\n",
565 IS_ERR(fp) ? -PTR_ERR(fp) : 0,
566 fc_rport_state(rdata), rdata->retries);
568 switch (rdata->rp_state) {
570 rdata->flags &= ~FC_RP_STARTED;
571 fc_rport_enter_delete(rdata, RPORT_EV_FAILED);
574 if (lport->point_to_multipoint) {
575 rdata->flags &= ~FC_RP_STARTED;
576 fc_rport_enter_delete(rdata, RPORT_EV_FAILED);
578 fc_rport_enter_logo(rdata);
581 fc_rport_enter_ready(rdata);
585 fc_rport_enter_logo(rdata);
587 case RPORT_ST_PLOGI_WAIT:
588 case RPORT_ST_DELETE:
596 * fc_rport_error_retry() - Handler for remote port state retries
597 * @rdata: The remote port whose state is to be retried
598 * @fp: The error code encapsulated in a frame pointer
600 * If the error was an exchange timeout retry immediately,
601 * otherwise wait for E_D_TOV.
603 * Locking Note: The rport lock is expected to be held before
604 * calling this routine
606 static void fc_rport_error_retry(struct fc_rport_priv *rdata,
609 unsigned long delay = msecs_to_jiffies(FC_DEF_E_D_TOV);
611 /* make sure this isn't an FC_EX_CLOSED error, never retry those */
612 if (PTR_ERR(fp) == -FC_EX_CLOSED)
615 if (rdata->retries < rdata->local_port->max_rport_retry_count) {
616 FC_RPORT_DBG(rdata, "Error %ld in state %s, retrying\n",
617 PTR_ERR(fp), fc_rport_state(rdata));
619 /* no additional delay on exchange timeouts */
620 if (PTR_ERR(fp) == -FC_EX_TIMEOUT)
622 schedule_delayed_work(&rdata->retry_work, delay);
627 fc_rport_error(rdata, fp);
631 * fc_rport_login_complete() - Handle parameters and completion of p-mp login.
632 * @rdata: The remote port which we logged into or which logged into us.
633 * @fp: The FLOGI or PLOGI request or response frame
635 * Returns non-zero error if a problem is detected with the frame.
636 * Does not free the frame.
638 * This is only used in point-to-multipoint mode for FIP currently.
640 static int fc_rport_login_complete(struct fc_rport_priv *rdata,
643 struct fc_lport *lport = rdata->local_port;
644 struct fc_els_flogi *flogi;
645 unsigned int e_d_tov;
648 flogi = fc_frame_payload_get(fp, sizeof(*flogi));
652 csp_flags = ntohs(flogi->fl_csp.sp_features);
654 if (fc_frame_payload_op(fp) == ELS_FLOGI) {
655 if (csp_flags & FC_SP_FT_FPORT) {
656 FC_RPORT_DBG(rdata, "Fabric bit set in FLOGI\n");
662 * E_D_TOV is not valid on an incoming FLOGI request.
664 e_d_tov = ntohl(flogi->fl_csp.sp_e_d_tov);
665 if (csp_flags & FC_SP_FT_EDTR)
667 if (e_d_tov > rdata->e_d_tov)
668 rdata->e_d_tov = e_d_tov;
670 rdata->maxframe_size = fc_plogi_get_maxframe(flogi, lport->mfs);
675 * fc_rport_flogi_resp() - Handle response to FLOGI request for p-mp mode
676 * @sp: The sequence that the FLOGI was on
677 * @fp: The FLOGI response frame
678 * @rp_arg: The remote port that received the FLOGI response
680 static void fc_rport_flogi_resp(struct fc_seq *sp, struct fc_frame *fp,
683 struct fc_rport_priv *rdata = rp_arg;
684 struct fc_lport *lport = rdata->local_port;
685 struct fc_els_flogi *flogi;
686 unsigned int r_a_tov;
688 FC_RPORT_DBG(rdata, "Received a FLOGI %s\n", fc_els_resp_type(fp));
690 if (fp == ERR_PTR(-FC_EX_CLOSED))
693 mutex_lock(&rdata->rp_mutex);
695 if (rdata->rp_state != RPORT_ST_FLOGI) {
696 FC_RPORT_DBG(rdata, "Received a FLOGI response, but in state "
697 "%s\n", fc_rport_state(rdata));
704 fc_rport_error(rdata, fp);
708 if (fc_frame_payload_op(fp) != ELS_LS_ACC)
710 if (fc_rport_login_complete(rdata, fp))
713 flogi = fc_frame_payload_get(fp, sizeof(*flogi));
716 r_a_tov = ntohl(flogi->fl_csp.sp_r_a_tov);
717 if (r_a_tov > rdata->r_a_tov)
718 rdata->r_a_tov = r_a_tov;
720 if (rdata->ids.port_name < lport->wwpn)
721 fc_rport_enter_plogi(rdata);
723 fc_rport_state_enter(rdata, RPORT_ST_PLOGI_WAIT);
727 mutex_unlock(&rdata->rp_mutex);
729 kref_put(&rdata->kref, lport->tt.rport_destroy);
732 FC_RPORT_DBG(rdata, "Bad FLOGI response\n");
733 fc_rport_error_retry(rdata, fp);
738 * fc_rport_enter_flogi() - Send a FLOGI request to the remote port for p-mp
739 * @rdata: The remote port to send a FLOGI to
741 * Locking Note: The rport lock is expected to be held before calling
744 static void fc_rport_enter_flogi(struct fc_rport_priv *rdata)
746 struct fc_lport *lport = rdata->local_port;
749 if (!lport->point_to_multipoint)
750 return fc_rport_enter_plogi(rdata);
752 FC_RPORT_DBG(rdata, "Entered FLOGI state from %s state\n",
753 fc_rport_state(rdata));
755 fc_rport_state_enter(rdata, RPORT_ST_FLOGI);
757 fp = fc_frame_alloc(lport, sizeof(struct fc_els_flogi));
759 return fc_rport_error_retry(rdata, fp);
761 if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_FLOGI,
762 fc_rport_flogi_resp, rdata,
764 fc_rport_error_retry(rdata, NULL);
766 kref_get(&rdata->kref);
770 * fc_rport_recv_flogi_req() - Handle Fabric Login (FLOGI) request in p-mp mode
771 * @lport: The local port that received the PLOGI request
772 * @rx_fp: The PLOGI request frame
774 static void fc_rport_recv_flogi_req(struct fc_lport *lport,
775 struct fc_frame *rx_fp)
777 struct fc_disc *disc;
778 struct fc_els_flogi *flp;
779 struct fc_rport_priv *rdata;
780 struct fc_frame *fp = rx_fp;
781 struct fc_seq_els_data rjt_data;
784 sid = fc_frame_sid(fp);
786 FC_RPORT_ID_DBG(lport, sid, "Received FLOGI request\n");
789 if (!lport->point_to_multipoint) {
790 rjt_data.reason = ELS_RJT_UNSUP;
791 rjt_data.explan = ELS_EXPL_NONE;
795 flp = fc_frame_payload_get(fp, sizeof(*flp));
797 rjt_data.reason = ELS_RJT_LOGIC;
798 rjt_data.explan = ELS_EXPL_INV_LEN;
802 rdata = lport->tt.rport_lookup(lport, sid);
804 rjt_data.reason = ELS_RJT_FIP;
805 rjt_data.explan = ELS_EXPL_NOT_NEIGHBOR;
808 mutex_lock(&rdata->rp_mutex);
810 FC_RPORT_DBG(rdata, "Received FLOGI in %s state\n",
811 fc_rport_state(rdata));
813 switch (rdata->rp_state) {
816 * If received the FLOGI request on RPORT which is INIT state
817 * (means not transition to FLOGI either fc_rport timeout
818 * function didn;t trigger or this end hasn;t received
819 * beacon yet from other end. In that case only, allow RPORT
820 * state machine to continue, otherwise fall through which
821 * causes the code to send reject response.
822 * NOTE; Not checking for FIP->state such as VNMP_UP or
823 * VNMP_CLAIM because if FIP state is not one of those,
824 * RPORT wouldn;t have created and 'rport_lookup' would have
825 * failed anyway in that case.
827 if (lport->point_to_multipoint)
829 case RPORT_ST_DELETE:
830 mutex_unlock(&rdata->rp_mutex);
831 rjt_data.reason = ELS_RJT_FIP;
832 rjt_data.explan = ELS_EXPL_NOT_NEIGHBOR;
835 case RPORT_ST_PLOGI_WAIT:
843 * Set the remote port to be deleted and to then restart.
844 * This queues work to be sure exchanges are reset.
846 fc_rport_enter_delete(rdata, RPORT_EV_LOGO);
847 mutex_unlock(&rdata->rp_mutex);
848 rjt_data.reason = ELS_RJT_BUSY;
849 rjt_data.explan = ELS_EXPL_NONE;
852 if (fc_rport_login_complete(rdata, fp)) {
853 mutex_unlock(&rdata->rp_mutex);
854 rjt_data.reason = ELS_RJT_LOGIC;
855 rjt_data.explan = ELS_EXPL_NONE;
859 fp = fc_frame_alloc(lport, sizeof(*flp));
863 fc_flogi_fill(lport, fp);
864 flp = fc_frame_payload_get(fp, sizeof(*flp));
865 flp->fl_cmd = ELS_LS_ACC;
867 fc_fill_reply_hdr(fp, rx_fp, FC_RCTL_ELS_REP, 0);
868 lport->tt.frame_send(lport, fp);
870 if (rdata->ids.port_name < lport->wwpn)
871 fc_rport_enter_plogi(rdata);
873 fc_rport_state_enter(rdata, RPORT_ST_PLOGI_WAIT);
875 mutex_unlock(&rdata->rp_mutex);
876 kref_put(&rdata->kref, lport->tt.rport_destroy);
877 fc_frame_free(rx_fp);
881 kref_put(&rdata->kref, lport->tt.rport_destroy);
883 lport->tt.seq_els_rsp_send(rx_fp, ELS_LS_RJT, &rjt_data);
884 fc_frame_free(rx_fp);
888 * fc_rport_plogi_resp() - Handler for ELS PLOGI responses
889 * @sp: The sequence the PLOGI is on
890 * @fp: The PLOGI response frame
891 * @rdata_arg: The remote port that sent the PLOGI response
893 * Locking Note: This function will be called without the rport lock
894 * held, but it will lock, call an _enter_* function or fc_rport_error
895 * and then unlock the rport.
897 static void fc_rport_plogi_resp(struct fc_seq *sp, struct fc_frame *fp,
900 struct fc_rport_priv *rdata = rdata_arg;
901 struct fc_lport *lport = rdata->local_port;
902 struct fc_els_flogi *plp = NULL;
907 mutex_lock(&rdata->rp_mutex);
909 FC_RPORT_DBG(rdata, "Received a PLOGI %s\n", fc_els_resp_type(fp));
911 if (rdata->rp_state != RPORT_ST_PLOGI) {
912 FC_RPORT_DBG(rdata, "Received a PLOGI response, but in state "
913 "%s\n", fc_rport_state(rdata));
920 fc_rport_error_retry(rdata, fp);
924 op = fc_frame_payload_op(fp);
925 if (op == ELS_LS_ACC &&
926 (plp = fc_frame_payload_get(fp, sizeof(*plp))) != NULL) {
927 rdata->ids.port_name = get_unaligned_be64(&plp->fl_wwpn);
928 rdata->ids.node_name = get_unaligned_be64(&plp->fl_wwnn);
930 /* save plogi response sp_features for further reference */
931 rdata->sp_features = ntohs(plp->fl_csp.sp_features);
933 if (lport->point_to_multipoint)
934 fc_rport_login_complete(rdata, fp);
935 csp_seq = ntohs(plp->fl_csp.sp_tot_seq);
936 cssp_seq = ntohs(plp->fl_cssp[3 - 1].cp_con_seq);
937 if (cssp_seq < csp_seq)
939 rdata->max_seq = csp_seq;
940 rdata->maxframe_size = fc_plogi_get_maxframe(plp, lport->mfs);
941 fc_rport_enter_prli(rdata);
943 fc_rport_error_retry(rdata, fp);
948 mutex_unlock(&rdata->rp_mutex);
949 kref_put(&rdata->kref, lport->tt.rport_destroy);
953 fc_rport_compatible_roles(struct fc_lport *lport, struct fc_rport_priv *rdata)
955 if (rdata->ids.roles == FC_PORT_ROLE_UNKNOWN)
957 if ((rdata->ids.roles & FC_PORT_ROLE_FCP_TARGET) &&
958 (lport->service_params & FCP_SPPF_INIT_FCN))
960 if ((rdata->ids.roles & FC_PORT_ROLE_FCP_INITIATOR) &&
961 (lport->service_params & FCP_SPPF_TARG_FCN))
967 * fc_rport_enter_plogi() - Send Port Login (PLOGI) request
968 * @rdata: The remote port to send a PLOGI to
970 * Locking Note: The rport lock is expected to be held before calling
973 static void fc_rport_enter_plogi(struct fc_rport_priv *rdata)
975 struct fc_lport *lport = rdata->local_port;
978 if (!fc_rport_compatible_roles(lport, rdata)) {
979 FC_RPORT_DBG(rdata, "PLOGI suppressed for incompatible role\n");
980 fc_rport_state_enter(rdata, RPORT_ST_PLOGI_WAIT);
984 FC_RPORT_DBG(rdata, "Port entered PLOGI state from %s state\n",
985 fc_rport_state(rdata));
987 fc_rport_state_enter(rdata, RPORT_ST_PLOGI);
989 rdata->maxframe_size = FC_MIN_MAX_PAYLOAD;
990 fp = fc_frame_alloc(lport, sizeof(struct fc_els_flogi));
992 FC_RPORT_DBG(rdata, "%s frame alloc failed\n", __func__);
993 fc_rport_error_retry(rdata, fp);
996 rdata->e_d_tov = lport->e_d_tov;
998 if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_PLOGI,
999 fc_rport_plogi_resp, rdata,
1000 2 * lport->r_a_tov))
1001 fc_rport_error_retry(rdata, NULL);
1003 kref_get(&rdata->kref);
1007 * fc_rport_prli_resp() - Process Login (PRLI) response handler
1008 * @sp: The sequence the PRLI response was on
1009 * @fp: The PRLI response frame
1010 * @rdata_arg: The remote port that sent the PRLI response
1012 * Locking Note: This function will be called without the rport lock
1013 * held, but it will lock, call an _enter_* function or fc_rport_error
1014 * and then unlock the rport.
1016 static void fc_rport_prli_resp(struct fc_seq *sp, struct fc_frame *fp,
1019 struct fc_rport_priv *rdata = rdata_arg;
1021 struct fc_els_prli prli;
1022 struct fc_els_spp spp;
1024 struct fc_els_spp temp_spp;
1025 struct fc4_prov *prov;
1026 u32 roles = FC_RPORT_ROLE_UNKNOWN;
1031 mutex_lock(&rdata->rp_mutex);
1033 FC_RPORT_DBG(rdata, "Received a PRLI %s\n", fc_els_resp_type(fp));
1035 if (rdata->rp_state != RPORT_ST_PRLI) {
1036 FC_RPORT_DBG(rdata, "Received a PRLI response, but in state "
1037 "%s\n", fc_rport_state(rdata));
1044 fc_rport_error_retry(rdata, fp);
1048 /* reinitialize remote port roles */
1049 rdata->ids.roles = FC_RPORT_ROLE_UNKNOWN;
1051 op = fc_frame_payload_op(fp);
1052 if (op == ELS_LS_ACC) {
1053 pp = fc_frame_payload_get(fp, sizeof(*pp));
1057 resp_code = (pp->spp.spp_flags & FC_SPP_RESP_MASK);
1058 FC_RPORT_DBG(rdata, "PRLI spp_flags = 0x%x\n",
1060 rdata->spp_type = pp->spp.spp_type;
1061 if (resp_code != FC_SPP_RESP_ACK) {
1062 if (resp_code == FC_SPP_RESP_CONF)
1063 fc_rport_error(rdata, fp);
1065 fc_rport_error_retry(rdata, fp);
1068 if (pp->prli.prli_spp_len < sizeof(pp->spp))
1071 fcp_parm = ntohl(pp->spp.spp_params);
1072 if (fcp_parm & FCP_SPPF_RETRY)
1073 rdata->flags |= FC_RP_FLAGS_RETRY;
1074 if (fcp_parm & FCP_SPPF_CONF_COMPL)
1075 rdata->flags |= FC_RP_FLAGS_CONF_REQ;
1077 prov = fc_passive_prov[FC_TYPE_FCP];
1079 memset(&temp_spp, 0, sizeof(temp_spp));
1080 prov->prli(rdata, pp->prli.prli_spp_len,
1081 &pp->spp, &temp_spp);
1084 rdata->supported_classes = FC_COS_CLASS3;
1085 if (fcp_parm & FCP_SPPF_INIT_FCN)
1086 roles |= FC_RPORT_ROLE_FCP_INITIATOR;
1087 if (fcp_parm & FCP_SPPF_TARG_FCN)
1088 roles |= FC_RPORT_ROLE_FCP_TARGET;
1090 rdata->ids.roles = roles;
1091 fc_rport_enter_rtv(rdata);
1094 FC_RPORT_DBG(rdata, "Bad ELS response for PRLI command\n");
1095 fc_rport_error_retry(rdata, fp);
1101 mutex_unlock(&rdata->rp_mutex);
1102 kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
1106 * fc_rport_enter_prli() - Send Process Login (PRLI) request
1107 * @rdata: The remote port to send the PRLI request to
1109 * Locking Note: The rport lock is expected to be held before calling
1112 static void fc_rport_enter_prli(struct fc_rport_priv *rdata)
1114 struct fc_lport *lport = rdata->local_port;
1116 struct fc_els_prli prli;
1117 struct fc_els_spp spp;
1119 struct fc_frame *fp;
1120 struct fc4_prov *prov;
1123 * If the rport is one of the well known addresses
1124 * we skip PRLI and RTV and go straight to READY.
1126 if (rdata->ids.port_id >= FC_FID_DOM_MGR) {
1127 fc_rport_enter_ready(rdata);
1131 FC_RPORT_DBG(rdata, "Port entered PRLI state from %s state\n",
1132 fc_rport_state(rdata));
1134 fc_rport_state_enter(rdata, RPORT_ST_PRLI);
1136 fp = fc_frame_alloc(lport, sizeof(*pp));
1138 fc_rport_error_retry(rdata, fp);
1142 fc_prli_fill(lport, fp);
1144 prov = fc_passive_prov[FC_TYPE_FCP];
1146 pp = fc_frame_payload_get(fp, sizeof(*pp));
1147 prov->prli(rdata, sizeof(pp->spp), NULL, &pp->spp);
1150 fc_fill_fc_hdr(fp, FC_RCTL_ELS_REQ, rdata->ids.port_id,
1151 fc_host_port_id(lport->host), FC_TYPE_ELS,
1152 FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT, 0);
1154 if (!lport->tt.exch_seq_send(lport, fp, fc_rport_prli_resp,
1155 NULL, rdata, 2 * lport->r_a_tov))
1156 fc_rport_error_retry(rdata, NULL);
1158 kref_get(&rdata->kref);
1162 * fc_rport_els_rtv_resp() - Handler for Request Timeout Value (RTV) responses
1163 * @sp: The sequence the RTV was on
1164 * @fp: The RTV response frame
1165 * @rdata_arg: The remote port that sent the RTV response
1167 * Many targets don't seem to support this.
1169 * Locking Note: This function will be called without the rport lock
1170 * held, but it will lock, call an _enter_* function or fc_rport_error
1171 * and then unlock the rport.
1173 static void fc_rport_rtv_resp(struct fc_seq *sp, struct fc_frame *fp,
1176 struct fc_rport_priv *rdata = rdata_arg;
1179 mutex_lock(&rdata->rp_mutex);
1181 FC_RPORT_DBG(rdata, "Received a RTV %s\n", fc_els_resp_type(fp));
1183 if (rdata->rp_state != RPORT_ST_RTV) {
1184 FC_RPORT_DBG(rdata, "Received a RTV response, but in state "
1185 "%s\n", fc_rport_state(rdata));
1192 fc_rport_error(rdata, fp);
1196 op = fc_frame_payload_op(fp);
1197 if (op == ELS_LS_ACC) {
1198 struct fc_els_rtv_acc *rtv;
1202 rtv = fc_frame_payload_get(fp, sizeof(*rtv));
1204 toq = ntohl(rtv->rtv_toq);
1205 tov = ntohl(rtv->rtv_r_a_tov);
1208 rdata->r_a_tov = tov;
1209 tov = ntohl(rtv->rtv_e_d_tov);
1210 if (toq & FC_ELS_RTV_EDRES)
1214 rdata->e_d_tov = tov;
1218 fc_rport_enter_ready(rdata);
1223 mutex_unlock(&rdata->rp_mutex);
1224 kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
1228 * fc_rport_enter_rtv() - Send Request Timeout Value (RTV) request
1229 * @rdata: The remote port to send the RTV request to
1231 * Locking Note: The rport lock is expected to be held before calling
1234 static void fc_rport_enter_rtv(struct fc_rport_priv *rdata)
1236 struct fc_frame *fp;
1237 struct fc_lport *lport = rdata->local_port;
1239 FC_RPORT_DBG(rdata, "Port entered RTV state from %s state\n",
1240 fc_rport_state(rdata));
1242 fc_rport_state_enter(rdata, RPORT_ST_RTV);
1244 fp = fc_frame_alloc(lport, sizeof(struct fc_els_rtv));
1246 fc_rport_error_retry(rdata, fp);
1250 if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_RTV,
1251 fc_rport_rtv_resp, rdata,
1252 2 * lport->r_a_tov))
1253 fc_rport_error_retry(rdata, NULL);
1255 kref_get(&rdata->kref);
1259 * fc_rport_logo_resp() - Handler for logout (LOGO) responses
1260 * @sp: The sequence the LOGO was on
1261 * @fp: The LOGO response frame
1262 * @lport_arg: The local port
1264 static void fc_rport_logo_resp(struct fc_seq *sp, struct fc_frame *fp,
1267 struct fc_lport *lport = lport_arg;
1269 FC_RPORT_ID_DBG(lport, fc_seq_exch(sp)->did,
1270 "Received a LOGO %s\n", fc_els_resp_type(fp));
1277 * fc_rport_enter_logo() - Send a logout (LOGO) request
1278 * @rdata: The remote port to send the LOGO request to
1280 * Locking Note: The rport lock is expected to be held before calling
1283 static void fc_rport_enter_logo(struct fc_rport_priv *rdata)
1285 struct fc_lport *lport = rdata->local_port;
1286 struct fc_frame *fp;
1288 FC_RPORT_DBG(rdata, "Port sending LOGO from %s state\n",
1289 fc_rport_state(rdata));
1291 fp = fc_frame_alloc(lport, sizeof(struct fc_els_logo));
1294 (void)lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_LOGO,
1295 fc_rport_logo_resp, lport, 0);
1299 * fc_rport_els_adisc_resp() - Handler for Address Discovery (ADISC) responses
1300 * @sp: The sequence the ADISC response was on
1301 * @fp: The ADISC response frame
1302 * @rdata_arg: The remote port that sent the ADISC response
1304 * Locking Note: This function will be called without the rport lock
1305 * held, but it will lock, call an _enter_* function or fc_rport_error
1306 * and then unlock the rport.
1308 static void fc_rport_adisc_resp(struct fc_seq *sp, struct fc_frame *fp,
1311 struct fc_rport_priv *rdata = rdata_arg;
1312 struct fc_els_adisc *adisc;
1315 mutex_lock(&rdata->rp_mutex);
1317 FC_RPORT_DBG(rdata, "Received a ADISC response\n");
1319 if (rdata->rp_state != RPORT_ST_ADISC) {
1320 FC_RPORT_DBG(rdata, "Received a ADISC resp but in state %s\n",
1321 fc_rport_state(rdata));
1328 fc_rport_error(rdata, fp);
1333 * If address verification failed. Consider us logged out of the rport.
1334 * Since the rport is still in discovery, we want to be
1335 * logged in, so go to PLOGI state. Otherwise, go back to READY.
1337 op = fc_frame_payload_op(fp);
1338 adisc = fc_frame_payload_get(fp, sizeof(*adisc));
1339 if (op != ELS_LS_ACC || !adisc ||
1340 ntoh24(adisc->adisc_port_id) != rdata->ids.port_id ||
1341 get_unaligned_be64(&adisc->adisc_wwpn) != rdata->ids.port_name ||
1342 get_unaligned_be64(&adisc->adisc_wwnn) != rdata->ids.node_name) {
1343 FC_RPORT_DBG(rdata, "ADISC error or mismatch\n");
1344 fc_rport_enter_flogi(rdata);
1346 FC_RPORT_DBG(rdata, "ADISC OK\n");
1347 fc_rport_enter_ready(rdata);
1352 mutex_unlock(&rdata->rp_mutex);
1353 kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
1357 * fc_rport_enter_adisc() - Send Address Discover (ADISC) request
1358 * @rdata: The remote port to send the ADISC request to
1360 * Locking Note: The rport lock is expected to be held before calling
1363 static void fc_rport_enter_adisc(struct fc_rport_priv *rdata)
1365 struct fc_lport *lport = rdata->local_port;
1366 struct fc_frame *fp;
1368 FC_RPORT_DBG(rdata, "sending ADISC from %s state\n",
1369 fc_rport_state(rdata));
1371 fc_rport_state_enter(rdata, RPORT_ST_ADISC);
1373 fp = fc_frame_alloc(lport, sizeof(struct fc_els_adisc));
1375 fc_rport_error_retry(rdata, fp);
1378 if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_ADISC,
1379 fc_rport_adisc_resp, rdata,
1380 2 * lport->r_a_tov))
1381 fc_rport_error_retry(rdata, NULL);
1383 kref_get(&rdata->kref);
1387 * fc_rport_recv_adisc_req() - Handler for Address Discovery (ADISC) requests
1388 * @rdata: The remote port that sent the ADISC request
1389 * @in_fp: The ADISC request frame
1391 * Locking Note: Called with the lport and rport locks held.
1393 static void fc_rport_recv_adisc_req(struct fc_rport_priv *rdata,
1394 struct fc_frame *in_fp)
1396 struct fc_lport *lport = rdata->local_port;
1397 struct fc_frame *fp;
1398 struct fc_els_adisc *adisc;
1399 struct fc_seq_els_data rjt_data;
1401 FC_RPORT_DBG(rdata, "Received ADISC request\n");
1403 adisc = fc_frame_payload_get(in_fp, sizeof(*adisc));
1405 rjt_data.reason = ELS_RJT_PROT;
1406 rjt_data.explan = ELS_EXPL_INV_LEN;
1407 lport->tt.seq_els_rsp_send(in_fp, ELS_LS_RJT, &rjt_data);
1411 fp = fc_frame_alloc(lport, sizeof(*adisc));
1414 fc_adisc_fill(lport, fp);
1415 adisc = fc_frame_payload_get(fp, sizeof(*adisc));
1416 adisc->adisc_cmd = ELS_LS_ACC;
1417 fc_fill_reply_hdr(fp, in_fp, FC_RCTL_ELS_REP, 0);
1418 lport->tt.frame_send(lport, fp);
1420 fc_frame_free(in_fp);
1424 * fc_rport_recv_rls_req() - Handle received Read Link Status request
1425 * @rdata: The remote port that sent the RLS request
1426 * @rx_fp: The PRLI request frame
1428 * Locking Note: The rport lock is expected to be held before calling
1431 static void fc_rport_recv_rls_req(struct fc_rport_priv *rdata,
1432 struct fc_frame *rx_fp)
1435 struct fc_lport *lport = rdata->local_port;
1436 struct fc_frame *fp;
1437 struct fc_els_rls *rls;
1438 struct fc_els_rls_resp *rsp;
1439 struct fc_els_lesb *lesb;
1440 struct fc_seq_els_data rjt_data;
1441 struct fc_host_statistics *hst;
1443 FC_RPORT_DBG(rdata, "Received RLS request while in state %s\n",
1444 fc_rport_state(rdata));
1446 rls = fc_frame_payload_get(rx_fp, sizeof(*rls));
1448 rjt_data.reason = ELS_RJT_PROT;
1449 rjt_data.explan = ELS_EXPL_INV_LEN;
1453 fp = fc_frame_alloc(lport, sizeof(*rsp));
1455 rjt_data.reason = ELS_RJT_UNAB;
1456 rjt_data.explan = ELS_EXPL_INSUF_RES;
1460 rsp = fc_frame_payload_get(fp, sizeof(*rsp));
1461 memset(rsp, 0, sizeof(*rsp));
1462 rsp->rls_cmd = ELS_LS_ACC;
1463 lesb = &rsp->rls_lesb;
1464 if (lport->tt.get_lesb) {
1465 /* get LESB from LLD if it supports it */
1466 lport->tt.get_lesb(lport, lesb);
1468 fc_get_host_stats(lport->host);
1469 hst = &lport->host_stats;
1470 lesb->lesb_link_fail = htonl(hst->link_failure_count);
1471 lesb->lesb_sync_loss = htonl(hst->loss_of_sync_count);
1472 lesb->lesb_sig_loss = htonl(hst->loss_of_signal_count);
1473 lesb->lesb_prim_err = htonl(hst->prim_seq_protocol_err_count);
1474 lesb->lesb_inv_word = htonl(hst->invalid_tx_word_count);
1475 lesb->lesb_inv_crc = htonl(hst->invalid_crc_count);
1478 fc_fill_reply_hdr(fp, rx_fp, FC_RCTL_ELS_REP, 0);
1479 lport->tt.frame_send(lport, fp);
1483 lport->tt.seq_els_rsp_send(rx_fp, ELS_LS_RJT, &rjt_data);
1485 fc_frame_free(rx_fp);
1489 * fc_rport_recv_els_req() - Handler for validated ELS requests
1490 * @lport: The local port that received the ELS request
1491 * @fp: The ELS request frame
1493 * Handle incoming ELS requests that require port login.
1494 * The ELS opcode has already been validated by the caller.
1496 * Locking Note: Called with the lport lock held.
1498 static void fc_rport_recv_els_req(struct fc_lport *lport, struct fc_frame *fp)
1500 struct fc_rport_priv *rdata;
1501 struct fc_seq_els_data els_data;
1503 rdata = lport->tt.rport_lookup(lport, fc_frame_sid(fp));
1507 mutex_lock(&rdata->rp_mutex);
1509 switch (rdata->rp_state) {
1512 case RPORT_ST_READY:
1513 case RPORT_ST_ADISC:
1516 mutex_unlock(&rdata->rp_mutex);
1517 kref_put(&rdata->kref, lport->tt.rport_destroy);
1521 switch (fc_frame_payload_op(fp)) {
1523 fc_rport_recv_prli_req(rdata, fp);
1526 fc_rport_recv_prlo_req(rdata, fp);
1529 fc_rport_recv_adisc_req(rdata, fp);
1532 lport->tt.seq_els_rsp_send(fp, ELS_RRQ, NULL);
1536 lport->tt.seq_els_rsp_send(fp, ELS_REC, NULL);
1540 fc_rport_recv_rls_req(rdata, fp);
1543 fc_frame_free(fp); /* can't happen */
1547 mutex_unlock(&rdata->rp_mutex);
1548 kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
1552 els_data.reason = ELS_RJT_UNAB;
1553 els_data.explan = ELS_EXPL_PLOGI_REQD;
1554 lport->tt.seq_els_rsp_send(fp, ELS_LS_RJT, &els_data);
1559 * fc_rport_recv_req() - Handler for requests
1560 * @lport: The local port that received the request
1561 * @fp: The request frame
1563 * Locking Note: Called with the lport lock held.
1565 static void fc_rport_recv_req(struct fc_lport *lport, struct fc_frame *fp)
1567 struct fc_seq_els_data els_data;
1570 * Handle FLOGI, PLOGI and LOGO requests separately, since they
1571 * don't require prior login.
1572 * Check for unsupported opcodes first and reject them.
1573 * For some ops, it would be incorrect to reject with "PLOGI required".
1575 switch (fc_frame_payload_op(fp)) {
1577 fc_rport_recv_flogi_req(lport, fp);
1580 fc_rport_recv_plogi_req(lport, fp);
1583 fc_rport_recv_logo_req(lport, fp);
1591 fc_rport_recv_els_req(lport, fp);
1594 els_data.reason = ELS_RJT_UNSUP;
1595 els_data.explan = ELS_EXPL_NONE;
1596 lport->tt.seq_els_rsp_send(fp, ELS_LS_RJT, &els_data);
1603 * fc_rport_recv_plogi_req() - Handler for Port Login (PLOGI) requests
1604 * @lport: The local port that received the PLOGI request
1605 * @rx_fp: The PLOGI request frame
1607 * Locking Note: The rport lock is held before calling this function.
1609 static void fc_rport_recv_plogi_req(struct fc_lport *lport,
1610 struct fc_frame *rx_fp)
1612 struct fc_disc *disc;
1613 struct fc_rport_priv *rdata;
1614 struct fc_frame *fp = rx_fp;
1615 struct fc_els_flogi *pl;
1616 struct fc_seq_els_data rjt_data;
1619 sid = fc_frame_sid(fp);
1621 FC_RPORT_ID_DBG(lport, sid, "Received PLOGI request\n");
1623 pl = fc_frame_payload_get(fp, sizeof(*pl));
1625 FC_RPORT_ID_DBG(lport, sid, "Received PLOGI too short\n");
1626 rjt_data.reason = ELS_RJT_PROT;
1627 rjt_data.explan = ELS_EXPL_INV_LEN;
1631 disc = &lport->disc;
1632 mutex_lock(&disc->disc_mutex);
1633 rdata = lport->tt.rport_create(lport, sid);
1635 mutex_unlock(&disc->disc_mutex);
1636 rjt_data.reason = ELS_RJT_UNAB;
1637 rjt_data.explan = ELS_EXPL_INSUF_RES;
1641 mutex_lock(&rdata->rp_mutex);
1642 mutex_unlock(&disc->disc_mutex);
1644 rdata->ids.port_name = get_unaligned_be64(&pl->fl_wwpn);
1645 rdata->ids.node_name = get_unaligned_be64(&pl->fl_wwnn);
1648 * If the rport was just created, possibly due to the incoming PLOGI,
1649 * set the state appropriately and accept the PLOGI.
1651 * If we had also sent a PLOGI, and if the received PLOGI is from a
1652 * higher WWPN, we accept it, otherwise an LS_RJT is sent with reason
1653 * "command already in progress".
1655 * XXX TBD: If the session was ready before, the PLOGI should result in
1656 * all outstanding exchanges being reset.
1658 switch (rdata->rp_state) {
1660 FC_RPORT_DBG(rdata, "Received PLOGI in INIT state\n");
1662 case RPORT_ST_PLOGI_WAIT:
1663 FC_RPORT_DBG(rdata, "Received PLOGI in PLOGI_WAIT state\n");
1665 case RPORT_ST_PLOGI:
1666 FC_RPORT_DBG(rdata, "Received PLOGI in PLOGI state\n");
1667 if (rdata->ids.port_name < lport->wwpn) {
1668 mutex_unlock(&rdata->rp_mutex);
1669 rjt_data.reason = ELS_RJT_INPROG;
1670 rjt_data.explan = ELS_EXPL_NONE;
1676 case RPORT_ST_READY:
1677 case RPORT_ST_ADISC:
1678 FC_RPORT_DBG(rdata, "Received PLOGI in logged-in state %d "
1679 "- ignored for now\n", rdata->rp_state);
1680 /* XXX TBD - should reset */
1682 case RPORT_ST_FLOGI:
1683 case RPORT_ST_DELETE:
1684 FC_RPORT_DBG(rdata, "Received PLOGI in state %s - send busy\n",
1685 fc_rport_state(rdata));
1686 mutex_unlock(&rdata->rp_mutex);
1687 rjt_data.reason = ELS_RJT_BUSY;
1688 rjt_data.explan = ELS_EXPL_NONE;
1691 if (!fc_rport_compatible_roles(lport, rdata)) {
1692 FC_RPORT_DBG(rdata, "Received PLOGI for incompatible role\n");
1693 mutex_unlock(&rdata->rp_mutex);
1694 rjt_data.reason = ELS_RJT_LOGIC;
1695 rjt_data.explan = ELS_EXPL_NONE;
1700 * Get session payload size from incoming PLOGI.
1702 rdata->maxframe_size = fc_plogi_get_maxframe(pl, lport->mfs);
1705 * Send LS_ACC. If this fails, the originator should retry.
1707 fp = fc_frame_alloc(lport, sizeof(*pl));
1711 fc_plogi_fill(lport, fp, ELS_LS_ACC);
1712 fc_fill_reply_hdr(fp, rx_fp, FC_RCTL_ELS_REP, 0);
1713 lport->tt.frame_send(lport, fp);
1714 fc_rport_enter_prli(rdata);
1716 mutex_unlock(&rdata->rp_mutex);
1717 fc_frame_free(rx_fp);
1721 lport->tt.seq_els_rsp_send(fp, ELS_LS_RJT, &rjt_data);
1726 * fc_rport_recv_prli_req() - Handler for process login (PRLI) requests
1727 * @rdata: The remote port that sent the PRLI request
1728 * @rx_fp: The PRLI request frame
1730 * Locking Note: The rport lock is expected to be held before calling
1733 static void fc_rport_recv_prli_req(struct fc_rport_priv *rdata,
1734 struct fc_frame *rx_fp)
1736 struct fc_lport *lport = rdata->local_port;
1737 struct fc_frame *fp;
1739 struct fc_els_prli prli;
1740 struct fc_els_spp spp;
1742 struct fc_els_spp *rspp; /* request service param page */
1743 struct fc_els_spp *spp; /* response spp */
1746 enum fc_els_spp_resp resp;
1747 enum fc_els_spp_resp passive;
1748 struct fc_seq_els_data rjt_data;
1749 struct fc4_prov *prov;
1751 FC_RPORT_DBG(rdata, "Received PRLI request while in state %s\n",
1752 fc_rport_state(rdata));
1754 len = fr_len(rx_fp) - sizeof(struct fc_frame_header);
1755 pp = fc_frame_payload_get(rx_fp, sizeof(*pp));
1758 plen = ntohs(pp->prli.prli_len);
1759 if ((plen % 4) != 0 || plen > len || plen < 16)
1763 plen = pp->prli.prli_spp_len;
1764 if ((plen % 4) != 0 || plen < sizeof(*spp) ||
1765 plen > len || len < sizeof(*pp) || plen < 12)
1769 fp = fc_frame_alloc(lport, len);
1771 rjt_data.reason = ELS_RJT_UNAB;
1772 rjt_data.explan = ELS_EXPL_INSUF_RES;
1775 pp = fc_frame_payload_get(fp, len);
1778 pp->prli.prli_cmd = ELS_LS_ACC;
1779 pp->prli.prli_spp_len = plen;
1780 pp->prli.prli_len = htons(len);
1781 len -= sizeof(struct fc_els_prli);
1784 * Go through all the service parameter pages and build
1785 * response. If plen indicates longer SPP than standard,
1786 * use that. The entire response has been pre-cleared above.
1789 mutex_lock(&fc_prov_mutex);
1790 while (len >= plen) {
1791 rdata->spp_type = rspp->spp_type;
1792 spp->spp_type = rspp->spp_type;
1793 spp->spp_type_ext = rspp->spp_type_ext;
1796 if (rspp->spp_type < FC_FC4_PROV_SIZE) {
1797 prov = fc_active_prov[rspp->spp_type];
1799 resp = prov->prli(rdata, plen, rspp, spp);
1800 prov = fc_passive_prov[rspp->spp_type];
1802 passive = prov->prli(rdata, plen, rspp, spp);
1803 if (!resp || passive == FC_SPP_RESP_ACK)
1808 if (spp->spp_flags & FC_SPP_EST_IMG_PAIR)
1809 resp |= FC_SPP_RESP_CONF;
1811 resp |= FC_SPP_RESP_INVL;
1813 spp->spp_flags |= resp;
1815 rspp = (struct fc_els_spp *)((char *)rspp + plen);
1816 spp = (struct fc_els_spp *)((char *)spp + plen);
1818 mutex_unlock(&fc_prov_mutex);
1821 * Send LS_ACC. If this fails, the originator should retry.
1823 fc_fill_reply_hdr(fp, rx_fp, FC_RCTL_ELS_REP, 0);
1824 lport->tt.frame_send(lport, fp);
1826 switch (rdata->rp_state) {
1828 fc_rport_enter_ready(rdata);
1836 rjt_data.reason = ELS_RJT_PROT;
1837 rjt_data.explan = ELS_EXPL_INV_LEN;
1839 lport->tt.seq_els_rsp_send(rx_fp, ELS_LS_RJT, &rjt_data);
1841 fc_frame_free(rx_fp);
1845 * fc_rport_recv_prlo_req() - Handler for process logout (PRLO) requests
1846 * @rdata: The remote port that sent the PRLO request
1847 * @rx_fp: The PRLO request frame
1849 * Locking Note: The rport lock is expected to be held before calling
1852 static void fc_rport_recv_prlo_req(struct fc_rport_priv *rdata,
1853 struct fc_frame *rx_fp)
1855 struct fc_lport *lport = rdata->local_port;
1856 struct fc_frame *fp;
1858 struct fc_els_prlo prlo;
1859 struct fc_els_spp spp;
1861 struct fc_els_spp *rspp; /* request service param page */
1862 struct fc_els_spp *spp; /* response spp */
1865 struct fc_seq_els_data rjt_data;
1867 FC_RPORT_DBG(rdata, "Received PRLO request while in state %s\n",
1868 fc_rport_state(rdata));
1870 len = fr_len(rx_fp) - sizeof(struct fc_frame_header);
1871 pp = fc_frame_payload_get(rx_fp, sizeof(*pp));
1874 plen = ntohs(pp->prlo.prlo_len);
1882 fp = fc_frame_alloc(lport, len);
1884 rjt_data.reason = ELS_RJT_UNAB;
1885 rjt_data.explan = ELS_EXPL_INSUF_RES;
1889 pp = fc_frame_payload_get(fp, len);
1892 pp->prlo.prlo_cmd = ELS_LS_ACC;
1893 pp->prlo.prlo_obs = 0x10;
1894 pp->prlo.prlo_len = htons(len);
1896 spp->spp_type = rspp->spp_type;
1897 spp->spp_type_ext = rspp->spp_type_ext;
1898 spp->spp_flags = FC_SPP_RESP_ACK;
1900 fc_rport_enter_prli(rdata);
1902 fc_fill_reply_hdr(fp, rx_fp, FC_RCTL_ELS_REP, 0);
1903 lport->tt.frame_send(lport, fp);
1907 rjt_data.reason = ELS_RJT_PROT;
1908 rjt_data.explan = ELS_EXPL_INV_LEN;
1910 lport->tt.seq_els_rsp_send(rx_fp, ELS_LS_RJT, &rjt_data);
1912 fc_frame_free(rx_fp);
1916 * fc_rport_recv_logo_req() - Handler for logout (LOGO) requests
1917 * @lport: The local port that received the LOGO request
1918 * @fp: The LOGO request frame
1920 * Locking Note: The rport lock is expected to be held before calling
1923 static void fc_rport_recv_logo_req(struct fc_lport *lport, struct fc_frame *fp)
1925 struct fc_rport_priv *rdata;
1928 lport->tt.seq_els_rsp_send(fp, ELS_LS_ACC, NULL);
1930 sid = fc_frame_sid(fp);
1932 rdata = lport->tt.rport_lookup(lport, sid);
1934 mutex_lock(&rdata->rp_mutex);
1935 FC_RPORT_DBG(rdata, "Received LOGO request while in state %s\n",
1936 fc_rport_state(rdata));
1938 fc_rport_enter_delete(rdata, RPORT_EV_STOP);
1939 mutex_unlock(&rdata->rp_mutex);
1940 kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
1942 FC_RPORT_ID_DBG(lport, sid,
1943 "Received LOGO from non-logged-in port\n");
1948 * fc_rport_flush_queue() - Flush the rport_event_queue
1950 static void fc_rport_flush_queue(void)
1952 flush_workqueue(rport_event_queue);
1956 * fc_rport_init() - Initialize the remote port layer for a local port
1957 * @lport: The local port to initialize the remote port layer for
1959 int fc_rport_init(struct fc_lport *lport)
1961 if (!lport->tt.rport_lookup)
1962 lport->tt.rport_lookup = fc_rport_lookup;
1964 if (!lport->tt.rport_create)
1965 lport->tt.rport_create = fc_rport_create;
1967 if (!lport->tt.rport_login)
1968 lport->tt.rport_login = fc_rport_login;
1970 if (!lport->tt.rport_logoff)
1971 lport->tt.rport_logoff = fc_rport_logoff;
1973 if (!lport->tt.rport_recv_req)
1974 lport->tt.rport_recv_req = fc_rport_recv_req;
1976 if (!lport->tt.rport_flush_queue)
1977 lport->tt.rport_flush_queue = fc_rport_flush_queue;
1979 if (!lport->tt.rport_destroy)
1980 lport->tt.rport_destroy = fc_rport_destroy;
1984 EXPORT_SYMBOL(fc_rport_init);
1987 * fc_rport_fcp_prli() - Handle incoming PRLI for the FCP initiator.
1988 * @rdata: remote port private
1989 * @spp_len: service parameter page length
1990 * @rspp: received service parameter page
1991 * @spp: response service parameter page
1993 * Returns the value for the response code to be placed in spp_flags;
1994 * Returns 0 if not an initiator.
1996 static int fc_rport_fcp_prli(struct fc_rport_priv *rdata, u32 spp_len,
1997 const struct fc_els_spp *rspp,
1998 struct fc_els_spp *spp)
2000 struct fc_lport *lport = rdata->local_port;
2003 fcp_parm = ntohl(rspp->spp_params);
2004 rdata->ids.roles = FC_RPORT_ROLE_UNKNOWN;
2005 if (fcp_parm & FCP_SPPF_INIT_FCN)
2006 rdata->ids.roles |= FC_RPORT_ROLE_FCP_INITIATOR;
2007 if (fcp_parm & FCP_SPPF_TARG_FCN)
2008 rdata->ids.roles |= FC_RPORT_ROLE_FCP_TARGET;
2009 if (fcp_parm & FCP_SPPF_RETRY)
2010 rdata->flags |= FC_RP_FLAGS_RETRY;
2011 rdata->supported_classes = FC_COS_CLASS3;
2013 if (!(lport->service_params & FCP_SPPF_INIT_FCN))
2016 spp->spp_flags |= rspp->spp_flags & FC_SPP_EST_IMG_PAIR;
2019 * OR in our service parameters with other providers (target), if any.
2021 fcp_parm = ntohl(spp->spp_params);
2022 spp->spp_params = htonl(fcp_parm | lport->service_params);
2023 return FC_SPP_RESP_ACK;
2027 * FC-4 provider ops for FCP initiator.
2029 struct fc4_prov fc_rport_fcp_init = {
2030 .prli = fc_rport_fcp_prli,
2034 * fc_rport_t0_prli() - Handle incoming PRLI parameters for type 0
2035 * @rdata: remote port private
2036 * @spp_len: service parameter page length
2037 * @rspp: received service parameter page
2038 * @spp: response service parameter page
2040 static int fc_rport_t0_prli(struct fc_rport_priv *rdata, u32 spp_len,
2041 const struct fc_els_spp *rspp,
2042 struct fc_els_spp *spp)
2044 if (rspp->spp_flags & FC_SPP_EST_IMG_PAIR)
2045 return FC_SPP_RESP_INVL;
2046 return FC_SPP_RESP_ACK;
2050 * FC-4 provider ops for type 0 service parameters.
2052 * This handles the special case of type 0 which is always successful
2053 * but doesn't do anything otherwise.
2055 struct fc4_prov fc_rport_t0_prov = {
2056 .prli = fc_rport_t0_prli,
2060 * fc_setup_rport() - Initialize the rport_event_queue
2062 int fc_setup_rport(void)
2064 rport_event_queue = create_singlethread_workqueue("fc_rport_eq");
2065 if (!rport_event_queue)
2071 * fc_destroy_rport() - Destroy the rport_event_queue
2073 void fc_destroy_rport(void)
2075 destroy_workqueue(rport_event_queue);
2079 * fc_rport_terminate_io() - Stop all outstanding I/O on a remote port
2080 * @rport: The remote port whose I/O should be terminated
2082 void fc_rport_terminate_io(struct fc_rport *rport)
2084 struct fc_rport_libfc_priv *rpriv = rport->dd_data;
2085 struct fc_lport *lport = rpriv->local_port;
2087 lport->tt.exch_mgr_reset(lport, 0, rport->port_id);
2088 lport->tt.exch_mgr_reset(lport, rport->port_id, 0);
2090 EXPORT_SYMBOL(fc_rport_terminate_io);