fc_lport.c 49 KB

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