fc_rport.c 42 KB

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