fc_rport.c 35 KB

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