fc_lport.c 42 KB

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