fc_lport.c 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902
  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. * HIERARCHY
  35. *
  36. * The following hierarchy 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 hierarchy-
  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 because 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, successful 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 <linux/delay.h>
  89. #include <linux/slab.h>
  90. #include <asm/unaligned.h>
  91. #include <scsi/fc/fc_gs.h>
  92. #include <scsi/libfc.h>
  93. #include <scsi/fc_encode.h>
  94. #include <linux/scatterlist.h>
  95. #include "fc_libfc.h"
  96. /* Fabric IDs to use for point-to-point mode, chosen on whims. */
  97. #define FC_LOCAL_PTP_FID_LO 0x010101
  98. #define FC_LOCAL_PTP_FID_HI 0x010102
  99. #define DNS_DELAY 3 /* Discovery delay after RSCN (in seconds)*/
  100. static void fc_lport_error(struct fc_lport *, struct fc_frame *);
  101. static void fc_lport_enter_reset(struct fc_lport *);
  102. static void fc_lport_enter_flogi(struct fc_lport *);
  103. static void fc_lport_enter_dns(struct fc_lport *);
  104. static void fc_lport_enter_ns(struct fc_lport *, enum fc_lport_state);
  105. static void fc_lport_enter_scr(struct fc_lport *);
  106. static void fc_lport_enter_ready(struct fc_lport *);
  107. static void fc_lport_enter_logo(struct fc_lport *);
  108. static const char *fc_lport_state_names[] = {
  109. [LPORT_ST_DISABLED] = "disabled",
  110. [LPORT_ST_FLOGI] = "FLOGI",
  111. [LPORT_ST_DNS] = "dNS",
  112. [LPORT_ST_RNN_ID] = "RNN_ID",
  113. [LPORT_ST_RSNN_NN] = "RSNN_NN",
  114. [LPORT_ST_RSPN_ID] = "RSPN_ID",
  115. [LPORT_ST_RFT_ID] = "RFT_ID",
  116. [LPORT_ST_RFF_ID] = "RFF_ID",
  117. [LPORT_ST_SCR] = "SCR",
  118. [LPORT_ST_READY] = "Ready",
  119. [LPORT_ST_LOGO] = "LOGO",
  120. [LPORT_ST_RESET] = "reset",
  121. };
  122. /**
  123. * struct fc_bsg_info - FC Passthrough managemet structure
  124. * @job: The passthrough job
  125. * @lport: The local port to pass through a command
  126. * @rsp_code: The expected response code
  127. * @sg: job->reply_payload.sg_list
  128. * @nents: job->reply_payload.sg_cnt
  129. * @offset: The offset into the response data
  130. */
  131. struct fc_bsg_info {
  132. struct fc_bsg_job *job;
  133. struct fc_lport *lport;
  134. u16 rsp_code;
  135. struct scatterlist *sg;
  136. u32 nents;
  137. size_t offset;
  138. };
  139. /**
  140. * fc_frame_drop() - Dummy frame handler
  141. * @lport: The local port the frame was received on
  142. * @fp: The received frame
  143. */
  144. static int fc_frame_drop(struct fc_lport *lport, struct fc_frame *fp)
  145. {
  146. fc_frame_free(fp);
  147. return 0;
  148. }
  149. /**
  150. * fc_lport_rport_callback() - Event handler for rport events
  151. * @lport: The lport which is receiving the event
  152. * @rdata: private remote port data
  153. * @event: The event that occurred
  154. *
  155. * Locking Note: The rport lock should not be held when calling
  156. * this function.
  157. */
  158. static void fc_lport_rport_callback(struct fc_lport *lport,
  159. struct fc_rport_priv *rdata,
  160. enum fc_rport_event event)
  161. {
  162. FC_LPORT_DBG(lport, "Received a %d event for port (%6.6x)\n", event,
  163. rdata->ids.port_id);
  164. mutex_lock(&lport->lp_mutex);
  165. switch (event) {
  166. case RPORT_EV_READY:
  167. if (lport->state == LPORT_ST_DNS) {
  168. lport->dns_rdata = rdata;
  169. fc_lport_enter_ns(lport, LPORT_ST_RNN_ID);
  170. } else {
  171. FC_LPORT_DBG(lport, "Received an READY event "
  172. "on port (%6.6x) for the directory "
  173. "server, but the lport is not "
  174. "in the DNS state, it's in the "
  175. "%d state", rdata->ids.port_id,
  176. lport->state);
  177. lport->tt.rport_logoff(rdata);
  178. }
  179. break;
  180. case RPORT_EV_LOGO:
  181. case RPORT_EV_FAILED:
  182. case RPORT_EV_STOP:
  183. lport->dns_rdata = NULL;
  184. break;
  185. case RPORT_EV_NONE:
  186. break;
  187. }
  188. mutex_unlock(&lport->lp_mutex);
  189. }
  190. /**
  191. * fc_lport_state() - Return a string which represents the lport's state
  192. * @lport: The lport whose state is to converted to a string
  193. */
  194. static const char *fc_lport_state(struct fc_lport *lport)
  195. {
  196. const char *cp;
  197. cp = fc_lport_state_names[lport->state];
  198. if (!cp)
  199. cp = "unknown";
  200. return cp;
  201. }
  202. /**
  203. * fc_lport_ptp_setup() - Create an rport for point-to-point mode
  204. * @lport: The lport to attach the ptp rport to
  205. * @remote_fid: The FID of the ptp rport
  206. * @remote_wwpn: The WWPN of the ptp rport
  207. * @remote_wwnn: The WWNN of the ptp rport
  208. */
  209. static void fc_lport_ptp_setup(struct fc_lport *lport,
  210. u32 remote_fid, u64 remote_wwpn,
  211. u64 remote_wwnn)
  212. {
  213. mutex_lock(&lport->disc.disc_mutex);
  214. if (lport->ptp_rdata) {
  215. lport->tt.rport_logoff(lport->ptp_rdata);
  216. kref_put(&lport->ptp_rdata->kref, lport->tt.rport_destroy);
  217. }
  218. lport->ptp_rdata = lport->tt.rport_create(lport, remote_fid);
  219. kref_get(&lport->ptp_rdata->kref);
  220. lport->ptp_rdata->ids.port_name = remote_wwpn;
  221. lport->ptp_rdata->ids.node_name = remote_wwnn;
  222. mutex_unlock(&lport->disc.disc_mutex);
  223. lport->tt.rport_login(lport->ptp_rdata);
  224. fc_lport_enter_ready(lport);
  225. }
  226. /**
  227. * fc_get_host_port_state() - Return the port state of the given Scsi_Host
  228. * @shost: The SCSI host whose port state is to be determined
  229. */
  230. void fc_get_host_port_state(struct Scsi_Host *shost)
  231. {
  232. struct fc_lport *lport = shost_priv(shost);
  233. mutex_lock(&lport->lp_mutex);
  234. if (!lport->link_up)
  235. fc_host_port_state(shost) = FC_PORTSTATE_LINKDOWN;
  236. else
  237. switch (lport->state) {
  238. case LPORT_ST_READY:
  239. fc_host_port_state(shost) = FC_PORTSTATE_ONLINE;
  240. break;
  241. default:
  242. fc_host_port_state(shost) = FC_PORTSTATE_OFFLINE;
  243. }
  244. mutex_unlock(&lport->lp_mutex);
  245. }
  246. EXPORT_SYMBOL(fc_get_host_port_state);
  247. /**
  248. * fc_get_host_speed() - Return the speed of the given Scsi_Host
  249. * @shost: The SCSI host whose port speed is to be determined
  250. */
  251. void fc_get_host_speed(struct Scsi_Host *shost)
  252. {
  253. struct fc_lport *lport = shost_priv(shost);
  254. fc_host_speed(shost) = lport->link_speed;
  255. }
  256. EXPORT_SYMBOL(fc_get_host_speed);
  257. /**
  258. * fc_get_host_stats() - Return the Scsi_Host's statistics
  259. * @shost: The SCSI host whose statistics are to be returned
  260. */
  261. struct fc_host_statistics *fc_get_host_stats(struct Scsi_Host *shost)
  262. {
  263. struct fc_host_statistics *fcoe_stats;
  264. struct fc_lport *lport = shost_priv(shost);
  265. struct timespec v0, v1;
  266. unsigned int cpu;
  267. u64 fcp_in_bytes = 0;
  268. u64 fcp_out_bytes = 0;
  269. fcoe_stats = &lport->host_stats;
  270. memset(fcoe_stats, 0, sizeof(struct fc_host_statistics));
  271. jiffies_to_timespec(jiffies, &v0);
  272. jiffies_to_timespec(lport->boot_time, &v1);
  273. fcoe_stats->seconds_since_last_reset = (v0.tv_sec - v1.tv_sec);
  274. for_each_possible_cpu(cpu) {
  275. struct fcoe_dev_stats *stats;
  276. stats = per_cpu_ptr(lport->dev_stats, cpu);
  277. fcoe_stats->tx_frames += stats->TxFrames;
  278. fcoe_stats->tx_words += stats->TxWords;
  279. fcoe_stats->rx_frames += stats->RxFrames;
  280. fcoe_stats->rx_words += stats->RxWords;
  281. fcoe_stats->error_frames += stats->ErrorFrames;
  282. fcoe_stats->invalid_crc_count += stats->InvalidCRCCount;
  283. fcoe_stats->fcp_input_requests += stats->InputRequests;
  284. fcoe_stats->fcp_output_requests += stats->OutputRequests;
  285. fcoe_stats->fcp_control_requests += stats->ControlRequests;
  286. fcp_in_bytes += stats->InputBytes;
  287. fcp_out_bytes += stats->OutputBytes;
  288. fcoe_stats->link_failure_count += stats->LinkFailureCount;
  289. }
  290. fcoe_stats->fcp_input_megabytes = div_u64(fcp_in_bytes, 1000000);
  291. fcoe_stats->fcp_output_megabytes = div_u64(fcp_out_bytes, 1000000);
  292. fcoe_stats->lip_count = -1;
  293. fcoe_stats->nos_count = -1;
  294. fcoe_stats->loss_of_sync_count = -1;
  295. fcoe_stats->loss_of_signal_count = -1;
  296. fcoe_stats->prim_seq_protocol_err_count = -1;
  297. fcoe_stats->dumped_frames = -1;
  298. return fcoe_stats;
  299. }
  300. EXPORT_SYMBOL(fc_get_host_stats);
  301. /**
  302. * fc_lport_flogi_fill() - Fill in FLOGI command for request
  303. * @lport: The local port the FLOGI is for
  304. * @flogi: The FLOGI command
  305. * @op: The opcode
  306. */
  307. static void fc_lport_flogi_fill(struct fc_lport *lport,
  308. struct fc_els_flogi *flogi,
  309. unsigned int op)
  310. {
  311. struct fc_els_csp *sp;
  312. struct fc_els_cssp *cp;
  313. memset(flogi, 0, sizeof(*flogi));
  314. flogi->fl_cmd = (u8) op;
  315. put_unaligned_be64(lport->wwpn, &flogi->fl_wwpn);
  316. put_unaligned_be64(lport->wwnn, &flogi->fl_wwnn);
  317. sp = &flogi->fl_csp;
  318. sp->sp_hi_ver = 0x20;
  319. sp->sp_lo_ver = 0x20;
  320. sp->sp_bb_cred = htons(10); /* this gets set by gateway */
  321. sp->sp_bb_data = htons((u16) lport->mfs);
  322. cp = &flogi->fl_cssp[3 - 1]; /* class 3 parameters */
  323. cp->cp_class = htons(FC_CPC_VALID | FC_CPC_SEQ);
  324. if (op != ELS_FLOGI) {
  325. sp->sp_features = htons(FC_SP_FT_CIRO);
  326. sp->sp_tot_seq = htons(255); /* seq. we accept */
  327. sp->sp_rel_off = htons(0x1f);
  328. sp->sp_e_d_tov = htonl(lport->e_d_tov);
  329. cp->cp_rdfs = htons((u16) lport->mfs);
  330. cp->cp_con_seq = htons(255);
  331. cp->cp_open_seq = 1;
  332. }
  333. }
  334. /**
  335. * fc_lport_add_fc4_type() - Add a supported FC-4 type to a local port
  336. * @lport: The local port to add a new FC-4 type to
  337. * @type: The new FC-4 type
  338. */
  339. static void fc_lport_add_fc4_type(struct fc_lport *lport, enum fc_fh_type type)
  340. {
  341. __be32 *mp;
  342. mp = &lport->fcts.ff_type_map[type / FC_NS_BPW];
  343. *mp = htonl(ntohl(*mp) | 1UL << (type % FC_NS_BPW));
  344. }
  345. /**
  346. * fc_lport_recv_rlir_req() - Handle received Registered Link Incident Report.
  347. * @lport: Fibre Channel local port receiving the RLIR
  348. * @fp: The RLIR request frame
  349. *
  350. * Locking Note: The lport lock is expected to be held before calling
  351. * this function.
  352. */
  353. static void fc_lport_recv_rlir_req(struct fc_lport *lport, struct fc_frame *fp)
  354. {
  355. FC_LPORT_DBG(lport, "Received RLIR request while in state %s\n",
  356. fc_lport_state(lport));
  357. lport->tt.seq_els_rsp_send(fp, ELS_LS_ACC, NULL);
  358. fc_frame_free(fp);
  359. }
  360. /**
  361. * fc_lport_recv_echo_req() - Handle received ECHO request
  362. * @lport: The local port receiving the ECHO
  363. * @fp: ECHO request frame
  364. *
  365. * Locking Note: The lport lock is expected to be held before calling
  366. * this function.
  367. */
  368. static void fc_lport_recv_echo_req(struct fc_lport *lport,
  369. struct fc_frame *in_fp)
  370. {
  371. struct fc_frame *fp;
  372. unsigned int len;
  373. void *pp;
  374. void *dp;
  375. FC_LPORT_DBG(lport, "Received ECHO request while in state %s\n",
  376. fc_lport_state(lport));
  377. len = fr_len(in_fp) - sizeof(struct fc_frame_header);
  378. pp = fc_frame_payload_get(in_fp, len);
  379. if (len < sizeof(__be32))
  380. len = sizeof(__be32);
  381. fp = fc_frame_alloc(lport, len);
  382. if (fp) {
  383. dp = fc_frame_payload_get(fp, len);
  384. memcpy(dp, pp, len);
  385. *((__be32 *)dp) = htonl(ELS_LS_ACC << 24);
  386. fc_fill_reply_hdr(fp, in_fp, FC_RCTL_ELS_REP, 0);
  387. lport->tt.frame_send(lport, fp);
  388. }
  389. fc_frame_free(in_fp);
  390. }
  391. /**
  392. * fc_lport_recv_rnid_req() - Handle received Request Node ID data request
  393. * @lport: The local port receiving the RNID
  394. * @fp: The RNID request frame
  395. *
  396. * Locking Note: The lport lock is expected to be held before calling
  397. * this function.
  398. */
  399. static void fc_lport_recv_rnid_req(struct fc_lport *lport,
  400. struct fc_frame *in_fp)
  401. {
  402. struct fc_frame *fp;
  403. struct fc_els_rnid *req;
  404. struct {
  405. struct fc_els_rnid_resp rnid;
  406. struct fc_els_rnid_cid cid;
  407. struct fc_els_rnid_gen gen;
  408. } *rp;
  409. struct fc_seq_els_data rjt_data;
  410. u8 fmt;
  411. size_t len;
  412. FC_LPORT_DBG(lport, "Received RNID request while in state %s\n",
  413. fc_lport_state(lport));
  414. req = fc_frame_payload_get(in_fp, sizeof(*req));
  415. if (!req) {
  416. rjt_data.reason = ELS_RJT_LOGIC;
  417. rjt_data.explan = ELS_EXPL_NONE;
  418. lport->tt.seq_els_rsp_send(in_fp, ELS_LS_RJT, &rjt_data);
  419. } else {
  420. fmt = req->rnid_fmt;
  421. len = sizeof(*rp);
  422. if (fmt != ELS_RNIDF_GEN ||
  423. ntohl(lport->rnid_gen.rnid_atype) == 0) {
  424. fmt = ELS_RNIDF_NONE; /* nothing to provide */
  425. len -= sizeof(rp->gen);
  426. }
  427. fp = fc_frame_alloc(lport, len);
  428. if (fp) {
  429. rp = fc_frame_payload_get(fp, len);
  430. memset(rp, 0, len);
  431. rp->rnid.rnid_cmd = ELS_LS_ACC;
  432. rp->rnid.rnid_fmt = fmt;
  433. rp->rnid.rnid_cid_len = sizeof(rp->cid);
  434. rp->cid.rnid_wwpn = htonll(lport->wwpn);
  435. rp->cid.rnid_wwnn = htonll(lport->wwnn);
  436. if (fmt == ELS_RNIDF_GEN) {
  437. rp->rnid.rnid_sid_len = sizeof(rp->gen);
  438. memcpy(&rp->gen, &lport->rnid_gen,
  439. sizeof(rp->gen));
  440. }
  441. fc_fill_reply_hdr(fp, in_fp, FC_RCTL_ELS_REP, 0);
  442. lport->tt.frame_send(lport, fp);
  443. }
  444. }
  445. fc_frame_free(in_fp);
  446. }
  447. /**
  448. * fc_lport_recv_logo_req() - Handle received fabric LOGO request
  449. * @lport: The local port receiving the LOGO
  450. * @fp: The LOGO request frame
  451. *
  452. * Locking Note: The lport lock is exected to be held before calling
  453. * this function.
  454. */
  455. static void fc_lport_recv_logo_req(struct fc_lport *lport, struct fc_frame *fp)
  456. {
  457. lport->tt.seq_els_rsp_send(fp, ELS_LS_ACC, NULL);
  458. fc_lport_enter_reset(lport);
  459. fc_frame_free(fp);
  460. }
  461. /**
  462. * fc_fabric_login() - Start the lport state machine
  463. * @lport: The local port that should log into the fabric
  464. *
  465. * Locking Note: This function should not be called
  466. * with the lport lock held.
  467. */
  468. int fc_fabric_login(struct fc_lport *lport)
  469. {
  470. int rc = -1;
  471. mutex_lock(&lport->lp_mutex);
  472. if (lport->state == LPORT_ST_DISABLED ||
  473. lport->state == LPORT_ST_LOGO) {
  474. fc_lport_state_enter(lport, LPORT_ST_RESET);
  475. fc_lport_enter_reset(lport);
  476. rc = 0;
  477. }
  478. mutex_unlock(&lport->lp_mutex);
  479. return rc;
  480. }
  481. EXPORT_SYMBOL(fc_fabric_login);
  482. /**
  483. * __fc_linkup() - Handler for transport linkup events
  484. * @lport: The lport whose link is up
  485. *
  486. * Locking: must be called with the lp_mutex held
  487. */
  488. void __fc_linkup(struct fc_lport *lport)
  489. {
  490. if (!lport->link_up) {
  491. lport->link_up = 1;
  492. if (lport->state == LPORT_ST_RESET)
  493. fc_lport_enter_flogi(lport);
  494. }
  495. }
  496. /**
  497. * fc_linkup() - Handler for transport linkup events
  498. * @lport: The local port whose link is up
  499. */
  500. void fc_linkup(struct fc_lport *lport)
  501. {
  502. printk(KERN_INFO "host%d: libfc: Link up on port (%6.6x)\n",
  503. lport->host->host_no, lport->port_id);
  504. mutex_lock(&lport->lp_mutex);
  505. __fc_linkup(lport);
  506. mutex_unlock(&lport->lp_mutex);
  507. }
  508. EXPORT_SYMBOL(fc_linkup);
  509. /**
  510. * __fc_linkdown() - Handler for transport linkdown events
  511. * @lport: The lport whose link is down
  512. *
  513. * Locking: must be called with the lp_mutex held
  514. */
  515. void __fc_linkdown(struct fc_lport *lport)
  516. {
  517. if (lport->link_up) {
  518. lport->link_up = 0;
  519. fc_lport_enter_reset(lport);
  520. lport->tt.fcp_cleanup(lport);
  521. }
  522. }
  523. /**
  524. * fc_linkdown() - Handler for transport linkdown events
  525. * @lport: The local port whose link is down
  526. */
  527. void fc_linkdown(struct fc_lport *lport)
  528. {
  529. printk(KERN_INFO "host%d: libfc: Link down on port (%6.6x)\n",
  530. lport->host->host_no, lport->port_id);
  531. mutex_lock(&lport->lp_mutex);
  532. __fc_linkdown(lport);
  533. mutex_unlock(&lport->lp_mutex);
  534. }
  535. EXPORT_SYMBOL(fc_linkdown);
  536. /**
  537. * fc_fabric_logoff() - Logout of the fabric
  538. * @lport: The local port to logoff the fabric
  539. *
  540. * Return value:
  541. * 0 for success, -1 for failure
  542. */
  543. int fc_fabric_logoff(struct fc_lport *lport)
  544. {
  545. lport->tt.disc_stop_final(lport);
  546. mutex_lock(&lport->lp_mutex);
  547. if (lport->dns_rdata)
  548. lport->tt.rport_logoff(lport->dns_rdata);
  549. mutex_unlock(&lport->lp_mutex);
  550. lport->tt.rport_flush_queue();
  551. mutex_lock(&lport->lp_mutex);
  552. fc_lport_enter_logo(lport);
  553. mutex_unlock(&lport->lp_mutex);
  554. cancel_delayed_work_sync(&lport->retry_work);
  555. return 0;
  556. }
  557. EXPORT_SYMBOL(fc_fabric_logoff);
  558. /**
  559. * fc_lport_destroy() - Unregister a fc_lport
  560. * @lport: The local port to unregister
  561. *
  562. * Note:
  563. * exit routine for fc_lport instance
  564. * clean-up all the allocated memory
  565. * and free up other system resources.
  566. *
  567. */
  568. int fc_lport_destroy(struct fc_lport *lport)
  569. {
  570. mutex_lock(&lport->lp_mutex);
  571. lport->state = LPORT_ST_DISABLED;
  572. lport->link_up = 0;
  573. lport->tt.frame_send = fc_frame_drop;
  574. mutex_unlock(&lport->lp_mutex);
  575. lport->tt.fcp_abort_io(lport);
  576. lport->tt.disc_stop_final(lport);
  577. lport->tt.exch_mgr_reset(lport, 0, 0);
  578. fc_fc4_del_lport(lport);
  579. return 0;
  580. }
  581. EXPORT_SYMBOL(fc_lport_destroy);
  582. /**
  583. * fc_set_mfs() - Set the maximum frame size for a local port
  584. * @lport: The local port to set the MFS for
  585. * @mfs: The new MFS
  586. */
  587. int fc_set_mfs(struct fc_lport *lport, u32 mfs)
  588. {
  589. unsigned int old_mfs;
  590. int rc = -EINVAL;
  591. mutex_lock(&lport->lp_mutex);
  592. old_mfs = lport->mfs;
  593. if (mfs >= FC_MIN_MAX_FRAME) {
  594. mfs &= ~3;
  595. if (mfs > FC_MAX_FRAME)
  596. mfs = FC_MAX_FRAME;
  597. mfs -= sizeof(struct fc_frame_header);
  598. lport->mfs = mfs;
  599. rc = 0;
  600. }
  601. if (!rc && mfs < old_mfs)
  602. fc_lport_enter_reset(lport);
  603. mutex_unlock(&lport->lp_mutex);
  604. return rc;
  605. }
  606. EXPORT_SYMBOL(fc_set_mfs);
  607. /**
  608. * fc_lport_disc_callback() - Callback for discovery events
  609. * @lport: The local port receiving the event
  610. * @event: The discovery event
  611. */
  612. void fc_lport_disc_callback(struct fc_lport *lport, enum fc_disc_event event)
  613. {
  614. switch (event) {
  615. case DISC_EV_SUCCESS:
  616. FC_LPORT_DBG(lport, "Discovery succeeded\n");
  617. break;
  618. case DISC_EV_FAILED:
  619. printk(KERN_ERR "host%d: libfc: "
  620. "Discovery failed for port (%6.6x)\n",
  621. lport->host->host_no, lport->port_id);
  622. mutex_lock(&lport->lp_mutex);
  623. fc_lport_enter_reset(lport);
  624. mutex_unlock(&lport->lp_mutex);
  625. break;
  626. case DISC_EV_NONE:
  627. WARN_ON(1);
  628. break;
  629. }
  630. }
  631. /**
  632. * fc_rport_enter_ready() - Enter the ready state and start discovery
  633. * @lport: The local port that is ready
  634. *
  635. * Locking Note: The lport lock is expected to be held before calling
  636. * this routine.
  637. */
  638. static void fc_lport_enter_ready(struct fc_lport *lport)
  639. {
  640. FC_LPORT_DBG(lport, "Entered READY from state %s\n",
  641. fc_lport_state(lport));
  642. fc_lport_state_enter(lport, LPORT_ST_READY);
  643. if (lport->vport)
  644. fc_vport_set_state(lport->vport, FC_VPORT_ACTIVE);
  645. fc_vports_linkchange(lport);
  646. if (!lport->ptp_rdata)
  647. lport->tt.disc_start(fc_lport_disc_callback, lport);
  648. }
  649. /**
  650. * fc_lport_set_port_id() - set the local port Port ID
  651. * @lport: The local port which will have its Port ID set.
  652. * @port_id: The new port ID.
  653. * @fp: The frame containing the incoming request, or NULL.
  654. *
  655. * Locking Note: The lport lock is expected to be held before calling
  656. * this function.
  657. */
  658. static void fc_lport_set_port_id(struct fc_lport *lport, u32 port_id,
  659. struct fc_frame *fp)
  660. {
  661. if (port_id)
  662. printk(KERN_INFO "host%d: Assigned Port ID %6.6x\n",
  663. lport->host->host_no, port_id);
  664. lport->port_id = port_id;
  665. /* Update the fc_host */
  666. fc_host_port_id(lport->host) = port_id;
  667. if (lport->tt.lport_set_port_id)
  668. lport->tt.lport_set_port_id(lport, port_id, fp);
  669. }
  670. /**
  671. * fc_lport_set_port_id() - set the local port Port ID for point-to-multipoint
  672. * @lport: The local port which will have its Port ID set.
  673. * @port_id: The new port ID.
  674. *
  675. * Called by the lower-level driver when transport sets the local port_id.
  676. * This is used in VN_port to VN_port mode for FCoE, and causes FLOGI and
  677. * discovery to be skipped.
  678. */
  679. void fc_lport_set_local_id(struct fc_lport *lport, u32 port_id)
  680. {
  681. mutex_lock(&lport->lp_mutex);
  682. fc_lport_set_port_id(lport, port_id, NULL);
  683. switch (lport->state) {
  684. case LPORT_ST_RESET:
  685. case LPORT_ST_FLOGI:
  686. if (port_id)
  687. fc_lport_enter_ready(lport);
  688. break;
  689. default:
  690. break;
  691. }
  692. mutex_unlock(&lport->lp_mutex);
  693. }
  694. EXPORT_SYMBOL(fc_lport_set_local_id);
  695. /**
  696. * fc_lport_recv_flogi_req() - Receive a FLOGI request
  697. * @lport: The local port that received the request
  698. * @rx_fp: The FLOGI frame
  699. *
  700. * A received FLOGI request indicates a point-to-point connection.
  701. * Accept it with the common service parameters indicating our N port.
  702. * Set up to do a PLOGI if we have the higher-number WWPN.
  703. *
  704. * Locking Note: The lport lock is expected to be held before calling
  705. * this function.
  706. */
  707. static void fc_lport_recv_flogi_req(struct fc_lport *lport,
  708. struct fc_frame *rx_fp)
  709. {
  710. struct fc_frame *fp;
  711. struct fc_frame_header *fh;
  712. struct fc_els_flogi *flp;
  713. struct fc_els_flogi *new_flp;
  714. u64 remote_wwpn;
  715. u32 remote_fid;
  716. u32 local_fid;
  717. FC_LPORT_DBG(lport, "Received FLOGI request while in state %s\n",
  718. fc_lport_state(lport));
  719. remote_fid = fc_frame_sid(rx_fp);
  720. flp = fc_frame_payload_get(rx_fp, sizeof(*flp));
  721. if (!flp)
  722. goto out;
  723. remote_wwpn = get_unaligned_be64(&flp->fl_wwpn);
  724. if (remote_wwpn == lport->wwpn) {
  725. printk(KERN_WARNING "host%d: libfc: Received FLOGI from port "
  726. "with same WWPN %16.16llx\n",
  727. lport->host->host_no, remote_wwpn);
  728. goto out;
  729. }
  730. FC_LPORT_DBG(lport, "FLOGI from port WWPN %16.16llx\n", remote_wwpn);
  731. /*
  732. * XXX what is the right thing to do for FIDs?
  733. * The originator might expect our S_ID to be 0xfffffe.
  734. * But if so, both of us could end up with the same FID.
  735. */
  736. local_fid = FC_LOCAL_PTP_FID_LO;
  737. if (remote_wwpn < lport->wwpn) {
  738. local_fid = FC_LOCAL_PTP_FID_HI;
  739. if (!remote_fid || remote_fid == local_fid)
  740. remote_fid = FC_LOCAL_PTP_FID_LO;
  741. } else if (!remote_fid) {
  742. remote_fid = FC_LOCAL_PTP_FID_HI;
  743. }
  744. fc_lport_set_port_id(lport, local_fid, rx_fp);
  745. fp = fc_frame_alloc(lport, sizeof(*flp));
  746. if (fp) {
  747. new_flp = fc_frame_payload_get(fp, sizeof(*flp));
  748. fc_lport_flogi_fill(lport, new_flp, ELS_FLOGI);
  749. new_flp->fl_cmd = (u8) ELS_LS_ACC;
  750. /*
  751. * Send the response. If this fails, the originator should
  752. * repeat the sequence.
  753. */
  754. fc_fill_reply_hdr(fp, rx_fp, FC_RCTL_ELS_REP, 0);
  755. fh = fc_frame_header_get(fp);
  756. hton24(fh->fh_s_id, local_fid);
  757. hton24(fh->fh_d_id, remote_fid);
  758. lport->tt.frame_send(lport, fp);
  759. } else {
  760. fc_lport_error(lport, fp);
  761. }
  762. fc_lport_ptp_setup(lport, remote_fid, remote_wwpn,
  763. get_unaligned_be64(&flp->fl_wwnn));
  764. out:
  765. fc_frame_free(rx_fp);
  766. }
  767. /**
  768. * fc_lport_recv_els_req() - The generic lport ELS request handler
  769. * @lport: The local port that received the request
  770. * @fp: The request frame
  771. *
  772. * This function will see if the lport handles the request or
  773. * if an rport should handle the request.
  774. *
  775. * Locking Note: This function should not be called with the lport
  776. * lock held because it will grab the lock.
  777. */
  778. static void fc_lport_recv_els_req(struct fc_lport *lport,
  779. struct fc_frame *fp)
  780. {
  781. void (*recv)(struct fc_lport *, struct fc_frame *);
  782. mutex_lock(&lport->lp_mutex);
  783. /*
  784. * Handle special ELS cases like FLOGI, LOGO, and
  785. * RSCN here. These don't require a session.
  786. * Even if we had a session, it might not be ready.
  787. */
  788. if (!lport->link_up)
  789. fc_frame_free(fp);
  790. else {
  791. /*
  792. * Check opcode.
  793. */
  794. recv = lport->tt.rport_recv_req;
  795. switch (fc_frame_payload_op(fp)) {
  796. case ELS_FLOGI:
  797. if (!lport->point_to_multipoint)
  798. recv = fc_lport_recv_flogi_req;
  799. break;
  800. case ELS_LOGO:
  801. if (fc_frame_sid(fp) == FC_FID_FLOGI)
  802. recv = fc_lport_recv_logo_req;
  803. break;
  804. case ELS_RSCN:
  805. recv = lport->tt.disc_recv_req;
  806. break;
  807. case ELS_ECHO:
  808. recv = fc_lport_recv_echo_req;
  809. break;
  810. case ELS_RLIR:
  811. recv = fc_lport_recv_rlir_req;
  812. break;
  813. case ELS_RNID:
  814. recv = fc_lport_recv_rnid_req;
  815. break;
  816. }
  817. recv(lport, fp);
  818. }
  819. mutex_unlock(&lport->lp_mutex);
  820. }
  821. static int fc_lport_els_prli(struct fc_rport_priv *rdata, u32 spp_len,
  822. const struct fc_els_spp *spp_in,
  823. struct fc_els_spp *spp_out)
  824. {
  825. return FC_SPP_RESP_INVL;
  826. }
  827. struct fc4_prov fc_lport_els_prov = {
  828. .prli = fc_lport_els_prli,
  829. .recv = fc_lport_recv_els_req,
  830. };
  831. /**
  832. * fc_lport_recv_req() - The generic lport request handler
  833. * @lport: The lport that received the request
  834. * @fp: The frame the request is in
  835. *
  836. * Locking Note: This function should not be called with the lport
  837. * lock held because it may grab the lock.
  838. */
  839. static void fc_lport_recv_req(struct fc_lport *lport,
  840. struct fc_frame *fp)
  841. {
  842. struct fc_frame_header *fh = fc_frame_header_get(fp);
  843. struct fc_seq *sp = fr_seq(fp);
  844. struct fc4_prov *prov;
  845. /*
  846. * Use RCU read lock and module_lock to be sure module doesn't
  847. * deregister and get unloaded while we're calling it.
  848. * try_module_get() is inlined and accepts a NULL parameter.
  849. * Only ELSes and FCP target ops should come through here.
  850. * The locking is unfortunate, and a better scheme is being sought.
  851. */
  852. rcu_read_lock();
  853. if (fh->fh_type >= FC_FC4_PROV_SIZE)
  854. goto drop;
  855. prov = rcu_dereference(fc_passive_prov[fh->fh_type]);
  856. if (!prov || !try_module_get(prov->module))
  857. goto drop;
  858. rcu_read_unlock();
  859. prov->recv(lport, fp);
  860. module_put(prov->module);
  861. return;
  862. drop:
  863. rcu_read_unlock();
  864. FC_LPORT_DBG(lport, "dropping unexpected frame type %x\n", fh->fh_type);
  865. fc_frame_free(fp);
  866. lport->tt.exch_done(sp);
  867. }
  868. /**
  869. * fc_lport_reset() - Reset a local port
  870. * @lport: The local port which should be reset
  871. *
  872. * Locking Note: This functions should not be called with the
  873. * lport lock held.
  874. */
  875. int fc_lport_reset(struct fc_lport *lport)
  876. {
  877. cancel_delayed_work_sync(&lport->retry_work);
  878. mutex_lock(&lport->lp_mutex);
  879. fc_lport_enter_reset(lport);
  880. mutex_unlock(&lport->lp_mutex);
  881. return 0;
  882. }
  883. EXPORT_SYMBOL(fc_lport_reset);
  884. /**
  885. * fc_lport_reset_locked() - Reset the local port w/ the lport lock held
  886. * @lport: The local port to be reset
  887. *
  888. * Locking Note: The lport lock is expected to be held before calling
  889. * this routine.
  890. */
  891. static void fc_lport_reset_locked(struct fc_lport *lport)
  892. {
  893. if (lport->dns_rdata)
  894. lport->tt.rport_logoff(lport->dns_rdata);
  895. if (lport->ptp_rdata) {
  896. lport->tt.rport_logoff(lport->ptp_rdata);
  897. kref_put(&lport->ptp_rdata->kref, lport->tt.rport_destroy);
  898. lport->ptp_rdata = NULL;
  899. }
  900. lport->tt.disc_stop(lport);
  901. lport->tt.exch_mgr_reset(lport, 0, 0);
  902. fc_host_fabric_name(lport->host) = 0;
  903. if (lport->port_id && (!lport->point_to_multipoint || !lport->link_up))
  904. fc_lport_set_port_id(lport, 0, NULL);
  905. }
  906. /**
  907. * fc_lport_enter_reset() - Reset the local port
  908. * @lport: The local port to be reset
  909. *
  910. * Locking Note: The lport lock is expected to be held before calling
  911. * this routine.
  912. */
  913. static void fc_lport_enter_reset(struct fc_lport *lport)
  914. {
  915. FC_LPORT_DBG(lport, "Entered RESET state from %s state\n",
  916. fc_lport_state(lport));
  917. if (lport->state == LPORT_ST_DISABLED || lport->state == LPORT_ST_LOGO)
  918. return;
  919. if (lport->vport) {
  920. if (lport->link_up)
  921. fc_vport_set_state(lport->vport, FC_VPORT_INITIALIZING);
  922. else
  923. fc_vport_set_state(lport->vport, FC_VPORT_LINKDOWN);
  924. }
  925. fc_lport_state_enter(lport, LPORT_ST_RESET);
  926. fc_host_post_event(lport->host, fc_get_event_number(),
  927. FCH_EVT_LIPRESET, 0);
  928. fc_vports_linkchange(lport);
  929. fc_lport_reset_locked(lport);
  930. if (lport->link_up)
  931. fc_lport_enter_flogi(lport);
  932. }
  933. /**
  934. * fc_lport_enter_disabled() - Disable the local port
  935. * @lport: The local port to be reset
  936. *
  937. * Locking Note: The lport lock is expected to be held before calling
  938. * this routine.
  939. */
  940. static void fc_lport_enter_disabled(struct fc_lport *lport)
  941. {
  942. FC_LPORT_DBG(lport, "Entered disabled state from %s state\n",
  943. fc_lport_state(lport));
  944. fc_lport_state_enter(lport, LPORT_ST_DISABLED);
  945. fc_vports_linkchange(lport);
  946. fc_lport_reset_locked(lport);
  947. }
  948. /**
  949. * fc_lport_error() - Handler for any errors
  950. * @lport: The local port that the error was on
  951. * @fp: The error code encoded in a frame pointer
  952. *
  953. * If the error was caused by a resource allocation failure
  954. * then wait for half a second and retry, otherwise retry
  955. * after the e_d_tov time.
  956. */
  957. static void fc_lport_error(struct fc_lport *lport, struct fc_frame *fp)
  958. {
  959. unsigned long delay = 0;
  960. FC_LPORT_DBG(lport, "Error %ld in state %s, retries %d\n",
  961. PTR_ERR(fp), fc_lport_state(lport),
  962. lport->retry_count);
  963. if (PTR_ERR(fp) == -FC_EX_CLOSED)
  964. return;
  965. /*
  966. * Memory allocation failure, or the exchange timed out
  967. * or we received LS_RJT.
  968. * Retry after delay
  969. */
  970. if (lport->retry_count < lport->max_retry_count) {
  971. lport->retry_count++;
  972. if (!fp)
  973. delay = msecs_to_jiffies(500);
  974. else
  975. delay = msecs_to_jiffies(lport->e_d_tov);
  976. schedule_delayed_work(&lport->retry_work, delay);
  977. } else
  978. fc_lport_enter_reset(lport);
  979. }
  980. /**
  981. * fc_lport_ns_resp() - Handle response to a name server
  982. * registration exchange
  983. * @sp: current sequence in exchange
  984. * @fp: response frame
  985. * @lp_arg: Fibre Channel host port instance
  986. *
  987. * Locking Note: This function will be called without the lport lock
  988. * held, but it will lock, call an _enter_* function or fc_lport_error()
  989. * and then unlock the lport.
  990. */
  991. static void fc_lport_ns_resp(struct fc_seq *sp, struct fc_frame *fp,
  992. void *lp_arg)
  993. {
  994. struct fc_lport *lport = lp_arg;
  995. struct fc_frame_header *fh;
  996. struct fc_ct_hdr *ct;
  997. FC_LPORT_DBG(lport, "Received a ns %s\n", fc_els_resp_type(fp));
  998. if (fp == ERR_PTR(-FC_EX_CLOSED))
  999. return;
  1000. mutex_lock(&lport->lp_mutex);
  1001. if (lport->state < LPORT_ST_RNN_ID || lport->state > LPORT_ST_RFF_ID) {
  1002. FC_LPORT_DBG(lport, "Received a name server response, "
  1003. "but in state %s\n", fc_lport_state(lport));
  1004. if (IS_ERR(fp))
  1005. goto err;
  1006. goto out;
  1007. }
  1008. if (IS_ERR(fp)) {
  1009. fc_lport_error(lport, fp);
  1010. goto err;
  1011. }
  1012. fh = fc_frame_header_get(fp);
  1013. ct = fc_frame_payload_get(fp, sizeof(*ct));
  1014. if (fh && ct && fh->fh_type == FC_TYPE_CT &&
  1015. ct->ct_fs_type == FC_FST_DIR &&
  1016. ct->ct_fs_subtype == FC_NS_SUBTYPE &&
  1017. ntohs(ct->ct_cmd) == FC_FS_ACC)
  1018. switch (lport->state) {
  1019. case LPORT_ST_RNN_ID:
  1020. fc_lport_enter_ns(lport, LPORT_ST_RSNN_NN);
  1021. break;
  1022. case LPORT_ST_RSNN_NN:
  1023. fc_lport_enter_ns(lport, LPORT_ST_RSPN_ID);
  1024. break;
  1025. case LPORT_ST_RSPN_ID:
  1026. fc_lport_enter_ns(lport, LPORT_ST_RFT_ID);
  1027. break;
  1028. case LPORT_ST_RFT_ID:
  1029. fc_lport_enter_ns(lport, LPORT_ST_RFF_ID);
  1030. break;
  1031. case LPORT_ST_RFF_ID:
  1032. fc_lport_enter_scr(lport);
  1033. break;
  1034. default:
  1035. /* should have already been caught by state checks */
  1036. break;
  1037. }
  1038. else
  1039. fc_lport_error(lport, fp);
  1040. out:
  1041. fc_frame_free(fp);
  1042. err:
  1043. mutex_unlock(&lport->lp_mutex);
  1044. }
  1045. /**
  1046. * fc_lport_scr_resp() - Handle response to State Change Register (SCR) request
  1047. * @sp: current sequence in SCR exchange
  1048. * @fp: response frame
  1049. * @lp_arg: Fibre Channel lport port instance that sent the registration request
  1050. *
  1051. * Locking Note: This function will be called without the lport lock
  1052. * held, but it will lock, call an _enter_* function or fc_lport_error
  1053. * and then unlock the lport.
  1054. */
  1055. static void fc_lport_scr_resp(struct fc_seq *sp, struct fc_frame *fp,
  1056. void *lp_arg)
  1057. {
  1058. struct fc_lport *lport = lp_arg;
  1059. u8 op;
  1060. FC_LPORT_DBG(lport, "Received a SCR %s\n", fc_els_resp_type(fp));
  1061. if (fp == ERR_PTR(-FC_EX_CLOSED))
  1062. return;
  1063. mutex_lock(&lport->lp_mutex);
  1064. if (lport->state != LPORT_ST_SCR) {
  1065. FC_LPORT_DBG(lport, "Received a SCR response, but in state "
  1066. "%s\n", fc_lport_state(lport));
  1067. if (IS_ERR(fp))
  1068. goto err;
  1069. goto out;
  1070. }
  1071. if (IS_ERR(fp)) {
  1072. fc_lport_error(lport, fp);
  1073. goto err;
  1074. }
  1075. op = fc_frame_payload_op(fp);
  1076. if (op == ELS_LS_ACC)
  1077. fc_lport_enter_ready(lport);
  1078. else
  1079. fc_lport_error(lport, fp);
  1080. out:
  1081. fc_frame_free(fp);
  1082. err:
  1083. mutex_unlock(&lport->lp_mutex);
  1084. }
  1085. /**
  1086. * fc_lport_enter_scr() - Send a SCR (State Change Register) request
  1087. * @lport: The local port to register for state changes
  1088. *
  1089. * Locking Note: The lport lock is expected to be held before calling
  1090. * this routine.
  1091. */
  1092. static void fc_lport_enter_scr(struct fc_lport *lport)
  1093. {
  1094. struct fc_frame *fp;
  1095. FC_LPORT_DBG(lport, "Entered SCR state from %s state\n",
  1096. fc_lport_state(lport));
  1097. fc_lport_state_enter(lport, LPORT_ST_SCR);
  1098. fp = fc_frame_alloc(lport, sizeof(struct fc_els_scr));
  1099. if (!fp) {
  1100. fc_lport_error(lport, fp);
  1101. return;
  1102. }
  1103. if (!lport->tt.elsct_send(lport, FC_FID_FCTRL, fp, ELS_SCR,
  1104. fc_lport_scr_resp, lport,
  1105. 2 * lport->r_a_tov))
  1106. fc_lport_error(lport, NULL);
  1107. }
  1108. /**
  1109. * fc_lport_enter_ns() - register some object with the name server
  1110. * @lport: Fibre Channel local port to register
  1111. *
  1112. * Locking Note: The lport lock is expected to be held before calling
  1113. * this routine.
  1114. */
  1115. static void fc_lport_enter_ns(struct fc_lport *lport, enum fc_lport_state state)
  1116. {
  1117. struct fc_frame *fp;
  1118. enum fc_ns_req cmd;
  1119. int size = sizeof(struct fc_ct_hdr);
  1120. size_t len;
  1121. FC_LPORT_DBG(lport, "Entered %s state from %s state\n",
  1122. fc_lport_state_names[state],
  1123. fc_lport_state(lport));
  1124. fc_lport_state_enter(lport, state);
  1125. switch (state) {
  1126. case LPORT_ST_RNN_ID:
  1127. cmd = FC_NS_RNN_ID;
  1128. size += sizeof(struct fc_ns_rn_id);
  1129. break;
  1130. case LPORT_ST_RSNN_NN:
  1131. len = strnlen(fc_host_symbolic_name(lport->host), 255);
  1132. /* if there is no symbolic name, skip to RFT_ID */
  1133. if (!len)
  1134. return fc_lport_enter_ns(lport, LPORT_ST_RFT_ID);
  1135. cmd = FC_NS_RSNN_NN;
  1136. size += sizeof(struct fc_ns_rsnn) + len;
  1137. break;
  1138. case LPORT_ST_RSPN_ID:
  1139. len = strnlen(fc_host_symbolic_name(lport->host), 255);
  1140. /* if there is no symbolic name, skip to RFT_ID */
  1141. if (!len)
  1142. return fc_lport_enter_ns(lport, LPORT_ST_RFT_ID);
  1143. cmd = FC_NS_RSPN_ID;
  1144. size += sizeof(struct fc_ns_rspn) + len;
  1145. break;
  1146. case LPORT_ST_RFT_ID:
  1147. cmd = FC_NS_RFT_ID;
  1148. size += sizeof(struct fc_ns_rft);
  1149. break;
  1150. case LPORT_ST_RFF_ID:
  1151. cmd = FC_NS_RFF_ID;
  1152. size += sizeof(struct fc_ns_rff_id);
  1153. break;
  1154. default:
  1155. fc_lport_error(lport, NULL);
  1156. return;
  1157. }
  1158. fp = fc_frame_alloc(lport, size);
  1159. if (!fp) {
  1160. fc_lport_error(lport, fp);
  1161. return;
  1162. }
  1163. if (!lport->tt.elsct_send(lport, FC_FID_DIR_SERV, fp, cmd,
  1164. fc_lport_ns_resp,
  1165. lport, 3 * lport->r_a_tov))
  1166. fc_lport_error(lport, fp);
  1167. }
  1168. static struct fc_rport_operations fc_lport_rport_ops = {
  1169. .event_callback = fc_lport_rport_callback,
  1170. };
  1171. /**
  1172. * fc_rport_enter_dns() - Create a fc_rport for the name server
  1173. * @lport: The local port requesting a remote port for the name server
  1174. *
  1175. * Locking Note: The lport lock is expected to be held before calling
  1176. * this routine.
  1177. */
  1178. static void fc_lport_enter_dns(struct fc_lport *lport)
  1179. {
  1180. struct fc_rport_priv *rdata;
  1181. FC_LPORT_DBG(lport, "Entered DNS state from %s state\n",
  1182. fc_lport_state(lport));
  1183. fc_lport_state_enter(lport, LPORT_ST_DNS);
  1184. mutex_lock(&lport->disc.disc_mutex);
  1185. rdata = lport->tt.rport_create(lport, FC_FID_DIR_SERV);
  1186. mutex_unlock(&lport->disc.disc_mutex);
  1187. if (!rdata)
  1188. goto err;
  1189. rdata->ops = &fc_lport_rport_ops;
  1190. lport->tt.rport_login(rdata);
  1191. return;
  1192. err:
  1193. fc_lport_error(lport, NULL);
  1194. }
  1195. /**
  1196. * fc_lport_timeout() - Handler for the retry_work timer
  1197. * @work: The work struct of the local port
  1198. */
  1199. static void fc_lport_timeout(struct work_struct *work)
  1200. {
  1201. struct fc_lport *lport =
  1202. container_of(work, struct fc_lport,
  1203. retry_work.work);
  1204. mutex_lock(&lport->lp_mutex);
  1205. switch (lport->state) {
  1206. case LPORT_ST_DISABLED:
  1207. WARN_ON(1);
  1208. break;
  1209. case LPORT_ST_READY:
  1210. break;
  1211. case LPORT_ST_RESET:
  1212. break;
  1213. case LPORT_ST_FLOGI:
  1214. fc_lport_enter_flogi(lport);
  1215. break;
  1216. case LPORT_ST_DNS:
  1217. fc_lport_enter_dns(lport);
  1218. break;
  1219. case LPORT_ST_RNN_ID:
  1220. case LPORT_ST_RSNN_NN:
  1221. case LPORT_ST_RSPN_ID:
  1222. case LPORT_ST_RFT_ID:
  1223. case LPORT_ST_RFF_ID:
  1224. fc_lport_enter_ns(lport, lport->state);
  1225. break;
  1226. case LPORT_ST_SCR:
  1227. fc_lport_enter_scr(lport);
  1228. break;
  1229. case LPORT_ST_LOGO:
  1230. fc_lport_enter_logo(lport);
  1231. break;
  1232. }
  1233. mutex_unlock(&lport->lp_mutex);
  1234. }
  1235. /**
  1236. * fc_lport_logo_resp() - Handle response to LOGO request
  1237. * @sp: The sequence that the LOGO was on
  1238. * @fp: The LOGO frame
  1239. * @lp_arg: The lport port that received the LOGO request
  1240. *
  1241. * Locking Note: This function will be called without the lport lock
  1242. * held, but it will lock, call an _enter_* function or fc_lport_error()
  1243. * and then unlock the lport.
  1244. */
  1245. void fc_lport_logo_resp(struct fc_seq *sp, struct fc_frame *fp,
  1246. void *lp_arg)
  1247. {
  1248. struct fc_lport *lport = lp_arg;
  1249. u8 op;
  1250. FC_LPORT_DBG(lport, "Received a LOGO %s\n", fc_els_resp_type(fp));
  1251. if (fp == ERR_PTR(-FC_EX_CLOSED))
  1252. return;
  1253. mutex_lock(&lport->lp_mutex);
  1254. if (lport->state != LPORT_ST_LOGO) {
  1255. FC_LPORT_DBG(lport, "Received a LOGO response, but in state "
  1256. "%s\n", fc_lport_state(lport));
  1257. if (IS_ERR(fp))
  1258. goto err;
  1259. goto out;
  1260. }
  1261. if (IS_ERR(fp)) {
  1262. fc_lport_error(lport, fp);
  1263. goto err;
  1264. }
  1265. op = fc_frame_payload_op(fp);
  1266. if (op == ELS_LS_ACC)
  1267. fc_lport_enter_disabled(lport);
  1268. else
  1269. fc_lport_error(lport, fp);
  1270. out:
  1271. fc_frame_free(fp);
  1272. err:
  1273. mutex_unlock(&lport->lp_mutex);
  1274. }
  1275. EXPORT_SYMBOL(fc_lport_logo_resp);
  1276. /**
  1277. * fc_rport_enter_logo() - Logout of the fabric
  1278. * @lport: The local port to be logged out
  1279. *
  1280. * Locking Note: The lport lock is expected to be held before calling
  1281. * this routine.
  1282. */
  1283. static void fc_lport_enter_logo(struct fc_lport *lport)
  1284. {
  1285. struct fc_frame *fp;
  1286. struct fc_els_logo *logo;
  1287. FC_LPORT_DBG(lport, "Entered LOGO state from %s state\n",
  1288. fc_lport_state(lport));
  1289. fc_lport_state_enter(lport, LPORT_ST_LOGO);
  1290. fc_vports_linkchange(lport);
  1291. fp = fc_frame_alloc(lport, sizeof(*logo));
  1292. if (!fp) {
  1293. fc_lport_error(lport, fp);
  1294. return;
  1295. }
  1296. if (!lport->tt.elsct_send(lport, FC_FID_FLOGI, fp, ELS_LOGO,
  1297. fc_lport_logo_resp, lport,
  1298. 2 * lport->r_a_tov))
  1299. fc_lport_error(lport, NULL);
  1300. }
  1301. /**
  1302. * fc_lport_flogi_resp() - Handle response to FLOGI request
  1303. * @sp: The sequence that the FLOGI was on
  1304. * @fp: The FLOGI response frame
  1305. * @lp_arg: The lport port that received the FLOGI response
  1306. *
  1307. * Locking Note: This function will be called without the lport lock
  1308. * held, but it will lock, call an _enter_* function or fc_lport_error()
  1309. * and then unlock the lport.
  1310. */
  1311. void fc_lport_flogi_resp(struct fc_seq *sp, struct fc_frame *fp,
  1312. void *lp_arg)
  1313. {
  1314. struct fc_lport *lport = lp_arg;
  1315. struct fc_frame_header *fh;
  1316. struct fc_els_flogi *flp;
  1317. u32 did;
  1318. u16 csp_flags;
  1319. unsigned int r_a_tov;
  1320. unsigned int e_d_tov;
  1321. u16 mfs;
  1322. FC_LPORT_DBG(lport, "Received a FLOGI %s\n", fc_els_resp_type(fp));
  1323. if (fp == ERR_PTR(-FC_EX_CLOSED))
  1324. return;
  1325. mutex_lock(&lport->lp_mutex);
  1326. if (lport->state != LPORT_ST_FLOGI) {
  1327. FC_LPORT_DBG(lport, "Received a FLOGI response, but in state "
  1328. "%s\n", fc_lport_state(lport));
  1329. if (IS_ERR(fp))
  1330. goto err;
  1331. goto out;
  1332. }
  1333. if (IS_ERR(fp)) {
  1334. fc_lport_error(lport, fp);
  1335. goto err;
  1336. }
  1337. fh = fc_frame_header_get(fp);
  1338. did = fc_frame_did(fp);
  1339. if (fh->fh_r_ctl != FC_RCTL_ELS_REP || did == 0 ||
  1340. fc_frame_payload_op(fp) != ELS_LS_ACC) {
  1341. FC_LPORT_DBG(lport, "FLOGI not accepted or bad response\n");
  1342. fc_lport_error(lport, fp);
  1343. goto err;
  1344. }
  1345. flp = fc_frame_payload_get(fp, sizeof(*flp));
  1346. if (!flp) {
  1347. FC_LPORT_DBG(lport, "FLOGI bad response\n");
  1348. fc_lport_error(lport, fp);
  1349. goto err;
  1350. }
  1351. mfs = ntohs(flp->fl_csp.sp_bb_data) &
  1352. FC_SP_BB_DATA_MASK;
  1353. if (mfs >= FC_SP_MIN_MAX_PAYLOAD &&
  1354. mfs < lport->mfs)
  1355. lport->mfs = mfs;
  1356. csp_flags = ntohs(flp->fl_csp.sp_features);
  1357. r_a_tov = ntohl(flp->fl_csp.sp_r_a_tov);
  1358. e_d_tov = ntohl(flp->fl_csp.sp_e_d_tov);
  1359. if (csp_flags & FC_SP_FT_EDTR)
  1360. e_d_tov /= 1000000;
  1361. lport->npiv_enabled = !!(csp_flags & FC_SP_FT_NPIV_ACC);
  1362. if ((csp_flags & FC_SP_FT_FPORT) == 0) {
  1363. if (e_d_tov > lport->e_d_tov)
  1364. lport->e_d_tov = e_d_tov;
  1365. lport->r_a_tov = 2 * e_d_tov;
  1366. fc_lport_set_port_id(lport, did, fp);
  1367. printk(KERN_INFO "host%d: libfc: "
  1368. "Port (%6.6x) entered "
  1369. "point-to-point mode\n",
  1370. lport->host->host_no, did);
  1371. fc_lport_ptp_setup(lport, fc_frame_sid(fp),
  1372. get_unaligned_be64(
  1373. &flp->fl_wwpn),
  1374. get_unaligned_be64(
  1375. &flp->fl_wwnn));
  1376. } else {
  1377. lport->e_d_tov = e_d_tov;
  1378. lport->r_a_tov = r_a_tov;
  1379. fc_host_fabric_name(lport->host) =
  1380. get_unaligned_be64(&flp->fl_wwnn);
  1381. fc_lport_set_port_id(lport, did, fp);
  1382. fc_lport_enter_dns(lport);
  1383. }
  1384. out:
  1385. fc_frame_free(fp);
  1386. err:
  1387. mutex_unlock(&lport->lp_mutex);
  1388. }
  1389. EXPORT_SYMBOL(fc_lport_flogi_resp);
  1390. /**
  1391. * fc_rport_enter_flogi() - Send a FLOGI request to the fabric manager
  1392. * @lport: Fibre Channel local port to be logged in to the fabric
  1393. *
  1394. * Locking Note: The lport lock is expected to be held before calling
  1395. * this routine.
  1396. */
  1397. void fc_lport_enter_flogi(struct fc_lport *lport)
  1398. {
  1399. struct fc_frame *fp;
  1400. FC_LPORT_DBG(lport, "Entered FLOGI state from %s state\n",
  1401. fc_lport_state(lport));
  1402. fc_lport_state_enter(lport, LPORT_ST_FLOGI);
  1403. if (lport->point_to_multipoint) {
  1404. if (lport->port_id)
  1405. fc_lport_enter_ready(lport);
  1406. return;
  1407. }
  1408. fp = fc_frame_alloc(lport, sizeof(struct fc_els_flogi));
  1409. if (!fp)
  1410. return fc_lport_error(lport, fp);
  1411. if (!lport->tt.elsct_send(lport, FC_FID_FLOGI, fp,
  1412. lport->vport ? ELS_FDISC : ELS_FLOGI,
  1413. fc_lport_flogi_resp, lport,
  1414. lport->vport ? 2 * lport->r_a_tov :
  1415. lport->e_d_tov))
  1416. fc_lport_error(lport, NULL);
  1417. }
  1418. /**
  1419. * fc_lport_config() - Configure a fc_lport
  1420. * @lport: The local port to be configured
  1421. */
  1422. int fc_lport_config(struct fc_lport *lport)
  1423. {
  1424. INIT_DELAYED_WORK(&lport->retry_work, fc_lport_timeout);
  1425. mutex_init(&lport->lp_mutex);
  1426. fc_lport_state_enter(lport, LPORT_ST_DISABLED);
  1427. fc_lport_add_fc4_type(lport, FC_TYPE_FCP);
  1428. fc_lport_add_fc4_type(lport, FC_TYPE_CT);
  1429. fc_fc4_conf_lport_params(lport, FC_TYPE_FCP);
  1430. return 0;
  1431. }
  1432. EXPORT_SYMBOL(fc_lport_config);
  1433. /**
  1434. * fc_lport_init() - Initialize the lport layer for a local port
  1435. * @lport: The local port to initialize the exchange layer for
  1436. */
  1437. int fc_lport_init(struct fc_lport *lport)
  1438. {
  1439. if (!lport->tt.lport_recv)
  1440. lport->tt.lport_recv = fc_lport_recv_req;
  1441. if (!lport->tt.lport_reset)
  1442. lport->tt.lport_reset = fc_lport_reset;
  1443. fc_host_port_type(lport->host) = FC_PORTTYPE_NPORT;
  1444. fc_host_node_name(lport->host) = lport->wwnn;
  1445. fc_host_port_name(lport->host) = lport->wwpn;
  1446. fc_host_supported_classes(lport->host) = FC_COS_CLASS3;
  1447. memset(fc_host_supported_fc4s(lport->host), 0,
  1448. sizeof(fc_host_supported_fc4s(lport->host)));
  1449. fc_host_supported_fc4s(lport->host)[2] = 1;
  1450. fc_host_supported_fc4s(lport->host)[7] = 1;
  1451. /* This value is also unchanging */
  1452. memset(fc_host_active_fc4s(lport->host), 0,
  1453. sizeof(fc_host_active_fc4s(lport->host)));
  1454. fc_host_active_fc4s(lport->host)[2] = 1;
  1455. fc_host_active_fc4s(lport->host)[7] = 1;
  1456. fc_host_maxframe_size(lport->host) = lport->mfs;
  1457. fc_host_supported_speeds(lport->host) = 0;
  1458. if (lport->link_supported_speeds & FC_PORTSPEED_1GBIT)
  1459. fc_host_supported_speeds(lport->host) |= FC_PORTSPEED_1GBIT;
  1460. if (lport->link_supported_speeds & FC_PORTSPEED_10GBIT)
  1461. fc_host_supported_speeds(lport->host) |= FC_PORTSPEED_10GBIT;
  1462. fc_fc4_add_lport(lport);
  1463. return 0;
  1464. }
  1465. EXPORT_SYMBOL(fc_lport_init);
  1466. /**
  1467. * fc_lport_bsg_resp() - The common response handler for FC Passthrough requests
  1468. * @sp: The sequence for the FC Passthrough response
  1469. * @fp: The response frame
  1470. * @info_arg: The BSG info that the response is for
  1471. */
  1472. static void fc_lport_bsg_resp(struct fc_seq *sp, struct fc_frame *fp,
  1473. void *info_arg)
  1474. {
  1475. struct fc_bsg_info *info = info_arg;
  1476. struct fc_bsg_job *job = info->job;
  1477. struct fc_lport *lport = info->lport;
  1478. struct fc_frame_header *fh;
  1479. size_t len;
  1480. void *buf;
  1481. if (IS_ERR(fp)) {
  1482. job->reply->result = (PTR_ERR(fp) == -FC_EX_CLOSED) ?
  1483. -ECONNABORTED : -ETIMEDOUT;
  1484. job->reply_len = sizeof(uint32_t);
  1485. job->state_flags |= FC_RQST_STATE_DONE;
  1486. job->job_done(job);
  1487. kfree(info);
  1488. return;
  1489. }
  1490. mutex_lock(&lport->lp_mutex);
  1491. fh = fc_frame_header_get(fp);
  1492. len = fr_len(fp) - sizeof(*fh);
  1493. buf = fc_frame_payload_get(fp, 0);
  1494. if (fr_sof(fp) == FC_SOF_I3 && !ntohs(fh->fh_seq_cnt)) {
  1495. /* Get the response code from the first frame payload */
  1496. unsigned short cmd = (info->rsp_code == FC_FS_ACC) ?
  1497. ntohs(((struct fc_ct_hdr *)buf)->ct_cmd) :
  1498. (unsigned short)fc_frame_payload_op(fp);
  1499. /* Save the reply status of the job */
  1500. job->reply->reply_data.ctels_reply.status =
  1501. (cmd == info->rsp_code) ?
  1502. FC_CTELS_STATUS_OK : FC_CTELS_STATUS_REJECT;
  1503. }
  1504. job->reply->reply_payload_rcv_len +=
  1505. fc_copy_buffer_to_sglist(buf, len, info->sg, &info->nents,
  1506. &info->offset, KM_BIO_SRC_IRQ, NULL);
  1507. if (fr_eof(fp) == FC_EOF_T &&
  1508. (ntoh24(fh->fh_f_ctl) & (FC_FC_LAST_SEQ | FC_FC_END_SEQ)) ==
  1509. (FC_FC_LAST_SEQ | FC_FC_END_SEQ)) {
  1510. if (job->reply->reply_payload_rcv_len >
  1511. job->reply_payload.payload_len)
  1512. job->reply->reply_payload_rcv_len =
  1513. job->reply_payload.payload_len;
  1514. job->reply->result = 0;
  1515. job->state_flags |= FC_RQST_STATE_DONE;
  1516. job->job_done(job);
  1517. kfree(info);
  1518. }
  1519. fc_frame_free(fp);
  1520. mutex_unlock(&lport->lp_mutex);
  1521. }
  1522. /**
  1523. * fc_lport_els_request() - Send ELS passthrough request
  1524. * @job: The BSG Passthrough job
  1525. * @lport: The local port sending the request
  1526. * @did: The destination port id
  1527. *
  1528. * Locking Note: The lport lock is expected to be held before calling
  1529. * this routine.
  1530. */
  1531. static int fc_lport_els_request(struct fc_bsg_job *job,
  1532. struct fc_lport *lport,
  1533. u32 did, u32 tov)
  1534. {
  1535. struct fc_bsg_info *info;
  1536. struct fc_frame *fp;
  1537. struct fc_frame_header *fh;
  1538. char *pp;
  1539. int len;
  1540. fp = fc_frame_alloc(lport, job->request_payload.payload_len);
  1541. if (!fp)
  1542. return -ENOMEM;
  1543. len = job->request_payload.payload_len;
  1544. pp = fc_frame_payload_get(fp, len);
  1545. sg_copy_to_buffer(job->request_payload.sg_list,
  1546. job->request_payload.sg_cnt,
  1547. pp, len);
  1548. fh = fc_frame_header_get(fp);
  1549. fh->fh_r_ctl = FC_RCTL_ELS_REQ;
  1550. hton24(fh->fh_d_id, did);
  1551. hton24(fh->fh_s_id, lport->port_id);
  1552. fh->fh_type = FC_TYPE_ELS;
  1553. hton24(fh->fh_f_ctl, FC_FCTL_REQ);
  1554. fh->fh_cs_ctl = 0;
  1555. fh->fh_df_ctl = 0;
  1556. fh->fh_parm_offset = 0;
  1557. info = kzalloc(sizeof(struct fc_bsg_info), GFP_KERNEL);
  1558. if (!info) {
  1559. fc_frame_free(fp);
  1560. return -ENOMEM;
  1561. }
  1562. info->job = job;
  1563. info->lport = lport;
  1564. info->rsp_code = ELS_LS_ACC;
  1565. info->nents = job->reply_payload.sg_cnt;
  1566. info->sg = job->reply_payload.sg_list;
  1567. if (!lport->tt.exch_seq_send(lport, fp, fc_lport_bsg_resp,
  1568. NULL, info, tov)) {
  1569. kfree(info);
  1570. return -ECOMM;
  1571. }
  1572. return 0;
  1573. }
  1574. /**
  1575. * fc_lport_ct_request() - Send CT Passthrough request
  1576. * @job: The BSG Passthrough job
  1577. * @lport: The local port sending the request
  1578. * @did: The destination FC-ID
  1579. * @tov: The timeout period to wait for the response
  1580. *
  1581. * Locking Note: The lport lock is expected to be held before calling
  1582. * this routine.
  1583. */
  1584. static int fc_lport_ct_request(struct fc_bsg_job *job,
  1585. struct fc_lport *lport, u32 did, u32 tov)
  1586. {
  1587. struct fc_bsg_info *info;
  1588. struct fc_frame *fp;
  1589. struct fc_frame_header *fh;
  1590. struct fc_ct_req *ct;
  1591. size_t len;
  1592. fp = fc_frame_alloc(lport, sizeof(struct fc_ct_hdr) +
  1593. job->request_payload.payload_len);
  1594. if (!fp)
  1595. return -ENOMEM;
  1596. len = job->request_payload.payload_len;
  1597. ct = fc_frame_payload_get(fp, len);
  1598. sg_copy_to_buffer(job->request_payload.sg_list,
  1599. job->request_payload.sg_cnt,
  1600. ct, len);
  1601. fh = fc_frame_header_get(fp);
  1602. fh->fh_r_ctl = FC_RCTL_DD_UNSOL_CTL;
  1603. hton24(fh->fh_d_id, did);
  1604. hton24(fh->fh_s_id, lport->port_id);
  1605. fh->fh_type = FC_TYPE_CT;
  1606. hton24(fh->fh_f_ctl, FC_FCTL_REQ);
  1607. fh->fh_cs_ctl = 0;
  1608. fh->fh_df_ctl = 0;
  1609. fh->fh_parm_offset = 0;
  1610. info = kzalloc(sizeof(struct fc_bsg_info), GFP_KERNEL);
  1611. if (!info) {
  1612. fc_frame_free(fp);
  1613. return -ENOMEM;
  1614. }
  1615. info->job = job;
  1616. info->lport = lport;
  1617. info->rsp_code = FC_FS_ACC;
  1618. info->nents = job->reply_payload.sg_cnt;
  1619. info->sg = job->reply_payload.sg_list;
  1620. if (!lport->tt.exch_seq_send(lport, fp, fc_lport_bsg_resp,
  1621. NULL, info, tov)) {
  1622. kfree(info);
  1623. return -ECOMM;
  1624. }
  1625. return 0;
  1626. }
  1627. /**
  1628. * fc_lport_bsg_request() - The common entry point for sending
  1629. * FC Passthrough requests
  1630. * @job: The BSG passthrough job
  1631. */
  1632. int fc_lport_bsg_request(struct fc_bsg_job *job)
  1633. {
  1634. struct request *rsp = job->req->next_rq;
  1635. struct Scsi_Host *shost = job->shost;
  1636. struct fc_lport *lport = shost_priv(shost);
  1637. struct fc_rport *rport;
  1638. struct fc_rport_priv *rdata;
  1639. int rc = -EINVAL;
  1640. u32 did;
  1641. job->reply->reply_payload_rcv_len = 0;
  1642. if (rsp)
  1643. rsp->resid_len = job->reply_payload.payload_len;
  1644. mutex_lock(&lport->lp_mutex);
  1645. switch (job->request->msgcode) {
  1646. case FC_BSG_RPT_ELS:
  1647. rport = job->rport;
  1648. if (!rport)
  1649. break;
  1650. rdata = rport->dd_data;
  1651. rc = fc_lport_els_request(job, lport, rport->port_id,
  1652. rdata->e_d_tov);
  1653. break;
  1654. case FC_BSG_RPT_CT:
  1655. rport = job->rport;
  1656. if (!rport)
  1657. break;
  1658. rdata = rport->dd_data;
  1659. rc = fc_lport_ct_request(job, lport, rport->port_id,
  1660. rdata->e_d_tov);
  1661. break;
  1662. case FC_BSG_HST_CT:
  1663. did = ntoh24(job->request->rqst_data.h_ct.port_id);
  1664. if (did == FC_FID_DIR_SERV)
  1665. rdata = lport->dns_rdata;
  1666. else
  1667. rdata = lport->tt.rport_lookup(lport, did);
  1668. if (!rdata)
  1669. break;
  1670. rc = fc_lport_ct_request(job, lport, did, rdata->e_d_tov);
  1671. break;
  1672. case FC_BSG_HST_ELS_NOLOGIN:
  1673. did = ntoh24(job->request->rqst_data.h_els.port_id);
  1674. rc = fc_lport_els_request(job, lport, did, lport->e_d_tov);
  1675. break;
  1676. }
  1677. mutex_unlock(&lport->lp_mutex);
  1678. return rc;
  1679. }
  1680. EXPORT_SYMBOL(fc_lport_bsg_request);