fc_lport.c 49 KB

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