fc_lport.c 42 KB

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