fc_lport.c 56 KB

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