fc_rport.c 47 KB

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