fc_rport.c 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717
  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. u8 resp_code = 0;
  626. mutex_lock(&rdata->rp_mutex);
  627. FC_RPORT_DBG(rdata, "Received a PRLI %s\n", fc_els_resp_type(fp));
  628. if (rdata->rp_state != RPORT_ST_PRLI) {
  629. FC_RPORT_DBG(rdata, "Received a PRLI response, but in state "
  630. "%s\n", fc_rport_state(rdata));
  631. if (IS_ERR(fp))
  632. goto err;
  633. goto out;
  634. }
  635. if (IS_ERR(fp)) {
  636. fc_rport_error_retry(rdata, fp);
  637. goto err;
  638. }
  639. /* reinitialize remote port roles */
  640. rdata->ids.roles = FC_RPORT_ROLE_UNKNOWN;
  641. op = fc_frame_payload_op(fp);
  642. if (op == ELS_LS_ACC) {
  643. pp = fc_frame_payload_get(fp, sizeof(*pp));
  644. if (!pp)
  645. goto out;
  646. resp_code = (pp->spp.spp_flags & FC_SPP_RESP_MASK);
  647. FC_RPORT_DBG(rdata, "PRLI spp_flags = 0x%x\n",
  648. pp->spp.spp_flags);
  649. if (resp_code != FC_SPP_RESP_ACK) {
  650. if (resp_code == FC_SPP_RESP_CONF)
  651. fc_rport_error(rdata, fp);
  652. else
  653. fc_rport_error_retry(rdata, fp);
  654. goto out;
  655. }
  656. if (pp->prli.prli_spp_len < sizeof(pp->spp))
  657. goto out;
  658. fcp_parm = ntohl(pp->spp.spp_params);
  659. if (fcp_parm & FCP_SPPF_RETRY)
  660. rdata->flags |= FC_RP_FLAGS_RETRY;
  661. rdata->supported_classes = FC_COS_CLASS3;
  662. if (fcp_parm & FCP_SPPF_INIT_FCN)
  663. roles |= FC_RPORT_ROLE_FCP_INITIATOR;
  664. if (fcp_parm & FCP_SPPF_TARG_FCN)
  665. roles |= FC_RPORT_ROLE_FCP_TARGET;
  666. rdata->ids.roles = roles;
  667. fc_rport_enter_rtv(rdata);
  668. } else {
  669. FC_RPORT_DBG(rdata, "Bad ELS response for PRLI command\n");
  670. fc_rport_error_retry(rdata, fp);
  671. }
  672. out:
  673. fc_frame_free(fp);
  674. err:
  675. mutex_unlock(&rdata->rp_mutex);
  676. kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
  677. }
  678. /**
  679. * fc_rport_logo_resp() - Handler for logout (LOGO) responses
  680. * @sp: The sequence the LOGO was on
  681. * @fp: The LOGO response frame
  682. * @rdata_arg: The remote port that sent the LOGO response
  683. *
  684. * Locking Note: This function will be called without the rport lock
  685. * held, but it will lock, call an _enter_* function or fc_rport_error
  686. * and then unlock the rport.
  687. */
  688. static void fc_rport_logo_resp(struct fc_seq *sp, struct fc_frame *fp,
  689. void *rdata_arg)
  690. {
  691. struct fc_rport_priv *rdata = rdata_arg;
  692. u8 op;
  693. mutex_lock(&rdata->rp_mutex);
  694. FC_RPORT_DBG(rdata, "Received a LOGO %s\n", fc_els_resp_type(fp));
  695. if (rdata->rp_state != RPORT_ST_LOGO) {
  696. FC_RPORT_DBG(rdata, "Received a LOGO response, but in state "
  697. "%s\n", fc_rport_state(rdata));
  698. if (IS_ERR(fp))
  699. goto err;
  700. goto out;
  701. }
  702. if (IS_ERR(fp)) {
  703. fc_rport_error_retry(rdata, fp);
  704. goto err;
  705. }
  706. op = fc_frame_payload_op(fp);
  707. if (op != ELS_LS_ACC)
  708. FC_RPORT_DBG(rdata, "Bad ELS response op %x for LOGO command\n",
  709. op);
  710. fc_rport_enter_delete(rdata, RPORT_EV_LOGO);
  711. out:
  712. fc_frame_free(fp);
  713. err:
  714. mutex_unlock(&rdata->rp_mutex);
  715. kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
  716. }
  717. /**
  718. * fc_rport_enter_prli() - Send Process Login (PRLI) request
  719. * @rdata: The remote port to send the PRLI request to
  720. *
  721. * Locking Note: The rport lock is expected to be held before calling
  722. * this routine.
  723. */
  724. static void fc_rport_enter_prli(struct fc_rport_priv *rdata)
  725. {
  726. struct fc_lport *lport = rdata->local_port;
  727. struct {
  728. struct fc_els_prli prli;
  729. struct fc_els_spp spp;
  730. } *pp;
  731. struct fc_frame *fp;
  732. /*
  733. * If the rport is one of the well known addresses
  734. * we skip PRLI and RTV and go straight to READY.
  735. */
  736. if (rdata->ids.port_id >= FC_FID_DOM_MGR) {
  737. fc_rport_enter_ready(rdata);
  738. return;
  739. }
  740. FC_RPORT_DBG(rdata, "Port entered PRLI state from %s state\n",
  741. fc_rport_state(rdata));
  742. fc_rport_state_enter(rdata, RPORT_ST_PRLI);
  743. fp = fc_frame_alloc(lport, sizeof(*pp));
  744. if (!fp) {
  745. fc_rport_error_retry(rdata, fp);
  746. return;
  747. }
  748. if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_PRLI,
  749. fc_rport_prli_resp, rdata,
  750. 2 * lport->r_a_tov))
  751. fc_rport_error_retry(rdata, NULL);
  752. else
  753. kref_get(&rdata->kref);
  754. }
  755. /**
  756. * fc_rport_els_rtv_resp() - Handler for Request Timeout Value (RTV) responses
  757. * @sp: The sequence the RTV was on
  758. * @fp: The RTV response frame
  759. * @rdata_arg: The remote port that sent the RTV response
  760. *
  761. * Many targets don't seem to support this.
  762. *
  763. * Locking Note: This function will be called without the rport lock
  764. * held, but it will lock, call an _enter_* function or fc_rport_error
  765. * and then unlock the rport.
  766. */
  767. static void fc_rport_rtv_resp(struct fc_seq *sp, struct fc_frame *fp,
  768. void *rdata_arg)
  769. {
  770. struct fc_rport_priv *rdata = rdata_arg;
  771. u8 op;
  772. mutex_lock(&rdata->rp_mutex);
  773. FC_RPORT_DBG(rdata, "Received a RTV %s\n", fc_els_resp_type(fp));
  774. if (rdata->rp_state != RPORT_ST_RTV) {
  775. FC_RPORT_DBG(rdata, "Received a RTV response, but in state "
  776. "%s\n", fc_rport_state(rdata));
  777. if (IS_ERR(fp))
  778. goto err;
  779. goto out;
  780. }
  781. if (IS_ERR(fp)) {
  782. fc_rport_error(rdata, fp);
  783. goto err;
  784. }
  785. op = fc_frame_payload_op(fp);
  786. if (op == ELS_LS_ACC) {
  787. struct fc_els_rtv_acc *rtv;
  788. u32 toq;
  789. u32 tov;
  790. rtv = fc_frame_payload_get(fp, sizeof(*rtv));
  791. if (rtv) {
  792. toq = ntohl(rtv->rtv_toq);
  793. tov = ntohl(rtv->rtv_r_a_tov);
  794. if (tov == 0)
  795. tov = 1;
  796. rdata->r_a_tov = tov;
  797. tov = ntohl(rtv->rtv_e_d_tov);
  798. if (toq & FC_ELS_RTV_EDRES)
  799. tov /= 1000000;
  800. if (tov == 0)
  801. tov = 1;
  802. rdata->e_d_tov = tov;
  803. }
  804. }
  805. fc_rport_enter_ready(rdata);
  806. out:
  807. fc_frame_free(fp);
  808. err:
  809. mutex_unlock(&rdata->rp_mutex);
  810. kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
  811. }
  812. /**
  813. * fc_rport_enter_rtv() - Send Request Timeout Value (RTV) request
  814. * @rdata: The remote port to send the RTV request to
  815. *
  816. * Locking Note: The rport lock is expected to be held before calling
  817. * this routine.
  818. */
  819. static void fc_rport_enter_rtv(struct fc_rport_priv *rdata)
  820. {
  821. struct fc_frame *fp;
  822. struct fc_lport *lport = rdata->local_port;
  823. FC_RPORT_DBG(rdata, "Port entered RTV state from %s state\n",
  824. fc_rport_state(rdata));
  825. fc_rport_state_enter(rdata, RPORT_ST_RTV);
  826. fp = fc_frame_alloc(lport, sizeof(struct fc_els_rtv));
  827. if (!fp) {
  828. fc_rport_error_retry(rdata, fp);
  829. return;
  830. }
  831. if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_RTV,
  832. fc_rport_rtv_resp, rdata,
  833. 2 * lport->r_a_tov))
  834. fc_rport_error_retry(rdata, NULL);
  835. else
  836. kref_get(&rdata->kref);
  837. }
  838. /**
  839. * fc_rport_enter_logo() - Send a logout (LOGO) request
  840. * @rdata: The remote port to send the LOGO request to
  841. *
  842. * Locking Note: The rport lock is expected to be held before calling
  843. * this routine.
  844. */
  845. static void fc_rport_enter_logo(struct fc_rport_priv *rdata)
  846. {
  847. struct fc_lport *lport = rdata->local_port;
  848. struct fc_frame *fp;
  849. FC_RPORT_DBG(rdata, "Port entered LOGO state from %s state\n",
  850. fc_rport_state(rdata));
  851. fc_rport_state_enter(rdata, RPORT_ST_LOGO);
  852. fp = fc_frame_alloc(lport, sizeof(struct fc_els_logo));
  853. if (!fp) {
  854. fc_rport_error_retry(rdata, fp);
  855. return;
  856. }
  857. if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_LOGO,
  858. fc_rport_logo_resp, rdata,
  859. 2 * lport->r_a_tov))
  860. fc_rport_error_retry(rdata, NULL);
  861. else
  862. kref_get(&rdata->kref);
  863. }
  864. /**
  865. * fc_rport_els_adisc_resp() - Handler for Address Discovery (ADISC) responses
  866. * @sp: The sequence the ADISC response was on
  867. * @fp: The ADISC response frame
  868. * @rdata_arg: The remote port that sent the ADISC response
  869. *
  870. * Locking Note: This function will be called without the rport lock
  871. * held, but it will lock, call an _enter_* function or fc_rport_error
  872. * and then unlock the rport.
  873. */
  874. static void fc_rport_adisc_resp(struct fc_seq *sp, struct fc_frame *fp,
  875. void *rdata_arg)
  876. {
  877. struct fc_rport_priv *rdata = rdata_arg;
  878. struct fc_els_adisc *adisc;
  879. u8 op;
  880. mutex_lock(&rdata->rp_mutex);
  881. FC_RPORT_DBG(rdata, "Received a ADISC response\n");
  882. if (rdata->rp_state != RPORT_ST_ADISC) {
  883. FC_RPORT_DBG(rdata, "Received a ADISC resp but in state %s\n",
  884. fc_rport_state(rdata));
  885. if (IS_ERR(fp))
  886. goto err;
  887. goto out;
  888. }
  889. if (IS_ERR(fp)) {
  890. fc_rport_error(rdata, fp);
  891. goto err;
  892. }
  893. /*
  894. * If address verification failed. Consider us logged out of the rport.
  895. * Since the rport is still in discovery, we want to be
  896. * logged in, so go to PLOGI state. Otherwise, go back to READY.
  897. */
  898. op = fc_frame_payload_op(fp);
  899. adisc = fc_frame_payload_get(fp, sizeof(*adisc));
  900. if (op != ELS_LS_ACC || !adisc ||
  901. ntoh24(adisc->adisc_port_id) != rdata->ids.port_id ||
  902. get_unaligned_be64(&adisc->adisc_wwpn) != rdata->ids.port_name ||
  903. get_unaligned_be64(&adisc->adisc_wwnn) != rdata->ids.node_name) {
  904. FC_RPORT_DBG(rdata, "ADISC error or mismatch\n");
  905. fc_rport_enter_plogi(rdata);
  906. } else {
  907. FC_RPORT_DBG(rdata, "ADISC OK\n");
  908. fc_rport_enter_ready(rdata);
  909. }
  910. out:
  911. fc_frame_free(fp);
  912. err:
  913. mutex_unlock(&rdata->rp_mutex);
  914. kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
  915. }
  916. /**
  917. * fc_rport_enter_adisc() - Send Address Discover (ADISC) request
  918. * @rdata: The remote port to send the ADISC request to
  919. *
  920. * Locking Note: The rport lock is expected to be held before calling
  921. * this routine.
  922. */
  923. static void fc_rport_enter_adisc(struct fc_rport_priv *rdata)
  924. {
  925. struct fc_lport *lport = rdata->local_port;
  926. struct fc_frame *fp;
  927. FC_RPORT_DBG(rdata, "sending ADISC from %s state\n",
  928. fc_rport_state(rdata));
  929. fc_rport_state_enter(rdata, RPORT_ST_ADISC);
  930. fp = fc_frame_alloc(lport, sizeof(struct fc_els_adisc));
  931. if (!fp) {
  932. fc_rport_error_retry(rdata, fp);
  933. return;
  934. }
  935. if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_ADISC,
  936. fc_rport_adisc_resp, rdata,
  937. 2 * lport->r_a_tov))
  938. fc_rport_error_retry(rdata, NULL);
  939. else
  940. kref_get(&rdata->kref);
  941. }
  942. /**
  943. * fc_rport_recv_adisc_req() - Handler for Address Discovery (ADISC) requests
  944. * @rdata: The remote port that sent the ADISC request
  945. * @sp: The sequence the ADISC request was on
  946. * @in_fp: The ADISC request frame
  947. *
  948. * Locking Note: Called with the lport and rport locks held.
  949. */
  950. static void fc_rport_recv_adisc_req(struct fc_rport_priv *rdata,
  951. struct fc_seq *sp, struct fc_frame *in_fp)
  952. {
  953. struct fc_lport *lport = rdata->local_port;
  954. struct fc_frame *fp;
  955. struct fc_exch *ep = fc_seq_exch(sp);
  956. struct fc_els_adisc *adisc;
  957. struct fc_seq_els_data rjt_data;
  958. u32 f_ctl;
  959. FC_RPORT_DBG(rdata, "Received ADISC request\n");
  960. adisc = fc_frame_payload_get(in_fp, sizeof(*adisc));
  961. if (!adisc) {
  962. rjt_data.fp = NULL;
  963. rjt_data.reason = ELS_RJT_PROT;
  964. rjt_data.explan = ELS_EXPL_INV_LEN;
  965. lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
  966. goto drop;
  967. }
  968. fp = fc_frame_alloc(lport, sizeof(*adisc));
  969. if (!fp)
  970. goto drop;
  971. fc_adisc_fill(lport, fp);
  972. adisc = fc_frame_payload_get(fp, sizeof(*adisc));
  973. adisc->adisc_cmd = ELS_LS_ACC;
  974. sp = lport->tt.seq_start_next(sp);
  975. f_ctl = FC_FC_EX_CTX | FC_FC_LAST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT;
  976. fc_fill_fc_hdr(fp, FC_RCTL_ELS_REP, ep->did, ep->sid,
  977. FC_TYPE_ELS, f_ctl, 0);
  978. lport->tt.seq_send(lport, sp, fp);
  979. drop:
  980. fc_frame_free(in_fp);
  981. }
  982. /**
  983. * fc_rport_recv_rls_req() - Handle received Read Link Status request
  984. * @rdata: The remote port that sent the RLS request
  985. * @sp: The sequence that the RLS was on
  986. * @rx_fp: The PRLI request frame
  987. *
  988. * Locking Note: The rport lock is expected to be held before calling
  989. * this function.
  990. */
  991. static void fc_rport_recv_rls_req(struct fc_rport_priv *rdata,
  992. struct fc_seq *sp, struct fc_frame *rx_fp)
  993. {
  994. struct fc_lport *lport = rdata->local_port;
  995. struct fc_frame *fp;
  996. struct fc_exch *ep = fc_seq_exch(sp);
  997. struct fc_els_rls *rls;
  998. struct fc_els_rls_resp *rsp;
  999. struct fc_els_lesb *lesb;
  1000. struct fc_seq_els_data rjt_data;
  1001. struct fc_host_statistics *hst;
  1002. u32 f_ctl;
  1003. FC_RPORT_DBG(rdata, "Received RLS request while in state %s\n",
  1004. fc_rport_state(rdata));
  1005. rls = fc_frame_payload_get(rx_fp, sizeof(*rls));
  1006. if (!rls) {
  1007. rjt_data.reason = ELS_RJT_PROT;
  1008. rjt_data.explan = ELS_EXPL_INV_LEN;
  1009. goto out_rjt;
  1010. }
  1011. fp = fc_frame_alloc(lport, sizeof(*rsp));
  1012. if (!fp) {
  1013. rjt_data.reason = ELS_RJT_UNAB;
  1014. rjt_data.explan = ELS_EXPL_INSUF_RES;
  1015. goto out_rjt;
  1016. }
  1017. rsp = fc_frame_payload_get(fp, sizeof(*rsp));
  1018. memset(rsp, 0, sizeof(*rsp));
  1019. rsp->rls_cmd = ELS_LS_ACC;
  1020. lesb = &rsp->rls_lesb;
  1021. if (lport->tt.get_lesb) {
  1022. /* get LESB from LLD if it supports it */
  1023. lport->tt.get_lesb(lport, lesb);
  1024. } else {
  1025. fc_get_host_stats(lport->host);
  1026. hst = &lport->host_stats;
  1027. lesb->lesb_link_fail = htonl(hst->link_failure_count);
  1028. lesb->lesb_sync_loss = htonl(hst->loss_of_sync_count);
  1029. lesb->lesb_sig_loss = htonl(hst->loss_of_signal_count);
  1030. lesb->lesb_prim_err = htonl(hst->prim_seq_protocol_err_count);
  1031. lesb->lesb_inv_word = htonl(hst->invalid_tx_word_count);
  1032. lesb->lesb_inv_crc = htonl(hst->invalid_crc_count);
  1033. }
  1034. sp = lport->tt.seq_start_next(sp);
  1035. f_ctl = FC_FC_EX_CTX | FC_FC_LAST_SEQ | FC_FC_END_SEQ;
  1036. fc_fill_fc_hdr(fp, FC_RCTL_ELS_REP, ep->did, ep->sid,
  1037. FC_TYPE_ELS, f_ctl, 0);
  1038. lport->tt.seq_send(lport, sp, fp);
  1039. goto out;
  1040. out_rjt:
  1041. rjt_data.fp = NULL;
  1042. lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
  1043. out:
  1044. fc_frame_free(rx_fp);
  1045. }
  1046. /**
  1047. * fc_rport_recv_els_req() - Handler for validated ELS requests
  1048. * @lport: The local port that received the ELS request
  1049. * @sp: The sequence that the ELS request was on
  1050. * @fp: The ELS request frame
  1051. *
  1052. * Handle incoming ELS requests that require port login.
  1053. * The ELS opcode has already been validated by the caller.
  1054. *
  1055. * Locking Note: Called with the lport lock held.
  1056. */
  1057. static void fc_rport_recv_els_req(struct fc_lport *lport,
  1058. struct fc_seq *sp, struct fc_frame *fp)
  1059. {
  1060. struct fc_rport_priv *rdata;
  1061. struct fc_frame_header *fh;
  1062. struct fc_seq_els_data els_data;
  1063. els_data.fp = NULL;
  1064. els_data.reason = ELS_RJT_UNAB;
  1065. els_data.explan = ELS_EXPL_PLOGI_REQD;
  1066. fh = fc_frame_header_get(fp);
  1067. mutex_lock(&lport->disc.disc_mutex);
  1068. rdata = lport->tt.rport_lookup(lport, ntoh24(fh->fh_s_id));
  1069. if (!rdata) {
  1070. mutex_unlock(&lport->disc.disc_mutex);
  1071. goto reject;
  1072. }
  1073. mutex_lock(&rdata->rp_mutex);
  1074. mutex_unlock(&lport->disc.disc_mutex);
  1075. switch (rdata->rp_state) {
  1076. case RPORT_ST_PRLI:
  1077. case RPORT_ST_RTV:
  1078. case RPORT_ST_READY:
  1079. case RPORT_ST_ADISC:
  1080. break;
  1081. default:
  1082. mutex_unlock(&rdata->rp_mutex);
  1083. goto reject;
  1084. }
  1085. switch (fc_frame_payload_op(fp)) {
  1086. case ELS_PRLI:
  1087. fc_rport_recv_prli_req(rdata, sp, fp);
  1088. break;
  1089. case ELS_PRLO:
  1090. fc_rport_recv_prlo_req(rdata, sp, fp);
  1091. break;
  1092. case ELS_ADISC:
  1093. fc_rport_recv_adisc_req(rdata, sp, fp);
  1094. break;
  1095. case ELS_RRQ:
  1096. els_data.fp = fp;
  1097. lport->tt.seq_els_rsp_send(sp, ELS_RRQ, &els_data);
  1098. break;
  1099. case ELS_REC:
  1100. els_data.fp = fp;
  1101. lport->tt.seq_els_rsp_send(sp, ELS_REC, &els_data);
  1102. break;
  1103. case ELS_RLS:
  1104. fc_rport_recv_rls_req(rdata, sp, fp);
  1105. break;
  1106. default:
  1107. fc_frame_free(fp); /* can't happen */
  1108. break;
  1109. }
  1110. mutex_unlock(&rdata->rp_mutex);
  1111. return;
  1112. reject:
  1113. lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &els_data);
  1114. fc_frame_free(fp);
  1115. }
  1116. /**
  1117. * fc_rport_recv_req() - Handler for requests
  1118. * @sp: The sequence the request was on
  1119. * @fp: The request frame
  1120. * @lport: The local port that received the request
  1121. *
  1122. * Locking Note: Called with the lport lock held.
  1123. */
  1124. void fc_rport_recv_req(struct fc_seq *sp, struct fc_frame *fp,
  1125. struct fc_lport *lport)
  1126. {
  1127. struct fc_seq_els_data els_data;
  1128. /*
  1129. * Handle PLOGI and LOGO requests separately, since they
  1130. * don't require prior login.
  1131. * Check for unsupported opcodes first and reject them.
  1132. * For some ops, it would be incorrect to reject with "PLOGI required".
  1133. */
  1134. switch (fc_frame_payload_op(fp)) {
  1135. case ELS_PLOGI:
  1136. fc_rport_recv_plogi_req(lport, sp, fp);
  1137. break;
  1138. case ELS_LOGO:
  1139. fc_rport_recv_logo_req(lport, sp, fp);
  1140. break;
  1141. case ELS_PRLI:
  1142. case ELS_PRLO:
  1143. case ELS_ADISC:
  1144. case ELS_RRQ:
  1145. case ELS_REC:
  1146. case ELS_RLS:
  1147. fc_rport_recv_els_req(lport, sp, fp);
  1148. break;
  1149. default:
  1150. fc_frame_free(fp);
  1151. els_data.fp = NULL;
  1152. els_data.reason = ELS_RJT_UNSUP;
  1153. els_data.explan = ELS_EXPL_NONE;
  1154. lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &els_data);
  1155. break;
  1156. }
  1157. }
  1158. /**
  1159. * fc_rport_recv_plogi_req() - Handler for Port Login (PLOGI) requests
  1160. * @lport: The local port that received the PLOGI request
  1161. * @sp: The sequence that the PLOGI request was on
  1162. * @rx_fp: The PLOGI request frame
  1163. *
  1164. * Locking Note: The rport lock is held before calling this function.
  1165. */
  1166. static void fc_rport_recv_plogi_req(struct fc_lport *lport,
  1167. struct fc_seq *sp, struct fc_frame *rx_fp)
  1168. {
  1169. struct fc_disc *disc;
  1170. struct fc_rport_priv *rdata;
  1171. struct fc_frame *fp = rx_fp;
  1172. struct fc_exch *ep;
  1173. struct fc_frame_header *fh;
  1174. struct fc_els_flogi *pl;
  1175. struct fc_seq_els_data rjt_data;
  1176. u32 sid, f_ctl;
  1177. rjt_data.fp = NULL;
  1178. fh = fc_frame_header_get(fp);
  1179. sid = ntoh24(fh->fh_s_id);
  1180. FC_RPORT_ID_DBG(lport, sid, "Received PLOGI request\n");
  1181. pl = fc_frame_payload_get(fp, sizeof(*pl));
  1182. if (!pl) {
  1183. FC_RPORT_ID_DBG(lport, sid, "Received PLOGI too short\n");
  1184. rjt_data.reason = ELS_RJT_PROT;
  1185. rjt_data.explan = ELS_EXPL_INV_LEN;
  1186. goto reject;
  1187. }
  1188. disc = &lport->disc;
  1189. mutex_lock(&disc->disc_mutex);
  1190. rdata = lport->tt.rport_create(lport, sid);
  1191. if (!rdata) {
  1192. mutex_unlock(&disc->disc_mutex);
  1193. rjt_data.reason = ELS_RJT_UNAB;
  1194. rjt_data.explan = ELS_EXPL_INSUF_RES;
  1195. goto reject;
  1196. }
  1197. mutex_lock(&rdata->rp_mutex);
  1198. mutex_unlock(&disc->disc_mutex);
  1199. rdata->ids.port_name = get_unaligned_be64(&pl->fl_wwpn);
  1200. rdata->ids.node_name = get_unaligned_be64(&pl->fl_wwnn);
  1201. /*
  1202. * If the rport was just created, possibly due to the incoming PLOGI,
  1203. * set the state appropriately and accept the PLOGI.
  1204. *
  1205. * If we had also sent a PLOGI, and if the received PLOGI is from a
  1206. * higher WWPN, we accept it, otherwise an LS_RJT is sent with reason
  1207. * "command already in progress".
  1208. *
  1209. * XXX TBD: If the session was ready before, the PLOGI should result in
  1210. * all outstanding exchanges being reset.
  1211. */
  1212. switch (rdata->rp_state) {
  1213. case RPORT_ST_INIT:
  1214. FC_RPORT_DBG(rdata, "Received PLOGI in INIT state\n");
  1215. break;
  1216. case RPORT_ST_PLOGI:
  1217. FC_RPORT_DBG(rdata, "Received PLOGI in PLOGI state\n");
  1218. if (rdata->ids.port_name < lport->wwpn) {
  1219. mutex_unlock(&rdata->rp_mutex);
  1220. rjt_data.reason = ELS_RJT_INPROG;
  1221. rjt_data.explan = ELS_EXPL_NONE;
  1222. goto reject;
  1223. }
  1224. break;
  1225. case RPORT_ST_PRLI:
  1226. case RPORT_ST_RTV:
  1227. case RPORT_ST_READY:
  1228. case RPORT_ST_ADISC:
  1229. FC_RPORT_DBG(rdata, "Received PLOGI in logged-in state %d "
  1230. "- ignored for now\n", rdata->rp_state);
  1231. /* XXX TBD - should reset */
  1232. break;
  1233. case RPORT_ST_DELETE:
  1234. case RPORT_ST_LOGO:
  1235. case RPORT_ST_RESTART:
  1236. FC_RPORT_DBG(rdata, "Received PLOGI in state %s - send busy\n",
  1237. fc_rport_state(rdata));
  1238. mutex_unlock(&rdata->rp_mutex);
  1239. rjt_data.reason = ELS_RJT_BUSY;
  1240. rjt_data.explan = ELS_EXPL_NONE;
  1241. goto reject;
  1242. }
  1243. /*
  1244. * Get session payload size from incoming PLOGI.
  1245. */
  1246. rdata->maxframe_size = fc_plogi_get_maxframe(pl, lport->mfs);
  1247. fc_frame_free(rx_fp);
  1248. /*
  1249. * Send LS_ACC. If this fails, the originator should retry.
  1250. */
  1251. sp = lport->tt.seq_start_next(sp);
  1252. if (!sp)
  1253. goto out;
  1254. fp = fc_frame_alloc(lport, sizeof(*pl));
  1255. if (!fp)
  1256. goto out;
  1257. fc_plogi_fill(lport, fp, ELS_LS_ACC);
  1258. f_ctl = FC_FC_EX_CTX | FC_FC_LAST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT;
  1259. ep = fc_seq_exch(sp);
  1260. fc_fill_fc_hdr(fp, FC_RCTL_ELS_REP, ep->did, ep->sid,
  1261. FC_TYPE_ELS, f_ctl, 0);
  1262. lport->tt.seq_send(lport, sp, fp);
  1263. fc_rport_enter_prli(rdata);
  1264. out:
  1265. mutex_unlock(&rdata->rp_mutex);
  1266. return;
  1267. reject:
  1268. lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
  1269. fc_frame_free(fp);
  1270. }
  1271. /**
  1272. * fc_rport_recv_prli_req() - Handler for process login (PRLI) requests
  1273. * @rdata: The remote port that sent the PRLI request
  1274. * @sp: The sequence that the PRLI was on
  1275. * @rx_fp: The PRLI request frame
  1276. *
  1277. * Locking Note: The rport lock is exected to be held before calling
  1278. * this function.
  1279. */
  1280. static void fc_rport_recv_prli_req(struct fc_rport_priv *rdata,
  1281. struct fc_seq *sp, struct fc_frame *rx_fp)
  1282. {
  1283. struct fc_lport *lport = rdata->local_port;
  1284. struct fc_exch *ep;
  1285. struct fc_frame *fp;
  1286. struct fc_frame_header *fh;
  1287. struct {
  1288. struct fc_els_prli prli;
  1289. struct fc_els_spp spp;
  1290. } *pp;
  1291. struct fc_els_spp *rspp; /* request service param page */
  1292. struct fc_els_spp *spp; /* response spp */
  1293. unsigned int len;
  1294. unsigned int plen;
  1295. enum fc_els_spp_resp resp;
  1296. struct fc_seq_els_data rjt_data;
  1297. u32 f_ctl;
  1298. u32 fcp_parm;
  1299. u32 roles = FC_RPORT_ROLE_UNKNOWN;
  1300. rjt_data.fp = NULL;
  1301. fh = fc_frame_header_get(rx_fp);
  1302. FC_RPORT_DBG(rdata, "Received PRLI request while in state %s\n",
  1303. fc_rport_state(rdata));
  1304. len = fr_len(rx_fp) - sizeof(*fh);
  1305. pp = fc_frame_payload_get(rx_fp, sizeof(*pp));
  1306. if (!pp)
  1307. goto reject_len;
  1308. plen = ntohs(pp->prli.prli_len);
  1309. if ((plen % 4) != 0 || plen > len || plen < 16)
  1310. goto reject_len;
  1311. if (plen < len)
  1312. len = plen;
  1313. plen = pp->prli.prli_spp_len;
  1314. if ((plen % 4) != 0 || plen < sizeof(*spp) ||
  1315. plen > len || len < sizeof(*pp) || plen < 12)
  1316. goto reject_len;
  1317. rspp = &pp->spp;
  1318. fp = fc_frame_alloc(lport, len);
  1319. if (!fp) {
  1320. rjt_data.reason = ELS_RJT_UNAB;
  1321. rjt_data.explan = ELS_EXPL_INSUF_RES;
  1322. goto reject;
  1323. }
  1324. sp = lport->tt.seq_start_next(sp);
  1325. WARN_ON(!sp);
  1326. pp = fc_frame_payload_get(fp, len);
  1327. WARN_ON(!pp);
  1328. memset(pp, 0, len);
  1329. pp->prli.prli_cmd = ELS_LS_ACC;
  1330. pp->prli.prli_spp_len = plen;
  1331. pp->prli.prli_len = htons(len);
  1332. len -= sizeof(struct fc_els_prli);
  1333. /* reinitialize remote port roles */
  1334. rdata->ids.roles = FC_RPORT_ROLE_UNKNOWN;
  1335. /*
  1336. * Go through all the service parameter pages and build
  1337. * response. If plen indicates longer SPP than standard,
  1338. * use that. The entire response has been pre-cleared above.
  1339. */
  1340. spp = &pp->spp;
  1341. while (len >= plen) {
  1342. spp->spp_type = rspp->spp_type;
  1343. spp->spp_type_ext = rspp->spp_type_ext;
  1344. spp->spp_flags = rspp->spp_flags & FC_SPP_EST_IMG_PAIR;
  1345. resp = FC_SPP_RESP_ACK;
  1346. switch (rspp->spp_type) {
  1347. case 0: /* common to all FC-4 types */
  1348. break;
  1349. case FC_TYPE_FCP:
  1350. fcp_parm = ntohl(rspp->spp_params);
  1351. if (fcp_parm & FCP_SPPF_RETRY)
  1352. rdata->flags |= FC_RP_FLAGS_RETRY;
  1353. rdata->supported_classes = FC_COS_CLASS3;
  1354. if (fcp_parm & FCP_SPPF_INIT_FCN)
  1355. roles |= FC_RPORT_ROLE_FCP_INITIATOR;
  1356. if (fcp_parm & FCP_SPPF_TARG_FCN)
  1357. roles |= FC_RPORT_ROLE_FCP_TARGET;
  1358. rdata->ids.roles = roles;
  1359. spp->spp_params = htonl(lport->service_params);
  1360. break;
  1361. default:
  1362. resp = FC_SPP_RESP_INVL;
  1363. break;
  1364. }
  1365. spp->spp_flags |= resp;
  1366. len -= plen;
  1367. rspp = (struct fc_els_spp *)((char *)rspp + plen);
  1368. spp = (struct fc_els_spp *)((char *)spp + plen);
  1369. }
  1370. /*
  1371. * Send LS_ACC. If this fails, the originator should retry.
  1372. */
  1373. f_ctl = FC_FC_EX_CTX | FC_FC_LAST_SEQ;
  1374. f_ctl |= FC_FC_END_SEQ | FC_FC_SEQ_INIT;
  1375. ep = fc_seq_exch(sp);
  1376. fc_fill_fc_hdr(fp, FC_RCTL_ELS_REP, ep->did, ep->sid,
  1377. FC_TYPE_ELS, f_ctl, 0);
  1378. lport->tt.seq_send(lport, sp, fp);
  1379. switch (rdata->rp_state) {
  1380. case RPORT_ST_PRLI:
  1381. fc_rport_enter_ready(rdata);
  1382. break;
  1383. default:
  1384. break;
  1385. }
  1386. goto drop;
  1387. reject_len:
  1388. rjt_data.reason = ELS_RJT_PROT;
  1389. rjt_data.explan = ELS_EXPL_INV_LEN;
  1390. reject:
  1391. lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
  1392. drop:
  1393. fc_frame_free(rx_fp);
  1394. }
  1395. /**
  1396. * fc_rport_recv_prlo_req() - Handler for process logout (PRLO) requests
  1397. * @rdata: The remote port that sent the PRLO request
  1398. * @sp: The sequence that the PRLO was on
  1399. * @fp: The PRLO request frame
  1400. *
  1401. * Locking Note: The rport lock is exected to be held before calling
  1402. * this function.
  1403. */
  1404. static void fc_rport_recv_prlo_req(struct fc_rport_priv *rdata,
  1405. struct fc_seq *sp,
  1406. struct fc_frame *fp)
  1407. {
  1408. struct fc_lport *lport = rdata->local_port;
  1409. struct fc_frame_header *fh;
  1410. struct fc_seq_els_data rjt_data;
  1411. fh = fc_frame_header_get(fp);
  1412. FC_RPORT_DBG(rdata, "Received PRLO request while in state %s\n",
  1413. fc_rport_state(rdata));
  1414. rjt_data.fp = NULL;
  1415. rjt_data.reason = ELS_RJT_UNAB;
  1416. rjt_data.explan = ELS_EXPL_NONE;
  1417. lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
  1418. fc_frame_free(fp);
  1419. }
  1420. /**
  1421. * fc_rport_recv_logo_req() - Handler for logout (LOGO) requests
  1422. * @lport: The local port that received the LOGO request
  1423. * @sp: The sequence that the LOGO request was on
  1424. * @fp: The LOGO request frame
  1425. *
  1426. * Locking Note: The rport lock is exected to be held before calling
  1427. * this function.
  1428. */
  1429. static void fc_rport_recv_logo_req(struct fc_lport *lport,
  1430. struct fc_seq *sp,
  1431. struct fc_frame *fp)
  1432. {
  1433. struct fc_frame_header *fh;
  1434. struct fc_rport_priv *rdata;
  1435. u32 sid;
  1436. lport->tt.seq_els_rsp_send(sp, ELS_LS_ACC, NULL);
  1437. fh = fc_frame_header_get(fp);
  1438. sid = ntoh24(fh->fh_s_id);
  1439. mutex_lock(&lport->disc.disc_mutex);
  1440. rdata = lport->tt.rport_lookup(lport, sid);
  1441. if (rdata) {
  1442. mutex_lock(&rdata->rp_mutex);
  1443. FC_RPORT_DBG(rdata, "Received LOGO request while in state %s\n",
  1444. fc_rport_state(rdata));
  1445. fc_rport_enter_delete(rdata, RPORT_EV_LOGO);
  1446. /*
  1447. * If the remote port was created due to discovery, set state
  1448. * to log back in. It may have seen a stale RSCN about us.
  1449. */
  1450. if (rdata->disc_id)
  1451. fc_rport_state_enter(rdata, RPORT_ST_RESTART);
  1452. mutex_unlock(&rdata->rp_mutex);
  1453. } else
  1454. FC_RPORT_ID_DBG(lport, sid,
  1455. "Received LOGO from non-logged-in port\n");
  1456. mutex_unlock(&lport->disc.disc_mutex);
  1457. fc_frame_free(fp);
  1458. }
  1459. /**
  1460. * fc_rport_flush_queue() - Flush the rport_event_queue
  1461. */
  1462. static void fc_rport_flush_queue(void)
  1463. {
  1464. flush_workqueue(rport_event_queue);
  1465. }
  1466. /**
  1467. * fc_rport_init() - Initialize the remote port layer for a local port
  1468. * @lport: The local port to initialize the remote port layer for
  1469. */
  1470. int fc_rport_init(struct fc_lport *lport)
  1471. {
  1472. if (!lport->tt.rport_lookup)
  1473. lport->tt.rport_lookup = fc_rport_lookup;
  1474. if (!lport->tt.rport_create)
  1475. lport->tt.rport_create = fc_rport_create;
  1476. if (!lport->tt.rport_login)
  1477. lport->tt.rport_login = fc_rport_login;
  1478. if (!lport->tt.rport_logoff)
  1479. lport->tt.rport_logoff = fc_rport_logoff;
  1480. if (!lport->tt.rport_recv_req)
  1481. lport->tt.rport_recv_req = fc_rport_recv_req;
  1482. if (!lport->tt.rport_flush_queue)
  1483. lport->tt.rport_flush_queue = fc_rport_flush_queue;
  1484. if (!lport->tt.rport_destroy)
  1485. lport->tt.rport_destroy = fc_rport_destroy;
  1486. return 0;
  1487. }
  1488. EXPORT_SYMBOL(fc_rport_init);
  1489. /**
  1490. * fc_setup_rport() - Initialize the rport_event_queue
  1491. */
  1492. int fc_setup_rport()
  1493. {
  1494. rport_event_queue = create_singlethread_workqueue("fc_rport_eq");
  1495. if (!rport_event_queue)
  1496. return -ENOMEM;
  1497. return 0;
  1498. }
  1499. /**
  1500. * fc_destroy_rport() - Destroy the rport_event_queue
  1501. */
  1502. void fc_destroy_rport()
  1503. {
  1504. destroy_workqueue(rport_event_queue);
  1505. }
  1506. /**
  1507. * fc_rport_terminate_io() - Stop all outstanding I/O on a remote port
  1508. * @rport: The remote port whose I/O should be terminated
  1509. */
  1510. void fc_rport_terminate_io(struct fc_rport *rport)
  1511. {
  1512. struct fc_rport_libfc_priv *rpriv = rport->dd_data;
  1513. struct fc_lport *lport = rpriv->local_port;
  1514. lport->tt.exch_mgr_reset(lport, 0, rport->port_id);
  1515. lport->tt.exch_mgr_reset(lport, rport->port_id, 0);
  1516. }
  1517. EXPORT_SYMBOL(fc_rport_terminate_io);