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_work(struct work_struct *);
86 static const char *fc_rport_state_names[] = {
87 [RPORT_ST_NONE] = "None",
88 [RPORT_ST_INIT] = "Init",
89 [RPORT_ST_PLOGI] = "PLOGI",
90 [RPORT_ST_PRLI] = "PRLI",
91 [RPORT_ST_RTV] = "RTV",
92 [RPORT_ST_READY] = "Ready",
93 [RPORT_ST_LOGO] = "LOGO",
96 static void fc_rport_rogue_destroy(struct device *dev)
98 struct fc_rport *rport = dev_to_rport(dev);
99 FC_DEBUG_RPORT("Destroying rogue rport (%6x)\n", rport->port_id);
103 struct fc_rport *fc_rport_rogue_create(struct fc_disc_port *dp)
105 struct fc_rport *rport;
106 struct fc_rport_libfc_priv *rdata;
107 rport = kzalloc(sizeof(*rport) + sizeof(*rdata), GFP_KERNEL);
112 rdata = RPORT_TO_PRIV(rport);
114 rport->dd_data = rdata;
115 rport->port_id = dp->ids.port_id;
116 rport->port_name = dp->ids.port_name;
117 rport->node_name = dp->ids.node_name;
118 rport->roles = dp->ids.roles;
119 rport->maxframe_size = FC_MIN_MAX_PAYLOAD;
121 * Note: all this libfc rogue rport code will be removed for
122 * upstream so it fine that this is really ugly and hacky right now.
124 device_initialize(&rport->dev);
125 rport->dev.release = fc_rport_rogue_destroy;
127 mutex_init(&rdata->rp_mutex);
128 rdata->local_port = dp->lp;
129 rdata->trans_state = FC_PORTSTATE_ROGUE;
130 rdata->rp_state = RPORT_ST_INIT;
131 rdata->event = RPORT_EV_NONE;
132 rdata->flags = FC_RP_FLAGS_REC_SUPPORTED;
134 rdata->e_d_tov = dp->lp->e_d_tov;
135 rdata->r_a_tov = dp->lp->r_a_tov;
136 INIT_DELAYED_WORK(&rdata->retry_work, fc_rport_timeout);
137 INIT_WORK(&rdata->event_work, fc_rport_work);
139 * For good measure, but not necessary as we should only
140 * add REAL rport to the lport list.
142 INIT_LIST_HEAD(&rdata->peers);
148 * fc_rport_state - return a string for the state the rport is in
149 * @rport: The rport whose state we want to get a string for
151 static const char *fc_rport_state(struct fc_rport *rport)
154 struct fc_rport_libfc_priv *rdata = rport->dd_data;
156 cp = fc_rport_state_names[rdata->rp_state];
163 * fc_set_rport_loss_tmo - Set the remote port loss timeout in seconds.
164 * @rport: Pointer to Fibre Channel remote port structure
165 * @timeout: timeout in seconds
167 void fc_set_rport_loss_tmo(struct fc_rport *rport, u32 timeout)
170 rport->dev_loss_tmo = timeout + 5;
172 rport->dev_loss_tmo = 30;
174 EXPORT_SYMBOL(fc_set_rport_loss_tmo);
177 * fc_plogi_get_maxframe - Get max payload from the common service parameters
178 * @flp: FLOGI payload structure
179 * @maxval: upper limit, may be less than what is in the service parameters
182 fc_plogi_get_maxframe(struct fc_els_flogi *flp, unsigned int maxval)
187 * Get max payload from the common service parameters and the
188 * class 3 receive data field size.
190 mfs = ntohs(flp->fl_csp.sp_bb_data) & FC_SP_BB_DATA_MASK;
191 if (mfs >= FC_SP_MIN_MAX_PAYLOAD && mfs < maxval)
193 mfs = ntohs(flp->fl_cssp[3 - 1].cp_rdfs);
194 if (mfs >= FC_SP_MIN_MAX_PAYLOAD && mfs < maxval)
200 * fc_rport_state_enter - Change the rport's state
201 * @rport: The rport whose state should change
202 * @new: The new state of the rport
204 * Locking Note: Called with the rport lock held
206 static void fc_rport_state_enter(struct fc_rport *rport,
207 enum fc_rport_state new)
209 struct fc_rport_libfc_priv *rdata = rport->dd_data;
210 if (rdata->rp_state != new)
212 rdata->rp_state = new;
215 static void fc_rport_work(struct work_struct *work)
218 struct fc_rport_libfc_priv *rdata =
219 container_of(work, struct fc_rport_libfc_priv, event_work);
220 enum fc_rport_event event;
221 enum fc_rport_trans_state trans_state;
222 struct fc_lport *lport = rdata->local_port;
223 struct fc_rport_operations *rport_ops;
224 struct fc_rport *rport = PRIV_TO_RPORT(rdata);
226 mutex_lock(&rdata->rp_mutex);
227 event = rdata->event;
228 rport_ops = rdata->ops;
230 if (event == RPORT_EV_CREATED) {
231 struct fc_rport *new_rport;
232 struct fc_rport_libfc_priv *new_rdata;
233 struct fc_rport_identifiers ids;
235 ids.port_id = rport->port_id;
236 ids.roles = rport->roles;
237 ids.port_name = rport->port_name;
238 ids.node_name = rport->node_name;
240 mutex_unlock(&rdata->rp_mutex);
242 new_rport = fc_remote_port_add(lport->host, 0, &ids);
245 * Switch from the rogue rport to the rport
246 * returned by the FC class.
248 new_rport->maxframe_size = rport->maxframe_size;
250 new_rdata = new_rport->dd_data;
251 new_rdata->e_d_tov = rdata->e_d_tov;
252 new_rdata->r_a_tov = rdata->r_a_tov;
253 new_rdata->ops = rdata->ops;
254 new_rdata->local_port = rdata->local_port;
255 new_rdata->flags = FC_RP_FLAGS_REC_SUPPORTED;
256 new_rdata->trans_state = FC_PORTSTATE_REAL;
257 mutex_init(&new_rdata->rp_mutex);
258 INIT_DELAYED_WORK(&new_rdata->retry_work,
260 INIT_LIST_HEAD(&new_rdata->peers);
261 INIT_WORK(&new_rdata->event_work, fc_rport_work);
263 fc_rport_state_enter(new_rport, RPORT_ST_READY);
265 FC_DBG("Failed to create the rport for port "
266 "(%6x).\n", ids.port_id);
267 event = RPORT_EV_FAILED;
269 put_device(&rport->dev);
271 rdata = new_rport->dd_data;
272 if (rport_ops->event_callback)
273 rport_ops->event_callback(lport, rport, event);
274 } else if ((event == RPORT_EV_FAILED) ||
275 (event == RPORT_EV_LOGO) ||
276 (event == RPORT_EV_STOP)) {
277 trans_state = rdata->trans_state;
278 mutex_unlock(&rdata->rp_mutex);
279 if (rport_ops->event_callback)
280 rport_ops->event_callback(lport, rport, event);
281 if (trans_state == FC_PORTSTATE_ROGUE)
282 put_device(&rport->dev);
284 port_id = rport->port_id;
285 fc_remote_port_delete(rport);
286 lport->tt.exch_mgr_reset(lport, 0, port_id);
287 lport->tt.exch_mgr_reset(lport, port_id, 0);
290 mutex_unlock(&rdata->rp_mutex);
294 * fc_rport_login - Start the remote port login state machine
295 * @rport: Fibre Channel remote port
297 * Locking Note: Called without the rport lock held. This
298 * function will hold the rport lock, call an _enter_*
299 * function and then unlock the rport.
301 int fc_rport_login(struct fc_rport *rport)
303 struct fc_rport_libfc_priv *rdata = rport->dd_data;
305 mutex_lock(&rdata->rp_mutex);
307 FC_DEBUG_RPORT("Login to port (%6x)\n", rport->port_id);
309 fc_rport_enter_plogi(rport);
311 mutex_unlock(&rdata->rp_mutex);
317 * fc_rport_logoff - Logoff and remove an rport
318 * @rport: Fibre Channel remote port to be removed
320 * Locking Note: Called without the rport lock held. This
321 * function will hold the rport lock, call an _enter_*
322 * function and then unlock the rport.
324 int fc_rport_logoff(struct fc_rport *rport)
326 struct fc_rport_libfc_priv *rdata = rport->dd_data;
328 mutex_lock(&rdata->rp_mutex);
330 FC_DEBUG_RPORT("Remove port (%6x)\n", rport->port_id);
332 fc_rport_enter_logo(rport);
335 * Change the state to NONE so that we discard
338 fc_rport_state_enter(rport, RPORT_ST_NONE);
340 mutex_unlock(&rdata->rp_mutex);
342 cancel_delayed_work_sync(&rdata->retry_work);
344 mutex_lock(&rdata->rp_mutex);
346 rdata->event = RPORT_EV_STOP;
347 queue_work(rport_event_queue, &rdata->event_work);
349 mutex_unlock(&rdata->rp_mutex);
355 * fc_rport_enter_ready - The rport is ready
356 * @rport: Fibre Channel remote port that is ready
358 * Locking Note: The rport lock is expected to be held before calling
361 static void fc_rport_enter_ready(struct fc_rport *rport)
363 struct fc_rport_libfc_priv *rdata = rport->dd_data;
365 fc_rport_state_enter(rport, RPORT_ST_READY);
367 FC_DEBUG_RPORT("Port (%6x) is Ready\n", rport->port_id);
369 rdata->event = RPORT_EV_CREATED;
370 queue_work(rport_event_queue, &rdata->event_work);
374 * fc_rport_timeout - Handler for the retry_work timer.
375 * @work: The work struct of the fc_rport_libfc_priv
377 * Locking Note: Called without the rport lock held. This
378 * function will hold the rport lock, call an _enter_*
379 * function and then unlock the rport.
381 static void fc_rport_timeout(struct work_struct *work)
383 struct fc_rport_libfc_priv *rdata =
384 container_of(work, struct fc_rport_libfc_priv, retry_work.work);
385 struct fc_rport *rport = PRIV_TO_RPORT(rdata);
387 mutex_lock(&rdata->rp_mutex);
389 switch (rdata->rp_state) {
391 fc_rport_enter_plogi(rport);
394 fc_rport_enter_prli(rport);
397 fc_rport_enter_rtv(rport);
400 fc_rport_enter_logo(rport);
408 mutex_unlock(&rdata->rp_mutex);
409 put_device(&rport->dev);
413 * fc_rport_error - Handler for any errors
414 * @rport: The fc_rport object
415 * @fp: The frame pointer
417 * If the error was caused by a resource allocation failure
418 * then wait for half a second and retry, otherwise retry
421 * Locking Note: The rport lock is expected to be held before
422 * calling this routine
424 static void fc_rport_error(struct fc_rport *rport, struct fc_frame *fp)
426 struct fc_rport_libfc_priv *rdata = rport->dd_data;
427 unsigned long delay = 0;
429 FC_DEBUG_RPORT("Error %ld in state %s, retries %d\n",
430 PTR_ERR(fp), fc_rport_state(rport), rdata->retries);
432 if (!fp || PTR_ERR(fp) == -FC_EX_TIMEOUT) {
434 * Memory allocation failure, or the exchange timed out.
437 if (rdata->retries < rdata->local_port->max_retry_count) {
440 delay = msecs_to_jiffies(500);
441 get_device(&rport->dev);
442 schedule_delayed_work(&rdata->retry_work, delay);
444 switch (rdata->rp_state) {
448 rdata->event = RPORT_EV_FAILED;
449 queue_work(rport_event_queue,
453 fc_rport_enter_ready(rport);
465 * fc_rport_plogi_recv_resp - Handle incoming ELS PLOGI response
466 * @sp: current sequence in the PLOGI exchange
467 * @fp: response frame
468 * @rp_arg: Fibre Channel remote port
470 * Locking Note: This function will be called without the rport lock
471 * held, but it will lock, call an _enter_* function or fc_rport_error
472 * and then unlock the rport.
474 static void fc_rport_plogi_resp(struct fc_seq *sp, struct fc_frame *fp,
477 struct fc_rport *rport = rp_arg;
478 struct fc_rport_libfc_priv *rdata = rport->dd_data;
479 struct fc_lport *lport = rdata->local_port;
480 struct fc_els_flogi *plp;
486 mutex_lock(&rdata->rp_mutex);
488 FC_DEBUG_RPORT("Received a PLOGI response from port (%6x)\n",
491 if (rdata->rp_state != RPORT_ST_PLOGI) {
492 FC_DBG("Received a PLOGI response, but in state %s\n",
493 fc_rport_state(rport));
498 fc_rport_error(rport, fp);
502 op = fc_frame_payload_op(fp);
503 if (op == ELS_LS_ACC &&
504 (plp = fc_frame_payload_get(fp, sizeof(*plp))) != NULL) {
505 rport->port_name = get_unaligned_be64(&plp->fl_wwpn);
506 rport->node_name = get_unaligned_be64(&plp->fl_wwnn);
508 tov = ntohl(plp->fl_csp.sp_e_d_tov);
509 if (ntohs(plp->fl_csp.sp_features) & FC_SP_FT_EDTR)
511 if (tov > rdata->e_d_tov)
512 rdata->e_d_tov = tov;
513 csp_seq = ntohs(plp->fl_csp.sp_tot_seq);
514 cssp_seq = ntohs(plp->fl_cssp[3 - 1].cp_con_seq);
515 if (cssp_seq < csp_seq)
517 rdata->max_seq = csp_seq;
518 rport->maxframe_size =
519 fc_plogi_get_maxframe(plp, lport->mfs);
522 * If the rport is one of the well known addresses
523 * we skip PRLI and RTV and go straight to READY.
525 if (rport->port_id >= FC_FID_DOM_MGR)
526 fc_rport_enter_ready(rport);
528 fc_rport_enter_prli(rport);
530 fc_rport_error(rport, fp);
535 mutex_unlock(&rdata->rp_mutex);
536 put_device(&rport->dev);
540 * fc_rport_enter_plogi - Send Port Login (PLOGI) request to peer
541 * @rport: Fibre Channel remote port to send PLOGI to
543 * Locking Note: The rport lock is expected to be held before calling
546 static void fc_rport_enter_plogi(struct fc_rport *rport)
548 struct fc_rport_libfc_priv *rdata = rport->dd_data;
549 struct fc_lport *lport = rdata->local_port;
552 FC_DEBUG_RPORT("Port (%6x) entered PLOGI state from %s state\n",
553 rport->port_id, fc_rport_state(rport));
555 fc_rport_state_enter(rport, RPORT_ST_PLOGI);
557 rport->maxframe_size = FC_MIN_MAX_PAYLOAD;
558 fp = fc_frame_alloc(lport, sizeof(struct fc_els_flogi));
560 fc_rport_error(rport, fp);
563 rdata->e_d_tov = lport->e_d_tov;
565 if (!lport->tt.elsct_send(lport, rport, fp, ELS_PLOGI,
566 fc_rport_plogi_resp, rport, lport->e_d_tov))
567 fc_rport_error(rport, fp);
569 get_device(&rport->dev);
573 * fc_rport_prli_resp - Process Login (PRLI) response handler
574 * @sp: current sequence in the PRLI exchange
575 * @fp: response frame
576 * @rp_arg: Fibre Channel remote port
578 * Locking Note: This function will be called without the rport lock
579 * held, but it will lock, call an _enter_* function or fc_rport_error
580 * and then unlock the rport.
582 static void fc_rport_prli_resp(struct fc_seq *sp, struct fc_frame *fp,
585 struct fc_rport *rport = rp_arg;
586 struct fc_rport_libfc_priv *rdata = rport->dd_data;
588 struct fc_els_prli prli;
589 struct fc_els_spp spp;
591 u32 roles = FC_RPORT_ROLE_UNKNOWN;
595 mutex_lock(&rdata->rp_mutex);
597 FC_DEBUG_RPORT("Received a PRLI response from port (%6x)\n",
600 if (rdata->rp_state != RPORT_ST_PRLI) {
601 FC_DBG("Received a PRLI response, but in state %s\n",
602 fc_rport_state(rport));
607 fc_rport_error(rport, fp);
611 op = fc_frame_payload_op(fp);
612 if (op == ELS_LS_ACC) {
613 pp = fc_frame_payload_get(fp, sizeof(*pp));
614 if (pp && pp->prli.prli_spp_len >= sizeof(pp->spp)) {
615 fcp_parm = ntohl(pp->spp.spp_params);
616 if (fcp_parm & FCP_SPPF_RETRY)
617 rdata->flags |= FC_RP_FLAGS_RETRY;
620 rport->supported_classes = FC_COS_CLASS3;
621 if (fcp_parm & FCP_SPPF_INIT_FCN)
622 roles |= FC_RPORT_ROLE_FCP_INITIATOR;
623 if (fcp_parm & FCP_SPPF_TARG_FCN)
624 roles |= FC_RPORT_ROLE_FCP_TARGET;
626 rport->roles = roles;
627 fc_rport_enter_rtv(rport);
630 FC_DBG("Bad ELS response\n");
631 rdata->event = RPORT_EV_FAILED;
632 queue_work(rport_event_queue, &rdata->event_work);
638 mutex_unlock(&rdata->rp_mutex);
639 put_device(&rport->dev);
643 * fc_rport_logo_resp - Logout (LOGO) response handler
644 * @sp: current sequence in the LOGO exchange
645 * @fp: response frame
646 * @rp_arg: Fibre Channel remote port
648 * Locking Note: This function will be called without the rport lock
649 * held, but it will lock, call an _enter_* function or fc_rport_error
650 * and then unlock the rport.
652 static void fc_rport_logo_resp(struct fc_seq *sp, struct fc_frame *fp,
655 struct fc_rport *rport = rp_arg;
656 struct fc_rport_libfc_priv *rdata = rport->dd_data;
659 mutex_lock(&rdata->rp_mutex);
661 FC_DEBUG_RPORT("Received a LOGO response from port (%6x)\n",
665 fc_rport_error(rport, fp);
669 if (rdata->rp_state != RPORT_ST_LOGO) {
670 FC_DEBUG_RPORT("Received a LOGO response, but in state %s\n",
671 fc_rport_state(rport));
675 op = fc_frame_payload_op(fp);
676 if (op == ELS_LS_ACC) {
677 fc_rport_enter_rtv(rport);
679 FC_DBG("Bad ELS response\n");
680 rdata->event = RPORT_EV_LOGO;
681 queue_work(rport_event_queue, &rdata->event_work);
687 mutex_unlock(&rdata->rp_mutex);
688 put_device(&rport->dev);
692 * fc_rport_enter_prli - Send Process Login (PRLI) request to peer
693 * @rport: Fibre Channel remote port to send PRLI to
695 * Locking Note: The rport lock is expected to be held before calling
698 static void fc_rport_enter_prli(struct fc_rport *rport)
700 struct fc_rport_libfc_priv *rdata = rport->dd_data;
701 struct fc_lport *lport = rdata->local_port;
703 struct fc_els_prli prli;
704 struct fc_els_spp spp;
708 FC_DEBUG_RPORT("Port (%6x) entered PRLI state from %s state\n",
709 rport->port_id, fc_rport_state(rport));
711 fc_rport_state_enter(rport, RPORT_ST_PRLI);
713 fp = fc_frame_alloc(lport, sizeof(*pp));
715 fc_rport_error(rport, fp);
719 if (!lport->tt.elsct_send(lport, rport, fp, ELS_PRLI,
720 fc_rport_prli_resp, rport, lport->e_d_tov))
721 fc_rport_error(rport, fp);
723 get_device(&rport->dev);
727 * fc_rport_els_rtv_resp - Request Timeout Value response handler
728 * @sp: current sequence in the RTV exchange
729 * @fp: response frame
730 * @rp_arg: Fibre Channel remote port
732 * Many targets don't seem to support this.
734 * Locking Note: This function will be called without the rport lock
735 * held, but it will lock, call an _enter_* function or fc_rport_error
736 * and then unlock the rport.
738 static void fc_rport_rtv_resp(struct fc_seq *sp, struct fc_frame *fp,
741 struct fc_rport *rport = rp_arg;
742 struct fc_rport_libfc_priv *rdata = rport->dd_data;
745 mutex_lock(&rdata->rp_mutex);
747 FC_DEBUG_RPORT("Received a RTV response from port (%6x)\n",
750 if (rdata->rp_state != RPORT_ST_RTV) {
751 FC_DBG("Received a RTV response, but in state %s\n",
752 fc_rport_state(rport));
757 fc_rport_error(rport, fp);
761 op = fc_frame_payload_op(fp);
762 if (op == ELS_LS_ACC) {
763 struct fc_els_rtv_acc *rtv;
767 rtv = fc_frame_payload_get(fp, sizeof(*rtv));
769 toq = ntohl(rtv->rtv_toq);
770 tov = ntohl(rtv->rtv_r_a_tov);
773 rdata->r_a_tov = tov;
774 tov = ntohl(rtv->rtv_e_d_tov);
775 if (toq & FC_ELS_RTV_EDRES)
779 rdata->e_d_tov = tov;
783 fc_rport_enter_ready(rport);
788 mutex_unlock(&rdata->rp_mutex);
789 put_device(&rport->dev);
793 * fc_rport_enter_rtv - Send Request Timeout Value (RTV) request to peer
794 * @rport: Fibre Channel remote port to send RTV to
796 * Locking Note: The rport lock is expected to be held before calling
799 static void fc_rport_enter_rtv(struct fc_rport *rport)
802 struct fc_rport_libfc_priv *rdata = rport->dd_data;
803 struct fc_lport *lport = rdata->local_port;
805 FC_DEBUG_RPORT("Port (%6x) entered RTV state from %s state\n",
806 rport->port_id, fc_rport_state(rport));
808 fc_rport_state_enter(rport, RPORT_ST_RTV);
810 fp = fc_frame_alloc(lport, sizeof(struct fc_els_rtv));
812 fc_rport_error(rport, fp);
816 if (!lport->tt.elsct_send(lport, rport, fp, ELS_RTV,
817 fc_rport_rtv_resp, rport, lport->e_d_tov))
818 fc_rport_error(rport, fp);
820 get_device(&rport->dev);
824 * fc_rport_enter_logo - Send Logout (LOGO) request to peer
825 * @rport: Fibre Channel remote port to send LOGO to
827 * Locking Note: The rport lock is expected to be held before calling
830 static void fc_rport_enter_logo(struct fc_rport *rport)
832 struct fc_rport_libfc_priv *rdata = rport->dd_data;
833 struct fc_lport *lport = rdata->local_port;
836 FC_DEBUG_RPORT("Port (%6x) entered LOGO state from %s state\n",
837 rport->port_id, fc_rport_state(rport));
839 fc_rport_state_enter(rport, RPORT_ST_LOGO);
841 fp = fc_frame_alloc(lport, sizeof(struct fc_els_logo));
843 fc_rport_error(rport, fp);
847 if (!lport->tt.elsct_send(lport, rport, fp, ELS_LOGO,
848 fc_rport_logo_resp, rport, lport->e_d_tov))
849 fc_rport_error(rport, fp);
851 get_device(&rport->dev);
856 * fc_rport_recv_req - Receive a request from a rport
857 * @sp: current sequence in the PLOGI exchange
858 * @fp: response frame
859 * @rp_arg: Fibre Channel remote port
861 * Locking Note: Called without the rport lock held. This
862 * function will hold the rport lock, call an _enter_*
863 * function and then unlock the rport.
865 void fc_rport_recv_req(struct fc_seq *sp, struct fc_frame *fp,
866 struct fc_rport *rport)
868 struct fc_rport_libfc_priv *rdata = rport->dd_data;
869 struct fc_lport *lport = rdata->local_port;
871 struct fc_frame_header *fh;
872 struct fc_seq_els_data els_data;
875 mutex_lock(&rdata->rp_mutex);
878 els_data.explan = ELS_EXPL_NONE;
879 els_data.reason = ELS_RJT_NONE;
881 fh = fc_frame_header_get(fp);
883 if (fh->fh_r_ctl == FC_RCTL_ELS_REQ && fh->fh_type == FC_TYPE_ELS) {
884 op = fc_frame_payload_op(fp);
887 fc_rport_recv_plogi_req(rport, sp, fp);
890 fc_rport_recv_prli_req(rport, sp, fp);
893 fc_rport_recv_prlo_req(rport, sp, fp);
896 fc_rport_recv_logo_req(rport, sp, fp);
900 lport->tt.seq_els_rsp_send(sp, ELS_RRQ, &els_data);
904 lport->tt.seq_els_rsp_send(sp, ELS_REC, &els_data);
907 els_data.reason = ELS_RJT_UNSUP;
908 lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &els_data);
913 mutex_unlock(&rdata->rp_mutex);
917 * fc_rport_recv_plogi_req - Handle incoming Port Login (PLOGI) request
918 * @rport: Fibre Channel remote port that initiated PLOGI
919 * @sp: current sequence in the PLOGI exchange
920 * @fp: PLOGI request frame
922 * Locking Note: The rport lock is exected to be held before calling
925 static void fc_rport_recv_plogi_req(struct fc_rport *rport,
926 struct fc_seq *sp, struct fc_frame *rx_fp)
928 struct fc_rport_libfc_priv *rdata = rport->dd_data;
929 struct fc_lport *lport = rdata->local_port;
930 struct fc_frame *fp = rx_fp;
932 struct fc_frame_header *fh;
933 struct fc_els_flogi *pl;
934 struct fc_seq_els_data rjt_data;
938 enum fc_els_rjt_reason reject = 0;
942 fh = fc_frame_header_get(fp);
944 FC_DEBUG_RPORT("Received PLOGI request from port (%6x) "
945 "while in state %s\n", ntoh24(fh->fh_s_id),
946 fc_rport_state(rport));
948 sid = ntoh24(fh->fh_s_id);
949 pl = fc_frame_payload_get(fp, sizeof(*pl));
951 FC_DBG("incoming PLOGI from %x too short\n", sid);
953 /* XXX TBD: send reject? */
957 wwpn = get_unaligned_be64(&pl->fl_wwpn);
958 wwnn = get_unaligned_be64(&pl->fl_wwnn);
961 * If the session was just created, possibly due to the incoming PLOGI,
962 * set the state appropriately and accept the PLOGI.
964 * If we had also sent a PLOGI, and if the received PLOGI is from a
965 * higher WWPN, we accept it, otherwise an LS_RJT is sent with reason
966 * "command already in progress".
968 * XXX TBD: If the session was ready before, the PLOGI should result in
969 * all outstanding exchanges being reset.
971 switch (rdata->rp_state) {
973 FC_DEBUG_RPORT("incoming PLOGI from %6x wwpn %llx state INIT "
974 "- reject\n", sid, wwpn);
975 reject = ELS_RJT_UNSUP;
978 FC_DEBUG_RPORT("incoming PLOGI from %x in PLOGI state %d\n",
979 sid, rdata->rp_state);
980 if (wwpn < lport->wwpn)
981 reject = ELS_RJT_INPROG;
985 FC_DEBUG_RPORT("incoming PLOGI from %x in logged-in state %d "
986 "- ignored for now\n", sid, rdata->rp_state);
987 /* XXX TBD - should reset */
991 FC_DEBUG_RPORT("incoming PLOGI from %x in unexpected "
992 "state %d\n", sid, rdata->rp_state);
997 rjt_data.reason = reject;
998 rjt_data.explan = ELS_EXPL_NONE;
999 lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
1002 fp = fc_frame_alloc(lport, sizeof(*pl));
1005 rjt_data.reason = ELS_RJT_UNAB;
1006 rjt_data.explan = ELS_EXPL_NONE;
1007 lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
1010 sp = lport->tt.seq_start_next(sp);
1012 fc_rport_set_name(rport, wwpn, wwnn);
1015 * Get session payload size from incoming PLOGI.
1017 rport->maxframe_size =
1018 fc_plogi_get_maxframe(pl, lport->mfs);
1019 fc_frame_free(rx_fp);
1020 fc_plogi_fill(lport, fp, ELS_LS_ACC);
1023 * Send LS_ACC. If this fails,
1024 * the originator should retry.
1026 f_ctl = FC_FC_EX_CTX | FC_FC_LAST_SEQ;
1027 f_ctl |= FC_FC_END_SEQ | FC_FC_SEQ_INIT;
1028 ep = fc_seq_exch(sp);
1029 fc_fill_fc_hdr(fp, FC_RCTL_ELS_REP, ep->did, ep->sid,
1030 FC_TYPE_ELS, f_ctl, 0);
1031 lport->tt.seq_send(lport, sp, fp);
1032 if (rdata->rp_state == RPORT_ST_PLOGI)
1033 fc_rport_enter_prli(rport);
1039 * fc_rport_recv_prli_req - Handle incoming Process Login (PRLI) request
1040 * @rport: Fibre Channel remote port that initiated PRLI
1041 * @sp: current sequence in the PRLI exchange
1042 * @fp: PRLI request frame
1044 * Locking Note: The rport lock is exected to be held before calling
1047 static void fc_rport_recv_prli_req(struct fc_rport *rport,
1048 struct fc_seq *sp, struct fc_frame *rx_fp)
1050 struct fc_rport_libfc_priv *rdata = rport->dd_data;
1051 struct fc_lport *lport = rdata->local_port;
1053 struct fc_frame *fp;
1054 struct fc_frame_header *fh;
1056 struct fc_els_prli prli;
1057 struct fc_els_spp spp;
1059 struct fc_els_spp *rspp; /* request service param page */
1060 struct fc_els_spp *spp; /* response spp */
1063 enum fc_els_rjt_reason reason = ELS_RJT_UNAB;
1064 enum fc_els_rjt_explan explan = ELS_EXPL_NONE;
1065 enum fc_els_spp_resp resp;
1066 struct fc_seq_els_data rjt_data;
1069 u32 roles = FC_RPORT_ROLE_UNKNOWN;
1072 fh = fc_frame_header_get(rx_fp);
1074 FC_DEBUG_RPORT("Received PRLI request from port (%6x) "
1075 "while in state %s\n", ntoh24(fh->fh_s_id),
1076 fc_rport_state(rport));
1078 switch (rdata->rp_state) {
1080 case RPORT_ST_READY:
1081 reason = ELS_RJT_NONE;
1086 len = fr_len(rx_fp) - sizeof(*fh);
1087 pp = fc_frame_payload_get(rx_fp, sizeof(*pp));
1089 reason = ELS_RJT_PROT;
1090 explan = ELS_EXPL_INV_LEN;
1092 plen = ntohs(pp->prli.prli_len);
1093 if ((plen % 4) != 0 || plen > len) {
1094 reason = ELS_RJT_PROT;
1095 explan = ELS_EXPL_INV_LEN;
1096 } else if (plen < len) {
1099 plen = pp->prli.prli_spp_len;
1100 if ((plen % 4) != 0 || plen < sizeof(*spp) ||
1101 plen > len || len < sizeof(*pp)) {
1102 reason = ELS_RJT_PROT;
1103 explan = ELS_EXPL_INV_LEN;
1107 if (reason != ELS_RJT_NONE ||
1108 (fp = fc_frame_alloc(lport, len)) == NULL) {
1109 rjt_data.reason = reason;
1110 rjt_data.explan = explan;
1111 lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
1113 sp = lport->tt.seq_start_next(sp);
1115 pp = fc_frame_payload_get(fp, len);
1118 pp->prli.prli_cmd = ELS_LS_ACC;
1119 pp->prli.prli_spp_len = plen;
1120 pp->prli.prli_len = htons(len);
1121 len -= sizeof(struct fc_els_prli);
1124 * Go through all the service parameter pages and build
1125 * response. If plen indicates longer SPP than standard,
1126 * use that. The entire response has been pre-cleared above.
1129 while (len >= plen) {
1130 spp->spp_type = rspp->spp_type;
1131 spp->spp_type_ext = rspp->spp_type_ext;
1132 spp->spp_flags = rspp->spp_flags & FC_SPP_EST_IMG_PAIR;
1133 resp = FC_SPP_RESP_ACK;
1134 if (rspp->spp_flags & FC_SPP_RPA_VAL)
1135 resp = FC_SPP_RESP_NO_PA;
1136 switch (rspp->spp_type) {
1137 case 0: /* common to all FC-4 types */
1140 fcp_parm = ntohl(rspp->spp_params);
1141 if (fcp_parm * FCP_SPPF_RETRY)
1142 rdata->flags |= FC_RP_FLAGS_RETRY;
1143 rport->supported_classes = FC_COS_CLASS3;
1144 if (fcp_parm & FCP_SPPF_INIT_FCN)
1145 roles |= FC_RPORT_ROLE_FCP_INITIATOR;
1146 if (fcp_parm & FCP_SPPF_TARG_FCN)
1147 roles |= FC_RPORT_ROLE_FCP_TARGET;
1148 rport->roles = roles;
1151 htonl(lport->service_params);
1154 resp = FC_SPP_RESP_INVL;
1157 spp->spp_flags |= resp;
1159 rspp = (struct fc_els_spp *)((char *)rspp + plen);
1160 spp = (struct fc_els_spp *)((char *)spp + plen);
1164 * Send LS_ACC. If this fails, the originator should retry.
1166 f_ctl = FC_FC_EX_CTX | FC_FC_LAST_SEQ;
1167 f_ctl |= FC_FC_END_SEQ | FC_FC_SEQ_INIT;
1168 ep = fc_seq_exch(sp);
1169 fc_fill_fc_hdr(fp, FC_RCTL_ELS_REP, ep->did, ep->sid,
1170 FC_TYPE_ELS, f_ctl, 0);
1171 lport->tt.seq_send(lport, sp, fp);
1174 * Get lock and re-check state.
1176 switch (rdata->rp_state) {
1178 fc_rport_enter_ready(rport);
1180 case RPORT_ST_READY:
1186 fc_frame_free(rx_fp);
1190 * fc_rport_recv_prlo_req - Handle incoming Process Logout (PRLO) request
1191 * @rport: Fibre Channel remote port that initiated PRLO
1192 * @sp: current sequence in the PRLO exchange
1193 * @fp: PRLO request frame
1195 * Locking Note: The rport lock is exected to be held before calling
1198 static void fc_rport_recv_prlo_req(struct fc_rport *rport, struct fc_seq *sp,
1199 struct fc_frame *fp)
1201 struct fc_rport_libfc_priv *rdata = rport->dd_data;
1202 struct fc_lport *lport = rdata->local_port;
1204 struct fc_frame_header *fh;
1205 struct fc_seq_els_data rjt_data;
1207 fh = fc_frame_header_get(fp);
1209 FC_DEBUG_RPORT("Received PRLO request from port (%6x) "
1210 "while in state %s\n", ntoh24(fh->fh_s_id),
1211 fc_rport_state(rport));
1214 rjt_data.reason = ELS_RJT_UNAB;
1215 rjt_data.explan = ELS_EXPL_NONE;
1216 lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
1221 * fc_rport_recv_logo_req - Handle incoming Logout (LOGO) request
1222 * @rport: Fibre Channel remote port that initiated LOGO
1223 * @sp: current sequence in the LOGO exchange
1224 * @fp: LOGO request frame
1226 * Locking Note: The rport lock is exected to be held before calling
1229 static void fc_rport_recv_logo_req(struct fc_rport *rport, struct fc_seq *sp,
1230 struct fc_frame *fp)
1232 struct fc_frame_header *fh;
1233 struct fc_rport_libfc_priv *rdata = rport->dd_data;
1234 struct fc_lport *lport = rdata->local_port;
1236 fh = fc_frame_header_get(fp);
1238 FC_DEBUG_RPORT("Received LOGO request from port (%6x) "
1239 "while in state %s\n", ntoh24(fh->fh_s_id),
1240 fc_rport_state(rport));
1242 rdata->event = RPORT_EV_LOGO;
1243 queue_work(rport_event_queue, &rdata->event_work);
1245 lport->tt.seq_els_rsp_send(sp, ELS_LS_ACC, NULL);
1249 static void fc_rport_flush_queue(void)
1251 flush_workqueue(rport_event_queue);
1255 int fc_rport_init(struct fc_lport *lport)
1257 if (!lport->tt.rport_login)
1258 lport->tt.rport_login = fc_rport_login;
1260 if (!lport->tt.rport_logoff)
1261 lport->tt.rport_logoff = fc_rport_logoff;
1263 if (!lport->tt.rport_recv_req)
1264 lport->tt.rport_recv_req = fc_rport_recv_req;
1266 if (!lport->tt.rport_flush_queue)
1267 lport->tt.rport_flush_queue = fc_rport_flush_queue;
1271 EXPORT_SYMBOL(fc_rport_init);
1273 int fc_setup_rport()
1275 rport_event_queue = create_singlethread_workqueue("fc_rport_eq");
1276 if (!rport_event_queue)
1280 EXPORT_SYMBOL(fc_setup_rport);
1282 void fc_destroy_rport()
1284 destroy_workqueue(rport_event_queue);
1286 EXPORT_SYMBOL(fc_destroy_rport);
1288 void fc_rport_terminate_io(struct fc_rport *rport)
1290 struct fc_rport_libfc_priv *rdata = rport->dd_data;
1291 struct fc_lport *lport = rdata->local_port;
1293 lport->tt.exch_mgr_reset(lport, 0, rport->port_id);
1294 lport->tt.exch_mgr_reset(lport, rport->port_id, 0);
1296 EXPORT_SYMBOL(fc_rport_terminate_io);