fc_rport.c 35 KB

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