fc_lport.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514
  1. /*
  2. * Copyright(c) 2007 Intel Corporation. All rights reserved.
  3. *
  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.
  7. *
  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
  11. * more details.
  12. *
  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.
  16. *
  17. * Maintained at www.Open-FCoE.org
  18. */
  19. /*
  20. * PORT LOCKING NOTES
  21. *
  22. * These comments only apply to the 'port code' which consists of the lport,
  23. * disc and rport blocks.
  24. *
  25. * MOTIVATION
  26. *
  27. * The lport, disc and rport blocks all have mutexes that are used to protect
  28. * those objects. The main motivation for these locks is to prevent from
  29. * having an lport reset just before we send a frame. In that scenario the
  30. * lport's FID would get set to zero and then we'd send a frame with an
  31. * invalid SID. We also need to ensure that states don't change unexpectedly
  32. * while processing another state.
  33. *
  34. * HEIRARCHY
  35. *
  36. * The following heirarchy defines the locking rules. A greater lock
  37. * may be held before acquiring a lesser lock, but a lesser lock should never
  38. * be held while attempting to acquire a greater lock. Here is the heirarchy-
  39. *
  40. * lport > disc, lport > rport, disc > rport
  41. *
  42. * CALLBACKS
  43. *
  44. * The callbacks cause complications with this scheme. There is a callback
  45. * from the rport (to either lport or disc) and a callback from disc
  46. * (to the lport).
  47. *
  48. * As rports exit the rport state machine a callback is made to the owner of
  49. * the rport to notify success or failure. Since the callback is likely to
  50. * cause the lport or disc to grab its lock we cannot hold the rport lock
  51. * while making the callback. To ensure that the rport is not free'd while
  52. * processing the callback the rport callbacks are serialized through a
  53. * single-threaded workqueue. An rport would never be free'd while in a
  54. * callback handler becuase no other rport work in this queue can be executed
  55. * at the same time.
  56. *
  57. * When discovery succeeds or fails a callback is made to the lport as
  58. * notification. Currently, succesful discovery causes the lport to take no
  59. * action. A failure will cause the lport to reset. There is likely a circular
  60. * locking problem with this implementation.
  61. */
  62. /*
  63. * LPORT LOCKING
  64. *
  65. * The critical sections protected by the lport's mutex are quite broad and
  66. * may be improved upon in the future. The lport code and its locking doesn't
  67. * influence the I/O path, so excessive locking doesn't penalize I/O
  68. * performance.
  69. *
  70. * The strategy is to lock whenever processing a request or response. Note
  71. * that every _enter_* function corresponds to a state change. They generally
  72. * change the lports state and then send a request out on the wire. We lock
  73. * before calling any of these functions to protect that state change. This
  74. * means that the entry points into the lport block manage the locks while
  75. * the state machine can transition between states (i.e. _enter_* functions)
  76. * while always staying protected.
  77. *
  78. * When handling responses we also hold the lport mutex broadly. When the
  79. * lport receives the response frame it locks the mutex and then calls the
  80. * appropriate handler for the particuar response. Generally a response will
  81. * trigger a state change and so the lock must already be held.
  82. *
  83. * Retries also have to consider the locking. The retries occur from a work
  84. * context and the work function will lock the lport and then retry the state
  85. * (i.e. _enter_* function).
  86. */
  87. #include <linux/timer.h>
  88. #include <asm/unaligned.h>
  89. #include <scsi/fc/fc_gs.h>
  90. #include <scsi/libfc.h>
  91. #include <scsi/fc_encode.h>
  92. #include "fc_libfc.h"
  93. /* Fabric IDs to use for point-to-point mode, chosen on whims. */
  94. #define FC_LOCAL_PTP_FID_LO 0x010101
  95. #define FC_LOCAL_PTP_FID_HI 0x010102
  96. #define DNS_DELAY 3 /* Discovery delay after RSCN (in seconds)*/
  97. static void fc_lport_error(struct fc_lport *, struct fc_frame *);
  98. static void fc_lport_enter_reset(struct fc_lport *);
  99. static void fc_lport_enter_flogi(struct fc_lport *);
  100. static void fc_lport_enter_dns(struct fc_lport *);
  101. static void fc_lport_enter_ns(struct fc_lport *, enum fc_lport_state);
  102. static void fc_lport_enter_scr(struct fc_lport *);
  103. static void fc_lport_enter_ready(struct fc_lport *);
  104. static void fc_lport_enter_logo(struct fc_lport *);
  105. static const char *fc_lport_state_names[] = {
  106. [LPORT_ST_DISABLED] = "disabled",
  107. [LPORT_ST_FLOGI] = "FLOGI",
  108. [LPORT_ST_DNS] = "dNS",
  109. [LPORT_ST_RNN_ID] = "RNN_ID",
  110. [LPORT_ST_RSNN_NN] = "RSNN_NN",
  111. [LPORT_ST_RSPN_ID] = "RSPN_ID",
  112. [LPORT_ST_RFT_ID] = "RFT_ID",
  113. [LPORT_ST_SCR] = "SCR",
  114. [LPORT_ST_READY] = "Ready",
  115. [LPORT_ST_LOGO] = "LOGO",
  116. [LPORT_ST_RESET] = "reset",
  117. };
  118. static int fc_frame_drop(struct fc_lport *lport, struct fc_frame *fp)
  119. {
  120. fc_frame_free(fp);
  121. return 0;
  122. }
  123. /**
  124. * fc_lport_rport_callback() - Event handler for rport events
  125. * @lport: The lport which is receiving the event
  126. * @rdata: private remote port data
  127. * @event: The event that occured
  128. *
  129. * Locking Note: The rport lock should not be held when calling
  130. * this function.
  131. */
  132. static void fc_lport_rport_callback(struct fc_lport *lport,
  133. struct fc_rport_priv *rdata,
  134. enum fc_rport_event event)
  135. {
  136. FC_LPORT_DBG(lport, "Received a %d event for port (%6x)\n", event,
  137. rdata->ids.port_id);
  138. mutex_lock(&lport->lp_mutex);
  139. switch (event) {
  140. case RPORT_EV_READY:
  141. if (lport->state == LPORT_ST_DNS) {
  142. lport->dns_rp = rdata;
  143. fc_lport_enter_ns(lport, LPORT_ST_RNN_ID);
  144. } else {
  145. FC_LPORT_DBG(lport, "Received an READY event "
  146. "on port (%6x) for the directory "
  147. "server, but the lport is not "
  148. "in the DNS state, it's in the "
  149. "%d state", rdata->ids.port_id,
  150. lport->state);
  151. lport->tt.rport_logoff(rdata);
  152. }
  153. break;
  154. case RPORT_EV_LOGO:
  155. case RPORT_EV_FAILED:
  156. case RPORT_EV_STOP:
  157. lport->dns_rp = NULL;
  158. break;
  159. case RPORT_EV_NONE:
  160. break;
  161. }
  162. mutex_unlock(&lport->lp_mutex);
  163. }
  164. /**
  165. * fc_lport_state() - Return a string which represents the lport's state
  166. * @lport: The lport whose state is to converted to a string
  167. */
  168. static const char *fc_lport_state(struct fc_lport *lport)
  169. {
  170. const char *cp;
  171. cp = fc_lport_state_names[lport->state];
  172. if (!cp)
  173. cp = "unknown";
  174. return cp;
  175. }
  176. /**
  177. * fc_lport_ptp_setup() - Create an rport for point-to-point mode
  178. * @lport: The lport to attach the ptp rport to
  179. * @fid: The FID of the ptp rport
  180. * @remote_wwpn: The WWPN of the ptp rport
  181. * @remote_wwnn: The WWNN of the ptp rport
  182. */
  183. static void fc_lport_ptp_setup(struct fc_lport *lport,
  184. u32 remote_fid, u64 remote_wwpn,
  185. u64 remote_wwnn)
  186. {
  187. mutex_lock(&lport->disc.disc_mutex);
  188. if (lport->ptp_rp)
  189. lport->tt.rport_logoff(lport->ptp_rp);
  190. lport->ptp_rp = lport->tt.rport_create(lport, remote_fid);
  191. lport->ptp_rp->ids.port_name = remote_wwpn;
  192. lport->ptp_rp->ids.node_name = remote_wwnn;
  193. mutex_unlock(&lport->disc.disc_mutex);
  194. lport->tt.rport_login(lport->ptp_rp);
  195. fc_lport_enter_ready(lport);
  196. }
  197. void fc_get_host_port_type(struct Scsi_Host *shost)
  198. {
  199. /* TODO - currently just NPORT */
  200. fc_host_port_type(shost) = FC_PORTTYPE_NPORT;
  201. }
  202. EXPORT_SYMBOL(fc_get_host_port_type);
  203. void fc_get_host_port_state(struct Scsi_Host *shost)
  204. {
  205. struct fc_lport *lp = shost_priv(shost);
  206. mutex_lock(&lp->lp_mutex);
  207. if (!lp->link_up)
  208. fc_host_port_state(shost) = FC_PORTSTATE_LINKDOWN;
  209. else
  210. switch (lp->state) {
  211. case LPORT_ST_READY:
  212. fc_host_port_state(shost) = FC_PORTSTATE_ONLINE;
  213. break;
  214. default:
  215. fc_host_port_state(shost) = FC_PORTSTATE_OFFLINE;
  216. }
  217. mutex_unlock(&lp->lp_mutex);
  218. }
  219. EXPORT_SYMBOL(fc_get_host_port_state);
  220. void fc_get_host_speed(struct Scsi_Host *shost)
  221. {
  222. struct fc_lport *lport = shost_priv(shost);
  223. fc_host_speed(shost) = lport->link_speed;
  224. }
  225. EXPORT_SYMBOL(fc_get_host_speed);
  226. struct fc_host_statistics *fc_get_host_stats(struct Scsi_Host *shost)
  227. {
  228. struct fc_host_statistics *fcoe_stats;
  229. struct fc_lport *lp = shost_priv(shost);
  230. struct timespec v0, v1;
  231. unsigned int cpu;
  232. fcoe_stats = &lp->host_stats;
  233. memset(fcoe_stats, 0, sizeof(struct fc_host_statistics));
  234. jiffies_to_timespec(jiffies, &v0);
  235. jiffies_to_timespec(lp->boot_time, &v1);
  236. fcoe_stats->seconds_since_last_reset = (v0.tv_sec - v1.tv_sec);
  237. for_each_possible_cpu(cpu) {
  238. struct fcoe_dev_stats *stats;
  239. stats = per_cpu_ptr(lp->dev_stats, cpu);
  240. fcoe_stats->tx_frames += stats->TxFrames;
  241. fcoe_stats->tx_words += stats->TxWords;
  242. fcoe_stats->rx_frames += stats->RxFrames;
  243. fcoe_stats->rx_words += stats->RxWords;
  244. fcoe_stats->error_frames += stats->ErrorFrames;
  245. fcoe_stats->invalid_crc_count += stats->InvalidCRCCount;
  246. fcoe_stats->fcp_input_requests += stats->InputRequests;
  247. fcoe_stats->fcp_output_requests += stats->OutputRequests;
  248. fcoe_stats->fcp_control_requests += stats->ControlRequests;
  249. fcoe_stats->fcp_input_megabytes += stats->InputMegabytes;
  250. fcoe_stats->fcp_output_megabytes += stats->OutputMegabytes;
  251. fcoe_stats->link_failure_count += stats->LinkFailureCount;
  252. }
  253. fcoe_stats->lip_count = -1;
  254. fcoe_stats->nos_count = -1;
  255. fcoe_stats->loss_of_sync_count = -1;
  256. fcoe_stats->loss_of_signal_count = -1;
  257. fcoe_stats->prim_seq_protocol_err_count = -1;
  258. fcoe_stats->dumped_frames = -1;
  259. return fcoe_stats;
  260. }
  261. EXPORT_SYMBOL(fc_get_host_stats);
  262. /*
  263. * Fill in FLOGI command for request.
  264. */
  265. static void
  266. fc_lport_flogi_fill(struct fc_lport *lport, struct fc_els_flogi *flogi,
  267. unsigned int op)
  268. {
  269. struct fc_els_csp *sp;
  270. struct fc_els_cssp *cp;
  271. memset(flogi, 0, sizeof(*flogi));
  272. flogi->fl_cmd = (u8) op;
  273. put_unaligned_be64(lport->wwpn, &flogi->fl_wwpn);
  274. put_unaligned_be64(lport->wwnn, &flogi->fl_wwnn);
  275. sp = &flogi->fl_csp;
  276. sp->sp_hi_ver = 0x20;
  277. sp->sp_lo_ver = 0x20;
  278. sp->sp_bb_cred = htons(10); /* this gets set by gateway */
  279. sp->sp_bb_data = htons((u16) lport->mfs);
  280. cp = &flogi->fl_cssp[3 - 1]; /* class 3 parameters */
  281. cp->cp_class = htons(FC_CPC_VALID | FC_CPC_SEQ);
  282. if (op != ELS_FLOGI) {
  283. sp->sp_features = htons(FC_SP_FT_CIRO);
  284. sp->sp_tot_seq = htons(255); /* seq. we accept */
  285. sp->sp_rel_off = htons(0x1f);
  286. sp->sp_e_d_tov = htonl(lport->e_d_tov);
  287. cp->cp_rdfs = htons((u16) lport->mfs);
  288. cp->cp_con_seq = htons(255);
  289. cp->cp_open_seq = 1;
  290. }
  291. }
  292. /*
  293. * Add a supported FC-4 type.
  294. */
  295. static void fc_lport_add_fc4_type(struct fc_lport *lport, enum fc_fh_type type)
  296. {
  297. __be32 *mp;
  298. mp = &lport->fcts.ff_type_map[type / FC_NS_BPW];
  299. *mp = htonl(ntohl(*mp) | 1UL << (type % FC_NS_BPW));
  300. }
  301. /**
  302. * fc_lport_recv_rlir_req() - Handle received Registered Link Incident Report.
  303. * @lport: Fibre Channel local port recieving the RLIR
  304. * @sp: current sequence in the RLIR exchange
  305. * @fp: RLIR request frame
  306. *
  307. * Locking Note: The lport lock is expected to be held before calling
  308. * this function.
  309. */
  310. static void fc_lport_recv_rlir_req(struct fc_seq *sp, struct fc_frame *fp,
  311. struct fc_lport *lport)
  312. {
  313. FC_LPORT_DBG(lport, "Received RLIR request while in state %s\n",
  314. fc_lport_state(lport));
  315. lport->tt.seq_els_rsp_send(sp, ELS_LS_ACC, NULL);
  316. fc_frame_free(fp);
  317. }
  318. /**
  319. * fc_lport_recv_echo_req() - Handle received ECHO request
  320. * @lport: Fibre Channel local port recieving the ECHO
  321. * @sp: current sequence in the ECHO exchange
  322. * @fp: ECHO request frame
  323. *
  324. * Locking Note: The lport lock is expected to be held before calling
  325. * this function.
  326. */
  327. static void fc_lport_recv_echo_req(struct fc_seq *sp, struct fc_frame *in_fp,
  328. struct fc_lport *lport)
  329. {
  330. struct fc_frame *fp;
  331. struct fc_exch *ep = fc_seq_exch(sp);
  332. unsigned int len;
  333. void *pp;
  334. void *dp;
  335. u32 f_ctl;
  336. FC_LPORT_DBG(lport, "Received ECHO request while in state %s\n",
  337. fc_lport_state(lport));
  338. len = fr_len(in_fp) - sizeof(struct fc_frame_header);
  339. pp = fc_frame_payload_get(in_fp, len);
  340. if (len < sizeof(__be32))
  341. len = sizeof(__be32);
  342. fp = fc_frame_alloc(lport, len);
  343. if (fp) {
  344. dp = fc_frame_payload_get(fp, len);
  345. memcpy(dp, pp, len);
  346. *((__be32 *)dp) = htonl(ELS_LS_ACC << 24);
  347. sp = lport->tt.seq_start_next(sp);
  348. f_ctl = FC_FC_EX_CTX | FC_FC_LAST_SEQ | FC_FC_END_SEQ;
  349. fc_fill_fc_hdr(fp, FC_RCTL_ELS_REP, ep->did, ep->sid,
  350. FC_TYPE_ELS, f_ctl, 0);
  351. lport->tt.seq_send(lport, sp, fp);
  352. }
  353. fc_frame_free(in_fp);
  354. }
  355. /**
  356. * fc_lport_recv_rnid_req() - Handle received Request Node ID data request
  357. * @sp: The sequence in the RNID exchange
  358. * @fp: The RNID request frame
  359. * @lport: The local port recieving the RNID
  360. *
  361. * Locking Note: The lport lock is expected to be held before calling
  362. * this function.
  363. */
  364. static void fc_lport_recv_rnid_req(struct fc_seq *sp, struct fc_frame *in_fp,
  365. struct fc_lport *lport)
  366. {
  367. struct fc_frame *fp;
  368. struct fc_exch *ep = fc_seq_exch(sp);
  369. struct fc_els_rnid *req;
  370. struct {
  371. struct fc_els_rnid_resp rnid;
  372. struct fc_els_rnid_cid cid;
  373. struct fc_els_rnid_gen gen;
  374. } *rp;
  375. struct fc_seq_els_data rjt_data;
  376. u8 fmt;
  377. size_t len;
  378. u32 f_ctl;
  379. FC_LPORT_DBG(lport, "Received RNID request while in state %s\n",
  380. fc_lport_state(lport));
  381. req = fc_frame_payload_get(in_fp, sizeof(*req));
  382. if (!req) {
  383. rjt_data.fp = NULL;
  384. rjt_data.reason = ELS_RJT_LOGIC;
  385. rjt_data.explan = ELS_EXPL_NONE;
  386. lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
  387. } else {
  388. fmt = req->rnid_fmt;
  389. len = sizeof(*rp);
  390. if (fmt != ELS_RNIDF_GEN ||
  391. ntohl(lport->rnid_gen.rnid_atype) == 0) {
  392. fmt = ELS_RNIDF_NONE; /* nothing to provide */
  393. len -= sizeof(rp->gen);
  394. }
  395. fp = fc_frame_alloc(lport, len);
  396. if (fp) {
  397. rp = fc_frame_payload_get(fp, len);
  398. memset(rp, 0, len);
  399. rp->rnid.rnid_cmd = ELS_LS_ACC;
  400. rp->rnid.rnid_fmt = fmt;
  401. rp->rnid.rnid_cid_len = sizeof(rp->cid);
  402. rp->cid.rnid_wwpn = htonll(lport->wwpn);
  403. rp->cid.rnid_wwnn = htonll(lport->wwnn);
  404. if (fmt == ELS_RNIDF_GEN) {
  405. rp->rnid.rnid_sid_len = sizeof(rp->gen);
  406. memcpy(&rp->gen, &lport->rnid_gen,
  407. sizeof(rp->gen));
  408. }
  409. sp = lport->tt.seq_start_next(sp);
  410. f_ctl = FC_FC_EX_CTX | FC_FC_LAST_SEQ;
  411. f_ctl |= FC_FC_END_SEQ | FC_FC_SEQ_INIT;
  412. fc_fill_fc_hdr(fp, FC_RCTL_ELS_REP, ep->did, ep->sid,
  413. FC_TYPE_ELS, f_ctl, 0);
  414. lport->tt.seq_send(lport, sp, fp);
  415. }
  416. }
  417. fc_frame_free(in_fp);
  418. }
  419. /**
  420. * fc_lport_recv_logo_req() - Handle received fabric LOGO request
  421. * @lport: Fibre Channel local port recieving the LOGO
  422. * @sp: current sequence in the LOGO exchange
  423. * @fp: LOGO request frame
  424. *
  425. * Locking Note: The lport lock is exected to be held before calling
  426. * this function.
  427. */
  428. static void fc_lport_recv_logo_req(struct fc_seq *sp, struct fc_frame *fp,
  429. struct fc_lport *lport)
  430. {
  431. lport->tt.seq_els_rsp_send(sp, ELS_LS_ACC, NULL);
  432. fc_lport_enter_reset(lport);
  433. fc_frame_free(fp);
  434. }
  435. /**
  436. * fc_fabric_login() - Start the lport state machine
  437. * @lport: The lport that should log into the fabric
  438. *
  439. * Locking Note: This function should not be called
  440. * with the lport lock held.
  441. */
  442. int fc_fabric_login(struct fc_lport *lport)
  443. {
  444. int rc = -1;
  445. mutex_lock(&lport->lp_mutex);
  446. if (lport->state == LPORT_ST_DISABLED) {
  447. fc_lport_enter_reset(lport);
  448. rc = 0;
  449. }
  450. mutex_unlock(&lport->lp_mutex);
  451. return rc;
  452. }
  453. EXPORT_SYMBOL(fc_fabric_login);
  454. /**
  455. * __fc_linkup() - Handler for transport linkup events
  456. * @lport: The lport whose link is up
  457. *
  458. * Locking: must be called with the lp_mutex held
  459. */
  460. void __fc_linkup(struct fc_lport *lport)
  461. {
  462. if (!lport->link_up) {
  463. lport->link_up = 1;
  464. if (lport->state == LPORT_ST_RESET)
  465. fc_lport_enter_flogi(lport);
  466. }
  467. }
  468. /**
  469. * fc_linkup() - Handler for transport linkup events
  470. * @lport: The lport whose link is up
  471. */
  472. void fc_linkup(struct fc_lport *lport)
  473. {
  474. printk(KERN_INFO "libfc: Link up on port (%6x)\n",
  475. fc_host_port_id(lport->host));
  476. mutex_lock(&lport->lp_mutex);
  477. __fc_linkup(lport);
  478. mutex_unlock(&lport->lp_mutex);
  479. }
  480. EXPORT_SYMBOL(fc_linkup);
  481. /**
  482. * __fc_linkdown() - Handler for transport linkdown events
  483. * @lport: The lport whose link is down
  484. *
  485. * Locking: must be called with the lp_mutex held
  486. */
  487. void __fc_linkdown(struct fc_lport *lport)
  488. {
  489. if (lport->link_up) {
  490. lport->link_up = 0;
  491. fc_lport_enter_reset(lport);
  492. lport->tt.fcp_cleanup(lport);
  493. }
  494. }
  495. /**
  496. * fc_linkdown() - Handler for transport linkdown events
  497. * @lport: The lport whose link is down
  498. */
  499. void fc_linkdown(struct fc_lport *lport)
  500. {
  501. printk(KERN_INFO "libfc: Link down on port (%6x)\n",
  502. fc_host_port_id(lport->host));
  503. mutex_lock(&lport->lp_mutex);
  504. __fc_linkdown(lport);
  505. mutex_unlock(&lport->lp_mutex);
  506. }
  507. EXPORT_SYMBOL(fc_linkdown);
  508. /**
  509. * fc_fabric_logoff() - Logout of the fabric
  510. * @lport: fc_lport pointer to logoff the fabric
  511. *
  512. * Return value:
  513. * 0 for success, -1 for failure
  514. */
  515. int fc_fabric_logoff(struct fc_lport *lport)
  516. {
  517. lport->tt.disc_stop_final(lport);
  518. mutex_lock(&lport->lp_mutex);
  519. if (lport->dns_rp)
  520. lport->tt.rport_logoff(lport->dns_rp);
  521. mutex_unlock(&lport->lp_mutex);
  522. lport->tt.rport_flush_queue();
  523. mutex_lock(&lport->lp_mutex);
  524. fc_lport_enter_logo(lport);
  525. mutex_unlock(&lport->lp_mutex);
  526. cancel_delayed_work_sync(&lport->retry_work);
  527. return 0;
  528. }
  529. EXPORT_SYMBOL(fc_fabric_logoff);
  530. /**
  531. * fc_lport_destroy() - unregister a fc_lport
  532. * @lport: fc_lport pointer to unregister
  533. *
  534. * Return value:
  535. * None
  536. * Note:
  537. * exit routine for fc_lport instance
  538. * clean-up all the allocated memory
  539. * and free up other system resources.
  540. *
  541. */
  542. int fc_lport_destroy(struct fc_lport *lport)
  543. {
  544. mutex_lock(&lport->lp_mutex);
  545. lport->state = LPORT_ST_DISABLED;
  546. lport->link_up = 0;
  547. lport->tt.frame_send = fc_frame_drop;
  548. mutex_unlock(&lport->lp_mutex);
  549. lport->tt.fcp_abort_io(lport);
  550. lport->tt.disc_stop_final(lport);
  551. lport->tt.exch_mgr_reset(lport, 0, 0);
  552. return 0;
  553. }
  554. EXPORT_SYMBOL(fc_lport_destroy);
  555. /**
  556. * fc_set_mfs() - sets up the mfs for the corresponding fc_lport
  557. * @lport: fc_lport pointer to unregister
  558. * @mfs: the new mfs for fc_lport
  559. *
  560. * Set mfs for the given fc_lport to the new mfs.
  561. *
  562. * Return: 0 for success
  563. */
  564. int fc_set_mfs(struct fc_lport *lport, u32 mfs)
  565. {
  566. unsigned int old_mfs;
  567. int rc = -EINVAL;
  568. mutex_lock(&lport->lp_mutex);
  569. old_mfs = lport->mfs;
  570. if (mfs >= FC_MIN_MAX_FRAME) {
  571. mfs &= ~3;
  572. if (mfs > FC_MAX_FRAME)
  573. mfs = FC_MAX_FRAME;
  574. mfs -= sizeof(struct fc_frame_header);
  575. lport->mfs = mfs;
  576. rc = 0;
  577. }
  578. if (!rc && mfs < old_mfs)
  579. fc_lport_enter_reset(lport);
  580. mutex_unlock(&lport->lp_mutex);
  581. return rc;
  582. }
  583. EXPORT_SYMBOL(fc_set_mfs);
  584. /**
  585. * fc_lport_disc_callback() - Callback for discovery events
  586. * @lport: FC local port
  587. * @event: The discovery event
  588. */
  589. void fc_lport_disc_callback(struct fc_lport *lport, enum fc_disc_event event)
  590. {
  591. switch (event) {
  592. case DISC_EV_SUCCESS:
  593. FC_LPORT_DBG(lport, "Discovery succeeded\n");
  594. break;
  595. case DISC_EV_FAILED:
  596. printk(KERN_ERR "libfc: Discovery failed for port (%6x)\n",
  597. fc_host_port_id(lport->host));
  598. mutex_lock(&lport->lp_mutex);
  599. fc_lport_enter_reset(lport);
  600. mutex_unlock(&lport->lp_mutex);
  601. break;
  602. case DISC_EV_NONE:
  603. WARN_ON(1);
  604. break;
  605. }
  606. }
  607. /**
  608. * fc_rport_enter_ready() - Enter the ready state and start discovery
  609. * @lport: Fibre Channel local port that is ready
  610. *
  611. * Locking Note: The lport lock is expected to be held before calling
  612. * this routine.
  613. */
  614. static void fc_lport_enter_ready(struct fc_lport *lport)
  615. {
  616. FC_LPORT_DBG(lport, "Entered READY from state %s\n",
  617. fc_lport_state(lport));
  618. fc_lport_state_enter(lport, LPORT_ST_READY);
  619. if (lport->vport)
  620. fc_vport_set_state(lport->vport, FC_VPORT_ACTIVE);
  621. fc_vports_linkchange(lport);
  622. if (!lport->ptp_rp)
  623. lport->tt.disc_start(fc_lport_disc_callback, lport);
  624. }
  625. /**
  626. * fc_lport_recv_flogi_req() - Receive a FLOGI request
  627. * @sp_in: The sequence the FLOGI is on
  628. * @rx_fp: The frame the FLOGI is in
  629. * @lport: The lport that recieved the request
  630. *
  631. * A received FLOGI request indicates a point-to-point connection.
  632. * Accept it with the common service parameters indicating our N port.
  633. * Set up to do a PLOGI if we have the higher-number WWPN.
  634. *
  635. * Locking Note: The lport lock is expected to be held before calling
  636. * this function.
  637. */
  638. static void fc_lport_recv_flogi_req(struct fc_seq *sp_in,
  639. struct fc_frame *rx_fp,
  640. struct fc_lport *lport)
  641. {
  642. struct fc_frame *fp;
  643. struct fc_frame_header *fh;
  644. struct fc_seq *sp;
  645. struct fc_exch *ep;
  646. struct fc_els_flogi *flp;
  647. struct fc_els_flogi *new_flp;
  648. u64 remote_wwpn;
  649. u32 remote_fid;
  650. u32 local_fid;
  651. u32 f_ctl;
  652. FC_LPORT_DBG(lport, "Received FLOGI request while in state %s\n",
  653. fc_lport_state(lport));
  654. fh = fc_frame_header_get(rx_fp);
  655. remote_fid = ntoh24(fh->fh_s_id);
  656. flp = fc_frame_payload_get(rx_fp, sizeof(*flp));
  657. if (!flp)
  658. goto out;
  659. remote_wwpn = get_unaligned_be64(&flp->fl_wwpn);
  660. if (remote_wwpn == lport->wwpn) {
  661. printk(KERN_WARNING "libfc: Received FLOGI from port "
  662. "with same WWPN %llx\n", remote_wwpn);
  663. goto out;
  664. }
  665. FC_LPORT_DBG(lport, "FLOGI from port WWPN %llx\n", remote_wwpn);
  666. /*
  667. * XXX what is the right thing to do for FIDs?
  668. * The originator might expect our S_ID to be 0xfffffe.
  669. * But if so, both of us could end up with the same FID.
  670. */
  671. local_fid = FC_LOCAL_PTP_FID_LO;
  672. if (remote_wwpn < lport->wwpn) {
  673. local_fid = FC_LOCAL_PTP_FID_HI;
  674. if (!remote_fid || remote_fid == local_fid)
  675. remote_fid = FC_LOCAL_PTP_FID_LO;
  676. } else if (!remote_fid) {
  677. remote_fid = FC_LOCAL_PTP_FID_HI;
  678. }
  679. fc_host_port_id(lport->host) = local_fid;
  680. fp = fc_frame_alloc(lport, sizeof(*flp));
  681. if (fp) {
  682. sp = lport->tt.seq_start_next(fr_seq(rx_fp));
  683. new_flp = fc_frame_payload_get(fp, sizeof(*flp));
  684. fc_lport_flogi_fill(lport, new_flp, ELS_FLOGI);
  685. new_flp->fl_cmd = (u8) ELS_LS_ACC;
  686. /*
  687. * Send the response. If this fails, the originator should
  688. * repeat the sequence.
  689. */
  690. f_ctl = FC_FC_EX_CTX | FC_FC_LAST_SEQ | FC_FC_END_SEQ;
  691. ep = fc_seq_exch(sp);
  692. fc_fill_fc_hdr(fp, FC_RCTL_ELS_REP, ep->did, ep->sid,
  693. FC_TYPE_ELS, f_ctl, 0);
  694. lport->tt.seq_send(lport, sp, fp);
  695. } else {
  696. fc_lport_error(lport, fp);
  697. }
  698. fc_lport_ptp_setup(lport, remote_fid, remote_wwpn,
  699. get_unaligned_be64(&flp->fl_wwnn));
  700. out:
  701. sp = fr_seq(rx_fp);
  702. fc_frame_free(rx_fp);
  703. }
  704. /**
  705. * fc_lport_recv_req() - The generic lport request handler
  706. * @lport: The lport that received the request
  707. * @sp: The sequence the request is on
  708. * @fp: The frame the request is in
  709. *
  710. * This function will see if the lport handles the request or
  711. * if an rport should handle the request.
  712. *
  713. * Locking Note: This function should not be called with the lport
  714. * lock held becuase it will grab the lock.
  715. */
  716. static void fc_lport_recv_req(struct fc_lport *lport, struct fc_seq *sp,
  717. struct fc_frame *fp)
  718. {
  719. struct fc_frame_header *fh = fc_frame_header_get(fp);
  720. void (*recv) (struct fc_seq *, struct fc_frame *, struct fc_lport *);
  721. mutex_lock(&lport->lp_mutex);
  722. /*
  723. * Handle special ELS cases like FLOGI, LOGO, and
  724. * RSCN here. These don't require a session.
  725. * Even if we had a session, it might not be ready.
  726. */
  727. if (!lport->link_up)
  728. fc_frame_free(fp);
  729. else if (fh->fh_type == FC_TYPE_ELS &&
  730. fh->fh_r_ctl == FC_RCTL_ELS_REQ) {
  731. /*
  732. * Check opcode.
  733. */
  734. recv = lport->tt.rport_recv_req;
  735. switch (fc_frame_payload_op(fp)) {
  736. case ELS_FLOGI:
  737. recv = fc_lport_recv_flogi_req;
  738. break;
  739. case ELS_LOGO:
  740. fh = fc_frame_header_get(fp);
  741. if (ntoh24(fh->fh_s_id) == FC_FID_FLOGI)
  742. recv = fc_lport_recv_logo_req;
  743. break;
  744. case ELS_RSCN:
  745. recv = lport->tt.disc_recv_req;
  746. break;
  747. case ELS_ECHO:
  748. recv = fc_lport_recv_echo_req;
  749. break;
  750. case ELS_RLIR:
  751. recv = fc_lport_recv_rlir_req;
  752. break;
  753. case ELS_RNID:
  754. recv = fc_lport_recv_rnid_req;
  755. break;
  756. }
  757. recv(sp, fp, lport);
  758. } else {
  759. FC_LPORT_DBG(lport, "dropping invalid frame (eof %x)\n",
  760. fr_eof(fp));
  761. fc_frame_free(fp);
  762. }
  763. mutex_unlock(&lport->lp_mutex);
  764. /*
  765. * The common exch_done for all request may not be good
  766. * if any request requires longer hold on exhange. XXX
  767. */
  768. lport->tt.exch_done(sp);
  769. }
  770. /**
  771. * fc_lport_reset() - Reset an lport
  772. * @lport: The lport which should be reset
  773. *
  774. * Locking Note: This functions should not be called with the
  775. * lport lock held.
  776. */
  777. int fc_lport_reset(struct fc_lport *lport)
  778. {
  779. cancel_delayed_work_sync(&lport->retry_work);
  780. mutex_lock(&lport->lp_mutex);
  781. fc_lport_enter_reset(lport);
  782. mutex_unlock(&lport->lp_mutex);
  783. return 0;
  784. }
  785. EXPORT_SYMBOL(fc_lport_reset);
  786. /**
  787. * fc_lport_reset_locked() - Reset the local port
  788. * @lport: Fibre Channel local port to be reset
  789. *
  790. * Locking Note: The lport lock is expected to be held before calling
  791. * this routine.
  792. */
  793. static void fc_lport_reset_locked(struct fc_lport *lport)
  794. {
  795. if (lport->dns_rp)
  796. lport->tt.rport_logoff(lport->dns_rp);
  797. lport->ptp_rp = NULL;
  798. lport->tt.disc_stop(lport);
  799. lport->tt.exch_mgr_reset(lport, 0, 0);
  800. fc_host_fabric_name(lport->host) = 0;
  801. fc_host_port_id(lport->host) = 0;
  802. }
  803. /**
  804. * fc_lport_enter_reset() - Reset the local port
  805. * @lport: Fibre Channel local port to be reset
  806. *
  807. * Locking Note: The lport lock is expected to be held before calling
  808. * this routine.
  809. */
  810. static void fc_lport_enter_reset(struct fc_lport *lport)
  811. {
  812. FC_LPORT_DBG(lport, "Entered RESET state from %s state\n",
  813. fc_lport_state(lport));
  814. if (lport->vport) {
  815. if (lport->link_up)
  816. fc_vport_set_state(lport->vport, FC_VPORT_INITIALIZING);
  817. else
  818. fc_vport_set_state(lport->vport, FC_VPORT_LINKDOWN);
  819. }
  820. fc_lport_state_enter(lport, LPORT_ST_RESET);
  821. fc_vports_linkchange(lport);
  822. fc_lport_reset_locked(lport);
  823. if (lport->link_up)
  824. fc_lport_enter_flogi(lport);
  825. }
  826. /**
  827. * fc_lport_enter_disabled() - disable the local port
  828. * @lport: Fibre Channel local port to be reset
  829. *
  830. * Locking Note: The lport lock is expected to be held before calling
  831. * this routine.
  832. */
  833. static void fc_lport_enter_disabled(struct fc_lport *lport)
  834. {
  835. FC_LPORT_DBG(lport, "Entered disabled state from %s state\n",
  836. fc_lport_state(lport));
  837. fc_lport_state_enter(lport, LPORT_ST_DISABLED);
  838. fc_vports_linkchange(lport);
  839. fc_lport_reset_locked(lport);
  840. }
  841. /**
  842. * fc_lport_error() - Handler for any errors
  843. * @lport: The fc_lport object
  844. * @fp: The frame pointer
  845. *
  846. * If the error was caused by a resource allocation failure
  847. * then wait for half a second and retry, otherwise retry
  848. * after the e_d_tov time.
  849. */
  850. static void fc_lport_error(struct fc_lport *lport, struct fc_frame *fp)
  851. {
  852. unsigned long delay = 0;
  853. FC_LPORT_DBG(lport, "Error %ld in state %s, retries %d\n",
  854. PTR_ERR(fp), fc_lport_state(lport),
  855. lport->retry_count);
  856. if (!fp || PTR_ERR(fp) == -FC_EX_TIMEOUT) {
  857. /*
  858. * Memory allocation failure, or the exchange timed out.
  859. * Retry after delay
  860. */
  861. if (lport->retry_count < lport->max_retry_count) {
  862. lport->retry_count++;
  863. if (!fp)
  864. delay = msecs_to_jiffies(500);
  865. else
  866. delay = msecs_to_jiffies(lport->e_d_tov);
  867. schedule_delayed_work(&lport->retry_work, delay);
  868. } else {
  869. switch (lport->state) {
  870. case LPORT_ST_DISABLED:
  871. case LPORT_ST_READY:
  872. case LPORT_ST_RESET:
  873. case LPORT_ST_RNN_ID:
  874. case LPORT_ST_RSNN_NN:
  875. case LPORT_ST_RSPN_ID:
  876. case LPORT_ST_RFT_ID:
  877. case LPORT_ST_SCR:
  878. case LPORT_ST_DNS:
  879. case LPORT_ST_FLOGI:
  880. case LPORT_ST_LOGO:
  881. fc_lport_enter_reset(lport);
  882. break;
  883. }
  884. }
  885. }
  886. }
  887. /**
  888. * fc_lport_ns_resp() - Handle response to a name server
  889. * registration exchange
  890. * @sp: current sequence in exchange
  891. * @fp: response frame
  892. * @lp_arg: Fibre Channel host port instance
  893. *
  894. * Locking Note: This function will be called without the lport lock
  895. * held, but it will lock, call an _enter_* function or fc_lport_error
  896. * and then unlock the lport.
  897. */
  898. static void fc_lport_ns_resp(struct fc_seq *sp, struct fc_frame *fp,
  899. void *lp_arg)
  900. {
  901. struct fc_lport *lport = lp_arg;
  902. struct fc_frame_header *fh;
  903. struct fc_ct_hdr *ct;
  904. FC_LPORT_DBG(lport, "Received a ns %s\n", fc_els_resp_type(fp));
  905. if (fp == ERR_PTR(-FC_EX_CLOSED))
  906. return;
  907. mutex_lock(&lport->lp_mutex);
  908. if (lport->state < LPORT_ST_RNN_ID || lport->state > LPORT_ST_RFT_ID) {
  909. FC_LPORT_DBG(lport, "Received a name server response, "
  910. "but in state %s\n", fc_lport_state(lport));
  911. if (IS_ERR(fp))
  912. goto err;
  913. goto out;
  914. }
  915. if (IS_ERR(fp)) {
  916. fc_lport_error(lport, fp);
  917. goto err;
  918. }
  919. fh = fc_frame_header_get(fp);
  920. ct = fc_frame_payload_get(fp, sizeof(*ct));
  921. if (fh && ct && fh->fh_type == FC_TYPE_CT &&
  922. ct->ct_fs_type == FC_FST_DIR &&
  923. ct->ct_fs_subtype == FC_NS_SUBTYPE &&
  924. ntohs(ct->ct_cmd) == FC_FS_ACC)
  925. switch (lport->state) {
  926. case LPORT_ST_RNN_ID:
  927. fc_lport_enter_ns(lport, LPORT_ST_RSNN_NN);
  928. break;
  929. case LPORT_ST_RSNN_NN:
  930. fc_lport_enter_ns(lport, LPORT_ST_RSPN_ID);
  931. break;
  932. case LPORT_ST_RSPN_ID:
  933. fc_lport_enter_ns(lport, LPORT_ST_RFT_ID);
  934. break;
  935. case LPORT_ST_RFT_ID:
  936. fc_lport_enter_scr(lport);
  937. break;
  938. default:
  939. /* should have already been caught by state checks */
  940. break;
  941. }
  942. else
  943. fc_lport_error(lport, fp);
  944. out:
  945. fc_frame_free(fp);
  946. err:
  947. mutex_unlock(&lport->lp_mutex);
  948. }
  949. /**
  950. * fc_lport_scr_resp() - Handle response to State Change Register (SCR) request
  951. * @sp: current sequence in SCR exchange
  952. * @fp: response frame
  953. * @lp_arg: Fibre Channel lport port instance that sent the registration request
  954. *
  955. * Locking Note: This function will be called without the lport lock
  956. * held, but it will lock, call an _enter_* function or fc_lport_error
  957. * and then unlock the lport.
  958. */
  959. static void fc_lport_scr_resp(struct fc_seq *sp, struct fc_frame *fp,
  960. void *lp_arg)
  961. {
  962. struct fc_lport *lport = lp_arg;
  963. u8 op;
  964. FC_LPORT_DBG(lport, "Received a SCR %s\n", fc_els_resp_type(fp));
  965. if (fp == ERR_PTR(-FC_EX_CLOSED))
  966. return;
  967. mutex_lock(&lport->lp_mutex);
  968. if (lport->state != LPORT_ST_SCR) {
  969. FC_LPORT_DBG(lport, "Received a SCR response, but in state "
  970. "%s\n", fc_lport_state(lport));
  971. if (IS_ERR(fp))
  972. goto err;
  973. goto out;
  974. }
  975. if (IS_ERR(fp)) {
  976. fc_lport_error(lport, fp);
  977. goto err;
  978. }
  979. op = fc_frame_payload_op(fp);
  980. if (op == ELS_LS_ACC)
  981. fc_lport_enter_ready(lport);
  982. else
  983. fc_lport_error(lport, fp);
  984. out:
  985. fc_frame_free(fp);
  986. err:
  987. mutex_unlock(&lport->lp_mutex);
  988. }
  989. /**
  990. * fc_lport_enter_scr() - Send a State Change Register (SCR) request
  991. * @lport: Fibre Channel local port to register for state changes
  992. *
  993. * Locking Note: The lport lock is expected to be held before calling
  994. * this routine.
  995. */
  996. static void fc_lport_enter_scr(struct fc_lport *lport)
  997. {
  998. struct fc_frame *fp;
  999. FC_LPORT_DBG(lport, "Entered SCR state from %s state\n",
  1000. fc_lport_state(lport));
  1001. fc_lport_state_enter(lport, LPORT_ST_SCR);
  1002. fp = fc_frame_alloc(lport, sizeof(struct fc_els_scr));
  1003. if (!fp) {
  1004. fc_lport_error(lport, fp);
  1005. return;
  1006. }
  1007. if (!lport->tt.elsct_send(lport, FC_FID_FCTRL, fp, ELS_SCR,
  1008. fc_lport_scr_resp, lport, lport->e_d_tov))
  1009. fc_lport_error(lport, NULL);
  1010. }
  1011. /**
  1012. * fc_lport_enter_ns() - register some object with the name server
  1013. * @lport: Fibre Channel local port to register
  1014. *
  1015. * Locking Note: The lport lock is expected to be held before calling
  1016. * this routine.
  1017. */
  1018. static void fc_lport_enter_ns(struct fc_lport *lport, enum fc_lport_state state)
  1019. {
  1020. struct fc_frame *fp;
  1021. enum fc_ns_req cmd;
  1022. int size = sizeof(struct fc_ct_hdr);
  1023. size_t len;
  1024. FC_LPORT_DBG(lport, "Entered %s state from %s state\n",
  1025. fc_lport_state_names[state],
  1026. fc_lport_state(lport));
  1027. fc_lport_state_enter(lport, state);
  1028. switch (state) {
  1029. case LPORT_ST_RNN_ID:
  1030. cmd = FC_NS_RNN_ID;
  1031. size += sizeof(struct fc_ns_rn_id);
  1032. break;
  1033. case LPORT_ST_RSNN_NN:
  1034. len = strnlen(fc_host_symbolic_name(lport->host), 255);
  1035. /* if there is no symbolic name, skip to RFT_ID */
  1036. if (!len)
  1037. return fc_lport_enter_ns(lport, LPORT_ST_RFT_ID);
  1038. cmd = FC_NS_RSNN_NN;
  1039. size += sizeof(struct fc_ns_rsnn) + len;
  1040. break;
  1041. case LPORT_ST_RSPN_ID:
  1042. len = strnlen(fc_host_symbolic_name(lport->host), 255);
  1043. /* if there is no symbolic name, skip to RFT_ID */
  1044. if (!len)
  1045. return fc_lport_enter_ns(lport, LPORT_ST_RFT_ID);
  1046. cmd = FC_NS_RSPN_ID;
  1047. size += sizeof(struct fc_ns_rspn) + len;
  1048. break;
  1049. case LPORT_ST_RFT_ID:
  1050. cmd = FC_NS_RFT_ID;
  1051. size += sizeof(struct fc_ns_rft);
  1052. break;
  1053. default:
  1054. fc_lport_error(lport, NULL);
  1055. return;
  1056. }
  1057. fp = fc_frame_alloc(lport, size);
  1058. if (!fp) {
  1059. fc_lport_error(lport, fp);
  1060. return;
  1061. }
  1062. if (!lport->tt.elsct_send(lport, FC_FID_DIR_SERV, fp, cmd,
  1063. fc_lport_ns_resp,
  1064. lport, lport->e_d_tov))
  1065. fc_lport_error(lport, fp);
  1066. }
  1067. static struct fc_rport_operations fc_lport_rport_ops = {
  1068. .event_callback = fc_lport_rport_callback,
  1069. };
  1070. /**
  1071. * fc_rport_enter_dns() - Create a rport to the name server
  1072. * @lport: Fibre Channel local port requesting a rport for the name server
  1073. *
  1074. * Locking Note: The lport lock is expected to be held before calling
  1075. * this routine.
  1076. */
  1077. static void fc_lport_enter_dns(struct fc_lport *lport)
  1078. {
  1079. struct fc_rport_priv *rdata;
  1080. FC_LPORT_DBG(lport, "Entered DNS state from %s state\n",
  1081. fc_lport_state(lport));
  1082. fc_lport_state_enter(lport, LPORT_ST_DNS);
  1083. mutex_lock(&lport->disc.disc_mutex);
  1084. rdata = lport->tt.rport_create(lport, FC_FID_DIR_SERV);
  1085. mutex_unlock(&lport->disc.disc_mutex);
  1086. if (!rdata)
  1087. goto err;
  1088. rdata->ops = &fc_lport_rport_ops;
  1089. lport->tt.rport_login(rdata);
  1090. return;
  1091. err:
  1092. fc_lport_error(lport, NULL);
  1093. }
  1094. /**
  1095. * fc_lport_timeout() - Handler for the retry_work timer.
  1096. * @work: The work struct of the fc_lport
  1097. */
  1098. static void fc_lport_timeout(struct work_struct *work)
  1099. {
  1100. struct fc_lport *lport =
  1101. container_of(work, struct fc_lport,
  1102. retry_work.work);
  1103. mutex_lock(&lport->lp_mutex);
  1104. switch (lport->state) {
  1105. case LPORT_ST_DISABLED:
  1106. WARN_ON(1);
  1107. break;
  1108. case LPORT_ST_READY:
  1109. WARN_ON(1);
  1110. break;
  1111. case LPORT_ST_RESET:
  1112. break;
  1113. case LPORT_ST_FLOGI:
  1114. fc_lport_enter_flogi(lport);
  1115. break;
  1116. case LPORT_ST_DNS:
  1117. fc_lport_enter_dns(lport);
  1118. break;
  1119. case LPORT_ST_RNN_ID:
  1120. case LPORT_ST_RSNN_NN:
  1121. case LPORT_ST_RSPN_ID:
  1122. case LPORT_ST_RFT_ID:
  1123. fc_lport_enter_ns(lport, lport->state);
  1124. break;
  1125. case LPORT_ST_SCR:
  1126. fc_lport_enter_scr(lport);
  1127. break;
  1128. case LPORT_ST_LOGO:
  1129. fc_lport_enter_logo(lport);
  1130. break;
  1131. }
  1132. mutex_unlock(&lport->lp_mutex);
  1133. }
  1134. /**
  1135. * fc_lport_logo_resp() - Handle response to LOGO request
  1136. * @sp: current sequence in LOGO exchange
  1137. * @fp: response frame
  1138. * @lp_arg: Fibre Channel lport port instance that sent the LOGO request
  1139. *
  1140. * Locking Note: This function will be called without the lport lock
  1141. * held, but it will lock, call an _enter_* function or fc_lport_error
  1142. * and then unlock the lport.
  1143. */
  1144. void fc_lport_logo_resp(struct fc_seq *sp, struct fc_frame *fp,
  1145. void *lp_arg)
  1146. {
  1147. struct fc_lport *lport = lp_arg;
  1148. u8 op;
  1149. FC_LPORT_DBG(lport, "Received a LOGO %s\n", fc_els_resp_type(fp));
  1150. if (fp == ERR_PTR(-FC_EX_CLOSED))
  1151. return;
  1152. mutex_lock(&lport->lp_mutex);
  1153. if (lport->state != LPORT_ST_LOGO) {
  1154. FC_LPORT_DBG(lport, "Received a LOGO response, but in state "
  1155. "%s\n", fc_lport_state(lport));
  1156. if (IS_ERR(fp))
  1157. goto err;
  1158. goto out;
  1159. }
  1160. if (IS_ERR(fp)) {
  1161. fc_lport_error(lport, fp);
  1162. goto err;
  1163. }
  1164. op = fc_frame_payload_op(fp);
  1165. if (op == ELS_LS_ACC)
  1166. fc_lport_enter_disabled(lport);
  1167. else
  1168. fc_lport_error(lport, fp);
  1169. out:
  1170. fc_frame_free(fp);
  1171. err:
  1172. mutex_unlock(&lport->lp_mutex);
  1173. }
  1174. EXPORT_SYMBOL(fc_lport_logo_resp);
  1175. /**
  1176. * fc_rport_enter_logo() - Logout of the fabric
  1177. * @lport: Fibre Channel local port to be logged out
  1178. *
  1179. * Locking Note: The lport lock is expected to be held before calling
  1180. * this routine.
  1181. */
  1182. static void fc_lport_enter_logo(struct fc_lport *lport)
  1183. {
  1184. struct fc_frame *fp;
  1185. struct fc_els_logo *logo;
  1186. FC_LPORT_DBG(lport, "Entered LOGO state from %s state\n",
  1187. fc_lport_state(lport));
  1188. fc_lport_state_enter(lport, LPORT_ST_LOGO);
  1189. fc_vports_linkchange(lport);
  1190. fp = fc_frame_alloc(lport, sizeof(*logo));
  1191. if (!fp) {
  1192. fc_lport_error(lport, fp);
  1193. return;
  1194. }
  1195. if (!lport->tt.elsct_send(lport, FC_FID_FLOGI, fp, ELS_LOGO,
  1196. fc_lport_logo_resp, lport, lport->e_d_tov))
  1197. fc_lport_error(lport, NULL);
  1198. }
  1199. /**
  1200. * fc_lport_flogi_resp() - Handle response to FLOGI request
  1201. * @sp: current sequence in FLOGI exchange
  1202. * @fp: response frame
  1203. * @lp_arg: Fibre Channel lport port instance that sent the FLOGI request
  1204. *
  1205. * Locking Note: This function will be called without the lport lock
  1206. * held, but it will lock, call an _enter_* function or fc_lport_error
  1207. * and then unlock the lport.
  1208. */
  1209. void fc_lport_flogi_resp(struct fc_seq *sp, struct fc_frame *fp,
  1210. void *lp_arg)
  1211. {
  1212. struct fc_lport *lport = lp_arg;
  1213. struct fc_frame_header *fh;
  1214. struct fc_els_flogi *flp;
  1215. u32 did;
  1216. u16 csp_flags;
  1217. unsigned int r_a_tov;
  1218. unsigned int e_d_tov;
  1219. u16 mfs;
  1220. FC_LPORT_DBG(lport, "Received a FLOGI %s\n", fc_els_resp_type(fp));
  1221. if (fp == ERR_PTR(-FC_EX_CLOSED))
  1222. return;
  1223. mutex_lock(&lport->lp_mutex);
  1224. if (lport->state != LPORT_ST_FLOGI) {
  1225. FC_LPORT_DBG(lport, "Received a FLOGI response, but in state "
  1226. "%s\n", fc_lport_state(lport));
  1227. if (IS_ERR(fp))
  1228. goto err;
  1229. goto out;
  1230. }
  1231. if (IS_ERR(fp)) {
  1232. fc_lport_error(lport, fp);
  1233. goto err;
  1234. }
  1235. fh = fc_frame_header_get(fp);
  1236. did = ntoh24(fh->fh_d_id);
  1237. if (fc_frame_payload_op(fp) == ELS_LS_ACC && did != 0) {
  1238. printk(KERN_INFO "libfc: Assigned FID (%6x) in FLOGI response\n",
  1239. did);
  1240. fc_host_port_id(lport->host) = did;
  1241. flp = fc_frame_payload_get(fp, sizeof(*flp));
  1242. if (flp) {
  1243. mfs = ntohs(flp->fl_csp.sp_bb_data) &
  1244. FC_SP_BB_DATA_MASK;
  1245. if (mfs >= FC_SP_MIN_MAX_PAYLOAD &&
  1246. mfs < lport->mfs)
  1247. lport->mfs = mfs;
  1248. csp_flags = ntohs(flp->fl_csp.sp_features);
  1249. r_a_tov = ntohl(flp->fl_csp.sp_r_a_tov);
  1250. e_d_tov = ntohl(flp->fl_csp.sp_e_d_tov);
  1251. if (csp_flags & FC_SP_FT_EDTR)
  1252. e_d_tov /= 1000000;
  1253. lport->npiv_enabled = !!(csp_flags & FC_SP_FT_NPIV_ACC);
  1254. if ((csp_flags & FC_SP_FT_FPORT) == 0) {
  1255. if (e_d_tov > lport->e_d_tov)
  1256. lport->e_d_tov = e_d_tov;
  1257. lport->r_a_tov = 2 * e_d_tov;
  1258. printk(KERN_INFO "libfc: Port (%6x) entered "
  1259. "point to point mode\n", did);
  1260. fc_lport_ptp_setup(lport, ntoh24(fh->fh_s_id),
  1261. get_unaligned_be64(
  1262. &flp->fl_wwpn),
  1263. get_unaligned_be64(
  1264. &flp->fl_wwnn));
  1265. } else {
  1266. lport->e_d_tov = e_d_tov;
  1267. lport->r_a_tov = r_a_tov;
  1268. fc_host_fabric_name(lport->host) =
  1269. get_unaligned_be64(&flp->fl_wwnn);
  1270. fc_lport_enter_dns(lport);
  1271. }
  1272. }
  1273. } else {
  1274. FC_LPORT_DBG(lport, "Bad FLOGI response\n");
  1275. }
  1276. out:
  1277. fc_frame_free(fp);
  1278. err:
  1279. mutex_unlock(&lport->lp_mutex);
  1280. }
  1281. EXPORT_SYMBOL(fc_lport_flogi_resp);
  1282. /**
  1283. * fc_rport_enter_flogi() - Send a FLOGI request to the fabric manager
  1284. * @lport: Fibre Channel local port to be logged in to the fabric
  1285. *
  1286. * Locking Note: The lport lock is expected to be held before calling
  1287. * this routine.
  1288. */
  1289. void fc_lport_enter_flogi(struct fc_lport *lport)
  1290. {
  1291. struct fc_frame *fp;
  1292. FC_LPORT_DBG(lport, "Entered FLOGI state from %s state\n",
  1293. fc_lport_state(lport));
  1294. fc_lport_state_enter(lport, LPORT_ST_FLOGI);
  1295. fp = fc_frame_alloc(lport, sizeof(struct fc_els_flogi));
  1296. if (!fp)
  1297. return fc_lport_error(lport, fp);
  1298. if (!lport->tt.elsct_send(lport, FC_FID_FLOGI, fp,
  1299. lport->vport ? ELS_FDISC : ELS_FLOGI,
  1300. fc_lport_flogi_resp, lport, lport->e_d_tov))
  1301. fc_lport_error(lport, NULL);
  1302. }
  1303. /* Configure a fc_lport */
  1304. int fc_lport_config(struct fc_lport *lport)
  1305. {
  1306. INIT_DELAYED_WORK(&lport->retry_work, fc_lport_timeout);
  1307. mutex_init(&lport->lp_mutex);
  1308. fc_lport_state_enter(lport, LPORT_ST_DISABLED);
  1309. fc_lport_add_fc4_type(lport, FC_TYPE_FCP);
  1310. fc_lport_add_fc4_type(lport, FC_TYPE_CT);
  1311. return 0;
  1312. }
  1313. EXPORT_SYMBOL(fc_lport_config);
  1314. int fc_lport_init(struct fc_lport *lport)
  1315. {
  1316. if (!lport->tt.lport_recv)
  1317. lport->tt.lport_recv = fc_lport_recv_req;
  1318. if (!lport->tt.lport_reset)
  1319. lport->tt.lport_reset = fc_lport_reset;
  1320. fc_host_port_type(lport->host) = FC_PORTTYPE_NPORT;
  1321. fc_host_node_name(lport->host) = lport->wwnn;
  1322. fc_host_port_name(lport->host) = lport->wwpn;
  1323. fc_host_supported_classes(lport->host) = FC_COS_CLASS3;
  1324. memset(fc_host_supported_fc4s(lport->host), 0,
  1325. sizeof(fc_host_supported_fc4s(lport->host)));
  1326. fc_host_supported_fc4s(lport->host)[2] = 1;
  1327. fc_host_supported_fc4s(lport->host)[7] = 1;
  1328. /* This value is also unchanging */
  1329. memset(fc_host_active_fc4s(lport->host), 0,
  1330. sizeof(fc_host_active_fc4s(lport->host)));
  1331. fc_host_active_fc4s(lport->host)[2] = 1;
  1332. fc_host_active_fc4s(lport->host)[7] = 1;
  1333. fc_host_maxframe_size(lport->host) = lport->mfs;
  1334. fc_host_supported_speeds(lport->host) = 0;
  1335. if (lport->link_supported_speeds & FC_PORTSPEED_1GBIT)
  1336. fc_host_supported_speeds(lport->host) |= FC_PORTSPEED_1GBIT;
  1337. if (lport->link_supported_speeds & FC_PORTSPEED_10GBIT)
  1338. fc_host_supported_speeds(lport->host) |= FC_PORTSPEED_10GBIT;
  1339. return 0;
  1340. }
  1341. EXPORT_SYMBOL(fc_lport_init);