fc_rport.c 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043
  1. /*
  2. * Copyright(c) 2007 - 2008 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. * RPORT GENERAL INFO
  21. *
  22. * This file contains all processing regarding fc_rports. It contains the
  23. * rport state machine and does all rport interaction with the transport class.
  24. * There should be no other places in libfc that interact directly with the
  25. * transport class in regards to adding and deleting rports.
  26. *
  27. * fc_rport's represent N_Port's within the fabric.
  28. */
  29. /*
  30. * RPORT LOCKING
  31. *
  32. * The rport should never hold the rport mutex and then attempt to acquire
  33. * either the lport or disc mutexes. The rport's mutex is considered lesser
  34. * than both the lport's mutex and the disc mutex. Refer to fc_lport.c for
  35. * more comments on the heirarchy.
  36. *
  37. * The locking strategy is similar to the lport's strategy. The lock protects
  38. * the rport's states and is held and released by the entry points to the rport
  39. * block. All _enter_* functions correspond to rport states and expect the rport
  40. * mutex to be locked before calling them. This means that rports only handle
  41. * one request or response at a time, since they're not critical for the I/O
  42. * path this potential over-use of the mutex is acceptable.
  43. */
  44. #include <linux/kernel.h>
  45. #include <linux/spinlock.h>
  46. #include <linux/interrupt.h>
  47. #include <linux/slab.h>
  48. #include <linux/rcupdate.h>
  49. #include <linux/timer.h>
  50. #include <linux/workqueue.h>
  51. #include <asm/unaligned.h>
  52. #include <scsi/libfc.h>
  53. #include <scsi/fc_encode.h>
  54. #include "fc_libfc.h"
  55. struct workqueue_struct *rport_event_queue;
  56. static void fc_rport_enter_flogi(struct fc_rport_priv *);
  57. static void fc_rport_enter_plogi(struct fc_rport_priv *);
  58. static void fc_rport_enter_prli(struct fc_rport_priv *);
  59. static void fc_rport_enter_rtv(struct fc_rport_priv *);
  60. static void fc_rport_enter_ready(struct fc_rport_priv *);
  61. static void fc_rport_enter_logo(struct fc_rport_priv *);
  62. static void fc_rport_enter_adisc(struct fc_rport_priv *);
  63. static void fc_rport_recv_plogi_req(struct fc_lport *,
  64. struct fc_seq *, struct fc_frame *);
  65. static void fc_rport_recv_prli_req(struct fc_rport_priv *,
  66. struct fc_seq *, struct fc_frame *);
  67. static void fc_rport_recv_prlo_req(struct fc_rport_priv *,
  68. struct fc_seq *, struct fc_frame *);
  69. static void fc_rport_recv_logo_req(struct fc_lport *,
  70. struct fc_seq *, struct fc_frame *);
  71. static void fc_rport_timeout(struct work_struct *);
  72. static void fc_rport_error(struct fc_rport_priv *, struct fc_frame *);
  73. static void fc_rport_error_retry(struct fc_rport_priv *, struct fc_frame *);
  74. static void fc_rport_work(struct work_struct *);
  75. static const char *fc_rport_state_names[] = {
  76. [RPORT_ST_INIT] = "Init",
  77. [RPORT_ST_FLOGI] = "FLOGI",
  78. [RPORT_ST_PLOGI_WAIT] = "PLOGI_WAIT",
  79. [RPORT_ST_PLOGI] = "PLOGI",
  80. [RPORT_ST_PRLI] = "PRLI",
  81. [RPORT_ST_RTV] = "RTV",
  82. [RPORT_ST_READY] = "Ready",
  83. [RPORT_ST_LOGO] = "LOGO",
  84. [RPORT_ST_ADISC] = "ADISC",
  85. [RPORT_ST_DELETE] = "Delete",
  86. };
  87. /**
  88. * fc_rport_lookup() - Lookup a remote port by port_id
  89. * @lport: The local port to lookup the remote port on
  90. * @port_id: The remote port ID to look up
  91. *
  92. * The caller must hold either disc_mutex or rcu_read_lock().
  93. */
  94. static struct fc_rport_priv *fc_rport_lookup(const struct fc_lport *lport,
  95. u32 port_id)
  96. {
  97. struct fc_rport_priv *rdata;
  98. list_for_each_entry_rcu(rdata, &lport->disc.rports, peers)
  99. if (rdata->ids.port_id == port_id)
  100. return rdata;
  101. return NULL;
  102. }
  103. /**
  104. * fc_rport_create() - Create a new remote port
  105. * @lport: The local port this remote port will be associated with
  106. * @ids: The identifiers for the new remote port
  107. *
  108. * The remote port will start in the INIT state.
  109. *
  110. * Locking note: must be called with the disc_mutex held.
  111. */
  112. static struct fc_rport_priv *fc_rport_create(struct fc_lport *lport,
  113. u32 port_id)
  114. {
  115. struct fc_rport_priv *rdata;
  116. rdata = lport->tt.rport_lookup(lport, port_id);
  117. if (rdata)
  118. return rdata;
  119. rdata = kzalloc(sizeof(*rdata) + lport->rport_priv_size, GFP_KERNEL);
  120. if (!rdata)
  121. return NULL;
  122. rdata->ids.node_name = -1;
  123. rdata->ids.port_name = -1;
  124. rdata->ids.port_id = port_id;
  125. rdata->ids.roles = FC_RPORT_ROLE_UNKNOWN;
  126. kref_init(&rdata->kref);
  127. mutex_init(&rdata->rp_mutex);
  128. rdata->local_port = lport;
  129. rdata->rp_state = RPORT_ST_INIT;
  130. rdata->event = RPORT_EV_NONE;
  131. rdata->flags = FC_RP_FLAGS_REC_SUPPORTED;
  132. rdata->e_d_tov = lport->e_d_tov;
  133. rdata->r_a_tov = lport->r_a_tov;
  134. rdata->maxframe_size = FC_MIN_MAX_PAYLOAD;
  135. INIT_DELAYED_WORK(&rdata->retry_work, fc_rport_timeout);
  136. INIT_WORK(&rdata->event_work, fc_rport_work);
  137. if (port_id != FC_FID_DIR_SERV)
  138. list_add_rcu(&rdata->peers, &lport->disc.rports);
  139. return rdata;
  140. }
  141. /**
  142. * fc_rport_free_rcu() - Free a remote port
  143. * @rcu: The rcu_head structure inside the remote port
  144. */
  145. static void fc_rport_free_rcu(struct rcu_head *rcu)
  146. {
  147. struct fc_rport_priv *rdata;
  148. rdata = container_of(rcu, struct fc_rport_priv, rcu);
  149. kfree(rdata);
  150. }
  151. /**
  152. * fc_rport_destroy() - Free a remote port after last reference is released
  153. * @kref: The remote port's kref
  154. */
  155. static void fc_rport_destroy(struct kref *kref)
  156. {
  157. struct fc_rport_priv *rdata;
  158. rdata = container_of(kref, struct fc_rport_priv, kref);
  159. call_rcu(&rdata->rcu, fc_rport_free_rcu);
  160. }
  161. /**
  162. * fc_rport_state() - Return a string identifying the remote port's state
  163. * @rdata: The remote port
  164. */
  165. static const char *fc_rport_state(struct fc_rport_priv *rdata)
  166. {
  167. const char *cp;
  168. cp = fc_rport_state_names[rdata->rp_state];
  169. if (!cp)
  170. cp = "Unknown";
  171. return cp;
  172. }
  173. /**
  174. * fc_set_rport_loss_tmo() - Set the remote port loss timeout
  175. * @rport: The remote port that gets a new timeout value
  176. * @timeout: The new timeout value (in seconds)
  177. */
  178. void fc_set_rport_loss_tmo(struct fc_rport *rport, u32 timeout)
  179. {
  180. if (timeout)
  181. rport->dev_loss_tmo = timeout + 5;
  182. else
  183. rport->dev_loss_tmo = 30;
  184. }
  185. EXPORT_SYMBOL(fc_set_rport_loss_tmo);
  186. /**
  187. * fc_plogi_get_maxframe() - Get the maximum payload from the common service
  188. * parameters in a FLOGI frame
  189. * @flp: The FLOGI or PLOGI payload
  190. * @maxval: The maximum frame size upper limit; this may be less than what
  191. * is in the service parameters
  192. */
  193. static unsigned int fc_plogi_get_maxframe(struct fc_els_flogi *flp,
  194. unsigned int maxval)
  195. {
  196. unsigned int mfs;
  197. /*
  198. * Get max payload from the common service parameters and the
  199. * class 3 receive data field size.
  200. */
  201. mfs = ntohs(flp->fl_csp.sp_bb_data) & FC_SP_BB_DATA_MASK;
  202. if (mfs >= FC_SP_MIN_MAX_PAYLOAD && mfs < maxval)
  203. maxval = mfs;
  204. mfs = ntohs(flp->fl_cssp[3 - 1].cp_rdfs);
  205. if (mfs >= FC_SP_MIN_MAX_PAYLOAD && mfs < maxval)
  206. maxval = mfs;
  207. return maxval;
  208. }
  209. /**
  210. * fc_rport_state_enter() - Change the state of a remote port
  211. * @rdata: The remote port whose state should change
  212. * @new: The new state
  213. *
  214. * Locking Note: Called with the rport lock held
  215. */
  216. static void fc_rport_state_enter(struct fc_rport_priv *rdata,
  217. enum fc_rport_state new)
  218. {
  219. if (rdata->rp_state != new)
  220. rdata->retries = 0;
  221. rdata->rp_state = new;
  222. }
  223. /**
  224. * fc_rport_work() - Handler for remote port events in the rport_event_queue
  225. * @work: Handle to the remote port being dequeued
  226. */
  227. static void fc_rport_work(struct work_struct *work)
  228. {
  229. u32 port_id;
  230. struct fc_rport_priv *rdata =
  231. container_of(work, struct fc_rport_priv, event_work);
  232. struct fc_rport_libfc_priv *rpriv;
  233. enum fc_rport_event event;
  234. struct fc_lport *lport = rdata->local_port;
  235. struct fc_rport_operations *rport_ops;
  236. struct fc_rport_identifiers ids;
  237. struct fc_rport *rport;
  238. mutex_lock(&rdata->rp_mutex);
  239. event = rdata->event;
  240. rport_ops = rdata->ops;
  241. rport = rdata->rport;
  242. FC_RPORT_DBG(rdata, "work event %u\n", event);
  243. switch (event) {
  244. case RPORT_EV_READY:
  245. ids = rdata->ids;
  246. rdata->event = RPORT_EV_NONE;
  247. rdata->major_retries = 0;
  248. kref_get(&rdata->kref);
  249. mutex_unlock(&rdata->rp_mutex);
  250. if (!rport)
  251. rport = fc_remote_port_add(lport->host, 0, &ids);
  252. if (!rport) {
  253. FC_RPORT_DBG(rdata, "Failed to add the rport\n");
  254. lport->tt.rport_logoff(rdata);
  255. kref_put(&rdata->kref, lport->tt.rport_destroy);
  256. return;
  257. }
  258. mutex_lock(&rdata->rp_mutex);
  259. if (rdata->rport)
  260. FC_RPORT_DBG(rdata, "rport already allocated\n");
  261. rdata->rport = rport;
  262. rport->maxframe_size = rdata->maxframe_size;
  263. rport->supported_classes = rdata->supported_classes;
  264. rpriv = rport->dd_data;
  265. rpriv->local_port = lport;
  266. rpriv->rp_state = rdata->rp_state;
  267. rpriv->flags = rdata->flags;
  268. rpriv->e_d_tov = rdata->e_d_tov;
  269. rpriv->r_a_tov = rdata->r_a_tov;
  270. mutex_unlock(&rdata->rp_mutex);
  271. if (rport_ops && rport_ops->event_callback) {
  272. FC_RPORT_DBG(rdata, "callback ev %d\n", event);
  273. rport_ops->event_callback(lport, rdata, event);
  274. }
  275. kref_put(&rdata->kref, lport->tt.rport_destroy);
  276. break;
  277. case RPORT_EV_FAILED:
  278. case RPORT_EV_LOGO:
  279. case RPORT_EV_STOP:
  280. port_id = rdata->ids.port_id;
  281. mutex_unlock(&rdata->rp_mutex);
  282. if (rport_ops && rport_ops->event_callback) {
  283. FC_RPORT_DBG(rdata, "callback ev %d\n", event);
  284. rport_ops->event_callback(lport, rdata, event);
  285. }
  286. cancel_delayed_work_sync(&rdata->retry_work);
  287. /*
  288. * Reset any outstanding exchanges before freeing rport.
  289. */
  290. lport->tt.exch_mgr_reset(lport, 0, port_id);
  291. lport->tt.exch_mgr_reset(lport, port_id, 0);
  292. if (rport) {
  293. rpriv = rport->dd_data;
  294. rpriv->rp_state = RPORT_ST_DELETE;
  295. mutex_lock(&rdata->rp_mutex);
  296. rdata->rport = NULL;
  297. mutex_unlock(&rdata->rp_mutex);
  298. fc_remote_port_delete(rport);
  299. }
  300. mutex_lock(&lport->disc.disc_mutex);
  301. mutex_lock(&rdata->rp_mutex);
  302. if (rdata->rp_state == RPORT_ST_DELETE) {
  303. if (port_id == FC_FID_DIR_SERV) {
  304. rdata->event = RPORT_EV_NONE;
  305. mutex_unlock(&rdata->rp_mutex);
  306. } else if ((rdata->flags & FC_RP_STARTED) &&
  307. rdata->major_retries <
  308. lport->max_rport_retry_count) {
  309. rdata->major_retries++;
  310. rdata->event = RPORT_EV_NONE;
  311. FC_RPORT_DBG(rdata, "work restart\n");
  312. fc_rport_enter_flogi(rdata);
  313. mutex_unlock(&rdata->rp_mutex);
  314. } else {
  315. FC_RPORT_DBG(rdata, "work delete\n");
  316. list_del_rcu(&rdata->peers);
  317. mutex_unlock(&rdata->rp_mutex);
  318. kref_put(&rdata->kref, lport->tt.rport_destroy);
  319. }
  320. } else {
  321. /*
  322. * Re-open for events. Reissue READY event if ready.
  323. */
  324. rdata->event = RPORT_EV_NONE;
  325. if (rdata->rp_state == RPORT_ST_READY)
  326. fc_rport_enter_ready(rdata);
  327. mutex_unlock(&rdata->rp_mutex);
  328. }
  329. mutex_unlock(&lport->disc.disc_mutex);
  330. break;
  331. default:
  332. mutex_unlock(&rdata->rp_mutex);
  333. break;
  334. }
  335. }
  336. /**
  337. * fc_rport_login() - Start the remote port login state machine
  338. * @rdata: The remote port to be logged in to
  339. *
  340. * Locking Note: Called without the rport lock held. This
  341. * function will hold the rport lock, call an _enter_*
  342. * function and then unlock the rport.
  343. *
  344. * This indicates the intent to be logged into the remote port.
  345. * If it appears we are already logged in, ADISC is used to verify
  346. * the setup.
  347. */
  348. int fc_rport_login(struct fc_rport_priv *rdata)
  349. {
  350. mutex_lock(&rdata->rp_mutex);
  351. rdata->flags |= FC_RP_STARTED;
  352. switch (rdata->rp_state) {
  353. case RPORT_ST_READY:
  354. FC_RPORT_DBG(rdata, "ADISC port\n");
  355. fc_rport_enter_adisc(rdata);
  356. break;
  357. case RPORT_ST_DELETE:
  358. FC_RPORT_DBG(rdata, "Restart deleted port\n");
  359. break;
  360. default:
  361. FC_RPORT_DBG(rdata, "Login to port\n");
  362. fc_rport_enter_flogi(rdata);
  363. break;
  364. }
  365. mutex_unlock(&rdata->rp_mutex);
  366. return 0;
  367. }
  368. /**
  369. * fc_rport_enter_delete() - Schedule a remote port to be deleted
  370. * @rdata: The remote port to be deleted
  371. * @event: The event to report as the reason for deletion
  372. *
  373. * Locking Note: Called with the rport lock held.
  374. *
  375. * Allow state change into DELETE only once.
  376. *
  377. * Call queue_work only if there's no event already pending.
  378. * Set the new event so that the old pending event will not occur.
  379. * Since we have the mutex, even if fc_rport_work() is already started,
  380. * it'll see the new event.
  381. */
  382. static void fc_rport_enter_delete(struct fc_rport_priv *rdata,
  383. enum fc_rport_event event)
  384. {
  385. if (rdata->rp_state == RPORT_ST_DELETE)
  386. return;
  387. FC_RPORT_DBG(rdata, "Delete port\n");
  388. fc_rport_state_enter(rdata, RPORT_ST_DELETE);
  389. if (rdata->event == RPORT_EV_NONE)
  390. queue_work(rport_event_queue, &rdata->event_work);
  391. rdata->event = event;
  392. }
  393. /**
  394. * fc_rport_logoff() - Logoff and remove a remote port
  395. * @rdata: The remote port to be logged off of
  396. *
  397. * Locking Note: Called without the rport lock held. This
  398. * function will hold the rport lock, call an _enter_*
  399. * function and then unlock the rport.
  400. */
  401. int fc_rport_logoff(struct fc_rport_priv *rdata)
  402. {
  403. mutex_lock(&rdata->rp_mutex);
  404. FC_RPORT_DBG(rdata, "Remove port\n");
  405. rdata->flags &= ~FC_RP_STARTED;
  406. if (rdata->rp_state == RPORT_ST_DELETE) {
  407. FC_RPORT_DBG(rdata, "Port in Delete state, not removing\n");
  408. goto out;
  409. }
  410. fc_rport_enter_logo(rdata);
  411. /*
  412. * Change the state to Delete so that we discard
  413. * the response.
  414. */
  415. fc_rport_enter_delete(rdata, RPORT_EV_STOP);
  416. out:
  417. mutex_unlock(&rdata->rp_mutex);
  418. return 0;
  419. }
  420. /**
  421. * fc_rport_enter_ready() - Transition to the RPORT_ST_READY state
  422. * @rdata: The remote port that is ready
  423. *
  424. * Locking Note: The rport lock is expected to be held before calling
  425. * this routine.
  426. */
  427. static void fc_rport_enter_ready(struct fc_rport_priv *rdata)
  428. {
  429. fc_rport_state_enter(rdata, RPORT_ST_READY);
  430. FC_RPORT_DBG(rdata, "Port is Ready\n");
  431. if (rdata->event == RPORT_EV_NONE)
  432. queue_work(rport_event_queue, &rdata->event_work);
  433. rdata->event = RPORT_EV_READY;
  434. }
  435. /**
  436. * fc_rport_timeout() - Handler for the retry_work timer
  437. * @work: Handle to the remote port that has timed out
  438. *
  439. * Locking Note: Called without the rport lock held. This
  440. * function will hold the rport lock, call an _enter_*
  441. * function and then unlock the rport.
  442. */
  443. static void fc_rport_timeout(struct work_struct *work)
  444. {
  445. struct fc_rport_priv *rdata =
  446. container_of(work, struct fc_rport_priv, retry_work.work);
  447. mutex_lock(&rdata->rp_mutex);
  448. switch (rdata->rp_state) {
  449. case RPORT_ST_FLOGI:
  450. fc_rport_enter_flogi(rdata);
  451. break;
  452. case RPORT_ST_PLOGI:
  453. fc_rport_enter_plogi(rdata);
  454. break;
  455. case RPORT_ST_PRLI:
  456. fc_rport_enter_prli(rdata);
  457. break;
  458. case RPORT_ST_RTV:
  459. fc_rport_enter_rtv(rdata);
  460. break;
  461. case RPORT_ST_LOGO:
  462. fc_rport_enter_logo(rdata);
  463. break;
  464. case RPORT_ST_ADISC:
  465. fc_rport_enter_adisc(rdata);
  466. break;
  467. case RPORT_ST_PLOGI_WAIT:
  468. case RPORT_ST_READY:
  469. case RPORT_ST_INIT:
  470. case RPORT_ST_DELETE:
  471. break;
  472. }
  473. mutex_unlock(&rdata->rp_mutex);
  474. }
  475. /**
  476. * fc_rport_error() - Error handler, called once retries have been exhausted
  477. * @rdata: The remote port the error is happened on
  478. * @fp: The error code encapsulated in a frame pointer
  479. *
  480. * Locking Note: The rport lock is expected to be held before
  481. * calling this routine
  482. */
  483. static void fc_rport_error(struct fc_rport_priv *rdata, struct fc_frame *fp)
  484. {
  485. FC_RPORT_DBG(rdata, "Error %ld in state %s, retries %d\n",
  486. IS_ERR(fp) ? -PTR_ERR(fp) : 0,
  487. fc_rport_state(rdata), rdata->retries);
  488. switch (rdata->rp_state) {
  489. case RPORT_ST_FLOGI:
  490. case RPORT_ST_PLOGI:
  491. case RPORT_ST_LOGO:
  492. rdata->flags &= ~FC_RP_STARTED;
  493. fc_rport_enter_delete(rdata, RPORT_EV_FAILED);
  494. break;
  495. case RPORT_ST_RTV:
  496. fc_rport_enter_ready(rdata);
  497. break;
  498. case RPORT_ST_PRLI:
  499. case RPORT_ST_ADISC:
  500. fc_rport_enter_logo(rdata);
  501. break;
  502. case RPORT_ST_PLOGI_WAIT:
  503. case RPORT_ST_DELETE:
  504. case RPORT_ST_READY:
  505. case RPORT_ST_INIT:
  506. break;
  507. }
  508. }
  509. /**
  510. * fc_rport_error_retry() - Handler for remote port state retries
  511. * @rdata: The remote port whose state is to be retried
  512. * @fp: The error code encapsulated in a frame pointer
  513. *
  514. * If the error was an exchange timeout retry immediately,
  515. * otherwise wait for E_D_TOV.
  516. *
  517. * Locking Note: The rport lock is expected to be held before
  518. * calling this routine
  519. */
  520. static void fc_rport_error_retry(struct fc_rport_priv *rdata,
  521. struct fc_frame *fp)
  522. {
  523. unsigned long delay = FC_DEF_E_D_TOV;
  524. /* make sure this isn't an FC_EX_CLOSED error, never retry those */
  525. if (PTR_ERR(fp) == -FC_EX_CLOSED)
  526. return fc_rport_error(rdata, fp);
  527. if (rdata->retries < rdata->local_port->max_rport_retry_count) {
  528. FC_RPORT_DBG(rdata, "Error %ld in state %s, retrying\n",
  529. PTR_ERR(fp), fc_rport_state(rdata));
  530. rdata->retries++;
  531. /* no additional delay on exchange timeouts */
  532. if (PTR_ERR(fp) == -FC_EX_TIMEOUT)
  533. delay = 0;
  534. schedule_delayed_work(&rdata->retry_work, delay);
  535. return;
  536. }
  537. return fc_rport_error(rdata, fp);
  538. }
  539. /**
  540. * fc_rport_login_complete() - Handle parameters and completion of p-mp login.
  541. * @rdata: The remote port which we logged into or which logged into us.
  542. * @fp: The FLOGI or PLOGI request or response frame
  543. *
  544. * Returns non-zero error if a problem is detected with the frame.
  545. * Does not free the frame.
  546. *
  547. * This is only used in point-to-multipoint mode for FIP currently.
  548. */
  549. static int fc_rport_login_complete(struct fc_rport_priv *rdata,
  550. struct fc_frame *fp)
  551. {
  552. struct fc_lport *lport = rdata->local_port;
  553. struct fc_els_flogi *flogi;
  554. unsigned int e_d_tov;
  555. u16 csp_flags;
  556. flogi = fc_frame_payload_get(fp, sizeof(*flogi));
  557. if (!flogi)
  558. return -EINVAL;
  559. csp_flags = ntohs(flogi->fl_csp.sp_features);
  560. if (fc_frame_payload_op(fp) == ELS_FLOGI) {
  561. if (csp_flags & FC_SP_FT_FPORT) {
  562. FC_RPORT_DBG(rdata, "Fabric bit set in FLOGI\n");
  563. return -EINVAL;
  564. }
  565. } else {
  566. /*
  567. * E_D_TOV is not valid on an incoming FLOGI request.
  568. */
  569. e_d_tov = ntohl(flogi->fl_csp.sp_e_d_tov);
  570. if (csp_flags & FC_SP_FT_EDTR)
  571. e_d_tov /= 1000000;
  572. if (e_d_tov > rdata->e_d_tov)
  573. rdata->e_d_tov = e_d_tov;
  574. }
  575. rdata->maxframe_size = fc_plogi_get_maxframe(flogi, lport->mfs);
  576. return 0;
  577. }
  578. /**
  579. * fc_rport_flogi_resp() - Handle response to FLOGI request for p-mp mode
  580. * @sp: The sequence that the FLOGI was on
  581. * @fp: The FLOGI response frame
  582. * @rp_arg: The remote port that received the FLOGI response
  583. */
  584. void fc_rport_flogi_resp(struct fc_seq *sp, struct fc_frame *fp,
  585. void *rp_arg)
  586. {
  587. struct fc_rport_priv *rdata = rp_arg;
  588. struct fc_lport *lport = rdata->local_port;
  589. struct fc_els_flogi *flogi;
  590. unsigned int r_a_tov;
  591. FC_RPORT_DBG(rdata, "Received a FLOGI %s\n", fc_els_resp_type(fp));
  592. if (fp == ERR_PTR(-FC_EX_CLOSED))
  593. return;
  594. mutex_lock(&rdata->rp_mutex);
  595. if (rdata->rp_state != RPORT_ST_FLOGI) {
  596. FC_RPORT_DBG(rdata, "Received a FLOGI response, but in state "
  597. "%s\n", fc_rport_state(rdata));
  598. if (IS_ERR(fp))
  599. goto err;
  600. goto out;
  601. }
  602. if (IS_ERR(fp)) {
  603. fc_rport_error(rdata, fp);
  604. goto err;
  605. }
  606. if (fc_frame_payload_op(fp) != ELS_LS_ACC)
  607. goto bad;
  608. if (fc_rport_login_complete(rdata, fp))
  609. goto bad;
  610. flogi = fc_frame_payload_get(fp, sizeof(*flogi));
  611. if (!flogi)
  612. goto bad;
  613. r_a_tov = ntohl(flogi->fl_csp.sp_r_a_tov);
  614. if (r_a_tov > rdata->r_a_tov)
  615. rdata->r_a_tov = r_a_tov;
  616. if (rdata->ids.port_name < lport->wwpn)
  617. fc_rport_enter_plogi(rdata);
  618. else
  619. fc_rport_state_enter(rdata, RPORT_ST_PLOGI_WAIT);
  620. out:
  621. fc_frame_free(fp);
  622. err:
  623. mutex_unlock(&rdata->rp_mutex);
  624. kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
  625. return;
  626. bad:
  627. FC_RPORT_DBG(rdata, "Bad FLOGI response\n");
  628. fc_rport_error_retry(rdata, fp);
  629. goto out;
  630. }
  631. /**
  632. * fc_rport_enter_flogi() - Send a FLOGI request to the remote port for p-mp
  633. * @rdata: The remote port to send a FLOGI to
  634. *
  635. * Locking Note: The rport lock is expected to be held before calling
  636. * this routine.
  637. */
  638. static void fc_rport_enter_flogi(struct fc_rport_priv *rdata)
  639. {
  640. struct fc_lport *lport = rdata->local_port;
  641. struct fc_frame *fp;
  642. if (!lport->point_to_multipoint)
  643. return fc_rport_enter_plogi(rdata);
  644. FC_RPORT_DBG(rdata, "Entered FLOGI state from %s state\n",
  645. fc_rport_state(rdata));
  646. fc_rport_state_enter(rdata, RPORT_ST_FLOGI);
  647. fp = fc_frame_alloc(lport, sizeof(struct fc_els_flogi));
  648. if (!fp)
  649. return fc_rport_error_retry(rdata, fp);
  650. if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_FLOGI,
  651. fc_rport_flogi_resp, rdata,
  652. 2 * lport->r_a_tov))
  653. fc_rport_error_retry(rdata, NULL);
  654. else
  655. kref_get(&rdata->kref);
  656. }
  657. /**
  658. * fc_rport_recv_flogi_req() - Handle Fabric Login (FLOGI) request in p-mp mode
  659. * @lport: The local port that received the PLOGI request
  660. * @sp: The sequence that the PLOGI request was on
  661. * @rx_fp: The PLOGI request frame
  662. */
  663. static void fc_rport_recv_flogi_req(struct fc_lport *lport,
  664. struct fc_seq *sp, struct fc_frame *rx_fp)
  665. {
  666. struct fc_disc *disc;
  667. struct fc_els_flogi *flp;
  668. struct fc_rport_priv *rdata;
  669. struct fc_frame *fp = rx_fp;
  670. struct fc_exch *ep;
  671. struct fc_frame_header *fh;
  672. struct fc_seq_els_data rjt_data;
  673. u32 sid, f_ctl;
  674. rjt_data.fp = NULL;
  675. fh = fc_frame_header_get(fp);
  676. sid = ntoh24(fh->fh_s_id);
  677. FC_RPORT_ID_DBG(lport, sid, "Received FLOGI request\n");
  678. disc = &lport->disc;
  679. mutex_lock(&disc->disc_mutex);
  680. if (!lport->point_to_multipoint) {
  681. rjt_data.reason = ELS_RJT_UNSUP;
  682. rjt_data.explan = ELS_EXPL_NONE;
  683. goto reject;
  684. }
  685. flp = fc_frame_payload_get(fp, sizeof(*flp));
  686. if (!flp) {
  687. rjt_data.reason = ELS_RJT_LOGIC;
  688. rjt_data.explan = ELS_EXPL_INV_LEN;
  689. goto reject;
  690. }
  691. rdata = lport->tt.rport_lookup(lport, sid);
  692. if (!rdata) {
  693. rjt_data.reason = ELS_RJT_FIP;
  694. rjt_data.explan = ELS_EXPL_NOT_NEIGHBOR;
  695. goto reject;
  696. }
  697. mutex_lock(&rdata->rp_mutex);
  698. FC_RPORT_DBG(rdata, "Received FLOGI in %s state\n",
  699. fc_rport_state(rdata));
  700. switch (rdata->rp_state) {
  701. case RPORT_ST_INIT:
  702. case RPORT_ST_LOGO:
  703. case RPORT_ST_DELETE:
  704. mutex_unlock(&rdata->rp_mutex);
  705. rjt_data.reason = ELS_RJT_FIP;
  706. rjt_data.explan = ELS_EXPL_NOT_NEIGHBOR;
  707. goto reject;
  708. case RPORT_ST_FLOGI:
  709. case RPORT_ST_PLOGI_WAIT:
  710. case RPORT_ST_PLOGI:
  711. break;
  712. case RPORT_ST_PRLI:
  713. case RPORT_ST_RTV:
  714. case RPORT_ST_READY:
  715. case RPORT_ST_ADISC:
  716. /*
  717. * Set the remote port to be deleted and to then restart.
  718. * This queues work to be sure exchanges are reset.
  719. */
  720. fc_rport_enter_delete(rdata, RPORT_EV_LOGO);
  721. mutex_unlock(&rdata->rp_mutex);
  722. rjt_data.reason = ELS_RJT_BUSY;
  723. rjt_data.explan = ELS_EXPL_NONE;
  724. goto reject;
  725. }
  726. if (fc_rport_login_complete(rdata, fp)) {
  727. mutex_unlock(&rdata->rp_mutex);
  728. rjt_data.reason = ELS_RJT_LOGIC;
  729. rjt_data.explan = ELS_EXPL_NONE;
  730. goto reject;
  731. }
  732. fc_frame_free(rx_fp);
  733. fp = fc_frame_alloc(lport, sizeof(*flp));
  734. if (!fp)
  735. goto out;
  736. sp = lport->tt.seq_start_next(sp);
  737. fc_flogi_fill(lport, fp);
  738. flp = fc_frame_payload_get(fp, sizeof(*flp));
  739. flp->fl_cmd = ELS_LS_ACC;
  740. f_ctl = FC_FC_EX_CTX | FC_FC_LAST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT;
  741. ep = fc_seq_exch(sp);
  742. fc_fill_fc_hdr(fp, FC_RCTL_ELS_REP, ep->did, ep->sid,
  743. FC_TYPE_ELS, f_ctl, 0);
  744. lport->tt.seq_send(lport, sp, fp);
  745. if (rdata->ids.port_name < lport->wwpn)
  746. fc_rport_enter_plogi(rdata);
  747. else
  748. fc_rport_state_enter(rdata, RPORT_ST_PLOGI_WAIT);
  749. out:
  750. mutex_unlock(&rdata->rp_mutex);
  751. mutex_unlock(&disc->disc_mutex);
  752. return;
  753. reject:
  754. mutex_unlock(&disc->disc_mutex);
  755. lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
  756. fc_frame_free(fp);
  757. }
  758. /**
  759. * fc_rport_plogi_resp() - Handler for ELS PLOGI responses
  760. * @sp: The sequence the PLOGI is on
  761. * @fp: The PLOGI response frame
  762. * @rdata_arg: The remote port that sent the PLOGI response
  763. *
  764. * Locking Note: This function will be called without the rport lock
  765. * held, but it will lock, call an _enter_* function or fc_rport_error
  766. * and then unlock the rport.
  767. */
  768. static void fc_rport_plogi_resp(struct fc_seq *sp, struct fc_frame *fp,
  769. void *rdata_arg)
  770. {
  771. struct fc_rport_priv *rdata = rdata_arg;
  772. struct fc_lport *lport = rdata->local_port;
  773. struct fc_els_flogi *plp = NULL;
  774. u16 csp_seq;
  775. u16 cssp_seq;
  776. u8 op;
  777. mutex_lock(&rdata->rp_mutex);
  778. FC_RPORT_DBG(rdata, "Received a PLOGI %s\n", fc_els_resp_type(fp));
  779. if (rdata->rp_state != RPORT_ST_PLOGI) {
  780. FC_RPORT_DBG(rdata, "Received a PLOGI response, but in state "
  781. "%s\n", fc_rport_state(rdata));
  782. if (IS_ERR(fp))
  783. goto err;
  784. goto out;
  785. }
  786. if (IS_ERR(fp)) {
  787. fc_rport_error_retry(rdata, fp);
  788. goto err;
  789. }
  790. op = fc_frame_payload_op(fp);
  791. if (op == ELS_LS_ACC &&
  792. (plp = fc_frame_payload_get(fp, sizeof(*plp))) != NULL) {
  793. rdata->ids.port_name = get_unaligned_be64(&plp->fl_wwpn);
  794. rdata->ids.node_name = get_unaligned_be64(&plp->fl_wwnn);
  795. if (lport->point_to_multipoint)
  796. fc_rport_login_complete(rdata, fp);
  797. csp_seq = ntohs(plp->fl_csp.sp_tot_seq);
  798. cssp_seq = ntohs(plp->fl_cssp[3 - 1].cp_con_seq);
  799. if (cssp_seq < csp_seq)
  800. csp_seq = cssp_seq;
  801. rdata->max_seq = csp_seq;
  802. rdata->maxframe_size = fc_plogi_get_maxframe(plp, lport->mfs);
  803. fc_rport_enter_prli(rdata);
  804. } else
  805. fc_rport_error_retry(rdata, fp);
  806. out:
  807. fc_frame_free(fp);
  808. err:
  809. mutex_unlock(&rdata->rp_mutex);
  810. kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
  811. }
  812. /**
  813. * fc_rport_enter_plogi() - Send Port Login (PLOGI) request
  814. * @rdata: The remote port to send a PLOGI to
  815. *
  816. * Locking Note: The rport lock is expected to be held before calling
  817. * this routine.
  818. */
  819. static void fc_rport_enter_plogi(struct fc_rport_priv *rdata)
  820. {
  821. struct fc_lport *lport = rdata->local_port;
  822. struct fc_frame *fp;
  823. FC_RPORT_DBG(rdata, "Port entered PLOGI state from %s state\n",
  824. fc_rport_state(rdata));
  825. fc_rport_state_enter(rdata, RPORT_ST_PLOGI);
  826. rdata->maxframe_size = FC_MIN_MAX_PAYLOAD;
  827. fp = fc_frame_alloc(lport, sizeof(struct fc_els_flogi));
  828. if (!fp) {
  829. FC_RPORT_DBG(rdata, "%s frame alloc failed\n", __func__);
  830. fc_rport_error_retry(rdata, fp);
  831. return;
  832. }
  833. rdata->e_d_tov = lport->e_d_tov;
  834. if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_PLOGI,
  835. fc_rport_plogi_resp, rdata,
  836. 2 * lport->r_a_tov))
  837. fc_rport_error_retry(rdata, NULL);
  838. else
  839. kref_get(&rdata->kref);
  840. }
  841. /**
  842. * fc_rport_prli_resp() - Process Login (PRLI) response handler
  843. * @sp: The sequence the PRLI response was on
  844. * @fp: The PRLI response frame
  845. * @rdata_arg: The remote port that sent the PRLI response
  846. *
  847. * Locking Note: This function will be called without the rport lock
  848. * held, but it will lock, call an _enter_* function or fc_rport_error
  849. * and then unlock the rport.
  850. */
  851. static void fc_rport_prli_resp(struct fc_seq *sp, struct fc_frame *fp,
  852. void *rdata_arg)
  853. {
  854. struct fc_rport_priv *rdata = rdata_arg;
  855. struct {
  856. struct fc_els_prli prli;
  857. struct fc_els_spp spp;
  858. } *pp;
  859. u32 roles = FC_RPORT_ROLE_UNKNOWN;
  860. u32 fcp_parm = 0;
  861. u8 op;
  862. u8 resp_code = 0;
  863. mutex_lock(&rdata->rp_mutex);
  864. FC_RPORT_DBG(rdata, "Received a PRLI %s\n", fc_els_resp_type(fp));
  865. if (rdata->rp_state != RPORT_ST_PRLI) {
  866. FC_RPORT_DBG(rdata, "Received a PRLI response, but in state "
  867. "%s\n", fc_rport_state(rdata));
  868. if (IS_ERR(fp))
  869. goto err;
  870. goto out;
  871. }
  872. if (IS_ERR(fp)) {
  873. fc_rport_error_retry(rdata, fp);
  874. goto err;
  875. }
  876. /* reinitialize remote port roles */
  877. rdata->ids.roles = FC_RPORT_ROLE_UNKNOWN;
  878. op = fc_frame_payload_op(fp);
  879. if (op == ELS_LS_ACC) {
  880. pp = fc_frame_payload_get(fp, sizeof(*pp));
  881. if (!pp)
  882. goto out;
  883. resp_code = (pp->spp.spp_flags & FC_SPP_RESP_MASK);
  884. FC_RPORT_DBG(rdata, "PRLI spp_flags = 0x%x\n",
  885. pp->spp.spp_flags);
  886. if (resp_code != FC_SPP_RESP_ACK) {
  887. if (resp_code == FC_SPP_RESP_CONF)
  888. fc_rport_error(rdata, fp);
  889. else
  890. fc_rport_error_retry(rdata, fp);
  891. goto out;
  892. }
  893. if (pp->prli.prli_spp_len < sizeof(pp->spp))
  894. goto out;
  895. fcp_parm = ntohl(pp->spp.spp_params);
  896. if (fcp_parm & FCP_SPPF_RETRY)
  897. rdata->flags |= FC_RP_FLAGS_RETRY;
  898. rdata->supported_classes = FC_COS_CLASS3;
  899. if (fcp_parm & FCP_SPPF_INIT_FCN)
  900. roles |= FC_RPORT_ROLE_FCP_INITIATOR;
  901. if (fcp_parm & FCP_SPPF_TARG_FCN)
  902. roles |= FC_RPORT_ROLE_FCP_TARGET;
  903. rdata->ids.roles = roles;
  904. fc_rport_enter_rtv(rdata);
  905. } else {
  906. FC_RPORT_DBG(rdata, "Bad ELS response for PRLI command\n");
  907. fc_rport_error_retry(rdata, fp);
  908. }
  909. out:
  910. fc_frame_free(fp);
  911. err:
  912. mutex_unlock(&rdata->rp_mutex);
  913. kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
  914. }
  915. /**
  916. * fc_rport_logo_resp() - Handler for logout (LOGO) responses
  917. * @sp: The sequence the LOGO was on
  918. * @fp: The LOGO response frame
  919. * @rdata_arg: The remote port that sent the LOGO response
  920. *
  921. * Locking Note: This function will be called without the rport lock
  922. * held, but it will lock, call an _enter_* function or fc_rport_error
  923. * and then unlock the rport.
  924. */
  925. static void fc_rport_logo_resp(struct fc_seq *sp, struct fc_frame *fp,
  926. void *rdata_arg)
  927. {
  928. struct fc_rport_priv *rdata = rdata_arg;
  929. u8 op;
  930. mutex_lock(&rdata->rp_mutex);
  931. FC_RPORT_DBG(rdata, "Received a LOGO %s\n", fc_els_resp_type(fp));
  932. if (rdata->rp_state != RPORT_ST_LOGO) {
  933. FC_RPORT_DBG(rdata, "Received a LOGO response, but in state "
  934. "%s\n", fc_rport_state(rdata));
  935. if (IS_ERR(fp))
  936. goto err;
  937. goto out;
  938. }
  939. if (IS_ERR(fp)) {
  940. fc_rport_error_retry(rdata, fp);
  941. goto err;
  942. }
  943. op = fc_frame_payload_op(fp);
  944. if (op != ELS_LS_ACC)
  945. FC_RPORT_DBG(rdata, "Bad ELS response op %x for LOGO command\n",
  946. op);
  947. fc_rport_enter_delete(rdata, RPORT_EV_LOGO);
  948. out:
  949. fc_frame_free(fp);
  950. err:
  951. mutex_unlock(&rdata->rp_mutex);
  952. kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
  953. }
  954. /**
  955. * fc_rport_enter_prli() - Send Process Login (PRLI) request
  956. * @rdata: The remote port to send the PRLI request to
  957. *
  958. * Locking Note: The rport lock is expected to be held before calling
  959. * this routine.
  960. */
  961. static void fc_rport_enter_prli(struct fc_rport_priv *rdata)
  962. {
  963. struct fc_lport *lport = rdata->local_port;
  964. struct {
  965. struct fc_els_prli prli;
  966. struct fc_els_spp spp;
  967. } *pp;
  968. struct fc_frame *fp;
  969. /*
  970. * If the rport is one of the well known addresses
  971. * we skip PRLI and RTV and go straight to READY.
  972. */
  973. if (rdata->ids.port_id >= FC_FID_DOM_MGR) {
  974. fc_rport_enter_ready(rdata);
  975. return;
  976. }
  977. FC_RPORT_DBG(rdata, "Port entered PRLI state from %s state\n",
  978. fc_rport_state(rdata));
  979. fc_rport_state_enter(rdata, RPORT_ST_PRLI);
  980. fp = fc_frame_alloc(lport, sizeof(*pp));
  981. if (!fp) {
  982. fc_rport_error_retry(rdata, fp);
  983. return;
  984. }
  985. if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_PRLI,
  986. fc_rport_prli_resp, rdata,
  987. 2 * lport->r_a_tov))
  988. fc_rport_error_retry(rdata, NULL);
  989. else
  990. kref_get(&rdata->kref);
  991. }
  992. /**
  993. * fc_rport_els_rtv_resp() - Handler for Request Timeout Value (RTV) responses
  994. * @sp: The sequence the RTV was on
  995. * @fp: The RTV response frame
  996. * @rdata_arg: The remote port that sent the RTV response
  997. *
  998. * Many targets don't seem to support this.
  999. *
  1000. * Locking Note: This function will be called without the rport lock
  1001. * held, but it will lock, call an _enter_* function or fc_rport_error
  1002. * and then unlock the rport.
  1003. */
  1004. static void fc_rport_rtv_resp(struct fc_seq *sp, struct fc_frame *fp,
  1005. void *rdata_arg)
  1006. {
  1007. struct fc_rport_priv *rdata = rdata_arg;
  1008. u8 op;
  1009. mutex_lock(&rdata->rp_mutex);
  1010. FC_RPORT_DBG(rdata, "Received a RTV %s\n", fc_els_resp_type(fp));
  1011. if (rdata->rp_state != RPORT_ST_RTV) {
  1012. FC_RPORT_DBG(rdata, "Received a RTV response, but in state "
  1013. "%s\n", fc_rport_state(rdata));
  1014. if (IS_ERR(fp))
  1015. goto err;
  1016. goto out;
  1017. }
  1018. if (IS_ERR(fp)) {
  1019. fc_rport_error(rdata, fp);
  1020. goto err;
  1021. }
  1022. op = fc_frame_payload_op(fp);
  1023. if (op == ELS_LS_ACC) {
  1024. struct fc_els_rtv_acc *rtv;
  1025. u32 toq;
  1026. u32 tov;
  1027. rtv = fc_frame_payload_get(fp, sizeof(*rtv));
  1028. if (rtv) {
  1029. toq = ntohl(rtv->rtv_toq);
  1030. tov = ntohl(rtv->rtv_r_a_tov);
  1031. if (tov == 0)
  1032. tov = 1;
  1033. rdata->r_a_tov = tov;
  1034. tov = ntohl(rtv->rtv_e_d_tov);
  1035. if (toq & FC_ELS_RTV_EDRES)
  1036. tov /= 1000000;
  1037. if (tov == 0)
  1038. tov = 1;
  1039. rdata->e_d_tov = tov;
  1040. }
  1041. }
  1042. fc_rport_enter_ready(rdata);
  1043. out:
  1044. fc_frame_free(fp);
  1045. err:
  1046. mutex_unlock(&rdata->rp_mutex);
  1047. kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
  1048. }
  1049. /**
  1050. * fc_rport_enter_rtv() - Send Request Timeout Value (RTV) request
  1051. * @rdata: The remote port to send the RTV request to
  1052. *
  1053. * Locking Note: The rport lock is expected to be held before calling
  1054. * this routine.
  1055. */
  1056. static void fc_rport_enter_rtv(struct fc_rport_priv *rdata)
  1057. {
  1058. struct fc_frame *fp;
  1059. struct fc_lport *lport = rdata->local_port;
  1060. FC_RPORT_DBG(rdata, "Port entered RTV state from %s state\n",
  1061. fc_rport_state(rdata));
  1062. fc_rport_state_enter(rdata, RPORT_ST_RTV);
  1063. fp = fc_frame_alloc(lport, sizeof(struct fc_els_rtv));
  1064. if (!fp) {
  1065. fc_rport_error_retry(rdata, fp);
  1066. return;
  1067. }
  1068. if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_RTV,
  1069. fc_rport_rtv_resp, rdata,
  1070. 2 * lport->r_a_tov))
  1071. fc_rport_error_retry(rdata, NULL);
  1072. else
  1073. kref_get(&rdata->kref);
  1074. }
  1075. /**
  1076. * fc_rport_enter_logo() - Send a logout (LOGO) request
  1077. * @rdata: The remote port to send the LOGO request to
  1078. *
  1079. * Locking Note: The rport lock is expected to be held before calling
  1080. * this routine.
  1081. */
  1082. static void fc_rport_enter_logo(struct fc_rport_priv *rdata)
  1083. {
  1084. struct fc_lport *lport = rdata->local_port;
  1085. struct fc_frame *fp;
  1086. FC_RPORT_DBG(rdata, "Port entered LOGO state from %s state\n",
  1087. fc_rport_state(rdata));
  1088. fc_rport_state_enter(rdata, RPORT_ST_LOGO);
  1089. fp = fc_frame_alloc(lport, sizeof(struct fc_els_logo));
  1090. if (!fp) {
  1091. fc_rport_error_retry(rdata, fp);
  1092. return;
  1093. }
  1094. if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_LOGO,
  1095. fc_rport_logo_resp, rdata,
  1096. 2 * lport->r_a_tov))
  1097. fc_rport_error_retry(rdata, NULL);
  1098. else
  1099. kref_get(&rdata->kref);
  1100. }
  1101. /**
  1102. * fc_rport_els_adisc_resp() - Handler for Address Discovery (ADISC) responses
  1103. * @sp: The sequence the ADISC response was on
  1104. * @fp: The ADISC response frame
  1105. * @rdata_arg: The remote port that sent the ADISC response
  1106. *
  1107. * Locking Note: This function will be called without the rport lock
  1108. * held, but it will lock, call an _enter_* function or fc_rport_error
  1109. * and then unlock the rport.
  1110. */
  1111. static void fc_rport_adisc_resp(struct fc_seq *sp, struct fc_frame *fp,
  1112. void *rdata_arg)
  1113. {
  1114. struct fc_rport_priv *rdata = rdata_arg;
  1115. struct fc_els_adisc *adisc;
  1116. u8 op;
  1117. mutex_lock(&rdata->rp_mutex);
  1118. FC_RPORT_DBG(rdata, "Received a ADISC response\n");
  1119. if (rdata->rp_state != RPORT_ST_ADISC) {
  1120. FC_RPORT_DBG(rdata, "Received a ADISC resp but in state %s\n",
  1121. fc_rport_state(rdata));
  1122. if (IS_ERR(fp))
  1123. goto err;
  1124. goto out;
  1125. }
  1126. if (IS_ERR(fp)) {
  1127. fc_rport_error(rdata, fp);
  1128. goto err;
  1129. }
  1130. /*
  1131. * If address verification failed. Consider us logged out of the rport.
  1132. * Since the rport is still in discovery, we want to be
  1133. * logged in, so go to PLOGI state. Otherwise, go back to READY.
  1134. */
  1135. op = fc_frame_payload_op(fp);
  1136. adisc = fc_frame_payload_get(fp, sizeof(*adisc));
  1137. if (op != ELS_LS_ACC || !adisc ||
  1138. ntoh24(adisc->adisc_port_id) != rdata->ids.port_id ||
  1139. get_unaligned_be64(&adisc->adisc_wwpn) != rdata->ids.port_name ||
  1140. get_unaligned_be64(&adisc->adisc_wwnn) != rdata->ids.node_name) {
  1141. FC_RPORT_DBG(rdata, "ADISC error or mismatch\n");
  1142. fc_rport_enter_flogi(rdata);
  1143. } else {
  1144. FC_RPORT_DBG(rdata, "ADISC OK\n");
  1145. fc_rport_enter_ready(rdata);
  1146. }
  1147. out:
  1148. fc_frame_free(fp);
  1149. err:
  1150. mutex_unlock(&rdata->rp_mutex);
  1151. kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
  1152. }
  1153. /**
  1154. * fc_rport_enter_adisc() - Send Address Discover (ADISC) request
  1155. * @rdata: The remote port to send the ADISC request to
  1156. *
  1157. * Locking Note: The rport lock is expected to be held before calling
  1158. * this routine.
  1159. */
  1160. static void fc_rport_enter_adisc(struct fc_rport_priv *rdata)
  1161. {
  1162. struct fc_lport *lport = rdata->local_port;
  1163. struct fc_frame *fp;
  1164. FC_RPORT_DBG(rdata, "sending ADISC from %s state\n",
  1165. fc_rport_state(rdata));
  1166. fc_rport_state_enter(rdata, RPORT_ST_ADISC);
  1167. fp = fc_frame_alloc(lport, sizeof(struct fc_els_adisc));
  1168. if (!fp) {
  1169. fc_rport_error_retry(rdata, fp);
  1170. return;
  1171. }
  1172. if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_ADISC,
  1173. fc_rport_adisc_resp, rdata,
  1174. 2 * lport->r_a_tov))
  1175. fc_rport_error_retry(rdata, NULL);
  1176. else
  1177. kref_get(&rdata->kref);
  1178. }
  1179. /**
  1180. * fc_rport_recv_adisc_req() - Handler for Address Discovery (ADISC) requests
  1181. * @rdata: The remote port that sent the ADISC request
  1182. * @sp: The sequence the ADISC request was on
  1183. * @in_fp: The ADISC request frame
  1184. *
  1185. * Locking Note: Called with the lport and rport locks held.
  1186. */
  1187. static void fc_rport_recv_adisc_req(struct fc_rport_priv *rdata,
  1188. struct fc_seq *sp, struct fc_frame *in_fp)
  1189. {
  1190. struct fc_lport *lport = rdata->local_port;
  1191. struct fc_frame *fp;
  1192. struct fc_exch *ep = fc_seq_exch(sp);
  1193. struct fc_els_adisc *adisc;
  1194. struct fc_seq_els_data rjt_data;
  1195. u32 f_ctl;
  1196. FC_RPORT_DBG(rdata, "Received ADISC request\n");
  1197. adisc = fc_frame_payload_get(in_fp, sizeof(*adisc));
  1198. if (!adisc) {
  1199. rjt_data.fp = NULL;
  1200. rjt_data.reason = ELS_RJT_PROT;
  1201. rjt_data.explan = ELS_EXPL_INV_LEN;
  1202. lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
  1203. goto drop;
  1204. }
  1205. fp = fc_frame_alloc(lport, sizeof(*adisc));
  1206. if (!fp)
  1207. goto drop;
  1208. fc_adisc_fill(lport, fp);
  1209. adisc = fc_frame_payload_get(fp, sizeof(*adisc));
  1210. adisc->adisc_cmd = ELS_LS_ACC;
  1211. sp = lport->tt.seq_start_next(sp);
  1212. f_ctl = FC_FC_EX_CTX | FC_FC_LAST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT;
  1213. fc_fill_fc_hdr(fp, FC_RCTL_ELS_REP, ep->did, ep->sid,
  1214. FC_TYPE_ELS, f_ctl, 0);
  1215. lport->tt.seq_send(lport, sp, fp);
  1216. drop:
  1217. fc_frame_free(in_fp);
  1218. }
  1219. /**
  1220. * fc_rport_recv_rls_req() - Handle received Read Link Status request
  1221. * @rdata: The remote port that sent the RLS request
  1222. * @sp: The sequence that the RLS was on
  1223. * @rx_fp: The PRLI request frame
  1224. *
  1225. * Locking Note: The rport lock is expected to be held before calling
  1226. * this function.
  1227. */
  1228. static void fc_rport_recv_rls_req(struct fc_rport_priv *rdata,
  1229. struct fc_seq *sp, struct fc_frame *rx_fp)
  1230. {
  1231. struct fc_lport *lport = rdata->local_port;
  1232. struct fc_frame *fp;
  1233. struct fc_exch *ep = fc_seq_exch(sp);
  1234. struct fc_els_rls *rls;
  1235. struct fc_els_rls_resp *rsp;
  1236. struct fc_els_lesb *lesb;
  1237. struct fc_seq_els_data rjt_data;
  1238. struct fc_host_statistics *hst;
  1239. u32 f_ctl;
  1240. FC_RPORT_DBG(rdata, "Received RLS request while in state %s\n",
  1241. fc_rport_state(rdata));
  1242. rls = fc_frame_payload_get(rx_fp, sizeof(*rls));
  1243. if (!rls) {
  1244. rjt_data.reason = ELS_RJT_PROT;
  1245. rjt_data.explan = ELS_EXPL_INV_LEN;
  1246. goto out_rjt;
  1247. }
  1248. fp = fc_frame_alloc(lport, sizeof(*rsp));
  1249. if (!fp) {
  1250. rjt_data.reason = ELS_RJT_UNAB;
  1251. rjt_data.explan = ELS_EXPL_INSUF_RES;
  1252. goto out_rjt;
  1253. }
  1254. rsp = fc_frame_payload_get(fp, sizeof(*rsp));
  1255. memset(rsp, 0, sizeof(*rsp));
  1256. rsp->rls_cmd = ELS_LS_ACC;
  1257. lesb = &rsp->rls_lesb;
  1258. if (lport->tt.get_lesb) {
  1259. /* get LESB from LLD if it supports it */
  1260. lport->tt.get_lesb(lport, lesb);
  1261. } else {
  1262. fc_get_host_stats(lport->host);
  1263. hst = &lport->host_stats;
  1264. lesb->lesb_link_fail = htonl(hst->link_failure_count);
  1265. lesb->lesb_sync_loss = htonl(hst->loss_of_sync_count);
  1266. lesb->lesb_sig_loss = htonl(hst->loss_of_signal_count);
  1267. lesb->lesb_prim_err = htonl(hst->prim_seq_protocol_err_count);
  1268. lesb->lesb_inv_word = htonl(hst->invalid_tx_word_count);
  1269. lesb->lesb_inv_crc = htonl(hst->invalid_crc_count);
  1270. }
  1271. sp = lport->tt.seq_start_next(sp);
  1272. f_ctl = FC_FC_EX_CTX | FC_FC_LAST_SEQ | FC_FC_END_SEQ;
  1273. fc_fill_fc_hdr(fp, FC_RCTL_ELS_REP, ep->did, ep->sid,
  1274. FC_TYPE_ELS, f_ctl, 0);
  1275. lport->tt.seq_send(lport, sp, fp);
  1276. goto out;
  1277. out_rjt:
  1278. rjt_data.fp = NULL;
  1279. lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
  1280. out:
  1281. fc_frame_free(rx_fp);
  1282. }
  1283. /**
  1284. * fc_rport_recv_els_req() - Handler for validated ELS requests
  1285. * @lport: The local port that received the ELS request
  1286. * @sp: The sequence that the ELS request was on
  1287. * @fp: The ELS request frame
  1288. *
  1289. * Handle incoming ELS requests that require port login.
  1290. * The ELS opcode has already been validated by the caller.
  1291. *
  1292. * Locking Note: Called with the lport lock held.
  1293. */
  1294. static void fc_rport_recv_els_req(struct fc_lport *lport,
  1295. struct fc_seq *sp, struct fc_frame *fp)
  1296. {
  1297. struct fc_rport_priv *rdata;
  1298. struct fc_frame_header *fh;
  1299. struct fc_seq_els_data els_data;
  1300. els_data.fp = NULL;
  1301. els_data.reason = ELS_RJT_UNAB;
  1302. els_data.explan = ELS_EXPL_PLOGI_REQD;
  1303. fh = fc_frame_header_get(fp);
  1304. mutex_lock(&lport->disc.disc_mutex);
  1305. rdata = lport->tt.rport_lookup(lport, ntoh24(fh->fh_s_id));
  1306. if (!rdata) {
  1307. mutex_unlock(&lport->disc.disc_mutex);
  1308. goto reject;
  1309. }
  1310. mutex_lock(&rdata->rp_mutex);
  1311. mutex_unlock(&lport->disc.disc_mutex);
  1312. switch (rdata->rp_state) {
  1313. case RPORT_ST_PRLI:
  1314. case RPORT_ST_RTV:
  1315. case RPORT_ST_READY:
  1316. case RPORT_ST_ADISC:
  1317. break;
  1318. default:
  1319. mutex_unlock(&rdata->rp_mutex);
  1320. goto reject;
  1321. }
  1322. switch (fc_frame_payload_op(fp)) {
  1323. case ELS_PRLI:
  1324. fc_rport_recv_prli_req(rdata, sp, fp);
  1325. break;
  1326. case ELS_PRLO:
  1327. fc_rport_recv_prlo_req(rdata, sp, fp);
  1328. break;
  1329. case ELS_ADISC:
  1330. fc_rport_recv_adisc_req(rdata, sp, fp);
  1331. break;
  1332. case ELS_RRQ:
  1333. els_data.fp = fp;
  1334. lport->tt.seq_els_rsp_send(sp, ELS_RRQ, &els_data);
  1335. break;
  1336. case ELS_REC:
  1337. els_data.fp = fp;
  1338. lport->tt.seq_els_rsp_send(sp, ELS_REC, &els_data);
  1339. break;
  1340. case ELS_RLS:
  1341. fc_rport_recv_rls_req(rdata, sp, fp);
  1342. break;
  1343. default:
  1344. fc_frame_free(fp); /* can't happen */
  1345. break;
  1346. }
  1347. mutex_unlock(&rdata->rp_mutex);
  1348. return;
  1349. reject:
  1350. lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &els_data);
  1351. fc_frame_free(fp);
  1352. }
  1353. /**
  1354. * fc_rport_recv_req() - Handler for requests
  1355. * @sp: The sequence the request was on
  1356. * @fp: The request frame
  1357. * @lport: The local port that received the request
  1358. *
  1359. * Locking Note: Called with the lport lock held.
  1360. */
  1361. void fc_rport_recv_req(struct fc_seq *sp, struct fc_frame *fp,
  1362. struct fc_lport *lport)
  1363. {
  1364. struct fc_seq_els_data els_data;
  1365. /*
  1366. * Handle FLOGI, PLOGI and LOGO requests separately, since they
  1367. * don't require prior login.
  1368. * Check for unsupported opcodes first and reject them.
  1369. * For some ops, it would be incorrect to reject with "PLOGI required".
  1370. */
  1371. switch (fc_frame_payload_op(fp)) {
  1372. case ELS_FLOGI:
  1373. fc_rport_recv_flogi_req(lport, sp, fp);
  1374. break;
  1375. case ELS_PLOGI:
  1376. fc_rport_recv_plogi_req(lport, sp, fp);
  1377. break;
  1378. case ELS_LOGO:
  1379. fc_rport_recv_logo_req(lport, sp, fp);
  1380. break;
  1381. case ELS_PRLI:
  1382. case ELS_PRLO:
  1383. case ELS_ADISC:
  1384. case ELS_RRQ:
  1385. case ELS_REC:
  1386. case ELS_RLS:
  1387. fc_rport_recv_els_req(lport, sp, fp);
  1388. break;
  1389. default:
  1390. fc_frame_free(fp);
  1391. els_data.fp = NULL;
  1392. els_data.reason = ELS_RJT_UNSUP;
  1393. els_data.explan = ELS_EXPL_NONE;
  1394. lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &els_data);
  1395. break;
  1396. }
  1397. }
  1398. /**
  1399. * fc_rport_recv_plogi_req() - Handler for Port Login (PLOGI) requests
  1400. * @lport: The local port that received the PLOGI request
  1401. * @sp: The sequence that the PLOGI request was on
  1402. * @rx_fp: The PLOGI request frame
  1403. *
  1404. * Locking Note: The rport lock is held before calling this function.
  1405. */
  1406. static void fc_rport_recv_plogi_req(struct fc_lport *lport,
  1407. struct fc_seq *sp, struct fc_frame *rx_fp)
  1408. {
  1409. struct fc_disc *disc;
  1410. struct fc_rport_priv *rdata;
  1411. struct fc_frame *fp = rx_fp;
  1412. struct fc_exch *ep;
  1413. struct fc_frame_header *fh;
  1414. struct fc_els_flogi *pl;
  1415. struct fc_seq_els_data rjt_data;
  1416. u32 sid, f_ctl;
  1417. rjt_data.fp = NULL;
  1418. fh = fc_frame_header_get(fp);
  1419. sid = ntoh24(fh->fh_s_id);
  1420. FC_RPORT_ID_DBG(lport, sid, "Received PLOGI request\n");
  1421. pl = fc_frame_payload_get(fp, sizeof(*pl));
  1422. if (!pl) {
  1423. FC_RPORT_ID_DBG(lport, sid, "Received PLOGI too short\n");
  1424. rjt_data.reason = ELS_RJT_PROT;
  1425. rjt_data.explan = ELS_EXPL_INV_LEN;
  1426. goto reject;
  1427. }
  1428. disc = &lport->disc;
  1429. mutex_lock(&disc->disc_mutex);
  1430. rdata = lport->tt.rport_create(lport, sid);
  1431. if (!rdata) {
  1432. mutex_unlock(&disc->disc_mutex);
  1433. rjt_data.reason = ELS_RJT_UNAB;
  1434. rjt_data.explan = ELS_EXPL_INSUF_RES;
  1435. goto reject;
  1436. }
  1437. mutex_lock(&rdata->rp_mutex);
  1438. mutex_unlock(&disc->disc_mutex);
  1439. rdata->ids.port_name = get_unaligned_be64(&pl->fl_wwpn);
  1440. rdata->ids.node_name = get_unaligned_be64(&pl->fl_wwnn);
  1441. /*
  1442. * If the rport was just created, possibly due to the incoming PLOGI,
  1443. * set the state appropriately and accept the PLOGI.
  1444. *
  1445. * If we had also sent a PLOGI, and if the received PLOGI is from a
  1446. * higher WWPN, we accept it, otherwise an LS_RJT is sent with reason
  1447. * "command already in progress".
  1448. *
  1449. * XXX TBD: If the session was ready before, the PLOGI should result in
  1450. * all outstanding exchanges being reset.
  1451. */
  1452. switch (rdata->rp_state) {
  1453. case RPORT_ST_INIT:
  1454. FC_RPORT_DBG(rdata, "Received PLOGI in INIT state\n");
  1455. break;
  1456. case RPORT_ST_PLOGI_WAIT:
  1457. FC_RPORT_DBG(rdata, "Received PLOGI in PLOGI_WAIT state\n");
  1458. break;
  1459. case RPORT_ST_PLOGI:
  1460. FC_RPORT_DBG(rdata, "Received PLOGI in PLOGI state\n");
  1461. if (rdata->ids.port_name < lport->wwpn) {
  1462. mutex_unlock(&rdata->rp_mutex);
  1463. rjt_data.reason = ELS_RJT_INPROG;
  1464. rjt_data.explan = ELS_EXPL_NONE;
  1465. goto reject;
  1466. }
  1467. break;
  1468. case RPORT_ST_PRLI:
  1469. case RPORT_ST_RTV:
  1470. case RPORT_ST_READY:
  1471. case RPORT_ST_ADISC:
  1472. FC_RPORT_DBG(rdata, "Received PLOGI in logged-in state %d "
  1473. "- ignored for now\n", rdata->rp_state);
  1474. /* XXX TBD - should reset */
  1475. break;
  1476. case RPORT_ST_FLOGI:
  1477. case RPORT_ST_DELETE:
  1478. case RPORT_ST_LOGO:
  1479. FC_RPORT_DBG(rdata, "Received PLOGI in state %s - send busy\n",
  1480. fc_rport_state(rdata));
  1481. mutex_unlock(&rdata->rp_mutex);
  1482. rjt_data.reason = ELS_RJT_BUSY;
  1483. rjt_data.explan = ELS_EXPL_NONE;
  1484. goto reject;
  1485. }
  1486. /*
  1487. * Get session payload size from incoming PLOGI.
  1488. */
  1489. rdata->maxframe_size = fc_plogi_get_maxframe(pl, lport->mfs);
  1490. fc_frame_free(rx_fp);
  1491. /*
  1492. * Send LS_ACC. If this fails, the originator should retry.
  1493. */
  1494. sp = lport->tt.seq_start_next(sp);
  1495. if (!sp)
  1496. goto out;
  1497. fp = fc_frame_alloc(lport, sizeof(*pl));
  1498. if (!fp)
  1499. goto out;
  1500. fc_plogi_fill(lport, fp, ELS_LS_ACC);
  1501. f_ctl = FC_FC_EX_CTX | FC_FC_LAST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT;
  1502. ep = fc_seq_exch(sp);
  1503. fc_fill_fc_hdr(fp, FC_RCTL_ELS_REP, ep->did, ep->sid,
  1504. FC_TYPE_ELS, f_ctl, 0);
  1505. lport->tt.seq_send(lport, sp, fp);
  1506. fc_rport_enter_prli(rdata);
  1507. out:
  1508. mutex_unlock(&rdata->rp_mutex);
  1509. return;
  1510. reject:
  1511. lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
  1512. fc_frame_free(fp);
  1513. }
  1514. /**
  1515. * fc_rport_recv_prli_req() - Handler for process login (PRLI) requests
  1516. * @rdata: The remote port that sent the PRLI request
  1517. * @sp: The sequence that the PRLI was on
  1518. * @rx_fp: The PRLI request frame
  1519. *
  1520. * Locking Note: The rport lock is exected to be held before calling
  1521. * this function.
  1522. */
  1523. static void fc_rport_recv_prli_req(struct fc_rport_priv *rdata,
  1524. struct fc_seq *sp, struct fc_frame *rx_fp)
  1525. {
  1526. struct fc_lport *lport = rdata->local_port;
  1527. struct fc_exch *ep;
  1528. struct fc_frame *fp;
  1529. struct fc_frame_header *fh;
  1530. struct {
  1531. struct fc_els_prli prli;
  1532. struct fc_els_spp spp;
  1533. } *pp;
  1534. struct fc_els_spp *rspp; /* request service param page */
  1535. struct fc_els_spp *spp; /* response spp */
  1536. unsigned int len;
  1537. unsigned int plen;
  1538. enum fc_els_spp_resp resp;
  1539. struct fc_seq_els_data rjt_data;
  1540. u32 f_ctl;
  1541. u32 fcp_parm;
  1542. u32 roles = FC_RPORT_ROLE_UNKNOWN;
  1543. rjt_data.fp = NULL;
  1544. fh = fc_frame_header_get(rx_fp);
  1545. FC_RPORT_DBG(rdata, "Received PRLI request while in state %s\n",
  1546. fc_rport_state(rdata));
  1547. len = fr_len(rx_fp) - sizeof(*fh);
  1548. pp = fc_frame_payload_get(rx_fp, sizeof(*pp));
  1549. if (!pp)
  1550. goto reject_len;
  1551. plen = ntohs(pp->prli.prli_len);
  1552. if ((plen % 4) != 0 || plen > len || plen < 16)
  1553. goto reject_len;
  1554. if (plen < len)
  1555. len = plen;
  1556. plen = pp->prli.prli_spp_len;
  1557. if ((plen % 4) != 0 || plen < sizeof(*spp) ||
  1558. plen > len || len < sizeof(*pp) || plen < 12)
  1559. goto reject_len;
  1560. rspp = &pp->spp;
  1561. fp = fc_frame_alloc(lport, len);
  1562. if (!fp) {
  1563. rjt_data.reason = ELS_RJT_UNAB;
  1564. rjt_data.explan = ELS_EXPL_INSUF_RES;
  1565. goto reject;
  1566. }
  1567. sp = lport->tt.seq_start_next(sp);
  1568. WARN_ON(!sp);
  1569. pp = fc_frame_payload_get(fp, len);
  1570. WARN_ON(!pp);
  1571. memset(pp, 0, len);
  1572. pp->prli.prli_cmd = ELS_LS_ACC;
  1573. pp->prli.prli_spp_len = plen;
  1574. pp->prli.prli_len = htons(len);
  1575. len -= sizeof(struct fc_els_prli);
  1576. /* reinitialize remote port roles */
  1577. rdata->ids.roles = FC_RPORT_ROLE_UNKNOWN;
  1578. /*
  1579. * Go through all the service parameter pages and build
  1580. * response. If plen indicates longer SPP than standard,
  1581. * use that. The entire response has been pre-cleared above.
  1582. */
  1583. spp = &pp->spp;
  1584. while (len >= plen) {
  1585. spp->spp_type = rspp->spp_type;
  1586. spp->spp_type_ext = rspp->spp_type_ext;
  1587. spp->spp_flags = rspp->spp_flags & FC_SPP_EST_IMG_PAIR;
  1588. resp = FC_SPP_RESP_ACK;
  1589. switch (rspp->spp_type) {
  1590. case 0: /* common to all FC-4 types */
  1591. break;
  1592. case FC_TYPE_FCP:
  1593. fcp_parm = ntohl(rspp->spp_params);
  1594. if (fcp_parm & FCP_SPPF_RETRY)
  1595. rdata->flags |= FC_RP_FLAGS_RETRY;
  1596. rdata->supported_classes = FC_COS_CLASS3;
  1597. if (fcp_parm & FCP_SPPF_INIT_FCN)
  1598. roles |= FC_RPORT_ROLE_FCP_INITIATOR;
  1599. if (fcp_parm & FCP_SPPF_TARG_FCN)
  1600. roles |= FC_RPORT_ROLE_FCP_TARGET;
  1601. rdata->ids.roles = roles;
  1602. spp->spp_params = htonl(lport->service_params);
  1603. break;
  1604. default:
  1605. resp = FC_SPP_RESP_INVL;
  1606. break;
  1607. }
  1608. spp->spp_flags |= resp;
  1609. len -= plen;
  1610. rspp = (struct fc_els_spp *)((char *)rspp + plen);
  1611. spp = (struct fc_els_spp *)((char *)spp + plen);
  1612. }
  1613. /*
  1614. * Send LS_ACC. If this fails, the originator should retry.
  1615. */
  1616. f_ctl = FC_FC_EX_CTX | FC_FC_LAST_SEQ;
  1617. f_ctl |= FC_FC_END_SEQ | FC_FC_SEQ_INIT;
  1618. ep = fc_seq_exch(sp);
  1619. fc_fill_fc_hdr(fp, FC_RCTL_ELS_REP, ep->did, ep->sid,
  1620. FC_TYPE_ELS, f_ctl, 0);
  1621. lport->tt.seq_send(lport, sp, fp);
  1622. switch (rdata->rp_state) {
  1623. case RPORT_ST_PRLI:
  1624. fc_rport_enter_ready(rdata);
  1625. break;
  1626. default:
  1627. break;
  1628. }
  1629. goto drop;
  1630. reject_len:
  1631. rjt_data.reason = ELS_RJT_PROT;
  1632. rjt_data.explan = ELS_EXPL_INV_LEN;
  1633. reject:
  1634. lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
  1635. drop:
  1636. fc_frame_free(rx_fp);
  1637. }
  1638. /**
  1639. * fc_rport_recv_prlo_req() - Handler for process logout (PRLO) requests
  1640. * @rdata: The remote port that sent the PRLO request
  1641. * @sp: The sequence that the PRLO was on
  1642. * @rx_fp: The PRLO request frame
  1643. *
  1644. * Locking Note: The rport lock is exected to be held before calling
  1645. * this function.
  1646. */
  1647. static void fc_rport_recv_prlo_req(struct fc_rport_priv *rdata,
  1648. struct fc_seq *sp,
  1649. struct fc_frame *rx_fp)
  1650. {
  1651. struct fc_lport *lport = rdata->local_port;
  1652. struct fc_frame_header *fh;
  1653. struct fc_exch *ep;
  1654. struct fc_frame *fp;
  1655. struct {
  1656. struct fc_els_prlo prlo;
  1657. struct fc_els_spp spp;
  1658. } *pp;
  1659. struct fc_els_spp *rspp; /* request service param page */
  1660. struct fc_els_spp *spp; /* response spp */
  1661. unsigned int len;
  1662. unsigned int plen;
  1663. u32 f_ctl;
  1664. struct fc_seq_els_data rjt_data;
  1665. rjt_data.fp = NULL;
  1666. fh = fc_frame_header_get(rx_fp);
  1667. FC_RPORT_DBG(rdata, "Received PRLO request while in state %s\n",
  1668. fc_rport_state(rdata));
  1669. len = fr_len(rx_fp) - sizeof(*fh);
  1670. pp = fc_frame_payload_get(rx_fp, sizeof(*pp));
  1671. if (!pp)
  1672. goto reject_len;
  1673. plen = ntohs(pp->prlo.prlo_len);
  1674. if (plen != 20)
  1675. goto reject_len;
  1676. if (plen < len)
  1677. len = plen;
  1678. rspp = &pp->spp;
  1679. fp = fc_frame_alloc(lport, len);
  1680. if (!fp) {
  1681. rjt_data.reason = ELS_RJT_UNAB;
  1682. rjt_data.explan = ELS_EXPL_INSUF_RES;
  1683. goto reject;
  1684. }
  1685. sp = lport->tt.seq_start_next(sp);
  1686. WARN_ON(!sp);
  1687. pp = fc_frame_payload_get(fp, len);
  1688. WARN_ON(!pp);
  1689. memset(pp, 0, len);
  1690. pp->prlo.prlo_cmd = ELS_LS_ACC;
  1691. pp->prlo.prlo_obs = 0x10;
  1692. pp->prlo.prlo_len = htons(len);
  1693. spp = &pp->spp;
  1694. spp->spp_type = rspp->spp_type;
  1695. spp->spp_type_ext = rspp->spp_type_ext;
  1696. spp->spp_flags = FC_SPP_RESP_ACK;
  1697. fc_rport_enter_delete(rdata, RPORT_EV_LOGO);
  1698. f_ctl = FC_FC_EX_CTX | FC_FC_LAST_SEQ;
  1699. f_ctl |= FC_FC_END_SEQ | FC_FC_SEQ_INIT;
  1700. ep = fc_seq_exch(sp);
  1701. fc_fill_fc_hdr(fp, FC_RCTL_ELS_REP, ep->did, ep->sid,
  1702. FC_TYPE_ELS, f_ctl, 0);
  1703. lport->tt.seq_send(lport, sp, fp);
  1704. goto drop;
  1705. reject_len:
  1706. rjt_data.reason = ELS_RJT_PROT;
  1707. rjt_data.explan = ELS_EXPL_INV_LEN;
  1708. reject:
  1709. lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
  1710. drop:
  1711. fc_frame_free(rx_fp);
  1712. }
  1713. /**
  1714. * fc_rport_recv_logo_req() - Handler for logout (LOGO) requests
  1715. * @lport: The local port that received the LOGO request
  1716. * @sp: The sequence that the LOGO request was on
  1717. * @fp: The LOGO request frame
  1718. *
  1719. * Locking Note: The rport lock is exected to be held before calling
  1720. * this function.
  1721. */
  1722. static void fc_rport_recv_logo_req(struct fc_lport *lport,
  1723. struct fc_seq *sp,
  1724. struct fc_frame *fp)
  1725. {
  1726. struct fc_frame_header *fh;
  1727. struct fc_rport_priv *rdata;
  1728. u32 sid;
  1729. lport->tt.seq_els_rsp_send(sp, ELS_LS_ACC, NULL);
  1730. fh = fc_frame_header_get(fp);
  1731. sid = ntoh24(fh->fh_s_id);
  1732. mutex_lock(&lport->disc.disc_mutex);
  1733. rdata = lport->tt.rport_lookup(lport, sid);
  1734. if (rdata) {
  1735. mutex_lock(&rdata->rp_mutex);
  1736. FC_RPORT_DBG(rdata, "Received LOGO request while in state %s\n",
  1737. fc_rport_state(rdata));
  1738. fc_rport_enter_delete(rdata, RPORT_EV_LOGO);
  1739. mutex_unlock(&rdata->rp_mutex);
  1740. } else
  1741. FC_RPORT_ID_DBG(lport, sid,
  1742. "Received LOGO from non-logged-in port\n");
  1743. mutex_unlock(&lport->disc.disc_mutex);
  1744. fc_frame_free(fp);
  1745. }
  1746. /**
  1747. * fc_rport_flush_queue() - Flush the rport_event_queue
  1748. */
  1749. static void fc_rport_flush_queue(void)
  1750. {
  1751. flush_workqueue(rport_event_queue);
  1752. }
  1753. /**
  1754. * fc_rport_init() - Initialize the remote port layer for a local port
  1755. * @lport: The local port to initialize the remote port layer for
  1756. */
  1757. int fc_rport_init(struct fc_lport *lport)
  1758. {
  1759. if (!lport->tt.rport_lookup)
  1760. lport->tt.rport_lookup = fc_rport_lookup;
  1761. if (!lport->tt.rport_create)
  1762. lport->tt.rport_create = fc_rport_create;
  1763. if (!lport->tt.rport_login)
  1764. lport->tt.rport_login = fc_rport_login;
  1765. if (!lport->tt.rport_logoff)
  1766. lport->tt.rport_logoff = fc_rport_logoff;
  1767. if (!lport->tt.rport_recv_req)
  1768. lport->tt.rport_recv_req = fc_rport_recv_req;
  1769. if (!lport->tt.rport_flush_queue)
  1770. lport->tt.rport_flush_queue = fc_rport_flush_queue;
  1771. if (!lport->tt.rport_destroy)
  1772. lport->tt.rport_destroy = fc_rport_destroy;
  1773. return 0;
  1774. }
  1775. EXPORT_SYMBOL(fc_rport_init);
  1776. /**
  1777. * fc_setup_rport() - Initialize the rport_event_queue
  1778. */
  1779. int fc_setup_rport()
  1780. {
  1781. rport_event_queue = create_singlethread_workqueue("fc_rport_eq");
  1782. if (!rport_event_queue)
  1783. return -ENOMEM;
  1784. return 0;
  1785. }
  1786. /**
  1787. * fc_destroy_rport() - Destroy the rport_event_queue
  1788. */
  1789. void fc_destroy_rport()
  1790. {
  1791. destroy_workqueue(rport_event_queue);
  1792. }
  1793. /**
  1794. * fc_rport_terminate_io() - Stop all outstanding I/O on a remote port
  1795. * @rport: The remote port whose I/O should be terminated
  1796. */
  1797. void fc_rport_terminate_io(struct fc_rport *rport)
  1798. {
  1799. struct fc_rport_libfc_priv *rpriv = rport->dd_data;
  1800. struct fc_lport *lport = rpriv->local_port;
  1801. lport->tt.exch_mgr_reset(lport, 0, rport->port_id);
  1802. lport->tt.exch_mgr_reset(lport, rport->port_id, 0);
  1803. }
  1804. EXPORT_SYMBOL(fc_rport_terminate_io);