142e9a92fSRobert Love /* 242e9a92fSRobert Love * Copyright(c) 2007 Intel Corporation. All rights reserved. 342e9a92fSRobert Love * 442e9a92fSRobert Love * This program is free software; you can redistribute it and/or modify it 542e9a92fSRobert Love * under the terms and conditions of the GNU General Public License, 642e9a92fSRobert Love * version 2, as published by the Free Software Foundation. 742e9a92fSRobert Love * 842e9a92fSRobert Love * This program is distributed in the hope it will be useful, but WITHOUT 942e9a92fSRobert Love * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1042e9a92fSRobert Love * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 1142e9a92fSRobert Love * more details. 1242e9a92fSRobert Love * 1342e9a92fSRobert Love * You should have received a copy of the GNU General Public License along with 1442e9a92fSRobert Love * this program; if not, write to the Free Software Foundation, Inc., 1542e9a92fSRobert Love * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA. 1642e9a92fSRobert Love * 1742e9a92fSRobert Love * Maintained at www.Open-FCoE.org 1842e9a92fSRobert Love */ 1942e9a92fSRobert Love 2042e9a92fSRobert Love /* 2142e9a92fSRobert Love * PORT LOCKING NOTES 2242e9a92fSRobert Love * 2342e9a92fSRobert Love * These comments only apply to the 'port code' which consists of the lport, 2442e9a92fSRobert Love * disc and rport blocks. 2542e9a92fSRobert Love * 2642e9a92fSRobert Love * MOTIVATION 2742e9a92fSRobert Love * 2842e9a92fSRobert Love * The lport, disc and rport blocks all have mutexes that are used to protect 2942e9a92fSRobert Love * those objects. The main motivation for these locks is to prevent from 3042e9a92fSRobert Love * having an lport reset just before we send a frame. In that scenario the 3142e9a92fSRobert Love * lport's FID would get set to zero and then we'd send a frame with an 3242e9a92fSRobert Love * invalid SID. We also need to ensure that states don't change unexpectedly 3342e9a92fSRobert Love * while processing another state. 3442e9a92fSRobert Love * 3542e9a92fSRobert Love * HEIRARCHY 3642e9a92fSRobert Love * 3742e9a92fSRobert Love * The following heirarchy defines the locking rules. A greater lock 3842e9a92fSRobert Love * may be held before acquiring a lesser lock, but a lesser lock should never 3942e9a92fSRobert Love * be held while attempting to acquire a greater lock. Here is the heirarchy- 4042e9a92fSRobert Love * 4142e9a92fSRobert Love * lport > disc, lport > rport, disc > rport 4242e9a92fSRobert Love * 4342e9a92fSRobert Love * CALLBACKS 4442e9a92fSRobert Love * 4542e9a92fSRobert Love * The callbacks cause complications with this scheme. There is a callback 4642e9a92fSRobert Love * from the rport (to either lport or disc) and a callback from disc 4742e9a92fSRobert Love * (to the lport). 4842e9a92fSRobert Love * 4942e9a92fSRobert Love * As rports exit the rport state machine a callback is made to the owner of 5042e9a92fSRobert Love * the rport to notify success or failure. Since the callback is likely to 5142e9a92fSRobert Love * cause the lport or disc to grab its lock we cannot hold the rport lock 5242e9a92fSRobert Love * while making the callback. To ensure that the rport is not free'd while 5342e9a92fSRobert Love * processing the callback the rport callbacks are serialized through a 5442e9a92fSRobert Love * single-threaded workqueue. An rport would never be free'd while in a 5542e9a92fSRobert Love * callback handler becuase no other rport work in this queue can be executed 5642e9a92fSRobert Love * at the same time. 5742e9a92fSRobert Love * 5842e9a92fSRobert Love * When discovery succeeds or fails a callback is made to the lport as 5942e9a92fSRobert Love * notification. Currently, succesful discovery causes the lport to take no 6042e9a92fSRobert Love * action. A failure will cause the lport to reset. There is likely a circular 6142e9a92fSRobert Love * locking problem with this implementation. 6242e9a92fSRobert Love */ 6342e9a92fSRobert Love 6442e9a92fSRobert Love /* 6542e9a92fSRobert Love * LPORT LOCKING 6642e9a92fSRobert Love * 6742e9a92fSRobert Love * The critical sections protected by the lport's mutex are quite broad and 6842e9a92fSRobert Love * may be improved upon in the future. The lport code and its locking doesn't 6942e9a92fSRobert Love * influence the I/O path, so excessive locking doesn't penalize I/O 7042e9a92fSRobert Love * performance. 7142e9a92fSRobert Love * 7242e9a92fSRobert Love * The strategy is to lock whenever processing a request or response. Note 7342e9a92fSRobert Love * that every _enter_* function corresponds to a state change. They generally 7442e9a92fSRobert Love * change the lports state and then send a request out on the wire. We lock 7542e9a92fSRobert Love * before calling any of these functions to protect that state change. This 7642e9a92fSRobert Love * means that the entry points into the lport block manage the locks while 7742e9a92fSRobert Love * the state machine can transition between states (i.e. _enter_* functions) 7842e9a92fSRobert Love * while always staying protected. 7942e9a92fSRobert Love * 8042e9a92fSRobert Love * When handling responses we also hold the lport mutex broadly. When the 8142e9a92fSRobert Love * lport receives the response frame it locks the mutex and then calls the 8242e9a92fSRobert Love * appropriate handler for the particuar response. Generally a response will 8342e9a92fSRobert Love * trigger a state change and so the lock must already be held. 8442e9a92fSRobert Love * 8542e9a92fSRobert Love * Retries also have to consider the locking. The retries occur from a work 8642e9a92fSRobert Love * context and the work function will lock the lport and then retry the state 8742e9a92fSRobert Love * (i.e. _enter_* function). 8842e9a92fSRobert Love */ 8942e9a92fSRobert Love 9042e9a92fSRobert Love #include <linux/timer.h> 9142e9a92fSRobert Love #include <asm/unaligned.h> 9242e9a92fSRobert Love 9342e9a92fSRobert Love #include <scsi/fc/fc_gs.h> 9442e9a92fSRobert Love 9542e9a92fSRobert Love #include <scsi/libfc.h> 9642e9a92fSRobert Love #include <scsi/fc_encode.h> 9742e9a92fSRobert Love 988866a5d9SRobert Love #include "fc_libfc.h" 998866a5d9SRobert Love 10042e9a92fSRobert Love /* Fabric IDs to use for point-to-point mode, chosen on whims. */ 10142e9a92fSRobert Love #define FC_LOCAL_PTP_FID_LO 0x010101 10242e9a92fSRobert Love #define FC_LOCAL_PTP_FID_HI 0x010102 10342e9a92fSRobert Love 10442e9a92fSRobert Love #define DNS_DELAY 3 /* Discovery delay after RSCN (in seconds)*/ 10542e9a92fSRobert Love 10642e9a92fSRobert Love static void fc_lport_error(struct fc_lport *, struct fc_frame *); 10742e9a92fSRobert Love 10842e9a92fSRobert Love static void fc_lport_enter_reset(struct fc_lport *); 10942e9a92fSRobert Love static void fc_lport_enter_flogi(struct fc_lport *); 11042e9a92fSRobert Love static void fc_lport_enter_dns(struct fc_lport *); 111c914f7d1SChris Leech static void fc_lport_enter_ns(struct fc_lport *, enum fc_lport_state); 11242e9a92fSRobert Love static void fc_lport_enter_scr(struct fc_lport *); 11342e9a92fSRobert Love static void fc_lport_enter_ready(struct fc_lport *); 11442e9a92fSRobert Love static void fc_lport_enter_logo(struct fc_lport *); 11542e9a92fSRobert Love 11642e9a92fSRobert Love static const char *fc_lport_state_names[] = { 117b1d9fd55SJoe Eykholt [LPORT_ST_DISABLED] = "disabled", 11842e9a92fSRobert Love [LPORT_ST_FLOGI] = "FLOGI", 11942e9a92fSRobert Love [LPORT_ST_DNS] = "dNS", 120c9c7bd7aSChris Leech [LPORT_ST_RNN_ID] = "RNN_ID", 1215baa17c3SChris Leech [LPORT_ST_RSNN_NN] = "RSNN_NN", 122c9866a54SChris Leech [LPORT_ST_RSPN_ID] = "RSPN_ID", 12342e9a92fSRobert Love [LPORT_ST_RFT_ID] = "RFT_ID", 12442e9a92fSRobert Love [LPORT_ST_SCR] = "SCR", 12542e9a92fSRobert Love [LPORT_ST_READY] = "Ready", 12642e9a92fSRobert Love [LPORT_ST_LOGO] = "LOGO", 12742e9a92fSRobert Love [LPORT_ST_RESET] = "reset", 12842e9a92fSRobert Love }; 12942e9a92fSRobert Love 13042e9a92fSRobert Love static int fc_frame_drop(struct fc_lport *lport, struct fc_frame *fp) 13142e9a92fSRobert Love { 13242e9a92fSRobert Love fc_frame_free(fp); 13342e9a92fSRobert Love return 0; 13442e9a92fSRobert Love } 13542e9a92fSRobert Love 13642e9a92fSRobert Love /** 13734f42a07SRobert Love * fc_lport_rport_callback() - Event handler for rport events 13842e9a92fSRobert Love * @lport: The lport which is receiving the event 1399fb9d328SJoe Eykholt * @rdata: private remote port data 14042e9a92fSRobert Love * @event: The event that occured 14142e9a92fSRobert Love * 14242e9a92fSRobert Love * Locking Note: The rport lock should not be held when calling 14342e9a92fSRobert Love * this function. 14442e9a92fSRobert Love */ 14542e9a92fSRobert Love static void fc_lport_rport_callback(struct fc_lport *lport, 1469fb9d328SJoe Eykholt struct fc_rport_priv *rdata, 14742e9a92fSRobert Love enum fc_rport_event event) 14842e9a92fSRobert Love { 1497414705eSRobert Love FC_LPORT_DBG(lport, "Received a %d event for port (%6x)\n", event, 150f211fa51SJoe Eykholt rdata->ids.port_id); 15142e9a92fSRobert Love 152b5cbf083SJoe Eykholt mutex_lock(&lport->lp_mutex); 15342e9a92fSRobert Love switch (event) { 1544c0f62b5SJoe Eykholt case RPORT_EV_READY: 15542e9a92fSRobert Love if (lport->state == LPORT_ST_DNS) { 1569fb9d328SJoe Eykholt lport->dns_rp = rdata; 157c914f7d1SChris Leech fc_lport_enter_ns(lport, LPORT_ST_RNN_ID); 15842e9a92fSRobert Love } else { 1594c0f62b5SJoe Eykholt FC_LPORT_DBG(lport, "Received an READY event " 1607414705eSRobert Love "on port (%6x) for the directory " 16142e9a92fSRobert Love "server, but the lport is not " 16242e9a92fSRobert Love "in the DNS state, it's in the " 163f211fa51SJoe Eykholt "%d state", rdata->ids.port_id, 16442e9a92fSRobert Love lport->state); 1659fb9d328SJoe Eykholt lport->tt.rport_logoff(rdata); 16642e9a92fSRobert Love } 16742e9a92fSRobert Love break; 16842e9a92fSRobert Love case RPORT_EV_LOGO: 16942e9a92fSRobert Love case RPORT_EV_FAILED: 17042e9a92fSRobert Love case RPORT_EV_STOP: 17142e9a92fSRobert Love lport->dns_rp = NULL; 17242e9a92fSRobert Love break; 17342e9a92fSRobert Love case RPORT_EV_NONE: 17442e9a92fSRobert Love break; 17542e9a92fSRobert Love } 176b5cbf083SJoe Eykholt mutex_unlock(&lport->lp_mutex); 17742e9a92fSRobert Love } 17842e9a92fSRobert Love 17942e9a92fSRobert Love /** 18034f42a07SRobert Love * fc_lport_state() - Return a string which represents the lport's state 18142e9a92fSRobert Love * @lport: The lport whose state is to converted to a string 18242e9a92fSRobert Love */ 18342e9a92fSRobert Love static const char *fc_lport_state(struct fc_lport *lport) 18442e9a92fSRobert Love { 18542e9a92fSRobert Love const char *cp; 18642e9a92fSRobert Love 18742e9a92fSRobert Love cp = fc_lport_state_names[lport->state]; 18842e9a92fSRobert Love if (!cp) 18942e9a92fSRobert Love cp = "unknown"; 19042e9a92fSRobert Love return cp; 19142e9a92fSRobert Love } 19242e9a92fSRobert Love 19342e9a92fSRobert Love /** 19434f42a07SRobert Love * fc_lport_ptp_setup() - Create an rport for point-to-point mode 19542e9a92fSRobert Love * @lport: The lport to attach the ptp rport to 19642e9a92fSRobert Love * @fid: The FID of the ptp rport 19742e9a92fSRobert Love * @remote_wwpn: The WWPN of the ptp rport 19842e9a92fSRobert Love * @remote_wwnn: The WWNN of the ptp rport 19942e9a92fSRobert Love */ 20042e9a92fSRobert Love static void fc_lport_ptp_setup(struct fc_lport *lport, 20142e9a92fSRobert Love u32 remote_fid, u64 remote_wwpn, 20242e9a92fSRobert Love u64 remote_wwnn) 20342e9a92fSRobert Love { 20448f00902SJoe Eykholt mutex_lock(&lport->disc.disc_mutex); 20548f00902SJoe Eykholt if (lport->ptp_rp) 20642e9a92fSRobert Love lport->tt.rport_logoff(lport->ptp_rp); 2079737e6a7SRobert Love lport->ptp_rp = lport->tt.rport_create(lport, remote_fid); 2089737e6a7SRobert Love lport->ptp_rp->ids.port_name = remote_wwpn; 2099737e6a7SRobert Love lport->ptp_rp->ids.node_name = remote_wwnn; 21048f00902SJoe Eykholt mutex_unlock(&lport->disc.disc_mutex); 21142e9a92fSRobert Love 21242e9a92fSRobert Love lport->tt.rport_login(lport->ptp_rp); 21342e9a92fSRobert Love 21442e9a92fSRobert Love fc_lport_enter_ready(lport); 21542e9a92fSRobert Love } 21642e9a92fSRobert Love 21742e9a92fSRobert Love void fc_get_host_port_type(struct Scsi_Host *shost) 21842e9a92fSRobert Love { 21942e9a92fSRobert Love /* TODO - currently just NPORT */ 22042e9a92fSRobert Love fc_host_port_type(shost) = FC_PORTTYPE_NPORT; 22142e9a92fSRobert Love } 22242e9a92fSRobert Love EXPORT_SYMBOL(fc_get_host_port_type); 22342e9a92fSRobert Love 22442e9a92fSRobert Love void fc_get_host_port_state(struct Scsi_Host *shost) 22542e9a92fSRobert Love { 22642e9a92fSRobert Love struct fc_lport *lp = shost_priv(shost); 22742e9a92fSRobert Love 2288faecddbSChris Leech mutex_lock(&lp->lp_mutex); 2298faecddbSChris Leech if (!lp->link_up) 2308faecddbSChris Leech fc_host_port_state(shost) = FC_PORTSTATE_LINKDOWN; 23142e9a92fSRobert Love else 2328faecddbSChris Leech switch (lp->state) { 2338faecddbSChris Leech case LPORT_ST_READY: 2348faecddbSChris Leech fc_host_port_state(shost) = FC_PORTSTATE_ONLINE; 2358faecddbSChris Leech break; 2368faecddbSChris Leech default: 23742e9a92fSRobert Love fc_host_port_state(shost) = FC_PORTSTATE_OFFLINE; 23842e9a92fSRobert Love } 2398faecddbSChris Leech mutex_unlock(&lp->lp_mutex); 2408faecddbSChris Leech } 24142e9a92fSRobert Love EXPORT_SYMBOL(fc_get_host_port_state); 24242e9a92fSRobert Love 24342e9a92fSRobert Love void fc_get_host_speed(struct Scsi_Host *shost) 24442e9a92fSRobert Love { 24542e9a92fSRobert Love struct fc_lport *lport = shost_priv(shost); 24642e9a92fSRobert Love 24742e9a92fSRobert Love fc_host_speed(shost) = lport->link_speed; 24842e9a92fSRobert Love } 24942e9a92fSRobert Love EXPORT_SYMBOL(fc_get_host_speed); 25042e9a92fSRobert Love 25142e9a92fSRobert Love struct fc_host_statistics *fc_get_host_stats(struct Scsi_Host *shost) 25242e9a92fSRobert Love { 25342e9a92fSRobert Love struct fc_host_statistics *fcoe_stats; 25442e9a92fSRobert Love struct fc_lport *lp = shost_priv(shost); 25542e9a92fSRobert Love struct timespec v0, v1; 256582b45bcSRobert Love unsigned int cpu; 25742e9a92fSRobert Love 25842e9a92fSRobert Love fcoe_stats = &lp->host_stats; 25942e9a92fSRobert Love memset(fcoe_stats, 0, sizeof(struct fc_host_statistics)); 26042e9a92fSRobert Love 26142e9a92fSRobert Love jiffies_to_timespec(jiffies, &v0); 26242e9a92fSRobert Love jiffies_to_timespec(lp->boot_time, &v1); 26342e9a92fSRobert Love fcoe_stats->seconds_since_last_reset = (v0.tv_sec - v1.tv_sec); 26442e9a92fSRobert Love 265582b45bcSRobert Love for_each_possible_cpu(cpu) { 266582b45bcSRobert Love struct fcoe_dev_stats *stats; 267582b45bcSRobert Love 268582b45bcSRobert Love stats = per_cpu_ptr(lp->dev_stats, cpu); 269582b45bcSRobert Love 27042e9a92fSRobert Love fcoe_stats->tx_frames += stats->TxFrames; 27142e9a92fSRobert Love fcoe_stats->tx_words += stats->TxWords; 27242e9a92fSRobert Love fcoe_stats->rx_frames += stats->RxFrames; 27342e9a92fSRobert Love fcoe_stats->rx_words += stats->RxWords; 27442e9a92fSRobert Love fcoe_stats->error_frames += stats->ErrorFrames; 27542e9a92fSRobert Love fcoe_stats->invalid_crc_count += stats->InvalidCRCCount; 27642e9a92fSRobert Love fcoe_stats->fcp_input_requests += stats->InputRequests; 27742e9a92fSRobert Love fcoe_stats->fcp_output_requests += stats->OutputRequests; 27842e9a92fSRobert Love fcoe_stats->fcp_control_requests += stats->ControlRequests; 27942e9a92fSRobert Love fcoe_stats->fcp_input_megabytes += stats->InputMegabytes; 28042e9a92fSRobert Love fcoe_stats->fcp_output_megabytes += stats->OutputMegabytes; 28142e9a92fSRobert Love fcoe_stats->link_failure_count += stats->LinkFailureCount; 28242e9a92fSRobert Love } 28342e9a92fSRobert Love fcoe_stats->lip_count = -1; 28442e9a92fSRobert Love fcoe_stats->nos_count = -1; 28542e9a92fSRobert Love fcoe_stats->loss_of_sync_count = -1; 28642e9a92fSRobert Love fcoe_stats->loss_of_signal_count = -1; 28742e9a92fSRobert Love fcoe_stats->prim_seq_protocol_err_count = -1; 28842e9a92fSRobert Love fcoe_stats->dumped_frames = -1; 28942e9a92fSRobert Love return fcoe_stats; 29042e9a92fSRobert Love } 29142e9a92fSRobert Love EXPORT_SYMBOL(fc_get_host_stats); 29242e9a92fSRobert Love 29342e9a92fSRobert Love /* 29442e9a92fSRobert Love * Fill in FLOGI command for request. 29542e9a92fSRobert Love */ 29642e9a92fSRobert Love static void 29742e9a92fSRobert Love fc_lport_flogi_fill(struct fc_lport *lport, struct fc_els_flogi *flogi, 29842e9a92fSRobert Love unsigned int op) 29942e9a92fSRobert Love { 30042e9a92fSRobert Love struct fc_els_csp *sp; 30142e9a92fSRobert Love struct fc_els_cssp *cp; 30242e9a92fSRobert Love 30342e9a92fSRobert Love memset(flogi, 0, sizeof(*flogi)); 30442e9a92fSRobert Love flogi->fl_cmd = (u8) op; 30542e9a92fSRobert Love put_unaligned_be64(lport->wwpn, &flogi->fl_wwpn); 30642e9a92fSRobert Love put_unaligned_be64(lport->wwnn, &flogi->fl_wwnn); 30742e9a92fSRobert Love sp = &flogi->fl_csp; 30842e9a92fSRobert Love sp->sp_hi_ver = 0x20; 30942e9a92fSRobert Love sp->sp_lo_ver = 0x20; 31042e9a92fSRobert Love sp->sp_bb_cred = htons(10); /* this gets set by gateway */ 31142e9a92fSRobert Love sp->sp_bb_data = htons((u16) lport->mfs); 31242e9a92fSRobert Love cp = &flogi->fl_cssp[3 - 1]; /* class 3 parameters */ 31342e9a92fSRobert Love cp->cp_class = htons(FC_CPC_VALID | FC_CPC_SEQ); 31442e9a92fSRobert Love if (op != ELS_FLOGI) { 31542e9a92fSRobert Love sp->sp_features = htons(FC_SP_FT_CIRO); 31642e9a92fSRobert Love sp->sp_tot_seq = htons(255); /* seq. we accept */ 31742e9a92fSRobert Love sp->sp_rel_off = htons(0x1f); 31842e9a92fSRobert Love sp->sp_e_d_tov = htonl(lport->e_d_tov); 31942e9a92fSRobert Love 32042e9a92fSRobert Love cp->cp_rdfs = htons((u16) lport->mfs); 32142e9a92fSRobert Love cp->cp_con_seq = htons(255); 32242e9a92fSRobert Love cp->cp_open_seq = 1; 32342e9a92fSRobert Love } 32442e9a92fSRobert Love } 32542e9a92fSRobert Love 32642e9a92fSRobert Love /* 32742e9a92fSRobert Love * Add a supported FC-4 type. 32842e9a92fSRobert Love */ 32942e9a92fSRobert Love static void fc_lport_add_fc4_type(struct fc_lport *lport, enum fc_fh_type type) 33042e9a92fSRobert Love { 33142e9a92fSRobert Love __be32 *mp; 33242e9a92fSRobert Love 33342e9a92fSRobert Love mp = &lport->fcts.ff_type_map[type / FC_NS_BPW]; 33442e9a92fSRobert Love *mp = htonl(ntohl(*mp) | 1UL << (type % FC_NS_BPW)); 33542e9a92fSRobert Love } 33642e9a92fSRobert Love 33742e9a92fSRobert Love /** 33834f42a07SRobert Love * fc_lport_recv_rlir_req() - Handle received Registered Link Incident Report. 33942e9a92fSRobert Love * @lport: Fibre Channel local port recieving the RLIR 34042e9a92fSRobert Love * @sp: current sequence in the RLIR exchange 34142e9a92fSRobert Love * @fp: RLIR request frame 34242e9a92fSRobert Love * 3431b69bc06SJoe Eykholt * Locking Note: The lport lock is expected to be held before calling 34442e9a92fSRobert Love * this function. 34542e9a92fSRobert Love */ 34642e9a92fSRobert Love static void fc_lport_recv_rlir_req(struct fc_seq *sp, struct fc_frame *fp, 34742e9a92fSRobert Love struct fc_lport *lport) 34842e9a92fSRobert Love { 3497414705eSRobert Love FC_LPORT_DBG(lport, "Received RLIR request while in state %s\n", 35042e9a92fSRobert Love fc_lport_state(lport)); 35142e9a92fSRobert Love 35242e9a92fSRobert Love lport->tt.seq_els_rsp_send(sp, ELS_LS_ACC, NULL); 35342e9a92fSRobert Love fc_frame_free(fp); 35442e9a92fSRobert Love } 35542e9a92fSRobert Love 35642e9a92fSRobert Love /** 35734f42a07SRobert Love * fc_lport_recv_echo_req() - Handle received ECHO request 35842e9a92fSRobert Love * @lport: Fibre Channel local port recieving the ECHO 35942e9a92fSRobert Love * @sp: current sequence in the ECHO exchange 36042e9a92fSRobert Love * @fp: ECHO request frame 36142e9a92fSRobert Love * 3621b69bc06SJoe Eykholt * Locking Note: The lport lock is expected to be held before calling 36342e9a92fSRobert Love * this function. 36442e9a92fSRobert Love */ 36542e9a92fSRobert Love static void fc_lport_recv_echo_req(struct fc_seq *sp, struct fc_frame *in_fp, 36642e9a92fSRobert Love struct fc_lport *lport) 36742e9a92fSRobert Love { 36842e9a92fSRobert Love struct fc_frame *fp; 36942e9a92fSRobert Love struct fc_exch *ep = fc_seq_exch(sp); 37042e9a92fSRobert Love unsigned int len; 37142e9a92fSRobert Love void *pp; 37242e9a92fSRobert Love void *dp; 37342e9a92fSRobert Love u32 f_ctl; 37442e9a92fSRobert Love 3751b69bc06SJoe Eykholt FC_LPORT_DBG(lport, "Received ECHO request while in state %s\n", 37642e9a92fSRobert Love fc_lport_state(lport)); 37742e9a92fSRobert Love 37842e9a92fSRobert Love len = fr_len(in_fp) - sizeof(struct fc_frame_header); 37942e9a92fSRobert Love pp = fc_frame_payload_get(in_fp, len); 38042e9a92fSRobert Love 38142e9a92fSRobert Love if (len < sizeof(__be32)) 38242e9a92fSRobert Love len = sizeof(__be32); 38342e9a92fSRobert Love 38442e9a92fSRobert Love fp = fc_frame_alloc(lport, len); 38542e9a92fSRobert Love if (fp) { 38642e9a92fSRobert Love dp = fc_frame_payload_get(fp, len); 38742e9a92fSRobert Love memcpy(dp, pp, len); 3881b69bc06SJoe Eykholt *((__be32 *)dp) = htonl(ELS_LS_ACC << 24); 38942e9a92fSRobert Love sp = lport->tt.seq_start_next(sp); 39042e9a92fSRobert Love f_ctl = FC_FC_EX_CTX | FC_FC_LAST_SEQ | FC_FC_END_SEQ; 39142e9a92fSRobert Love fc_fill_fc_hdr(fp, FC_RCTL_ELS_REP, ep->did, ep->sid, 39242e9a92fSRobert Love FC_TYPE_ELS, f_ctl, 0); 39342e9a92fSRobert Love lport->tt.seq_send(lport, sp, fp); 39442e9a92fSRobert Love } 39542e9a92fSRobert Love fc_frame_free(in_fp); 39642e9a92fSRobert Love } 39742e9a92fSRobert Love 39842e9a92fSRobert Love /** 3991b69bc06SJoe Eykholt * fc_lport_recv_rnid_req() - Handle received Request Node ID data request 4001b69bc06SJoe Eykholt * @sp: The sequence in the RNID exchange 4011b69bc06SJoe Eykholt * @fp: The RNID request frame 4021b69bc06SJoe Eykholt * @lport: The local port recieving the RNID 40342e9a92fSRobert Love * 4041b69bc06SJoe Eykholt * Locking Note: The lport lock is expected to be held before calling 40542e9a92fSRobert Love * this function. 40642e9a92fSRobert Love */ 40742e9a92fSRobert Love static void fc_lport_recv_rnid_req(struct fc_seq *sp, struct fc_frame *in_fp, 40842e9a92fSRobert Love struct fc_lport *lport) 40942e9a92fSRobert Love { 41042e9a92fSRobert Love struct fc_frame *fp; 41142e9a92fSRobert Love struct fc_exch *ep = fc_seq_exch(sp); 41242e9a92fSRobert Love struct fc_els_rnid *req; 41342e9a92fSRobert Love struct { 41442e9a92fSRobert Love struct fc_els_rnid_resp rnid; 41542e9a92fSRobert Love struct fc_els_rnid_cid cid; 41642e9a92fSRobert Love struct fc_els_rnid_gen gen; 41742e9a92fSRobert Love } *rp; 41842e9a92fSRobert Love struct fc_seq_els_data rjt_data; 41942e9a92fSRobert Love u8 fmt; 42042e9a92fSRobert Love size_t len; 42142e9a92fSRobert Love u32 f_ctl; 42242e9a92fSRobert Love 4237414705eSRobert Love FC_LPORT_DBG(lport, "Received RNID request while in state %s\n", 42442e9a92fSRobert Love fc_lport_state(lport)); 42542e9a92fSRobert Love 42642e9a92fSRobert Love req = fc_frame_payload_get(in_fp, sizeof(*req)); 42742e9a92fSRobert Love if (!req) { 42842e9a92fSRobert Love rjt_data.fp = NULL; 42942e9a92fSRobert Love rjt_data.reason = ELS_RJT_LOGIC; 43042e9a92fSRobert Love rjt_data.explan = ELS_EXPL_NONE; 43142e9a92fSRobert Love lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data); 43242e9a92fSRobert Love } else { 43342e9a92fSRobert Love fmt = req->rnid_fmt; 43442e9a92fSRobert Love len = sizeof(*rp); 43542e9a92fSRobert Love if (fmt != ELS_RNIDF_GEN || 43642e9a92fSRobert Love ntohl(lport->rnid_gen.rnid_atype) == 0) { 43742e9a92fSRobert Love fmt = ELS_RNIDF_NONE; /* nothing to provide */ 43842e9a92fSRobert Love len -= sizeof(rp->gen); 43942e9a92fSRobert Love } 44042e9a92fSRobert Love fp = fc_frame_alloc(lport, len); 44142e9a92fSRobert Love if (fp) { 44242e9a92fSRobert Love rp = fc_frame_payload_get(fp, len); 44342e9a92fSRobert Love memset(rp, 0, len); 44442e9a92fSRobert Love rp->rnid.rnid_cmd = ELS_LS_ACC; 44542e9a92fSRobert Love rp->rnid.rnid_fmt = fmt; 44642e9a92fSRobert Love rp->rnid.rnid_cid_len = sizeof(rp->cid); 44742e9a92fSRobert Love rp->cid.rnid_wwpn = htonll(lport->wwpn); 44842e9a92fSRobert Love rp->cid.rnid_wwnn = htonll(lport->wwnn); 44942e9a92fSRobert Love if (fmt == ELS_RNIDF_GEN) { 45042e9a92fSRobert Love rp->rnid.rnid_sid_len = sizeof(rp->gen); 45142e9a92fSRobert Love memcpy(&rp->gen, &lport->rnid_gen, 45242e9a92fSRobert Love sizeof(rp->gen)); 45342e9a92fSRobert Love } 45442e9a92fSRobert Love sp = lport->tt.seq_start_next(sp); 45542e9a92fSRobert Love f_ctl = FC_FC_EX_CTX | FC_FC_LAST_SEQ; 45642e9a92fSRobert Love f_ctl |= FC_FC_END_SEQ | FC_FC_SEQ_INIT; 45742e9a92fSRobert Love fc_fill_fc_hdr(fp, FC_RCTL_ELS_REP, ep->did, ep->sid, 45842e9a92fSRobert Love FC_TYPE_ELS, f_ctl, 0); 45942e9a92fSRobert Love lport->tt.seq_send(lport, sp, fp); 46042e9a92fSRobert Love } 46142e9a92fSRobert Love } 46242e9a92fSRobert Love fc_frame_free(in_fp); 46342e9a92fSRobert Love } 46442e9a92fSRobert Love 46542e9a92fSRobert Love /** 46634f42a07SRobert Love * fc_lport_recv_logo_req() - Handle received fabric LOGO request 46742e9a92fSRobert Love * @lport: Fibre Channel local port recieving the LOGO 46842e9a92fSRobert Love * @sp: current sequence in the LOGO exchange 46942e9a92fSRobert Love * @fp: LOGO request frame 47042e9a92fSRobert Love * 47142e9a92fSRobert Love * Locking Note: The lport lock is exected to be held before calling 47242e9a92fSRobert Love * this function. 47342e9a92fSRobert Love */ 47442e9a92fSRobert Love static void fc_lport_recv_logo_req(struct fc_seq *sp, struct fc_frame *fp, 47542e9a92fSRobert Love struct fc_lport *lport) 47642e9a92fSRobert Love { 47742e9a92fSRobert Love lport->tt.seq_els_rsp_send(sp, ELS_LS_ACC, NULL); 47842e9a92fSRobert Love fc_lport_enter_reset(lport); 47942e9a92fSRobert Love fc_frame_free(fp); 48042e9a92fSRobert Love } 48142e9a92fSRobert Love 48242e9a92fSRobert Love /** 48334f42a07SRobert Love * fc_fabric_login() - Start the lport state machine 48442e9a92fSRobert Love * @lport: The lport that should log into the fabric 48542e9a92fSRobert Love * 48642e9a92fSRobert Love * Locking Note: This function should not be called 48742e9a92fSRobert Love * with the lport lock held. 48842e9a92fSRobert Love */ 48942e9a92fSRobert Love int fc_fabric_login(struct fc_lport *lport) 49042e9a92fSRobert Love { 49142e9a92fSRobert Love int rc = -1; 49242e9a92fSRobert Love 49342e9a92fSRobert Love mutex_lock(&lport->lp_mutex); 494b1d9fd55SJoe Eykholt if (lport->state == LPORT_ST_DISABLED) { 49542e9a92fSRobert Love fc_lport_enter_reset(lport); 49642e9a92fSRobert Love rc = 0; 49742e9a92fSRobert Love } 49842e9a92fSRobert Love mutex_unlock(&lport->lp_mutex); 49942e9a92fSRobert Love 50042e9a92fSRobert Love return rc; 50142e9a92fSRobert Love } 50242e9a92fSRobert Love EXPORT_SYMBOL(fc_fabric_login); 50342e9a92fSRobert Love 50442e9a92fSRobert Love /** 5058faecddbSChris Leech * __fc_linkup() - Handler for transport linkup events 5068faecddbSChris Leech * @lport: The lport whose link is up 5078faecddbSChris Leech * 5088faecddbSChris Leech * Locking: must be called with the lp_mutex held 5098faecddbSChris Leech */ 5108faecddbSChris Leech void __fc_linkup(struct fc_lport *lport) 5118faecddbSChris Leech { 5128faecddbSChris Leech if (!lport->link_up) { 5138faecddbSChris Leech lport->link_up = 1; 5148faecddbSChris Leech 5158faecddbSChris Leech if (lport->state == LPORT_ST_RESET) 5168faecddbSChris Leech fc_lport_enter_flogi(lport); 5178faecddbSChris Leech } 5188faecddbSChris Leech } 5198faecddbSChris Leech 5208faecddbSChris Leech /** 52134f42a07SRobert Love * fc_linkup() - Handler for transport linkup events 52242e9a92fSRobert Love * @lport: The lport whose link is up 52342e9a92fSRobert Love */ 52442e9a92fSRobert Love void fc_linkup(struct fc_lport *lport) 52542e9a92fSRobert Love { 5267414705eSRobert Love printk(KERN_INFO "libfc: Link up on port (%6x)\n", 52742e9a92fSRobert Love fc_host_port_id(lport->host)); 52842e9a92fSRobert Love 52942e9a92fSRobert Love mutex_lock(&lport->lp_mutex); 5308faecddbSChris Leech __fc_linkup(lport); 53142e9a92fSRobert Love mutex_unlock(&lport->lp_mutex); 53242e9a92fSRobert Love } 53342e9a92fSRobert Love EXPORT_SYMBOL(fc_linkup); 53442e9a92fSRobert Love 53542e9a92fSRobert Love /** 5368faecddbSChris Leech * __fc_linkdown() - Handler for transport linkdown events 5378faecddbSChris Leech * @lport: The lport whose link is down 5388faecddbSChris Leech * 5398faecddbSChris Leech * Locking: must be called with the lp_mutex held 5408faecddbSChris Leech */ 5418faecddbSChris Leech void __fc_linkdown(struct fc_lport *lport) 5428faecddbSChris Leech { 5438faecddbSChris Leech if (lport->link_up) { 5448faecddbSChris Leech lport->link_up = 0; 5458faecddbSChris Leech fc_lport_enter_reset(lport); 5468faecddbSChris Leech lport->tt.fcp_cleanup(lport); 5478faecddbSChris Leech } 5488faecddbSChris Leech } 5498faecddbSChris Leech 5508faecddbSChris Leech /** 55134f42a07SRobert Love * fc_linkdown() - Handler for transport linkdown events 55242e9a92fSRobert Love * @lport: The lport whose link is down 55342e9a92fSRobert Love */ 55442e9a92fSRobert Love void fc_linkdown(struct fc_lport *lport) 55542e9a92fSRobert Love { 5567414705eSRobert Love printk(KERN_INFO "libfc: Link down on port (%6x)\n", 55742e9a92fSRobert Love fc_host_port_id(lport->host)); 55842e9a92fSRobert Love 5598faecddbSChris Leech mutex_lock(&lport->lp_mutex); 5608faecddbSChris Leech __fc_linkdown(lport); 56142e9a92fSRobert Love mutex_unlock(&lport->lp_mutex); 56242e9a92fSRobert Love } 56342e9a92fSRobert Love EXPORT_SYMBOL(fc_linkdown); 56442e9a92fSRobert Love 56542e9a92fSRobert Love /** 56634f42a07SRobert Love * fc_fabric_logoff() - Logout of the fabric 56742e9a92fSRobert Love * @lport: fc_lport pointer to logoff the fabric 56842e9a92fSRobert Love * 56942e9a92fSRobert Love * Return value: 57042e9a92fSRobert Love * 0 for success, -1 for failure 57134f42a07SRobert Love */ 57242e9a92fSRobert Love int fc_fabric_logoff(struct fc_lport *lport) 57342e9a92fSRobert Love { 57442e9a92fSRobert Love lport->tt.disc_stop_final(lport); 57542e9a92fSRobert Love mutex_lock(&lport->lp_mutex); 576a0fd2e49SAbhijeet Joglekar if (lport->dns_rp) 577a0fd2e49SAbhijeet Joglekar lport->tt.rport_logoff(lport->dns_rp); 578a0fd2e49SAbhijeet Joglekar mutex_unlock(&lport->lp_mutex); 579a0fd2e49SAbhijeet Joglekar lport->tt.rport_flush_queue(); 580a0fd2e49SAbhijeet Joglekar mutex_lock(&lport->lp_mutex); 58142e9a92fSRobert Love fc_lport_enter_logo(lport); 58242e9a92fSRobert Love mutex_unlock(&lport->lp_mutex); 583f7db2c15SSteve Ma cancel_delayed_work_sync(&lport->retry_work); 58442e9a92fSRobert Love return 0; 58542e9a92fSRobert Love } 58642e9a92fSRobert Love EXPORT_SYMBOL(fc_fabric_logoff); 58742e9a92fSRobert Love 58842e9a92fSRobert Love /** 58934f42a07SRobert Love * fc_lport_destroy() - unregister a fc_lport 59042e9a92fSRobert Love * @lport: fc_lport pointer to unregister 59142e9a92fSRobert Love * 59242e9a92fSRobert Love * Return value: 59342e9a92fSRobert Love * None 59442e9a92fSRobert Love * Note: 59542e9a92fSRobert Love * exit routine for fc_lport instance 59642e9a92fSRobert Love * clean-up all the allocated memory 59742e9a92fSRobert Love * and free up other system resources. 59842e9a92fSRobert Love * 59934f42a07SRobert Love */ 60042e9a92fSRobert Love int fc_lport_destroy(struct fc_lport *lport) 60142e9a92fSRobert Love { 602bbf15669SAbhijeet Joglekar mutex_lock(&lport->lp_mutex); 603b1d9fd55SJoe Eykholt lport->state = LPORT_ST_DISABLED; 604bbf15669SAbhijeet Joglekar lport->link_up = 0; 60542e9a92fSRobert Love lport->tt.frame_send = fc_frame_drop; 606bbf15669SAbhijeet Joglekar mutex_unlock(&lport->lp_mutex); 607bbf15669SAbhijeet Joglekar 60842e9a92fSRobert Love lport->tt.fcp_abort_io(lport); 609e9ba8b42SJoe Eykholt lport->tt.disc_stop_final(lport); 6101f6ff364SAbhijeet Joglekar lport->tt.exch_mgr_reset(lport, 0, 0); 61142e9a92fSRobert Love return 0; 61242e9a92fSRobert Love } 61342e9a92fSRobert Love EXPORT_SYMBOL(fc_lport_destroy); 61442e9a92fSRobert Love 61542e9a92fSRobert Love /** 61634f42a07SRobert Love * fc_set_mfs() - sets up the mfs for the corresponding fc_lport 61742e9a92fSRobert Love * @lport: fc_lport pointer to unregister 61842e9a92fSRobert Love * @mfs: the new mfs for fc_lport 61942e9a92fSRobert Love * 62042e9a92fSRobert Love * Set mfs for the given fc_lport to the new mfs. 62142e9a92fSRobert Love * 62242e9a92fSRobert Love * Return: 0 for success 62334f42a07SRobert Love */ 62442e9a92fSRobert Love int fc_set_mfs(struct fc_lport *lport, u32 mfs) 62542e9a92fSRobert Love { 62642e9a92fSRobert Love unsigned int old_mfs; 62742e9a92fSRobert Love int rc = -EINVAL; 62842e9a92fSRobert Love 62942e9a92fSRobert Love mutex_lock(&lport->lp_mutex); 63042e9a92fSRobert Love 63142e9a92fSRobert Love old_mfs = lport->mfs; 63242e9a92fSRobert Love 63342e9a92fSRobert Love if (mfs >= FC_MIN_MAX_FRAME) { 63442e9a92fSRobert Love mfs &= ~3; 63542e9a92fSRobert Love if (mfs > FC_MAX_FRAME) 63642e9a92fSRobert Love mfs = FC_MAX_FRAME; 63742e9a92fSRobert Love mfs -= sizeof(struct fc_frame_header); 63842e9a92fSRobert Love lport->mfs = mfs; 63942e9a92fSRobert Love rc = 0; 64042e9a92fSRobert Love } 64142e9a92fSRobert Love 64242e9a92fSRobert Love if (!rc && mfs < old_mfs) 64342e9a92fSRobert Love fc_lport_enter_reset(lport); 64442e9a92fSRobert Love 64542e9a92fSRobert Love mutex_unlock(&lport->lp_mutex); 64642e9a92fSRobert Love 64742e9a92fSRobert Love return rc; 64842e9a92fSRobert Love } 64942e9a92fSRobert Love EXPORT_SYMBOL(fc_set_mfs); 65042e9a92fSRobert Love 65142e9a92fSRobert Love /** 65234f42a07SRobert Love * fc_lport_disc_callback() - Callback for discovery events 65342e9a92fSRobert Love * @lport: FC local port 65442e9a92fSRobert Love * @event: The discovery event 65542e9a92fSRobert Love */ 65642e9a92fSRobert Love void fc_lport_disc_callback(struct fc_lport *lport, enum fc_disc_event event) 65742e9a92fSRobert Love { 65842e9a92fSRobert Love switch (event) { 65942e9a92fSRobert Love case DISC_EV_SUCCESS: 6607414705eSRobert Love FC_LPORT_DBG(lport, "Discovery succeeded\n"); 66142e9a92fSRobert Love break; 66242e9a92fSRobert Love case DISC_EV_FAILED: 6637414705eSRobert Love printk(KERN_ERR "libfc: Discovery failed for port (%6x)\n", 66442e9a92fSRobert Love fc_host_port_id(lport->host)); 66542e9a92fSRobert Love mutex_lock(&lport->lp_mutex); 66642e9a92fSRobert Love fc_lport_enter_reset(lport); 66742e9a92fSRobert Love mutex_unlock(&lport->lp_mutex); 66842e9a92fSRobert Love break; 66942e9a92fSRobert Love case DISC_EV_NONE: 67042e9a92fSRobert Love WARN_ON(1); 67142e9a92fSRobert Love break; 67242e9a92fSRobert Love } 67342e9a92fSRobert Love } 67442e9a92fSRobert Love 67542e9a92fSRobert Love /** 67634f42a07SRobert Love * fc_rport_enter_ready() - Enter the ready state and start discovery 67742e9a92fSRobert Love * @lport: Fibre Channel local port that is ready 67842e9a92fSRobert Love * 67942e9a92fSRobert Love * Locking Note: The lport lock is expected to be held before calling 68042e9a92fSRobert Love * this routine. 68142e9a92fSRobert Love */ 68242e9a92fSRobert Love static void fc_lport_enter_ready(struct fc_lport *lport) 68342e9a92fSRobert Love { 6847414705eSRobert Love FC_LPORT_DBG(lport, "Entered READY from state %s\n", 6857414705eSRobert Love fc_lport_state(lport)); 68642e9a92fSRobert Love 68742e9a92fSRobert Love fc_lport_state_enter(lport, LPORT_ST_READY); 6888faecddbSChris Leech if (lport->vport) 6898faecddbSChris Leech fc_vport_set_state(lport->vport, FC_VPORT_ACTIVE); 6908faecddbSChris Leech fc_vports_linkchange(lport); 69142e9a92fSRobert Love 69229d898e9SJoe Eykholt if (!lport->ptp_rp) 69342e9a92fSRobert Love lport->tt.disc_start(fc_lport_disc_callback, lport); 69442e9a92fSRobert Love } 69542e9a92fSRobert Love 69642e9a92fSRobert Love /** 69734f42a07SRobert Love * fc_lport_recv_flogi_req() - Receive a FLOGI request 69842e9a92fSRobert Love * @sp_in: The sequence the FLOGI is on 69942e9a92fSRobert Love * @rx_fp: The frame the FLOGI is in 70042e9a92fSRobert Love * @lport: The lport that recieved the request 70142e9a92fSRobert Love * 70242e9a92fSRobert Love * A received FLOGI request indicates a point-to-point connection. 70342e9a92fSRobert Love * Accept it with the common service parameters indicating our N port. 70442e9a92fSRobert Love * Set up to do a PLOGI if we have the higher-number WWPN. 70542e9a92fSRobert Love * 7061b69bc06SJoe Eykholt * Locking Note: The lport lock is expected to be held before calling 70742e9a92fSRobert Love * this function. 70842e9a92fSRobert Love */ 70942e9a92fSRobert Love static void fc_lport_recv_flogi_req(struct fc_seq *sp_in, 71042e9a92fSRobert Love struct fc_frame *rx_fp, 71142e9a92fSRobert Love struct fc_lport *lport) 71242e9a92fSRobert Love { 71342e9a92fSRobert Love struct fc_frame *fp; 71442e9a92fSRobert Love struct fc_frame_header *fh; 71542e9a92fSRobert Love struct fc_seq *sp; 71642e9a92fSRobert Love struct fc_exch *ep; 71742e9a92fSRobert Love struct fc_els_flogi *flp; 71842e9a92fSRobert Love struct fc_els_flogi *new_flp; 71942e9a92fSRobert Love u64 remote_wwpn; 72042e9a92fSRobert Love u32 remote_fid; 72142e9a92fSRobert Love u32 local_fid; 72242e9a92fSRobert Love u32 f_ctl; 72342e9a92fSRobert Love 7247414705eSRobert Love FC_LPORT_DBG(lport, "Received FLOGI request while in state %s\n", 72542e9a92fSRobert Love fc_lport_state(lport)); 72642e9a92fSRobert Love 72742e9a92fSRobert Love fh = fc_frame_header_get(rx_fp); 72842e9a92fSRobert Love remote_fid = ntoh24(fh->fh_s_id); 72942e9a92fSRobert Love flp = fc_frame_payload_get(rx_fp, sizeof(*flp)); 73042e9a92fSRobert Love if (!flp) 73142e9a92fSRobert Love goto out; 73242e9a92fSRobert Love remote_wwpn = get_unaligned_be64(&flp->fl_wwpn); 73342e9a92fSRobert Love if (remote_wwpn == lport->wwpn) { 7347414705eSRobert Love printk(KERN_WARNING "libfc: Received FLOGI from port " 7357414705eSRobert Love "with same WWPN %llx\n", remote_wwpn); 73642e9a92fSRobert Love goto out; 73742e9a92fSRobert Love } 7387414705eSRobert Love FC_LPORT_DBG(lport, "FLOGI from port WWPN %llx\n", remote_wwpn); 73942e9a92fSRobert Love 74042e9a92fSRobert Love /* 74142e9a92fSRobert Love * XXX what is the right thing to do for FIDs? 74242e9a92fSRobert Love * The originator might expect our S_ID to be 0xfffffe. 74342e9a92fSRobert Love * But if so, both of us could end up with the same FID. 74442e9a92fSRobert Love */ 74542e9a92fSRobert Love local_fid = FC_LOCAL_PTP_FID_LO; 74642e9a92fSRobert Love if (remote_wwpn < lport->wwpn) { 74742e9a92fSRobert Love local_fid = FC_LOCAL_PTP_FID_HI; 74842e9a92fSRobert Love if (!remote_fid || remote_fid == local_fid) 74942e9a92fSRobert Love remote_fid = FC_LOCAL_PTP_FID_LO; 75042e9a92fSRobert Love } else if (!remote_fid) { 75142e9a92fSRobert Love remote_fid = FC_LOCAL_PTP_FID_HI; 75242e9a92fSRobert Love } 75342e9a92fSRobert Love 75442e9a92fSRobert Love fc_host_port_id(lport->host) = local_fid; 75542e9a92fSRobert Love 75642e9a92fSRobert Love fp = fc_frame_alloc(lport, sizeof(*flp)); 75742e9a92fSRobert Love if (fp) { 75842e9a92fSRobert Love sp = lport->tt.seq_start_next(fr_seq(rx_fp)); 75942e9a92fSRobert Love new_flp = fc_frame_payload_get(fp, sizeof(*flp)); 76042e9a92fSRobert Love fc_lport_flogi_fill(lport, new_flp, ELS_FLOGI); 76142e9a92fSRobert Love new_flp->fl_cmd = (u8) ELS_LS_ACC; 76242e9a92fSRobert Love 76342e9a92fSRobert Love /* 76442e9a92fSRobert Love * Send the response. If this fails, the originator should 76542e9a92fSRobert Love * repeat the sequence. 76642e9a92fSRobert Love */ 76742e9a92fSRobert Love f_ctl = FC_FC_EX_CTX | FC_FC_LAST_SEQ | FC_FC_END_SEQ; 76842e9a92fSRobert Love ep = fc_seq_exch(sp); 76942e9a92fSRobert Love fc_fill_fc_hdr(fp, FC_RCTL_ELS_REP, ep->did, ep->sid, 77042e9a92fSRobert Love FC_TYPE_ELS, f_ctl, 0); 77142e9a92fSRobert Love lport->tt.seq_send(lport, sp, fp); 77242e9a92fSRobert Love 77342e9a92fSRobert Love } else { 77442e9a92fSRobert Love fc_lport_error(lport, fp); 77542e9a92fSRobert Love } 77642e9a92fSRobert Love fc_lport_ptp_setup(lport, remote_fid, remote_wwpn, 77742e9a92fSRobert Love get_unaligned_be64(&flp->fl_wwnn)); 77842e9a92fSRobert Love 77942e9a92fSRobert Love out: 78042e9a92fSRobert Love sp = fr_seq(rx_fp); 78142e9a92fSRobert Love fc_frame_free(rx_fp); 78242e9a92fSRobert Love } 78342e9a92fSRobert Love 78442e9a92fSRobert Love /** 78534f42a07SRobert Love * fc_lport_recv_req() - The generic lport request handler 78642e9a92fSRobert Love * @lport: The lport that received the request 78742e9a92fSRobert Love * @sp: The sequence the request is on 78842e9a92fSRobert Love * @fp: The frame the request is in 78942e9a92fSRobert Love * 79042e9a92fSRobert Love * This function will see if the lport handles the request or 79142e9a92fSRobert Love * if an rport should handle the request. 79242e9a92fSRobert Love * 79342e9a92fSRobert Love * Locking Note: This function should not be called with the lport 79442e9a92fSRobert Love * lock held becuase it will grab the lock. 79542e9a92fSRobert Love */ 79642e9a92fSRobert Love static void fc_lport_recv_req(struct fc_lport *lport, struct fc_seq *sp, 79742e9a92fSRobert Love struct fc_frame *fp) 79842e9a92fSRobert Love { 79942e9a92fSRobert Love struct fc_frame_header *fh = fc_frame_header_get(fp); 80042e9a92fSRobert Love void (*recv) (struct fc_seq *, struct fc_frame *, struct fc_lport *); 80142e9a92fSRobert Love 80242e9a92fSRobert Love mutex_lock(&lport->lp_mutex); 80342e9a92fSRobert Love 80442e9a92fSRobert Love /* 80542e9a92fSRobert Love * Handle special ELS cases like FLOGI, LOGO, and 80642e9a92fSRobert Love * RSCN here. These don't require a session. 80742e9a92fSRobert Love * Even if we had a session, it might not be ready. 80842e9a92fSRobert Love */ 809e9ba8b42SJoe Eykholt if (!lport->link_up) 810e9ba8b42SJoe Eykholt fc_frame_free(fp); 811e9ba8b42SJoe Eykholt else if (fh->fh_type == FC_TYPE_ELS && 812e9ba8b42SJoe Eykholt fh->fh_r_ctl == FC_RCTL_ELS_REQ) { 81342e9a92fSRobert Love /* 81442e9a92fSRobert Love * Check opcode. 81542e9a92fSRobert Love */ 816131203a1SJoe Eykholt recv = lport->tt.rport_recv_req; 81742e9a92fSRobert Love switch (fc_frame_payload_op(fp)) { 81842e9a92fSRobert Love case ELS_FLOGI: 81942e9a92fSRobert Love recv = fc_lport_recv_flogi_req; 82042e9a92fSRobert Love break; 82142e9a92fSRobert Love case ELS_LOGO: 82242e9a92fSRobert Love fh = fc_frame_header_get(fp); 82342e9a92fSRobert Love if (ntoh24(fh->fh_s_id) == FC_FID_FLOGI) 82442e9a92fSRobert Love recv = fc_lport_recv_logo_req; 82542e9a92fSRobert Love break; 82642e9a92fSRobert Love case ELS_RSCN: 82742e9a92fSRobert Love recv = lport->tt.disc_recv_req; 82842e9a92fSRobert Love break; 82942e9a92fSRobert Love case ELS_ECHO: 83042e9a92fSRobert Love recv = fc_lport_recv_echo_req; 83142e9a92fSRobert Love break; 83242e9a92fSRobert Love case ELS_RLIR: 83342e9a92fSRobert Love recv = fc_lport_recv_rlir_req; 83442e9a92fSRobert Love break; 83542e9a92fSRobert Love case ELS_RNID: 83642e9a92fSRobert Love recv = fc_lport_recv_rnid_req; 83742e9a92fSRobert Love break; 83842e9a92fSRobert Love } 83942e9a92fSRobert Love 84042e9a92fSRobert Love recv(sp, fp, lport); 84142e9a92fSRobert Love } else { 8427414705eSRobert Love FC_LPORT_DBG(lport, "dropping invalid frame (eof %x)\n", 8437414705eSRobert Love fr_eof(fp)); 84442e9a92fSRobert Love fc_frame_free(fp); 84542e9a92fSRobert Love } 84642e9a92fSRobert Love mutex_unlock(&lport->lp_mutex); 84742e9a92fSRobert Love 84842e9a92fSRobert Love /* 84942e9a92fSRobert Love * The common exch_done for all request may not be good 85042e9a92fSRobert Love * if any request requires longer hold on exhange. XXX 85142e9a92fSRobert Love */ 85242e9a92fSRobert Love lport->tt.exch_done(sp); 85342e9a92fSRobert Love } 85442e9a92fSRobert Love 85542e9a92fSRobert Love /** 85634f42a07SRobert Love * fc_lport_reset() - Reset an lport 85742e9a92fSRobert Love * @lport: The lport which should be reset 85842e9a92fSRobert Love * 85942e9a92fSRobert Love * Locking Note: This functions should not be called with the 86042e9a92fSRobert Love * lport lock held. 86142e9a92fSRobert Love */ 86242e9a92fSRobert Love int fc_lport_reset(struct fc_lport *lport) 86342e9a92fSRobert Love { 864f7db2c15SSteve Ma cancel_delayed_work_sync(&lport->retry_work); 86542e9a92fSRobert Love mutex_lock(&lport->lp_mutex); 86642e9a92fSRobert Love fc_lport_enter_reset(lport); 86742e9a92fSRobert Love mutex_unlock(&lport->lp_mutex); 86842e9a92fSRobert Love return 0; 86942e9a92fSRobert Love } 87042e9a92fSRobert Love EXPORT_SYMBOL(fc_lport_reset); 87142e9a92fSRobert Love 87242e9a92fSRobert Love /** 8731190d925SJoe Eykholt * fc_lport_reset_locked() - Reset the local port 87442e9a92fSRobert Love * @lport: Fibre Channel local port to be reset 87542e9a92fSRobert Love * 87642e9a92fSRobert Love * Locking Note: The lport lock is expected to be held before calling 87742e9a92fSRobert Love * this routine. 87842e9a92fSRobert Love */ 8791190d925SJoe Eykholt static void fc_lport_reset_locked(struct fc_lport *lport) 88042e9a92fSRobert Love { 88142e9a92fSRobert Love if (lport->dns_rp) 88242e9a92fSRobert Love lport->tt.rport_logoff(lport->dns_rp); 88342e9a92fSRobert Love 88442e9a92fSRobert Love lport->ptp_rp = NULL; 88542e9a92fSRobert Love 88642e9a92fSRobert Love lport->tt.disc_stop(lport); 88742e9a92fSRobert Love 8881f6ff364SAbhijeet Joglekar lport->tt.exch_mgr_reset(lport, 0, 0); 88942e9a92fSRobert Love fc_host_fabric_name(lport->host) = 0; 89042e9a92fSRobert Love fc_host_port_id(lport->host) = 0; 8911190d925SJoe Eykholt } 89242e9a92fSRobert Love 8931190d925SJoe Eykholt /** 8941190d925SJoe Eykholt * fc_lport_enter_reset() - Reset the local port 8951190d925SJoe Eykholt * @lport: Fibre Channel local port to be reset 8961190d925SJoe Eykholt * 8971190d925SJoe Eykholt * Locking Note: The lport lock is expected to be held before calling 8981190d925SJoe Eykholt * this routine. 8991190d925SJoe Eykholt */ 9001190d925SJoe Eykholt static void fc_lport_enter_reset(struct fc_lport *lport) 9011190d925SJoe Eykholt { 9021190d925SJoe Eykholt FC_LPORT_DBG(lport, "Entered RESET state from %s state\n", 9031190d925SJoe Eykholt fc_lport_state(lport)); 9041190d925SJoe Eykholt 9058faecddbSChris Leech if (lport->vport) { 9068faecddbSChris Leech if (lport->link_up) 9078faecddbSChris Leech fc_vport_set_state(lport->vport, FC_VPORT_INITIALIZING); 9088faecddbSChris Leech else 9098faecddbSChris Leech fc_vport_set_state(lport->vport, FC_VPORT_LINKDOWN); 9108faecddbSChris Leech } 9111190d925SJoe Eykholt fc_lport_state_enter(lport, LPORT_ST_RESET); 9128faecddbSChris Leech fc_vports_linkchange(lport); 9131190d925SJoe Eykholt fc_lport_reset_locked(lport); 914bc0e17f6SVasu Dev if (lport->link_up) 91542e9a92fSRobert Love fc_lport_enter_flogi(lport); 91642e9a92fSRobert Love } 91742e9a92fSRobert Love 91842e9a92fSRobert Love /** 9191190d925SJoe Eykholt * fc_lport_enter_disabled() - disable the local port 9201190d925SJoe Eykholt * @lport: Fibre Channel local port to be reset 9211190d925SJoe Eykholt * 9221190d925SJoe Eykholt * Locking Note: The lport lock is expected to be held before calling 9231190d925SJoe Eykholt * this routine. 9241190d925SJoe Eykholt */ 9251190d925SJoe Eykholt static void fc_lport_enter_disabled(struct fc_lport *lport) 9261190d925SJoe Eykholt { 9271190d925SJoe Eykholt FC_LPORT_DBG(lport, "Entered disabled state from %s state\n", 9281190d925SJoe Eykholt fc_lport_state(lport)); 9291190d925SJoe Eykholt 9301190d925SJoe Eykholt fc_lport_state_enter(lport, LPORT_ST_DISABLED); 9318faecddbSChris Leech fc_vports_linkchange(lport); 9321190d925SJoe Eykholt fc_lport_reset_locked(lport); 9331190d925SJoe Eykholt } 9341190d925SJoe Eykholt 9351190d925SJoe Eykholt /** 93634f42a07SRobert Love * fc_lport_error() - Handler for any errors 93742e9a92fSRobert Love * @lport: The fc_lport object 93842e9a92fSRobert Love * @fp: The frame pointer 93942e9a92fSRobert Love * 94042e9a92fSRobert Love * If the error was caused by a resource allocation failure 94142e9a92fSRobert Love * then wait for half a second and retry, otherwise retry 94242e9a92fSRobert Love * after the e_d_tov time. 94342e9a92fSRobert Love */ 94442e9a92fSRobert Love static void fc_lport_error(struct fc_lport *lport, struct fc_frame *fp) 94542e9a92fSRobert Love { 94642e9a92fSRobert Love unsigned long delay = 0; 9477414705eSRobert Love FC_LPORT_DBG(lport, "Error %ld in state %s, retries %d\n", 94842e9a92fSRobert Love PTR_ERR(fp), fc_lport_state(lport), 94942e9a92fSRobert Love lport->retry_count); 95042e9a92fSRobert Love 95142e9a92fSRobert Love if (!fp || PTR_ERR(fp) == -FC_EX_TIMEOUT) { 95242e9a92fSRobert Love /* 95342e9a92fSRobert Love * Memory allocation failure, or the exchange timed out. 95442e9a92fSRobert Love * Retry after delay 95542e9a92fSRobert Love */ 95642e9a92fSRobert Love if (lport->retry_count < lport->max_retry_count) { 95742e9a92fSRobert Love lport->retry_count++; 95842e9a92fSRobert Love if (!fp) 95942e9a92fSRobert Love delay = msecs_to_jiffies(500); 96042e9a92fSRobert Love else 96142e9a92fSRobert Love delay = msecs_to_jiffies(lport->e_d_tov); 96242e9a92fSRobert Love 96342e9a92fSRobert Love schedule_delayed_work(&lport->retry_work, delay); 96442e9a92fSRobert Love } else { 96542e9a92fSRobert Love switch (lport->state) { 966b1d9fd55SJoe Eykholt case LPORT_ST_DISABLED: 96742e9a92fSRobert Love case LPORT_ST_READY: 96842e9a92fSRobert Love case LPORT_ST_RESET: 969c9c7bd7aSChris Leech case LPORT_ST_RNN_ID: 9705baa17c3SChris Leech case LPORT_ST_RSNN_NN: 971c9866a54SChris Leech case LPORT_ST_RSPN_ID: 97242e9a92fSRobert Love case LPORT_ST_RFT_ID: 97342e9a92fSRobert Love case LPORT_ST_SCR: 97442e9a92fSRobert Love case LPORT_ST_DNS: 97542e9a92fSRobert Love case LPORT_ST_FLOGI: 97642e9a92fSRobert Love case LPORT_ST_LOGO: 97742e9a92fSRobert Love fc_lport_enter_reset(lport); 97842e9a92fSRobert Love break; 97942e9a92fSRobert Love } 98042e9a92fSRobert Love } 98142e9a92fSRobert Love } 98242e9a92fSRobert Love } 98342e9a92fSRobert Love 98442e9a92fSRobert Love /** 9857cccc157SChris Leech * fc_lport_ns_resp() - Handle response to a name server 9867cccc157SChris Leech * registration exchange 9877cccc157SChris Leech * @sp: current sequence in exchange 98842e9a92fSRobert Love * @fp: response frame 98942e9a92fSRobert Love * @lp_arg: Fibre Channel host port instance 99042e9a92fSRobert Love * 99142e9a92fSRobert Love * Locking Note: This function will be called without the lport lock 99242e9a92fSRobert Love * held, but it will lock, call an _enter_* function or fc_lport_error 99342e9a92fSRobert Love * and then unlock the lport. 99442e9a92fSRobert Love */ 9957cccc157SChris Leech static void fc_lport_ns_resp(struct fc_seq *sp, struct fc_frame *fp, 99642e9a92fSRobert Love void *lp_arg) 99742e9a92fSRobert Love { 99842e9a92fSRobert Love struct fc_lport *lport = lp_arg; 99942e9a92fSRobert Love struct fc_frame_header *fh; 100042e9a92fSRobert Love struct fc_ct_hdr *ct; 100142e9a92fSRobert Love 10027cccc157SChris Leech FC_LPORT_DBG(lport, "Received a ns %s\n", fc_els_resp_type(fp)); 1003f657d299SJoe Eykholt 100442e9a92fSRobert Love if (fp == ERR_PTR(-FC_EX_CLOSED)) 100542e9a92fSRobert Love return; 100642e9a92fSRobert Love 100742e9a92fSRobert Love mutex_lock(&lport->lp_mutex); 100842e9a92fSRobert Love 10097cccc157SChris Leech if (lport->state < LPORT_ST_RNN_ID || lport->state > LPORT_ST_RFT_ID) { 10107cccc157SChris Leech FC_LPORT_DBG(lport, "Received a name server response, " 10117cccc157SChris Leech "but in state %s\n", fc_lport_state(lport)); 101276f6804eSAbhijeet Joglekar if (IS_ERR(fp)) 101376f6804eSAbhijeet Joglekar goto err; 101442e9a92fSRobert Love goto out; 101542e9a92fSRobert Love } 101642e9a92fSRobert Love 101776f6804eSAbhijeet Joglekar if (IS_ERR(fp)) { 101876f6804eSAbhijeet Joglekar fc_lport_error(lport, fp); 101976f6804eSAbhijeet Joglekar goto err; 102076f6804eSAbhijeet Joglekar } 102176f6804eSAbhijeet Joglekar 102242e9a92fSRobert Love fh = fc_frame_header_get(fp); 102342e9a92fSRobert Love ct = fc_frame_payload_get(fp, sizeof(*ct)); 102442e9a92fSRobert Love 102542e9a92fSRobert Love if (fh && ct && fh->fh_type == FC_TYPE_CT && 102642e9a92fSRobert Love ct->ct_fs_type == FC_FST_DIR && 102742e9a92fSRobert Love ct->ct_fs_subtype == FC_NS_SUBTYPE && 102842e9a92fSRobert Love ntohs(ct->ct_cmd) == FC_FS_ACC) 10297cccc157SChris Leech switch (lport->state) { 10307cccc157SChris Leech case LPORT_ST_RNN_ID: 1031c914f7d1SChris Leech fc_lport_enter_ns(lport, LPORT_ST_RSNN_NN); 10327cccc157SChris Leech break; 10337cccc157SChris Leech case LPORT_ST_RSNN_NN: 1034c914f7d1SChris Leech fc_lport_enter_ns(lport, LPORT_ST_RSPN_ID); 10357cccc157SChris Leech break; 10367cccc157SChris Leech case LPORT_ST_RSPN_ID: 1037c914f7d1SChris Leech fc_lport_enter_ns(lport, LPORT_ST_RFT_ID); 10387cccc157SChris Leech break; 10397cccc157SChris Leech case LPORT_ST_RFT_ID: 10407cccc157SChris Leech fc_lport_enter_scr(lport); 10417cccc157SChris Leech break; 10427cccc157SChris Leech default: 10437cccc157SChris Leech /* should have already been caught by state checks */ 10447cccc157SChris Leech break; 10457cccc157SChris Leech } 1046c9c7bd7aSChris Leech else 1047c9c7bd7aSChris Leech fc_lport_error(lport, fp); 1048c9c7bd7aSChris Leech out: 1049c9c7bd7aSChris Leech fc_frame_free(fp); 1050c9c7bd7aSChris Leech err: 1051c9c7bd7aSChris Leech mutex_unlock(&lport->lp_mutex); 1052c9c7bd7aSChris Leech } 1053c9c7bd7aSChris Leech 1054c9c7bd7aSChris Leech /** 105534f42a07SRobert Love * fc_lport_scr_resp() - Handle response to State Change Register (SCR) request 105642e9a92fSRobert Love * @sp: current sequence in SCR exchange 105742e9a92fSRobert Love * @fp: response frame 105842e9a92fSRobert Love * @lp_arg: Fibre Channel lport port instance that sent the registration request 105942e9a92fSRobert Love * 106042e9a92fSRobert Love * Locking Note: This function will be called without the lport lock 106142e9a92fSRobert Love * held, but it will lock, call an _enter_* function or fc_lport_error 106242e9a92fSRobert Love * and then unlock the lport. 106342e9a92fSRobert Love */ 106442e9a92fSRobert Love static void fc_lport_scr_resp(struct fc_seq *sp, struct fc_frame *fp, 106542e9a92fSRobert Love void *lp_arg) 106642e9a92fSRobert Love { 106742e9a92fSRobert Love struct fc_lport *lport = lp_arg; 106842e9a92fSRobert Love u8 op; 106942e9a92fSRobert Love 1070f657d299SJoe Eykholt FC_LPORT_DBG(lport, "Received a SCR %s\n", fc_els_resp_type(fp)); 1071f657d299SJoe Eykholt 107242e9a92fSRobert Love if (fp == ERR_PTR(-FC_EX_CLOSED)) 107342e9a92fSRobert Love return; 107442e9a92fSRobert Love 107542e9a92fSRobert Love mutex_lock(&lport->lp_mutex); 107642e9a92fSRobert Love 107742e9a92fSRobert Love if (lport->state != LPORT_ST_SCR) { 10787414705eSRobert Love FC_LPORT_DBG(lport, "Received a SCR response, but in state " 10797414705eSRobert Love "%s\n", fc_lport_state(lport)); 108076f6804eSAbhijeet Joglekar if (IS_ERR(fp)) 108176f6804eSAbhijeet Joglekar goto err; 108242e9a92fSRobert Love goto out; 108342e9a92fSRobert Love } 108442e9a92fSRobert Love 108576f6804eSAbhijeet Joglekar if (IS_ERR(fp)) { 108676f6804eSAbhijeet Joglekar fc_lport_error(lport, fp); 108776f6804eSAbhijeet Joglekar goto err; 108876f6804eSAbhijeet Joglekar } 108976f6804eSAbhijeet Joglekar 109042e9a92fSRobert Love op = fc_frame_payload_op(fp); 109142e9a92fSRobert Love if (op == ELS_LS_ACC) 109242e9a92fSRobert Love fc_lport_enter_ready(lport); 109342e9a92fSRobert Love else 109442e9a92fSRobert Love fc_lport_error(lport, fp); 109542e9a92fSRobert Love 109642e9a92fSRobert Love out: 109742e9a92fSRobert Love fc_frame_free(fp); 109842e9a92fSRobert Love err: 109942e9a92fSRobert Love mutex_unlock(&lport->lp_mutex); 110042e9a92fSRobert Love } 110142e9a92fSRobert Love 110242e9a92fSRobert Love /** 110334f42a07SRobert Love * fc_lport_enter_scr() - Send a State Change Register (SCR) request 110442e9a92fSRobert Love * @lport: Fibre Channel local port to register for state changes 110542e9a92fSRobert Love * 110642e9a92fSRobert Love * Locking Note: The lport lock is expected to be held before calling 110742e9a92fSRobert Love * this routine. 110842e9a92fSRobert Love */ 110942e9a92fSRobert Love static void fc_lport_enter_scr(struct fc_lport *lport) 111042e9a92fSRobert Love { 111142e9a92fSRobert Love struct fc_frame *fp; 111242e9a92fSRobert Love 11137414705eSRobert Love FC_LPORT_DBG(lport, "Entered SCR state from %s state\n", 11147414705eSRobert Love fc_lport_state(lport)); 111542e9a92fSRobert Love 111642e9a92fSRobert Love fc_lport_state_enter(lport, LPORT_ST_SCR); 111742e9a92fSRobert Love 111842e9a92fSRobert Love fp = fc_frame_alloc(lport, sizeof(struct fc_els_scr)); 111942e9a92fSRobert Love if (!fp) { 112042e9a92fSRobert Love fc_lport_error(lport, fp); 112142e9a92fSRobert Love return; 112242e9a92fSRobert Love } 112342e9a92fSRobert Love 1124a46f327aSJoe Eykholt if (!lport->tt.elsct_send(lport, FC_FID_FCTRL, fp, ELS_SCR, 112542e9a92fSRobert Love fc_lport_scr_resp, lport, lport->e_d_tov)) 11268f550f93SChris Leech fc_lport_error(lport, NULL); 112742e9a92fSRobert Love } 112842e9a92fSRobert Love 112942e9a92fSRobert Love /** 1130c914f7d1SChris Leech * fc_lport_enter_ns() - register some object with the name server 113142e9a92fSRobert Love * @lport: Fibre Channel local port to register 113242e9a92fSRobert Love * 113342e9a92fSRobert Love * Locking Note: The lport lock is expected to be held before calling 113442e9a92fSRobert Love * this routine. 113542e9a92fSRobert Love */ 1136c914f7d1SChris Leech static void fc_lport_enter_ns(struct fc_lport *lport, enum fc_lport_state state) 113742e9a92fSRobert Love { 113842e9a92fSRobert Love struct fc_frame *fp; 1139c914f7d1SChris Leech enum fc_ns_req cmd; 1140c914f7d1SChris Leech int size = sizeof(struct fc_ct_hdr); 1141c914f7d1SChris Leech size_t len; 114242e9a92fSRobert Love 1143c914f7d1SChris Leech FC_LPORT_DBG(lport, "Entered %s state from %s state\n", 1144c914f7d1SChris Leech fc_lport_state_names[state], 11457414705eSRobert Love fc_lport_state(lport)); 114642e9a92fSRobert Love 1147c914f7d1SChris Leech fc_lport_state_enter(lport, state); 114842e9a92fSRobert Love 1149c914f7d1SChris Leech switch (state) { 1150c914f7d1SChris Leech case LPORT_ST_RNN_ID: 1151c914f7d1SChris Leech cmd = FC_NS_RNN_ID; 1152c914f7d1SChris Leech size += sizeof(struct fc_ns_rn_id); 115342e9a92fSRobert Love break; 1154c914f7d1SChris Leech case LPORT_ST_RSNN_NN: 1155c9866a54SChris Leech len = strnlen(fc_host_symbolic_name(lport->host), 255); 1156c914f7d1SChris Leech /* if there is no symbolic name, skip to RFT_ID */ 1157c914f7d1SChris Leech if (!len) 1158c914f7d1SChris Leech return fc_lport_enter_ns(lport, LPORT_ST_RFT_ID); 1159c914f7d1SChris Leech cmd = FC_NS_RSNN_NN; 1160c914f7d1SChris Leech size += sizeof(struct fc_ns_rsnn) + len; 1161c914f7d1SChris Leech break; 1162c914f7d1SChris Leech case LPORT_ST_RSPN_ID: 11635baa17c3SChris Leech len = strnlen(fc_host_symbolic_name(lport->host), 255); 1164c914f7d1SChris Leech /* if there is no symbolic name, skip to RFT_ID */ 1165c914f7d1SChris Leech if (!len) 1166c914f7d1SChris Leech return fc_lport_enter_ns(lport, LPORT_ST_RFT_ID); 1167c914f7d1SChris Leech cmd = FC_NS_RSPN_ID; 1168c914f7d1SChris Leech size += sizeof(struct fc_ns_rspn) + len; 1169c914f7d1SChris Leech break; 1170c914f7d1SChris Leech case LPORT_ST_RFT_ID: 1171c914f7d1SChris Leech cmd = FC_NS_RFT_ID; 1172c914f7d1SChris Leech size += sizeof(struct fc_ns_rft); 1173c914f7d1SChris Leech break; 1174c914f7d1SChris Leech default: 1175c914f7d1SChris Leech fc_lport_error(lport, NULL); 1176c914f7d1SChris Leech return; 1177c914f7d1SChris Leech } 1178c914f7d1SChris Leech 1179c914f7d1SChris Leech fp = fc_frame_alloc(lport, size); 11805baa17c3SChris Leech if (!fp) { 11815baa17c3SChris Leech fc_lport_error(lport, fp); 11825baa17c3SChris Leech return; 11835baa17c3SChris Leech } 11845baa17c3SChris Leech 1185c914f7d1SChris Leech if (!lport->tt.elsct_send(lport, FC_FID_DIR_SERV, fp, cmd, 11867cccc157SChris Leech fc_lport_ns_resp, 1187c9c7bd7aSChris Leech lport, lport->e_d_tov)) 1188c9c7bd7aSChris Leech fc_lport_error(lport, fp); 1189c9c7bd7aSChris Leech } 1190c9c7bd7aSChris Leech 119142e9a92fSRobert Love static struct fc_rport_operations fc_lport_rport_ops = { 119242e9a92fSRobert Love .event_callback = fc_lport_rport_callback, 119342e9a92fSRobert Love }; 119442e9a92fSRobert Love 119542e9a92fSRobert Love /** 119634f42a07SRobert Love * fc_rport_enter_dns() - Create a rport to the name server 119742e9a92fSRobert Love * @lport: Fibre Channel local port requesting a rport for the name server 119842e9a92fSRobert Love * 119942e9a92fSRobert Love * Locking Note: The lport lock is expected to be held before calling 120042e9a92fSRobert Love * this routine. 120142e9a92fSRobert Love */ 120242e9a92fSRobert Love static void fc_lport_enter_dns(struct fc_lport *lport) 120342e9a92fSRobert Love { 1204ab28f1fdSJoe Eykholt struct fc_rport_priv *rdata; 120542e9a92fSRobert Love 12067414705eSRobert Love FC_LPORT_DBG(lport, "Entered DNS state from %s state\n", 12077414705eSRobert Love fc_lport_state(lport)); 120842e9a92fSRobert Love 120942e9a92fSRobert Love fc_lport_state_enter(lport, LPORT_ST_DNS); 121042e9a92fSRobert Love 121148f00902SJoe Eykholt mutex_lock(&lport->disc.disc_mutex); 12129737e6a7SRobert Love rdata = lport->tt.rport_create(lport, FC_FID_DIR_SERV); 121348f00902SJoe Eykholt mutex_unlock(&lport->disc.disc_mutex); 12149fb9d328SJoe Eykholt if (!rdata) 121542e9a92fSRobert Love goto err; 121642e9a92fSRobert Love 121742e9a92fSRobert Love rdata->ops = &fc_lport_rport_ops; 12189fb9d328SJoe Eykholt lport->tt.rport_login(rdata); 121942e9a92fSRobert Love return; 122042e9a92fSRobert Love 122142e9a92fSRobert Love err: 122242e9a92fSRobert Love fc_lport_error(lport, NULL); 122342e9a92fSRobert Love } 122442e9a92fSRobert Love 122542e9a92fSRobert Love /** 122634f42a07SRobert Love * fc_lport_timeout() - Handler for the retry_work timer. 122742e9a92fSRobert Love * @work: The work struct of the fc_lport 122842e9a92fSRobert Love */ 122942e9a92fSRobert Love static void fc_lport_timeout(struct work_struct *work) 123042e9a92fSRobert Love { 123142e9a92fSRobert Love struct fc_lport *lport = 123242e9a92fSRobert Love container_of(work, struct fc_lport, 123342e9a92fSRobert Love retry_work.work); 123442e9a92fSRobert Love 123542e9a92fSRobert Love mutex_lock(&lport->lp_mutex); 123642e9a92fSRobert Love 123742e9a92fSRobert Love switch (lport->state) { 1238b1d9fd55SJoe Eykholt case LPORT_ST_DISABLED: 123942e9a92fSRobert Love WARN_ON(1); 124042e9a92fSRobert Love break; 124122655ac2SJoe Eykholt case LPORT_ST_READY: 124222655ac2SJoe Eykholt WARN_ON(1); 124322655ac2SJoe Eykholt break; 124422655ac2SJoe Eykholt case LPORT_ST_RESET: 124522655ac2SJoe Eykholt break; 124642e9a92fSRobert Love case LPORT_ST_FLOGI: 124742e9a92fSRobert Love fc_lport_enter_flogi(lport); 124842e9a92fSRobert Love break; 124942e9a92fSRobert Love case LPORT_ST_DNS: 125042e9a92fSRobert Love fc_lport_enter_dns(lport); 125142e9a92fSRobert Love break; 1252c9c7bd7aSChris Leech case LPORT_ST_RNN_ID: 12535baa17c3SChris Leech case LPORT_ST_RSNN_NN: 1254c9866a54SChris Leech case LPORT_ST_RSPN_ID: 125542e9a92fSRobert Love case LPORT_ST_RFT_ID: 1256c914f7d1SChris Leech fc_lport_enter_ns(lport, lport->state); 125742e9a92fSRobert Love break; 125842e9a92fSRobert Love case LPORT_ST_SCR: 125942e9a92fSRobert Love fc_lport_enter_scr(lport); 126042e9a92fSRobert Love break; 126142e9a92fSRobert Love case LPORT_ST_LOGO: 126242e9a92fSRobert Love fc_lport_enter_logo(lport); 126342e9a92fSRobert Love break; 126442e9a92fSRobert Love } 126542e9a92fSRobert Love 126642e9a92fSRobert Love mutex_unlock(&lport->lp_mutex); 126742e9a92fSRobert Love } 126842e9a92fSRobert Love 126942e9a92fSRobert Love /** 127034f42a07SRobert Love * fc_lport_logo_resp() - Handle response to LOGO request 127142e9a92fSRobert Love * @sp: current sequence in LOGO exchange 127242e9a92fSRobert Love * @fp: response frame 127342e9a92fSRobert Love * @lp_arg: Fibre Channel lport port instance that sent the LOGO request 127442e9a92fSRobert Love * 127542e9a92fSRobert Love * Locking Note: This function will be called without the lport lock 127642e9a92fSRobert Love * held, but it will lock, call an _enter_* function or fc_lport_error 127742e9a92fSRobert Love * and then unlock the lport. 127842e9a92fSRobert Love */ 127911b56188SChris Leech void fc_lport_logo_resp(struct fc_seq *sp, struct fc_frame *fp, 128042e9a92fSRobert Love void *lp_arg) 128142e9a92fSRobert Love { 128242e9a92fSRobert Love struct fc_lport *lport = lp_arg; 128342e9a92fSRobert Love u8 op; 128442e9a92fSRobert Love 1285f657d299SJoe Eykholt FC_LPORT_DBG(lport, "Received a LOGO %s\n", fc_els_resp_type(fp)); 1286f657d299SJoe Eykholt 128742e9a92fSRobert Love if (fp == ERR_PTR(-FC_EX_CLOSED)) 128842e9a92fSRobert Love return; 128942e9a92fSRobert Love 129042e9a92fSRobert Love mutex_lock(&lport->lp_mutex); 129142e9a92fSRobert Love 129242e9a92fSRobert Love if (lport->state != LPORT_ST_LOGO) { 12937414705eSRobert Love FC_LPORT_DBG(lport, "Received a LOGO response, but in state " 12947414705eSRobert Love "%s\n", fc_lport_state(lport)); 129576f6804eSAbhijeet Joglekar if (IS_ERR(fp)) 129676f6804eSAbhijeet Joglekar goto err; 129742e9a92fSRobert Love goto out; 129842e9a92fSRobert Love } 129942e9a92fSRobert Love 130076f6804eSAbhijeet Joglekar if (IS_ERR(fp)) { 130176f6804eSAbhijeet Joglekar fc_lport_error(lport, fp); 130276f6804eSAbhijeet Joglekar goto err; 130376f6804eSAbhijeet Joglekar } 130476f6804eSAbhijeet Joglekar 130542e9a92fSRobert Love op = fc_frame_payload_op(fp); 130642e9a92fSRobert Love if (op == ELS_LS_ACC) 13071190d925SJoe Eykholt fc_lport_enter_disabled(lport); 130842e9a92fSRobert Love else 130942e9a92fSRobert Love fc_lport_error(lport, fp); 131042e9a92fSRobert Love 131142e9a92fSRobert Love out: 131242e9a92fSRobert Love fc_frame_free(fp); 131342e9a92fSRobert Love err: 131442e9a92fSRobert Love mutex_unlock(&lport->lp_mutex); 131542e9a92fSRobert Love } 131611b56188SChris Leech EXPORT_SYMBOL(fc_lport_logo_resp); 131742e9a92fSRobert Love 131842e9a92fSRobert Love /** 131934f42a07SRobert Love * fc_rport_enter_logo() - Logout of the fabric 132042e9a92fSRobert Love * @lport: Fibre Channel local port to be logged out 132142e9a92fSRobert Love * 132242e9a92fSRobert Love * Locking Note: The lport lock is expected to be held before calling 132342e9a92fSRobert Love * this routine. 132442e9a92fSRobert Love */ 132542e9a92fSRobert Love static void fc_lport_enter_logo(struct fc_lport *lport) 132642e9a92fSRobert Love { 132742e9a92fSRobert Love struct fc_frame *fp; 132842e9a92fSRobert Love struct fc_els_logo *logo; 132942e9a92fSRobert Love 13307414705eSRobert Love FC_LPORT_DBG(lport, "Entered LOGO state from %s state\n", 13317414705eSRobert Love fc_lport_state(lport)); 133242e9a92fSRobert Love 133342e9a92fSRobert Love fc_lport_state_enter(lport, LPORT_ST_LOGO); 13348faecddbSChris Leech fc_vports_linkchange(lport); 133542e9a92fSRobert Love 133642e9a92fSRobert Love fp = fc_frame_alloc(lport, sizeof(*logo)); 133742e9a92fSRobert Love if (!fp) { 133842e9a92fSRobert Love fc_lport_error(lport, fp); 133942e9a92fSRobert Love return; 134042e9a92fSRobert Love } 134142e9a92fSRobert Love 1342a46f327aSJoe Eykholt if (!lport->tt.elsct_send(lport, FC_FID_FLOGI, fp, ELS_LOGO, 1343a46f327aSJoe Eykholt fc_lport_logo_resp, lport, lport->e_d_tov)) 13448f550f93SChris Leech fc_lport_error(lport, NULL); 134542e9a92fSRobert Love } 134642e9a92fSRobert Love 134742e9a92fSRobert Love /** 134834f42a07SRobert Love * fc_lport_flogi_resp() - Handle response to FLOGI request 134942e9a92fSRobert Love * @sp: current sequence in FLOGI exchange 135042e9a92fSRobert Love * @fp: response frame 135142e9a92fSRobert Love * @lp_arg: Fibre Channel lport port instance that sent the FLOGI request 135242e9a92fSRobert Love * 135342e9a92fSRobert Love * Locking Note: This function will be called without the lport lock 135442e9a92fSRobert Love * held, but it will lock, call an _enter_* function or fc_lport_error 135542e9a92fSRobert Love * and then unlock the lport. 135642e9a92fSRobert Love */ 135711b56188SChris Leech void fc_lport_flogi_resp(struct fc_seq *sp, struct fc_frame *fp, 135842e9a92fSRobert Love void *lp_arg) 135942e9a92fSRobert Love { 136042e9a92fSRobert Love struct fc_lport *lport = lp_arg; 136142e9a92fSRobert Love struct fc_frame_header *fh; 136242e9a92fSRobert Love struct fc_els_flogi *flp; 136342e9a92fSRobert Love u32 did; 136442e9a92fSRobert Love u16 csp_flags; 136542e9a92fSRobert Love unsigned int r_a_tov; 136642e9a92fSRobert Love unsigned int e_d_tov; 136742e9a92fSRobert Love u16 mfs; 136842e9a92fSRobert Love 1369f657d299SJoe Eykholt FC_LPORT_DBG(lport, "Received a FLOGI %s\n", fc_els_resp_type(fp)); 1370f657d299SJoe Eykholt 137142e9a92fSRobert Love if (fp == ERR_PTR(-FC_EX_CLOSED)) 137242e9a92fSRobert Love return; 137342e9a92fSRobert Love 137442e9a92fSRobert Love mutex_lock(&lport->lp_mutex); 137542e9a92fSRobert Love 137642e9a92fSRobert Love if (lport->state != LPORT_ST_FLOGI) { 13777414705eSRobert Love FC_LPORT_DBG(lport, "Received a FLOGI response, but in state " 13787414705eSRobert Love "%s\n", fc_lport_state(lport)); 137976f6804eSAbhijeet Joglekar if (IS_ERR(fp)) 138076f6804eSAbhijeet Joglekar goto err; 138142e9a92fSRobert Love goto out; 138242e9a92fSRobert Love } 138342e9a92fSRobert Love 138476f6804eSAbhijeet Joglekar if (IS_ERR(fp)) { 138576f6804eSAbhijeet Joglekar fc_lport_error(lport, fp); 138676f6804eSAbhijeet Joglekar goto err; 138776f6804eSAbhijeet Joglekar } 138876f6804eSAbhijeet Joglekar 138942e9a92fSRobert Love fh = fc_frame_header_get(fp); 139042e9a92fSRobert Love did = ntoh24(fh->fh_d_id); 139142e9a92fSRobert Love if (fc_frame_payload_op(fp) == ELS_LS_ACC && did != 0) { 139242e9a92fSRobert Love 13937414705eSRobert Love printk(KERN_INFO "libfc: Assigned FID (%6x) in FLOGI response\n", 13947414705eSRobert Love did); 139542e9a92fSRobert Love fc_host_port_id(lport->host) = did; 139642e9a92fSRobert Love 139742e9a92fSRobert Love flp = fc_frame_payload_get(fp, sizeof(*flp)); 139842e9a92fSRobert Love if (flp) { 139942e9a92fSRobert Love mfs = ntohs(flp->fl_csp.sp_bb_data) & 140042e9a92fSRobert Love FC_SP_BB_DATA_MASK; 140142e9a92fSRobert Love if (mfs >= FC_SP_MIN_MAX_PAYLOAD && 140242e9a92fSRobert Love mfs < lport->mfs) 140342e9a92fSRobert Love lport->mfs = mfs; 140442e9a92fSRobert Love csp_flags = ntohs(flp->fl_csp.sp_features); 140542e9a92fSRobert Love r_a_tov = ntohl(flp->fl_csp.sp_r_a_tov); 140642e9a92fSRobert Love e_d_tov = ntohl(flp->fl_csp.sp_e_d_tov); 140742e9a92fSRobert Love if (csp_flags & FC_SP_FT_EDTR) 140842e9a92fSRobert Love e_d_tov /= 1000000; 1409db36c06cSChris Leech 1410db36c06cSChris Leech lport->npiv_enabled = !!(csp_flags & FC_SP_FT_NPIV_ACC); 1411db36c06cSChris Leech 141242e9a92fSRobert Love if ((csp_flags & FC_SP_FT_FPORT) == 0) { 141342e9a92fSRobert Love if (e_d_tov > lport->e_d_tov) 141442e9a92fSRobert Love lport->e_d_tov = e_d_tov; 141542e9a92fSRobert Love lport->r_a_tov = 2 * e_d_tov; 14167414705eSRobert Love printk(KERN_INFO "libfc: Port (%6x) entered " 14177414705eSRobert Love "point to point mode\n", did); 141842e9a92fSRobert Love fc_lport_ptp_setup(lport, ntoh24(fh->fh_s_id), 141942e9a92fSRobert Love get_unaligned_be64( 142042e9a92fSRobert Love &flp->fl_wwpn), 142142e9a92fSRobert Love get_unaligned_be64( 142242e9a92fSRobert Love &flp->fl_wwnn)); 142342e9a92fSRobert Love } else { 142442e9a92fSRobert Love lport->e_d_tov = e_d_tov; 142542e9a92fSRobert Love lport->r_a_tov = r_a_tov; 142642e9a92fSRobert Love fc_host_fabric_name(lport->host) = 142742e9a92fSRobert Love get_unaligned_be64(&flp->fl_wwnn); 142842e9a92fSRobert Love fc_lport_enter_dns(lport); 142942e9a92fSRobert Love } 143042e9a92fSRobert Love } 143142e9a92fSRobert Love } else { 14327414705eSRobert Love FC_LPORT_DBG(lport, "Bad FLOGI response\n"); 143342e9a92fSRobert Love } 143442e9a92fSRobert Love 143542e9a92fSRobert Love out: 143642e9a92fSRobert Love fc_frame_free(fp); 143742e9a92fSRobert Love err: 143842e9a92fSRobert Love mutex_unlock(&lport->lp_mutex); 143942e9a92fSRobert Love } 144011b56188SChris Leech EXPORT_SYMBOL(fc_lport_flogi_resp); 144142e9a92fSRobert Love 144242e9a92fSRobert Love /** 144334f42a07SRobert Love * fc_rport_enter_flogi() - Send a FLOGI request to the fabric manager 144442e9a92fSRobert Love * @lport: Fibre Channel local port to be logged in to the fabric 144542e9a92fSRobert Love * 144642e9a92fSRobert Love * Locking Note: The lport lock is expected to be held before calling 144742e9a92fSRobert Love * this routine. 144842e9a92fSRobert Love */ 144942e9a92fSRobert Love void fc_lport_enter_flogi(struct fc_lport *lport) 145042e9a92fSRobert Love { 145142e9a92fSRobert Love struct fc_frame *fp; 145242e9a92fSRobert Love 14537414705eSRobert Love FC_LPORT_DBG(lport, "Entered FLOGI state from %s state\n", 14547414705eSRobert Love fc_lport_state(lport)); 145542e9a92fSRobert Love 145642e9a92fSRobert Love fc_lport_state_enter(lport, LPORT_ST_FLOGI); 145742e9a92fSRobert Love 145842e9a92fSRobert Love fp = fc_frame_alloc(lport, sizeof(struct fc_els_flogi)); 145942e9a92fSRobert Love if (!fp) 146042e9a92fSRobert Love return fc_lport_error(lport, fp); 146142e9a92fSRobert Love 1462db36c06cSChris Leech if (!lport->tt.elsct_send(lport, FC_FID_FLOGI, fp, 1463db36c06cSChris Leech lport->vport ? ELS_FDISC : ELS_FLOGI, 146442e9a92fSRobert Love fc_lport_flogi_resp, lport, lport->e_d_tov)) 14658f550f93SChris Leech fc_lport_error(lport, NULL); 146642e9a92fSRobert Love } 146742e9a92fSRobert Love 146842e9a92fSRobert Love /* Configure a fc_lport */ 146942e9a92fSRobert Love int fc_lport_config(struct fc_lport *lport) 147042e9a92fSRobert Love { 147142e9a92fSRobert Love INIT_DELAYED_WORK(&lport->retry_work, fc_lport_timeout); 147242e9a92fSRobert Love mutex_init(&lport->lp_mutex); 147342e9a92fSRobert Love 1474b1d9fd55SJoe Eykholt fc_lport_state_enter(lport, LPORT_ST_DISABLED); 147542e9a92fSRobert Love 147642e9a92fSRobert Love fc_lport_add_fc4_type(lport, FC_TYPE_FCP); 147742e9a92fSRobert Love fc_lport_add_fc4_type(lport, FC_TYPE_CT); 147842e9a92fSRobert Love 147942e9a92fSRobert Love return 0; 148042e9a92fSRobert Love } 148142e9a92fSRobert Love EXPORT_SYMBOL(fc_lport_config); 148242e9a92fSRobert Love 148342e9a92fSRobert Love int fc_lport_init(struct fc_lport *lport) 148442e9a92fSRobert Love { 148542e9a92fSRobert Love if (!lport->tt.lport_recv) 148642e9a92fSRobert Love lport->tt.lport_recv = fc_lport_recv_req; 148742e9a92fSRobert Love 148842e9a92fSRobert Love if (!lport->tt.lport_reset) 148942e9a92fSRobert Love lport->tt.lport_reset = fc_lport_reset; 149042e9a92fSRobert Love 149142e9a92fSRobert Love fc_host_port_type(lport->host) = FC_PORTTYPE_NPORT; 149242e9a92fSRobert Love fc_host_node_name(lport->host) = lport->wwnn; 149342e9a92fSRobert Love fc_host_port_name(lport->host) = lport->wwpn; 149442e9a92fSRobert Love fc_host_supported_classes(lport->host) = FC_COS_CLASS3; 149542e9a92fSRobert Love memset(fc_host_supported_fc4s(lport->host), 0, 149642e9a92fSRobert Love sizeof(fc_host_supported_fc4s(lport->host))); 149742e9a92fSRobert Love fc_host_supported_fc4s(lport->host)[2] = 1; 149842e9a92fSRobert Love fc_host_supported_fc4s(lport->host)[7] = 1; 149942e9a92fSRobert Love 150042e9a92fSRobert Love /* This value is also unchanging */ 150142e9a92fSRobert Love memset(fc_host_active_fc4s(lport->host), 0, 150242e9a92fSRobert Love sizeof(fc_host_active_fc4s(lport->host))); 150342e9a92fSRobert Love fc_host_active_fc4s(lport->host)[2] = 1; 150442e9a92fSRobert Love fc_host_active_fc4s(lport->host)[7] = 1; 150542e9a92fSRobert Love fc_host_maxframe_size(lport->host) = lport->mfs; 150642e9a92fSRobert Love fc_host_supported_speeds(lport->host) = 0; 150742e9a92fSRobert Love if (lport->link_supported_speeds & FC_PORTSPEED_1GBIT) 150842e9a92fSRobert Love fc_host_supported_speeds(lport->host) |= FC_PORTSPEED_1GBIT; 150942e9a92fSRobert Love if (lport->link_supported_speeds & FC_PORTSPEED_10GBIT) 151042e9a92fSRobert Love fc_host_supported_speeds(lport->host) |= FC_PORTSPEED_10GBIT; 151142e9a92fSRobert Love 151242e9a92fSRobert Love return 0; 151342e9a92fSRobert Love } 151442e9a92fSRobert Love EXPORT_SYMBOL(fc_lport_init); 1515