fc_rport.c 46 KB

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