fc_rport.c 46 KB

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