fc_rport.c 42 KB

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