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 heirarchy.
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/rcupdate.h>
51 #include <linux/timer.h>
52 #include <linux/workqueue.h>
53 #include <asm/unaligned.h>
55 #include <scsi/libfc.h>
56 #include <scsi/fc_encode.h>
58 static int fc_rport_debug;
60 #define FC_DEBUG_RPORT(fmt...) \
66 struct workqueue_struct *rport_event_queue;
68 static void fc_rport_enter_plogi(struct fc_rport *);
69 static void fc_rport_enter_prli(struct fc_rport *);
70 static void fc_rport_enter_rtv(struct fc_rport *);
71 static void fc_rport_enter_ready(struct fc_rport *);
72 static void fc_rport_enter_logo(struct fc_rport *);
74 static void fc_rport_recv_plogi_req(struct fc_rport *,
75 struct fc_seq *, struct fc_frame *);
76 static void fc_rport_recv_prli_req(struct fc_rport *,
77 struct fc_seq *, struct fc_frame *);
78 static void fc_rport_recv_prlo_req(struct fc_rport *,
79 struct fc_seq *, struct fc_frame *);
80 static void fc_rport_recv_logo_req(struct fc_rport *,
81 struct fc_seq *, struct fc_frame *);
82 static void fc_rport_timeout(struct work_struct *);
83 static void fc_rport_error(struct fc_rport *, struct fc_frame *);
84 static void fc_rport_error_retry(struct fc_rport *, struct fc_frame *);
85 static void fc_rport_work(struct work_struct *);
87 static const char *fc_rport_state_names[] = {
88 [RPORT_ST_NONE] = "None",
89 [RPORT_ST_INIT] = "Init",
90 [RPORT_ST_PLOGI] = "PLOGI",
91 [RPORT_ST_PRLI] = "PRLI",
92 [RPORT_ST_RTV] = "RTV",
93 [RPORT_ST_READY] = "Ready",
94 [RPORT_ST_LOGO] = "LOGO",
97 static void fc_rport_rogue_destroy(struct device *dev)
99 struct fc_rport *rport = dev_to_rport(dev);
100 FC_DEBUG_RPORT("Destroying rogue rport (%6x)\n", rport->port_id);
104 struct fc_rport *fc_rport_rogue_create(struct fc_disc_port *dp)
106 struct fc_rport *rport;
107 struct fc_rport_libfc_priv *rdata;
108 rport = kzalloc(sizeof(*rport) + sizeof(*rdata), GFP_KERNEL);
113 rdata = RPORT_TO_PRIV(rport);
115 rport->dd_data = rdata;
116 rport->port_id = dp->ids.port_id;
117 rport->port_name = dp->ids.port_name;
118 rport->node_name = dp->ids.node_name;
119 rport->roles = dp->ids.roles;
120 rport->maxframe_size = FC_MIN_MAX_PAYLOAD;
122 * Note: all this libfc rogue rport code will be removed for
123 * upstream so it fine that this is really ugly and hacky right now.
125 device_initialize(&rport->dev);
126 rport->dev.release = fc_rport_rogue_destroy;
128 mutex_init(&rdata->rp_mutex);
129 rdata->local_port = dp->lp;
130 rdata->trans_state = FC_PORTSTATE_ROGUE;
131 rdata->rp_state = RPORT_ST_INIT;
132 rdata->event = RPORT_EV_NONE;
133 rdata->flags = FC_RP_FLAGS_REC_SUPPORTED;
135 rdata->e_d_tov = dp->lp->e_d_tov;
136 rdata->r_a_tov = dp->lp->r_a_tov;
137 INIT_DELAYED_WORK(&rdata->retry_work, fc_rport_timeout);
138 INIT_WORK(&rdata->event_work, fc_rport_work);
140 * For good measure, but not necessary as we should only
141 * add REAL rport to the lport list.
143 INIT_LIST_HEAD(&rdata->peers);
149 * fc_rport_state() - return a string for the state the rport is in
150 * @rport: The rport whose state we want to get a string for
152 static const char *fc_rport_state(struct fc_rport *rport)
155 struct fc_rport_libfc_priv *rdata = rport->dd_data;
157 cp = fc_rport_state_names[rdata->rp_state];
164 * fc_set_rport_loss_tmo() - Set the remote port loss timeout in seconds.
165 * @rport: Pointer to Fibre Channel remote port structure
166 * @timeout: timeout in seconds
168 void fc_set_rport_loss_tmo(struct fc_rport *rport, u32 timeout)
171 rport->dev_loss_tmo = timeout + 5;
173 rport->dev_loss_tmo = 30;
175 EXPORT_SYMBOL(fc_set_rport_loss_tmo);
178 * fc_plogi_get_maxframe() - Get max payload from the common service parameters
179 * @flp: FLOGI payload structure
180 * @maxval: upper limit, may be less than what is in the service parameters
182 static unsigned int fc_plogi_get_maxframe(struct fc_els_flogi *flp,
188 * Get max payload from the common service parameters and the
189 * class 3 receive data field size.
191 mfs = ntohs(flp->fl_csp.sp_bb_data) & FC_SP_BB_DATA_MASK;
192 if (mfs >= FC_SP_MIN_MAX_PAYLOAD && mfs < maxval)
194 mfs = ntohs(flp->fl_cssp[3 - 1].cp_rdfs);
195 if (mfs >= FC_SP_MIN_MAX_PAYLOAD && mfs < maxval)
201 * fc_rport_state_enter() - Change the rport's state
202 * @rport: The rport whose state should change
203 * @new: The new state of the rport
205 * Locking Note: Called with the rport lock held
207 static void fc_rport_state_enter(struct fc_rport *rport,
208 enum fc_rport_state new)
210 struct fc_rport_libfc_priv *rdata = rport->dd_data;
211 if (rdata->rp_state != new)
213 rdata->rp_state = new;
216 static void fc_rport_work(struct work_struct *work)
219 struct fc_rport_libfc_priv *rdata =
220 container_of(work, struct fc_rport_libfc_priv, event_work);
221 enum fc_rport_event event;
222 enum fc_rport_trans_state trans_state;
223 struct fc_lport *lport = rdata->local_port;
224 struct fc_rport_operations *rport_ops;
225 struct fc_rport *rport = PRIV_TO_RPORT(rdata);
227 mutex_lock(&rdata->rp_mutex);
228 event = rdata->event;
229 rport_ops = rdata->ops;
231 if (event == RPORT_EV_CREATED) {
232 struct fc_rport *new_rport;
233 struct fc_rport_libfc_priv *new_rdata;
234 struct fc_rport_identifiers ids;
236 ids.port_id = rport->port_id;
237 ids.roles = rport->roles;
238 ids.port_name = rport->port_name;
239 ids.node_name = rport->node_name;
241 mutex_unlock(&rdata->rp_mutex);
243 new_rport = fc_remote_port_add(lport->host, 0, &ids);
246 * Switch from the rogue rport to the rport
247 * returned by the FC class.
249 new_rport->maxframe_size = rport->maxframe_size;
251 new_rdata = new_rport->dd_data;
252 new_rdata->e_d_tov = rdata->e_d_tov;
253 new_rdata->r_a_tov = rdata->r_a_tov;
254 new_rdata->ops = rdata->ops;
255 new_rdata->local_port = rdata->local_port;
256 new_rdata->flags = FC_RP_FLAGS_REC_SUPPORTED;
257 new_rdata->trans_state = FC_PORTSTATE_REAL;
258 mutex_init(&new_rdata->rp_mutex);
259 INIT_DELAYED_WORK(&new_rdata->retry_work,
261 INIT_LIST_HEAD(&new_rdata->peers);
262 INIT_WORK(&new_rdata->event_work, fc_rport_work);
264 fc_rport_state_enter(new_rport, RPORT_ST_READY);
266 FC_DBG("Failed to create the rport for port "
267 "(%6x).\n", ids.port_id);
268 event = RPORT_EV_FAILED;
270 if (rport->port_id != FC_FID_DIR_SERV)
271 if (rport_ops->event_callback)
272 rport_ops->event_callback(lport, rport,
274 put_device(&rport->dev);
276 rdata = new_rport->dd_data;
277 if (rport_ops->event_callback)
278 rport_ops->event_callback(lport, rport, event);
279 } else if ((event == RPORT_EV_FAILED) ||
280 (event == RPORT_EV_LOGO) ||
281 (event == RPORT_EV_STOP)) {
282 trans_state = rdata->trans_state;
283 mutex_unlock(&rdata->rp_mutex);
284 if (rport_ops->event_callback)
285 rport_ops->event_callback(lport, rport, event);
286 if (trans_state == FC_PORTSTATE_ROGUE)
287 put_device(&rport->dev);
289 port_id = rport->port_id;
290 fc_remote_port_delete(rport);
291 lport->tt.exch_mgr_reset(lport, 0, port_id);
292 lport->tt.exch_mgr_reset(lport, port_id, 0);
295 mutex_unlock(&rdata->rp_mutex);
299 * fc_rport_login() - Start the remote port login state machine
300 * @rport: Fibre Channel remote port
302 * Locking Note: Called without the rport lock held. This
303 * function will hold the rport lock, call an _enter_*
304 * function and then unlock the rport.
306 int fc_rport_login(struct fc_rport *rport)
308 struct fc_rport_libfc_priv *rdata = rport->dd_data;
310 mutex_lock(&rdata->rp_mutex);
312 FC_DEBUG_RPORT("Login to port (%6x)\n", rport->port_id);
314 fc_rport_enter_plogi(rport);
316 mutex_unlock(&rdata->rp_mutex);
322 * fc_rport_logoff() - Logoff and remove an rport
323 * @rport: Fibre Channel remote port to be removed
325 * Locking Note: Called without the rport lock held. This
326 * function will hold the rport lock, call an _enter_*
327 * function and then unlock the rport.
329 int fc_rport_logoff(struct fc_rport *rport)
331 struct fc_rport_libfc_priv *rdata = rport->dd_data;
332 struct fc_lport *lport = rdata->local_port;
334 mutex_lock(&rdata->rp_mutex);
336 FC_DEBUG_RPORT("Remove port (%6x)\n", rport->port_id);
338 if (rdata->rp_state == RPORT_ST_NONE) {
339 FC_DEBUG_RPORT("(%6x): Port (%6x) in NONE state,"
340 " not removing", fc_host_port_id(lport->host),
342 mutex_unlock(&rdata->rp_mutex);
346 fc_rport_enter_logo(rport);
349 * Change the state to NONE so that we discard
352 fc_rport_state_enter(rport, RPORT_ST_NONE);
354 mutex_unlock(&rdata->rp_mutex);
356 cancel_delayed_work_sync(&rdata->retry_work);
358 mutex_lock(&rdata->rp_mutex);
360 rdata->event = RPORT_EV_STOP;
361 queue_work(rport_event_queue, &rdata->event_work);
363 mutex_unlock(&rdata->rp_mutex);
370 * fc_rport_enter_ready() - The rport is ready
371 * @rport: Fibre Channel remote port that is ready
373 * Locking Note: The rport lock is expected to be held before calling
376 static void fc_rport_enter_ready(struct fc_rport *rport)
378 struct fc_rport_libfc_priv *rdata = rport->dd_data;
380 fc_rport_state_enter(rport, RPORT_ST_READY);
382 FC_DEBUG_RPORT("Port (%6x) is Ready\n", rport->port_id);
384 rdata->event = RPORT_EV_CREATED;
385 queue_work(rport_event_queue, &rdata->event_work);
389 * fc_rport_timeout() - Handler for the retry_work timer.
390 * @work: The work struct of the fc_rport_libfc_priv
392 * Locking Note: Called without the rport lock held. This
393 * function will hold the rport lock, call an _enter_*
394 * function and then unlock the rport.
396 static void fc_rport_timeout(struct work_struct *work)
398 struct fc_rport_libfc_priv *rdata =
399 container_of(work, struct fc_rport_libfc_priv, retry_work.work);
400 struct fc_rport *rport = PRIV_TO_RPORT(rdata);
402 mutex_lock(&rdata->rp_mutex);
404 switch (rdata->rp_state) {
406 fc_rport_enter_plogi(rport);
409 fc_rport_enter_prli(rport);
412 fc_rport_enter_rtv(rport);
415 fc_rport_enter_logo(rport);
423 mutex_unlock(&rdata->rp_mutex);
424 put_device(&rport->dev);
428 * fc_rport_error() - Error handler, called once retries have been exhausted
429 * @rport: The fc_rport object
430 * @fp: The frame pointer
432 * Locking Note: The rport lock is expected to be held before
433 * calling this routine
435 static void fc_rport_error(struct fc_rport *rport, struct fc_frame *fp)
437 struct fc_rport_libfc_priv *rdata = rport->dd_data;
439 FC_DEBUG_RPORT("Error %ld in state %s, retries %d\n",
440 PTR_ERR(fp), fc_rport_state(rport), rdata->retries);
442 switch (rdata->rp_state) {
446 rdata->event = RPORT_EV_FAILED;
447 fc_rport_state_enter(rport, RPORT_ST_NONE);
448 queue_work(rport_event_queue,
452 fc_rport_enter_ready(rport);
462 * fc_rport_error_retry() - Error handler when retries are desired
463 * @rport: The fc_rport object
464 * @fp: The frame pointer
466 * If the error was an exchange timeout retry immediately,
467 * otherwise wait for E_D_TOV.
469 * Locking Note: The rport lock is expected to be held before
470 * calling this routine
472 static void fc_rport_error_retry(struct fc_rport *rport, struct fc_frame *fp)
474 struct fc_rport_libfc_priv *rdata = rport->dd_data;
475 unsigned long delay = FC_DEF_E_D_TOV;
477 /* make sure this isn't an FC_EX_CLOSED error, never retry those */
478 if (PTR_ERR(fp) == -FC_EX_CLOSED)
479 return fc_rport_error(rport, fp);
481 if (rdata->retries < rdata->local_port->max_retry_count) {
482 FC_DEBUG_RPORT("Error %ld in state %s, retrying\n",
483 PTR_ERR(fp), fc_rport_state(rport));
485 /* no additional delay on exchange timeouts */
486 if (PTR_ERR(fp) == -FC_EX_TIMEOUT)
488 get_device(&rport->dev);
489 schedule_delayed_work(&rdata->retry_work, delay);
493 return fc_rport_error(rport, fp);
497 * fc_rport_plogi_recv_resp() - Handle incoming ELS PLOGI response
498 * @sp: current sequence in the PLOGI exchange
499 * @fp: response frame
500 * @rp_arg: Fibre Channel remote port
502 * Locking Note: This function will be called without the rport lock
503 * held, but it will lock, call an _enter_* function or fc_rport_error
504 * and then unlock the rport.
506 static void fc_rport_plogi_resp(struct fc_seq *sp, struct fc_frame *fp,
509 struct fc_rport *rport = rp_arg;
510 struct fc_rport_libfc_priv *rdata = rport->dd_data;
511 struct fc_lport *lport = rdata->local_port;
512 struct fc_els_flogi *plp = NULL;
518 mutex_lock(&rdata->rp_mutex);
520 FC_DEBUG_RPORT("Received a PLOGI response from port (%6x)\n",
523 if (rdata->rp_state != RPORT_ST_PLOGI) {
524 FC_DBG("Received a PLOGI response, but in state %s\n",
525 fc_rport_state(rport));
532 fc_rport_error_retry(rport, fp);
536 op = fc_frame_payload_op(fp);
537 if (op == ELS_LS_ACC &&
538 (plp = fc_frame_payload_get(fp, sizeof(*plp))) != NULL) {
539 rport->port_name = get_unaligned_be64(&plp->fl_wwpn);
540 rport->node_name = get_unaligned_be64(&plp->fl_wwnn);
542 tov = ntohl(plp->fl_csp.sp_e_d_tov);
543 if (ntohs(plp->fl_csp.sp_features) & FC_SP_FT_EDTR)
545 if (tov > rdata->e_d_tov)
546 rdata->e_d_tov = tov;
547 csp_seq = ntohs(plp->fl_csp.sp_tot_seq);
548 cssp_seq = ntohs(plp->fl_cssp[3 - 1].cp_con_seq);
549 if (cssp_seq < csp_seq)
551 rdata->max_seq = csp_seq;
552 rport->maxframe_size =
553 fc_plogi_get_maxframe(plp, lport->mfs);
556 * If the rport is one of the well known addresses
557 * we skip PRLI and RTV and go straight to READY.
559 if (rport->port_id >= FC_FID_DOM_MGR)
560 fc_rport_enter_ready(rport);
562 fc_rport_enter_prli(rport);
564 fc_rport_error_retry(rport, fp);
569 mutex_unlock(&rdata->rp_mutex);
570 put_device(&rport->dev);
574 * fc_rport_enter_plogi() - Send Port Login (PLOGI) request to peer
575 * @rport: Fibre Channel remote port to send PLOGI to
577 * Locking Note: The rport lock is expected to be held before calling
580 static void fc_rport_enter_plogi(struct fc_rport *rport)
582 struct fc_rport_libfc_priv *rdata = rport->dd_data;
583 struct fc_lport *lport = rdata->local_port;
586 FC_DEBUG_RPORT("Port (%6x) entered PLOGI state from %s state\n",
587 rport->port_id, fc_rport_state(rport));
589 fc_rport_state_enter(rport, RPORT_ST_PLOGI);
591 rport->maxframe_size = FC_MIN_MAX_PAYLOAD;
592 fp = fc_frame_alloc(lport, sizeof(struct fc_els_flogi));
594 fc_rport_error_retry(rport, fp);
597 rdata->e_d_tov = lport->e_d_tov;
599 if (!lport->tt.elsct_send(lport, rport, fp, ELS_PLOGI,
600 fc_rport_plogi_resp, rport, lport->e_d_tov))
601 fc_rport_error_retry(rport, fp);
603 get_device(&rport->dev);
607 * fc_rport_prli_resp() - Process Login (PRLI) response handler
608 * @sp: current sequence in the PRLI exchange
609 * @fp: response frame
610 * @rp_arg: Fibre Channel remote port
612 * Locking Note: This function will be called without the rport lock
613 * held, but it will lock, call an _enter_* function or fc_rport_error
614 * and then unlock the rport.
616 static void fc_rport_prli_resp(struct fc_seq *sp, struct fc_frame *fp,
619 struct fc_rport *rport = rp_arg;
620 struct fc_rport_libfc_priv *rdata = rport->dd_data;
622 struct fc_els_prli prli;
623 struct fc_els_spp spp;
625 u32 roles = FC_RPORT_ROLE_UNKNOWN;
629 mutex_lock(&rdata->rp_mutex);
631 FC_DEBUG_RPORT("Received a PRLI response from port (%6x)\n",
634 if (rdata->rp_state != RPORT_ST_PRLI) {
635 FC_DBG("Received a PRLI response, but in state %s\n",
636 fc_rport_state(rport));
643 fc_rport_error_retry(rport, fp);
647 op = fc_frame_payload_op(fp);
648 if (op == ELS_LS_ACC) {
649 pp = fc_frame_payload_get(fp, sizeof(*pp));
650 if (pp && pp->prli.prli_spp_len >= sizeof(pp->spp)) {
651 fcp_parm = ntohl(pp->spp.spp_params);
652 if (fcp_parm & FCP_SPPF_RETRY)
653 rdata->flags |= FC_RP_FLAGS_RETRY;
656 rport->supported_classes = FC_COS_CLASS3;
657 if (fcp_parm & FCP_SPPF_INIT_FCN)
658 roles |= FC_RPORT_ROLE_FCP_INITIATOR;
659 if (fcp_parm & FCP_SPPF_TARG_FCN)
660 roles |= FC_RPORT_ROLE_FCP_TARGET;
662 rport->roles = roles;
663 fc_rport_enter_rtv(rport);
666 FC_DBG("Bad ELS response\n");
667 rdata->event = RPORT_EV_FAILED;
668 fc_rport_state_enter(rport, RPORT_ST_NONE);
669 queue_work(rport_event_queue, &rdata->event_work);
675 mutex_unlock(&rdata->rp_mutex);
676 put_device(&rport->dev);
680 * fc_rport_logo_resp() - Logout (LOGO) response handler
681 * @sp: current sequence in the LOGO exchange
682 * @fp: response frame
683 * @rp_arg: Fibre Channel remote port
685 * Locking Note: This function will be called without the rport lock
686 * held, but it will lock, call an _enter_* function or fc_rport_error
687 * and then unlock the rport.
689 static void fc_rport_logo_resp(struct fc_seq *sp, struct fc_frame *fp,
692 struct fc_rport *rport = rp_arg;
693 struct fc_rport_libfc_priv *rdata = rport->dd_data;
696 mutex_lock(&rdata->rp_mutex);
698 FC_DEBUG_RPORT("Received a LOGO response from port (%6x)\n",
701 if (rdata->rp_state != RPORT_ST_LOGO) {
702 FC_DEBUG_RPORT("Received a LOGO response, but in state %s\n",
703 fc_rport_state(rport));
710 fc_rport_error_retry(rport, fp);
714 op = fc_frame_payload_op(fp);
715 if (op == ELS_LS_ACC) {
716 fc_rport_enter_rtv(rport);
718 FC_DBG("Bad ELS response\n");
719 rdata->event = RPORT_EV_LOGO;
720 fc_rport_state_enter(rport, RPORT_ST_NONE);
721 queue_work(rport_event_queue, &rdata->event_work);
727 mutex_unlock(&rdata->rp_mutex);
728 put_device(&rport->dev);
732 * fc_rport_enter_prli() - Send Process Login (PRLI) request to peer
733 * @rport: Fibre Channel remote port to send PRLI to
735 * Locking Note: The rport lock is expected to be held before calling
738 static void fc_rport_enter_prli(struct fc_rport *rport)
740 struct fc_rport_libfc_priv *rdata = rport->dd_data;
741 struct fc_lport *lport = rdata->local_port;
743 struct fc_els_prli prli;
744 struct fc_els_spp spp;
748 FC_DEBUG_RPORT("Port (%6x) entered PRLI state from %s state\n",
749 rport->port_id, fc_rport_state(rport));
751 fc_rport_state_enter(rport, RPORT_ST_PRLI);
753 fp = fc_frame_alloc(lport, sizeof(*pp));
755 fc_rport_error_retry(rport, fp);
759 if (!lport->tt.elsct_send(lport, rport, fp, ELS_PRLI,
760 fc_rport_prli_resp, rport, lport->e_d_tov))
761 fc_rport_error_retry(rport, fp);
763 get_device(&rport->dev);
767 * fc_rport_els_rtv_resp() - Request Timeout Value response handler
768 * @sp: current sequence in the RTV exchange
769 * @fp: response frame
770 * @rp_arg: Fibre Channel remote port
772 * Many targets don't seem to support this.
774 * Locking Note: This function will be called without the rport lock
775 * held, but it will lock, call an _enter_* function or fc_rport_error
776 * and then unlock the rport.
778 static void fc_rport_rtv_resp(struct fc_seq *sp, struct fc_frame *fp,
781 struct fc_rport *rport = rp_arg;
782 struct fc_rport_libfc_priv *rdata = rport->dd_data;
785 mutex_lock(&rdata->rp_mutex);
787 FC_DEBUG_RPORT("Received a RTV response from port (%6x)\n",
790 if (rdata->rp_state != RPORT_ST_RTV) {
791 FC_DBG("Received a RTV response, but in state %s\n",
792 fc_rport_state(rport));
799 fc_rport_error(rport, fp);
803 op = fc_frame_payload_op(fp);
804 if (op == ELS_LS_ACC) {
805 struct fc_els_rtv_acc *rtv;
809 rtv = fc_frame_payload_get(fp, sizeof(*rtv));
811 toq = ntohl(rtv->rtv_toq);
812 tov = ntohl(rtv->rtv_r_a_tov);
815 rdata->r_a_tov = tov;
816 tov = ntohl(rtv->rtv_e_d_tov);
817 if (toq & FC_ELS_RTV_EDRES)
821 rdata->e_d_tov = tov;
825 fc_rport_enter_ready(rport);
830 mutex_unlock(&rdata->rp_mutex);
831 put_device(&rport->dev);
835 * fc_rport_enter_rtv() - Send Request Timeout Value (RTV) request to peer
836 * @rport: Fibre Channel remote port to send RTV to
838 * Locking Note: The rport lock is expected to be held before calling
841 static void fc_rport_enter_rtv(struct fc_rport *rport)
844 struct fc_rport_libfc_priv *rdata = rport->dd_data;
845 struct fc_lport *lport = rdata->local_port;
847 FC_DEBUG_RPORT("Port (%6x) entered RTV state from %s state\n",
848 rport->port_id, fc_rport_state(rport));
850 fc_rport_state_enter(rport, RPORT_ST_RTV);
852 fp = fc_frame_alloc(lport, sizeof(struct fc_els_rtv));
854 fc_rport_error_retry(rport, fp);
858 if (!lport->tt.elsct_send(lport, rport, fp, ELS_RTV,
859 fc_rport_rtv_resp, rport, lport->e_d_tov))
860 fc_rport_error_retry(rport, fp);
862 get_device(&rport->dev);
866 * fc_rport_enter_logo() - Send Logout (LOGO) request to peer
867 * @rport: Fibre Channel remote port to send LOGO to
869 * Locking Note: The rport lock is expected to be held before calling
872 static void fc_rport_enter_logo(struct fc_rport *rport)
874 struct fc_rport_libfc_priv *rdata = rport->dd_data;
875 struct fc_lport *lport = rdata->local_port;
878 FC_DEBUG_RPORT("Port (%6x) entered LOGO state from %s state\n",
879 rport->port_id, fc_rport_state(rport));
881 fc_rport_state_enter(rport, RPORT_ST_LOGO);
883 fp = fc_frame_alloc(lport, sizeof(struct fc_els_logo));
885 fc_rport_error_retry(rport, fp);
889 if (!lport->tt.elsct_send(lport, rport, fp, ELS_LOGO,
890 fc_rport_logo_resp, rport, lport->e_d_tov))
891 fc_rport_error_retry(rport, fp);
893 get_device(&rport->dev);
898 * fc_rport_recv_req() - Receive a request from a rport
899 * @sp: current sequence in the PLOGI exchange
900 * @fp: response frame
901 * @rp_arg: Fibre Channel remote port
903 * Locking Note: Called without the rport lock held. This
904 * function will hold the rport lock, call an _enter_*
905 * function and then unlock the rport.
907 void fc_rport_recv_req(struct fc_seq *sp, struct fc_frame *fp,
908 struct fc_rport *rport)
910 struct fc_rport_libfc_priv *rdata = rport->dd_data;
911 struct fc_lport *lport = rdata->local_port;
913 struct fc_frame_header *fh;
914 struct fc_seq_els_data els_data;
917 mutex_lock(&rdata->rp_mutex);
920 els_data.explan = ELS_EXPL_NONE;
921 els_data.reason = ELS_RJT_NONE;
923 fh = fc_frame_header_get(fp);
925 if (fh->fh_r_ctl == FC_RCTL_ELS_REQ && fh->fh_type == FC_TYPE_ELS) {
926 op = fc_frame_payload_op(fp);
929 fc_rport_recv_plogi_req(rport, sp, fp);
932 fc_rport_recv_prli_req(rport, sp, fp);
935 fc_rport_recv_prlo_req(rport, sp, fp);
938 fc_rport_recv_logo_req(rport, sp, fp);
942 lport->tt.seq_els_rsp_send(sp, ELS_RRQ, &els_data);
946 lport->tt.seq_els_rsp_send(sp, ELS_REC, &els_data);
949 els_data.reason = ELS_RJT_UNSUP;
950 lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &els_data);
955 mutex_unlock(&rdata->rp_mutex);
959 * fc_rport_recv_plogi_req() - Handle incoming Port Login (PLOGI) request
960 * @rport: Fibre Channel remote port that initiated PLOGI
961 * @sp: current sequence in the PLOGI exchange
962 * @fp: PLOGI request frame
964 * Locking Note: The rport lock is exected to be held before calling
967 static void fc_rport_recv_plogi_req(struct fc_rport *rport,
968 struct fc_seq *sp, struct fc_frame *rx_fp)
970 struct fc_rport_libfc_priv *rdata = rport->dd_data;
971 struct fc_lport *lport = rdata->local_port;
972 struct fc_frame *fp = rx_fp;
974 struct fc_frame_header *fh;
975 struct fc_els_flogi *pl;
976 struct fc_seq_els_data rjt_data;
980 enum fc_els_rjt_reason reject = 0;
984 fh = fc_frame_header_get(fp);
986 FC_DEBUG_RPORT("Received PLOGI request from port (%6x) "
987 "while in state %s\n", ntoh24(fh->fh_s_id),
988 fc_rport_state(rport));
990 sid = ntoh24(fh->fh_s_id);
991 pl = fc_frame_payload_get(fp, sizeof(*pl));
993 FC_DBG("incoming PLOGI from %x too short\n", sid);
995 /* XXX TBD: send reject? */
999 wwpn = get_unaligned_be64(&pl->fl_wwpn);
1000 wwnn = get_unaligned_be64(&pl->fl_wwnn);
1003 * If the session was just created, possibly due to the incoming PLOGI,
1004 * set the state appropriately and accept the PLOGI.
1006 * If we had also sent a PLOGI, and if the received PLOGI is from a
1007 * higher WWPN, we accept it, otherwise an LS_RJT is sent with reason
1008 * "command already in progress".
1010 * XXX TBD: If the session was ready before, the PLOGI should result in
1011 * all outstanding exchanges being reset.
1013 switch (rdata->rp_state) {
1015 FC_DEBUG_RPORT("incoming PLOGI from %6x wwpn %llx state INIT "
1016 "- reject\n", sid, (unsigned long long)wwpn);
1017 reject = ELS_RJT_UNSUP;
1019 case RPORT_ST_PLOGI:
1020 FC_DEBUG_RPORT("incoming PLOGI from %x in PLOGI state %d\n",
1021 sid, rdata->rp_state);
1022 if (wwpn < lport->wwpn)
1023 reject = ELS_RJT_INPROG;
1026 case RPORT_ST_READY:
1027 FC_DEBUG_RPORT("incoming PLOGI from %x in logged-in state %d "
1028 "- ignored for now\n", sid, rdata->rp_state);
1029 /* XXX TBD - should reset */
1033 FC_DEBUG_RPORT("incoming PLOGI from %x in unexpected "
1034 "state %d\n", sid, rdata->rp_state);
1041 rjt_data.reason = reject;
1042 rjt_data.explan = ELS_EXPL_NONE;
1043 lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
1046 fp = fc_frame_alloc(lport, sizeof(*pl));
1049 rjt_data.reason = ELS_RJT_UNAB;
1050 rjt_data.explan = ELS_EXPL_NONE;
1051 lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
1054 sp = lport->tt.seq_start_next(sp);
1056 fc_rport_set_name(rport, wwpn, wwnn);
1059 * Get session payload size from incoming PLOGI.
1061 rport->maxframe_size =
1062 fc_plogi_get_maxframe(pl, lport->mfs);
1063 fc_frame_free(rx_fp);
1064 fc_plogi_fill(lport, fp, ELS_LS_ACC);
1067 * Send LS_ACC. If this fails,
1068 * the originator should retry.
1070 f_ctl = FC_FC_EX_CTX | FC_FC_LAST_SEQ;
1071 f_ctl |= FC_FC_END_SEQ | FC_FC_SEQ_INIT;
1072 ep = fc_seq_exch(sp);
1073 fc_fill_fc_hdr(fp, FC_RCTL_ELS_REP, ep->did, ep->sid,
1074 FC_TYPE_ELS, f_ctl, 0);
1075 lport->tt.seq_send(lport, sp, fp);
1076 if (rdata->rp_state == RPORT_ST_PLOGI)
1077 fc_rport_enter_prli(rport);
1083 * fc_rport_recv_prli_req() - Handle incoming Process Login (PRLI) request
1084 * @rport: Fibre Channel remote port that initiated PRLI
1085 * @sp: current sequence in the PRLI exchange
1086 * @fp: PRLI request frame
1088 * Locking Note: The rport lock is exected to be held before calling
1091 static void fc_rport_recv_prli_req(struct fc_rport *rport,
1092 struct fc_seq *sp, struct fc_frame *rx_fp)
1094 struct fc_rport_libfc_priv *rdata = rport->dd_data;
1095 struct fc_lport *lport = rdata->local_port;
1097 struct fc_frame *fp;
1098 struct fc_frame_header *fh;
1100 struct fc_els_prli prli;
1101 struct fc_els_spp spp;
1103 struct fc_els_spp *rspp; /* request service param page */
1104 struct fc_els_spp *spp; /* response spp */
1107 enum fc_els_rjt_reason reason = ELS_RJT_UNAB;
1108 enum fc_els_rjt_explan explan = ELS_EXPL_NONE;
1109 enum fc_els_spp_resp resp;
1110 struct fc_seq_els_data rjt_data;
1113 u32 roles = FC_RPORT_ROLE_UNKNOWN;
1116 fh = fc_frame_header_get(rx_fp);
1118 FC_DEBUG_RPORT("Received PRLI request from port (%6x) "
1119 "while in state %s\n", ntoh24(fh->fh_s_id),
1120 fc_rport_state(rport));
1122 switch (rdata->rp_state) {
1124 case RPORT_ST_READY:
1125 reason = ELS_RJT_NONE;
1128 fc_frame_free(rx_fp);
1132 len = fr_len(rx_fp) - sizeof(*fh);
1133 pp = fc_frame_payload_get(rx_fp, sizeof(*pp));
1135 reason = ELS_RJT_PROT;
1136 explan = ELS_EXPL_INV_LEN;
1138 plen = ntohs(pp->prli.prli_len);
1139 if ((plen % 4) != 0 || plen > len) {
1140 reason = ELS_RJT_PROT;
1141 explan = ELS_EXPL_INV_LEN;
1142 } else if (plen < len) {
1145 plen = pp->prli.prli_spp_len;
1146 if ((plen % 4) != 0 || plen < sizeof(*spp) ||
1147 plen > len || len < sizeof(*pp)) {
1148 reason = ELS_RJT_PROT;
1149 explan = ELS_EXPL_INV_LEN;
1153 if (reason != ELS_RJT_NONE ||
1154 (fp = fc_frame_alloc(lport, len)) == NULL) {
1155 rjt_data.reason = reason;
1156 rjt_data.explan = explan;
1157 lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
1159 sp = lport->tt.seq_start_next(sp);
1161 pp = fc_frame_payload_get(fp, len);
1164 pp->prli.prli_cmd = ELS_LS_ACC;
1165 pp->prli.prli_spp_len = plen;
1166 pp->prli.prli_len = htons(len);
1167 len -= sizeof(struct fc_els_prli);
1170 * Go through all the service parameter pages and build
1171 * response. If plen indicates longer SPP than standard,
1172 * use that. The entire response has been pre-cleared above.
1175 while (len >= plen) {
1176 spp->spp_type = rspp->spp_type;
1177 spp->spp_type_ext = rspp->spp_type_ext;
1178 spp->spp_flags = rspp->spp_flags & FC_SPP_EST_IMG_PAIR;
1179 resp = FC_SPP_RESP_ACK;
1180 if (rspp->spp_flags & FC_SPP_RPA_VAL)
1181 resp = FC_SPP_RESP_NO_PA;
1182 switch (rspp->spp_type) {
1183 case 0: /* common to all FC-4 types */
1186 fcp_parm = ntohl(rspp->spp_params);
1187 if (fcp_parm * FCP_SPPF_RETRY)
1188 rdata->flags |= FC_RP_FLAGS_RETRY;
1189 rport->supported_classes = FC_COS_CLASS3;
1190 if (fcp_parm & FCP_SPPF_INIT_FCN)
1191 roles |= FC_RPORT_ROLE_FCP_INITIATOR;
1192 if (fcp_parm & FCP_SPPF_TARG_FCN)
1193 roles |= FC_RPORT_ROLE_FCP_TARGET;
1194 rport->roles = roles;
1197 htonl(lport->service_params);
1200 resp = FC_SPP_RESP_INVL;
1203 spp->spp_flags |= resp;
1205 rspp = (struct fc_els_spp *)((char *)rspp + plen);
1206 spp = (struct fc_els_spp *)((char *)spp + plen);
1210 * Send LS_ACC. If this fails, the originator should retry.
1212 f_ctl = FC_FC_EX_CTX | FC_FC_LAST_SEQ;
1213 f_ctl |= FC_FC_END_SEQ | FC_FC_SEQ_INIT;
1214 ep = fc_seq_exch(sp);
1215 fc_fill_fc_hdr(fp, FC_RCTL_ELS_REP, ep->did, ep->sid,
1216 FC_TYPE_ELS, f_ctl, 0);
1217 lport->tt.seq_send(lport, sp, fp);
1220 * Get lock and re-check state.
1222 switch (rdata->rp_state) {
1224 fc_rport_enter_ready(rport);
1226 case RPORT_ST_READY:
1232 fc_frame_free(rx_fp);
1236 * fc_rport_recv_prlo_req() - Handle incoming Process Logout (PRLO) request
1237 * @rport: Fibre Channel remote port that initiated PRLO
1238 * @sp: current sequence in the PRLO exchange
1239 * @fp: PRLO request frame
1241 * Locking Note: The rport lock is exected to be held before calling
1244 static void fc_rport_recv_prlo_req(struct fc_rport *rport, struct fc_seq *sp,
1245 struct fc_frame *fp)
1247 struct fc_rport_libfc_priv *rdata = rport->dd_data;
1248 struct fc_lport *lport = rdata->local_port;
1250 struct fc_frame_header *fh;
1251 struct fc_seq_els_data rjt_data;
1253 fh = fc_frame_header_get(fp);
1255 FC_DEBUG_RPORT("Received PRLO request from port (%6x) "
1256 "while in state %s\n", ntoh24(fh->fh_s_id),
1257 fc_rport_state(rport));
1259 if (rdata->rp_state == RPORT_ST_NONE) {
1265 rjt_data.reason = ELS_RJT_UNAB;
1266 rjt_data.explan = ELS_EXPL_NONE;
1267 lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
1272 * fc_rport_recv_logo_req() - Handle incoming Logout (LOGO) request
1273 * @rport: Fibre Channel remote port that initiated LOGO
1274 * @sp: current sequence in the LOGO exchange
1275 * @fp: LOGO request frame
1277 * Locking Note: The rport lock is exected to be held before calling
1280 static void fc_rport_recv_logo_req(struct fc_rport *rport, struct fc_seq *sp,
1281 struct fc_frame *fp)
1283 struct fc_frame_header *fh;
1284 struct fc_rport_libfc_priv *rdata = rport->dd_data;
1285 struct fc_lport *lport = rdata->local_port;
1287 fh = fc_frame_header_get(fp);
1289 FC_DEBUG_RPORT("Received LOGO request from port (%6x) "
1290 "while in state %s\n", ntoh24(fh->fh_s_id),
1291 fc_rport_state(rport));
1293 if (rdata->rp_state == RPORT_ST_NONE) {
1298 rdata->event = RPORT_EV_LOGO;
1299 fc_rport_state_enter(rport, RPORT_ST_NONE);
1300 queue_work(rport_event_queue, &rdata->event_work);
1302 lport->tt.seq_els_rsp_send(sp, ELS_LS_ACC, NULL);
1306 static void fc_rport_flush_queue(void)
1308 flush_workqueue(rport_event_queue);
1312 int fc_rport_init(struct fc_lport *lport)
1314 if (!lport->tt.rport_create)
1315 lport->tt.rport_create = fc_rport_rogue_create;
1317 if (!lport->tt.rport_login)
1318 lport->tt.rport_login = fc_rport_login;
1320 if (!lport->tt.rport_logoff)
1321 lport->tt.rport_logoff = fc_rport_logoff;
1323 if (!lport->tt.rport_recv_req)
1324 lport->tt.rport_recv_req = fc_rport_recv_req;
1326 if (!lport->tt.rport_flush_queue)
1327 lport->tt.rport_flush_queue = fc_rport_flush_queue;
1331 EXPORT_SYMBOL(fc_rport_init);
1333 int fc_setup_rport()
1335 rport_event_queue = create_singlethread_workqueue("fc_rport_eq");
1336 if (!rport_event_queue)
1340 EXPORT_SYMBOL(fc_setup_rport);
1342 void fc_destroy_rport()
1344 destroy_workqueue(rport_event_queue);
1346 EXPORT_SYMBOL(fc_destroy_rport);
1348 void fc_rport_terminate_io(struct fc_rport *rport)
1350 struct fc_rport_libfc_priv *rdata = rport->dd_data;
1351 struct fc_lport *lport = rdata->local_port;
1353 lport->tt.exch_mgr_reset(lport, 0, rport->port_id);
1354 lport->tt.exch_mgr_reset(lport, rport->port_id, 0);
1356 EXPORT_SYMBOL(fc_rport_terminate_io);