fc_lport.c 41 KB

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