fc_rport.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584
  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_adisc_req() - Handle incoming Address Discovery (ADISC) Request
  887. * @rdata: remote port private
  888. * @sp: current sequence in the ADISC exchange
  889. * @in_fp: ADISC request frame
  890. *
  891. * Locking Note: Called with the lport and rport locks held.
  892. */
  893. static void fc_rport_recv_adisc_req(struct fc_rport_priv *rdata,
  894. struct fc_seq *sp, struct fc_frame *in_fp)
  895. {
  896. struct fc_lport *lport = rdata->local_port;
  897. struct fc_frame *fp;
  898. struct fc_exch *ep = fc_seq_exch(sp);
  899. struct fc_els_adisc *adisc;
  900. struct fc_seq_els_data rjt_data;
  901. u32 f_ctl;
  902. FC_RPORT_DBG(rdata, "Received ADISC request\n");
  903. adisc = fc_frame_payload_get(in_fp, sizeof(*adisc));
  904. if (!adisc) {
  905. rjt_data.fp = NULL;
  906. rjt_data.reason = ELS_RJT_PROT;
  907. rjt_data.explan = ELS_EXPL_INV_LEN;
  908. lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
  909. goto drop;
  910. }
  911. fp = fc_frame_alloc(lport, sizeof(*adisc));
  912. if (!fp)
  913. goto drop;
  914. fc_adisc_fill(lport, fp);
  915. adisc = fc_frame_payload_get(fp, sizeof(*adisc));
  916. adisc->adisc_cmd = ELS_LS_ACC;
  917. sp = lport->tt.seq_start_next(sp);
  918. f_ctl = FC_FC_EX_CTX | FC_FC_LAST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT;
  919. fc_fill_fc_hdr(fp, FC_RCTL_ELS_REP, ep->did, ep->sid,
  920. FC_TYPE_ELS, f_ctl, 0);
  921. lport->tt.seq_send(lport, sp, fp);
  922. drop:
  923. fc_frame_free(in_fp);
  924. }
  925. /**
  926. * fc_rport_recv_els_req() - handle a validated ELS request.
  927. * @lport: Fibre Channel local port
  928. * @sp: current sequence in the PLOGI exchange
  929. * @fp: response frame
  930. *
  931. * Handle incoming ELS requests that require port login.
  932. * The ELS opcode has already been validated by the caller.
  933. *
  934. * Locking Note: Called with the lport lock held.
  935. */
  936. static void fc_rport_recv_els_req(struct fc_lport *lport,
  937. struct fc_seq *sp, struct fc_frame *fp)
  938. {
  939. struct fc_rport_priv *rdata;
  940. struct fc_frame_header *fh;
  941. struct fc_seq_els_data els_data;
  942. els_data.fp = NULL;
  943. els_data.reason = ELS_RJT_UNAB;
  944. els_data.explan = ELS_EXPL_PLOGI_REQD;
  945. fh = fc_frame_header_get(fp);
  946. mutex_lock(&lport->disc.disc_mutex);
  947. rdata = lport->tt.rport_lookup(lport, ntoh24(fh->fh_s_id));
  948. if (!rdata) {
  949. mutex_unlock(&lport->disc.disc_mutex);
  950. goto reject;
  951. }
  952. mutex_lock(&rdata->rp_mutex);
  953. mutex_unlock(&lport->disc.disc_mutex);
  954. switch (rdata->rp_state) {
  955. case RPORT_ST_PRLI:
  956. case RPORT_ST_RTV:
  957. case RPORT_ST_READY:
  958. case RPORT_ST_ADISC:
  959. break;
  960. default:
  961. mutex_unlock(&rdata->rp_mutex);
  962. goto reject;
  963. }
  964. switch (fc_frame_payload_op(fp)) {
  965. case ELS_PRLI:
  966. fc_rport_recv_prli_req(rdata, sp, fp);
  967. break;
  968. case ELS_PRLO:
  969. fc_rport_recv_prlo_req(rdata, sp, fp);
  970. break;
  971. case ELS_ADISC:
  972. fc_rport_recv_adisc_req(rdata, sp, fp);
  973. break;
  974. case ELS_RRQ:
  975. els_data.fp = fp;
  976. lport->tt.seq_els_rsp_send(sp, ELS_RRQ, &els_data);
  977. break;
  978. case ELS_REC:
  979. els_data.fp = fp;
  980. lport->tt.seq_els_rsp_send(sp, ELS_REC, &els_data);
  981. break;
  982. default:
  983. fc_frame_free(fp); /* can't happen */
  984. break;
  985. }
  986. mutex_unlock(&rdata->rp_mutex);
  987. return;
  988. reject:
  989. lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &els_data);
  990. fc_frame_free(fp);
  991. }
  992. /**
  993. * fc_rport_recv_req() - Handle a received ELS request from a rport
  994. * @sp: current sequence in the PLOGI exchange
  995. * @fp: response frame
  996. * @lport: Fibre Channel local port
  997. *
  998. * Locking Note: Called with the lport lock held.
  999. */
  1000. void fc_rport_recv_req(struct fc_seq *sp, struct fc_frame *fp,
  1001. struct fc_lport *lport)
  1002. {
  1003. struct fc_seq_els_data els_data;
  1004. /*
  1005. * Handle PLOGI and LOGO requests separately, since they
  1006. * don't require prior login.
  1007. * Check for unsupported opcodes first and reject them.
  1008. * For some ops, it would be incorrect to reject with "PLOGI required".
  1009. */
  1010. switch (fc_frame_payload_op(fp)) {
  1011. case ELS_PLOGI:
  1012. fc_rport_recv_plogi_req(lport, sp, fp);
  1013. break;
  1014. case ELS_LOGO:
  1015. fc_rport_recv_logo_req(lport, sp, fp);
  1016. break;
  1017. case ELS_PRLI:
  1018. case ELS_PRLO:
  1019. case ELS_ADISC:
  1020. case ELS_RRQ:
  1021. case ELS_REC:
  1022. fc_rport_recv_els_req(lport, sp, fp);
  1023. break;
  1024. default:
  1025. fc_frame_free(fp);
  1026. els_data.fp = NULL;
  1027. els_data.reason = ELS_RJT_UNSUP;
  1028. els_data.explan = ELS_EXPL_NONE;
  1029. lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &els_data);
  1030. break;
  1031. }
  1032. }
  1033. /**
  1034. * fc_rport_recv_plogi_req() - Handle incoming Port Login (PLOGI) request
  1035. * @lport: local port
  1036. * @sp: current sequence in the PLOGI exchange
  1037. * @fp: PLOGI request frame
  1038. *
  1039. * Locking Note: The rport lock is held before calling this function.
  1040. */
  1041. static void fc_rport_recv_plogi_req(struct fc_lport *lport,
  1042. struct fc_seq *sp, struct fc_frame *rx_fp)
  1043. {
  1044. struct fc_disc *disc;
  1045. struct fc_rport_priv *rdata;
  1046. struct fc_frame *fp = rx_fp;
  1047. struct fc_exch *ep;
  1048. struct fc_frame_header *fh;
  1049. struct fc_els_flogi *pl;
  1050. struct fc_seq_els_data rjt_data;
  1051. u32 sid, f_ctl;
  1052. rjt_data.fp = NULL;
  1053. fh = fc_frame_header_get(fp);
  1054. sid = ntoh24(fh->fh_s_id);
  1055. FC_RPORT_ID_DBG(lport, sid, "Received PLOGI request\n");
  1056. pl = fc_frame_payload_get(fp, sizeof(*pl));
  1057. if (!pl) {
  1058. FC_RPORT_ID_DBG(lport, sid, "Received PLOGI too short\n");
  1059. rjt_data.reason = ELS_RJT_PROT;
  1060. rjt_data.explan = ELS_EXPL_INV_LEN;
  1061. goto reject;
  1062. }
  1063. disc = &lport->disc;
  1064. mutex_lock(&disc->disc_mutex);
  1065. rdata = lport->tt.rport_create(lport, sid);
  1066. if (!rdata) {
  1067. mutex_unlock(&disc->disc_mutex);
  1068. rjt_data.reason = ELS_RJT_UNAB;
  1069. rjt_data.explan = ELS_EXPL_INSUF_RES;
  1070. goto reject;
  1071. }
  1072. mutex_lock(&rdata->rp_mutex);
  1073. mutex_unlock(&disc->disc_mutex);
  1074. rdata->ids.port_name = get_unaligned_be64(&pl->fl_wwpn);
  1075. rdata->ids.node_name = get_unaligned_be64(&pl->fl_wwnn);
  1076. /*
  1077. * If the rport was just created, possibly due to the incoming PLOGI,
  1078. * set the state appropriately and accept the PLOGI.
  1079. *
  1080. * If we had also sent a PLOGI, and if the received PLOGI is from a
  1081. * higher WWPN, we accept it, otherwise an LS_RJT is sent with reason
  1082. * "command already in progress".
  1083. *
  1084. * XXX TBD: If the session was ready before, the PLOGI should result in
  1085. * all outstanding exchanges being reset.
  1086. */
  1087. switch (rdata->rp_state) {
  1088. case RPORT_ST_INIT:
  1089. FC_RPORT_DBG(rdata, "Received PLOGI in INIT state\n");
  1090. break;
  1091. case RPORT_ST_PLOGI:
  1092. FC_RPORT_DBG(rdata, "Received PLOGI in PLOGI state\n");
  1093. if (rdata->ids.port_name < lport->wwpn) {
  1094. mutex_unlock(&rdata->rp_mutex);
  1095. rjt_data.reason = ELS_RJT_INPROG;
  1096. rjt_data.explan = ELS_EXPL_NONE;
  1097. goto reject;
  1098. }
  1099. break;
  1100. case RPORT_ST_PRLI:
  1101. case RPORT_ST_READY:
  1102. case RPORT_ST_ADISC:
  1103. FC_RPORT_DBG(rdata, "Received PLOGI in logged-in state %d "
  1104. "- ignored for now\n", rdata->rp_state);
  1105. /* XXX TBD - should reset */
  1106. break;
  1107. case RPORT_ST_DELETE:
  1108. default:
  1109. FC_RPORT_DBG(rdata, "Received PLOGI in unexpected state %d\n",
  1110. rdata->rp_state);
  1111. fc_frame_free(rx_fp);
  1112. goto out;
  1113. }
  1114. /*
  1115. * Get session payload size from incoming PLOGI.
  1116. */
  1117. rdata->maxframe_size = fc_plogi_get_maxframe(pl, lport->mfs);
  1118. fc_frame_free(rx_fp);
  1119. /*
  1120. * Send LS_ACC. If this fails, the originator should retry.
  1121. */
  1122. sp = lport->tt.seq_start_next(sp);
  1123. if (!sp)
  1124. goto out;
  1125. fp = fc_frame_alloc(lport, sizeof(*pl));
  1126. if (!fp)
  1127. goto out;
  1128. fc_plogi_fill(lport, fp, ELS_LS_ACC);
  1129. f_ctl = FC_FC_EX_CTX | FC_FC_LAST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT;
  1130. ep = fc_seq_exch(sp);
  1131. fc_fill_fc_hdr(fp, FC_RCTL_ELS_REP, ep->did, ep->sid,
  1132. FC_TYPE_ELS, f_ctl, 0);
  1133. lport->tt.seq_send(lport, sp, fp);
  1134. fc_rport_enter_prli(rdata);
  1135. out:
  1136. mutex_unlock(&rdata->rp_mutex);
  1137. return;
  1138. reject:
  1139. lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
  1140. fc_frame_free(fp);
  1141. }
  1142. /**
  1143. * fc_rport_recv_prli_req() - Handle incoming Process Login (PRLI) request
  1144. * @rdata: private remote port data
  1145. * @sp: current sequence in the PRLI exchange
  1146. * @fp: PRLI request frame
  1147. *
  1148. * Locking Note: The rport lock is exected to be held before calling
  1149. * this function.
  1150. */
  1151. static void fc_rport_recv_prli_req(struct fc_rport_priv *rdata,
  1152. struct fc_seq *sp, struct fc_frame *rx_fp)
  1153. {
  1154. struct fc_lport *lport = rdata->local_port;
  1155. struct fc_exch *ep;
  1156. struct fc_frame *fp;
  1157. struct fc_frame_header *fh;
  1158. struct {
  1159. struct fc_els_prli prli;
  1160. struct fc_els_spp spp;
  1161. } *pp;
  1162. struct fc_els_spp *rspp; /* request service param page */
  1163. struct fc_els_spp *spp; /* response spp */
  1164. unsigned int len;
  1165. unsigned int plen;
  1166. enum fc_els_rjt_reason reason = ELS_RJT_UNAB;
  1167. enum fc_els_rjt_explan explan = ELS_EXPL_NONE;
  1168. enum fc_els_spp_resp resp;
  1169. struct fc_seq_els_data rjt_data;
  1170. u32 f_ctl;
  1171. u32 fcp_parm;
  1172. u32 roles = FC_RPORT_ROLE_UNKNOWN;
  1173. rjt_data.fp = NULL;
  1174. fh = fc_frame_header_get(rx_fp);
  1175. FC_RPORT_DBG(rdata, "Received PRLI request while in state %s\n",
  1176. fc_rport_state(rdata));
  1177. switch (rdata->rp_state) {
  1178. case RPORT_ST_PRLI:
  1179. case RPORT_ST_RTV:
  1180. case RPORT_ST_READY:
  1181. case RPORT_ST_ADISC:
  1182. reason = ELS_RJT_NONE;
  1183. break;
  1184. default:
  1185. fc_frame_free(rx_fp);
  1186. return;
  1187. break;
  1188. }
  1189. len = fr_len(rx_fp) - sizeof(*fh);
  1190. pp = fc_frame_payload_get(rx_fp, sizeof(*pp));
  1191. if (pp == NULL) {
  1192. reason = ELS_RJT_PROT;
  1193. explan = ELS_EXPL_INV_LEN;
  1194. } else {
  1195. plen = ntohs(pp->prli.prli_len);
  1196. if ((plen % 4) != 0 || plen > len) {
  1197. reason = ELS_RJT_PROT;
  1198. explan = ELS_EXPL_INV_LEN;
  1199. } else if (plen < len) {
  1200. len = plen;
  1201. }
  1202. plen = pp->prli.prli_spp_len;
  1203. if ((plen % 4) != 0 || plen < sizeof(*spp) ||
  1204. plen > len || len < sizeof(*pp)) {
  1205. reason = ELS_RJT_PROT;
  1206. explan = ELS_EXPL_INV_LEN;
  1207. }
  1208. rspp = &pp->spp;
  1209. }
  1210. if (reason != ELS_RJT_NONE ||
  1211. (fp = fc_frame_alloc(lport, len)) == NULL) {
  1212. rjt_data.reason = reason;
  1213. rjt_data.explan = explan;
  1214. lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
  1215. } else {
  1216. sp = lport->tt.seq_start_next(sp);
  1217. WARN_ON(!sp);
  1218. pp = fc_frame_payload_get(fp, len);
  1219. WARN_ON(!pp);
  1220. memset(pp, 0, len);
  1221. pp->prli.prli_cmd = ELS_LS_ACC;
  1222. pp->prli.prli_spp_len = plen;
  1223. pp->prli.prli_len = htons(len);
  1224. len -= sizeof(struct fc_els_prli);
  1225. /* reinitialize remote port roles */
  1226. rdata->ids.roles = FC_RPORT_ROLE_UNKNOWN;
  1227. /*
  1228. * Go through all the service parameter pages and build
  1229. * response. If plen indicates longer SPP than standard,
  1230. * use that. The entire response has been pre-cleared above.
  1231. */
  1232. spp = &pp->spp;
  1233. while (len >= plen) {
  1234. spp->spp_type = rspp->spp_type;
  1235. spp->spp_type_ext = rspp->spp_type_ext;
  1236. spp->spp_flags = rspp->spp_flags & FC_SPP_EST_IMG_PAIR;
  1237. resp = FC_SPP_RESP_ACK;
  1238. if (rspp->spp_flags & FC_SPP_RPA_VAL)
  1239. resp = FC_SPP_RESP_NO_PA;
  1240. switch (rspp->spp_type) {
  1241. case 0: /* common to all FC-4 types */
  1242. break;
  1243. case FC_TYPE_FCP:
  1244. fcp_parm = ntohl(rspp->spp_params);
  1245. if (fcp_parm * FCP_SPPF_RETRY)
  1246. rdata->flags |= FC_RP_FLAGS_RETRY;
  1247. rdata->supported_classes = FC_COS_CLASS3;
  1248. if (fcp_parm & FCP_SPPF_INIT_FCN)
  1249. roles |= FC_RPORT_ROLE_FCP_INITIATOR;
  1250. if (fcp_parm & FCP_SPPF_TARG_FCN)
  1251. roles |= FC_RPORT_ROLE_FCP_TARGET;
  1252. rdata->ids.roles = roles;
  1253. spp->spp_params =
  1254. htonl(lport->service_params);
  1255. break;
  1256. default:
  1257. resp = FC_SPP_RESP_INVL;
  1258. break;
  1259. }
  1260. spp->spp_flags |= resp;
  1261. len -= plen;
  1262. rspp = (struct fc_els_spp *)((char *)rspp + plen);
  1263. spp = (struct fc_els_spp *)((char *)spp + plen);
  1264. }
  1265. /*
  1266. * Send LS_ACC. If this fails, the originator should retry.
  1267. */
  1268. f_ctl = FC_FC_EX_CTX | FC_FC_LAST_SEQ;
  1269. f_ctl |= FC_FC_END_SEQ | FC_FC_SEQ_INIT;
  1270. ep = fc_seq_exch(sp);
  1271. fc_fill_fc_hdr(fp, FC_RCTL_ELS_REP, ep->did, ep->sid,
  1272. FC_TYPE_ELS, f_ctl, 0);
  1273. lport->tt.seq_send(lport, sp, fp);
  1274. /*
  1275. * Get lock and re-check state.
  1276. */
  1277. switch (rdata->rp_state) {
  1278. case RPORT_ST_PRLI:
  1279. fc_rport_enter_ready(rdata);
  1280. break;
  1281. case RPORT_ST_READY:
  1282. case RPORT_ST_ADISC:
  1283. break;
  1284. default:
  1285. break;
  1286. }
  1287. }
  1288. fc_frame_free(rx_fp);
  1289. }
  1290. /**
  1291. * fc_rport_recv_prlo_req() - Handle incoming Process Logout (PRLO) request
  1292. * @rdata: private remote port data
  1293. * @sp: current sequence in the PRLO exchange
  1294. * @fp: PRLO request frame
  1295. *
  1296. * Locking Note: The rport lock is exected to be held before calling
  1297. * this function.
  1298. */
  1299. static void fc_rport_recv_prlo_req(struct fc_rport_priv *rdata,
  1300. struct fc_seq *sp,
  1301. struct fc_frame *fp)
  1302. {
  1303. struct fc_lport *lport = rdata->local_port;
  1304. struct fc_frame_header *fh;
  1305. struct fc_seq_els_data rjt_data;
  1306. fh = fc_frame_header_get(fp);
  1307. FC_RPORT_DBG(rdata, "Received PRLO request while in state %s\n",
  1308. fc_rport_state(rdata));
  1309. rjt_data.fp = NULL;
  1310. rjt_data.reason = ELS_RJT_UNAB;
  1311. rjt_data.explan = ELS_EXPL_NONE;
  1312. lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
  1313. fc_frame_free(fp);
  1314. }
  1315. /**
  1316. * fc_rport_recv_logo_req() - Handle incoming Logout (LOGO) request
  1317. * @lport: local port.
  1318. * @sp: current sequence in the LOGO exchange
  1319. * @fp: LOGO request frame
  1320. *
  1321. * Locking Note: The rport lock is exected to be held before calling
  1322. * this function.
  1323. */
  1324. static void fc_rport_recv_logo_req(struct fc_lport *lport,
  1325. struct fc_seq *sp,
  1326. struct fc_frame *fp)
  1327. {
  1328. struct fc_frame_header *fh;
  1329. struct fc_rport_priv *rdata;
  1330. u32 sid;
  1331. lport->tt.seq_els_rsp_send(sp, ELS_LS_ACC, NULL);
  1332. fh = fc_frame_header_get(fp);
  1333. sid = ntoh24(fh->fh_s_id);
  1334. mutex_lock(&lport->disc.disc_mutex);
  1335. rdata = lport->tt.rport_lookup(lport, sid);
  1336. if (rdata) {
  1337. mutex_lock(&rdata->rp_mutex);
  1338. FC_RPORT_DBG(rdata, "Received LOGO request while in state %s\n",
  1339. fc_rport_state(rdata));
  1340. /*
  1341. * If the remote port was created due to discovery,
  1342. * log back in. It may have seen a stale RSCN about us.
  1343. */
  1344. if (rdata->rp_state != RPORT_ST_DELETE && rdata->disc_id)
  1345. fc_rport_enter_plogi(rdata);
  1346. else
  1347. fc_rport_enter_delete(rdata, RPORT_EV_LOGO);
  1348. mutex_unlock(&rdata->rp_mutex);
  1349. } else
  1350. FC_RPORT_ID_DBG(lport, sid,
  1351. "Received LOGO from non-logged-in port\n");
  1352. mutex_unlock(&lport->disc.disc_mutex);
  1353. fc_frame_free(fp);
  1354. }
  1355. static void fc_rport_flush_queue(void)
  1356. {
  1357. flush_workqueue(rport_event_queue);
  1358. }
  1359. int fc_rport_init(struct fc_lport *lport)
  1360. {
  1361. if (!lport->tt.rport_lookup)
  1362. lport->tt.rport_lookup = fc_rport_lookup;
  1363. if (!lport->tt.rport_create)
  1364. lport->tt.rport_create = fc_rport_create;
  1365. if (!lport->tt.rport_login)
  1366. lport->tt.rport_login = fc_rport_login;
  1367. if (!lport->tt.rport_logoff)
  1368. lport->tt.rport_logoff = fc_rport_logoff;
  1369. if (!lport->tt.rport_recv_req)
  1370. lport->tt.rport_recv_req = fc_rport_recv_req;
  1371. if (!lport->tt.rport_flush_queue)
  1372. lport->tt.rport_flush_queue = fc_rport_flush_queue;
  1373. if (!lport->tt.rport_destroy)
  1374. lport->tt.rport_destroy = fc_rport_destroy;
  1375. return 0;
  1376. }
  1377. EXPORT_SYMBOL(fc_rport_init);
  1378. int fc_setup_rport(void)
  1379. {
  1380. rport_event_queue = create_singlethread_workqueue("fc_rport_eq");
  1381. if (!rport_event_queue)
  1382. return -ENOMEM;
  1383. return 0;
  1384. }
  1385. EXPORT_SYMBOL(fc_setup_rport);
  1386. void fc_destroy_rport(void)
  1387. {
  1388. destroy_workqueue(rport_event_queue);
  1389. }
  1390. EXPORT_SYMBOL(fc_destroy_rport);
  1391. void fc_rport_terminate_io(struct fc_rport *rport)
  1392. {
  1393. struct fc_rport_libfc_priv *rp = rport->dd_data;
  1394. struct fc_lport *lport = rp->local_port;
  1395. lport->tt.exch_mgr_reset(lport, 0, rport->port_id);
  1396. lport->tt.exch_mgr_reset(lport, rport->port_id, 0);
  1397. }
  1398. EXPORT_SYMBOL(fc_rport_terminate_io);