fc_rport.c 52 KB

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