fc_rport.c 53 KB

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