fc_rport.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645
  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. #include "fc_libfc.h"
  54. struct workqueue_struct *rport_event_queue;
  55. static void fc_rport_enter_plogi(struct fc_rport_priv *);
  56. static void fc_rport_enter_prli(struct fc_rport_priv *);
  57. static void fc_rport_enter_rtv(struct fc_rport_priv *);
  58. static void fc_rport_enter_ready(struct fc_rport_priv *);
  59. static void fc_rport_enter_logo(struct fc_rport_priv *);
  60. static void fc_rport_enter_adisc(struct fc_rport_priv *);
  61. static void fc_rport_recv_plogi_req(struct fc_lport *,
  62. struct fc_seq *, struct fc_frame *);
  63. static void fc_rport_recv_prli_req(struct fc_rport_priv *,
  64. struct fc_seq *, struct fc_frame *);
  65. static void fc_rport_recv_prlo_req(struct fc_rport_priv *,
  66. struct fc_seq *, struct fc_frame *);
  67. static void fc_rport_recv_logo_req(struct fc_lport *,
  68. struct fc_seq *, struct fc_frame *);
  69. static void fc_rport_timeout(struct work_struct *);
  70. static void fc_rport_error(struct fc_rport_priv *, struct fc_frame *);
  71. static void fc_rport_error_retry(struct fc_rport_priv *, struct fc_frame *);
  72. static void fc_rport_work(struct work_struct *);
  73. static const char *fc_rport_state_names[] = {
  74. [RPORT_ST_INIT] = "Init",
  75. [RPORT_ST_PLOGI] = "PLOGI",
  76. [RPORT_ST_PRLI] = "PRLI",
  77. [RPORT_ST_RTV] = "RTV",
  78. [RPORT_ST_READY] = "Ready",
  79. [RPORT_ST_LOGO] = "LOGO",
  80. [RPORT_ST_ADISC] = "ADISC",
  81. [RPORT_ST_DELETE] = "Delete",
  82. [RPORT_ST_RESTART] = "Restart",
  83. };
  84. /**
  85. * fc_rport_lookup() - Lookup a remote port by port_id
  86. * @lport: The local port to lookup the remote port on
  87. * @port_id: The remote port ID to look up
  88. */
  89. static struct fc_rport_priv *fc_rport_lookup(const struct fc_lport *lport,
  90. u32 port_id)
  91. {
  92. struct fc_rport_priv *rdata;
  93. list_for_each_entry(rdata, &lport->disc.rports, peers)
  94. if (rdata->ids.port_id == port_id)
  95. return rdata;
  96. return NULL;
  97. }
  98. /**
  99. * fc_rport_create() - Create a new remote port
  100. * @lport: The local port this remote port will be associated with
  101. * @ids: The identifiers for the new remote port
  102. *
  103. * The remote port will start in the INIT state.
  104. *
  105. * Locking note: must be called with the disc_mutex held.
  106. */
  107. static struct fc_rport_priv *fc_rport_create(struct fc_lport *lport,
  108. u32 port_id)
  109. {
  110. struct fc_rport_priv *rdata;
  111. rdata = lport->tt.rport_lookup(lport, port_id);
  112. if (rdata)
  113. return rdata;
  114. rdata = kzalloc(sizeof(*rdata), GFP_KERNEL);
  115. if (!rdata)
  116. return NULL;
  117. rdata->ids.node_name = -1;
  118. rdata->ids.port_name = -1;
  119. rdata->ids.port_id = port_id;
  120. rdata->ids.roles = FC_RPORT_ROLE_UNKNOWN;
  121. kref_init(&rdata->kref);
  122. mutex_init(&rdata->rp_mutex);
  123. rdata->local_port = lport;
  124. rdata->rp_state = RPORT_ST_INIT;
  125. rdata->event = RPORT_EV_NONE;
  126. rdata->flags = FC_RP_FLAGS_REC_SUPPORTED;
  127. rdata->e_d_tov = lport->e_d_tov;
  128. rdata->r_a_tov = lport->r_a_tov;
  129. rdata->maxframe_size = FC_MIN_MAX_PAYLOAD;
  130. INIT_DELAYED_WORK(&rdata->retry_work, fc_rport_timeout);
  131. INIT_WORK(&rdata->event_work, fc_rport_work);
  132. if (port_id != FC_FID_DIR_SERV)
  133. list_add(&rdata->peers, &lport->disc.rports);
  134. return rdata;
  135. }
  136. /**
  137. * fc_rport_destroy() - Free a remote port after last reference is released
  138. * @kref: The remote port's kref
  139. */
  140. static void fc_rport_destroy(struct kref *kref)
  141. {
  142. struct fc_rport_priv *rdata;
  143. rdata = container_of(kref, struct fc_rport_priv, kref);
  144. kfree(rdata);
  145. }
  146. /**
  147. * fc_rport_state() - Return a string identifying the remote port's state
  148. * @rdata: The remote port
  149. */
  150. static const char *fc_rport_state(struct fc_rport_priv *rdata)
  151. {
  152. const char *cp;
  153. cp = fc_rport_state_names[rdata->rp_state];
  154. if (!cp)
  155. cp = "Unknown";
  156. return cp;
  157. }
  158. /**
  159. * fc_set_rport_loss_tmo() - Set the remote port loss timeout
  160. * @rport: The remote port that gets a new timeout value
  161. * @timeout: The new timeout value (in seconds)
  162. */
  163. void fc_set_rport_loss_tmo(struct fc_rport *rport, u32 timeout)
  164. {
  165. if (timeout)
  166. rport->dev_loss_tmo = timeout + 5;
  167. else
  168. rport->dev_loss_tmo = 30;
  169. }
  170. EXPORT_SYMBOL(fc_set_rport_loss_tmo);
  171. /**
  172. * fc_plogi_get_maxframe() - Get the maximum payload from the common service
  173. * parameters in a FLOGI frame
  174. * @flp: The FLOGI payload
  175. * @maxval: The maximum frame size upper limit; this may be less than what
  176. * is in the service parameters
  177. */
  178. static unsigned int fc_plogi_get_maxframe(struct fc_els_flogi *flp,
  179. unsigned int maxval)
  180. {
  181. unsigned int mfs;
  182. /*
  183. * Get max payload from the common service parameters and the
  184. * class 3 receive data field size.
  185. */
  186. mfs = ntohs(flp->fl_csp.sp_bb_data) & FC_SP_BB_DATA_MASK;
  187. if (mfs >= FC_SP_MIN_MAX_PAYLOAD && mfs < maxval)
  188. maxval = mfs;
  189. mfs = ntohs(flp->fl_cssp[3 - 1].cp_rdfs);
  190. if (mfs >= FC_SP_MIN_MAX_PAYLOAD && mfs < maxval)
  191. maxval = mfs;
  192. return maxval;
  193. }
  194. /**
  195. * fc_rport_state_enter() - Change the state of a remote port
  196. * @rdata: The remote port whose state should change
  197. * @new: The new state
  198. *
  199. * Locking Note: Called with the rport lock held
  200. */
  201. static void fc_rport_state_enter(struct fc_rport_priv *rdata,
  202. enum fc_rport_state new)
  203. {
  204. if (rdata->rp_state != new)
  205. rdata->retries = 0;
  206. rdata->rp_state = new;
  207. }
  208. /**
  209. * fc_rport_work() - Handler for remote port events in the rport_event_queue
  210. * @work: Handle to the remote port being dequeued
  211. */
  212. static void fc_rport_work(struct work_struct *work)
  213. {
  214. u32 port_id;
  215. struct fc_rport_priv *rdata =
  216. container_of(work, struct fc_rport_priv, event_work);
  217. struct fc_rport_libfc_priv *rpriv;
  218. enum fc_rport_event event;
  219. struct fc_lport *lport = rdata->local_port;
  220. struct fc_rport_operations *rport_ops;
  221. struct fc_rport_identifiers ids;
  222. struct fc_rport *rport;
  223. int restart = 0;
  224. mutex_lock(&rdata->rp_mutex);
  225. event = rdata->event;
  226. rport_ops = rdata->ops;
  227. rport = rdata->rport;
  228. FC_RPORT_DBG(rdata, "work event %u\n", event);
  229. switch (event) {
  230. case RPORT_EV_READY:
  231. ids = rdata->ids;
  232. rdata->event = RPORT_EV_NONE;
  233. kref_get(&rdata->kref);
  234. mutex_unlock(&rdata->rp_mutex);
  235. if (!rport)
  236. rport = fc_remote_port_add(lport->host, 0, &ids);
  237. if (!rport) {
  238. FC_RPORT_DBG(rdata, "Failed to add the rport\n");
  239. lport->tt.rport_logoff(rdata);
  240. kref_put(&rdata->kref, lport->tt.rport_destroy);
  241. return;
  242. }
  243. mutex_lock(&rdata->rp_mutex);
  244. if (rdata->rport)
  245. FC_RPORT_DBG(rdata, "rport already allocated\n");
  246. rdata->rport = rport;
  247. rport->maxframe_size = rdata->maxframe_size;
  248. rport->supported_classes = rdata->supported_classes;
  249. rpriv = rport->dd_data;
  250. rpriv->local_port = lport;
  251. rpriv->rp_state = rdata->rp_state;
  252. rpriv->flags = rdata->flags;
  253. rpriv->e_d_tov = rdata->e_d_tov;
  254. rpriv->r_a_tov = rdata->r_a_tov;
  255. mutex_unlock(&rdata->rp_mutex);
  256. if (rport_ops && rport_ops->event_callback) {
  257. FC_RPORT_DBG(rdata, "callback ev %d\n", event);
  258. rport_ops->event_callback(lport, rdata, event);
  259. }
  260. kref_put(&rdata->kref, lport->tt.rport_destroy);
  261. break;
  262. case RPORT_EV_FAILED:
  263. case RPORT_EV_LOGO:
  264. case RPORT_EV_STOP:
  265. port_id = rdata->ids.port_id;
  266. mutex_unlock(&rdata->rp_mutex);
  267. if (port_id != FC_FID_DIR_SERV) {
  268. /*
  269. * We must drop rp_mutex before taking disc_mutex.
  270. * Re-evaluate state to allow for restart.
  271. * A transition to RESTART state must only happen
  272. * while disc_mutex is held and rdata is on the list.
  273. */
  274. mutex_lock(&lport->disc.disc_mutex);
  275. mutex_lock(&rdata->rp_mutex);
  276. if (rdata->rp_state == RPORT_ST_RESTART)
  277. restart = 1;
  278. else
  279. list_del(&rdata->peers);
  280. mutex_unlock(&rdata->rp_mutex);
  281. mutex_unlock(&lport->disc.disc_mutex);
  282. }
  283. if (rport_ops && rport_ops->event_callback) {
  284. FC_RPORT_DBG(rdata, "callback ev %d\n", event);
  285. rport_ops->event_callback(lport, rdata, event);
  286. }
  287. cancel_delayed_work_sync(&rdata->retry_work);
  288. /*
  289. * Reset any outstanding exchanges before freeing rport.
  290. */
  291. lport->tt.exch_mgr_reset(lport, 0, port_id);
  292. lport->tt.exch_mgr_reset(lport, port_id, 0);
  293. if (rport) {
  294. rpriv = rport->dd_data;
  295. rpriv->rp_state = RPORT_ST_DELETE;
  296. mutex_lock(&rdata->rp_mutex);
  297. rdata->rport = NULL;
  298. mutex_unlock(&rdata->rp_mutex);
  299. fc_remote_port_delete(rport);
  300. }
  301. if (restart) {
  302. mutex_lock(&rdata->rp_mutex);
  303. FC_RPORT_DBG(rdata, "work restart\n");
  304. fc_rport_enter_plogi(rdata);
  305. mutex_unlock(&rdata->rp_mutex);
  306. } else
  307. kref_put(&rdata->kref, lport->tt.rport_destroy);
  308. break;
  309. default:
  310. mutex_unlock(&rdata->rp_mutex);
  311. break;
  312. }
  313. }
  314. /**
  315. * fc_rport_login() - Start the remote port login state machine
  316. * @rdata: The remote port to be logged in to
  317. *
  318. * Locking Note: Called without the rport lock held. This
  319. * function will hold the rport lock, call an _enter_*
  320. * function and then unlock the rport.
  321. *
  322. * This indicates the intent to be logged into the remote port.
  323. * If it appears we are already logged in, ADISC is used to verify
  324. * the setup.
  325. */
  326. int fc_rport_login(struct fc_rport_priv *rdata)
  327. {
  328. mutex_lock(&rdata->rp_mutex);
  329. switch (rdata->rp_state) {
  330. case RPORT_ST_READY:
  331. FC_RPORT_DBG(rdata, "ADISC port\n");
  332. fc_rport_enter_adisc(rdata);
  333. break;
  334. case RPORT_ST_RESTART:
  335. break;
  336. case RPORT_ST_DELETE:
  337. FC_RPORT_DBG(rdata, "Restart deleted port\n");
  338. fc_rport_state_enter(rdata, RPORT_ST_RESTART);
  339. break;
  340. default:
  341. FC_RPORT_DBG(rdata, "Login to port\n");
  342. fc_rport_enter_plogi(rdata);
  343. break;
  344. }
  345. mutex_unlock(&rdata->rp_mutex);
  346. return 0;
  347. }
  348. /**
  349. * fc_rport_enter_delete() - Schedule a remote port to be deleted
  350. * @rdata: The remote port to be deleted
  351. * @event: The event to report as the reason for deletion
  352. *
  353. * Locking Note: Called with the rport lock held.
  354. *
  355. * Allow state change into DELETE only once.
  356. *
  357. * Call queue_work only if there's no event already pending.
  358. * Set the new event so that the old pending event will not occur.
  359. * Since we have the mutex, even if fc_rport_work() is already started,
  360. * it'll see the new event.
  361. */
  362. static void fc_rport_enter_delete(struct fc_rport_priv *rdata,
  363. enum fc_rport_event event)
  364. {
  365. if (rdata->rp_state == RPORT_ST_DELETE)
  366. return;
  367. FC_RPORT_DBG(rdata, "Delete port\n");
  368. fc_rport_state_enter(rdata, RPORT_ST_DELETE);
  369. if (rdata->event == RPORT_EV_NONE)
  370. queue_work(rport_event_queue, &rdata->event_work);
  371. rdata->event = event;
  372. }
  373. /**
  374. * fc_rport_logoff() - Logoff and remove a remote port
  375. * @rdata: The remote port to be logged off of
  376. *
  377. * Locking Note: Called without the rport lock held. This
  378. * function will hold the rport lock, call an _enter_*
  379. * function and then unlock the rport.
  380. */
  381. int fc_rport_logoff(struct fc_rport_priv *rdata)
  382. {
  383. mutex_lock(&rdata->rp_mutex);
  384. FC_RPORT_DBG(rdata, "Remove port\n");
  385. if (rdata->rp_state == RPORT_ST_DELETE) {
  386. FC_RPORT_DBG(rdata, "Port in Delete state, not removing\n");
  387. goto out;
  388. }
  389. if (rdata->rp_state == RPORT_ST_RESTART)
  390. FC_RPORT_DBG(rdata, "Port in Restart state, deleting\n");
  391. else
  392. fc_rport_enter_logo(rdata);
  393. /*
  394. * Change the state to Delete so that we discard
  395. * the response.
  396. */
  397. fc_rport_enter_delete(rdata, RPORT_EV_STOP);
  398. out:
  399. mutex_unlock(&rdata->rp_mutex);
  400. return 0;
  401. }
  402. /**
  403. * fc_rport_enter_ready() - Transition to the RPORT_ST_READY state
  404. * @rdata: The remote port that is ready
  405. *
  406. * Locking Note: The rport lock is expected to be held before calling
  407. * this routine.
  408. */
  409. static void fc_rport_enter_ready(struct fc_rport_priv *rdata)
  410. {
  411. fc_rport_state_enter(rdata, RPORT_ST_READY);
  412. FC_RPORT_DBG(rdata, "Port is Ready\n");
  413. if (rdata->event == RPORT_EV_NONE)
  414. queue_work(rport_event_queue, &rdata->event_work);
  415. rdata->event = RPORT_EV_READY;
  416. }
  417. /**
  418. * fc_rport_timeout() - Handler for the retry_work timer
  419. * @work: Handle to the remote port that has timed out
  420. *
  421. * Locking Note: Called without the rport lock held. This
  422. * function will hold the rport lock, call an _enter_*
  423. * function and then unlock the rport.
  424. */
  425. static void fc_rport_timeout(struct work_struct *work)
  426. {
  427. struct fc_rport_priv *rdata =
  428. container_of(work, struct fc_rport_priv, retry_work.work);
  429. mutex_lock(&rdata->rp_mutex);
  430. switch (rdata->rp_state) {
  431. case RPORT_ST_PLOGI:
  432. fc_rport_enter_plogi(rdata);
  433. break;
  434. case RPORT_ST_PRLI:
  435. fc_rport_enter_prli(rdata);
  436. break;
  437. case RPORT_ST_RTV:
  438. fc_rport_enter_rtv(rdata);
  439. break;
  440. case RPORT_ST_LOGO:
  441. fc_rport_enter_logo(rdata);
  442. break;
  443. case RPORT_ST_ADISC:
  444. fc_rport_enter_adisc(rdata);
  445. break;
  446. case RPORT_ST_READY:
  447. case RPORT_ST_INIT:
  448. case RPORT_ST_DELETE:
  449. case RPORT_ST_RESTART:
  450. break;
  451. }
  452. mutex_unlock(&rdata->rp_mutex);
  453. }
  454. /**
  455. * fc_rport_error() - Error handler, called once retries have been exhausted
  456. * @rdata: The remote port the error is happened on
  457. * @fp: The error code encapsulated in a frame pointer
  458. *
  459. * Locking Note: The rport lock is expected to be held before
  460. * calling this routine
  461. */
  462. static void fc_rport_error(struct fc_rport_priv *rdata, struct fc_frame *fp)
  463. {
  464. FC_RPORT_DBG(rdata, "Error %ld in state %s, retries %d\n",
  465. IS_ERR(fp) ? -PTR_ERR(fp) : 0,
  466. fc_rport_state(rdata), rdata->retries);
  467. switch (rdata->rp_state) {
  468. case RPORT_ST_PLOGI:
  469. case RPORT_ST_LOGO:
  470. fc_rport_enter_delete(rdata, RPORT_EV_FAILED);
  471. break;
  472. case RPORT_ST_RTV:
  473. fc_rport_enter_ready(rdata);
  474. break;
  475. case RPORT_ST_PRLI:
  476. case RPORT_ST_ADISC:
  477. fc_rport_enter_logo(rdata);
  478. break;
  479. case RPORT_ST_DELETE:
  480. case RPORT_ST_RESTART:
  481. case RPORT_ST_READY:
  482. case RPORT_ST_INIT:
  483. break;
  484. }
  485. }
  486. /**
  487. * fc_rport_error_retry() - Handler for remote port state retries
  488. * @rdata: The remote port whose state is to be retried
  489. * @fp: The error code encapsulated in a frame pointer
  490. *
  491. * If the error was an exchange timeout retry immediately,
  492. * otherwise wait for E_D_TOV.
  493. *
  494. * Locking Note: The rport lock is expected to be held before
  495. * calling this routine
  496. */
  497. static void fc_rport_error_retry(struct fc_rport_priv *rdata,
  498. struct fc_frame *fp)
  499. {
  500. unsigned long delay = FC_DEF_E_D_TOV;
  501. /* make sure this isn't an FC_EX_CLOSED error, never retry those */
  502. if (PTR_ERR(fp) == -FC_EX_CLOSED)
  503. return fc_rport_error(rdata, fp);
  504. if (rdata->retries < rdata->local_port->max_rport_retry_count) {
  505. FC_RPORT_DBG(rdata, "Error %ld in state %s, retrying\n",
  506. PTR_ERR(fp), fc_rport_state(rdata));
  507. rdata->retries++;
  508. /* no additional delay on exchange timeouts */
  509. if (PTR_ERR(fp) == -FC_EX_TIMEOUT)
  510. delay = 0;
  511. schedule_delayed_work(&rdata->retry_work, delay);
  512. return;
  513. }
  514. return fc_rport_error(rdata, fp);
  515. }
  516. /**
  517. * fc_rport_plogi_recv_resp() - Handler for ELS PLOGI responses
  518. * @sp: The sequence the PLOGI is on
  519. * @fp: The PLOGI response frame
  520. * @rdata_arg: The remote port that sent the PLOGI response
  521. *
  522. * Locking Note: This function will be called without the rport lock
  523. * held, but it will lock, call an _enter_* function or fc_rport_error
  524. * and then unlock the rport.
  525. */
  526. static void fc_rport_plogi_resp(struct fc_seq *sp, struct fc_frame *fp,
  527. void *rdata_arg)
  528. {
  529. struct fc_rport_priv *rdata = rdata_arg;
  530. struct fc_lport *lport = rdata->local_port;
  531. struct fc_els_flogi *plp = NULL;
  532. unsigned int tov;
  533. u16 csp_seq;
  534. u16 cssp_seq;
  535. u8 op;
  536. mutex_lock(&rdata->rp_mutex);
  537. FC_RPORT_DBG(rdata, "Received a PLOGI %s\n", fc_els_resp_type(fp));
  538. if (rdata->rp_state != RPORT_ST_PLOGI) {
  539. FC_RPORT_DBG(rdata, "Received a PLOGI response, but in state "
  540. "%s\n", fc_rport_state(rdata));
  541. if (IS_ERR(fp))
  542. goto err;
  543. goto out;
  544. }
  545. if (IS_ERR(fp)) {
  546. fc_rport_error_retry(rdata, fp);
  547. goto err;
  548. }
  549. op = fc_frame_payload_op(fp);
  550. if (op == ELS_LS_ACC &&
  551. (plp = fc_frame_payload_get(fp, sizeof(*plp))) != NULL) {
  552. rdata->ids.port_name = get_unaligned_be64(&plp->fl_wwpn);
  553. rdata->ids.node_name = get_unaligned_be64(&plp->fl_wwnn);
  554. tov = ntohl(plp->fl_csp.sp_e_d_tov);
  555. if (ntohs(plp->fl_csp.sp_features) & FC_SP_FT_EDTR)
  556. tov /= 1000;
  557. if (tov > rdata->e_d_tov)
  558. rdata->e_d_tov = tov;
  559. csp_seq = ntohs(plp->fl_csp.sp_tot_seq);
  560. cssp_seq = ntohs(plp->fl_cssp[3 - 1].cp_con_seq);
  561. if (cssp_seq < csp_seq)
  562. csp_seq = cssp_seq;
  563. rdata->max_seq = csp_seq;
  564. rdata->maxframe_size = fc_plogi_get_maxframe(plp, lport->mfs);
  565. fc_rport_enter_prli(rdata);
  566. } else
  567. fc_rport_error_retry(rdata, fp);
  568. out:
  569. fc_frame_free(fp);
  570. err:
  571. mutex_unlock(&rdata->rp_mutex);
  572. kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
  573. }
  574. /**
  575. * fc_rport_enter_plogi() - Send Port Login (PLOGI) request
  576. * @rdata: The remote port to send a PLOGI to
  577. *
  578. * Locking Note: The rport lock is expected to be held before calling
  579. * this routine.
  580. */
  581. static void fc_rport_enter_plogi(struct fc_rport_priv *rdata)
  582. {
  583. struct fc_lport *lport = rdata->local_port;
  584. struct fc_frame *fp;
  585. FC_RPORT_DBG(rdata, "Port entered PLOGI state from %s state\n",
  586. fc_rport_state(rdata));
  587. fc_rport_state_enter(rdata, RPORT_ST_PLOGI);
  588. rdata->maxframe_size = FC_MIN_MAX_PAYLOAD;
  589. fp = fc_frame_alloc(lport, sizeof(struct fc_els_flogi));
  590. if (!fp) {
  591. fc_rport_error_retry(rdata, fp);
  592. return;
  593. }
  594. rdata->e_d_tov = lport->e_d_tov;
  595. if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_PLOGI,
  596. fc_rport_plogi_resp, rdata,
  597. 2 * lport->r_a_tov))
  598. fc_rport_error_retry(rdata, NULL);
  599. else
  600. kref_get(&rdata->kref);
  601. }
  602. /**
  603. * fc_rport_prli_resp() - Process Login (PRLI) response handler
  604. * @sp: The sequence the PRLI response was on
  605. * @fp: The PRLI response frame
  606. * @rdata_arg: The remote port that sent the PRLI response
  607. *
  608. * Locking Note: This function will be called without the rport lock
  609. * held, but it will lock, call an _enter_* function or fc_rport_error
  610. * and then unlock the rport.
  611. */
  612. static void fc_rport_prli_resp(struct fc_seq *sp, struct fc_frame *fp,
  613. void *rdata_arg)
  614. {
  615. struct fc_rport_priv *rdata = rdata_arg;
  616. struct {
  617. struct fc_els_prli prli;
  618. struct fc_els_spp spp;
  619. } *pp;
  620. u32 roles = FC_RPORT_ROLE_UNKNOWN;
  621. u32 fcp_parm = 0;
  622. u8 op;
  623. mutex_lock(&rdata->rp_mutex);
  624. FC_RPORT_DBG(rdata, "Received a PRLI %s\n", fc_els_resp_type(fp));
  625. if (rdata->rp_state != RPORT_ST_PRLI) {
  626. FC_RPORT_DBG(rdata, "Received a PRLI response, but in state "
  627. "%s\n", fc_rport_state(rdata));
  628. if (IS_ERR(fp))
  629. goto err;
  630. goto out;
  631. }
  632. if (IS_ERR(fp)) {
  633. fc_rport_error_retry(rdata, fp);
  634. goto err;
  635. }
  636. /* reinitialize remote port roles */
  637. rdata->ids.roles = FC_RPORT_ROLE_UNKNOWN;
  638. op = fc_frame_payload_op(fp);
  639. if (op == ELS_LS_ACC) {
  640. pp = fc_frame_payload_get(fp, sizeof(*pp));
  641. if (pp && pp->prli.prli_spp_len >= sizeof(pp->spp)) {
  642. fcp_parm = ntohl(pp->spp.spp_params);
  643. if (fcp_parm & FCP_SPPF_RETRY)
  644. rdata->flags |= FC_RP_FLAGS_RETRY;
  645. }
  646. rdata->supported_classes = FC_COS_CLASS3;
  647. if (fcp_parm & FCP_SPPF_INIT_FCN)
  648. roles |= FC_RPORT_ROLE_FCP_INITIATOR;
  649. if (fcp_parm & FCP_SPPF_TARG_FCN)
  650. roles |= FC_RPORT_ROLE_FCP_TARGET;
  651. rdata->ids.roles = roles;
  652. fc_rport_enter_rtv(rdata);
  653. } else {
  654. FC_RPORT_DBG(rdata, "Bad ELS response for PRLI command\n");
  655. fc_rport_enter_delete(rdata, RPORT_EV_FAILED);
  656. }
  657. out:
  658. fc_frame_free(fp);
  659. err:
  660. mutex_unlock(&rdata->rp_mutex);
  661. kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
  662. }
  663. /**
  664. * fc_rport_logo_resp() - Handler for logout (LOGO) responses
  665. * @sp: The sequence the LOGO was on
  666. * @fp: The LOGO response frame
  667. * @rdata_arg: The remote port that sent the LOGO response
  668. *
  669. * Locking Note: This function will be called without the rport lock
  670. * held, but it will lock, call an _enter_* function or fc_rport_error
  671. * and then unlock the rport.
  672. */
  673. static void fc_rport_logo_resp(struct fc_seq *sp, struct fc_frame *fp,
  674. void *rdata_arg)
  675. {
  676. struct fc_rport_priv *rdata = rdata_arg;
  677. u8 op;
  678. mutex_lock(&rdata->rp_mutex);
  679. FC_RPORT_DBG(rdata, "Received a LOGO %s\n", fc_els_resp_type(fp));
  680. if (rdata->rp_state != RPORT_ST_LOGO) {
  681. FC_RPORT_DBG(rdata, "Received a LOGO response, but in state "
  682. "%s\n", fc_rport_state(rdata));
  683. if (IS_ERR(fp))
  684. goto err;
  685. goto out;
  686. }
  687. if (IS_ERR(fp)) {
  688. fc_rport_error_retry(rdata, fp);
  689. goto err;
  690. }
  691. op = fc_frame_payload_op(fp);
  692. if (op != ELS_LS_ACC)
  693. FC_RPORT_DBG(rdata, "Bad ELS response op %x for LOGO command\n",
  694. op);
  695. fc_rport_enter_delete(rdata, RPORT_EV_LOGO);
  696. out:
  697. fc_frame_free(fp);
  698. err:
  699. mutex_unlock(&rdata->rp_mutex);
  700. kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
  701. }
  702. /**
  703. * fc_rport_enter_prli() - Send Process Login (PRLI) request
  704. * @rdata: The remote port to send the PRLI request to
  705. *
  706. * Locking Note: The rport lock is expected to be held before calling
  707. * this routine.
  708. */
  709. static void fc_rport_enter_prli(struct fc_rport_priv *rdata)
  710. {
  711. struct fc_lport *lport = rdata->local_port;
  712. struct {
  713. struct fc_els_prli prli;
  714. struct fc_els_spp spp;
  715. } *pp;
  716. struct fc_frame *fp;
  717. /*
  718. * If the rport is one of the well known addresses
  719. * we skip PRLI and RTV and go straight to READY.
  720. */
  721. if (rdata->ids.port_id >= FC_FID_DOM_MGR) {
  722. fc_rport_enter_ready(rdata);
  723. return;
  724. }
  725. FC_RPORT_DBG(rdata, "Port entered PRLI state from %s state\n",
  726. fc_rport_state(rdata));
  727. fc_rport_state_enter(rdata, RPORT_ST_PRLI);
  728. fp = fc_frame_alloc(lport, sizeof(*pp));
  729. if (!fp) {
  730. fc_rport_error_retry(rdata, fp);
  731. return;
  732. }
  733. if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_PRLI,
  734. fc_rport_prli_resp, rdata,
  735. 2 * lport->r_a_tov))
  736. fc_rport_error_retry(rdata, NULL);
  737. else
  738. kref_get(&rdata->kref);
  739. }
  740. /**
  741. * fc_rport_els_rtv_resp() - Handler for Request Timeout Value (RTV) responses
  742. * @sp: The sequence the RTV was on
  743. * @fp: The RTV response frame
  744. * @rdata_arg: The remote port that sent the RTV response
  745. *
  746. * Many targets don't seem to support this.
  747. *
  748. * Locking Note: This function will be called without the rport lock
  749. * held, but it will lock, call an _enter_* function or fc_rport_error
  750. * and then unlock the rport.
  751. */
  752. static void fc_rport_rtv_resp(struct fc_seq *sp, struct fc_frame *fp,
  753. void *rdata_arg)
  754. {
  755. struct fc_rport_priv *rdata = rdata_arg;
  756. u8 op;
  757. mutex_lock(&rdata->rp_mutex);
  758. FC_RPORT_DBG(rdata, "Received a RTV %s\n", fc_els_resp_type(fp));
  759. if (rdata->rp_state != RPORT_ST_RTV) {
  760. FC_RPORT_DBG(rdata, "Received a RTV response, but in state "
  761. "%s\n", fc_rport_state(rdata));
  762. if (IS_ERR(fp))
  763. goto err;
  764. goto out;
  765. }
  766. if (IS_ERR(fp)) {
  767. fc_rport_error(rdata, fp);
  768. goto err;
  769. }
  770. op = fc_frame_payload_op(fp);
  771. if (op == ELS_LS_ACC) {
  772. struct fc_els_rtv_acc *rtv;
  773. u32 toq;
  774. u32 tov;
  775. rtv = fc_frame_payload_get(fp, sizeof(*rtv));
  776. if (rtv) {
  777. toq = ntohl(rtv->rtv_toq);
  778. tov = ntohl(rtv->rtv_r_a_tov);
  779. if (tov == 0)
  780. tov = 1;
  781. rdata->r_a_tov = tov;
  782. tov = ntohl(rtv->rtv_e_d_tov);
  783. if (toq & FC_ELS_RTV_EDRES)
  784. tov /= 1000000;
  785. if (tov == 0)
  786. tov = 1;
  787. rdata->e_d_tov = tov;
  788. }
  789. }
  790. fc_rport_enter_ready(rdata);
  791. out:
  792. fc_frame_free(fp);
  793. err:
  794. mutex_unlock(&rdata->rp_mutex);
  795. kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
  796. }
  797. /**
  798. * fc_rport_enter_rtv() - Send Request Timeout Value (RTV) request
  799. * @rdata: The remote port to send the RTV request to
  800. *
  801. * Locking Note: The rport lock is expected to be held before calling
  802. * this routine.
  803. */
  804. static void fc_rport_enter_rtv(struct fc_rport_priv *rdata)
  805. {
  806. struct fc_frame *fp;
  807. struct fc_lport *lport = rdata->local_port;
  808. FC_RPORT_DBG(rdata, "Port entered RTV state from %s state\n",
  809. fc_rport_state(rdata));
  810. fc_rport_state_enter(rdata, RPORT_ST_RTV);
  811. fp = fc_frame_alloc(lport, sizeof(struct fc_els_rtv));
  812. if (!fp) {
  813. fc_rport_error_retry(rdata, fp);
  814. return;
  815. }
  816. if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_RTV,
  817. fc_rport_rtv_resp, rdata,
  818. 2 * lport->r_a_tov))
  819. fc_rport_error_retry(rdata, NULL);
  820. else
  821. kref_get(&rdata->kref);
  822. }
  823. /**
  824. * fc_rport_enter_logo() - Send a logout (LOGO) request
  825. * @rdata: The remote port to send the LOGO request to
  826. *
  827. * Locking Note: The rport lock is expected to be held before calling
  828. * this routine.
  829. */
  830. static void fc_rport_enter_logo(struct fc_rport_priv *rdata)
  831. {
  832. struct fc_lport *lport = rdata->local_port;
  833. struct fc_frame *fp;
  834. FC_RPORT_DBG(rdata, "Port entered LOGO state from %s state\n",
  835. fc_rport_state(rdata));
  836. fc_rport_state_enter(rdata, RPORT_ST_LOGO);
  837. fp = fc_frame_alloc(lport, sizeof(struct fc_els_logo));
  838. if (!fp) {
  839. fc_rport_error_retry(rdata, fp);
  840. return;
  841. }
  842. if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_LOGO,
  843. fc_rport_logo_resp, rdata,
  844. 2 * lport->r_a_tov))
  845. fc_rport_error_retry(rdata, NULL);
  846. else
  847. kref_get(&rdata->kref);
  848. }
  849. /**
  850. * fc_rport_els_adisc_resp() - Handler for Address Discovery (ADISC) responses
  851. * @sp: The sequence the ADISC response was on
  852. * @fp: The ADISC response frame
  853. * @rdata_arg: The remote port that sent the ADISC response
  854. *
  855. * Locking Note: This function will be called without the rport lock
  856. * held, but it will lock, call an _enter_* function or fc_rport_error
  857. * and then unlock the rport.
  858. */
  859. static void fc_rport_adisc_resp(struct fc_seq *sp, struct fc_frame *fp,
  860. void *rdata_arg)
  861. {
  862. struct fc_rport_priv *rdata = rdata_arg;
  863. struct fc_els_adisc *adisc;
  864. u8 op;
  865. mutex_lock(&rdata->rp_mutex);
  866. FC_RPORT_DBG(rdata, "Received a ADISC response\n");
  867. if (rdata->rp_state != RPORT_ST_ADISC) {
  868. FC_RPORT_DBG(rdata, "Received a ADISC resp but in state %s\n",
  869. fc_rport_state(rdata));
  870. if (IS_ERR(fp))
  871. goto err;
  872. goto out;
  873. }
  874. if (IS_ERR(fp)) {
  875. fc_rport_error(rdata, fp);
  876. goto err;
  877. }
  878. /*
  879. * If address verification failed. Consider us logged out of the rport.
  880. * Since the rport is still in discovery, we want to be
  881. * logged in, so go to PLOGI state. Otherwise, go back to READY.
  882. */
  883. op = fc_frame_payload_op(fp);
  884. adisc = fc_frame_payload_get(fp, sizeof(*adisc));
  885. if (op != ELS_LS_ACC || !adisc ||
  886. ntoh24(adisc->adisc_port_id) != rdata->ids.port_id ||
  887. get_unaligned_be64(&adisc->adisc_wwpn) != rdata->ids.port_name ||
  888. get_unaligned_be64(&adisc->adisc_wwnn) != rdata->ids.node_name) {
  889. FC_RPORT_DBG(rdata, "ADISC error or mismatch\n");
  890. fc_rport_enter_plogi(rdata);
  891. } else {
  892. FC_RPORT_DBG(rdata, "ADISC OK\n");
  893. fc_rport_enter_ready(rdata);
  894. }
  895. out:
  896. fc_frame_free(fp);
  897. err:
  898. mutex_unlock(&rdata->rp_mutex);
  899. kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
  900. }
  901. /**
  902. * fc_rport_enter_adisc() - Send Address Discover (ADISC) request
  903. * @rdata: The remote port to send the ADISC request to
  904. *
  905. * Locking Note: The rport lock is expected to be held before calling
  906. * this routine.
  907. */
  908. static void fc_rport_enter_adisc(struct fc_rport_priv *rdata)
  909. {
  910. struct fc_lport *lport = rdata->local_port;
  911. struct fc_frame *fp;
  912. FC_RPORT_DBG(rdata, "sending ADISC from %s state\n",
  913. fc_rport_state(rdata));
  914. fc_rport_state_enter(rdata, RPORT_ST_ADISC);
  915. fp = fc_frame_alloc(lport, sizeof(struct fc_els_adisc));
  916. if (!fp) {
  917. fc_rport_error_retry(rdata, fp);
  918. return;
  919. }
  920. if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_ADISC,
  921. fc_rport_adisc_resp, rdata,
  922. 2 * lport->r_a_tov))
  923. fc_rport_error_retry(rdata, NULL);
  924. else
  925. kref_get(&rdata->kref);
  926. }
  927. /**
  928. * fc_rport_recv_adisc_req() - Handler for Address Discovery (ADISC) requests
  929. * @rdata: The remote port that sent the ADISC request
  930. * @sp: The sequence the ADISC request was on
  931. * @in_fp: The ADISC request frame
  932. *
  933. * Locking Note: Called with the lport and rport locks held.
  934. */
  935. static void fc_rport_recv_adisc_req(struct fc_rport_priv *rdata,
  936. struct fc_seq *sp, struct fc_frame *in_fp)
  937. {
  938. struct fc_lport *lport = rdata->local_port;
  939. struct fc_frame *fp;
  940. struct fc_exch *ep = fc_seq_exch(sp);
  941. struct fc_els_adisc *adisc;
  942. struct fc_seq_els_data rjt_data;
  943. u32 f_ctl;
  944. FC_RPORT_DBG(rdata, "Received ADISC request\n");
  945. adisc = fc_frame_payload_get(in_fp, sizeof(*adisc));
  946. if (!adisc) {
  947. rjt_data.fp = NULL;
  948. rjt_data.reason = ELS_RJT_PROT;
  949. rjt_data.explan = ELS_EXPL_INV_LEN;
  950. lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
  951. goto drop;
  952. }
  953. fp = fc_frame_alloc(lport, sizeof(*adisc));
  954. if (!fp)
  955. goto drop;
  956. fc_adisc_fill(lport, fp);
  957. adisc = fc_frame_payload_get(fp, sizeof(*adisc));
  958. adisc->adisc_cmd = ELS_LS_ACC;
  959. sp = lport->tt.seq_start_next(sp);
  960. f_ctl = FC_FC_EX_CTX | FC_FC_LAST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT;
  961. fc_fill_fc_hdr(fp, FC_RCTL_ELS_REP, ep->did, ep->sid,
  962. FC_TYPE_ELS, f_ctl, 0);
  963. lport->tt.seq_send(lport, sp, fp);
  964. drop:
  965. fc_frame_free(in_fp);
  966. }
  967. /**
  968. * fc_rport_recv_els_req() - Handler for validated ELS requests
  969. * @lport: The local port that received the ELS request
  970. * @sp: The sequence that the ELS request was on
  971. * @fp: The ELS request frame
  972. *
  973. * Handle incoming ELS requests that require port login.
  974. * The ELS opcode has already been validated by the caller.
  975. *
  976. * Locking Note: Called with the lport lock held.
  977. */
  978. static void fc_rport_recv_els_req(struct fc_lport *lport,
  979. struct fc_seq *sp, struct fc_frame *fp)
  980. {
  981. struct fc_rport_priv *rdata;
  982. struct fc_frame_header *fh;
  983. struct fc_seq_els_data els_data;
  984. els_data.fp = NULL;
  985. els_data.reason = ELS_RJT_UNAB;
  986. els_data.explan = ELS_EXPL_PLOGI_REQD;
  987. fh = fc_frame_header_get(fp);
  988. mutex_lock(&lport->disc.disc_mutex);
  989. rdata = lport->tt.rport_lookup(lport, ntoh24(fh->fh_s_id));
  990. if (!rdata) {
  991. mutex_unlock(&lport->disc.disc_mutex);
  992. goto reject;
  993. }
  994. mutex_lock(&rdata->rp_mutex);
  995. mutex_unlock(&lport->disc.disc_mutex);
  996. switch (rdata->rp_state) {
  997. case RPORT_ST_PRLI:
  998. case RPORT_ST_RTV:
  999. case RPORT_ST_READY:
  1000. case RPORT_ST_ADISC:
  1001. break;
  1002. default:
  1003. mutex_unlock(&rdata->rp_mutex);
  1004. goto reject;
  1005. }
  1006. switch (fc_frame_payload_op(fp)) {
  1007. case ELS_PRLI:
  1008. fc_rport_recv_prli_req(rdata, sp, fp);
  1009. break;
  1010. case ELS_PRLO:
  1011. fc_rport_recv_prlo_req(rdata, sp, fp);
  1012. break;
  1013. case ELS_ADISC:
  1014. fc_rport_recv_adisc_req(rdata, sp, fp);
  1015. break;
  1016. case ELS_RRQ:
  1017. els_data.fp = fp;
  1018. lport->tt.seq_els_rsp_send(sp, ELS_RRQ, &els_data);
  1019. break;
  1020. case ELS_REC:
  1021. els_data.fp = fp;
  1022. lport->tt.seq_els_rsp_send(sp, ELS_REC, &els_data);
  1023. break;
  1024. default:
  1025. fc_frame_free(fp); /* can't happen */
  1026. break;
  1027. }
  1028. mutex_unlock(&rdata->rp_mutex);
  1029. return;
  1030. reject:
  1031. lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &els_data);
  1032. fc_frame_free(fp);
  1033. }
  1034. /**
  1035. * fc_rport_recv_req() - Handler for requests
  1036. * @sp: The sequence the request was on
  1037. * @fp: The request frame
  1038. * @lport: The local port that received the request
  1039. *
  1040. * Locking Note: Called with the lport lock held.
  1041. */
  1042. void fc_rport_recv_req(struct fc_seq *sp, struct fc_frame *fp,
  1043. struct fc_lport *lport)
  1044. {
  1045. struct fc_seq_els_data els_data;
  1046. /*
  1047. * Handle PLOGI and LOGO requests separately, since they
  1048. * don't require prior login.
  1049. * Check for unsupported opcodes first and reject them.
  1050. * For some ops, it would be incorrect to reject with "PLOGI required".
  1051. */
  1052. switch (fc_frame_payload_op(fp)) {
  1053. case ELS_PLOGI:
  1054. fc_rport_recv_plogi_req(lport, sp, fp);
  1055. break;
  1056. case ELS_LOGO:
  1057. fc_rport_recv_logo_req(lport, sp, fp);
  1058. break;
  1059. case ELS_PRLI:
  1060. case ELS_PRLO:
  1061. case ELS_ADISC:
  1062. case ELS_RRQ:
  1063. case ELS_REC:
  1064. fc_rport_recv_els_req(lport, sp, fp);
  1065. break;
  1066. default:
  1067. fc_frame_free(fp);
  1068. els_data.fp = NULL;
  1069. els_data.reason = ELS_RJT_UNSUP;
  1070. els_data.explan = ELS_EXPL_NONE;
  1071. lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &els_data);
  1072. break;
  1073. }
  1074. }
  1075. /**
  1076. * fc_rport_recv_plogi_req() - Handler for Port Login (PLOGI) requests
  1077. * @lport: The local port that received the PLOGI request
  1078. * @sp: The sequence that the PLOGI request was on
  1079. * @rx_fp: The PLOGI request frame
  1080. *
  1081. * Locking Note: The rport lock is held before calling this function.
  1082. */
  1083. static void fc_rport_recv_plogi_req(struct fc_lport *lport,
  1084. struct fc_seq *sp, struct fc_frame *rx_fp)
  1085. {
  1086. struct fc_disc *disc;
  1087. struct fc_rport_priv *rdata;
  1088. struct fc_frame *fp = rx_fp;
  1089. struct fc_exch *ep;
  1090. struct fc_frame_header *fh;
  1091. struct fc_els_flogi *pl;
  1092. struct fc_seq_els_data rjt_data;
  1093. u32 sid, f_ctl;
  1094. rjt_data.fp = NULL;
  1095. fh = fc_frame_header_get(fp);
  1096. sid = ntoh24(fh->fh_s_id);
  1097. FC_RPORT_ID_DBG(lport, sid, "Received PLOGI request\n");
  1098. pl = fc_frame_payload_get(fp, sizeof(*pl));
  1099. if (!pl) {
  1100. FC_RPORT_ID_DBG(lport, sid, "Received PLOGI too short\n");
  1101. rjt_data.reason = ELS_RJT_PROT;
  1102. rjt_data.explan = ELS_EXPL_INV_LEN;
  1103. goto reject;
  1104. }
  1105. disc = &lport->disc;
  1106. mutex_lock(&disc->disc_mutex);
  1107. rdata = lport->tt.rport_create(lport, sid);
  1108. if (!rdata) {
  1109. mutex_unlock(&disc->disc_mutex);
  1110. rjt_data.reason = ELS_RJT_UNAB;
  1111. rjt_data.explan = ELS_EXPL_INSUF_RES;
  1112. goto reject;
  1113. }
  1114. mutex_lock(&rdata->rp_mutex);
  1115. mutex_unlock(&disc->disc_mutex);
  1116. rdata->ids.port_name = get_unaligned_be64(&pl->fl_wwpn);
  1117. rdata->ids.node_name = get_unaligned_be64(&pl->fl_wwnn);
  1118. /*
  1119. * If the rport was just created, possibly due to the incoming PLOGI,
  1120. * set the state appropriately and accept the PLOGI.
  1121. *
  1122. * If we had also sent a PLOGI, and if the received PLOGI is from a
  1123. * higher WWPN, we accept it, otherwise an LS_RJT is sent with reason
  1124. * "command already in progress".
  1125. *
  1126. * XXX TBD: If the session was ready before, the PLOGI should result in
  1127. * all outstanding exchanges being reset.
  1128. */
  1129. switch (rdata->rp_state) {
  1130. case RPORT_ST_INIT:
  1131. FC_RPORT_DBG(rdata, "Received PLOGI in INIT state\n");
  1132. break;
  1133. case RPORT_ST_PLOGI:
  1134. FC_RPORT_DBG(rdata, "Received PLOGI in PLOGI state\n");
  1135. if (rdata->ids.port_name < lport->wwpn) {
  1136. mutex_unlock(&rdata->rp_mutex);
  1137. rjt_data.reason = ELS_RJT_INPROG;
  1138. rjt_data.explan = ELS_EXPL_NONE;
  1139. goto reject;
  1140. }
  1141. break;
  1142. case RPORT_ST_PRLI:
  1143. case RPORT_ST_RTV:
  1144. case RPORT_ST_READY:
  1145. case RPORT_ST_ADISC:
  1146. FC_RPORT_DBG(rdata, "Received PLOGI in logged-in state %d "
  1147. "- ignored for now\n", rdata->rp_state);
  1148. /* XXX TBD - should reset */
  1149. break;
  1150. case RPORT_ST_DELETE:
  1151. case RPORT_ST_LOGO:
  1152. case RPORT_ST_RESTART:
  1153. FC_RPORT_DBG(rdata, "Received PLOGI in state %s - send busy\n",
  1154. fc_rport_state(rdata));
  1155. mutex_unlock(&rdata->rp_mutex);
  1156. rjt_data.reason = ELS_RJT_BUSY;
  1157. rjt_data.explan = ELS_EXPL_NONE;
  1158. goto reject;
  1159. }
  1160. /*
  1161. * Get session payload size from incoming PLOGI.
  1162. */
  1163. rdata->maxframe_size = fc_plogi_get_maxframe(pl, lport->mfs);
  1164. fc_frame_free(rx_fp);
  1165. /*
  1166. * Send LS_ACC. If this fails, the originator should retry.
  1167. */
  1168. sp = lport->tt.seq_start_next(sp);
  1169. if (!sp)
  1170. goto out;
  1171. fp = fc_frame_alloc(lport, sizeof(*pl));
  1172. if (!fp)
  1173. goto out;
  1174. fc_plogi_fill(lport, fp, ELS_LS_ACC);
  1175. f_ctl = FC_FC_EX_CTX | FC_FC_LAST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT;
  1176. ep = fc_seq_exch(sp);
  1177. fc_fill_fc_hdr(fp, FC_RCTL_ELS_REP, ep->did, ep->sid,
  1178. FC_TYPE_ELS, f_ctl, 0);
  1179. lport->tt.seq_send(lport, sp, fp);
  1180. fc_rport_enter_prli(rdata);
  1181. out:
  1182. mutex_unlock(&rdata->rp_mutex);
  1183. return;
  1184. reject:
  1185. lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
  1186. fc_frame_free(fp);
  1187. }
  1188. /**
  1189. * fc_rport_recv_prli_req() - Handler for process login (PRLI) requests
  1190. * @rdata: The remote port that sent the PRLI request
  1191. * @sp: The sequence that the PRLI was on
  1192. * @rx_fp: The PRLI request frame
  1193. *
  1194. * Locking Note: The rport lock is exected to be held before calling
  1195. * this function.
  1196. */
  1197. static void fc_rport_recv_prli_req(struct fc_rport_priv *rdata,
  1198. struct fc_seq *sp, struct fc_frame *rx_fp)
  1199. {
  1200. struct fc_lport *lport = rdata->local_port;
  1201. struct fc_exch *ep;
  1202. struct fc_frame *fp;
  1203. struct fc_frame_header *fh;
  1204. struct {
  1205. struct fc_els_prli prli;
  1206. struct fc_els_spp spp;
  1207. } *pp;
  1208. struct fc_els_spp *rspp; /* request service param page */
  1209. struct fc_els_spp *spp; /* response spp */
  1210. unsigned int len;
  1211. unsigned int plen;
  1212. enum fc_els_rjt_reason reason = ELS_RJT_UNAB;
  1213. enum fc_els_rjt_explan explan = ELS_EXPL_NONE;
  1214. enum fc_els_spp_resp resp;
  1215. struct fc_seq_els_data rjt_data;
  1216. u32 f_ctl;
  1217. u32 fcp_parm;
  1218. u32 roles = FC_RPORT_ROLE_UNKNOWN;
  1219. rjt_data.fp = NULL;
  1220. fh = fc_frame_header_get(rx_fp);
  1221. FC_RPORT_DBG(rdata, "Received PRLI request while in state %s\n",
  1222. fc_rport_state(rdata));
  1223. switch (rdata->rp_state) {
  1224. case RPORT_ST_PRLI:
  1225. case RPORT_ST_RTV:
  1226. case RPORT_ST_READY:
  1227. case RPORT_ST_ADISC:
  1228. reason = ELS_RJT_NONE;
  1229. break;
  1230. default:
  1231. fc_frame_free(rx_fp);
  1232. return;
  1233. break;
  1234. }
  1235. len = fr_len(rx_fp) - sizeof(*fh);
  1236. pp = fc_frame_payload_get(rx_fp, sizeof(*pp));
  1237. if (pp == NULL) {
  1238. reason = ELS_RJT_PROT;
  1239. explan = ELS_EXPL_INV_LEN;
  1240. } else {
  1241. plen = ntohs(pp->prli.prli_len);
  1242. if ((plen % 4) != 0 || plen > len) {
  1243. reason = ELS_RJT_PROT;
  1244. explan = ELS_EXPL_INV_LEN;
  1245. } else if (plen < len) {
  1246. len = plen;
  1247. }
  1248. plen = pp->prli.prli_spp_len;
  1249. if ((plen % 4) != 0 || plen < sizeof(*spp) ||
  1250. plen > len || len < sizeof(*pp)) {
  1251. reason = ELS_RJT_PROT;
  1252. explan = ELS_EXPL_INV_LEN;
  1253. }
  1254. rspp = &pp->spp;
  1255. }
  1256. if (reason != ELS_RJT_NONE ||
  1257. (fp = fc_frame_alloc(lport, len)) == NULL) {
  1258. rjt_data.reason = reason;
  1259. rjt_data.explan = explan;
  1260. lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
  1261. } else {
  1262. sp = lport->tt.seq_start_next(sp);
  1263. WARN_ON(!sp);
  1264. pp = fc_frame_payload_get(fp, len);
  1265. WARN_ON(!pp);
  1266. memset(pp, 0, len);
  1267. pp->prli.prli_cmd = ELS_LS_ACC;
  1268. pp->prli.prli_spp_len = plen;
  1269. pp->prli.prli_len = htons(len);
  1270. len -= sizeof(struct fc_els_prli);
  1271. /* reinitialize remote port roles */
  1272. rdata->ids.roles = FC_RPORT_ROLE_UNKNOWN;
  1273. /*
  1274. * Go through all the service parameter pages and build
  1275. * response. If plen indicates longer SPP than standard,
  1276. * use that. The entire response has been pre-cleared above.
  1277. */
  1278. spp = &pp->spp;
  1279. while (len >= plen) {
  1280. spp->spp_type = rspp->spp_type;
  1281. spp->spp_type_ext = rspp->spp_type_ext;
  1282. spp->spp_flags = rspp->spp_flags & FC_SPP_EST_IMG_PAIR;
  1283. resp = FC_SPP_RESP_ACK;
  1284. if (rspp->spp_flags & FC_SPP_RPA_VAL)
  1285. resp = FC_SPP_RESP_NO_PA;
  1286. switch (rspp->spp_type) {
  1287. case 0: /* common to all FC-4 types */
  1288. break;
  1289. case FC_TYPE_FCP:
  1290. fcp_parm = ntohl(rspp->spp_params);
  1291. if (fcp_parm & FCP_SPPF_RETRY)
  1292. rdata->flags |= FC_RP_FLAGS_RETRY;
  1293. rdata->supported_classes = FC_COS_CLASS3;
  1294. if (fcp_parm & FCP_SPPF_INIT_FCN)
  1295. roles |= FC_RPORT_ROLE_FCP_INITIATOR;
  1296. if (fcp_parm & FCP_SPPF_TARG_FCN)
  1297. roles |= FC_RPORT_ROLE_FCP_TARGET;
  1298. rdata->ids.roles = roles;
  1299. spp->spp_params =
  1300. htonl(lport->service_params);
  1301. break;
  1302. default:
  1303. resp = FC_SPP_RESP_INVL;
  1304. break;
  1305. }
  1306. spp->spp_flags |= resp;
  1307. len -= plen;
  1308. rspp = (struct fc_els_spp *)((char *)rspp + plen);
  1309. spp = (struct fc_els_spp *)((char *)spp + plen);
  1310. }
  1311. /*
  1312. * Send LS_ACC. If this fails, the originator should retry.
  1313. */
  1314. f_ctl = FC_FC_EX_CTX | FC_FC_LAST_SEQ;
  1315. f_ctl |= FC_FC_END_SEQ | FC_FC_SEQ_INIT;
  1316. ep = fc_seq_exch(sp);
  1317. fc_fill_fc_hdr(fp, FC_RCTL_ELS_REP, ep->did, ep->sid,
  1318. FC_TYPE_ELS, f_ctl, 0);
  1319. lport->tt.seq_send(lport, sp, fp);
  1320. /*
  1321. * Get lock and re-check state.
  1322. */
  1323. switch (rdata->rp_state) {
  1324. case RPORT_ST_PRLI:
  1325. fc_rport_enter_ready(rdata);
  1326. break;
  1327. case RPORT_ST_READY:
  1328. case RPORT_ST_ADISC:
  1329. break;
  1330. default:
  1331. break;
  1332. }
  1333. }
  1334. fc_frame_free(rx_fp);
  1335. }
  1336. /**
  1337. * fc_rport_recv_prlo_req() - Handler for process logout (PRLO) requests
  1338. * @rdata: The remote port that sent the PRLO request
  1339. * @sp: The sequence that the PRLO was on
  1340. * @fp: The PRLO request frame
  1341. *
  1342. * Locking Note: The rport lock is exected to be held before calling
  1343. * this function.
  1344. */
  1345. static void fc_rport_recv_prlo_req(struct fc_rport_priv *rdata,
  1346. struct fc_seq *sp,
  1347. struct fc_frame *fp)
  1348. {
  1349. struct fc_lport *lport = rdata->local_port;
  1350. struct fc_frame_header *fh;
  1351. struct fc_seq_els_data rjt_data;
  1352. fh = fc_frame_header_get(fp);
  1353. FC_RPORT_DBG(rdata, "Received PRLO request while in state %s\n",
  1354. fc_rport_state(rdata));
  1355. rjt_data.fp = NULL;
  1356. rjt_data.reason = ELS_RJT_UNAB;
  1357. rjt_data.explan = ELS_EXPL_NONE;
  1358. lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
  1359. fc_frame_free(fp);
  1360. }
  1361. /**
  1362. * fc_rport_recv_logo_req() - Handler for logout (LOGO) requests
  1363. * @lport: The local port that received the LOGO request
  1364. * @sp: The sequence that the LOGO request was on
  1365. * @fp: The LOGO request frame
  1366. *
  1367. * Locking Note: The rport lock is exected to be held before calling
  1368. * this function.
  1369. */
  1370. static void fc_rport_recv_logo_req(struct fc_lport *lport,
  1371. struct fc_seq *sp,
  1372. struct fc_frame *fp)
  1373. {
  1374. struct fc_frame_header *fh;
  1375. struct fc_rport_priv *rdata;
  1376. u32 sid;
  1377. lport->tt.seq_els_rsp_send(sp, ELS_LS_ACC, NULL);
  1378. fh = fc_frame_header_get(fp);
  1379. sid = ntoh24(fh->fh_s_id);
  1380. mutex_lock(&lport->disc.disc_mutex);
  1381. rdata = lport->tt.rport_lookup(lport, sid);
  1382. if (rdata) {
  1383. mutex_lock(&rdata->rp_mutex);
  1384. FC_RPORT_DBG(rdata, "Received LOGO request while in state %s\n",
  1385. fc_rport_state(rdata));
  1386. fc_rport_enter_delete(rdata, RPORT_EV_LOGO);
  1387. /*
  1388. * If the remote port was created due to discovery, set state
  1389. * to log back in. It may have seen a stale RSCN about us.
  1390. */
  1391. if (rdata->disc_id)
  1392. fc_rport_state_enter(rdata, RPORT_ST_RESTART);
  1393. mutex_unlock(&rdata->rp_mutex);
  1394. } else
  1395. FC_RPORT_ID_DBG(lport, sid,
  1396. "Received LOGO from non-logged-in port\n");
  1397. mutex_unlock(&lport->disc.disc_mutex);
  1398. fc_frame_free(fp);
  1399. }
  1400. /**
  1401. * fc_rport_flush_queue() - Flush the rport_event_queue
  1402. */
  1403. static void fc_rport_flush_queue(void)
  1404. {
  1405. flush_workqueue(rport_event_queue);
  1406. }
  1407. /**
  1408. * fc_rport_init() - Initialize the remote port layer for a local port
  1409. * @lport: The local port to initialize the remote port layer for
  1410. */
  1411. int fc_rport_init(struct fc_lport *lport)
  1412. {
  1413. if (!lport->tt.rport_lookup)
  1414. lport->tt.rport_lookup = fc_rport_lookup;
  1415. if (!lport->tt.rport_create)
  1416. lport->tt.rport_create = fc_rport_create;
  1417. if (!lport->tt.rport_login)
  1418. lport->tt.rport_login = fc_rport_login;
  1419. if (!lport->tt.rport_logoff)
  1420. lport->tt.rport_logoff = fc_rport_logoff;
  1421. if (!lport->tt.rport_recv_req)
  1422. lport->tt.rport_recv_req = fc_rport_recv_req;
  1423. if (!lport->tt.rport_flush_queue)
  1424. lport->tt.rport_flush_queue = fc_rport_flush_queue;
  1425. if (!lport->tt.rport_destroy)
  1426. lport->tt.rport_destroy = fc_rport_destroy;
  1427. return 0;
  1428. }
  1429. EXPORT_SYMBOL(fc_rport_init);
  1430. /**
  1431. * fc_setup_rport() - Initialize the rport_event_queue
  1432. */
  1433. int fc_setup_rport()
  1434. {
  1435. rport_event_queue = create_singlethread_workqueue("fc_rport_eq");
  1436. if (!rport_event_queue)
  1437. return -ENOMEM;
  1438. return 0;
  1439. }
  1440. /**
  1441. * fc_destroy_rport() - Destroy the rport_event_queue
  1442. */
  1443. void fc_destroy_rport()
  1444. {
  1445. destroy_workqueue(rport_event_queue);
  1446. }
  1447. /**
  1448. * fc_rport_terminate_io() - Stop all outstanding I/O on a remote port
  1449. * @rport: The remote port whose I/O should be terminated
  1450. */
  1451. void fc_rport_terminate_io(struct fc_rport *rport)
  1452. {
  1453. struct fc_rport_libfc_priv *rpriv = rport->dd_data;
  1454. struct fc_lport *lport = rpriv->local_port;
  1455. lport->tt.exch_mgr_reset(lport, 0, rport->port_id);
  1456. lport->tt.exch_mgr_reset(lport, rport->port_id, 0);
  1457. }
  1458. EXPORT_SYMBOL(fc_rport_terminate_io);