fc_lport.c 40 KB

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