fc_rport.c 35 KB

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