fc_lport.c 42 KB

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