fc_rport.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340
  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_recv_plogi_req(struct fc_rport_priv *,
  60. struct fc_seq *, struct fc_frame *);
  61. static void fc_rport_recv_prli_req(struct fc_rport_priv *,
  62. struct fc_seq *, struct fc_frame *);
  63. static void fc_rport_recv_prlo_req(struct fc_rport_priv *,
  64. struct fc_seq *, struct fc_frame *);
  65. static void fc_rport_recv_logo_req(struct fc_rport_priv *,
  66. struct fc_seq *, struct fc_frame *);
  67. static void fc_rport_timeout(struct work_struct *);
  68. static void fc_rport_error(struct fc_rport_priv *, struct fc_frame *);
  69. static void fc_rport_error_retry(struct fc_rport_priv *, struct fc_frame *);
  70. static void fc_rport_work(struct work_struct *);
  71. static const char *fc_rport_state_names[] = {
  72. [RPORT_ST_INIT] = "Init",
  73. [RPORT_ST_PLOGI] = "PLOGI",
  74. [RPORT_ST_PRLI] = "PRLI",
  75. [RPORT_ST_RTV] = "RTV",
  76. [RPORT_ST_READY] = "Ready",
  77. [RPORT_ST_LOGO] = "LOGO",
  78. [RPORT_ST_DELETE] = "Delete",
  79. };
  80. static void fc_rport_rogue_destroy(struct device *dev)
  81. {
  82. struct fc_rport *rport = dev_to_rport(dev);
  83. struct fc_rport_priv *rdata = RPORT_TO_PRIV(rport);
  84. FC_RPORT_DBG(rdata, "Destroying rogue rport\n");
  85. kfree(rport);
  86. }
  87. struct fc_rport_priv *fc_rport_rogue_create(struct fc_lport *lport,
  88. struct fc_rport_identifiers *ids)
  89. {
  90. struct fc_rport *rport;
  91. struct fc_rport_priv *rdata;
  92. rport = kzalloc(sizeof(*rport) + sizeof(*rdata), GFP_KERNEL);
  93. if (!rport)
  94. return NULL;
  95. rdata = RPORT_TO_PRIV(rport);
  96. rport->dd_data = rdata;
  97. rport->port_id = ids->port_id;
  98. rport->port_name = ids->port_name;
  99. rport->node_name = ids->node_name;
  100. rport->roles = ids->roles;
  101. rport->maxframe_size = FC_MIN_MAX_PAYLOAD;
  102. /*
  103. * Note: all this libfc rogue rport code will be removed for
  104. * upstream so it fine that this is really ugly and hacky right now.
  105. */
  106. device_initialize(&rport->dev);
  107. rport->dev.release = fc_rport_rogue_destroy;
  108. mutex_init(&rdata->rp_mutex);
  109. rdata->local_port = lport;
  110. rdata->trans_state = FC_PORTSTATE_ROGUE;
  111. rdata->rp_state = RPORT_ST_INIT;
  112. rdata->event = RPORT_EV_NONE;
  113. rdata->flags = FC_RP_FLAGS_REC_SUPPORTED;
  114. rdata->ops = NULL;
  115. rdata->e_d_tov = lport->e_d_tov;
  116. rdata->r_a_tov = lport->r_a_tov;
  117. INIT_DELAYED_WORK(&rdata->retry_work, fc_rport_timeout);
  118. INIT_WORK(&rdata->event_work, fc_rport_work);
  119. /*
  120. * For good measure, but not necessary as we should only
  121. * add REAL rport to the lport list.
  122. */
  123. INIT_LIST_HEAD(&rdata->peers);
  124. return rdata;
  125. }
  126. /**
  127. * fc_rport_state() - return a string for the state the rport is in
  128. * @rdata: remote port private data
  129. */
  130. static const char *fc_rport_state(struct fc_rport_priv *rdata)
  131. {
  132. const char *cp;
  133. cp = fc_rport_state_names[rdata->rp_state];
  134. if (!cp)
  135. cp = "Unknown";
  136. return cp;
  137. }
  138. /**
  139. * fc_set_rport_loss_tmo() - Set the remote port loss timeout in seconds.
  140. * @rport: Pointer to Fibre Channel remote port structure
  141. * @timeout: timeout in seconds
  142. */
  143. void fc_set_rport_loss_tmo(struct fc_rport *rport, u32 timeout)
  144. {
  145. if (timeout)
  146. rport->dev_loss_tmo = timeout + 5;
  147. else
  148. rport->dev_loss_tmo = 30;
  149. }
  150. EXPORT_SYMBOL(fc_set_rport_loss_tmo);
  151. /**
  152. * fc_plogi_get_maxframe() - Get max payload from the common service parameters
  153. * @flp: FLOGI payload structure
  154. * @maxval: upper limit, may be less than what is in the service parameters
  155. */
  156. static unsigned int fc_plogi_get_maxframe(struct fc_els_flogi *flp,
  157. unsigned int maxval)
  158. {
  159. unsigned int mfs;
  160. /*
  161. * Get max payload from the common service parameters and the
  162. * class 3 receive data field size.
  163. */
  164. mfs = ntohs(flp->fl_csp.sp_bb_data) & FC_SP_BB_DATA_MASK;
  165. if (mfs >= FC_SP_MIN_MAX_PAYLOAD && mfs < maxval)
  166. maxval = mfs;
  167. mfs = ntohs(flp->fl_cssp[3 - 1].cp_rdfs);
  168. if (mfs >= FC_SP_MIN_MAX_PAYLOAD && mfs < maxval)
  169. maxval = mfs;
  170. return maxval;
  171. }
  172. /**
  173. * fc_rport_state_enter() - Change the rport's state
  174. * @rdata: The rport whose state should change
  175. * @new: The new state of the rport
  176. *
  177. * Locking Note: Called with the rport lock held
  178. */
  179. static void fc_rport_state_enter(struct fc_rport_priv *rdata,
  180. enum fc_rport_state new)
  181. {
  182. if (rdata->rp_state != new)
  183. rdata->retries = 0;
  184. rdata->rp_state = new;
  185. }
  186. static void fc_rport_work(struct work_struct *work)
  187. {
  188. u32 port_id;
  189. struct fc_rport_priv *rdata =
  190. container_of(work, struct fc_rport_priv, event_work);
  191. enum fc_rport_event event;
  192. enum fc_rport_trans_state trans_state;
  193. struct fc_lport *lport = rdata->local_port;
  194. struct fc_rport_operations *rport_ops;
  195. struct fc_rport *rport = PRIV_TO_RPORT(rdata);
  196. mutex_lock(&rdata->rp_mutex);
  197. event = rdata->event;
  198. rport_ops = rdata->ops;
  199. if (event == RPORT_EV_CREATED) {
  200. struct fc_rport *new_rport;
  201. struct fc_rport_priv *new_rdata;
  202. struct fc_rport_identifiers ids;
  203. ids.port_id = rport->port_id;
  204. ids.roles = rport->roles;
  205. ids.port_name = rport->port_name;
  206. ids.node_name = rport->node_name;
  207. rdata->event = RPORT_EV_NONE;
  208. mutex_unlock(&rdata->rp_mutex);
  209. new_rport = fc_remote_port_add(lport->host, 0, &ids);
  210. if (new_rport) {
  211. /*
  212. * Switch from the rogue rport to the rport
  213. * returned by the FC class.
  214. */
  215. new_rport->maxframe_size = rport->maxframe_size;
  216. new_rdata = new_rport->dd_data;
  217. new_rdata->e_d_tov = rdata->e_d_tov;
  218. new_rdata->r_a_tov = rdata->r_a_tov;
  219. new_rdata->ops = rdata->ops;
  220. new_rdata->local_port = rdata->local_port;
  221. new_rdata->flags = FC_RP_FLAGS_REC_SUPPORTED;
  222. new_rdata->trans_state = FC_PORTSTATE_REAL;
  223. mutex_init(&new_rdata->rp_mutex);
  224. INIT_DELAYED_WORK(&new_rdata->retry_work,
  225. fc_rport_timeout);
  226. INIT_LIST_HEAD(&new_rdata->peers);
  227. INIT_WORK(&new_rdata->event_work, fc_rport_work);
  228. fc_rport_state_enter(new_rdata, RPORT_ST_READY);
  229. } else {
  230. printk(KERN_WARNING "libfc: Failed to allocate "
  231. " memory for rport (%6x)\n", ids.port_id);
  232. event = RPORT_EV_FAILED;
  233. }
  234. if (rport->port_id != FC_FID_DIR_SERV)
  235. if (rport_ops->event_callback)
  236. rport_ops->event_callback(lport, rdata,
  237. RPORT_EV_FAILED);
  238. put_device(&rport->dev);
  239. rport = new_rport;
  240. rdata = new_rport->dd_data;
  241. if (rport_ops->event_callback)
  242. rport_ops->event_callback(lport, rdata, event);
  243. } else if ((event == RPORT_EV_FAILED) ||
  244. (event == RPORT_EV_LOGO) ||
  245. (event == RPORT_EV_STOP)) {
  246. trans_state = rdata->trans_state;
  247. mutex_unlock(&rdata->rp_mutex);
  248. if (rport_ops->event_callback)
  249. rport_ops->event_callback(lport, rdata, event);
  250. cancel_delayed_work_sync(&rdata->retry_work);
  251. if (trans_state == FC_PORTSTATE_ROGUE)
  252. put_device(&rport->dev);
  253. else {
  254. port_id = rport->port_id;
  255. fc_remote_port_delete(rport);
  256. lport->tt.exch_mgr_reset(lport, 0, port_id);
  257. lport->tt.exch_mgr_reset(lport, port_id, 0);
  258. }
  259. } else
  260. mutex_unlock(&rdata->rp_mutex);
  261. }
  262. /**
  263. * fc_rport_login() - Start the remote port login state machine
  264. * @rdata: private remote port
  265. *
  266. * Locking Note: Called without the rport lock held. This
  267. * function will hold the rport lock, call an _enter_*
  268. * function and then unlock the rport.
  269. */
  270. int fc_rport_login(struct fc_rport_priv *rdata)
  271. {
  272. mutex_lock(&rdata->rp_mutex);
  273. FC_RPORT_DBG(rdata, "Login to port\n");
  274. fc_rport_enter_plogi(rdata);
  275. mutex_unlock(&rdata->rp_mutex);
  276. return 0;
  277. }
  278. /**
  279. * fc_rport_enter_delete() - schedule a remote port to be deleted.
  280. * @rdata: private remote port
  281. * @event: event to report as the reason for deletion
  282. *
  283. * Locking Note: Called with the rport lock held.
  284. *
  285. * Allow state change into DELETE only once.
  286. *
  287. * Call queue_work only if there's no event already pending.
  288. * Set the new event so that the old pending event will not occur.
  289. * Since we have the mutex, even if fc_rport_work() is already started,
  290. * it'll see the new event.
  291. */
  292. static void fc_rport_enter_delete(struct fc_rport_priv *rdata,
  293. enum fc_rport_event event)
  294. {
  295. if (rdata->rp_state == RPORT_ST_DELETE)
  296. return;
  297. FC_RPORT_DBG(rdata, "Delete port\n");
  298. fc_rport_state_enter(rdata, RPORT_ST_DELETE);
  299. if (rdata->event == RPORT_EV_NONE)
  300. queue_work(rport_event_queue, &rdata->event_work);
  301. rdata->event = event;
  302. }
  303. /**
  304. * fc_rport_logoff() - Logoff and remove an rport
  305. * @rdata: private remote port
  306. *
  307. * Locking Note: Called without the rport lock held. This
  308. * function will hold the rport lock, call an _enter_*
  309. * function and then unlock the rport.
  310. */
  311. int fc_rport_logoff(struct fc_rport_priv *rdata)
  312. {
  313. mutex_lock(&rdata->rp_mutex);
  314. FC_RPORT_DBG(rdata, "Remove port\n");
  315. if (rdata->rp_state == RPORT_ST_DELETE) {
  316. FC_RPORT_DBG(rdata, "Port in Delete state, not removing\n");
  317. mutex_unlock(&rdata->rp_mutex);
  318. goto out;
  319. }
  320. fc_rport_enter_logo(rdata);
  321. /*
  322. * Change the state to Delete so that we discard
  323. * the response.
  324. */
  325. fc_rport_enter_delete(rdata, RPORT_EV_STOP);
  326. mutex_unlock(&rdata->rp_mutex);
  327. out:
  328. return 0;
  329. }
  330. /**
  331. * fc_rport_enter_ready() - The rport is ready
  332. * @rdata: private remote port
  333. *
  334. * Locking Note: The rport lock is expected to be held before calling
  335. * this routine.
  336. */
  337. static void fc_rport_enter_ready(struct fc_rport_priv *rdata)
  338. {
  339. fc_rport_state_enter(rdata, RPORT_ST_READY);
  340. FC_RPORT_DBG(rdata, "Port is Ready\n");
  341. if (rdata->event == RPORT_EV_NONE)
  342. queue_work(rport_event_queue, &rdata->event_work);
  343. rdata->event = RPORT_EV_CREATED;
  344. }
  345. /**
  346. * fc_rport_timeout() - Handler for the retry_work timer.
  347. * @work: The work struct of the fc_rport_priv
  348. *
  349. * Locking Note: Called without the rport lock held. This
  350. * function will hold the rport lock, call an _enter_*
  351. * function and then unlock the rport.
  352. */
  353. static void fc_rport_timeout(struct work_struct *work)
  354. {
  355. struct fc_rport_priv *rdata =
  356. container_of(work, struct fc_rport_priv, retry_work.work);
  357. mutex_lock(&rdata->rp_mutex);
  358. switch (rdata->rp_state) {
  359. case RPORT_ST_PLOGI:
  360. fc_rport_enter_plogi(rdata);
  361. break;
  362. case RPORT_ST_PRLI:
  363. fc_rport_enter_prli(rdata);
  364. break;
  365. case RPORT_ST_RTV:
  366. fc_rport_enter_rtv(rdata);
  367. break;
  368. case RPORT_ST_LOGO:
  369. fc_rport_enter_logo(rdata);
  370. break;
  371. case RPORT_ST_READY:
  372. case RPORT_ST_INIT:
  373. case RPORT_ST_DELETE:
  374. break;
  375. }
  376. mutex_unlock(&rdata->rp_mutex);
  377. }
  378. /**
  379. * fc_rport_error() - Error handler, called once retries have been exhausted
  380. * @rdata: private remote port
  381. * @fp: The frame pointer
  382. *
  383. * Locking Note: The rport lock is expected to be held before
  384. * calling this routine
  385. */
  386. static void fc_rport_error(struct fc_rport_priv *rdata, struct fc_frame *fp)
  387. {
  388. FC_RPORT_DBG(rdata, "Error %ld in state %s, retries %d\n",
  389. PTR_ERR(fp), fc_rport_state(rdata), rdata->retries);
  390. switch (rdata->rp_state) {
  391. case RPORT_ST_PLOGI:
  392. case RPORT_ST_PRLI:
  393. case RPORT_ST_LOGO:
  394. fc_rport_enter_delete(rdata, RPORT_EV_FAILED);
  395. break;
  396. case RPORT_ST_RTV:
  397. fc_rport_enter_ready(rdata);
  398. break;
  399. case RPORT_ST_DELETE:
  400. case RPORT_ST_READY:
  401. case RPORT_ST_INIT:
  402. break;
  403. }
  404. }
  405. /**
  406. * fc_rport_error_retry() - Error handler when retries are desired
  407. * @rdata: private remote port data
  408. * @fp: The frame pointer
  409. *
  410. * If the error was an exchange timeout retry immediately,
  411. * otherwise wait for E_D_TOV.
  412. *
  413. * Locking Note: The rport lock is expected to be held before
  414. * calling this routine
  415. */
  416. static void fc_rport_error_retry(struct fc_rport_priv *rdata,
  417. struct fc_frame *fp)
  418. {
  419. unsigned long delay = FC_DEF_E_D_TOV;
  420. /* make sure this isn't an FC_EX_CLOSED error, never retry those */
  421. if (PTR_ERR(fp) == -FC_EX_CLOSED)
  422. return fc_rport_error(rdata, fp);
  423. if (rdata->retries < rdata->local_port->max_rport_retry_count) {
  424. FC_RPORT_DBG(rdata, "Error %ld in state %s, retrying\n",
  425. PTR_ERR(fp), fc_rport_state(rdata));
  426. rdata->retries++;
  427. /* no additional delay on exchange timeouts */
  428. if (PTR_ERR(fp) == -FC_EX_TIMEOUT)
  429. delay = 0;
  430. schedule_delayed_work(&rdata->retry_work, delay);
  431. return;
  432. }
  433. return fc_rport_error(rdata, fp);
  434. }
  435. /**
  436. * fc_rport_plogi_recv_resp() - Handle incoming ELS PLOGI response
  437. * @sp: current sequence in the PLOGI exchange
  438. * @fp: response frame
  439. * @rdata_arg: private remote port data
  440. *
  441. * Locking Note: This function will be called without the rport lock
  442. * held, but it will lock, call an _enter_* function or fc_rport_error
  443. * and then unlock the rport.
  444. */
  445. static void fc_rport_plogi_resp(struct fc_seq *sp, struct fc_frame *fp,
  446. void *rdata_arg)
  447. {
  448. struct fc_rport_priv *rdata = rdata_arg;
  449. struct fc_rport *rport = PRIV_TO_RPORT(rdata);
  450. struct fc_lport *lport = rdata->local_port;
  451. struct fc_els_flogi *plp = NULL;
  452. unsigned int tov;
  453. u16 csp_seq;
  454. u16 cssp_seq;
  455. u8 op;
  456. mutex_lock(&rdata->rp_mutex);
  457. FC_RPORT_DBG(rdata, "Received a PLOGI response\n");
  458. if (rdata->rp_state != RPORT_ST_PLOGI) {
  459. FC_RPORT_DBG(rdata, "Received a PLOGI response, but in state "
  460. "%s\n", fc_rport_state(rdata));
  461. if (IS_ERR(fp))
  462. goto err;
  463. goto out;
  464. }
  465. if (IS_ERR(fp)) {
  466. fc_rport_error_retry(rdata, fp);
  467. goto err;
  468. }
  469. op = fc_frame_payload_op(fp);
  470. if (op == ELS_LS_ACC &&
  471. (plp = fc_frame_payload_get(fp, sizeof(*plp))) != NULL) {
  472. rport->port_name = get_unaligned_be64(&plp->fl_wwpn);
  473. rport->node_name = get_unaligned_be64(&plp->fl_wwnn);
  474. tov = ntohl(plp->fl_csp.sp_e_d_tov);
  475. if (ntohs(plp->fl_csp.sp_features) & FC_SP_FT_EDTR)
  476. tov /= 1000;
  477. if (tov > rdata->e_d_tov)
  478. rdata->e_d_tov = tov;
  479. csp_seq = ntohs(plp->fl_csp.sp_tot_seq);
  480. cssp_seq = ntohs(plp->fl_cssp[3 - 1].cp_con_seq);
  481. if (cssp_seq < csp_seq)
  482. csp_seq = cssp_seq;
  483. rdata->max_seq = csp_seq;
  484. rport->maxframe_size =
  485. fc_plogi_get_maxframe(plp, lport->mfs);
  486. /*
  487. * If the rport is one of the well known addresses
  488. * we skip PRLI and RTV and go straight to READY.
  489. */
  490. if (rport->port_id >= FC_FID_DOM_MGR)
  491. fc_rport_enter_ready(rdata);
  492. else
  493. fc_rport_enter_prli(rdata);
  494. } else
  495. fc_rport_error_retry(rdata, fp);
  496. out:
  497. fc_frame_free(fp);
  498. err:
  499. mutex_unlock(&rdata->rp_mutex);
  500. put_device(&rport->dev);
  501. }
  502. /**
  503. * fc_rport_enter_plogi() - Send Port Login (PLOGI) request to peer
  504. * @rdata: private remote port data
  505. *
  506. * Locking Note: The rport lock is expected to be held before calling
  507. * this routine.
  508. */
  509. static void fc_rport_enter_plogi(struct fc_rport_priv *rdata)
  510. {
  511. struct fc_lport *lport = rdata->local_port;
  512. struct fc_rport *rport = PRIV_TO_RPORT(rdata);
  513. struct fc_frame *fp;
  514. FC_RPORT_DBG(rdata, "Port entered PLOGI state from %s state\n",
  515. fc_rport_state(rdata));
  516. fc_rport_state_enter(rdata, RPORT_ST_PLOGI);
  517. rport->maxframe_size = FC_MIN_MAX_PAYLOAD;
  518. fp = fc_frame_alloc(lport, sizeof(struct fc_els_flogi));
  519. if (!fp) {
  520. fc_rport_error_retry(rdata, fp);
  521. return;
  522. }
  523. rdata->e_d_tov = lport->e_d_tov;
  524. if (!lport->tt.elsct_send(lport, rport->port_id, fp, ELS_PLOGI,
  525. fc_rport_plogi_resp, rdata, lport->e_d_tov))
  526. fc_rport_error_retry(rdata, fp);
  527. else
  528. get_device(&rport->dev);
  529. }
  530. /**
  531. * fc_rport_prli_resp() - Process Login (PRLI) response handler
  532. * @sp: current sequence in the PRLI exchange
  533. * @fp: response frame
  534. * @rdata_arg: private remote port data
  535. *
  536. * Locking Note: This function will be called without the rport lock
  537. * held, but it will lock, call an _enter_* function or fc_rport_error
  538. * and then unlock the rport.
  539. */
  540. static void fc_rport_prli_resp(struct fc_seq *sp, struct fc_frame *fp,
  541. void *rdata_arg)
  542. {
  543. struct fc_rport_priv *rdata = rdata_arg;
  544. struct fc_rport *rport = PRIV_TO_RPORT(rdata);
  545. struct {
  546. struct fc_els_prli prli;
  547. struct fc_els_spp spp;
  548. } *pp;
  549. u32 roles = FC_RPORT_ROLE_UNKNOWN;
  550. u32 fcp_parm = 0;
  551. u8 op;
  552. mutex_lock(&rdata->rp_mutex);
  553. FC_RPORT_DBG(rdata, "Received a PRLI response\n");
  554. if (rdata->rp_state != RPORT_ST_PRLI) {
  555. FC_RPORT_DBG(rdata, "Received a PRLI response, but in state "
  556. "%s\n", fc_rport_state(rdata));
  557. if (IS_ERR(fp))
  558. goto err;
  559. goto out;
  560. }
  561. if (IS_ERR(fp)) {
  562. fc_rport_error_retry(rdata, fp);
  563. goto err;
  564. }
  565. op = fc_frame_payload_op(fp);
  566. if (op == ELS_LS_ACC) {
  567. pp = fc_frame_payload_get(fp, sizeof(*pp));
  568. if (pp && pp->prli.prli_spp_len >= sizeof(pp->spp)) {
  569. fcp_parm = ntohl(pp->spp.spp_params);
  570. if (fcp_parm & FCP_SPPF_RETRY)
  571. rdata->flags |= FC_RP_FLAGS_RETRY;
  572. }
  573. rport->supported_classes = FC_COS_CLASS3;
  574. if (fcp_parm & FCP_SPPF_INIT_FCN)
  575. roles |= FC_RPORT_ROLE_FCP_INITIATOR;
  576. if (fcp_parm & FCP_SPPF_TARG_FCN)
  577. roles |= FC_RPORT_ROLE_FCP_TARGET;
  578. rport->roles = roles;
  579. fc_rport_enter_rtv(rdata);
  580. } else {
  581. FC_RPORT_DBG(rdata, "Bad ELS response for PRLI command\n");
  582. fc_rport_enter_delete(rdata, RPORT_EV_FAILED);
  583. }
  584. out:
  585. fc_frame_free(fp);
  586. err:
  587. mutex_unlock(&rdata->rp_mutex);
  588. put_device(&rport->dev);
  589. }
  590. /**
  591. * fc_rport_logo_resp() - Logout (LOGO) response handler
  592. * @sp: current sequence in the LOGO exchange
  593. * @fp: response frame
  594. * @rdata_arg: private remote port data
  595. *
  596. * Locking Note: This function will be called without the rport lock
  597. * held, but it will lock, call an _enter_* function or fc_rport_error
  598. * and then unlock the rport.
  599. */
  600. static void fc_rport_logo_resp(struct fc_seq *sp, struct fc_frame *fp,
  601. void *rdata_arg)
  602. {
  603. struct fc_rport_priv *rdata = rdata_arg;
  604. struct fc_rport *rport = PRIV_TO_RPORT(rdata);
  605. u8 op;
  606. mutex_lock(&rdata->rp_mutex);
  607. FC_RPORT_DBG(rdata, "Received a LOGO response\n");
  608. if (rdata->rp_state != RPORT_ST_LOGO) {
  609. FC_RPORT_DBG(rdata, "Received a LOGO response, but in state "
  610. "%s\n", fc_rport_state(rdata));
  611. if (IS_ERR(fp))
  612. goto err;
  613. goto out;
  614. }
  615. if (IS_ERR(fp)) {
  616. fc_rport_error_retry(rdata, fp);
  617. goto err;
  618. }
  619. op = fc_frame_payload_op(fp);
  620. if (op == ELS_LS_ACC) {
  621. fc_rport_enter_rtv(rdata);
  622. } else {
  623. FC_RPORT_DBG(rdata, "Bad ELS response for LOGO command\n");
  624. fc_rport_enter_delete(rdata, RPORT_EV_LOGO);
  625. }
  626. out:
  627. fc_frame_free(fp);
  628. err:
  629. mutex_unlock(&rdata->rp_mutex);
  630. put_device(&rport->dev);
  631. }
  632. /**
  633. * fc_rport_enter_prli() - Send Process Login (PRLI) request to peer
  634. * @rdata: private remote port data
  635. *
  636. * Locking Note: The rport lock is expected to be held before calling
  637. * this routine.
  638. */
  639. static void fc_rport_enter_prli(struct fc_rport_priv *rdata)
  640. {
  641. struct fc_rport *rport = PRIV_TO_RPORT(rdata);
  642. struct fc_lport *lport = rdata->local_port;
  643. struct {
  644. struct fc_els_prli prli;
  645. struct fc_els_spp spp;
  646. } *pp;
  647. struct fc_frame *fp;
  648. FC_RPORT_DBG(rdata, "Port entered PRLI state from %s state\n",
  649. fc_rport_state(rdata));
  650. fc_rport_state_enter(rdata, RPORT_ST_PRLI);
  651. fp = fc_frame_alloc(lport, sizeof(*pp));
  652. if (!fp) {
  653. fc_rport_error_retry(rdata, fp);
  654. return;
  655. }
  656. if (!lport->tt.elsct_send(lport, rport->port_id, fp, ELS_PRLI,
  657. fc_rport_prli_resp, rdata, lport->e_d_tov))
  658. fc_rport_error_retry(rdata, fp);
  659. else
  660. get_device(&rport->dev);
  661. }
  662. /**
  663. * fc_rport_els_rtv_resp() - Request Timeout Value response handler
  664. * @sp: current sequence in the RTV exchange
  665. * @fp: response frame
  666. * @rdata_arg: private remote port data
  667. *
  668. * Many targets don't seem to support this.
  669. *
  670. * Locking Note: This function will be called without the rport lock
  671. * held, but it will lock, call an _enter_* function or fc_rport_error
  672. * and then unlock the rport.
  673. */
  674. static void fc_rport_rtv_resp(struct fc_seq *sp, struct fc_frame *fp,
  675. void *rdata_arg)
  676. {
  677. struct fc_rport_priv *rdata = rdata_arg;
  678. struct fc_rport *rport = PRIV_TO_RPORT(rdata);
  679. u8 op;
  680. mutex_lock(&rdata->rp_mutex);
  681. FC_RPORT_DBG(rdata, "Received a RTV response\n");
  682. if (rdata->rp_state != RPORT_ST_RTV) {
  683. FC_RPORT_DBG(rdata, "Received a RTV response, but in state "
  684. "%s\n", fc_rport_state(rdata));
  685. if (IS_ERR(fp))
  686. goto err;
  687. goto out;
  688. }
  689. if (IS_ERR(fp)) {
  690. fc_rport_error(rdata, fp);
  691. goto err;
  692. }
  693. op = fc_frame_payload_op(fp);
  694. if (op == ELS_LS_ACC) {
  695. struct fc_els_rtv_acc *rtv;
  696. u32 toq;
  697. u32 tov;
  698. rtv = fc_frame_payload_get(fp, sizeof(*rtv));
  699. if (rtv) {
  700. toq = ntohl(rtv->rtv_toq);
  701. tov = ntohl(rtv->rtv_r_a_tov);
  702. if (tov == 0)
  703. tov = 1;
  704. rdata->r_a_tov = tov;
  705. tov = ntohl(rtv->rtv_e_d_tov);
  706. if (toq & FC_ELS_RTV_EDRES)
  707. tov /= 1000000;
  708. if (tov == 0)
  709. tov = 1;
  710. rdata->e_d_tov = tov;
  711. }
  712. }
  713. fc_rport_enter_ready(rdata);
  714. out:
  715. fc_frame_free(fp);
  716. err:
  717. mutex_unlock(&rdata->rp_mutex);
  718. put_device(&rport->dev);
  719. }
  720. /**
  721. * fc_rport_enter_rtv() - Send Request Timeout Value (RTV) request to peer
  722. * @rdata: private remote port data
  723. *
  724. * Locking Note: The rport lock is expected to be held before calling
  725. * this routine.
  726. */
  727. static void fc_rport_enter_rtv(struct fc_rport_priv *rdata)
  728. {
  729. struct fc_frame *fp;
  730. struct fc_lport *lport = rdata->local_port;
  731. struct fc_rport *rport = PRIV_TO_RPORT(rdata);
  732. FC_RPORT_DBG(rdata, "Port entered RTV state from %s state\n",
  733. fc_rport_state(rdata));
  734. fc_rport_state_enter(rdata, RPORT_ST_RTV);
  735. fp = fc_frame_alloc(lport, sizeof(struct fc_els_rtv));
  736. if (!fp) {
  737. fc_rport_error_retry(rdata, fp);
  738. return;
  739. }
  740. if (!lport->tt.elsct_send(lport, rport->port_id, fp, ELS_RTV,
  741. fc_rport_rtv_resp, rdata, lport->e_d_tov))
  742. fc_rport_error_retry(rdata, fp);
  743. else
  744. get_device(&rport->dev);
  745. }
  746. /**
  747. * fc_rport_enter_logo() - Send Logout (LOGO) request to peer
  748. * @rdata: private remote port data
  749. *
  750. * Locking Note: The rport lock is expected to be held before calling
  751. * this routine.
  752. */
  753. static void fc_rport_enter_logo(struct fc_rport_priv *rdata)
  754. {
  755. struct fc_lport *lport = rdata->local_port;
  756. struct fc_rport *rport = PRIV_TO_RPORT(rdata);
  757. struct fc_frame *fp;
  758. FC_RPORT_DBG(rdata, "Port entered LOGO state from %s state\n",
  759. fc_rport_state(rdata));
  760. fc_rport_state_enter(rdata, RPORT_ST_LOGO);
  761. fp = fc_frame_alloc(lport, sizeof(struct fc_els_logo));
  762. if (!fp) {
  763. fc_rport_error_retry(rdata, fp);
  764. return;
  765. }
  766. if (!lport->tt.elsct_send(lport, rport->port_id, fp, ELS_LOGO,
  767. fc_rport_logo_resp, rdata, lport->e_d_tov))
  768. fc_rport_error_retry(rdata, fp);
  769. else
  770. get_device(&rport->dev);
  771. }
  772. /**
  773. * fc_rport_recv_req() - Receive a request from a rport
  774. * @sp: current sequence in the PLOGI exchange
  775. * @fp: response frame
  776. * @rdata_arg: private remote port data
  777. *
  778. * Locking Note: Called without the rport lock held. This
  779. * function will hold the rport lock, call an _enter_*
  780. * function and then unlock the rport.
  781. */
  782. void fc_rport_recv_req(struct fc_seq *sp, struct fc_frame *fp,
  783. struct fc_rport_priv *rdata)
  784. {
  785. struct fc_lport *lport = rdata->local_port;
  786. struct fc_frame_header *fh;
  787. struct fc_seq_els_data els_data;
  788. u8 op;
  789. mutex_lock(&rdata->rp_mutex);
  790. els_data.fp = NULL;
  791. els_data.explan = ELS_EXPL_NONE;
  792. els_data.reason = ELS_RJT_NONE;
  793. fh = fc_frame_header_get(fp);
  794. if (fh->fh_r_ctl == FC_RCTL_ELS_REQ && fh->fh_type == FC_TYPE_ELS) {
  795. op = fc_frame_payload_op(fp);
  796. switch (op) {
  797. case ELS_PLOGI:
  798. fc_rport_recv_plogi_req(rdata, sp, fp);
  799. break;
  800. case ELS_PRLI:
  801. fc_rport_recv_prli_req(rdata, sp, fp);
  802. break;
  803. case ELS_PRLO:
  804. fc_rport_recv_prlo_req(rdata, sp, fp);
  805. break;
  806. case ELS_LOGO:
  807. fc_rport_recv_logo_req(rdata, sp, fp);
  808. break;
  809. case ELS_RRQ:
  810. els_data.fp = fp;
  811. lport->tt.seq_els_rsp_send(sp, ELS_RRQ, &els_data);
  812. break;
  813. case ELS_REC:
  814. els_data.fp = fp;
  815. lport->tt.seq_els_rsp_send(sp, ELS_REC, &els_data);
  816. break;
  817. default:
  818. els_data.reason = ELS_RJT_UNSUP;
  819. lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &els_data);
  820. break;
  821. }
  822. }
  823. mutex_unlock(&rdata->rp_mutex);
  824. }
  825. /**
  826. * fc_rport_recv_plogi_req() - Handle incoming Port Login (PLOGI) request
  827. * @rdata: private remote port data
  828. * @sp: current sequence in the PLOGI exchange
  829. * @fp: PLOGI request frame
  830. *
  831. * Locking Note: The rport lock is exected to be held before calling
  832. * this function.
  833. */
  834. static void fc_rport_recv_plogi_req(struct fc_rport_priv *rdata,
  835. struct fc_seq *sp, struct fc_frame *rx_fp)
  836. {
  837. struct fc_rport *rport = PRIV_TO_RPORT(rdata);
  838. struct fc_lport *lport = rdata->local_port;
  839. struct fc_frame *fp = rx_fp;
  840. struct fc_exch *ep;
  841. struct fc_frame_header *fh;
  842. struct fc_els_flogi *pl;
  843. struct fc_seq_els_data rjt_data;
  844. u32 sid;
  845. u64 wwpn;
  846. u64 wwnn;
  847. enum fc_els_rjt_reason reject = 0;
  848. u32 f_ctl;
  849. rjt_data.fp = NULL;
  850. fh = fc_frame_header_get(fp);
  851. FC_RPORT_DBG(rdata, "Received PLOGI request while in state %s\n",
  852. fc_rport_state(rdata));
  853. sid = ntoh24(fh->fh_s_id);
  854. pl = fc_frame_payload_get(fp, sizeof(*pl));
  855. if (!pl) {
  856. FC_RPORT_DBG(rdata, "Received PLOGI too short\n");
  857. WARN_ON(1);
  858. /* XXX TBD: send reject? */
  859. fc_frame_free(fp);
  860. return;
  861. }
  862. wwpn = get_unaligned_be64(&pl->fl_wwpn);
  863. wwnn = get_unaligned_be64(&pl->fl_wwnn);
  864. /*
  865. * If the session was just created, possibly due to the incoming PLOGI,
  866. * set the state appropriately and accept the PLOGI.
  867. *
  868. * If we had also sent a PLOGI, and if the received PLOGI is from a
  869. * higher WWPN, we accept it, otherwise an LS_RJT is sent with reason
  870. * "command already in progress".
  871. *
  872. * XXX TBD: If the session was ready before, the PLOGI should result in
  873. * all outstanding exchanges being reset.
  874. */
  875. switch (rdata->rp_state) {
  876. case RPORT_ST_INIT:
  877. FC_RPORT_DBG(rdata, "Received PLOGI, wwpn %llx state INIT "
  878. "- reject\n", (unsigned long long)wwpn);
  879. reject = ELS_RJT_UNSUP;
  880. break;
  881. case RPORT_ST_PLOGI:
  882. FC_RPORT_DBG(rdata, "Received PLOGI in PLOGI state %d\n",
  883. rdata->rp_state);
  884. if (wwpn < lport->wwpn)
  885. reject = ELS_RJT_INPROG;
  886. break;
  887. case RPORT_ST_PRLI:
  888. case RPORT_ST_READY:
  889. FC_RPORT_DBG(rdata, "Received PLOGI in logged-in state %d "
  890. "- ignored for now\n", rdata->rp_state);
  891. /* XXX TBD - should reset */
  892. break;
  893. case RPORT_ST_DELETE:
  894. default:
  895. FC_RPORT_DBG(rdata, "Received PLOGI in unexpected "
  896. "state %d\n", rdata->rp_state);
  897. fc_frame_free(fp);
  898. return;
  899. break;
  900. }
  901. if (reject) {
  902. rjt_data.reason = reject;
  903. rjt_data.explan = ELS_EXPL_NONE;
  904. lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
  905. fc_frame_free(fp);
  906. } else {
  907. fp = fc_frame_alloc(lport, sizeof(*pl));
  908. if (fp == NULL) {
  909. fp = rx_fp;
  910. rjt_data.reason = ELS_RJT_UNAB;
  911. rjt_data.explan = ELS_EXPL_NONE;
  912. lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
  913. fc_frame_free(fp);
  914. } else {
  915. sp = lport->tt.seq_start_next(sp);
  916. WARN_ON(!sp);
  917. fc_rport_set_name(rport, wwpn, wwnn);
  918. /*
  919. * Get session payload size from incoming PLOGI.
  920. */
  921. rport->maxframe_size =
  922. fc_plogi_get_maxframe(pl, lport->mfs);
  923. fc_frame_free(rx_fp);
  924. fc_plogi_fill(lport, fp, ELS_LS_ACC);
  925. /*
  926. * Send LS_ACC. If this fails,
  927. * the originator should retry.
  928. */
  929. f_ctl = FC_FC_EX_CTX | FC_FC_LAST_SEQ;
  930. f_ctl |= FC_FC_END_SEQ | FC_FC_SEQ_INIT;
  931. ep = fc_seq_exch(sp);
  932. fc_fill_fc_hdr(fp, FC_RCTL_ELS_REP, ep->did, ep->sid,
  933. FC_TYPE_ELS, f_ctl, 0);
  934. lport->tt.seq_send(lport, sp, fp);
  935. if (rdata->rp_state == RPORT_ST_PLOGI)
  936. fc_rport_enter_prli(rdata);
  937. }
  938. }
  939. }
  940. /**
  941. * fc_rport_recv_prli_req() - Handle incoming Process Login (PRLI) request
  942. * @rdata: private remote port data
  943. * @sp: current sequence in the PRLI exchange
  944. * @fp: PRLI request frame
  945. *
  946. * Locking Note: The rport lock is exected to be held before calling
  947. * this function.
  948. */
  949. static void fc_rport_recv_prli_req(struct fc_rport_priv *rdata,
  950. struct fc_seq *sp, struct fc_frame *rx_fp)
  951. {
  952. struct fc_rport *rport = PRIV_TO_RPORT(rdata);
  953. struct fc_lport *lport = rdata->local_port;
  954. struct fc_exch *ep;
  955. struct fc_frame *fp;
  956. struct fc_frame_header *fh;
  957. struct {
  958. struct fc_els_prli prli;
  959. struct fc_els_spp spp;
  960. } *pp;
  961. struct fc_els_spp *rspp; /* request service param page */
  962. struct fc_els_spp *spp; /* response spp */
  963. unsigned int len;
  964. unsigned int plen;
  965. enum fc_els_rjt_reason reason = ELS_RJT_UNAB;
  966. enum fc_els_rjt_explan explan = ELS_EXPL_NONE;
  967. enum fc_els_spp_resp resp;
  968. struct fc_seq_els_data rjt_data;
  969. u32 f_ctl;
  970. u32 fcp_parm;
  971. u32 roles = FC_RPORT_ROLE_UNKNOWN;
  972. rjt_data.fp = NULL;
  973. fh = fc_frame_header_get(rx_fp);
  974. FC_RPORT_DBG(rdata, "Received PRLI request while in state %s\n",
  975. fc_rport_state(rdata));
  976. switch (rdata->rp_state) {
  977. case RPORT_ST_PRLI:
  978. case RPORT_ST_READY:
  979. reason = ELS_RJT_NONE;
  980. break;
  981. default:
  982. fc_frame_free(rx_fp);
  983. return;
  984. break;
  985. }
  986. len = fr_len(rx_fp) - sizeof(*fh);
  987. pp = fc_frame_payload_get(rx_fp, sizeof(*pp));
  988. if (pp == NULL) {
  989. reason = ELS_RJT_PROT;
  990. explan = ELS_EXPL_INV_LEN;
  991. } else {
  992. plen = ntohs(pp->prli.prli_len);
  993. if ((plen % 4) != 0 || plen > len) {
  994. reason = ELS_RJT_PROT;
  995. explan = ELS_EXPL_INV_LEN;
  996. } else if (plen < len) {
  997. len = plen;
  998. }
  999. plen = pp->prli.prli_spp_len;
  1000. if ((plen % 4) != 0 || plen < sizeof(*spp) ||
  1001. plen > len || len < sizeof(*pp)) {
  1002. reason = ELS_RJT_PROT;
  1003. explan = ELS_EXPL_INV_LEN;
  1004. }
  1005. rspp = &pp->spp;
  1006. }
  1007. if (reason != ELS_RJT_NONE ||
  1008. (fp = fc_frame_alloc(lport, len)) == NULL) {
  1009. rjt_data.reason = reason;
  1010. rjt_data.explan = explan;
  1011. lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
  1012. } else {
  1013. sp = lport->tt.seq_start_next(sp);
  1014. WARN_ON(!sp);
  1015. pp = fc_frame_payload_get(fp, len);
  1016. WARN_ON(!pp);
  1017. memset(pp, 0, len);
  1018. pp->prli.prli_cmd = ELS_LS_ACC;
  1019. pp->prli.prli_spp_len = plen;
  1020. pp->prli.prli_len = htons(len);
  1021. len -= sizeof(struct fc_els_prli);
  1022. /*
  1023. * Go through all the service parameter pages and build
  1024. * response. If plen indicates longer SPP than standard,
  1025. * use that. The entire response has been pre-cleared above.
  1026. */
  1027. spp = &pp->spp;
  1028. while (len >= plen) {
  1029. spp->spp_type = rspp->spp_type;
  1030. spp->spp_type_ext = rspp->spp_type_ext;
  1031. spp->spp_flags = rspp->spp_flags & FC_SPP_EST_IMG_PAIR;
  1032. resp = FC_SPP_RESP_ACK;
  1033. if (rspp->spp_flags & FC_SPP_RPA_VAL)
  1034. resp = FC_SPP_RESP_NO_PA;
  1035. switch (rspp->spp_type) {
  1036. case 0: /* common to all FC-4 types */
  1037. break;
  1038. case FC_TYPE_FCP:
  1039. fcp_parm = ntohl(rspp->spp_params);
  1040. if (fcp_parm * FCP_SPPF_RETRY)
  1041. rdata->flags |= FC_RP_FLAGS_RETRY;
  1042. rport->supported_classes = FC_COS_CLASS3;
  1043. if (fcp_parm & FCP_SPPF_INIT_FCN)
  1044. roles |= FC_RPORT_ROLE_FCP_INITIATOR;
  1045. if (fcp_parm & FCP_SPPF_TARG_FCN)
  1046. roles |= FC_RPORT_ROLE_FCP_TARGET;
  1047. rport->roles = roles;
  1048. spp->spp_params =
  1049. htonl(lport->service_params);
  1050. break;
  1051. default:
  1052. resp = FC_SPP_RESP_INVL;
  1053. break;
  1054. }
  1055. spp->spp_flags |= resp;
  1056. len -= plen;
  1057. rspp = (struct fc_els_spp *)((char *)rspp + plen);
  1058. spp = (struct fc_els_spp *)((char *)spp + plen);
  1059. }
  1060. /*
  1061. * Send LS_ACC. If this fails, the originator should retry.
  1062. */
  1063. f_ctl = FC_FC_EX_CTX | FC_FC_LAST_SEQ;
  1064. f_ctl |= FC_FC_END_SEQ | FC_FC_SEQ_INIT;
  1065. ep = fc_seq_exch(sp);
  1066. fc_fill_fc_hdr(fp, FC_RCTL_ELS_REP, ep->did, ep->sid,
  1067. FC_TYPE_ELS, f_ctl, 0);
  1068. lport->tt.seq_send(lport, sp, fp);
  1069. /*
  1070. * Get lock and re-check state.
  1071. */
  1072. switch (rdata->rp_state) {
  1073. case RPORT_ST_PRLI:
  1074. fc_rport_enter_ready(rdata);
  1075. break;
  1076. case RPORT_ST_READY:
  1077. break;
  1078. default:
  1079. break;
  1080. }
  1081. }
  1082. fc_frame_free(rx_fp);
  1083. }
  1084. /**
  1085. * fc_rport_recv_prlo_req() - Handle incoming Process Logout (PRLO) request
  1086. * @rdata: private remote port data
  1087. * @sp: current sequence in the PRLO exchange
  1088. * @fp: PRLO request frame
  1089. *
  1090. * Locking Note: The rport lock is exected to be held before calling
  1091. * this function.
  1092. */
  1093. static void fc_rport_recv_prlo_req(struct fc_rport_priv *rdata,
  1094. struct fc_seq *sp,
  1095. struct fc_frame *fp)
  1096. {
  1097. struct fc_lport *lport = rdata->local_port;
  1098. struct fc_frame_header *fh;
  1099. struct fc_seq_els_data rjt_data;
  1100. fh = fc_frame_header_get(fp);
  1101. FC_RPORT_DBG(rdata, "Received PRLO request while in state %s\n",
  1102. fc_rport_state(rdata));
  1103. if (rdata->rp_state == RPORT_ST_DELETE) {
  1104. fc_frame_free(fp);
  1105. return;
  1106. }
  1107. rjt_data.fp = NULL;
  1108. rjt_data.reason = ELS_RJT_UNAB;
  1109. rjt_data.explan = ELS_EXPL_NONE;
  1110. lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
  1111. fc_frame_free(fp);
  1112. }
  1113. /**
  1114. * fc_rport_recv_logo_req() - Handle incoming Logout (LOGO) request
  1115. * @rdata: private remote port data
  1116. * @sp: current sequence in the LOGO exchange
  1117. * @fp: LOGO request frame
  1118. *
  1119. * Locking Note: The rport lock is exected to be held before calling
  1120. * this function.
  1121. */
  1122. static void fc_rport_recv_logo_req(struct fc_rport_priv *rdata,
  1123. struct fc_seq *sp,
  1124. struct fc_frame *fp)
  1125. {
  1126. struct fc_frame_header *fh;
  1127. struct fc_lport *lport = rdata->local_port;
  1128. fh = fc_frame_header_get(fp);
  1129. FC_RPORT_DBG(rdata, "Received LOGO request while in state %s\n",
  1130. fc_rport_state(rdata));
  1131. if (rdata->rp_state == RPORT_ST_DELETE) {
  1132. fc_frame_free(fp);
  1133. return;
  1134. }
  1135. rdata->event = RPORT_EV_LOGO;
  1136. fc_rport_state_enter(rdata, RPORT_ST_DELETE);
  1137. queue_work(rport_event_queue, &rdata->event_work);
  1138. lport->tt.seq_els_rsp_send(sp, ELS_LS_ACC, NULL);
  1139. fc_frame_free(fp);
  1140. }
  1141. static void fc_rport_flush_queue(void)
  1142. {
  1143. flush_workqueue(rport_event_queue);
  1144. }
  1145. int fc_rport_init(struct fc_lport *lport)
  1146. {
  1147. if (!lport->tt.rport_create)
  1148. lport->tt.rport_create = fc_rport_rogue_create;
  1149. if (!lport->tt.rport_login)
  1150. lport->tt.rport_login = fc_rport_login;
  1151. if (!lport->tt.rport_logoff)
  1152. lport->tt.rport_logoff = fc_rport_logoff;
  1153. if (!lport->tt.rport_recv_req)
  1154. lport->tt.rport_recv_req = fc_rport_recv_req;
  1155. if (!lport->tt.rport_flush_queue)
  1156. lport->tt.rport_flush_queue = fc_rport_flush_queue;
  1157. return 0;
  1158. }
  1159. EXPORT_SYMBOL(fc_rport_init);
  1160. int fc_setup_rport(void)
  1161. {
  1162. rport_event_queue = create_singlethread_workqueue("fc_rport_eq");
  1163. if (!rport_event_queue)
  1164. return -ENOMEM;
  1165. return 0;
  1166. }
  1167. EXPORT_SYMBOL(fc_setup_rport);
  1168. void fc_destroy_rport(void)
  1169. {
  1170. destroy_workqueue(rport_event_queue);
  1171. }
  1172. EXPORT_SYMBOL(fc_destroy_rport);
  1173. void fc_rport_terminate_io(struct fc_rport *rport)
  1174. {
  1175. struct fc_rport_libfc_priv *rp = rport->dd_data;
  1176. struct fc_lport *lport = rp->local_port;
  1177. lport->tt.exch_mgr_reset(lport, 0, rport->port_id);
  1178. lport->tt.exch_mgr_reset(lport, rport->port_id, 0);
  1179. }
  1180. EXPORT_SYMBOL(fc_rport_terminate_io);