fc_rport.c 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919
  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 hierarchy.
  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/slab.h>
  48. #include <linux/rcupdate.h>
  49. #include <linux/timer.h>
  50. #include <linux/workqueue.h>
  51. #include <asm/unaligned.h>
  52. #include <scsi/libfc.h>
  53. #include <scsi/fc_encode.h>
  54. #include "fc_libfc.h"
  55. struct workqueue_struct *rport_event_queue;
  56. static void fc_rport_enter_flogi(struct fc_rport_priv *);
  57. static void fc_rport_enter_plogi(struct fc_rport_priv *);
  58. static void fc_rport_enter_prli(struct fc_rport_priv *);
  59. static void fc_rport_enter_rtv(struct fc_rport_priv *);
  60. static void fc_rport_enter_ready(struct fc_rport_priv *);
  61. static void fc_rport_enter_logo(struct fc_rport_priv *);
  62. static void fc_rport_enter_adisc(struct fc_rport_priv *);
  63. static void fc_rport_recv_plogi_req(struct fc_lport *, struct fc_frame *);
  64. static void fc_rport_recv_prli_req(struct fc_rport_priv *, struct fc_frame *);
  65. static void fc_rport_recv_prlo_req(struct fc_rport_priv *, struct fc_frame *);
  66. static void fc_rport_recv_logo_req(struct fc_lport *, 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_FLOGI] = "FLOGI",
  74. [RPORT_ST_PLOGI_WAIT] = "PLOGI_WAIT",
  75. [RPORT_ST_PLOGI] = "PLOGI",
  76. [RPORT_ST_PRLI] = "PRLI",
  77. [RPORT_ST_RTV] = "RTV",
  78. [RPORT_ST_READY] = "Ready",
  79. [RPORT_ST_ADISC] = "ADISC",
  80. [RPORT_ST_DELETE] = "Delete",
  81. };
  82. /**
  83. * fc_rport_lookup() - Lookup a remote port by port_id
  84. * @lport: The local port to lookup the remote port on
  85. * @port_id: The remote port ID to look up
  86. *
  87. * The caller must hold either disc_mutex or rcu_read_lock().
  88. */
  89. static struct fc_rport_priv *fc_rport_lookup(const struct fc_lport *lport,
  90. u32 port_id)
  91. {
  92. struct fc_rport_priv *rdata;
  93. list_for_each_entry_rcu(rdata, &lport->disc.rports, peers)
  94. if (rdata->ids.port_id == port_id)
  95. return rdata;
  96. return NULL;
  97. }
  98. /**
  99. * fc_rport_create() - Create a new remote port
  100. * @lport: The local port this remote port will be associated with
  101. * @ids: The identifiers for the new remote port
  102. *
  103. * The remote port will start in the INIT state.
  104. *
  105. * Locking note: must be called with the disc_mutex held.
  106. */
  107. static struct fc_rport_priv *fc_rport_create(struct fc_lport *lport,
  108. u32 port_id)
  109. {
  110. struct fc_rport_priv *rdata;
  111. rdata = lport->tt.rport_lookup(lport, port_id);
  112. if (rdata)
  113. return rdata;
  114. rdata = kzalloc(sizeof(*rdata) + lport->rport_priv_size, GFP_KERNEL);
  115. if (!rdata)
  116. return NULL;
  117. rdata->ids.node_name = -1;
  118. rdata->ids.port_name = -1;
  119. rdata->ids.port_id = port_id;
  120. rdata->ids.roles = FC_RPORT_ROLE_UNKNOWN;
  121. kref_init(&rdata->kref);
  122. mutex_init(&rdata->rp_mutex);
  123. rdata->local_port = lport;
  124. rdata->rp_state = RPORT_ST_INIT;
  125. rdata->event = RPORT_EV_NONE;
  126. rdata->flags = FC_RP_FLAGS_REC_SUPPORTED;
  127. rdata->e_d_tov = lport->e_d_tov;
  128. rdata->r_a_tov = lport->r_a_tov;
  129. rdata->maxframe_size = FC_MIN_MAX_PAYLOAD;
  130. INIT_DELAYED_WORK(&rdata->retry_work, fc_rport_timeout);
  131. INIT_WORK(&rdata->event_work, fc_rport_work);
  132. if (port_id != FC_FID_DIR_SERV)
  133. list_add_rcu(&rdata->peers, &lport->disc.rports);
  134. return rdata;
  135. }
  136. /**
  137. * fc_rport_free_rcu() - Free a remote port
  138. * @rcu: The rcu_head structure inside the remote port
  139. */
  140. static void fc_rport_free_rcu(struct rcu_head *rcu)
  141. {
  142. struct fc_rport_priv *rdata;
  143. rdata = container_of(rcu, struct fc_rport_priv, rcu);
  144. kfree(rdata);
  145. }
  146. /**
  147. * fc_rport_destroy() - Free a remote port after last reference is released
  148. * @kref: The remote port's kref
  149. */
  150. static void fc_rport_destroy(struct kref *kref)
  151. {
  152. struct fc_rport_priv *rdata;
  153. rdata = container_of(kref, struct fc_rport_priv, kref);
  154. call_rcu(&rdata->rcu, fc_rport_free_rcu);
  155. }
  156. /**
  157. * fc_rport_state() - Return a string identifying the remote port's state
  158. * @rdata: The remote port
  159. */
  160. static const char *fc_rport_state(struct fc_rport_priv *rdata)
  161. {
  162. const char *cp;
  163. cp = fc_rport_state_names[rdata->rp_state];
  164. if (!cp)
  165. cp = "Unknown";
  166. return cp;
  167. }
  168. /**
  169. * fc_set_rport_loss_tmo() - Set the remote port loss timeout
  170. * @rport: The remote port that gets a new timeout value
  171. * @timeout: The new timeout value (in seconds)
  172. */
  173. void fc_set_rport_loss_tmo(struct fc_rport *rport, u32 timeout)
  174. {
  175. if (timeout)
  176. rport->dev_loss_tmo = timeout + 5;
  177. else
  178. rport->dev_loss_tmo = 30;
  179. }
  180. EXPORT_SYMBOL(fc_set_rport_loss_tmo);
  181. /**
  182. * fc_plogi_get_maxframe() - Get the maximum payload from the common service
  183. * parameters in a FLOGI frame
  184. * @flp: The FLOGI or PLOGI payload
  185. * @maxval: The maximum frame size upper limit; this may be less than what
  186. * is in the service parameters
  187. */
  188. static unsigned int fc_plogi_get_maxframe(struct fc_els_flogi *flp,
  189. unsigned int maxval)
  190. {
  191. unsigned int mfs;
  192. /*
  193. * Get max payload from the common service parameters and the
  194. * class 3 receive data field size.
  195. */
  196. mfs = ntohs(flp->fl_csp.sp_bb_data) & FC_SP_BB_DATA_MASK;
  197. if (mfs >= FC_SP_MIN_MAX_PAYLOAD && mfs < maxval)
  198. maxval = mfs;
  199. mfs = ntohs(flp->fl_cssp[3 - 1].cp_rdfs);
  200. if (mfs >= FC_SP_MIN_MAX_PAYLOAD && mfs < maxval)
  201. maxval = mfs;
  202. return maxval;
  203. }
  204. /**
  205. * fc_rport_state_enter() - Change the state of a remote port
  206. * @rdata: The remote port whose state should change
  207. * @new: The new state
  208. *
  209. * Locking Note: Called with the rport lock held
  210. */
  211. static void fc_rport_state_enter(struct fc_rport_priv *rdata,
  212. enum fc_rport_state new)
  213. {
  214. if (rdata->rp_state != new)
  215. rdata->retries = 0;
  216. rdata->rp_state = new;
  217. }
  218. /**
  219. * fc_rport_work() - Handler for remote port events in the rport_event_queue
  220. * @work: Handle to the remote port being dequeued
  221. */
  222. static void fc_rport_work(struct work_struct *work)
  223. {
  224. u32 port_id;
  225. struct fc_rport_priv *rdata =
  226. container_of(work, struct fc_rport_priv, event_work);
  227. struct fc_rport_libfc_priv *rpriv;
  228. enum fc_rport_event event;
  229. struct fc_lport *lport = rdata->local_port;
  230. struct fc_rport_operations *rport_ops;
  231. struct fc_rport_identifiers ids;
  232. struct fc_rport *rport;
  233. mutex_lock(&rdata->rp_mutex);
  234. event = rdata->event;
  235. rport_ops = rdata->ops;
  236. rport = rdata->rport;
  237. FC_RPORT_DBG(rdata, "work event %u\n", event);
  238. switch (event) {
  239. case RPORT_EV_READY:
  240. ids = rdata->ids;
  241. rdata->event = RPORT_EV_NONE;
  242. rdata->major_retries = 0;
  243. kref_get(&rdata->kref);
  244. mutex_unlock(&rdata->rp_mutex);
  245. if (!rport)
  246. rport = fc_remote_port_add(lport->host, 0, &ids);
  247. if (!rport) {
  248. FC_RPORT_DBG(rdata, "Failed to add the rport\n");
  249. lport->tt.rport_logoff(rdata);
  250. kref_put(&rdata->kref, lport->tt.rport_destroy);
  251. return;
  252. }
  253. mutex_lock(&rdata->rp_mutex);
  254. if (rdata->rport)
  255. FC_RPORT_DBG(rdata, "rport already allocated\n");
  256. rdata->rport = rport;
  257. rport->maxframe_size = rdata->maxframe_size;
  258. rport->supported_classes = rdata->supported_classes;
  259. rpriv = rport->dd_data;
  260. rpriv->local_port = lport;
  261. rpriv->rp_state = rdata->rp_state;
  262. rpriv->flags = rdata->flags;
  263. rpriv->e_d_tov = rdata->e_d_tov;
  264. rpriv->r_a_tov = rdata->r_a_tov;
  265. mutex_unlock(&rdata->rp_mutex);
  266. if (rport_ops && rport_ops->event_callback) {
  267. FC_RPORT_DBG(rdata, "callback ev %d\n", event);
  268. rport_ops->event_callback(lport, rdata, event);
  269. }
  270. kref_put(&rdata->kref, lport->tt.rport_destroy);
  271. break;
  272. case RPORT_EV_FAILED:
  273. case RPORT_EV_LOGO:
  274. case RPORT_EV_STOP:
  275. port_id = rdata->ids.port_id;
  276. mutex_unlock(&rdata->rp_mutex);
  277. if (rport_ops && rport_ops->event_callback) {
  278. FC_RPORT_DBG(rdata, "callback ev %d\n", event);
  279. rport_ops->event_callback(lport, rdata, event);
  280. }
  281. cancel_delayed_work_sync(&rdata->retry_work);
  282. /*
  283. * Reset any outstanding exchanges before freeing rport.
  284. */
  285. lport->tt.exch_mgr_reset(lport, 0, port_id);
  286. lport->tt.exch_mgr_reset(lport, port_id, 0);
  287. if (rport) {
  288. rpriv = rport->dd_data;
  289. rpriv->rp_state = RPORT_ST_DELETE;
  290. mutex_lock(&rdata->rp_mutex);
  291. rdata->rport = NULL;
  292. mutex_unlock(&rdata->rp_mutex);
  293. fc_remote_port_delete(rport);
  294. }
  295. mutex_lock(&lport->disc.disc_mutex);
  296. mutex_lock(&rdata->rp_mutex);
  297. if (rdata->rp_state == RPORT_ST_DELETE) {
  298. if (port_id == FC_FID_DIR_SERV) {
  299. rdata->event = RPORT_EV_NONE;
  300. mutex_unlock(&rdata->rp_mutex);
  301. } else if ((rdata->flags & FC_RP_STARTED) &&
  302. rdata->major_retries <
  303. lport->max_rport_retry_count) {
  304. rdata->major_retries++;
  305. rdata->event = RPORT_EV_NONE;
  306. FC_RPORT_DBG(rdata, "work restart\n");
  307. fc_rport_enter_flogi(rdata);
  308. mutex_unlock(&rdata->rp_mutex);
  309. } else {
  310. FC_RPORT_DBG(rdata, "work delete\n");
  311. list_del_rcu(&rdata->peers);
  312. mutex_unlock(&rdata->rp_mutex);
  313. kref_put(&rdata->kref, lport->tt.rport_destroy);
  314. }
  315. } else {
  316. /*
  317. * Re-open for events. Reissue READY event if ready.
  318. */
  319. rdata->event = RPORT_EV_NONE;
  320. if (rdata->rp_state == RPORT_ST_READY)
  321. fc_rport_enter_ready(rdata);
  322. mutex_unlock(&rdata->rp_mutex);
  323. }
  324. mutex_unlock(&lport->disc.disc_mutex);
  325. break;
  326. default:
  327. mutex_unlock(&rdata->rp_mutex);
  328. break;
  329. }
  330. }
  331. /**
  332. * fc_rport_login() - Start the remote port login state machine
  333. * @rdata: The remote port to be logged in to
  334. *
  335. * Locking Note: Called without the rport lock held. This
  336. * function will hold the rport lock, call an _enter_*
  337. * function and then unlock the rport.
  338. *
  339. * This indicates the intent to be logged into the remote port.
  340. * If it appears we are already logged in, ADISC is used to verify
  341. * the setup.
  342. */
  343. int fc_rport_login(struct fc_rport_priv *rdata)
  344. {
  345. mutex_lock(&rdata->rp_mutex);
  346. rdata->flags |= FC_RP_STARTED;
  347. switch (rdata->rp_state) {
  348. case RPORT_ST_READY:
  349. FC_RPORT_DBG(rdata, "ADISC port\n");
  350. fc_rport_enter_adisc(rdata);
  351. break;
  352. case RPORT_ST_DELETE:
  353. FC_RPORT_DBG(rdata, "Restart deleted port\n");
  354. break;
  355. default:
  356. FC_RPORT_DBG(rdata, "Login to port\n");
  357. fc_rport_enter_flogi(rdata);
  358. break;
  359. }
  360. mutex_unlock(&rdata->rp_mutex);
  361. return 0;
  362. }
  363. /**
  364. * fc_rport_enter_delete() - Schedule a remote port to be deleted
  365. * @rdata: The remote port to be deleted
  366. * @event: The event to report as the reason for deletion
  367. *
  368. * Locking Note: Called with the rport lock held.
  369. *
  370. * Allow state change into DELETE only once.
  371. *
  372. * Call queue_work only if there's no event already pending.
  373. * Set the new event so that the old pending event will not occur.
  374. * Since we have the mutex, even if fc_rport_work() is already started,
  375. * it'll see the new event.
  376. */
  377. static void fc_rport_enter_delete(struct fc_rport_priv *rdata,
  378. enum fc_rport_event event)
  379. {
  380. if (rdata->rp_state == RPORT_ST_DELETE)
  381. return;
  382. FC_RPORT_DBG(rdata, "Delete port\n");
  383. fc_rport_state_enter(rdata, RPORT_ST_DELETE);
  384. if (rdata->event == RPORT_EV_NONE)
  385. queue_work(rport_event_queue, &rdata->event_work);
  386. rdata->event = event;
  387. }
  388. /**
  389. * fc_rport_logoff() - Logoff and remove a remote port
  390. * @rdata: The remote port to be logged off of
  391. *
  392. * Locking Note: Called without the rport lock held. This
  393. * function will hold the rport lock, call an _enter_*
  394. * function and then unlock the rport.
  395. */
  396. int fc_rport_logoff(struct fc_rport_priv *rdata)
  397. {
  398. mutex_lock(&rdata->rp_mutex);
  399. FC_RPORT_DBG(rdata, "Remove port\n");
  400. rdata->flags &= ~FC_RP_STARTED;
  401. if (rdata->rp_state == RPORT_ST_DELETE) {
  402. FC_RPORT_DBG(rdata, "Port in Delete state, not removing\n");
  403. goto out;
  404. }
  405. fc_rport_enter_logo(rdata);
  406. /*
  407. * Change the state to Delete so that we discard
  408. * the response.
  409. */
  410. fc_rport_enter_delete(rdata, RPORT_EV_STOP);
  411. out:
  412. mutex_unlock(&rdata->rp_mutex);
  413. return 0;
  414. }
  415. /**
  416. * fc_rport_enter_ready() - Transition to the RPORT_ST_READY state
  417. * @rdata: The remote port that is ready
  418. *
  419. * Locking Note: The rport lock is expected to be held before calling
  420. * this routine.
  421. */
  422. static void fc_rport_enter_ready(struct fc_rport_priv *rdata)
  423. {
  424. fc_rport_state_enter(rdata, RPORT_ST_READY);
  425. FC_RPORT_DBG(rdata, "Port is Ready\n");
  426. if (rdata->event == RPORT_EV_NONE)
  427. queue_work(rport_event_queue, &rdata->event_work);
  428. rdata->event = RPORT_EV_READY;
  429. }
  430. /**
  431. * fc_rport_timeout() - Handler for the retry_work timer
  432. * @work: Handle to the remote port that has timed out
  433. *
  434. * Locking Note: Called without the rport lock held. This
  435. * function will hold the rport lock, call an _enter_*
  436. * function and then unlock the rport.
  437. */
  438. static void fc_rport_timeout(struct work_struct *work)
  439. {
  440. struct fc_rport_priv *rdata =
  441. container_of(work, struct fc_rport_priv, retry_work.work);
  442. mutex_lock(&rdata->rp_mutex);
  443. switch (rdata->rp_state) {
  444. case RPORT_ST_FLOGI:
  445. fc_rport_enter_flogi(rdata);
  446. break;
  447. case RPORT_ST_PLOGI:
  448. fc_rport_enter_plogi(rdata);
  449. break;
  450. case RPORT_ST_PRLI:
  451. fc_rport_enter_prli(rdata);
  452. break;
  453. case RPORT_ST_RTV:
  454. fc_rport_enter_rtv(rdata);
  455. break;
  456. case RPORT_ST_ADISC:
  457. fc_rport_enter_adisc(rdata);
  458. break;
  459. case RPORT_ST_PLOGI_WAIT:
  460. case RPORT_ST_READY:
  461. case RPORT_ST_INIT:
  462. case RPORT_ST_DELETE:
  463. break;
  464. }
  465. mutex_unlock(&rdata->rp_mutex);
  466. }
  467. /**
  468. * fc_rport_error() - Error handler, called once retries have been exhausted
  469. * @rdata: The remote port the error is happened on
  470. * @fp: The error code encapsulated in a frame pointer
  471. *
  472. * Locking Note: The rport lock is expected to be held before
  473. * calling this routine
  474. */
  475. static void fc_rport_error(struct fc_rport_priv *rdata, struct fc_frame *fp)
  476. {
  477. FC_RPORT_DBG(rdata, "Error %ld in state %s, retries %d\n",
  478. IS_ERR(fp) ? -PTR_ERR(fp) : 0,
  479. fc_rport_state(rdata), rdata->retries);
  480. switch (rdata->rp_state) {
  481. case RPORT_ST_FLOGI:
  482. case RPORT_ST_PLOGI:
  483. rdata->flags &= ~FC_RP_STARTED;
  484. fc_rport_enter_delete(rdata, RPORT_EV_FAILED);
  485. break;
  486. case RPORT_ST_RTV:
  487. fc_rport_enter_ready(rdata);
  488. break;
  489. case RPORT_ST_PRLI:
  490. case RPORT_ST_ADISC:
  491. fc_rport_enter_logo(rdata);
  492. break;
  493. case RPORT_ST_PLOGI_WAIT:
  494. case RPORT_ST_DELETE:
  495. case RPORT_ST_READY:
  496. case RPORT_ST_INIT:
  497. break;
  498. }
  499. }
  500. /**
  501. * fc_rport_error_retry() - Handler for remote port state retries
  502. * @rdata: The remote port whose state is to be retried
  503. * @fp: The error code encapsulated in a frame pointer
  504. *
  505. * If the error was an exchange timeout retry immediately,
  506. * otherwise wait for E_D_TOV.
  507. *
  508. * Locking Note: The rport lock is expected to be held before
  509. * calling this routine
  510. */
  511. static void fc_rport_error_retry(struct fc_rport_priv *rdata,
  512. struct fc_frame *fp)
  513. {
  514. unsigned long delay = FC_DEF_E_D_TOV;
  515. /* make sure this isn't an FC_EX_CLOSED error, never retry those */
  516. if (PTR_ERR(fp) == -FC_EX_CLOSED)
  517. return fc_rport_error(rdata, fp);
  518. if (rdata->retries < rdata->local_port->max_rport_retry_count) {
  519. FC_RPORT_DBG(rdata, "Error %ld in state %s, retrying\n",
  520. PTR_ERR(fp), fc_rport_state(rdata));
  521. rdata->retries++;
  522. /* no additional delay on exchange timeouts */
  523. if (PTR_ERR(fp) == -FC_EX_TIMEOUT)
  524. delay = 0;
  525. schedule_delayed_work(&rdata->retry_work, delay);
  526. return;
  527. }
  528. return fc_rport_error(rdata, fp);
  529. }
  530. /**
  531. * fc_rport_login_complete() - Handle parameters and completion of p-mp login.
  532. * @rdata: The remote port which we logged into or which logged into us.
  533. * @fp: The FLOGI or PLOGI request or response frame
  534. *
  535. * Returns non-zero error if a problem is detected with the frame.
  536. * Does not free the frame.
  537. *
  538. * This is only used in point-to-multipoint mode for FIP currently.
  539. */
  540. static int fc_rport_login_complete(struct fc_rport_priv *rdata,
  541. struct fc_frame *fp)
  542. {
  543. struct fc_lport *lport = rdata->local_port;
  544. struct fc_els_flogi *flogi;
  545. unsigned int e_d_tov;
  546. u16 csp_flags;
  547. flogi = fc_frame_payload_get(fp, sizeof(*flogi));
  548. if (!flogi)
  549. return -EINVAL;
  550. csp_flags = ntohs(flogi->fl_csp.sp_features);
  551. if (fc_frame_payload_op(fp) == ELS_FLOGI) {
  552. if (csp_flags & FC_SP_FT_FPORT) {
  553. FC_RPORT_DBG(rdata, "Fabric bit set in FLOGI\n");
  554. return -EINVAL;
  555. }
  556. } else {
  557. /*
  558. * E_D_TOV is not valid on an incoming FLOGI request.
  559. */
  560. e_d_tov = ntohl(flogi->fl_csp.sp_e_d_tov);
  561. if (csp_flags & FC_SP_FT_EDTR)
  562. e_d_tov /= 1000000;
  563. if (e_d_tov > rdata->e_d_tov)
  564. rdata->e_d_tov = e_d_tov;
  565. }
  566. rdata->maxframe_size = fc_plogi_get_maxframe(flogi, lport->mfs);
  567. return 0;
  568. }
  569. /**
  570. * fc_rport_flogi_resp() - Handle response to FLOGI request for p-mp mode
  571. * @sp: The sequence that the FLOGI was on
  572. * @fp: The FLOGI response frame
  573. * @rp_arg: The remote port that received the FLOGI response
  574. */
  575. void fc_rport_flogi_resp(struct fc_seq *sp, struct fc_frame *fp,
  576. void *rp_arg)
  577. {
  578. struct fc_rport_priv *rdata = rp_arg;
  579. struct fc_lport *lport = rdata->local_port;
  580. struct fc_els_flogi *flogi;
  581. unsigned int r_a_tov;
  582. FC_RPORT_DBG(rdata, "Received a FLOGI %s\n", fc_els_resp_type(fp));
  583. if (fp == ERR_PTR(-FC_EX_CLOSED))
  584. return;
  585. mutex_lock(&rdata->rp_mutex);
  586. if (rdata->rp_state != RPORT_ST_FLOGI) {
  587. FC_RPORT_DBG(rdata, "Received a FLOGI response, but in state "
  588. "%s\n", fc_rport_state(rdata));
  589. if (IS_ERR(fp))
  590. goto err;
  591. goto out;
  592. }
  593. if (IS_ERR(fp)) {
  594. fc_rport_error(rdata, fp);
  595. goto err;
  596. }
  597. if (fc_frame_payload_op(fp) != ELS_LS_ACC)
  598. goto bad;
  599. if (fc_rport_login_complete(rdata, fp))
  600. goto bad;
  601. flogi = fc_frame_payload_get(fp, sizeof(*flogi));
  602. if (!flogi)
  603. goto bad;
  604. r_a_tov = ntohl(flogi->fl_csp.sp_r_a_tov);
  605. if (r_a_tov > rdata->r_a_tov)
  606. rdata->r_a_tov = r_a_tov;
  607. if (rdata->ids.port_name < lport->wwpn)
  608. fc_rport_enter_plogi(rdata);
  609. else
  610. fc_rport_state_enter(rdata, RPORT_ST_PLOGI_WAIT);
  611. out:
  612. fc_frame_free(fp);
  613. err:
  614. mutex_unlock(&rdata->rp_mutex);
  615. kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
  616. return;
  617. bad:
  618. FC_RPORT_DBG(rdata, "Bad FLOGI response\n");
  619. fc_rport_error_retry(rdata, fp);
  620. goto out;
  621. }
  622. /**
  623. * fc_rport_enter_flogi() - Send a FLOGI request to the remote port for p-mp
  624. * @rdata: The remote port to send a FLOGI to
  625. *
  626. * Locking Note: The rport lock is expected to be held before calling
  627. * this routine.
  628. */
  629. static void fc_rport_enter_flogi(struct fc_rport_priv *rdata)
  630. {
  631. struct fc_lport *lport = rdata->local_port;
  632. struct fc_frame *fp;
  633. if (!lport->point_to_multipoint)
  634. return fc_rport_enter_plogi(rdata);
  635. FC_RPORT_DBG(rdata, "Entered FLOGI state from %s state\n",
  636. fc_rport_state(rdata));
  637. fc_rport_state_enter(rdata, RPORT_ST_FLOGI);
  638. fp = fc_frame_alloc(lport, sizeof(struct fc_els_flogi));
  639. if (!fp)
  640. return fc_rport_error_retry(rdata, fp);
  641. if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_FLOGI,
  642. fc_rport_flogi_resp, rdata,
  643. 2 * lport->r_a_tov))
  644. fc_rport_error_retry(rdata, NULL);
  645. else
  646. kref_get(&rdata->kref);
  647. }
  648. /**
  649. * fc_rport_recv_flogi_req() - Handle Fabric Login (FLOGI) request in p-mp mode
  650. * @lport: The local port that received the PLOGI request
  651. * @rx_fp: The PLOGI request frame
  652. */
  653. static void fc_rport_recv_flogi_req(struct fc_lport *lport,
  654. struct fc_frame *rx_fp)
  655. {
  656. struct fc_disc *disc;
  657. struct fc_els_flogi *flp;
  658. struct fc_rport_priv *rdata;
  659. struct fc_frame *fp = rx_fp;
  660. struct fc_seq_els_data rjt_data;
  661. u32 sid;
  662. sid = fc_frame_sid(fp);
  663. FC_RPORT_ID_DBG(lport, sid, "Received FLOGI request\n");
  664. disc = &lport->disc;
  665. mutex_lock(&disc->disc_mutex);
  666. if (!lport->point_to_multipoint) {
  667. rjt_data.reason = ELS_RJT_UNSUP;
  668. rjt_data.explan = ELS_EXPL_NONE;
  669. goto reject;
  670. }
  671. flp = fc_frame_payload_get(fp, sizeof(*flp));
  672. if (!flp) {
  673. rjt_data.reason = ELS_RJT_LOGIC;
  674. rjt_data.explan = ELS_EXPL_INV_LEN;
  675. goto reject;
  676. }
  677. rdata = lport->tt.rport_lookup(lport, sid);
  678. if (!rdata) {
  679. rjt_data.reason = ELS_RJT_FIP;
  680. rjt_data.explan = ELS_EXPL_NOT_NEIGHBOR;
  681. goto reject;
  682. }
  683. mutex_lock(&rdata->rp_mutex);
  684. FC_RPORT_DBG(rdata, "Received FLOGI in %s state\n",
  685. fc_rport_state(rdata));
  686. switch (rdata->rp_state) {
  687. case RPORT_ST_INIT:
  688. case RPORT_ST_DELETE:
  689. mutex_unlock(&rdata->rp_mutex);
  690. rjt_data.reason = ELS_RJT_FIP;
  691. rjt_data.explan = ELS_EXPL_NOT_NEIGHBOR;
  692. goto reject;
  693. case RPORT_ST_FLOGI:
  694. case RPORT_ST_PLOGI_WAIT:
  695. case RPORT_ST_PLOGI:
  696. break;
  697. case RPORT_ST_PRLI:
  698. case RPORT_ST_RTV:
  699. case RPORT_ST_READY:
  700. case RPORT_ST_ADISC:
  701. /*
  702. * Set the remote port to be deleted and to then restart.
  703. * This queues work to be sure exchanges are reset.
  704. */
  705. fc_rport_enter_delete(rdata, RPORT_EV_LOGO);
  706. mutex_unlock(&rdata->rp_mutex);
  707. rjt_data.reason = ELS_RJT_BUSY;
  708. rjt_data.explan = ELS_EXPL_NONE;
  709. goto reject;
  710. }
  711. if (fc_rport_login_complete(rdata, fp)) {
  712. mutex_unlock(&rdata->rp_mutex);
  713. rjt_data.reason = ELS_RJT_LOGIC;
  714. rjt_data.explan = ELS_EXPL_NONE;
  715. goto reject;
  716. }
  717. fp = fc_frame_alloc(lport, sizeof(*flp));
  718. if (!fp)
  719. goto out;
  720. fc_flogi_fill(lport, fp);
  721. flp = fc_frame_payload_get(fp, sizeof(*flp));
  722. flp->fl_cmd = ELS_LS_ACC;
  723. fc_fill_reply_hdr(fp, rx_fp, FC_RCTL_ELS_REP, 0);
  724. lport->tt.frame_send(lport, fp);
  725. if (rdata->ids.port_name < lport->wwpn)
  726. fc_rport_enter_plogi(rdata);
  727. else
  728. fc_rport_state_enter(rdata, RPORT_ST_PLOGI_WAIT);
  729. out:
  730. mutex_unlock(&rdata->rp_mutex);
  731. mutex_unlock(&disc->disc_mutex);
  732. fc_frame_free(rx_fp);
  733. return;
  734. reject:
  735. mutex_unlock(&disc->disc_mutex);
  736. lport->tt.seq_els_rsp_send(rx_fp, ELS_LS_RJT, &rjt_data);
  737. fc_frame_free(rx_fp);
  738. }
  739. /**
  740. * fc_rport_plogi_resp() - Handler for ELS PLOGI responses
  741. * @sp: The sequence the PLOGI is on
  742. * @fp: The PLOGI response frame
  743. * @rdata_arg: The remote port that sent the PLOGI response
  744. *
  745. * Locking Note: This function will be called without the rport lock
  746. * held, but it will lock, call an _enter_* function or fc_rport_error
  747. * and then unlock the rport.
  748. */
  749. static void fc_rport_plogi_resp(struct fc_seq *sp, struct fc_frame *fp,
  750. void *rdata_arg)
  751. {
  752. struct fc_rport_priv *rdata = rdata_arg;
  753. struct fc_lport *lport = rdata->local_port;
  754. struct fc_els_flogi *plp = NULL;
  755. u16 csp_seq;
  756. u16 cssp_seq;
  757. u8 op;
  758. mutex_lock(&rdata->rp_mutex);
  759. FC_RPORT_DBG(rdata, "Received a PLOGI %s\n", fc_els_resp_type(fp));
  760. if (rdata->rp_state != RPORT_ST_PLOGI) {
  761. FC_RPORT_DBG(rdata, "Received a PLOGI response, but in state "
  762. "%s\n", fc_rport_state(rdata));
  763. if (IS_ERR(fp))
  764. goto err;
  765. goto out;
  766. }
  767. if (IS_ERR(fp)) {
  768. fc_rport_error_retry(rdata, fp);
  769. goto err;
  770. }
  771. op = fc_frame_payload_op(fp);
  772. if (op == ELS_LS_ACC &&
  773. (plp = fc_frame_payload_get(fp, sizeof(*plp))) != NULL) {
  774. rdata->ids.port_name = get_unaligned_be64(&plp->fl_wwpn);
  775. rdata->ids.node_name = get_unaligned_be64(&plp->fl_wwnn);
  776. if (lport->point_to_multipoint)
  777. fc_rport_login_complete(rdata, fp);
  778. csp_seq = ntohs(plp->fl_csp.sp_tot_seq);
  779. cssp_seq = ntohs(plp->fl_cssp[3 - 1].cp_con_seq);
  780. if (cssp_seq < csp_seq)
  781. csp_seq = cssp_seq;
  782. rdata->max_seq = csp_seq;
  783. rdata->maxframe_size = fc_plogi_get_maxframe(plp, lport->mfs);
  784. fc_rport_enter_prli(rdata);
  785. } else
  786. fc_rport_error_retry(rdata, fp);
  787. out:
  788. fc_frame_free(fp);
  789. err:
  790. mutex_unlock(&rdata->rp_mutex);
  791. kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
  792. }
  793. /**
  794. * fc_rport_enter_plogi() - Send Port Login (PLOGI) request
  795. * @rdata: The remote port to send a PLOGI to
  796. *
  797. * Locking Note: The rport lock is expected to be held before calling
  798. * this routine.
  799. */
  800. static void fc_rport_enter_plogi(struct fc_rport_priv *rdata)
  801. {
  802. struct fc_lport *lport = rdata->local_port;
  803. struct fc_frame *fp;
  804. FC_RPORT_DBG(rdata, "Port entered PLOGI state from %s state\n",
  805. fc_rport_state(rdata));
  806. fc_rport_state_enter(rdata, RPORT_ST_PLOGI);
  807. rdata->maxframe_size = FC_MIN_MAX_PAYLOAD;
  808. fp = fc_frame_alloc(lport, sizeof(struct fc_els_flogi));
  809. if (!fp) {
  810. FC_RPORT_DBG(rdata, "%s frame alloc failed\n", __func__);
  811. fc_rport_error_retry(rdata, fp);
  812. return;
  813. }
  814. rdata->e_d_tov = lport->e_d_tov;
  815. if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_PLOGI,
  816. fc_rport_plogi_resp, rdata,
  817. 2 * lport->r_a_tov))
  818. fc_rport_error_retry(rdata, NULL);
  819. else
  820. kref_get(&rdata->kref);
  821. }
  822. /**
  823. * fc_rport_prli_resp() - Process Login (PRLI) response handler
  824. * @sp: The sequence the PRLI response was on
  825. * @fp: The PRLI response frame
  826. * @rdata_arg: The remote port that sent the PRLI response
  827. *
  828. * Locking Note: This function will be called without the rport lock
  829. * held, but it will lock, call an _enter_* function or fc_rport_error
  830. * and then unlock the rport.
  831. */
  832. static void fc_rport_prli_resp(struct fc_seq *sp, struct fc_frame *fp,
  833. void *rdata_arg)
  834. {
  835. struct fc_rport_priv *rdata = rdata_arg;
  836. struct {
  837. struct fc_els_prli prli;
  838. struct fc_els_spp spp;
  839. } *pp;
  840. u32 roles = FC_RPORT_ROLE_UNKNOWN;
  841. u32 fcp_parm = 0;
  842. u8 op;
  843. u8 resp_code = 0;
  844. mutex_lock(&rdata->rp_mutex);
  845. FC_RPORT_DBG(rdata, "Received a PRLI %s\n", fc_els_resp_type(fp));
  846. if (rdata->rp_state != RPORT_ST_PRLI) {
  847. FC_RPORT_DBG(rdata, "Received a PRLI response, but in state "
  848. "%s\n", fc_rport_state(rdata));
  849. if (IS_ERR(fp))
  850. goto err;
  851. goto out;
  852. }
  853. if (IS_ERR(fp)) {
  854. fc_rport_error_retry(rdata, fp);
  855. goto err;
  856. }
  857. /* reinitialize remote port roles */
  858. rdata->ids.roles = FC_RPORT_ROLE_UNKNOWN;
  859. op = fc_frame_payload_op(fp);
  860. if (op == ELS_LS_ACC) {
  861. pp = fc_frame_payload_get(fp, sizeof(*pp));
  862. if (!pp)
  863. goto out;
  864. resp_code = (pp->spp.spp_flags & FC_SPP_RESP_MASK);
  865. FC_RPORT_DBG(rdata, "PRLI spp_flags = 0x%x\n",
  866. pp->spp.spp_flags);
  867. if (resp_code != FC_SPP_RESP_ACK) {
  868. if (resp_code == FC_SPP_RESP_CONF)
  869. fc_rport_error(rdata, fp);
  870. else
  871. fc_rport_error_retry(rdata, fp);
  872. goto out;
  873. }
  874. if (pp->prli.prli_spp_len < sizeof(pp->spp))
  875. goto out;
  876. fcp_parm = ntohl(pp->spp.spp_params);
  877. if (fcp_parm & FCP_SPPF_RETRY)
  878. rdata->flags |= FC_RP_FLAGS_RETRY;
  879. rdata->supported_classes = FC_COS_CLASS3;
  880. if (fcp_parm & FCP_SPPF_INIT_FCN)
  881. roles |= FC_RPORT_ROLE_FCP_INITIATOR;
  882. if (fcp_parm & FCP_SPPF_TARG_FCN)
  883. roles |= FC_RPORT_ROLE_FCP_TARGET;
  884. rdata->ids.roles = roles;
  885. fc_rport_enter_rtv(rdata);
  886. } else {
  887. FC_RPORT_DBG(rdata, "Bad ELS response for PRLI command\n");
  888. fc_rport_error_retry(rdata, fp);
  889. }
  890. out:
  891. fc_frame_free(fp);
  892. err:
  893. mutex_unlock(&rdata->rp_mutex);
  894. kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
  895. }
  896. /**
  897. * fc_rport_enter_prli() - Send Process Login (PRLI) request
  898. * @rdata: The remote port to send the PRLI request to
  899. *
  900. * Locking Note: The rport lock is expected to be held before calling
  901. * this routine.
  902. */
  903. static void fc_rport_enter_prli(struct fc_rport_priv *rdata)
  904. {
  905. struct fc_lport *lport = rdata->local_port;
  906. struct {
  907. struct fc_els_prli prli;
  908. struct fc_els_spp spp;
  909. } *pp;
  910. struct fc_frame *fp;
  911. /*
  912. * If the rport is one of the well known addresses
  913. * we skip PRLI and RTV and go straight to READY.
  914. */
  915. if (rdata->ids.port_id >= FC_FID_DOM_MGR) {
  916. fc_rport_enter_ready(rdata);
  917. return;
  918. }
  919. FC_RPORT_DBG(rdata, "Port entered PRLI state from %s state\n",
  920. fc_rport_state(rdata));
  921. fc_rport_state_enter(rdata, RPORT_ST_PRLI);
  922. fp = fc_frame_alloc(lport, sizeof(*pp));
  923. if (!fp) {
  924. fc_rport_error_retry(rdata, fp);
  925. return;
  926. }
  927. if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_PRLI,
  928. fc_rport_prli_resp, rdata,
  929. 2 * lport->r_a_tov))
  930. fc_rport_error_retry(rdata, NULL);
  931. else
  932. kref_get(&rdata->kref);
  933. }
  934. /**
  935. * fc_rport_els_rtv_resp() - Handler for Request Timeout Value (RTV) responses
  936. * @sp: The sequence the RTV was on
  937. * @fp: The RTV response frame
  938. * @rdata_arg: The remote port that sent the RTV response
  939. *
  940. * Many targets don't seem to support this.
  941. *
  942. * Locking Note: This function will be called without the rport lock
  943. * held, but it will lock, call an _enter_* function or fc_rport_error
  944. * and then unlock the rport.
  945. */
  946. static void fc_rport_rtv_resp(struct fc_seq *sp, struct fc_frame *fp,
  947. void *rdata_arg)
  948. {
  949. struct fc_rport_priv *rdata = rdata_arg;
  950. u8 op;
  951. mutex_lock(&rdata->rp_mutex);
  952. FC_RPORT_DBG(rdata, "Received a RTV %s\n", fc_els_resp_type(fp));
  953. if (rdata->rp_state != RPORT_ST_RTV) {
  954. FC_RPORT_DBG(rdata, "Received a RTV response, but in state "
  955. "%s\n", fc_rport_state(rdata));
  956. if (IS_ERR(fp))
  957. goto err;
  958. goto out;
  959. }
  960. if (IS_ERR(fp)) {
  961. fc_rport_error(rdata, fp);
  962. goto err;
  963. }
  964. op = fc_frame_payload_op(fp);
  965. if (op == ELS_LS_ACC) {
  966. struct fc_els_rtv_acc *rtv;
  967. u32 toq;
  968. u32 tov;
  969. rtv = fc_frame_payload_get(fp, sizeof(*rtv));
  970. if (rtv) {
  971. toq = ntohl(rtv->rtv_toq);
  972. tov = ntohl(rtv->rtv_r_a_tov);
  973. if (tov == 0)
  974. tov = 1;
  975. rdata->r_a_tov = tov;
  976. tov = ntohl(rtv->rtv_e_d_tov);
  977. if (toq & FC_ELS_RTV_EDRES)
  978. tov /= 1000000;
  979. if (tov == 0)
  980. tov = 1;
  981. rdata->e_d_tov = tov;
  982. }
  983. }
  984. fc_rport_enter_ready(rdata);
  985. out:
  986. fc_frame_free(fp);
  987. err:
  988. mutex_unlock(&rdata->rp_mutex);
  989. kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
  990. }
  991. /**
  992. * fc_rport_enter_rtv() - Send Request Timeout Value (RTV) request
  993. * @rdata: The remote port to send the RTV request to
  994. *
  995. * Locking Note: The rport lock is expected to be held before calling
  996. * this routine.
  997. */
  998. static void fc_rport_enter_rtv(struct fc_rport_priv *rdata)
  999. {
  1000. struct fc_frame *fp;
  1001. struct fc_lport *lport = rdata->local_port;
  1002. FC_RPORT_DBG(rdata, "Port entered RTV state from %s state\n",
  1003. fc_rport_state(rdata));
  1004. fc_rport_state_enter(rdata, RPORT_ST_RTV);
  1005. fp = fc_frame_alloc(lport, sizeof(struct fc_els_rtv));
  1006. if (!fp) {
  1007. fc_rport_error_retry(rdata, fp);
  1008. return;
  1009. }
  1010. if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_RTV,
  1011. fc_rport_rtv_resp, rdata,
  1012. 2 * lport->r_a_tov))
  1013. fc_rport_error_retry(rdata, NULL);
  1014. else
  1015. kref_get(&rdata->kref);
  1016. }
  1017. /**
  1018. * fc_rport_logo_resp() - Handler for logout (LOGO) responses
  1019. * @sp: The sequence the LOGO was on
  1020. * @fp: The LOGO response frame
  1021. * @lport_arg: The local port
  1022. */
  1023. static void fc_rport_logo_resp(struct fc_seq *sp, struct fc_frame *fp,
  1024. void *lport_arg)
  1025. {
  1026. struct fc_lport *lport = lport_arg;
  1027. FC_RPORT_ID_DBG(lport, fc_seq_exch(sp)->did,
  1028. "Received a LOGO %s\n", fc_els_resp_type(fp));
  1029. if (IS_ERR(fp))
  1030. return;
  1031. fc_frame_free(fp);
  1032. }
  1033. /**
  1034. * fc_rport_enter_logo() - Send a logout (LOGO) request
  1035. * @rdata: The remote port to send the LOGO request to
  1036. *
  1037. * Locking Note: The rport lock is expected to be held before calling
  1038. * this routine.
  1039. */
  1040. static void fc_rport_enter_logo(struct fc_rport_priv *rdata)
  1041. {
  1042. struct fc_lport *lport = rdata->local_port;
  1043. struct fc_frame *fp;
  1044. FC_RPORT_DBG(rdata, "Port sending LOGO from %s state\n",
  1045. fc_rport_state(rdata));
  1046. fp = fc_frame_alloc(lport, sizeof(struct fc_els_logo));
  1047. if (!fp)
  1048. return;
  1049. (void)lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_LOGO,
  1050. fc_rport_logo_resp, lport, 0);
  1051. }
  1052. /**
  1053. * fc_rport_els_adisc_resp() - Handler for Address Discovery (ADISC) responses
  1054. * @sp: The sequence the ADISC response was on
  1055. * @fp: The ADISC response frame
  1056. * @rdata_arg: The remote port that sent the ADISC response
  1057. *
  1058. * Locking Note: This function will be called without the rport lock
  1059. * held, but it will lock, call an _enter_* function or fc_rport_error
  1060. * and then unlock the rport.
  1061. */
  1062. static void fc_rport_adisc_resp(struct fc_seq *sp, struct fc_frame *fp,
  1063. void *rdata_arg)
  1064. {
  1065. struct fc_rport_priv *rdata = rdata_arg;
  1066. struct fc_els_adisc *adisc;
  1067. u8 op;
  1068. mutex_lock(&rdata->rp_mutex);
  1069. FC_RPORT_DBG(rdata, "Received a ADISC response\n");
  1070. if (rdata->rp_state != RPORT_ST_ADISC) {
  1071. FC_RPORT_DBG(rdata, "Received a ADISC resp but in state %s\n",
  1072. fc_rport_state(rdata));
  1073. if (IS_ERR(fp))
  1074. goto err;
  1075. goto out;
  1076. }
  1077. if (IS_ERR(fp)) {
  1078. fc_rport_error(rdata, fp);
  1079. goto err;
  1080. }
  1081. /*
  1082. * If address verification failed. Consider us logged out of the rport.
  1083. * Since the rport is still in discovery, we want to be
  1084. * logged in, so go to PLOGI state. Otherwise, go back to READY.
  1085. */
  1086. op = fc_frame_payload_op(fp);
  1087. adisc = fc_frame_payload_get(fp, sizeof(*adisc));
  1088. if (op != ELS_LS_ACC || !adisc ||
  1089. ntoh24(adisc->adisc_port_id) != rdata->ids.port_id ||
  1090. get_unaligned_be64(&adisc->adisc_wwpn) != rdata->ids.port_name ||
  1091. get_unaligned_be64(&adisc->adisc_wwnn) != rdata->ids.node_name) {
  1092. FC_RPORT_DBG(rdata, "ADISC error or mismatch\n");
  1093. fc_rport_enter_flogi(rdata);
  1094. } else {
  1095. FC_RPORT_DBG(rdata, "ADISC OK\n");
  1096. fc_rport_enter_ready(rdata);
  1097. }
  1098. out:
  1099. fc_frame_free(fp);
  1100. err:
  1101. mutex_unlock(&rdata->rp_mutex);
  1102. kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
  1103. }
  1104. /**
  1105. * fc_rport_enter_adisc() - Send Address Discover (ADISC) request
  1106. * @rdata: The remote port to send the ADISC request to
  1107. *
  1108. * Locking Note: The rport lock is expected to be held before calling
  1109. * this routine.
  1110. */
  1111. static void fc_rport_enter_adisc(struct fc_rport_priv *rdata)
  1112. {
  1113. struct fc_lport *lport = rdata->local_port;
  1114. struct fc_frame *fp;
  1115. FC_RPORT_DBG(rdata, "sending ADISC from %s state\n",
  1116. fc_rport_state(rdata));
  1117. fc_rport_state_enter(rdata, RPORT_ST_ADISC);
  1118. fp = fc_frame_alloc(lport, sizeof(struct fc_els_adisc));
  1119. if (!fp) {
  1120. fc_rport_error_retry(rdata, fp);
  1121. return;
  1122. }
  1123. if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_ADISC,
  1124. fc_rport_adisc_resp, rdata,
  1125. 2 * lport->r_a_tov))
  1126. fc_rport_error_retry(rdata, NULL);
  1127. else
  1128. kref_get(&rdata->kref);
  1129. }
  1130. /**
  1131. * fc_rport_recv_adisc_req() - Handler for Address Discovery (ADISC) requests
  1132. * @rdata: The remote port that sent the ADISC request
  1133. * @in_fp: The ADISC request frame
  1134. *
  1135. * Locking Note: Called with the lport and rport locks held.
  1136. */
  1137. static void fc_rport_recv_adisc_req(struct fc_rport_priv *rdata,
  1138. struct fc_frame *in_fp)
  1139. {
  1140. struct fc_lport *lport = rdata->local_port;
  1141. struct fc_frame *fp;
  1142. struct fc_els_adisc *adisc;
  1143. struct fc_seq_els_data rjt_data;
  1144. FC_RPORT_DBG(rdata, "Received ADISC request\n");
  1145. adisc = fc_frame_payload_get(in_fp, sizeof(*adisc));
  1146. if (!adisc) {
  1147. rjt_data.reason = ELS_RJT_PROT;
  1148. rjt_data.explan = ELS_EXPL_INV_LEN;
  1149. lport->tt.seq_els_rsp_send(in_fp, ELS_LS_RJT, &rjt_data);
  1150. goto drop;
  1151. }
  1152. fp = fc_frame_alloc(lport, sizeof(*adisc));
  1153. if (!fp)
  1154. goto drop;
  1155. fc_adisc_fill(lport, fp);
  1156. adisc = fc_frame_payload_get(fp, sizeof(*adisc));
  1157. adisc->adisc_cmd = ELS_LS_ACC;
  1158. fc_fill_reply_hdr(fp, in_fp, FC_RCTL_ELS_REP, 0);
  1159. lport->tt.frame_send(lport, fp);
  1160. drop:
  1161. fc_frame_free(in_fp);
  1162. }
  1163. /**
  1164. * fc_rport_recv_rls_req() - Handle received Read Link Status request
  1165. * @rdata: The remote port that sent the RLS request
  1166. * @rx_fp: The PRLI request frame
  1167. *
  1168. * Locking Note: The rport lock is expected to be held before calling
  1169. * this function.
  1170. */
  1171. static void fc_rport_recv_rls_req(struct fc_rport_priv *rdata,
  1172. struct fc_frame *rx_fp)
  1173. {
  1174. struct fc_lport *lport = rdata->local_port;
  1175. struct fc_frame *fp;
  1176. struct fc_els_rls *rls;
  1177. struct fc_els_rls_resp *rsp;
  1178. struct fc_els_lesb *lesb;
  1179. struct fc_seq_els_data rjt_data;
  1180. struct fc_host_statistics *hst;
  1181. FC_RPORT_DBG(rdata, "Received RLS request while in state %s\n",
  1182. fc_rport_state(rdata));
  1183. rls = fc_frame_payload_get(rx_fp, sizeof(*rls));
  1184. if (!rls) {
  1185. rjt_data.reason = ELS_RJT_PROT;
  1186. rjt_data.explan = ELS_EXPL_INV_LEN;
  1187. goto out_rjt;
  1188. }
  1189. fp = fc_frame_alloc(lport, sizeof(*rsp));
  1190. if (!fp) {
  1191. rjt_data.reason = ELS_RJT_UNAB;
  1192. rjt_data.explan = ELS_EXPL_INSUF_RES;
  1193. goto out_rjt;
  1194. }
  1195. rsp = fc_frame_payload_get(fp, sizeof(*rsp));
  1196. memset(rsp, 0, sizeof(*rsp));
  1197. rsp->rls_cmd = ELS_LS_ACC;
  1198. lesb = &rsp->rls_lesb;
  1199. if (lport->tt.get_lesb) {
  1200. /* get LESB from LLD if it supports it */
  1201. lport->tt.get_lesb(lport, lesb);
  1202. } else {
  1203. fc_get_host_stats(lport->host);
  1204. hst = &lport->host_stats;
  1205. lesb->lesb_link_fail = htonl(hst->link_failure_count);
  1206. lesb->lesb_sync_loss = htonl(hst->loss_of_sync_count);
  1207. lesb->lesb_sig_loss = htonl(hst->loss_of_signal_count);
  1208. lesb->lesb_prim_err = htonl(hst->prim_seq_protocol_err_count);
  1209. lesb->lesb_inv_word = htonl(hst->invalid_tx_word_count);
  1210. lesb->lesb_inv_crc = htonl(hst->invalid_crc_count);
  1211. }
  1212. fc_fill_reply_hdr(fp, rx_fp, FC_RCTL_ELS_REP, 0);
  1213. lport->tt.frame_send(lport, fp);
  1214. goto out;
  1215. out_rjt:
  1216. lport->tt.seq_els_rsp_send(rx_fp, ELS_LS_RJT, &rjt_data);
  1217. out:
  1218. fc_frame_free(rx_fp);
  1219. }
  1220. /**
  1221. * fc_rport_recv_els_req() - Handler for validated ELS requests
  1222. * @lport: The local port that received the ELS request
  1223. * @fp: The ELS request frame
  1224. *
  1225. * Handle incoming ELS requests that require port login.
  1226. * The ELS opcode has already been validated by the caller.
  1227. *
  1228. * Locking Note: Called with the lport lock held.
  1229. */
  1230. static void fc_rport_recv_els_req(struct fc_lport *lport, struct fc_frame *fp)
  1231. {
  1232. struct fc_rport_priv *rdata;
  1233. struct fc_seq_els_data els_data;
  1234. mutex_lock(&lport->disc.disc_mutex);
  1235. rdata = lport->tt.rport_lookup(lport, fc_frame_sid(fp));
  1236. if (!rdata) {
  1237. mutex_unlock(&lport->disc.disc_mutex);
  1238. goto reject;
  1239. }
  1240. mutex_lock(&rdata->rp_mutex);
  1241. mutex_unlock(&lport->disc.disc_mutex);
  1242. switch (rdata->rp_state) {
  1243. case RPORT_ST_PRLI:
  1244. case RPORT_ST_RTV:
  1245. case RPORT_ST_READY:
  1246. case RPORT_ST_ADISC:
  1247. break;
  1248. default:
  1249. mutex_unlock(&rdata->rp_mutex);
  1250. goto reject;
  1251. }
  1252. switch (fc_frame_payload_op(fp)) {
  1253. case ELS_PRLI:
  1254. fc_rport_recv_prli_req(rdata, fp);
  1255. break;
  1256. case ELS_PRLO:
  1257. fc_rport_recv_prlo_req(rdata, fp);
  1258. break;
  1259. case ELS_ADISC:
  1260. fc_rport_recv_adisc_req(rdata, fp);
  1261. break;
  1262. case ELS_RRQ:
  1263. lport->tt.seq_els_rsp_send(fp, ELS_RRQ, NULL);
  1264. fc_frame_free(fp);
  1265. break;
  1266. case ELS_REC:
  1267. lport->tt.seq_els_rsp_send(fp, ELS_REC, NULL);
  1268. fc_frame_free(fp);
  1269. break;
  1270. case ELS_RLS:
  1271. fc_rport_recv_rls_req(rdata, fp);
  1272. break;
  1273. default:
  1274. fc_frame_free(fp); /* can't happen */
  1275. break;
  1276. }
  1277. mutex_unlock(&rdata->rp_mutex);
  1278. return;
  1279. reject:
  1280. els_data.reason = ELS_RJT_UNAB;
  1281. els_data.explan = ELS_EXPL_PLOGI_REQD;
  1282. lport->tt.seq_els_rsp_send(fp, ELS_LS_RJT, &els_data);
  1283. fc_frame_free(fp);
  1284. }
  1285. /**
  1286. * fc_rport_recv_req() - Handler for requests
  1287. * @lport: The local port that received the request
  1288. * @fp: The request frame
  1289. *
  1290. * Locking Note: Called with the lport lock held.
  1291. */
  1292. void fc_rport_recv_req(struct fc_lport *lport, struct fc_frame *fp)
  1293. {
  1294. struct fc_seq_els_data els_data;
  1295. /*
  1296. * Handle FLOGI, PLOGI and LOGO requests separately, since they
  1297. * don't require prior login.
  1298. * Check for unsupported opcodes first and reject them.
  1299. * For some ops, it would be incorrect to reject with "PLOGI required".
  1300. */
  1301. switch (fc_frame_payload_op(fp)) {
  1302. case ELS_FLOGI:
  1303. fc_rport_recv_flogi_req(lport, fp);
  1304. break;
  1305. case ELS_PLOGI:
  1306. fc_rport_recv_plogi_req(lport, fp);
  1307. break;
  1308. case ELS_LOGO:
  1309. fc_rport_recv_logo_req(lport, fp);
  1310. break;
  1311. case ELS_PRLI:
  1312. case ELS_PRLO:
  1313. case ELS_ADISC:
  1314. case ELS_RRQ:
  1315. case ELS_REC:
  1316. case ELS_RLS:
  1317. fc_rport_recv_els_req(lport, fp);
  1318. break;
  1319. default:
  1320. els_data.reason = ELS_RJT_UNSUP;
  1321. els_data.explan = ELS_EXPL_NONE;
  1322. lport->tt.seq_els_rsp_send(fp, ELS_LS_RJT, &els_data);
  1323. fc_frame_free(fp);
  1324. break;
  1325. }
  1326. }
  1327. /**
  1328. * fc_rport_recv_plogi_req() - Handler for Port Login (PLOGI) requests
  1329. * @lport: The local port that received the PLOGI request
  1330. * @rx_fp: The PLOGI request frame
  1331. *
  1332. * Locking Note: The rport lock is held before calling this function.
  1333. */
  1334. static void fc_rport_recv_plogi_req(struct fc_lport *lport,
  1335. struct fc_frame *rx_fp)
  1336. {
  1337. struct fc_disc *disc;
  1338. struct fc_rport_priv *rdata;
  1339. struct fc_frame *fp = rx_fp;
  1340. struct fc_els_flogi *pl;
  1341. struct fc_seq_els_data rjt_data;
  1342. u32 sid;
  1343. sid = fc_frame_sid(fp);
  1344. FC_RPORT_ID_DBG(lport, sid, "Received PLOGI request\n");
  1345. pl = fc_frame_payload_get(fp, sizeof(*pl));
  1346. if (!pl) {
  1347. FC_RPORT_ID_DBG(lport, sid, "Received PLOGI too short\n");
  1348. rjt_data.reason = ELS_RJT_PROT;
  1349. rjt_data.explan = ELS_EXPL_INV_LEN;
  1350. goto reject;
  1351. }
  1352. disc = &lport->disc;
  1353. mutex_lock(&disc->disc_mutex);
  1354. rdata = lport->tt.rport_create(lport, sid);
  1355. if (!rdata) {
  1356. mutex_unlock(&disc->disc_mutex);
  1357. rjt_data.reason = ELS_RJT_UNAB;
  1358. rjt_data.explan = ELS_EXPL_INSUF_RES;
  1359. goto reject;
  1360. }
  1361. mutex_lock(&rdata->rp_mutex);
  1362. mutex_unlock(&disc->disc_mutex);
  1363. rdata->ids.port_name = get_unaligned_be64(&pl->fl_wwpn);
  1364. rdata->ids.node_name = get_unaligned_be64(&pl->fl_wwnn);
  1365. /*
  1366. * If the rport was just created, possibly due to the incoming PLOGI,
  1367. * set the state appropriately and accept the PLOGI.
  1368. *
  1369. * If we had also sent a PLOGI, and if the received PLOGI is from a
  1370. * higher WWPN, we accept it, otherwise an LS_RJT is sent with reason
  1371. * "command already in progress".
  1372. *
  1373. * XXX TBD: If the session was ready before, the PLOGI should result in
  1374. * all outstanding exchanges being reset.
  1375. */
  1376. switch (rdata->rp_state) {
  1377. case RPORT_ST_INIT:
  1378. FC_RPORT_DBG(rdata, "Received PLOGI in INIT state\n");
  1379. break;
  1380. case RPORT_ST_PLOGI_WAIT:
  1381. FC_RPORT_DBG(rdata, "Received PLOGI in PLOGI_WAIT state\n");
  1382. break;
  1383. case RPORT_ST_PLOGI:
  1384. FC_RPORT_DBG(rdata, "Received PLOGI in PLOGI state\n");
  1385. if (rdata->ids.port_name < lport->wwpn) {
  1386. mutex_unlock(&rdata->rp_mutex);
  1387. rjt_data.reason = ELS_RJT_INPROG;
  1388. rjt_data.explan = ELS_EXPL_NONE;
  1389. goto reject;
  1390. }
  1391. break;
  1392. case RPORT_ST_PRLI:
  1393. case RPORT_ST_RTV:
  1394. case RPORT_ST_READY:
  1395. case RPORT_ST_ADISC:
  1396. FC_RPORT_DBG(rdata, "Received PLOGI in logged-in state %d "
  1397. "- ignored for now\n", rdata->rp_state);
  1398. /* XXX TBD - should reset */
  1399. break;
  1400. case RPORT_ST_FLOGI:
  1401. case RPORT_ST_DELETE:
  1402. FC_RPORT_DBG(rdata, "Received PLOGI in state %s - send busy\n",
  1403. fc_rport_state(rdata));
  1404. mutex_unlock(&rdata->rp_mutex);
  1405. rjt_data.reason = ELS_RJT_BUSY;
  1406. rjt_data.explan = ELS_EXPL_NONE;
  1407. goto reject;
  1408. }
  1409. /*
  1410. * Get session payload size from incoming PLOGI.
  1411. */
  1412. rdata->maxframe_size = fc_plogi_get_maxframe(pl, lport->mfs);
  1413. /*
  1414. * Send LS_ACC. If this fails, the originator should retry.
  1415. */
  1416. fp = fc_frame_alloc(lport, sizeof(*pl));
  1417. if (!fp)
  1418. goto out;
  1419. fc_plogi_fill(lport, fp, ELS_LS_ACC);
  1420. fc_fill_reply_hdr(fp, rx_fp, FC_RCTL_ELS_REP, 0);
  1421. lport->tt.frame_send(lport, fp);
  1422. fc_rport_enter_prli(rdata);
  1423. out:
  1424. mutex_unlock(&rdata->rp_mutex);
  1425. fc_frame_free(rx_fp);
  1426. return;
  1427. reject:
  1428. lport->tt.seq_els_rsp_send(fp, ELS_LS_RJT, &rjt_data);
  1429. fc_frame_free(fp);
  1430. }
  1431. /**
  1432. * fc_rport_recv_prli_req() - Handler for process login (PRLI) requests
  1433. * @rdata: The remote port that sent the PRLI request
  1434. * @rx_fp: The PRLI request frame
  1435. *
  1436. * Locking Note: The rport lock is exected to be held before calling
  1437. * this function.
  1438. */
  1439. static void fc_rport_recv_prli_req(struct fc_rport_priv *rdata,
  1440. struct fc_frame *rx_fp)
  1441. {
  1442. struct fc_lport *lport = rdata->local_port;
  1443. struct fc_frame *fp;
  1444. struct {
  1445. struct fc_els_prli prli;
  1446. struct fc_els_spp spp;
  1447. } *pp;
  1448. struct fc_els_spp *rspp; /* request service param page */
  1449. struct fc_els_spp *spp; /* response spp */
  1450. unsigned int len;
  1451. unsigned int plen;
  1452. enum fc_els_spp_resp resp;
  1453. struct fc_seq_els_data rjt_data;
  1454. u32 fcp_parm;
  1455. u32 roles = FC_RPORT_ROLE_UNKNOWN;
  1456. FC_RPORT_DBG(rdata, "Received PRLI request while in state %s\n",
  1457. fc_rport_state(rdata));
  1458. len = fr_len(rx_fp) - sizeof(struct fc_frame_header);
  1459. pp = fc_frame_payload_get(rx_fp, sizeof(*pp));
  1460. if (!pp)
  1461. goto reject_len;
  1462. plen = ntohs(pp->prli.prli_len);
  1463. if ((plen % 4) != 0 || plen > len || plen < 16)
  1464. goto reject_len;
  1465. if (plen < len)
  1466. len = plen;
  1467. plen = pp->prli.prli_spp_len;
  1468. if ((plen % 4) != 0 || plen < sizeof(*spp) ||
  1469. plen > len || len < sizeof(*pp) || plen < 12)
  1470. goto reject_len;
  1471. rspp = &pp->spp;
  1472. fp = fc_frame_alloc(lport, len);
  1473. if (!fp) {
  1474. rjt_data.reason = ELS_RJT_UNAB;
  1475. rjt_data.explan = ELS_EXPL_INSUF_RES;
  1476. goto reject;
  1477. }
  1478. pp = fc_frame_payload_get(fp, len);
  1479. WARN_ON(!pp);
  1480. memset(pp, 0, len);
  1481. pp->prli.prli_cmd = ELS_LS_ACC;
  1482. pp->prli.prli_spp_len = plen;
  1483. pp->prli.prli_len = htons(len);
  1484. len -= sizeof(struct fc_els_prli);
  1485. /* reinitialize remote port roles */
  1486. rdata->ids.roles = FC_RPORT_ROLE_UNKNOWN;
  1487. /*
  1488. * Go through all the service parameter pages and build
  1489. * response. If plen indicates longer SPP than standard,
  1490. * use that. The entire response has been pre-cleared above.
  1491. */
  1492. spp = &pp->spp;
  1493. while (len >= plen) {
  1494. spp->spp_type = rspp->spp_type;
  1495. spp->spp_type_ext = rspp->spp_type_ext;
  1496. spp->spp_flags = rspp->spp_flags & FC_SPP_EST_IMG_PAIR;
  1497. resp = FC_SPP_RESP_ACK;
  1498. switch (rspp->spp_type) {
  1499. case 0: /* common to all FC-4 types */
  1500. break;
  1501. case FC_TYPE_FCP:
  1502. fcp_parm = ntohl(rspp->spp_params);
  1503. if (fcp_parm & FCP_SPPF_RETRY)
  1504. rdata->flags |= FC_RP_FLAGS_RETRY;
  1505. rdata->supported_classes = FC_COS_CLASS3;
  1506. if (fcp_parm & FCP_SPPF_INIT_FCN)
  1507. roles |= FC_RPORT_ROLE_FCP_INITIATOR;
  1508. if (fcp_parm & FCP_SPPF_TARG_FCN)
  1509. roles |= FC_RPORT_ROLE_FCP_TARGET;
  1510. rdata->ids.roles = roles;
  1511. spp->spp_params = htonl(lport->service_params);
  1512. break;
  1513. default:
  1514. resp = FC_SPP_RESP_INVL;
  1515. break;
  1516. }
  1517. spp->spp_flags |= resp;
  1518. len -= plen;
  1519. rspp = (struct fc_els_spp *)((char *)rspp + plen);
  1520. spp = (struct fc_els_spp *)((char *)spp + plen);
  1521. }
  1522. /*
  1523. * Send LS_ACC. If this fails, the originator should retry.
  1524. */
  1525. fc_fill_reply_hdr(fp, rx_fp, FC_RCTL_ELS_REP, 0);
  1526. lport->tt.frame_send(lport, fp);
  1527. switch (rdata->rp_state) {
  1528. case RPORT_ST_PRLI:
  1529. fc_rport_enter_ready(rdata);
  1530. break;
  1531. default:
  1532. break;
  1533. }
  1534. goto drop;
  1535. reject_len:
  1536. rjt_data.reason = ELS_RJT_PROT;
  1537. rjt_data.explan = ELS_EXPL_INV_LEN;
  1538. reject:
  1539. lport->tt.seq_els_rsp_send(rx_fp, ELS_LS_RJT, &rjt_data);
  1540. drop:
  1541. fc_frame_free(rx_fp);
  1542. }
  1543. /**
  1544. * fc_rport_recv_prlo_req() - Handler for process logout (PRLO) requests
  1545. * @rdata: The remote port that sent the PRLO request
  1546. * @rx_fp: The PRLO request frame
  1547. *
  1548. * Locking Note: The rport lock is exected to be held before calling
  1549. * this function.
  1550. */
  1551. static void fc_rport_recv_prlo_req(struct fc_rport_priv *rdata,
  1552. struct fc_frame *rx_fp)
  1553. {
  1554. struct fc_lport *lport = rdata->local_port;
  1555. struct fc_frame *fp;
  1556. struct {
  1557. struct fc_els_prlo prlo;
  1558. struct fc_els_spp spp;
  1559. } *pp;
  1560. struct fc_els_spp *rspp; /* request service param page */
  1561. struct fc_els_spp *spp; /* response spp */
  1562. unsigned int len;
  1563. unsigned int plen;
  1564. struct fc_seq_els_data rjt_data;
  1565. FC_RPORT_DBG(rdata, "Received PRLO request while in state %s\n",
  1566. fc_rport_state(rdata));
  1567. len = fr_len(rx_fp) - sizeof(struct fc_frame_header);
  1568. pp = fc_frame_payload_get(rx_fp, sizeof(*pp));
  1569. if (!pp)
  1570. goto reject_len;
  1571. plen = ntohs(pp->prlo.prlo_len);
  1572. if (plen != 20)
  1573. goto reject_len;
  1574. if (plen < len)
  1575. len = plen;
  1576. rspp = &pp->spp;
  1577. fp = fc_frame_alloc(lport, len);
  1578. if (!fp) {
  1579. rjt_data.reason = ELS_RJT_UNAB;
  1580. rjt_data.explan = ELS_EXPL_INSUF_RES;
  1581. goto reject;
  1582. }
  1583. pp = fc_frame_payload_get(fp, len);
  1584. WARN_ON(!pp);
  1585. memset(pp, 0, len);
  1586. pp->prlo.prlo_cmd = ELS_LS_ACC;
  1587. pp->prlo.prlo_obs = 0x10;
  1588. pp->prlo.prlo_len = htons(len);
  1589. spp = &pp->spp;
  1590. spp->spp_type = rspp->spp_type;
  1591. spp->spp_type_ext = rspp->spp_type_ext;
  1592. spp->spp_flags = FC_SPP_RESP_ACK;
  1593. fc_rport_enter_delete(rdata, RPORT_EV_LOGO);
  1594. fc_fill_reply_hdr(fp, rx_fp, FC_RCTL_ELS_REP, 0);
  1595. lport->tt.frame_send(lport, fp);
  1596. goto drop;
  1597. reject_len:
  1598. rjt_data.reason = ELS_RJT_PROT;
  1599. rjt_data.explan = ELS_EXPL_INV_LEN;
  1600. reject:
  1601. lport->tt.seq_els_rsp_send(rx_fp, ELS_LS_RJT, &rjt_data);
  1602. drop:
  1603. fc_frame_free(rx_fp);
  1604. }
  1605. /**
  1606. * fc_rport_recv_logo_req() - Handler for logout (LOGO) requests
  1607. * @lport: The local port that received the LOGO request
  1608. * @fp: The LOGO request frame
  1609. *
  1610. * Locking Note: The rport lock is exected to be held before calling
  1611. * this function.
  1612. */
  1613. static void fc_rport_recv_logo_req(struct fc_lport *lport, struct fc_frame *fp)
  1614. {
  1615. struct fc_rport_priv *rdata;
  1616. u32 sid;
  1617. lport->tt.seq_els_rsp_send(fp, ELS_LS_ACC, NULL);
  1618. sid = fc_frame_sid(fp);
  1619. mutex_lock(&lport->disc.disc_mutex);
  1620. rdata = lport->tt.rport_lookup(lport, sid);
  1621. if (rdata) {
  1622. mutex_lock(&rdata->rp_mutex);
  1623. FC_RPORT_DBG(rdata, "Received LOGO request while in state %s\n",
  1624. fc_rport_state(rdata));
  1625. fc_rport_enter_delete(rdata, RPORT_EV_LOGO);
  1626. mutex_unlock(&rdata->rp_mutex);
  1627. } else
  1628. FC_RPORT_ID_DBG(lport, sid,
  1629. "Received LOGO from non-logged-in port\n");
  1630. mutex_unlock(&lport->disc.disc_mutex);
  1631. fc_frame_free(fp);
  1632. }
  1633. /**
  1634. * fc_rport_flush_queue() - Flush the rport_event_queue
  1635. */
  1636. static void fc_rport_flush_queue(void)
  1637. {
  1638. flush_workqueue(rport_event_queue);
  1639. }
  1640. /**
  1641. * fc_rport_init() - Initialize the remote port layer for a local port
  1642. * @lport: The local port to initialize the remote port layer for
  1643. */
  1644. int fc_rport_init(struct fc_lport *lport)
  1645. {
  1646. if (!lport->tt.rport_lookup)
  1647. lport->tt.rport_lookup = fc_rport_lookup;
  1648. if (!lport->tt.rport_create)
  1649. lport->tt.rport_create = fc_rport_create;
  1650. if (!lport->tt.rport_login)
  1651. lport->tt.rport_login = fc_rport_login;
  1652. if (!lport->tt.rport_logoff)
  1653. lport->tt.rport_logoff = fc_rport_logoff;
  1654. if (!lport->tt.rport_recv_req)
  1655. lport->tt.rport_recv_req = fc_rport_recv_req;
  1656. if (!lport->tt.rport_flush_queue)
  1657. lport->tt.rport_flush_queue = fc_rport_flush_queue;
  1658. if (!lport->tt.rport_destroy)
  1659. lport->tt.rport_destroy = fc_rport_destroy;
  1660. return 0;
  1661. }
  1662. EXPORT_SYMBOL(fc_rport_init);
  1663. /**
  1664. * fc_setup_rport() - Initialize the rport_event_queue
  1665. */
  1666. int fc_setup_rport()
  1667. {
  1668. rport_event_queue = create_singlethread_workqueue("fc_rport_eq");
  1669. if (!rport_event_queue)
  1670. return -ENOMEM;
  1671. return 0;
  1672. }
  1673. /**
  1674. * fc_destroy_rport() - Destroy the rport_event_queue
  1675. */
  1676. void fc_destroy_rport()
  1677. {
  1678. destroy_workqueue(rport_event_queue);
  1679. }
  1680. /**
  1681. * fc_rport_terminate_io() - Stop all outstanding I/O on a remote port
  1682. * @rport: The remote port whose I/O should be terminated
  1683. */
  1684. void fc_rport_terminate_io(struct fc_rport *rport)
  1685. {
  1686. struct fc_rport_libfc_priv *rpriv = rport->dd_data;
  1687. struct fc_lport *lport = rpriv->local_port;
  1688. lport->tt.exch_mgr_reset(lport, 0, rport->port_id);
  1689. lport->tt.exch_mgr_reset(lport, rport->port_id, 0);
  1690. }
  1691. EXPORT_SYMBOL(fc_rport_terminate_io);