2 * Copyright(c) 2007 - 2008 Intel Corporation. All rights reserved.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms and conditions of the GNU General Public License,
6 * version 2, as published by the Free Software Foundation.
8 * This program is distributed in the hope it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
17 * Maintained at www.Open-FCoE.org
23 * This file contains all processing regarding fc_rports. It contains the
24 * rport state machine and does all rport interaction with the transport class.
25 * There should be no other places in libfc that interact directly with the
26 * transport class in regards to adding and deleting rports.
28 * fc_rport's represent N_Port's within the fabric.
34 * The rport should never hold the rport mutex and then attempt to acquire
35 * either the lport or disc mutexes. The rport's mutex is considered lesser
36 * than both the lport's mutex and the disc mutex. Refer to fc_lport.c for
37 * more comments on the hierarchy.
39 * The locking strategy is similar to the lport's strategy. The lock protects
40 * the rport's states and is held and released by the entry points to the rport
41 * block. All _enter_* functions correspond to rport states and expect the rport
42 * mutex to be locked before calling them. This means that rports only handle
43 * one request or response at a time, since they're not critical for the I/O
44 * path this potential over-use of the mutex is acceptable.
47 #include <linux/kernel.h>
48 #include <linux/spinlock.h>
49 #include <linux/interrupt.h>
50 #include <linux/slab.h>
51 #include <linux/rcupdate.h>
52 #include <linux/timer.h>
53 #include <linux/workqueue.h>
54 #include <asm/unaligned.h>
56 #include <scsi/libfc.h>
57 #include <scsi/fc_encode.h>
61 struct workqueue_struct
*rport_event_queue
;
63 static void fc_rport_enter_flogi(struct fc_rport_priv
*);
64 static void fc_rport_enter_plogi(struct fc_rport_priv
*);
65 static void fc_rport_enter_prli(struct fc_rport_priv
*);
66 static void fc_rport_enter_rtv(struct fc_rport_priv
*);
67 static void fc_rport_enter_ready(struct fc_rport_priv
*);
68 static void fc_rport_enter_logo(struct fc_rport_priv
*);
69 static void fc_rport_enter_adisc(struct fc_rport_priv
*);
71 static void fc_rport_recv_plogi_req(struct fc_lport
*, struct fc_frame
*);
72 static void fc_rport_recv_prli_req(struct fc_rport_priv
*, struct fc_frame
*);
73 static void fc_rport_recv_prlo_req(struct fc_rport_priv
*, struct fc_frame
*);
74 static void fc_rport_recv_logo_req(struct fc_lport
*, struct fc_frame
*);
75 static void fc_rport_timeout(struct work_struct
*);
76 static void fc_rport_error(struct fc_rport_priv
*, struct fc_frame
*);
77 static void fc_rport_error_retry(struct fc_rport_priv
*, struct fc_frame
*);
78 static void fc_rport_work(struct work_struct
*);
80 static const char *fc_rport_state_names
[] = {
81 [RPORT_ST_INIT
] = "Init",
82 [RPORT_ST_FLOGI
] = "FLOGI",
83 [RPORT_ST_PLOGI_WAIT
] = "PLOGI_WAIT",
84 [RPORT_ST_PLOGI
] = "PLOGI",
85 [RPORT_ST_PRLI
] = "PRLI",
86 [RPORT_ST_RTV
] = "RTV",
87 [RPORT_ST_READY
] = "Ready",
88 [RPORT_ST_ADISC
] = "ADISC",
89 [RPORT_ST_DELETE
] = "Delete",
93 * fc_rport_lookup() - Lookup a remote port by port_id
94 * @lport: The local port to lookup the remote port on
95 * @port_id: The remote port ID to look up
97 * The caller must hold either disc_mutex or rcu_read_lock().
99 static struct fc_rport_priv
*fc_rport_lookup(const struct fc_lport
*lport
,
102 struct fc_rport_priv
*rdata
;
104 list_for_each_entry_rcu(rdata
, &lport
->disc
.rports
, peers
)
105 if (rdata
->ids
.port_id
== port_id
)
111 * fc_rport_create() - Create a new remote port
112 * @lport: The local port this remote port will be associated with
113 * @ids: The identifiers for the new remote port
115 * The remote port will start in the INIT state.
117 * Locking note: must be called with the disc_mutex held.
119 static struct fc_rport_priv
*fc_rport_create(struct fc_lport
*lport
,
122 struct fc_rport_priv
*rdata
;
124 rdata
= lport
->tt
.rport_lookup(lport
, port_id
);
128 rdata
= kzalloc(sizeof(*rdata
) + lport
->rport_priv_size
, GFP_KERNEL
);
132 rdata
->ids
.node_name
= -1;
133 rdata
->ids
.port_name
= -1;
134 rdata
->ids
.port_id
= port_id
;
135 rdata
->ids
.roles
= FC_RPORT_ROLE_UNKNOWN
;
137 kref_init(&rdata
->kref
);
138 mutex_init(&rdata
->rp_mutex
);
139 rdata
->local_port
= lport
;
140 rdata
->rp_state
= RPORT_ST_INIT
;
141 rdata
->event
= RPORT_EV_NONE
;
142 rdata
->flags
= FC_RP_FLAGS_REC_SUPPORTED
;
143 rdata
->e_d_tov
= lport
->e_d_tov
;
144 rdata
->r_a_tov
= lport
->r_a_tov
;
145 rdata
->maxframe_size
= FC_MIN_MAX_PAYLOAD
;
146 INIT_DELAYED_WORK(&rdata
->retry_work
, fc_rport_timeout
);
147 INIT_WORK(&rdata
->event_work
, fc_rport_work
);
148 if (port_id
!= FC_FID_DIR_SERV
)
149 list_add_rcu(&rdata
->peers
, &lport
->disc
.rports
);
154 * fc_rport_free_rcu() - Free a remote port
155 * @rcu: The rcu_head structure inside the remote port
157 static void fc_rport_free_rcu(struct rcu_head
*rcu
)
159 struct fc_rport_priv
*rdata
;
161 rdata
= container_of(rcu
, struct fc_rport_priv
, rcu
);
166 * fc_rport_destroy() - Free a remote port after last reference is released
167 * @kref: The remote port's kref
169 static void fc_rport_destroy(struct kref
*kref
)
171 struct fc_rport_priv
*rdata
;
173 rdata
= container_of(kref
, struct fc_rport_priv
, kref
);
174 call_rcu(&rdata
->rcu
, fc_rport_free_rcu
);
178 * fc_rport_state() - Return a string identifying the remote port's state
179 * @rdata: The remote port
181 static const char *fc_rport_state(struct fc_rport_priv
*rdata
)
185 cp
= fc_rport_state_names
[rdata
->rp_state
];
192 * fc_set_rport_loss_tmo() - Set the remote port loss timeout
193 * @rport: The remote port that gets a new timeout value
194 * @timeout: The new timeout value (in seconds)
196 void fc_set_rport_loss_tmo(struct fc_rport
*rport
, u32 timeout
)
199 rport
->dev_loss_tmo
= timeout
+ 5;
201 rport
->dev_loss_tmo
= 30;
203 EXPORT_SYMBOL(fc_set_rport_loss_tmo
);
206 * fc_plogi_get_maxframe() - Get the maximum payload from the common service
207 * parameters in a FLOGI frame
208 * @flp: The FLOGI or PLOGI payload
209 * @maxval: The maximum frame size upper limit; this may be less than what
210 * is in the service parameters
212 static unsigned int fc_plogi_get_maxframe(struct fc_els_flogi
*flp
,
218 * Get max payload from the common service parameters and the
219 * class 3 receive data field size.
221 mfs
= ntohs(flp
->fl_csp
.sp_bb_data
) & FC_SP_BB_DATA_MASK
;
222 if (mfs
>= FC_SP_MIN_MAX_PAYLOAD
&& mfs
< maxval
)
224 mfs
= ntohs(flp
->fl_cssp
[3 - 1].cp_rdfs
);
225 if (mfs
>= FC_SP_MIN_MAX_PAYLOAD
&& mfs
< maxval
)
231 * fc_rport_state_enter() - Change the state of a remote port
232 * @rdata: The remote port whose state should change
233 * @new: The new state
235 * Locking Note: Called with the rport lock held
237 static void fc_rport_state_enter(struct fc_rport_priv
*rdata
,
238 enum fc_rport_state
new)
240 if (rdata
->rp_state
!= new)
242 rdata
->rp_state
= new;
246 * fc_rport_work() - Handler for remote port events in the rport_event_queue
247 * @work: Handle to the remote port being dequeued
249 static void fc_rport_work(struct work_struct
*work
)
252 struct fc_rport_priv
*rdata
=
253 container_of(work
, struct fc_rport_priv
, event_work
);
254 struct fc_rport_libfc_priv
*rpriv
;
255 enum fc_rport_event event
;
256 struct fc_lport
*lport
= rdata
->local_port
;
257 struct fc_rport_operations
*rport_ops
;
258 struct fc_rport_identifiers ids
;
259 struct fc_rport
*rport
;
261 mutex_lock(&rdata
->rp_mutex
);
262 event
= rdata
->event
;
263 rport_ops
= rdata
->ops
;
264 rport
= rdata
->rport
;
266 FC_RPORT_DBG(rdata
, "work event %u\n", event
);
271 rdata
->event
= RPORT_EV_NONE
;
272 rdata
->major_retries
= 0;
273 kref_get(&rdata
->kref
);
274 mutex_unlock(&rdata
->rp_mutex
);
277 rport
= fc_remote_port_add(lport
->host
, 0, &ids
);
279 FC_RPORT_DBG(rdata
, "Failed to add the rport\n");
280 lport
->tt
.rport_logoff(rdata
);
281 kref_put(&rdata
->kref
, lport
->tt
.rport_destroy
);
284 mutex_lock(&rdata
->rp_mutex
);
286 FC_RPORT_DBG(rdata
, "rport already allocated\n");
287 rdata
->rport
= rport
;
288 rport
->maxframe_size
= rdata
->maxframe_size
;
289 rport
->supported_classes
= rdata
->supported_classes
;
291 rpriv
= rport
->dd_data
;
292 rpriv
->local_port
= lport
;
293 rpriv
->rp_state
= rdata
->rp_state
;
294 rpriv
->flags
= rdata
->flags
;
295 rpriv
->e_d_tov
= rdata
->e_d_tov
;
296 rpriv
->r_a_tov
= rdata
->r_a_tov
;
297 mutex_unlock(&rdata
->rp_mutex
);
299 if (rport_ops
&& rport_ops
->event_callback
) {
300 FC_RPORT_DBG(rdata
, "callback ev %d\n", event
);
301 rport_ops
->event_callback(lport
, rdata
, event
);
303 kref_put(&rdata
->kref
, lport
->tt
.rport_destroy
);
306 case RPORT_EV_FAILED
:
309 port_id
= rdata
->ids
.port_id
;
310 mutex_unlock(&rdata
->rp_mutex
);
312 if (rport_ops
&& rport_ops
->event_callback
) {
313 FC_RPORT_DBG(rdata
, "callback ev %d\n", event
);
314 rport_ops
->event_callback(lport
, rdata
, event
);
316 cancel_delayed_work_sync(&rdata
->retry_work
);
319 * Reset any outstanding exchanges before freeing rport.
321 lport
->tt
.exch_mgr_reset(lport
, 0, port_id
);
322 lport
->tt
.exch_mgr_reset(lport
, port_id
, 0);
325 rpriv
= rport
->dd_data
;
326 rpriv
->rp_state
= RPORT_ST_DELETE
;
327 mutex_lock(&rdata
->rp_mutex
);
329 mutex_unlock(&rdata
->rp_mutex
);
330 fc_remote_port_delete(rport
);
333 mutex_lock(&lport
->disc
.disc_mutex
);
334 mutex_lock(&rdata
->rp_mutex
);
335 if (rdata
->rp_state
== RPORT_ST_DELETE
) {
336 if (port_id
== FC_FID_DIR_SERV
) {
337 rdata
->event
= RPORT_EV_NONE
;
338 mutex_unlock(&rdata
->rp_mutex
);
339 } else if ((rdata
->flags
& FC_RP_STARTED
) &&
340 rdata
->major_retries
<
341 lport
->max_rport_retry_count
) {
342 rdata
->major_retries
++;
343 rdata
->event
= RPORT_EV_NONE
;
344 FC_RPORT_DBG(rdata
, "work restart\n");
345 fc_rport_enter_flogi(rdata
);
346 mutex_unlock(&rdata
->rp_mutex
);
348 FC_RPORT_DBG(rdata
, "work delete\n");
349 list_del_rcu(&rdata
->peers
);
350 mutex_unlock(&rdata
->rp_mutex
);
351 kref_put(&rdata
->kref
, lport
->tt
.rport_destroy
);
355 * Re-open for events. Reissue READY event if ready.
357 rdata
->event
= RPORT_EV_NONE
;
358 if (rdata
->rp_state
== RPORT_ST_READY
)
359 fc_rport_enter_ready(rdata
);
360 mutex_unlock(&rdata
->rp_mutex
);
362 mutex_unlock(&lport
->disc
.disc_mutex
);
366 mutex_unlock(&rdata
->rp_mutex
);
372 * fc_rport_login() - Start the remote port login state machine
373 * @rdata: The remote port to be logged in to
375 * Locking Note: Called without the rport lock held. This
376 * function will hold the rport lock, call an _enter_*
377 * function and then unlock the rport.
379 * This indicates the intent to be logged into the remote port.
380 * If it appears we are already logged in, ADISC is used to verify
383 int fc_rport_login(struct fc_rport_priv
*rdata
)
385 mutex_lock(&rdata
->rp_mutex
);
387 rdata
->flags
|= FC_RP_STARTED
;
388 switch (rdata
->rp_state
) {
390 FC_RPORT_DBG(rdata
, "ADISC port\n");
391 fc_rport_enter_adisc(rdata
);
393 case RPORT_ST_DELETE
:
394 FC_RPORT_DBG(rdata
, "Restart deleted port\n");
397 FC_RPORT_DBG(rdata
, "Login to port\n");
398 fc_rport_enter_flogi(rdata
);
401 mutex_unlock(&rdata
->rp_mutex
);
407 * fc_rport_enter_delete() - Schedule a remote port to be deleted
408 * @rdata: The remote port to be deleted
409 * @event: The event to report as the reason for deletion
411 * Locking Note: Called with the rport lock held.
413 * Allow state change into DELETE only once.
415 * Call queue_work only if there's no event already pending.
416 * Set the new event so that the old pending event will not occur.
417 * Since we have the mutex, even if fc_rport_work() is already started,
418 * it'll see the new event.
420 static void fc_rport_enter_delete(struct fc_rport_priv
*rdata
,
421 enum fc_rport_event event
)
423 if (rdata
->rp_state
== RPORT_ST_DELETE
)
426 FC_RPORT_DBG(rdata
, "Delete port\n");
428 fc_rport_state_enter(rdata
, RPORT_ST_DELETE
);
430 if (rdata
->event
== RPORT_EV_NONE
)
431 queue_work(rport_event_queue
, &rdata
->event_work
);
432 rdata
->event
= event
;
436 * fc_rport_logoff() - Logoff and remove a remote port
437 * @rdata: The remote port to be logged off of
439 * Locking Note: Called without the rport lock held. This
440 * function will hold the rport lock, call an _enter_*
441 * function and then unlock the rport.
443 int fc_rport_logoff(struct fc_rport_priv
*rdata
)
445 mutex_lock(&rdata
->rp_mutex
);
447 FC_RPORT_DBG(rdata
, "Remove port\n");
449 rdata
->flags
&= ~FC_RP_STARTED
;
450 if (rdata
->rp_state
== RPORT_ST_DELETE
) {
451 FC_RPORT_DBG(rdata
, "Port in Delete state, not removing\n");
454 fc_rport_enter_logo(rdata
);
457 * Change the state to Delete so that we discard
460 fc_rport_enter_delete(rdata
, RPORT_EV_STOP
);
462 mutex_unlock(&rdata
->rp_mutex
);
467 * fc_rport_enter_ready() - Transition to the RPORT_ST_READY state
468 * @rdata: The remote port that is ready
470 * Locking Note: The rport lock is expected to be held before calling
473 static void fc_rport_enter_ready(struct fc_rport_priv
*rdata
)
475 fc_rport_state_enter(rdata
, RPORT_ST_READY
);
477 FC_RPORT_DBG(rdata
, "Port is Ready\n");
479 if (rdata
->event
== RPORT_EV_NONE
)
480 queue_work(rport_event_queue
, &rdata
->event_work
);
481 rdata
->event
= RPORT_EV_READY
;
485 * fc_rport_timeout() - Handler for the retry_work timer
486 * @work: Handle to the remote port that has timed out
488 * Locking Note: Called without the rport lock held. This
489 * function will hold the rport lock, call an _enter_*
490 * function and then unlock the rport.
492 static void fc_rport_timeout(struct work_struct
*work
)
494 struct fc_rport_priv
*rdata
=
495 container_of(work
, struct fc_rport_priv
, retry_work
.work
);
497 mutex_lock(&rdata
->rp_mutex
);
499 switch (rdata
->rp_state
) {
501 fc_rport_enter_flogi(rdata
);
504 fc_rport_enter_plogi(rdata
);
507 fc_rport_enter_prli(rdata
);
510 fc_rport_enter_rtv(rdata
);
513 fc_rport_enter_adisc(rdata
);
515 case RPORT_ST_PLOGI_WAIT
:
518 case RPORT_ST_DELETE
:
522 mutex_unlock(&rdata
->rp_mutex
);
526 * fc_rport_error() - Error handler, called once retries have been exhausted
527 * @rdata: The remote port the error is happened on
528 * @fp: The error code encapsulated in a frame pointer
530 * Locking Note: The rport lock is expected to be held before
531 * calling this routine
533 static void fc_rport_error(struct fc_rport_priv
*rdata
, struct fc_frame
*fp
)
535 FC_RPORT_DBG(rdata
, "Error %ld in state %s, retries %d\n",
536 IS_ERR(fp
) ? -PTR_ERR(fp
) : 0,
537 fc_rport_state(rdata
), rdata
->retries
);
539 switch (rdata
->rp_state
) {
542 rdata
->flags
&= ~FC_RP_STARTED
;
543 fc_rport_enter_delete(rdata
, RPORT_EV_FAILED
);
546 fc_rport_enter_ready(rdata
);
550 fc_rport_enter_logo(rdata
);
552 case RPORT_ST_PLOGI_WAIT
:
553 case RPORT_ST_DELETE
:
561 * fc_rport_error_retry() - Handler for remote port state retries
562 * @rdata: The remote port whose state is to be retried
563 * @fp: The error code encapsulated in a frame pointer
565 * If the error was an exchange timeout retry immediately,
566 * otherwise wait for E_D_TOV.
568 * Locking Note: The rport lock is expected to be held before
569 * calling this routine
571 static void fc_rport_error_retry(struct fc_rport_priv
*rdata
,
574 unsigned long delay
= FC_DEF_E_D_TOV
;
576 /* make sure this isn't an FC_EX_CLOSED error, never retry those */
577 if (PTR_ERR(fp
) == -FC_EX_CLOSED
)
578 return fc_rport_error(rdata
, fp
);
580 if (rdata
->retries
< rdata
->local_port
->max_rport_retry_count
) {
581 FC_RPORT_DBG(rdata
, "Error %ld in state %s, retrying\n",
582 PTR_ERR(fp
), fc_rport_state(rdata
));
584 /* no additional delay on exchange timeouts */
585 if (PTR_ERR(fp
) == -FC_EX_TIMEOUT
)
587 schedule_delayed_work(&rdata
->retry_work
, delay
);
591 return fc_rport_error(rdata
, fp
);
595 * fc_rport_login_complete() - Handle parameters and completion of p-mp login.
596 * @rdata: The remote port which we logged into or which logged into us.
597 * @fp: The FLOGI or PLOGI request or response frame
599 * Returns non-zero error if a problem is detected with the frame.
600 * Does not free the frame.
602 * This is only used in point-to-multipoint mode for FIP currently.
604 static int fc_rport_login_complete(struct fc_rport_priv
*rdata
,
607 struct fc_lport
*lport
= rdata
->local_port
;
608 struct fc_els_flogi
*flogi
;
609 unsigned int e_d_tov
;
612 flogi
= fc_frame_payload_get(fp
, sizeof(*flogi
));
616 csp_flags
= ntohs(flogi
->fl_csp
.sp_features
);
618 if (fc_frame_payload_op(fp
) == ELS_FLOGI
) {
619 if (csp_flags
& FC_SP_FT_FPORT
) {
620 FC_RPORT_DBG(rdata
, "Fabric bit set in FLOGI\n");
626 * E_D_TOV is not valid on an incoming FLOGI request.
628 e_d_tov
= ntohl(flogi
->fl_csp
.sp_e_d_tov
);
629 if (csp_flags
& FC_SP_FT_EDTR
)
631 if (e_d_tov
> rdata
->e_d_tov
)
632 rdata
->e_d_tov
= e_d_tov
;
634 rdata
->maxframe_size
= fc_plogi_get_maxframe(flogi
, lport
->mfs
);
639 * fc_rport_flogi_resp() - Handle response to FLOGI request for p-mp mode
640 * @sp: The sequence that the FLOGI was on
641 * @fp: The FLOGI response frame
642 * @rp_arg: The remote port that received the FLOGI response
644 void fc_rport_flogi_resp(struct fc_seq
*sp
, struct fc_frame
*fp
,
647 struct fc_rport_priv
*rdata
= rp_arg
;
648 struct fc_lport
*lport
= rdata
->local_port
;
649 struct fc_els_flogi
*flogi
;
650 unsigned int r_a_tov
;
652 FC_RPORT_DBG(rdata
, "Received a FLOGI %s\n", fc_els_resp_type(fp
));
654 if (fp
== ERR_PTR(-FC_EX_CLOSED
))
657 mutex_lock(&rdata
->rp_mutex
);
659 if (rdata
->rp_state
!= RPORT_ST_FLOGI
) {
660 FC_RPORT_DBG(rdata
, "Received a FLOGI response, but in state "
661 "%s\n", fc_rport_state(rdata
));
668 fc_rport_error(rdata
, fp
);
672 if (fc_frame_payload_op(fp
) != ELS_LS_ACC
)
674 if (fc_rport_login_complete(rdata
, fp
))
677 flogi
= fc_frame_payload_get(fp
, sizeof(*flogi
));
680 r_a_tov
= ntohl(flogi
->fl_csp
.sp_r_a_tov
);
681 if (r_a_tov
> rdata
->r_a_tov
)
682 rdata
->r_a_tov
= r_a_tov
;
684 if (rdata
->ids
.port_name
< lport
->wwpn
)
685 fc_rport_enter_plogi(rdata
);
687 fc_rport_state_enter(rdata
, RPORT_ST_PLOGI_WAIT
);
691 mutex_unlock(&rdata
->rp_mutex
);
692 kref_put(&rdata
->kref
, rdata
->local_port
->tt
.rport_destroy
);
695 FC_RPORT_DBG(rdata
, "Bad FLOGI response\n");
696 fc_rport_error_retry(rdata
, fp
);
701 * fc_rport_enter_flogi() - Send a FLOGI request to the remote port for p-mp
702 * @rdata: The remote port to send a FLOGI to
704 * Locking Note: The rport lock is expected to be held before calling
707 static void fc_rport_enter_flogi(struct fc_rport_priv
*rdata
)
709 struct fc_lport
*lport
= rdata
->local_port
;
712 if (!lport
->point_to_multipoint
)
713 return fc_rport_enter_plogi(rdata
);
715 FC_RPORT_DBG(rdata
, "Entered FLOGI state from %s state\n",
716 fc_rport_state(rdata
));
718 fc_rport_state_enter(rdata
, RPORT_ST_FLOGI
);
720 fp
= fc_frame_alloc(lport
, sizeof(struct fc_els_flogi
));
722 return fc_rport_error_retry(rdata
, fp
);
724 if (!lport
->tt
.elsct_send(lport
, rdata
->ids
.port_id
, fp
, ELS_FLOGI
,
725 fc_rport_flogi_resp
, rdata
,
727 fc_rport_error_retry(rdata
, NULL
);
729 kref_get(&rdata
->kref
);
733 * fc_rport_recv_flogi_req() - Handle Fabric Login (FLOGI) request in p-mp mode
734 * @lport: The local port that received the PLOGI request
735 * @rx_fp: The PLOGI request frame
737 static void fc_rport_recv_flogi_req(struct fc_lport
*lport
,
738 struct fc_frame
*rx_fp
)
740 struct fc_disc
*disc
;
741 struct fc_els_flogi
*flp
;
742 struct fc_rport_priv
*rdata
;
743 struct fc_frame
*fp
= rx_fp
;
744 struct fc_seq_els_data rjt_data
;
747 sid
= fc_frame_sid(fp
);
749 FC_RPORT_ID_DBG(lport
, sid
, "Received FLOGI request\n");
752 mutex_lock(&disc
->disc_mutex
);
754 if (!lport
->point_to_multipoint
) {
755 rjt_data
.reason
= ELS_RJT_UNSUP
;
756 rjt_data
.explan
= ELS_EXPL_NONE
;
760 flp
= fc_frame_payload_get(fp
, sizeof(*flp
));
762 rjt_data
.reason
= ELS_RJT_LOGIC
;
763 rjt_data
.explan
= ELS_EXPL_INV_LEN
;
767 rdata
= lport
->tt
.rport_lookup(lport
, sid
);
769 rjt_data
.reason
= ELS_RJT_FIP
;
770 rjt_data
.explan
= ELS_EXPL_NOT_NEIGHBOR
;
773 mutex_lock(&rdata
->rp_mutex
);
775 FC_RPORT_DBG(rdata
, "Received FLOGI in %s state\n",
776 fc_rport_state(rdata
));
778 switch (rdata
->rp_state
) {
780 case RPORT_ST_DELETE
:
781 mutex_unlock(&rdata
->rp_mutex
);
782 rjt_data
.reason
= ELS_RJT_FIP
;
783 rjt_data
.explan
= ELS_EXPL_NOT_NEIGHBOR
;
786 case RPORT_ST_PLOGI_WAIT
:
794 * Set the remote port to be deleted and to then restart.
795 * This queues work to be sure exchanges are reset.
797 fc_rport_enter_delete(rdata
, RPORT_EV_LOGO
);
798 mutex_unlock(&rdata
->rp_mutex
);
799 rjt_data
.reason
= ELS_RJT_BUSY
;
800 rjt_data
.explan
= ELS_EXPL_NONE
;
803 if (fc_rport_login_complete(rdata
, fp
)) {
804 mutex_unlock(&rdata
->rp_mutex
);
805 rjt_data
.reason
= ELS_RJT_LOGIC
;
806 rjt_data
.explan
= ELS_EXPL_NONE
;
810 fp
= fc_frame_alloc(lport
, sizeof(*flp
));
814 fc_flogi_fill(lport
, fp
);
815 flp
= fc_frame_payload_get(fp
, sizeof(*flp
));
816 flp
->fl_cmd
= ELS_LS_ACC
;
818 fc_fill_reply_hdr(fp
, rx_fp
, FC_RCTL_ELS_REP
, 0);
819 lport
->tt
.frame_send(lport
, fp
);
821 if (rdata
->ids
.port_name
< lport
->wwpn
)
822 fc_rport_enter_plogi(rdata
);
824 fc_rport_state_enter(rdata
, RPORT_ST_PLOGI_WAIT
);
826 mutex_unlock(&rdata
->rp_mutex
);
827 mutex_unlock(&disc
->disc_mutex
);
828 fc_frame_free(rx_fp
);
832 mutex_unlock(&disc
->disc_mutex
);
833 lport
->tt
.seq_els_rsp_send(rx_fp
, ELS_LS_RJT
, &rjt_data
);
834 fc_frame_free(rx_fp
);
838 * fc_rport_plogi_resp() - Handler for ELS PLOGI responses
839 * @sp: The sequence the PLOGI is on
840 * @fp: The PLOGI response frame
841 * @rdata_arg: The remote port that sent the PLOGI response
843 * Locking Note: This function will be called without the rport lock
844 * held, but it will lock, call an _enter_* function or fc_rport_error
845 * and then unlock the rport.
847 static void fc_rport_plogi_resp(struct fc_seq
*sp
, struct fc_frame
*fp
,
850 struct fc_rport_priv
*rdata
= rdata_arg
;
851 struct fc_lport
*lport
= rdata
->local_port
;
852 struct fc_els_flogi
*plp
= NULL
;
857 mutex_lock(&rdata
->rp_mutex
);
859 FC_RPORT_DBG(rdata
, "Received a PLOGI %s\n", fc_els_resp_type(fp
));
861 if (rdata
->rp_state
!= RPORT_ST_PLOGI
) {
862 FC_RPORT_DBG(rdata
, "Received a PLOGI response, but in state "
863 "%s\n", fc_rport_state(rdata
));
870 fc_rport_error_retry(rdata
, fp
);
874 op
= fc_frame_payload_op(fp
);
875 if (op
== ELS_LS_ACC
&&
876 (plp
= fc_frame_payload_get(fp
, sizeof(*plp
))) != NULL
) {
877 rdata
->ids
.port_name
= get_unaligned_be64(&plp
->fl_wwpn
);
878 rdata
->ids
.node_name
= get_unaligned_be64(&plp
->fl_wwnn
);
880 if (lport
->point_to_multipoint
)
881 fc_rport_login_complete(rdata
, fp
);
882 csp_seq
= ntohs(plp
->fl_csp
.sp_tot_seq
);
883 cssp_seq
= ntohs(plp
->fl_cssp
[3 - 1].cp_con_seq
);
884 if (cssp_seq
< csp_seq
)
886 rdata
->max_seq
= csp_seq
;
887 rdata
->maxframe_size
= fc_plogi_get_maxframe(plp
, lport
->mfs
);
888 fc_rport_enter_prli(rdata
);
890 fc_rport_error_retry(rdata
, fp
);
895 mutex_unlock(&rdata
->rp_mutex
);
896 kref_put(&rdata
->kref
, rdata
->local_port
->tt
.rport_destroy
);
900 * fc_rport_enter_plogi() - Send Port Login (PLOGI) request
901 * @rdata: The remote port to send a PLOGI to
903 * Locking Note: The rport lock is expected to be held before calling
906 static void fc_rport_enter_plogi(struct fc_rport_priv
*rdata
)
908 struct fc_lport
*lport
= rdata
->local_port
;
911 FC_RPORT_DBG(rdata
, "Port entered PLOGI state from %s state\n",
912 fc_rport_state(rdata
));
914 fc_rport_state_enter(rdata
, RPORT_ST_PLOGI
);
916 rdata
->maxframe_size
= FC_MIN_MAX_PAYLOAD
;
917 fp
= fc_frame_alloc(lport
, sizeof(struct fc_els_flogi
));
919 FC_RPORT_DBG(rdata
, "%s frame alloc failed\n", __func__
);
920 fc_rport_error_retry(rdata
, fp
);
923 rdata
->e_d_tov
= lport
->e_d_tov
;
925 if (!lport
->tt
.elsct_send(lport
, rdata
->ids
.port_id
, fp
, ELS_PLOGI
,
926 fc_rport_plogi_resp
, rdata
,
928 fc_rport_error_retry(rdata
, NULL
);
930 kref_get(&rdata
->kref
);
934 * fc_rport_prli_resp() - Process Login (PRLI) response handler
935 * @sp: The sequence the PRLI response was on
936 * @fp: The PRLI response frame
937 * @rdata_arg: The remote port that sent the PRLI response
939 * Locking Note: This function will be called without the rport lock
940 * held, but it will lock, call an _enter_* function or fc_rport_error
941 * and then unlock the rport.
943 static void fc_rport_prli_resp(struct fc_seq
*sp
, struct fc_frame
*fp
,
946 struct fc_rport_priv
*rdata
= rdata_arg
;
948 struct fc_els_prli prli
;
949 struct fc_els_spp spp
;
951 u32 roles
= FC_RPORT_ROLE_UNKNOWN
;
956 mutex_lock(&rdata
->rp_mutex
);
958 FC_RPORT_DBG(rdata
, "Received a PRLI %s\n", fc_els_resp_type(fp
));
960 if (rdata
->rp_state
!= RPORT_ST_PRLI
) {
961 FC_RPORT_DBG(rdata
, "Received a PRLI response, but in state "
962 "%s\n", fc_rport_state(rdata
));
969 fc_rport_error_retry(rdata
, fp
);
973 /* reinitialize remote port roles */
974 rdata
->ids
.roles
= FC_RPORT_ROLE_UNKNOWN
;
976 op
= fc_frame_payload_op(fp
);
977 if (op
== ELS_LS_ACC
) {
978 pp
= fc_frame_payload_get(fp
, sizeof(*pp
));
982 resp_code
= (pp
->spp
.spp_flags
& FC_SPP_RESP_MASK
);
983 FC_RPORT_DBG(rdata
, "PRLI spp_flags = 0x%x\n",
985 if (resp_code
!= FC_SPP_RESP_ACK
) {
986 if (resp_code
== FC_SPP_RESP_CONF
)
987 fc_rport_error(rdata
, fp
);
989 fc_rport_error_retry(rdata
, fp
);
992 if (pp
->prli
.prli_spp_len
< sizeof(pp
->spp
))
995 fcp_parm
= ntohl(pp
->spp
.spp_params
);
996 if (fcp_parm
& FCP_SPPF_RETRY
)
997 rdata
->flags
|= FC_RP_FLAGS_RETRY
;
999 rdata
->supported_classes
= FC_COS_CLASS3
;
1000 if (fcp_parm
& FCP_SPPF_INIT_FCN
)
1001 roles
|= FC_RPORT_ROLE_FCP_INITIATOR
;
1002 if (fcp_parm
& FCP_SPPF_TARG_FCN
)
1003 roles
|= FC_RPORT_ROLE_FCP_TARGET
;
1005 rdata
->ids
.roles
= roles
;
1006 fc_rport_enter_rtv(rdata
);
1009 FC_RPORT_DBG(rdata
, "Bad ELS response for PRLI command\n");
1010 fc_rport_error_retry(rdata
, fp
);
1016 mutex_unlock(&rdata
->rp_mutex
);
1017 kref_put(&rdata
->kref
, rdata
->local_port
->tt
.rport_destroy
);
1021 * fc_rport_enter_prli() - Send Process Login (PRLI) request
1022 * @rdata: The remote port to send the PRLI request to
1024 * Locking Note: The rport lock is expected to be held before calling
1027 static void fc_rport_enter_prli(struct fc_rport_priv
*rdata
)
1029 struct fc_lport
*lport
= rdata
->local_port
;
1031 struct fc_els_prli prli
;
1032 struct fc_els_spp spp
;
1034 struct fc_frame
*fp
;
1037 * If the rport is one of the well known addresses
1038 * we skip PRLI and RTV and go straight to READY.
1040 if (rdata
->ids
.port_id
>= FC_FID_DOM_MGR
) {
1041 fc_rport_enter_ready(rdata
);
1045 FC_RPORT_DBG(rdata
, "Port entered PRLI state from %s state\n",
1046 fc_rport_state(rdata
));
1048 fc_rport_state_enter(rdata
, RPORT_ST_PRLI
);
1050 fp
= fc_frame_alloc(lport
, sizeof(*pp
));
1052 fc_rport_error_retry(rdata
, fp
);
1056 if (!lport
->tt
.elsct_send(lport
, rdata
->ids
.port_id
, fp
, ELS_PRLI
,
1057 fc_rport_prli_resp
, rdata
,
1058 2 * lport
->r_a_tov
))
1059 fc_rport_error_retry(rdata
, NULL
);
1061 kref_get(&rdata
->kref
);
1065 * fc_rport_els_rtv_resp() - Handler for Request Timeout Value (RTV) responses
1066 * @sp: The sequence the RTV was on
1067 * @fp: The RTV response frame
1068 * @rdata_arg: The remote port that sent the RTV response
1070 * Many targets don't seem to support this.
1072 * Locking Note: This function will be called without the rport lock
1073 * held, but it will lock, call an _enter_* function or fc_rport_error
1074 * and then unlock the rport.
1076 static void fc_rport_rtv_resp(struct fc_seq
*sp
, struct fc_frame
*fp
,
1079 struct fc_rport_priv
*rdata
= rdata_arg
;
1082 mutex_lock(&rdata
->rp_mutex
);
1084 FC_RPORT_DBG(rdata
, "Received a RTV %s\n", fc_els_resp_type(fp
));
1086 if (rdata
->rp_state
!= RPORT_ST_RTV
) {
1087 FC_RPORT_DBG(rdata
, "Received a RTV response, but in state "
1088 "%s\n", fc_rport_state(rdata
));
1095 fc_rport_error(rdata
, fp
);
1099 op
= fc_frame_payload_op(fp
);
1100 if (op
== ELS_LS_ACC
) {
1101 struct fc_els_rtv_acc
*rtv
;
1105 rtv
= fc_frame_payload_get(fp
, sizeof(*rtv
));
1107 toq
= ntohl(rtv
->rtv_toq
);
1108 tov
= ntohl(rtv
->rtv_r_a_tov
);
1111 rdata
->r_a_tov
= tov
;
1112 tov
= ntohl(rtv
->rtv_e_d_tov
);
1113 if (toq
& FC_ELS_RTV_EDRES
)
1117 rdata
->e_d_tov
= tov
;
1121 fc_rport_enter_ready(rdata
);
1126 mutex_unlock(&rdata
->rp_mutex
);
1127 kref_put(&rdata
->kref
, rdata
->local_port
->tt
.rport_destroy
);
1131 * fc_rport_enter_rtv() - Send Request Timeout Value (RTV) request
1132 * @rdata: The remote port to send the RTV request to
1134 * Locking Note: The rport lock is expected to be held before calling
1137 static void fc_rport_enter_rtv(struct fc_rport_priv
*rdata
)
1139 struct fc_frame
*fp
;
1140 struct fc_lport
*lport
= rdata
->local_port
;
1142 FC_RPORT_DBG(rdata
, "Port entered RTV state from %s state\n",
1143 fc_rport_state(rdata
));
1145 fc_rport_state_enter(rdata
, RPORT_ST_RTV
);
1147 fp
= fc_frame_alloc(lport
, sizeof(struct fc_els_rtv
));
1149 fc_rport_error_retry(rdata
, fp
);
1153 if (!lport
->tt
.elsct_send(lport
, rdata
->ids
.port_id
, fp
, ELS_RTV
,
1154 fc_rport_rtv_resp
, rdata
,
1155 2 * lport
->r_a_tov
))
1156 fc_rport_error_retry(rdata
, NULL
);
1158 kref_get(&rdata
->kref
);
1162 * fc_rport_logo_resp() - Handler for logout (LOGO) responses
1163 * @sp: The sequence the LOGO was on
1164 * @fp: The LOGO response frame
1165 * @lport_arg: The local port
1167 static void fc_rport_logo_resp(struct fc_seq
*sp
, struct fc_frame
*fp
,
1170 struct fc_lport
*lport
= lport_arg
;
1172 FC_RPORT_ID_DBG(lport
, fc_seq_exch(sp
)->did
,
1173 "Received a LOGO %s\n", fc_els_resp_type(fp
));
1180 * fc_rport_enter_logo() - Send a logout (LOGO) request
1181 * @rdata: The remote port to send the LOGO request to
1183 * Locking Note: The rport lock is expected to be held before calling
1186 static void fc_rport_enter_logo(struct fc_rport_priv
*rdata
)
1188 struct fc_lport
*lport
= rdata
->local_port
;
1189 struct fc_frame
*fp
;
1191 FC_RPORT_DBG(rdata
, "Port sending LOGO from %s state\n",
1192 fc_rport_state(rdata
));
1194 fp
= fc_frame_alloc(lport
, sizeof(struct fc_els_logo
));
1197 (void)lport
->tt
.elsct_send(lport
, rdata
->ids
.port_id
, fp
, ELS_LOGO
,
1198 fc_rport_logo_resp
, lport
, 0);
1202 * fc_rport_els_adisc_resp() - Handler for Address Discovery (ADISC) responses
1203 * @sp: The sequence the ADISC response was on
1204 * @fp: The ADISC response frame
1205 * @rdata_arg: The remote port that sent the ADISC response
1207 * Locking Note: This function will be called without the rport lock
1208 * held, but it will lock, call an _enter_* function or fc_rport_error
1209 * and then unlock the rport.
1211 static void fc_rport_adisc_resp(struct fc_seq
*sp
, struct fc_frame
*fp
,
1214 struct fc_rport_priv
*rdata
= rdata_arg
;
1215 struct fc_els_adisc
*adisc
;
1218 mutex_lock(&rdata
->rp_mutex
);
1220 FC_RPORT_DBG(rdata
, "Received a ADISC response\n");
1222 if (rdata
->rp_state
!= RPORT_ST_ADISC
) {
1223 FC_RPORT_DBG(rdata
, "Received a ADISC resp but in state %s\n",
1224 fc_rport_state(rdata
));
1231 fc_rport_error(rdata
, fp
);
1236 * If address verification failed. Consider us logged out of the rport.
1237 * Since the rport is still in discovery, we want to be
1238 * logged in, so go to PLOGI state. Otherwise, go back to READY.
1240 op
= fc_frame_payload_op(fp
);
1241 adisc
= fc_frame_payload_get(fp
, sizeof(*adisc
));
1242 if (op
!= ELS_LS_ACC
|| !adisc
||
1243 ntoh24(adisc
->adisc_port_id
) != rdata
->ids
.port_id
||
1244 get_unaligned_be64(&adisc
->adisc_wwpn
) != rdata
->ids
.port_name
||
1245 get_unaligned_be64(&adisc
->adisc_wwnn
) != rdata
->ids
.node_name
) {
1246 FC_RPORT_DBG(rdata
, "ADISC error or mismatch\n");
1247 fc_rport_enter_flogi(rdata
);
1249 FC_RPORT_DBG(rdata
, "ADISC OK\n");
1250 fc_rport_enter_ready(rdata
);
1255 mutex_unlock(&rdata
->rp_mutex
);
1256 kref_put(&rdata
->kref
, rdata
->local_port
->tt
.rport_destroy
);
1260 * fc_rport_enter_adisc() - Send Address Discover (ADISC) request
1261 * @rdata: The remote port to send the ADISC request to
1263 * Locking Note: The rport lock is expected to be held before calling
1266 static void fc_rport_enter_adisc(struct fc_rport_priv
*rdata
)
1268 struct fc_lport
*lport
= rdata
->local_port
;
1269 struct fc_frame
*fp
;
1271 FC_RPORT_DBG(rdata
, "sending ADISC from %s state\n",
1272 fc_rport_state(rdata
));
1274 fc_rport_state_enter(rdata
, RPORT_ST_ADISC
);
1276 fp
= fc_frame_alloc(lport
, sizeof(struct fc_els_adisc
));
1278 fc_rport_error_retry(rdata
, fp
);
1281 if (!lport
->tt
.elsct_send(lport
, rdata
->ids
.port_id
, fp
, ELS_ADISC
,
1282 fc_rport_adisc_resp
, rdata
,
1283 2 * lport
->r_a_tov
))
1284 fc_rport_error_retry(rdata
, NULL
);
1286 kref_get(&rdata
->kref
);
1290 * fc_rport_recv_adisc_req() - Handler for Address Discovery (ADISC) requests
1291 * @rdata: The remote port that sent the ADISC request
1292 * @in_fp: The ADISC request frame
1294 * Locking Note: Called with the lport and rport locks held.
1296 static void fc_rport_recv_adisc_req(struct fc_rport_priv
*rdata
,
1297 struct fc_frame
*in_fp
)
1299 struct fc_lport
*lport
= rdata
->local_port
;
1300 struct fc_frame
*fp
;
1301 struct fc_els_adisc
*adisc
;
1302 struct fc_seq_els_data rjt_data
;
1304 FC_RPORT_DBG(rdata
, "Received ADISC request\n");
1306 adisc
= fc_frame_payload_get(in_fp
, sizeof(*adisc
));
1308 rjt_data
.reason
= ELS_RJT_PROT
;
1309 rjt_data
.explan
= ELS_EXPL_INV_LEN
;
1310 lport
->tt
.seq_els_rsp_send(in_fp
, ELS_LS_RJT
, &rjt_data
);
1314 fp
= fc_frame_alloc(lport
, sizeof(*adisc
));
1317 fc_adisc_fill(lport
, fp
);
1318 adisc
= fc_frame_payload_get(fp
, sizeof(*adisc
));
1319 adisc
->adisc_cmd
= ELS_LS_ACC
;
1320 fc_fill_reply_hdr(fp
, in_fp
, FC_RCTL_ELS_REP
, 0);
1321 lport
->tt
.frame_send(lport
, fp
);
1323 fc_frame_free(in_fp
);
1327 * fc_rport_recv_rls_req() - Handle received Read Link Status request
1328 * @rdata: The remote port that sent the RLS request
1329 * @rx_fp: The PRLI request frame
1331 * Locking Note: The rport lock is expected to be held before calling
1334 static void fc_rport_recv_rls_req(struct fc_rport_priv
*rdata
,
1335 struct fc_frame
*rx_fp
)
1338 struct fc_lport
*lport
= rdata
->local_port
;
1339 struct fc_frame
*fp
;
1340 struct fc_els_rls
*rls
;
1341 struct fc_els_rls_resp
*rsp
;
1342 struct fc_els_lesb
*lesb
;
1343 struct fc_seq_els_data rjt_data
;
1344 struct fc_host_statistics
*hst
;
1346 FC_RPORT_DBG(rdata
, "Received RLS request while in state %s\n",
1347 fc_rport_state(rdata
));
1349 rls
= fc_frame_payload_get(rx_fp
, sizeof(*rls
));
1351 rjt_data
.reason
= ELS_RJT_PROT
;
1352 rjt_data
.explan
= ELS_EXPL_INV_LEN
;
1356 fp
= fc_frame_alloc(lport
, sizeof(*rsp
));
1358 rjt_data
.reason
= ELS_RJT_UNAB
;
1359 rjt_data
.explan
= ELS_EXPL_INSUF_RES
;
1363 rsp
= fc_frame_payload_get(fp
, sizeof(*rsp
));
1364 memset(rsp
, 0, sizeof(*rsp
));
1365 rsp
->rls_cmd
= ELS_LS_ACC
;
1366 lesb
= &rsp
->rls_lesb
;
1367 if (lport
->tt
.get_lesb
) {
1368 /* get LESB from LLD if it supports it */
1369 lport
->tt
.get_lesb(lport
, lesb
);
1371 fc_get_host_stats(lport
->host
);
1372 hst
= &lport
->host_stats
;
1373 lesb
->lesb_link_fail
= htonl(hst
->link_failure_count
);
1374 lesb
->lesb_sync_loss
= htonl(hst
->loss_of_sync_count
);
1375 lesb
->lesb_sig_loss
= htonl(hst
->loss_of_signal_count
);
1376 lesb
->lesb_prim_err
= htonl(hst
->prim_seq_protocol_err_count
);
1377 lesb
->lesb_inv_word
= htonl(hst
->invalid_tx_word_count
);
1378 lesb
->lesb_inv_crc
= htonl(hst
->invalid_crc_count
);
1381 fc_fill_reply_hdr(fp
, rx_fp
, FC_RCTL_ELS_REP
, 0);
1382 lport
->tt
.frame_send(lport
, fp
);
1386 lport
->tt
.seq_els_rsp_send(rx_fp
, ELS_LS_RJT
, &rjt_data
);
1388 fc_frame_free(rx_fp
);
1392 * fc_rport_recv_els_req() - Handler for validated ELS requests
1393 * @lport: The local port that received the ELS request
1394 * @fp: The ELS request frame
1396 * Handle incoming ELS requests that require port login.
1397 * The ELS opcode has already been validated by the caller.
1399 * Locking Note: Called with the lport lock held.
1401 static void fc_rport_recv_els_req(struct fc_lport
*lport
, struct fc_frame
*fp
)
1403 struct fc_rport_priv
*rdata
;
1404 struct fc_seq_els_data els_data
;
1406 mutex_lock(&lport
->disc
.disc_mutex
);
1407 rdata
= lport
->tt
.rport_lookup(lport
, fc_frame_sid(fp
));
1409 mutex_unlock(&lport
->disc
.disc_mutex
);
1412 mutex_lock(&rdata
->rp_mutex
);
1413 mutex_unlock(&lport
->disc
.disc_mutex
);
1415 switch (rdata
->rp_state
) {
1418 case RPORT_ST_READY
:
1419 case RPORT_ST_ADISC
:
1422 mutex_unlock(&rdata
->rp_mutex
);
1426 switch (fc_frame_payload_op(fp
)) {
1428 fc_rport_recv_prli_req(rdata
, fp
);
1431 fc_rport_recv_prlo_req(rdata
, fp
);
1434 fc_rport_recv_adisc_req(rdata
, fp
);
1437 lport
->tt
.seq_els_rsp_send(fp
, ELS_RRQ
, NULL
);
1441 lport
->tt
.seq_els_rsp_send(fp
, ELS_REC
, NULL
);
1445 fc_rport_recv_rls_req(rdata
, fp
);
1448 fc_frame_free(fp
); /* can't happen */
1452 mutex_unlock(&rdata
->rp_mutex
);
1456 els_data
.reason
= ELS_RJT_UNAB
;
1457 els_data
.explan
= ELS_EXPL_PLOGI_REQD
;
1458 lport
->tt
.seq_els_rsp_send(fp
, ELS_LS_RJT
, &els_data
);
1463 * fc_rport_recv_req() - Handler for requests
1464 * @lport: The local port that received the request
1465 * @fp: The request frame
1467 * Locking Note: Called with the lport lock held.
1469 void fc_rport_recv_req(struct fc_lport
*lport
, struct fc_frame
*fp
)
1471 struct fc_seq_els_data els_data
;
1474 * Handle FLOGI, PLOGI and LOGO requests separately, since they
1475 * don't require prior login.
1476 * Check for unsupported opcodes first and reject them.
1477 * For some ops, it would be incorrect to reject with "PLOGI required".
1479 switch (fc_frame_payload_op(fp
)) {
1481 fc_rport_recv_flogi_req(lport
, fp
);
1484 fc_rport_recv_plogi_req(lport
, fp
);
1487 fc_rport_recv_logo_req(lport
, fp
);
1495 fc_rport_recv_els_req(lport
, fp
);
1498 els_data
.reason
= ELS_RJT_UNSUP
;
1499 els_data
.explan
= ELS_EXPL_NONE
;
1500 lport
->tt
.seq_els_rsp_send(fp
, ELS_LS_RJT
, &els_data
);
1507 * fc_rport_recv_plogi_req() - Handler for Port Login (PLOGI) requests
1508 * @lport: The local port that received the PLOGI request
1509 * @rx_fp: The PLOGI request frame
1511 * Locking Note: The rport lock is held before calling this function.
1513 static void fc_rport_recv_plogi_req(struct fc_lport
*lport
,
1514 struct fc_frame
*rx_fp
)
1516 struct fc_disc
*disc
;
1517 struct fc_rport_priv
*rdata
;
1518 struct fc_frame
*fp
= rx_fp
;
1519 struct fc_els_flogi
*pl
;
1520 struct fc_seq_els_data rjt_data
;
1523 sid
= fc_frame_sid(fp
);
1525 FC_RPORT_ID_DBG(lport
, sid
, "Received PLOGI request\n");
1527 pl
= fc_frame_payload_get(fp
, sizeof(*pl
));
1529 FC_RPORT_ID_DBG(lport
, sid
, "Received PLOGI too short\n");
1530 rjt_data
.reason
= ELS_RJT_PROT
;
1531 rjt_data
.explan
= ELS_EXPL_INV_LEN
;
1535 disc
= &lport
->disc
;
1536 mutex_lock(&disc
->disc_mutex
);
1537 rdata
= lport
->tt
.rport_create(lport
, sid
);
1539 mutex_unlock(&disc
->disc_mutex
);
1540 rjt_data
.reason
= ELS_RJT_UNAB
;
1541 rjt_data
.explan
= ELS_EXPL_INSUF_RES
;
1545 mutex_lock(&rdata
->rp_mutex
);
1546 mutex_unlock(&disc
->disc_mutex
);
1548 rdata
->ids
.port_name
= get_unaligned_be64(&pl
->fl_wwpn
);
1549 rdata
->ids
.node_name
= get_unaligned_be64(&pl
->fl_wwnn
);
1552 * If the rport was just created, possibly due to the incoming PLOGI,
1553 * set the state appropriately and accept the PLOGI.
1555 * If we had also sent a PLOGI, and if the received PLOGI is from a
1556 * higher WWPN, we accept it, otherwise an LS_RJT is sent with reason
1557 * "command already in progress".
1559 * XXX TBD: If the session was ready before, the PLOGI should result in
1560 * all outstanding exchanges being reset.
1562 switch (rdata
->rp_state
) {
1564 FC_RPORT_DBG(rdata
, "Received PLOGI in INIT state\n");
1566 case RPORT_ST_PLOGI_WAIT
:
1567 FC_RPORT_DBG(rdata
, "Received PLOGI in PLOGI_WAIT state\n");
1569 case RPORT_ST_PLOGI
:
1570 FC_RPORT_DBG(rdata
, "Received PLOGI in PLOGI state\n");
1571 if (rdata
->ids
.port_name
< lport
->wwpn
) {
1572 mutex_unlock(&rdata
->rp_mutex
);
1573 rjt_data
.reason
= ELS_RJT_INPROG
;
1574 rjt_data
.explan
= ELS_EXPL_NONE
;
1580 case RPORT_ST_READY
:
1581 case RPORT_ST_ADISC
:
1582 FC_RPORT_DBG(rdata
, "Received PLOGI in logged-in state %d "
1583 "- ignored for now\n", rdata
->rp_state
);
1584 /* XXX TBD - should reset */
1586 case RPORT_ST_FLOGI
:
1587 case RPORT_ST_DELETE
:
1588 FC_RPORT_DBG(rdata
, "Received PLOGI in state %s - send busy\n",
1589 fc_rport_state(rdata
));
1590 mutex_unlock(&rdata
->rp_mutex
);
1591 rjt_data
.reason
= ELS_RJT_BUSY
;
1592 rjt_data
.explan
= ELS_EXPL_NONE
;
1597 * Get session payload size from incoming PLOGI.
1599 rdata
->maxframe_size
= fc_plogi_get_maxframe(pl
, lport
->mfs
);
1602 * Send LS_ACC. If this fails, the originator should retry.
1604 fp
= fc_frame_alloc(lport
, sizeof(*pl
));
1608 fc_plogi_fill(lport
, fp
, ELS_LS_ACC
);
1609 fc_fill_reply_hdr(fp
, rx_fp
, FC_RCTL_ELS_REP
, 0);
1610 lport
->tt
.frame_send(lport
, fp
);
1611 fc_rport_enter_prli(rdata
);
1613 mutex_unlock(&rdata
->rp_mutex
);
1614 fc_frame_free(rx_fp
);
1618 lport
->tt
.seq_els_rsp_send(fp
, ELS_LS_RJT
, &rjt_data
);
1623 * fc_rport_recv_prli_req() - Handler for process login (PRLI) requests
1624 * @rdata: The remote port that sent the PRLI request
1625 * @rx_fp: The PRLI request frame
1627 * Locking Note: The rport lock is exected to be held before calling
1630 static void fc_rport_recv_prli_req(struct fc_rport_priv
*rdata
,
1631 struct fc_frame
*rx_fp
)
1633 struct fc_lport
*lport
= rdata
->local_port
;
1634 struct fc_frame
*fp
;
1636 struct fc_els_prli prli
;
1637 struct fc_els_spp spp
;
1639 struct fc_els_spp
*rspp
; /* request service param page */
1640 struct fc_els_spp
*spp
; /* response spp */
1643 enum fc_els_spp_resp resp
;
1644 struct fc_seq_els_data rjt_data
;
1646 u32 roles
= FC_RPORT_ROLE_UNKNOWN
;
1648 FC_RPORT_DBG(rdata
, "Received PRLI request while in state %s\n",
1649 fc_rport_state(rdata
));
1651 len
= fr_len(rx_fp
) - sizeof(struct fc_frame_header
);
1652 pp
= fc_frame_payload_get(rx_fp
, sizeof(*pp
));
1655 plen
= ntohs(pp
->prli
.prli_len
);
1656 if ((plen
% 4) != 0 || plen
> len
|| plen
< 16)
1660 plen
= pp
->prli
.prli_spp_len
;
1661 if ((plen
% 4) != 0 || plen
< sizeof(*spp
) ||
1662 plen
> len
|| len
< sizeof(*pp
) || plen
< 12)
1666 fp
= fc_frame_alloc(lport
, len
);
1668 rjt_data
.reason
= ELS_RJT_UNAB
;
1669 rjt_data
.explan
= ELS_EXPL_INSUF_RES
;
1672 pp
= fc_frame_payload_get(fp
, len
);
1675 pp
->prli
.prli_cmd
= ELS_LS_ACC
;
1676 pp
->prli
.prli_spp_len
= plen
;
1677 pp
->prli
.prli_len
= htons(len
);
1678 len
-= sizeof(struct fc_els_prli
);
1680 /* reinitialize remote port roles */
1681 rdata
->ids
.roles
= FC_RPORT_ROLE_UNKNOWN
;
1684 * Go through all the service parameter pages and build
1685 * response. If plen indicates longer SPP than standard,
1686 * use that. The entire response has been pre-cleared above.
1689 while (len
>= plen
) {
1690 spp
->spp_type
= rspp
->spp_type
;
1691 spp
->spp_type_ext
= rspp
->spp_type_ext
;
1692 spp
->spp_flags
= rspp
->spp_flags
& FC_SPP_EST_IMG_PAIR
;
1693 resp
= FC_SPP_RESP_ACK
;
1695 switch (rspp
->spp_type
) {
1696 case 0: /* common to all FC-4 types */
1699 fcp_parm
= ntohl(rspp
->spp_params
);
1700 if (fcp_parm
& FCP_SPPF_RETRY
)
1701 rdata
->flags
|= FC_RP_FLAGS_RETRY
;
1702 rdata
->supported_classes
= FC_COS_CLASS3
;
1703 if (fcp_parm
& FCP_SPPF_INIT_FCN
)
1704 roles
|= FC_RPORT_ROLE_FCP_INITIATOR
;
1705 if (fcp_parm
& FCP_SPPF_TARG_FCN
)
1706 roles
|= FC_RPORT_ROLE_FCP_TARGET
;
1707 rdata
->ids
.roles
= roles
;
1709 spp
->spp_params
= htonl(lport
->service_params
);
1712 resp
= FC_SPP_RESP_INVL
;
1715 spp
->spp_flags
|= resp
;
1717 rspp
= (struct fc_els_spp
*)((char *)rspp
+ plen
);
1718 spp
= (struct fc_els_spp
*)((char *)spp
+ plen
);
1722 * Send LS_ACC. If this fails, the originator should retry.
1724 fc_fill_reply_hdr(fp
, rx_fp
, FC_RCTL_ELS_REP
, 0);
1725 lport
->tt
.frame_send(lport
, fp
);
1727 switch (rdata
->rp_state
) {
1729 fc_rport_enter_ready(rdata
);
1737 rjt_data
.reason
= ELS_RJT_PROT
;
1738 rjt_data
.explan
= ELS_EXPL_INV_LEN
;
1740 lport
->tt
.seq_els_rsp_send(rx_fp
, ELS_LS_RJT
, &rjt_data
);
1742 fc_frame_free(rx_fp
);
1746 * fc_rport_recv_prlo_req() - Handler for process logout (PRLO) requests
1747 * @rdata: The remote port that sent the PRLO request
1748 * @rx_fp: The PRLO request frame
1750 * Locking Note: The rport lock is exected to be held before calling
1753 static void fc_rport_recv_prlo_req(struct fc_rport_priv
*rdata
,
1754 struct fc_frame
*rx_fp
)
1756 struct fc_lport
*lport
= rdata
->local_port
;
1757 struct fc_frame
*fp
;
1759 struct fc_els_prlo prlo
;
1760 struct fc_els_spp spp
;
1762 struct fc_els_spp
*rspp
; /* request service param page */
1763 struct fc_els_spp
*spp
; /* response spp */
1766 struct fc_seq_els_data rjt_data
;
1768 FC_RPORT_DBG(rdata
, "Received PRLO request while in state %s\n",
1769 fc_rport_state(rdata
));
1771 len
= fr_len(rx_fp
) - sizeof(struct fc_frame_header
);
1772 pp
= fc_frame_payload_get(rx_fp
, sizeof(*pp
));
1775 plen
= ntohs(pp
->prlo
.prlo_len
);
1783 fp
= fc_frame_alloc(lport
, len
);
1785 rjt_data
.reason
= ELS_RJT_UNAB
;
1786 rjt_data
.explan
= ELS_EXPL_INSUF_RES
;
1790 pp
= fc_frame_payload_get(fp
, len
);
1793 pp
->prlo
.prlo_cmd
= ELS_LS_ACC
;
1794 pp
->prlo
.prlo_obs
= 0x10;
1795 pp
->prlo
.prlo_len
= htons(len
);
1797 spp
->spp_type
= rspp
->spp_type
;
1798 spp
->spp_type_ext
= rspp
->spp_type_ext
;
1799 spp
->spp_flags
= FC_SPP_RESP_ACK
;
1801 fc_rport_enter_delete(rdata
, RPORT_EV_LOGO
);
1803 fc_fill_reply_hdr(fp
, rx_fp
, FC_RCTL_ELS_REP
, 0);
1804 lport
->tt
.frame_send(lport
, fp
);
1808 rjt_data
.reason
= ELS_RJT_PROT
;
1809 rjt_data
.explan
= ELS_EXPL_INV_LEN
;
1811 lport
->tt
.seq_els_rsp_send(rx_fp
, ELS_LS_RJT
, &rjt_data
);
1813 fc_frame_free(rx_fp
);
1817 * fc_rport_recv_logo_req() - Handler for logout (LOGO) requests
1818 * @lport: The local port that received the LOGO request
1819 * @fp: The LOGO request frame
1821 * Locking Note: The rport lock is exected to be held before calling
1824 static void fc_rport_recv_logo_req(struct fc_lport
*lport
, struct fc_frame
*fp
)
1826 struct fc_rport_priv
*rdata
;
1829 lport
->tt
.seq_els_rsp_send(fp
, ELS_LS_ACC
, NULL
);
1831 sid
= fc_frame_sid(fp
);
1833 mutex_lock(&lport
->disc
.disc_mutex
);
1834 rdata
= lport
->tt
.rport_lookup(lport
, sid
);
1836 mutex_lock(&rdata
->rp_mutex
);
1837 FC_RPORT_DBG(rdata
, "Received LOGO request while in state %s\n",
1838 fc_rport_state(rdata
));
1840 fc_rport_enter_delete(rdata
, RPORT_EV_LOGO
);
1841 mutex_unlock(&rdata
->rp_mutex
);
1843 FC_RPORT_ID_DBG(lport
, sid
,
1844 "Received LOGO from non-logged-in port\n");
1845 mutex_unlock(&lport
->disc
.disc_mutex
);
1850 * fc_rport_flush_queue() - Flush the rport_event_queue
1852 static void fc_rport_flush_queue(void)
1854 flush_workqueue(rport_event_queue
);
1858 * fc_rport_init() - Initialize the remote port layer for a local port
1859 * @lport: The local port to initialize the remote port layer for
1861 int fc_rport_init(struct fc_lport
*lport
)
1863 if (!lport
->tt
.rport_lookup
)
1864 lport
->tt
.rport_lookup
= fc_rport_lookup
;
1866 if (!lport
->tt
.rport_create
)
1867 lport
->tt
.rport_create
= fc_rport_create
;
1869 if (!lport
->tt
.rport_login
)
1870 lport
->tt
.rport_login
= fc_rport_login
;
1872 if (!lport
->tt
.rport_logoff
)
1873 lport
->tt
.rport_logoff
= fc_rport_logoff
;
1875 if (!lport
->tt
.rport_recv_req
)
1876 lport
->tt
.rport_recv_req
= fc_rport_recv_req
;
1878 if (!lport
->tt
.rport_flush_queue
)
1879 lport
->tt
.rport_flush_queue
= fc_rport_flush_queue
;
1881 if (!lport
->tt
.rport_destroy
)
1882 lport
->tt
.rport_destroy
= fc_rport_destroy
;
1886 EXPORT_SYMBOL(fc_rport_init
);
1889 * fc_setup_rport() - Initialize the rport_event_queue
1891 int fc_setup_rport()
1893 rport_event_queue
= create_singlethread_workqueue("fc_rport_eq");
1894 if (!rport_event_queue
)
1900 * fc_destroy_rport() - Destroy the rport_event_queue
1902 void fc_destroy_rport()
1904 destroy_workqueue(rport_event_queue
);
1908 * fc_rport_terminate_io() - Stop all outstanding I/O on a remote port
1909 * @rport: The remote port whose I/O should be terminated
1911 void fc_rport_terminate_io(struct fc_rport
*rport
)
1913 struct fc_rport_libfc_priv
*rpriv
= rport
->dd_data
;
1914 struct fc_lport
*lport
= rpriv
->local_port
;
1916 lport
->tt
.exch_mgr_reset(lport
, 0, rport
->port_id
);
1917 lport
->tt
.exch_mgr_reset(lport
, rport
->port_id
, 0);
1919 EXPORT_SYMBOL(fc_rport_terminate_io
);