fc_rport.c 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054
  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. /*
  713. * If received the FLOGI request on RPORT which is INIT state
  714. * (means not transition to FLOGI either fc_rport timeout
  715. * function didn;t trigger or this end hasn;t received
  716. * beacon yet from other end. In that case only, allow RPORT
  717. * state machine to continue, otherwise fall through which
  718. * causes the code to send reject response.
  719. * NOTE; Not checking for FIP->state such as VNMP_UP or
  720. * VNMP_CLAIM because if FIP state is not one of those,
  721. * RPORT wouldn;t have created and 'rport_lookup' would have
  722. * failed anyway in that case.
  723. */
  724. if (lport->point_to_multipoint)
  725. break;
  726. case RPORT_ST_DELETE:
  727. mutex_unlock(&rdata->rp_mutex);
  728. rjt_data.reason = ELS_RJT_FIP;
  729. rjt_data.explan = ELS_EXPL_NOT_NEIGHBOR;
  730. goto reject;
  731. case RPORT_ST_FLOGI:
  732. case RPORT_ST_PLOGI_WAIT:
  733. case RPORT_ST_PLOGI:
  734. break;
  735. case RPORT_ST_PRLI:
  736. case RPORT_ST_RTV:
  737. case RPORT_ST_READY:
  738. case RPORT_ST_ADISC:
  739. /*
  740. * Set the remote port to be deleted and to then restart.
  741. * This queues work to be sure exchanges are reset.
  742. */
  743. fc_rport_enter_delete(rdata, RPORT_EV_LOGO);
  744. mutex_unlock(&rdata->rp_mutex);
  745. rjt_data.reason = ELS_RJT_BUSY;
  746. rjt_data.explan = ELS_EXPL_NONE;
  747. goto reject;
  748. }
  749. if (fc_rport_login_complete(rdata, fp)) {
  750. mutex_unlock(&rdata->rp_mutex);
  751. rjt_data.reason = ELS_RJT_LOGIC;
  752. rjt_data.explan = ELS_EXPL_NONE;
  753. goto reject;
  754. }
  755. fp = fc_frame_alloc(lport, sizeof(*flp));
  756. if (!fp)
  757. goto out;
  758. fc_flogi_fill(lport, fp);
  759. flp = fc_frame_payload_get(fp, sizeof(*flp));
  760. flp->fl_cmd = ELS_LS_ACC;
  761. fc_fill_reply_hdr(fp, rx_fp, FC_RCTL_ELS_REP, 0);
  762. lport->tt.frame_send(lport, fp);
  763. if (rdata->ids.port_name < lport->wwpn)
  764. fc_rport_enter_plogi(rdata);
  765. else
  766. fc_rport_state_enter(rdata, RPORT_ST_PLOGI_WAIT);
  767. out:
  768. mutex_unlock(&rdata->rp_mutex);
  769. mutex_unlock(&disc->disc_mutex);
  770. fc_frame_free(rx_fp);
  771. return;
  772. reject:
  773. mutex_unlock(&disc->disc_mutex);
  774. lport->tt.seq_els_rsp_send(rx_fp, ELS_LS_RJT, &rjt_data);
  775. fc_frame_free(rx_fp);
  776. }
  777. /**
  778. * fc_rport_plogi_resp() - Handler for ELS PLOGI responses
  779. * @sp: The sequence the PLOGI is on
  780. * @fp: The PLOGI response frame
  781. * @rdata_arg: The remote port that sent the PLOGI response
  782. *
  783. * Locking Note: This function will be called without the rport lock
  784. * held, but it will lock, call an _enter_* function or fc_rport_error
  785. * and then unlock the rport.
  786. */
  787. static void fc_rport_plogi_resp(struct fc_seq *sp, struct fc_frame *fp,
  788. void *rdata_arg)
  789. {
  790. struct fc_rport_priv *rdata = rdata_arg;
  791. struct fc_lport *lport = rdata->local_port;
  792. struct fc_els_flogi *plp = NULL;
  793. u16 csp_seq;
  794. u16 cssp_seq;
  795. u8 op;
  796. mutex_lock(&rdata->rp_mutex);
  797. FC_RPORT_DBG(rdata, "Received a PLOGI %s\n", fc_els_resp_type(fp));
  798. if (rdata->rp_state != RPORT_ST_PLOGI) {
  799. FC_RPORT_DBG(rdata, "Received a PLOGI response, but in state "
  800. "%s\n", fc_rport_state(rdata));
  801. if (IS_ERR(fp))
  802. goto err;
  803. goto out;
  804. }
  805. if (IS_ERR(fp)) {
  806. fc_rport_error_retry(rdata, fp);
  807. goto err;
  808. }
  809. op = fc_frame_payload_op(fp);
  810. if (op == ELS_LS_ACC &&
  811. (plp = fc_frame_payload_get(fp, sizeof(*plp))) != NULL) {
  812. rdata->ids.port_name = get_unaligned_be64(&plp->fl_wwpn);
  813. rdata->ids.node_name = get_unaligned_be64(&plp->fl_wwnn);
  814. /* save plogi response sp_features for further reference */
  815. rdata->sp_features = ntohs(plp->fl_csp.sp_features);
  816. if (lport->point_to_multipoint)
  817. fc_rport_login_complete(rdata, fp);
  818. csp_seq = ntohs(plp->fl_csp.sp_tot_seq);
  819. cssp_seq = ntohs(plp->fl_cssp[3 - 1].cp_con_seq);
  820. if (cssp_seq < csp_seq)
  821. csp_seq = cssp_seq;
  822. rdata->max_seq = csp_seq;
  823. rdata->maxframe_size = fc_plogi_get_maxframe(plp, lport->mfs);
  824. fc_rport_enter_prli(rdata);
  825. } else
  826. fc_rport_error_retry(rdata, fp);
  827. out:
  828. fc_frame_free(fp);
  829. err:
  830. mutex_unlock(&rdata->rp_mutex);
  831. kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
  832. }
  833. /**
  834. * fc_rport_enter_plogi() - Send Port Login (PLOGI) request
  835. * @rdata: The remote port to send a PLOGI to
  836. *
  837. * Locking Note: The rport lock is expected to be held before calling
  838. * this routine.
  839. */
  840. static void fc_rport_enter_plogi(struct fc_rport_priv *rdata)
  841. {
  842. struct fc_lport *lport = rdata->local_port;
  843. struct fc_frame *fp;
  844. FC_RPORT_DBG(rdata, "Port entered PLOGI state from %s state\n",
  845. fc_rport_state(rdata));
  846. fc_rport_state_enter(rdata, RPORT_ST_PLOGI);
  847. rdata->maxframe_size = FC_MIN_MAX_PAYLOAD;
  848. fp = fc_frame_alloc(lport, sizeof(struct fc_els_flogi));
  849. if (!fp) {
  850. FC_RPORT_DBG(rdata, "%s frame alloc failed\n", __func__);
  851. fc_rport_error_retry(rdata, fp);
  852. return;
  853. }
  854. rdata->e_d_tov = lport->e_d_tov;
  855. if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_PLOGI,
  856. fc_rport_plogi_resp, rdata,
  857. 2 * lport->r_a_tov))
  858. fc_rport_error_retry(rdata, NULL);
  859. else
  860. kref_get(&rdata->kref);
  861. }
  862. /**
  863. * fc_rport_prli_resp() - Process Login (PRLI) response handler
  864. * @sp: The sequence the PRLI response was on
  865. * @fp: The PRLI response frame
  866. * @rdata_arg: The remote port that sent the PRLI response
  867. *
  868. * Locking Note: This function will be called without the rport lock
  869. * held, but it will lock, call an _enter_* function or fc_rport_error
  870. * and then unlock the rport.
  871. */
  872. static void fc_rport_prli_resp(struct fc_seq *sp, struct fc_frame *fp,
  873. void *rdata_arg)
  874. {
  875. struct fc_rport_priv *rdata = rdata_arg;
  876. struct {
  877. struct fc_els_prli prli;
  878. struct fc_els_spp spp;
  879. } *pp;
  880. struct fc_els_spp temp_spp;
  881. struct fc4_prov *prov;
  882. u32 roles = FC_RPORT_ROLE_UNKNOWN;
  883. u32 fcp_parm = 0;
  884. u8 op;
  885. u8 resp_code = 0;
  886. mutex_lock(&rdata->rp_mutex);
  887. FC_RPORT_DBG(rdata, "Received a PRLI %s\n", fc_els_resp_type(fp));
  888. if (rdata->rp_state != RPORT_ST_PRLI) {
  889. FC_RPORT_DBG(rdata, "Received a PRLI response, but in state "
  890. "%s\n", fc_rport_state(rdata));
  891. if (IS_ERR(fp))
  892. goto err;
  893. goto out;
  894. }
  895. if (IS_ERR(fp)) {
  896. fc_rport_error_retry(rdata, fp);
  897. goto err;
  898. }
  899. /* reinitialize remote port roles */
  900. rdata->ids.roles = FC_RPORT_ROLE_UNKNOWN;
  901. op = fc_frame_payload_op(fp);
  902. if (op == ELS_LS_ACC) {
  903. pp = fc_frame_payload_get(fp, sizeof(*pp));
  904. if (!pp)
  905. goto out;
  906. resp_code = (pp->spp.spp_flags & FC_SPP_RESP_MASK);
  907. FC_RPORT_DBG(rdata, "PRLI spp_flags = 0x%x\n",
  908. pp->spp.spp_flags);
  909. rdata->spp_type = pp->spp.spp_type;
  910. if (resp_code != FC_SPP_RESP_ACK) {
  911. if (resp_code == FC_SPP_RESP_CONF)
  912. fc_rport_error(rdata, fp);
  913. else
  914. fc_rport_error_retry(rdata, fp);
  915. goto out;
  916. }
  917. if (pp->prli.prli_spp_len < sizeof(pp->spp))
  918. goto out;
  919. fcp_parm = ntohl(pp->spp.spp_params);
  920. if (fcp_parm & FCP_SPPF_RETRY)
  921. rdata->flags |= FC_RP_FLAGS_RETRY;
  922. if (fcp_parm & FCP_SPPF_CONF_COMPL)
  923. rdata->flags |= FC_RP_FLAGS_CONF_REQ;
  924. prov = fc_passive_prov[FC_TYPE_FCP];
  925. if (prov) {
  926. memset(&temp_spp, 0, sizeof(temp_spp));
  927. prov->prli(rdata, pp->prli.prli_spp_len,
  928. &pp->spp, &temp_spp);
  929. }
  930. rdata->supported_classes = FC_COS_CLASS3;
  931. if (fcp_parm & FCP_SPPF_INIT_FCN)
  932. roles |= FC_RPORT_ROLE_FCP_INITIATOR;
  933. if (fcp_parm & FCP_SPPF_TARG_FCN)
  934. roles |= FC_RPORT_ROLE_FCP_TARGET;
  935. rdata->ids.roles = roles;
  936. fc_rport_enter_rtv(rdata);
  937. } else {
  938. FC_RPORT_DBG(rdata, "Bad ELS response for PRLI command\n");
  939. fc_rport_error_retry(rdata, fp);
  940. }
  941. out:
  942. fc_frame_free(fp);
  943. err:
  944. mutex_unlock(&rdata->rp_mutex);
  945. kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
  946. }
  947. /**
  948. * fc_rport_enter_prli() - Send Process Login (PRLI) request
  949. * @rdata: The remote port to send the PRLI request to
  950. *
  951. * Locking Note: The rport lock is expected to be held before calling
  952. * this routine.
  953. */
  954. static void fc_rport_enter_prli(struct fc_rport_priv *rdata)
  955. {
  956. struct fc_lport *lport = rdata->local_port;
  957. struct {
  958. struct fc_els_prli prli;
  959. struct fc_els_spp spp;
  960. } *pp;
  961. struct fc_frame *fp;
  962. struct fc4_prov *prov;
  963. /*
  964. * If the rport is one of the well known addresses
  965. * we skip PRLI and RTV and go straight to READY.
  966. */
  967. if (rdata->ids.port_id >= FC_FID_DOM_MGR) {
  968. fc_rport_enter_ready(rdata);
  969. return;
  970. }
  971. FC_RPORT_DBG(rdata, "Port entered PRLI state from %s state\n",
  972. fc_rport_state(rdata));
  973. fc_rport_state_enter(rdata, RPORT_ST_PRLI);
  974. fp = fc_frame_alloc(lport, sizeof(*pp));
  975. if (!fp) {
  976. fc_rport_error_retry(rdata, fp);
  977. return;
  978. }
  979. fc_prli_fill(lport, fp);
  980. prov = fc_passive_prov[FC_TYPE_FCP];
  981. if (prov) {
  982. pp = fc_frame_payload_get(fp, sizeof(*pp));
  983. prov->prli(rdata, sizeof(pp->spp), NULL, &pp->spp);
  984. }
  985. fc_fill_fc_hdr(fp, FC_RCTL_ELS_REQ, rdata->ids.port_id,
  986. fc_host_port_id(lport->host), FC_TYPE_ELS,
  987. FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT, 0);
  988. if (!lport->tt.exch_seq_send(lport, fp, fc_rport_prli_resp,
  989. NULL, rdata, 2 * lport->r_a_tov))
  990. fc_rport_error_retry(rdata, NULL);
  991. else
  992. kref_get(&rdata->kref);
  993. }
  994. /**
  995. * fc_rport_els_rtv_resp() - Handler for Request Timeout Value (RTV) responses
  996. * @sp: The sequence the RTV was on
  997. * @fp: The RTV response frame
  998. * @rdata_arg: The remote port that sent the RTV response
  999. *
  1000. * Many targets don't seem to support this.
  1001. *
  1002. * Locking Note: This function will be called without the rport lock
  1003. * held, but it will lock, call an _enter_* function or fc_rport_error
  1004. * and then unlock the rport.
  1005. */
  1006. static void fc_rport_rtv_resp(struct fc_seq *sp, struct fc_frame *fp,
  1007. void *rdata_arg)
  1008. {
  1009. struct fc_rport_priv *rdata = rdata_arg;
  1010. u8 op;
  1011. mutex_lock(&rdata->rp_mutex);
  1012. FC_RPORT_DBG(rdata, "Received a RTV %s\n", fc_els_resp_type(fp));
  1013. if (rdata->rp_state != RPORT_ST_RTV) {
  1014. FC_RPORT_DBG(rdata, "Received a RTV response, but in state "
  1015. "%s\n", fc_rport_state(rdata));
  1016. if (IS_ERR(fp))
  1017. goto err;
  1018. goto out;
  1019. }
  1020. if (IS_ERR(fp)) {
  1021. fc_rport_error(rdata, fp);
  1022. goto err;
  1023. }
  1024. op = fc_frame_payload_op(fp);
  1025. if (op == ELS_LS_ACC) {
  1026. struct fc_els_rtv_acc *rtv;
  1027. u32 toq;
  1028. u32 tov;
  1029. rtv = fc_frame_payload_get(fp, sizeof(*rtv));
  1030. if (rtv) {
  1031. toq = ntohl(rtv->rtv_toq);
  1032. tov = ntohl(rtv->rtv_r_a_tov);
  1033. if (tov == 0)
  1034. tov = 1;
  1035. rdata->r_a_tov = tov;
  1036. tov = ntohl(rtv->rtv_e_d_tov);
  1037. if (toq & FC_ELS_RTV_EDRES)
  1038. tov /= 1000000;
  1039. if (tov == 0)
  1040. tov = 1;
  1041. rdata->e_d_tov = tov;
  1042. }
  1043. }
  1044. fc_rport_enter_ready(rdata);
  1045. out:
  1046. fc_frame_free(fp);
  1047. err:
  1048. mutex_unlock(&rdata->rp_mutex);
  1049. kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
  1050. }
  1051. /**
  1052. * fc_rport_enter_rtv() - Send Request Timeout Value (RTV) request
  1053. * @rdata: The remote port to send the RTV request to
  1054. *
  1055. * Locking Note: The rport lock is expected to be held before calling
  1056. * this routine.
  1057. */
  1058. static void fc_rport_enter_rtv(struct fc_rport_priv *rdata)
  1059. {
  1060. struct fc_frame *fp;
  1061. struct fc_lport *lport = rdata->local_port;
  1062. FC_RPORT_DBG(rdata, "Port entered RTV state from %s state\n",
  1063. fc_rport_state(rdata));
  1064. fc_rport_state_enter(rdata, RPORT_ST_RTV);
  1065. fp = fc_frame_alloc(lport, sizeof(struct fc_els_rtv));
  1066. if (!fp) {
  1067. fc_rport_error_retry(rdata, fp);
  1068. return;
  1069. }
  1070. if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_RTV,
  1071. fc_rport_rtv_resp, rdata,
  1072. 2 * lport->r_a_tov))
  1073. fc_rport_error_retry(rdata, NULL);
  1074. else
  1075. kref_get(&rdata->kref);
  1076. }
  1077. /**
  1078. * fc_rport_logo_resp() - Handler for logout (LOGO) responses
  1079. * @sp: The sequence the LOGO was on
  1080. * @fp: The LOGO response frame
  1081. * @lport_arg: The local port
  1082. */
  1083. static void fc_rport_logo_resp(struct fc_seq *sp, struct fc_frame *fp,
  1084. void *lport_arg)
  1085. {
  1086. struct fc_lport *lport = lport_arg;
  1087. FC_RPORT_ID_DBG(lport, fc_seq_exch(sp)->did,
  1088. "Received a LOGO %s\n", fc_els_resp_type(fp));
  1089. if (IS_ERR(fp))
  1090. return;
  1091. fc_frame_free(fp);
  1092. }
  1093. /**
  1094. * fc_rport_enter_logo() - Send a logout (LOGO) request
  1095. * @rdata: The remote port to send the LOGO request to
  1096. *
  1097. * Locking Note: The rport lock is expected to be held before calling
  1098. * this routine.
  1099. */
  1100. static void fc_rport_enter_logo(struct fc_rport_priv *rdata)
  1101. {
  1102. struct fc_lport *lport = rdata->local_port;
  1103. struct fc_frame *fp;
  1104. FC_RPORT_DBG(rdata, "Port sending LOGO from %s state\n",
  1105. fc_rport_state(rdata));
  1106. fp = fc_frame_alloc(lport, sizeof(struct fc_els_logo));
  1107. if (!fp)
  1108. return;
  1109. (void)lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_LOGO,
  1110. fc_rport_logo_resp, lport, 0);
  1111. }
  1112. /**
  1113. * fc_rport_els_adisc_resp() - Handler for Address Discovery (ADISC) responses
  1114. * @sp: The sequence the ADISC response was on
  1115. * @fp: The ADISC response frame
  1116. * @rdata_arg: The remote port that sent the ADISC response
  1117. *
  1118. * Locking Note: This function will be called without the rport lock
  1119. * held, but it will lock, call an _enter_* function or fc_rport_error
  1120. * and then unlock the rport.
  1121. */
  1122. static void fc_rport_adisc_resp(struct fc_seq *sp, struct fc_frame *fp,
  1123. void *rdata_arg)
  1124. {
  1125. struct fc_rport_priv *rdata = rdata_arg;
  1126. struct fc_els_adisc *adisc;
  1127. u8 op;
  1128. mutex_lock(&rdata->rp_mutex);
  1129. FC_RPORT_DBG(rdata, "Received a ADISC response\n");
  1130. if (rdata->rp_state != RPORT_ST_ADISC) {
  1131. FC_RPORT_DBG(rdata, "Received a ADISC resp but in state %s\n",
  1132. fc_rport_state(rdata));
  1133. if (IS_ERR(fp))
  1134. goto err;
  1135. goto out;
  1136. }
  1137. if (IS_ERR(fp)) {
  1138. fc_rport_error(rdata, fp);
  1139. goto err;
  1140. }
  1141. /*
  1142. * If address verification failed. Consider us logged out of the rport.
  1143. * Since the rport is still in discovery, we want to be
  1144. * logged in, so go to PLOGI state. Otherwise, go back to READY.
  1145. */
  1146. op = fc_frame_payload_op(fp);
  1147. adisc = fc_frame_payload_get(fp, sizeof(*adisc));
  1148. if (op != ELS_LS_ACC || !adisc ||
  1149. ntoh24(adisc->adisc_port_id) != rdata->ids.port_id ||
  1150. get_unaligned_be64(&adisc->adisc_wwpn) != rdata->ids.port_name ||
  1151. get_unaligned_be64(&adisc->adisc_wwnn) != rdata->ids.node_name) {
  1152. FC_RPORT_DBG(rdata, "ADISC error or mismatch\n");
  1153. fc_rport_enter_flogi(rdata);
  1154. } else {
  1155. FC_RPORT_DBG(rdata, "ADISC OK\n");
  1156. fc_rport_enter_ready(rdata);
  1157. }
  1158. out:
  1159. fc_frame_free(fp);
  1160. err:
  1161. mutex_unlock(&rdata->rp_mutex);
  1162. kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
  1163. }
  1164. /**
  1165. * fc_rport_enter_adisc() - Send Address Discover (ADISC) request
  1166. * @rdata: The remote port to send the ADISC request to
  1167. *
  1168. * Locking Note: The rport lock is expected to be held before calling
  1169. * this routine.
  1170. */
  1171. static void fc_rport_enter_adisc(struct fc_rport_priv *rdata)
  1172. {
  1173. struct fc_lport *lport = rdata->local_port;
  1174. struct fc_frame *fp;
  1175. FC_RPORT_DBG(rdata, "sending ADISC from %s state\n",
  1176. fc_rport_state(rdata));
  1177. fc_rport_state_enter(rdata, RPORT_ST_ADISC);
  1178. fp = fc_frame_alloc(lport, sizeof(struct fc_els_adisc));
  1179. if (!fp) {
  1180. fc_rport_error_retry(rdata, fp);
  1181. return;
  1182. }
  1183. if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_ADISC,
  1184. fc_rport_adisc_resp, rdata,
  1185. 2 * lport->r_a_tov))
  1186. fc_rport_error_retry(rdata, NULL);
  1187. else
  1188. kref_get(&rdata->kref);
  1189. }
  1190. /**
  1191. * fc_rport_recv_adisc_req() - Handler for Address Discovery (ADISC) requests
  1192. * @rdata: The remote port that sent the ADISC request
  1193. * @in_fp: The ADISC request frame
  1194. *
  1195. * Locking Note: Called with the lport and rport locks held.
  1196. */
  1197. static void fc_rport_recv_adisc_req(struct fc_rport_priv *rdata,
  1198. struct fc_frame *in_fp)
  1199. {
  1200. struct fc_lport *lport = rdata->local_port;
  1201. struct fc_frame *fp;
  1202. struct fc_els_adisc *adisc;
  1203. struct fc_seq_els_data rjt_data;
  1204. FC_RPORT_DBG(rdata, "Received ADISC request\n");
  1205. adisc = fc_frame_payload_get(in_fp, sizeof(*adisc));
  1206. if (!adisc) {
  1207. rjt_data.reason = ELS_RJT_PROT;
  1208. rjt_data.explan = ELS_EXPL_INV_LEN;
  1209. lport->tt.seq_els_rsp_send(in_fp, ELS_LS_RJT, &rjt_data);
  1210. goto drop;
  1211. }
  1212. fp = fc_frame_alloc(lport, sizeof(*adisc));
  1213. if (!fp)
  1214. goto drop;
  1215. fc_adisc_fill(lport, fp);
  1216. adisc = fc_frame_payload_get(fp, sizeof(*adisc));
  1217. adisc->adisc_cmd = ELS_LS_ACC;
  1218. fc_fill_reply_hdr(fp, in_fp, FC_RCTL_ELS_REP, 0);
  1219. lport->tt.frame_send(lport, fp);
  1220. drop:
  1221. fc_frame_free(in_fp);
  1222. }
  1223. /**
  1224. * fc_rport_recv_rls_req() - Handle received Read Link Status request
  1225. * @rdata: The remote port that sent the RLS request
  1226. * @rx_fp: The PRLI request frame
  1227. *
  1228. * Locking Note: The rport lock is expected to be held before calling
  1229. * this function.
  1230. */
  1231. static void fc_rport_recv_rls_req(struct fc_rport_priv *rdata,
  1232. struct fc_frame *rx_fp)
  1233. {
  1234. struct fc_lport *lport = rdata->local_port;
  1235. struct fc_frame *fp;
  1236. struct fc_els_rls *rls;
  1237. struct fc_els_rls_resp *rsp;
  1238. struct fc_els_lesb *lesb;
  1239. struct fc_seq_els_data rjt_data;
  1240. struct fc_host_statistics *hst;
  1241. FC_RPORT_DBG(rdata, "Received RLS request while in state %s\n",
  1242. fc_rport_state(rdata));
  1243. rls = fc_frame_payload_get(rx_fp, sizeof(*rls));
  1244. if (!rls) {
  1245. rjt_data.reason = ELS_RJT_PROT;
  1246. rjt_data.explan = ELS_EXPL_INV_LEN;
  1247. goto out_rjt;
  1248. }
  1249. fp = fc_frame_alloc(lport, sizeof(*rsp));
  1250. if (!fp) {
  1251. rjt_data.reason = ELS_RJT_UNAB;
  1252. rjt_data.explan = ELS_EXPL_INSUF_RES;
  1253. goto out_rjt;
  1254. }
  1255. rsp = fc_frame_payload_get(fp, sizeof(*rsp));
  1256. memset(rsp, 0, sizeof(*rsp));
  1257. rsp->rls_cmd = ELS_LS_ACC;
  1258. lesb = &rsp->rls_lesb;
  1259. if (lport->tt.get_lesb) {
  1260. /* get LESB from LLD if it supports it */
  1261. lport->tt.get_lesb(lport, lesb);
  1262. } else {
  1263. fc_get_host_stats(lport->host);
  1264. hst = &lport->host_stats;
  1265. lesb->lesb_link_fail = htonl(hst->link_failure_count);
  1266. lesb->lesb_sync_loss = htonl(hst->loss_of_sync_count);
  1267. lesb->lesb_sig_loss = htonl(hst->loss_of_signal_count);
  1268. lesb->lesb_prim_err = htonl(hst->prim_seq_protocol_err_count);
  1269. lesb->lesb_inv_word = htonl(hst->invalid_tx_word_count);
  1270. lesb->lesb_inv_crc = htonl(hst->invalid_crc_count);
  1271. }
  1272. fc_fill_reply_hdr(fp, rx_fp, FC_RCTL_ELS_REP, 0);
  1273. lport->tt.frame_send(lport, fp);
  1274. goto out;
  1275. out_rjt:
  1276. lport->tt.seq_els_rsp_send(rx_fp, ELS_LS_RJT, &rjt_data);
  1277. out:
  1278. fc_frame_free(rx_fp);
  1279. }
  1280. /**
  1281. * fc_rport_recv_els_req() - Handler for validated ELS requests
  1282. * @lport: The local port that received the ELS request
  1283. * @fp: The ELS request frame
  1284. *
  1285. * Handle incoming ELS requests that require port login.
  1286. * The ELS opcode has already been validated by the caller.
  1287. *
  1288. * Locking Note: Called with the lport lock held.
  1289. */
  1290. static void fc_rport_recv_els_req(struct fc_lport *lport, struct fc_frame *fp)
  1291. {
  1292. struct fc_rport_priv *rdata;
  1293. struct fc_seq_els_data els_data;
  1294. mutex_lock(&lport->disc.disc_mutex);
  1295. rdata = lport->tt.rport_lookup(lport, fc_frame_sid(fp));
  1296. if (!rdata) {
  1297. mutex_unlock(&lport->disc.disc_mutex);
  1298. goto reject;
  1299. }
  1300. mutex_lock(&rdata->rp_mutex);
  1301. mutex_unlock(&lport->disc.disc_mutex);
  1302. switch (rdata->rp_state) {
  1303. case RPORT_ST_PRLI:
  1304. case RPORT_ST_RTV:
  1305. case RPORT_ST_READY:
  1306. case RPORT_ST_ADISC:
  1307. break;
  1308. default:
  1309. mutex_unlock(&rdata->rp_mutex);
  1310. goto reject;
  1311. }
  1312. switch (fc_frame_payload_op(fp)) {
  1313. case ELS_PRLI:
  1314. fc_rport_recv_prli_req(rdata, fp);
  1315. break;
  1316. case ELS_PRLO:
  1317. fc_rport_recv_prlo_req(rdata, fp);
  1318. break;
  1319. case ELS_ADISC:
  1320. fc_rport_recv_adisc_req(rdata, fp);
  1321. break;
  1322. case ELS_RRQ:
  1323. lport->tt.seq_els_rsp_send(fp, ELS_RRQ, NULL);
  1324. fc_frame_free(fp);
  1325. break;
  1326. case ELS_REC:
  1327. lport->tt.seq_els_rsp_send(fp, ELS_REC, NULL);
  1328. fc_frame_free(fp);
  1329. break;
  1330. case ELS_RLS:
  1331. fc_rport_recv_rls_req(rdata, fp);
  1332. break;
  1333. default:
  1334. fc_frame_free(fp); /* can't happen */
  1335. break;
  1336. }
  1337. mutex_unlock(&rdata->rp_mutex);
  1338. return;
  1339. reject:
  1340. els_data.reason = ELS_RJT_UNAB;
  1341. els_data.explan = ELS_EXPL_PLOGI_REQD;
  1342. lport->tt.seq_els_rsp_send(fp, ELS_LS_RJT, &els_data);
  1343. fc_frame_free(fp);
  1344. }
  1345. /**
  1346. * fc_rport_recv_req() - Handler for requests
  1347. * @lport: The local port that received the request
  1348. * @fp: The request frame
  1349. *
  1350. * Locking Note: Called with the lport lock held.
  1351. */
  1352. void fc_rport_recv_req(struct fc_lport *lport, struct fc_frame *fp)
  1353. {
  1354. struct fc_seq_els_data els_data;
  1355. /*
  1356. * Handle FLOGI, PLOGI and LOGO requests separately, since they
  1357. * don't require prior login.
  1358. * Check for unsupported opcodes first and reject them.
  1359. * For some ops, it would be incorrect to reject with "PLOGI required".
  1360. */
  1361. switch (fc_frame_payload_op(fp)) {
  1362. case ELS_FLOGI:
  1363. fc_rport_recv_flogi_req(lport, fp);
  1364. break;
  1365. case ELS_PLOGI:
  1366. fc_rport_recv_plogi_req(lport, fp);
  1367. break;
  1368. case ELS_LOGO:
  1369. fc_rport_recv_logo_req(lport, fp);
  1370. break;
  1371. case ELS_PRLI:
  1372. case ELS_PRLO:
  1373. case ELS_ADISC:
  1374. case ELS_RRQ:
  1375. case ELS_REC:
  1376. case ELS_RLS:
  1377. fc_rport_recv_els_req(lport, fp);
  1378. break;
  1379. default:
  1380. els_data.reason = ELS_RJT_UNSUP;
  1381. els_data.explan = ELS_EXPL_NONE;
  1382. lport->tt.seq_els_rsp_send(fp, ELS_LS_RJT, &els_data);
  1383. fc_frame_free(fp);
  1384. break;
  1385. }
  1386. }
  1387. /**
  1388. * fc_rport_recv_plogi_req() - Handler for Port Login (PLOGI) requests
  1389. * @lport: The local port that received the PLOGI request
  1390. * @rx_fp: The PLOGI request frame
  1391. *
  1392. * Locking Note: The rport lock is held before calling this function.
  1393. */
  1394. static void fc_rport_recv_plogi_req(struct fc_lport *lport,
  1395. struct fc_frame *rx_fp)
  1396. {
  1397. struct fc_disc *disc;
  1398. struct fc_rport_priv *rdata;
  1399. struct fc_frame *fp = rx_fp;
  1400. struct fc_els_flogi *pl;
  1401. struct fc_seq_els_data rjt_data;
  1402. u32 sid;
  1403. sid = fc_frame_sid(fp);
  1404. FC_RPORT_ID_DBG(lport, sid, "Received PLOGI request\n");
  1405. pl = fc_frame_payload_get(fp, sizeof(*pl));
  1406. if (!pl) {
  1407. FC_RPORT_ID_DBG(lport, sid, "Received PLOGI too short\n");
  1408. rjt_data.reason = ELS_RJT_PROT;
  1409. rjt_data.explan = ELS_EXPL_INV_LEN;
  1410. goto reject;
  1411. }
  1412. disc = &lport->disc;
  1413. mutex_lock(&disc->disc_mutex);
  1414. rdata = lport->tt.rport_create(lport, sid);
  1415. if (!rdata) {
  1416. mutex_unlock(&disc->disc_mutex);
  1417. rjt_data.reason = ELS_RJT_UNAB;
  1418. rjt_data.explan = ELS_EXPL_INSUF_RES;
  1419. goto reject;
  1420. }
  1421. mutex_lock(&rdata->rp_mutex);
  1422. mutex_unlock(&disc->disc_mutex);
  1423. rdata->ids.port_name = get_unaligned_be64(&pl->fl_wwpn);
  1424. rdata->ids.node_name = get_unaligned_be64(&pl->fl_wwnn);
  1425. /*
  1426. * If the rport was just created, possibly due to the incoming PLOGI,
  1427. * set the state appropriately and accept the PLOGI.
  1428. *
  1429. * If we had also sent a PLOGI, and if the received PLOGI is from a
  1430. * higher WWPN, we accept it, otherwise an LS_RJT is sent with reason
  1431. * "command already in progress".
  1432. *
  1433. * XXX TBD: If the session was ready before, the PLOGI should result in
  1434. * all outstanding exchanges being reset.
  1435. */
  1436. switch (rdata->rp_state) {
  1437. case RPORT_ST_INIT:
  1438. FC_RPORT_DBG(rdata, "Received PLOGI in INIT state\n");
  1439. break;
  1440. case RPORT_ST_PLOGI_WAIT:
  1441. FC_RPORT_DBG(rdata, "Received PLOGI in PLOGI_WAIT state\n");
  1442. break;
  1443. case RPORT_ST_PLOGI:
  1444. FC_RPORT_DBG(rdata, "Received PLOGI in PLOGI state\n");
  1445. if (rdata->ids.port_name < lport->wwpn) {
  1446. mutex_unlock(&rdata->rp_mutex);
  1447. rjt_data.reason = ELS_RJT_INPROG;
  1448. rjt_data.explan = ELS_EXPL_NONE;
  1449. goto reject;
  1450. }
  1451. break;
  1452. case RPORT_ST_PRLI:
  1453. case RPORT_ST_RTV:
  1454. case RPORT_ST_READY:
  1455. case RPORT_ST_ADISC:
  1456. FC_RPORT_DBG(rdata, "Received PLOGI in logged-in state %d "
  1457. "- ignored for now\n", rdata->rp_state);
  1458. /* XXX TBD - should reset */
  1459. break;
  1460. case RPORT_ST_FLOGI:
  1461. case RPORT_ST_DELETE:
  1462. FC_RPORT_DBG(rdata, "Received PLOGI in state %s - send busy\n",
  1463. fc_rport_state(rdata));
  1464. mutex_unlock(&rdata->rp_mutex);
  1465. rjt_data.reason = ELS_RJT_BUSY;
  1466. rjt_data.explan = ELS_EXPL_NONE;
  1467. goto reject;
  1468. }
  1469. /*
  1470. * Get session payload size from incoming PLOGI.
  1471. */
  1472. rdata->maxframe_size = fc_plogi_get_maxframe(pl, lport->mfs);
  1473. /*
  1474. * Send LS_ACC. If this fails, the originator should retry.
  1475. */
  1476. fp = fc_frame_alloc(lport, sizeof(*pl));
  1477. if (!fp)
  1478. goto out;
  1479. fc_plogi_fill(lport, fp, ELS_LS_ACC);
  1480. fc_fill_reply_hdr(fp, rx_fp, FC_RCTL_ELS_REP, 0);
  1481. lport->tt.frame_send(lport, fp);
  1482. fc_rport_enter_prli(rdata);
  1483. out:
  1484. mutex_unlock(&rdata->rp_mutex);
  1485. fc_frame_free(rx_fp);
  1486. return;
  1487. reject:
  1488. lport->tt.seq_els_rsp_send(fp, ELS_LS_RJT, &rjt_data);
  1489. fc_frame_free(fp);
  1490. }
  1491. /**
  1492. * fc_rport_recv_prli_req() - Handler for process login (PRLI) requests
  1493. * @rdata: The remote port that sent the PRLI request
  1494. * @rx_fp: The PRLI request frame
  1495. *
  1496. * Locking Note: The rport lock is exected to be held before calling
  1497. * this function.
  1498. */
  1499. static void fc_rport_recv_prli_req(struct fc_rport_priv *rdata,
  1500. struct fc_frame *rx_fp)
  1501. {
  1502. struct fc_lport *lport = rdata->local_port;
  1503. struct fc_frame *fp;
  1504. struct {
  1505. struct fc_els_prli prli;
  1506. struct fc_els_spp spp;
  1507. } *pp;
  1508. struct fc_els_spp *rspp; /* request service param page */
  1509. struct fc_els_spp *spp; /* response spp */
  1510. unsigned int len;
  1511. unsigned int plen;
  1512. enum fc_els_spp_resp resp;
  1513. enum fc_els_spp_resp passive;
  1514. struct fc_seq_els_data rjt_data;
  1515. struct fc4_prov *prov;
  1516. FC_RPORT_DBG(rdata, "Received PRLI request while in state %s\n",
  1517. fc_rport_state(rdata));
  1518. len = fr_len(rx_fp) - sizeof(struct fc_frame_header);
  1519. pp = fc_frame_payload_get(rx_fp, sizeof(*pp));
  1520. if (!pp)
  1521. goto reject_len;
  1522. plen = ntohs(pp->prli.prli_len);
  1523. if ((plen % 4) != 0 || plen > len || plen < 16)
  1524. goto reject_len;
  1525. if (plen < len)
  1526. len = plen;
  1527. plen = pp->prli.prli_spp_len;
  1528. if ((plen % 4) != 0 || plen < sizeof(*spp) ||
  1529. plen > len || len < sizeof(*pp) || plen < 12)
  1530. goto reject_len;
  1531. rspp = &pp->spp;
  1532. fp = fc_frame_alloc(lport, len);
  1533. if (!fp) {
  1534. rjt_data.reason = ELS_RJT_UNAB;
  1535. rjt_data.explan = ELS_EXPL_INSUF_RES;
  1536. goto reject;
  1537. }
  1538. pp = fc_frame_payload_get(fp, len);
  1539. WARN_ON(!pp);
  1540. memset(pp, 0, len);
  1541. pp->prli.prli_cmd = ELS_LS_ACC;
  1542. pp->prli.prli_spp_len = plen;
  1543. pp->prli.prli_len = htons(len);
  1544. len -= sizeof(struct fc_els_prli);
  1545. /*
  1546. * Go through all the service parameter pages and build
  1547. * response. If plen indicates longer SPP than standard,
  1548. * use that. The entire response has been pre-cleared above.
  1549. */
  1550. spp = &pp->spp;
  1551. mutex_lock(&fc_prov_mutex);
  1552. while (len >= plen) {
  1553. rdata->spp_type = rspp->spp_type;
  1554. spp->spp_type = rspp->spp_type;
  1555. spp->spp_type_ext = rspp->spp_type_ext;
  1556. resp = 0;
  1557. if (rspp->spp_type < FC_FC4_PROV_SIZE) {
  1558. prov = fc_active_prov[rspp->spp_type];
  1559. if (prov)
  1560. resp = prov->prli(rdata, plen, rspp, spp);
  1561. prov = fc_passive_prov[rspp->spp_type];
  1562. if (prov) {
  1563. passive = prov->prli(rdata, plen, rspp, spp);
  1564. if (!resp || passive == FC_SPP_RESP_ACK)
  1565. resp = passive;
  1566. }
  1567. }
  1568. if (!resp) {
  1569. if (spp->spp_flags & FC_SPP_EST_IMG_PAIR)
  1570. resp |= FC_SPP_RESP_CONF;
  1571. else
  1572. resp |= FC_SPP_RESP_INVL;
  1573. }
  1574. spp->spp_flags |= resp;
  1575. len -= plen;
  1576. rspp = (struct fc_els_spp *)((char *)rspp + plen);
  1577. spp = (struct fc_els_spp *)((char *)spp + plen);
  1578. }
  1579. mutex_unlock(&fc_prov_mutex);
  1580. /*
  1581. * Send LS_ACC. If this fails, the originator should retry.
  1582. */
  1583. fc_fill_reply_hdr(fp, rx_fp, FC_RCTL_ELS_REP, 0);
  1584. lport->tt.frame_send(lport, fp);
  1585. switch (rdata->rp_state) {
  1586. case RPORT_ST_PRLI:
  1587. fc_rport_enter_ready(rdata);
  1588. break;
  1589. default:
  1590. break;
  1591. }
  1592. goto drop;
  1593. reject_len:
  1594. rjt_data.reason = ELS_RJT_PROT;
  1595. rjt_data.explan = ELS_EXPL_INV_LEN;
  1596. reject:
  1597. lport->tt.seq_els_rsp_send(rx_fp, ELS_LS_RJT, &rjt_data);
  1598. drop:
  1599. fc_frame_free(rx_fp);
  1600. }
  1601. /**
  1602. * fc_rport_recv_prlo_req() - Handler for process logout (PRLO) requests
  1603. * @rdata: The remote port that sent the PRLO request
  1604. * @rx_fp: The PRLO request frame
  1605. *
  1606. * Locking Note: The rport lock is exected to be held before calling
  1607. * this function.
  1608. */
  1609. static void fc_rport_recv_prlo_req(struct fc_rport_priv *rdata,
  1610. struct fc_frame *rx_fp)
  1611. {
  1612. struct fc_lport *lport = rdata->local_port;
  1613. struct fc_frame *fp;
  1614. struct {
  1615. struct fc_els_prlo prlo;
  1616. struct fc_els_spp spp;
  1617. } *pp;
  1618. struct fc_els_spp *rspp; /* request service param page */
  1619. struct fc_els_spp *spp; /* response spp */
  1620. unsigned int len;
  1621. unsigned int plen;
  1622. struct fc_seq_els_data rjt_data;
  1623. FC_RPORT_DBG(rdata, "Received PRLO request while in state %s\n",
  1624. fc_rport_state(rdata));
  1625. len = fr_len(rx_fp) - sizeof(struct fc_frame_header);
  1626. pp = fc_frame_payload_get(rx_fp, sizeof(*pp));
  1627. if (!pp)
  1628. goto reject_len;
  1629. plen = ntohs(pp->prlo.prlo_len);
  1630. if (plen != 20)
  1631. goto reject_len;
  1632. if (plen < len)
  1633. len = plen;
  1634. rspp = &pp->spp;
  1635. fp = fc_frame_alloc(lport, len);
  1636. if (!fp) {
  1637. rjt_data.reason = ELS_RJT_UNAB;
  1638. rjt_data.explan = ELS_EXPL_INSUF_RES;
  1639. goto reject;
  1640. }
  1641. pp = fc_frame_payload_get(fp, len);
  1642. WARN_ON(!pp);
  1643. memset(pp, 0, len);
  1644. pp->prlo.prlo_cmd = ELS_LS_ACC;
  1645. pp->prlo.prlo_obs = 0x10;
  1646. pp->prlo.prlo_len = htons(len);
  1647. spp = &pp->spp;
  1648. spp->spp_type = rspp->spp_type;
  1649. spp->spp_type_ext = rspp->spp_type_ext;
  1650. spp->spp_flags = FC_SPP_RESP_ACK;
  1651. fc_rport_enter_delete(rdata, RPORT_EV_LOGO);
  1652. fc_fill_reply_hdr(fp, rx_fp, FC_RCTL_ELS_REP, 0);
  1653. lport->tt.frame_send(lport, fp);
  1654. goto drop;
  1655. reject_len:
  1656. rjt_data.reason = ELS_RJT_PROT;
  1657. rjt_data.explan = ELS_EXPL_INV_LEN;
  1658. reject:
  1659. lport->tt.seq_els_rsp_send(rx_fp, ELS_LS_RJT, &rjt_data);
  1660. drop:
  1661. fc_frame_free(rx_fp);
  1662. }
  1663. /**
  1664. * fc_rport_recv_logo_req() - Handler for logout (LOGO) requests
  1665. * @lport: The local port that received the LOGO request
  1666. * @fp: The LOGO request frame
  1667. *
  1668. * Locking Note: The rport lock is exected to be held before calling
  1669. * this function.
  1670. */
  1671. static void fc_rport_recv_logo_req(struct fc_lport *lport, struct fc_frame *fp)
  1672. {
  1673. struct fc_rport_priv *rdata;
  1674. u32 sid;
  1675. lport->tt.seq_els_rsp_send(fp, ELS_LS_ACC, NULL);
  1676. sid = fc_frame_sid(fp);
  1677. mutex_lock(&lport->disc.disc_mutex);
  1678. rdata = lport->tt.rport_lookup(lport, sid);
  1679. if (rdata) {
  1680. mutex_lock(&rdata->rp_mutex);
  1681. FC_RPORT_DBG(rdata, "Received LOGO request while in state %s\n",
  1682. fc_rport_state(rdata));
  1683. fc_rport_enter_delete(rdata, RPORT_EV_LOGO);
  1684. mutex_unlock(&rdata->rp_mutex);
  1685. } else
  1686. FC_RPORT_ID_DBG(lport, sid,
  1687. "Received LOGO from non-logged-in port\n");
  1688. mutex_unlock(&lport->disc.disc_mutex);
  1689. fc_frame_free(fp);
  1690. }
  1691. /**
  1692. * fc_rport_flush_queue() - Flush the rport_event_queue
  1693. */
  1694. static void fc_rport_flush_queue(void)
  1695. {
  1696. flush_workqueue(rport_event_queue);
  1697. }
  1698. /**
  1699. * fc_rport_init() - Initialize the remote port layer for a local port
  1700. * @lport: The local port to initialize the remote port layer for
  1701. */
  1702. int fc_rport_init(struct fc_lport *lport)
  1703. {
  1704. if (!lport->tt.rport_lookup)
  1705. lport->tt.rport_lookup = fc_rport_lookup;
  1706. if (!lport->tt.rport_create)
  1707. lport->tt.rport_create = fc_rport_create;
  1708. if (!lport->tt.rport_login)
  1709. lport->tt.rport_login = fc_rport_login;
  1710. if (!lport->tt.rport_logoff)
  1711. lport->tt.rport_logoff = fc_rport_logoff;
  1712. if (!lport->tt.rport_recv_req)
  1713. lport->tt.rport_recv_req = fc_rport_recv_req;
  1714. if (!lport->tt.rport_flush_queue)
  1715. lport->tt.rport_flush_queue = fc_rport_flush_queue;
  1716. if (!lport->tt.rport_destroy)
  1717. lport->tt.rport_destroy = fc_rport_destroy;
  1718. return 0;
  1719. }
  1720. EXPORT_SYMBOL(fc_rport_init);
  1721. /**
  1722. * fc_rport_fcp_prli() - Handle incoming PRLI for the FCP initiator.
  1723. * @rdata: remote port private
  1724. * @spp_len: service parameter page length
  1725. * @rspp: received service parameter page
  1726. * @spp: response service parameter page
  1727. *
  1728. * Returns the value for the response code to be placed in spp_flags;
  1729. * Returns 0 if not an initiator.
  1730. */
  1731. static int fc_rport_fcp_prli(struct fc_rport_priv *rdata, u32 spp_len,
  1732. const struct fc_els_spp *rspp,
  1733. struct fc_els_spp *spp)
  1734. {
  1735. struct fc_lport *lport = rdata->local_port;
  1736. u32 fcp_parm;
  1737. fcp_parm = ntohl(rspp->spp_params);
  1738. rdata->ids.roles = FC_RPORT_ROLE_UNKNOWN;
  1739. if (fcp_parm & FCP_SPPF_INIT_FCN)
  1740. rdata->ids.roles |= FC_RPORT_ROLE_FCP_INITIATOR;
  1741. if (fcp_parm & FCP_SPPF_TARG_FCN)
  1742. rdata->ids.roles |= FC_RPORT_ROLE_FCP_TARGET;
  1743. if (fcp_parm & FCP_SPPF_RETRY)
  1744. rdata->flags |= FC_RP_FLAGS_RETRY;
  1745. rdata->supported_classes = FC_COS_CLASS3;
  1746. if (!(lport->service_params & FC_RPORT_ROLE_FCP_INITIATOR))
  1747. return 0;
  1748. spp->spp_flags |= rspp->spp_flags & FC_SPP_EST_IMG_PAIR;
  1749. /*
  1750. * OR in our service parameters with other providers (target), if any.
  1751. */
  1752. fcp_parm = ntohl(spp->spp_params);
  1753. spp->spp_params = htonl(fcp_parm | lport->service_params);
  1754. return FC_SPP_RESP_ACK;
  1755. }
  1756. /*
  1757. * FC-4 provider ops for FCP initiator.
  1758. */
  1759. struct fc4_prov fc_rport_fcp_init = {
  1760. .prli = fc_rport_fcp_prli,
  1761. };
  1762. /**
  1763. * fc_rport_t0_prli() - Handle incoming PRLI parameters for type 0
  1764. * @rdata: remote port private
  1765. * @spp_len: service parameter page length
  1766. * @rspp: received service parameter page
  1767. * @spp: response service parameter page
  1768. */
  1769. static int fc_rport_t0_prli(struct fc_rport_priv *rdata, u32 spp_len,
  1770. const struct fc_els_spp *rspp,
  1771. struct fc_els_spp *spp)
  1772. {
  1773. if (rspp->spp_flags & FC_SPP_EST_IMG_PAIR)
  1774. return FC_SPP_RESP_INVL;
  1775. return FC_SPP_RESP_ACK;
  1776. }
  1777. /*
  1778. * FC-4 provider ops for type 0 service parameters.
  1779. *
  1780. * This handles the special case of type 0 which is always successful
  1781. * but doesn't do anything otherwise.
  1782. */
  1783. struct fc4_prov fc_rport_t0_prov = {
  1784. .prli = fc_rport_t0_prli,
  1785. };
  1786. /**
  1787. * fc_setup_rport() - Initialize the rport_event_queue
  1788. */
  1789. int fc_setup_rport(void)
  1790. {
  1791. rport_event_queue = create_singlethread_workqueue("fc_rport_eq");
  1792. if (!rport_event_queue)
  1793. return -ENOMEM;
  1794. return 0;
  1795. }
  1796. /**
  1797. * fc_destroy_rport() - Destroy the rport_event_queue
  1798. */
  1799. void fc_destroy_rport(void)
  1800. {
  1801. destroy_workqueue(rport_event_queue);
  1802. }
  1803. /**
  1804. * fc_rport_terminate_io() - Stop all outstanding I/O on a remote port
  1805. * @rport: The remote port whose I/O should be terminated
  1806. */
  1807. void fc_rport_terminate_io(struct fc_rport *rport)
  1808. {
  1809. struct fc_rport_libfc_priv *rpriv = rport->dd_data;
  1810. struct fc_lport *lport = rpriv->local_port;
  1811. lport->tt.exch_mgr_reset(lport, 0, rport->port_id);
  1812. lport->tt.exch_mgr_reset(lport, rport->port_id, 0);
  1813. }
  1814. EXPORT_SYMBOL(fc_rport_terminate_io);