fc_lport.c 42 KB

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