fc_lport.c 55 KB

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