fc_rport.c 36 KB

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