fc_rport.c 50 KB

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