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