fc_rport.c 34 KB

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