2 * Copyright(c) 2007 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 * These comments only apply to the 'port code' which consists of the lport,
24 * disc and rport blocks.
28 * The lport, disc and rport blocks all have mutexes that are used to protect
29 * those objects. The main motivation for these locks is to prevent from
30 * having an lport reset just before we send a frame. In that scenario the
31 * lport's FID would get set to zero and then we'd send a frame with an
32 * invalid SID. We also need to ensure that states don't change unexpectedly
33 * while processing another state.
37 * The following heirarchy defines the locking rules. A greater lock
38 * may be held before acquiring a lesser lock, but a lesser lock should never
39 * be held while attempting to acquire a greater lock. Here is the heirarchy-
41 * lport > disc, lport > rport, disc > rport
45 * The callbacks cause complications with this scheme. There is a callback
46 * from the rport (to either lport or disc) and a callback from disc
49 * As rports exit the rport state machine a callback is made to the owner of
50 * the rport to notify success or failure. Since the callback is likely to
51 * cause the lport or disc to grab its lock we cannot hold the rport lock
52 * while making the callback. To ensure that the rport is not free'd while
53 * processing the callback the rport callbacks are serialized through a
54 * single-threaded workqueue. An rport would never be free'd while in a
55 * callback handler becuase no other rport work in this queue can be executed
58 * When discovery succeeds or fails a callback is made to the lport as
59 * notification. Currently, succesful discovery causes the lport to take no
60 * action. A failure will cause the lport to reset. There is likely a circular
61 * locking problem with this implementation.
67 * The critical sections protected by the lport's mutex are quite broad and
68 * may be improved upon in the future. The lport code and its locking doesn't
69 * influence the I/O path, so excessive locking doesn't penalize I/O
72 * The strategy is to lock whenever processing a request or response. Note
73 * that every _enter_* function corresponds to a state change. They generally
74 * change the lports state and then send a request out on the wire. We lock
75 * before calling any of these functions to protect that state change. This
76 * means that the entry points into the lport block manage the locks while
77 * the state machine can transition between states (i.e. _enter_* functions)
78 * while always staying protected.
80 * When handling responses we also hold the lport mutex broadly. When the
81 * lport receives the response frame it locks the mutex and then calls the
82 * appropriate handler for the particuar response. Generally a response will
83 * trigger a state change and so the lock must already be held.
85 * Retries also have to consider the locking. The retries occur from a work
86 * context and the work function will lock the lport and then retry the state
87 * (i.e. _enter_* function).
90 #include <linux/timer.h>
91 #include <asm/unaligned.h>
93 #include <scsi/fc/fc_gs.h>
95 #include <scsi/libfc.h>
96 #include <scsi/fc_encode.h>
100 /* Fabric IDs to use for point-to-point mode, chosen on whims. */
101 #define FC_LOCAL_PTP_FID_LO 0x010101
102 #define FC_LOCAL_PTP_FID_HI 0x010102
104 #define DNS_DELAY 3 /* Discovery delay after RSCN (in seconds)*/
106 static void fc_lport_error(struct fc_lport *, struct fc_frame *);
108 static void fc_lport_enter_reset(struct fc_lport *);
109 static void fc_lport_enter_flogi(struct fc_lport *);
110 static void fc_lport_enter_dns(struct fc_lport *);
111 static void fc_lport_enter_rpn_id(struct fc_lport *);
112 static void fc_lport_enter_rft_id(struct fc_lport *);
113 static void fc_lport_enter_scr(struct fc_lport *);
114 static void fc_lport_enter_ready(struct fc_lport *);
115 static void fc_lport_enter_logo(struct fc_lport *);
117 static const char *fc_lport_state_names[] = {
118 [LPORT_ST_DISABLED] = "disabled",
119 [LPORT_ST_FLOGI] = "FLOGI",
120 [LPORT_ST_DNS] = "dNS",
121 [LPORT_ST_RPN_ID] = "RPN_ID",
122 [LPORT_ST_RFT_ID] = "RFT_ID",
123 [LPORT_ST_SCR] = "SCR",
124 [LPORT_ST_READY] = "Ready",
125 [LPORT_ST_LOGO] = "LOGO",
126 [LPORT_ST_RESET] = "reset",
129 static int fc_frame_drop(struct fc_lport *lport, struct fc_frame *fp)
136 * fc_lport_rport_callback() - Event handler for rport events
137 * @lport: The lport which is receiving the event
138 * @rdata: private remote port data
139 * @event: The event that occured
141 * Locking Note: The rport lock should not be held when calling
144 static void fc_lport_rport_callback(struct fc_lport *lport,
145 struct fc_rport_priv *rdata,
146 enum fc_rport_event event)
148 FC_LPORT_DBG(lport, "Received a %d event for port (%6x)\n", event,
151 mutex_lock(&lport->lp_mutex);
154 if (lport->state == LPORT_ST_DNS) {
155 lport->dns_rp = rdata;
156 fc_lport_enter_rpn_id(lport);
158 FC_LPORT_DBG(lport, "Received an READY event "
159 "on port (%6x) for the directory "
160 "server, but the lport is not "
161 "in the DNS state, it's in the "
162 "%d state", rdata->ids.port_id,
164 lport->tt.rport_logoff(rdata);
168 case RPORT_EV_FAILED:
170 lport->dns_rp = NULL;
175 mutex_unlock(&lport->lp_mutex);
179 * fc_lport_state() - Return a string which represents the lport's state
180 * @lport: The lport whose state is to converted to a string
182 static const char *fc_lport_state(struct fc_lport *lport)
186 cp = fc_lport_state_names[lport->state];
193 * fc_lport_ptp_setup() - Create an rport for point-to-point mode
194 * @lport: The lport to attach the ptp rport to
195 * @fid: The FID of the ptp rport
196 * @remote_wwpn: The WWPN of the ptp rport
197 * @remote_wwnn: The WWNN of the ptp rport
199 static void fc_lport_ptp_setup(struct fc_lport *lport,
200 u32 remote_fid, u64 remote_wwpn,
203 mutex_lock(&lport->disc.disc_mutex);
205 lport->tt.rport_logoff(lport->ptp_rp);
206 lport->ptp_rp = lport->tt.rport_create(lport, remote_fid);
207 lport->ptp_rp->ids.port_name = remote_wwpn;
208 lport->ptp_rp->ids.node_name = remote_wwnn;
209 mutex_unlock(&lport->disc.disc_mutex);
211 lport->tt.rport_login(lport->ptp_rp);
213 fc_lport_enter_ready(lport);
216 void fc_get_host_port_type(struct Scsi_Host *shost)
218 /* TODO - currently just NPORT */
219 fc_host_port_type(shost) = FC_PORTTYPE_NPORT;
221 EXPORT_SYMBOL(fc_get_host_port_type);
223 void fc_get_host_port_state(struct Scsi_Host *shost)
225 struct fc_lport *lp = shost_priv(shost);
227 mutex_lock(&lp->lp_mutex);
229 fc_host_port_state(shost) = FC_PORTSTATE_LINKDOWN;
233 fc_host_port_state(shost) = FC_PORTSTATE_ONLINE;
236 fc_host_port_state(shost) = FC_PORTSTATE_OFFLINE;
238 mutex_unlock(&lp->lp_mutex);
240 EXPORT_SYMBOL(fc_get_host_port_state);
242 void fc_get_host_speed(struct Scsi_Host *shost)
244 struct fc_lport *lport = shost_priv(shost);
246 fc_host_speed(shost) = lport->link_speed;
248 EXPORT_SYMBOL(fc_get_host_speed);
250 struct fc_host_statistics *fc_get_host_stats(struct Scsi_Host *shost)
252 struct fc_host_statistics *fcoe_stats;
253 struct fc_lport *lp = shost_priv(shost);
254 struct timespec v0, v1;
257 fcoe_stats = &lp->host_stats;
258 memset(fcoe_stats, 0, sizeof(struct fc_host_statistics));
260 jiffies_to_timespec(jiffies, &v0);
261 jiffies_to_timespec(lp->boot_time, &v1);
262 fcoe_stats->seconds_since_last_reset = (v0.tv_sec - v1.tv_sec);
264 for_each_possible_cpu(cpu) {
265 struct fcoe_dev_stats *stats;
267 stats = per_cpu_ptr(lp->dev_stats, cpu);
269 fcoe_stats->tx_frames += stats->TxFrames;
270 fcoe_stats->tx_words += stats->TxWords;
271 fcoe_stats->rx_frames += stats->RxFrames;
272 fcoe_stats->rx_words += stats->RxWords;
273 fcoe_stats->error_frames += stats->ErrorFrames;
274 fcoe_stats->invalid_crc_count += stats->InvalidCRCCount;
275 fcoe_stats->fcp_input_requests += stats->InputRequests;
276 fcoe_stats->fcp_output_requests += stats->OutputRequests;
277 fcoe_stats->fcp_control_requests += stats->ControlRequests;
278 fcoe_stats->fcp_input_megabytes += stats->InputMegabytes;
279 fcoe_stats->fcp_output_megabytes += stats->OutputMegabytes;
280 fcoe_stats->link_failure_count += stats->LinkFailureCount;
282 fcoe_stats->lip_count = -1;
283 fcoe_stats->nos_count = -1;
284 fcoe_stats->loss_of_sync_count = -1;
285 fcoe_stats->loss_of_signal_count = -1;
286 fcoe_stats->prim_seq_protocol_err_count = -1;
287 fcoe_stats->dumped_frames = -1;
290 EXPORT_SYMBOL(fc_get_host_stats);
293 * Fill in FLOGI command for request.
296 fc_lport_flogi_fill(struct fc_lport *lport, struct fc_els_flogi *flogi,
299 struct fc_els_csp *sp;
300 struct fc_els_cssp *cp;
302 memset(flogi, 0, sizeof(*flogi));
303 flogi->fl_cmd = (u8) op;
304 put_unaligned_be64(lport->wwpn, &flogi->fl_wwpn);
305 put_unaligned_be64(lport->wwnn, &flogi->fl_wwnn);
307 sp->sp_hi_ver = 0x20;
308 sp->sp_lo_ver = 0x20;
309 sp->sp_bb_cred = htons(10); /* this gets set by gateway */
310 sp->sp_bb_data = htons((u16) lport->mfs);
311 cp = &flogi->fl_cssp[3 - 1]; /* class 3 parameters */
312 cp->cp_class = htons(FC_CPC_VALID | FC_CPC_SEQ);
313 if (op != ELS_FLOGI) {
314 sp->sp_features = htons(FC_SP_FT_CIRO);
315 sp->sp_tot_seq = htons(255); /* seq. we accept */
316 sp->sp_rel_off = htons(0x1f);
317 sp->sp_e_d_tov = htonl(lport->e_d_tov);
319 cp->cp_rdfs = htons((u16) lport->mfs);
320 cp->cp_con_seq = htons(255);
326 * Add a supported FC-4 type.
328 static void fc_lport_add_fc4_type(struct fc_lport *lport, enum fc_fh_type type)
332 mp = &lport->fcts.ff_type_map[type / FC_NS_BPW];
333 *mp = htonl(ntohl(*mp) | 1UL << (type % FC_NS_BPW));
337 * fc_lport_recv_rlir_req() - Handle received Registered Link Incident Report.
338 * @lport: Fibre Channel local port recieving the RLIR
339 * @sp: current sequence in the RLIR exchange
340 * @fp: RLIR request frame
342 * Locking Note: The lport lock is expected to be held before calling
345 static void fc_lport_recv_rlir_req(struct fc_seq *sp, struct fc_frame *fp,
346 struct fc_lport *lport)
348 FC_LPORT_DBG(lport, "Received RLIR request while in state %s\n",
349 fc_lport_state(lport));
351 lport->tt.seq_els_rsp_send(sp, ELS_LS_ACC, NULL);
356 * fc_lport_recv_echo_req() - Handle received ECHO request
357 * @lport: Fibre Channel local port recieving the ECHO
358 * @sp: current sequence in the ECHO exchange
359 * @fp: ECHO request frame
361 * Locking Note: The lport lock is expected to be held before calling
364 static void fc_lport_recv_echo_req(struct fc_seq *sp, struct fc_frame *in_fp,
365 struct fc_lport *lport)
368 struct fc_exch *ep = fc_seq_exch(sp);
374 FC_LPORT_DBG(lport, "Received ECHO request while in state %s\n",
375 fc_lport_state(lport));
377 len = fr_len(in_fp) - sizeof(struct fc_frame_header);
378 pp = fc_frame_payload_get(in_fp, len);
380 if (len < sizeof(__be32))
381 len = sizeof(__be32);
383 fp = fc_frame_alloc(lport, len);
385 dp = fc_frame_payload_get(fp, len);
387 *((__be32 *)dp) = htonl(ELS_LS_ACC << 24);
388 sp = lport->tt.seq_start_next(sp);
389 f_ctl = FC_FC_EX_CTX | FC_FC_LAST_SEQ | FC_FC_END_SEQ;
390 fc_fill_fc_hdr(fp, FC_RCTL_ELS_REP, ep->did, ep->sid,
391 FC_TYPE_ELS, f_ctl, 0);
392 lport->tt.seq_send(lport, sp, fp);
394 fc_frame_free(in_fp);
398 * fc_lport_recv_rnid_req() - Handle received Request Node ID data request
399 * @sp: The sequence in the RNID exchange
400 * @fp: The RNID request frame
401 * @lport: The local port recieving the RNID
403 * Locking Note: The lport lock is expected to be held before calling
406 static void fc_lport_recv_rnid_req(struct fc_seq *sp, struct fc_frame *in_fp,
407 struct fc_lport *lport)
410 struct fc_exch *ep = fc_seq_exch(sp);
411 struct fc_els_rnid *req;
413 struct fc_els_rnid_resp rnid;
414 struct fc_els_rnid_cid cid;
415 struct fc_els_rnid_gen gen;
417 struct fc_seq_els_data rjt_data;
422 FC_LPORT_DBG(lport, "Received RNID request while in state %s\n",
423 fc_lport_state(lport));
425 req = fc_frame_payload_get(in_fp, sizeof(*req));
428 rjt_data.reason = ELS_RJT_LOGIC;
429 rjt_data.explan = ELS_EXPL_NONE;
430 lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
434 if (fmt != ELS_RNIDF_GEN ||
435 ntohl(lport->rnid_gen.rnid_atype) == 0) {
436 fmt = ELS_RNIDF_NONE; /* nothing to provide */
437 len -= sizeof(rp->gen);
439 fp = fc_frame_alloc(lport, len);
441 rp = fc_frame_payload_get(fp, len);
443 rp->rnid.rnid_cmd = ELS_LS_ACC;
444 rp->rnid.rnid_fmt = fmt;
445 rp->rnid.rnid_cid_len = sizeof(rp->cid);
446 rp->cid.rnid_wwpn = htonll(lport->wwpn);
447 rp->cid.rnid_wwnn = htonll(lport->wwnn);
448 if (fmt == ELS_RNIDF_GEN) {
449 rp->rnid.rnid_sid_len = sizeof(rp->gen);
450 memcpy(&rp->gen, &lport->rnid_gen,
453 sp = lport->tt.seq_start_next(sp);
454 f_ctl = FC_FC_EX_CTX | FC_FC_LAST_SEQ;
455 f_ctl |= FC_FC_END_SEQ | FC_FC_SEQ_INIT;
456 fc_fill_fc_hdr(fp, FC_RCTL_ELS_REP, ep->did, ep->sid,
457 FC_TYPE_ELS, f_ctl, 0);
458 lport->tt.seq_send(lport, sp, fp);
461 fc_frame_free(in_fp);
465 * fc_lport_recv_logo_req() - Handle received fabric LOGO request
466 * @lport: Fibre Channel local port recieving the LOGO
467 * @sp: current sequence in the LOGO exchange
468 * @fp: LOGO request frame
470 * Locking Note: The lport lock is exected to be held before calling
473 static void fc_lport_recv_logo_req(struct fc_seq *sp, struct fc_frame *fp,
474 struct fc_lport *lport)
476 lport->tt.seq_els_rsp_send(sp, ELS_LS_ACC, NULL);
477 fc_lport_enter_reset(lport);
482 * fc_fabric_login() - Start the lport state machine
483 * @lport: The lport that should log into the fabric
485 * Locking Note: This function should not be called
486 * with the lport lock held.
488 int fc_fabric_login(struct fc_lport *lport)
492 mutex_lock(&lport->lp_mutex);
493 if (lport->state == LPORT_ST_DISABLED) {
494 fc_lport_enter_reset(lport);
497 mutex_unlock(&lport->lp_mutex);
501 EXPORT_SYMBOL(fc_fabric_login);
504 * __fc_linkup() - Handler for transport linkup events
505 * @lport: The lport whose link is up
507 * Locking: must be called with the lp_mutex held
509 void __fc_linkup(struct fc_lport *lport)
511 if (!lport->link_up) {
514 if (lport->state == LPORT_ST_RESET)
515 fc_lport_enter_flogi(lport);
520 * fc_linkup() - Handler for transport linkup events
521 * @lport: The lport whose link is up
523 void fc_linkup(struct fc_lport *lport)
525 printk(KERN_INFO "libfc: Link up on port (%6x)\n",
526 fc_host_port_id(lport->host));
528 mutex_lock(&lport->lp_mutex);
530 mutex_unlock(&lport->lp_mutex);
532 EXPORT_SYMBOL(fc_linkup);
535 * __fc_linkdown() - Handler for transport linkdown events
536 * @lport: The lport whose link is down
538 * Locking: must be called with the lp_mutex held
540 void __fc_linkdown(struct fc_lport *lport)
542 if (lport->link_up) {
544 fc_lport_enter_reset(lport);
545 lport->tt.fcp_cleanup(lport);
550 * fc_linkdown() - Handler for transport linkdown events
551 * @lport: The lport whose link is down
553 void fc_linkdown(struct fc_lport *lport)
555 printk(KERN_INFO "libfc: Link down on port (%6x)\n",
556 fc_host_port_id(lport->host));
558 mutex_lock(&lport->lp_mutex);
559 __fc_linkdown(lport);
560 mutex_unlock(&lport->lp_mutex);
562 EXPORT_SYMBOL(fc_linkdown);
565 * fc_fabric_logoff() - Logout of the fabric
566 * @lport: fc_lport pointer to logoff the fabric
569 * 0 for success, -1 for failure
571 int fc_fabric_logoff(struct fc_lport *lport)
573 lport->tt.disc_stop_final(lport);
574 mutex_lock(&lport->lp_mutex);
576 lport->tt.rport_logoff(lport->dns_rp);
577 mutex_unlock(&lport->lp_mutex);
578 lport->tt.rport_flush_queue();
579 mutex_lock(&lport->lp_mutex);
580 fc_lport_enter_logo(lport);
581 mutex_unlock(&lport->lp_mutex);
582 cancel_delayed_work_sync(&lport->retry_work);
585 EXPORT_SYMBOL(fc_fabric_logoff);
588 * fc_lport_destroy() - unregister a fc_lport
589 * @lport: fc_lport pointer to unregister
594 * exit routine for fc_lport instance
595 * clean-up all the allocated memory
596 * and free up other system resources.
599 int fc_lport_destroy(struct fc_lport *lport)
601 mutex_lock(&lport->lp_mutex);
602 lport->state = LPORT_ST_DISABLED;
604 lport->tt.frame_send = fc_frame_drop;
605 mutex_unlock(&lport->lp_mutex);
607 lport->tt.fcp_abort_io(lport);
608 lport->tt.disc_stop_final(lport);
609 lport->tt.exch_mgr_reset(lport, 0, 0);
612 EXPORT_SYMBOL(fc_lport_destroy);
615 * fc_set_mfs() - sets up the mfs for the corresponding fc_lport
616 * @lport: fc_lport pointer to unregister
617 * @mfs: the new mfs for fc_lport
619 * Set mfs for the given fc_lport to the new mfs.
621 * Return: 0 for success
623 int fc_set_mfs(struct fc_lport *lport, u32 mfs)
625 unsigned int old_mfs;
628 mutex_lock(&lport->lp_mutex);
630 old_mfs = lport->mfs;
632 if (mfs >= FC_MIN_MAX_FRAME) {
634 if (mfs > FC_MAX_FRAME)
636 mfs -= sizeof(struct fc_frame_header);
641 if (!rc && mfs < old_mfs)
642 fc_lport_enter_reset(lport);
644 mutex_unlock(&lport->lp_mutex);
648 EXPORT_SYMBOL(fc_set_mfs);
651 * fc_lport_disc_callback() - Callback for discovery events
652 * @lport: FC local port
653 * @event: The discovery event
655 void fc_lport_disc_callback(struct fc_lport *lport, enum fc_disc_event event)
658 case DISC_EV_SUCCESS:
659 FC_LPORT_DBG(lport, "Discovery succeeded\n");
662 printk(KERN_ERR "libfc: Discovery failed for port (%6x)\n",
663 fc_host_port_id(lport->host));
664 mutex_lock(&lport->lp_mutex);
665 fc_lport_enter_reset(lport);
666 mutex_unlock(&lport->lp_mutex);
675 * fc_rport_enter_ready() - Enter the ready state and start discovery
676 * @lport: Fibre Channel local port that is ready
678 * Locking Note: The lport lock is expected to be held before calling
681 static void fc_lport_enter_ready(struct fc_lport *lport)
683 FC_LPORT_DBG(lport, "Entered READY from state %s\n",
684 fc_lport_state(lport));
686 fc_lport_state_enter(lport, LPORT_ST_READY);
688 fc_vport_set_state(lport->vport, FC_VPORT_ACTIVE);
689 fc_vports_linkchange(lport);
692 lport->tt.disc_start(fc_lport_disc_callback, lport);
696 * fc_lport_recv_flogi_req() - Receive a FLOGI request
697 * @sp_in: The sequence the FLOGI is on
698 * @rx_fp: The frame the FLOGI is in
699 * @lport: The lport that recieved the request
701 * A received FLOGI request indicates a point-to-point connection.
702 * Accept it with the common service parameters indicating our N port.
703 * Set up to do a PLOGI if we have the higher-number WWPN.
705 * Locking Note: The lport lock is expected to be held before calling
708 static void fc_lport_recv_flogi_req(struct fc_seq *sp_in,
709 struct fc_frame *rx_fp,
710 struct fc_lport *lport)
713 struct fc_frame_header *fh;
716 struct fc_els_flogi *flp;
717 struct fc_els_flogi *new_flp;
723 FC_LPORT_DBG(lport, "Received FLOGI request while in state %s\n",
724 fc_lport_state(lport));
726 fh = fc_frame_header_get(rx_fp);
727 remote_fid = ntoh24(fh->fh_s_id);
728 flp = fc_frame_payload_get(rx_fp, sizeof(*flp));
731 remote_wwpn = get_unaligned_be64(&flp->fl_wwpn);
732 if (remote_wwpn == lport->wwpn) {
733 printk(KERN_WARNING "libfc: Received FLOGI from port "
734 "with same WWPN %llx\n", remote_wwpn);
737 FC_LPORT_DBG(lport, "FLOGI from port WWPN %llx\n", remote_wwpn);
740 * XXX what is the right thing to do for FIDs?
741 * The originator might expect our S_ID to be 0xfffffe.
742 * But if so, both of us could end up with the same FID.
744 local_fid = FC_LOCAL_PTP_FID_LO;
745 if (remote_wwpn < lport->wwpn) {
746 local_fid = FC_LOCAL_PTP_FID_HI;
747 if (!remote_fid || remote_fid == local_fid)
748 remote_fid = FC_LOCAL_PTP_FID_LO;
749 } else if (!remote_fid) {
750 remote_fid = FC_LOCAL_PTP_FID_HI;
753 fc_host_port_id(lport->host) = local_fid;
755 fp = fc_frame_alloc(lport, sizeof(*flp));
757 sp = lport->tt.seq_start_next(fr_seq(rx_fp));
758 new_flp = fc_frame_payload_get(fp, sizeof(*flp));
759 fc_lport_flogi_fill(lport, new_flp, ELS_FLOGI);
760 new_flp->fl_cmd = (u8) ELS_LS_ACC;
763 * Send the response. If this fails, the originator should
764 * repeat the sequence.
766 f_ctl = FC_FC_EX_CTX | FC_FC_LAST_SEQ | FC_FC_END_SEQ;
767 ep = fc_seq_exch(sp);
768 fc_fill_fc_hdr(fp, FC_RCTL_ELS_REP, ep->did, ep->sid,
769 FC_TYPE_ELS, f_ctl, 0);
770 lport->tt.seq_send(lport, sp, fp);
773 fc_lport_error(lport, fp);
775 fc_lport_ptp_setup(lport, remote_fid, remote_wwpn,
776 get_unaligned_be64(&flp->fl_wwnn));
780 fc_frame_free(rx_fp);
784 * fc_lport_recv_req() - The generic lport request handler
785 * @lport: The lport that received the request
786 * @sp: The sequence the request is on
787 * @fp: The frame the request is in
789 * This function will see if the lport handles the request or
790 * if an rport should handle the request.
792 * Locking Note: This function should not be called with the lport
793 * lock held becuase it will grab the lock.
795 static void fc_lport_recv_req(struct fc_lport *lport, struct fc_seq *sp,
798 struct fc_frame_header *fh = fc_frame_header_get(fp);
799 void (*recv) (struct fc_seq *, struct fc_frame *, struct fc_lport *);
801 mutex_lock(&lport->lp_mutex);
804 * Handle special ELS cases like FLOGI, LOGO, and
805 * RSCN here. These don't require a session.
806 * Even if we had a session, it might not be ready.
810 else if (fh->fh_type == FC_TYPE_ELS &&
811 fh->fh_r_ctl == FC_RCTL_ELS_REQ) {
815 recv = lport->tt.rport_recv_req;
816 switch (fc_frame_payload_op(fp)) {
818 recv = fc_lport_recv_flogi_req;
821 fh = fc_frame_header_get(fp);
822 if (ntoh24(fh->fh_s_id) == FC_FID_FLOGI)
823 recv = fc_lport_recv_logo_req;
826 recv = lport->tt.disc_recv_req;
829 recv = fc_lport_recv_echo_req;
832 recv = fc_lport_recv_rlir_req;
835 recv = fc_lport_recv_rnid_req;
841 FC_LPORT_DBG(lport, "dropping invalid frame (eof %x)\n",
845 mutex_unlock(&lport->lp_mutex);
848 * The common exch_done for all request may not be good
849 * if any request requires longer hold on exhange. XXX
851 lport->tt.exch_done(sp);
855 * fc_lport_reset() - Reset an lport
856 * @lport: The lport which should be reset
858 * Locking Note: This functions should not be called with the
861 int fc_lport_reset(struct fc_lport *lport)
863 cancel_delayed_work_sync(&lport->retry_work);
864 mutex_lock(&lport->lp_mutex);
865 fc_lport_enter_reset(lport);
866 mutex_unlock(&lport->lp_mutex);
869 EXPORT_SYMBOL(fc_lport_reset);
872 * fc_lport_reset_locked() - Reset the local port
873 * @lport: Fibre Channel local port to be reset
875 * Locking Note: The lport lock is expected to be held before calling
878 static void fc_lport_reset_locked(struct fc_lport *lport)
881 lport->tt.rport_logoff(lport->dns_rp);
883 lport->ptp_rp = NULL;
885 lport->tt.disc_stop(lport);
887 lport->tt.exch_mgr_reset(lport, 0, 0);
888 fc_host_fabric_name(lport->host) = 0;
889 fc_host_port_id(lport->host) = 0;
893 * fc_lport_enter_reset() - Reset the local port
894 * @lport: Fibre Channel local port to be reset
896 * Locking Note: The lport lock is expected to be held before calling
899 static void fc_lport_enter_reset(struct fc_lport *lport)
901 FC_LPORT_DBG(lport, "Entered RESET state from %s state\n",
902 fc_lport_state(lport));
906 fc_vport_set_state(lport->vport, FC_VPORT_INITIALIZING);
908 fc_vport_set_state(lport->vport, FC_VPORT_LINKDOWN);
910 fc_lport_state_enter(lport, LPORT_ST_RESET);
911 fc_vports_linkchange(lport);
912 fc_lport_reset_locked(lport);
914 fc_lport_enter_flogi(lport);
918 * fc_lport_enter_disabled() - disable the local port
919 * @lport: Fibre Channel local port to be reset
921 * Locking Note: The lport lock is expected to be held before calling
924 static void fc_lport_enter_disabled(struct fc_lport *lport)
926 FC_LPORT_DBG(lport, "Entered disabled state from %s state\n",
927 fc_lport_state(lport));
929 fc_lport_state_enter(lport, LPORT_ST_DISABLED);
930 fc_vports_linkchange(lport);
931 fc_lport_reset_locked(lport);
935 * fc_lport_error() - Handler for any errors
936 * @lport: The fc_lport object
937 * @fp: The frame pointer
939 * If the error was caused by a resource allocation failure
940 * then wait for half a second and retry, otherwise retry
941 * after the e_d_tov time.
943 static void fc_lport_error(struct fc_lport *lport, struct fc_frame *fp)
945 unsigned long delay = 0;
946 FC_LPORT_DBG(lport, "Error %ld in state %s, retries %d\n",
947 PTR_ERR(fp), fc_lport_state(lport),
950 if (!fp || PTR_ERR(fp) == -FC_EX_TIMEOUT) {
952 * Memory allocation failure, or the exchange timed out.
955 if (lport->retry_count < lport->max_retry_count) {
956 lport->retry_count++;
958 delay = msecs_to_jiffies(500);
960 delay = msecs_to_jiffies(lport->e_d_tov);
962 schedule_delayed_work(&lport->retry_work, delay);
964 switch (lport->state) {
965 case LPORT_ST_DISABLED:
968 case LPORT_ST_RPN_ID:
969 case LPORT_ST_RFT_ID:
974 fc_lport_enter_reset(lport);
982 * fc_lport_rft_id_resp() - Handle response to Register Fibre
983 * Channel Types by ID (RPN_ID) request
984 * @sp: current sequence in RPN_ID exchange
985 * @fp: response frame
986 * @lp_arg: Fibre Channel host port instance
988 * Locking Note: This function will be called without the lport lock
989 * held, but it will lock, call an _enter_* function or fc_lport_error
990 * and then unlock the lport.
992 static void fc_lport_rft_id_resp(struct fc_seq *sp, struct fc_frame *fp,
995 struct fc_lport *lport = lp_arg;
996 struct fc_frame_header *fh;
997 struct fc_ct_hdr *ct;
999 FC_LPORT_DBG(lport, "Received a RFT_ID %s\n", fc_els_resp_type(fp));
1001 if (fp == ERR_PTR(-FC_EX_CLOSED))
1004 mutex_lock(&lport->lp_mutex);
1006 if (lport->state != LPORT_ST_RFT_ID) {
1007 FC_LPORT_DBG(lport, "Received a RFT_ID response, but in state "
1008 "%s\n", fc_lport_state(lport));
1015 fc_lport_error(lport, fp);
1019 fh = fc_frame_header_get(fp);
1020 ct = fc_frame_payload_get(fp, sizeof(*ct));
1022 if (fh && ct && fh->fh_type == FC_TYPE_CT &&
1023 ct->ct_fs_type == FC_FST_DIR &&
1024 ct->ct_fs_subtype == FC_NS_SUBTYPE &&
1025 ntohs(ct->ct_cmd) == FC_FS_ACC)
1026 fc_lport_enter_scr(lport);
1028 fc_lport_error(lport, fp);
1032 mutex_unlock(&lport->lp_mutex);
1036 * fc_lport_rpn_id_resp() - Handle response to Register Port
1037 * Name by ID (RPN_ID) request
1038 * @sp: current sequence in RPN_ID exchange
1039 * @fp: response frame
1040 * @lp_arg: Fibre Channel host port instance
1042 * Locking Note: This function will be called without the lport lock
1043 * held, but it will lock, call an _enter_* function or fc_lport_error
1044 * and then unlock the lport.
1046 static void fc_lport_rpn_id_resp(struct fc_seq *sp, struct fc_frame *fp,
1049 struct fc_lport *lport = lp_arg;
1050 struct fc_frame_header *fh;
1051 struct fc_ct_hdr *ct;
1053 FC_LPORT_DBG(lport, "Received a RPN_ID %s\n", fc_els_resp_type(fp));
1055 if (fp == ERR_PTR(-FC_EX_CLOSED))
1058 mutex_lock(&lport->lp_mutex);
1060 if (lport->state != LPORT_ST_RPN_ID) {
1061 FC_LPORT_DBG(lport, "Received a RPN_ID response, but in state "
1062 "%s\n", fc_lport_state(lport));
1069 fc_lport_error(lport, fp);
1073 fh = fc_frame_header_get(fp);
1074 ct = fc_frame_payload_get(fp, sizeof(*ct));
1075 if (fh && ct && fh->fh_type == FC_TYPE_CT &&
1076 ct->ct_fs_type == FC_FST_DIR &&
1077 ct->ct_fs_subtype == FC_NS_SUBTYPE &&
1078 ntohs(ct->ct_cmd) == FC_FS_ACC)
1079 fc_lport_enter_rft_id(lport);
1081 fc_lport_error(lport, fp);
1086 mutex_unlock(&lport->lp_mutex);
1090 * fc_lport_scr_resp() - Handle response to State Change Register (SCR) request
1091 * @sp: current sequence in SCR exchange
1092 * @fp: response frame
1093 * @lp_arg: Fibre Channel lport port instance that sent the registration request
1095 * Locking Note: This function will be called without the lport lock
1096 * held, but it will lock, call an _enter_* function or fc_lport_error
1097 * and then unlock the lport.
1099 static void fc_lport_scr_resp(struct fc_seq *sp, struct fc_frame *fp,
1102 struct fc_lport *lport = lp_arg;
1105 FC_LPORT_DBG(lport, "Received a SCR %s\n", fc_els_resp_type(fp));
1107 if (fp == ERR_PTR(-FC_EX_CLOSED))
1110 mutex_lock(&lport->lp_mutex);
1112 if (lport->state != LPORT_ST_SCR) {
1113 FC_LPORT_DBG(lport, "Received a SCR response, but in state "
1114 "%s\n", fc_lport_state(lport));
1121 fc_lport_error(lport, fp);
1125 op = fc_frame_payload_op(fp);
1126 if (op == ELS_LS_ACC)
1127 fc_lport_enter_ready(lport);
1129 fc_lport_error(lport, fp);
1134 mutex_unlock(&lport->lp_mutex);
1138 * fc_lport_enter_scr() - Send a State Change Register (SCR) request
1139 * @lport: Fibre Channel local port to register for state changes
1141 * Locking Note: The lport lock is expected to be held before calling
1144 static void fc_lport_enter_scr(struct fc_lport *lport)
1146 struct fc_frame *fp;
1148 FC_LPORT_DBG(lport, "Entered SCR state from %s state\n",
1149 fc_lport_state(lport));
1151 fc_lport_state_enter(lport, LPORT_ST_SCR);
1153 fp = fc_frame_alloc(lport, sizeof(struct fc_els_scr));
1155 fc_lport_error(lport, fp);
1159 if (!lport->tt.elsct_send(lport, FC_FID_FCTRL, fp, ELS_SCR,
1160 fc_lport_scr_resp, lport, lport->e_d_tov))
1161 fc_lport_error(lport, NULL);
1165 * fc_lport_enter_rft_id() - Register FC4-types with the name server
1166 * @lport: Fibre Channel local port to register
1168 * Locking Note: The lport lock is expected to be held before calling
1171 static void fc_lport_enter_rft_id(struct fc_lport *lport)
1173 struct fc_frame *fp;
1174 struct fc_ns_fts *lps;
1177 FC_LPORT_DBG(lport, "Entered RFT_ID state from %s state\n",
1178 fc_lport_state(lport));
1180 fc_lport_state_enter(lport, LPORT_ST_RFT_ID);
1183 i = sizeof(lps->ff_type_map) / sizeof(lps->ff_type_map[0]);
1185 if (ntohl(lps->ff_type_map[i]) != 0)
1188 /* nothing to register, move on to SCR */
1189 fc_lport_enter_scr(lport);
1193 fp = fc_frame_alloc(lport, sizeof(struct fc_ct_hdr) +
1194 sizeof(struct fc_ns_rft));
1196 fc_lport_error(lport, fp);
1200 if (!lport->tt.elsct_send(lport, FC_FID_DIR_SERV, fp, FC_NS_RFT_ID,
1201 fc_lport_rft_id_resp,
1202 lport, lport->e_d_tov))
1203 fc_lport_error(lport, fp);
1207 * fc_rport_enter_rft_id() - Register port name with the name server
1208 * @lport: Fibre Channel local port to register
1210 * Locking Note: The lport lock is expected to be held before calling
1213 static void fc_lport_enter_rpn_id(struct fc_lport *lport)
1215 struct fc_frame *fp;
1217 FC_LPORT_DBG(lport, "Entered RPN_ID state from %s state\n",
1218 fc_lport_state(lport));
1220 fc_lport_state_enter(lport, LPORT_ST_RPN_ID);
1222 fp = fc_frame_alloc(lport, sizeof(struct fc_ct_hdr) +
1223 sizeof(struct fc_ns_rn_id));
1225 fc_lport_error(lport, fp);
1229 if (!lport->tt.elsct_send(lport, FC_FID_DIR_SERV, fp, FC_NS_RPN_ID,
1230 fc_lport_rpn_id_resp,
1231 lport, lport->e_d_tov))
1232 fc_lport_error(lport, NULL);
1235 static struct fc_rport_operations fc_lport_rport_ops = {
1236 .event_callback = fc_lport_rport_callback,
1240 * fc_rport_enter_dns() - Create a rport to the name server
1241 * @lport: Fibre Channel local port requesting a rport for the name server
1243 * Locking Note: The lport lock is expected to be held before calling
1246 static void fc_lport_enter_dns(struct fc_lport *lport)
1248 struct fc_rport_priv *rdata;
1250 FC_LPORT_DBG(lport, "Entered DNS state from %s state\n",
1251 fc_lport_state(lport));
1253 fc_lport_state_enter(lport, LPORT_ST_DNS);
1255 mutex_lock(&lport->disc.disc_mutex);
1256 rdata = lport->tt.rport_create(lport, FC_FID_DIR_SERV);
1257 mutex_unlock(&lport->disc.disc_mutex);
1261 rdata->ops = &fc_lport_rport_ops;
1262 lport->tt.rport_login(rdata);
1266 fc_lport_error(lport, NULL);
1270 * fc_lport_timeout() - Handler for the retry_work timer.
1271 * @work: The work struct of the fc_lport
1273 static void fc_lport_timeout(struct work_struct *work)
1275 struct fc_lport *lport =
1276 container_of(work, struct fc_lport,
1279 mutex_lock(&lport->lp_mutex);
1281 switch (lport->state) {
1282 case LPORT_ST_DISABLED:
1285 case LPORT_ST_READY:
1288 case LPORT_ST_RESET:
1290 case LPORT_ST_FLOGI:
1291 fc_lport_enter_flogi(lport);
1294 fc_lport_enter_dns(lport);
1296 case LPORT_ST_RPN_ID:
1297 fc_lport_enter_rpn_id(lport);
1299 case LPORT_ST_RFT_ID:
1300 fc_lport_enter_rft_id(lport);
1303 fc_lport_enter_scr(lport);
1306 fc_lport_enter_logo(lport);
1310 mutex_unlock(&lport->lp_mutex);
1314 * fc_lport_logo_resp() - Handle response to LOGO request
1315 * @sp: current sequence in LOGO exchange
1316 * @fp: response frame
1317 * @lp_arg: Fibre Channel lport port instance that sent the LOGO request
1319 * Locking Note: This function will be called without the lport lock
1320 * held, but it will lock, call an _enter_* function or fc_lport_error
1321 * and then unlock the lport.
1323 static void fc_lport_logo_resp(struct fc_seq *sp, struct fc_frame *fp,
1326 struct fc_lport *lport = lp_arg;
1329 FC_LPORT_DBG(lport, "Received a LOGO %s\n", fc_els_resp_type(fp));
1331 if (fp == ERR_PTR(-FC_EX_CLOSED))
1334 mutex_lock(&lport->lp_mutex);
1336 if (lport->state != LPORT_ST_LOGO) {
1337 FC_LPORT_DBG(lport, "Received a LOGO response, but in state "
1338 "%s\n", fc_lport_state(lport));
1345 fc_lport_error(lport, fp);
1349 op = fc_frame_payload_op(fp);
1350 if (op == ELS_LS_ACC)
1351 fc_lport_enter_disabled(lport);
1353 fc_lport_error(lport, fp);
1358 mutex_unlock(&lport->lp_mutex);
1362 * fc_rport_enter_logo() - Logout of the fabric
1363 * @lport: Fibre Channel local port to be logged out
1365 * Locking Note: The lport lock is expected to be held before calling
1368 static void fc_lport_enter_logo(struct fc_lport *lport)
1370 struct fc_frame *fp;
1371 struct fc_els_logo *logo;
1373 FC_LPORT_DBG(lport, "Entered LOGO state from %s state\n",
1374 fc_lport_state(lport));
1376 fc_lport_state_enter(lport, LPORT_ST_LOGO);
1377 fc_vports_linkchange(lport);
1379 fp = fc_frame_alloc(lport, sizeof(*logo));
1381 fc_lport_error(lport, fp);
1385 if (!lport->tt.elsct_send(lport, FC_FID_FLOGI, fp, ELS_LOGO,
1386 fc_lport_logo_resp, lport, lport->e_d_tov))
1387 fc_lport_error(lport, NULL);
1391 * fc_lport_flogi_resp() - Handle response to FLOGI request
1392 * @sp: current sequence in FLOGI exchange
1393 * @fp: response frame
1394 * @lp_arg: Fibre Channel lport port instance that sent the FLOGI request
1396 * Locking Note: This function will be called without the lport lock
1397 * held, but it will lock, call an _enter_* function or fc_lport_error
1398 * and then unlock the lport.
1400 static void fc_lport_flogi_resp(struct fc_seq *sp, struct fc_frame *fp,
1403 struct fc_lport *lport = lp_arg;
1404 struct fc_frame_header *fh;
1405 struct fc_els_flogi *flp;
1408 unsigned int r_a_tov;
1409 unsigned int e_d_tov;
1412 FC_LPORT_DBG(lport, "Received a FLOGI %s\n", fc_els_resp_type(fp));
1414 if (fp == ERR_PTR(-FC_EX_CLOSED))
1417 mutex_lock(&lport->lp_mutex);
1419 if (lport->state != LPORT_ST_FLOGI) {
1420 FC_LPORT_DBG(lport, "Received a FLOGI response, but in state "
1421 "%s\n", fc_lport_state(lport));
1428 fc_lport_error(lport, fp);
1432 fh = fc_frame_header_get(fp);
1433 did = ntoh24(fh->fh_d_id);
1434 if (fc_frame_payload_op(fp) == ELS_LS_ACC && did != 0) {
1436 printk(KERN_INFO "libfc: Assigned FID (%6x) in FLOGI response\n",
1438 fc_host_port_id(lport->host) = did;
1440 flp = fc_frame_payload_get(fp, sizeof(*flp));
1442 mfs = ntohs(flp->fl_csp.sp_bb_data) &
1444 if (mfs >= FC_SP_MIN_MAX_PAYLOAD &&
1447 csp_flags = ntohs(flp->fl_csp.sp_features);
1448 r_a_tov = ntohl(flp->fl_csp.sp_r_a_tov);
1449 e_d_tov = ntohl(flp->fl_csp.sp_e_d_tov);
1450 if (csp_flags & FC_SP_FT_EDTR)
1452 if ((csp_flags & FC_SP_FT_FPORT) == 0) {
1453 if (e_d_tov > lport->e_d_tov)
1454 lport->e_d_tov = e_d_tov;
1455 lport->r_a_tov = 2 * e_d_tov;
1456 printk(KERN_INFO "libfc: Port (%6x) entered "
1457 "point to point mode\n", did);
1458 fc_lport_ptp_setup(lport, ntoh24(fh->fh_s_id),
1464 lport->e_d_tov = e_d_tov;
1465 lport->r_a_tov = r_a_tov;
1466 fc_host_fabric_name(lport->host) =
1467 get_unaligned_be64(&flp->fl_wwnn);
1468 fc_lport_enter_dns(lport);
1472 FC_LPORT_DBG(lport, "Bad FLOGI response\n");
1478 mutex_unlock(&lport->lp_mutex);
1482 * fc_rport_enter_flogi() - Send a FLOGI request to the fabric manager
1483 * @lport: Fibre Channel local port to be logged in to the fabric
1485 * Locking Note: The lport lock is expected to be held before calling
1488 void fc_lport_enter_flogi(struct fc_lport *lport)
1490 struct fc_frame *fp;
1492 FC_LPORT_DBG(lport, "Entered FLOGI state from %s state\n",
1493 fc_lport_state(lport));
1495 fc_lport_state_enter(lport, LPORT_ST_FLOGI);
1497 fp = fc_frame_alloc(lport, sizeof(struct fc_els_flogi));
1499 return fc_lport_error(lport, fp);
1501 if (!lport->tt.elsct_send(lport, FC_FID_FLOGI, fp, ELS_FLOGI,
1502 fc_lport_flogi_resp, lport, lport->e_d_tov))
1503 fc_lport_error(lport, NULL);
1506 /* Configure a fc_lport */
1507 int fc_lport_config(struct fc_lport *lport)
1509 INIT_DELAYED_WORK(&lport->retry_work, fc_lport_timeout);
1510 mutex_init(&lport->lp_mutex);
1512 fc_lport_state_enter(lport, LPORT_ST_DISABLED);
1514 fc_lport_add_fc4_type(lport, FC_TYPE_FCP);
1515 fc_lport_add_fc4_type(lport, FC_TYPE_CT);
1519 EXPORT_SYMBOL(fc_lport_config);
1521 int fc_lport_init(struct fc_lport *lport)
1523 if (!lport->tt.lport_recv)
1524 lport->tt.lport_recv = fc_lport_recv_req;
1526 if (!lport->tt.lport_reset)
1527 lport->tt.lport_reset = fc_lport_reset;
1529 fc_host_port_type(lport->host) = FC_PORTTYPE_NPORT;
1530 fc_host_node_name(lport->host) = lport->wwnn;
1531 fc_host_port_name(lport->host) = lport->wwpn;
1532 fc_host_supported_classes(lport->host) = FC_COS_CLASS3;
1533 memset(fc_host_supported_fc4s(lport->host), 0,
1534 sizeof(fc_host_supported_fc4s(lport->host)));
1535 fc_host_supported_fc4s(lport->host)[2] = 1;
1536 fc_host_supported_fc4s(lport->host)[7] = 1;
1538 /* This value is also unchanging */
1539 memset(fc_host_active_fc4s(lport->host), 0,
1540 sizeof(fc_host_active_fc4s(lport->host)));
1541 fc_host_active_fc4s(lport->host)[2] = 1;
1542 fc_host_active_fc4s(lport->host)[7] = 1;
1543 fc_host_maxframe_size(lport->host) = lport->mfs;
1544 fc_host_supported_speeds(lport->host) = 0;
1545 if (lport->link_supported_speeds & FC_PORTSPEED_1GBIT)
1546 fc_host_supported_speeds(lport->host) |= FC_PORTSPEED_1GBIT;
1547 if (lport->link_supported_speeds & FC_PORTSPEED_10GBIT)
1548 fc_host_supported_speeds(lport->host) |= FC_PORTSPEED_10GBIT;
1552 EXPORT_SYMBOL(fc_lport_init);