fc_lport.c 43 KB

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