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 caller must hold either disc_mutex or rcu_read_lock().
100 static struct fc_rport_priv *fc_rport_lookup(const struct fc_lport *lport,
103 struct fc_rport_priv *rdata;
105 list_for_each_entry_rcu(rdata, &lport->disc.rports, peers)
106 if (rdata->ids.port_id == port_id)
112 * fc_rport_create() - Create a new remote port
113 * @lport: The local port this remote port will be associated with
114 * @ids: The identifiers for the new remote port
116 * The remote port will start in the INIT state.
118 * Locking note: must be called with the disc_mutex held.
120 static struct fc_rport_priv *fc_rport_create(struct fc_lport *lport,
123 struct fc_rport_priv *rdata;
125 rdata = lport->tt.rport_lookup(lport, port_id);
129 rdata = kzalloc(sizeof(*rdata) + lport->rport_priv_size, GFP_KERNEL);
133 rdata->ids.node_name = -1;
134 rdata->ids.port_name = -1;
135 rdata->ids.port_id = port_id;
136 rdata->ids.roles = FC_RPORT_ROLE_UNKNOWN;
138 kref_init(&rdata->kref);
139 mutex_init(&rdata->rp_mutex);
140 rdata->local_port = lport;
141 rdata->rp_state = RPORT_ST_INIT;
142 rdata->event = RPORT_EV_NONE;
143 rdata->flags = FC_RP_FLAGS_REC_SUPPORTED;
144 rdata->e_d_tov = lport->e_d_tov;
145 rdata->r_a_tov = lport->r_a_tov;
146 rdata->maxframe_size = FC_MIN_MAX_PAYLOAD;
147 INIT_DELAYED_WORK(&rdata->retry_work, fc_rport_timeout);
148 INIT_WORK(&rdata->event_work, fc_rport_work);
149 if (port_id != FC_FID_DIR_SERV) {
150 rdata->lld_event_callback = lport->tt.rport_event_callback;
151 list_add_rcu(&rdata->peers, &lport->disc.rports);
157 * fc_rport_destroy() - Free a remote port after last reference is released
158 * @kref: The remote port's kref
160 static void fc_rport_destroy(struct kref *kref)
162 struct fc_rport_priv *rdata;
164 rdata = container_of(kref, struct fc_rport_priv, kref);
165 kfree_rcu(rdata, rcu);
169 * fc_rport_state() - Return a string identifying the remote port's state
170 * @rdata: The remote port
172 static const char *fc_rport_state(struct fc_rport_priv *rdata)
176 cp = fc_rport_state_names[rdata->rp_state];
183 * fc_set_rport_loss_tmo() - Set the remote port loss timeout
184 * @rport: The remote port that gets a new timeout value
185 * @timeout: The new timeout value (in seconds)
187 void fc_set_rport_loss_tmo(struct fc_rport *rport, u32 timeout)
190 rport->dev_loss_tmo = timeout;
192 rport->dev_loss_tmo = 1;
194 EXPORT_SYMBOL(fc_set_rport_loss_tmo);
197 * fc_plogi_get_maxframe() - Get the maximum payload from the common service
198 * parameters in a FLOGI frame
199 * @flp: The FLOGI or PLOGI payload
200 * @maxval: The maximum frame size upper limit; this may be less than what
201 * is in the service parameters
203 static unsigned int fc_plogi_get_maxframe(struct fc_els_flogi *flp,
209 * Get max payload from the common service parameters and the
210 * class 3 receive data field size.
212 mfs = ntohs(flp->fl_csp.sp_bb_data) & FC_SP_BB_DATA_MASK;
213 if (mfs >= FC_SP_MIN_MAX_PAYLOAD && mfs < maxval)
215 mfs = ntohs(flp->fl_cssp[3 - 1].cp_rdfs);
216 if (mfs >= FC_SP_MIN_MAX_PAYLOAD && mfs < maxval)
222 * fc_rport_state_enter() - Change the state of a remote port
223 * @rdata: The remote port whose state should change
224 * @new: The new state
226 * Locking Note: Called with the rport lock held
228 static void fc_rport_state_enter(struct fc_rport_priv *rdata,
229 enum fc_rport_state new)
231 if (rdata->rp_state != new)
233 rdata->rp_state = new;
237 * fc_rport_work() - Handler for remote port events in the rport_event_queue
238 * @work: Handle to the remote port being dequeued
240 static void fc_rport_work(struct work_struct *work)
243 struct fc_rport_priv *rdata =
244 container_of(work, struct fc_rport_priv, event_work);
245 struct fc_rport_libfc_priv *rpriv;
246 enum fc_rport_event event;
247 struct fc_lport *lport = rdata->local_port;
248 struct fc_rport_operations *rport_ops;
249 struct fc_rport_identifiers ids;
250 struct fc_rport *rport;
251 struct fc4_prov *prov;
254 mutex_lock(&rdata->rp_mutex);
255 event = rdata->event;
256 rport_ops = rdata->ops;
257 rport = rdata->rport;
259 FC_RPORT_DBG(rdata, "work event %u\n", event);
264 rdata->event = RPORT_EV_NONE;
265 rdata->major_retries = 0;
266 kref_get(&rdata->kref);
267 mutex_unlock(&rdata->rp_mutex);
270 rport = fc_remote_port_add(lport->host, 0, &ids);
272 FC_RPORT_DBG(rdata, "Failed to add the rport\n");
273 lport->tt.rport_logoff(rdata);
274 kref_put(&rdata->kref, lport->tt.rport_destroy);
277 mutex_lock(&rdata->rp_mutex);
279 FC_RPORT_DBG(rdata, "rport already allocated\n");
280 rdata->rport = rport;
281 rport->maxframe_size = rdata->maxframe_size;
282 rport->supported_classes = rdata->supported_classes;
284 rpriv = rport->dd_data;
285 rpriv->local_port = lport;
286 rpriv->rp_state = rdata->rp_state;
287 rpriv->flags = rdata->flags;
288 rpriv->e_d_tov = rdata->e_d_tov;
289 rpriv->r_a_tov = rdata->r_a_tov;
290 mutex_unlock(&rdata->rp_mutex);
292 if (rport_ops && rport_ops->event_callback) {
293 FC_RPORT_DBG(rdata, "callback ev %d\n", event);
294 rport_ops->event_callback(lport, rdata, event);
296 if (rdata->lld_event_callback) {
297 FC_RPORT_DBG(rdata, "lld callback ev %d\n", event);
298 rdata->lld_event_callback(lport, rdata, event);
300 kref_put(&rdata->kref, lport->tt.rport_destroy);
303 case RPORT_EV_FAILED:
306 if (rdata->prli_count) {
307 mutex_lock(&fc_prov_mutex);
308 for (type = 1; type < FC_FC4_PROV_SIZE; type++) {
309 prov = fc_passive_prov[type];
310 if (prov && prov->prlo)
313 mutex_unlock(&fc_prov_mutex);
315 port_id = rdata->ids.port_id;
316 mutex_unlock(&rdata->rp_mutex);
318 if (rport_ops && rport_ops->event_callback) {
319 FC_RPORT_DBG(rdata, "callback ev %d\n", event);
320 rport_ops->event_callback(lport, rdata, event);
322 if (rdata->lld_event_callback) {
323 FC_RPORT_DBG(rdata, "lld callback ev %d\n", event);
324 rdata->lld_event_callback(lport, rdata, event);
326 cancel_delayed_work_sync(&rdata->retry_work);
329 * Reset any outstanding exchanges before freeing rport.
331 lport->tt.exch_mgr_reset(lport, 0, port_id);
332 lport->tt.exch_mgr_reset(lport, port_id, 0);
335 rpriv = rport->dd_data;
336 rpriv->rp_state = RPORT_ST_DELETE;
337 mutex_lock(&rdata->rp_mutex);
339 mutex_unlock(&rdata->rp_mutex);
340 fc_remote_port_delete(rport);
343 mutex_lock(&lport->disc.disc_mutex);
344 mutex_lock(&rdata->rp_mutex);
345 if (rdata->rp_state == RPORT_ST_DELETE) {
346 if (port_id == FC_FID_DIR_SERV) {
347 rdata->event = RPORT_EV_NONE;
348 mutex_unlock(&rdata->rp_mutex);
349 kref_put(&rdata->kref, lport->tt.rport_destroy);
350 } else if ((rdata->flags & FC_RP_STARTED) &&
351 rdata->major_retries <
352 lport->max_rport_retry_count) {
353 rdata->major_retries++;
354 rdata->event = RPORT_EV_NONE;
355 FC_RPORT_DBG(rdata, "work restart\n");
356 fc_rport_enter_flogi(rdata);
357 mutex_unlock(&rdata->rp_mutex);
359 FC_RPORT_DBG(rdata, "work delete\n");
360 list_del_rcu(&rdata->peers);
361 mutex_unlock(&rdata->rp_mutex);
362 kref_put(&rdata->kref, lport->tt.rport_destroy);
366 * Re-open for events. Reissue READY event if ready.
368 rdata->event = RPORT_EV_NONE;
369 if (rdata->rp_state == RPORT_ST_READY)
370 fc_rport_enter_ready(rdata);
371 mutex_unlock(&rdata->rp_mutex);
373 mutex_unlock(&lport->disc.disc_mutex);
377 mutex_unlock(&rdata->rp_mutex);
383 * fc_rport_login() - Start the remote port login state machine
384 * @rdata: The remote port to be logged in to
386 * Locking Note: Called without the rport lock held. This
387 * function will hold the rport lock, call an _enter_*
388 * function and then unlock the rport.
390 * This indicates the intent to be logged into the remote port.
391 * If it appears we are already logged in, ADISC is used to verify
394 static int fc_rport_login(struct fc_rport_priv *rdata)
396 mutex_lock(&rdata->rp_mutex);
398 rdata->flags |= FC_RP_STARTED;
399 switch (rdata->rp_state) {
401 FC_RPORT_DBG(rdata, "ADISC port\n");
402 fc_rport_enter_adisc(rdata);
404 case RPORT_ST_DELETE:
405 FC_RPORT_DBG(rdata, "Restart deleted port\n");
408 FC_RPORT_DBG(rdata, "Login to port\n");
409 fc_rport_enter_flogi(rdata);
412 mutex_unlock(&rdata->rp_mutex);
418 * fc_rport_enter_delete() - Schedule a remote port to be deleted
419 * @rdata: The remote port to be deleted
420 * @event: The event to report as the reason for deletion
422 * Locking Note: Called with the rport lock held.
424 * Allow state change into DELETE only once.
426 * Call queue_work only if there's no event already pending.
427 * Set the new event so that the old pending event will not occur.
428 * Since we have the mutex, even if fc_rport_work() is already started,
429 * it'll see the new event.
431 static void fc_rport_enter_delete(struct fc_rport_priv *rdata,
432 enum fc_rport_event event)
434 if (rdata->rp_state == RPORT_ST_DELETE)
437 FC_RPORT_DBG(rdata, "Delete port\n");
439 fc_rport_state_enter(rdata, RPORT_ST_DELETE);
441 if (rdata->event == RPORT_EV_NONE)
442 queue_work(rport_event_queue, &rdata->event_work);
443 rdata->event = event;
447 * fc_rport_logoff() - Logoff and remove a remote port
448 * @rdata: The remote port to be logged off of
450 * Locking Note: Called without the rport lock held. This
451 * function will hold the rport lock, call an _enter_*
452 * function and then unlock the rport.
454 static int fc_rport_logoff(struct fc_rport_priv *rdata)
456 mutex_lock(&rdata->rp_mutex);
458 FC_RPORT_DBG(rdata, "Remove port\n");
460 rdata->flags &= ~FC_RP_STARTED;
461 if (rdata->rp_state == RPORT_ST_DELETE) {
462 FC_RPORT_DBG(rdata, "Port in Delete state, not removing\n");
465 fc_rport_enter_logo(rdata);
468 * Change the state to Delete so that we discard
471 fc_rport_enter_delete(rdata, RPORT_EV_STOP);
473 mutex_unlock(&rdata->rp_mutex);
478 * fc_rport_enter_ready() - Transition to the RPORT_ST_READY state
479 * @rdata: The remote port that is ready
481 * Locking Note: The rport lock is expected to be held before calling
484 static void fc_rport_enter_ready(struct fc_rport_priv *rdata)
486 fc_rport_state_enter(rdata, RPORT_ST_READY);
488 FC_RPORT_DBG(rdata, "Port is Ready\n");
490 if (rdata->event == RPORT_EV_NONE)
491 queue_work(rport_event_queue, &rdata->event_work);
492 rdata->event = RPORT_EV_READY;
496 * fc_rport_timeout() - Handler for the retry_work timer
497 * @work: Handle to the remote port that has timed out
499 * Locking Note: Called without the rport lock held. This
500 * function will hold the rport lock, call an _enter_*
501 * function and then unlock the rport.
503 static void fc_rport_timeout(struct work_struct *work)
505 struct fc_rport_priv *rdata =
506 container_of(work, struct fc_rport_priv, retry_work.work);
508 mutex_lock(&rdata->rp_mutex);
510 switch (rdata->rp_state) {
512 fc_rport_enter_flogi(rdata);
515 fc_rport_enter_plogi(rdata);
518 fc_rport_enter_prli(rdata);
521 fc_rport_enter_rtv(rdata);
524 fc_rport_enter_adisc(rdata);
526 case RPORT_ST_PLOGI_WAIT:
529 case RPORT_ST_DELETE:
533 mutex_unlock(&rdata->rp_mutex);
537 * fc_rport_error() - Error handler, called once retries have been exhausted
538 * @rdata: The remote port the error is happened on
539 * @fp: The error code encapsulated in a frame pointer
541 * Locking Note: The rport lock is expected to be held before
542 * calling this routine
544 static void fc_rport_error(struct fc_rport_priv *rdata, struct fc_frame *fp)
546 FC_RPORT_DBG(rdata, "Error %ld in state %s, retries %d\n",
547 IS_ERR(fp) ? -PTR_ERR(fp) : 0,
548 fc_rport_state(rdata), rdata->retries);
550 switch (rdata->rp_state) {
553 rdata->flags &= ~FC_RP_STARTED;
554 fc_rport_enter_delete(rdata, RPORT_EV_FAILED);
557 fc_rport_enter_ready(rdata);
561 fc_rport_enter_logo(rdata);
563 case RPORT_ST_PLOGI_WAIT:
564 case RPORT_ST_DELETE:
572 * fc_rport_error_retry() - Handler for remote port state retries
573 * @rdata: The remote port whose state is to be retried
574 * @fp: The error code encapsulated in a frame pointer
576 * If the error was an exchange timeout retry immediately,
577 * otherwise wait for E_D_TOV.
579 * Locking Note: The rport lock is expected to be held before
580 * calling this routine
582 static void fc_rport_error_retry(struct fc_rport_priv *rdata,
585 unsigned long delay = msecs_to_jiffies(FC_DEF_E_D_TOV);
587 /* make sure this isn't an FC_EX_CLOSED error, never retry those */
588 if (PTR_ERR(fp) == -FC_EX_CLOSED)
591 if (rdata->retries < rdata->local_port->max_rport_retry_count) {
592 FC_RPORT_DBG(rdata, "Error %ld in state %s, retrying\n",
593 PTR_ERR(fp), fc_rport_state(rdata));
595 /* no additional delay on exchange timeouts */
596 if (PTR_ERR(fp) == -FC_EX_TIMEOUT)
598 schedule_delayed_work(&rdata->retry_work, delay);
603 fc_rport_error(rdata, fp);
607 * fc_rport_login_complete() - Handle parameters and completion of p-mp login.
608 * @rdata: The remote port which we logged into or which logged into us.
609 * @fp: The FLOGI or PLOGI request or response frame
611 * Returns non-zero error if a problem is detected with the frame.
612 * Does not free the frame.
614 * This is only used in point-to-multipoint mode for FIP currently.
616 static int fc_rport_login_complete(struct fc_rport_priv *rdata,
619 struct fc_lport *lport = rdata->local_port;
620 struct fc_els_flogi *flogi;
621 unsigned int e_d_tov;
624 flogi = fc_frame_payload_get(fp, sizeof(*flogi));
628 csp_flags = ntohs(flogi->fl_csp.sp_features);
630 if (fc_frame_payload_op(fp) == ELS_FLOGI) {
631 if (csp_flags & FC_SP_FT_FPORT) {
632 FC_RPORT_DBG(rdata, "Fabric bit set in FLOGI\n");
638 * E_D_TOV is not valid on an incoming FLOGI request.
640 e_d_tov = ntohl(flogi->fl_csp.sp_e_d_tov);
641 if (csp_flags & FC_SP_FT_EDTR)
643 if (e_d_tov > rdata->e_d_tov)
644 rdata->e_d_tov = e_d_tov;
646 rdata->maxframe_size = fc_plogi_get_maxframe(flogi, lport->mfs);
651 * fc_rport_flogi_resp() - Handle response to FLOGI request for p-mp mode
652 * @sp: The sequence that the FLOGI was on
653 * @fp: The FLOGI response frame
654 * @rp_arg: The remote port that received the FLOGI response
656 static void fc_rport_flogi_resp(struct fc_seq *sp, struct fc_frame *fp,
659 struct fc_rport_priv *rdata = rp_arg;
660 struct fc_lport *lport = rdata->local_port;
661 struct fc_els_flogi *flogi;
662 unsigned int r_a_tov;
664 FC_RPORT_DBG(rdata, "Received a FLOGI %s\n", fc_els_resp_type(fp));
666 if (fp == ERR_PTR(-FC_EX_CLOSED))
669 mutex_lock(&rdata->rp_mutex);
671 if (rdata->rp_state != RPORT_ST_FLOGI) {
672 FC_RPORT_DBG(rdata, "Received a FLOGI response, but in state "
673 "%s\n", fc_rport_state(rdata));
680 fc_rport_error(rdata, fp);
684 if (fc_frame_payload_op(fp) != ELS_LS_ACC)
686 if (fc_rport_login_complete(rdata, fp))
689 flogi = fc_frame_payload_get(fp, sizeof(*flogi));
692 r_a_tov = ntohl(flogi->fl_csp.sp_r_a_tov);
693 if (r_a_tov > rdata->r_a_tov)
694 rdata->r_a_tov = r_a_tov;
696 if (rdata->ids.port_name < lport->wwpn)
697 fc_rport_enter_plogi(rdata);
699 fc_rport_state_enter(rdata, RPORT_ST_PLOGI_WAIT);
703 mutex_unlock(&rdata->rp_mutex);
705 kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
708 FC_RPORT_DBG(rdata, "Bad FLOGI response\n");
709 fc_rport_error_retry(rdata, fp);
714 * fc_rport_enter_flogi() - Send a FLOGI request to the remote port for p-mp
715 * @rdata: The remote port to send a FLOGI to
717 * Locking Note: The rport lock is expected to be held before calling
720 static void fc_rport_enter_flogi(struct fc_rport_priv *rdata)
722 struct fc_lport *lport = rdata->local_port;
725 if (!lport->point_to_multipoint)
726 return fc_rport_enter_plogi(rdata);
728 FC_RPORT_DBG(rdata, "Entered FLOGI state from %s state\n",
729 fc_rport_state(rdata));
731 fc_rport_state_enter(rdata, RPORT_ST_FLOGI);
733 fp = fc_frame_alloc(lport, sizeof(struct fc_els_flogi));
735 return fc_rport_error_retry(rdata, fp);
737 if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_FLOGI,
738 fc_rport_flogi_resp, rdata,
740 fc_rport_error_retry(rdata, NULL);
742 kref_get(&rdata->kref);
746 * fc_rport_recv_flogi_req() - Handle Fabric Login (FLOGI) request in p-mp mode
747 * @lport: The local port that received the PLOGI request
748 * @rx_fp: The PLOGI request frame
750 static void fc_rport_recv_flogi_req(struct fc_lport *lport,
751 struct fc_frame *rx_fp)
753 struct fc_disc *disc;
754 struct fc_els_flogi *flp;
755 struct fc_rport_priv *rdata;
756 struct fc_frame *fp = rx_fp;
757 struct fc_seq_els_data rjt_data;
760 sid = fc_frame_sid(fp);
762 FC_RPORT_ID_DBG(lport, sid, "Received FLOGI request\n");
765 mutex_lock(&disc->disc_mutex);
767 if (!lport->point_to_multipoint) {
768 rjt_data.reason = ELS_RJT_UNSUP;
769 rjt_data.explan = ELS_EXPL_NONE;
773 flp = fc_frame_payload_get(fp, sizeof(*flp));
775 rjt_data.reason = ELS_RJT_LOGIC;
776 rjt_data.explan = ELS_EXPL_INV_LEN;
780 rdata = lport->tt.rport_lookup(lport, sid);
782 rjt_data.reason = ELS_RJT_FIP;
783 rjt_data.explan = ELS_EXPL_NOT_NEIGHBOR;
786 mutex_lock(&rdata->rp_mutex);
788 FC_RPORT_DBG(rdata, "Received FLOGI in %s state\n",
789 fc_rport_state(rdata));
791 switch (rdata->rp_state) {
794 * If received the FLOGI request on RPORT which is INIT state
795 * (means not transition to FLOGI either fc_rport timeout
796 * function didn;t trigger or this end hasn;t received
797 * beacon yet from other end. In that case only, allow RPORT
798 * state machine to continue, otherwise fall through which
799 * causes the code to send reject response.
800 * NOTE; Not checking for FIP->state such as VNMP_UP or
801 * VNMP_CLAIM because if FIP state is not one of those,
802 * RPORT wouldn;t have created and 'rport_lookup' would have
803 * failed anyway in that case.
805 if (lport->point_to_multipoint)
807 case RPORT_ST_DELETE:
808 mutex_unlock(&rdata->rp_mutex);
809 rjt_data.reason = ELS_RJT_FIP;
810 rjt_data.explan = ELS_EXPL_NOT_NEIGHBOR;
813 case RPORT_ST_PLOGI_WAIT:
821 * Set the remote port to be deleted and to then restart.
822 * This queues work to be sure exchanges are reset.
824 fc_rport_enter_delete(rdata, RPORT_EV_LOGO);
825 mutex_unlock(&rdata->rp_mutex);
826 rjt_data.reason = ELS_RJT_BUSY;
827 rjt_data.explan = ELS_EXPL_NONE;
830 if (fc_rport_login_complete(rdata, fp)) {
831 mutex_unlock(&rdata->rp_mutex);
832 rjt_data.reason = ELS_RJT_LOGIC;
833 rjt_data.explan = ELS_EXPL_NONE;
837 fp = fc_frame_alloc(lport, sizeof(*flp));
841 fc_flogi_fill(lport, fp);
842 flp = fc_frame_payload_get(fp, sizeof(*flp));
843 flp->fl_cmd = ELS_LS_ACC;
845 fc_fill_reply_hdr(fp, rx_fp, FC_RCTL_ELS_REP, 0);
846 lport->tt.frame_send(lport, fp);
848 if (rdata->ids.port_name < lport->wwpn)
849 fc_rport_enter_plogi(rdata);
851 fc_rport_state_enter(rdata, RPORT_ST_PLOGI_WAIT);
853 mutex_unlock(&rdata->rp_mutex);
854 mutex_unlock(&disc->disc_mutex);
855 fc_frame_free(rx_fp);
859 mutex_unlock(&disc->disc_mutex);
860 lport->tt.seq_els_rsp_send(rx_fp, ELS_LS_RJT, &rjt_data);
861 fc_frame_free(rx_fp);
865 * fc_rport_plogi_resp() - Handler for ELS PLOGI responses
866 * @sp: The sequence the PLOGI is on
867 * @fp: The PLOGI response frame
868 * @rdata_arg: The remote port that sent the PLOGI response
870 * Locking Note: This function will be called without the rport lock
871 * held, but it will lock, call an _enter_* function or fc_rport_error
872 * and then unlock the rport.
874 static void fc_rport_plogi_resp(struct fc_seq *sp, struct fc_frame *fp,
877 struct fc_rport_priv *rdata = rdata_arg;
878 struct fc_lport *lport = rdata->local_port;
879 struct fc_els_flogi *plp = NULL;
884 mutex_lock(&rdata->rp_mutex);
886 FC_RPORT_DBG(rdata, "Received a PLOGI %s\n", fc_els_resp_type(fp));
888 if (rdata->rp_state != RPORT_ST_PLOGI) {
889 FC_RPORT_DBG(rdata, "Received a PLOGI response, but in state "
890 "%s\n", fc_rport_state(rdata));
897 fc_rport_error_retry(rdata, fp);
901 op = fc_frame_payload_op(fp);
902 if (op == ELS_LS_ACC &&
903 (plp = fc_frame_payload_get(fp, sizeof(*plp))) != NULL) {
904 rdata->ids.port_name = get_unaligned_be64(&plp->fl_wwpn);
905 rdata->ids.node_name = get_unaligned_be64(&plp->fl_wwnn);
907 /* save plogi response sp_features for further reference */
908 rdata->sp_features = ntohs(plp->fl_csp.sp_features);
910 if (lport->point_to_multipoint)
911 fc_rport_login_complete(rdata, fp);
912 csp_seq = ntohs(plp->fl_csp.sp_tot_seq);
913 cssp_seq = ntohs(plp->fl_cssp[3 - 1].cp_con_seq);
914 if (cssp_seq < csp_seq)
916 rdata->max_seq = csp_seq;
917 rdata->maxframe_size = fc_plogi_get_maxframe(plp, lport->mfs);
918 fc_rport_enter_prli(rdata);
920 fc_rport_error_retry(rdata, fp);
925 mutex_unlock(&rdata->rp_mutex);
926 kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
930 fc_rport_compatible_roles(struct fc_lport *lport, struct fc_rport_priv *rdata)
932 if (rdata->ids.roles == FC_PORT_ROLE_UNKNOWN)
934 if ((rdata->ids.roles & FC_PORT_ROLE_FCP_TARGET) &&
935 (lport->service_params & FCP_SPPF_INIT_FCN))
937 if ((rdata->ids.roles & FC_PORT_ROLE_FCP_INITIATOR) &&
938 (lport->service_params & FCP_SPPF_TARG_FCN))
944 * fc_rport_enter_plogi() - Send Port Login (PLOGI) request
945 * @rdata: The remote port to send a PLOGI to
947 * Locking Note: The rport lock is expected to be held before calling
950 static void fc_rport_enter_plogi(struct fc_rport_priv *rdata)
952 struct fc_lport *lport = rdata->local_port;
955 if (!fc_rport_compatible_roles(lport, rdata)) {
956 FC_RPORT_DBG(rdata, "PLOGI suppressed for incompatible role\n");
957 fc_rport_state_enter(rdata, RPORT_ST_PLOGI_WAIT);
961 FC_RPORT_DBG(rdata, "Port entered PLOGI state from %s state\n",
962 fc_rport_state(rdata));
964 fc_rport_state_enter(rdata, RPORT_ST_PLOGI);
966 rdata->maxframe_size = FC_MIN_MAX_PAYLOAD;
967 fp = fc_frame_alloc(lport, sizeof(struct fc_els_flogi));
969 FC_RPORT_DBG(rdata, "%s frame alloc failed\n", __func__);
970 fc_rport_error_retry(rdata, fp);
973 rdata->e_d_tov = lport->e_d_tov;
975 if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_PLOGI,
976 fc_rport_plogi_resp, rdata,
978 fc_rport_error_retry(rdata, NULL);
980 kref_get(&rdata->kref);
984 * fc_rport_prli_resp() - Process Login (PRLI) response handler
985 * @sp: The sequence the PRLI response was on
986 * @fp: The PRLI response frame
987 * @rdata_arg: The remote port that sent the PRLI response
989 * Locking Note: This function will be called without the rport lock
990 * held, but it will lock, call an _enter_* function or fc_rport_error
991 * and then unlock the rport.
993 static void fc_rport_prli_resp(struct fc_seq *sp, struct fc_frame *fp,
996 struct fc_rport_priv *rdata = rdata_arg;
998 struct fc_els_prli prli;
999 struct fc_els_spp spp;
1001 struct fc_els_spp temp_spp;
1002 struct fc4_prov *prov;
1003 u32 roles = FC_RPORT_ROLE_UNKNOWN;
1008 mutex_lock(&rdata->rp_mutex);
1010 FC_RPORT_DBG(rdata, "Received a PRLI %s\n", fc_els_resp_type(fp));
1012 if (rdata->rp_state != RPORT_ST_PRLI) {
1013 FC_RPORT_DBG(rdata, "Received a PRLI response, but in state "
1014 "%s\n", fc_rport_state(rdata));
1021 fc_rport_error_retry(rdata, fp);
1025 /* reinitialize remote port roles */
1026 rdata->ids.roles = FC_RPORT_ROLE_UNKNOWN;
1028 op = fc_frame_payload_op(fp);
1029 if (op == ELS_LS_ACC) {
1030 pp = fc_frame_payload_get(fp, sizeof(*pp));
1034 resp_code = (pp->spp.spp_flags & FC_SPP_RESP_MASK);
1035 FC_RPORT_DBG(rdata, "PRLI spp_flags = 0x%x\n",
1037 rdata->spp_type = pp->spp.spp_type;
1038 if (resp_code != FC_SPP_RESP_ACK) {
1039 if (resp_code == FC_SPP_RESP_CONF)
1040 fc_rport_error(rdata, fp);
1042 fc_rport_error_retry(rdata, fp);
1045 if (pp->prli.prli_spp_len < sizeof(pp->spp))
1048 fcp_parm = ntohl(pp->spp.spp_params);
1049 if (fcp_parm & FCP_SPPF_RETRY)
1050 rdata->flags |= FC_RP_FLAGS_RETRY;
1051 if (fcp_parm & FCP_SPPF_CONF_COMPL)
1052 rdata->flags |= FC_RP_FLAGS_CONF_REQ;
1054 prov = fc_passive_prov[FC_TYPE_FCP];
1056 memset(&temp_spp, 0, sizeof(temp_spp));
1057 prov->prli(rdata, pp->prli.prli_spp_len,
1058 &pp->spp, &temp_spp);
1061 rdata->supported_classes = FC_COS_CLASS3;
1062 if (fcp_parm & FCP_SPPF_INIT_FCN)
1063 roles |= FC_RPORT_ROLE_FCP_INITIATOR;
1064 if (fcp_parm & FCP_SPPF_TARG_FCN)
1065 roles |= FC_RPORT_ROLE_FCP_TARGET;
1067 rdata->ids.roles = roles;
1068 fc_rport_enter_rtv(rdata);
1071 FC_RPORT_DBG(rdata, "Bad ELS response for PRLI command\n");
1072 fc_rport_error_retry(rdata, fp);
1078 mutex_unlock(&rdata->rp_mutex);
1079 kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
1083 * fc_rport_enter_prli() - Send Process Login (PRLI) request
1084 * @rdata: The remote port to send the PRLI request to
1086 * Locking Note: The rport lock is expected to be held before calling
1089 static void fc_rport_enter_prli(struct fc_rport_priv *rdata)
1091 struct fc_lport *lport = rdata->local_port;
1093 struct fc_els_prli prli;
1094 struct fc_els_spp spp;
1096 struct fc_frame *fp;
1097 struct fc4_prov *prov;
1100 * If the rport is one of the well known addresses
1101 * we skip PRLI and RTV and go straight to READY.
1103 if (rdata->ids.port_id >= FC_FID_DOM_MGR) {
1104 fc_rport_enter_ready(rdata);
1108 FC_RPORT_DBG(rdata, "Port entered PRLI state from %s state\n",
1109 fc_rport_state(rdata));
1111 fc_rport_state_enter(rdata, RPORT_ST_PRLI);
1113 fp = fc_frame_alloc(lport, sizeof(*pp));
1115 fc_rport_error_retry(rdata, fp);
1119 fc_prli_fill(lport, fp);
1121 prov = fc_passive_prov[FC_TYPE_FCP];
1123 pp = fc_frame_payload_get(fp, sizeof(*pp));
1124 prov->prli(rdata, sizeof(pp->spp), NULL, &pp->spp);
1127 fc_fill_fc_hdr(fp, FC_RCTL_ELS_REQ, rdata->ids.port_id,
1128 fc_host_port_id(lport->host), FC_TYPE_ELS,
1129 FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT, 0);
1131 if (!lport->tt.exch_seq_send(lport, fp, fc_rport_prli_resp,
1132 NULL, rdata, 2 * lport->r_a_tov))
1133 fc_rport_error_retry(rdata, NULL);
1135 kref_get(&rdata->kref);
1139 * fc_rport_els_rtv_resp() - Handler for Request Timeout Value (RTV) responses
1140 * @sp: The sequence the RTV was on
1141 * @fp: The RTV response frame
1142 * @rdata_arg: The remote port that sent the RTV response
1144 * Many targets don't seem to support this.
1146 * Locking Note: This function will be called without the rport lock
1147 * held, but it will lock, call an _enter_* function or fc_rport_error
1148 * and then unlock the rport.
1150 static void fc_rport_rtv_resp(struct fc_seq *sp, struct fc_frame *fp,
1153 struct fc_rport_priv *rdata = rdata_arg;
1156 mutex_lock(&rdata->rp_mutex);
1158 FC_RPORT_DBG(rdata, "Received a RTV %s\n", fc_els_resp_type(fp));
1160 if (rdata->rp_state != RPORT_ST_RTV) {
1161 FC_RPORT_DBG(rdata, "Received a RTV response, but in state "
1162 "%s\n", fc_rport_state(rdata));
1169 fc_rport_error(rdata, fp);
1173 op = fc_frame_payload_op(fp);
1174 if (op == ELS_LS_ACC) {
1175 struct fc_els_rtv_acc *rtv;
1179 rtv = fc_frame_payload_get(fp, sizeof(*rtv));
1181 toq = ntohl(rtv->rtv_toq);
1182 tov = ntohl(rtv->rtv_r_a_tov);
1185 rdata->r_a_tov = tov;
1186 tov = ntohl(rtv->rtv_e_d_tov);
1187 if (toq & FC_ELS_RTV_EDRES)
1191 rdata->e_d_tov = tov;
1195 fc_rport_enter_ready(rdata);
1200 mutex_unlock(&rdata->rp_mutex);
1201 kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
1205 * fc_rport_enter_rtv() - Send Request Timeout Value (RTV) request
1206 * @rdata: The remote port to send the RTV request to
1208 * Locking Note: The rport lock is expected to be held before calling
1211 static void fc_rport_enter_rtv(struct fc_rport_priv *rdata)
1213 struct fc_frame *fp;
1214 struct fc_lport *lport = rdata->local_port;
1216 FC_RPORT_DBG(rdata, "Port entered RTV state from %s state\n",
1217 fc_rport_state(rdata));
1219 fc_rport_state_enter(rdata, RPORT_ST_RTV);
1221 fp = fc_frame_alloc(lport, sizeof(struct fc_els_rtv));
1223 fc_rport_error_retry(rdata, fp);
1227 if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_RTV,
1228 fc_rport_rtv_resp, rdata,
1229 2 * lport->r_a_tov))
1230 fc_rport_error_retry(rdata, NULL);
1232 kref_get(&rdata->kref);
1236 * fc_rport_logo_resp() - Handler for logout (LOGO) responses
1237 * @sp: The sequence the LOGO was on
1238 * @fp: The LOGO response frame
1239 * @lport_arg: The local port
1241 static void fc_rport_logo_resp(struct fc_seq *sp, struct fc_frame *fp,
1244 struct fc_lport *lport = lport_arg;
1246 FC_RPORT_ID_DBG(lport, fc_seq_exch(sp)->did,
1247 "Received a LOGO %s\n", fc_els_resp_type(fp));
1254 * fc_rport_enter_logo() - Send a logout (LOGO) request
1255 * @rdata: The remote port to send the LOGO request to
1257 * Locking Note: The rport lock is expected to be held before calling
1260 static void fc_rport_enter_logo(struct fc_rport_priv *rdata)
1262 struct fc_lport *lport = rdata->local_port;
1263 struct fc_frame *fp;
1265 FC_RPORT_DBG(rdata, "Port sending LOGO from %s state\n",
1266 fc_rport_state(rdata));
1268 fp = fc_frame_alloc(lport, sizeof(struct fc_els_logo));
1271 (void)lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_LOGO,
1272 fc_rport_logo_resp, lport, 0);
1276 * fc_rport_els_adisc_resp() - Handler for Address Discovery (ADISC) responses
1277 * @sp: The sequence the ADISC response was on
1278 * @fp: The ADISC response frame
1279 * @rdata_arg: The remote port that sent the ADISC response
1281 * Locking Note: This function will be called without the rport lock
1282 * held, but it will lock, call an _enter_* function or fc_rport_error
1283 * and then unlock the rport.
1285 static void fc_rport_adisc_resp(struct fc_seq *sp, struct fc_frame *fp,
1288 struct fc_rport_priv *rdata = rdata_arg;
1289 struct fc_els_adisc *adisc;
1292 mutex_lock(&rdata->rp_mutex);
1294 FC_RPORT_DBG(rdata, "Received a ADISC response\n");
1296 if (rdata->rp_state != RPORT_ST_ADISC) {
1297 FC_RPORT_DBG(rdata, "Received a ADISC resp but in state %s\n",
1298 fc_rport_state(rdata));
1305 fc_rport_error(rdata, fp);
1310 * If address verification failed. Consider us logged out of the rport.
1311 * Since the rport is still in discovery, we want to be
1312 * logged in, so go to PLOGI state. Otherwise, go back to READY.
1314 op = fc_frame_payload_op(fp);
1315 adisc = fc_frame_payload_get(fp, sizeof(*adisc));
1316 if (op != ELS_LS_ACC || !adisc ||
1317 ntoh24(adisc->adisc_port_id) != rdata->ids.port_id ||
1318 get_unaligned_be64(&adisc->adisc_wwpn) != rdata->ids.port_name ||
1319 get_unaligned_be64(&adisc->adisc_wwnn) != rdata->ids.node_name) {
1320 FC_RPORT_DBG(rdata, "ADISC error or mismatch\n");
1321 fc_rport_enter_flogi(rdata);
1323 FC_RPORT_DBG(rdata, "ADISC OK\n");
1324 fc_rport_enter_ready(rdata);
1329 mutex_unlock(&rdata->rp_mutex);
1330 kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
1334 * fc_rport_enter_adisc() - Send Address Discover (ADISC) request
1335 * @rdata: The remote port to send the ADISC request to
1337 * Locking Note: The rport lock is expected to be held before calling
1340 static void fc_rport_enter_adisc(struct fc_rport_priv *rdata)
1342 struct fc_lport *lport = rdata->local_port;
1343 struct fc_frame *fp;
1345 FC_RPORT_DBG(rdata, "sending ADISC from %s state\n",
1346 fc_rport_state(rdata));
1348 fc_rport_state_enter(rdata, RPORT_ST_ADISC);
1350 fp = fc_frame_alloc(lport, sizeof(struct fc_els_adisc));
1352 fc_rport_error_retry(rdata, fp);
1355 if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_ADISC,
1356 fc_rport_adisc_resp, rdata,
1357 2 * lport->r_a_tov))
1358 fc_rport_error_retry(rdata, NULL);
1360 kref_get(&rdata->kref);
1364 * fc_rport_recv_adisc_req() - Handler for Address Discovery (ADISC) requests
1365 * @rdata: The remote port that sent the ADISC request
1366 * @in_fp: The ADISC request frame
1368 * Locking Note: Called with the lport and rport locks held.
1370 static void fc_rport_recv_adisc_req(struct fc_rport_priv *rdata,
1371 struct fc_frame *in_fp)
1373 struct fc_lport *lport = rdata->local_port;
1374 struct fc_frame *fp;
1375 struct fc_els_adisc *adisc;
1376 struct fc_seq_els_data rjt_data;
1378 FC_RPORT_DBG(rdata, "Received ADISC request\n");
1380 adisc = fc_frame_payload_get(in_fp, sizeof(*adisc));
1382 rjt_data.reason = ELS_RJT_PROT;
1383 rjt_data.explan = ELS_EXPL_INV_LEN;
1384 lport->tt.seq_els_rsp_send(in_fp, ELS_LS_RJT, &rjt_data);
1388 fp = fc_frame_alloc(lport, sizeof(*adisc));
1391 fc_adisc_fill(lport, fp);
1392 adisc = fc_frame_payload_get(fp, sizeof(*adisc));
1393 adisc->adisc_cmd = ELS_LS_ACC;
1394 fc_fill_reply_hdr(fp, in_fp, FC_RCTL_ELS_REP, 0);
1395 lport->tt.frame_send(lport, fp);
1397 fc_frame_free(in_fp);
1401 * fc_rport_recv_rls_req() - Handle received Read Link Status request
1402 * @rdata: The remote port that sent the RLS request
1403 * @rx_fp: The PRLI request frame
1405 * Locking Note: The rport lock is expected to be held before calling
1408 static void fc_rport_recv_rls_req(struct fc_rport_priv *rdata,
1409 struct fc_frame *rx_fp)
1412 struct fc_lport *lport = rdata->local_port;
1413 struct fc_frame *fp;
1414 struct fc_els_rls *rls;
1415 struct fc_els_rls_resp *rsp;
1416 struct fc_els_lesb *lesb;
1417 struct fc_seq_els_data rjt_data;
1418 struct fc_host_statistics *hst;
1420 FC_RPORT_DBG(rdata, "Received RLS request while in state %s\n",
1421 fc_rport_state(rdata));
1423 rls = fc_frame_payload_get(rx_fp, sizeof(*rls));
1425 rjt_data.reason = ELS_RJT_PROT;
1426 rjt_data.explan = ELS_EXPL_INV_LEN;
1430 fp = fc_frame_alloc(lport, sizeof(*rsp));
1432 rjt_data.reason = ELS_RJT_UNAB;
1433 rjt_data.explan = ELS_EXPL_INSUF_RES;
1437 rsp = fc_frame_payload_get(fp, sizeof(*rsp));
1438 memset(rsp, 0, sizeof(*rsp));
1439 rsp->rls_cmd = ELS_LS_ACC;
1440 lesb = &rsp->rls_lesb;
1441 if (lport->tt.get_lesb) {
1442 /* get LESB from LLD if it supports it */
1443 lport->tt.get_lesb(lport, lesb);
1445 fc_get_host_stats(lport->host);
1446 hst = &lport->host_stats;
1447 lesb->lesb_link_fail = htonl(hst->link_failure_count);
1448 lesb->lesb_sync_loss = htonl(hst->loss_of_sync_count);
1449 lesb->lesb_sig_loss = htonl(hst->loss_of_signal_count);
1450 lesb->lesb_prim_err = htonl(hst->prim_seq_protocol_err_count);
1451 lesb->lesb_inv_word = htonl(hst->invalid_tx_word_count);
1452 lesb->lesb_inv_crc = htonl(hst->invalid_crc_count);
1455 fc_fill_reply_hdr(fp, rx_fp, FC_RCTL_ELS_REP, 0);
1456 lport->tt.frame_send(lport, fp);
1460 lport->tt.seq_els_rsp_send(rx_fp, ELS_LS_RJT, &rjt_data);
1462 fc_frame_free(rx_fp);
1466 * fc_rport_recv_els_req() - Handler for validated ELS requests
1467 * @lport: The local port that received the ELS request
1468 * @fp: The ELS request frame
1470 * Handle incoming ELS requests that require port login.
1471 * The ELS opcode has already been validated by the caller.
1473 * Locking Note: Called with the lport lock held.
1475 static void fc_rport_recv_els_req(struct fc_lport *lport, struct fc_frame *fp)
1477 struct fc_rport_priv *rdata;
1478 struct fc_seq_els_data els_data;
1480 mutex_lock(&lport->disc.disc_mutex);
1481 rdata = lport->tt.rport_lookup(lport, fc_frame_sid(fp));
1483 mutex_unlock(&lport->disc.disc_mutex);
1486 mutex_lock(&rdata->rp_mutex);
1487 mutex_unlock(&lport->disc.disc_mutex);
1489 switch (rdata->rp_state) {
1492 case RPORT_ST_READY:
1493 case RPORT_ST_ADISC:
1496 mutex_unlock(&rdata->rp_mutex);
1500 switch (fc_frame_payload_op(fp)) {
1502 fc_rport_recv_prli_req(rdata, fp);
1505 fc_rport_recv_prlo_req(rdata, fp);
1508 fc_rport_recv_adisc_req(rdata, fp);
1511 lport->tt.seq_els_rsp_send(fp, ELS_RRQ, NULL);
1515 lport->tt.seq_els_rsp_send(fp, ELS_REC, NULL);
1519 fc_rport_recv_rls_req(rdata, fp);
1522 fc_frame_free(fp); /* can't happen */
1526 mutex_unlock(&rdata->rp_mutex);
1530 els_data.reason = ELS_RJT_UNAB;
1531 els_data.explan = ELS_EXPL_PLOGI_REQD;
1532 lport->tt.seq_els_rsp_send(fp, ELS_LS_RJT, &els_data);
1537 * fc_rport_recv_req() - Handler for requests
1538 * @lport: The local port that received the request
1539 * @fp: The request frame
1541 * Locking Note: Called with the lport lock held.
1543 static void fc_rport_recv_req(struct fc_lport *lport, struct fc_frame *fp)
1545 struct fc_seq_els_data els_data;
1548 * Handle FLOGI, PLOGI and LOGO requests separately, since they
1549 * don't require prior login.
1550 * Check for unsupported opcodes first and reject them.
1551 * For some ops, it would be incorrect to reject with "PLOGI required".
1553 switch (fc_frame_payload_op(fp)) {
1555 fc_rport_recv_flogi_req(lport, fp);
1558 fc_rport_recv_plogi_req(lport, fp);
1561 fc_rport_recv_logo_req(lport, fp);
1569 fc_rport_recv_els_req(lport, fp);
1572 els_data.reason = ELS_RJT_UNSUP;
1573 els_data.explan = ELS_EXPL_NONE;
1574 lport->tt.seq_els_rsp_send(fp, ELS_LS_RJT, &els_data);
1581 * fc_rport_recv_plogi_req() - Handler for Port Login (PLOGI) requests
1582 * @lport: The local port that received the PLOGI request
1583 * @rx_fp: The PLOGI request frame
1585 * Locking Note: The rport lock is held before calling this function.
1587 static void fc_rport_recv_plogi_req(struct fc_lport *lport,
1588 struct fc_frame *rx_fp)
1590 struct fc_disc *disc;
1591 struct fc_rport_priv *rdata;
1592 struct fc_frame *fp = rx_fp;
1593 struct fc_els_flogi *pl;
1594 struct fc_seq_els_data rjt_data;
1597 sid = fc_frame_sid(fp);
1599 FC_RPORT_ID_DBG(lport, sid, "Received PLOGI request\n");
1601 pl = fc_frame_payload_get(fp, sizeof(*pl));
1603 FC_RPORT_ID_DBG(lport, sid, "Received PLOGI too short\n");
1604 rjt_data.reason = ELS_RJT_PROT;
1605 rjt_data.explan = ELS_EXPL_INV_LEN;
1609 disc = &lport->disc;
1610 mutex_lock(&disc->disc_mutex);
1611 rdata = lport->tt.rport_create(lport, sid);
1613 mutex_unlock(&disc->disc_mutex);
1614 rjt_data.reason = ELS_RJT_UNAB;
1615 rjt_data.explan = ELS_EXPL_INSUF_RES;
1619 mutex_lock(&rdata->rp_mutex);
1620 mutex_unlock(&disc->disc_mutex);
1622 rdata->ids.port_name = get_unaligned_be64(&pl->fl_wwpn);
1623 rdata->ids.node_name = get_unaligned_be64(&pl->fl_wwnn);
1626 * If the rport was just created, possibly due to the incoming PLOGI,
1627 * set the state appropriately and accept the PLOGI.
1629 * If we had also sent a PLOGI, and if the received PLOGI is from a
1630 * higher WWPN, we accept it, otherwise an LS_RJT is sent with reason
1631 * "command already in progress".
1633 * XXX TBD: If the session was ready before, the PLOGI should result in
1634 * all outstanding exchanges being reset.
1636 switch (rdata->rp_state) {
1638 FC_RPORT_DBG(rdata, "Received PLOGI in INIT state\n");
1640 case RPORT_ST_PLOGI_WAIT:
1641 FC_RPORT_DBG(rdata, "Received PLOGI in PLOGI_WAIT state\n");
1643 case RPORT_ST_PLOGI:
1644 FC_RPORT_DBG(rdata, "Received PLOGI in PLOGI state\n");
1645 if (rdata->ids.port_name < lport->wwpn) {
1646 mutex_unlock(&rdata->rp_mutex);
1647 rjt_data.reason = ELS_RJT_INPROG;
1648 rjt_data.explan = ELS_EXPL_NONE;
1654 case RPORT_ST_READY:
1655 case RPORT_ST_ADISC:
1656 FC_RPORT_DBG(rdata, "Received PLOGI in logged-in state %d "
1657 "- ignored for now\n", rdata->rp_state);
1658 /* XXX TBD - should reset */
1660 case RPORT_ST_FLOGI:
1661 case RPORT_ST_DELETE:
1662 FC_RPORT_DBG(rdata, "Received PLOGI in state %s - send busy\n",
1663 fc_rport_state(rdata));
1664 mutex_unlock(&rdata->rp_mutex);
1665 rjt_data.reason = ELS_RJT_BUSY;
1666 rjt_data.explan = ELS_EXPL_NONE;
1669 if (!fc_rport_compatible_roles(lport, rdata)) {
1670 FC_RPORT_DBG(rdata, "Received PLOGI for incompatible role\n");
1671 mutex_unlock(&rdata->rp_mutex);
1672 rjt_data.reason = ELS_RJT_LOGIC;
1673 rjt_data.explan = ELS_EXPL_NONE;
1678 * Get session payload size from incoming PLOGI.
1680 rdata->maxframe_size = fc_plogi_get_maxframe(pl, lport->mfs);
1683 * Send LS_ACC. If this fails, the originator should retry.
1685 fp = fc_frame_alloc(lport, sizeof(*pl));
1689 fc_plogi_fill(lport, fp, ELS_LS_ACC);
1690 fc_fill_reply_hdr(fp, rx_fp, FC_RCTL_ELS_REP, 0);
1691 lport->tt.frame_send(lport, fp);
1692 fc_rport_enter_prli(rdata);
1694 mutex_unlock(&rdata->rp_mutex);
1695 fc_frame_free(rx_fp);
1699 lport->tt.seq_els_rsp_send(fp, ELS_LS_RJT, &rjt_data);
1704 * fc_rport_recv_prli_req() - Handler for process login (PRLI) requests
1705 * @rdata: The remote port that sent the PRLI request
1706 * @rx_fp: The PRLI request frame
1708 * Locking Note: The rport lock is exected to be held before calling
1711 static void fc_rport_recv_prli_req(struct fc_rport_priv *rdata,
1712 struct fc_frame *rx_fp)
1714 struct fc_lport *lport = rdata->local_port;
1715 struct fc_frame *fp;
1717 struct fc_els_prli prli;
1718 struct fc_els_spp spp;
1720 struct fc_els_spp *rspp; /* request service param page */
1721 struct fc_els_spp *spp; /* response spp */
1724 enum fc_els_spp_resp resp;
1725 enum fc_els_spp_resp passive;
1726 struct fc_seq_els_data rjt_data;
1727 struct fc4_prov *prov;
1729 FC_RPORT_DBG(rdata, "Received PRLI request while in state %s\n",
1730 fc_rport_state(rdata));
1732 len = fr_len(rx_fp) - sizeof(struct fc_frame_header);
1733 pp = fc_frame_payload_get(rx_fp, sizeof(*pp));
1736 plen = ntohs(pp->prli.prli_len);
1737 if ((plen % 4) != 0 || plen > len || plen < 16)
1741 plen = pp->prli.prli_spp_len;
1742 if ((plen % 4) != 0 || plen < sizeof(*spp) ||
1743 plen > len || len < sizeof(*pp) || plen < 12)
1747 fp = fc_frame_alloc(lport, len);
1749 rjt_data.reason = ELS_RJT_UNAB;
1750 rjt_data.explan = ELS_EXPL_INSUF_RES;
1753 pp = fc_frame_payload_get(fp, len);
1756 pp->prli.prli_cmd = ELS_LS_ACC;
1757 pp->prli.prli_spp_len = plen;
1758 pp->prli.prli_len = htons(len);
1759 len -= sizeof(struct fc_els_prli);
1762 * Go through all the service parameter pages and build
1763 * response. If plen indicates longer SPP than standard,
1764 * use that. The entire response has been pre-cleared above.
1767 mutex_lock(&fc_prov_mutex);
1768 while (len >= plen) {
1769 rdata->spp_type = rspp->spp_type;
1770 spp->spp_type = rspp->spp_type;
1771 spp->spp_type_ext = rspp->spp_type_ext;
1774 if (rspp->spp_type < FC_FC4_PROV_SIZE) {
1775 prov = fc_active_prov[rspp->spp_type];
1777 resp = prov->prli(rdata, plen, rspp, spp);
1778 prov = fc_passive_prov[rspp->spp_type];
1780 passive = prov->prli(rdata, plen, rspp, spp);
1781 if (!resp || passive == FC_SPP_RESP_ACK)
1786 if (spp->spp_flags & FC_SPP_EST_IMG_PAIR)
1787 resp |= FC_SPP_RESP_CONF;
1789 resp |= FC_SPP_RESP_INVL;
1791 spp->spp_flags |= resp;
1793 rspp = (struct fc_els_spp *)((char *)rspp + plen);
1794 spp = (struct fc_els_spp *)((char *)spp + plen);
1796 mutex_unlock(&fc_prov_mutex);
1799 * Send LS_ACC. If this fails, the originator should retry.
1801 fc_fill_reply_hdr(fp, rx_fp, FC_RCTL_ELS_REP, 0);
1802 lport->tt.frame_send(lport, fp);
1804 switch (rdata->rp_state) {
1806 fc_rport_enter_ready(rdata);
1814 rjt_data.reason = ELS_RJT_PROT;
1815 rjt_data.explan = ELS_EXPL_INV_LEN;
1817 lport->tt.seq_els_rsp_send(rx_fp, ELS_LS_RJT, &rjt_data);
1819 fc_frame_free(rx_fp);
1823 * fc_rport_recv_prlo_req() - Handler for process logout (PRLO) requests
1824 * @rdata: The remote port that sent the PRLO request
1825 * @rx_fp: The PRLO request frame
1827 * Locking Note: The rport lock is exected to be held before calling
1830 static void fc_rport_recv_prlo_req(struct fc_rport_priv *rdata,
1831 struct fc_frame *rx_fp)
1833 struct fc_lport *lport = rdata->local_port;
1834 struct fc_frame *fp;
1836 struct fc_els_prlo prlo;
1837 struct fc_els_spp spp;
1839 struct fc_els_spp *rspp; /* request service param page */
1840 struct fc_els_spp *spp; /* response spp */
1843 struct fc_seq_els_data rjt_data;
1845 FC_RPORT_DBG(rdata, "Received PRLO request while in state %s\n",
1846 fc_rport_state(rdata));
1848 len = fr_len(rx_fp) - sizeof(struct fc_frame_header);
1849 pp = fc_frame_payload_get(rx_fp, sizeof(*pp));
1852 plen = ntohs(pp->prlo.prlo_len);
1860 fp = fc_frame_alloc(lport, len);
1862 rjt_data.reason = ELS_RJT_UNAB;
1863 rjt_data.explan = ELS_EXPL_INSUF_RES;
1867 pp = fc_frame_payload_get(fp, len);
1870 pp->prlo.prlo_cmd = ELS_LS_ACC;
1871 pp->prlo.prlo_obs = 0x10;
1872 pp->prlo.prlo_len = htons(len);
1874 spp->spp_type = rspp->spp_type;
1875 spp->spp_type_ext = rspp->spp_type_ext;
1876 spp->spp_flags = FC_SPP_RESP_ACK;
1878 fc_rport_enter_delete(rdata, RPORT_EV_LOGO);
1880 fc_fill_reply_hdr(fp, rx_fp, FC_RCTL_ELS_REP, 0);
1881 lport->tt.frame_send(lport, fp);
1885 rjt_data.reason = ELS_RJT_PROT;
1886 rjt_data.explan = ELS_EXPL_INV_LEN;
1888 lport->tt.seq_els_rsp_send(rx_fp, ELS_LS_RJT, &rjt_data);
1890 fc_frame_free(rx_fp);
1894 * fc_rport_recv_logo_req() - Handler for logout (LOGO) requests
1895 * @lport: The local port that received the LOGO request
1896 * @fp: The LOGO request frame
1898 * Locking Note: The rport lock is exected to be held before calling
1901 static void fc_rport_recv_logo_req(struct fc_lport *lport, struct fc_frame *fp)
1903 struct fc_rport_priv *rdata;
1906 lport->tt.seq_els_rsp_send(fp, ELS_LS_ACC, NULL);
1908 sid = fc_frame_sid(fp);
1910 mutex_lock(&lport->disc.disc_mutex);
1911 rdata = lport->tt.rport_lookup(lport, sid);
1913 mutex_lock(&rdata->rp_mutex);
1914 FC_RPORT_DBG(rdata, "Received LOGO request while in state %s\n",
1915 fc_rport_state(rdata));
1917 fc_rport_enter_delete(rdata, RPORT_EV_LOGO);
1918 mutex_unlock(&rdata->rp_mutex);
1920 FC_RPORT_ID_DBG(lport, sid,
1921 "Received LOGO from non-logged-in port\n");
1922 mutex_unlock(&lport->disc.disc_mutex);
1927 * fc_rport_flush_queue() - Flush the rport_event_queue
1929 static void fc_rport_flush_queue(void)
1931 flush_workqueue(rport_event_queue);
1935 * fc_rport_init() - Initialize the remote port layer for a local port
1936 * @lport: The local port to initialize the remote port layer for
1938 int fc_rport_init(struct fc_lport *lport)
1940 if (!lport->tt.rport_lookup)
1941 lport->tt.rport_lookup = fc_rport_lookup;
1943 if (!lport->tt.rport_create)
1944 lport->tt.rport_create = fc_rport_create;
1946 if (!lport->tt.rport_login)
1947 lport->tt.rport_login = fc_rport_login;
1949 if (!lport->tt.rport_logoff)
1950 lport->tt.rport_logoff = fc_rport_logoff;
1952 if (!lport->tt.rport_recv_req)
1953 lport->tt.rport_recv_req = fc_rport_recv_req;
1955 if (!lport->tt.rport_flush_queue)
1956 lport->tt.rport_flush_queue = fc_rport_flush_queue;
1958 if (!lport->tt.rport_destroy)
1959 lport->tt.rport_destroy = fc_rport_destroy;
1963 EXPORT_SYMBOL(fc_rport_init);
1966 * fc_rport_fcp_prli() - Handle incoming PRLI for the FCP initiator.
1967 * @rdata: remote port private
1968 * @spp_len: service parameter page length
1969 * @rspp: received service parameter page
1970 * @spp: response service parameter page
1972 * Returns the value for the response code to be placed in spp_flags;
1973 * Returns 0 if not an initiator.
1975 static int fc_rport_fcp_prli(struct fc_rport_priv *rdata, u32 spp_len,
1976 const struct fc_els_spp *rspp,
1977 struct fc_els_spp *spp)
1979 struct fc_lport *lport = rdata->local_port;
1982 fcp_parm = ntohl(rspp->spp_params);
1983 rdata->ids.roles = FC_RPORT_ROLE_UNKNOWN;
1984 if (fcp_parm & FCP_SPPF_INIT_FCN)
1985 rdata->ids.roles |= FC_RPORT_ROLE_FCP_INITIATOR;
1986 if (fcp_parm & FCP_SPPF_TARG_FCN)
1987 rdata->ids.roles |= FC_RPORT_ROLE_FCP_TARGET;
1988 if (fcp_parm & FCP_SPPF_RETRY)
1989 rdata->flags |= FC_RP_FLAGS_RETRY;
1990 rdata->supported_classes = FC_COS_CLASS3;
1992 if (!(lport->service_params & FCP_SPPF_INIT_FCN))
1995 spp->spp_flags |= rspp->spp_flags & FC_SPP_EST_IMG_PAIR;
1998 * OR in our service parameters with other providers (target), if any.
2000 fcp_parm = ntohl(spp->spp_params);
2001 spp->spp_params = htonl(fcp_parm | lport->service_params);
2002 return FC_SPP_RESP_ACK;
2006 * FC-4 provider ops for FCP initiator.
2008 struct fc4_prov fc_rport_fcp_init = {
2009 .prli = fc_rport_fcp_prli,
2013 * fc_rport_t0_prli() - Handle incoming PRLI parameters for type 0
2014 * @rdata: remote port private
2015 * @spp_len: service parameter page length
2016 * @rspp: received service parameter page
2017 * @spp: response service parameter page
2019 static int fc_rport_t0_prli(struct fc_rport_priv *rdata, u32 spp_len,
2020 const struct fc_els_spp *rspp,
2021 struct fc_els_spp *spp)
2023 if (rspp->spp_flags & FC_SPP_EST_IMG_PAIR)
2024 return FC_SPP_RESP_INVL;
2025 return FC_SPP_RESP_ACK;
2029 * FC-4 provider ops for type 0 service parameters.
2031 * This handles the special case of type 0 which is always successful
2032 * but doesn't do anything otherwise.
2034 struct fc4_prov fc_rport_t0_prov = {
2035 .prli = fc_rport_t0_prli,
2039 * fc_setup_rport() - Initialize the rport_event_queue
2041 int fc_setup_rport(void)
2043 rport_event_queue = create_singlethread_workqueue("fc_rport_eq");
2044 if (!rport_event_queue)
2050 * fc_destroy_rport() - Destroy the rport_event_queue
2052 void fc_destroy_rport(void)
2054 destroy_workqueue(rport_event_queue);
2058 * fc_rport_terminate_io() - Stop all outstanding I/O on a remote port
2059 * @rport: The remote port whose I/O should be terminated
2061 void fc_rport_terminate_io(struct fc_rport *rport)
2063 struct fc_rport_libfc_priv *rpriv = rport->dd_data;
2064 struct fc_lport *lport = rpriv->local_port;
2066 lport->tt.exch_mgr_reset(lport, 0, rport->port_id);
2067 lport->tt.exch_mgr_reset(lport, rport->port_id, 0);
2069 EXPORT_SYMBOL(fc_rport_terminate_io);