fc_rport.c 35 KB

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