fc_rport.c 37 KB

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