fc_rport.c 36 KB

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