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