fc_rport.c 55 KB

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