fc_rport.c 54 KB

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