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