fc_rport.c 54 KB

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