fc_rport.c 55 KB

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