fc_rport.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536
  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. struct workqueue_struct *rport_event_queue;
  54. static void fc_rport_enter_plogi(struct fc_rport_priv *);
  55. static void fc_rport_enter_prli(struct fc_rport_priv *);
  56. static void fc_rport_enter_rtv(struct fc_rport_priv *);
  57. static void fc_rport_enter_ready(struct fc_rport_priv *);
  58. static void fc_rport_enter_logo(struct fc_rport_priv *);
  59. static void fc_rport_enter_adisc(struct fc_rport_priv *);
  60. static void fc_rport_recv_plogi_req(struct fc_lport *,
  61. struct fc_seq *, struct fc_frame *);
  62. static void fc_rport_recv_prli_req(struct fc_rport_priv *,
  63. struct fc_seq *, struct fc_frame *);
  64. static void fc_rport_recv_prlo_req(struct fc_rport_priv *,
  65. struct fc_seq *, struct fc_frame *);
  66. static void fc_rport_recv_logo_req(struct fc_lport *,
  67. struct fc_seq *, 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_PLOGI] = "PLOGI",
  75. [RPORT_ST_PRLI] = "PRLI",
  76. [RPORT_ST_RTV] = "RTV",
  77. [RPORT_ST_READY] = "Ready",
  78. [RPORT_ST_LOGO] = "LOGO",
  79. [RPORT_ST_ADISC] = "ADISC",
  80. [RPORT_ST_DELETE] = "Delete",
  81. };
  82. /**
  83. * fc_rport_lookup() - lookup a remote port by port_id
  84. * @lport: Fibre Channel host port instance
  85. * @port_id: remote port port_id to match
  86. */
  87. static struct fc_rport_priv *fc_rport_lookup(const struct fc_lport *lport,
  88. u32 port_id)
  89. {
  90. struct fc_rport_priv *rdata;
  91. list_for_each_entry(rdata, &lport->disc.rports, peers)
  92. if (rdata->ids.port_id == port_id &&
  93. rdata->rp_state != RPORT_ST_DELETE)
  94. return rdata;
  95. return NULL;
  96. }
  97. /**
  98. * fc_rport_create() - Create a new remote port
  99. * @lport: The local port that the new remote port is for
  100. * @port_id: The port ID for the new remote port
  101. *
  102. * Locking note: must be called with the disc_mutex held.
  103. */
  104. static struct fc_rport_priv *fc_rport_create(struct fc_lport *lport,
  105. u32 port_id)
  106. {
  107. struct fc_rport_priv *rdata;
  108. rdata = lport->tt.rport_lookup(lport, port_id);
  109. if (rdata)
  110. return rdata;
  111. rdata = kzalloc(sizeof(*rdata), GFP_KERNEL);
  112. if (!rdata)
  113. return NULL;
  114. rdata->ids.node_name = -1;
  115. rdata->ids.port_name = -1;
  116. rdata->ids.port_id = port_id;
  117. rdata->ids.roles = FC_RPORT_ROLE_UNKNOWN;
  118. kref_init(&rdata->kref);
  119. mutex_init(&rdata->rp_mutex);
  120. rdata->local_port = lport;
  121. rdata->rp_state = RPORT_ST_INIT;
  122. rdata->event = RPORT_EV_NONE;
  123. rdata->flags = FC_RP_FLAGS_REC_SUPPORTED;
  124. rdata->e_d_tov = lport->e_d_tov;
  125. rdata->r_a_tov = lport->r_a_tov;
  126. rdata->maxframe_size = FC_MIN_MAX_PAYLOAD;
  127. INIT_DELAYED_WORK(&rdata->retry_work, fc_rport_timeout);
  128. INIT_WORK(&rdata->event_work, fc_rport_work);
  129. if (port_id != FC_FID_DIR_SERV)
  130. list_add(&rdata->peers, &lport->disc.rports);
  131. return rdata;
  132. }
  133. /**
  134. * fc_rport_destroy() - free a remote port after last reference is released.
  135. * @kref: pointer to kref inside struct fc_rport_priv
  136. */
  137. static void fc_rport_destroy(struct kref *kref)
  138. {
  139. struct fc_rport_priv *rdata;
  140. rdata = container_of(kref, struct fc_rport_priv, kref);
  141. kfree(rdata);
  142. }
  143. /**
  144. * fc_rport_state() - return a string for the state the rport is in
  145. * @rdata: remote port private data
  146. */
  147. static const char *fc_rport_state(struct fc_rport_priv *rdata)
  148. {
  149. const char *cp;
  150. cp = fc_rport_state_names[rdata->rp_state];
  151. if (!cp)
  152. cp = "Unknown";
  153. return cp;
  154. }
  155. /**
  156. * fc_set_rport_loss_tmo() - Set the remote port loss timeout in seconds.
  157. * @rport: Pointer to Fibre Channel remote port structure
  158. * @timeout: timeout in seconds
  159. */
  160. void fc_set_rport_loss_tmo(struct fc_rport *rport, u32 timeout)
  161. {
  162. if (timeout)
  163. rport->dev_loss_tmo = timeout + 5;
  164. else
  165. rport->dev_loss_tmo = 30;
  166. }
  167. EXPORT_SYMBOL(fc_set_rport_loss_tmo);
  168. /**
  169. * fc_plogi_get_maxframe() - Get max payload from the common service parameters
  170. * @flp: FLOGI payload structure
  171. * @maxval: upper limit, may be less than what is in the service parameters
  172. */
  173. static unsigned int fc_plogi_get_maxframe(struct fc_els_flogi *flp,
  174. unsigned int maxval)
  175. {
  176. unsigned int mfs;
  177. /*
  178. * Get max payload from the common service parameters and the
  179. * class 3 receive data field size.
  180. */
  181. mfs = ntohs(flp->fl_csp.sp_bb_data) & FC_SP_BB_DATA_MASK;
  182. if (mfs >= FC_SP_MIN_MAX_PAYLOAD && mfs < maxval)
  183. maxval = mfs;
  184. mfs = ntohs(flp->fl_cssp[3 - 1].cp_rdfs);
  185. if (mfs >= FC_SP_MIN_MAX_PAYLOAD && mfs < maxval)
  186. maxval = mfs;
  187. return maxval;
  188. }
  189. /**
  190. * fc_rport_state_enter() - Change the rport's state
  191. * @rdata: The rport whose state should change
  192. * @new: The new state of the rport
  193. *
  194. * Locking Note: Called with the rport lock held
  195. */
  196. static void fc_rport_state_enter(struct fc_rport_priv *rdata,
  197. enum fc_rport_state new)
  198. {
  199. if (rdata->rp_state != new)
  200. rdata->retries = 0;
  201. rdata->rp_state = new;
  202. }
  203. static void fc_rport_work(struct work_struct *work)
  204. {
  205. u32 port_id;
  206. struct fc_rport_priv *rdata =
  207. container_of(work, struct fc_rport_priv, event_work);
  208. struct fc_rport_libfc_priv *rp;
  209. enum fc_rport_event event;
  210. struct fc_lport *lport = rdata->local_port;
  211. struct fc_rport_operations *rport_ops;
  212. struct fc_rport_identifiers ids;
  213. struct fc_rport *rport;
  214. mutex_lock(&rdata->rp_mutex);
  215. event = rdata->event;
  216. rport_ops = rdata->ops;
  217. rport = rdata->rport;
  218. FC_RPORT_DBG(rdata, "work event %u\n", event);
  219. switch (event) {
  220. case RPORT_EV_READY:
  221. ids = rdata->ids;
  222. rdata->event = RPORT_EV_NONE;
  223. kref_get(&rdata->kref);
  224. mutex_unlock(&rdata->rp_mutex);
  225. if (!rport)
  226. rport = fc_remote_port_add(lport->host, 0, &ids);
  227. if (!rport) {
  228. FC_RPORT_DBG(rdata, "Failed to add the rport\n");
  229. lport->tt.rport_logoff(rdata);
  230. kref_put(&rdata->kref, lport->tt.rport_destroy);
  231. return;
  232. }
  233. mutex_lock(&rdata->rp_mutex);
  234. if (rdata->rport)
  235. FC_RPORT_DBG(rdata, "rport already allocated\n");
  236. rdata->rport = rport;
  237. rport->maxframe_size = rdata->maxframe_size;
  238. rport->supported_classes = rdata->supported_classes;
  239. rp = rport->dd_data;
  240. rp->local_port = lport;
  241. rp->rp_state = rdata->rp_state;
  242. rp->flags = rdata->flags;
  243. rp->e_d_tov = rdata->e_d_tov;
  244. rp->r_a_tov = rdata->r_a_tov;
  245. mutex_unlock(&rdata->rp_mutex);
  246. if (rport_ops && rport_ops->event_callback) {
  247. FC_RPORT_DBG(rdata, "callback ev %d\n", event);
  248. rport_ops->event_callback(lport, rdata, event);
  249. }
  250. kref_put(&rdata->kref, lport->tt.rport_destroy);
  251. break;
  252. case RPORT_EV_FAILED:
  253. case RPORT_EV_LOGO:
  254. case RPORT_EV_STOP:
  255. port_id = rdata->ids.port_id;
  256. mutex_unlock(&rdata->rp_mutex);
  257. if (port_id != FC_FID_DIR_SERV) {
  258. mutex_lock(&lport->disc.disc_mutex);
  259. list_del(&rdata->peers);
  260. mutex_unlock(&lport->disc.disc_mutex);
  261. }
  262. if (rport_ops && rport_ops->event_callback) {
  263. FC_RPORT_DBG(rdata, "callback ev %d\n", event);
  264. rport_ops->event_callback(lport, rdata, event);
  265. }
  266. cancel_delayed_work_sync(&rdata->retry_work);
  267. /*
  268. * Reset any outstanding exchanges before freeing rport.
  269. */
  270. lport->tt.exch_mgr_reset(lport, 0, port_id);
  271. lport->tt.exch_mgr_reset(lport, port_id, 0);
  272. if (rport) {
  273. rp = rport->dd_data;
  274. rp->rp_state = RPORT_ST_DELETE;
  275. mutex_lock(&rdata->rp_mutex);
  276. rdata->rport = NULL;
  277. mutex_unlock(&rdata->rp_mutex);
  278. fc_remote_port_delete(rport);
  279. }
  280. kref_put(&rdata->kref, lport->tt.rport_destroy);
  281. break;
  282. default:
  283. mutex_unlock(&rdata->rp_mutex);
  284. break;
  285. }
  286. }
  287. /**
  288. * fc_rport_login() - Start the remote port login state machine
  289. * @rdata: private remote port
  290. *
  291. * Locking Note: Called without the rport lock held. This
  292. * function will hold the rport lock, call an _enter_*
  293. * function and then unlock the rport.
  294. *
  295. * This indicates the intent to be logged into the remote port.
  296. * If it appears we are already logged in, ADISC is used to verify
  297. * the setup.
  298. */
  299. int fc_rport_login(struct fc_rport_priv *rdata)
  300. {
  301. mutex_lock(&rdata->rp_mutex);
  302. switch (rdata->rp_state) {
  303. case RPORT_ST_READY:
  304. FC_RPORT_DBG(rdata, "ADISC port\n");
  305. fc_rport_enter_adisc(rdata);
  306. break;
  307. default:
  308. FC_RPORT_DBG(rdata, "Login to port\n");
  309. fc_rport_enter_plogi(rdata);
  310. break;
  311. }
  312. mutex_unlock(&rdata->rp_mutex);
  313. return 0;
  314. }
  315. /**
  316. * fc_rport_enter_delete() - schedule a remote port to be deleted.
  317. * @rdata: private remote port
  318. * @event: event to report as the reason for deletion
  319. *
  320. * Locking Note: Called with the rport lock held.
  321. *
  322. * Allow state change into DELETE only once.
  323. *
  324. * Call queue_work only if there's no event already pending.
  325. * Set the new event so that the old pending event will not occur.
  326. * Since we have the mutex, even if fc_rport_work() is already started,
  327. * it'll see the new event.
  328. */
  329. static void fc_rport_enter_delete(struct fc_rport_priv *rdata,
  330. enum fc_rport_event event)
  331. {
  332. if (rdata->rp_state == RPORT_ST_DELETE)
  333. return;
  334. FC_RPORT_DBG(rdata, "Delete port\n");
  335. fc_rport_state_enter(rdata, RPORT_ST_DELETE);
  336. if (rdata->event == RPORT_EV_NONE)
  337. queue_work(rport_event_queue, &rdata->event_work);
  338. rdata->event = event;
  339. }
  340. /**
  341. * fc_rport_logoff() - Logoff and remove an rport
  342. * @rdata: private remote port
  343. *
  344. * Locking Note: Called without the rport lock held. This
  345. * function will hold the rport lock, call an _enter_*
  346. * function and then unlock the rport.
  347. */
  348. int fc_rport_logoff(struct fc_rport_priv *rdata)
  349. {
  350. mutex_lock(&rdata->rp_mutex);
  351. FC_RPORT_DBG(rdata, "Remove port\n");
  352. if (rdata->rp_state == RPORT_ST_DELETE) {
  353. FC_RPORT_DBG(rdata, "Port in Delete state, not removing\n");
  354. mutex_unlock(&rdata->rp_mutex);
  355. goto out;
  356. }
  357. fc_rport_enter_logo(rdata);
  358. /*
  359. * Change the state to Delete so that we discard
  360. * the response.
  361. */
  362. fc_rport_enter_delete(rdata, RPORT_EV_STOP);
  363. mutex_unlock(&rdata->rp_mutex);
  364. out:
  365. return 0;
  366. }
  367. /**
  368. * fc_rport_enter_ready() - The rport is ready
  369. * @rdata: private remote port
  370. *
  371. * Locking Note: The rport lock is expected to be held before calling
  372. * this routine.
  373. */
  374. static void fc_rport_enter_ready(struct fc_rport_priv *rdata)
  375. {
  376. fc_rport_state_enter(rdata, RPORT_ST_READY);
  377. FC_RPORT_DBG(rdata, "Port is Ready\n");
  378. if (rdata->event == RPORT_EV_NONE)
  379. queue_work(rport_event_queue, &rdata->event_work);
  380. rdata->event = RPORT_EV_READY;
  381. }
  382. /**
  383. * fc_rport_timeout() - Handler for the retry_work timer.
  384. * @work: The work struct of the fc_rport_priv
  385. *
  386. * Locking Note: Called without the rport lock held. This
  387. * function will hold the rport lock, call an _enter_*
  388. * function and then unlock the rport.
  389. */
  390. static void fc_rport_timeout(struct work_struct *work)
  391. {
  392. struct fc_rport_priv *rdata =
  393. container_of(work, struct fc_rport_priv, retry_work.work);
  394. mutex_lock(&rdata->rp_mutex);
  395. switch (rdata->rp_state) {
  396. case RPORT_ST_PLOGI:
  397. fc_rport_enter_plogi(rdata);
  398. break;
  399. case RPORT_ST_PRLI:
  400. fc_rport_enter_prli(rdata);
  401. break;
  402. case RPORT_ST_RTV:
  403. fc_rport_enter_rtv(rdata);
  404. break;
  405. case RPORT_ST_LOGO:
  406. fc_rport_enter_logo(rdata);
  407. break;
  408. case RPORT_ST_ADISC:
  409. fc_rport_enter_adisc(rdata);
  410. break;
  411. case RPORT_ST_READY:
  412. case RPORT_ST_INIT:
  413. case RPORT_ST_DELETE:
  414. break;
  415. }
  416. mutex_unlock(&rdata->rp_mutex);
  417. }
  418. /**
  419. * fc_rport_error() - Error handler, called once retries have been exhausted
  420. * @rdata: private remote port
  421. * @fp: The frame pointer
  422. *
  423. * Locking Note: The rport lock is expected to be held before
  424. * calling this routine
  425. */
  426. static void fc_rport_error(struct fc_rport_priv *rdata, struct fc_frame *fp)
  427. {
  428. FC_RPORT_DBG(rdata, "Error %ld in state %s, retries %d\n",
  429. IS_ERR(fp) ? -PTR_ERR(fp) : 0,
  430. fc_rport_state(rdata), rdata->retries);
  431. switch (rdata->rp_state) {
  432. case RPORT_ST_PLOGI:
  433. case RPORT_ST_LOGO:
  434. fc_rport_enter_delete(rdata, RPORT_EV_FAILED);
  435. break;
  436. case RPORT_ST_RTV:
  437. fc_rport_enter_ready(rdata);
  438. break;
  439. case RPORT_ST_PRLI:
  440. case RPORT_ST_ADISC:
  441. fc_rport_enter_logo(rdata);
  442. break;
  443. case RPORT_ST_DELETE:
  444. case RPORT_ST_READY:
  445. case RPORT_ST_INIT:
  446. break;
  447. }
  448. }
  449. /**
  450. * fc_rport_error_retry() - Error handler when retries are desired
  451. * @rdata: private remote port data
  452. * @fp: The frame pointer
  453. *
  454. * If the error was an exchange timeout retry immediately,
  455. * otherwise wait for E_D_TOV.
  456. *
  457. * Locking Note: The rport lock is expected to be held before
  458. * calling this routine
  459. */
  460. static void fc_rport_error_retry(struct fc_rport_priv *rdata,
  461. struct fc_frame *fp)
  462. {
  463. unsigned long delay = FC_DEF_E_D_TOV;
  464. /* make sure this isn't an FC_EX_CLOSED error, never retry those */
  465. if (PTR_ERR(fp) == -FC_EX_CLOSED)
  466. return fc_rport_error(rdata, fp);
  467. if (rdata->retries < rdata->local_port->max_rport_retry_count) {
  468. FC_RPORT_DBG(rdata, "Error %ld in state %s, retrying\n",
  469. PTR_ERR(fp), fc_rport_state(rdata));
  470. rdata->retries++;
  471. /* no additional delay on exchange timeouts */
  472. if (PTR_ERR(fp) == -FC_EX_TIMEOUT)
  473. delay = 0;
  474. schedule_delayed_work(&rdata->retry_work, delay);
  475. return;
  476. }
  477. return fc_rport_error(rdata, fp);
  478. }
  479. /**
  480. * fc_rport_plogi_recv_resp() - Handle incoming ELS PLOGI response
  481. * @sp: current sequence in the PLOGI exchange
  482. * @fp: response frame
  483. * @rdata_arg: private remote port data
  484. *
  485. * Locking Note: This function will be called without the rport lock
  486. * held, but it will lock, call an _enter_* function or fc_rport_error
  487. * and then unlock the rport.
  488. */
  489. static void fc_rport_plogi_resp(struct fc_seq *sp, struct fc_frame *fp,
  490. void *rdata_arg)
  491. {
  492. struct fc_rport_priv *rdata = rdata_arg;
  493. struct fc_lport *lport = rdata->local_port;
  494. struct fc_els_flogi *plp = NULL;
  495. unsigned int tov;
  496. u16 csp_seq;
  497. u16 cssp_seq;
  498. u8 op;
  499. mutex_lock(&rdata->rp_mutex);
  500. FC_RPORT_DBG(rdata, "Received a PLOGI %s\n", fc_els_resp_type(fp));
  501. if (rdata->rp_state != RPORT_ST_PLOGI) {
  502. FC_RPORT_DBG(rdata, "Received a PLOGI response, but in state "
  503. "%s\n", fc_rport_state(rdata));
  504. if (IS_ERR(fp))
  505. goto err;
  506. goto out;
  507. }
  508. if (IS_ERR(fp)) {
  509. fc_rport_error_retry(rdata, fp);
  510. goto err;
  511. }
  512. op = fc_frame_payload_op(fp);
  513. if (op == ELS_LS_ACC &&
  514. (plp = fc_frame_payload_get(fp, sizeof(*plp))) != NULL) {
  515. rdata->ids.port_name = get_unaligned_be64(&plp->fl_wwpn);
  516. rdata->ids.node_name = get_unaligned_be64(&plp->fl_wwnn);
  517. tov = ntohl(plp->fl_csp.sp_e_d_tov);
  518. if (ntohs(plp->fl_csp.sp_features) & FC_SP_FT_EDTR)
  519. tov /= 1000;
  520. if (tov > rdata->e_d_tov)
  521. rdata->e_d_tov = tov;
  522. csp_seq = ntohs(plp->fl_csp.sp_tot_seq);
  523. cssp_seq = ntohs(plp->fl_cssp[3 - 1].cp_con_seq);
  524. if (cssp_seq < csp_seq)
  525. csp_seq = cssp_seq;
  526. rdata->max_seq = csp_seq;
  527. rdata->maxframe_size = fc_plogi_get_maxframe(plp, lport->mfs);
  528. fc_rport_enter_prli(rdata);
  529. } else
  530. fc_rport_error_retry(rdata, fp);
  531. out:
  532. fc_frame_free(fp);
  533. err:
  534. mutex_unlock(&rdata->rp_mutex);
  535. kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
  536. }
  537. /**
  538. * fc_rport_enter_plogi() - Send Port Login (PLOGI) request to peer
  539. * @rdata: private remote port data
  540. *
  541. * Locking Note: The rport lock is expected to be held before calling
  542. * this routine.
  543. */
  544. static void fc_rport_enter_plogi(struct fc_rport_priv *rdata)
  545. {
  546. struct fc_lport *lport = rdata->local_port;
  547. struct fc_frame *fp;
  548. FC_RPORT_DBG(rdata, "Port entered PLOGI state from %s state\n",
  549. fc_rport_state(rdata));
  550. fc_rport_state_enter(rdata, RPORT_ST_PLOGI);
  551. rdata->maxframe_size = FC_MIN_MAX_PAYLOAD;
  552. fp = fc_frame_alloc(lport, sizeof(struct fc_els_flogi));
  553. if (!fp) {
  554. fc_rport_error_retry(rdata, fp);
  555. return;
  556. }
  557. rdata->e_d_tov = lport->e_d_tov;
  558. if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_PLOGI,
  559. fc_rport_plogi_resp, rdata, lport->e_d_tov))
  560. fc_rport_error_retry(rdata, fp);
  561. else
  562. kref_get(&rdata->kref);
  563. }
  564. /**
  565. * fc_rport_prli_resp() - Process Login (PRLI) response handler
  566. * @sp: current sequence in the PRLI exchange
  567. * @fp: response frame
  568. * @rdata_arg: private remote port data
  569. *
  570. * Locking Note: This function will be called without the rport lock
  571. * held, but it will lock, call an _enter_* function or fc_rport_error
  572. * and then unlock the rport.
  573. */
  574. static void fc_rport_prli_resp(struct fc_seq *sp, struct fc_frame *fp,
  575. void *rdata_arg)
  576. {
  577. struct fc_rport_priv *rdata = rdata_arg;
  578. struct {
  579. struct fc_els_prli prli;
  580. struct fc_els_spp spp;
  581. } *pp;
  582. u32 roles = FC_RPORT_ROLE_UNKNOWN;
  583. u32 fcp_parm = 0;
  584. u8 op;
  585. mutex_lock(&rdata->rp_mutex);
  586. FC_RPORT_DBG(rdata, "Received a PRLI %s\n", fc_els_resp_type(fp));
  587. if (rdata->rp_state != RPORT_ST_PRLI) {
  588. FC_RPORT_DBG(rdata, "Received a PRLI response, but in state "
  589. "%s\n", fc_rport_state(rdata));
  590. if (IS_ERR(fp))
  591. goto err;
  592. goto out;
  593. }
  594. if (IS_ERR(fp)) {
  595. fc_rport_error_retry(rdata, fp);
  596. goto err;
  597. }
  598. /* reinitialize remote port roles */
  599. rdata->ids.roles = FC_RPORT_ROLE_UNKNOWN;
  600. op = fc_frame_payload_op(fp);
  601. if (op == ELS_LS_ACC) {
  602. pp = fc_frame_payload_get(fp, sizeof(*pp));
  603. if (pp && pp->prli.prli_spp_len >= sizeof(pp->spp)) {
  604. fcp_parm = ntohl(pp->spp.spp_params);
  605. if (fcp_parm & FCP_SPPF_RETRY)
  606. rdata->flags |= FC_RP_FLAGS_RETRY;
  607. }
  608. rdata->supported_classes = FC_COS_CLASS3;
  609. if (fcp_parm & FCP_SPPF_INIT_FCN)
  610. roles |= FC_RPORT_ROLE_FCP_INITIATOR;
  611. if (fcp_parm & FCP_SPPF_TARG_FCN)
  612. roles |= FC_RPORT_ROLE_FCP_TARGET;
  613. rdata->ids.roles = roles;
  614. fc_rport_enter_rtv(rdata);
  615. } else {
  616. FC_RPORT_DBG(rdata, "Bad ELS response for PRLI command\n");
  617. fc_rport_enter_delete(rdata, RPORT_EV_FAILED);
  618. }
  619. out:
  620. fc_frame_free(fp);
  621. err:
  622. mutex_unlock(&rdata->rp_mutex);
  623. kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
  624. }
  625. /**
  626. * fc_rport_logo_resp() - Logout (LOGO) response handler
  627. * @sp: current sequence in the LOGO exchange
  628. * @fp: response frame
  629. * @rdata_arg: private remote port data
  630. *
  631. * Locking Note: This function will be called without the rport lock
  632. * held, but it will lock, call an _enter_* function or fc_rport_error
  633. * and then unlock the rport.
  634. */
  635. static void fc_rport_logo_resp(struct fc_seq *sp, struct fc_frame *fp,
  636. void *rdata_arg)
  637. {
  638. struct fc_rport_priv *rdata = rdata_arg;
  639. u8 op;
  640. mutex_lock(&rdata->rp_mutex);
  641. FC_RPORT_DBG(rdata, "Received a LOGO %s\n", fc_els_resp_type(fp));
  642. if (rdata->rp_state != RPORT_ST_LOGO) {
  643. FC_RPORT_DBG(rdata, "Received a LOGO response, but in state "
  644. "%s\n", fc_rport_state(rdata));
  645. if (IS_ERR(fp))
  646. goto err;
  647. goto out;
  648. }
  649. if (IS_ERR(fp)) {
  650. fc_rport_error_retry(rdata, fp);
  651. goto err;
  652. }
  653. op = fc_frame_payload_op(fp);
  654. if (op != ELS_LS_ACC)
  655. FC_RPORT_DBG(rdata, "Bad ELS response op %x for LOGO command\n",
  656. op);
  657. fc_rport_enter_delete(rdata, RPORT_EV_LOGO);
  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_enter_prli() - Send Process Login (PRLI) request to peer
  666. * @rdata: private remote port data
  667. *
  668. * Locking Note: The rport lock is expected to be held before calling
  669. * this routine.
  670. */
  671. static void fc_rport_enter_prli(struct fc_rport_priv *rdata)
  672. {
  673. struct fc_lport *lport = rdata->local_port;
  674. struct {
  675. struct fc_els_prli prli;
  676. struct fc_els_spp spp;
  677. } *pp;
  678. struct fc_frame *fp;
  679. /*
  680. * If the rport is one of the well known addresses
  681. * we skip PRLI and RTV and go straight to READY.
  682. */
  683. if (rdata->ids.port_id >= FC_FID_DOM_MGR) {
  684. fc_rport_enter_ready(rdata);
  685. return;
  686. }
  687. FC_RPORT_DBG(rdata, "Port entered PRLI state from %s state\n",
  688. fc_rport_state(rdata));
  689. fc_rport_state_enter(rdata, RPORT_ST_PRLI);
  690. fp = fc_frame_alloc(lport, sizeof(*pp));
  691. if (!fp) {
  692. fc_rport_error_retry(rdata, fp);
  693. return;
  694. }
  695. if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_PRLI,
  696. fc_rport_prli_resp, rdata, lport->e_d_tov))
  697. fc_rport_error_retry(rdata, fp);
  698. else
  699. kref_get(&rdata->kref);
  700. }
  701. /**
  702. * fc_rport_els_rtv_resp() - Request Timeout Value response handler
  703. * @sp: current sequence in the RTV exchange
  704. * @fp: response frame
  705. * @rdata_arg: private remote port data
  706. *
  707. * Many targets don't seem to support this.
  708. *
  709. * Locking Note: This function will be called without the rport lock
  710. * held, but it will lock, call an _enter_* function or fc_rport_error
  711. * and then unlock the rport.
  712. */
  713. static void fc_rport_rtv_resp(struct fc_seq *sp, struct fc_frame *fp,
  714. void *rdata_arg)
  715. {
  716. struct fc_rport_priv *rdata = rdata_arg;
  717. u8 op;
  718. mutex_lock(&rdata->rp_mutex);
  719. FC_RPORT_DBG(rdata, "Received a RTV %s\n", fc_els_resp_type(fp));
  720. if (rdata->rp_state != RPORT_ST_RTV) {
  721. FC_RPORT_DBG(rdata, "Received a RTV response, but in state "
  722. "%s\n", fc_rport_state(rdata));
  723. if (IS_ERR(fp))
  724. goto err;
  725. goto out;
  726. }
  727. if (IS_ERR(fp)) {
  728. fc_rport_error(rdata, fp);
  729. goto err;
  730. }
  731. op = fc_frame_payload_op(fp);
  732. if (op == ELS_LS_ACC) {
  733. struct fc_els_rtv_acc *rtv;
  734. u32 toq;
  735. u32 tov;
  736. rtv = fc_frame_payload_get(fp, sizeof(*rtv));
  737. if (rtv) {
  738. toq = ntohl(rtv->rtv_toq);
  739. tov = ntohl(rtv->rtv_r_a_tov);
  740. if (tov == 0)
  741. tov = 1;
  742. rdata->r_a_tov = tov;
  743. tov = ntohl(rtv->rtv_e_d_tov);
  744. if (toq & FC_ELS_RTV_EDRES)
  745. tov /= 1000000;
  746. if (tov == 0)
  747. tov = 1;
  748. rdata->e_d_tov = tov;
  749. }
  750. }
  751. fc_rport_enter_ready(rdata);
  752. out:
  753. fc_frame_free(fp);
  754. err:
  755. mutex_unlock(&rdata->rp_mutex);
  756. kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
  757. }
  758. /**
  759. * fc_rport_enter_rtv() - Send Request Timeout Value (RTV) request to peer
  760. * @rdata: private remote port data
  761. *
  762. * Locking Note: The rport lock is expected to be held before calling
  763. * this routine.
  764. */
  765. static void fc_rport_enter_rtv(struct fc_rport_priv *rdata)
  766. {
  767. struct fc_frame *fp;
  768. struct fc_lport *lport = rdata->local_port;
  769. FC_RPORT_DBG(rdata, "Port entered RTV state from %s state\n",
  770. fc_rport_state(rdata));
  771. fc_rport_state_enter(rdata, RPORT_ST_RTV);
  772. fp = fc_frame_alloc(lport, sizeof(struct fc_els_rtv));
  773. if (!fp) {
  774. fc_rport_error_retry(rdata, fp);
  775. return;
  776. }
  777. if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_RTV,
  778. fc_rport_rtv_resp, rdata, lport->e_d_tov))
  779. fc_rport_error_retry(rdata, fp);
  780. else
  781. kref_get(&rdata->kref);
  782. }
  783. /**
  784. * fc_rport_enter_logo() - Send Logout (LOGO) request to peer
  785. * @rdata: private remote port data
  786. *
  787. * Locking Note: The rport lock is expected to be held before calling
  788. * this routine.
  789. */
  790. static void fc_rport_enter_logo(struct fc_rport_priv *rdata)
  791. {
  792. struct fc_lport *lport = rdata->local_port;
  793. struct fc_frame *fp;
  794. FC_RPORT_DBG(rdata, "Port entered LOGO state from %s state\n",
  795. fc_rport_state(rdata));
  796. fc_rport_state_enter(rdata, RPORT_ST_LOGO);
  797. fp = fc_frame_alloc(lport, sizeof(struct fc_els_logo));
  798. if (!fp) {
  799. fc_rport_error_retry(rdata, fp);
  800. return;
  801. }
  802. if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_LOGO,
  803. fc_rport_logo_resp, rdata, lport->e_d_tov))
  804. fc_rport_error_retry(rdata, fp);
  805. else
  806. kref_get(&rdata->kref);
  807. }
  808. /**
  809. * fc_rport_els_adisc_resp() - Address Discovery response handler
  810. * @sp: current sequence in the ADISC exchange
  811. * @fp: response frame
  812. * @rdata_arg: remote port private.
  813. *
  814. * Locking Note: This function will be called without the rport lock
  815. * held, but it will lock, call an _enter_* function or fc_rport_error
  816. * and then unlock the rport.
  817. */
  818. static void fc_rport_adisc_resp(struct fc_seq *sp, struct fc_frame *fp,
  819. void *rdata_arg)
  820. {
  821. struct fc_rport_priv *rdata = rdata_arg;
  822. struct fc_els_adisc *adisc;
  823. u8 op;
  824. mutex_lock(&rdata->rp_mutex);
  825. FC_RPORT_DBG(rdata, "Received a ADISC response\n");
  826. if (rdata->rp_state != RPORT_ST_ADISC) {
  827. FC_RPORT_DBG(rdata, "Received a ADISC resp but in state %s\n",
  828. fc_rport_state(rdata));
  829. if (IS_ERR(fp))
  830. goto err;
  831. goto out;
  832. }
  833. if (IS_ERR(fp)) {
  834. fc_rport_error(rdata, fp);
  835. goto err;
  836. }
  837. /*
  838. * If address verification failed. Consider us logged out of the rport.
  839. * Since the rport is still in discovery, we want to be
  840. * logged in, so go to PLOGI state. Otherwise, go back to READY.
  841. */
  842. op = fc_frame_payload_op(fp);
  843. adisc = fc_frame_payload_get(fp, sizeof(*adisc));
  844. if (op != ELS_LS_ACC || !adisc ||
  845. ntoh24(adisc->adisc_port_id) != rdata->ids.port_id ||
  846. get_unaligned_be64(&adisc->adisc_wwpn) != rdata->ids.port_name ||
  847. get_unaligned_be64(&adisc->adisc_wwnn) != rdata->ids.node_name) {
  848. FC_RPORT_DBG(rdata, "ADISC error or mismatch\n");
  849. fc_rport_enter_plogi(rdata);
  850. } else {
  851. FC_RPORT_DBG(rdata, "ADISC OK\n");
  852. fc_rport_enter_ready(rdata);
  853. }
  854. out:
  855. fc_frame_free(fp);
  856. err:
  857. mutex_unlock(&rdata->rp_mutex);
  858. kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
  859. }
  860. /**
  861. * fc_rport_enter_adisc() - Send Address Discover (ADISC) request to peer
  862. * @rdata: remote port private data
  863. *
  864. * Locking Note: The rport lock is expected to be held before calling
  865. * this routine.
  866. */
  867. static void fc_rport_enter_adisc(struct fc_rport_priv *rdata)
  868. {
  869. struct fc_lport *lport = rdata->local_port;
  870. struct fc_frame *fp;
  871. FC_RPORT_DBG(rdata, "sending ADISC from %s state\n",
  872. fc_rport_state(rdata));
  873. fc_rport_state_enter(rdata, RPORT_ST_ADISC);
  874. fp = fc_frame_alloc(lport, sizeof(struct fc_els_adisc));
  875. if (!fp) {
  876. fc_rport_error_retry(rdata, fp);
  877. return;
  878. }
  879. if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_ADISC,
  880. fc_rport_adisc_resp, rdata, lport->e_d_tov))
  881. fc_rport_error_retry(rdata, fp);
  882. else
  883. kref_get(&rdata->kref);
  884. }
  885. /**
  886. * fc_rport_recv_els_req() - handle a validated ELS request.
  887. * @lport: Fibre Channel local port
  888. * @sp: current sequence in the PLOGI exchange
  889. * @fp: response frame
  890. *
  891. * Handle incoming ELS requests that require port login.
  892. * The ELS opcode has already been validated by the caller.
  893. *
  894. * Locking Note: Called with the lport lock held.
  895. */
  896. static void fc_rport_recv_els_req(struct fc_lport *lport,
  897. struct fc_seq *sp, struct fc_frame *fp)
  898. {
  899. struct fc_rport_priv *rdata;
  900. struct fc_frame_header *fh;
  901. struct fc_seq_els_data els_data;
  902. els_data.fp = NULL;
  903. els_data.reason = ELS_RJT_UNAB;
  904. els_data.explan = ELS_EXPL_PLOGI_REQD;
  905. fh = fc_frame_header_get(fp);
  906. mutex_lock(&lport->disc.disc_mutex);
  907. rdata = lport->tt.rport_lookup(lport, ntoh24(fh->fh_s_id));
  908. if (!rdata) {
  909. mutex_unlock(&lport->disc.disc_mutex);
  910. goto reject;
  911. }
  912. mutex_lock(&rdata->rp_mutex);
  913. mutex_unlock(&lport->disc.disc_mutex);
  914. switch (rdata->rp_state) {
  915. case RPORT_ST_PRLI:
  916. case RPORT_ST_RTV:
  917. case RPORT_ST_READY:
  918. case RPORT_ST_ADISC:
  919. break;
  920. default:
  921. mutex_unlock(&rdata->rp_mutex);
  922. goto reject;
  923. }
  924. switch (fc_frame_payload_op(fp)) {
  925. case ELS_PRLI:
  926. fc_rport_recv_prli_req(rdata, sp, fp);
  927. break;
  928. case ELS_PRLO:
  929. fc_rport_recv_prlo_req(rdata, sp, fp);
  930. break;
  931. case ELS_RRQ:
  932. els_data.fp = fp;
  933. lport->tt.seq_els_rsp_send(sp, ELS_RRQ, &els_data);
  934. break;
  935. case ELS_REC:
  936. els_data.fp = fp;
  937. lport->tt.seq_els_rsp_send(sp, ELS_REC, &els_data);
  938. break;
  939. default:
  940. fc_frame_free(fp); /* can't happen */
  941. break;
  942. }
  943. mutex_unlock(&rdata->rp_mutex);
  944. return;
  945. reject:
  946. lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &els_data);
  947. fc_frame_free(fp);
  948. }
  949. /**
  950. * fc_rport_recv_req() - Handle a received ELS request from a rport
  951. * @sp: current sequence in the PLOGI exchange
  952. * @fp: response frame
  953. * @lport: Fibre Channel local port
  954. *
  955. * Locking Note: Called with the lport lock held.
  956. */
  957. void fc_rport_recv_req(struct fc_seq *sp, struct fc_frame *fp,
  958. struct fc_lport *lport)
  959. {
  960. struct fc_seq_els_data els_data;
  961. /*
  962. * Handle PLOGI and LOGO requests separately, since they
  963. * don't require prior login.
  964. * Check for unsupported opcodes first and reject them.
  965. * For some ops, it would be incorrect to reject with "PLOGI required".
  966. */
  967. switch (fc_frame_payload_op(fp)) {
  968. case ELS_PLOGI:
  969. fc_rport_recv_plogi_req(lport, sp, fp);
  970. break;
  971. case ELS_LOGO:
  972. fc_rport_recv_logo_req(lport, sp, fp);
  973. break;
  974. case ELS_PRLI:
  975. case ELS_PRLO:
  976. case ELS_RRQ:
  977. case ELS_REC:
  978. fc_rport_recv_els_req(lport, sp, fp);
  979. break;
  980. default:
  981. fc_frame_free(fp);
  982. els_data.fp = NULL;
  983. els_data.reason = ELS_RJT_UNSUP;
  984. els_data.explan = ELS_EXPL_NONE;
  985. lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &els_data);
  986. break;
  987. }
  988. }
  989. /**
  990. * fc_rport_recv_plogi_req() - Handle incoming Port Login (PLOGI) request
  991. * @lport: local port
  992. * @sp: current sequence in the PLOGI exchange
  993. * @fp: PLOGI request frame
  994. *
  995. * Locking Note: The rport lock is held before calling this function.
  996. */
  997. static void fc_rport_recv_plogi_req(struct fc_lport *lport,
  998. struct fc_seq *sp, struct fc_frame *rx_fp)
  999. {
  1000. struct fc_disc *disc;
  1001. struct fc_rport_priv *rdata;
  1002. struct fc_frame *fp = rx_fp;
  1003. struct fc_exch *ep;
  1004. struct fc_frame_header *fh;
  1005. struct fc_els_flogi *pl;
  1006. struct fc_seq_els_data rjt_data;
  1007. u32 sid, f_ctl;
  1008. rjt_data.fp = NULL;
  1009. fh = fc_frame_header_get(fp);
  1010. sid = ntoh24(fh->fh_s_id);
  1011. FC_RPORT_ID_DBG(lport, sid, "Received PLOGI request\n");
  1012. pl = fc_frame_payload_get(fp, sizeof(*pl));
  1013. if (!pl) {
  1014. FC_RPORT_ID_DBG(lport, sid, "Received PLOGI too short\n");
  1015. rjt_data.reason = ELS_RJT_PROT;
  1016. rjt_data.explan = ELS_EXPL_INV_LEN;
  1017. goto reject;
  1018. }
  1019. disc = &lport->disc;
  1020. mutex_lock(&disc->disc_mutex);
  1021. rdata = lport->tt.rport_create(lport, sid);
  1022. if (!rdata) {
  1023. mutex_unlock(&disc->disc_mutex);
  1024. rjt_data.reason = ELS_RJT_UNAB;
  1025. rjt_data.explan = ELS_EXPL_INSUF_RES;
  1026. goto reject;
  1027. }
  1028. mutex_lock(&rdata->rp_mutex);
  1029. mutex_unlock(&disc->disc_mutex);
  1030. rdata->ids.port_name = get_unaligned_be64(&pl->fl_wwpn);
  1031. rdata->ids.node_name = get_unaligned_be64(&pl->fl_wwnn);
  1032. /*
  1033. * If the rport was just created, possibly due to the incoming PLOGI,
  1034. * set the state appropriately and accept the PLOGI.
  1035. *
  1036. * If we had also sent a PLOGI, and if the received PLOGI is from a
  1037. * higher WWPN, we accept it, otherwise an LS_RJT is sent with reason
  1038. * "command already in progress".
  1039. *
  1040. * XXX TBD: If the session was ready before, the PLOGI should result in
  1041. * all outstanding exchanges being reset.
  1042. */
  1043. switch (rdata->rp_state) {
  1044. case RPORT_ST_INIT:
  1045. FC_RPORT_DBG(rdata, "Received PLOGI in INIT state\n");
  1046. break;
  1047. case RPORT_ST_PLOGI:
  1048. FC_RPORT_DBG(rdata, "Received PLOGI in PLOGI state\n");
  1049. if (rdata->ids.port_name < lport->wwpn) {
  1050. mutex_unlock(&rdata->rp_mutex);
  1051. rjt_data.reason = ELS_RJT_INPROG;
  1052. rjt_data.explan = ELS_EXPL_NONE;
  1053. goto reject;
  1054. }
  1055. break;
  1056. case RPORT_ST_PRLI:
  1057. case RPORT_ST_READY:
  1058. case RPORT_ST_ADISC:
  1059. FC_RPORT_DBG(rdata, "Received PLOGI in logged-in state %d "
  1060. "- ignored for now\n", rdata->rp_state);
  1061. /* XXX TBD - should reset */
  1062. break;
  1063. case RPORT_ST_DELETE:
  1064. default:
  1065. FC_RPORT_DBG(rdata, "Received PLOGI in unexpected state %d\n",
  1066. rdata->rp_state);
  1067. fc_frame_free(rx_fp);
  1068. goto out;
  1069. }
  1070. /*
  1071. * Get session payload size from incoming PLOGI.
  1072. */
  1073. rdata->maxframe_size = fc_plogi_get_maxframe(pl, lport->mfs);
  1074. fc_frame_free(rx_fp);
  1075. /*
  1076. * Send LS_ACC. If this fails, the originator should retry.
  1077. */
  1078. sp = lport->tt.seq_start_next(sp);
  1079. if (!sp)
  1080. goto out;
  1081. fp = fc_frame_alloc(lport, sizeof(*pl));
  1082. if (!fp)
  1083. goto out;
  1084. fc_plogi_fill(lport, fp, ELS_LS_ACC);
  1085. f_ctl = FC_FC_EX_CTX | FC_FC_LAST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT;
  1086. ep = fc_seq_exch(sp);
  1087. fc_fill_fc_hdr(fp, FC_RCTL_ELS_REP, ep->did, ep->sid,
  1088. FC_TYPE_ELS, f_ctl, 0);
  1089. lport->tt.seq_send(lport, sp, fp);
  1090. fc_rport_enter_prli(rdata);
  1091. out:
  1092. mutex_unlock(&rdata->rp_mutex);
  1093. return;
  1094. reject:
  1095. lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
  1096. fc_frame_free(fp);
  1097. }
  1098. /**
  1099. * fc_rport_recv_prli_req() - Handle incoming Process Login (PRLI) request
  1100. * @rdata: private remote port data
  1101. * @sp: current sequence in the PRLI exchange
  1102. * @fp: PRLI request frame
  1103. *
  1104. * Locking Note: The rport lock is exected to be held before calling
  1105. * this function.
  1106. */
  1107. static void fc_rport_recv_prli_req(struct fc_rport_priv *rdata,
  1108. struct fc_seq *sp, struct fc_frame *rx_fp)
  1109. {
  1110. struct fc_lport *lport = rdata->local_port;
  1111. struct fc_exch *ep;
  1112. struct fc_frame *fp;
  1113. struct fc_frame_header *fh;
  1114. struct {
  1115. struct fc_els_prli prli;
  1116. struct fc_els_spp spp;
  1117. } *pp;
  1118. struct fc_els_spp *rspp; /* request service param page */
  1119. struct fc_els_spp *spp; /* response spp */
  1120. unsigned int len;
  1121. unsigned int plen;
  1122. enum fc_els_rjt_reason reason = ELS_RJT_UNAB;
  1123. enum fc_els_rjt_explan explan = ELS_EXPL_NONE;
  1124. enum fc_els_spp_resp resp;
  1125. struct fc_seq_els_data rjt_data;
  1126. u32 f_ctl;
  1127. u32 fcp_parm;
  1128. u32 roles = FC_RPORT_ROLE_UNKNOWN;
  1129. rjt_data.fp = NULL;
  1130. fh = fc_frame_header_get(rx_fp);
  1131. FC_RPORT_DBG(rdata, "Received PRLI request while in state %s\n",
  1132. fc_rport_state(rdata));
  1133. switch (rdata->rp_state) {
  1134. case RPORT_ST_PRLI:
  1135. case RPORT_ST_RTV:
  1136. case RPORT_ST_READY:
  1137. case RPORT_ST_ADISC:
  1138. reason = ELS_RJT_NONE;
  1139. break;
  1140. default:
  1141. fc_frame_free(rx_fp);
  1142. return;
  1143. break;
  1144. }
  1145. len = fr_len(rx_fp) - sizeof(*fh);
  1146. pp = fc_frame_payload_get(rx_fp, sizeof(*pp));
  1147. if (pp == NULL) {
  1148. reason = ELS_RJT_PROT;
  1149. explan = ELS_EXPL_INV_LEN;
  1150. } else {
  1151. plen = ntohs(pp->prli.prli_len);
  1152. if ((plen % 4) != 0 || plen > len) {
  1153. reason = ELS_RJT_PROT;
  1154. explan = ELS_EXPL_INV_LEN;
  1155. } else if (plen < len) {
  1156. len = plen;
  1157. }
  1158. plen = pp->prli.prli_spp_len;
  1159. if ((plen % 4) != 0 || plen < sizeof(*spp) ||
  1160. plen > len || len < sizeof(*pp)) {
  1161. reason = ELS_RJT_PROT;
  1162. explan = ELS_EXPL_INV_LEN;
  1163. }
  1164. rspp = &pp->spp;
  1165. }
  1166. if (reason != ELS_RJT_NONE ||
  1167. (fp = fc_frame_alloc(lport, len)) == NULL) {
  1168. rjt_data.reason = reason;
  1169. rjt_data.explan = explan;
  1170. lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
  1171. } else {
  1172. sp = lport->tt.seq_start_next(sp);
  1173. WARN_ON(!sp);
  1174. pp = fc_frame_payload_get(fp, len);
  1175. WARN_ON(!pp);
  1176. memset(pp, 0, len);
  1177. pp->prli.prli_cmd = ELS_LS_ACC;
  1178. pp->prli.prli_spp_len = plen;
  1179. pp->prli.prli_len = htons(len);
  1180. len -= sizeof(struct fc_els_prli);
  1181. /* reinitialize remote port roles */
  1182. rdata->ids.roles = FC_RPORT_ROLE_UNKNOWN;
  1183. /*
  1184. * Go through all the service parameter pages and build
  1185. * response. If plen indicates longer SPP than standard,
  1186. * use that. The entire response has been pre-cleared above.
  1187. */
  1188. spp = &pp->spp;
  1189. while (len >= plen) {
  1190. spp->spp_type = rspp->spp_type;
  1191. spp->spp_type_ext = rspp->spp_type_ext;
  1192. spp->spp_flags = rspp->spp_flags & FC_SPP_EST_IMG_PAIR;
  1193. resp = FC_SPP_RESP_ACK;
  1194. if (rspp->spp_flags & FC_SPP_RPA_VAL)
  1195. resp = FC_SPP_RESP_NO_PA;
  1196. switch (rspp->spp_type) {
  1197. case 0: /* common to all FC-4 types */
  1198. break;
  1199. case FC_TYPE_FCP:
  1200. fcp_parm = ntohl(rspp->spp_params);
  1201. if (fcp_parm * FCP_SPPF_RETRY)
  1202. rdata->flags |= FC_RP_FLAGS_RETRY;
  1203. rdata->supported_classes = FC_COS_CLASS3;
  1204. if (fcp_parm & FCP_SPPF_INIT_FCN)
  1205. roles |= FC_RPORT_ROLE_FCP_INITIATOR;
  1206. if (fcp_parm & FCP_SPPF_TARG_FCN)
  1207. roles |= FC_RPORT_ROLE_FCP_TARGET;
  1208. rdata->ids.roles = roles;
  1209. spp->spp_params =
  1210. htonl(lport->service_params);
  1211. break;
  1212. default:
  1213. resp = FC_SPP_RESP_INVL;
  1214. break;
  1215. }
  1216. spp->spp_flags |= resp;
  1217. len -= plen;
  1218. rspp = (struct fc_els_spp *)((char *)rspp + plen);
  1219. spp = (struct fc_els_spp *)((char *)spp + plen);
  1220. }
  1221. /*
  1222. * Send LS_ACC. If this fails, the originator should retry.
  1223. */
  1224. f_ctl = FC_FC_EX_CTX | FC_FC_LAST_SEQ;
  1225. f_ctl |= FC_FC_END_SEQ | FC_FC_SEQ_INIT;
  1226. ep = fc_seq_exch(sp);
  1227. fc_fill_fc_hdr(fp, FC_RCTL_ELS_REP, ep->did, ep->sid,
  1228. FC_TYPE_ELS, f_ctl, 0);
  1229. lport->tt.seq_send(lport, sp, fp);
  1230. /*
  1231. * Get lock and re-check state.
  1232. */
  1233. switch (rdata->rp_state) {
  1234. case RPORT_ST_PRLI:
  1235. fc_rport_enter_ready(rdata);
  1236. break;
  1237. case RPORT_ST_READY:
  1238. case RPORT_ST_ADISC:
  1239. break;
  1240. default:
  1241. break;
  1242. }
  1243. }
  1244. fc_frame_free(rx_fp);
  1245. }
  1246. /**
  1247. * fc_rport_recv_prlo_req() - Handle incoming Process Logout (PRLO) request
  1248. * @rdata: private remote port data
  1249. * @sp: current sequence in the PRLO exchange
  1250. * @fp: PRLO request frame
  1251. *
  1252. * Locking Note: The rport lock is exected to be held before calling
  1253. * this function.
  1254. */
  1255. static void fc_rport_recv_prlo_req(struct fc_rport_priv *rdata,
  1256. struct fc_seq *sp,
  1257. struct fc_frame *fp)
  1258. {
  1259. struct fc_lport *lport = rdata->local_port;
  1260. struct fc_frame_header *fh;
  1261. struct fc_seq_els_data rjt_data;
  1262. fh = fc_frame_header_get(fp);
  1263. FC_RPORT_DBG(rdata, "Received PRLO request while in state %s\n",
  1264. fc_rport_state(rdata));
  1265. rjt_data.fp = NULL;
  1266. rjt_data.reason = ELS_RJT_UNAB;
  1267. rjt_data.explan = ELS_EXPL_NONE;
  1268. lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
  1269. fc_frame_free(fp);
  1270. }
  1271. /**
  1272. * fc_rport_recv_logo_req() - Handle incoming Logout (LOGO) request
  1273. * @lport: local port.
  1274. * @sp: current sequence in the LOGO exchange
  1275. * @fp: LOGO request frame
  1276. *
  1277. * Locking Note: The rport lock is exected to be held before calling
  1278. * this function.
  1279. */
  1280. static void fc_rport_recv_logo_req(struct fc_lport *lport,
  1281. struct fc_seq *sp,
  1282. struct fc_frame *fp)
  1283. {
  1284. struct fc_frame_header *fh;
  1285. struct fc_rport_priv *rdata;
  1286. u32 sid;
  1287. lport->tt.seq_els_rsp_send(sp, ELS_LS_ACC, NULL);
  1288. fh = fc_frame_header_get(fp);
  1289. sid = ntoh24(fh->fh_s_id);
  1290. mutex_lock(&lport->disc.disc_mutex);
  1291. rdata = lport->tt.rport_lookup(lport, sid);
  1292. if (rdata) {
  1293. mutex_lock(&rdata->rp_mutex);
  1294. FC_RPORT_DBG(rdata, "Received LOGO request while in state %s\n",
  1295. fc_rport_state(rdata));
  1296. /*
  1297. * If the remote port was created due to discovery,
  1298. * log back in. It may have seen a stale RSCN about us.
  1299. */
  1300. if (rdata->rp_state != RPORT_ST_DELETE && rdata->disc_id)
  1301. fc_rport_enter_plogi(rdata);
  1302. else
  1303. fc_rport_enter_delete(rdata, RPORT_EV_LOGO);
  1304. mutex_unlock(&rdata->rp_mutex);
  1305. } else
  1306. FC_RPORT_ID_DBG(lport, sid,
  1307. "Received LOGO from non-logged-in port\n");
  1308. mutex_unlock(&lport->disc.disc_mutex);
  1309. fc_frame_free(fp);
  1310. }
  1311. static void fc_rport_flush_queue(void)
  1312. {
  1313. flush_workqueue(rport_event_queue);
  1314. }
  1315. int fc_rport_init(struct fc_lport *lport)
  1316. {
  1317. if (!lport->tt.rport_lookup)
  1318. lport->tt.rport_lookup = fc_rport_lookup;
  1319. if (!lport->tt.rport_create)
  1320. lport->tt.rport_create = fc_rport_create;
  1321. if (!lport->tt.rport_login)
  1322. lport->tt.rport_login = fc_rport_login;
  1323. if (!lport->tt.rport_logoff)
  1324. lport->tt.rport_logoff = fc_rport_logoff;
  1325. if (!lport->tt.rport_recv_req)
  1326. lport->tt.rport_recv_req = fc_rport_recv_req;
  1327. if (!lport->tt.rport_flush_queue)
  1328. lport->tt.rport_flush_queue = fc_rport_flush_queue;
  1329. if (!lport->tt.rport_destroy)
  1330. lport->tt.rport_destroy = fc_rport_destroy;
  1331. return 0;
  1332. }
  1333. EXPORT_SYMBOL(fc_rport_init);
  1334. int fc_setup_rport(void)
  1335. {
  1336. rport_event_queue = create_singlethread_workqueue("fc_rport_eq");
  1337. if (!rport_event_queue)
  1338. return -ENOMEM;
  1339. return 0;
  1340. }
  1341. EXPORT_SYMBOL(fc_setup_rport);
  1342. void fc_destroy_rport(void)
  1343. {
  1344. destroy_workqueue(rport_event_queue);
  1345. }
  1346. EXPORT_SYMBOL(fc_destroy_rport);
  1347. void fc_rport_terminate_io(struct fc_rport *rport)
  1348. {
  1349. struct fc_rport_libfc_priv *rp = rport->dd_data;
  1350. struct fc_lport *lport = rp->local_port;
  1351. lport->tt.exch_mgr_reset(lport, 0, rport->port_id);
  1352. lport->tt.exch_mgr_reset(lport, rport->port_id, 0);
  1353. }
  1354. EXPORT_SYMBOL(fc_rport_terminate_io);