fc_lport.c 43 KB

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