fc_rport.c 36 KB

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