fc_rport.c 36 KB

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