fc_rport.c 50 KB

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