fc_lport.c 41 KB

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