fc_lport.c 42 KB

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