fc_rport.c 46 KB

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