fc_lport.c 42 KB

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