fc_rport.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640
  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, lport->e_d_tov))
  597. fc_rport_error_retry(rdata, NULL);
  598. else
  599. kref_get(&rdata->kref);
  600. }
  601. /**
  602. * fc_rport_prli_resp() - Process Login (PRLI) response handler
  603. * @sp: The sequence the PRLI response was on
  604. * @fp: The PRLI response frame
  605. * @rdata_arg: The remote port that sent the PRLI response
  606. *
  607. * Locking Note: This function will be called without the rport lock
  608. * held, but it will lock, call an _enter_* function or fc_rport_error
  609. * and then unlock the rport.
  610. */
  611. static void fc_rport_prli_resp(struct fc_seq *sp, struct fc_frame *fp,
  612. void *rdata_arg)
  613. {
  614. struct fc_rport_priv *rdata = rdata_arg;
  615. struct {
  616. struct fc_els_prli prli;
  617. struct fc_els_spp spp;
  618. } *pp;
  619. u32 roles = FC_RPORT_ROLE_UNKNOWN;
  620. u32 fcp_parm = 0;
  621. u8 op;
  622. mutex_lock(&rdata->rp_mutex);
  623. FC_RPORT_DBG(rdata, "Received a PRLI %s\n", fc_els_resp_type(fp));
  624. if (rdata->rp_state != RPORT_ST_PRLI) {
  625. FC_RPORT_DBG(rdata, "Received a PRLI response, but in state "
  626. "%s\n", fc_rport_state(rdata));
  627. if (IS_ERR(fp))
  628. goto err;
  629. goto out;
  630. }
  631. if (IS_ERR(fp)) {
  632. fc_rport_error_retry(rdata, fp);
  633. goto err;
  634. }
  635. /* reinitialize remote port roles */
  636. rdata->ids.roles = FC_RPORT_ROLE_UNKNOWN;
  637. op = fc_frame_payload_op(fp);
  638. if (op == ELS_LS_ACC) {
  639. pp = fc_frame_payload_get(fp, sizeof(*pp));
  640. if (pp && pp->prli.prli_spp_len >= sizeof(pp->spp)) {
  641. fcp_parm = ntohl(pp->spp.spp_params);
  642. if (fcp_parm & FCP_SPPF_RETRY)
  643. rdata->flags |= FC_RP_FLAGS_RETRY;
  644. }
  645. rdata->supported_classes = FC_COS_CLASS3;
  646. if (fcp_parm & FCP_SPPF_INIT_FCN)
  647. roles |= FC_RPORT_ROLE_FCP_INITIATOR;
  648. if (fcp_parm & FCP_SPPF_TARG_FCN)
  649. roles |= FC_RPORT_ROLE_FCP_TARGET;
  650. rdata->ids.roles = roles;
  651. fc_rport_enter_rtv(rdata);
  652. } else {
  653. FC_RPORT_DBG(rdata, "Bad ELS response for PRLI command\n");
  654. fc_rport_enter_delete(rdata, RPORT_EV_FAILED);
  655. }
  656. out:
  657. fc_frame_free(fp);
  658. err:
  659. mutex_unlock(&rdata->rp_mutex);
  660. kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
  661. }
  662. /**
  663. * fc_rport_logo_resp() - Handler for logout (LOGO) responses
  664. * @sp: The sequence the LOGO was on
  665. * @fp: The LOGO response frame
  666. * @rdata_arg: The remote port that sent the LOGO response
  667. *
  668. * Locking Note: This function will be called without the rport lock
  669. * held, but it will lock, call an _enter_* function or fc_rport_error
  670. * and then unlock the rport.
  671. */
  672. static void fc_rport_logo_resp(struct fc_seq *sp, struct fc_frame *fp,
  673. void *rdata_arg)
  674. {
  675. struct fc_rport_priv *rdata = rdata_arg;
  676. u8 op;
  677. mutex_lock(&rdata->rp_mutex);
  678. FC_RPORT_DBG(rdata, "Received a LOGO %s\n", fc_els_resp_type(fp));
  679. if (rdata->rp_state != RPORT_ST_LOGO) {
  680. FC_RPORT_DBG(rdata, "Received a LOGO response, but in state "
  681. "%s\n", fc_rport_state(rdata));
  682. if (IS_ERR(fp))
  683. goto err;
  684. goto out;
  685. }
  686. if (IS_ERR(fp)) {
  687. fc_rport_error_retry(rdata, fp);
  688. goto err;
  689. }
  690. op = fc_frame_payload_op(fp);
  691. if (op != ELS_LS_ACC)
  692. FC_RPORT_DBG(rdata, "Bad ELS response op %x for LOGO command\n",
  693. op);
  694. fc_rport_enter_delete(rdata, RPORT_EV_LOGO);
  695. out:
  696. fc_frame_free(fp);
  697. err:
  698. mutex_unlock(&rdata->rp_mutex);
  699. kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
  700. }
  701. /**
  702. * fc_rport_enter_prli() - Send Process Login (PRLI) request
  703. * @rdata: The remote port to send the PRLI request to
  704. *
  705. * Locking Note: The rport lock is expected to be held before calling
  706. * this routine.
  707. */
  708. static void fc_rport_enter_prli(struct fc_rport_priv *rdata)
  709. {
  710. struct fc_lport *lport = rdata->local_port;
  711. struct {
  712. struct fc_els_prli prli;
  713. struct fc_els_spp spp;
  714. } *pp;
  715. struct fc_frame *fp;
  716. /*
  717. * If the rport is one of the well known addresses
  718. * we skip PRLI and RTV and go straight to READY.
  719. */
  720. if (rdata->ids.port_id >= FC_FID_DOM_MGR) {
  721. fc_rport_enter_ready(rdata);
  722. return;
  723. }
  724. FC_RPORT_DBG(rdata, "Port entered PRLI state from %s state\n",
  725. fc_rport_state(rdata));
  726. fc_rport_state_enter(rdata, RPORT_ST_PRLI);
  727. fp = fc_frame_alloc(lport, sizeof(*pp));
  728. if (!fp) {
  729. fc_rport_error_retry(rdata, fp);
  730. return;
  731. }
  732. if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_PRLI,
  733. fc_rport_prli_resp, rdata, lport->e_d_tov))
  734. fc_rport_error_retry(rdata, NULL);
  735. else
  736. kref_get(&rdata->kref);
  737. }
  738. /**
  739. * fc_rport_els_rtv_resp() - Handler for Request Timeout Value (RTV) responses
  740. * @sp: The sequence the RTV was on
  741. * @fp: The RTV response frame
  742. * @rdata_arg: The remote port that sent the RTV response
  743. *
  744. * Many targets don't seem to support this.
  745. *
  746. * Locking Note: This function will be called without the rport lock
  747. * held, but it will lock, call an _enter_* function or fc_rport_error
  748. * and then unlock the rport.
  749. */
  750. static void fc_rport_rtv_resp(struct fc_seq *sp, struct fc_frame *fp,
  751. void *rdata_arg)
  752. {
  753. struct fc_rport_priv *rdata = rdata_arg;
  754. u8 op;
  755. mutex_lock(&rdata->rp_mutex);
  756. FC_RPORT_DBG(rdata, "Received a RTV %s\n", fc_els_resp_type(fp));
  757. if (rdata->rp_state != RPORT_ST_RTV) {
  758. FC_RPORT_DBG(rdata, "Received a RTV response, but in state "
  759. "%s\n", fc_rport_state(rdata));
  760. if (IS_ERR(fp))
  761. goto err;
  762. goto out;
  763. }
  764. if (IS_ERR(fp)) {
  765. fc_rport_error(rdata, fp);
  766. goto err;
  767. }
  768. op = fc_frame_payload_op(fp);
  769. if (op == ELS_LS_ACC) {
  770. struct fc_els_rtv_acc *rtv;
  771. u32 toq;
  772. u32 tov;
  773. rtv = fc_frame_payload_get(fp, sizeof(*rtv));
  774. if (rtv) {
  775. toq = ntohl(rtv->rtv_toq);
  776. tov = ntohl(rtv->rtv_r_a_tov);
  777. if (tov == 0)
  778. tov = 1;
  779. rdata->r_a_tov = tov;
  780. tov = ntohl(rtv->rtv_e_d_tov);
  781. if (toq & FC_ELS_RTV_EDRES)
  782. tov /= 1000000;
  783. if (tov == 0)
  784. tov = 1;
  785. rdata->e_d_tov = tov;
  786. }
  787. }
  788. fc_rport_enter_ready(rdata);
  789. out:
  790. fc_frame_free(fp);
  791. err:
  792. mutex_unlock(&rdata->rp_mutex);
  793. kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
  794. }
  795. /**
  796. * fc_rport_enter_rtv() - Send Request Timeout Value (RTV) request
  797. * @rdata: The remote port to send the RTV request to
  798. *
  799. * Locking Note: The rport lock is expected to be held before calling
  800. * this routine.
  801. */
  802. static void fc_rport_enter_rtv(struct fc_rport_priv *rdata)
  803. {
  804. struct fc_frame *fp;
  805. struct fc_lport *lport = rdata->local_port;
  806. FC_RPORT_DBG(rdata, "Port entered RTV state from %s state\n",
  807. fc_rport_state(rdata));
  808. fc_rport_state_enter(rdata, RPORT_ST_RTV);
  809. fp = fc_frame_alloc(lport, sizeof(struct fc_els_rtv));
  810. if (!fp) {
  811. fc_rport_error_retry(rdata, fp);
  812. return;
  813. }
  814. if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_RTV,
  815. fc_rport_rtv_resp, rdata, lport->e_d_tov))
  816. fc_rport_error_retry(rdata, NULL);
  817. else
  818. kref_get(&rdata->kref);
  819. }
  820. /**
  821. * fc_rport_enter_logo() - Send a logout (LOGO) request
  822. * @rdata: The remote port to send the LOGO request to
  823. *
  824. * Locking Note: The rport lock is expected to be held before calling
  825. * this routine.
  826. */
  827. static void fc_rport_enter_logo(struct fc_rport_priv *rdata)
  828. {
  829. struct fc_lport *lport = rdata->local_port;
  830. struct fc_frame *fp;
  831. FC_RPORT_DBG(rdata, "Port entered LOGO state from %s state\n",
  832. fc_rport_state(rdata));
  833. fc_rport_state_enter(rdata, RPORT_ST_LOGO);
  834. fp = fc_frame_alloc(lport, sizeof(struct fc_els_logo));
  835. if (!fp) {
  836. fc_rport_error_retry(rdata, fp);
  837. return;
  838. }
  839. if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_LOGO,
  840. fc_rport_logo_resp, rdata, lport->e_d_tov))
  841. fc_rport_error_retry(rdata, NULL);
  842. else
  843. kref_get(&rdata->kref);
  844. }
  845. /**
  846. * fc_rport_els_adisc_resp() - Handler for Address Discovery (ADISC) responses
  847. * @sp: The sequence the ADISC response was on
  848. * @fp: The ADISC response frame
  849. * @rdata_arg: The remote port that sent the ADISC response
  850. *
  851. * Locking Note: This function will be called without the rport lock
  852. * held, but it will lock, call an _enter_* function or fc_rport_error
  853. * and then unlock the rport.
  854. */
  855. static void fc_rport_adisc_resp(struct fc_seq *sp, struct fc_frame *fp,
  856. void *rdata_arg)
  857. {
  858. struct fc_rport_priv *rdata = rdata_arg;
  859. struct fc_els_adisc *adisc;
  860. u8 op;
  861. mutex_lock(&rdata->rp_mutex);
  862. FC_RPORT_DBG(rdata, "Received a ADISC response\n");
  863. if (rdata->rp_state != RPORT_ST_ADISC) {
  864. FC_RPORT_DBG(rdata, "Received a ADISC resp but in state %s\n",
  865. fc_rport_state(rdata));
  866. if (IS_ERR(fp))
  867. goto err;
  868. goto out;
  869. }
  870. if (IS_ERR(fp)) {
  871. fc_rport_error(rdata, fp);
  872. goto err;
  873. }
  874. /*
  875. * If address verification failed. Consider us logged out of the rport.
  876. * Since the rport is still in discovery, we want to be
  877. * logged in, so go to PLOGI state. Otherwise, go back to READY.
  878. */
  879. op = fc_frame_payload_op(fp);
  880. adisc = fc_frame_payload_get(fp, sizeof(*adisc));
  881. if (op != ELS_LS_ACC || !adisc ||
  882. ntoh24(adisc->adisc_port_id) != rdata->ids.port_id ||
  883. get_unaligned_be64(&adisc->adisc_wwpn) != rdata->ids.port_name ||
  884. get_unaligned_be64(&adisc->adisc_wwnn) != rdata->ids.node_name) {
  885. FC_RPORT_DBG(rdata, "ADISC error or mismatch\n");
  886. fc_rport_enter_plogi(rdata);
  887. } else {
  888. FC_RPORT_DBG(rdata, "ADISC OK\n");
  889. fc_rport_enter_ready(rdata);
  890. }
  891. out:
  892. fc_frame_free(fp);
  893. err:
  894. mutex_unlock(&rdata->rp_mutex);
  895. kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
  896. }
  897. /**
  898. * fc_rport_enter_adisc() - Send Address Discover (ADISC) request
  899. * @rdata: The remote port to send the ADISC request to
  900. *
  901. * Locking Note: The rport lock is expected to be held before calling
  902. * this routine.
  903. */
  904. static void fc_rport_enter_adisc(struct fc_rport_priv *rdata)
  905. {
  906. struct fc_lport *lport = rdata->local_port;
  907. struct fc_frame *fp;
  908. FC_RPORT_DBG(rdata, "sending ADISC from %s state\n",
  909. fc_rport_state(rdata));
  910. fc_rport_state_enter(rdata, RPORT_ST_ADISC);
  911. fp = fc_frame_alloc(lport, sizeof(struct fc_els_adisc));
  912. if (!fp) {
  913. fc_rport_error_retry(rdata, fp);
  914. return;
  915. }
  916. if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_ADISC,
  917. fc_rport_adisc_resp, rdata, lport->e_d_tov))
  918. fc_rport_error_retry(rdata, NULL);
  919. else
  920. kref_get(&rdata->kref);
  921. }
  922. /**
  923. * fc_rport_recv_adisc_req() - Handler for Address Discovery (ADISC) requests
  924. * @rdata: The remote port that sent the ADISC request
  925. * @sp: The sequence the ADISC request was on
  926. * @in_fp: The ADISC request frame
  927. *
  928. * Locking Note: Called with the lport and rport locks held.
  929. */
  930. static void fc_rport_recv_adisc_req(struct fc_rport_priv *rdata,
  931. struct fc_seq *sp, struct fc_frame *in_fp)
  932. {
  933. struct fc_lport *lport = rdata->local_port;
  934. struct fc_frame *fp;
  935. struct fc_exch *ep = fc_seq_exch(sp);
  936. struct fc_els_adisc *adisc;
  937. struct fc_seq_els_data rjt_data;
  938. u32 f_ctl;
  939. FC_RPORT_DBG(rdata, "Received ADISC request\n");
  940. adisc = fc_frame_payload_get(in_fp, sizeof(*adisc));
  941. if (!adisc) {
  942. rjt_data.fp = NULL;
  943. rjt_data.reason = ELS_RJT_PROT;
  944. rjt_data.explan = ELS_EXPL_INV_LEN;
  945. lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
  946. goto drop;
  947. }
  948. fp = fc_frame_alloc(lport, sizeof(*adisc));
  949. if (!fp)
  950. goto drop;
  951. fc_adisc_fill(lport, fp);
  952. adisc = fc_frame_payload_get(fp, sizeof(*adisc));
  953. adisc->adisc_cmd = ELS_LS_ACC;
  954. sp = lport->tt.seq_start_next(sp);
  955. f_ctl = FC_FC_EX_CTX | FC_FC_LAST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT;
  956. fc_fill_fc_hdr(fp, FC_RCTL_ELS_REP, ep->did, ep->sid,
  957. FC_TYPE_ELS, f_ctl, 0);
  958. lport->tt.seq_send(lport, sp, fp);
  959. drop:
  960. fc_frame_free(in_fp);
  961. }
  962. /**
  963. * fc_rport_recv_els_req() - Handler for validated ELS requests
  964. * @lport: The local port that received the ELS request
  965. * @sp: The sequence that the ELS request was on
  966. * @fp: The ELS request frame
  967. *
  968. * Handle incoming ELS requests that require port login.
  969. * The ELS opcode has already been validated by the caller.
  970. *
  971. * Locking Note: Called with the lport lock held.
  972. */
  973. static void fc_rport_recv_els_req(struct fc_lport *lport,
  974. struct fc_seq *sp, struct fc_frame *fp)
  975. {
  976. struct fc_rport_priv *rdata;
  977. struct fc_frame_header *fh;
  978. struct fc_seq_els_data els_data;
  979. els_data.fp = NULL;
  980. els_data.reason = ELS_RJT_UNAB;
  981. els_data.explan = ELS_EXPL_PLOGI_REQD;
  982. fh = fc_frame_header_get(fp);
  983. mutex_lock(&lport->disc.disc_mutex);
  984. rdata = lport->tt.rport_lookup(lport, ntoh24(fh->fh_s_id));
  985. if (!rdata) {
  986. mutex_unlock(&lport->disc.disc_mutex);
  987. goto reject;
  988. }
  989. mutex_lock(&rdata->rp_mutex);
  990. mutex_unlock(&lport->disc.disc_mutex);
  991. switch (rdata->rp_state) {
  992. case RPORT_ST_PRLI:
  993. case RPORT_ST_RTV:
  994. case RPORT_ST_READY:
  995. case RPORT_ST_ADISC:
  996. break;
  997. default:
  998. mutex_unlock(&rdata->rp_mutex);
  999. goto reject;
  1000. }
  1001. switch (fc_frame_payload_op(fp)) {
  1002. case ELS_PRLI:
  1003. fc_rport_recv_prli_req(rdata, sp, fp);
  1004. break;
  1005. case ELS_PRLO:
  1006. fc_rport_recv_prlo_req(rdata, sp, fp);
  1007. break;
  1008. case ELS_ADISC:
  1009. fc_rport_recv_adisc_req(rdata, sp, fp);
  1010. break;
  1011. case ELS_RRQ:
  1012. els_data.fp = fp;
  1013. lport->tt.seq_els_rsp_send(sp, ELS_RRQ, &els_data);
  1014. break;
  1015. case ELS_REC:
  1016. els_data.fp = fp;
  1017. lport->tt.seq_els_rsp_send(sp, ELS_REC, &els_data);
  1018. break;
  1019. default:
  1020. fc_frame_free(fp); /* can't happen */
  1021. break;
  1022. }
  1023. mutex_unlock(&rdata->rp_mutex);
  1024. return;
  1025. reject:
  1026. lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &els_data);
  1027. fc_frame_free(fp);
  1028. }
  1029. /**
  1030. * fc_rport_recv_req() - Handler for requests
  1031. * @sp: The sequence the request was on
  1032. * @fp: The request frame
  1033. * @lport: The local port that received the request
  1034. *
  1035. * Locking Note: Called with the lport lock held.
  1036. */
  1037. void fc_rport_recv_req(struct fc_seq *sp, struct fc_frame *fp,
  1038. struct fc_lport *lport)
  1039. {
  1040. struct fc_seq_els_data els_data;
  1041. /*
  1042. * Handle PLOGI and LOGO requests separately, since they
  1043. * don't require prior login.
  1044. * Check for unsupported opcodes first and reject them.
  1045. * For some ops, it would be incorrect to reject with "PLOGI required".
  1046. */
  1047. switch (fc_frame_payload_op(fp)) {
  1048. case ELS_PLOGI:
  1049. fc_rport_recv_plogi_req(lport, sp, fp);
  1050. break;
  1051. case ELS_LOGO:
  1052. fc_rport_recv_logo_req(lport, sp, fp);
  1053. break;
  1054. case ELS_PRLI:
  1055. case ELS_PRLO:
  1056. case ELS_ADISC:
  1057. case ELS_RRQ:
  1058. case ELS_REC:
  1059. fc_rport_recv_els_req(lport, sp, fp);
  1060. break;
  1061. default:
  1062. fc_frame_free(fp);
  1063. els_data.fp = NULL;
  1064. els_data.reason = ELS_RJT_UNSUP;
  1065. els_data.explan = ELS_EXPL_NONE;
  1066. lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &els_data);
  1067. break;
  1068. }
  1069. }
  1070. /**
  1071. * fc_rport_recv_plogi_req() - Handler for Port Login (PLOGI) requests
  1072. * @lport: The local port that received the PLOGI request
  1073. * @sp: The sequence that the PLOGI request was on
  1074. * @rx_fp: The PLOGI request frame
  1075. *
  1076. * Locking Note: The rport lock is held before calling this function.
  1077. */
  1078. static void fc_rport_recv_plogi_req(struct fc_lport *lport,
  1079. struct fc_seq *sp, struct fc_frame *rx_fp)
  1080. {
  1081. struct fc_disc *disc;
  1082. struct fc_rport_priv *rdata;
  1083. struct fc_frame *fp = rx_fp;
  1084. struct fc_exch *ep;
  1085. struct fc_frame_header *fh;
  1086. struct fc_els_flogi *pl;
  1087. struct fc_seq_els_data rjt_data;
  1088. u32 sid, f_ctl;
  1089. rjt_data.fp = NULL;
  1090. fh = fc_frame_header_get(fp);
  1091. sid = ntoh24(fh->fh_s_id);
  1092. FC_RPORT_ID_DBG(lport, sid, "Received PLOGI request\n");
  1093. pl = fc_frame_payload_get(fp, sizeof(*pl));
  1094. if (!pl) {
  1095. FC_RPORT_ID_DBG(lport, sid, "Received PLOGI too short\n");
  1096. rjt_data.reason = ELS_RJT_PROT;
  1097. rjt_data.explan = ELS_EXPL_INV_LEN;
  1098. goto reject;
  1099. }
  1100. disc = &lport->disc;
  1101. mutex_lock(&disc->disc_mutex);
  1102. rdata = lport->tt.rport_create(lport, sid);
  1103. if (!rdata) {
  1104. mutex_unlock(&disc->disc_mutex);
  1105. rjt_data.reason = ELS_RJT_UNAB;
  1106. rjt_data.explan = ELS_EXPL_INSUF_RES;
  1107. goto reject;
  1108. }
  1109. mutex_lock(&rdata->rp_mutex);
  1110. mutex_unlock(&disc->disc_mutex);
  1111. rdata->ids.port_name = get_unaligned_be64(&pl->fl_wwpn);
  1112. rdata->ids.node_name = get_unaligned_be64(&pl->fl_wwnn);
  1113. /*
  1114. * If the rport was just created, possibly due to the incoming PLOGI,
  1115. * set the state appropriately and accept the PLOGI.
  1116. *
  1117. * If we had also sent a PLOGI, and if the received PLOGI is from a
  1118. * higher WWPN, we accept it, otherwise an LS_RJT is sent with reason
  1119. * "command already in progress".
  1120. *
  1121. * XXX TBD: If the session was ready before, the PLOGI should result in
  1122. * all outstanding exchanges being reset.
  1123. */
  1124. switch (rdata->rp_state) {
  1125. case RPORT_ST_INIT:
  1126. FC_RPORT_DBG(rdata, "Received PLOGI in INIT state\n");
  1127. break;
  1128. case RPORT_ST_PLOGI:
  1129. FC_RPORT_DBG(rdata, "Received PLOGI in PLOGI state\n");
  1130. if (rdata->ids.port_name < lport->wwpn) {
  1131. mutex_unlock(&rdata->rp_mutex);
  1132. rjt_data.reason = ELS_RJT_INPROG;
  1133. rjt_data.explan = ELS_EXPL_NONE;
  1134. goto reject;
  1135. }
  1136. break;
  1137. case RPORT_ST_PRLI:
  1138. case RPORT_ST_RTV:
  1139. case RPORT_ST_READY:
  1140. case RPORT_ST_ADISC:
  1141. FC_RPORT_DBG(rdata, "Received PLOGI in logged-in state %d "
  1142. "- ignored for now\n", rdata->rp_state);
  1143. /* XXX TBD - should reset */
  1144. break;
  1145. case RPORT_ST_DELETE:
  1146. case RPORT_ST_LOGO:
  1147. case RPORT_ST_RESTART:
  1148. FC_RPORT_DBG(rdata, "Received PLOGI in state %s - send busy\n",
  1149. fc_rport_state(rdata));
  1150. mutex_unlock(&rdata->rp_mutex);
  1151. rjt_data.reason = ELS_RJT_BUSY;
  1152. rjt_data.explan = ELS_EXPL_NONE;
  1153. goto reject;
  1154. }
  1155. /*
  1156. * Get session payload size from incoming PLOGI.
  1157. */
  1158. rdata->maxframe_size = fc_plogi_get_maxframe(pl, lport->mfs);
  1159. fc_frame_free(rx_fp);
  1160. /*
  1161. * Send LS_ACC. If this fails, the originator should retry.
  1162. */
  1163. sp = lport->tt.seq_start_next(sp);
  1164. if (!sp)
  1165. goto out;
  1166. fp = fc_frame_alloc(lport, sizeof(*pl));
  1167. if (!fp)
  1168. goto out;
  1169. fc_plogi_fill(lport, fp, ELS_LS_ACC);
  1170. f_ctl = FC_FC_EX_CTX | FC_FC_LAST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT;
  1171. ep = fc_seq_exch(sp);
  1172. fc_fill_fc_hdr(fp, FC_RCTL_ELS_REP, ep->did, ep->sid,
  1173. FC_TYPE_ELS, f_ctl, 0);
  1174. lport->tt.seq_send(lport, sp, fp);
  1175. fc_rport_enter_prli(rdata);
  1176. out:
  1177. mutex_unlock(&rdata->rp_mutex);
  1178. return;
  1179. reject:
  1180. lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
  1181. fc_frame_free(fp);
  1182. }
  1183. /**
  1184. * fc_rport_recv_prli_req() - Handler for process login (PRLI) requests
  1185. * @rdata: The remote port that sent the PRLI request
  1186. * @sp: The sequence that the PRLI was on
  1187. * @rx_fp: The PRLI request frame
  1188. *
  1189. * Locking Note: The rport lock is exected to be held before calling
  1190. * this function.
  1191. */
  1192. static void fc_rport_recv_prli_req(struct fc_rport_priv *rdata,
  1193. struct fc_seq *sp, struct fc_frame *rx_fp)
  1194. {
  1195. struct fc_lport *lport = rdata->local_port;
  1196. struct fc_exch *ep;
  1197. struct fc_frame *fp;
  1198. struct fc_frame_header *fh;
  1199. struct {
  1200. struct fc_els_prli prli;
  1201. struct fc_els_spp spp;
  1202. } *pp;
  1203. struct fc_els_spp *rspp; /* request service param page */
  1204. struct fc_els_spp *spp; /* response spp */
  1205. unsigned int len;
  1206. unsigned int plen;
  1207. enum fc_els_rjt_reason reason = ELS_RJT_UNAB;
  1208. enum fc_els_rjt_explan explan = ELS_EXPL_NONE;
  1209. enum fc_els_spp_resp resp;
  1210. struct fc_seq_els_data rjt_data;
  1211. u32 f_ctl;
  1212. u32 fcp_parm;
  1213. u32 roles = FC_RPORT_ROLE_UNKNOWN;
  1214. rjt_data.fp = NULL;
  1215. fh = fc_frame_header_get(rx_fp);
  1216. FC_RPORT_DBG(rdata, "Received PRLI request while in state %s\n",
  1217. fc_rport_state(rdata));
  1218. switch (rdata->rp_state) {
  1219. case RPORT_ST_PRLI:
  1220. case RPORT_ST_RTV:
  1221. case RPORT_ST_READY:
  1222. case RPORT_ST_ADISC:
  1223. reason = ELS_RJT_NONE;
  1224. break;
  1225. default:
  1226. fc_frame_free(rx_fp);
  1227. return;
  1228. break;
  1229. }
  1230. len = fr_len(rx_fp) - sizeof(*fh);
  1231. pp = fc_frame_payload_get(rx_fp, sizeof(*pp));
  1232. if (pp == NULL) {
  1233. reason = ELS_RJT_PROT;
  1234. explan = ELS_EXPL_INV_LEN;
  1235. } else {
  1236. plen = ntohs(pp->prli.prli_len);
  1237. if ((plen % 4) != 0 || plen > len) {
  1238. reason = ELS_RJT_PROT;
  1239. explan = ELS_EXPL_INV_LEN;
  1240. } else if (plen < len) {
  1241. len = plen;
  1242. }
  1243. plen = pp->prli.prli_spp_len;
  1244. if ((plen % 4) != 0 || plen < sizeof(*spp) ||
  1245. plen > len || len < sizeof(*pp)) {
  1246. reason = ELS_RJT_PROT;
  1247. explan = ELS_EXPL_INV_LEN;
  1248. }
  1249. rspp = &pp->spp;
  1250. }
  1251. if (reason != ELS_RJT_NONE ||
  1252. (fp = fc_frame_alloc(lport, len)) == NULL) {
  1253. rjt_data.reason = reason;
  1254. rjt_data.explan = explan;
  1255. lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
  1256. } else {
  1257. sp = lport->tt.seq_start_next(sp);
  1258. WARN_ON(!sp);
  1259. pp = fc_frame_payload_get(fp, len);
  1260. WARN_ON(!pp);
  1261. memset(pp, 0, len);
  1262. pp->prli.prli_cmd = ELS_LS_ACC;
  1263. pp->prli.prli_spp_len = plen;
  1264. pp->prli.prli_len = htons(len);
  1265. len -= sizeof(struct fc_els_prli);
  1266. /* reinitialize remote port roles */
  1267. rdata->ids.roles = FC_RPORT_ROLE_UNKNOWN;
  1268. /*
  1269. * Go through all the service parameter pages and build
  1270. * response. If plen indicates longer SPP than standard,
  1271. * use that. The entire response has been pre-cleared above.
  1272. */
  1273. spp = &pp->spp;
  1274. while (len >= plen) {
  1275. spp->spp_type = rspp->spp_type;
  1276. spp->spp_type_ext = rspp->spp_type_ext;
  1277. spp->spp_flags = rspp->spp_flags & FC_SPP_EST_IMG_PAIR;
  1278. resp = FC_SPP_RESP_ACK;
  1279. if (rspp->spp_flags & FC_SPP_RPA_VAL)
  1280. resp = FC_SPP_RESP_NO_PA;
  1281. switch (rspp->spp_type) {
  1282. case 0: /* common to all FC-4 types */
  1283. break;
  1284. case FC_TYPE_FCP:
  1285. fcp_parm = ntohl(rspp->spp_params);
  1286. if (fcp_parm & FCP_SPPF_RETRY)
  1287. rdata->flags |= FC_RP_FLAGS_RETRY;
  1288. rdata->supported_classes = FC_COS_CLASS3;
  1289. if (fcp_parm & FCP_SPPF_INIT_FCN)
  1290. roles |= FC_RPORT_ROLE_FCP_INITIATOR;
  1291. if (fcp_parm & FCP_SPPF_TARG_FCN)
  1292. roles |= FC_RPORT_ROLE_FCP_TARGET;
  1293. rdata->ids.roles = roles;
  1294. spp->spp_params =
  1295. htonl(lport->service_params);
  1296. break;
  1297. default:
  1298. resp = FC_SPP_RESP_INVL;
  1299. break;
  1300. }
  1301. spp->spp_flags |= resp;
  1302. len -= plen;
  1303. rspp = (struct fc_els_spp *)((char *)rspp + plen);
  1304. spp = (struct fc_els_spp *)((char *)spp + plen);
  1305. }
  1306. /*
  1307. * Send LS_ACC. If this fails, the originator should retry.
  1308. */
  1309. f_ctl = FC_FC_EX_CTX | FC_FC_LAST_SEQ;
  1310. f_ctl |= FC_FC_END_SEQ | FC_FC_SEQ_INIT;
  1311. ep = fc_seq_exch(sp);
  1312. fc_fill_fc_hdr(fp, FC_RCTL_ELS_REP, ep->did, ep->sid,
  1313. FC_TYPE_ELS, f_ctl, 0);
  1314. lport->tt.seq_send(lport, sp, fp);
  1315. /*
  1316. * Get lock and re-check state.
  1317. */
  1318. switch (rdata->rp_state) {
  1319. case RPORT_ST_PRLI:
  1320. fc_rport_enter_ready(rdata);
  1321. break;
  1322. case RPORT_ST_READY:
  1323. case RPORT_ST_ADISC:
  1324. break;
  1325. default:
  1326. break;
  1327. }
  1328. }
  1329. fc_frame_free(rx_fp);
  1330. }
  1331. /**
  1332. * fc_rport_recv_prlo_req() - Handler for process logout (PRLO) requests
  1333. * @rdata: The remote port that sent the PRLO request
  1334. * @sp: The sequence that the PRLO was on
  1335. * @fp: The PRLO request frame
  1336. *
  1337. * Locking Note: The rport lock is exected to be held before calling
  1338. * this function.
  1339. */
  1340. static void fc_rport_recv_prlo_req(struct fc_rport_priv *rdata,
  1341. struct fc_seq *sp,
  1342. struct fc_frame *fp)
  1343. {
  1344. struct fc_lport *lport = rdata->local_port;
  1345. struct fc_frame_header *fh;
  1346. struct fc_seq_els_data rjt_data;
  1347. fh = fc_frame_header_get(fp);
  1348. FC_RPORT_DBG(rdata, "Received PRLO request while in state %s\n",
  1349. fc_rport_state(rdata));
  1350. rjt_data.fp = NULL;
  1351. rjt_data.reason = ELS_RJT_UNAB;
  1352. rjt_data.explan = ELS_EXPL_NONE;
  1353. lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
  1354. fc_frame_free(fp);
  1355. }
  1356. /**
  1357. * fc_rport_recv_logo_req() - Handler for logout (LOGO) requests
  1358. * @lport: The local port that received the LOGO request
  1359. * @sp: The sequence that the LOGO request was on
  1360. * @fp: The LOGO request frame
  1361. *
  1362. * Locking Note: The rport lock is exected to be held before calling
  1363. * this function.
  1364. */
  1365. static void fc_rport_recv_logo_req(struct fc_lport *lport,
  1366. struct fc_seq *sp,
  1367. struct fc_frame *fp)
  1368. {
  1369. struct fc_frame_header *fh;
  1370. struct fc_rport_priv *rdata;
  1371. u32 sid;
  1372. lport->tt.seq_els_rsp_send(sp, ELS_LS_ACC, NULL);
  1373. fh = fc_frame_header_get(fp);
  1374. sid = ntoh24(fh->fh_s_id);
  1375. mutex_lock(&lport->disc.disc_mutex);
  1376. rdata = lport->tt.rport_lookup(lport, sid);
  1377. if (rdata) {
  1378. mutex_lock(&rdata->rp_mutex);
  1379. FC_RPORT_DBG(rdata, "Received LOGO request while in state %s\n",
  1380. fc_rport_state(rdata));
  1381. fc_rport_enter_delete(rdata, RPORT_EV_LOGO);
  1382. /*
  1383. * If the remote port was created due to discovery, set state
  1384. * to log back in. It may have seen a stale RSCN about us.
  1385. */
  1386. if (rdata->disc_id)
  1387. fc_rport_state_enter(rdata, RPORT_ST_RESTART);
  1388. mutex_unlock(&rdata->rp_mutex);
  1389. } else
  1390. FC_RPORT_ID_DBG(lport, sid,
  1391. "Received LOGO from non-logged-in port\n");
  1392. mutex_unlock(&lport->disc.disc_mutex);
  1393. fc_frame_free(fp);
  1394. }
  1395. /**
  1396. * fc_rport_flush_queue() - Flush the rport_event_queue
  1397. */
  1398. static void fc_rport_flush_queue(void)
  1399. {
  1400. flush_workqueue(rport_event_queue);
  1401. }
  1402. /**
  1403. * fc_rport_init() - Initialize the remote port layer for a local port
  1404. * @lport: The local port to initialize the remote port layer for
  1405. */
  1406. int fc_rport_init(struct fc_lport *lport)
  1407. {
  1408. if (!lport->tt.rport_lookup)
  1409. lport->tt.rport_lookup = fc_rport_lookup;
  1410. if (!lport->tt.rport_create)
  1411. lport->tt.rport_create = fc_rport_create;
  1412. if (!lport->tt.rport_login)
  1413. lport->tt.rport_login = fc_rport_login;
  1414. if (!lport->tt.rport_logoff)
  1415. lport->tt.rport_logoff = fc_rport_logoff;
  1416. if (!lport->tt.rport_recv_req)
  1417. lport->tt.rport_recv_req = fc_rport_recv_req;
  1418. if (!lport->tt.rport_flush_queue)
  1419. lport->tt.rport_flush_queue = fc_rport_flush_queue;
  1420. if (!lport->tt.rport_destroy)
  1421. lport->tt.rport_destroy = fc_rport_destroy;
  1422. return 0;
  1423. }
  1424. EXPORT_SYMBOL(fc_rport_init);
  1425. /**
  1426. * fc_setup_rport() - Initialize the rport_event_queue
  1427. */
  1428. int fc_setup_rport()
  1429. {
  1430. rport_event_queue = create_singlethread_workqueue("fc_rport_eq");
  1431. if (!rport_event_queue)
  1432. return -ENOMEM;
  1433. return 0;
  1434. }
  1435. /**
  1436. * fc_destroy_rport() - Destroy the rport_event_queue
  1437. */
  1438. void fc_destroy_rport()
  1439. {
  1440. destroy_workqueue(rport_event_queue);
  1441. }
  1442. /**
  1443. * fc_rport_terminate_io() - Stop all outstanding I/O on a remote port
  1444. * @rport: The remote port whose I/O should be terminated
  1445. */
  1446. void fc_rport_terminate_io(struct fc_rport *rport)
  1447. {
  1448. struct fc_rport_libfc_priv *rpriv = rport->dd_data;
  1449. struct fc_lport *lport = rpriv->local_port;
  1450. lport->tt.exch_mgr_reset(lport, 0, rport->port_id);
  1451. lport->tt.exch_mgr_reset(lport, rport->port_id, 0);
  1452. }
  1453. EXPORT_SYMBOL(fc_rport_terminate_io);